static int batch(const char *name) { char *line = NULL; size_t len = 0; int ret = 0; if (name && strcmp(name, "-") != 0) { if (freopen(name, "r", stdin) == NULL) { fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n", name, strerror(errno)); return -1; } } if (rtnl_open(&rth, 0) < 0) { fprintf(stderr, "Cannot open rtnetlink\n"); return -1; } cmdlineno = 0; while (getcmdline(&line, &len, stdin) != -1) { char *largv[100]; int largc; largc = makeargs(line, largv, 100); if (largc == 0) continue; /* blank line */ if (my_do_cmd(largc, largv)) { fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno); ret = 1; if (!force) break; } } if (line) free(line); rtnl_close(&rth); return ret; }
void runAlias(Node * head, char * s, char * path){ char command[1000]; strcpy(command, findAlias(&head, s)); char ** argv; int argc; argc = makeargs(command, &argv); #if TESTER printf("command = %s\n", command); printf("ARGC = %d<----\n", argc); printargs(argc, argv); #endif runCommand(argv, argc, path); }
int main() { char **argv = NULL, s[MAX]; printf("Enter Strings serperated by space(exit to exit): "); fgets(s, MAX, stdin); strip(s); int argc; while(strcmp(s, "exit") != 0){ argc = makeargs(s, &argv); if(argc != -1) { printf("There are %d tokens.\nThe tokens are:\n", argc); printargs(argc, argv); clean(argc, argv); }// end if argv = NULL; printf("Enter Strings serperated by space(exit to exit): "); fgets(s, MAX, stdin); strip(s); } }// end main
/* fork_exec spawns a new process with the given commandline. * it returns -1 on error (errno set), 0 on process start error (rcode set), * a process descriptor otherwise. * Basically this is a child process and we run in the child's environment * after the first few lines. The setup hasn't been done and the command needs * to be started. * Redirection takes place if one of the handles env->input.hdls[0], * env->output.hdls[1] or env->error.hdls[1] is defined. Other handles (except * standard handles) are closed. env->subtype must be a SUBENVIR_... constant. * cmdline is the whole command line. * Never use TSD after the fork() since this is not only a different thread, * it's a different process! * Although mentioned in the documentation we have to use a backslash a * escape character nevertheless. It's a bug of EMX not to recognize a * circumflex as the default escape character and therefore we have to * use the "wrong" escape character. Note the difference between EMX and OS/2. * Maybe, I'm wrong. Drop me an email in this case. FGC */ static int Os2_fork_exec(tsd_t *TSD, environment *env, const char *cmdline, int *rcode) { static const char *interpreter[] = { "regina.exe", /* preferable even if */ /* not dynamic */ "rexx.exe" }; char **args = NULL; int saved_in = -1, saved_out = -1, saved_err = -1; int rc; const char *ipret; char *argline; int broken_address_command = get_options_flag( TSD->currlevel, EXT_BROKEN_ADDRESS_COMMAND ); int subtype; if (env->subtype == SUBENVIR_REXX) /*special situation caused by recursion*/ { environment e = *env; char *new_cmdline; int i, rc; unsigned len; if (argv0 == NULL) len = 11; /* max("rexx.exe", "regina.exe") */ else { len = strlen(argv0); if (len < 11) len = 11; /* max("rexx.exe", "regina.exe") */ } len += strlen(cmdline) + 2; /* Blank + term ASCII0 */ if ((new_cmdline = malloc(len)) == NULL) return(-1); /* ENOMEM is set */ if (argv0 != NULL) /* always the best choice */ { strcpy(new_cmdline, argv0); strcat(new_cmdline, " "); strcat(new_cmdline, cmdline); e.subtype = SUBENVIR_COMMAND; rc = Os2_fork_exec(TSD, &e, new_cmdline, &rc); if ( ( rc != 0 ) && ( rc != -1 ) ) { free(new_cmdline); return(rc); } } /* load an interpreter by name from the path */ for (i = 0; i < sizeof(interpreter) / sizeof(interpreter[0]);i++) { strcpy(new_cmdline, interpreter[i]); strcat(new_cmdline, " "); strcat(new_cmdline, cmdline); e.subtype = SUBENVIR_COMMAND; rc = Os2_fork_exec(TSD, &e, new_cmdline, &rc); if ( ( rc != 0 ) && ( rc != -1 ) ) { free(new_cmdline); return(rc); } } #ifndef __EMX__ *rcode = -errno; /* assume a load error */ free( new_cmdline ); return 0; #else if ( ( rc = fork() ) != 0 ) /* EMX is fork-capable */ return rc; #endif } #ifdef __EMX__ /* redirect this call to the non-OS/2-code if DOS is running */ /* SUBENVIR_REXX must(!!) fork if we are here! */ { extern OS_Dep_funcs __regina_OS_Other; if ((_osmode != OS2_MODE) && (env->subtype != SUBENVIR_REXX)) return(__regina_OS_Other.fork_exec(TSD, env, cmdline, rcode)); } #endif #define STD_REDIR(hdl,dest,save) if ((hdl != -1) && (hdl != dest)) \ { save = dup(dest); dup2(hdl, dest); } #define STD_RESTORE(saved,dest) if (saved != -1) \ { close(dest); dup2(saved,dest); \ close(saved); } #define SET_MAXHDL(hdl) if (hdl > max_hdls) max_hdls = hdl #define SET_MAXHDLS(ep) SET_MAXHDL(ep.hdls[0]); SET_MAXHDL(ep.hdls[1]) /* Force the standard redirections: */ STD_REDIR(env->input.hdls[0], 0, saved_in); STD_REDIR(env->output.hdls[1], 1, saved_out); if (env->error.SameAsOutput) { saved_err = dup(2); dup2(1, 2); } else { STD_REDIR(env->error.hdls[1], 2, saved_err); } /* * If the BROKEN_ADDRESS_COMMAND OPTION is in place, * and our environment is COMMAND, change it to SYSTEM */ if ( env->subtype == SUBENVIR_PATH /* was SUBENVIR_COMMAND */ && broken_address_command ) subtype = SUBENVIR_SYSTEM; else subtype = env->subtype; rc = -1; switch ( subtype ) { case SUBENVIR_PATH: args = makeargs(cmdline, '^'); rc = spawnvp(P_NOWAIT, *args, args); break; case SUBENVIR_COMMAND: args = makeargs(cmdline, '^'); rc = spawnv(P_NOWAIT, *args, args); break; case SUBENVIR_SYSTEM: /* insert "%COMSPEC% /c " or "%SHELL% -c " in front */ if ((ipret = getenv("COMSPEC")) != NULL) { argline = MallocTSD(strlen(ipret) + strlen(cmdline) + 5); strcpy(argline,ipret); strcat(argline," /c "); } else if ((ipret = getenv("SHELL")) != NULL) { argline = MallocTSD(strlen(ipret) + strlen(cmdline) + 5); strcpy(argline,ipret); strcat(argline," -c "); } else { ipret = "CMD.EXE"; argline = MallocTSD(strlen(ipret) + strlen(cmdline) + 5); strcpy(argline,ipret); strcat(argline," /c "); } strcat(argline, cmdline); args = makeargs(argline, '^'); rc = spawnvp(P_NOWAIT, *args, args); break; case SUBENVIR_REXX: { /* we are forked and we are the child!!!! */ /* last chance, worst choice, use the re-entering code: */ char *new_cmdline = malloc(strlen(cmdline) + 4); char **run; int i; strcpy(new_cmdline, "\"\" "); strcat(new_cmdline, cmdline); args = makeargs(new_cmdline, '^'); for (i = 0, run = args; *run; run++) i++; exit(__regina_reexecute_main(i, args)); } default: /* illegal subtype */ STD_RESTORE(saved_in, 0); STD_RESTORE(saved_out, 1); STD_RESTORE(saved_err, 2); errno = EINVAL; return -1; } *rcode = errno; STD_RESTORE(saved_in, 0); STD_RESTORE(saved_out, 1); STD_RESTORE(saved_err, 2); if (args != NULL) destroyargs(args); return ( rc == -1 ) ? 0 : rc; #undef SET_MAXHDLS #undef SET_MAXHDL #undef STD_RESTORE #undef STD_REDIR }
int main() { int argc, hist_count, dir, pipeCount, x; char **argv = NULL, s[MAX]; int preCount = 0, postCount = 0; int * execute;// (int *)calloc(2, sizeof(int)); char ** prePipe = NULL, ** postPipe = NULL, **preRedir = NULL, ** postRedir = NULL; char * path = NULL; LinkedList * hist_list = linkedList(); //--This will store history of size 100 LinkedList * alias_list = linkedList(); //--This will hold our history file data //////////--Begin initialization // check for .msshrc, if it's there // open it, otherwise create if( access( ".msshrc", F_OK ) != -1 ) //--.msshrc does exist { //--Since the file exists we are guarenteed // at least the HISTCOUNT and HISTFILECOUNT FILE * mssh_fin = fopen( ".msshrc", "r" ); init_boot( mssh_fin, alias_list, &path ); printf( ".msshrc successfully loaded.\n" ); printf("histcount: %d\nhistfilecount: %d\nPath: %s\n", HISTCOUNT, HISTFILECOUNT, path); }//end if else //--.msshrc does NOT exist { printf( ".msshrc not found, setting globals...\n" ); HISTCOUNT = 100; HISTFILECOUNT = 1000; }//end else // //--Check for history files if( access( ".mssh_history", F_OK ) != -1 ) //--.mssh_history does exist { FILE * hist_fin = fopen( ".mssh_history", "r" ); //--Fill our history list // //hist_count = read_hist_file( hist_fin, hist_list); printf( ".mssh_history successfully loaded.\n" ); }//end if else //--.mssh_history does NOT exist { printf( ".mssh_history not found.\n" ); }//end else //////////--End initialization printf( "Welcome to MSSH!\n\n" ); printf( "command?: " ); fgets( s, MAX, stdin ); strip( s ); while( strcmp( s, "exit" ) != 0 ) { int function_flag = 0; argc = makeargs( s, &argv, " \t\n" ); pipeCount = containsPipe( s ); // count pipes present int leftRedirCount = containsLeftRedir( s ); int rightRedirCount = containsRightRedir( s ); // Whatever the command may be, must be added to history if(strlen( s ) > 0) { argc = makeargs( s, &argv, " \t\n" ); addLast( hist_list, buildNode_hist( buildType_hist( argc, argv, hist_list->size + 1 ))); }//end if if( strcmp( s, "history" ) == 0 ) // History command check { setStart( hist_list, 0 ); printSection( hist_list, printType_hist, 0 ); //printList( hist_list, printType_hist ); }//end if if(leftRedirCount > 0 || rightRedirCount > 0 || pipeCount > 0) // contains redir or pipe { function_flag++; if( pipeCount > 0 ) // contains a pipe { if( leftRedirCount > 0 ) // contains pipe and a left redir '<' { if( leftRedirCount > 0 ) // contains all 3 { }//end if }//end if if( leftRedirCount > 0 ) // contains a pipe right redir '>' { }//end if else // contains only a pipe { prePipe = parsePrePipe( s, &preCount, '|'); postPipe = parsePostPipe( s, &postCount, '|'); pipeIt( prePipe, postPipe ); clean( preCount, prePipe ); clean( postCount, postPipe ); }//end else }//end if else if( leftRedirCount > 0 ) // contains a left redir '<' { if( rightRedirCount > 0 ) // contains a left and right redir '<' & '>' { }//end if else // only contains a left redir '<' { preRedir = parsePrePipe( s, &preCount, '<'); postRedir = parsePostPipe( s, &postCount, '<' ); executeRedir(preRedir, postRedir, '<'); clean( preCount, preRedir ); clean( postCount, postRedir ); }//end else }//end if else // only contains a right redir '>' { preRedir = parsePrePipe( s, &preCount, '>'); postRedir = parsePostPipe( s, &postCount, '>' ); executeRedir(preRedir, postRedir, '>'); clean( preCount, preRedir ); clean( postCount, postRedir ); }//end else }//end if // If none of cases hit, just make args and run it if( function_flag == 0 ) { argc = makeargs( s, &argv, " \t\n" ); if( strcmp(argv[0], "alias") == 0) { argc = makeargs( s, &argv, " "); Node * newAlias = buildNode_alias( buildType_alias( s )); addLast( alias_list, newAlias); printf("Current list of Alias's:\n"); printList( alias_list, printType_alias ); //See list of alias's }//end if /* --was using for alias structure, currently broken, causes a segfault. else { //checkAlias( s, alias_list, &execute ); // lastly we should check for an alias call //return value of 1 will execute the alias Node * cur = alias_list->head->next; Alias * run_al; if(*(execute + 0) == 1) { for( x = 0; x < *(execute + 1); x++ ) cur = cur->next; run_al = convert_data_Alias( cur->data ); forkIt( run_al->command ); }//end if }//end else */ if ( strcmp( argv[0], "cd" ) == 0 ) dir = chdir( argv[1] ); if( argc != -1 ) forkIt( argv ); clean( argc, argv ); argv = NULL; }//end else ////////////////////////////////////////////////////////////////////////////////////////// // Re-prompt for input printf( "command?: " ); fgets( s, MAX, stdin ); strip( s ); }// end while //--Write .mssh_history file setStart( hist_list, 1 ); printSection( hist_list, printType_histfile, 1 ); //--Clean: history clearList( hist_list, cleanType_hist ); free( hist_list ); hist_list = NULL; // //--Clean: alias clearList( alias_list, cleanType_alias ); free( alias_list ); alias_list = NULL; //free( execute ); // // Finished cleaning up memory // Safe to return. return 0; }// end main
int main(int argc, const char *argv[]) { char input[MAX_LINE]; int exitcode = 0; bool is_bootscript = true; coreid_t my_core_id = disp_get_core_id(); // XXX: parse aguments to determine input sources to use unsigned stdin_sources = 0; vfs_init(); for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "serial") == 0) { stdin_sources |= TERMINAL_SOURCE_SERIAL; } else if (strcmp(argv[i], "keyboard") == 0) { stdin_sources |= TERMINAL_SOURCE_KEYBOARD; } else if (strcmp(argv[i], "nobootscript") == 0) { is_bootscript = false; } } // fallback default: use serial, as before if (stdin_sources == 0) { stdin_sources = TERMINAL_SOURCE_SERIAL; } // XXX: All the following calls should go away once we have stable APIs errval_t e = terminal_want_stdin(stdin_sources); assert(err_is_ok(e)); cwd = strdup("/"); printf("fish v0.2 -- pleased to meet you!\n"); // run canned pre-boot commands if(is_bootscript) { runbootscript(); } for(;;) { int cmd_argc; char *cmd_argv[64]; // Support a max of 64 cmd args struct cmd *cmd; printf("> "); fflush(stdout); getline(input, MAX_LINE); cmd_argc = makeargs(input, cmd_argv); /* check for trailing '&' (== run in background) */ bool wait = true; if (cmd_argc > 0) { size_t len = strlen(cmd_argv[cmd_argc - 1]); if (len > 0 && cmd_argv[cmd_argc - 1][len - 1] == '&') { wait = false; // remove '&' character from args if (len == 1) { cmd_argc--; } else { cmd_argv[cmd_argc - 1][len - 1] = '\0'; } } } if (cmd_argc == 0) { continue; } else if ((cmd = find_command(cmd_argv[0])) != NULL) { exitcode = cmd->cmd(cmd_argc, cmd_argv); } else { // Try loading a program off disk if VFS is initialized domainid_t domain_id; exitcode = execute_program(my_core_id, cmd_argc, cmd_argv, &domain_id); // wait if it succeeds if(exitcode == 0 && wait) { exitcode = wait_domain_id(domain_id); char exitstr[128]; snprintf(exitstr, 128, "%u", exitcode); int r = setenv("EXITCODE", exitstr, 1); assert(r == 0); // Reacquire terminal for stdin e = terminal_want_stdin(stdin_sources); assert(err_is_ok(e)); } } } }
/** * Loads a model from a file. * Returns a pointer to the bone struct containing the data loaded. */ model *load_model(const char *file_name) { FILE *fp; char buffer[BUFF_LEN], *arg_list[MAX_ARGS]; int i, line = 0, arg_count; mesh *geo; bone *b_new; model *new_mdl = NULL; /* Attempt to open the requested file. Prints error message on error. */ if((fp = fopen(file_name, "r")) == NULL) { fprintf(stderr, "ERROR(load_model): Unable to open file '%s'.\n", file_name); return NULL; } while(fgets(buffer, BUFF_LEN - 1, fp) != NULL) { line++; /* No lines should be more then the max buffer size in length. If any * lines do breach this contraint, then the file is corrupt and will * not be loaded. */ if(buffer[strlen(buffer) - 1] != '\n') { fprintf(stderr, "ERROR(load_model): Buffer overflow in file '%s' line %d\n", file_name, line); return NULL; } buffer[strlen(buffer) - 1] = '\0'; arg_count = makeargs(buffer, SEPERATOR, arg_list, MAX_ARGS); if(arg_count == 0) continue; /* If there's an m as the first character (which there should be at the * head of each model file) then we prepare the model. */ if(buffer[0] == 'm') { if(arg_count != 4) continue; /* Create a new model struct. */ new_mdl = new_model(arg_list[1], atoi(arg_list[2])); if(new_mdl == NULL) return NULL; /* Initialise all the new models variables. */ new_mdl->texture = loadTexture(arg_list[3]); printf("New model '%s' created. Loading Model...\n", arg_list[1]); } /* If we have a bone, indicated by a b as the first character. Note that * bones can only be added if a model line has been read in. */ else if(buffer[0] == 'b') { if(new_mdl == NULL) { fprintf(stderr, "ERROR(load_mdl): Attempted load bone without model decl.\n"); continue; } if(arg_count < 7) continue; printf("Loading bone '%s' into model '%s'.\n", arg_list[1], new_mdl->name); new_mdl->n_bones++; /* Allocate memory for the bone object and various chores that need * to be done to the bone to keep it healthy. */ b_new = malloc(sizeof(bone)); if(b_new == NULL) { fprintf(stderr, "ERROR(load_model): Cannot allocate memory.\n"); exit(1); } b_new->child = NULL; b_new->sibling = NULL; b_new->child_count = 0; /** * Allocate memory for the bone name and copy the string accross. */ b_new->name = malloc(strlen(arg_list[1])); strcpy(b_new->name, arg_list[1]); /* Parse translations from the strings and put them into the bone's * translation struct. */ for(i = 0; i < TRANS_SIZE; i++) b_new->rot[i] = atof(arg_list[i + 2]); b_new->length = atof(arg_list[5]); /* Attempt to load a new mesh into the current bone. */ if(strlen(arg_list[7]) > 0) { geo = load_obj(arg_list[7]); if(geo == NULL) { fprintf(stderr, "Error loading obj %s\n", arg_list[7]); exit(1); } /* Create a vertex array out of the loaded obj and free the mesh * struct that was temporarily created. */ if(strlen(arg_list[8]) > 0 && arg_list[8][0] == 'F') b_new->geometry = mesh_to_array(geo, NORM_FLAT); else b_new->geometry = mesh_to_array(geo, NORM_SMOOTH); b_new->tri_count = geo->n_elements; free_mesh(geo); } else b_new->geometry = NULL; /* If the root bone has not been set then this is obviously the root * bone, or a file format error. */ if(new_mdl->root == NULL) { if(strlen(arg_list[6]) == 0) new_mdl->root = b_new; else { fprintf(stderr, "ERROR(load_model): Error, root bone must not have parent.\n"); free_model(new_mdl); return NULL; } } else { if(!skel_add_child(arg_list[6], new_mdl->root, b_new)) { printf("Cannot add child %s.\n", b_new->name); free_model(new_mdl); return NULL; } } } /* Start of an animation. */ else if(buffer[0] == 'a') { if(new_mdl == NULL) { fprintf(stderr, "ERROR(load_mdl): Attempted load animation without model decl.\n"); continue; } if(arg_count != 2) continue; load_animation(new_mdl, atoi(arg_list[1]), fp); } } if(new_mdl != NULL) new_mdl->bone_array = skel_make_array(new_mdl->root, new_mdl->n_bones); printf("New model '%s' successfully loaded.\n", new_mdl->name); printf("%d bones loaded.\n", new_mdl->n_bones); printf("%d animations loaded.\n", new_mdl->n_anims); return new_mdl; }