QVariant variantFromString(const QString &s) { if (s.startsWith(QLatin1Char('@'))) { if (s.endsWith(")")) { if (s.startsWith(QLatin1String("@ByteArray("))) { return QVariant(QByteArray::fromBase64(s.toLatin1().mid(11, s.size() - 12))); // return QVariant(s.toLatin1().mid(11, s.size() - 12)); } else if (s.startsWith(QLatin1String("@Variant("))) { QByteArray a(QByteArray::fromBase64(s.toLatin1().mid(9, s.size() - 10))); QDataStream stream(&a, QIODevice::ReadOnly); stream.setVersion(QDataStream::Qt_4_5); QVariant result; stream >> result; return result; } else if (s.startsWith(QLatin1String("@Rect("))) { QStringList args = splitArgs(s, 5); if (args.size() == 4) return QVariant(QRect(args[0].toInt(), args[1].toInt(), args[2].toInt(), args[3].toInt())); } else if (s.startsWith(QLatin1String("@Size("))) { QStringList args = splitArgs(s, 5); if (args.size() == 2) return QVariant(QSize(args[0].toInt(), args[1].toInt())); } else if (s.startsWith(QLatin1String("@Point("))) { QStringList args = splitArgs(s, 6); if (args.size() == 2) return QVariant(QPoint(args[0].toInt(), args[1].toInt())); } else if (s == QLatin1String("@Invalid()")) { return QVariant(); } }
QSettings::SettingsMap jsonDocumentToVariantMap(const QJsonDocument& document) { if (!document.isObject()) { qWarning() << "Settings file does not contain a JSON object"; return QSettings::SettingsMap(); } auto object = document.object(); QSettings::SettingsMap map; for (auto it = object.begin(); it != object.end(); ++it) { QVariant result; if (!it->isString()) { result = it->toVariant(); } else { auto string = it->toString(); if (string.startsWith(QLatin1String("@@"))) { // Standard string starting with '@' result = QVariant(string.mid(1)); } else if (string.startsWith(QLatin1Char('@'))) { // Custom type to string if (string.endsWith(QLatin1Char(')'))) { if (string.startsWith(QLatin1String("@ByteArray("))) { result = QVariant(string.toLatin1().mid(11, string.size() - 12)); } else if (string.startsWith(QLatin1String("@Variant("))) { QByteArray a(string.toLatin1().mid(9)); QDataStream stream(&a, QIODevice::ReadOnly); stream.setVersion(QDataStream::Qt_4_0); stream >> result; } else if (string.startsWith(QLatin1String("@Rect("))) { QStringList args = splitArgs(string, 5); if (args.size() == 4) { result = QRect(args[0].toInt(), args[1].toInt(), args[2].toInt(), args[3].toInt()); } } else if (string.startsWith(QLatin1String("@Size("))) { QStringList args = splitArgs(string, 5); if (args.size() == 2) { result = QSize(args[0].toInt(), args[1].toInt()); } } else if (string.startsWith(QLatin1String("@Point("))) { QStringList args = splitArgs(string, 6); if (args.size() == 2) { result = QPoint(args[0].toInt(), args[1].toInt()); } } } } else { // Standard string
/* Gets an input line from the shell and splits it by spaces. * If the first token is an known function and the arguments match, the function is called. * Otherwise, the function prints an "invalid argument" message and returns. */ void parse(char* buff) { int index, cant; char flag = 0; char* args[6]; cant = splitArgs(args, buff); if (cant == -1) { printf(ANSI_COLOR_RED "No injection allowed\n" ANSI_COLOR_RESET); return; } // The first input is not a parameter cant -= 1; for (index = 0; !flag && index < COM_SIZE ; index++) { if (!strcmp(args[0], commands[index].name)) { flag = 1; if (cant != commands[index].argsCant) { printf("Se esperaban %i argumentos y se recibieron %i, revisa el comando \n", commands[index].argsCant, cant); } else { convertArg(args, commands[index].args, commands[index].argsCant); switch (commands[index].argsCant) { case 0: commands[index].function(); break; case 1: commands[index].function(args[1]); break; case 2: commands[index].function(args[1], args[2]); break; case 3: commands[index].function(args[1], args[2], args[3]); break; case 5: commands[index].function(args[1], args[2], args[3], args[4], args[5]); break; } } } } if (!flag) { printf(ANSI_COLOR_MAGENTA "Comando invalido: %s. Intente el comando 'help'\n", args[0]); } }
int main(int argc, char *argv[]) { initRoller(); //User input prompt variables char prompt[20] = "Dungeon Master>"; char input[300]; char *inputArgs[30]; int nowait; char buf[600]; ProcessList * pList = (ProcessList*) malloc(sizeof(ProcessList)); pList->size = 0; //Scans for argument inputs extern char *optarg; int ch; //Get all optional arguments using the getopt function while ((ch = getopt(argc, argv, "p:")) != EOF) { switch (ch) { //Prompt argument case 'p': strcpy(prompt, optarg); break; } } //Main loop while (1) { int status = 0; //Zero out the input buffer memset(&input, '\0', sizeof(input)); //Display the shell prompt printf("%s ", prompt); /** * * */ // int c = 0; // c = getchar(); // if (c == KEY_UP) { // printf("KEYUP\n"); // } else if (c == KEY_DOWN) { // printf("KEYDOWN\n"); // } else { // printf("\n\nKey entered: %c \n\n", c); // } /** * * */ //Grab input from user prompt fgets(input, sizeof(input), stdin); //Split the input line into an array of char arrays (string array) int numArgs; numArgs = splitArgs(input, inputArgs); //Check if a valid number of arguments have been set if (numArgs < 0) { printf("Invalid Argument(s)\n"); continue; } //Prevent execute from attempting to execute nothing (Simply pressing enter) if (numArgs == 1 && !strcmp(inputArgs[0], "\0")) { continue; } //Check for ampersand if (!(strcmp(inputArgs[numArgs - 1], "&"))) { //Set the nowait flag nowait = 1; //Remove the ampersand from the list of arguments sent to execute inputArgs[numArgs - 1] = '\0'; numArgs--; } else { nowait = 0; } //All of the checks for user input //exit - exit normally if (!(strcmp(inputArgs[0], "exit")) || !(strcmp(inputArgs[0], "quit"))) { exit(2); } //roll - roll some dice else if (!(strcmp(inputArgs[0], "roll"))) { if (numArgs > 2 && (!(strcmp(inputArgs[2], "-v")) || !(strcmp(inputArgs[2], "-verbose")))) { rollDiceCommand(inputArgs[1], 1); } else if (numArgs > 1) { rollDiceCommand(inputArgs[1], 0); } } // Command not found else { printf("Command not found\n"); } } }
::std::vector< ::std::string> ConstructorImpl::argumentSpellings() const { return splitArgs(m_argSpellings); }
static int parse_opcode_args(CSOUND *csound, OENTRY *opc) { OPCODINFO *inm = (OPCODINFO*) opc->useropinfo; char** in_args; char** out_args; char intypes[256]; char typeSpecifier[2]; char tempName[20]; int i = 0, err = 0; ARRAY_VAR_INIT varInit; typeSpecifier[1] = '\0'; // The following handles adding of extra 'o' type for optional // ksmps arg for all UDO's if (*inm->intypes == '0') { intypes[0] = 'o'; intypes[1] = '\0'; } else { snprintf(intypes, 256, "%so", inm->intypes); } in_args = splitArgs(csound, intypes); out_args = splitArgs(csound, inm->outtypes); if (*in_args[0] != '0') { while (in_args[i] != NULL) { char* in_arg = in_args[i]; snprintf(tempName, 20, "in%d", i); if (*in_arg == '[') { int dimensions = 0; while (*in_arg == '[') { dimensions += 1; in_arg += 1; } typeSpecifier[0] = *in_arg; // printf("Dimensions: %d SubArgType: %s\n", dimensions, typeSpecifier); CS_TYPE* type = csoundGetTypeWithVarTypeName(csound->typePool, typeSpecifier); if (type == NULL) { synterr(csound, Str("invalid input type for opcode %s"), in_arg); err++; continue; } varInit.dimensions = dimensions; varInit.type = type; CS_VARIABLE* var = csoundCreateVariable(csound, csound->typePool, (CS_TYPE*)&CS_VAR_TYPE_ARRAY, tempName, &varInit); var->dimensions = dimensions; csoundAddVariable(csound, inm->in_arg_pool, var); } else { char c = map_udo_in_arg_type(*in_arg); // printf("found arg type %s -> %c\n", in_arg, c); typeSpecifier[0] = c; CS_TYPE* type = csoundGetTypeWithVarTypeName(csound->typePool, typeSpecifier); if (type == NULL) { synterr(csound, Str("invalid input type for opcode %s"), in_arg); err++; continue; } CS_VARIABLE* var = csoundCreateVariable(csound, csound->typePool, type, tempName, NULL); csoundAddVariable(csound, inm->in_arg_pool, var); } i++; } } // inm->inchns = i + 1; /* Add one for optional local ksmps */ inm->inchns = i - 1; i = 0; if (*out_args[0] != '0') { while(out_args[i] != NULL) { char* out_arg = out_args[i]; snprintf(tempName, 20, "out%d", i); if (*out_arg == '[') { int dimensions = 0; while (*out_arg == '[') { dimensions += 1; out_arg += 1; } typeSpecifier[0] = *out_arg; //printf("Dimensions: %d SubArgType: %s\n", dimensions, typeSpecifier); CS_TYPE* type = csoundGetTypeWithVarTypeName(csound->typePool, typeSpecifier); if (type == NULL) { synterr(csound, Str("invalid output type for opcode %s"), out_arg); err++; continue; } varInit.dimensions = dimensions; varInit.type = type; CS_VARIABLE* var = csoundCreateVariable(csound, csound->typePool, (CS_TYPE*)&CS_VAR_TYPE_ARRAY, tempName, &varInit); var->dimensions = dimensions; csoundAddVariable(csound, inm->out_arg_pool, var); } else { char c = map_udo_out_arg_type(*out_arg); // printf("found arg type %s -> %c\n", out_arg, c); typeSpecifier[0] = c; CS_TYPE* type = csoundGetTypeWithVarTypeName(csound->typePool, typeSpecifier); if (type == NULL) { synterr(csound, Str("invalid output type for opcode %s"), out_arg); err++; continue; } CS_VARIABLE* var = csoundCreateVariable(csound, csound->typePool, type, tempName, NULL); csoundAddVariable(csound, inm->out_arg_pool, var); } i++; } } inm->outchns = i; opc->dsblksiz = (uint16) (sizeof(UOPCODE) + sizeof(MYFLT*) * (inm->inchns + inm->outchns)); opc->dsblksiz = ((opc->dsblksiz + (uint16) 15) & (~((uint16) 15))); /* align (needed ?) */ opc->intypes = cs_strdup(csound, intypes); opc->outypes = cs_strdup(csound, (inm->outtypes[0] == '0') ? "" : inm->outtypes); map_args(opc->intypes); map_args(opc->outypes); // /* count the number of arguments, and check types */ // default: // synterr(csound, Str("invalid input type for opcode %s"), inm->name); // err++; i--; // } // i++; types++; // if (UNLIKELY(i > OPCODENUMOUTS_MAX)) { // synterr(csound, Str("too many input args for opcode %s"), inm->name); // csound->LongJmp(csound, 1); // } // } // default: // synterr(csound, Str("invalid output type for opcode %s"), inm->name); // err++; i--; // } // i++; types++; // } int n=0; while(in_args[n] != NULL) { // printf("delete %p \n", argsFound[n]); csound->Free(csound, in_args[n]); n++; } csound->Free(csound, in_args); n = 0; while(out_args[n] != NULL) { // printf("delete %p \n", argsFound[n]); csound->Free(csound, out_args[n]); n++; } csound->Free(csound, out_args); return err; }
// Spawn //------------------------------------------------------------------------------ bool Process::Spawn( const char * executable, const char * args, const char * workingDir, const char * environment, bool shareHandles ) { ASSERT( !m_Started ); ASSERT( executable ); #if defined( __WINDOWS__ ) // Set up the start up info struct. STARTUPINFO si; ZeroMemory( &si, sizeof(STARTUPINFO) ); si.cb = sizeof( STARTUPINFO ); si.dwFlags |= STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; SECURITY_ATTRIBUTES sa; ZeroMemory( &sa, sizeof( SECURITY_ATTRIBUTES ) ); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = nullptr; m_SharingHandles = shareHandles; if ( m_RedirectHandles ) { // create the pipes if ( shareHandles ) { si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); si.hStdError = GetStdHandle(STD_ERROR_HANDLE); si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); } else { if ( ! CreatePipe( &m_StdOutRead, &m_StdOutWrite, &sa, MEGABYTE ) ) { return false; } SetHandleInformation( m_StdOutRead, HANDLE_FLAG_INHERIT, 0 ); if ( ! CreatePipe( &m_StdErrRead, &m_StdErrWrite, &sa, MEGABYTE ) ) { VERIFY( CloseHandle( m_StdOutRead ) ); VERIFY( CloseHandle( m_StdOutWrite ) ); return false; } SetHandleInformation( m_StdErrRead, HANDLE_FLAG_INHERIT, 0 ); si.hStdOutput = m_StdOutWrite; si.hStdError = m_StdErrWrite; si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); // m_StdInRead; } si.dwFlags |= STARTF_USESTDHANDLES; } // Make sure the first arg is the executable // We also need to make a copy, as CreateProcess can write back to this string AStackString< 1024 > fullArgs; fullArgs += '\"'; fullArgs += executable; fullArgs += '\"'; if ( args ) { fullArgs += ' '; fullArgs += args; } //fullArgs.Format( "\"%s\" %s", executable, args ); // create the child if ( !CreateProcess( nullptr, //executable, fullArgs.Get(), nullptr, nullptr, (BOOL)m_RedirectHandles, // inherit handles 0, (void *)environment, workingDir, &si, (LPPROCESS_INFORMATION)&m_ProcessInfo ) ) { return false; } m_Started = true; return true; #elif defined( __LINUX__ ) || defined( __APPLE__ ) // create StdOut and StdErr pipes to capture output of spawned process int stdOutPipeFDs[ 2 ]; int stdErrPipeFDs[ 2 ]; VERIFY( pipe( stdOutPipeFDs ) == 0 ); VERIFY( pipe( stdErrPipeFDs ) == 0 ); // fork the process const pid_t childProcessPid = fork(); if ( childProcessPid == -1 ) { // cleanup pipes VERIFY( close( stdOutPipeFDs[ 0 ] ) == 0 ); VERIFY( close( stdOutPipeFDs[ 1 ] ) == 0 ); VERIFY( close( stdErrPipeFDs[ 0 ] ) == 0 ); VERIFY( close( stdErrPipeFDs[ 1 ] ) == 0 ); ASSERT( false ); // fork failed - should not happen in normal operation return false; } const bool isChild = ( childProcessPid == 0 ); if ( isChild ) { VERIFY( dup2( stdOutPipeFDs[ 1 ], STDOUT_FILENO ) != -1 ); VERIFY( dup2( stdErrPipeFDs[ 1 ], STDERR_FILENO ) != -1 ); VERIFY( close( stdOutPipeFDs[ 0 ] ) == 0 ); VERIFY( close( stdOutPipeFDs[ 1 ] ) == 0 ); VERIFY( close( stdErrPipeFDs[ 0 ] ) == 0 ); VERIFY( close( stdErrPipeFDs[ 1 ] ) == 0 ); if ( workingDir ) { FileIO::SetCurrentDir( AStackString<>( workingDir ) ); } // split args AString fullArgs( args ); Array< AString > splitArgs( 64, true ); fullArgs.Tokenize( splitArgs ); // prepare args const size_t numArgs = splitArgs.GetSize(); char ** argv = FNEW( char *[ numArgs + 2 ] ); argv[ 0 ] = const_cast< char * >( executable ); for ( size_t i=0; i<numArgs; ++i ) { AString & thisArg = splitArgs[ i ]; if ( thisArg.BeginsWith( '"' ) && thisArg.EndsWith( '"' ) ) { // strip quotes thisArg.SetLength( thisArg.GetLength() - 1 ); // trim end quote argv[ i + 1 ] = thisArg.Get() + 1; // skip start quote continue; } argv[ i + 1 ] = thisArg.Get(); // leave arg as-is } argv[ numArgs + 1 ] = nullptr; // transfer execution to new executable execv( executable, argv ); exit( -1 ); // only get here if execv fails } else {
int main(int argc, char *argv[]) { //User input prompt variables char prompt[20] = "308sh>"; char input[300]; char *inputArgs[30]; int nowait; char buf[600]; ProcessList * pList = (ProcessList*) malloc(sizeof(ProcessList)); pList->size = 0; //Scans for argument inputs extern char *optarg; int ch; //Get all optional arguments using the getopt function while ((ch = getopt(argc, argv, "p:")) != EOF) { switch (ch) { //Prompt argument case 'p': strcpy(prompt, optarg); break; } } //Main loop while (1) { int status = 0; //Zero out the input buffer memset(&input, '\0', sizeof(input)); //Display the shell prompt printf("%s ", prompt); //Grab input from user prompt fgets(input, sizeof(input), stdin); //Check for status change of background child processes pid_t pid = waitpid(-1, &status, WNOHANG); if (pid > 0) { //Remove from process list Process * ended = removeFromList(pList, pid); if (ended != NULL) { printstatus(status, ended->pid, ended->procname); } } //Split the input line into an array of char arrays (string array) int numArgs; numArgs = splitArgs(input, inputArgs); //Check if a valid number of arguments have been set if (numArgs < 0) { printf("Invalid Argument(s)\n"); continue; } //Prevent execute from attempting to execute nothing (Simply pressing enter) if (numArgs == 1 && !strcmp(inputArgs[0], "\0")) { continue; } //Check for ampersand if (!(strcmp(inputArgs[numArgs - 1], "&"))) { //Set the nowait flag nowait = 1; //Remove the ampersand from the list of arguments sent to execute inputArgs[numArgs - 1] = '\0'; numArgs--; } else { nowait = 0; } int stdout_old = dup(1); int fd = 1; //Check for file redirect, needs at least 3 arguments: // "CMD", the file redirect ">" and the file to redirect to "FILE" // CMD > FILE if (numArgs > 2 && !(strcmp(inputArgs[numArgs - 2], ">"))) { //Open the file fd = open(inputArgs[numArgs - 1], O_WRONLY | O_APPEND | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); //Redirect standard out if (dup2(fd, 1) < 0 || fd < 0) { exit(-1); } //Zero these arguments memset(&(inputArgs[numArgs - 1]), '\0', sizeof(inputArgs[numArgs - 1])); memset(&(inputArgs[numArgs - 2]), '\0', sizeof(inputArgs[numArgs - 2])); numArgs -= 2; } //All of the checks for user input //exit - exit normally if (!(strcmp(inputArgs[0], "exit"))) { exit(2); } //pid - print the process ID else if (!(strcmp(inputArgs[0], "pid"))) { printf("Process id is: [%d]\n", getpid()); } //ppid - print the parent's process ID else if (!(strcmp(inputArgs[0], "ppid"))) { printf("Parent Process id is: [%d]\n", getppid()); } //pwd - print the parent's process ID else if (!(strcmp(inputArgs[0], "pwd"))) { printf("Present Wording Directory is: %s\n", getcwd(buf, sizeof(buf))); } //cd - change current directory else if (!(strcmp(inputArgs[0], "cd"))) { if (numArgs == 1 || !(strcmp(inputArgs[1], "~"))) { chdir(getenv("HOME")); } else { chdir(inputArgs[1]); } printf("Present Wording Directory is: %s\n", getcwd(buf, sizeof(buf))); } //get - get an environment variable else if (!(strcmp(inputArgs[0], "get"))) { if (numArgs > 1) { //Check if variable is set if (getenv(inputArgs[1])) { //If it is set, print it. printf("Environment variable %s has value: %s\n", inputArgs[1], getenv(inputArgs[1])); } else { //If not set, notify user printf("Environment variable %s is not set\n", inputArgs[1]); } } else { printf("No environment varialbe given\n"); } } else if (!(strcmp(inputArgs[0], "set"))) { if (numArgs > 2) { //Set environment variable and notify user what has been set to what if (!setenv(inputArgs[1], inputArgs[2], 1)) { printf( "Environment variable %s has been set with value: %s\n", inputArgs[1], inputArgs[2]); } } else if (numArgs == 2) { if (!unsetenv(inputArgs[1])) { printf("Environment variable %s has been unset\n", inputArgs[1]); } } else { printf("No environment varialbe given\n"); } } else if (!(strcmp(inputArgs[0], "jobs"))) { printProcesses(pList); } else { //If none of the built in functions, search the PATH for an executable Process * executed = (Process *) malloc(sizeof(Process)); execute(inputArgs, executed); if (!nowait) { //If nowait flag is not set, wait until child process completes waitpid(executed->pid, &status, 0); printstatus(status, executed->pid, executed->procname); } else { //Put the process in the processList table. addToList(pList, executed); //Or, if nowait, then process in the background printf("Process %s executing in background\n", executed->procname); } } //Route stdout to stdout //(In case previous command was piped to a file) //close previous file descriptor close(fd); //Flush standard out fflush(stdout); //Redirect file stream to old standard out if (dup2(stdout_old, 1) < 0) { exit(-1); } //Close the saved old standard out file descriptor close(stdout_old); } }