~ chicken-core (chicken-5) /chicken-do.c


  1/* chicken-do
  2;
  3; Execute command if dependency changed or target is out of date.
  4;
  5; Copyright (c) 2017-2022, The CHICKEN Team
  6; All rights reserved.
  7;
  8; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
  9; conditions are met:
 10;
 11;   Redistributions of source code must retain the above copyright notice, this list of conditions and the following
 12;     disclaimer.
 13;   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 14;     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 promote
 16;     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 EXPRESS
 19; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 20; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 21; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 22; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 23; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 24; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 25; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26; POSSIBILITY OF SUCH DAMAGE.
 27*/
 28
 29
 30#include "chicken.h"
 31
 32#ifdef WIN32
 33# include <windows.h>
 34# include <sys/types.h>
 35#else
 36# include <sys/wait.h>
 37#endif
 38
 39#include <sys/stat.h>
 40#include <errno.h>
 41
 42#define MAX_TARGETS 256
 43#define MAX_DEPENDS 1024
 44
 45#ifdef WIN32
 46# define MAX_COMMAND_LEN 32767
 47#endif
 48
 49static 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;
 55
 56
 57static void usage(int code)
 58{
 59  fputs("usage: chicken-do [-q] [-h] [--] TARGET ... : DEPENDENCY ... : COMMAND ...\n", stderr);
 60  exit(code);
 61}
 62
 63
 64static void cleanup()
 65{
 66  char **t;
 67
 68  for(t = targets; *t != NULL; ++t)
 69#ifdef WIN32
 70    DeleteFile(*t);
 71#else
 72    unlink(*t);
 73#endif
 74}
 75
 76
 77static int execute(char **argv)
 78{
 79#ifdef WIN32
 80  static PROCESS_INFORMATION process_info;
 81  static STARTUPINFO startup_info;
 82  static TCHAR cmdline[ MAX_COMMAND_LEN ];
 83  static int len;
 84
 85  startup_info.cb = sizeof(STARTUPINFO);
 86
 87  /* 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  }
 95
 96  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  }
102
103  CloseHandle(process_info.hThread);
104
105  WaitForSingleObject(process_info.hProcess, INFINITE);
106  DWORD code;
107
108  if(!GetExitCodeProcess(process_info.hProcess, &code)) {
109    fprintf(stderr, "unable to obtain exit status of subprocess\n");
110    exit(1);
111  }
112
113  return code;
114#else
115  pid_t child = fork();
116
117  if(child == -1) {
118    perror("forking subprocess failed");
119    exit(1);
120  }
121
122  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  }
129
130  for(;;) {
131    int status;
132    pid_t w = waitpid(child, &status, 0);
133
134    if(w == -1) {
135      perror("waiting for subprocess failed");
136      cleanup();
137      exit(1);
138    }
139
140    if(WIFEXITED(status))
141      return WEXITSTATUS(status);
142
143    if(WIFSIGNALED(status)) {
144      fprintf(stderr, "subprocess killed by signal %d\n", WTERMSIG(status));
145      cleanup();
146      exit(1);
147    }
148  }
149#endif
150}
151
152
153int main(int argc, char *argv[]) 
154{
155  int i, a = 0;
156  struct stat *st, sd;
157  char **t = targets;
158  char **d = depends;
159
160  if(argc < 3) usage(1);
161
162  for(i = 1; i < argc; ++i) {
163    if(!strcmp(argv[ i ], ":")) {
164      *t = NULL;
165      break;
166    }
167
168    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  }
182
183  if(i == argc) usage(1);
184
185  while(++i < argc) {
186    if(!strcmp(argv[ i ], ":")) {
187      *d = NULL;
188      break;
189    }
190
191    if(d >= depends + MAX_DEPENDS) {
192      fprintf(stderr, "too many dependencies\n");
193      exit(1);
194    }
195
196    *(d++) = argv[ i ];
197  }
198
199  if(i == argc) usage(1);
200
201  cmd = argv + i + 1;
202  st = tstats;
203
204  for(t = targets; *t != NULL; ++t) {
205    if(stat(*t, st++) == -1) {
206      if(errno == ENOENT) goto build;
207
208      fprintf(stderr, "%s: %s\n", *t, strerror(errno));
209      exit(1);
210    }
211  }
212
213  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    }      
218
219    st = tstats;
220
221    for(t = targets; *t != NULL; ++t) {
222      if(sd.st_mtime > (st++)->st_mtime) goto build;
223    }
224  }
225
226  return 0;
227
228build:
229  if(!quiet) {
230    fputs("  ", stdout);
231
232    for(t = cmd; *t != NULL; ++t)
233      printf(" %s", *t);
234
235    putchar('\n');
236    fflush(stdout);
237  }
238
239  int s = execute(cmd);
240
241  if(s != 0) cleanup();
242
243  return s;
244}
Trap