3 * Common definitions for `runlisp'
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/>.
33 /*----- Header files ------------------------------------------------------*/
40 /*----- Handy macros ------------------------------------------------------*/
42 #define N(v) (sizeof(v)/sizeof((v)[0]))
43 /* The number of elements in the array V. */
45 /* Figure out the compiler version to see whether fancy tricks will work. */
47 # define GCC_VERSION_P(maj, min) \
48 (__GNUC__ > (maj) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
50 # define GCC_VERSION_P(maj, min) 0
54 # define CLANG_VERSION_P(maj, min) \
55 (__clang_major__ > (maj) || (__clang_major__ == (maj) && \
56 __clang_minor__ >= (min)))
58 # define CLANG_VERSION_P(maj, min) 0
61 #if GCC_VERSION_P(2, 5) || CLANG_VERSION_P(3, 3)
63 # define NORETURN __attribute__((__noreturn__))
64 /* Mark a function as not returning. */
66 # define PRINTF_LIKE(fix, aix) __attribute__((__format__(printf, fix, aix)))
67 /* Mark a function as accepting a printf(3)-like format string as
68 * argument FIX, with arguments to be substituted starting at AIX.
72 #if GCC_VERSION_P(4, 0) || CLANG_VERSION_P(3, 3)
74 # define EXECL_LIKE(ntrail) __attribute__((__sentinel__(ntrail)))
75 /* Mark a function as expecting a variable number of arguments
76 * terminated by a null pointer, followed by NTRAIL further
82 /* Couldn't detect fancy compiler features. We'll have to make do
89 # define PRINTF_LIKE(fix, aix)
92 # define EXECL_LIKE(ntrail)
95 #define DISCARD(x) do if (x); while (0)
96 /* Discard the result of evaluating expression X, without upsetting
100 #define END ((const char *)0)
101 /* A null pointer to terminate the argument tail to an `EXECL_LIKE'
102 * function. (Note that `NULL' is /not/ adequate for this purpose,
103 * since it might expand simply to `0', which is an integer, not a
104 * pointer, and might well be the wrong size and/or value.)
107 /* Wrap up <ctype.h> macros with explicit conversions to `unsigned char'. */
108 #define CTYPE_HACK(func, ch) (func((unsigned char)(ch)))
109 #define ISSPACE(ch) CTYPE_HACK(isspace, ch)
110 #define ISALNUM(ch) CTYPE_HACK(isalnum, ch)
111 #define ISXDIGIT(ch) CTYPE_HACK(isxdigit, ch)
112 #define TOLOWER(ch) CTYPE_HACK(tolower, ch)
113 #define TOUPPER(ch) CTYPE_HACK(toupper, ch)
115 /* Wrap up comparison functions to take an ordering relation as part of their
116 * syntax. This makes it much harder to screw up.
118 #define MEMCMP(x, op, y, n) (memcmp((x), (y), (n)) op 0)
119 #define STRCMP(x, op, y) (strcmp((x), (y)) op 0)
120 #define STRNCMP(x, op, y, n) (strncmp((x), (y), (n)) op 0)
123 # define SIZE_MAX (-(size_t)1)
125 /* The largest value that can be stored in an object of type
126 * `size_t'. A proper <limits.h> setting would be a preprocessor-
127 * time constant, but we don't actually need that.
130 /*----- Diagnostic utilities ----------------------------------------------*/
132 extern const char *progname;
133 /* Our program name, for use in error messages. */
135 extern void set_progname(const char */*prog*/);
136 /* Set `progname' from the pathname in PROG (typically from
140 extern void vmoan(const char */*msg*/, va_list /*ap*/);
141 /* Report an error or warning in Unix style, given a captured
145 extern PRINTF_LIKE(1, 2) void moan(const char */*msg*/, ...);
146 /* Issue a warning message. */
148 extern NORETURN PRINTF_LIKE(1, 2) void lose(const char */*msg*/, ...);
149 /* Issue a fatal error message and exit unsuccessfully. */
151 /*----- Memory allocation -------------------------------------------------*/
153 extern void *xmalloc(size_t /*n*/);
154 /* Allocate and return a pointer to N bytes, or report a fatal error.
156 * Release the pointer using `free' as usual. If N is zero, returns
157 * null (but you are not expected to check for this).
160 extern void *xrealloc(void */*p*/, size_t /*n*/);
161 /* Resize the block at P (from `malloc' or `xmalloc') to be N bytes
164 * The block might (and probably will) move, so it returns the new
165 * address. If N is zero, then the block is freed (if necessary) and
166 * a null pointer returned; otherwise, if P is null then a fresh
167 * block is allocated. If allocation fails, then a fatal error is
171 extern char *xstrndup(const char */*p*/, size_t /*n*/);
172 /* Allocate and return a copy of the N-byte string starting at P.
174 * The new string is null-terminated, though P need not be. If
175 * allocation fails, then a fatal error is reported.
178 extern char *xstrdup(const char */*p*/);
179 /* Allocate and return a copy of the null-terminated string starting
182 * If allocation fails, then a fatal error is reported.
185 /*----- Dynamic strings ---------------------------------------------------*/
189 * Note that the string might not be null-terminated.
192 char *p; /* string base address */
193 size_t len; /* current string length */
194 size_t sz; /* allocated size of buffer */
196 #define DSTR_INIT { 0, 0, 0 }
198 extern void dstr_init(struct dstr */*d*/);
199 /* Initialize the string D.
201 * Usually you'd use the static initializer `DSTR_INIT'.
204 extern void dstr_reset(struct dstr */*d*/);
205 /* Reset string D so it's empty again. */
207 extern void dstr_ensure(struct dstr */*d*/, size_t /*n*/);
208 /* Ensure that D has at least N unused bytes available. */
210 extern void dstr_release(struct dstr */*d*/);
211 /* Release the memory held by D.
213 * It must be reinitialized (e.g., by `dstr_init') before it can be
217 extern void dstr_putm(struct dstr */*d*/, const void */*p*/, size_t /*n*/);
218 /* Append the N-byte string at P to D.
220 * P need not be null-terminated. D will not be null-terminated
224 extern void dstr_puts(struct dstr */*d*/, const char */*p*/);
225 /* Append the null-terminated string P to D.
227 * D /is/ guaranteed to be null-terminated after this.
230 extern void dstr_putc(struct dstr */*d*/, int /*ch*/);
231 /* Append the single character CH to D.
233 * D will not be null-terminated afterwards.
236 extern void dstr_putcn(struct dstr */*d*/, int /*ch*/, size_t /*n*/);
237 /* Append N copies of the character CH to D.
239 * D will not be null-terminated afterwards.
242 extern void dstr_putz(struct dstr */*d*/);
243 /* Null-terminate the string D.
245 * This doesn't change the length of D. If further stuff is appended
246 * then the null terminator will be overwritten.
249 extern void dstr_vputf(struct dstr */*d*/,
250 const char */*p*/, va_list /*ap*/);
251 /* Append stuff to D, determined by printf(3) format string P and
254 * D will not be null-terminated afterwards.
257 extern PRINTF_LIKE(2, 3)
258 void dstr_putf(struct dstr */*d*/, const char */*p*/, ...);
259 /* Append stuff to D, determined by printf(3) format string P and
262 * D will not be null-terminated afterwards.
265 extern int dstr_readline(struct dstr */*d*/, FILE */*fp*/);
266 /* Append the next input line from FP to D.
268 * Return 0 on success, or -1 if reading immediately fails or
269 * encounters end-of-file (call ferror(3) to distinguish). Any
270 * trailing newline is discarded: it is not possible to determine
271 * whether the last line was ended with a newline. D is guaranteed
272 * to be null-terminated afterwards.
275 /*----- Dynamic vectors of strings ----------------------------------------*/
277 /* A dynamic vector of strings.
279 * This machinery only actually tracks character pointers. It assumes that
280 * the caller will manage the underlying storage for the strings.
282 * Note that `v' always points to the first element in the vector. The
283 * underlying storage starts `o' slots before this.
286 char **v; /* pointer the first element */
287 size_t n; /* length of the vector */
288 size_t o; /* number of spare slots at start */
289 size_t sz; /* allocated size (in slots) */
291 #define ARGV_INIT { 0, 0, 0, 0 }
293 extern void argv_init(struct argv */*a*/v);
294 /* Initialize the vector AV.
296 * Usually you'd use the static initializer `ARGV_INIT'.
299 extern void argv_reset(struct argv */*av*/);
300 /* Reset the vector AV so that it's empty again. */
302 extern void argv_ensure(struct argv */*av*/, size_t /*n*/);
303 /* Ensure that AV has at least N unused slots at the end. */
305 extern void argv_ensure_offset(struct argv */*av*/, size_t /*n*/);
306 /* Ensure that AV has at least N unused slots at the /start/. */
308 extern void argv_release(struct argv */*av*/);
309 /* Release the memory held by AV.
311 * It must be reinitialized (e.g., by `argv_init') before it can be
315 extern void argv_append(struct argv */*av*/, char */*p*/);
316 /* Append the pointer P to AV. */
318 extern void argv_appendz(struct argv */*av*/);
319 /* Append a null pointer to AV, without extending the vactor length.
321 * The null pointer will be overwritten when the next string is
325 extern void argv_appendn(struct argv */*av*/,
326 char *const */*v*/, size_t /*n*/);
327 /* Append a N-element vector V of pointers to AV. */
329 extern void argv_appendav(struct argv */*av*/, const struct argv */*bv*/);
330 /* Append the variable-length vector BV to AV. */
332 extern void argv_appendv(struct argv */*av*/, va_list /*ap*/);
333 /* Append the pointers from a variable-length argument list AP to AV.
335 * The list is terminated by a null pointer.
338 extern EXECL_LIKE(0) void argv_appendl(struct argv */*av*/, ...);
339 /* Append the argument pointers, terminated by a null pointer, to
343 extern void argv_prepend(struct argv */*av*/, char */*p*/);
344 /* Prepend the pointer P to AV. */
346 extern void argv_prependn(struct argv */*av*/,
347 char *const */*v*/, size_t /*n*/);
348 /* Prepend a N-element vector V of pointers to AV. */
350 extern void argv_prependav(struct argv */*av*/, const struct argv */*bv*/);
351 /* Prepend the variable-length vector BV to AV. */
353 extern void argv_prependv(struct argv */*av*/, va_list /*ap*/);
354 /* Prepend the pointers from a variable-length argument list AP to
357 * The list is terminated by a null pointer.
360 extern EXECL_LIKE(0) void argv_prependl(struct argv */*av*/, ...);
361 /* Prepend the argument pointers, terminated by a null pointer, to
365 /*----- Treaps ------------------------------------------------------------*/
367 /* A `treap' is a data structure for associating values with keys. This
368 * implementation assumes that keys are simply text strings.
371 struct treap_node *root;
373 #define TREAP_INIT { 0 }
375 /* A treap is a combination of a binary search tree and a binary heap. The
376 * nodes are ordered according to the search keys, in the usual way, so that
377 * all the keys in a node's left subtree precede that node's key, and all of
378 * the keys in its right subtree follow the node's key. The trick is that
379 * the tree must /also/ satisfy the heap condition regarding randomly
380 * assigned `weights' attached to each node: so a node's weight must not be
381 * less than their weight of either of its children.
383 * This combination uniquely determines the structure of the tree, except for
384 * nodes whose weights exactly match one (or both) of their children. (The
385 * root must be the heaviest node in the tree. The root's key splits the
386 * remaining nodes into left and right subtrees, whose structure is then
387 * uniquely determined by induction.)
389 * This is an /intrusive/ data structure. A caller is expected to include a
390 * `struct treap_node' as (probably) the initial part of a larger structure.
393 unsigned wt; /* weight (randomly assigned) */
394 struct treap_node *left, *right; /* left and right subtrees */
395 char *k; size_t kn; /* key pointer and length */
397 #define TREAP_NODE_KEY(n) (((const struct treap_node *)(n))->k + 0)
398 #define TREAP_NODE_KEYLEN(n) (((const struct treap_node *)(n))->kn + 0)
400 /* We can't allocate nodes ourselves, because only the caller knows how.
401 * Instead, insertion is split into two operations: `treap_probe' looks to
402 * see whether a matching node is already in the treap, and returns it if so;
403 * otherwise, it fills in this `treap_path' structure, which is passed back
404 * to `treap_insert' to help it add the fresh node into the treap. (See the
405 * commentary in `treap_probe' and `treap_insert' for the details.)
407 #define TREAP_PATHMAX 64
409 struct treap_node **path[TREAP_PATHMAX];
413 /* An external iterator for a treap. (See the commentary for
414 * `treap_start_iter' and `treap_next' for the details.)
417 struct treap_node *stack[TREAP_PATHMAX];
421 extern void treap_init(struct treap */*t*/);
422 /* Initialize the treap T.
424 * Usually you'd use the static initializer `TREAP_INIT'.
427 extern void *treap_lookup(const struct treap */*t*/,
428 const char */*k*/, size_t /*kn*/);
429 /* Look up the KN-byte key K in the treap T.
431 * Return a pointer to the matching node if one was found, or null
435 extern void *treap_probe(struct treap */*t*/,
436 const char */*k*/, size_t /*kn*/,
437 struct treap_path */*p*/);
438 /* Look up the KN-byte K in the treap T, recording a path in P.
440 * This is similar to `treap_lookup', in that it returns the
441 * requested node if it already exists, or null otherwise, but it
442 * also records in P information to be used by `treap_insert' to
443 * insert a new node with the given key if it's not there already.
446 extern void treap_insert(struct treap */*t*/, const struct treap_path */*p*/,
447 struct treap_node */*n*/,
448 const char */*k*/, size_t /*kn*/);
449 /* Insert a new node N into T, associating it with the KN-byte key K.
451 * Use the path data P, from `treap_probe', to help with insertion.
454 extern void *treap_remove(struct treap */*t*/,
455 const char */*k*/, size_t /*kn*/);
456 /* Remove the node with the KN-byte K from T.
458 * Return the address of the node we removed, or null if it couldn't
462 extern void treap_start_iter(struct treap */*t*/, struct treap_iter */*i*/);
463 /* Initialize an iterator I over T's nodes. */
465 extern void *treap_next(struct treap_iter */*i*/);
466 /* Return the next node from I, in ascending order by key.
468 * If there are no more nodes, then return null.
471 extern void treap_check(struct treap */*t*/);
472 /* Check the treap structure rules for T. */
474 extern void treap_dump(struct treap */*t*/);
475 /* Dump the treap T to standard output, for debugging purposes. */
477 /*----- Configuration file parsing ----------------------------------------*/
479 /* A configuration file. */
481 struct treap sections; /* treap of sections */
482 struct config_section *head, **tail; /* section list, in creation order */
483 struct config_section *fallback; /* default parent section */
485 #define CONFIG_INIT { TREAP_INIT, 0, 0 }
487 /* A configuration section. */
488 struct config_section {
489 struct treap_node _node; /* treap intrustion */
490 struct config_section *next; /* next section in creation order */
491 struct config_section **parents; size_t nparents; /* vector of parents */
492 struct treap vars; /* treap of variables */
493 struct treap cache; /* inheritance cache */
495 #define CONFIG_SECTION_NAME(sect) TREAP_NODE_KEY(sect)
496 #define CONFIG_SECTION_NAMELEN(sect) TREAP_NODE_KEYLEN(sect)
498 /* An entry in a section's inheritance cache: see `search_recursive' for
501 struct config_cache_entry {
502 struct treap_node _node; /* treap intrusion */
503 unsigned f; /* flags */
504 #define CF_OPEN 1u /* traps inheritance cycles */
505 struct config_var *var; /* pointer to inherited variable */
508 /* A configuration variable. */
510 struct treap_node _node; /* treap intrusion */
511 char *file; unsigned line; /* source location, or null/0 */
512 char *val; size_t n; /* value pointer and length */
513 unsigned f; /* flags */
514 #define CF_LITERAL 1u /* value should not be expanded */
515 #define CF_EXPAND 2u /* traps expansion cycles */
516 #define CF_OVERRIDE 4u /* override settings from files */
518 #define CONFIG_VAR_NAME(var) TREAP_NODE_KEY(var)
519 #define CONFIG_VAR_NAMELEN(var) TREAP_NODE_KEYLEN(var)
521 /* A section iterator.
523 * (Sections are visited in the order in which they were created.)
525 struct config_section_iter {
526 struct config_section *sect; /* next section to return */
529 /* A variable iterator.
531 * (Variables are visited in lexicographical order.)
533 struct config_var_iter {
538 #define CF_CREAT 1u /* create section or variable */
539 #define CF_INHERIT 2u /* look up variable in parents */
541 extern void config_init(struct config */*conf*/);
542 /* Initialize the configuration state CONF.
544 * Usually you'd use the static initializer `CONFIG_INIT'.
547 extern struct config_section *config_find_section(struct config */*conf*/,
549 const char */*name*/);
550 /* Find and return the section with null-terminated NAME in CONF.
552 * If no section is found, the behaviour depends on whether
553 * `CF_CREAT' is set in F: if so, an empty section is created and
554 * returned; otherwise, a null pointer is returned.
557 extern struct config_section *config_find_section_n(struct config */*conf*/,
559 const char */*name*/,
561 /* Find and return the section with the given SZ-byte NAME in CONF.
563 * This works like `config_find_section', but with an explicit length
564 * for the NAME rather than null-termination.
567 extern void config_set_fallback(struct config */*conf*/,
568 struct config_section */*sect*/);
569 /* Set the fallback section for CONF to be SECT.
571 * That is, if a section has no explicit parents, then by default it
572 * will have a single parent which is SECT. If SECT is null then
573 * there is no fallback section, and sections which don't have
574 * explicitly specified parents have no parents at all. (This is the
575 * default situation.)
578 extern void config_set_parent(struct config_section */*sect*/,
579 struct config_section */*parent*/);
580 /* Arrange that SECT has PARENT as its single parent section.
582 * If PARENT is null, then arrange that SECT has no parents at all.
583 * In either case, any `@parents' setting will be ignored.
586 extern void config_start_section_iter(struct config */*conf*/,
587 struct config_section_iter */*i*/);
588 /* Initialize I to iterate over the sections defined in CONF. */
590 extern struct config_section *config_next_section
591 (struct config_section_iter */*i*/);
592 /* Return the next section from I, in order of creation.
594 * If there are no more sections, then return null.
597 extern struct config_var *config_find_var(struct config */*conf*/,
598 struct config_section */*sect*/,
600 const char */*name*/);
601 /* Find and return the variable with null-terminated NAME in SECT.
603 * If `CF_INHERIT' is set in F, then the function searches the
604 * section's parents recursively; otherwise, it only checks to see
605 * whether the variable is set directly in SECT.
607 * If no variable is found, the behaviour depends on whether
608 * `CF_CREAT' is set in F: if so, an empty variable is created and
609 * returned; otherwise, a null pointer is returned.
611 * Setting both `CF_INHERIT' and `CF_CREAT' is not useful.
614 extern struct config_var *config_find_var_n(struct config */*conf*/,
615 struct config_section */*sect*/,
617 const char */*name*/,
619 /* Find and return the variable with the given SZ-byte NAME in SECT.
621 * This works like `config_find_var', but with an explicit length for
622 * the NAME rather than null-termination.
625 extern struct config_var *config_set_var(struct config */*conf*/,
626 struct config_section */*sect*/,
628 const char */*name*/,
629 const char */*value*/);
630 /* Set variable NAME to VALUE in SECT, with associated flags F.
632 * The names are null-terminated. The flags are variable flags: see
633 * `struct config_var' for details. Returns the variable.
635 * If the variable is already set and has the `CF_OVERRIDE' flag,
636 * then this function does nothing unless `CF_OVERRIDE' is /also/ set
640 extern struct config_var *config_set_var_n(struct config */*conf*/,
641 struct config_section */*sect*/,
643 const char */*name*/,
645 const char */*value*/,
646 size_t /*valuelen*/);
647 /* As `config_set_var', except that the variable NAME and VALUE have
648 * explicit lengths (NAMELEN and VALUELEN, respectively) rather than
649 * being null-terminated.
652 extern void config_start_var_iter(struct config */*conf*/,
653 struct config_section */*sect*/,
654 struct config_var_iter */*i*/);
655 /* Initialize I to iterate over the variables directly defined in
659 extern struct config_var *config_next_var(struct config_var_iter */*i*/);
660 /* Return next variable from I, in ascending lexicographical order.
662 * If there are no more variables, then return null.
665 extern int config_read_file(struct config */*conf*/, const char */*file*/,
667 #define CF_NOENTOK 1u
668 /* Read and parse configuration FILE, applying its settings to CONF.
670 * If all goes well, the function returns 0. If the file is not
671 * found, then the behaviour depends on whether `CF_NOENTOK' is set
672 * in F: if so, then the function simply returns -1. Otherwise, a
673 * fatal error is reported. Note that this /only/ applies if the
674 * file does not exist (specifically, opening it fails with `ENOENT')
675 * -- any other problems are reported as fatal errors regardless of
679 extern void config_read_env(struct config */*conf*/,
680 struct config_section */*sect*/);
681 /* Populate SECT with environment variables.
683 * Environment variables are always set with `CF_LITERAL'.
686 extern void config_subst_string(struct config */*config*/,
687 struct config_section */*home*/,
688 const char */*what*/,
689 const char */*p*/, struct dstr */*d*/);
690 /* Expand substitutions in a string.
692 * Expand the null-terminated string P relative to the HOME section,
693 * using configuration CONFIG, and appending the result to dynamic
694 * string D. Blame WHAT in any error messages.
697 extern char *config_subst_string_alloc(struct config */*config*/,
698 struct config_section */*home*/,
699 const char */*what*/,
701 /* Expand substitutions in a string.
703 * Expand the null-terminated string P relative to the HOME section,
704 * using configuration CONFIG, returning the result as a freshly
705 * malloc(3)ed string. Blame WHAT in any error messages.
708 extern void config_subst_var(struct config */*config*/,
709 struct config_section */*home*/,
710 struct config_var */*var*/,
712 /* Expand substitutions in a variable.
714 * Expand the value of the variable VAR relative to the HOME section,
715 * using configuration CONFIG, appending the result to dynamic string
719 extern char *config_subst_var_alloc(struct config */*config*/,
720 struct config_section */*home*/,
721 struct config_var */*var*/);
722 /* Expand substitutions in a variable.
724 * Expand the value of the variable VAR relative to the HOME section,
725 * using configuration CONFIG, returning the result as a freshly
726 * malloc(3)ed string.
729 extern void config_subst_split_var(struct config */*config*/,
730 struct config_section */*home*/,
731 struct config_var */*var*/,
732 struct argv */*av*/);
733 /* Expand substitutions in a variable and split into words.
735 * Expand and word-split the value of the variable VAR relative to
736 * the HOME section, using configuration CONFIG, appending the
737 * resulting words into the vector AV.
740 /*----- That's all, folks -------------------------------------------------*/