myopt.c 5.1 KB

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