| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Common functionality of a less principled nature |
| 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 | #ifndef COMMON_H |
| 27 | #define COMMON_H |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
| 33 | /*----- Externally defined types ------------------------------------------*/ |
| 34 | |
| 35 | struct dstr; |
| 36 | struct argv; |
| 37 | |
| 38 | /*----- Public variables --------------------------------------------------*/ |
| 39 | |
| 40 | extern struct config config; |
| 41 | extern struct config_section *toplevel, *builtin, *common, *env; |
| 42 | extern unsigned verbose; |
| 43 | |
| 44 | /*----- Functions provided ------------------------------------------------*/ |
| 45 | |
| 46 | extern const char *my_getenv(const char */*name*/, const char */*dflt*/); |
| 47 | /* Look up the environment variable NAME. |
| 48 | * |
| 49 | * If it's found, return the value; otherwise return DFLT. This |
| 50 | * function looks up the environment variable in the `@ENV' |
| 51 | * configuration section, so (a) it's likely more efficient than |
| 52 | * getenv(3), and (b) the `init_config' function must have been |
| 53 | * called earlier. |
| 54 | */ |
| 55 | |
| 56 | extern long parse_int(const char */*what*/, const char */*p*/, |
| 57 | long /*min*/, long /*max*/); |
| 58 | /* Parse and return an integer from the string P. |
| 59 | * |
| 60 | * Report an error if the string doesn't look like an integer, or if |
| 61 | * it's not between MIN and MAX (inclusive). Qualify error messages |
| 62 | * using the adjective WHAT. |
| 63 | */ |
| 64 | |
| 65 | extern void argv_string(struct dstr */*d*/, const struct argv */*av*/); |
| 66 | /* Format string-vector AV as a sequence of possibly-quoted words. |
| 67 | * |
| 68 | * Append the resulting list to D. |
| 69 | */ |
| 70 | |
| 71 | extern int file_exists_p(const char */*path*/, unsigned /*f*/); |
| 72 | #define FEF_EXEC 1u |
| 73 | #define FEF_VERBOSE 2u |
| 74 | /* Return whether PATH names an existing file. |
| 75 | * |
| 76 | * This will return zero if PATH names something which isn't a |
| 77 | * regular file. If `FEF_EXEC' is set in F, then additionally ensure |
| 78 | * that it's executable by the (real) calling uid. If `FEF_VERBOSE' |
| 79 | * is set in F, then report on the outcome of the check to standard |
| 80 | * error. |
| 81 | */ |
| 82 | |
| 83 | extern int found_in_path_p(const char */*prog*/, unsigned /*f*/); |
| 84 | /* Return whether PROG can be found in the `PATH'. |
| 85 | * |
| 86 | * If PROG is a pathname (absolute or relative -- i.e., if it |
| 87 | * contains a `/'), then just check that it names an executable |
| 88 | * program. Otherwise check to see whether `DIR/PROG' exists and is |
| 89 | * executable for any DIR in the `PATH'. The flags F are as for |
| 90 | * `file_exists_p'. |
| 91 | */ |
| 92 | |
| 93 | extern int try_exec(struct argv */*av*/, unsigned /*f*/); |
| 94 | #define TEF_DRYRUN 1u |
| 95 | #define TEF_VERBOSE 2u |
| 96 | /* Try to run a program as indicated by the argument list AV. |
| 97 | * |
| 98 | * This is essentially execvp(3). If `TEF_VERBOSE' is set in F then |
| 99 | * trace what's going on to standard error. If `TEF_DRYRUN' is set |
| 100 | * in F then don't actually try to run the program: just check |
| 101 | * whether it exists and is vaguely plausible. Return -1 if there |
| 102 | * was a problem, or 0 if it was successful but didn't actually run |
| 103 | * the program because of the flags settings. |
| 104 | */ |
| 105 | |
| 106 | extern void init_config(void); |
| 107 | /* Initialize the configuration machinery. |
| 108 | * |
| 109 | * This establishes the standard configuration sections `@CONFIG', |
| 110 | * `@BUILTIN', `@COMMON', and `@ENV', setting the corresponding |
| 111 | * global variables, and populates `@BUILTIN' (from compile-time |
| 112 | * configuration) and `@ENV' (from the environment variables). |
| 113 | */ |
| 114 | |
| 115 | extern void read_config_file(const char */*what*/, |
| 116 | const char */*file*/, unsigned /*f*/); |
| 117 | /* Read a named configuration FILE. |
| 118 | * |
| 119 | * WHAT is an adjective describing the configuration file, to be used |
| 120 | * in diagnostics; FILE is the actual filename to read; and F holds |
| 121 | * `CF_...' flags for `config_read_file', which actually does most |
| 122 | * of the work. |
| 123 | */ |
| 124 | |
| 125 | extern void read_config_dir(const char */*what*/, |
| 126 | const char */*path*/, unsigned /*f*/); |
| 127 | /* Read all of the configuration files in directory PATH. |
| 128 | * |
| 129 | * WHAT is an adjective describing the configuration directory, to be |
| 130 | * used in diagnostics; FILE is the actual filename to read; and F |
| 131 | * holds `CF_...' flags for `config_read_file', which actually reads |
| 132 | * the files. |
| 133 | * |
| 134 | * All of the files named `*.conf' in the directory are read, in |
| 135 | * ascending lexicographical order by name. If `CF_NOENTOK' is set |
| 136 | * in F, then ignore an error explaining that the directory doesn't |
| 137 | * exist. (This only ignores `ENOENT': any other problem is still a |
| 138 | * fatal error.) |
| 139 | */ |
| 140 | |
| 141 | extern void read_config_path(const char */*path*/, unsigned /*f*/); |
| 142 | /* Read configuration from a file or directory PATH. |
| 143 | * |
| 144 | * If PATH exists and names a directory then process all of the files |
| 145 | * within, as for `read_config_dir'; otherwise try to read it as a |
| 146 | * file, as for `read_config_file'. The flags F are passed to the |
| 147 | * respective function. |
| 148 | */ |
| 149 | |
| 150 | extern int set_config_var(const char */*assign*/); |
| 151 | /* Apply a configuration variable setting in command-line syntax. |
| 152 | * |
| 153 | * ASSIGN should be a string in the form `[SECT:]VAR=VALUE'. Set VAR |
| 154 | * to VALUE in section SECT (defaults to `@CONFIG'). The variable is |
| 155 | * set with `CF_OVERRIDE' set to prevent the setting from being |
| 156 | * overwritten by a configuration file. |
| 157 | */ |
| 158 | |
| 159 | extern void load_default_config(void); |
| 160 | /* Load the default configuration files. |
| 161 | * |
| 162 | * This will read `ETCDIR/runlisp.d/*.conf', `ETCDIR/runlisp.conf', |
| 163 | * `~/.runlisp.conf', and `~/.config/runlisp.conf'. |
| 164 | */ |
| 165 | |
| 166 | extern void dump_config(void); |
| 167 | /* Dump the configuration to standard error. */ |
| 168 | |
| 169 | /*----- That's all, folks -------------------------------------------------*/ |
| 170 | |
| 171 | #ifdef __cplusplus |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | #endif |