From de57335296361ebb6fb82ff32320c79bb8c67f6c Mon Sep 17 00:00:00 2001 From: carl Date: Sun, 17 Mar 2019 22:42:58 +0800 Subject: [PATCH] Try to update frontend driver. --- fast/driver/driver.c | 939 +++++++++++++++++++++++-------------------- 1 file changed, 493 insertions(+), 446 deletions(-) diff --git a/fast/driver/driver.c b/fast/driver/driver.c index 3f4388909..6499683f7 100644 --- a/fast/driver/driver.c +++ b/fast/driver/driver.c @@ -1,14 +1,14 @@ /* fcc/fm2/fpc - Driver for fast ACK compilers. + Driver for fast ACK compilers. - Derived from the C compiler driver from Minix. + Derived from the C compiler driver from Minix. - Compile this file with - cc -O -I/config -DF?? driver.c - where F?? is either FCC, FPC, or FM2. - Install the resulting binaries in the EM bin directory. - Suggested names: afcc, afm2, and afpc. -*/ + Compile this file with + cc -O -I/config -DF?? driver.c + where F?? is either FCC, FPC, or FM2. + Install the resulting binaries in the EM bin directory. + Suggested names: afcc, afm2, and afpc. + */ #if FM2+FPC+FCC > 1 Something wrong here! Only one of FM2, FPC, or FCC must be defined @@ -29,70 +29,76 @@ Something wrong here! Only one of FM2, FPC, or FCC must be defined #define SYSNAME "i386" #endif +#include +#include #include #include #include -#include -#if __STDC__ #include +#include +#include +#include "em_path.h" +#include "system.h" + +#if(CHAR_BIT!=8) +We do not support machines with non 8-bit characters. +#endif + +/* Create em based compiler if otherwise not specified. + * Size of EM machine depends on current machine. */ +#ifndef MACHNAME +#if(INT_MAX==32767) +#define MACHNAME "em22" +#define SYSNAME "em22" #else -#include +#define MACHNAME "em44" +#define SYSNAME "em44" +#endif #endif /* - Version producing ACK .o files in one pass. -*/ + Version producing ACK .o files in one pass. + */ #define MAXARGC 256 /* maximum number of arguments allowed in a list */ -#define USTR_SIZE 128 /* maximum length of string variable */ +#define USTR_SIZE FILENAME_MAX /* maximum length of string variable */ typedef char USTRING[USTR_SIZE]; -struct arglist { +struct arglist +{ int al_argc; char *al_argv[MAXARGC]; }; -#define CPP_NAME "$H/lib.bin/cpp" -#define LD_NAME "$H/lib.bin/em_led" -#define CV_NAME "$H/lib.bin/$S/cv" -#define SHELL "/bin/sh" +#define CPP_NAME "$H/lib/ack/em_cpp" +#define LD_NAME "$H/lib/ack/em_ass" +#define CV_NAME "$H/lib/ack/$S/cv" -char *CPP; -char *COMP; -char *cc = "cc"; +static char *CPP; +static char *COMP; +static char *cc = "cc"; -int kids = -1; -int ecount = 0; +static int kids = -1; +static int ecount = 0; -struct arglist CPP_FLAGS = { +struct arglist CPP_FLAGS = +{ #ifdef FCC - 7, + 7, #else - 13, + 13, #endif - { - "-D__unix", - "-D_EM_WSIZE=4", - "-D_EM_PSIZE=4", - "-D_EM_SSIZE=2", - "-D_EM_LSIZE=4", - "-D_EM_FSIZE=4", - "-D_EM_DSIZE=8", + { "-D__unix", "-D_EM_WSIZE=4", "-D_EM_PSIZE=4", "-D_EM_SSIZE=2", + "-D_EM_LSIZE=4", "-D_EM_FSIZE=4", "-D_EM_DSIZE=8", #ifndef FCC - "-DEM_WSIZE=4", - "-DEM_PSIZE=4", - "-DEM_SSIZE=2", - "-DEM_LSIZE=4", - "-DEM_FSIZE=4", - "-DEM_DSIZE=8", + "-DEM_WSIZE=4", "-DEM_PSIZE=4", "-DEM_SSIZE=2", "-DEM_LSIZE=4", + "-DEM_FSIZE=4", "-DEM_DSIZE=8", #endif - } -}; + } }; -struct arglist LD_HEAD = { - 2, - { - "$H/lib/$S/head_em", +struct arglist LD_HEAD = +{ 2, +{ "$H/lib/$S/head_em", #ifdef FCC "$H/lib/$S/head_$A" #endif @@ -102,36 +108,34 @@ struct arglist LD_HEAD = { #ifdef FPC "$H/lib/$S/head_pc" #endif - } -}; +}}; -struct arglist LD_TAIL = { +struct arglist LD_TAIL = + { #if defined(sun3) || defined(i386) - 5, + 5, #else - 4, + 4, #endif - { + { #ifdef FCC - "$H/lib/$S/tail_$A", + "$H/lib/$S/tail_$A", #endif #ifdef FM2 - "$H/lib/$S/tail_m2", + "$H/lib/$S/tail_m2", #endif #ifdef FPC - "$H/lib/$S/tail_pc", + "$H/lib/$S/tail_pc", #endif #if defined(sun3) || defined(i386) - "$H/lib/$M/tail_fp", + "$H/lib/$M/tail_fp", #endif - "$H/lib/$M/tail_em", - "$H/lib/$S/tail_mon", - "$H/lib/$M/end_em" - } -}; + "$H/lib/$M/tail_em", "$H/lib/$S/tail_mon", + "$H/lib/$M/end_em" } }; -struct arglist align = { - 5, { +struct arglist align = +{ 5, +{ #ifdef sun3 "-a0:4", "-a1:4", @@ -153,112 +157,117 @@ struct arglist align = { "-a3:4", "-b1:0x1880000" #endif - } -}; + } }; struct arglist COMP_FLAGS; -char *o_FILE = "a.out"; /* default name for executable file */ +static char *o_FILE = "a.out"; /* default name for executable file */ + -#define remove(str) ((noexec || unlink(str)), (str)[0] = '\0') #define cleanup(str) (str && str[0] && remove(str)) -#define init(al) ((al)->al_argc = 1) +#define init(al) ((al)->al_argc = 0) -char ProgCall[128]; +static char ProgCall[FILENAME_MAX]; -struct arglist SRCFILES; -struct arglist LDFILES; +static struct arglist SRCFILES; +static struct arglist LDFILES; -int RET_CODE = 0; +static int RET_CODE = 0; -struct arglist LD_FLAGS; +/* The environment variable pointing to ACK_HOME */ +static char* ackhome = 0; +/* The environment variable pointing to the temporary directory. */ +static char* tmpdir = 0; +static struct arglist LD_FLAGS; -struct arglist CALL_VEC; +static struct arglist CALL_VEC; -int o_flag = 0; -int c_flag = 0; -int g_flag = 0; -int v_flag = 0; -int O_flag = 0; -int ansi_c = 0; +static int o_flag = 0; +static int c_flag = 0; +static int g_flag = 0; +static int v_flag = 0; +static int O_flag = 0; +/* Only ANSI C is now supported. */ +static int ansi_c = 1; -#if __STDC__ char *mkstr(char *, ...); -#else -char *mkstr(); -#endif -char *malloc(); -char *alloc(); -char *extension(); -char *expand_string(); - -USTRING ofile; -USTRING BASE; -USTRING tmp_file; +static char *alloc(unsigned int); +static char *extension(char *); +static char *expand_string(char *); +static void error(char *, char *, char *); +static void warning(char *, char *, char *); +static void panic(char *); +static void append(register struct arglist *, char *); +static void expand(register struct arglist *); +static void concat(struct arglist *, struct arglist *); +static int runvec(struct arglist *, char *); +static int needsprep(char *); + +static USTRING ofile; +static USTRING BASE; +static char tmp_file[L_tmpnam]; int noexec = 0; -extern char *strcat(), *strcpy(), *mktemp(), *strchr(); - -trapcc(sig) - int sig; +void trapcc(int sig) { - signal(sig, SIG_IGN); - if (kids != -1) kill(kids, sig); cleanup(ofile); cleanup(tmp_file); - exit(1); + if (ackhome != 0) + { + free(ackhome); + } + if (tmpdir != 0) + { + free(tmpdir); + } + exit(EXIT_FAILURE); } #ifdef FCC #define lang_suffix() "c" -#define comp_name() "$H/lib.bin/c_ce" -#define ansi_c_name() "$H/lib.bin/c_ce.ansi" +#define comp_name() "$H/lib/ack/em_cemcom.ansi" +#define ansi_c_name() "$H/lib/ack/em_cemcom.ansi" #endif /* FCC */ #ifdef FM2 #define lang_suffix() "mod" -#define comp_name() "$H/lib.bin/m2_ce" +#define comp_name() "$H/lib/ack/em_m2" #endif /* FM2 */ #ifdef FPC #define lang_suffix() "p" -#define comp_name() "$H/lib.bin/pc_ce" +#define comp_name() "$H/lib/ack/em_pc" #endif /* FPC */ - #ifdef FCC -int -lang_opt(str) - char *str; +int lang_opt(char *str) { - switch(str[1]) { + switch (str[1]) + { case 'R': - if (! ansi_c) { + if (!ansi_c) + { append(&COMP_FLAGS, str); return 1; } break; - case '-': /* debug options */ + case '-': /* debug options */ append(&COMP_FLAGS, str); return 1; - case 'a': /* -ansi flag */ - if (! strcmp(str, "-ansi")) { - ansi_c = 1; - COMP = expand_string(ansi_c_name()); - return 1; - } - break; - case 'w': /* disable warnings */ - if (! ansi_c) { + case 'w': /* disable warnings */ + if (!ansi_c) + { append(&COMP_FLAGS, str); return 1; } - if (str[2]) { + if (str[2]) + { str[1] = '-'; append(&COMP_FLAGS, &str[1]); } - else append(&COMP_FLAGS, "-a"); + else + append(&COMP_FLAGS, "-a"); return 1; } return 0; @@ -266,32 +275,32 @@ lang_opt(str) #endif /* FCC */ #ifdef FM2 -int -lang_opt(str) - char *str; +int lang_opt(char *str) { - switch(str[1]) { - case '-': /* debug options */ - case 'w': /* disable warnings */ - case 'R': /* no runtime checks */ - case 'W': /* add warnings */ - case 'L': /* no line numbers */ - case 'A': /* extra array bound checks */ - case '3': /* only accept 3rd edition Modula-2 */ + switch(str[1]) + { + case '-': /* debug options */ + case 'w': /* disable warnings */ + case 'R': /* no runtime checks */ + case 'W': /* add warnings */ + case 'L': /* no line numbers */ + case 'A': /* extra array bound checks */ + case '3': /* only accept 3rd edition Modula-2 */ append(&COMP_FLAGS, str); return 1; - case 'I': + case 'I': append(&COMP_FLAGS, str); - break; /* !!! */ - case 'U': /* underscores in identifiers allowed */ - if (str[2] == '\0') { + break; /* !!! */ + case 'U': /* underscores in identifiers allowed */ + if (str[2] == '\0') + { append(&COMP_FLAGS, str); return 1; } break; - case 'e': /* local extension for Modula-2 compiler: - procedure constants - */ + case 'e': /* local extension for Modula-2 compiler: + procedure constants + */ str[1] = 'l'; append(&COMP_FLAGS, str); return 1; @@ -301,31 +310,31 @@ lang_opt(str) #endif /* FM2 */ #ifdef FPC -int -lang_opt(str) - char *str; +int lang_opt(char *str) { - switch(str[1]) { - case '-': /* debug options */ - case 'a': /* enable assertions */ - case 'd': /* allow doubles (longs) */ - case 'i': /* set size of integer sets */ - case 't': /* tracing */ - case 'w': /* disable warnings */ - case 'A': /* extra array bound checks */ - case 'C': /* distinguish between lower case and upper case */ - case 'L': /* no FIL and LIN instructions */ - case 'R': /* no runtime checks */ + switch(str[1]) + { + case '-': /* debug options */ + case 'a': /* enable assertions */ + case 'd': /* allow doubles (longs) */ + case 'i': /* set size of integer sets */ + case 't': /* tracing */ + case 'w': /* disable warnings */ + case 'A': /* extra array bound checks */ + case 'C': /* distinguish between lower case and upper case */ + case 'L': /* no FIL and LIN instructions */ + case 'R': /* no runtime checks */ append(&COMP_FLAGS, str); return 1; - case 'u': - case 'U': + case 'u': + case 'U': /* underscores in identifiers */ - case 's': + case 's': /* only compile standard pascal */ - case 'c': + case 'c': /* C type strings */ - if (str[2] == '+' && str[3] == '\0') { + if (str[2] == '+' && str[3] == '\0') + { str[2] = 0; append(&COMP_FLAGS, str); return 1; @@ -335,23 +344,63 @@ lang_opt(str) } #endif /* FPC */ -main(argc, argv) - char *argv[]; +char* stringdup(const char* s) +{ + if (s == NULL) + return NULL; + char *p = alloc(strlen(s) + sizeof(char)); + strcpy(p, s); + return p; +} + +char* getackhome(void) +{ + char *value = stringdup(getenv("ACK_HOME")); + if (value == NULL) + { +#ifndef EM_DIR + panic("ACK_HOME must be set."); +#else + return stringdup(EM_DIR); +#endif + } + return value; +} + + + + +int main(int argc, char *argv[]) { char *str; char **argvec; int count; char *ext; + FILE* fd; register struct arglist *call = &CALL_VEC; char *file; char *ldfile; - char *INCLUDE = 0; + char *INCLUDE = NULL; int compile_cnt = 0; + ackhome = getackhome(); + if (ackhome == NULL) + { + panic("ACK_HOME Environment variable is not set."); + } + tmpdir = sys_gettmpdir(); + if (tmpdir == NULL) + { + panic("TMPDIR Environment variable is not set."); + } + setbuf(stdout, (char *) 0); - basename(*argv++,ProgCall); + /* get basebame of application. */ + sys_basename(*argv++, ProgCall); + /* get compiler to use. */ COMP = expand_string(comp_name()); + /* get c pre-processor to use */ CPP = expand_string(CPP_NAME); #ifdef vax4 @@ -365,99 +414,101 @@ main(argc, argv) append(&CPP_FLAGS, "-D__mc68000"); #endif - if (signal(SIGHUP, SIG_IGN) != SIG_IGN) - signal(SIGHUP, trapcc); if (signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, trapcc); - if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) - signal(SIGQUIT, trapcc); - while (--argc > 0) { - if (*(str = *argv++) != '-') { + if (signal(SIGTERM, SIG_IGN) != SIG_IGN) + signal(SIGTERM, trapcc); + while (--argc > 0) + { + if (*(str = *argv++) != '-') + { append(&SRCFILES, str); continue; } - if (lang_opt(str)) { + if (lang_opt(str)) + { } - else switch (str[1]) { - - case 'c': /* stop after producing .o files */ - c_flag = 1; - break; - case 'D': /* preprocessor #define */ - case 'U': /* preprocessor #undef */ - append(&CPP_FLAGS, str); - break; - case 'I': /* include directory */ - append(&CPP_FLAGS, str); - break; - case 'g': /* debugger support */ - append(&COMP_FLAGS, str); - g_flag = 1; - break; - case 'a': /* -ansi flag */ - if (! strcmp(str, "-ansi")) { - ansi_c = 1; - return 1; - } - break; - case 'o': /* target file */ - if (argc-- >= 0) { - o_flag = 1; - o_FILE = *argv++; - ext = extension(o_FILE); - if (ext != o_FILE && ! strcmp(ext, lang_suffix()) - ) { - error("-o would overwrite %s", o_FILE); + else + switch (str[1]) + { + + case 'c': /* stop after producing .o files */ + c_flag = 1; + break; + case 'D': /* preprocessor #define */ + case 'U': /* preprocessor #undef */ + append(&CPP_FLAGS, str); + break; + case 'I': /* include directory */ + append(&CPP_FLAGS, str); + break; + case 'g': /* debugger support */ + append(&COMP_FLAGS, str); + g_flag = 1; + break; + case 'o': /* target file */ + if (argc-- >= 0) + { + o_flag = 1; + o_FILE = *argv++; + ext = extension(o_FILE); + if (ext != o_FILE && !strcmp(ext, lang_suffix())) + { + error("-o would overwrite %s", o_FILE, NULL); + } } - } - break; - case 'u': /* mark identifier as undefined */ - append(&LD_FLAGS, str); - if (argc-- >= 0) - append(&LD_FLAGS, *argv++); - break; - case 'O': /* use built in peephole optimizer */ - O_flag = 1; - break; - case 'v': /* verbose */ - v_flag++; - if (str[2] == 'n') - noexec = 1; - break; - case 'l': /* library file */ - append(&SRCFILES, str); - break; - case 'M': /* use other compiler (for testing) */ - strcpy(COMP, str+2); - break; - case 's': /* strip */ - if (str[2] == '\0') { + break; + case 'u': /* mark identifier as undefined */ append(&LD_FLAGS, str); + if (argc-- >= 0) + append(&LD_FLAGS, *argv++); + break; + case 'O': /* use built in peephole optimizer */ + O_flag = 1; + break; + case 'v': /* verbose */ + v_flag++; + if (str[2] == 'n') + noexec = 1; + break; + case 'l': /* library file */ + append(&SRCFILES, str); + break; + case 'M': /* use other compiler (for testing) */ + strcpy(COMP, str + 2); + break; + case 's': /* strip */ + if (str[2] == '\0') + { + append(&LD_FLAGS, str); + break; + } + /* fall through */ + default: + warning("%s flag ignored", str, NULL); break; } - /* fall through */ - default: - warning("%s flag ignored", str); - break; - } } - if (ecount) exit(1); + if (ecount) + exit(EXIT_FAILURE); count = SRCFILES.al_argc; argvec = &(SRCFILES.al_argv[0]); - while (count-- > 0) { + while (count-- > 0) + { ext = extension(*argvec); - if (*argvec[0] != '-' && - ext != *argvec++ && (! strcmp(ext, lang_suffix()) - )) { + if (*argvec[0] != '-' && ext != *argvec++ + && (!strcmp(ext, lang_suffix()))) + { compile_cnt++; } } - if (compile_cnt > 1 && c_flag && o_flag) { - warning("-o flag ignored"); + if (compile_cnt > 1 && c_flag && o_flag) + { + warning("-o flag ignored", NULL, NULL); o_flag = 0; } @@ -465,64 +516,95 @@ main(argc, argv) INCLUDE = expand_string("-I$H/lib/m2"); #endif /* FM2 */ #ifdef FCC - INCLUDE = expand_string(ansi_c ? "-I$H/include/tail_ac" : "-I$H/include/_tail_cc"); +/* INCLUDE = expand_string( + ansi_c ? "-I$H/include/tail_ac" : "-I$H/include/_tail_cc"); */ + INCLUDE = expand_string("-I$H/share/ack/include"); append(&COMP_FLAGS, "-L"); #endif /* FCC */ count = SRCFILES.al_argc; argvec = &(SRCFILES.al_argv[0]); - while (count-- > 0) { + + /* For each source file... */ + while (count-- > 0) + { register char *f; - basename(file = *argvec++, BASE); + sys_basename(file = *argvec++, BASE); ext = extension(file); - if (file[0] != '-' && - ext != file && (!strcmp(ext, lang_suffix()) - )) { - if (compile_cnt > 1) printf("%s\n", file); + /* if not standard input and file is equal to the supported language suffix. */ + if (file[0] != '-' && ext != file && (!strcmp(ext, lang_suffix()))) + { + if (compile_cnt > 1) + printf("%s\n", file); - ldfile = c_flag ? ofile : alloc((unsigned)strlen(BASE)+3); + ldfile = c_flag ? ofile : alloc((unsigned) strlen(BASE) + 3); + + /**************************************************** + * Run C preprocessor on the source files and save + * result in temporary output file. + ****************************************************/ if ( #ifdef FCC - !strcmp(ext, "s") && +/* (!strcmp(ext, "s")) && */ #endif - needsprep(file)) { - strcpy(tmp_file, TMP_DIR); - strcat(tmp_file, "/F_XXXXXX"); - mktemp(tmp_file); + needsprep(file)) + { + if (sys_tmpnam(tmp_file)==NULL) + { + panic("Cannot get temporary filename."); + } + fd = fopen(tmp_file,"w+"); + if (fd==NULL) + { + fclose(fd); + panic("Cannot write temporary file."); + } init(call); append(call, CPP); concat(call, &CPP_FLAGS); append(call, INCLUDE); append(call, file); - if (runvec(call, tmp_file)) { + if (runvec(call, tmp_file)==EXIT_SUCCESS) + { + /* The input file is now the preprocessor + * output file. + */ file = tmp_file; } - else { + else + { remove(tmp_file); tmp_file[0] = '\0'; continue; } } + + /**************************************************** + * Compile the source file. + ****************************************************/ init(call); - if (o_flag && c_flag) { + if (o_flag && c_flag) + { f = o_FILE; } - else f = mkstr(ldfile, BASE, ".", "o", (char *)0); - append(call, COMP); -#ifdef FCC - concat(call, &CPP_FLAGS); -#endif - concat(call, &COMP_FLAGS); -#if FM2 || FCC - append(call, INCLUDE); + else + { + f = mkstr(ldfile, BASE, ".", "o", (char *) 0); + } + append(call, COMP); + concat(call, &COMP_FLAGS); +#ifdef FM2 + append(call, INCLUDE); #endif - append(call, file); - append(call, f); - if (runvec(call, (char *) 0)) { + append(call, file); + append(call, f); + if (runvec(call, (char *) 0)==EXIT_SUCCESS) + { file = f; } - else { + else + { remove(f); continue; } @@ -530,97 +612,109 @@ main(argc, argv) tmp_file[0] = '\0'; } - else if (file[0] != '-' && - strcmp(ext, "o") && strcmp(ext, "a")) { - warning("file with unknown suffix (%s) passed to the loader", ext); + else if (file[0] != '-' && strcmp(ext, "o") && strcmp(ext, "a")) + { + warning("file with unknown suffix (%s) passed to the loader", ext, + NULL); } if (c_flag) continue; + /* Add the output object file to the list of files to link. */ append(&LDFILES, file); } /* *.s to a.out */ - if (RET_CODE == 0 && LDFILES.al_argc > 0) { + if (RET_CODE == 0 && LDFILES.al_argc > 0) + { init(call); - expand(&LD_HEAD); - cc = "cc.2g"; - expand(&LD_TAIL); +// expand(&LD_HEAD); +// cc = "cc.2g"; +// expand(&LD_TAIL); append(call, expand_string(LD_NAME)); - concat(call, &align); +// concat(call, &align); append(call, "-o"); - strcpy(tmp_file, TMP_DIR); - strcat(tmp_file, "/F_XXXXXX"); - mktemp(tmp_file); + + if (sys_tmpnam(tmp_file)==NULL) + { + panic("Cannot get temporary filename."); + } append(call, tmp_file); - concat(call, &LD_HEAD); - concat(call, &LD_FLAGS); +// concat(call, &LD_HEAD); +// concat(call, &LD_FLAGS); concat(call, &LDFILES); - if (g_flag) append(call, expand_string("$H/lib/$M/tail_db")); +/* if (g_flag) + append(call, expand_string("$H/lib/$M/tail_db")); #ifdef FCC - if (! ansi_c) append(call, expand_string("$H/lib/$S/tail_cc.1s")); + if (!ansi_c) + append(call, expand_string("$H/lib/$S/tail_cc.1s")); #endif - concat(call, &LD_TAIL); - if (! runvec(call, (char *) 0)) { + concat(call, &LD_TAIL);*/ + if (runvec(call, (char *) 0)==EXIT_FAILURE) + { cleanup(tmp_file); exit(RET_CODE); } - init(call); + /* init(call); append(call, expand_string(CV_NAME)); append(call, tmp_file); append(call, o_FILE); - runvec(call, (char *) 0); + runvec(call, (char *) 0);*/ cleanup(tmp_file); } exit(RET_CODE); } -needsprep(name) - char *name; +static int needsprep(char *name) { - int file; +/* FILE *file; char fc; - file = open(name,0); - if (file < 0) return 0; - if (read(file, &fc, 1) != 1) fc = 0; - close(file); - return fc == '#'; + file = fopen(name, "r"); + if (file == 0) + return 0; + if (fread(file, &fc, 1) != 1) + fc = 0; + fclose(file); + return fc == '#';*/ + return 1; } -char * -alloc(u) - unsigned u; +static char * alloc(unsigned int u) { char *p = malloc(u); - if (p == 0) + if (p == NULL) panic("no space"); return p; } -char * -expand_string(s) - char *s; +static char * expand_string(char *s) { - char buf[1024]; - register char *p = s; - register char *q = &buf[0]; + char buf[1024]; + register char *p = s; + register char *q = &buf[0]; int expanded = 0; - if (!p) return p; - while (*p) { - if (*p == '$') { + if (!p) + return p; + while (*p) + { + if (*p == '$') + { p++; expanded = 1; - switch(*p++) { + switch (*p++) + { case 'A': - if (ansi_c) strcpy(q, "ac"); - else strcpy(q, cc); + if (ansi_c) + strcpy(q, "ac"); + else + strcpy(q, cc); break; case 'H': - strcpy(q, EM_DIR); + strcpy(q, ackhome); break; case 'M': strcpy(q, MACHNAME); @@ -632,55 +726,55 @@ expand_string(s) panic("internal error"); break; } - while (*q) q++; + while (*q) + q++; } - else *q++ = *p++; + else + *q++ = *p++; } - if (! expanded) return s; + if (!expanded) + return s; *q++ = '\0'; p = alloc((unsigned int) (q - buf)); return strcpy(p, buf); } -append(al, arg) - register struct arglist *al; - char *arg; +static void append(register struct arglist *al, char *arg) { - if (!arg || !*arg) return; + if (!arg || !*arg) + return; if (al->al_argc >= MAXARGC) panic("argument list overflow"); al->al_argv[(al->al_argc)++] = arg; } -expand(al) - register struct arglist *al; +static void expand(register struct arglist *al) { register int i = al->al_argc; register char **p = &(al->al_argv[0]); - while (i-- > 0) { + while (i-- > 0) + { *p = expand_string(*p); p++; } } -concat(al1, al2) - struct arglist *al1, *al2; +static void concat(struct arglist *al1, struct arglist *al2) { - register i = al2->al_argc; + register int i = al2->al_argc; register char **p = &(al1->al_argv[al1->al_argc]); register char **q = &(al2->al_argv[0]); if ((al1->al_argc += i) >= MAXARGC) panic("argument list overflow"); - while (i-- > 0) { + while (i-- > 0) + { *p++ = *q++; } } -#if __STDC__ -/*VARARGS*/ -char * -mkstr(char *dst, ...) + +char *mkstr(char *dst, ...) { va_list ap; @@ -692,8 +786,10 @@ mkstr(char *dst, ...) q = dst; p = va_arg(ap, char *); - while (p) { - while (*q++ = *p++); + while (p) + { + while ((*q++ = *p++) != 0) + ; q--; p = va_arg(ap, char *); } @@ -702,106 +798,92 @@ mkstr(char *dst, ...) return dst; } -#else -/*VARARGS*/ -char * -mkstr(va_alist) - va_dcl + +static char * extension(char *fn) { - va_list ap; - char *dst; + register char *c = fn; - va_start(ap); + while (*c++) + ; + while (*--c != '.' && c >= fn) { - register char *p; - register char *q; - - dst = q = va_arg(ap, char *); - p = va_arg(ap, char *); - - while (p) { - while (*q++ = *p++); - q--; - p = va_arg(ap, char *); - } } - va_end(ap); - - return dst; + if (c++ < fn || !*c) + return fn; + return c; } -#endif -basename(str, dst) - char *str; - register char *dst; + +/* Converts an argument structure to a string. + * The memory allocated should be freed by + * the caller. + * */ +char *arg2str(struct arglist *vec) { - register char *p1 = str; - register char *p2 = p1; - - while (*p1) - if (*p1++ == '/') - p2 = p1; - p1--; - while (*p1 != '.' && p1 >= p2) p1--; - if (p1 >= p2) { - *p1 = '\0'; - while (*dst++ = *p2++); - *p1 = '.'; + int i; + unsigned int length; + char *p; + + length = 0; + for (i = 0; i < vec->al_argc; i++) + { + length += strlen(vec->al_argv[i]); } - else - while (*dst++ = *p2++); + /* Add space character */ + length += vec->al_argc * sizeof(char); + p = alloc(length + sizeof(char)); + *p = '\0'; + for (i = 0; i < vec->al_argc; i++) + { + strcat(p, vec->al_argv[i]); + strcat(p, " "); + } + return p; } -char * -extension(fn) - char *fn; +/* Run the system command, and outputs to "outp" if not + * NULL. + */ +static int runvec(struct arglist *vec, char *outp) { - register char *c = fn; + int status; + char *p; + char *redirect; - while (*c++) ; - while (*--c != '.' && c >= fn) { } - if (c++ < fn || !*c) return fn; - return c; -} + redirect = NULL; -runvec(vec, outp) - struct arglist *vec; - char *outp; -{ - int pid, status; + /* Check if a command interpreter is available. */ + if (system(NULL) == 0) + { + panic("No command interpreter available!"); + } + if(outp != NULL) + { + redirect = alloc(strlen(outp)+sizeof(char)*2); + strcpy(redirect,"1>"); + strcat(redirect,outp); + append(vec,redirect); + } + p = arg2str(vec); - if (v_flag) { - pr_vec(vec); - putc('\n', stderr); + status = system(p); + free(p); + if (outp != NULL) + { + free(redirect); } - if ((pid = fork()) == 0) { /* start up the process */ - if (outp) { /* redirect standard output */ - close(1); - if (creat(outp, 0666) != 1) - panic("cannot create output file"); - } - ex_vec(vec); + if (status == 0) + { + RET_CODE = EXIT_SUCCESS; } - if (pid == -1) - panic("no more processes"); - kids = pid; - wait(&status); - if (status) switch(status & 0177) { - case SIGHUP: - case SIGINT: - case SIGQUIT: - case SIGTERM: - case 0: - break; - default: - error("%s died with signal %d\n", vec->al_argv[1], status&0177); + else + { + RET_CODE = EXIT_FAILURE; } - kids = -1; - return status ? ((RET_CODE = 1), 0) : 1; + return status; } /*VARARGS1*/ -error(str, s1, s2) - char *str, *s1, *s2; +static void error(char *str, char *s1, char *s2) { fprintf(stderr, "%s: ", ProgCall); fprintf(stderr, str, s1, s2); @@ -810,51 +892,16 @@ error(str, s1, s2) } /*VARARGS1*/ -warning(str, s1, s2) - char *str, *s1, *s2; +static void warning(char *str, char *s1, char *s2) { fprintf(stderr, "%s: (warning) ", ProgCall); fprintf(stderr, str, s1, s2); putc('\n', stderr); } -panic(str) - char *str; +static void panic(char *str) { - error(str); + error(str, NULL, NULL); trapcc(SIGINT); } -pr_vec(vec) - register struct arglist *vec; -{ - register char **ap = &vec->al_argv[1]; - - vec->al_argv[vec->al_argc] = 0; - fprintf(stderr, "%s", *ap); - while (*++ap) { - fprintf(stderr, " %s", *ap); - } -} - -extern int errno; - -ex_vec(vec) - register struct arglist *vec; -{ - if (noexec) - exit(0); - vec->al_argv[vec->al_argc] = 0; - execv(vec->al_argv[1], &(vec->al_argv[1])); - if (errno == ENOEXEC) { /* not an a.out, try it with the SHELL */ - vec->al_argv[0] = SHELL; - execv(SHELL, &(vec->al_argv[0])); - } - if (access(vec->al_argv[1], 1) == 0) { - /* File is executable. */ - error("cannot execute %s", vec->al_argv[1]); - } else { - error("%s is not executable", vec->al_argv[1]); - } - exit(1); -} -- 2.34.1