/[cvs]/joko/Uni/BSArch/03/win32/min_shell.c
ViewVC logotype

Diff of /joko/Uni/BSArch/03/win32/min_shell.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by joko, Thu Jun 15 13:21:05 2006 UTC revision 1.9 by joko, Thu Jun 15 23:16:01 2006 UTC
# Line 3  Line 3 
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  #define BOOL int  #include "min_shell.h"
8  #define TRUE 1  
9  #define FALSE 0  
10    #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) {
25        FILE * script = fopen(argv[1], "r");
26        run_commands(script);
27        fclose(script);
28    }    }
29  }  }
30    
31  BOOL start_process(char *filename[], BOOL background) {  void run_commands(FILE * fp) {
     
   STARTUPINFO si;  
   PROCESS_INFORMATION pi;  
32        
33    ZeroMemory(&si,sizeof(si));    char command[MAX_COMMAND_LENGTH];
   si.cb=sizeof(STARTUPINFO);  
34        
35    ZeroMemory(&pi,sizeof(pi));    while (fgets(command, MAX_COMMAND_LENGTH, fp)) {
36    //pi.cb=sizeof(PROCESS_INFORMATION);      int command_length;
37          
38    CreateProcess(      // strip newlines
39      NULL, "notepad.exe",      chomp(command);
40      NULL, NULL, FALSE, 0,      
41      NULL, NULL,      // ... and action
42      &si,      execute_command(command);
     &pi  
   );  
     
   if (!background) {  
     long status;  
     status = WaitForSingleObject(pi.hProcess, INFINITE);  
     if (status == WAIT_FAILED) {  
       check_alert_error("WaitForSingleObject");  
     }  
43    }    }
44      
45  }  }
46    
 check_alert_error(char *error_source[]) {  
   DWORD dwError = GetLastError();  
47    
48    HLOCAL hlocal = NULL;  void execute_command(char * command) {
49    
50      BOOL background = FALSE;
51      int command_length;
52    
53    BOOL fOk = FormatMessage(    printf("process_command: %s\n", command);
54      FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,    
55      NULL,    // background or foreground?
56      dwError,    command_length = strlen(command);
57      MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),    if (command[command_length-1] == '&') {
58      (PTSTR)&hlocal,      command[command_length-1] = '\0';
59      0,      background = TRUE;
     NULL  
   );  
   
   if (hlocal != NULL) {  
     fprintf(stderr, "Error with '%s': %s", error_source, hlocal);  
     LocalFree(hlocal);  
60    }    }
61        
62      os_start_process(command, background);
63  }  }
64    

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.9

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed