3 * Explore and debug `runlisp' configration
5 * (c) 2020 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Runlisp, a tool for invoking Common Lisp scripts.
12 * Runlisp is free software: you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * Runlisp is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with Runlisp. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
40 /*----- Static data -------------------------------------------------------*/
42 /* Query operations. */
44 OP_LISTSEC, /* list all sections */
45 OP_LISTVAR, /* list variables in a section */
46 OP_RAW, /* print a variable's value */
47 OP_SUBST, /* print variable's expansion */
48 OP_SPLIT, /* print word-split variable */
52 /* A node in the list of queued-up operations. */
54 struct op *next; /* link to next op in the list */
55 unsigned code; /* operation code (`OP_...') */
56 const char *arg; /* argument (from command-line) */
59 static struct op *oplist; /* list of queued-up operations */
61 static unsigned flags = 0; /* flags for the application */
62 #define AF_BOGUS 0x0001u /* invalid command-line syntax */
63 #define AF_SETCONF 0x0002u /* explicit configuration */
64 #define AF_PLAIN 0x0004u /* don't print labels */
66 /*----- Main code ---------------------------------------------------------*/
68 /* Append a new node to the list of delayed operations, with CODE and ARG.
70 * The address of the final link (initially, the list head) is in
71 * *TAIL_INOUT: make this point to the new node, and then update it to point
72 * to the link in the new node.
74 static void add_op(struct op ***tail_inout, unsigned code, const char *arg)
76 struct op *op = xmalloc(sizeof(*op));
77 op->code = code; op->arg = arg; **tail_inout = op; *tail_inout = &op->next;
80 /* Given an ARG of the form `[SECT:]VAR', set *SECT_OUT and *VAR_OUT to the
81 * requested home section and variable. Leave these null if they can't be
84 static void find_var(const char *arg,
85 struct config_section **sect_out,
86 struct config_var **var_out)
88 struct config_section *sect;
93 { sect = toplevel; p = arg; }
95 { sect = config_find_section_n(&config, 0, arg, p - arg); p++; }
97 if (!sect) *var_out = 0;
98 else *var_out = config_find_var(&config, sect, CF_INHERIT, p);
101 /* Help and related functions. */
102 static void version(FILE *fp)
103 { fprintf(fp, "%s, runlisp version %s\n", progname, PACKAGE_VERSION); }
105 static void usage(FILE *fp)
108 usage: %s [-LMqv] [+M] [-c CONF] [-o [SECT:]VAR=VAL]\n\
109 [-l SECT] [-p [SECT:]VAR] [-w [SECT:]VAR] [-x [SECT:]VAR]\n",
113 static void help(FILE *fp)
115 version(fp); fputc('\n', fp); usage(fp);
118 -h, --help Show this help text and exit successfully.\n\
119 -V, --version Show version number and exit successfully.\n\
122 -q, --quiet Don't print warning messages.\n\
123 -v, --verbose Print informational messages (repeatable).\n\
126 -c, --config-file=CONF Read configuration from CONF (repeatable).\n\
127 -o, --set-option=[SECT:]VAR=VAL Set configuration variable (repeatable).\n\
130 -L, --list-sections List all known section names in order.\n\
131 -M, --machine-readable Don't print headers or labels.\n\
132 -l, --list-variables=SECTION List all defined variables in SECTION.\n\
133 -p, --print-variable=[SECT:]VAR Print the raw (unexpanded) value of VAR.\n\
134 -w, --split-variable=[SECT:]VAR Expand and word-split VAR and print.\n\
135 -x, --expand-variable=[SECT:]VAR Expand VAR and print the result.\n", fp);
139 int main(int argc, char *argv[])
141 struct config_section_iter si;
142 struct config_section *sect;
143 struct config_var_iter vi;
144 struct config_var *var;
145 struct op *op, **tail = &oplist;
146 struct dstr d = DSTR_INIT;
147 struct argv av = ARGV_INIT;
150 /* Command-line options. */
151 static const struct option opts[] = {
152 { "help", 0, 0, 'h' },
153 { "version", 0, 0, 'V' },
154 { "list-sections", 0, 0, 'L' },
155 { "machine-readable", OPTF_NEGATE, 0, 'M' },
156 { "config-file", OPTF_ARGREQ, 0, 'c' },
157 { "list-variables", OPTF_ARGREQ, 0, 'l' },
158 { "set-option", OPTF_ARGREQ, 0, 'o' },
159 { "print-variable", OPTF_ARGREQ, 0, 'p' },
160 { "quiet", 0, 0, 'q' },
161 { "verbose", 0, 0, 'v' },
162 { "split-variable", OPTF_ARGREQ, 0, 'w' },
163 { "expand-variable", OPTF_ARGREQ, 0, 'x' },
168 set_progname(argv[0]);
171 /* Parse the options.
173 * We must delay the query operations until the configuration is loaded,
174 * but we won't know whether to load the default configuration until we're
175 * sure that that there are no `-c' options. So just stash the queries in
176 * a list until later.
178 optprog = (/*unconst*/ char *)progname;
180 #define FLAGOPT(ch, f) \
184 case ch | OPTF_NEGATED: \
189 i = mdwopt(argc - 1, argv + 1, "hVLM+c:l:o:p:qvw:x:", opts, 0, 0,
193 case 'h': help(stdout); exit(0);
194 case 'V': version(stdout); exit(0);
195 case 'L': add_op(&tail, OP_LISTSEC, 0); break;
196 FLAGOPT('M', AF_PLAIN);
197 case 'c': read_config_path(optarg, 0); flags |= AF_SETCONF; break;
198 case 'l': add_op(&tail, OP_LISTVAR, optarg); break;
199 case 'o': if (set_config_var(optarg)) flags |= AF_BOGUS; break;
200 case 'p': add_op(&tail, OP_RAW, optarg); break;
201 case 'q': if (verbose) verbose--; break;
202 case 'v': verbose++; break;
203 case 'w': add_op(&tail, OP_SPLIT, optarg); break;
204 case 'x': add_op(&tail, OP_SUBST, optarg); break;
205 default: flags |= AF_BOGUS; break;
211 /* Check that everything worked. */
213 if (optind < argc) flags |= AF_BOGUS;
214 if (flags&AF_BOGUS) { usage(stderr); exit(127); }
215 *tail = 0; if (!oplist) lose("nothing to do");
217 /* Load default configuration if no explicit files were requested. */
218 if (!(flags&AF_SETCONF)) load_default_config();
220 /* Work through the operations we stashed earlier. */
221 for (op = oplist; op; op = op->next)
225 if (!(flags&AF_PLAIN)) printf("sections:\n");
226 for (config_start_section_iter(&config, &si);
227 (sect = config_next_section(&si)); )
229 flags&AF_PLAIN ? "" : "\t",
230 CONFIG_SECTION_NAME(sect));
234 sect = config_find_section(&config, 0, op->arg);
236 if (!(flags&AF_PLAIN))
237 printf("section `%s' variables:\n", CONFIG_SECTION_NAME(sect));
238 for (config_start_var_iter(&config, sect, &vi);
239 (var = config_next_var(&vi)); )
241 flags&AF_PLAIN ? "" : "\t",
242 CONFIG_VAR_NAME(var));
244 if (flags&AF_PLAIN) lose("section `%s' not found", op->arg);
245 else printf("section `%s' not found\n", op->arg);
250 find_var(op->arg, §, &var);
251 if (flags&AF_PLAIN) {
252 if (var) puts(var->val);
253 else lose("variable `%s' not found", op->arg);
255 if (var) printf("%s = %s\n", op->arg, var->val);
256 else printf("%s not found\n", op->arg);
261 find_var(op->arg, §, &var);
263 dstr_reset(&d); config_subst_var(&config, sect, var, &d);
264 if (flags&AF_PLAIN) puts(d.p);
265 else printf("%s = %s\n", op->arg, d.p);
267 if (flags&AF_PLAIN) lose("variable %s not found", op->arg);
268 else printf("%s not found\n", op->arg);
273 find_var(op->arg, §, &var);
275 argv_reset(&av); config_subst_split_var(&config, sect, var, &av);
276 dstr_reset(&d); argv_string(&d, &av);
277 if (flags&AF_PLAIN) puts(d.p);
278 else printf("%s = %s\n", op->arg, d.p);
280 if (flags&AF_PLAIN) lose("variable %s not found", op->arg);
281 else printf("%s not found\n", op->arg);
293 /*----- That's all, folks -------------------------------------------------*/