myopt.c 4.4 KB

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