myopt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <stdarg.h>
  29. #include <stdlib.h>
  30. #include <myopt.h>
  31. #include <dpkg.h>
  32. void
  33. badusage(const char *fmt, ...)
  34. {
  35. char buf[1024];
  36. va_list al;
  37. va_start(al, fmt);
  38. vsnprintf(buf, sizeof(buf), fmt, al);
  39. va_end(al);
  40. ohshit("%s\n\n%s", buf, gettext(printforhelp));
  41. }
  42. void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) {
  43. FILE* file;
  44. char linebuf[MAX_CONFIG_LINE];
  45. file= fopen(fn, "r");
  46. if (!file) {
  47. if (errno==ENOENT)
  48. return;
  49. warning(_("failed to open configuration file '%.255s' for reading: %s"),
  50. fn, strerror(errno));
  51. return;
  52. }
  53. while (fgets(linebuf, sizeof(linebuf), file)) {
  54. char* opt;
  55. const struct cmdinfo *cip;
  56. int l;
  57. if ((linebuf[0] == '#') || (linebuf[0] == '\n') || (linebuf[0] == '\0'))
  58. continue;
  59. l=strlen(linebuf);
  60. if (linebuf[l - 1] == '\n')
  61. linebuf[l - 1] = '\0';
  62. for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ;
  63. if (*opt == '\0')
  64. opt=NULL;
  65. else {
  66. *opt++ = '\0';
  67. if (*opt=='=') opt++;
  68. while (isspace(*opt)) opt++;
  69. }
  70. for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
  71. int l;
  72. if (!cip->olong) continue;
  73. if (!strcmp(cip->olong,linebuf)) break;
  74. l=strlen(cip->olong);
  75. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  76. !opt && !strncmp(linebuf,cip->olong,l)) {
  77. opt=linebuf+l+1;
  78. break;
  79. }
  80. }
  81. if (!cip->olong) ohshite(_("configuration error: unknown option %s"), linebuf);
  82. if (cip->takesvalue) {
  83. if (!opt) ohshite(_("configuration error: %s needs a value"), linebuf);
  84. if (cip->call) cip->call(cip,opt);
  85. else
  86. *cip->sassignto = m_strdup(opt);
  87. } else {
  88. if (opt) ohshite(_("configuration error: %s does not take a value"), linebuf);
  89. if (cip->call) cip->call(cip,NULL);
  90. else *cip->iassignto= cip->arg;
  91. }
  92. }
  93. if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn);
  94. if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
  95. }
  96. void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
  97. char *home, *file;
  98. int l1, l2;
  99. l1 = strlen(CONFIGDIR "/.cfg") + strlen(prog);
  100. file = m_malloc(l1 + 1);
  101. sprintf(file, CONFIGDIR "/%s.cfg", prog);
  102. myfileopt(file, cmdinfos);
  103. if ((home = getenv("HOME")) != NULL) {
  104. l2 = strlen(home) + 1 + strlen("/.cfg") + strlen(prog);
  105. if (l2 > l1) {
  106. free(file);
  107. file = m_malloc(l2 + 1);
  108. }
  109. sprintf(file, "%s/.%s.cfg", home, prog);
  110. myfileopt(file, cmdinfos);
  111. }
  112. free(file);
  113. }
  114. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  115. const struct cmdinfo *cip;
  116. const char *p, *value;
  117. int l;
  118. ++(*argvp);
  119. while ((p= **argvp) && *p == '-') {
  120. ++(*argvp);
  121. if (!strcmp(p,"--")) break;
  122. if (*++p == '-') {
  123. ++p; value=NULL;
  124. for (cip= cmdinfos;
  125. cip->olong || cip->oshort;
  126. cip++) {
  127. if (!cip->olong) continue;
  128. if (!strcmp(p,cip->olong)) break;
  129. l= strlen(cip->olong);
  130. if (!strncmp(p,cip->olong,l) &&
  131. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  132. }
  133. if (!cip->olong) badusage(_("unknown option --%s"),p);
  134. if (cip->takesvalue) {
  135. if (!value) {
  136. value= *(*argvp)++;
  137. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  138. }
  139. if (cip->call) cip->call(cip,value);
  140. else *cip->sassignto= value;
  141. } else {
  142. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  143. if (cip->call) cip->call(cip,NULL);
  144. else *cip->iassignto= cip->arg;
  145. }
  146. } else {
  147. while (*p) {
  148. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  149. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  150. p++;
  151. if (cip->takesvalue) {
  152. if (!*p) {
  153. value= *(*argvp)++;
  154. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  155. } else {
  156. value= p; p="";
  157. if (*value == '=') value++;
  158. }
  159. if (cip->call) cip->call(cip,value);
  160. else *cip->sassignto= value;
  161. } else {
  162. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  163. if (cip->call) cip->call(cip,NULL);
  164. else *cip->iassignto= cip->arg;
  165. }
  166. }
  167. }
  168. }
  169. }