myopt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * myopt.c - my very own option parsing
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright (C) 2000,2002 Wichert Akkerman <wichert@deephackmode.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this file; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27. #include <myopt.h>
  28. #include <dpkg.h>
  29. void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) {
  30. FILE* file;
  31. char linebuf[MAXDIVERTFILENAME];
  32. file= fopen(fn, "r");
  33. if (!file) {
  34. if (errno==ENOENT)
  35. return;
  36. ohshite(_("failed to open configuration file `%.255s' for reading"), fn);
  37. }
  38. while (fgets(linebuf, sizeof(linebuf), file)) {
  39. char* opt;
  40. const struct cmdinfo *cip;
  41. int l;
  42. if ((linebuf[0]=='#') || (linebuf[0]=='\n') || (linebuf[0]==0)) continue;
  43. l=strlen(linebuf);
  44. if (linebuf[l-1]=='\n') linebuf[l-1]=0;
  45. for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ;
  46. if (*opt==0)
  47. opt=NULL;
  48. else {
  49. *opt++=0;
  50. if (*opt=='=') opt++;
  51. while (isspace(*opt)) opt++;
  52. }
  53. for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
  54. int l;
  55. if (!cip->olong) continue;
  56. if (!strcmp(cip->olong,linebuf)) break;
  57. l=strlen(cip->olong);
  58. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  59. !opt && !strncmp(linebuf,cip->olong,l)) {
  60. opt=linebuf+l+1;
  61. break;
  62. }
  63. }
  64. if (!cip->olong) ohshite(_("configuration error: unknown option %s"), linebuf);
  65. if (cip->takesvalue) {
  66. if (!opt) ohshite(_("configuration error: %s needs a value"), linebuf);
  67. if (cip->call) cip->call(cip,opt);
  68. else *cip->sassignto= opt;
  69. } else {
  70. if (opt) ohshite(_("configuration error: %s does not take a value"), linebuf);
  71. if (cip->call) cip->call(cip,NULL);
  72. else *cip->iassignto= cip->arg;
  73. }
  74. }
  75. if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn);
  76. if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
  77. }
  78. void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
  79. char *home, *file;
  80. int l1, l2;
  81. l1 = strlen(CONFIGDIR "/.cfg") + strlen(prog);
  82. file = malloc(l1 + 1);
  83. if (file==NULL) ohshite(_("Error allocating memory for cfgfilename"));
  84. sprintf(file, CONFIGDIR "/%s.cfg", prog);
  85. myfileopt(file, cmdinfos);
  86. if ((home = getenv("HOME")) != NULL) {
  87. l2 = strlen(home) + 1 + strlen("/.cfg") + strlen(prog);
  88. if (l2 > l1) {
  89. free(file);
  90. file = malloc(l2 + 1);
  91. if (file==NULL) ohshite(_("Error allocating memory for cfgfilename"));
  92. }
  93. sprintf(file, "%s/.%s.cfg", home, prog);
  94. myfileopt(file, cmdinfos);
  95. }
  96. free(file);
  97. }
  98. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  99. const struct cmdinfo *cip;
  100. const char *p, *value;
  101. int l;
  102. ++(*argvp);
  103. while ((p= **argvp) && *p == '-') {
  104. ++(*argvp);
  105. if (!strcmp(p,"--")) break;
  106. if (*++p == '-') {
  107. ++p; value=NULL;
  108. for (cip= cmdinfos;
  109. cip->olong || cip->oshort;
  110. cip++) {
  111. if (!cip->olong) continue;
  112. if (!strcmp(p,cip->olong)) break;
  113. l= strlen(cip->olong);
  114. if (!strncmp(p,cip->olong,l) &&
  115. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  116. }
  117. if (!cip->olong) badusage(_("unknown option --%s"),p);
  118. if (cip->takesvalue) {
  119. if (!value) {
  120. value= *(*argvp)++;
  121. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  122. }
  123. if (cip->call) cip->call(cip,value);
  124. else *cip->sassignto= value;
  125. } else {
  126. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  127. if (cip->call) cip->call(cip,NULL);
  128. else *cip->iassignto= cip->arg;
  129. }
  130. } else {
  131. while (*p) {
  132. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  133. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  134. p++;
  135. if (cip->takesvalue) {
  136. if (!*p) {
  137. value= *(*argvp)++;
  138. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  139. } else {
  140. value= p; p="";
  141. if (*value == '=') value++;
  142. }
  143. if (cip->call) cip->call(cip,value);
  144. else *cip->sassignto= value;
  145. } else {
  146. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  147. if (cip->call) cip->call(cip,NULL);
  148. else *cip->iassignto= cip->arg;
  149. }
  150. }
  151. }
  152. }
  153. }