/[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.6 by joko, Thu Jun 15 22:34:55 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  #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      char *command_ptr;
37        
38    fgets(command, MAX_COMMAND_LENGTH, fp);    // echo first prompt
39    chomp(command);    fprintf(stdout, "%s", prompt);
40        while (fgets(command, MAX_COMMAND_LENGTH, fp)) {
41    process_command(command);      
42  }      int error_number = ferror(fp);
43        
44  /* if last char is newline, "strip" it */      if (error_number) {
45  void chomp(char * string) {        char * message = strerror(error_number);
46    int string_length = strlen(string);        fprintf(stderr, "Error while reading from input stream: %s", message);
47    if (string[string_length-1] == '\n') {        clearerr(fp);
48      string[string_length-1] = '\0';        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      
67  }  }
68    
 void process_command(char * command) {  
   printf("process_command: %s\n", command);  
   start_process(command, 0);  
 }  
   
 BOOL start_process(char * filename, BOOL background) {  
69    
70    STARTUPINFO si;  void dispatch_command(char * command) {
   PROCESS_INFORMATION pi;  
71    
72    //printf("start_process: '%s'\n", filename);    BOOL background = FALSE;
73      int command_length;
74      char first_char;
75      char * shell_command;
76    
77      // 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);  
   }  
     
 }  

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

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