myopt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. int l;
  40. if ((linebuf[0]=='#') || (linebuf[0]=='\n') || (linebuf[0]==0)) continue;
  41. l=strlen(linebuf);
  42. if (linebuf[l-1]=='\n') linebuf[l-1]=0;
  43. for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ;
  44. if (*opt==0)
  45. opt=NULL;
  46. else {
  47. *opt++=0;
  48. if (*opt=='=') opt++;
  49. while (isspace(*opt)) opt++;
  50. }
  51. for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
  52. int l;
  53. if (!cip->olong) continue;
  54. if (!strcmp(cip->olong,linebuf)) break;
  55. l=strlen(cip->olong);
  56. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  57. !opt && !strncmp(linebuf,cip->olong,l)) {
  58. opt=linebuf+l+1;
  59. break;
  60. }
  61. }
  62. if (!cip->olong) ohshite(_("configuration error: unknown option %s"), linebuf);
  63. if (cip->takesvalue) {
  64. if (!opt) ohshite(_("configuration error: %s needs a value"), linebuf);
  65. if (cip->call) cip->call(cip,opt);
  66. else *cip->sassignto= opt;
  67. } else {
  68. if (opt) ohshite(_("configuration error: %s does not take a value"), linebuf);
  69. if (cip->call) cip->call(cip,0);
  70. else *cip->iassignto= cip->arg;
  71. }
  72. }
  73. if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn);
  74. if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
  75. }
  76. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  77. const struct cmdinfo *cip;
  78. const char *p, *value;
  79. int l;
  80. ++(*argvp);
  81. while ((p= **argvp) && *p == '-') {
  82. ++(*argvp);
  83. if (!strcmp(p,"--")) break;
  84. if (*++p == '-') {
  85. ++p; value=0;
  86. for (cip= cmdinfos;
  87. cip->olong || cip->oshort;
  88. cip++) {
  89. if (!cip->olong) continue;
  90. if (!strcmp(p,cip->olong)) break;
  91. l= strlen(cip->olong);
  92. if (!strncmp(p,cip->olong,l) &&
  93. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  94. }
  95. if (!cip->olong) badusage(_("unknown option --%s"),p);
  96. if (cip->takesvalue) {
  97. if (!value) {
  98. value= *(*argvp)++;
  99. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  100. }
  101. if (cip->call) cip->call(cip,value);
  102. else *cip->sassignto= value;
  103. } else {
  104. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  105. if (cip->call) cip->call(cip,0);
  106. else *cip->iassignto= cip->arg;
  107. }
  108. } else {
  109. while (*p) {
  110. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  111. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  112. p++;
  113. if (cip->takesvalue) {
  114. if (!*p) {
  115. value= *(*argvp)++;
  116. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  117. } else {
  118. value= p; p="";
  119. if (*value == '=') value++;
  120. }
  121. if (cip->call) cip->call(cip,value);
  122. else *cip->sassignto= value;
  123. } else {
  124. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  125. if (cip->call) cip->call(cip,0);
  126. else *cip->iassignto= cip->arg;
  127. }
  128. }
  129. }
  130. }
  131. }