myopt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * myopt.c - my very own option parsing
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2000,2002 Wichert Akkerman <wichert@deephackmode.org>
  7. * Copyright © 2008,2009 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2,
  12. * or (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <errno.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <dirent.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dpkg.h>
  32. #include <dpkg/string.h>
  33. #include <dpkg/myopt.h>
  34. void
  35. badusage(const char *fmt, ...)
  36. {
  37. char buf[1024];
  38. va_list al;
  39. va_start(al, fmt);
  40. vsnprintf(buf, sizeof(buf), fmt, al);
  41. va_end(al);
  42. ohshit("%s\n\n%s", buf, gettext(printforhelp));
  43. }
  44. static void
  45. config_error(const char *file_name, int line_num, const char *fmt, ...)
  46. {
  47. char buf[1024];
  48. va_list al;
  49. va_start(al, fmt);
  50. vsnprintf(buf, sizeof(buf), fmt, al);
  51. va_end(al);
  52. ohshit(_("configuration error: %s:%d: %s"), file_name, line_num, buf);
  53. }
  54. void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) {
  55. FILE* file;
  56. int line_num = 0;
  57. char linebuf[MAX_CONFIG_LINE];
  58. file= fopen(fn, "r");
  59. if (!file) {
  60. if (errno==ENOENT)
  61. return;
  62. warning(_("failed to open configuration file '%.255s' for reading: %s"),
  63. fn, strerror(errno));
  64. return;
  65. }
  66. while (fgets(linebuf, sizeof(linebuf), file)) {
  67. char* opt;
  68. const struct cmdinfo *cip;
  69. int l;
  70. line_num++;
  71. if ((linebuf[0] == '#') || (linebuf[0] == '\n') || (linebuf[0] == '\0'))
  72. continue;
  73. l=strlen(linebuf);
  74. if (linebuf[l - 1] == '\n')
  75. linebuf[l - 1] = '\0';
  76. for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ;
  77. if (*opt == '\0')
  78. opt=NULL;
  79. else {
  80. *opt++ = '\0';
  81. if (*opt=='=') opt++;
  82. while (isspace(*opt)) opt++;
  83. opt = str_strip_quotes(opt);
  84. if (opt == NULL)
  85. config_error(fn, line_num, _("unbalanced quotes in '%s'"), linebuf);
  86. }
  87. for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
  88. int l;
  89. if (!cip->olong) continue;
  90. if (!strcmp(cip->olong,linebuf)) break;
  91. l=strlen(cip->olong);
  92. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  93. !opt && !strncmp(linebuf,cip->olong,l)) {
  94. opt=linebuf+l+1;
  95. break;
  96. }
  97. }
  98. if (!cip->olong)
  99. config_error(fn, line_num, _("unknown option '%s'"), linebuf);
  100. if (cip->takesvalue) {
  101. if (!opt)
  102. config_error(fn, line_num, _("'%s' needs a value"), linebuf);
  103. if (cip->call) cip->call(cip,opt);
  104. else
  105. *cip->sassignto = m_strdup(opt);
  106. } else {
  107. if (opt)
  108. config_error(fn, line_num, _("'%s' does not take a value"), linebuf);
  109. if (cip->call) cip->call(cip,NULL);
  110. else *cip->iassignto= cip->arg;
  111. }
  112. }
  113. if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn);
  114. if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
  115. }
  116. static int
  117. valid_config_filename(const struct dirent *dent)
  118. {
  119. const char *c;
  120. if (dent->d_name[0] == '.')
  121. return 0;
  122. for (c = dent->d_name; *c; c++)
  123. if (!isalnum(*c) && *c != '_' && *c != '-')
  124. return 0;
  125. if (*c == '\0')
  126. return 1;
  127. else
  128. return 0;
  129. }
  130. static void
  131. load_config_dir(const char *prog, const struct cmdinfo* cmdinfos)
  132. {
  133. char *dirname;
  134. struct dirent **dlist;
  135. int dlist_n, i;
  136. dirname = m_malloc(strlen(CONFIGDIR "/.cfg.d") + strlen(prog) + 1);
  137. sprintf(dirname, "%s/%s.cfg.d", CONFIGDIR, prog);
  138. dlist_n = scandir(dirname, &dlist, valid_config_filename, alphasort);
  139. if (dlist_n < 0) {
  140. if (errno == ENOENT) {
  141. free(dirname);
  142. return;
  143. } else
  144. ohshite(_("error opening configuration directory '%s'"), dirname);
  145. }
  146. for (i = 0; i < dlist_n; i++) {
  147. char *filename;
  148. filename = m_malloc(strlen(dirname) + 1 + strlen(dlist[i]->d_name) + 1);
  149. sprintf(filename, "%s/%s", dirname, dlist[i]->d_name);
  150. myfileopt(filename, cmdinfos);
  151. free(filename);
  152. }
  153. free(dirname);
  154. free(dlist);
  155. }
  156. void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
  157. char *home, *file;
  158. int l1, l2;
  159. load_config_dir(prog, cmdinfos);
  160. l1 = strlen(CONFIGDIR "/.cfg") + strlen(prog);
  161. file = m_malloc(l1 + 1);
  162. sprintf(file, CONFIGDIR "/%s.cfg", prog);
  163. myfileopt(file, cmdinfos);
  164. if ((home = getenv("HOME")) != NULL) {
  165. l2 = strlen(home) + 1 + strlen("/.cfg") + strlen(prog);
  166. if (l2 > l1) {
  167. free(file);
  168. file = m_malloc(l2 + 1);
  169. }
  170. sprintf(file, "%s/.%s.cfg", home, prog);
  171. myfileopt(file, cmdinfos);
  172. }
  173. free(file);
  174. }
  175. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  176. const struct cmdinfo *cip;
  177. const char *p, *value;
  178. int l;
  179. ++(*argvp);
  180. while ((p= **argvp) && *p == '-') {
  181. ++(*argvp);
  182. if (!strcmp(p,"--")) break;
  183. if (*++p == '-') {
  184. ++p; value=NULL;
  185. for (cip= cmdinfos;
  186. cip->olong || cip->oshort;
  187. cip++) {
  188. if (!cip->olong) continue;
  189. if (!strcmp(p,cip->olong)) break;
  190. l= strlen(cip->olong);
  191. if (!strncmp(p,cip->olong,l) &&
  192. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  193. }
  194. if (!cip->olong) badusage(_("unknown option --%s"),p);
  195. if (cip->takesvalue) {
  196. if (!value) {
  197. value= *(*argvp)++;
  198. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  199. }
  200. if (cip->call) cip->call(cip,value);
  201. else *cip->sassignto= value;
  202. } else {
  203. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  204. if (cip->call) cip->call(cip,NULL);
  205. else *cip->iassignto= cip->arg;
  206. }
  207. } else {
  208. while (*p) {
  209. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  210. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  211. p++;
  212. if (cip->takesvalue) {
  213. if (!*p) {
  214. value= *(*argvp)++;
  215. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  216. } else {
  217. value= p; p="";
  218. if (*value == '=') value++;
  219. }
  220. if (cip->call) cip->call(cip,value);
  221. else *cip->sassignto= value;
  222. } else {
  223. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  224. if (cip->call) cip->call(cip,NULL);
  225. else *cip->iassignto= cip->arg;
  226. }
  227. }
  228. }
  229. }
  230. }