1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "ucc.h"
5 
6 #define _P_WAIT 0
7 #define UCCDIR "/usr/local/lib/ucc/"
8 
9 static char *CPPProg[] =
10 {
11 	"/usr/bin/gcc", "-U__GNUC__", "-D_POSIX_SOURCE", "-D__STRICT_ANSI__",
12 	"-Dunix", "-Di386", "-Dlinux", "-D__unix__", "-D__i386__", "-D__linux__",
13 	"-D__signed__=signed", "-D_UCC", "-I" UCCDIR "include", "$1", "-E", "$2", "-o", "$3", 0
14 };
15 static char *CCProg[] =
16 {
17 	UCCDIR "ucl", "-ext:.s", "$1", "$2", 0
18 };
19 static char *ASProg[] =
20 {
21 	"/usr/bin/as", "-o", "$3", "$1", "$2", 0
22 };
23 static char *LDProg[] =
24 {
25 	"/usr/bin/gcc", "-o", "$3", "$1", "$2", UCCDIR "assert.o", "-lc", "-lm", 0
26 };
27 char *ExtNames[] = { ".c", ".i", ".s", ".o", ".a;.so", 0 };
28 
Execute(char ** cmd)29 static int Execute(char **cmd)
30 {
31 	int pid, n, status;
32 
33 	pid = fork();
34 	if (pid == -1)
35 	{
36 		fprintf(stderr, "no more processes\n");
37 		return 100;
38 	}
39 	else if (pid == 0)
40 	{
41 		execv(cmd[0], cmd);
42 		perror(cmd[0]);
43 		fflush(stdout);
44 		exit(100);
45 	}
46 	while ((n = wait(&status)) != pid && n != -1)
47 		;
48 	if (n == -1)
49 		status = -1;
50 	if (status & 0xff)
51 	{
52 		fprintf(stderr, "fatal error in %s\n", cmd[0]);
53 		status |= 0x100;
54 	}
55 
56 	return (status >> 8) & 0xff;
57 }
58 
SetupToolChain(void)59 void SetupToolChain(void)
60 {
61 }
62 
InvokeProgram(int oftype)63 int InvokeProgram(int oftype)
64 {
65 	List p, il, ol;
66 	char *ofname;
67 	char **cmd;
68 	int status = 0;
69 
70 	switch (oftype)
71 	{
72 	case PP_FILE:
73 		for (p = Option.cfiles; p != NULL; p = p->next)
74 		{
75 			ofname = FileName(p->str, ".i");
76 			PPFiles = ListAppend(PPFiles, ofname);
77 			il = ListAppend(NULL, p->str);
78 			ol = ListAppend(NULL, ofname);
79 			cmd = BuildCommand(CPPProg, Option.pflags, il, ol);
80 			status = Execute(cmd);
81 		}
82 		Option.pfiles = ListCombine(Option.pfiles, PPFiles);
83 		break;
84 
85 	case ASM_FILE:
86 		if (Option.pfiles == NULL)
87 			return 0;
88 
89 		for (p = Option.aflags, Option.aflags = NULL; p != NULL; p = p->next)
90 		{
91 			Option.aflags = ListCombine(Option.aflags, ParseOption(p->str + 4));
92 		}
93 		for (p = Option.pfiles; p != NULL; p = p->next)
94 		{
95 			ASMFiles = ListAppend(ASMFiles, FileName(p->str, ".s"));
96 		}
97 		Option.afiles = ListCombine(Option.afiles, ASMFiles);
98 		cmd = BuildCommand(CCProg, Option.cflags, Option.pfiles, ASMFiles);
99 		status = Execute(cmd);
100 		break;
101 
102 	case OBJ_FILE:
103 		for (p = Option.afiles; p != NULL; p = p->next)
104 		{
105 			ofname = FileName(p->str, ".o");
106 			OBJFiles = ListAppend(OBJFiles, ofname);
107 			il = ListAppend(NULL, p->str);
108 			ol = ListAppend(NULL, ofname);
109 			cmd = BuildCommand(ASProg, Option.aflags, il, ol);
110 			status = Execute(cmd);
111 		}
112 		Option.ofiles = ListCombine(Option.ofiles, OBJFiles);
113 		break;
114 
115 	case LIB_FILE:
116 		return 0;
117 
118 	case EXE_FILE:
119 		if (Option.ofiles == NULL)
120 			return 0;
121 
122 		if (Option.out == NULL)
123 			Option.out = "a.out";
124 		cmd = BuildCommand(LDProg, Option.lflags, Option.linput, ListAppend(NULL, Option.out));
125 		status = Execute(cmd);
126 		break;
127 	}
128 
129 	return status;
130 }
131 
132