int main(int argc, char *argv[]) { int param = 1; myPrint(param); return 0; }
int main(int argc, char *argv[]) { char cmd_buff[1024]; char *pinput; char *fname = argv[1]; char *mode = "r"; FILE *f= fopen(fname, mode); cmd_line *c, *backup; char* extra_buff; char* test = malloc(sizeof(char)*514); char buf[1024]; if(argv[2] != NULL){ /*if trying to run batch with 2 filenames*/ err(); exit(0); } if(f == NULL){ /*if read is invalid*/ err(); exit(0); } dw = getcwd(buf, 1024); /*for global var accessible to function calls*/ while (1) { /*INTERACTIVE MODE*/ if(argv[1] == NULL){ myPrint("myshell> "); pinput = fgets(cmd_buff, 100, stdin); c = parseCmdLine(cmd_buff); backup = c; while(backup != NULL){ processCmd(backup->c); backup = backup->next; } /*BATCH MODE*/ } else if(argv[1] != NULL){ memset(cmd_buff,'\0',514); pinput = fgets(cmd_buff, 514, f); if (!pinput) { /*invalid fgets*/ exit(0); } if((extra_buff = strstr(cmd_buff,"\n"))!=NULL) {/*check if whole command has been taken in*/ if (check_space(cmd_buff)==0) /*if all blank spaces, don't print*/ continue; else { myPrint(cmd_buff); /*print command*/ collapsed_str(cmd_buff,test); /*CATCH ERRORS RIGHT AWAY*/ if(exception1(test)==1){ err(); } else if(exception2(test)==1) { err(); } else if(exception3(test)==1) { continue; } /*PARSE*/ else { c = parseCmdLine(cmd_buff); backup = c; while(backup != NULL){ processCmd(backup->c); backup = backup->next; } free_cmdline(c); } } } /*COMMAND TOO LONG*/ if(extra_buff==NULL) { myPrint(cmd_buff); while(fgets(cmd_buff,1024,f)!=NULL) {/*keep getting more chars while you can*/ if((strlen(cmd_buff)<1023) || cmd_buff[1022]=='\n') { /*if new one is shorter than max, break*/ myPrint(cmd_buff); memset(cmd_buff, '\0', 1024); break; } else myPrint(cmd_buff); /*else keep getting*/ memset(cmd_buff, '\0', 1024); } err(); } } } free(test); fclose(f); return 0; }
static void dumpBacktrace( unsigned int depth ) { if ( depth == 0 ) depth = 20; #if ((defined(linux) || defined(__linux__)) && !defined(ANDROID)) || defined(__FreeBSD__) // Below there is a bunch of operations that are not safe in multi-threaded // environment (dup()+close() combo, wait(), juggling with file descriptors). // Maybe some problems could be resolved with dup2() and waitpid(), but it seems // that if the operations on descriptors are not serialized, things will get nasty. // That's why there's this lovely mutex here... static QMutex sMutex; QMutexLocker locker( &sMutex ); int stderr_fd = -1; if ( access( "/usr/bin/c++filt", X_OK ) < 0 ) { myPrint( "Stacktrace (c++filt NOT FOUND):\n" ); } else { int fd[2]; if ( pipe( fd ) == 0 && fork() == 0 ) { close( STDIN_FILENO ); // close stdin // stdin from pipe if ( dup( fd[0] ) != STDIN_FILENO ) { QgsDebugMsg( "dup to stdin failed" ); } close( fd[1] ); // close writing end execl( "/usr/bin/c++filt", "c++filt", static_cast< char * >( nullptr ) ); perror( "could not start c++filt" ); exit( 1 ); } myPrint( "Stacktrace (piped through c++filt):\n" ); stderr_fd = dup( STDERR_FILENO ); close( fd[0] ); // close reading end close( STDERR_FILENO ); // close stderr // stderr to pipe int stderr_new = dup( fd[1] ); if ( stderr_new != STDERR_FILENO ) { if ( stderr_new >= 0 ) close( stderr_new ); QgsDebugMsg( "dup to stderr failed" ); } close( fd[1] ); // close duped pipe } void **buffer = new void *[ depth ]; int nptrs = backtrace( buffer, depth ); backtrace_symbols_fd( buffer, nptrs, STDERR_FILENO ); delete [] buffer; if ( stderr_fd >= 0 ) { int status; close( STDERR_FILENO ); int dup_stderr = dup( stderr_fd ); if ( dup_stderr != STDERR_FILENO ) { close( dup_stderr ); QgsDebugMsg( "dup to stderr failed" ); } close( stderr_fd ); wait( &status ); } #elif defined(Q_OS_WIN) // TODO Replace with incoming QgsStackTrace #else Q_UNUSED( depth ); #endif }
static void dumpBacktrace( unsigned int depth ) { if ( depth == 0 ) depth = 20; #if (defined(linux) && !defined(ANDROID)) || defined(__FreeBSD__) int stderr_fd = -1; if ( access( "/usr/bin/c++filt", X_OK ) < 0 ) { myPrint( "Stacktrace (c++filt NOT FOUND):\n" ); } else { int fd[2]; if ( pipe( fd ) == 0 && fork() == 0 ) { close( STDIN_FILENO ); // close stdin // stdin from pipe if ( dup( fd[0] ) != STDIN_FILENO ) { QgsDebugMsg( "dup to stdin failed" ); } close( fd[1] ); // close writing end execl( "/usr/bin/c++filt", "c++filt", ( char * ) 0 ); perror( "could not start c++filt" ); exit( 1 ); } myPrint( "Stacktrace (piped through c++filt):\n" ); stderr_fd = dup( STDERR_FILENO ); close( fd[0] ); // close reading end close( STDERR_FILENO ); // close stderr // stderr to pipe if ( dup( fd[1] ) != STDERR_FILENO ) { QgsDebugMsg( "dup to stderr failed" ); } close( fd[1] ); // close duped pipe } void **buffer = new void *[ depth ]; int nptrs = backtrace( buffer, depth ); backtrace_symbols_fd( buffer, nptrs, STDERR_FILENO ); delete [] buffer; if ( stderr_fd >= 0 ) { int status; close( STDERR_FILENO ); if ( dup( stderr_fd ) != STDERR_FILENO ) { QgsDebugMsg( "dup to stderr failed" ); } close( stderr_fd ); wait( &status ); } #elif defined(Q_OS_WIN) void **buffer = new void *[ depth ]; SymSetOptions( SYMOPT_DEFERRED_LOADS | SYMOPT_INCLUDE_32BIT_MODULES | SYMOPT_UNDNAME ); SymInitialize( GetCurrentProcess(), "http://msdl.microsoft.com/download/symbols", TRUE ); unsigned short nFrames = CaptureStackBackTrace( 1, depth, buffer, NULL ); SYMBOL_INFO *symbol = ( SYMBOL_INFO * ) qgsMalloc( sizeof( SYMBOL_INFO ) + 256 ); symbol->MaxNameLen = 255; symbol->SizeOfStruct = sizeof( SYMBOL_INFO ); for ( int i = 0; i < nFrames; i++ ) { SymFromAddr( GetCurrentProcess(), ( DWORD64 )( buffer[ i ] ), 0, symbol ); symbol->Name[ 255 ] = 0; myPrint( "%d: %s [%x]\n", i, symbol->Name, symbol->Address ); } qgsFree( symbol ); #endif }
void processCmd(cmd *c) /*to take a single command and then process it based on whether it is exit, cd, etc if it isnt any of them, pass off to execvp*/ { int n = r_length(c->t); char **argss = (char**)malloc(sizeof(char*) * (n+1)); arr_args(c->t, argss); char *first = c->t->tok; char buf[1024]; char *sw; char *filename; int status = 0; pid_t cpid; int x; char *str; char *curr; char cwd[256]; if(strcmp(first, "exit") == 0){ if(((c->t->next) == NULL) && (c->next == NULL)){ exit(0); } else { err(); } }else if (strcmp(first, "cd") == 0){ if(r_length(c->t) <= 2){ if(c->next == NULL){ if(c->t->next == NULL){ chdir((const char*) getenv("HOME")); } else if(chdir((const char*)c->t->next->tok) == -1) { err(); } else{ curr=getcwd(cwd, sizeof(cwd)); chdir(curr); setenv("PWD",curr,1); } } else { err(); } }else { err(); } } else if (strcmp(first, "pwd") == 0){ if((c->t->next == NULL) && (c->next == NULL)){ sw = getcwd(buf, 1024); if(sw != NULL){ myPrint(sw); write(1, "\n", 1); } else{ err(); } } else{ err(); } }else { if(cmd_length(c) > 2){ err(); } if(cmd_length(c) == 1){ cpid = fork(); waitpid(-1, &status, 0); if(cpid == 0){ if(execvp(first, argss) == -1) { err(); exit(-1); } kill(cpid, SIGKILL); } } if(cmd_length(c) == 2){ str =malloc((strlen(dw)+2)*sizeof(char)); strcpy(str, dw); strcat(str, "/"); filename = strcat(str, c->next->t->tok); if((x = open(filename, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) != -1){ cpid = fork(); waitpid(-1, &status, 0); if(cpid==0){ dup2(x, 1); if(execvp(first, argss) == -1) { err(); exit(-1); } kill(cpid, SIGKILL); close(x); free(str); } dup2(1,1); } else { err(); } } } free(argss); }
static void dumpBacktrace( unsigned int depth ) { if ( depth == 0 ) depth = 20; #if ((defined(linux) || defined(__linux__)) && !defined(ANDROID)) || defined(__FreeBSD__) int stderr_fd = -1; if ( access( "/usr/bin/c++filt", X_OK ) < 0 ) { myPrint( "Stacktrace (c++filt NOT FOUND):\n" ); } else { int fd[2]; if ( pipe( fd ) == 0 && fork() == 0 ) { close( STDIN_FILENO ); // close stdin // stdin from pipe if ( dup( fd[0] ) != STDIN_FILENO ) { QgsDebugMsg( "dup to stdin failed" ); } close( fd[1] ); // close writing end execl( "/usr/bin/c++filt", "c++filt", static_cast< char * >( nullptr ) ); perror( "could not start c++filt" ); exit( 1 ); } myPrint( "Stacktrace (piped through c++filt):\n" ); stderr_fd = dup( STDERR_FILENO ); close( fd[0] ); // close reading end close( STDERR_FILENO ); // close stderr // stderr to pipe int stderr_new = dup( fd[1] ); if ( stderr_new != STDERR_FILENO ) { if ( stderr_new >= 0 ) close( stderr_new ); QgsDebugMsg( "dup to stderr failed" ); } close( fd[1] ); // close duped pipe } void **buffer = new void *[ depth ]; int nptrs = backtrace( buffer, depth ); backtrace_symbols_fd( buffer, nptrs, STDERR_FILENO ); delete [] buffer; if ( stderr_fd >= 0 ) { int status; close( STDERR_FILENO ); int dup_stderr = dup( stderr_fd ); if ( dup_stderr != STDERR_FILENO ) { close( dup_stderr ); QgsDebugMsg( "dup to stderr failed" ); } close( stderr_fd ); wait( &status ); } #elif defined(Q_OS_WIN) void **buffer = new void *[ depth ]; SymSetOptions( SYMOPT_DEFERRED_LOADS | SYMOPT_INCLUDE_32BIT_MODULES | SYMOPT_UNDNAME ); SymInitialize( GetCurrentProcess(), "http://msdl.microsoft.com/download/symbols;http://download.osgeo.org/osgeo4w/symstore", TRUE ); unsigned short nFrames = CaptureStackBackTrace( 1, depth, buffer, nullptr ); SYMBOL_INFO *symbol = ( SYMBOL_INFO * ) qgsMalloc( sizeof( SYMBOL_INFO ) + 256 ); symbol->MaxNameLen = 255; symbol->SizeOfStruct = sizeof( SYMBOL_INFO ); IMAGEHLP_LINE *line = ( IMAGEHLP_LINE * ) qgsMalloc( sizeof( IMAGEHLP_LINE ) ); line->SizeOfStruct = sizeof( IMAGEHLP_LINE ); for ( int i = 0; i < nFrames; i++ ) { DWORD dwDisplacement; SymFromAddr( GetCurrentProcess(), ( DWORD64 )( buffer[ i ] ), 0, symbol ); symbol->Name[ 255 ] = 0; if ( SymGetLineFromAddr( GetCurrentProcess(), ( DWORD64 )( buffer[i] ), &dwDisplacement, line ) ) { myPrint( "%s(%d) : (%s) frame %d, address %x\n", line->FileName, line->LineNumber, symbol->Name, i, symbol->Address ); } else { myPrint( "%s(%d) : (%s) unknown source location, frame %d, address %x [GetLastError()=%d]\n", __FILE__, __LINE__, symbol->Name, i, symbol->Address, GetLastError() ); } } qgsFree( symbol ); qgsFree( line ); #else Q_UNUSED( depth ); #endif }
void execute(char *command) { int n = count_carrots(command); if(n>1){ myError(); return; } // char s[514]; //strcpy(s,command); // char* c = strtok(s, " "); if(!strncmp(command,">",1)){ myError(); return; } command = strtok(command, ">"); if(command==NULL) return; /*if(redir == 2){ myError(); return; }*/ char* fi = strtok(NULL, " > "); // fi = removeleadingspace(fi); // fi = strtok(fi, " "); //fi = strtok(fi, "\n"); if(n == 1){ sll* tempfi = parse_args(fi); if(sll_length(tempfi) > 1){ myError(); sll_free(tempfi); return; } sll_free(tempfi); } int des = 1; if(fi!=NULL) { if(!strncmp(command, "exit",4)|| !strncmp(command, "pwd",3) || !strncmp(command, "cd",2)){ myError(); return; } removeleadingspace(fi); fi = strtok(fi, " "); fi = strtok(fi, "\n"); des = open(fi, O_WRONLY | O_CREAT | O_EXCL, 0666); if(des==-1){ myError(); return; } } /*int bit = redirect(command); char* fname; if(bit == 2){ myError(); return; } else if(bit == 1) { // command to redirect command =strtok(command, ">"); fname = strtok(NULL, ">"); }*/ if(!strncmp(command, "exit",4)){ // myPrint(command); sll* temp = parse_args(command); if(sll_length(temp)==1){ sll_free(temp); exit(0); } else{ sll_free(temp); myError(); return; } } else if(!strncmp(command, " ",1)) { //separate(command, " ", execute); execute(removeleadingspace(command)); return; } else if(!strncmp(command, "cd",2)) { //myPrint(command); //myPrint("\n"); sll* temp = parse_args(command); if(sll_length(temp)>2){ myError(); sll_free(temp); return; } else if(sll_length(temp) == 1 && !strcmp(strtok(temp->s, "\n"),"cd")){ chdir(getenv("HOME")); return; } //char* dir = strtok(command, " "); char*dir = temp->s; // if(strtok(NULL, " ") == NULL){ //myError(); // return; //} if(strlen(dir)!=strlen("cd")&&strcmp(dir,"cd\n")){ myError(); return; } //dir = strtok(NULL, " "); dir = temp->next->s; char* dir2 = strtok(dir, "\n"); if(dir2==NULL) dir2 = getenv("HOME"); if(chdir(dir2)) myError(); return; } else if(!strcmp(command, "\n")) return; else{ pid_t pid = fork(); // child process executes command if(pid==0) { if(!strncmp(command, "pwd",3)){ // myPrint(command); // myPrint("\n"); sll* temp = parse_args(command); if(sll_length(temp) > 1){ sll_free(temp); myError(); exit(0); } sll_free(temp); char dir[514]; //if(!bit) dup2(des, STDOUT_FILENO); myPrint(getcwd(dir, 514)); //else // outp(getcwd(dir, 514),fname); dup2(des, STDOUT_FILENO); myPrint("\n"); exit(0); } // else if(!strncmp(command, " ",1)) { // separate(command, " ", execute); //} else{ sll* list = parse_args(command); char** argz = sll_to_array(list); // myPrint(command); //myPrint("\n"); dup2(des, STDOUT_FILENO); if(execvp(argz[0], argz) == -1){ myError(); exit(0); } sll_free(list); free(argz); } //else //myError(); dup2(des, STDOUT_FILENO); myPrint("\n"); exit(0); } //parent process waits else { int ch; waitpid(pid, &ch, 0); return; } } }
void ESOF_Callback(void) { myPrint(" USB ESOF", 32); }
void SUSP_Callback(void) { myPrint(" USB Suspend", 32); }
void WKUP_Callback(void) { myPrint(" USB Wakeup", 32); }
void ERR_Callback(void) { myPrint(" USB Error", 32); }
void RESET_Callback(void) { // print to display myPrint(" USB Reset", 32); }