options.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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-2015 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 <limits.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/c-ctype.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. l = strlen(linebuf);
  76. while (l && c_isspace(linebuf[l - 1]))
  77. l--;
  78. linebuf[l] = '\0';
  79. if ((linebuf[0] == '#') || (linebuf[0] == '\0'))
  80. continue;
  81. for (opt = linebuf; c_isalnum(*opt) || *opt == '-'; opt++) ;
  82. if (*opt == '\0')
  83. opt=NULL;
  84. else {
  85. *opt++ = '\0';
  86. if (*opt=='=') opt++;
  87. while (c_isspace(*opt))
  88. opt++;
  89. opt = str_strip_quotes(opt);
  90. if (opt == NULL)
  91. config_error(fn, line_num, _("unbalanced quotes in '%s'"), linebuf);
  92. }
  93. for (cip=cmdinfos; cip->olong || cip->oshort; cip++) {
  94. if (!cip->olong) continue;
  95. if (strcmp(cip->olong, linebuf) == 0)
  96. break;
  97. l=strlen(cip->olong);
  98. if ((cip->takesvalue==2) && (linebuf[l]=='-') &&
  99. !opt && strncmp(linebuf, cip->olong, l) == 0) {
  100. opt=linebuf+l+1;
  101. break;
  102. }
  103. }
  104. if (!cip->olong)
  105. config_error(fn, line_num, _("unknown option '%s'"), linebuf);
  106. if (cip->takesvalue) {
  107. if (!opt)
  108. config_error(fn, line_num, _("'%s' needs a value"), linebuf);
  109. if (cip->call) cip->call(cip,opt);
  110. else
  111. *cip->sassignto = m_strdup(opt);
  112. } else {
  113. if (opt)
  114. config_error(fn, line_num, _("'%s' does not take a value"), linebuf);
  115. if (cip->call) cip->call(cip,NULL);
  116. else
  117. *cip->iassignto = cip->arg_int;
  118. }
  119. }
  120. if (ferror(file))
  121. ohshite(_("read error in configuration file '%.255s'"), fn);
  122. if (fclose(file))
  123. ohshite(_("error closing configuration file '%.255s'"), fn);
  124. }
  125. static int
  126. valid_config_filename(const struct dirent *dent)
  127. {
  128. const char *c;
  129. if (dent->d_name[0] == '.')
  130. return 0;
  131. for (c = dent->d_name; *c; c++)
  132. if (!c_isalnum(*c) && *c != '_' && *c != '-')
  133. return 0;
  134. if (*c == '\0')
  135. return 1;
  136. else
  137. return 0;
  138. }
  139. static void
  140. dpkg_options_load_dir(const char *prog, const struct cmdinfo *cmdinfos)
  141. {
  142. char *dirname;
  143. struct dirent **dlist;
  144. int dlist_n, i;
  145. m_asprintf(&dirname, "%s/%s.cfg.d", CONFIGDIR, prog);
  146. dlist_n = scandir(dirname, &dlist, valid_config_filename, alphasort);
  147. if (dlist_n < 0) {
  148. if (errno == ENOENT) {
  149. free(dirname);
  150. return;
  151. } else
  152. ohshite(_("error opening configuration directory '%s'"), dirname);
  153. }
  154. for (i = 0; i < dlist_n; i++) {
  155. char *filename;
  156. m_asprintf(&filename, "%s/%s", dirname, dlist[i]->d_name);
  157. dpkg_options_load_file(filename, cmdinfos);
  158. free(dlist[i]);
  159. free(filename);
  160. }
  161. free(dirname);
  162. free(dlist);
  163. }
  164. void
  165. dpkg_options_load(const char *prog, const struct cmdinfo *cmdinfos)
  166. {
  167. char *home, *file;
  168. dpkg_options_load_dir(prog, cmdinfos);
  169. m_asprintf(&file, "%s/%s.cfg", CONFIGDIR, prog);
  170. dpkg_options_load_file(file, cmdinfos);
  171. free(file);
  172. home = getenv("HOME");
  173. if (home != NULL) {
  174. m_asprintf(&file, "%s/.%s.cfg", home, prog);
  175. dpkg_options_load_file(file, cmdinfos);
  176. free(file);
  177. }
  178. }
  179. void
  180. dpkg_options_parse(const char *const **argvp, const struct cmdinfo *cmdinfos,
  181. const char *help_str)
  182. {
  183. const struct cmdinfo *cip;
  184. const char *p, *value;
  185. int l;
  186. printforhelp = help_str;
  187. ++(*argvp);
  188. while ((p = **argvp) && p[0] == '-' && p[1] != '\0') {
  189. ++(*argvp);
  190. if (strcmp(p, "--") == 0)
  191. break;
  192. if (*++p == '-') {
  193. ++p; value=NULL;
  194. for (cip= cmdinfos;
  195. cip->olong || cip->oshort;
  196. cip++) {
  197. if (!cip->olong) continue;
  198. if (strcmp(p, cip->olong) == 0)
  199. break;
  200. l= strlen(cip->olong);
  201. if (strncmp(p, cip->olong, l) == 0 &&
  202. (p[l]== ((cip->takesvalue==2) ? '-' : '='))) { value=p+l+1; break; }
  203. }
  204. if (!cip->olong) badusage(_("unknown option --%s"),p);
  205. if (cip->takesvalue) {
  206. if (!value) {
  207. value= *(*argvp)++;
  208. if (!value) badusage(_("--%s option takes a value"),cip->olong);
  209. }
  210. if (cip->call) cip->call(cip,value);
  211. else *cip->sassignto= value;
  212. } else {
  213. if (value) badusage(_("--%s option does not take a value"),cip->olong);
  214. if (cip->call) cip->call(cip,NULL);
  215. else
  216. *cip->iassignto = cip->arg_int;
  217. }
  218. } else {
  219. while (*p) {
  220. for (cip= cmdinfos; (cip->olong || cip->oshort) && *p != cip->oshort; cip++);
  221. if (!cip->oshort) badusage(_("unknown option -%c"),*p);
  222. p++;
  223. if (cip->takesvalue) {
  224. if (!*p) {
  225. value= *(*argvp)++;
  226. if (!value) badusage(_("-%c option takes a value"),cip->oshort);
  227. } else {
  228. value= p; p="";
  229. if (*value == '=') value++;
  230. }
  231. if (cip->call) cip->call(cip,value);
  232. else *cip->sassignto= value;
  233. } else {
  234. if (*p == '=') badusage(_("-%c option does not take a value"),cip->oshort);
  235. if (cip->call) cip->call(cip,NULL);
  236. else
  237. *cip->iassignto = cip->arg_int;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. long
  244. dpkg_options_parse_arg_int(const struct cmdinfo *cmd, const char *str)
  245. {
  246. long value;
  247. char *end;
  248. errno = 0;
  249. value = strtol(str, &end, 0);
  250. if (str == end || *end || value < 0 || value > INT_MAX || errno != 0)
  251. badusage(_("invalid integer for --%s: '%.250s'"), cmd->olong, str);
  252. return value;
  253. }
  254. void
  255. setobsolete(const struct cmdinfo *cip, const char *value)
  256. {
  257. warning(_("obsolete option '--%s'"), cip->olong);
  258. }
  259. const struct cmdinfo *cipaction = NULL;
  260. /* XXX: This function is a hack. */
  261. static inline int
  262. option_short(int c)
  263. {
  264. return c ? c : '\b';
  265. }
  266. void
  267. setaction(const struct cmdinfo *cip, const char *value)
  268. {
  269. if (cipaction)
  270. badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
  271. option_short(cip->oshort), cip->olong,
  272. option_short(cipaction->oshort), cipaction->olong);
  273. cipaction = cip;
  274. }