myopt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but 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 args;
  39. va_start(args, fmt);
  40. vsnprintf(buf, sizeof(buf), fmt, args);
  41. va_end(args);
  42. ohshit("%s\n\n%s", buf, gettext(printforhelp));
  43. }
  44. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(3)
  45. config_error(const char *file_name, int line_num, const char *fmt, ...)
  46. {
  47. char buf[1024];
  48. va_list args;
  49. va_start(args, fmt);
  50. vsnprintf(buf, sizeof(buf), fmt, args);
  51. va_end(args);
  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. if (!cip->olong) continue;
  89. if (!strcmp(cip->olong,linebuf)) break;
  90. l=strlen(cip->olong);
  91. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  92. !opt && !strncmp(linebuf,cip->olong,l)) {
  93. opt=linebuf+l+1;
  94. break;
  95. }
  96. }
  97. if (!cip->olong)
  98. config_error(fn, line_num, _("unknown option '%s'"), linebuf);
  99. if (cip->takesvalue) {
  100. if (!opt)
  101. config_error(fn, line_num, _("'%s' needs a value"), linebuf);
  102. if (cip->call) cip->call(cip,opt);
  103. else
  104. *cip->sassignto = m_strdup(opt);
  105. } else {
  106. if (opt)
  107. config_error(fn, line_num, _("'%s' does not take a value"), linebuf);
  108. if (cip->call) cip->call(cip,NULL);
  109. else
  110. *cip->iassignto = cip->arg_int;
  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. m_asprintf(&dirname, "%s/%s.cfg.d", CONFIGDIR, prog);
  137. dlist_n = scandir(dirname, &dlist, valid_config_filename, alphasort);
  138. if (dlist_n < 0) {
  139. if (errno == ENOENT) {
  140. free(dirname);
  141. return;
  142. } else
  143. ohshite(_("error opening configuration directory '%s'"), dirname);
  144. }
  145. for (i = 0; i < dlist_n; i++) {
  146. char *filename;
  147. m_asprintf(&filename, "%s/%s", dirname, dlist[i]->d_name);
  148. myfileopt(filename, cmdinfos);
  149. free(dlist[i]);
  150. free(filename);
  151. }
  152. free(dirname);
  153. free(dlist);
  154. }
  155. void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
  156. char *home, *file;
  157. load_config_dir(prog, cmdinfos);
  158. m_asprintf(&file, "%s/%s.cfg", CONFIGDIR, prog);
  159. myfileopt(file, cmdinfos);
  160. free(file);
  161. if ((home = getenv("HOME")) != NULL) {
  162. m_asprintf(&file, "%s/.%s.cfg", home, prog);
  163. myfileopt(file, cmdinfos);
  164. free(file);
  165. }
  166. }
  167. void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
  168. const struct cmdinfo *cip;
  169. const char *p, *value;
  170. int l;
  171. ++(*argvp);
  172. while ((p= **argvp) && *p == '-') {
  173. ++(*argvp);
  174. if (!strcmp(p,"--")) break;
  175. if (*++p == '-') {
  176. ++p; value=NULL;
  177. for (cip= cmdinfos;
  178. cip->olong || cip->oshort;
  179. cip++) {
  180. if (!cip->olong) continue;
  181. if (!strcmp(p,cip->olong)) break;
  182. l= strlen(cip->olong);
  183. if (!strncmp(p,cip->olong,l) &&
  184. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  185. }
  186. if (!cip->olong) badusage(_("unknown option --%s"),p);
  187. if (cip->takesvalue) {
  188. if (!value) {
  189. value= *(*argvp)++;
  190. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  191. }
  192. if (cip->call) cip->call(cip,value);
  193. else *cip->sassignto= value;
  194. } else {
  195. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  196. if (cip->call) cip->call(cip,NULL);
  197. else
  198. *cip->iassignto = cip->arg_int;
  199. }
  200. } else {
  201. while (*p) {
  202. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  203. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  204. p++;
  205. if (cip->takesvalue) {
  206. if (!*p) {
  207. value= *(*argvp)++;
  208. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  209. } else {
  210. value= p; p="";
  211. if (*value == '=') value++;
  212. }
  213. if (cip->call) cip->call(cip,value);
  214. else *cip->sassignto= value;
  215. } else {
  216. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  217. if (cip->call) cip->call(cip,NULL);
  218. else
  219. *cip->iassignto = cip->arg_int;
  220. }
  221. }
  222. }
  223. }
  224. }
  225. void
  226. setobsolete(const struct cmdinfo *cip, const char *value)
  227. {
  228. warning(_("obsolete option '--%s'\n"), cip->olong);
  229. }
  230. const struct cmdinfo *cipaction = NULL;
  231. /* XXX: This function is a hack. */
  232. static inline int
  233. option_short(int c)
  234. {
  235. return c ? c : '\b';
  236. }
  237. void
  238. setaction(const struct cmdinfo *cip, const char *value)
  239. {
  240. if (cipaction)
  241. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  242. option_short(cip->oshort), cip->olong,
  243. option_short(cipaction->oshort), cipaction->olong);
  244. cipaction = cip;
  245. }