Advertisement
Guest User

Untitled

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