Advertisement
Guest User

shell.c

a guest
Apr 27th, 2016
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <readline/readline.h>
  4. #include <readline/history.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <ctype.h>
  8. #include <fcntl.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11. #include <assert.h>
  12.  
  13. #define MAXARGS 100
  14. #define PIPE_READ 0
  15. #define PIPE_WRITE 1
  16.  
  17. typedef struct command {
  18. char *cmd;
  19. int argc;
  20. char *argv[MAXARGS+1];
  21. struct command *next;
  22. } COMMAND;
  23.  
  24. char* inputfile = NULL;
  25. char* outputfile = NULL;
  26. int background_exec = 0;
  27.  
  28. COMMAND* parse(char* linha);
  29. void print_parse(COMMAND* commlist);
  30. void execute_commands(COMMAND* commlist);
  31. void free_commlist(COMMAND* commlist);
  32.  
  33. #include "parser.c"
  34.  
  35. int main(int argc, char* argv[]) {
  36. char *linha;
  37. COMMAND *com;
  38.  
  39. while (1) {
  40. if ((linha = readline("my_prompt$ ")) == NULL)
  41. exit(0);
  42. if (strlen(linha) != 0) {
  43. add_history(linha);
  44. com = parse(linha);
  45. if (com) {
  46. print_parse(com);
  47. execute_commands(com);
  48. free_commlist(com);
  49. }
  50. }
  51. free(linha);
  52. }
  53. }
  54.  
  55.  
  56. void print_parse(COMMAND* commlist) {
  57. int n, i;
  58.  
  59. printf("---------------------------------------------------------\n");
  60. printf("BG: %d IN: %s OUT: %s\n", background_exec, inputfile, outputfile);
  61. n = 1;
  62. while (commlist != NULL) {
  63. printf("#%d: cmd '%s' argc '%d' argv[] '", n, commlist->cmd, commlist->argc);
  64. i = 0;
  65. while (commlist->argv[i] != NULL) {
  66. printf("%s,", commlist->argv[i]);
  67. i++;
  68. }
  69. printf("%s'\n", commlist->argv[i]);
  70. commlist = commlist->next;
  71. n++;
  72. }
  73. printf("---------------------------------------------------------\n");
  74. }
  75.  
  76. void free_commlist(COMMAND *commlist){
  77. }
  78.  
  79. void execute_commands(COMMAND *commlist) {
  80. int fd[2];
  81. pid_t pid;
  82. int n_pipes=2; //pipes needed
  83. COMMAND *aux = commlist;
  84.  
  85. int i;
  86. for(i=0;i<n_pipes; i++){
  87. int oldfd = 0;
  88.  
  89. if(fd[0]!=0){
  90. close(fd[1]);
  91. oldfd = fd[0];
  92. }
  93. pipe(fd);
  94.  
  95. if((pid=fork())<0){perror("Fork Failed");}
  96. else
  97. if(pid == 0){
  98.  
  99. if(inputfile!=NULL){
  100. int in = open(inputfile,O_RDONLY);
  101. dup2(in,STDIN_FILENO);
  102. inputfile = NULL;
  103. }
  104.  
  105. if(outputfile != NULL){
  106. int out = open(outputfile, O_RDWR |O_CREAT | O_TRUNC, S_IRWXU);
  107. dup2(out,STDOUT_FILENO);
  108. outputfile = NULL;
  109. }
  110.  
  111. if(oldfd)
  112. dup2(oldfd,STDIN_FILENO);
  113.  
  114. if(commlist->cmd == "grepwc"){
  115. if(i==0){
  116. if(execlp("grep","grep","celio",NULL)<0){
  117. perror("Bad command");
  118. exit(1);
  119. }
  120. }
  121.  
  122. if(i==1){
  123. if(execlp("wc","wc","-l",NULL) < 0){
  124. perror("Bad command");
  125. exit(1);
  126. }
  127. }
  128. }
  129. }//child
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement