| 3 |
#include <stdlib.h> |
#include <stdlib.h> |
| 4 |
#include <stdio.h> |
#include <stdio.h> |
| 5 |
#include <errno.h> |
#include <errno.h> |
|
#include <windows.h> |
|
| 6 |
|
|
| 7 |
#include "min_shell.h" |
#include "min_shell.h" |
| 8 |
|
|
|
#define BOOL int |
|
|
#define TRUE 1 |
|
|
#define FALSE 0 |
|
|
|
|
| 9 |
|
|
| 10 |
#define MAX_COMMAND_LENGTH 1024 |
#define MAX_COMMAND_LENGTH 1024 |
| 11 |
|
|
| 12 |
int main(int argc, char * argv[]) { |
int main(int argc, char * argv[]) { |
| 13 |
|
|
| 14 |
|
// interactive mode with prompt |
| 15 |
if (argc == 1) { |
if (argc == 1) { |
| 16 |
//printf("No arguments given."); |
//printf("No arguments given."); |
| 17 |
//exit(EXIT_FAILURE); |
//exit(EXIT_FAILURE); |
| 18 |
char filename[255] = "abc\0"; |
char filename[255] = "abc\0"; |
| 19 |
BOOL background = 0; |
BOOL background = 0; |
| 20 |
start_process(filename, background); |
os_start_process(filename, background); |
| 21 |
} |
} |
| 22 |
|
|
| 23 |
|
// scripting mode |
| 24 |
if (argc == 2) { |
if (argc == 2) { |
| 25 |
FILE * script = fopen(argv[1], "r"); |
FILE * script = fopen(argv[1], "r"); |
| 26 |
read_commands(script); |
run_commands(script); |
| 27 |
fclose(script); |
fclose(script); |
| 28 |
} |
} |
| 29 |
} |
} |
| 30 |
|
|
| 31 |
void read_commands(FILE * fp) { |
void run_commands(FILE * fp) { |
| 32 |
|
|
| 33 |
char command[MAX_COMMAND_LENGTH]; |
char command[MAX_COMMAND_LENGTH]; |
| 34 |
|
|
| 35 |
fgets(command, MAX_COMMAND_LENGTH, fp); |
while (fgets(command, MAX_COMMAND_LENGTH, fp)) { |
| 36 |
chomp(command); |
int command_length; |
| 37 |
|
|
| 38 |
process_command(command); |
// strip newlines |
| 39 |
} |
chomp(command); |
| 40 |
|
|
| 41 |
/* if last char is newline, "strip" it */ |
// ... and action |
| 42 |
void chomp(char * string) { |
execute_command(command); |
|
int string_length = strlen(string); |
|
|
if (string[string_length-1] == '\n') { |
|
|
string[string_length-1] = '\0'; |
|
| 43 |
} |
} |
| 44 |
|
|
| 45 |
} |
} |
| 46 |
|
|
|
void process_command(char * command) { |
|
|
printf("process_command: %s\n", command); |
|
|
start_process(command, 0); |
|
|
} |
|
| 47 |
|
|
| 48 |
BOOL start_process(char * filename, BOOL background) { |
void execute_command(char * command) { |
| 49 |
|
|
| 50 |
STARTUPINFO si; |
BOOL background = FALSE; |
| 51 |
PROCESS_INFORMATION pi; |
int command_length; |
| 52 |
|
|
| 53 |
//printf("start_process: '%s'\n", filename); |
printf("process_command: %s\n", command); |
| 54 |
|
|
| 55 |
ZeroMemory(&si,sizeof(si)); |
// background or foreground? |
| 56 |
si.cb = sizeof(STARTUPINFO); |
command_length = strlen(command); |
| 57 |
|
if (command[command_length-1] == '&') { |
| 58 |
ZeroMemory(&pi,sizeof(pi)); |
command[command_length-1] = '\0'; |
| 59 |
|
background = TRUE; |
|
CreateProcess( |
|
|
NULL, filename, |
|
|
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; |
|
|
} |
|
| 60 |
} |
} |
| 61 |
|
|
| 62 |
return TRUE; |
os_start_process(command, background); |
|
|
|
| 63 |
} |
} |
| 64 |
|
|
|
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); |
|
|
} |
|
|
|
|
|
} |
|