myopt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Wichert Akkerman <wakkerma@debian.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 <string.h>
  23. #include <errno.h>
  24. #include <ctype.h>
  25. #include <stdlib.h>
  26. #include <config.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. sprintf(file, CONFIGDIR "/%s.cfg", prog);
  84. myfileopt(file, cmdinfos);
  85. if ((home = getenv("HOME")) != NULL) {
  86. l2 = strlen(home) + strlen("/.cfg") + strlen(prog);
  87. if (l2 > l1) {
  88. free(file);
  89. file = malloc(l2 + 1);
  90. l1 = l2;
  91. }
  92. myfileopt(file, cmdinfos);
  93. }
  94. free(file);
  95. }
  96. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  97. const struct cmdinfo *cip;
  98. const char *p, *value;
  99. int l;
  100. ++(*argvp);
  101. while ((p= **argvp) && *p == '-') {
  102. ++(*argvp);
  103. if (!strcmp(p,"--")) break;
  104. if (*++p == '-') {
  105. ++p; value=NULL;
  106. for (cip= cmdinfos;
  107. cip->olong || cip->oshort;
  108. cip++) {
  109. if (!cip->olong) continue;
  110. if (!strcmp(p,cip->olong)) break;
  111. l= strlen(cip->olong);
  112. if (!strncmp(p,cip->olong,l) &&
  113. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  114. }
  115. if (!cip->olong) badusage(_("unknown option --%s"),p);
  116. if (cip->takesvalue) {
  117. if (!value) {
  118. value= *(*argvp)++;
  119. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  120. }
  121. if (cip->call) cip->call(cip,value);
  122. else *cip->sassignto= value;
  123. } else {
  124. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  125. if (cip->call) cip->call(cip,NULL);
  126. else *cip->iassignto= cip->arg;
  127. }
  128. } else {
  129. while (*p) {
  130. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  131. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  132. p++;
  133. if (cip->takesvalue) {
  134. if (!*p) {
  135. value= *(*argvp)++;
  136. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  137. } else {
  138. value= p; p="";
  139. if (*value == '=') value++;
  140. }
  141. if (cip->call) cip->call(cip,value);
  142. else *cip->sassignto= value;
  143. } else {
  144. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  145. if (cip->call) cip->call(cip,NULL);
  146. else *cip->iassignto= cip->arg;
  147. }
  148. }
  149. }
  150. }
  151. }