Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8.  
  9. typedef struct {
  10. char *prog;
  11. char **args;
  12. } COMMAND;
  13.  
  14. typedef struct {
  15. COMMAND *commands;
  16. int numJobs;
  17. } JOB;
  18.  
  19. // parseInput splits command line input based on spaces and calls
  20. // splitter to divide input that didn't have spaces
  21. COMMAND parseInput(char* input);
  22. char** splitter(char *token, int *count);
  23.  
  24. // finds position of left carrot within the arguments array
  25. // returns -1 if not found
  26. void whereLeftCarrot(COMMAND *comm);
  27.  
  28. // finds position of right carrot within the arguments array
  29. // returns -1 if not found
  30. void whereRightCarrot(COMMAND *comm);
  31.  
  32. // removes special symbol from comm.args by taking in the pos
  33. // of the special char and shifting from that spot over
  34. void shiftArgs(COMMAND *comm, int pos);
  35.  
  36. int doCommands(COMMAND comm, char *input);
  37.  
  38. void printall(COMMAND comm);
  39.  
  40. JOB makeJobs(char *input);
  41.  
  42. int main(int argc, char *argv[])
  43. {
  44.  
  45. while (1) {
  46.  
  47. char input[512]; // store input line
  48. char inputcpy[512];
  49. printf("$sshell "); // shell prompt
  50. //fflush(stdin);
  51. fgets(input, 512, stdin); // read line into string
  52. input[strlen(input) - 1] = '\0';
  53. strcpy(inputcpy, input);
  54.  
  55. JOB jobs = makeJobs(input);
  56.  
  57.  
  58. //COMMAND comm = parseInput(input);
  59.  
  60. printf("RET FROM MAKE JOBS\n");
  61.  
  62. //check if input is empty or whitespace
  63. if (jobs.commands[0].prog == NULL) {
  64. continue;
  65. }
  66. else
  67.  
  68. // check for exit command
  69. if (strcmp(jobs.commands[0].prog, "exit") == 0) {
  70. printf("Bye...\n");
  71. exit(0);
  72.  
  73. }
  74. else
  75.  
  76. if (strcmp(jobs.commands[0].prog, "pwd") == 0) {
  77. char cwd[512];
  78. getcwd(cwd, sizeof(cwd));
  79. printf("%s\n", cwd);
  80. }
  81. else
  82.  
  83. if (strcmp(jobs.commands[0].prog, "cd") == 0) {
  84. if (chdir(jobs.commands[0].args[1]) == -1) {
  85. fprintf(stderr, "Error: no such directory.\n");
  86. }
  87. }
  88. else {
  89.  
  90. int exits[jobs.numJobs];
  91. int status;
  92.  
  93. for (int i = 0; i < jobs.numJobs; i++) {
  94.  
  95. status = doCommands(jobs.commands[i], inputcpy);
  96. exits[i] = status;
  97.  
  98. }
  99.  
  100. fprintf(stderr, "+ completed '%s'", input);
  101. for (int i = 0; i < jobs.numJobs; i++) {
  102. fprintf(stderr, "[%d]", exits[i]);
  103. }
  104. fprintf(stderr, "\n");
  105.  
  106. }
  107. }
  108. return EXIT_SUCCESS;
  109. }
  110.  
  111. COMMAND parseInput(char *input) {
  112.  
  113. COMMAND comm;
  114. int i = 1;
  115. char *token;
  116.  
  117. token = strtok(input, " \n");
  118. // printf("Got a token: %s\n", token);
  119.  
  120. // is empty or whitespace
  121. if (token == NULL) {
  122. comm.prog = malloc(sizeof(NULL));
  123. comm.prog = NULL;
  124. return comm;
  125. }
  126.  
  127. comm.prog = (char*)malloc(strlen(token) + 1);
  128. strcpy(comm.prog, token);
  129.  
  130. comm.args = (char**)malloc(sizeof(char*) * 512);
  131.  
  132. comm.args[0] = (char*)malloc(strlen(token) + 1);
  133. strcpy(comm.args[0], token);
  134.  
  135. while (1) {
  136. token = strtok(NULL, " \n");
  137.  
  138. if (token == NULL) {
  139. break;
  140. }
  141.  
  142. if (strpbrk(token, "<>|") != NULL) {
  143.  
  144. int count = 0;
  145. char **split = splitter(token, &count);
  146. int j;
  147.  
  148. for (j = 0; j < count; j++) {
  149. comm.args[i + j] = malloc(sizeof(split[j] + 1));
  150. strcpy(comm.args[i + j], split[j]);
  151. }
  152.  
  153. i = i + j;
  154.  
  155. }
  156. else {
  157.  
  158. comm.args[i] = (char*)malloc(strlen(token) + 1);
  159. strcpy(comm.args[i], token);
  160. i++;
  161. }
  162. }
  163. comm.args[i] = malloc(sizeof((char*)NULL));
  164. comm.args[i] = NULL;
  165.  
  166. /*
  167. for(int g = 0; g < i; g++) {
  168. printf("ARGS: %s\n", comm.args[g]);
  169. }
  170. */
  171.  
  172. return comm;
  173. }
  174.  
  175. char** splitter(char *token, int *count) {
  176.  
  177. char **tokens = malloc(sizeof(char*) * 512);
  178. char temp[512];
  179. int i = 0;
  180. int j = 0;
  181.  
  182.  
  183. while (token[i] != '\0') {
  184.  
  185. if (token[i] == '<' || token[i] == '>') {
  186.  
  187. temp[j] = '\0';
  188.  
  189. if (temp[0] != '\0') {
  190. tokens[*count] = (char*)malloc(strlen(temp) + 1);
  191. strcpy(tokens[*count], temp);
  192. ++*count;
  193. }
  194.  
  195. if (token[i] == '<') {
  196. tokens[*count] = (char*)malloc(strlen("<") + 1);
  197. strcpy(tokens[*count], "<");
  198. }
  199. else if (token[i] == '>') {
  200. tokens[*count] = (char*)malloc(strlen(">") + 1);
  201. strcpy(tokens[*count], ">");
  202. }
  203.  
  204. ++*count;
  205. j = 0;
  206. i++;
  207. temp[0] = '\0';
  208.  
  209. }
  210. else {
  211.  
  212. temp[j] = token[i];
  213. i++;
  214. j++;
  215. }
  216. }
  217.  
  218. if (temp[0] != '\0') {
  219. tokens[*count] = malloc(strlen(temp) + 1);
  220. strcpy(tokens[*count], temp);
  221. ++*count;
  222. }
  223.  
  224. // printf("# of tokens: %d\n", *count);
  225.  
  226. /*
  227. for(int g = 0; g < *count; g++) {
  228. printf("Token Stored: %s\n", tokens[g]);
  229. }
  230. */
  231.  
  232.  
  233. return tokens;
  234. }
  235.  
  236. void whereLeftCarrot(COMMAND *comm) {
  237.  
  238. int pos = 0;
  239.  
  240.  
  241. while (1) {
  242.  
  243. //printf("AFTER WHILE\n");
  244.  
  245. if ((*comm).args[pos] == NULL) {
  246. break;
  247. }
  248.  
  249. if (strcmp((*comm).args[pos], "<") == 0) {
  250. int fd = open((*comm).args[pos + 1], O_RDONLY);
  251. dup2(fd, STDIN_FILENO);
  252. close(fd);
  253. shiftArgs(comm, pos);
  254. //printf("position %d\n", pos);
  255. }
  256. pos++;
  257. }
  258. }
  259.  
  260. void whereRightCarrot(COMMAND *comm) {
  261.  
  262. int pos = 0;
  263.  
  264. while (1) {
  265.  
  266. //printf("AFTER WHILE\n");
  267.  
  268. if ((*comm).args[pos] == NULL) {
  269. break;
  270. }
  271.  
  272.  
  273.  
  274. if (strcmp((*comm).args[pos], ">") == 0) {
  275.  
  276. int fd = open((*comm).args[pos + 1], O_RDWR|O_CREAT|O_TRUNC, S_IRWXU);
  277.  
  278. dup2(fd, STDOUT_FILENO);
  279. close(fd);
  280. shiftArgs(comm, pos);
  281. (*comm).args[pos] = NULL;
  282.  
  283. }
  284. pos++;
  285. }
  286. }
  287.  
  288. void shiftArgs(COMMAND *comm, int pos) {
  289.  
  290.  
  291. while ((*comm).args[pos + 1] != NULL) {
  292. (*comm).args[pos] = realloc((*comm).args[pos], sizeof((*comm).args[pos + 1]));
  293. strcpy((*comm).args[pos], (*comm).args[pos + 1]);
  294. pos++;
  295. }
  296.  
  297. (*comm).args[pos] = malloc(sizeof(NULL));
  298. (*comm).args[pos] = NULL;
  299.  
  300. //int i = 0;
  301. /*while ((*comm).args[i] != NULL) {
  302. printf("ARGS[i]: %s\n", (*comm).args[i]);
  303. i++;
  304. }
  305. */
  306.  
  307. free((*comm).args[pos + 1]);
  308.  
  309. return;
  310.  
  311. }
  312.  
  313. int doCommands(COMMAND comm, char *input) {
  314. // create new process
  315. pid_t PID = fork();
  316.  
  317. // if child process
  318. if (PID == 0) {
  319.  
  320. int status;
  321.  
  322. //printall(comm);
  323.  
  324. whereLeftCarrot(&comm);
  325. whereRightCarrot(&comm);
  326.  
  327. //printall(comm);
  328.  
  329. status = execvp(comm.prog, comm.args);
  330.  
  331. // if exec fails
  332. if (status != 0) {
  333.  
  334. fprintf(stderr, "Error: command not found.\n");
  335. exit(42);
  336.  
  337. // else exit the process
  338. }
  339. else {
  340.  
  341. exit(0);
  342.  
  343. }
  344.  
  345. }
  346. else {
  347.  
  348. int status;
  349. wait(&status);
  350. return WEXITSTATUS(status);
  351.  
  352. }
  353. }
  354.  
  355.  
  356. void printall(COMMAND comm) {
  357. int i = 0;
  358.  
  359. printf("COMM.PROG: %s\n", comm.prog);
  360.  
  361. while (comm.args[i] != NULL) {
  362. printf("COMM.ARGS[i]: %s\n", comm.args[i]);
  363. i++;
  364. }
  365. }
  366.  
  367. JOB makeJobs(char *input) {
  368.  
  369. printf("IN MAKE JOBS\n");
  370.  
  371. JOB jobs;
  372. int pos = 1;
  373.  
  374. char *token;
  375. token = strtok(input, "|\n");
  376. printf("TOKEN: %s\n", token);
  377.  
  378. jobs.commands[0] = parseInput(token);
  379. printf("AFTER PARSE INPUT ON FIRST TOKEN\n");
  380.  
  381.  
  382. printf("RIGHT BEFORE LOOP\n");
  383. while (1) {
  384.  
  385. if (token == NULL) {
  386. break;
  387. }
  388.  
  389. token = strtok(NULL, "|\n");
  390. jobs.commands[pos] = parseInput(token);
  391. pos++;
  392. }
  393.  
  394.  
  395. for (int i = 0; i < pos; i++) {
  396. printf("JOB[%d]:", i);
  397. printall(jobs.commands[i]);
  398. }
  399.  
  400. jobs.numJobs = pos;
  401.  
  402. return jobs;
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement