aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

main.c (8431B)


      1 #include <ctype.h>
      2 #include <errno.h>
      3 #include <signal.h>
      4 #include <stdarg.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 
      9 #include "config.h"
     10 #include "make.h"
     11 
     12 #ifndef SIGINT
     13 #define SIGINT -1
     14 #endif
     15 
     16 #ifndef SIGTERM
     17 #define SIGTERM -1
     18 #endif
     19 
     20 #ifndef SIGQUIT
     21 #define SIGQUIT -1
     22 #endif
     23 
     24 #ifndef SIGHUP
     25 #define SIGHUP -1
     26 #endif
     27 
     28 int                   kflag, dflag, nflag, iflag, sflag;
     29 int                   eflag, pflag, tflag, qflag;
     30 int                   exitstatus;
     31 volatile sig_atomic_t stop;
     32 
     33 void
     34 debug(char *fmt, ...)
     35 {
     36   va_list va;
     37 
     38   if (!dflag)
     39     return;
     40 
     41   va_start(va, fmt);
     42   vfprintf(stdout, fmt, va);
     43   fputc('\n', stdout);
     44   va_end(va);
     45 }
     46 
     47 int
     48 hash(char *name)
     49 {
     50   int      c;
     51   unsigned h = 5381;
     52 
     53   while ((c = *name++))
     54     h = h * 33 ^ c;
     55 
     56   return h;
     57 }
     58 
     59 void
     60 sighandler(int signo)
     61 {
     62   killchild();
     63   stop = signo;
     64 }
     65 
     66 static void
     67 usage(void)
     68 {
     69   fputs(
     70       "usage: make [-eiknprSstd] [-f file] [-j jobs] "
     71       "[macro=value ...] [target ...]\n",
     72       stderr
     73   );
     74   exit(EXIT_FAILURE);
     75 }
     76 
     77 static char *
     78 getarg(char **args, char ***argv)
     79 {
     80   char *s;
     81 
     82   if ((*args)[1]) {
     83     s = (*args) + 1;
     84     *args += strlen(*args) - 1;
     85     return s;
     86   }
     87 
     88   if (!argv)
     89     usage();
     90 
     91   if ((*argv)[1] == NULL)
     92     usage();
     93   (*argv)++;
     94 
     95   return **argv;
     96 }
     97 
     98 // MAKEFLAGS is a list of words, so a space inside one, as in CFLAGS=-O2 -g,
     99 // goes in behind a backslash, and so does a backslash
    100 static void
    101 appendmakeflags(char *text)
    102 {
    103   size_t n;
    104   char  *s, *t, *w;
    105 
    106   s = getmacro("MAKEFLAGS");
    107   n = strlen(s);
    108   t = emalloc(n + 2 * strlen(text) + 2);
    109   memcpy(t, s, n);
    110   w = t + n;
    111   if (n)
    112     *w++ = ' ';
    113   for (; *text; text++) {
    114     if (*text == ' ' || *text == '\t' || *text == '\\')
    115       *w++ = '\\';
    116     *w++ = *text;
    117   }
    118   *w = '\0';
    119   setmacro("MAKEFLAGS", t, MAKEFLAGS, EXPORT);
    120 
    121   free(t);
    122 }
    123 
    124 // the next word of a MAKEFLAGS list, cut out in place and its escapes
    125 // undone. NULL at the end
    126 static char *
    127 flagword(char **list)
    128 {
    129   char *start, *w;
    130 
    131   while (**list == ' ' || **list == '\t')
    132     (*list)++;
    133   if (!**list)
    134     return NULL;
    135   start = w = *list;
    136   while (**list && **list != ' ' && **list != '\t') {
    137     if (**list == '\\' && (*list)[1])
    138       (*list)++;
    139     *w++ = *(*list)++;
    140   }
    141   if (**list)
    142     (*list)++;
    143   *w = '\0';
    144   return start;
    145 }
    146 
    147 static int
    148 hasargs(int c)
    149 {
    150   return c == 'f' || c == 'j';
    151 }
    152 
    153 // gmake compat: a bare -j, with nothing after it or something that is
    154 // not a count, asks for as many jobs as there can be and takes no argument
    155 static int
    156 takesarg(int c, char **args, char ***argv)
    157 {
    158 #if FEATURE_MAKE_GMAKE
    159   if (c == 'j') {
    160     if ((*args)[1])
    161       return 1;
    162     return argv && (*argv)[1] && isdigit((unsigned char)(*argv)[1][0]);
    163   }
    164 #else
    165   (void)args;
    166   (void)argv;
    167 #endif
    168   return hasargs(c);
    169 }
    170 
    171 static void
    172 parseflag(int flag, char **args, char ***argv)
    173 {
    174   if (takesarg(flag, args, argv))
    175     getarg(args, argv);
    176 
    177   switch (flag) {
    178     case 'j':
    179       /* ?man -j:jobs: number of parallel jobs (accepted, runs
    180        * serially) */
    181     case 'f':
    182       /* ?man -f:file: read rules from file instead of makefile or
    183        * makefile */
    184       break;
    185     case 'e':
    186       /* ?man -e: environment variables override makefile assignments
    187        */
    188       eflag = 1;
    189       appendmakeflags("-e");
    190       break;
    191     case 'i':
    192       /* ?man -i: ignore errors from executed commands */
    193       iflag = 1;
    194       appendmakeflags("-i");
    195       break;
    196     case 'k':
    197       /* ?man -k: continue building other targets after an error */
    198       kflag = 1;
    199       appendmakeflags("-k");
    200       break;
    201     case 'n':
    202       /* ?man -n: print commands without executing them */
    203       nflag = 1;
    204       appendmakeflags("-n");
    205       break;
    206     case 'p':
    207       /* ?man -p: print the rule and macro database then exit */
    208       pflag = 1;
    209       break;
    210     case 'q':
    211       /* ?man -q: question mode, return zero if up to date, one
    212        * otherwise */
    213       qflag = 1;
    214       appendmakeflags("-q");
    215       break;
    216     case 'r':
    217       /* ?man -r: clear the built-in suffix rules and variables */
    218       addtarget(".SUFFIXES", 0);
    219       appendmakeflags("-r");
    220       break;
    221     case 'S':
    222       /* ?man -S: cancel a previous -k, stop on first error */
    223       kflag = 0;
    224       appendmakeflags("-S");
    225       break;
    226     case 's':
    227       /* ?man -s: silent, do not echo commands before running them */
    228       sflag = 1;
    229       appendmakeflags("-s");
    230       break;
    231     case 't':
    232       /* ?man -t: touch targets instead of rebuilding them */
    233       tflag = 1;
    234       appendmakeflags("-t");
    235       break;
    236     case 'd':
    237       /* ?man -d: enable debug output */
    238       dflag = 1;
    239       appendmakeflags("-d");
    240       break;
    241     default:
    242       usage();
    243   }
    244 }
    245 
    246 static int
    247 assign(char *s, int where, int export)
    248 {
    249   int   pos;
    250   char *t;
    251 
    252   if ((t = strchr(s, '=')) == NULL)
    253     return 0;
    254 
    255   pos = t - s;
    256 
    257   appendmakeflags(s);
    258   t      = estrdup(s);
    259   t[pos] = '\0';
    260 
    261   setmacro(t, t + pos + 1, where, export);
    262   free(t);
    263   return 1;
    264 }
    265 
    266 // macros may come after targets too, as they do in "make all CC=cc", so a
    267 // command line is read to its end and what is not an option or a macro is a
    268 // target. the options of MAKEFLAGS have no targets to collect
    269 static void
    270 parseargv(char **argv, char ***targets, int where, int export)
    271 {
    272   char  *s;
    273   char  *hm    = NULL;
    274   char **found = NULL;
    275   int    n     = 0;
    276 
    277   for (; *argv; ++argv) {
    278     s = *argv;
    279     if (hm == NULL && strcmp(s, "--") == 0) {
    280       hm = *argv;
    281     } else if (hm && s[0] == '-') {
    282       break;
    283     } else if (s[0] != '-') {
    284       if (assign(s, where, export))
    285         continue;
    286       if (!targets)
    287         break;
    288       found      = erealloc(found, (n + 2) * sizeof(*found));
    289       found[n++] = s;
    290       found[n]   = NULL;
    291       continue;
    292     }
    293     while (hm == NULL && *++s)
    294       parseflag(*s, &s, &argv);
    295   }
    296 
    297   if (targets) {
    298     if (!found) {
    299       found    = emalloc(sizeof(*found));
    300       found[0] = NULL;
    301     }
    302     *targets = found;
    303   }
    304 }
    305 
    306 static void
    307 parsemakeflags(void)
    308 {
    309   int   n;
    310   char *s, *flags, **arr;
    311 
    312   if ((flags = getenv("MAKEFLAGS")) == NULL)
    313     return;
    314 
    315   setmacro("MAKEFLAGS", "", MAKEFLAGS, EXPORT);
    316 
    317   while (*flags == ' ' || *flags == '\t')
    318     flags++;
    319 
    320   if (flags[0] != '-' && !strchr(flags, '=')) {
    321     while (*flags) {
    322       parseflag(*flags, &flags, NULL);
    323       flags++;
    324     }
    325   } else {
    326     n   = 0;
    327     arr = NULL;
    328     while ((s = flagword(&flags))) {
    329       n++;
    330       arr        = erealloc(arr, sizeof(char *) * (n + 1));
    331       arr[n - 1] = s;
    332       arr[n]     = NULL;
    333     }
    334 
    335     if (arr)
    336       parseargv(arr, NULL, MAKEFLAGS, NOEXPORT);
    337     free(arr);
    338   }
    339 }
    340 
    341 static void
    342 parsemakefiles(char **argv)
    343 {
    344   char *s, *arg;
    345   int   c, hasmake;
    346 
    347   hasmake = 0;
    348   for (; *argv; ++argv) {
    349     if (**argv != '-')
    350       continue;
    351     for (s = *argv; (c = *s); ++s) {
    352       if (takesarg(c, &s, &argv))
    353         arg = getarg(&s, &argv);
    354 
    355       if (c == 'f') {
    356         if (strcmp(arg, "-") == 0)
    357           arg = NULL;
    358         parse(arg);
    359         hasmake = 1;
    360       }
    361     }
    362   }
    363 
    364   if (hasmake)
    365     return;
    366 
    367   if (parse("makefile"))
    368     return;
    369   if (parse("Makefile"))
    370     return;
    371 }
    372 
    373 /*
    374  * we want to enable debug as earlier as possible,
    375  * if we wait until we read the makefiles then
    376  * we are going to lose to much debug information
    377  */
    378 static void
    379 enadebug(char *argv[])
    380 {
    381   int   c;
    382   char *p;
    383 
    384   for (; *argv && **argv == '-'; ++argv) {
    385     p = *argv;
    386     for (++p; (c = *p); ++p) {
    387       if (takesarg(c, &p, &argv))
    388         getarg(&p, &argv);
    389       if (c == 'd')
    390         dflag = 1;
    391     }
    392   }
    393 }
    394 
    395 // ?man make: maintain program dependencies
    396 // ?man arguments: [name=value ...] [target ...]
    397 // ?man update files based on dependency rules in a makefile
    398 // ?man if no -f is given, make tries makefile then Makefile
    399 // ?man if no target is given the first non-dot target in the makefile is built
    400 int
    401 main(int argc, char *argv[])
    402 {
    403   char *arg0, **targets;
    404 
    405   (void)argc;
    406 
    407   signal(SIGINT, sighandler);
    408   signal(SIGHUP, sighandler);
    409   signal(SIGTERM, sighandler);
    410   signal(SIGQUIT, sighandler);
    411 
    412   targets = NULL;
    413   arg0    = *argv++;
    414 
    415   enadebug(argv);
    416   inject(defaults);
    417   setmacro("MAKE", arg0, MAKEFILE, NOEXPORT);
    418 
    419   parsemakeflags();
    420   parseargv(argv, &targets, CMDLINE, EXPORT);
    421   parsemakefiles(argv);
    422 
    423   if (pflag) {
    424     dumpmacros();
    425     dumprules();
    426     exit(EXIT_SUCCESS);
    427   }
    428 
    429   if (!*targets) {
    430     build(NULL);
    431   } else {
    432     while (*targets)
    433       build(*targets++);
    434   }
    435 
    436   exit(exitstatus);
    437 
    438   return 0;
    439 }