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 |
if (argc == 2) { |
// scripting mode |
18 |
FILE * script = fopen(argv[1], "r"); |
} else if (argc == 2) { |
19 |
read_commands(script); |
FILE * script; |
20 |
|
script = fopen(argv[1], "r"); |
21 |
|
if (script == NULL) { |
22 |
|
perror(NULL); |
23 |
|
exit(EXIT_FAILURE); |
24 |
|
} |
25 |
|
run_commands(script, ""); |
26 |
fclose(script); |
fclose(script); |
27 |
|
|
28 |
|
} else { |
29 |
|
fprintf(stderr, "Sorry, mini shell just accepts one parameter: a path to a script!\n"); |
30 |
} |
} |
31 |
} |
} |
32 |
|
|
33 |
void read_commands(FILE * fp) { |
void run_commands(FILE * fp, char * prompt) { |
34 |
|
|
35 |
char command[MAX_COMMAND_LENGTH]; |
char command[MAX_COMMAND_LENGTH]; |
36 |
int command_length; |
char *command_ptr; |
|
|
|
|
fgets(command, MAX_COMMAND_LENGTH, fp); |
|
|
command_length = strlen(command); |
|
37 |
|
|
38 |
/* if last char is newline, strip it */ |
// echo first prompt |
39 |
if (command[command_length-1] == '\n') { |
fprintf(stdout, "%s", prompt); |
40 |
command[command_length-1] = '\0'; |
while (fgets(command, MAX_COMMAND_LENGTH, fp)) { |
41 |
|
|
42 |
|
int error_number = ferror(fp); |
43 |
|
|
44 |
|
if (error_number) { |
45 |
|
char * message = strerror(error_number); |
46 |
|
fprintf(stderr, "Error while reading from input stream: %s", message); |
47 |
|
clearerr(fp); |
48 |
|
break; |
49 |
|
} |
50 |
|
|
51 |
|
command_ptr = command; |
52 |
|
|
53 |
|
// strip newlines |
54 |
|
chomp(command_ptr); |
55 |
|
|
56 |
|
// trim whitespace |
57 |
|
trim(&command_ptr); |
58 |
|
|
59 |
|
// ... and action |
60 |
|
if (strlen(command_ptr)) |
61 |
|
dispatch_command(command_ptr); |
62 |
|
|
63 |
|
// echo prompt after executing shell command |
64 |
|
fprintf(stdout, "%s", prompt); |
65 |
} |
} |
66 |
|
|
|
process_command(command); |
|
67 |
} |
} |
68 |
|
|
|
void process_command(char * command) { |
|
|
printf("process_command: %s\n", command); |
|
|
start_process(command, 0); |
|
|
} |
|
69 |
|
|
70 |
BOOL start_process(char * filename, BOOL background) { |
void dispatch_command(char * command) { |
71 |
|
|
72 |
STARTUPINFO si; |
BOOL background = FALSE; |
73 |
PROCESS_INFORMATION pi; |
int command_length; |
74 |
|
char first_char; |
75 |
|
char * shell_command; |
76 |
|
|
77 |
//printf("start_process: '%s'\n", filename); |
// is it a shell command? |
78 |
|
first_char = command[0]; |
79 |
|
if (strcmp(&first_char, ":") == 0) { |
80 |
|
|
81 |
|
// "calculate" shell command ... |
82 |
|
shell_command = command + 1; |
83 |
|
//printf("shell command: %s\n", shell_command); |
84 |
|
|
85 |
|
// ... and dispatch it |
86 |
|
|
87 |
|
// wait |
88 |
|
if (strcmp(shell_command, "wait") == 0) { |
89 |
|
// TODO |
90 |
|
os_wait_for_processes(); |
91 |
|
|
92 |
|
// exit |
93 |
|
} else if (strcmp(shell_command, "exit") == 0) { |
94 |
|
exit(EXIT_SUCCESS); |
95 |
|
|
96 |
|
// unknown command |
97 |
|
} else { |
98 |
|
fprintf(stderr, "ERROR: Unknown shell command '%s'\n", shell_command); |
99 |
|
} |
100 |
|
|
101 |
ZeroMemory(&si,sizeof(si)); |
// run program: background or foreground? |
102 |
si.cb = sizeof(STARTUPINFO); |
} else { |
103 |
|
//printf("running: %s\n", command); |
104 |
ZeroMemory(&pi,sizeof(pi)); |
command_length = strlen(command); |
105 |
|
if (command[command_length-1] == '&' && command[command_length-2] == ' ') { |
106 |
CreateProcess( |
command[command_length-1] = '\0'; |
107 |
NULL, filename, |
command[command_length-2] = '\0'; |
108 |
NULL, NULL, FALSE, 0, |
background = TRUE; |
|
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; |
|
109 |
} |
} |
110 |
|
|
111 |
|
os_start_process(command, background); |
112 |
} |
} |
|
|
|
|
return TRUE; |
|
|
|
|
113 |
} |
} |
114 |
|
|
|
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); |
|
|
} |
|
|
|
|
|
} |
|