myopt.c 4.3 KB

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