int main(int argc, char *argv[]) { #else int gstat_main(int argc, char *argv[]) { #endif DATA **data = NULL, *valdata = NULL; /* * initialise some global variables: */ atexit(close_gstat_log_file); init_userio(1); init_global_variables(); argv0 = argv[0]; /* * register command line arguments on command_line: */ command_line = store_argv(argc, argv); parse_gstatrc(); /* * INPUT: command line options; */ parse_options(argc, argv); /* exits on -e options */ /* * start with program heading: */ printlog("%s: %s version %s\n", GSTAT_NAME, GSTAT_OS, VERSION); printlog("%s\n", GSTAT_CR); gstat_start(); /* * INPUT: Parse command files: */ if (optind == argc) { /* there's no command file name left */ if (get_method() != UIF) { /* -i or -m command line option */ /* no arguments */ printlog("Updates, manuals and source code: %s\n", GSTAT_HOME); printlog("%s\n", USAGE); ErrMsg(ER_NOCMD, ""); } else { start_ui(); exit(0); } } else { /* we have a command file to be read */ for ( ; optind < argc; optind++) { command_file_name = argv[optind]; parse_file(command_file_name); if (logfile_name != NULL) set_gstat_log_file(efopen(logfile_name, "w")); /* * get global variables locally: */ data = get_gstat_data(); valdata = get_dataval(); set_seed(gl_seed); /* * check variable settings and next * INPUT: read data values from file: */ read_all_data(data, valdata, get_n_vars()); if (get_method() == NSP) /* Still no answer to this: choose default */ set_method(get_default_method()); set_mode(); check_global_variables(); setup_meschach_error_handler(); if (DEBUG_DUMP) dump_all(); if (get_method() != NSP) printlog("[%s]\n", method_string(get_method())); if (check_only) set_method(NSP); /* * start calculations && OUTPUT routines: */ switch (get_method()) { case UIF: start_ui(); break; case NSP: break; case COV: case SEM: do_variogram(get_n_vars(), get_method()); break; case POLY: setup_poly_method(); /*FALLTHROUGH*/ default: if (gl_xvalid) /* validation/cross validation */ cross_valid(data); else predict_all(data); /* or prediction: */ break; } /* switch get_method() */ remove_all(); /* free all data etc. */ init_global_variables(); /* re-init for next round */ } } if (DEBUG_DUMP) atexit(print_file_record); if (get_method() != UIF) elapsed(); /* * file closing & data freeing time: */ if (plotfile != NULL) efclose(plotfile); exit(0); } /* end of main() */
/* initialize a new struct vm according to an a.out executable * image. * returns NULL on fail. * the user stack initialized like this: * |--------------- esp * | argc * |--------------- * | argv * |--------------- * | argv[0] "..." * | argv[1] "..." * | ... * | argv[n] "..." * --------------- VM_STACK * note: ignored envp yet. * */ int do_exec(char *path, char **argv){ struct inode *ip; struct buf *bp; struct sigaction *sa; struct ahead *ah; struct page *pg; struct vm *vm; struct vma *vp; struct file *fp; uint bn, fd, argc, esp, nr; char **tmp; ip = namei(path, 0); if (ip==NULL) { return syserr(ENOENT); } // read the first block of file to get the a.out header. bn = bmap(ip, 0, 0); if (bn == 0) { syserr(EINVAL); goto _badf; } bp = bread(ip->i_dev, bn); ah = (struct ahead*)bp->b_data; // check this a.out header. if (ah->a_magic != NMAGIC) { syserr(EINVAL); goto _badf; } // restore the path and argv temporarily tmp = store_argv(path, argv); // dettach the previous address space, and initialize a new one vm = &cu->p_vm; vm_clear(vm); vm_renew(vm, ah, ip); // push arguments to the end of user stack, which always the same address. esp = VM_STACK; argc = upush_argv(&esp, tmp); if (argc<0) panic("exec(): bad mem"); upush(&esp, &argc, sizeof(uint)); // free_argv(tmp); // close all the file descriptors with FD_CLOEXEC for (fd=0; fd<NOFILE; fd++) { fp = cu->p_ofile[fd]; if ((fp!=NULL) && (fp->f_fdflag & FD_CLOEXEC)) { do_close(fd); } } // clear all sigactions for (nr=0; nr<NSIG; nr++){ sa = &cu->p_sigact[nr]; sa->sa_handler = SIG_DFL; sa->sa_mask = 0; sa->sa_flags = 0; sa->sa_restorer = NULL; } // never forget this: brelse(bp); iput(ip); // enter user mode _retu(vm->vm_entry, esp); return 0; _badf: brelse(bp); iput(ip); return -1; }