Advertisement
Guest User

Untitled

a guest
Jan 13th, 2011
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. /*
  2.  *    AGROS - The new Limited Shell
  3.  *
  4.  *    Author: Joe "rahmu" Hakim Rahme <joe.hakim.rahme@gmail.com>
  5.  *
  6.  *
  7.  *    This file is part of AGROS.
  8.  *
  9.  *    This program is free software: you can redistribute it and/or modify
  10.  *    it under the terms of the GNU General Public License as published by
  11.  *    the Free Software Foundation, either version 3 of the License, or
  12.  *    (at your option) any later version.
  13.  *
  14.  *    This program is distributed in the hope that it will be useful,
  15.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *    GNU General Public License for more details.
  18.  *
  19.  *    You should have received a copy of the GNU General Public License
  20.  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.  */
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "agros.h"
  28.  
  29.  
  30.  
  31. int main(int argc, char** argv, char** envp){
  32.     int pid = 0;
  33.     command_t cmd;
  34.     char* commandline;
  35.     int exit_code = 0;
  36.  
  37.     /*
  38.      *   Main loop:
  39.      *   - print prompt
  40.      *   - read input and parse it
  41.      *   - either a built-in command ("cd", "?" or "exit)
  42.      *   - or a system command, in which case the program forks and executes it with execvp()
  43.      */
  44.  
  45.     while (exit_code != 1){
  46.         commandline = ag_readline(return_prompt());
  47.  
  48.         parse_command(commandline, &cmd);
  49.  
  50.         switch (get_cmd_code(cmd.name)){
  51.             case EMPTY_CMD:
  52.                 break;
  53.  
  54.             case CD_CMD:
  55.                 change_directory(concat_spaces(cmd.argv));
  56.                 break;
  57.  
  58.             case HELP_CMD:
  59.                 print_help();
  60.                 break;
  61.  
  62.             case EXIT_CMD:
  63.                 return 0;
  64.  
  65.             case OTHER_CMD:
  66.                 pid = fork();
  67.                 if (pid == 0){
  68.                     execvp(cmd.name, cmd.argv);
  69.                     fprintf(stdout, "%s: Could not execute command!\nType '?' for help.\n", cmd.name);
  70.                     exit_code = 1;
  71.                     break;
  72.                 }else if (pid < 0){
  73.                     fprintf(stdout, "Error!\n");
  74.                 }else {
  75.                     wait(0);
  76.                 }
  77.                 break;
  78.         }
  79.     }
  80.     exit_code = 0;
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement