chiark / gitweb /
@@@ more wip
[runlisp] / runlisp.c
1 /* -*-c-*-
2  *
3  * Invoke Lisp scripts and implementations
4  *
5  * (c) 2020 Mark Wooding
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Runlisp, a tool for invoking Common Lisp scripts.
11  *
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.
16  *
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
20  * for more details.
21  *
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/>.
24  */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include "config.h"
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "common.h"
37 #include "lib.h"
38 #include "mdwopt.h"
39
40 /*----- Static data -------------------------------------------------------*/
41
42 /* The state we need for a Lisp system. */
43 struct lispsys {
44   struct treap_node _node;              /* treap intrusion */
45   struct lispsys *next_lisp,            /* link in all-Lisps list */
46     *next_accept,                       /* link acceptable-Lisps list */
47     *next_prefer,                       /* link in preferred-Lisps list */
48     *next_order;                        /* link in overall-order list */
49   unsigned f;                           /* flags */
50 #define LF_KNOWN 1u                     /*   this is actually a Lisp */
51 #define LF_ACCEPT 2u                    /*   this is an acceptable Lisp */
52 #define LF_PREFER 4u                    /*   this is a preferred Lisp */
53   struct config_section *sect;          /* configuration section */
54   struct config_var *var;               /* `run-script variable */
55 };
56 #define LISPSYS_NAME(lisp) TREAP_NODE_KEY(lisp)
57 #define LISPSYS_NAMELEN(lisp) TREAP_NODE_KEYLEN(lisp)
58
59 /* Pick out a link from a `struct lispsys' object given its offset. */
60 #define LISP_LINK(lisp, linkoff)                                        \
61         ((struct lispsys **)((unsigned char *)(lisp) + (linkoff)))
62
63 /* A list of Lisp systems. */
64 struct lispsys_list {
65   struct lispsys *head, **tail;         /* list head and tail */
66 };
67
68 static struct argv argv_tail = ARGV_INIT; /* accumulates eval-mode args */
69 struct treap lispsys = TREAP_INIT;      /* track duplicate Lisp systems */
70 static struct lispsys_list              /* lists of Lisp systems */
71   lisps = { 0, &lisps.head },           /*   all known */
72   accept = { 0, &accept.head },         /*   acceptable */
73   prefer = { 0, &prefer.head };         /*   preferred */
74
75 static unsigned flags = 0;              /* flags for the application */
76 #define AF_CMDLINE 0x0000u              /*   options are from command-line */
77 #define AF_EMBED 0x0001u                /*   reading embedded options */
78 #define AF_STATEMASK 0x000fu            /*   mask of option origin codes */
79 #define AF_BOGUS 0x0010u                /*   invalid command-line syntax */
80 #define AF_SETCONF 0x0020u              /*   explicit configuration */
81 #define AF_NOEMBED 0x0040u              /*   don't read embedded options */
82 #define AF_DRYRUN 0x0080u               /*   don't actually do it */
83 #define AF_VANILLA 0x0100u              /*   don't use custom images */
84
85 /*----- Main code ---------------------------------------------------------*/
86
87 /* Return the `struct lispsys' entry for the given N-byte NAME. */
88 static struct lispsys *ensure_lispsys(const char *name, size_t n)
89 {
90   struct lispsys *lisp;
91   struct treap_path path;
92
93   lisp = treap_probe(&lispsys, name, n, &path);
94   if (!lisp) {
95     lisp = xmalloc(sizeof(*lisp));
96     lisp->f = 0; lisp->sect = 0;
97     treap_insert(&lispsys, &path, &lisp->_node, name, n);
98   }
99   return (lisp);
100 }
101
102 /* Add Lisp systems from the comma- or space-sparated list P to LIST.
103  *
104  * WHAT is an adjective describing the list flavour; FLAG is a bit to set in
105  * the node's flags word; LINKOFF is the offset of the list's link member.
106  */
107 static void add_lispsys(const char *p, const char *what,
108                         struct lispsys_list *list,
109                         unsigned flag, size_t linkoff)
110 {
111   struct lispsys *lisp, **link;
112   const char *q;
113
114   if (!*p) return;
115   for (;;) {
116     while (ISSPACE(*p)) p++;
117     if (!*p) break;
118     q = p; while (*p && !ISSPACE(*p) && *p != ',') p++;
119     lisp = ensure_lispsys(q, p - q);
120     if (lisp->f&flag) {
121       if (verbose >= 1)
122         moan("ignoring duplicate %s Lisp `%.*s'", what, (int)(p - q), q);
123     } else {
124       link = LISP_LINK(lisp, linkoff);
125       lisp->f |= flag; *link = 0;
126       *list->tail = lisp; list->tail = link;
127     }
128     while (ISSPACE(*p)) p++;
129     if (!*p) break;
130     if (*p == ',') p++;
131   }
132 }
133
134 /* Check that the Lisp systems on LIST (linked through LINKOFF) are real.
135  *
136  * That is, `LF_KNOWN' is set in their flags.
137  */
138 static void check_lisps(const char *what,
139                         struct lispsys_list *list, size_t linkoff)
140 {
141   struct lispsys *lisp;
142
143   for (lisp = list->head; lisp; lisp = *LISP_LINK(lisp, linkoff))
144     if (!(lisp->f&LF_KNOWN))
145       lose("unknown Lisp implementation `%s'", LISPSYS_NAME(lisp));
146 }
147
148 /* Dump the names of the Lisp systems on LIST (linked through LINKOFF).
149  *
150  * WHAT is an adjective describing the list.
151  */
152 static void dump_lisps(const char *what,
153                        struct lispsys_list *list, size_t linkoff)
154 {
155   struct dstr d = DSTR_INIT;
156   struct lispsys *lisp;
157   int first;
158
159   first = 1;
160   for (lisp = list->head; lisp; lisp = *LISP_LINK(lisp, linkoff)) {
161     if (first) first = 0;
162     else dstr_puts(&d, ", ");
163     dstr_puts(&d, LISPSYS_NAME(lisp));
164   }
165   if (first) dstr_puts(&d, "(none)");
166   dstr_putz(&d);
167   moan("%s: %s", what, d.p);
168   dstr_release(&d);
169 }
170
171 /* Add an eval-mode operation to the `argv_tail' vector.
172  *
173  * OP is the operation character (see `eval.lisp' for these) and `val' is the
174  * argument (filename or expression).
175  */
176 static void push_eval_op(char op, const char *val)
177 {
178   char *p;
179   size_t n;
180
181   if ((flags&AF_STATEMASK) != AF_CMDLINE) {
182     moan("must use `-e', `-p', or `-l' on command line");
183     flags |= AF_BOGUS;
184     return;
185   }
186
187   n = strlen(val) + 1;
188   p = xmalloc(n + 1);
189   p[0] = op; memcpy(p + 1, val, n);
190   argv_append(&argv_tail, p);
191 }
192
193 /* Help and related functions. */
194 static void version(FILE *fp)
195   { fprintf(fp, "%s, version %s\n", progname, PACKAGE_VERSION); }
196
197 static void usage(FILE *fp)
198 {
199   fprintf(fp, "\
200 usage:\n\
201         %s [OPTIONS] [--] SCRIPT [ARGUMENTS ...]\n\
202         %s [OPTIONS] [-e EXPR] [-p EXPR] [-l FILE] [--] [ARGUMENTS ...]\n\
203 OPTIONS:\n\
204         [-CDEnqv] [+DEn] [-L SYS,SYS,...] [-c CONF] [-o [SECT:]VAR=VAL]\n",
205           progname, progname);
206 }
207
208 static void help(FILE *fp)
209 {
210   version(fp); fputc('\n', fp); usage(fp);
211   fputs("\n\
212 Help options:\n\
213   -h, --help                    Show this help text and exit successfully.\n\
214   -V, --version                 Show version number and exit successfully.\n\
215 \n\
216 Diagnostics:\n\
217   -n, --dry-run                 Don't run run anything (useful with `-v').\n\
218   -q, --quiet                   Don't print warning messages.\n\
219   -v, --verbose                 Print informational messages (repeatable).\n\
220 \n\
221 Configuration:\n\
222   -E, --command-line-only       Don't read embedded options from script.\n\
223   -c, --config-file=CONF        Read configuration from CONF (repeatable).\n\
224   -o, --set-option=[SECT:]VAR=VAL Set configuration variable (repeatable).\n\
225 \n\
226 Lisp implementation selection:\n\
227   -D, --vanilla-image           Run vanilla Lisp images, not custom ones.\n\
228   -L, --accept-lisp=SYS,SYS,... Only use the listed Lisp systems.\n\
229 \n\
230 Evaluation mode:\n\
231   -e, --evaluate-expression=EXPR Evaluate EXPR for effect (repeatable).\n\
232   -l, --load-file=FILE          Load FILE (repeatable).\n\
233   -p, --print-expression=EXPR   Print (`prin1') EXPR (repeatable).\n",
234         fp);
235 }
236
237 /* Parse the options in the argument vector. */
238 static void parse_options(int argc, char *argv[])
239 {
240   int i;
241
242   static const struct option opts[] = {
243     { "help",                   0,              0,      'h' },
244     { "version",                0,              0,      'V' },
245     { "vanilla-image",          OPTF_NEGATE,    0,      'D' },
246     { "command-line-only",      OPTF_NEGATE,    0,      'E' },
247     { "accept-lisp",            OPTF_ARGREQ,    0,      'L' },
248     { "config-file",            OPTF_ARGREQ,    0,      'c' },
249     { "evaluate-expression",    OPTF_ARGREQ,    0,      'e' },
250     { "load-file",              OPTF_ARGREQ,    0,      'l' },
251     { "dry-run",                OPTF_NEGATE,    0,      'n' },
252     { "set-option",             OPTF_ARGREQ,    0,      'o' },
253     { "print-expression",       OPTF_ARGREQ,    0,      'p' },
254     { "quiet",                  0,              0,      'q' },
255     { "verbose",                0,              0,      'v' },
256     { 0,                        0,              0,      0 }
257   };
258
259   optarg = 0; optind = 0; optprog = (/*unconst*/ char *)progname;
260   for (;;) {
261     i = mdwopt(argc, argv, "+hVD+E+L:c:e:l:n+o:p:qv", opts, 0, 0,
262                OPTF_NEGATION | OPTF_NOPROGNAME);
263     if (i < 0) break;
264     switch (i) {
265       case 'h': help(stdout); exit(0);
266       case 'V': version(stdout); exit(0);
267       case 'D': flags |= AF_VANILLA; break;
268       case 'D' | OPTF_NEGATED: flags &= ~AF_VANILLA; break;
269       case 'E': flags |= AF_NOEMBED; break;
270       case 'E' | OPTF_NEGATED: flags &= ~AF_NOEMBED; break;
271       case 'L':
272         add_lispsys(optarg, "acceptable", &accept, LF_ACCEPT,
273                     offsetof(struct lispsys, next_accept));
274         break;
275       case 'c': read_config_path(optarg, 0); flags |= AF_SETCONF; break;
276       case 'e': push_eval_op('!', optarg); break;
277       case 'l': push_eval_op('<', optarg); break;
278       case 'n': flags |= AF_DRYRUN; break;
279       case 'n' | OPTF_NEGATED: flags &= ~AF_DRYRUN; break;
280       case 'o': if (set_config_var(optarg)) flags |= AF_BOGUS; break;
281       case 'p': push_eval_op('?', optarg); break;
282       case 'q': if (verbose) verbose--; break;
283       case 'v': verbose++; break;
284       default: flags |= AF_BOGUS; break;
285     }
286   }
287 }
288
289 /* Extract and process the embedded options from a SCRIPT. */
290 static void handle_embedded_args(const char *script)
291 {
292   struct dstr d = DSTR_INIT;
293   struct argv av = ARGV_INIT;
294   char *p, *q, *r; const char *l;
295   size_t n;
296   int qstate = 0;
297   FILE *fp = 0;
298
299   /* Open the script.  If this doesn't work, then we have no hope. */
300   fp = fopen(script, "r");
301   if (!fp) lose("can't read script `%s': %s", script, strerror(errno));
302
303   /* Read the second line. */
304   if (dstr_readline(&d, fp)) goto end;
305   dstr_reset(&d); if (dstr_readline(&d, fp)) goto end;
306
307   /* Check to find the magic marker. */
308   p = strstr(d.p, "@RUNLISP:"); if (!p) goto end;
309   p += 9; q = p; l = d.p + d.len;
310
311   /* Split the line into words.
312    *
313    * Do this by hand because we have strange things to support, such as Emacs
314    * turds and the early `--' exit.
315    *
316    * We work in place: `p' is the input cursor and advances through the
317    * string as we parse, until it meets the limit pointer `l'; `q' is the
318    * output cursor which will always be no further forward than `p'.
319    */
320   for (;;) {
321     /* Iterate over the words. */
322
323     /* Skip spaces. */
324     while (p < l && ISSPACE(*p)) p++;
325
326     /* If we've reached the end then we're done. */
327     if (p >= l) break;
328
329     /* Check for an Emacs local-variables `-*-' turd.
330      *
331      * If we find one, find the matching end marker and move past it.
332      */
333     if (l - p >= 3 && p[0] == '-' && p[1] == '*' && p[2] == '-') {
334       p = strstr(p + 3, "-*-");
335       if (!p || p + 3 > l)
336         lose("%s:2: unfinished local-variables list", script);
337       p += 3;
338       continue;
339     }
340
341     /* If we find a `--' marker then stop immediately. */
342     if (l - p >= 2 && p[0] == '-' && p[1] == '-' &&
343         (l == p + 2 || ISSPACE(p[2])))
344       break;
345
346     /* Push the output cursor position onto the output, because this is where
347      * the next word will start.
348      */
349     argv_append(&av, q);
350
351     /* Collect characters until we find an unquoted space. */
352     while (p < l && (qstate || !ISSPACE(*p))) {
353
354       if (*p == '"')
355         /* A quote.  Skip past, and toggle quotedness. */
356
357         { p++; qstate = !qstate; }
358
359       else if (*p == '\\') {
360         /* A backslash.  Just emit the following character. */
361
362         p++; if (p >= l) lose("%s:2: unfinished `\\' escape", script);
363         *q++ = *p++;
364
365       } else if (*p == '\'') {
366         /* A single quote.  Find its matching end quote, and emit everything
367          * in between.
368          */
369
370         p++; r = strchr(p, '\'');
371         if (!r || r > l) lose("%s:2: missing `''", script);
372         n = r - p; memmove(q, p, n); q += n; p = r + 1;
373
374       } else {
375         /* An ordinary constituent.  Gather a bunch of these up and emit them
376          * all.
377          */
378         n = strcspn(p, qstate ? "\"\\" : "\"'\\ \f\n\r\t\v");
379         if (n > l - p) n = l - p;
380         memmove(q, p, n); q += n; p += n;
381       }
382     }
383
384     /* Check that we're not still inside quotes. */
385     if (qstate) lose("%s:2: missing `\"'", script);
386
387     /* Finish off this word and prepare to start the next. */
388     *q++ = 0; if (p < l) p++;
389   }
390
391   /* Parse the arguments we've collected as options.  Object if we find
392    * positional arguments.
393    */
394   flags = (flags&~AF_STATEMASK) | AF_EMBED;
395   parse_options(av.n, (char * /*unconst*/*)av.v);
396   if (optind < av.n)
397     lose("%s:2: positional argument `%s' not permitted here",
398          script, av.v[optind]);
399
400 end:
401   /* Tidy up. */
402   if (fp) {
403     if (ferror(fp))
404       lose("error reading script `%s': %s", script, strerror(errno));
405     fclose(fp);
406   }
407   dstr_release(&d); argv_release(&av);
408 }
409
410 /* Main program. */
411 int main(int argc, char *argv[])
412 {
413   struct config_section_iter si;
414   struct config_section *sect;
415   struct config_var *var;
416   struct lispsys_list order;
417   struct lispsys *lisp, **tail;
418   const char *p;
419   const char *script;
420   struct dstr d = DSTR_INIT;
421   struct argv av = ARGV_INIT;
422
423   /* initial setup. */
424   set_progname(argv[0]);
425   init_config();
426
427   /* Parse the command-line options. */
428   flags = (flags&~AF_STATEMASK) | AF_CMDLINE;
429   parse_options(argc - 1, argv + 1); optind++;
430
431   /* We now know enough to decide whether we're in eval or script mode.  In
432    * the former case, don't check for embedded options (it won't work because
433    * we don't know where the `eval.lisp' script is yet, and besides, there
434    * aren't any).  In the latter case, pick out the script name, leaving the
435    * remaining positional arguments for later.
436    */
437   if (argv_tail.n) { flags |= AF_NOEMBED; script = 0; }
438   else if (optind < argc) script = argv[optind++];
439   else flags |= AF_BOGUS;
440
441   /* Check that everything worked. */
442   if (flags&AF_BOGUS) { usage(stderr); exit(127); }
443
444   /* Reestablish ARGC/ARGV to refer to the tail of positional arguments to be
445    * passed onto the eventual script.  For eval mode, that includes the
446    * operations already queued up, so we'll have to accumulate everything in
447    * `argv_tail'.
448    */
449   argc -= optind; argv += optind;
450   if (argv_tail.n) {
451     argv_append(&argv_tail, "--");
452     argv_appendn(&argv_tail, argv, argc);
453     argc = argv_tail.n; argv = argv_tail.v;
454   }
455
456   /* Fetch embedded options. */
457   if (!(flags&AF_NOEMBED)) handle_embedded_args(script);
458
459   /* Load default configuration if no explicit files were requested. */
460   if (!(flags&AF_SETCONF)) load_default_config();
461
462   /* Determine the preferred Lisp systems.  Check the environment first;
463    * otherwise use the configuration file.
464    */
465   p = my_getenv("RUNLISP_PREFER", 0);
466   if (!p) {
467     var = config_find_var(&config, toplevel, CF_INHERIT, "prefer");
468     if (var) {
469       dstr_reset(&d);
470       config_subst_var(&config, toplevel, var, &d); p = d.p;
471     }
472   }
473   if (p)
474     add_lispsys(p, "preferred", &prefer, LF_PREFER,
475                 offsetof(struct lispsys, next_prefer));
476
477   /* If we're in eval mode, then find the `eval.lisp' script. */
478   if (!script)
479     script = config_subst_string_alloc
480       (&config, common, "<internal>",
481        "${@ENV:RUNLISP_EVAL?"
482          "${@CONFIG:eval-script?"
483            "${@data-dir}/eval.lisp}}");
484
485   /* We now have the script name, so publish it for `uiop'.
486    *
487    * As an aside, this is a terrible interface.  It's too easy to forget to
488    * set it.  (To illustrate this, `cl-launch -x' indeed forgets to set it.)
489    * If you're lucky, the script just thinks that its argument is `nil', in
490    * which case maybe it can use `*load-pathname*' as a fallback.  If you're
491    * unlucky, your script was invoked (possibly indirectly) by another
492    * script, and now you've accidentally inherited the calling script's name.
493    *
494    * It would have been far better simply to repeat the script name as the
495    * first user argument, if nothing else had come readily to mind.
496    */
497   if (setenv("__CL_ARGV0", script, 1))
498     lose("failed to set script-name environment variable");
499
500   /* And publish it in the configuration for the `run-script' commands. */
501   config_set_var(&config, builtin, CF_LITERAL, "@script", script);
502
503   /* Dump the final configuration if we're being very verbose. */
504   if (verbose >= 5) dump_config();
505
506   /* Identify the configuration sections which correspond to actual Lisp
507    * system definitions, and gather them into the `known' list.
508    */
509   tail = lisps.tail;
510   for (config_start_section_iter(&config, &si);
511        (sect = config_next_section(&si)); ) {
512     var = config_find_var(&config, sect, CF_INHERIT, "run-script");
513     if (!var) continue;
514     lisp = ensure_lispsys(CONFIG_SECTION_NAME(sect),
515                           CONFIG_SECTION_NAMELEN(sect));
516     lisp->f |= LF_KNOWN; lisp->sect = sect; lisp->var = var;
517     *tail = lisp; tail = &lisp->next_lisp;
518   }
519   *tail = 0; lisps.tail = tail;
520
521   /* Make sure that the acceptable and preferred Lisps actually exist. */
522   check_lisps("acceptable", &accept, offsetof(struct lispsys, next_accept));
523   check_lisps("preferred", &prefer, offsetof(struct lispsys, next_prefer));
524
525   /* If there are no acceptable Lisps, then we'll take all of them. */
526   if (!accept.head) {
527     if (verbose >= 2)
528       moan("no explicitly acceptable implementations: allowing all");
529     tail = accept.tail;
530     for (lisp = lisps.head; lisp; lisp = lisp->next_lisp)
531       { lisp->f |= LF_ACCEPT; *tail = lisp; tail = &lisp->next_accept; }
532     *tail = 0; accept.tail = tail;
533   }
534
535   /* Build the final list of Lisp systems in the order in which we'll try
536    * them: first, preferred Lisps which are acceptable, and then acceptable
537    * Lisps which aren't preferred.
538    */
539   tail = &order.head;
540   for (lisp = prefer.head; lisp; lisp = lisp->next_prefer)
541     if (lisp->f&LF_ACCEPT) { *tail = lisp; tail = &lisp->next_order; }
542   for (lisp = accept.head; lisp; lisp = lisp->next_accept)
543     if (!(lisp->f&LF_PREFER)) { *tail = lisp; tail = &lisp->next_order; }
544   *tail = 0;
545
546   /* Maybe dump out the various lists of Lisp systems we've collected. */
547   if (verbose >= 4)
548     dump_lisps("known Lisps", &lisps, offsetof(struct lispsys, next_lisp));
549   if (verbose >= 3) {
550     dump_lisps("acceptable Lisps", &accept,
551                offsetof(struct lispsys, next_accept));
552     dump_lisps("preferred Lisps", &prefer,
553                offsetof(struct lispsys, next_prefer));
554     dump_lisps("overall preference order", &order,
555                offsetof(struct lispsys, next_order));
556   }
557
558   /* Try to actually run the script. */
559   for (lisp = order.head; lisp; lisp = lisp->next_order) {
560     /* Try each of the selected systems in turn. */
561
562     /* See whether there's a custom image file.  If so, set `@image' in the
563      * system's configuration section.
564      */
565     if (!(flags&AF_VANILLA) &&
566         config_find_var(&config, lisp->sect, CF_INHERIT, "image-file")) {
567       var = config_find_var(&config, lisp->sect, CF_INHERIT, "image-path");
568       dstr_reset(&d); config_subst_var(&config, lisp->sect, var, &d);
569       if (file_exists_p(d.p, verbose >= 2 ? FEF_VERBOSE : 0))
570         config_set_var(&config, lisp->sect, CF_LITERAL, "@image", "t");
571     }
572
573     /* Build the command line from `run-script'. */
574     argv_reset(&av);
575     config_subst_split_var(&config, lisp->sect, lisp->var, &av);
576     if (!av.n) {
577       moan("empty command for Lisp implementation `%s'", LISPSYS_NAME(lisp));
578       continue;
579     }
580
581     /* Append our additional positional arguments. */
582     argv_appendn(&av, argv, argc);
583
584     /* Try to run the Lisp system. */
585     if (!try_exec(&av,
586                   (flags&AF_DRYRUN ? TEF_DRYRUN : 0) |
587                     (verbose >= 2 ? TEF_VERBOSE : 0)))
588       return (0);
589   }
590
591   /* No.  Much errors.  So failure.  Very sadness. */
592   lose("no acceptable Lisp systems found");
593 }
594
595 /*----- That's all, folks -------------------------------------------------*/