3 |
#include <stdlib.h> |
#include <stdlib.h> |
4 |
#include <stdio.h> |
#include <stdio.h> |
5 |
#include <errno.h> |
#include <errno.h> |
6 |
#include <windows.h> |
#include <string.h> |
7 |
|
|
8 |
#include "min_shell.h" |
#include "min_shell.h" |
9 |
|
|
|
#define BOOL int |
|
|
#define TRUE 1 |
|
|
#define FALSE 0 |
|
|
|
|
|
|
|
|
#define MAX_COMMAND_LENGTH 1024 |
|
|
|
|
10 |
int main(int argc, char * argv[]) { |
int main(int argc, char * argv[]) { |
11 |
|
|
12 |
|
// interactive mode with prompt |
13 |
if (argc == 1) { |
if (argc == 1) { |
14 |
//printf("No arguments given."); |
fprintf(stderr, "Welcome to mini shell. Have fun!\n"); |
15 |
//exit(EXIT_FAILURE); |
run_commands(stdin, "> "); |
|
char filename[255] = "abc\0"; |
|
|
BOOL background = 0; |
|
|
start_process(filename, background); |
|
16 |
} |
} |
17 |
|
|
18 |
|
// scripting mode |
19 |
if (argc == 2) { |
if (argc == 2) { |
20 |
FILE * script = fopen(argv[1], "r"); |
FILE * script = fopen(argv[1], "r"); |
21 |
read_commands(script); |
run_commands(script, ""); |
22 |
fclose(script); |
fclose(script); |
23 |
} |
} |
24 |
} |
} |
25 |
|
|
26 |
void read_commands(FILE * fp) { |
void run_commands(FILE * fp, char * prompt) { |
27 |
|
|
28 |
char command[MAX_COMMAND_LENGTH]; |
char command[MAX_COMMAND_LENGTH]; |
|
int command_length; |
|
29 |
|
|
30 |
fgets(command, MAX_COMMAND_LENGTH, fp); |
// echo first prompt |
31 |
command_length = strlen(command); |
fprintf(stdout, "%s", prompt); |
32 |
|
while (fgets(command, MAX_COMMAND_LENGTH, fp)) { |
33 |
/* if last char is newline, strip it */ |
char *command_ptr = command; |
34 |
if (command[command_length-1] == '\n') { |
|
35 |
command[command_length-1] = '\0'; |
// strip newlines |
36 |
|
chomp(command_ptr); |
37 |
|
|
38 |
|
// trim whitespace |
39 |
|
trim(&command_ptr); |
40 |
|
|
41 |
|
// ... and action |
42 |
|
dispatch_command(command_ptr); |
43 |
|
|
44 |
|
// echo prompt after executing shell command |
45 |
|
fprintf(stdout, "%s", prompt); |
46 |
} |
} |
47 |
|
|
|
process_command(command); |
|
48 |
} |
} |
49 |
|
|
|
void process_command(char * command) { |
|
|
printf("process_command: %s\n", command); |
|
|
start_process(command, 0); |
|
|
} |
|
50 |
|
|
51 |
BOOL start_process(char * filename, BOOL background) { |
void dispatch_command(char * command) { |
52 |
|
|
53 |
STARTUPINFO si; |
BOOL background = FALSE; |
54 |
PROCESS_INFORMATION pi; |
int command_length; |
55 |
|
char first_char; |
56 |
|
char * shell_command; |
57 |
|
|
58 |
//printf("start_process: '%s'\n", filename); |
// is it a shell command? |
59 |
|
first_char = command[0]; |
60 |
|
if (strcmp(&first_char, ":") == 0) { |
61 |
|
|
62 |
|
// "calculate" shell command ... |
63 |
|
shell_command = command + 1; |
64 |
|
//printf("shell command: %s\n", shell_command); |
65 |
|
|
66 |
|
// ... and dispatch it |
67 |
|
|
68 |
|
// wait |
69 |
|
if (strcmp(shell_command, "wait") == 0) { |
70 |
|
// TODO |
71 |
|
os_wait_for_processes(); |
72 |
|
|
73 |
|
// exit |
74 |
|
} else if (strcmp(shell_command, "exit") == 0) { |
75 |
|
exit(EXIT_SUCCESS); |
76 |
|
|
77 |
|
// unknown command |
78 |
|
} else { |
79 |
|
fprintf(stderr, "ERROR: Unknown shell command '%s'\n", shell_command); |
80 |
|
} |
81 |
|
|
82 |
ZeroMemory(&si,sizeof(si)); |
// run program: background or foreground? |
83 |
si.cb = sizeof(STARTUPINFO); |
} else { |
84 |
|
//printf("running: %s\n", command); |
85 |
ZeroMemory(&pi,sizeof(pi)); |
command_length = strlen(command); |
86 |
|
if (command[command_length-1] == '&') { |
87 |
CreateProcess( |
command[command_length-1] = '\0'; |
88 |
NULL, filename, |
background = TRUE; |
|
NULL, NULL, FALSE, 0, |
|
|
NULL, NULL, |
|
|
&si, |
|
|
&pi |
|
|
); |
|
|
check_alert_error("CreateProcess"); |
|
|
|
|
|
if (!background) { |
|
|
long status; |
|
|
status = WaitForSingleObject(pi.hProcess, INFINITE); |
|
|
if (status == WAIT_FAILED) { |
|
|
check_alert_error("WaitForSingleObject"); |
|
|
return FALSE; |
|
89 |
} |
} |
90 |
|
|
91 |
|
os_start_process(command, background); |
92 |
} |
} |
|
|
|
|
return TRUE; |
|
|
|
|
93 |
} |
} |
94 |
|
|
|
void check_alert_error(const char * error_source) { |
|
|
DWORD dwError = GetLastError(); |
|
|
|
|
|
HLOCAL message = NULL; |
|
|
BOOL fOk; |
|
|
|
|
|
if (dwError == 0) |
|
|
return; |
|
|
|
|
|
fOk = FormatMessage( |
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
|
|
NULL, |
|
|
dwError, |
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT), |
|
|
(PTSTR)&message, |
|
|
0, |
|
|
NULL |
|
|
); |
|
|
|
|
|
if (message != NULL) { |
|
|
fprintf(stderr, "Error with '%s': %s (Code %i)\n", error_source, message, dwError); |
|
|
LocalFree(message); |
|
|
} |
|
|
|
|
|
} |
|