1 |
/* $Id$ */ |
/* $Id$ */ |
2 |
|
|
3 |
#include <stdlib.h> |
#include <stdlib.h> |
4 |
|
#include <stdio.h> |
5 |
#include <errno.h> |
#include <errno.h> |
6 |
#include <windows.h> |
#include <windows.h> |
7 |
|
|
8 |
|
#include "min_shell.h" |
9 |
|
|
10 |
#define BOOL int |
#define BOOL int |
11 |
#define TRUE 1 |
#define TRUE 1 |
12 |
#define FALSE 0 |
#define FALSE 0 |
13 |
|
|
14 |
|
|
15 |
|
#define MAX_COMMAND_LENGTH 1024 |
16 |
|
|
17 |
int main(int argc, char * argv[]) { |
int main(int argc, char * argv[]) { |
18 |
if (argc == 1) { |
if (argc == 1) { |
19 |
//printf("No arguments given."); |
//printf("No arguments given."); |
22 |
BOOL background = 0; |
BOOL background = 0; |
23 |
start_process(filename, background); |
start_process(filename, background); |
24 |
} |
} |
25 |
|
|
26 |
|
if (argc == 2) { |
27 |
|
FILE * script = fopen(argv[1], "r"); |
28 |
|
read_commands(script); |
29 |
|
fclose(script); |
30 |
|
} |
31 |
} |
} |
32 |
|
|
33 |
BOOL start_process(char *filename[], BOOL background) { |
void read_commands(FILE * fp) { |
34 |
|
|
35 |
|
char command[MAX_COMMAND_LENGTH]; |
36 |
|
|
37 |
|
fgets(command, MAX_COMMAND_LENGTH, fp); |
38 |
|
chomp(command); |
39 |
|
|
40 |
|
process_command(command); |
41 |
|
} |
42 |
|
|
43 |
|
/* if last char is newline, "strip" it */ |
44 |
|
void chomp(char * string) { |
45 |
|
int string_length = strlen(string); |
46 |
|
if (string[string_length-1] == '\n') { |
47 |
|
string[string_length-1] = '\0'; |
48 |
|
} |
49 |
|
} |
50 |
|
|
51 |
|
void process_command(char * command) { |
52 |
|
printf("process_command: %s\n", command); |
53 |
|
start_process(command, 0); |
54 |
|
} |
55 |
|
|
56 |
|
BOOL start_process(char * filename, BOOL background) { |
57 |
|
|
58 |
STARTUPINFO si; |
STARTUPINFO si; |
59 |
PROCESS_INFORMATION pi; |
PROCESS_INFORMATION pi; |
60 |
|
|
61 |
|
//printf("start_process: '%s'\n", filename); |
62 |
|
|
63 |
ZeroMemory(&si,sizeof(si)); |
ZeroMemory(&si,sizeof(si)); |
64 |
si.cb=sizeof(STARTUPINFO); |
si.cb = sizeof(STARTUPINFO); |
65 |
|
|
66 |
ZeroMemory(&pi,sizeof(pi)); |
ZeroMemory(&pi,sizeof(pi)); |
|
//pi.cb=sizeof(PROCESS_INFORMATION); |
|
67 |
|
|
68 |
CreateProcess( |
CreateProcess( |
69 |
NULL, "notepad.exe", |
NULL, filename, |
70 |
NULL, NULL, FALSE, 0, |
NULL, NULL, FALSE, 0, |
71 |
NULL, NULL, |
NULL, NULL, |
72 |
&si, |
&si, |
73 |
&pi |
&pi |
74 |
); |
); |
75 |
|
check_alert_error("CreateProcess"); |
76 |
|
|
77 |
|
if (!background) { |
78 |
|
long status; |
79 |
|
status = WaitForSingleObject(pi.hProcess, INFINITE); |
80 |
|
if (status == WAIT_FAILED) { |
81 |
|
check_alert_error("WaitForSingleObject"); |
82 |
|
return FALSE; |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
|
return TRUE; |
87 |
|
|
88 |
|
} |
89 |
|
|
90 |
|
void check_alert_error(const char * error_source) { |
91 |
|
DWORD dwError = GetLastError(); |
92 |
|
|
93 |
|
HLOCAL message = NULL; |
94 |
|
BOOL fOk; |
95 |
|
|
96 |
|
if (dwError == 0) |
97 |
|
return; |
98 |
|
|
99 |
|
fOk = FormatMessage( |
100 |
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
101 |
|
NULL, |
102 |
|
dwError, |
103 |
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT), |
104 |
|
(PTSTR)&message, |
105 |
|
0, |
106 |
|
NULL |
107 |
|
); |
108 |
|
|
109 |
|
if (message != NULL) { |
110 |
|
fprintf(stderr, "Error with '%s': %s (Code %i)\n", error_source, message, dwError); |
111 |
|
LocalFree(message); |
112 |
|
} |
113 |
|
|
114 |
} |
} |