├── .gitattributes ├── .gitignore ├── Techinical report for MR. Ehsan.docx ├── command.h ├── defineVariable.h └── globalFunction.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /Techinical report for MR. Ehsan.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazeelkhalid/Advance-shell-in-c-language/6622cf70072e434d144e4cd2476456f7a920569e/Techinical report for MR. Ehsan.docx -------------------------------------------------------------------------------- /command.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include"defineVariable.h" 6 | #include"process.h" 7 | #include 8 | 9 | struct command_t{ 10 | char *name; 11 | int argc; 12 | char *argv[20]; 13 | 14 | }; 15 | void initialization_command_t(struct command_t* temp){ 16 | temp->name = (char*) malloc(100); 17 | temp->argc = 0; 18 | for(int i = 0; i< 20; i++){ 19 | temp->argv[i] =(char*) malloc(100+1); 20 | } 21 | } 22 | 23 | void deallocation_command_t(struct command_t* temp){ 24 | if(temp->name!=NULL){ 25 | free(temp->name); 26 | } 27 | for(int i = 0; i< 20; i++){ 28 | if(temp->argv[i]!=NULL){ 29 | free(temp->argv[i]); 30 | } 31 | } 32 | } 33 | /**/ 34 | int getwhichCommandEnter(char *command) { // decide user enter which command 35 | if (!strcmp(command,"exit")) { // exit command 36 | return 1; 37 | } 38 | else if (!strcmp(command,"cd")) { // cd 39 | return 2; 40 | } 41 | else if (!strcmp(command,"jobs")) { // jobs 42 | return 3; 43 | } 44 | else if(!strcmp(command,"help")){ 45 | return 4; 46 | } 47 | else { // check externalCommand 48 | return 0; 49 | } 50 | } 51 | 52 | /**/ 53 | char** tokenizeSpace(const char* userCommand) { 54 | char** temp = (char**)malloc(20); 55 | int uSCommand = 0;// control user command index 56 | int i = 0; // control temp row index 57 | 58 | for (; userCommand[uSCommand] != '\0'; i++) { 59 | temp[i] = (char*)malloc(100); 60 | int tempIndex = 0; 61 | while (userCommand[uSCommand] != ' ' && userCommand[uSCommand] != '\0') { 62 | temp[i][tempIndex] = userCommand[uSCommand]; 63 | tempIndex++; 64 | uSCommand++; 65 | 66 | } 67 | uSCommand++; 68 | temp[i][tempIndex] = '\0'; 69 | } 70 | temp[i] = NULL; 71 | return temp; 72 | } 73 | 74 | /**/ 75 | char** parsePipe(const char* userCommand) { 76 | char** temp = (char**)malloc(20); 77 | int uSCommand = 0;// control user command index 78 | int i = 0; // control temp row index 79 | 80 | for (; userCommand[uSCommand] != '\0'; i++) { 81 | temp[i] = (char*)malloc(100); 82 | int tempIndex = 0; 83 | while (userCommand[uSCommand] != '|' && userCommand[uSCommand] != '\0') { 84 | temp[i][tempIndex] = userCommand[uSCommand]; 85 | tempIndex++; 86 | uSCommand++; 87 | 88 | } 89 | uSCommand++; 90 | temp[i][tempIndex] = '\0'; 91 | } 92 | temp[i] = NULL; 93 | return temp; 94 | } 95 | /**/ 96 | struct process* parseSpaces(char *userCommand) { 97 | 98 | struct process *new_proc = (struct process*) malloc(sizeof(struct process)); 99 | new_proc->command = userCommand; 100 | new_proc->argv = tokenizeSpace(userCommand); 101 | new_proc->argc = totalSpace(userCommand); 102 | new_proc->type = getwhichCommandEnter(new_proc->argv [0]); // retrun command type let say 0 for external 1 for exit so on so far 103 | new_proc->input_path = NULL; 104 | new_proc->output_path = NULL; 105 | new_proc->pid = -1; // bydefault process id 106 | new_proc->next = NULL; 107 | return new_proc; 108 | } 109 | 110 | int isHome(char *path){ // is home directory present in path 111 | return SearchSubString(path,"home"); 112 | } 113 | 114 | int countDirectories(char * path){ // return how many backslash exit in a path 115 | int count = 0; 116 | for(int i = 0;path[i]!='\0'; i++){ 117 | if(path[i] =='/'){ 118 | count++; 119 | } 120 | } 121 | return count; 122 | } -------------------------------------------------------------------------------- /defineVariable.h: -------------------------------------------------------------------------------- 1 | #ifndef defineVariable_H 2 | #define defineVariable_H 3 | 4 | #define JobListLength 20 // total Number of jobs that we can run in the back ground 5 | #define inputCommandLength 500 6 | #define delimeters " \t\r\n\a" 7 | #define maxArgument = 20 // store how many argument we can store 8 | #define commandLength = 100 // store command length 9 | 10 | 11 | struct Shell *shell; 12 | 13 | const char* storeProcessStatus[] = { "running", "done", "suspended", "continued", "terminated"}; 14 | 15 | struct Shell { 16 | char pw_dir[1000]; 17 | struct job *jobs[JobListLength + 1]; 18 | }; 19 | 20 | 21 | 22 | #endif //!defineVariable_H -------------------------------------------------------------------------------- /globalFunction.h: -------------------------------------------------------------------------------- 1 | #ifndef glonbalFunction 2 | #define glonbalFunction 3 | 4 | #include 5 | #include 6 | //display text in differen tcolor for example red green white etc 7 | void blueColor(){ 8 | printf("\033[0;34m"); 9 | } 10 | void greenColor(){ 11 | printf("\033[0;32m"); 12 | } 13 | 14 | void redColor(){ 15 | printf("\033[0;31m"); 16 | } 17 | 18 | void resetColor() { 19 | printf("\033[0m"); 20 | } 21 | 22 | //change text size 23 | void boldText(){ 24 | printf("\033[1m"); 25 | } 26 | 27 | void resetText(){ 28 | printf("\\033[22m"); 29 | } 30 | 31 | int strSize(char* str){ // return string size 32 | int size = 0; 33 | for(; str[size]!='\0';size++); 34 | return size; 35 | } 36 | int isForwardSlash(char* str){ // it will check is forward slash present in the string or not 37 | for(int i = 0; str[i]!='\0'; i++){ 38 | if(str[i] == '/'){ 39 | return 1; // means forward slash present 40 | } 41 | } 42 | 43 | return 0;// means forward slash not present 44 | } 45 | 46 | void removeDot(char* str){ // remove first dot that present on zero index of the array 47 | for(int i = 0; str[i] != '\0'; i++){ 48 | str[i] = str[i+1]; 49 | } 50 | } 51 | 52 | 53 | int comapreString(char * str1, char*str2){ 54 | if(strSize(str1) == strSize(str2)){ 55 | for(int i = 0; str2[i] !='\0'; i++){ 56 | if(str1[i] != str2[i]){ 57 | return 0; // strin not same 58 | } 59 | } 60 | return 1; // string same 61 | } 62 | else{ // string are not same 63 | return 0; 64 | } 65 | } 66 | 67 | int totalSpace(char* str){ // return how many spaces + 1 present in a strings 68 | int count = 0; 69 | for(int i = 0; str[i]!='\0'; i++){ 70 | if(str[i] == ' '){ 71 | count++; 72 | } 73 | } 74 | return count + 1; 75 | } 76 | int SearchSubString(char* str, char*search ){ // search sub string 77 | int size1 = strSize(str); 78 | int size2 = strSize(search); 79 | int i, j, flag; 80 | 81 | for (i = 0; i <= size1 - size2; i++) { 82 | for (j = i; j < i + size2; j++) { 83 | flag = 1; 84 | if (str[j] != search[j - i]) { 85 | flag = 0; 86 | break; 87 | } 88 | } 89 | if (flag == 1) { 90 | break; 91 | } 92 | } 93 | 94 | if (flag == 1){ 95 | return 1; 96 | } 97 | else{ 98 | return 0; 99 | } 100 | 101 | } 102 | 103 | char* concatinate(char* str1, char *str2){ 104 | int size1 = strSize(str1); 105 | int size2 = strSize(str2); 106 | char *temp = (char*)malloc(200*sizeof(char)); 107 | 108 | int i = 0; // control temp index; 109 | for(; str1[i]!='\0'; i++){ 110 | temp[i] = str1[i]; 111 | } 112 | for(int j = 0; str2[j]; j++, i++){ 113 | temp[i] = str2[j]; 114 | } 115 | temp[i] = '\0'; 116 | return temp; 117 | } 118 | 119 | int Copy(char * str1, char* str2, int index){ // always start copy from index that given in parameters 120 | int size = strSize(str2); 121 | if(str1!=NULL){ 122 | free(str1); 123 | } 124 | str1 = (char*)malloc(1000); 125 | for(int i = index; str2[i] != ' ' || str2[i] != '\0'; i++){ 126 | str1[i] = str2[i]; 127 | } 128 | return index; 129 | } 130 | 131 | char* removeSpaces(char* line) { // remove spaces from start or from end of the command 132 | char* end = NULL; 133 | char* start = line; 134 | 135 | while (*start == ' ') 136 | start++; 137 | 138 | for (end = line + strSize(line) - 1; end[0] == ' '; end--) { 139 | end[0] = end[1]; 140 | } 141 | end[1] = '\0'; 142 | 143 | return start; 144 | } 145 | 146 | 147 | #endif //!glonbalFunction; --------------------------------------------------------------------------------