/*! Immediately send Error message * * @param value Error code to send */ void GdbInOut::sendErrorMessage(unsigned value) { if (errorLogger != 0) { char buff[200]; snprintf(buff, sizeof(buff), "Error response sent: E%2.2X", value); errorLogger(buff); } putGdbPreamble(); putGdbPrintf("E%2.2X", value); putGdbChecksum(); txGdbPkt(); }
/* * Sets the current work-directory, starting with ~ for the home directory */ void setCwd(){ if (getcwd(cwd, sizeof(cwd)) != NULL){ char* lowerCaseUsername = malloc(strlen(username) + 1); strcpy(lowerCaseUsername, username); lowerCaseUsername[0] = tolower(lowerCaseUsername[0]); char *s; s = strstr(cwd, lowerCaseUsername); if (s != NULL){ //Remove every character before up until the home directory and replace it with a ~ memmove(cwd, cwd + (s - cwd + strlen(username)) - 1, strlen(cwd+1)); cwd[0] = '~'; } }else{ errorLogger(1, "getcwd() error"); } }
/*! Immediately send Error message * * @param errorType One of E_Memory etc * @param msg Message to send */ void GdbInOut::sendErrorMessage(ErrorType errorType, const char *msg) { static const char *errStrings[] = {"fatal", "memType"}; if (errorType>(sizeof(errStrings)/sizeof(errStrings[0]))) { errorType = E_Fatal; } if (errorLogger != 0) { char buff[200]; snprintf(buff, sizeof(buff), "Error response sent: E.%s.%s", errStrings[errorType], msg); errorLogger(buff); } putGdbPreamble(); putGdbPrintf("E.%s.", errStrings[errorType]); while (*msg != '\0') { putGdbEscapedChar(*msg++); } putGdbChecksum(); txGdbPkt(); }
/* * Split line into command and arguments, then check via stat() if the file exists. * If file exists or may be in path, execute it. Otherwise exit. */ int parseCmdLine(char* cmd){ char *splitCmd, *savePtr; char *cmdArgs[MAX_ARGS]; int i = 1; //Splits the string by space or tabs. splitCmd = strtok_r(cmd, " ", &savePtr); cmdArgs[0] = splitCmd; //Chec if user is calling a file to execute, rather than a 'command' in Path if(cmdArgs[0][0] == '/' || cmdArgs[0][0] == '.'){ struct stat fileStat; if(stat(cmdArgs[0],&fileStat) < 0){ printf("The file " CCYAN "%s" CRESET " you were looking for %s, does unfortunately not exist there.\n", cmdArgs[0], username); _exit(0); } } //Split command/arguments as long as everything is split, or the limit is reached. while (splitCmd != NULL) { splitCmd = strtok_r(NULL, " ", &savePtr); if(splitCmd != NULL){ if(i < MAX_ARGS - 1){ cmdArgs[i] = splitCmd; i++; }else{ errorLogger(1, "Sorry, you can't enter more than 14 arguments. Executing may fail now. \n"); break; } } } cmdArgs[i] = NULL; //If command is succesfully executed, the program will never reach the printf or _exit(); execvp(cmdArgs[0], cmdArgs); printf("%s, " CCYAN "%s" CRESET " is unfortunately not a command.\n", username, cmdArgs[0]); _exit(0); }
inline void error(Args&&... args) { errorLogger()->output(std::forward<Args>(args)...); }