static int load_file (const char *name) { lua_pushliteral(L, "require"); lua_rawget(L, LUA_GLOBALSINDEX); if (!lua_isfunction(L, -1)) { /* no `require' defined? */ lua_pop(L, 1); return file_input(name); } else { lua_pushstring(L, name); return report(lcall(1, 1)); } }
static void manual_input (void) { int status; const char *oldprogname = progname; progname = NULL; while ((status = load_string()) != -1) { if (status == 0) status = lcall(0, 0); report(status); if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ lua_getglobal(L, "print"); lua_insert(L, 1); if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) l_message(progname, lua_pushfstring(L, "error calling `print' (%s)", lua_tostring(L, -1))); } } lua_settop(L, 0); /* clear stack */ fputs("\n", stdout); progname = oldprogname; }
static int docall (int status) { if (status == 0) status = lcall(0, 1); return report(status); }
int main(int argc, char **argv) { int j; struct inserts *psave; int c; int initsize; char *cmdname, **initlist; char *arg; char *next; /* initialization */ blank = wctype("blank"); n_inserts = 0; psave = saveargv; (void) setlocale(LC_ALL, ""); #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ #endif (void) textdomain(TEXT_DOMAIN); if (init_yes() < 0) { ermsg(_(ERR_MSG_INIT_YES), strerror(errno)); exit(1); } parseargs(argc, argv); /* handling all of xargs arguments: */ while ((c = getopt(mac, mav, "0tpe:E:I:i:L:l:n:s:x")) != EOF) { switch (c) { case '0': ZERO = TRUE; break; case 't': /* -t: turn trace mode on */ TRACE = TRUE; break; case 'p': /* -p: turn on prompt mode. */ if ((PROMPT = open("/dev/tty", O_RDONLY)) == -1) { PERR("can't read from tty for -p"); } else { TRACE = TRUE; } break; case 'e': /* * -e[eofstr]: set/disable end-of-file. * N.B. that an argument *isn't* required here; but * parseargs forced an argument if not was given. The * forced argument is the default... */ LEOF = optarg; /* can be empty */ break; case 'E': /* * -E eofstr: change end-of-file string. * eofstr *is* required here, but can be empty: */ LEOF = optarg; break; case 'I': /* -I replstr: Insert mode. replstr *is* required. */ INSERT = PER_LINE = LEGAL = TRUE; N_ARGS = 0; INSPAT = optarg; if (*optarg == '\0') { ermsg(_("Option requires an argument: -%c\n"), c); } break; case 'i': /* * -i [replstr]: insert mode, with *optional* replstr. * N.B. that an argument *isn't* required here; if * it's not given, then the string INSPAT_STR will * be assumed. * * Since getopts(3C) doesn't handle the case of an * optional variable argument at all, we have to * parse this by hand: */ INSERT = PER_LINE = LEGAL = TRUE; N_ARGS = 0; if ((optarg != NULL) && (*optarg != '\0')) { INSPAT = optarg; } else { /* * here, there is no next argument. so * we reset INSPAT to the INSPAT_STR. * we *have* to do this, as -i/I may have * been given previously, and XCU4 requires * that only "the last one specified takes * effect". */ INSPAT = INSPAT_STR; } break; case 'L': /* * -L number: # of times cmd is executed * number *is* required here: */ PER_LINE = TRUE; N_ARGS = 0; INSERT = FALSE; if ((PER_LINE = atoi(optarg)) <= 0) { ermsg(_("#lines must be positive int: %s\n"), optarg); } break; case 'l': /* * -l [number]: # of times cmd is executed * N.B. that an argument *isn't* required here; if * it's not given, then 1 is assumed. * * parseargs handles the optional arg processing. */ PER_LINE = LEGAL = TRUE; /* initialization */ N_ARGS = 0; INSERT = FALSE; if ((optarg != NULL) && (*optarg != '\0')) { if ((PER_LINE = atoi(optarg)) <= 0) PER_LINE = 1; } break; case 'n': /* -n number: # stdin args */ /* * -n number: # stdin args. * number *is* required here: */ if ((N_ARGS = atoi(optarg)) <= 0) { ermsg(_("#args must be positive int: %s\n"), optarg); } else { LEGAL = DASHX || N_ARGS == 1; INSERT = PER_LINE = FALSE; } break; case 's': /* -s size: set max size of each arg list */ BUFLIM = atoi(optarg); if (BUFLIM > BUFSIZE || BUFLIM <= 0) { ermsg(_("0 < max-cmd-line-size <= %d: %s\n"), BUFSIZE, optarg); } break; case 'x': /* -x: terminate if args > size limit */ DASHX = LEGAL = TRUE; break; default: /* * bad argument. complain and get ready to die. */ usage(); exit(2); break; } } /* * if anything called ermsg(), something screwed up, so * we exit early. */ if (OK == FALSE) { usage(); exit(2); } /* * we're finished handling xargs's options, so now pick up * the command name (if any), and it's options. */ mac -= optind; /* dec arg count by what we've processed */ mav += optind; /* inc to current mav */ if (mac <= 0) { /* if there're no more args to process, */ cmdname = "/usr/bin/echo"; /* our default command */ *ARGV++ = addarg(cmdname); /* use the default cmd. */ } else { /* otherwise keep parsing rest of the string. */ /* * note that we can't use getopts(3C), and *must* parse * this by hand, as we don't know apriori what options the * command will take. */ cmdname = *mav; /* get the command name */ /* pick up the remaining args from the command line: */ while ((OK == TRUE) && (mac-- > 0)) { /* * while we haven't crapped out, and there's * work to do: */ if (INSERT && ! ERR) { if (strstr(*mav, INSPAT) != NULL) { if (++n_inserts > MAXINSERTS) { ermsg(_("too many args " "with %s\n"), INSPAT); ERR = TRUE; } psave->p_ARGV = ARGV; (psave++)->p_skel = *mav; } } *ARGV++ = addarg(*mav++); } } /* pick up args from standard input */ initlist = ARGV; initsize = linesize; lastarg[0] = '\0'; while (OK) { N_args = 0; N_lines = 0; ARGV = initlist; linesize = initsize; next = argbuf; while (MORE || (lastarg[0] != '\0')) { int l; if (*lastarg != '\0') { arg = strcpy(next, lastarg); *lastarg = '\0'; } else if ((arg = getarg(next)) == NULL) { break; } l = strlen(arg) + 1; linesize += l; next += l; /* Inserts are handled specially later. */ if ((n_inserts == 0) && (linesize >= BUFLIM)) { /* * Legal indicates hard fail if the list is * truncated due to size. So fail, or if we * cannot create any list because it would be * too big. */ if (LEGAL || N_args == 0) { EMSG(LIST2LONG); exit(2); /* NOTREACHED */ } /* * Otherwise just save argument for later. */ (void) strcpy(lastarg, arg); break; } *ARGV++ = arg; N_args++; if ((PER_LINE && N_lines >= PER_LINE) || (N_ARGS && (N_args) >= N_ARGS)) { break; } if ((ARGV - arglist) == MAXARGS) { break; } } *ARGV = NULL; if (N_args == 0) { /* Reached the end with no more work. */ exit(exitstat); } /* insert arg if requested */ if (!ERR && INSERT) { p_ibuf = ins_buf; ARGV--; j = ibufsize = 0; for (psave = saveargv; ++j <= n_inserts; ++psave) { addibuf(psave); if (ERR) break; } } *ARGV = NULL; if (n_inserts > 0) { /* * if we've done any insertions, re-calculate the * linesize. bomb out if we've exceeded our length. */ linesize = 0; for (ARGV = arglist; *ARGV != NULL; ARGV++) { linesize += strlen(*ARGV) + 1; } if (linesize >= BUFLIM) { EMSG(LIST2LONG); exit(2); /* NOTREACHED */ } } /* exec command */ if (!ERR) { if (!MORE && (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0)) exit(exitstat); OK = TRUE; j = TRACE ? echoargs() : TRUE; if (j) { /* * for xcu4, all invocations of cmdname must * return 0, in order for us to return 0. * so if we have a non-zero status here, * quit immediately. */ exitstat |= lcall(cmdname, arglist); } } } if (OK) return (exitstat); /* * if exitstat was set, to match XCU4 complience, * return that value, otherwise, return 1. */ return (exitstat ? exitstat : 1); }