| 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 | 
  | 
  | 
| 15 | 
     //exit(EXIT_FAILURE); | 
     //exit(EXIT_FAILURE); | 
| 16 | 
     char filename[255] = "abc\0"; | 
     char filename[255] = "abc\0"; | 
| 17 | 
     BOOL background = 0; | 
     BOOL background = 0; | 
| 18 | 
     start_process(filename, background); | 
     os_start_process(filename, background); | 
| 19 | 
   } | 
   } | 
| 20 | 
    | 
    | 
| 21 | 
   if (argc == 2) { | 
   if (argc == 2) { | 
| 22 | 
     FILE * script = fopen(argv[1], "r"); | 
     FILE * script = fopen(argv[1], "r"); | 
| 23 | 
     read_commands(script); | 
     run_commands(script); | 
| 24 | 
     fclose(script); | 
     fclose(script); | 
| 25 | 
   } | 
   } | 
| 26 | 
 } | 
 } | 
| 27 | 
  | 
  | 
| 28 | 
 void read_commands(FILE * fp) { | 
 void run_commands(FILE * fp) { | 
| 29 | 
    | 
    | 
| 30 | 
   char command[MAX_COMMAND_LENGTH]; | 
   char command[MAX_COMMAND_LENGTH]; | 
 | 
   int command_length; | 
  | 
| 31 | 
    | 
    | 
| 32 | 
   fgets(command, MAX_COMMAND_LENGTH, fp); | 
   fgets(command, MAX_COMMAND_LENGTH, fp); | 
| 33 | 
   command_length = strlen(command); | 
   chomp(command); | 
 | 
    | 
  | 
 | 
   /* if last char is newline, strip it */ | 
  | 
 | 
   if (command[command_length-1] == '\n') { | 
  | 
 | 
     command[command_length-1] = '\0'; | 
  | 
 | 
   } | 
  | 
| 34 | 
    | 
    | 
| 35 | 
   process_command(command); | 
   execute_command(command); | 
 | 
 } | 
  | 
 | 
  | 
  | 
 | 
 void process_command(char * command) { | 
  | 
 | 
   printf("process_command: %s\n", command); | 
  | 
 | 
   start_process(command, 0); | 
  | 
| 36 | 
 } | 
 } | 
| 37 | 
  | 
  | 
 | 
 BOOL start_process(char * filename, BOOL background) { | 
  | 
 | 
  | 
  | 
 | 
   STARTUPINFO si; | 
  | 
 | 
   PROCESS_INFORMATION pi; | 
  | 
 | 
  | 
  | 
 | 
   printf("start_process: '%s'\n", filename); | 
  | 
 | 
    | 
  | 
 | 
   ZeroMemory(&si,sizeof(si)); | 
  | 
 | 
   si.cb = sizeof(STARTUPINFO); | 
  | 
 | 
    | 
  | 
 | 
   ZeroMemory(&pi,sizeof(pi)); | 
  | 
 | 
   //pi.cb=sizeof(PROCESS_INFORMATION); | 
  | 
 | 
    | 
  | 
 | 
   CreateProcess( | 
  | 
 | 
     //"C:\windows\system32\notepad.exe", NULL, | 
  | 
 | 
     //"C:\\windows\\system32\\notepad.exe", NULL, | 
  | 
 | 
     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; | 
  | 
 | 
     } | 
  | 
 | 
   } | 
  | 
 | 
    | 
  | 
 | 
   return TRUE; | 
  | 
| 38 | 
  | 
  | 
| 39 | 
  | 
 void execute_command(char * command) { | 
| 40 | 
  | 
   printf("process_command: %s\n", command); | 
| 41 | 
  | 
   os_start_process(command, 0); | 
| 42 | 
 } | 
 } | 
| 43 | 
  | 
  | 
 | 
 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); | 
  | 
 | 
   } | 
  | 
 | 
    | 
  | 
 | 
 } | 
  |