options.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * options.c - option parsing functions
  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/options.h>
  34. static const char *printforhelp;
  35. void
  36. badusage(const char *fmt, ...)
  37. {
  38. char buf[1024];
  39. va_list args;
  40. va_start(args, fmt);
  41. vsnprintf(buf, sizeof(buf), fmt, args);
  42. va_end(args);
  43. ohshit("%s\n\n%s", buf, gettext(printforhelp));
  44. }
  45. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(3)
  46. config_error(const char *file_name, int line_num, const char *fmt, ...)
  47. {
  48. char buf[1024];
  49. va_list args;
  50. va_start(args, fmt);
  51. vsnprintf(buf, sizeof(buf), fmt, args);
  52. va_end(args);
  53. ohshit(_("configuration error: %s:%d: %s"), file_name, line_num, buf);
  54. }
  55. void
  56. myfileopt(const char *fn, const struct cmdinfo *cmdinfos)
  57. {
  58. FILE *file;
  59. int line_num = 0;
  60. char linebuf[MAX_CONFIG_LINE];
  61. file= fopen(fn, "r");
  62. if (!file) {
  63. if (errno==ENOENT)
  64. return;
  65. warning(_("failed to open configuration file '%.255s' for reading: %s"),
  66. fn, strerror(errno));
  67. return;
  68. }
  69. while (fgets(linebuf, sizeof(linebuf), file)) {
  70. char *opt;
  71. const struct cmdinfo *cip;
  72. int l;
  73. line_num++;
  74. if ((linebuf[0] == '#') || (linebuf[0] == '\n') || (linebuf[0] == '\0'))
  75. continue;
  76. l=strlen(linebuf);
  77. if (linebuf[l - 1] == '\n')
  78. linebuf[l - 1] = '\0';
  79. for (opt=linebuf;isalnum(*opt)||*opt=='-';opt++) ;
  80. if (*opt == '\0')
  81. opt=NULL;
  82. else {
  83. *opt++ = '\0';
  84. if (*opt=='=') opt++;
  85. while (isspace(*opt)) opt++;
  86. opt = str_strip_quotes(opt);
  87. if (opt == NULL)
  88. config_error(fn, line_num, _("unbalanced quotes in '%s'"), linebuf);
  89. }
  90. for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
  91. if (!cip->olong) continue;
  92. if (strcmp(cip->olong, linebuf) == 0)
  93. break;
  94. l=strlen(cip->olong);
  95. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  96. !opt && strncmp(linebuf, cip->olong, l) == 0) {
  97. opt=linebuf+l+1;
  98. break;
  99. }
  100. }
  101. if (!cip->olong)
  102. config_error(fn, line_num, _("unknown option '%s'"), linebuf);
  103. if (cip->takesvalue) {
  104. if (!opt)
  105. config_error(fn, line_num, _("'%s' needs a value"), linebuf);
  106. if (cip->call) cip->call(cip,opt);
  107. else
  108. *cip->sassignto = m_strdup(opt);
  109. } else {
  110. if (opt)
  111. config_error(fn, line_num, _("'%s' does not take a value"), linebuf);
  112. if (cip->call) cip->call(cip,NULL);
  113. else
  114. *cip->iassignto = cip->arg_int;
  115. }
  116. }
  117. if (ferror(file)) ohshite(_("read error in configuration file `%.255s'"), fn);
  118. if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
  119. }
  120. static int
  121. valid_config_filename(const struct dirent *dent)
  122. {
  123. const char *c;
  124. if (dent->d_name[0] == '.')
  125. return 0;
  126. for (c = dent->d_name; *c; c++)
  127. if (!isalnum(*c) && *c != '_' && *c != '-')
  128. return 0;
  129. if (*c == '\0')
  130. return 1;
  131. else
  132. return 0;
  133. }
  134. static void
  135. load_config_dir(const char *prog, const struct cmdinfo *cmdinfos)
  136. {
  137. char *dirname;
  138. struct dirent **dlist;
  139. int dlist_n, i;
  140. m_asprintf(&dirname, "%s/%s.cfg.d", CONFIGDIR, prog);
  141. dlist_n = scandir(dirname, &dlist, valid_config_filename, alphasort);
  142. if (dlist_n < 0) {
  143. if (errno == ENOENT) {
  144. free(dirname);
  145. return;
  146. } else
  147. ohshite(_("error opening configuration directory '%s'"), dirname);
  148. }
  149. for (i = 0; i < dlist_n; i++) {
  150. char *filename;
  151. m_asprintf(&filename, "%s/%s", dirname, dlist[i]->d_name);
  152. myfileopt(filename, cmdinfos);
  153. free(dlist[i]);
  154. free(filename);
  155. }
  156. free(dirname);
  157. free(dlist);
  158. }
  159. void
  160. loadcfgfile(const char *prog, const struct cmdinfo *cmdinfos)
  161. {
  162. char *home, *file;
  163. load_config_dir(prog, cmdinfos);
  164. m_asprintf(&file, "%s/%s.cfg", CONFIGDIR, prog);
  165. myfileopt(file, cmdinfos);
  166. free(file);
  167. home = getenv("HOME");
  168. if (home != NULL) {
  169. m_asprintf(&file, "%s/.%s.cfg", home, prog);
  170. myfileopt(file, cmdinfos);
  171. free(file);
  172. }
  173. }
  174. void
  175. myopt(const char *const **argvp, const struct cmdinfo *cmdinfos,
  176. const char *help_str)
  177. {
  178. const struct cmdinfo *cip;
  179. const char *p, *value;
  180. int l;
  181. printforhelp = help_str;
  182. ++(*argvp);
  183. while ((p= **argvp) && *p == '-') {
  184. ++(*argvp);
  185. if (strcmp(p, "--") == 0)
  186. break;
  187. if (*++p == '-') {
  188. ++p; value=NULL;
  189. for (cip= cmdinfos;
  190. cip->olong || cip->oshort;
  191. cip++) {
  192. if (!cip->olong) continue;
  193. if (strcmp(p, cip->olong) == 0)
  194. break;
  195. l= strlen(cip->olong);
  196. if (strncmp(p, cip->olong, l) == 0 &&
  197. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  198. }
  199. if (!cip->olong) badusage(_("unknown option --%s"),p);
  200. if (cip->takesvalue) {
  201. if (!value) {
  202. value= *(*argvp)++;
  203. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  204. }
  205. if (cip->call) cip->call(cip,value);
  206. else *cip->sassignto= value;
  207. } else {
  208. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  209. if (cip->call) cip->call(cip,NULL);
  210. else
  211. *cip->iassignto = cip->arg_int;
  212. }
  213. } else {
  214. while (*p) {
  215. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  216. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  217. p++;
  218. if (cip->takesvalue) {
  219. if (!*p) {
  220. value= *(*argvp)++;
  221. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  222. } else {
  223. value= p; p="";
  224. if (*value == '=') value++;
  225. }
  226. if (cip->call) cip->call(cip,value);
  227. else *cip->sassignto= value;
  228. } else {
  229. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  230. if (cip->call) cip->call(cip,NULL);
  231. else
  232. *cip->iassignto = cip->arg_int;
  233. }
  234. }
  235. }
  236. }
  237. }
  238. void
  239. setobsolete(const struct cmdinfo *cip, const char *value)
  240. {
  241. warning(_("obsolete option '--%s'\n"), cip->olong);
  242. }
  243. const struct cmdinfo *cipaction = NULL;
  244. /* XXX: This function is a hack. */
  245. static inline int
  246. option_short(int c)
  247. {
  248. return c ? c : '\b';
  249. }
  250. void
  251. setaction(const struct cmdinfo *cip, const char *value)
  252. {
  253. if (cipaction)
  254. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  255. option_short(cip->oshort), cip->olong,
  256. option_short(cipaction->oshort), cipaction->olong);
  257. cipaction = cip;
  258. }