myopt.c 4.0 KB

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