myopt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * myopt.c - my very own option parsing
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. * Copyright (C) 2000 Wichert Akkerman <wakkerma@debian.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 <string.h>
  23. #include <errno.h>
  24. #include <config.h>
  25. #include <myopt.h>
  26. #include <dpkg.h>
  27. void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) {
  28. FILE* file;
  29. char linebuf[MAXDIVERTFILENAME];
  30. file= fopen(fn, "r");
  31. if (!file) {
  32. if (errno==ENOENT)
  33. return;
  34. ohshite(_("failed to open configuration file `%.255s' for reading"), fn);
  35. }
  36. while (fgets(linebuf, sizeof(linebuf), file)) {
  37. char* opt;
  38. const struct cmdinfo *cip;
  39. if ((linebuf[0]=='#') || (linebuf[0]=='\n') || (linebuf[0]==0)) continue;
  40. linebuf[strlen(linebuf)-1]=0;
  41. for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ;
  42. if (*opt==0)
  43. opt=NULL;
  44. else {
  45. *opt++=0;
  46. if (*opt=='=') opt++;
  47. while (isspace(*opt)) opt++;
  48. }
  49. for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
  50. int l;
  51. if (!cip->olong) continue;
  52. if (!strcmp(cip->olong,linebuf)) break;
  53. l=strlen(cip->olong);
  54. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  55. !opt && !strncmp(linebuf,cip->olong,l)) {
  56. opt=linebuf+l+1;
  57. break;
  58. }
  59. }
  60. if (!cip->olong) ohshite(_("configuration error: unknown option %s"), linebuf);
  61. if (cip->takesvalue) {
  62. if (!opt) ohshite(_("configuration error: %s needs a value"), linebuf);
  63. if (cip->call) cip->call(cip,opt);
  64. else *cip->sassignto= opt;
  65. } else {
  66. if (opt) ohshite(_("configuration error: %s does not take a value"), linebuf);
  67. if (cip->call) cip->call(cip,0);
  68. else *cip->iassignto= cip->arg;
  69. }
  70. }
  71. if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn);
  72. if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
  73. }
  74. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  75. const struct cmdinfo *cip;
  76. const char *p, *value;
  77. int l;
  78. ++(*argvp);
  79. while ((p= **argvp) && *p == '-') {
  80. ++(*argvp);
  81. if (!strcmp(p,"--")) break;
  82. if (*++p == '-') {
  83. ++p; value=0;
  84. for (cip= cmdinfos;
  85. cip->olong || cip->oshort;
  86. cip++) {
  87. if (!cip->olong) continue;
  88. if (!strcmp(p,cip->olong)) break;
  89. l= strlen(cip->olong);
  90. if (!strncmp(p,cip->olong,l) &&
  91. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  92. }
  93. if (!cip->olong) badusage(_("unknown option --%s"),p);
  94. if (cip->takesvalue) {
  95. if (!value) {
  96. value= *(*argvp)++;
  97. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  98. }
  99. if (cip->call) cip->call(cip,value);
  100. else *cip->sassignto= value;
  101. } else {
  102. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  103. if (cip->call) cip->call(cip,0);
  104. else *cip->iassignto= cip->arg;
  105. }
  106. } else {
  107. while (*p) {
  108. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  109. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  110. p++;
  111. if (cip->takesvalue) {
  112. if (!*p) {
  113. value= *(*argvp)++;
  114. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  115. } else {
  116. value= p; p="";
  117. if (*value == '=') value++;
  118. }
  119. if (cip->call) cip->call(cip,value);
  120. else *cip->sassignto= value;
  121. } else {
  122. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  123. if (cip->call) cip->call(cip,0);
  124. else *cip->iassignto= cip->arg;
  125. }
  126. }
  127. }
  128. }
  129. }