Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 13th, 2011  |  syntax: C  |  size: 4.71 KB  |  views: 83  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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 <unistd.h>
  28. #include <readline/readline.h>
  29. #include <readline/history.h>
  30. #include "agros.h"
  31.  
  32. /*
  33.  * A global array holding the associations between each built-in command
  34.  * and their command_code. More explanations can be found in the declaration
  35.  * of the "built_in_commands" structure.
  36.  *
  37.  * TODO: Replace array size (100) by a dynamic allocation.
  38.  */
  39.  
  40. built_in_commands my_commands[100] = {
  41.         {"exit" , EXIT_CMD  },
  42.         {""     , EMPTY_CMD },
  43.         {"cd"   , CD_CMD    },
  44.         {"?"    , HELP_CMD  }
  45. };
  46.  
  47.  
  48. /*
  49.  * This function parses a string and fills a command_t struct.
  50.  * It uses the strtok() to split the string into tokens. Then it fills the argv
  51.  * array with the tokens.
  52.  *
  53.  * After filling the array, it copies argv[0] as the cmd->name and cmd->argc
  54.  * as the length of the array.
  55.  *
  56.  */
  57.  
  58. void parse_command(char *cmdline, command_t *cmd){
  59.     int argc = 0;
  60.     char* word;
  61.  
  62.     word = strtok(cmdline, WHITESPACE);
  63.  
  64.     if (word == NULL) { word = ""; } // Fixes blank line bug
  65.  
  66.     while (word) {
  67.         cmd->argv[argc] = (char *) malloc(strlen(word)+1);
  68.         strcpy(cmd->argv[argc], word);
  69.         word = strtok(NULL, WHITESPACE);
  70.         argc++;
  71.     }
  72.     cmd->argv[argc] = NULL;
  73.  
  74.     cmd->argc = argc;
  75.     cmd->name = (char *) malloc(strlen(cmd->argv[0])+1);
  76.     strcpy(cmd->name, cmd->argv[0]);
  77. }
  78.  
  79.  
  80. /*
  81.  * Reads the input using fgets (don't use scanf!!!)
  82.  * DEPRECATED.
  83.  * DEPRECATED.
  84.  * DEPRECATED.
  85.  *
  86.  */
  87.  
  88. int read_input(char* string, int num){
  89.     char* CRPosition = NULL;
  90.     if (fgets(string, num, stdin)){
  91.         CRPosition = strchr(string, '\n');
  92.         if (CRPosition){
  93.             *CRPosition = '\0';
  94.         }
  95.         return 1;
  96.     }else {
  97.         return 0;
  98.     }
  99. }
  100.  
  101.  
  102. /*
  103.  * Modifiy this function to modify the prompt
  104.  */
  105.  
  106. char* return_prompt(void){
  107.     char prompt[10000] = "[AGROS]";
  108.  
  109.     strcat(prompt, getenv("USERNAME"));
  110.     strcat(prompt, ":");
  111.     strcat(prompt, getenv("PWD"));
  112.     strcat(prompt, "$ ");
  113.  
  114.     return prompt;
  115. }
  116.  
  117. /*
  118.  * Prints the help message.
  119.  * TODO: Store my string messages (help + error messages) in a separate file.
  120.  */
  121.  
  122. void print_help(void){
  123.     fprintf(stdout, "\nWelcome to AGROS, the newer limited shell.\nNote: At any time, you can type 'exit' to close the shell.\n\n\n");
  124. }
  125.  
  126. /*
  127.  * This command changes the current working directory used in the computation
  128.  * of relative paths. using the chdir() function.
  129.  * It then updates the $PWD environment variable with the new value of the current
  130.  * directory
  131.  */
  132.  
  133. void change_directory(char* PATH){
  134.     if (chdir(PATH) == 0){
  135.         getcwd(PATH, MAX_LINE_LEN);
  136.         setenv("PWD", PATH, 1);
  137.     }else{
  138.         fprintf(stdout, "%s: Could not change to such directory\n", PATH);
  139.     }
  140. }
  141.  
  142. /*
  143.  * This is used to send an argument to the CD command.
  144.  * It concatenates the arguments in case of a path containing a space character.
  145.  *
  146.  * TODO: This function should be updated to affect only parts of a global string.
  147.  */
  148.  
  149. char* concat_spaces (char** string_array){
  150.     char* tmp = string_array[1];
  151.     int count = 2;
  152.  
  153.     while (string_array[count]){
  154.         strcat(tmp, " ");
  155.         strcat(tmp, string_array[count]);
  156.         count++;
  157.     }
  158.  
  159.     return tmp;
  160. }
  161.  
  162. /*
  163.  * This function access the global array variable my_commands
  164.  * and returns the command_code eauivalent to each command.
  165.  *
  166.  */
  167.  
  168. int get_cmd_code(char* cmd_name){
  169.     int i = 0;
  170.     for (i=0; i<100; i++){
  171.         if (!strcmp(my_commands[i].command_name, cmd_name)){
  172.             return my_commands[i].command_code;
  173.         }
  174.     }
  175.     return OTHER_CMD;
  176. }
  177.  
  178. /*
  179.  * This is a wrapper for the GNU Readline function.
  180.  * Readline rocks! Thank you GNU!
  181.  *
  182.  */
  183.  
  184. char* ag_readline(char* prompt){
  185.     static char *line_read = (char *)NULL;
  186.  
  187.     if (line_read){
  188.         free (line_read);
  189.         line_read = (char *)NULL;
  190.     }
  191.  
  192.     line_read = readline (prompt);
  193.  
  194.     if (line_read && *line_read)
  195.       add_history (line_read);
  196.  
  197.     return line_read;
  198. }
clone this paste RAW Paste Data