#include #include #include #include #include #include #include #define MAXLENGTH 1000 // max dlugosc lini #define MAXCOMMANDS 100 // max liczba parametrow #define MAXHISTORY 50 // max l. el. w historii #define MAXUSERNAMELENGTH 50 static struct termios oldt, newt; int countHistory; char * username; char * cwd; void clear(){ printf("\033c");//\033 == \x1B == 27 == ESC } void printHelp() { printf("\n"); printf("MyShell help(). Usage:\n"); printf("ls {path}\t Prints content of a directory\n"); printf("\t Default directory is \".\"\n"); printf("cd {path}\n"); printf("cp [src] [dst]\t Copies file from source to destination\n"); printf("exec [programname]\tStarts a program\n"); printf("mkdir [name]\tCreates new directory\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); } void init() { //wylaczenie buforu terminala tcgetattr( STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON); tcsetattr( STDIN_FILENO, TCSANOW, &newt); clear(); sleep(1); username = getenv("USER"); printf("Witaj, %s!\n", username); sleep(1); clear(); } // int prompt(){ if ( getcwd( cwd, MAXLENGTH * sizeof(char) ) == NULL ) { printf("GETCWD ERROR !"); return 0; } else { printf("[%s] %s> $",cwd, username); return 1; } } int readInput(char *input) { char *buf = malloc( MAXLENGTH + 1 ); int c; int count = 0; while(1) { c = getchar(); if ( (c == "\n") || (c==EOF) ) { buf[count] = "\0"; strcpy(input,buf); free (buf); return count; } } } int findPipes(char *input, char *toParse, char *cmdRest) { char * b = strtok(input, "|"); char * c = strtok(NULL, ""); strcpy(toParse,input); strcpy(toParse,b); if (strcmp(toParse,"")) return -1; else if (strcmp(cmdRest,"")) return 0; else return 1; } int parseSpace(char *input, char **parsed) { int i; char * token; for (i = 0; i < MAXCOMMANDS; i++) { while ((token = strtok(input," "))!=NULL){ strcpy(parsed[i],token); } } return 0; } int parseInput(char *input, char **parsed, char *cmdRest) { int isPiped = 0; char *toParse = calloc((MAXLENGTH+1)*sizeof(char),sizeof(char)); isPiped = findPipes(input, toParse, cmdRest); printf("Input: %s \n toParse: %s \n cmdRest: %s \n",input,toParse,cmdRest); if(isPiped == 1) { printf("FOUND PIPE!\n"); } parseSpace(toParse, parsed); return 0; } int main() { int i,cArgs,c; char *inputLine = malloc(MAXLENGTH + 1); char *cmdRest = malloc(MAXLENGTH + 1); username = malloc(MAXLENGTH + 1); cwd = malloc(MAXLENGTH + 1); char **parsedCmds = malloc(MAXCOMMANDS*sizeof(char*)); for (i=0;i