/* command shell */ #include #include #include /* ::= { } ::= */ #define LINELEN 4096 #define MAXARGS 64 void process_line() { char buffer[LINELEN]; char * argv[MAXARGS]; int argc; int pos; fgets( buffer, LINELEN-1, stdin ); buffer[ LINELEN-1 ] = '\n'; /* FIX THIS LATER - no code to detect end of file */ /* note that the following logic is really stupid */ /* FIX THIS LATER - argument parsing should be a subroutine */ /* pick apart the line into argv, argc */ argc = 0; pos = 0; for (;;) { while (buffer[pos] == ' ') pos++; if (buffer[pos] == '\n') break; /* we have start of parameter */ argv[ argc ] = &buffer[pos]; argc = argc + 1; if ( argc >= (MAXARGS - 1) ) break; while (buffer[pos] > ' ') pos++; } /* null terminate all arguments */ argv[ argc ] = NULL; for (argc = 0; argv[ argc ] != NULL; argc++) { char * pos = argv[ argc ]; while (*pos > ' ') pos++; *pos = '\0'; } /* FIX THIS LATER - no code to detect and ignore comments */ /* launch application */ /* FIX THIS LATER - it ought to check for the right event */ /* FIX THIS LATER - applications launch ought to be a subroutine */ if ( fork() ) { /* parent */ wait(); } else { /* child */ /* FIX THIS LATER - Pass on caller's environment */ /* FIX THIS LATER - Use a search path to find application */ execve( argv[0], argv, NULL ); fprintf( stderr, "%s: command not found\n", argv[0] ); exit( EXIT_FAILURE ); } } int main() { for (;;) process_line(); }