main.c revision 16073
1/* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static char copyright[] = 36"@(#) Copyright (c) 1980, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 42#endif /* not lint */ 43 44#include <sys/types.h> 45#include <sys/stat.h> 46#include <sys/file.h> 47#include <sys/mman.h> 48#include <stdio.h> 49#include <ctype.h> 50#include <err.h> 51#include <sysexits.h> 52#include "y.tab.h" 53#include "config.h" 54 55#ifndef TRUE 56#define TRUE (1) 57#endif 58 59#ifndef FALSE 60#define FALSE (0) 61#endif 62 63static char *PREFIX; 64static int no_config_clobber = FALSE; 65int old_config_present; 66 67/* 68 * Config builds a set of files for building a UNIX 69 * system given a description of the desired system. 70 */ 71main(argc, argv) 72 int argc; 73 char **argv; 74{ 75 76 extern char *optarg; 77 extern int optind; 78 struct stat buf; 79 int ch; 80 char *p; 81 82 while ((ch = getopt(argc, argv, "gpn")) != EOF) 83 switch (ch) { 84 case 'g': 85 debugging++; 86 break; 87 case 'p': 88 profiling++; 89 break; 90 case 'n': 91 no_config_clobber = TRUE; 92 break; 93 case '?': 94 default: 95 goto usage; 96 } 97 argc -= optind; 98 argv += optind; 99 100 if (argc != 1) { 101usage: fputs("usage: config [-gpn] sysname\n", stderr); 102 exit(1); 103 } 104 105 if (freopen(PREFIX = *argv, "r", stdin) == NULL) { 106 perror(PREFIX); 107 exit(2); 108 } 109 if (getenv("NO_CONFIG_CLOBBER")) 110 no_config_clobber = TRUE; 111 112 p = path((char *)NULL); 113 if (stat(p, &buf)) { 114 if (mkdir(p, 0777)) { 115 perror(p); 116 exit(2); 117 } 118 } 119 else if ((buf.st_mode & S_IFMT) != S_IFDIR) { 120 fprintf(stderr, "config: %s isn't a directory.\n", p); 121 exit(2); 122 } 123#ifndef NO_CLOBBER_EVER 124 else if (!no_config_clobber) { 125 char tmp[strlen(p) + 8]; 126 127 fprintf(stderr, "Removing old directory %s: ", p); 128 fflush(stderr); 129 sprintf(tmp, "rm -rf %s", p); 130 if (system(tmp)) { 131 fprintf(stderr, "Failed!\n"); 132 perror(tmp); 133 exit(2); 134 } 135 fprintf(stderr, "Done.\n"); 136 if (mkdir(p, 0777)) { 137 perror(p); 138 exit(2); 139 } 140 } 141#endif 142 else 143 old_config_present++; 144 145 loadaddress = -1; 146 dtab = NULL; 147 confp = &conf_list; 148 compp = &comp_list; 149 if (yyparse()) 150 exit(3); 151 switch (machine) { 152 153 case MACHINE_VAX: 154 vax_ioconf(); /* Print ioconf.c */ 155 ubglue(); /* Create ubglue.s */ 156 break; 157 158 case MACHINE_TAHOE: 159 tahoe_ioconf(); 160 vbglue(); 161 break; 162 163 case MACHINE_HP300: 164 case MACHINE_LUNA68K: 165 hp300_ioconf(); 166 hpglue(); 167 break; 168 169 case MACHINE_I386: 170 i386_ioconf(); /* Print ioconf.c */ 171 vector(); /* Create vector.s */ 172 break; 173 174 case MACHINE_MIPS: 175 case MACHINE_PMAX: 176 pmax_ioconf(); 177 break; 178 179 case MACHINE_NEWS3400: 180 news_ioconf(); 181 break; 182 183 default: 184 printf("Specify machine type, e.g. ``machine vax''\n"); 185 exit(1); 186 } 187 /* 188 * make symbolic links in compilation directory 189 * for "sys" (to make genassym.c work along with #include <sys/xxx>) 190 * and similarly for "machine". 191 */ 192 { 193 char xxx[80]; 194 195 (void) sprintf(xxx, "../../%s/include", machinename); 196 (void) symlink(xxx, path("machine")); 197 } 198 options(); /* make options .h files */ 199 makefile(); /* build Makefile */ 200 headers(); /* make a lot of .h files */ 201 swapconf(); /* swap config files */ 202 printf("Kernel build directory is %s\n", p); 203 exit(0); 204} 205 206/* 207 * get_word 208 * returns EOF on end of file 209 * NULL on end of line 210 * pointer to the word otherwise 211 */ 212char * 213get_word(fp) 214 register FILE *fp; 215{ 216 static char line[80]; 217 register int ch; 218 register char *cp; 219 int escaped_nl = 0; 220 221begin: 222 while ((ch = getc(fp)) != EOF) 223 if (ch != ' ' && ch != '\t') 224 break; 225 if (ch == EOF) 226 return ((char *)EOF); 227 if (ch == '\\'){ 228 escaped_nl = 1; 229 goto begin; 230 } 231 if (ch == '\n') 232 if (escaped_nl){ 233 escaped_nl = 0; 234 goto begin; 235 } 236 else 237 return (NULL); 238 cp = line; 239 *cp++ = ch; 240 while ((ch = getc(fp)) != EOF) { 241 if (isspace(ch)) 242 break; 243 *cp++ = ch; 244 } 245 *cp = 0; 246 if (ch == EOF) 247 return ((char *)EOF); 248 (void) ungetc(ch, fp); 249 return (line); 250} 251 252/* 253 * get_quoted_word 254 * like get_word but will accept something in double or single quotes 255 * (to allow embedded spaces). 256 */ 257char * 258get_quoted_word(fp) 259 register FILE *fp; 260{ 261 static char line[256]; 262 register int ch; 263 register char *cp; 264 int escaped_nl = 0; 265 266begin: 267 while ((ch = getc(fp)) != EOF) 268 if (ch != ' ' && ch != '\t') 269 break; 270 if (ch == EOF) 271 return ((char *)EOF); 272 if (ch == '\\'){ 273 escaped_nl = 1; 274 goto begin; 275 } 276 if (ch == '\n') 277 if (escaped_nl){ 278 escaped_nl = 0; 279 goto begin; 280 } 281 else 282 return (NULL); 283 cp = line; 284 if (ch == '"' || ch == '\'') { 285 register int quote = ch; 286 287 while ((ch = getc(fp)) != EOF) { 288 if (ch == quote) 289 break; 290 if (ch == '\n') { 291 *cp = 0; 292 printf("config: missing quote reading `%s'\n", 293 line); 294 exit(2); 295 } 296 *cp++ = ch; 297 } 298 } else { 299 *cp++ = ch; 300 while ((ch = getc(fp)) != EOF) { 301 if (isspace(ch)) 302 break; 303 *cp++ = ch; 304 } 305 if (ch != EOF) 306 (void) ungetc(ch, fp); 307 } 308 *cp = 0; 309 if (ch == EOF) 310 return ((char *)EOF); 311 return (line); 312} 313 314/* 315 * prepend the path to a filename 316 */ 317char * 318path(file) 319 char *file; 320{ 321 register char *cp; 322 323#define CDIR "../../compile/" 324 cp = malloc((unsigned int)(sizeof(CDIR) + strlen(PREFIX) + 325 (file ? strlen(file) : 0) + 2)); 326 (void) strcpy(cp, CDIR); 327 (void) strcat(cp, PREFIX); 328 if (file) { 329 (void) strcat(cp, "/"); 330 (void) strcat(cp, file); 331 } 332 return (cp); 333} 334 335/* 336 * moveifchanged -- 337 * compare two files; rename if changed. 338 */ 339void 340moveifchanged(const char *from_name, const char *to_name) 341{ 342 char *p, *q; 343 int changed; 344 size_t tsize; 345 struct stat from_sb, to_sb; 346 int from_fd, to_fd; 347 348 changed = 0; 349 350 if ((from_fd = open(from_name, O_RDONLY)) < 0) 351 err(EX_OSERR, "moveifchanged open(%s)", from_name); 352 353 if ((to_fd = open(to_name, O_RDONLY)) < 0) 354 changed++; 355 356 if (!changed && fstat(from_fd, &from_sb) < 0) 357 err(EX_OSERR, "moveifchanged fstat(%s)", from_name); 358 359 if (!changed && fstat(to_fd, &to_sb) < 0) 360 err(EX_OSERR, "moveifchanged fstat(%s)", to_name); 361 362 if (!changed && from_sb.st_size != to_sb.st_size) 363 changed++; 364 365 tsize = (size_t)from_sb.st_size; 366 367 if (!changed) { 368 p = mmap(NULL, tsize, PROT_READ, 0, from_fd, (off_t)0); 369 if ((long)p == -1) 370 err(EX_OSERR, "mmap %s", from_name); 371 q = mmap(NULL, tsize, PROT_READ, 0, to_fd, (off_t)0); 372 if ((long)q == -1) 373 err(EX_OSERR, "mmap %s", to_name); 374 375 changed = memcmp(p, q, tsize); 376 munmap(p, tsize); 377 munmap(q, tsize); 378 } 379 if (changed) { 380 if (rename(from_name, to_name) < 0) 381 err(EX_OSERR, "rename(%s, %s)", from_name, to_name); 382 } else { 383 if (unlink(from_name) < 0) 384 err(EX_OSERR, "unlink(%s, %s)", from_name); 385 } 386 387#ifdef DIAG 388 if (changed) 389 printf("CHANGED! rename (%s, %s)\n", from_name, to_name); 390 else 391 printf("SAME! unlink (%s)\n", from_name); 392#endif 393 394 return; 395} 396