myopt.c 5.1 KB

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