void MainWindow::parse() { QString code = ui->textEdit->toPlainText(); // QMessageBox::information(this, tr("Parse()"), tr("Beginning parse")); int index = 0; while (index < code.length()) { // QMessageBox::information(this, tr("Parse()"), tr("In the loop")); QString word; (extractWord(code, index, word)); // QMessageBox::information(this, tr("Parse() got the word"), tr("%1").arg(word)); if(word.length() == 2) { while (!(code.at(index).isLetterOrNumber() || (code.at(index) == ';'))) { index++; if(index == code.length()) break; } continue; } else index += word.length() - 1; QString type = " "; if(word.at(1) == ';') type = "comment"; else if(instructions.contains(word, Qt::CaseInsensitive)) { type.append(tr("instruction with arity %1").arg(arity(word))); interpret(word, index); updateListing(); } else if(registers.contains(word, Qt::CaseInsensitive)) { type = "register"; } else if(word.at(word.length() - 2) == 'H' || word.at(word.length() - 1) == 'H') type = "number"; else if(word.at(word.length() - 2) == ':' || labels.contains(word)) { if(!labels.contains(word)) labels.append(tr("%1").arg(word.left(word.length() - 2))); type = "label"; } else type = "unknown"; // QMessageBox::information(this, tr("Word found"), tr("\"%1\"\n%2").arg(word).arg(type)); } ui->statusBar->showMessage(tr("Labels: %1").arg(labels)); }
/* Given the name of a file exported from indexer, parse it back into * the linked list form. * */ SortedListPtr parseFileToList(char *filename) { FILE *fp = fopen(filename,"r"); if(fp == NULL) { printf("Unable to open file %s\n",filename); return NULL; } SortedListPtr newList = SLCreate(*compareWordStruct); WordPtr curWord = NULL; char line[128]; while(fgets(line,128,fp) != NULL) { //Trim the newline character. if(strlen(line) > 0) { line[strlen(line)-1] = '\0'; //printf("Input trimmed.\n"); } //Classify the type of line. enum lineType curLine = classifyLine(line); //printf("Line type: %d\n",curLine); switch(curLine) { case WordLine: //Extract the word from the line. curWord = extractWord(line); //printf("Inserting new word into list.\n"); SLInsert(newList, curWord); break; case FileStatsLine: //Parse the line //Add the file stats to curWord's filestatslist curWord->filestats = buildFileStatsList(line); break; case Throwaway: //Do nothing I guess. break; } } fclose(fp); return newList; }
int MainWindow::interpret(QString instruction, int index) { QMessageBox::information(this, tr("Interpret()"), tr("Interpreting the instruction %1").arg(instruction)); switch(arity(instruction)) { case 0: { if(instruction == " NOP ") { return OK; } if(instruction == " RLC ") { } if(instruction == " XTHL ") { } if(instruction == " RRC ") { } if(instruction == " PCHL ") { } if(instruction == " RAL ") { } if(instruction == " RAR ") { } if(instruction == " DAA ") { } if(instruction == " CMA ") { AReg = ~AReg; } if(instruction == " STC ") { CY = 1; } if(instruction == " CMC ") { CY = !CY; } if(instruction == " HLT ") { } if(instruction == " RNZ ") { } if(instruction == " RZ ") { } if(instruction == " RET ") { } if(instruction == " RNC ") { } if(instruction == " RC ") { } if(instruction == " RPO ") { } if(instruction == " RPE ") { } if(instruction == " PCHL ") { } if(instruction == " XCHG ") { } if(instruction == " RP ") { } if(instruction == " RM ") { } if(instruction == " SPHL ") { } } case 1: { if(instruction == " STAX ") { } if(instruction == " INX ") { QString operand; extractWord(ui->textEdit->toPlainText(), index, operand); if(operand == " B ") { CReg++; if(!CReg) BReg++; return OK; } else if(operand == " D ") { EReg++; if(!EReg) DReg++; return OK; } else if(operand == " H ") { LReg++; if(!LReg) HReg++; return OK; } else return INVALID_REGISTER_PAIR; } if(instruction == " INR ") { // QMessageBox::information(this, tr("Interpret()::if"), tr("Ïnterpreting the instruction INR")); QString operand; extractWord(ui->textEdit->toPlainText(), index, operand); ui->statusBar->showMessage(tr("\"%1\"").arg(operand)); // QMessageBox::information(this, tr("Interpret()::if"), tr("Here?")); if(operand == " A ") { AReg++; } else if(operand == " B ") { BReg++; } else if(operand == " C ") { CReg++; } else if(operand == " D ") { DReg++; } else if(operand == " E ") { EReg++; } else if(operand == " H ") { HReg++; } else if(operand == " L ") { LReg++; } else if(operand == " M ") { uint addr = 256 * HReg + LReg; memory[addr]++; } } if(instruction == " DCR ") { QString operand; extractWord(ui->textEdit->toPlainText(), index, operand); ui->statusBar->showMessage(tr("\"%1\"").arg(operand)); // QMessageBox::information(this, tr("Interpret()::if"), tr("Here?")); if(operand == " A ") { AReg--; } else if(operand == " B ") { BReg--; } else if(operand == " C ") { CReg--; } else if(operand == " D ") { DReg--; } else if(operand == " E ") { EReg--; } else if(operand == " H ") { HReg--; } else if(operand == " L ") { LReg--; } else if(operand == " M ") { uint addr = 256 * HReg + LReg; memory[addr]--; } } if(instruction == " DAD ") { } if(instruction == " LDAX ") { } if(instruction == " DCX ") { } if(instruction == " SHLD ") { } if(instruction == " LHLD ") { } if(instruction == " STA ") { } if(instruction == " LDA ") { } if(instruction == " ADD ") { } if(instruction == " ADC ") { } if(instruction == " SUB ") { } if(instruction == " SBB ") { } if(instruction == " ANA ") { } if(instruction == " XRA ") { } if(instruction == " ORA ") { } if(instruction == " CMP ") { } if(instruction == " POP ") { } if(instruction == " JNZ ") { } if(instruction == " JMP ") { } if(instruction == " CNZ ") { } if(instruction == " PUSH ") { } if(instruction == " ADI ") { } if(instruction == " RST ") { } if(instruction == " JZ ") { } if(instruction == " CZ ") { } if(instruction == " CALL ") { } if(instruction == " ACI ") { } if(instruction == " JNC ") { } if(instruction == " CNC ") { } if(instruction == " SUI ") { } if(instruction == " IN ") { } if(instruction == " SBI ") { } if(instruction == " POP ") { } if(instruction == " JPO ") { } if(instruction == " CPO ") { } if(instruction == " ANI ") { } if(instruction == " JPE ") { } if(instruction == " CPE ") { } if(instruction == " XRI ") { } if(instruction == " JP ") { } if(instruction == " CP ") { } if(instruction == " ORI ") { } if(instruction == " JM ") { } if(instruction == " CM ") { } if(instruction == " CPI ") { } } case 2: { if(instruction == " LXI ") { } if(instruction == " MVI ") { } if(instruction == " MOV ") { } } } return UNKOWN_ERROR; }
void runshell(){ int flag, inp_flag, out_flag, ifd, ofd, stdin_copy, stdout_copy; char pth[BUFFER]; char fileIn[BUFFER], fileOut[BUFFER]; strcpy(pth,""); while(1){ printf("myshell@%s> ", getcwd(line,BUFFER)); line[0] = '\0'; fgets(line, BUFFER, stdin); clearTabs(line); removeLastEnter(line); // printf("\nLINE = {%s}\n",line); int pipeCount = isPipingNeeded(line); // printf("\npipeCount = %d\n",pipeCount); if(pipeCount>0){ piping(line,pipeCount); continue; } /************ NO PIPING IS REQUIRED ***********/ inp_flag = extractWord(line, '<' ,fileIn); out_flag = extractWord(line,'>', fileOut); if(inp_flag != -1 && out_flag != -1){ if(inp_flag == 1){ stdin_copy = dup(0); close(0); ifd = open(fileIn, O_RDONLY); if (ifd < 0) { fprintf(stderr, "Unable to open input file in read mode...\n"); continue; } } if(out_flag == 1){ stdout_copy = dup(1); close(1); ofd = open(fileOut, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (ofd < 0) { fprintf(stderr, "Unable to open output file in write mode...\n"); continue; } } } if(feof(stdin)){ printf("\n"); exit(0); } separateWord(line); //printf("%s",words[0]); if(!strcmp(words[0],"cd")){ strcpy(pth,""); strcat(pth,getcwd(line,BUFFER)); //if(words[1][0] != '/') strcat(pth,"/"); strcat(pth,words[1]); flag = chdir(pth); if(flag == -1){ flag = chdir(words[1]); if(flag == -1) printf("\nNo Such file or directory !!\n"); // runProg(words[0],"",words[1]); } } else if(!strcmp(words[0],"pwd")){ printf("%s\n",getcwd(line,BUFFER)); } else if(!strcmp(words[0],"mkdir")){ strcpy(pth,""); strcat(pth,getcwd(line,BUFFER)); strcat(pth,"/"); strcat(pth,words[1]); flag = mkdir(pth,0777); if(flag == -1){ flag = mkdir(words[1],0777); if(flag == -1) runProg(words[0],"",words[1],0); } } else if(!strcmp(words[0],"rmdir")){ strcpy(pth,""); strcat(pth,getcwd(line,BUFFER)); strcat(pth,"/"); strcat(pth,words[1]); flag = rmdir(pth); if(flag == -1){ flag = rmdir(words[1]); if(flag == -1) runProg(words[0],"",words[1],0); } } else if(!strcmp(words[0],"ls") && !(words[1][0] == '-' && words[1][1] == 'l')){ DIR *dp; struct dirent *ep; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) puts (ep->d_name); (void) closedir (dp); } else puts ("Couldn't open the directory."); } else if(!strcmp(words[0],"ls") && words[1][0] == '-' && words[1][1] == 'l'){ DIR *d; struct dirent *de; struct stat buf; int i,j; char P[10]="rwxrwxrwx",AP[10]=" "; struct passwd *p; struct group *g; struct tm *t; char time[26]; d=opendir("."); readdir(d); readdir(d); while((de=readdir(d))!=NULL) { stat(de->d_name,&buf); // File Type if(S_ISDIR(buf.st_mode)) printf("d"); else if(S_ISREG(buf.st_mode)) printf("-"); else if(S_ISCHR(buf.st_mode)) printf("c"); else if(S_ISBLK(buf.st_mode)) printf("b"); else if(S_ISLNK(buf.st_mode)) printf("l"); else if(S_ISFIFO(buf.st_mode)) printf("p"); else if(S_ISSOCK(buf.st_mode)) printf("s"); //File Permissions P-Full Permissions AP-Actual Permissions for(i=0,j=(1<<8);i<9;i++,j>>=1) AP[i]= (buf.st_mode & j ) ? P[i] : '-' ; printf("%s",AP); //No. of Hard Links printf("%lu",buf.st_nlink); //User Name p=getpwuid(buf.st_uid); printf(" %.8s",p->pw_name); //Group Name g=getgrgid(buf.st_gid); printf(" %-8.8s",g->gr_name); //File Size printf(" %lu",buf.st_size); //Date and Time of modification t=localtime(&buf.st_mtime); strftime(time,sizeof(time),"%b %d %H:%M",t); printf(" %s",time); //File Name printf(" %s\n",de->d_name); } } else if(!strcmp(words[0],"exit")){
void runPipe(char **proglist,int pCount){ // printf("\nIN RUNPIPE\n"); // printf("\nPCOUNT = %d",pCount); for(int i=0;i<pCount;i++){ // printf("\n{%s}",proglist[i]); } // sleep(1); int pid; int status; int pipes[pCount - 1][2]; int isRoot = 0; int isEnd = 0; char FileIn[100]; char FileOut[100]; int stdin_copy = 0,stdout_copy = 0; while(1){ pid = fork(); if(pid!=0){ // parent process if(isRoot == 0){ // code at the root // printf("\nWE ARE AT ROOT\n"); waitpid(pid,&status,0); break; }else{ // code at other places waitpid(pid,&status,0); // printf("\nIS END = %d\n",isEnd); close(pipes[isEnd-1][1]); // sprintf(buffer,"\nCLOSING PIPE WRITE = %d",isEnd-1); // print(buffer); if(isRoot == 1){ }else{ close(1); dup2(pipes[isEnd-2][1],1); } close(0); dup2(pipes[isEnd-1][0],0); // printf("\nGOING TO EXECWRAPPER = {%s}\n",proglist[pCount - isEnd]); int k = execwrapper(proglist[pCount - isEnd]); // int k = execlp(proglist[pCount - isEnd], // proglist[pCount - isEnd],NULL); if(k == -1){ printf("\nEXEC NOT SUCCESSFUL !!\n"); exit(-1); } } }else{ // child process isRoot++; if(isEnd == pCount - 1){ inp_flag = extractWord(proglist[pCount - isEnd - 1], '<' ,fileIn); out_flag = extractWord(proglist[pCount - isEnd - 1],'>', fileOut); if(inp_flag != -1 && out_flag != -1){ if(inp_flag == 1){ stdin_copy = dup(0); close(0); ifd = open(fileIn, O_RDONLY); if (ifd < 0) { fprintf(stderr, "Unable to open input file in read mode...\n"); continue; } } if(out_flag == 1){ stdout_copy = dup(1); close(1); ofd = open(fileOut, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (ofd < 0) { fprintf(stderr, "Unable to open output file in write mode...\n"); continue; } } } close(pipes[isEnd-1][0]); close(1); dup2(pipes[isEnd-1][1],1); // printf("\nGOING TO EXECWRAPPER(tail) = {%s}\n",proglist[pCount - isEnd-1]); int k = execwrapper(proglist[pCount - isEnd - 1]); // int k = execlp(proglist[pCount - isEnd - 1], // proglist[pCount - isEnd - 1],NULL); if(k == -1){ printf("\nEXEC NOT SUCCESSFUL !!\n"); // sleep(1); exit(-1); } } else{ // printf("\nPIPE = %d",isEnd); pipe(pipes[isEnd]); // pipeid++; isEnd++; } } } return; }