myopt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * myopt.c - my very own option parsing
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 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 <compat.h>
  24. #include <dpkg-i18n.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <ctype.h>
  28. #include <stdlib.h>
  29. #include <myopt.h>
  30. #include <dpkg.h>
  31. void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) {
  32. FILE* file;
  33. char linebuf[MAXDIVERTFILENAME];
  34. file= fopen(fn, "r");
  35. if (!file) {
  36. if (errno==ENOENT)
  37. return;
  38. warningf(_("failed to open configuration file `%.255s' for reading"),fn);
  39. return;
  40. }
  41. while (fgets(linebuf, sizeof(linebuf), file)) {
  42. char* opt;
  43. const struct cmdinfo *cip;
  44. int l;
  45. if ((linebuf[0]=='#') || (linebuf[0]=='\n') || (linebuf[0]==0)) continue;
  46. l=strlen(linebuf);
  47. if (linebuf[l-1]=='\n') linebuf[l-1]=0;
  48. for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ;
  49. if (*opt==0)
  50. opt=NULL;
  51. else {
  52. *opt++=0;
  53. if (*opt=='=') opt++;
  54. while (isspace(*opt)) opt++;
  55. }
  56. for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
  57. int l;
  58. if (!cip->olong) continue;
  59. if (!strcmp(cip->olong,linebuf)) break;
  60. l=strlen(cip->olong);
  61. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  62. !opt && !strncmp(linebuf,cip->olong,l)) {
  63. opt=linebuf+l+1;
  64. break;
  65. }
  66. }
  67. if (!cip->olong) ohshite(_("configuration error: unknown option %s"), linebuf);
  68. if (cip->takesvalue) {
  69. if (!opt) ohshite(_("configuration error: %s needs a value"), linebuf);
  70. if (cip->call) cip->call(cip,opt);
  71. else
  72. *cip->sassignto = m_strdup(opt);
  73. } else {
  74. if (opt) ohshite(_("configuration error: %s does not take a value"), linebuf);
  75. if (cip->call) cip->call(cip,NULL);
  76. else *cip->iassignto= cip->arg;
  77. }
  78. }
  79. if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn);
  80. if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
  81. }
  82. void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
  83. char *home, *file;
  84. int l1, l2;
  85. l1 = strlen(CONFIGDIR "/.cfg") + strlen(prog);
  86. file = m_malloc(l1 + 1);
  87. sprintf(file, CONFIGDIR "/%s.cfg", prog);
  88. myfileopt(file, cmdinfos);
  89. if ((home = getenv("HOME")) != NULL) {
  90. l2 = strlen(home) + 1 + strlen("/.cfg") + strlen(prog);
  91. if (l2 > l1) {
  92. free(file);
  93. file = m_malloc(l2 + 1);
  94. }
  95. sprintf(file, "%s/.%s.cfg", home, prog);
  96. myfileopt(file, cmdinfos);
  97. }
  98. free(file);
  99. }
  100. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  101. const struct cmdinfo *cip;
  102. const char *p, *value;
  103. int l;
  104. ++(*argvp);
  105. while ((p= **argvp) && *p == '-') {
  106. ++(*argvp);
  107. if (!strcmp(p,"--")) break;
  108. if (*++p == '-') {
  109. ++p; value=NULL;
  110. for (cip= cmdinfos;
  111. cip->olong || cip->oshort;
  112. cip++) {
  113. if (!cip->olong) continue;
  114. if (!strcmp(p,cip->olong)) break;
  115. l= strlen(cip->olong);
  116. if (!strncmp(p,cip->olong,l) &&
  117. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  118. }
  119. if (!cip->olong) badusage(_("unknown option --%s"),p);
  120. if (cip->takesvalue) {
  121. if (!value) {
  122. value= *(*argvp)++;
  123. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  124. }
  125. if (cip->call) cip->call(cip,value);
  126. else *cip->sassignto= value;
  127. } else {
  128. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  129. if (cip->call) cip->call(cip,NULL);
  130. else *cip->iassignto= cip->arg;
  131. }
  132. } else {
  133. while (*p) {
  134. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  135. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  136. p++;
  137. if (cip->takesvalue) {
  138. if (!*p) {
  139. value= *(*argvp)++;
  140. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  141. } else {
  142. value= p; p="";
  143. if (*value == '=') value++;
  144. }
  145. if (cip->call) cip->call(cip,value);
  146. else *cip->sassignto= value;
  147. } else {
  148. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  149. if (cip->call) cip->call(cip,NULL);
  150. else *cip->iassignto= cip->arg;
  151. }
  152. }
  153. }
  154. }
  155. }