myopt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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)) continue;
  58. l=strlen(linebuf);
  59. if (linebuf[l-1]=='\n') linebuf[l-1]=0;
  60. for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ;
  61. if (*opt==0)
  62. opt=NULL;
  63. else {
  64. *opt++=0;
  65. if (*opt=='=') opt++;
  66. while (isspace(*opt)) opt++;
  67. }
  68. for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
  69. int l;
  70. if (!cip->olong) continue;
  71. if (!strcmp(cip->olong,linebuf)) break;
  72. l=strlen(cip->olong);
  73. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  74. !opt && !strncmp(linebuf,cip->olong,l)) {
  75. opt=linebuf+l+1;
  76. break;
  77. }
  78. }
  79. if (!cip->olong) ohshite(_("configuration error: unknown option %s"), linebuf);
  80. if (cip->takesvalue) {
  81. if (!opt) ohshite(_("configuration error: %s needs a value"), linebuf);
  82. if (cip->call) cip->call(cip,opt);
  83. else
  84. *cip->sassignto = m_strdup(opt);
  85. } else {
  86. if (opt) ohshite(_("configuration error: %s does not take a value"), linebuf);
  87. if (cip->call) cip->call(cip,NULL);
  88. else *cip->iassignto= cip->arg;
  89. }
  90. }
  91. if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn);
  92. if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
  93. }
  94. void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
  95. char *home, *file;
  96. int l1, l2;
  97. l1 = strlen(CONFIGDIR "/.cfg") + strlen(prog);
  98. file = m_malloc(l1 + 1);
  99. sprintf(file, CONFIGDIR "/%s.cfg", prog);
  100. myfileopt(file, cmdinfos);
  101. if ((home = getenv("HOME")) != NULL) {
  102. l2 = strlen(home) + 1 + strlen("/.cfg") + strlen(prog);
  103. if (l2 > l1) {
  104. free(file);
  105. file = m_malloc(l2 + 1);
  106. }
  107. sprintf(file, "%s/.%s.cfg", home, prog);
  108. myfileopt(file, cmdinfos);
  109. }
  110. free(file);
  111. }
  112. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  113. const struct cmdinfo *cip;
  114. const char *p, *value;
  115. int l;
  116. ++(*argvp);
  117. while ((p= **argvp) && *p == '-') {
  118. ++(*argvp);
  119. if (!strcmp(p,"--")) break;
  120. if (*++p == '-') {
  121. ++p; value=NULL;
  122. for (cip= cmdinfos;
  123. cip->olong || cip->oshort;
  124. cip++) {
  125. if (!cip->olong) continue;
  126. if (!strcmp(p,cip->olong)) break;
  127. l= strlen(cip->olong);
  128. if (!strncmp(p,cip->olong,l) &&
  129. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  130. }
  131. if (!cip->olong) badusage(_("unknown option --%s"),p);
  132. if (cip->takesvalue) {
  133. if (!value) {
  134. value= *(*argvp)++;
  135. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  136. }
  137. if (cip->call) cip->call(cip,value);
  138. else *cip->sassignto= value;
  139. } else {
  140. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  141. if (cip->call) cip->call(cip,NULL);
  142. else *cip->iassignto= cip->arg;
  143. }
  144. } else {
  145. while (*p) {
  146. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  147. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  148. p++;
  149. if (cip->takesvalue) {
  150. if (!*p) {
  151. value= *(*argvp)++;
  152. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  153. } else {
  154. value= p; p="";
  155. if (*value == '=') value++;
  156. }
  157. if (cip->call) cip->call(cip,value);
  158. else *cip->sassignto= value;
  159. } else {
  160. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  161. if (cip->call) cip->call(cip,NULL);
  162. else *cip->iassignto= cip->arg;
  163. }
  164. }
  165. }
  166. }
  167. }