/[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.16 by joko, Fri Jun 16 20:32:37 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>
6  #include <windows.h>  #include <string.h>
7    
8  #define BOOL int  #include "min_shell.h"
 #define TRUE 1  
 #define FALSE 0  
9    
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, "> ");
16      char filename[255] = "abc\0";    
17      BOOL background = 0;    // scripting mode
18      start_process(filename, background);    } else if (argc == 2) {
19        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);
27      
28      } else {
29        fprintf(stderr, "Sorry, mini shell just accepts one parameter: a path to a script!\n");
30    }    }
31  }  }
32    
33  BOOL start_process(char *filename[], BOOL background) {  void run_commands(FILE * fp, char * prompt) {
     
   STARTUPINFO si;  
   PROCESS_INFORMATION pi;  
34        
35    ZeroMemory(&si,sizeof(si));    char command[MAX_COMMAND_LENGTH];
36    si.cb=sizeof(STARTUPINFO);    char *command_ptr;
37        
38    ZeroMemory(&pi,sizeof(pi));    // echo first prompt
39    //pi.cb=sizeof(PROCESS_INFORMATION);    fprintf(stdout, "%s", prompt);
40        while (fgets(command, MAX_COMMAND_LENGTH, fp)) {
41    CreateProcess(      
42      NULL, "notepad.exe",      int error_number = ferror(fp);
43      NULL, NULL, FALSE, 0,      
44      NULL, NULL,      if (error_number) {
45      &si,        char * message = strerror(error_number);
46      &pi        fprintf(stderr, "Error while reading from input stream: %s", message);
47    );        clearerr(fp);
48            break;
   if (!background) {  
     long status;  
     status = WaitForSingleObject(pi.hProcess, INFINITE);  
     if (status == WAIT_FAILED) {  
       check_alert_error("WaitForSingleObject");  
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      
67  }  }
68    
 check_alert_error(char *error_source[]) {  
   DWORD dwError = GetLastError();  
69    
70    HLOCAL hlocal = NULL;  void dispatch_command(char * command) {
71    
72    BOOL fOk = FormatMessage(    BOOL background = FALSE;
73      FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,    int command_length;
74      NULL,    char first_char;
75      dwError,    char * shell_command;
76      MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),  
77      (PTSTR)&hlocal,    // is it a shell command?
78      0,    first_char = command[0];
79      NULL    if (strcmp(&first_char, ":") == 0) {
80    );      
81        // "calculate" shell command ...
82    if (hlocal != NULL) {      shell_command = command + 1;
83      fprintf(stderr, "Error with '%s': %s", error_source, hlocal);      //printf("shell command: %s\n", shell_command);
84      LocalFree(hlocal);      
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      // run program: background or foreground?
102      } else {
103        //printf("running: %s\n", command);
104        command_length = strlen(command);
105        if (command[command_length-1] == '&' && command[command_length-2] == ' ') {
106          command[command_length-1] = '\0';
107          command[command_length-2] = '\0';
108          background = TRUE;
109        }
110        
111        os_start_process(command, background);
112      }
113  }  }
114    

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

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