options.c 7.8 KB

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