source: trunk/minix/commands/make/main.c@ 12

Last change on this file since 12 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 7.2 KB
Line 
1/*************************************************************************
2 *
3 * m a k e : m a i n . c
4 *
5 *========================================================================
6 * Edition history
7 *
8 * # Date Comments By
9 * --- -------- ---------------------------------------------------- ---
10 * 1 ?? ??
11 * 2 01.07.89 strcmp(makefile,..) only if makefile a valid ptr. RAL
12 * 3 23.08.89 initname() added RAL
13 * 4 30.08.89 argument parsing impr., indention ch., macro fl. add.PSH,RAL
14 * 5 03.09.89 k-option added, initname -> init changed RAL
15 * 6 06.09.89 environment, MAKEFLAGS, e,d,a options added, RAL
16 * 7 09.09.89 tos support added, fatal args added, fopen makefile PHH,RAL
17 * 8 17.09.89 setoptions fixed for __STDC__ RAL
18 * ------------ Version 2.0 released ------------------------------- RAL
19 *
20 *************************************************************************/
21
22/*
23 * make:
24 *
25 * -a try to guess undefined ambiguous macros (*,<)
26 * -d print debugging info
27 * -e environment macro def. overwrite makefile def.
28 * -f makefile name
29 * -i ignore exit status
30 * -k continue on errors
31 * -n pretend to make
32 * -p print all macros & targets
33 * -q question up-to-dateness of target. Return exit status 1 if not
34 * -r don't not use inbuilt rules
35 * -s make silently
36 * -t touch files instead of making them
37 * -m Change memory requirements (EON only)
38 */
39
40#define EXTERN
41#define INIT(x) = x
42#define INITARRAY
43#include "h.h"
44
45static char version[]= "2.0";
46
47static FILE *ifd; /* Input file desciptor */
48static char *ptrmakeflags;
49
50/* There must be enough 'space' for all possible flags ! */
51static char makeflags[] = "MAKEFLAGS= ";
52
53void main(argc, argv)
54int argc;
55char **argv;
56{
57 register char *p; /* For argument processing */
58 int estat = 0; /* For question */
59 register struct name *np;
60 struct macro *mp;
61 int targc; /* temporary for multiple scans */
62 char **targv;
63 char **nargv; /* for removing items from argv */
64 char **envp; /* enivironment ptr */
65
66
67 ptrmakeflags = &makeflags[10];
68 myname = (argc-- < 1) ? "make" : *argv++;
69#ifdef tos
70 myname = "Make";
71#endif
72
73 targc = argc;
74 targv = nargv = argv;
75 while (targc--) {
76 if((p = strchr(*targv, '=')) != (char *)NULL) {
77 *p = '\0';
78 mp = setmacro(*targv, p + 1);
79 mp->m_flag |= M_OVERRIDE;
80 --argc;
81 } else
82 *nargv++ = *targv;
83
84 ++targv;
85 }
86
87 targc = argc;
88 targv = nargv = argv;
89 while (targc--) {
90 if (**targv == '-') {
91 --argc;
92 p = *targv++;
93 while (*++p != '\0') {
94 switch(mylower(*p)) {
95 case 'f': /* Alternate file name */
96 if (*++p == '\0') {
97 --argc;
98 if (targc-- == 0)
99 usage();
100 p = *targv++;
101 }
102 makefile = p;
103 goto end_of_args;
104#ifdef eon
105 case 'm': /* Change space requirements */
106 if (*++p == '\0') {
107 --argc;
108 if (targc-- <= 0)
109 usage();
110 p = *targv++;
111 }
112 memspace = atoi(p);
113 goto end_of_args;
114#endif
115 default :
116 setoption(*p);
117 break;
118 }
119 }
120 end_of_args:;
121 } else
122 *nargv++ = *targv++;
123 }
124
125 /* evaluate and update environment MAKEFLAGS */
126 if((p =getenv("MAKEFLAGS")) != (char *)0)
127 while(*p) setoption(*p++);
128 for( p = ptrmakeflags; !isspace((int)*p); p++) ;
129 *p = '\0';
130 putenv(makeflags);
131
132
133#ifdef eon
134 if (initalloc(memspace) == 0xffff) /* Must get memory for alloc */
135 fatal("Cannot initalloc memory",(char *)0,0);
136#endif
137
138 if (makefile && strcmp(makefile, "-") == 0) /* use stdin as makefile */
139 ifd = stdin;
140 else if (!makefile) { /* If no file, then use default */
141 if ((ifd = fopen(makefile = DEFN1, "r")) == (FILE *)0) {
142 if (errno != MNOENT || !DEFN2)
143 fatal("Can't open %s: %s", DEFN1, errno);
144 else if ((ifd = fopen(makefile = DEFN2, "r")) == (FILE *)0)
145 fatal("Can't open %s: %s", DEFN2, errno);
146 }
147 }
148 else if ((ifd = fopen(makefile, "r")) == (FILE *)0)
149 fatal("Can't open %s: %s", makefile, errno);
150
151 init();
152
153 makerules();
154
155 mp = setmacro("MAKE", myname);
156 mp->m_flag |= M_MAKE;
157 setmacro("$", "$");
158
159 /* set environment macros */
160 envp = environ; /* get actual environment ptr. */
161 while (*envp) {
162 if((p = strchr(*envp, '=')) != (char *)NULL) {
163 *p = '\0';
164 mp = setmacro(*envp, p + 1);
165 *p = '=';
166 if (useenv) mp->m_flag |= M_OVERRIDE;
167 } else
168 fatal("invalid environment: %s",*envp,0);
169
170 ++envp;
171 }
172
173 input(ifd); /* Input all the gunga */
174 fclose(ifd); /* Finished with makefile */
175 lineno = 0; /* Any calls to error now print no line number */
176
177 if (print)
178 prt(); /* Print out structures */
179
180 np = newname(".SILENT");
181 if (np->n_flag & N_TARG) silent = TRUE;
182
183 np = newname(".IGNORE");
184 if (np->n_flag & N_TARG) ignore = TRUE;
185
186 precious();
187
188 if (!firstname)
189 fatal("No targets defined",(char *)0,0);
190
191 circh(); /* Check circles in target definitions */
192
193 if (!argc)
194 estat = make(firstname, 0);
195 else
196 while (argc--) {
197 estat |= make(newname(*argv++), 0);
198 }
199
200 if (quest)
201 exit(estat);
202 else
203 exit(0);
204}
205
206#ifdef __STDC__
207void setoption(char option)
208#else
209void setoption(option)
210char option;
211#endif
212{
213 register char *c;
214
215 option = mylower(option);
216 switch(option) {
217 case 'n': /* Pretend mode */
218 domake = FALSE;
219 break;
220 case 'i': /* Ignore fault mode */
221 ignore = TRUE;
222 break;
223 case 'k': /* Continue on errror */
224 conterr = TRUE;
225 break;
226 case 's': /* Silent about commands */
227 silent = TRUE;
228 break;
229 case 'p':
230 print = TRUE;
231 break;
232 case 'r':
233 rules = FALSE;
234 break;
235 case 't':
236 dotouch = TRUE;
237 break;
238 case 'q':
239 quest = TRUE;
240 break;
241 case 'e':
242 useenv = TRUE;
243 break;
244 case 'd':
245 dbginfo = TRUE;
246 break;
247 case 'a':
248 ambigmac = TRUE;
249 break;
250 default: /* Wrong option */
251 usage();
252 }
253 for( c = ptrmakeflags; !isspace((int)*c); c++)
254 if ( *c == option) return;
255 *c = option;
256}
257
258void usage()
259{
260 fprintf(stderr, "Syntax: %s [{options | macro=val | target}]\n", myname);
261 fprintf(stderr, "Function: maintaining computer programs V%s\n",version);
262 fprintf(stderr, "Options : -a : try to guess undefined ambiguous macros (*,<)\n");
263 fprintf(stderr, " -d : print debugging information\n");
264 fprintf(stderr, " -e : environment macro def. overwrite makefile def.\n");
265 fprintf(stderr, " -f filename : makefile name (default: makefile, Makefile)\n");
266 fprintf(stderr, " -i : ignore exit status of executed commands\n");
267 fprintf(stderr, " -k : continue with unrelated branches on errors\n");
268 fprintf(stderr, " -n : pretend to make\n");
269 fprintf(stderr, " -p : print all macros & targets\n");
270 fprintf(stderr, " -q : question up-to-dateness of target\n");
271 fprintf(stderr, " -r : don't use inbuilt rules\n");
272 fprintf(stderr, " -s : make silently\n");
273 fprintf(stderr, " -t : touch files instead of making them\n");
274 fprintf(stderr, "Environment: MAKEFLAGS\n");
275 exit(1);
276}
277
278
279void fatal(msg, a1, a2)
280char *msg;
281char *a1;
282int a2;
283{
284 fprintf(stderr, "%s: ", myname);
285 fprintf(stderr, msg, a1, strerror(a2));
286 fputc('\n', stderr);
287 exit(1);
288}
Note: See TracBrowser for help on using the repository browser.