summaryrefslogtreecommitdiff
path: root/sys/src/ape/cmd/make/dosys.c
blob: dfa417b847e53e246bb2a2c4d76d747891266153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "defs.h"
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

static int	metas(char *);
static int	waitproc(int *);
static int	doshell(char *, int);
static int	doexec(char *);

int
dosys(char *comstring, int nohalt, int nowait, char *prefix)
{
int status;
struct process *procp;

/* make sure there is room in the process stack */
if(nproc >= MAXPROC)
	waitstack(MAXPROC-1);

/* make sure fewer than proclimit processes are running */
while(proclive >= proclimit)
	{
	enbint(SIG_IGN);
	waitproc(&status);
	enbint(intrupt);
	}

if(prefix)
	{
	fputs(prefix, stdout);
	fputs(comstring, stdout);
	}

procp = procstack + nproc;
procp->pid = (forceshell || metas(comstring) ) ?
	doshell(comstring,nohalt) : doexec(comstring);
if(procp->pid == -1)
	fatal("fork failed");
procstack[nproc].nohalt = nohalt;
procstack[nproc].nowait = nowait;
procstack[nproc].done = NO;
++proclive;
++nproc;

if(nowait)
	{
	printf(" &%d\n", procp->pid);
	fflush(stdout);
	return 0;
	}
if(prefix)
	{
	putchar('\n');
	fflush(stdout);
	}
return waitstack(nproc-1);
}

static int
metas(char *s)   /* Are there are any  Shell meta-characters? */
{
char c;

while( (funny[c = *s++] & META) == 0 )
	;
return( c );
}

static void
doclose(void)	/* Close open directory files before exec'ing */
{
struct dirhd *od;

for (od = firstod; od; od = od->nxtdirhd)
	if(od->dirfc)
		closedir(od->dirfc);
}

/*  wait till none of the processes in the stack starting at k is live */
int
waitstack(int k)
{
int npending, status, totstatus;
int i;

totstatus = 0;
npending = 0;
for(i=k ; i<nproc; ++i)
	if(! procstack[i].done)
		++npending;
enbint(SIG_IGN);
if(dbgflag > 1)
	printf("waitstack(%d)\n", k);

while(npending>0 && proclive>0)
	{
	if(waitproc(&status) >= k)
		--npending;
	totstatus |= status;
	}

if(nproc > k)
	nproc = k;
enbint(intrupt);
return totstatus;
}

static int
waitproc(int *statp)
{
pid_t pid;
int status;
int i;
struct process *procp;
char junk[50];
static int inwait = NO;

if(inwait)	/* avoid infinite recursions on errors */
	return MAXPROC;
inwait = YES;

pid = wait(&status);
if(dbgflag > 1)
	fprintf(stderr, "process %d done, status = %d\n", pid, status);
if(pid == -1)
	{
	if(errno == ECHILD)	/* multiple deaths, no problem */
		{
		if(proclive)
			{
			for(i=0, procp=procstack; i<nproc; ++i, ++procp)
				procp->done = YES;
			proclive = nproc = 0;
			}
		return MAXPROC;
		}
	fatal("bad wait code");
	}
for(i=0, procp=procstack; i<nproc; ++i, ++procp)
	if(procp->pid == pid)
		{
		--proclive;
		procp->done = YES;

		if(status)
			{
			if(procp->nowait)
				printf("%d: ", pid);
			if( WEXITSTATUS(status) )
				printf("*** Error code %d", WEXITSTATUS(status) );
			else	printf("*** Termination code %d", WTERMSIG(status));
		
			printf(procp->nohalt ? "(ignored)\n" : "\n");
			fflush(stdout);
			if(!keepgoing && !procp->nohalt)
				fatal(CHNULL);
			}
		*statp = status;
		inwait = NO;
		return i;
		}

sprintf(junk, "spurious return from process %d", pid);
fatal(junk);
return -1;/*NOTREACHED*/
}

static int
doshell(char *comstring, int nohalt)
{
pid_t pid;

if((pid = fork()) == 0)
	{
	enbint(SIG_DFL);
	doclose();

	execl(SHELLCOM, "sh", (nohalt ? "-c" : "-ce"), comstring, NULL);
	fatal("Couldn't load Shell");
	}

return pid;
}

static int
doexec(char *str)
{
char *t, *tend;
char **argv;
char **p;
int nargs;
pid_t pid;

while( *str==' ' || *str=='\t' )
	++str;
if( *str == '\0' )
	return(-1);	/* no command */

nargs = 1;
for(t = str ; *t ; )
	{
	++nargs;
	while(*t!=' ' && *t!='\t' && *t!='\0')
		++t;
	if(*t)	/* replace first white space with \0, skip rest */
		for( *t++ = '\0' ; *t==' ' || *t=='\t'  ; ++t)
			;
	}

/* now allocate args array, copy pointer to start of each string,
   then terminate array with a null
*/
p = argv = (char **) ckalloc(nargs*sizeof(char *));
tend = t;
for(t = str ; t<tend ; )
	{
	*p++ = t;
	while( *t )
		++t;
	do	{
		++t;
		} while(t<tend && (*t==' ' || *t=='\t') );
	}
*p = NULL;
/*TEMP  for(p=argv; *p; ++p)printf("arg=%s\n", *p);*/

if((pid = fork()) == 0)
	{
	enbint(SIG_DFL);
	doclose();
	enbint(intrupt);
	execvp(str, argv);
	printf("\n");
	fatal1("Cannot load %s",str);
	}

free( (char *) argv);
return pid;
}

void
touch(int force, char *name)
{
struct stat stbuff;
char junk[1];
int fd;

if( stat(name,&stbuff) < 0)
	if(force)
		goto create;
	else
		{
		fprintf(stderr, "touch: file %s does not exist.\n", name);
		return;
		}

if(stbuff.st_size == 0)
	goto create;

if( (fd = open(name, O_RDWR)) < 0)
	goto bad;

if( read(fd, junk, 1) < 1)
	{
	close(fd);
	goto bad;
	}
lseek(fd, 0L, SEEK_SET);
if( write(fd, junk, 1) < 1 )
	{
	close(fd);
	goto bad;
	}
close(fd);
return;

bad:
	fprintf(stderr, "Cannot touch %s\n", name);
	return;

create:
	if( (fd = creat(name, 0666)) < 0)
		goto bad;
	close(fd);
}