static List *mkcmdarg(Node *n) { int fd; char *name; Edata efifo; Estack *e = enew(Estack); List *ret = nnew(List); static int fifonumber = 0; name = nprint("/tmp/rc%d.%d", getpid(), fifonumber++); if (mkfifo(name, 0666) < 0) { uerror("mkfifo"); return NULL; } if (rc_fork() == 0) { setsigdefaults(FALSE); fd = rc_open(name, (n->u[0].i != rFrom) ? rFrom : rCreate); /* stupid hack */ if (fd < 0) { uerror("open"); exit(1); } if (mvfd(fd, (n->u[0].i == rFrom)) < 0) /* same stupid hack */ exit(1); redirq = NULL; walk(n->u[2].p, FALSE); exit(getstatus()); } efifo.name = name; except(eFifo, efifo, e); ret->w = name; ret->m = NULL; ret->n = NULL; return ret; }
extern void b_dot(char **av) { int fd; bool old_i = interactive, i = FALSE; Estack e; Edata star; av++; if (*av == NULL) return; if (streq(*av, "-i")) { av++; i = TRUE; } if (dasheye) { /* rc -i file has to do the right thing. reset the dasheye state to FALSE, though. */ dasheye = FALSE; i = TRUE; } if (*av == NULL) return; fd = rc_open(*av, rFrom); if (fd < 0) { uerror(*av); set(FALSE); return; } starassign(*av, av+1, TRUE); interactive = i; pushfd(fd); star.name = "*"; except(eVarstack, star, &e); doit(TRUE); varrm("*", TRUE); unexcept(); /* eVarstack */ interactive = old_i; }
void menu_make_cmd(char *rcfile, void *menu, item_func make_item_cb) { FILE *rc; if ((rc = rc_open(rcfile, "clientsrc"))) { do_launch_menu(rc, menu, make_item_cb); fclose(rc); } else { fprintf(stderr, "can't find any rc files\n"); exit(1); } }
static int rcextract(const char *archive, char **members) { RcArchive rca = rc_open_archive(archive, RC_RDONLY); RcMember m; if ( rca ) { for(m=rca->members; m; m = m->next) { if ( memberOfList(m->name, members) ) { FILE *fd = fopen(m->name, "wb"); if ( fd ) { RcObject o = rc_open(rca, m->name, m->rc_class, RC_RDONLY); char buf[8192]; size_t size = m->size; while( size > 0 ) { ssize_t n = rc_read(o, buf, sizeof(buf)); if ( n > 0 ) { if ( fwrite(buf, sizeof(char), (size_t)n, fd) != (size_t)n ) { fclose(fd); error("Failure writing %s: %s", m->name, strerror(errno)); } size -= n; } else if ( n == 0 ) { error("Premature EOF on archive %s", m->name); break; } else { error("Read error on archive %s", m->name); break; } } if ( size == 0 ) verbose("x %s\n", m->name); fclose(fd); rc_close(o); } else error("Could not open %s: %s", m->name, strerror(errno)); } } rc_close_archive(rca); return 0; } return badarchive(archive); }
extern int my_execve(const char *path, const char **av, const char **ev) { int fd, len, fst, snd, end; bool noarg; char pb[256]; /* arbitrary but generous limit */ execve(path, av, ev); if (errno != ENOEXEC) return -1; fd = rc_open(path, rFrom); giveupif(fd < 0); len = read(fd, pb, sizeof pb); close(fd); /* reject scripts which don't begin with #! */ giveupif(len <= 0 || pb[0] != '#' || pb[1] != '!'); for (fst = 2; fst < len && (pb[fst] == ' ' || pb[fst] == '\t'); fst++) ; /* skip leading whitespace */ giveupif(fst == len); for (snd = fst; snd < len && pb[snd] != ' ' && pb[snd] != '\t' && pb[snd] != '\n'; snd++) ; /* skip first arg */ giveupif(snd == len); noarg = (pb[snd] == '\n'); pb[snd++] = '\0'; /* null terminate the first arg */ if (!noarg) { while (snd < len && (pb[snd] == ' ' || pb[snd] == '\t')) snd++; /* skip whitespace to second arg */ giveupif(snd == len); noarg = (pb[snd] == '\n'); /* could have trailing whitespace after only one arg */ if (!noarg) { for (end = snd; end < len && pb[end] != ' ' && pb[end] != '\t' && pb[end] != '\n'; end++) ; /* skip to the end of the second arg */ giveupif(end == len); if (pb[end] == '\n') { pb[end] = '\0'; /* null terminate the first arg */ } else { /* else check for a spurious third arg */ pb[end++] = '\0'; while (end < len && (pb[end] == ' ' || pb[end] == '\t')) end++; giveupif(end == len || pb[end] != '\n'); } } } *av = path; if (!noarg) *--av = pb + snd; *--av = pb + fst; execve(*av, av, ev); return -1; fail: errno = ENOEXEC; return -1; }
/* * first read ~/.smbrc, next try to merge SMB_CFG_FILE */ int smb_open_rcfile(void) { char *home; int error; home = getenv("HOME"); if (home) { char fn[1024]; snprintf(fn, sizeof(fn), "%s/.nsmbrc", home); error = rc_open(fn, "r", &smb_rc); } error = rc_merge(SMB_CFG_FILE, &smb_rc); if (smb_rc == NULL) return ENOENT; return 0; }
int rc_merge(char *filename,struct rcfile **rcfile) { struct rcfile *rcp = *rcfile; FILE *f, *t; if (rcp == NULL) { return rc_open(filename,"r",rcfile); } f = fopen (filename, "r"); if (f==NULL) return errno; t = rcp->rf_f; rcp->rf_f = f; rc_parse(rcp); rcp->rf_f = t; fclose(f); return 0; }
/* * first read ~/.nwfsrc, next try to merge NWFS_CFG_FILE */ int ncp_open_rcfile(void) { char *home, *fn; int error; home = getenv("HOME"); if (home) { fn = malloc(strlen(home) + 20); sprintf(fn, "%s/.nwfsrc", home); error = rc_open(fn,"r",&ncp_rc); free (fn); } error = rc_merge(NWFS_CFG_FILE, &ncp_rc); if( ncp_rc == NULL ) { printf("Warning: no cfg files found.\n"); return 1; } return 0; }
/* * first read ~/.smbrc, next try to merge SMB_CFG_FILE */ int smb_open_rcfile(void) { char *home, *fn; int error; home = getenv("HOME"); if (home) { fn = malloc(strlen(home) + 20); sprintf(fn, "%s/.nsmbrc", home); error = rc_open(fn, "r", &smb_rc); free(fn); } error = rc_merge(SMB_CFG_FILE, &smb_rc); if (smb_rc == NULL) { printf("Warning: no cfg file(s) found.\n"); return ENOENT; } return 0; }
extern int main(int argc, char *argv[], char *envp[]) { char *dashsee[2], *dollarzero, *null[1]; int c; initprint(); dashsee[0] = dashsee[1] = NULL; dollarzero = argv[0]; rc_pid = getpid(); dashell = (*argv[0] == '-'); /* Unix tradition */ while ((c = rc_getopt(argc, argv, "c:deiIlnopsvx")) != -1) switch (c) { case 'c': dashsee[0] = rc_optarg; goto quitopts; case 'd': dashdee = TRUE; break; case 'e': dashee = TRUE; break; case 'I': dashEYE = TRUE; interactive = FALSE; break; case 'i': dasheye = interactive = TRUE; break; case 'l': dashell = TRUE; break; case 'n': dashen = TRUE; break; case 'o': dashoh = TRUE; break; case 'p': dashpee = TRUE; break; case 's': dashess = TRUE; break; case 'v': dashvee = TRUE; break; case 'x': dashex = TRUE; break; case '?': exit(1); } quitopts: argv += rc_optind; /* use isatty() iff neither -i nor -I is set, and iff the input is not from a script or -c flags */ if (!dasheye && !dashEYE && dashsee[0] == NULL && (dashess || *argv == NULL)) interactive = isatty(0); if (!dashoh) { checkfd(0, rFrom); checkfd(1, rCreate); checkfd(2, rCreate); } initsignal(); inithash(); initparse(); assigndefault("ifs", " ", "\t", "\n", (void *)0); #ifdef DEFAULTPATH assigndefault("path", DEFAULTPATH, (void *)0); #endif assigndefault("pid", nprint("%d", rc_pid), (void *)0); assigndefault("prompt", "; ", "", (void *)0); assigndefault("version", VERSION, "$Release: @(#)" PACKAGE " " VERSION " " RELDATE " $", (void *)0); initenv(envp); initinput(); null[0] = NULL; starassign(dollarzero, null, FALSE); /* assign $0 to $* */ inithandler(); if (dashell) { char *rcrc; int fd; rcrc = concat(varlookup("home"), word("/.rcrc", NULL))->w; fd = rc_open(rcrc, rFrom); if (fd == -1) { if (errno != ENOENT) uerror(rcrc); } else { bool push_interactive; pushfd(fd); push_interactive = interactive; interactive = FALSE; doit(TRUE); interactive = push_interactive; close(fd); } } if (dashsee[0] != NULL || dashess) { /* input from -c or -s? */ if (*argv != NULL) starassign(dollarzero, argv, FALSE); if (dashess) pushfd(0); else pushstring(dashsee, TRUE); } else if (*argv != NULL) { /* else from a file? */ b_dot(--argv); rc_exit(getstatus()); } else { /* else stdin */ pushfd(0); } dasheye = FALSE; doit(TRUE); rc_exit(getstatus()); return 0; /* Never really reached. */ }
// S1 int HistologicalEntities::plFindNucleusCandidates(const Mat& img, Mat& seg_norbc, ::cciutils::SimpleCSVLogger *logger, ::cciutils::cv::IntermediateResultHandler *iresHandler) { std::vector<Mat> bgr; split(img, bgr); if (logger) logger->logTimeSinceLastLog("toRGB"); Mat background = ::nscale::HistologicalEntities::getBackground(bgr, logger, iresHandler); int bgArea = countNonZero(background); float ratio = (float)bgArea / (float)(img.size().area()); //TODO: TMEP std::cout << " background size: " << bgArea << " ratio: " << ratio << std::endl; if (logger) logger->log("backgroundRatio", ratio); if (ratio >= 0.99) { //TODO: TEMP std::cout << "background. next." << std::endl; if (logger) logger->logTimeSinceLastLog("background"); return ::nscale::HistologicalEntities::BACKGROUND; } else if (ratio >= 0.9) { //TODO: TEMP std::cout << "background. next." << std::endl; if (logger) logger->logTimeSinceLastLog("background likely"); return ::nscale::HistologicalEntities::BACKGROUND_LIKELY; } if (logger) logger->logTimeSinceLastLog("background"); if (iresHandler) iresHandler->saveIntermediate(background, 1); Mat rbc = ::nscale::HistologicalEntities::getRBC(bgr, logger, iresHandler); if (logger) logger->logTimeSinceLastLog("RBC"); int rbcPixelCount = countNonZero(rbc); if (logger) logger->log("RBCPixCount", rbcPixelCount); // imwrite("test/out-rbc.pbm", rbc); if (iresHandler) iresHandler->saveIntermediate(rbc, 2); /* rc = 255 - r; rc_open = imopen(rc, strel('disk',10)); rc_recon = imreconstruct(rc_open,rc); diffIm = rc-rc_recon; */ Mat rc = ::nscale::PixelOperations::invert<unsigned char>(bgr[2]); if (logger) logger->logTimeSinceLastLog("invert"); Mat rc_open(rc.size(), rc.type()); //Mat disk19 = getStructuringElement(MORPH_ELLIPSE, Size(19,19)); // structuring element is not the same between matlab and opencv. using the one from matlab explicitly.... // (for 4, 6, and 8 connected, they are approximations). unsigned char disk19raw[361] = { 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0}; std::vector<unsigned char> disk19vec(disk19raw, disk19raw+361); Mat disk19(disk19vec); disk19 = disk19.reshape(1, 19); rc_open = ::nscale::morphOpen<unsigned char>(rc, disk19); // morphologyEx(rc, rc_open, CV_MOP_OPEN, disk19, Point(-1, -1), 1); if (logger) logger->logTimeSinceLastLog("open19"); if (iresHandler) iresHandler->saveIntermediate(rc_open, 3); // for generating test data // imwrite("test/in-imrecon-gray-marker.pgm", rc_open); // imwrite("test/in-imrecon-gray-mask.pgm", rc); // exit(0); // END for generating test data Mat rc_recon = ::nscale::imreconstruct<unsigned char>(rc_open, rc, 8); if (iresHandler) iresHandler->saveIntermediate(rc_recon, 4); Mat diffIm = rc - rc_recon; // imwrite("test/out-redchannelvalleys.ppm", diffIm); if (logger) logger->logTimeSinceLastLog("reconToNuclei"); int rc_openPixelCount = countNonZero(rc_open); if (logger) logger->log("rc_openPixCount", rc_openPixelCount); if (iresHandler) iresHandler->saveIntermediate(diffIm, 5); /* G1=80; G2=45; % default settings %G1=80; G2=30; % 2nd run bw1 = imfill(diffIm>G1,'holes'); * */ unsigned char G1 = 80; Mat diffIm2 = diffIm > G1; if (logger) logger->logTimeSinceLastLog("threshold1"); if (iresHandler) iresHandler->saveIntermediate(diffIm2, 6); // imwrite("in-fillHolesDump.ppm", diffIm2); Mat bw1 = ::nscale::imfillHoles<unsigned char>(diffIm2, true, 4); // imwrite("test/out-rcvalleysfilledholes.ppm", bw1); if (logger) logger->logTimeSinceLastLog("fillHoles1"); if (iresHandler) iresHandler->saveIntermediate(bw1, 7); // // TODO: change back // return ::nscale::HistologicalEntities::SUCCESS; /* * %CHANGE [L] = bwlabel(bw1, 8); stats = regionprops(L, 'Area'); areas = [stats.Area]; %CHANGE ind = find(areas>10 & areas<1000); bw1 = ismember(L,ind); bw2 = diffIm>G2; ind = find(bw1); if isempty(ind) return; end * */ int compcount2; //#if defined (USE_UF_CCL) Mat bw1_t = ::nscale::bwareaopen2(bw1, false, true, 11, 1000, 8, compcount2); // printf(" cpu compcount 11-1000 = %d\n", compcount2); //#else // Mat bw1_t = ::nscale::bwareaopen<unsigned char>(bw1, 11, 1000, 8, compcount); //#endif if (logger) logger->logTimeSinceLastLog("areaThreshold1"); bw1.release(); if (iresHandler) iresHandler->saveIntermediate(bw1_t, 8); if (compcount2 == 0) { return ::nscale::HistologicalEntities::NO_CANDIDATES_LEFT; } unsigned char G2 = 45; Mat bw2 = diffIm > G2; if (iresHandler) iresHandler->saveIntermediate(bw2, 9); /* * [rows,cols] = ind2sub(size(diffIm),ind); seg_norbc = bwselect(bw2,cols,rows,8) & ~rbc; seg_nohole = imfill(seg_norbc,'holes'); seg_open = imopen(seg_nohole,strel('disk',1)); * */ seg_norbc = ::nscale::bwselect<unsigned char>(bw2, bw1_t, 8); if (iresHandler) iresHandler->saveIntermediate(seg_norbc, 10); seg_norbc = seg_norbc & (rbc == 0); if (iresHandler) iresHandler->saveIntermediate(seg_norbc, 11); // imwrite("test/out-nucleicandidatesnorbc.ppm", seg_norbc); if (logger) logger->logTimeSinceLastLog("blobsGt45"); return ::nscale::HistologicalEntities::CONTINUE; }
int parse_config(char *cfgfname, const char *devfname) { int err = 0, fd; unsigned int chipsectionscnt, ctrlsectionscnt, i; struct rcfile *f; struct sim_chip *chips; struct sim_ctrl *ctrls; err = rc_open(cfgfname, "r", &f); if (err) { error("could not open configuration file (%s)", cfgfname); return (EX_NOINPUT); } /* First, try to configure simulator itself. */ if (configure_sim(devfname, f) != EX_OK) { rc_close(f); return (EINVAL); } debug("SIM CONFIGURED!\n"); /* Then create controllers' configs */ if (create_ctrls(f, &ctrls, &ctrlsectionscnt) != 0) { rc_close(f); return (ENXIO); } debug("CTRLS CONFIG READ!\n"); /* Then create chips' configs */ if (create_chips(f, &chips, &chipsectionscnt) != 0) { destroy_ctrls(ctrls); rc_close(f); return (ENXIO); } debug("CHIPS CONFIG READ!\n"); if (validate_ctrls(ctrls, ctrlsectionscnt) != 0) { destroy_ctrls(ctrls); destroy_chips(chips); rc_close(f); return (EX_SOFTWARE); } if (validate_chips(chips, chipsectionscnt, ctrls, ctrlsectionscnt) != 0) { destroy_ctrls(ctrls); destroy_chips(chips); rc_close(f); return (EX_SOFTWARE); } /* Open device */ fd = open(devfname, O_RDWR); if (fd == -1) { error("could not open simulator device file (%s)!", devfname); rc_close(f); destroy_chips(chips); destroy_ctrls(ctrls); return (EX_OSFILE); } debug("SIM CONFIG STARTED!\n"); /* At this stage, both ctrls' and chips' configs should be valid */ for (i = 0; i < ctrlsectionscnt; i++) { err = ioctl(fd, NANDSIM_CREATE_CTRL, &ctrls[i]); if (err) { if (err == EEXIST) error("Controller#%d already created\n", ctrls[i].num); else if (err == EINVAL) error("Incorrect controler number (%d)\n", ctrls[i].num); else error("Could not created controller#%d\n", ctrls[i].num); /* Errors during controller creation stops parsing */ close(fd); rc_close(f); destroy_ctrls(ctrls); destroy_chips(chips); return (ENXIO); } debug("CTRL#%d CONFIG STARTED!\n", i); } for (i = 0; i < chipsectionscnt; i++) { err = ioctl(fd, NANDSIM_CREATE_CHIP, &chips[i]); if (err) { if (err == EEXIST) error("Chip#%d for controller#%d already " "created\n", chips[i].num, chips[i].ctrl_num); else if (err == EINVAL) error("Incorrect chip number (%d:%d)\n", chips[i].num, chips[i].ctrl_num); else error("Could not create chip (%d:%d)\n", chips[i].num, chips[i].ctrl_num); error("Could not start chip#%d\n", i); destroy_chips(chips); destroy_ctrls(ctrls); close(fd); rc_close(f); return (ENXIO); } } debug("CHIPS CONFIG STARTED!\n"); close(fd); rc_close(f); destroy_chips(chips); destroy_ctrls(ctrls); return (0); }
extern void doredirs() { List *fname; int fd, p[2]; Rq *r; for (r = redirq; r != NULL; r = r->n) { switch(r->r->type) { default: panic("unexpected node in doredirs"); /* NOTREACHED */ case nRedir: if (r->r->u[0].i == rHerestring) { fname = flatten(glom(r->r->u[2].p)); /* fname is really a string */ if (pipe(p) < 0) { uerror("pipe"); rc_error(NULL); } if (rc_fork() == 0) { /* child writes to pipe */ setsigdefaults(FALSE); close(p[0]); if (fname != NULL) writeall(p[1], fname->w, strlen(fname->w)); exit(0); } else { close(p[1]); if (mvfd(p[0], r->r->u[1].i) < 0) rc_error(NULL); } } else { fname = glob(glom(r->r->u[2].p)); if (fname == NULL) rc_error("null filename in redirection"); if (fname->n != NULL) rc_error("multi-word filename in redirection"); switch (r->r->u[0].i) { default: panic("unexpected node in doredirs"); /* NOTREACHED */ case rCreate: case rAppend: case rFrom: fd = rc_open(fname->w, r->r->u[0].i); break; } if (fd < 0) { uerror(fname->w); rc_error(NULL); } if (mvfd(fd, r->r->u[1].i) < 0) rc_error(NULL); } break; case nDup: if (r->r->u[2].i == -1) close(r->r->u[1].i); else if (r->r->u[2].i != r->r->u[1].i) { if (dup2(r->r->u[2].i, r->r->u[1].i) < 0) { uerror("dup2"); rc_error(NULL); } } } } redirq = NULL; }
int rc_parse (void) { char *fn; struct keyword *kw; fn = file_in_dir (user_rcdir, RC_FILE); rc_open (fn); g_free (fn); if(!rc) return -1; while ((token = rc_next_token ()) != TOKEN_EOF) { switch (token) { case TOKEN_KEYWORD: for (kw = keywords; kw->name; kw++) { if (strcmp (token_str, kw->name) == 0) { switch (kw->required) { case KEYWORD_INT: if (rc_expect_token (TOKEN_INT)) config_set_int (kw->config, token_int); break; case KEYWORD_BOOL: if (rc_expect_token (TOKEN_INT)) config_set_bool (kw->config, token_int); break; case KEYWORD_STRING: if (rc_expect_token (TOKEN_STRING)) config_set_string (kw->config, token_str); break; } break; } } break; case TOKEN_EOL: case TOKEN_EOF: break; default: syntax_error (); rc_skip_to_eol (); break; } /* switch */ } /* while */ rc_close (); /* Compatibility with old versions */ if (!games[Q1_SERVER].dir && games[QW_SERVER].dir) { games[Q1_SERVER].dir = g_strdup (games[QW_SERVER].dir); config_set_string ("/" CONFIG_FILE "/Game: QS/dir", games[Q1_SERVER].dir); } if (default_w_switch < 0) default_w_switch = 0; if (default_b_switch < 0) default_b_switch = 0; return 0; }
extern bool walk(Node *n, bool parent) { top: sigchk(); if (n == NULL) { if (!parent) exit(0); set(TRUE); return TRUE; } switch (n->type) { case nArgs: case nBackq: case nConcat: case nCount: case nFlat: case nLappend: case nRedir: case nVar: case nVarsub: case nWord: exec(glob(glom(n)), parent); /* simple command */ break; case nBody: walk(n->u[0].p, TRUE); WALK(n->u[1].p, parent); /* WALK doesn't fall through */ case nNowait: { int pid; if ((pid = rc_fork()) == 0) { #if defined(RC_JOB) && defined(SIGTTOU) && defined(SIGTTIN) && defined(SIGTSTP) setsigdefaults(FALSE); rc_signal(SIGTTOU, SIG_IGN); /* Berkeleyized version: put it in a new pgroup. */ rc_signal(SIGTTIN, SIG_IGN); rc_signal(SIGTSTP, SIG_IGN); setpgid(0, getpid()); #else setsigdefaults(TRUE); /* ignore SIGINT, SIGQUIT, SIGTERM */ #endif mvfd(rc_open("/dev/null", rFrom), 0); walk(n->u[0].p, FALSE); exit(getstatus()); } if (interactive) fprint(2, "%d\n", pid); varassign("apid", word(nprint("%d", pid), NULL), FALSE); redirq = NULL; /* kill pre-redir queue */ break; } case nAndalso: { bool oldcond = cond; cond = TRUE; if (walk(n->u[0].p, TRUE)) { cond = oldcond; WALK(n->u[1].p, parent); } else cond = oldcond; break; } case nOrelse: { bool oldcond = cond; cond = TRUE; if (!walk(n->u[0].p, TRUE)) { cond = oldcond; WALK(n->u[1].p, parent); } else cond = oldcond; break; } case nBang: set(!walk(n->u[0].p, TRUE)); break; case nIf: { bool oldcond = cond; Node *true_cmd = n->u[1].p, *false_cmd = NULL; if (true_cmd != NULL && true_cmd->type == nElse) { false_cmd = true_cmd->u[1].p; true_cmd = true_cmd->u[0].p; } cond = TRUE; if (!walk(n->u[0].p, TRUE)) true_cmd = false_cmd; /* run the else clause */ cond = oldcond; WALK(true_cmd, parent); } case nWhile: { Jbwrap j; Edata jbreak; Estack e1, e2; bool testtrue, oldcond = cond; cond = TRUE; if (!walk(n->u[0].p, TRUE)) { /* prevent spurious breaks inside test */ cond = oldcond; break; } if (sigsetjmp(j.j, 1)) break; jbreak.jb = &j; except(eBreak, jbreak, &e1); do { Edata block; block.b = newblock(); cond = oldcond; except(eArena, block, &e2); walk(n->u[1].p, TRUE); testtrue = walk(n->u[0].p, TRUE); unexcept(); /* eArena */ cond = TRUE; } while (testtrue); cond = oldcond; unexcept(); /* eBreak */ break; } case nForin: { List *l, *var = glom(n->u[0].p); Jbwrap j; Estack e1, e2; Edata jbreak; if (sigsetjmp(j.j, 1)) break; jbreak.jb = &j; except(eBreak, jbreak, &e1); for (l = listcpy(glob(glom(n->u[1].p)), nalloc); l != NULL; l = l->n) { Edata block; assign(var, word(l->w, NULL), FALSE); block.b = newblock(); except(eArena, block, &e2); walk(n->u[2].p, TRUE); unexcept(); /* eArena */ } unexcept(); /* eBreak */ break; } case nSubshell: if (dofork(TRUE)) { setsigdefaults(FALSE); walk(n->u[0].p, FALSE); rc_exit(getstatus()); } break; case nAssign: if (n->u[0].p == NULL) rc_error("null variable name"); assign(glom(n->u[0].p), glob(glom(n->u[1].p)), FALSE); set(TRUE); break; case nPipe: dopipe(n); break; case nNewfn: { List *l = glom(n->u[0].p); if (l == NULL) rc_error("null function name"); while (l != NULL) { if (dashex) prettyprint_fn(2, l->w, n->u[1].p); fnassign(l->w, n->u[1].p); l = l->n; } set(TRUE); break; } case nRmfn: { List *l = glom(n->u[0].p); while (l != NULL) { if (dashex) fprint(2, "fn %S\n", l->w); fnrm(l->w); l = l->n; } set(TRUE); break; } case nDup: redirq = NULL; break; /* Null command */ case nMatch: { List *a = glob(glom(n->u[0].p)), *b = glom(n->u[1].p); if (dashex) fprint(2, (a != NULL && a->n != NULL) ? "~ (%L) %L\n" : "~ %L %L\n", a, " ", b, " "); set(lmatch(a, b)); break; } case nSwitch: { List *v = glom(n->u[0].p); while (1) { do { n = n->u[1].p; if (n == NULL) return istrue(); } while (n->u[0].p == NULL || n->u[0].p->type != nCase); if (lmatch(v, glom(n->u[0].p->u[0].p))) { for (n = n->u[1].p; n != NULL && (n->u[0].p == NULL || n->u[0].p->type != nCase); n = n->u[1].p) walk(n->u[0].p, TRUE); break; } } break; } case nPre: { List *v; if (n->u[0].p->type == nRedir || n->u[0].p->type == nDup) { if (redirq == NULL && !dofork(parent)) /* subshell on first preredir */ break; setsigdefaults(FALSE); qredir(n->u[0].p); if (!haspreredir(n->u[1].p)) doredirs(); /* no more preredirs, empty queue */ walk(n->u[1].p, FALSE); rc_exit(getstatus()); /* NOTREACHED */ } else if (n->u[0].p->type == nAssign) { if (isallpre(n->u[1].p)) { walk(n->u[0].p, TRUE); WALK(n->u[1].p, parent); } else { Estack e; Edata var; v = glom(n->u[0].p->u[0].p); assign(v, glob(glom(n->u[0].p->u[1].p)), TRUE); var.name = v->w; except(eVarstack, var, &e); walk(n->u[1].p, parent); varrm(v->w, TRUE); unexcept(); /* eVarstack */ } } else panic("unexpected node in preredir section of walk"); break; } case nBrace: if (n->u[1].p == NULL) { WALK(n->u[0].p, parent); } else if (dofork(parent)) { setsigdefaults(FALSE); walk(n->u[1].p, TRUE); /* Do redirections */ redirq = NULL; /* Reset redirection queue */ walk(n->u[0].p, FALSE); /* Do commands */ rc_exit(getstatus()); /* NOTREACHED */ } break; case nEpilog: qredir(n->u[0].p); if (n->u[1].p != NULL) { WALK(n->u[1].p, parent); /* Do more redirections. */ } else { doredirs(); /* Okay, we hit the bottom. */ } break; case nNmpipe: rc_error("named pipes cannot be executed as commands"); /* NOTREACHED */ default: panic("unknown node in walk"); /* NOTREACHED */ } return istrue(); }