Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.64 KB | None | 0 0
  1. #!/bin/sh
  2. echo "-- Testing our implementation of OpenShell --"
  3. echo ""
  4. echo "- If you have any problem in passing a test read the corresponding"
  5. echo "- source file to understand what the test is checking"
  6. echo ""
  7. printf "********************* PRESS ENTER TO RUN TESTS ... "
  8. read
  9.  
  10. echo "top -b -n1|head -8|tail -1" | ./shell
  11. printf "********************* TEST WILDCARDS ... "
  12. read
  13.  
  14. echo ls -al openshell.* | ./shell
  15. printf "********************* TEST ALGORITHMS ... "
  16. read
  17.  
  18. echo "top -b -n1|head -8|tail -1" | ./shell
  19.  
  20. echo "who|awk '{print $4 ; print $3}'|sort -n|wc -l" | ./shell
  21.  
  22. printf "********************* TEST CHECKENV. ... "
  23.  
  24.  
  25. echo "checkenv" | ./shell
  26. printf "********************* TEST DONE. YOU SHOULD SEE OUTPUT FROM TEST ABOVE ... "
  27.  
  28. $ ./RUN_TESTS
  29. -- Testing our implementation of OpenShell --
  30.  
  31. - If you have any problem in passing a test read the corresponding
  32. - source file to understand what the test is checking
  33.  
  34. ********************* PRESS ENTER TO RUN TESTS ... ./RUN_TESTS: 8: read: arg count 'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin. stdin is a file or a pipe {top} {-b} {-n1} {|} {head} {-8} {|} {tail} {-1} {|} p[0][0] top p[0][1] -b p[0][2] -n1 p[1][0] head p[1][1] -8 p[2][0] tail p[2][1] -1
  35.  
  36. 889 message+ 20 0 44472 5528 3564 S 6,2 0,0 0:19.61 dbus-daemon
  37. ********************* TEST WILDCARDS ... ./RUN_TESTS: 12: read: arg count 'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin. stdin is a file or a pipe
  38.  
  39. p[0][0] ls p[0][1] -al p[0][2] openshell.h
  40.  
  41. -rw-rw-r-- 1 dac dac 1731 maj 2 06:55 openshell.h
  42. ********************* TEST ALGORITHMS ... ./RUN_TESTS: 16: read: arg count 'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin. stdin is a file or a pipe {top} {-b} {-n1} {|} {head} {-8} {|} {tail} {-1} {|} p[0][0] top p[0][1] -b p[0][2] -n1 p[1][0] head p[1][1] -8 p[2][0] tail p[2][1] -1
  43.  
  44. 2380 dac 20 0 1530908 145808 75156 S 6,7 0,9 5:14.76 compiz 'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin. stdin is a file or a pipe {who} {|} {awk} {{print $4 ; print $3}} {|} {sort} {-n} {|} {wc} {-l} {|} p[0][0] who p[1][0] awk p[1][1] {print $4 ; print $3} p[2][0] sort p[2][1] -n p[3][0] wc p[3][1] -l
  45.  
  46. 2
  47. ********************* TEST CHECKENV. ... 'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin. stdin is a file or a pipe
  48.  
  49. ********************* TEST DONE. YOU SHOULD SEE OUTPUT FROM TEST ABOVE ..
  50.  
  51. int main(int argc, char *argv[]) {
  52. struct sigaction sh;
  53. /* char *shell_prompt[100];*/
  54. sh.sa_handler = handler;
  55. sigemptyset(&sh.sa_mask);
  56. sh.sa_flags = 0;
  57. sigaction(SIGINT, &sh, NULL);
  58. int index = 0;
  59. int i;
  60.  
  61. while (1) {
  62. index = 0;
  63. i = getopt_long(argc, argv, "p:vh",
  64. options, &index);
  65. if (i == -1)
  66. break;
  67. switch (i) {
  68. case 'p': {
  69. break;
  70. }
  71. case 'v': {
  72. printf("OpenShell version 0.1(a)n");
  73. printf("Version: %sn", VERSION);
  74. exit(EXIT_SUCCESS);
  75.  
  76. }
  77. case 'h': {
  78. printf("Usage: ./shelln");
  79. exit(EXIT_SUCCESS);
  80.  
  81. }
  82. default: {
  83. return 1;
  84. }
  85. }
  86. }
  87. getPath();
  88.  
  89. char buf[256];
  90. char *malloced;
  91.  
  92.  
  93. char *shell_prompt = NULL;
  94. char *cwd = NULL;
  95. for (; ;) {
  96.  
  97. if (!isatty(fileno(stdin)))
  98. //printf("stdin is a terminaln");
  99. //else
  100. {
  101. printf("stdin is a file or a pipen");
  102. command(readline(shell_prompt));
  103. exit(0);
  104. }
  105.  
  106. cwd = malloc(sizeof(char *) * 100);
  107. if (cwd != NULL && getcwd(cwd, 99) == cwd) {
  108.  
  109. printf("%s: > ", cwd);
  110. char *input = readline(NULL);
  111.  
  112. add_history(input);
  113. command(input);
  114. free(input);
  115.  
  116. }
  117.  
  118. else {
  119. printf("%s: $ ", getenv("USER"));
  120. char *input = readline(NULL);
  121.  
  122. add_history(input);
  123. command(input);
  124. free(input);
  125.  
  126. }
  127. free(cwd);
  128. }
  129. return 0;
  130. }
  131.  
  132.  
  133. /*
  134. * Parse and execute one null-terminated command line string.
  135. * This breaks the command line up into words, checks to see if the
  136. * command is an alias, and expands wildcards.
  137. */
  138. int command(const char *cmd) {
  139. const char *endCmd;
  140. char cmdName[CMD_LEN];
  141. freeChunks();
  142.  
  143. /*
  144. * Skip leading blanks.
  145. */
  146. if (cmd) {
  147. while (isBlank(*cmd))
  148. cmd++;
  149. /*
  150. * If the command is empty or is a comment then ignore it.
  151. */
  152. if (cmd) if ((*cmd == '') || (*cmd == '#'))
  153. return 0;
  154. /*
  155. * Look for the end of the command name and then copy the
  156. * command name to a buffer so we can null terminate it.
  157. */
  158. endCmd = cmd;
  159. if (endCmd)
  160. while (*endCmd && !isBlank(*endCmd))
  161. endCmd++;
  162.  
  163. memcpy(cmdName, cmd, endCmd - cmd);
  164. cmdName[endCmd - cmd] = '';
  165. /*
  166. * Expand simple environment variables
  167. */
  168.  
  169.  
  170. if (cmd)
  171. while (strstr(cmd, "$("))
  172. expandVariable((char *) cmd);
  173. /*
  174. * Now look for the command in the builtin table, and execute
  175. * the command if found.
  176. */
  177. if (exec_builtin(cmd)) {
  178. return 0;
  179. }
  180. /*
  181. * The command is not a built-in, so run the program along
  182. * the PATH list.
  183. */
  184. return runCmd(cmd);
  185. }
  186. else return 0;
  187. }
  188.  
  189.  
  190.  
  191. static int runCmd(const char *cmd) {
  192. const char *cp;
  193. pid_t pid;
  194. int status;
  195. struct command structcommand[15];
  196. char **argv = 0;
  197. int argc = 1;
  198. bool pipe = false;
  199. char *string[z][z];
  200. char *pString3[40];
  201. char *pString2[40];
  202. int n = 0;
  203. char **ptr1;
  204. char string1[z];
  205. bool keep = false;
  206. char *pString1[z];
  207. char *pString[z];
  208. *pString1 = "";
  209. *pString = "";
  210. char *temp = {''};
  211. int w = 0;
  212. bool quote = false;
  213. int j = 0;
  214. int i;
  215. int p = 0;
  216. char **ptr;
  217. char *tmpchar;
  218. char *cmdtmp;
  219. bool b1 = false;
  220. int y = 0;
  221. i = 0;
  222. int h = 0;
  223. nullterminate(string);
  224. if (cmd) {
  225. for (cp = cmd; *cp; cp++) {
  226. if ((*cp >= 'a') && (*cp <= 'z')) {
  227. continue;
  228. }
  229. if ((*cp >= 'A') && (*cp <= 'Z')) {
  230. continue;
  231. }
  232. if (isDecimal(*cp)) {
  233. continue;
  234. }
  235. if (isBlank(*cp)) {
  236. continue;
  237. }
  238. if ((*cp == '.') || (*cp == '/') || (*cp == '-') ||
  239. (*cp == '+') || (*cp == '=') || (*cp == '_') ||
  240. (*cp == ':') || (*cp == ',') || (*cp == ''') ||
  241. (*cp == '"')) {
  242. continue;
  243. }
  244. }
  245. cmdtmp = strdup(cmd);
  246. ptr1 = str_split(pString3, cmdtmp, '|');
  247. if (strstr(cmd, "|") == NULL) { /* not a pipeline */
  248. makeArgs(cmd, &argc, (const char ***) &argv, pipe, 0, 0);
  249. write_argument(&argc, structcommand, argv, string[0]);
  250. n++;
  251. }
  252. else {
  253. for (i = 0; *(ptr1 + i); i++) { /* loop for each pipeline*/
  254.  
  255.  
  256. n++; /* save number of pipelines */
  257. int e = 0; /* a counter */
  258. *pString = ""; /* should malloc and free this? */
  259. strcpy(string1, *(ptr1 + i));
  260. if ((string1[0] != '') && !isspace(string1[0])) { /* this is neither the end nor a new argument */ /* BSD bug? check*/
  261. ptr = str_split(pString2, *(&string1), ' '); /* split the string at the arguments */
  262. h = 0;
  263. for (j = 0; *(ptr + j); j++) { /* step through the arguments */
  264. for (int d= 0; *(ptr + d+ j); d++) {
  265. //printf("%d in %s is between: %d", 7, cmd, isBetweenQuotes(7, cmd));
  266. }
  267. /* the pipeline is in cmdtmp and the argument/program is in ptr[i] */
  268. if (ptr + j && !quote && strstr(*(ptr + j), "'")) { /* is quote? */
  269. quote = true;
  270. strcpy(temp, *(ptr + j));
  271. if (y < 1) {
  272. y++;
  273. }
  274. }
  275. while (quote) {
  276. if (*(ptr + j) && strstr(*(ptr + j), "'")) { /* end of quote */
  277. quote = false;
  278. if (y < 1) {
  279. string[i][j] = strcpy(temp, *(ptr + j));
  280. }
  281. y = 0;
  282. }
  283. else if (*(ptr + j)) { /* read until end of quote */
  284. string[i][j] = temp;
  285. continue;
  286. } else {
  287. quote = false;
  288. break;
  289. }
  290. }
  291. if (ptr + j) {
  292. // printf("%d in %s is between: %d", 0, *(ptr + i)) ,isBetweenQuotes(0, *(ptr + i));
  293. if (*(ptr + j)[0] == '{') {
  294. keep = true;
  295. }
  296. if (testFn(*(ptr + j))) { /* test for last char */
  297. string[i][j - p] = concat(*pString1, *(ptr + j));
  298. keep = false;
  299. free(*pString1);
  300. goto mylabel;
  301. }
  302. if (keep) {
  303. *pString1 = concat(concat(*pString1, *(ptr + j)), " ");
  304. p++;
  305. } else {
  306. b1 = false;
  307. int q = j;
  308. for (e = 0; *(ptr + q + e); e++) { /* step through the string */
  309. b1 = true;
  310. if (*(ptr + e + q)) {
  311. *pString = concat(concat(*pString, *(ptr + e + q)), " ");
  312. }
  313. j = e; /* adjust the counter */
  314. }
  315. if (makeArgs(*pString, &argc, (const char ***) &argv, pipe, i, h)) {
  316. write_command(&w, argv, string[w]);
  317. w++;
  318.  
  319. } else {
  320. if (!b1) { /* no args (?) */
  321. for (int r = 0; argv[r] != NULL; r++) {
  322. string[i][r] = argv[r]; /* is this necessary? */
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. mylabel:
  330. free(ptr);
  331. bool boo = false;
  332. dump_argv((const char *) "d", argc, argv, boo);
  333. }
  334. }
  335. free(ptr1);
  336.  
  337. }
  338. for (i = 0; i < n; i++) {
  339. for (j = 0; DEBUG && string[i][j] != NULL; j++) {
  340. if (i == 0 && j == 0) printf("n");
  341. printf("p[%d][%d] %sn", i, j, string[i][j]);
  342. }
  343. structcommand[i].argv = string[i];
  344. }
  345. fflush(NULL);
  346. pid = fork();
  347. if (pid < 0) {
  348. perror("fork failed");
  349. return -1;
  350. }
  351. /* If we are the child process, then go execute the string.*/
  352. if (pid == 0) {
  353. /* spawn(cmd);*/
  354. fork_pipes(n, structcommand);
  355. }
  356. /*
  357. * We are the parent process.
  358. * Wait for the child to complete.
  359. */
  360. status = 0;
  361. while (((pid = waitpid(pid, &status, 0)) < 0) && (errno == EINTR));
  362. if (pid < 0) {
  363. fprintf(stderr, "Error from waitpid: %s", strerror(errno));
  364. return -1;
  365. }
  366. if (WIFSIGNALED(status)) {
  367. fprintf(stderr, "pid %ld: killed by signal %dn",
  368. (long) pid, WTERMSIG(status));
  369.  
  370. return -1;
  371. }
  372. }
  373. return WEXITSTATUS(status);
  374.  
  375.  
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement