Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- echo "-- Testing our implementation of OpenShell --"
- echo ""
- echo "- If you have any problem in passing a test read the corresponding"
- echo "- source file to understand what the test is checking"
- echo ""
- printf "********************* PRESS ENTER TO RUN TESTS ... "
- read
- echo "top -b -n1|head -8|tail -1" | ./shell
- printf "********************* TEST WILDCARDS ... "
- read
- echo ls -al openshell.* | ./shell
- printf "********************* TEST ALGORITHMS ... "
- read
- echo "top -b -n1|head -8|tail -1" | ./shell
- echo "who|awk '{print $4 ; print $3}'|sort -n|wc -l" | ./shell
- printf "********************* TEST CHECKENV. ... "
- echo "checkenv" | ./shell
- printf "********************* TEST DONE. YOU SHOULD SEE OUTPUT FROM TEST ABOVE ... "
- $ ./RUN_TESTS
- -- Testing our implementation of OpenShell --
- - If you have any problem in passing a test read the corresponding
- - source file to understand what the test is checking
- ********************* 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
- 889 message+ 20 0 44472 5528 3564 S 6,2 0,0 0:19.61 dbus-daemon
- ********************* 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
- p[0][0] ls p[0][1] -al p[0][2] openshell.h
- -rw-rw-r-- 1 dac dac 1731 maj 2 06:55 openshell.h
- ********************* 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
- 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
- 2
- ********************* 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
- ********************* TEST DONE. YOU SHOULD SEE OUTPUT FROM TEST ABOVE ..
- int main(int argc, char *argv[]) {
- struct sigaction sh;
- /* char *shell_prompt[100];*/
- sh.sa_handler = handler;
- sigemptyset(&sh.sa_mask);
- sh.sa_flags = 0;
- sigaction(SIGINT, &sh, NULL);
- int index = 0;
- int i;
- while (1) {
- index = 0;
- i = getopt_long(argc, argv, "p:vh",
- options, &index);
- if (i == -1)
- break;
- switch (i) {
- case 'p': {
- break;
- }
- case 'v': {
- printf("OpenShell version 0.1(a)n");
- printf("Version: %sn", VERSION);
- exit(EXIT_SUCCESS);
- }
- case 'h': {
- printf("Usage: ./shelln");
- exit(EXIT_SUCCESS);
- }
- default: {
- return 1;
- }
- }
- }
- getPath();
- char buf[256];
- char *malloced;
- char *shell_prompt = NULL;
- char *cwd = NULL;
- for (; ;) {
- if (!isatty(fileno(stdin)))
- //printf("stdin is a terminaln");
- //else
- {
- printf("stdin is a file or a pipen");
- command(readline(shell_prompt));
- exit(0);
- }
- cwd = malloc(sizeof(char *) * 100);
- if (cwd != NULL && getcwd(cwd, 99) == cwd) {
- printf("%s: > ", cwd);
- char *input = readline(NULL);
- add_history(input);
- command(input);
- free(input);
- }
- else {
- printf("%s: $ ", getenv("USER"));
- char *input = readline(NULL);
- add_history(input);
- command(input);
- free(input);
- }
- free(cwd);
- }
- return 0;
- }
- /*
- * Parse and execute one null-terminated command line string.
- * This breaks the command line up into words, checks to see if the
- * command is an alias, and expands wildcards.
- */
- int command(const char *cmd) {
- const char *endCmd;
- char cmdName[CMD_LEN];
- freeChunks();
- /*
- * Skip leading blanks.
- */
- if (cmd) {
- while (isBlank(*cmd))
- cmd++;
- /*
- * If the command is empty or is a comment then ignore it.
- */
- if (cmd) if ((*cmd == '