myopt.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with this file; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <string.h>
  22. #include <config.h>
  23. #include <myopt.h>
  24. #include <dpkg.h>
  25. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  26. const struct cmdinfo *cip;
  27. const char *p, *value;
  28. int l;
  29. ++(*argvp);
  30. while ((p= **argvp) && *p == '-') {
  31. ++(*argvp);
  32. if (!strcmp(p,"--")) break;
  33. if (*++p == '-') {
  34. ++p; value=0;
  35. for (cip= cmdinfos;
  36. cip->olong || cip->oshort;
  37. cip++) {
  38. if (!cip->olong) continue;
  39. if (!strcmp(p,cip->olong)) break;
  40. l= strlen(cip->olong);
  41. if (!strncmp(p,cip->olong,l) &&
  42. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  43. }
  44. if (!cip->olong) badusage(_("unknown option --%s"),p);
  45. if (cip->takesvalue) {
  46. if (!value) {
  47. value= *(*argvp)++;
  48. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  49. }
  50. if (cip->call) cip->call(cip,value);
  51. else *cip->sassignto= value;
  52. } else {
  53. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  54. if (cip->call) cip->call(cip,0);
  55. else *cip->iassignto= cip->arg;
  56. }
  57. } else {
  58. while (*p) {
  59. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  60. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  61. p++;
  62. if (cip->takesvalue) {
  63. if (!*p) {
  64. value= *(*argvp)++;
  65. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  66. } else {
  67. value= p; p="";
  68. if (*value == '=') value++;
  69. }
  70. if (cip->call) cip->call(cip,value);
  71. else *cip->sassignto= value;
  72. } else {
  73. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  74. if (cip->call) cip->call(cip,0);
  75. else *cip->iassignto= cip->arg;
  76. }
  77. }
  78. }
  79. }
  80. }