Guest User

Untitled

a guest
Mar 21st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. /***Student name : Mohamad Samir Soliman Selim ,Assignment 2 UNIX SHELL AND HISTORY FEATURE TO Eng.Sara Khalil
  2. //Dept. : Computer and Systems Engineering dept.
  3. //Enter command 'history' for history feature and CTRL - c to exit the 'osh>' shell
  4. /*Header files */
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define MAX_LINE 100 /* The maximum length of a command */
  10. #define BUFFER_SIZE 50
  11. #define buffer "\n\Shell Command History:\n"
  12. //declarations
  13. char history[10][BUFFER_SIZE]; //history array to store history commands
  14. int count = 0;
  15. //function to display the history of commands
  16. void displayHistory()
  17. {
  18. printf("Shell command history:\n");
  19. int i;
  20. int j = 0;
  21. int histCount = count;
  22. //loop for iterating through commands
  23. for (i = 0; i<10;i++)
  24. {
  25. //command index
  26. printf("%d. ", histCount);
  27. while (history[i][j] != '\n' && history[i][j] != '\0')
  28. {
  29. //printing command
  30. printf("%c", history[i][j]);
  31. j++;
  32. }
  33. printf("\n");
  34. j = 0;
  35. histCount--;
  36. if (histCount == 0)
  37. break;
  38. }
  39. printf("\n");
  40. }
  41. //Fuction to get the command from shell, tokenize it and set the args parameter
  42. int formatCommand(char inputBuffer[], char *args[],int *flag)
  43. {
  44. int length; // # of chars in command line
  45. int i; // loop index for inputBuffer
  46. int start; // index of beginning of next command
  47. int ct = 0; // index of where to place the next parameter into args[]
  48. int hist;
  49. //read user input on command line and checking whether the command is !! or !n
  50. length = read(STDIN_FILENO, inputBuffer, MAX_LINE);
  51. start = -1;
  52. if (length == 0)
  53. exit(0); //end of command
  54. if (length < 0)
  55. {
  56. printf("Command not read\n");
  57. exit(-1); //terminate
  58. }
  59. //examine each character
  60. for (i=0;i<length;i++)
  61. {
  62. switch (inputBuffer[i])
  63. {
  64. case ' ':
  65. case '\t' : // to seperate arguments
  66. if(start != -1)
  67. {
  68. args[ct] = &inputBuffer[start];
  69. ct++;
  70. }
  71. inputBuffer[i] = '\0'; // add a null char at the end
  72. start = -1;
  73. break;
  74. case '\n': //final char
  75. if (start != -1)
  76. {
  77. args[ct] = &inputBuffer[start];
  78. ct++;
  79. }
  80. inputBuffer[i] = '\0';
  81. args[ct] = NULL; // no more args
  82. break;
  83. default :
  84. if (start == -1)
  85. start = i;
  86. if (inputBuffer[i] == '&')
  87. {
  88. *flag = 1; //this flag is the differentiate whether the child process is invoked in background
  89. inputBuffer[i] = '\0';
  90. }
  91. }
  92. }
  93. args[ct] = NULL; //if the input line was > 100 //* The maximum length of a command *///
  94. if(strcmp(args[0],"history")==0)
  95. {
  96. if(count>0)
  97. {
  98. displayHistory();
  99. }
  100. else
  101. {
  102. printf("\nNo Commands in the history\n");
  103. }
  104. return -1;
  105. }
  106. else if (args[0][0]-'!' ==0)
  107. { int x = args[0][1]- '0';
  108. int z = args[0][2]- '0';
  109. if(x>count) //second letter check
  110. {
  111. printf("\nNo Such Command in the history\n");
  112. strcpy(inputBuffer,"Wrong command");
  113. }
  114. else if (z!=-48) //third letter check
  115. {
  116. printf("\nNo Such Command in the history. Enter <=!9 (buffer size is 10 along with current command)\n");
  117. strcpy(inputBuffer,"Wrong command");
  118. }
  119. else
  120. {
  121. if(x==-15)//Checking for '!!',ascii value of '!' is 33.
  122. { strcpy(inputBuffer,history[0]); // this will be your 10 th(last) command
  123. }
  124. else if(x==0) //Checking for '!0'
  125. { printf("Enter proper command");
  126. strcpy(inputBuffer,"Wrong command");
  127. }
  128. else if(x>=1) //Checking for '!n', n >=1
  129. {
  130. strcpy(inputBuffer,history[count-x]);
  131. }
  132. }
  133. }
  134. for (i = 9;i>0; i--) //Moving the history elements one step higher
  135. strcpy(history[i], history[i-1]);
  136. strcpy(history[0],inputBuffer); //Updating the history array with input buffer
  137. count++;
  138. if(count>10)
  139. { count=10;
  140. }
  141. }
  142. int main(void)
  143. {
  144. char inputBuffer[MAX_LINE]; /* buffer to hold the input command */
  145. int flag; // equals 1 if a command is followed by "&"
  146. char *args[MAX_LINE/2 + 1];/* max arguments */
  147. int should_run =1;
  148. pid_t pid,tpid;
  149. int i;
  150. while (should_run) //infinite loop for shell prompt
  151. {
  152. flag = 0; //flag =0 by default
  153. printf("OSMohamadSamirSolomon$>");
  154. fflush(stdout);
  155. if(-1!=formatCommand(inputBuffer,args,&flag)) // get next command
  156. {
  157. pid = fork();
  158. if (pid < 0)//if pid is less than 0, forking fails
  159. {
  160. printf("Fork failed.\n");
  161. exit (1);
  162. }
  163. else if (pid == 0)//if pid ==0
  164. {
  165. //command not executed
  166. if (execvp(args[0], args) == -1)
  167. {
  168. printf("Error executing command\n");
  169. }
  170. }
  171. // if flag == 0, the parent will wait,
  172. // otherwise returns to the formatCommand() function.
  173. else
  174. {
  175. i++;
  176. if (flag == 0)
  177. {
  178. i++;
  179. wait(NULL);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. //Thanks a lot!
Add Comment
Please, Sign In to add comment