int init(int argc, char *argv[]) { mtn = mtn_init(MODULE_NAME); ctx = calloc(1,sizeof(CTX)); if(!mtn){ return(-1); } if(!ctx){ mtnlogger(NULL, 0, "[error] %s: %s\n", __func__, strerror(errno)); return(-1); } mtn->mps_max = 512; mtn->logtype = 0; mtn->logmode = MTNLOG_STDERR; ctx->afd = -1; ctx->nobuf = 1; ctx->text = 1; ctx->delim = newstr(" "); ctx->targets = newarg(0); ctx->arg_max = sysconf(_SC_ARG_MAX); ctx->cpu_num = sysconf(_SC_NPROCESSORS_ONLN); ctx->job_max = sysconf(_SC_NPROCESSORS_ONLN); ctx->cmdargs = parse(argc, argv); gettimeofday(&(ctx->polltv), NULL); if(init_pipe() == -1){ return(-1); } if(init_poll() == -1){ return(-1); } ctx->job = calloc(ctx->job_max, sizeof(MTNJOB)); set_signal_handler(); mtnlogger(mtn, 1, "LogLevel: %d\n", mtn->loglevel); return(0); }
ARG cpconvarg(ARG a, MTNJOB *j) { int i; ARG r = newarg(0); if(!a){ return(r); } for(i=0;a[i];i++){ r = addarg(r, a[i]); r[i] = convarg(r[i], j); } return(r); }
ARG linearg() { int i; ARG argl = newarg(ctx->arg_num); for(i=0;i<ctx->arg_num;i++){ argl[i] = sftarg(ctx->linearg); if(!argl[i]){ clrarg(argl); return(NULL); } } return(argl); }
Expression linearfunexprgd(const char *n, const char *type, const Expression &arg) { if (arg.kindIs("constant") && arg.value == 0) return Expression(0); // Nice optimization! :) Expression newarg(arg); Expression prefactor = newarg.ScalarFactor(); Expression out = funexpr(n, newarg); out.kind = "linear function"; out.name = std::string(n) + "(gd, "; out.unlazy = true; out.type = type; out.SetDepth(); return prefactor*out; }
endcall() { char *pt, *p; rescan = c!=semi; newarg(); pushget(); pt = &pf->pa0; ge = move(pt,&gf->ga0,pe-pt); if(ge>gmax) diag("Call stack overflow"); if(traceflag) dotrace(); p = finddef(0); setscan(p); popput(); lp--; function(p->dswitch); }
ARG readarg(int f, int n) { int i; ARG arg; if(f == -1){ return(NULL); } arg = newarg(0); for(i=0;i<n;i++){ if(!(arg = readargline(f, arg))){ break; } } return(arg); }
control() { while(1) { get(); if(c==0 && lg>0) { popget(); trash(); rescan = 1; } else { if(!rescan) put(); else if(lq>0) { if(c==lquote) { lq++; put(); } else if(c==rquote || c==0) { lq--; if(lq>0 || l9>0) put (rquote); } else put(); } else if(l9>0) if(c==colon||c==0||c==semi) if(--l9<=0) endcall(); else put(); else { if(c==sharp) l9++; else if(c==lquote) lq++; put(); } else { if(c==sharp) begincall(); else if(c==lquote) lq++; else if(lp>0) { if(c==colon||c==0||c==semi) endcall(); else if(c==comma) newarg(); else put(); } else if(c==0) return; /* lg=lp=lq=0 */ else put(); } } } }
ARG cmdargs(MTNJOB *job) { int i; ARG cmd = newarg(0); if(job->args){ for(i=0;job->args[i];i++){ if(job->conv){ cmd = addarg(cmd, convarg(newstr(job->args[i]), job)); }else{ cmd = addarg(cmd, newstr(job->args[i])); } } } if(!job->conv && job->argl){ for(i=0;job->argl[i];i++){ cmd = addarg(cmd, job->argl[i]); } } return(cmd); }
ARG stdname(MTNJOB *job) { ARG std = newarg(3); if(ctx->stdin){ std[0] = convarg(newstr(ctx->stdin), job); }else{ std[0] = newstr("/dev/null"); } if(ctx->stdout){ std[1] = convarg(newstr(ctx->stdout), job); }else{ std[1] = newstr(""); } if(ctx->stderr){ std[2] = convarg(newstr(ctx->stderr), job); }else{ std[2] = newstr(""); } std[3] = NULL; return(std); }
void listdiranalyse(t_ls *ls, t_arg *arg, struct dirent *ent) { struct stat filestat; t_ent *ment; char *lpath; char *lpath2; lpath = ft_strjoin("/", ent->d_name); lpath2 = ft_strjoin(arg->path, lpath); if (lstat(lpath2, &filestat) == 0) { ment = newent(ent->d_name, &filestat); arg->ent = addent(&(arg->ent), ment); if (!ft_strequ(ent->d_name, ".") && !ft_strequ(ent->d_name, "..")) { if ((ent->d_type == 4 || ent->d_type == 0) && ls->opts['R']) { arg->sub = addarg(&arg->sub, newarg(lpath2)); } } } ft_strdel(&lpath2); ft_strdel(&lpath); }
void functioncleanup() { struct func *fp; struct arg *ap; for (fp = fhead; fp != NULL; fp = fp->next) { /* * Set the 'have' flags so we don't bother * trying to auto-create basic functions. */ switch (fp->type) { case DBOW_INSERT: fp->table->haveinsert = 1; break; case DBOW_DELETE: fp->table->havedelete = 1; break; case DBOW_SEARCH: fp->table->havesearch = 1; break; case DBOW_UPDATE: fp->table->haveupdate = 1; break; } } /* * Now check the tables and make sure we have sufficient * default functions for most activities. */ checktables(); /* * Next, add pseudo-arguments for those functions which * need them. */ for (fp = fhead; fp != NULL; fp = fp->next) { switch (fp->type) { case DBOW_INSERT: ap = newarg(fp->table, NULL); ap->handle = strdup("p"); ap->flags = ARGF_POINTER; ap->next = fp->args; fp->args = ap; break; case DBOW_UPDATE: ap = newarg(fp->table, NULL); ap->handle = strdup("p"); ap->flags = ARGF_POINTER; ap->next = fp->args; fp->args = ap; break; } } /* * OK, now it's time to run through all our functions and * perform any optimizations. */ for (fp = fhead; fp != NULL; fp = fp->next) { optimizefunc(fp); dumpfunction(fp); } }
ARG parse(int argc, char *argv[]) { int r; char *p; STR buff; ARG args; if(argc < 2){ usage(); exit(0); } optind = 0; while((r = getopt_long(argc, argv, "+hVvnBbig:j:e:I:RALT:P:N:m:p:d:f:D:", get_optlist(), NULL)) != -1){ switch(r){ case 'h': usage(); exit(0); case 'V': version(); exit(0); case 'F': ctx->info = 1; break; case 'f': ctx->arg_num = ctx->arg_num ? ctx->arg_num : 1; if(!strcmp("-", optarg)){ ctx->afd = 0; }else{ ctx->afd = open(optarg, O_RDONLY); if(ctx->afd == -1){ mtnlogger(mtn, 0, "[error] %s: %s %s\n", __func__, optarg, strerror(errno)); exit(1); } } break; case 't': test(); exit(0); case 'v': ctx->verbose = 1; break; case 'n': ctx->dryrun = 1; break; case 'i': ctx->conv = 1; break; case 'g': mtn->groupstr = newstr(optarg); mtn->grouparg = splitstr(optarg, ","); break; case 'j': ctx->cpu_lim = atoi(optarg) * 10; break; case 'e': ctx->echo = newstr(optarg); setbuf(stdout, NULL); break; case 'I': ctx->stdin = newstr(optarg); break; case 'O': ctx->stdout = newstr(optarg); break; case 'E': ctx->stderr = newstr(optarg); break; case 'S': ctx->putarg = splitstr(optarg, ","); break; case 'G': ctx->getarg = splitstr(optarg, ","); break; case 'B': ctx->text = 0; break; case 'b': ctx->nobuf = 0; break; case 'R': ctx->opt_R = 1; break; case 'A': ctx->opt_A = 1; break; case 'L': ctx->opt_L = 1; break; case 'P': ctx->job_max = atoi(optarg); break; case 'N': ctx->arg_num = atoi(optarg); break; case 'm': strcpy(mtn->mcast_addr, optarg); break; case 'p': mtn->mcast_port = atoi(optarg); break; case 'd': ctx->delim = modstr(ctx->delim, optarg); break; case 'D': mtn->loglevel = atoi(optarg); break; case 'T': buff = newstr(optarg); p = strtok(buff, ","); while(p){ ctx->targets = addarg(ctx->targets, p); p = strtok(NULL, ","); } break; case '?': usage(); exit(1); } } if(ctx->opt_R && ctx->opt_A){ usage(); exit(1); } if(ctx->opt_R && ctx->targets[0]){ usage(); exit(1); } if(ctx->opt_A && ctx->targets[0]){ usage(); exit(1); } if(ctx->opt_R){ ctx->mode = ctx->opt_L ? MTNEXECMODE_HYBRID : MTNEXECMODE_REMOTE; }else if(ctx->opt_A){ ctx->mode = ctx->opt_L ? MTNEXECMODE_ALL1 : MTNEXECMODE_ALL0; }else{ ctx->mode = ctx->targets[0] ? MTNEXECMODE_TARGET : MTNEXECMODE_LOCAL; } if(ctx->info){ info(); exit(0); } ctx->verbose = ctx->dryrun ? 0 : ctx->verbose; args = newarg(0); while(optind < argc){ if(strcmp(argv[optind], ":::") == 0){ optind++; ctx->linearg = newarg(0); break; } args = addarg(args, argv[optind++]); } if(ctx->linearg){ while(optind < argc){ ctx->linearg = addarg(ctx->linearg, argv[optind++]); } ctx->arg_num = ctx->arg_num ? ctx->arg_num : 1; } if((args[0] == NULL) && (ctx->arg_num == 0)){ ctx->arg_num = 1; } return(args); }