-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler_windows.c
139 lines (135 loc) · 4.15 KB
/
compiler_windows.c
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
/* compiler_windows.c */
#include <Windows.h>
void fatal_stop(const char* message)
{
fprintf(stderr,"%s: fatal error: %s\n",PROGRAM_NAME,message);
ExitProcess(1);
}
int lookup_ext(const char** ext,const char* source)
{
int i;
int top;
int length;
const char* useSource;
HANDLE fFindInfo;
WIN32_FIND_DATA findData;
/* set up out parameter */
top = 0;
*ext = NULL;
/* find file name part of source string */
length = strlen(source);
i = length - 1;
while (i>=0 && (source[i]!='\\' || source[i]!='/'))
--i;
++i;
useSource = source+i;
/* open needed directory */
if (i > 0) {
/* needed directory is specified in source string */
stringbuf part;
init_stringbuf(&part);
assign_stringbuf_ex(&part,source,i); /* assign filePath\\ */
concat_stringbuf(&part,"*"); /* concat to filePath\\* */
fFindInfo = FindFirstFile(part.buffer,&findData);
destroy_stringbuf(&part);
}
else
/* needed directory is the current working directory */
fFindInfo = FindFirstFile(".\\*",&findData);
length = strlen(useSource);
/* cycle through the directory's listing if it was successfully opened */
if (fFindInfo != INVALID_HANDLE_VALUE) {
do {
if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
int len;
const char* pext;
/* find extension */
len = strlen(findData.cFileName);
pext = findData.cFileName+len;
while (len>0 && *pext!='.') {
--len;
--pext;
}
/* check to see if prefix.ext matches prefix */
if (top<MAX_EXTENSIONS && *pext=='.' && len==length && strncmp(useSource,findData.cFileName,length)==0) {
/* see if the extension denotes a defined target */
ext[top] = check_extension(pext);
if (ext[top] != NULL)
++top;
}
}
} while (FindNextFile(fFindInfo,&findData) != 0);
FindClose(fFindInfo);
}
else
fatal_stop("could not open needed directory");
return top;
}
int check_file(const char* fileName)
{
DWORD dwAttribs;
dwAttribs = GetFileAttributes(fileName);
if (dwAttribs == INVALID_FILE_ATTRIBUTES) {
if (GetLastError() == ERROR_ACCESS_DENIED)
return FILE_CHECK_ACCESS_DENIED;
return FILE_CHECK_DOES_NOT_EXIST;
}
if (dwAttribs & FILE_ATTRIBUTE_DIRECTORY)
return FILE_CHECK_NOT_REGULAR_FILE;
return FILE_CHECK_SUCCESS;
}
int invoke_compiler(const char* compilerName,const char* arguments,const char* redirect)
{
int i;
HANDLE hFile;
DWORD exitCode;
stringbuf cmdLine;
STARTUPINFO startInfo;
SECURITY_ATTRIBUTES secattribs;
PROCESS_INFORMATION processInfo;
/* compile the command line (arguments are separated by zero bytes and contains program name) */
init_stringbuf(&cmdLine);
assign_stringbuf(&cmdLine,compilerName);
i = strlen(arguments)+1; /* move past first argument which is program name */
while ( arguments[i] ) {
concat_stringbuf(&cmdLine," ");
concat_stringbuf(&cmdLine,arguments+i);
while ( arguments[i] )
++i;
++i;
}
/* prepare process start info */
ZeroMemory(&processInfo,sizeof(PROCESS_INFORMATION));
ZeroMemory(&startInfo,sizeof(STARTUPINFO));
startInfo.cb = sizeof(STARTUPINFO);
if (redirect != NULL) {
ZeroMemory(&secattribs,sizeof(SECURITY_ATTRIBUTES));
secattribs.nLength = sizeof(SECURITY_ATTRIBUTES);
secattribs.bInheritHandle = TRUE;
hFile = CreateFile(redirect,GENERIC_WRITE,FILE_SHARE_WRITE,&secattribs,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile == INVALID_HANDLE_VALUE) {
fatal_stop("failed to open redirect file");
}
startInfo.dwFlags = STARTF_USESTDHANDLES;
startInfo.hStdOutput = hFile;
startInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
startInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
}
else {
hFile = INVALID_HANDLE_VALUE;
}
/* run the compiler process; don't specify an application name so that
the program name is run through the shell which will locate the compiler */
if (CreateProcess(NULL,cmdLine.buffer,NULL,NULL,TRUE,0,NULL,NULL,&startInfo,&processInfo) == 0)
return -1;
WaitForSingleObject(processInfo.hProcess,INFINITE);
exitCode = -1;
GetExitCodeProcess(processInfo.hProcess,&exitCode);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
if (hFile != INVALID_HANDLE_VALUE) {
CloseHandle(hFile);
}
destroy_stringbuf(&cmdLine);
return (int)exitCode;
}