~ chicken-core (chicken-5) /chicken-do.c
Trap1/* chicken-do2;3; Execute command if dependency changed or target is out of date.4;5; Copyright (c) 2017-2022, The CHICKEN Team6; All rights reserved.7;8; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following9; conditions are met:10;11; Redistributions of source code must retain the above copyright notice, this list of conditions and the following12; disclaimer.13; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following14; disclaimer in the documentation and/or other materials provided with the distribution.15; Neither the name of the author nor the names of its contributors may be used to endorse or promote16; products derived from this software without specific prior written permission.17;18; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS19; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY20; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR21; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR23; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR25; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE26; POSSIBILITY OF SUCH DAMAGE.27*/282930#include "chicken.h"3132#ifdef WIN3233# include <windows.h>34# include <sys/types.h>35#else36# include <sys/wait.h>37#endif3839#include <sys/stat.h>40#include <errno.h>4142#define MAX_TARGETS 25643#define MAX_DEPENDS 10244445#ifdef WIN3246# define MAX_COMMAND_LEN 3276747#endif4849static char *targets[ MAX_TARGETS ];50static char *depends[ MAX_DEPENDS ];51static struct stat tstats[ MAX_TARGETS ];52static char **cmd;53static int opts = 1;54static int quiet = 0;555657static void usage(int code)58{59 fputs("usage: chicken-do [-q] [-h] [--] TARGET ... : DEPENDENCY ... : COMMAND ...\n", stderr);60 exit(code);61}626364static void cleanup()65{66 char **t;6768 for(t = targets; *t != NULL; ++t)69#ifdef WIN3270 DeleteFile(*t);71#else72 unlink(*t);73#endif74}757677static int execute(char **argv)78{79#ifdef WIN3280 static PROCESS_INFORMATION process_info;81 static STARTUPINFO startup_info;82 static TCHAR cmdline[ MAX_COMMAND_LEN ];83 static int len;8485 startup_info.cb = sizeof(STARTUPINFO);8687 /* quote command arguments */88 while(*argv != NULL) {89 len += snprintf(cmdline + len, sizeof(cmdline) - len, "\"%s\" ", *(argv++));90 if(len > sizeof(cmdline)) {91 fprintf(stderr, "argument list too long\n");92 exit(1);93 }94 }9596 if(!CreateProcess(NULL, cmdline, NULL, NULL, TRUE,97 NORMAL_PRIORITY_CLASS, NULL, NULL, &startup_info,98 &process_info)) {99 fprintf(stderr, "creating subprocess failed\n");100 exit(1);101 }102103 CloseHandle(process_info.hThread);104105 WaitForSingleObject(process_info.hProcess, INFINITE);106 DWORD code;107108 if(!GetExitCodeProcess(process_info.hProcess, &code)) {109 fprintf(stderr, "unable to obtain exit status of subprocess\n");110 exit(1);111 }112113 return code;114#else115 pid_t child = fork();116117 if(child == -1) {118 perror("forking subprocess failed");119 exit(1);120 }121122 if(child == 0) {123 execvp(argv[ 0 ], argv);124 /* returns only in case of error */125 perror("executing command failed");126 cleanup();127 exit(1);128 }129130 for(;;) {131 int status;132 pid_t w = waitpid(child, &status, 0);133134 if(w == -1) {135 perror("waiting for subprocess failed");136 cleanup();137 exit(1);138 }139140 if(WIFEXITED(status))141 return WEXITSTATUS(status);142143 if(WIFSIGNALED(status)) {144 fprintf(stderr, "subprocess killed by signal %d\n", WTERMSIG(status));145 cleanup();146 exit(1);147 }148 }149#endif150}151152153int main(int argc, char *argv[])154{155 int i, a = 0;156 struct stat *st, sd;157 char **t = targets;158 char **d = depends;159160 if(argc < 3) usage(1);161162 for(i = 1; i < argc; ++i) {163 if(!strcmp(argv[ i ], ":")) {164 *t = NULL;165 break;166 }167168 if(opts && *argv[ i ] == '-') {169 switch(argv[ i ][ 1 ]) {170 case 'q': quiet = 1; break;171 case 'h': usage(0);172 case '-': opts = 0; break;173 default: usage(1);174 }175 }176 else if(t >= targets + MAX_TARGETS) {177 fprintf(stderr, "too many targets\n");178 exit(1);179 }180 else *(t++) = argv[ i ];181 }182183 if(i == argc) usage(1);184185 while(++i < argc) {186 if(!strcmp(argv[ i ], ":")) {187 *d = NULL;188 break;189 }190191 if(d >= depends + MAX_DEPENDS) {192 fprintf(stderr, "too many dependencies\n");193 exit(1);194 }195196 *(d++) = argv[ i ];197 }198199 if(i == argc) usage(1);200201 cmd = argv + i + 1;202 st = tstats;203204 for(t = targets; *t != NULL; ++t) {205 if(stat(*t, st++) == -1) {206 if(errno == ENOENT) goto build;207208 fprintf(stderr, "%s: %s\n", *t, strerror(errno));209 exit(1);210 }211 }212213 for(d = depends; *d != NULL; ++d) {214 if(stat(*d, &sd) == -1) {215 fprintf(stderr, "%s: %s\n", *d, strerror(errno));216 exit(1);217 }218219 st = tstats;220221 for(t = targets; *t != NULL; ++t) {222 if(sd.st_mtime > (st++)->st_mtime) goto build;223 }224 }225226 return 0;227228build:229 if(!quiet) {230 fputs(" ", stdout);231232 for(t = cmd; *t != NULL; ++t)233 printf(" %s", *t);234235 putchar('\n');236 fflush(stdout);237 }238239 int s = execute(cmd);240241 if(s != 0) cleanup();242243 return s;244}