/* function redirect(location: String, level: Number = null): Void */ static EjsFile *lf_redirect(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv) { cchar *logSpec; int level; logSpec = ejsToMulti(ejs, argv[0]); level = (argc >= 2) ? ejsGetInt(ejs, argv[1]) : -1; mprStartLogging(logSpec, 0); if (level >= 0) { mprSetLogLevel(level); } return 0; }
MAIN(ejsMain, int argc, char **argv, char **envp) { Mpr *mpr; EcCompiler *cp; Ejs *ejs; cchar *cmd, *className, *method, *homeDir; char *argp, *searchPath, *modules, *name, *tok, *extraFiles; int nextArg, err, ecFlags, stats, merge, bind, noout, debug, optimizeLevel, warnLevel, strict, i, next; /* Initialize Multithreaded Portable Runtime (MPR) */ mpr = mprCreate(argc, argv, 0); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddStandardSignals(); if (mprStart(mpr) < 0) { mprError("Cannot start mpr services"); return EJS_ERR; } err = 0; className = 0; cmd = 0; method = 0; searchPath = 0; stats = 0; merge = 0; bind = 1; noout = 1; debug = 1; warnLevel = 1; optimizeLevel = 9; strict = 0; app->files = mprCreateList(-1, 0); app->iterations = 1; argc = mpr->argc; argv = (char**) mpr->argv; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (smatch(argp, "--bind")) { bind = 1; } else if (smatch(argp, "--class")) { if (nextArg >= argc) { err++; } else { className = argv[++nextArg]; } } else if (smatch(argp, "--chdir") || smatch(argp, "--home") || smatch(argp, "-C")) { if (nextArg >= argc) { err++; } else { homeDir = argv[++nextArg]; if (chdir((char*) homeDir) < 0) { mprError("Cannot change directory to %s", homeDir); } } #if BIT_UNIX_LIKE } else if (smatch(argp, "--chroot")) { /* Not documented or supported */ if (nextArg >= argc) { err++; } else { homeDir = mprGetAbsPath(argv[++nextArg]); if (chroot(homeDir) < 0) { if (errno == EPERM) { mprPrintfError("%s: Must be super user to use the --chroot option", mprGetAppName(mpr)); } else { mprPrintfError("%s: Cannot change change root directory to %s, errno %d", mprGetAppName(), homeDir, errno); } return 4; } } #endif } else if (smatch(argp, "--cmd") || smatch(argp, "-c")) { if (nextArg >= argc) { err++; } else { cmd = argv[++nextArg]; } #if BIT_WIN_LIKE } else if (smatch(argp, "--cygroot")) { if (nextArg >= argc) { err++; } else { app->cygroot = sclone(argv[++nextArg]); } #endif } else if (smatch(argp, "--debug")) { debug = 1; } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--files") || smatch(argp, "-f")) { /* Compatibility with mozilla shell */ if (nextArg >= argc) { err++; } else { extraFiles = sclone(argv[++nextArg]); name = stok(extraFiles, " \t", &tok); while (name != NULL) { mprAddItem(app->files, sclone(name)); name = stok(NULL, " \t", &tok); } } } else if (smatch(argp, "--iterations") || smatch(argp, "-i")) { if (nextArg >= argc) { err++; } else { app->iterations = atoi(argv[++nextArg]); } } else if (smatch(argp, "--log")) { if (nextArg >= argc) { err++; } else { mprStartLogging(argv[++nextArg], 0); mprSetCmdlineLogging(1); } } else if (smatch(argp, "--method")) { if (nextArg >= argc) { err++; } else { method = argv[++nextArg]; } } else if (smatch(argp, "--name")) { /* Just ignore. Used to tag commands with a unique command line */ nextArg++; } else if (smatch(argp, "--nobind")) { bind = 0; } else if (smatch(argp, "--nodebug")) { debug = 0; } else if (smatch(argp, "--optimize")) { if (nextArg >= argc) { err++; } else { optimizeLevel = atoi(argv[++nextArg]); } } else if (smatch(argp, "-s")) { /* Compatibility with mozilla shell. Just ignore */ } else if (smatch(argp, "--search") || smatch(argp, "--searchpath")) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (smatch(argp, "--standard")) { strict = 0; } else if (smatch(argp, "--stats")) { stats = 1; } else if (smatch(argp, "--strict")) { strict = 1; } else if (smatch(argp, "--require")) { if (nextArg >= argc) { err++; } else { if (app->modules == 0) { app->modules = mprCreateList(-1, 0); } modules = sclone(argv[++nextArg]); name = stok(modules, " \t", &tok); while (name != NULL) { require(name); name = stok(NULL, " \t", &tok); } } } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { mprStartLogging("stderr:2", 0); mprSetCmdlineLogging(1); } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s-%s\n", BIT_VERSION, BIT_BUILD_NUMBER); return 0; } else if (smatch(argp, "--warn")) { if (nextArg >= argc) { err++; } else { warnLevel = atoi(argv[++nextArg]); } } else { err++; break; } } if (err) { /* If --method or --class is specified, then the named class.method will be run (method defaults to "main", class defaults to first class with a "main"). Examples: ejs ejs script.es arg1 arg2 arg3 ejs --class "Customer" --method "start" --files "script1.es script2.es" main.es */ mprPrintfError("Usage: %s [options] script.es [arguments] ...\n" " Ejscript shell program options:\n" " --class className # Name of class containing method to run\n" " --cmd ejscriptCode # Literal ejscript statements to execute\n" " --cygroot path # Set cygwin root for resolving script paths\n" " --debug # Use symbolic debugging information (default)\n" " --debugger # Disable timeouts to make using a debugger easier\n" " --files \"files..\" # Extra source to compile\n" " --log logSpec # Internal compiler diagnostics logging\n" " --method methodName # Name of method to run. Defaults to main\n" " --nodebug # Omit symbolic debugging information\n" " --optimize level # Set the optimization level (0-9 default is 9)\n" " --require 'module,...' # Required list of modules to pre-load\n" " --search ejsPath # Module search path\n" " --standard # Default compilation mode to standard (default)\n" " --stats # Print memory stats on exit\n" " --strict # Default compilation mode to strict\n" " --verbose | -v # Same as --log stderr:2 \n" " --version # Emit the compiler version information\n" " --warn level # Set the warning message level (0-9 default is 0)\n\n", mpr->name); return -1; } if ((ejs = ejsCreateVM(argc - nextArg, (cchar **) &argv[nextArg], 0)) == 0) { return MPR_ERR_MEMORY; } app->ejs = ejs; if (ejsLoadModules(ejs, searchPath, app->modules) < 0) { return MPR_ERR_CANT_READ; } ecFlags = 0; ecFlags |= (merge) ? EC_FLAGS_MERGE: 0; ecFlags |= (bind) ? EC_FLAGS_BIND: 0; ecFlags |= (noout) ? EC_FLAGS_NO_OUT: 0; ecFlags |= (debug) ? EC_FLAGS_DEBUG: 0; cp = app->compiler = ecCreateCompiler(ejs, ecFlags); if (cp == 0) { return MPR_ERR_MEMORY; } ecSetRequire(cp, app->modules); ecSetOptimizeLevel(cp, optimizeLevel); ecSetWarnLevel(cp, warnLevel); ecSetStrictMode(cp, strict); if (nextArg < argc) { mprAddItem(app->files, sclone(argv[nextArg])); } if (app->cygroot) { /* When cygwin invokes a script with shebang, it passes a cygwin path to the script The ejs --cygroot option permits ejscript to conver this to a native path */ for (next = 0; (name = mprGetNextItem(app->files, &next)) != 0; ) { if (*name == '/' || *name == '\\') { mprSetItem(app->files, next - 1, sjoin(app->cygroot, name, NULL)); } } } for (i = 0; !err && i < app->iterations; i++) { if (cmd) { if (interpretCommands(cp, cmd) < 0) { err++; } } else if (mprGetListLength(app->files) > 0) { if (interpretFiles(cp, app->files, argc - nextArg, &argv[nextArg], className, method) < 0) { err++; } } else { /* No args - run as an interactive shell */ if (interpretCommands(cp, NULL) < 0) { err++; } } } if (stats) { #if BIT_DEBUG mprSetLogLevel(1); mprPrintMem("Memory Usage", 1); #endif } if (!err) { err = mpr->exitStatus; } app->ejs = 0; mprTerminate(MPR_EXIT_DEFAULT, err); ejsDestroyVM(ejs); mprDestroy(MPR_EXIT_DEFAULT); return err; }
MAIN(appweb, int argc, char **argv, char **envp) { Mpr *mpr; cchar *argp, *jail; char *logSpec, *traceSpec; int argind; jail = 0; logSpec = 0; traceSpec = 0; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == NULL) { exit(1); } if ((app = mprAllocObj(AppwebApp, manageApp)) == NULL) { exit(2); } mprAddRoot(app); mprAddStandardSignals(); #if ME_ROM extern MprRomInode romFiles[]; mprSetRomFileSystem(romFiles); #endif if (httpCreate(HTTP_CLIENT_SIDE | HTTP_SERVER_SIDE) == 0) { exit(3); } app->mpr = mpr; app->workers = -1; app->home = sclone(ME_SERVER_ROOT); app->documents = app->home; argc = mpr->argc; argv = (char**) mpr->argv; for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (smatch(argp, "--config") || smatch(argp, "--conf")) { if (argind >= argc) { usageError(); } app->configFile = sclone(argv[++argind]); #if ME_UNIX_LIKE } else if (smatch(argp, "--chroot")) { if (argind >= argc) { usageError(); } jail = mprGetAbsPath(argv[++argind]); #endif } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--exe")) { if (argind >= argc) { usageError(); } mpr->argv[0] = mprGetAbsPath(argv[++argind]); mprSetAppPath(mpr->argv[0]); mprSetModuleSearchPath(NULL); } else if (smatch(argp, "--home")) { if (argind >= argc) { usageError(); } app->home = sclone(argv[++argind]); if (chdir(app->home) < 0) { mprLog("error appweb", 0, "Cannot change directory to %s", app->home); exit(4); } } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) { usageError(); } logSpec = argv[++argind]; } else if (smatch(argp, "--name") || smatch(argp, "-n")) { if (argind >= argc) { usageError(); } mprSetAppName(argv[++argind], 0, 0); } else if (smatch(argp, "--threads")) { if (argind >= argc) { usageError(); } app->workers = atoi(argv[++argind]); } else if (smatch(argp, "--show") || smatch(argp, "-s")) { app->show = 1; } else if (smatch(argp, "--trace") || smatch(argp, "-t")) { if (argind >= argc) { usageError(); } traceSpec = argv[++argind]; } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { if (!logSpec) { logSpec = sfmt("stderr:2"); } if (!traceSpec) { traceSpec = sfmt("stderr:2"); } } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s\n", ME_VERSION); exit(0); } else if (*argp == '-' && isdigit((uchar) argp[1])) { if (!logSpec) { logSpec = sfmt("stderr:%d", (int) stoi(&argp[1])); } if (!traceSpec) { traceSpec = sfmt("stderr:%d", (int) stoi(&argp[1])); } } else if (smatch(argp, "-?") || scontains(argp, "-help")) { usageError(); exit(5); } else if (*argp == '-') { mprLog("error appweb", 0, "Unknown switch \"%s\"", argp); usageError(); exit(5); } } app->home = mprGetAbsPath(app->home); if (logSpec) { mprStartLogging(logSpec, MPR_LOG_DETAILED | MPR_LOG_CONFIG | MPR_LOG_CMDLINE); } if (traceSpec) { httpStartTracing(traceSpec); } if (mprStart() < 0) { mprLog("error appweb", 0, "Cannot start MPR"); mprDestroy(); return MPR_ERR_CANT_INITIALIZE; } if (checkEnvironment(argv[0]) < 0) { exit(6); } if (argc == argind && !app->configFile) { if (findAppwebConf() < 0) { exit(7); } } if (jail && changeRoot(jail) < 0) { exit(8); } if (createEndpoints(argc - argind, &argv[argind]) < 0) { return MPR_ERR_CANT_INITIALIZE; } if (maStartAppweb(app->appweb) < 0) { mprLog("error appweb", 0, "Cannot start HTTP service, exiting."); exit(9); } if (app->show) { httpLogRoutes(0, 0); } mprServiceEvents(-1, 0); mprLog("info appweb", 1, "Stopping Appweb ..."); mprDestroy(); return mprGetExitStatus(); }
int APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *args, int junk2) { char **argv, *argp; int argc, err, nextArg; mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddTerminator(terminating); err = 0; app->appInst = inst; app->heartBeatPeriod = HEART_BEAT_PERIOD; setAppDefaults(); mprSetAppName(BLD_PRODUCT "Manager", BLD_NAME "Manager", BLD_VERSION); app->appName = mprGetAppName(); app->appTitle = mprGetAppTitle(mpr); mprSetLogHandler(logHandler); mprSetWinMsgCallback((MprMsgCallback) msgProc); if ((argc = mprMakeArgv(args, &argv, MPR_ARGV_ARGS_ONLY)) < 0) { return FALSE; } for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-' || strcmp(argp, "--") == 0) { break; } if (strcmp(argp, "--args") == 0) { /* Args to pass to service when it starts */ if (nextArg >= argc) { err++; } else { app->serviceArgs = argv[++nextArg]; } } else if (strcmp(argp, "--console") == 0) { /* Allow the service to interact with the console */ app->createConsole++; } else if (strcmp(argp, "--daemon") == 0) { /* Ignored on windows */ } else if (strcmp(argp, "--heartBeat") == 0) { /* Set the heart beat period */ if (nextArg >= argc) { err++; } else { app->heartBeatPeriod = atoi(argv[++nextArg]) * 1000; } } else if (strcmp(argp, "--home") == 0) { /* Change to this directory before starting the service */ if (nextArg >= argc) { err++; } else { app->serviceHome = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--log") == 0) { /* Pass the log directive through to the service */ if (nextArg >= argc) { err++; } else { app->logSpec = sclone(argv[++nextArg]); mprStartLogging(app->logSpec, 0); mprSetCmdlineLogging(1); } } else if (strcmp(argp, "--name") == 0) { if (nextArg >= argc) { err++; } else { app->serviceName = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--program") == 0) { if (nextArg >= argc) { err++; } else { app->serviceProgram = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--verbose") == 0 || strcmp(argp, "-v") == 0) { mprSetLogLevel(1); } else { err++; } if (err) { mprUserError("Bad command line: %s\n" " Usage: %s [options] [program args]\n" " Switches:\n" " --args # Args to pass to service\n" " --console # Display the service console\n" " --heartBeat interval # Heart beat interval period (secs)\n" " --home path # Home directory for service\n" " --log logFile:level # Log directive for service\n" " --name name # Name of the service to manage\n" " --program path # Service program to start\n" " --verbose # Show command feedback\n" " Commands:\n" " disable # Disable the service\n" " enable # Enable the service\n" " start # Start the service\n" " stop # Start the service\n" " run # Run and watch over the service\n", args, app->appName); return -1; } } if (mprStart() < 0) { mprUserError("Can't start MPR for %s", mprGetAppName()); } else { mprStartEventsThread(); if (nextArg >= argc) { return process("run"); } else for (; nextArg < argc; nextArg++) { if (!process(argv[nextArg])) { return FALSE; } } } return TRUE; }
int main(int argc, char *argv[]) { char *argp, *value; int err, nextArg, status; err = 0; mprCreate(argc, argv, MPR_USER_EVENTS_THREAD); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddTerminator(terminating); mprAddStandardSignals(); setAppDefaults(); for (nextArg = 1; nextArg < argc && !err; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--args") == 0) { /* Args to pass to service */ if (nextArg >= argc) { err++; } else { app->serviceArgs = argv[++nextArg]; } } else if (strcmp(argp, "--console") == 0) { /* Does nothing. Here for compatibility with windows watcher */ } else if (strcmp(argp, "--daemon") == 0) { app->runAsDaemon++; #if FUTURE } else if (strcmp(argp, "--heartBeat") == 0) { /* Set the frequency to check on the program. */ if (nextArg >= argc) { err++; } else { app->heartBeatPeriod = atoi(argv[++nextArg]); } #endif } else if (strcmp(argp, "--home") == 0) { /* Change to this directory before starting the service */ if (nextArg >= argc) { err++; } else { app->serviceHome = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--log") == 0) { /* Pass the log directive through to the service */ if (nextArg >= argc) { err++; } else { app->logSpec = sclone(argv[++nextArg]); mprStartLogging(app->logSpec, 0); mprSetCmdlineLogging(1); } } else if (strcmp(argp, "--name") == 0) { if (nextArg >= argc) { err++; } else { app->serviceName = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--pidfile") == 0) { if (nextArg >= argc) { err++; } else { app->pidPath = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--program") == 0) { if (nextArg >= argc) { err++; } else { app->serviceProgram = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--retries") == 0) { if (nextArg >= argc) { err++; } else { app->retries = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--signal") == 0) { if (nextArg >= argc) { err++; } else { value = argv[++nextArg]; if (smatch(value, "SIGABRT")) { app->signal = SIGABRT; } else if (smatch(value, "SIGINT")) { app->signal = SIGINT; } else if (smatch(value, "SIGHUP")) { app->signal = SIGHUP; } else if (smatch(value, "SIGQUIT")) { app->signal = SIGQUIT; } else if (smatch(value, "SIGTERM")) { app->signal = SIGTERM; } else if (smatch(value, "SIGUSR1")) { app->signal = SIGUSR1; } else if (smatch(value, "SIGUSR2")) { app->signal = SIGUSR2; } else { app->signal = atoi(argv[++nextArg]); } } } else if (strcmp(argp, "--verbose") == 0 || strcmp(argp, "-v") == 0) { mprSetLogLevel(1); } else { err++; break; } } if (nextArg >= argc) { err++; } if (err) { mprUserError("Bad command line: \n" " Usage: %s [commands]\n" " Switches:\n" " --args # Args to pass to service\n" " --daemon # Run manager as a daemon\n" " --home path # Home directory for service\n" " --log logFile:level # Log directive for service\n" " --retries count # Max count of app restarts\n" " --name name # Name of the service to manage\n" " --pidfile path # Location of the pid file\n" " --program path # Service program to start\n" " --signal signo # Signal number to terminate service\n" " --verbose # Show command feedback\n" #if FUTURE " --heartBeat interval # Heart beat interval period (secs) \n" #endif " Commands:\n" " disable # Disable the service\n" " enable # Enable the service\n" " install # Install the service\n" " run # Run and watch over the service\n" " start # Start the service\n" " stop # Start the service\n" " uninstall # Uninstall the service\n" , app->appName); return -1; } if (!app->pidPath) { app->pidPath = sjoin(app->pidDir, "/", app->serviceName, ".pid", NULL); } if (app->runAsDaemon) { makeDaemon(); } status = 0; if (getuid() != 0) { mprUserError("Must run with administrator privilege. Use sudo."); status = 1; } else if (mprStart() < 0) { mprUserError("Can't start MPR for %s", mprGetAppName()); status = 2; } else { mprStartEventsThread(); for (; nextArg < argc; nextArg++) { if (!process(argv[nextArg], 0)) { status = 3; break; } } } mprDestroy(MPR_EXIT_DEFAULT); return status; }
static bool parseArgs(int argc, char **argv) { char *argp, *key, *value; int i, setWorkers, nextArg; setWorkers = 0; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (smatch(argp, "--benchmark") || smatch(argp, "-b")) { app->benchmark++; } else if (smatch(argp, "--cert")) { if (nextArg >= argc) { return 0; } else { app->cert = sclone(argv[++nextArg]); } } else if (smatch(argp, "--chunk")) { if (nextArg >= argc) { return 0; } else { value = argv[++nextArg]; app->chunkSize = atoi(value); if (app->chunkSize < 0) { mprError("Bad chunksize %d", app->chunkSize); return 0; } } } else if (smatch(argp, "--continue")) { app->continueOnErrors++; } else if (smatch(argp, "--cookie")) { if (nextArg >= argc) { return 0; } else { mprAddItem(app->headers, mprCreateKeyPair("Cookie", argv[++nextArg])); } } else if (smatch(argp, "--data")) { if (nextArg >= argc) { return 0; } else { if (app->bodyData == 0) { app->bodyData = mprCreateBuf(-1, -1); } mprPutStringToBuf(app->bodyData, argv[++nextArg]); } } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); app->retries = 0; app->timeout = MAXINT; } else if (smatch(argp, "--delete")) { app->method = "DELETE"; } else if (smatch(argp, "--form") || smatch(argp, "-f")) { if (nextArg >= argc) { return 0; } else { if (app->formData == 0) { app->formData = mprCreateList(-1, 0); } addFormVars(argv[++nextArg]); } } else if (smatch(argp, "--header")) { if (nextArg >= argc) { return 0; } else { key = argv[++nextArg]; if ((value = strchr(key, ':')) == 0) { mprError("Bad header format. Must be \"key: value\""); return 0; } *value++ = '\0'; while (isspace((uchar) *value)) { value++; } mprAddItem(app->headers, mprCreateKeyPair(key, value)); } } else if (smatch(argp, "--host")) { if (nextArg >= argc) { return 0; } else { app->host = argv[++nextArg]; if (*app->host == ':') { app->host = &app->host[1]; } if (isPort(app->host)) { app->host = sfmt("http://127.0.0.1:%s", app->host); } else { app->host = sclone(app->host); } } } else if (smatch(argp, "--insecure")) { app->insecure++; } else if (smatch(argp, "--iterations") || smatch(argp, "-i")) { if (nextArg >= argc) { return 0; } else { app->iterations = atoi(argv[++nextArg]); } } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (nextArg >= argc) { return 0; } else { mprStartLogging(argv[++nextArg], 0); } } else if (smatch(argp, "--method") || smatch(argp, "-m")) { if (nextArg >= argc) { return 0; } else { app->method = argv[++nextArg]; } } else if (smatch(argp, "--out") || smatch(argp, "-o")) { if (nextArg >= argc) { return 0; } else { app->outFilename = sclone(argv[++nextArg]); } } else if (smatch(argp, "--noout") || smatch(argp, "-n") || smatch(argp, "--quiet") || smatch(argp, "-q")) { app->noout++; } else if (smatch(argp, "--nofollow")) { app->nofollow++; } else if (smatch(argp, "--password") || smatch(argp, "-p")) { if (nextArg >= argc) { return 0; } else { app->password = sclone(argv[++nextArg]); } } else if (smatch(argp, "--post")) { app->method = "POST"; } else if (smatch(argp, "--printable")) { app->printable++; } else if (smatch(argp, "--protocol")) { if (nextArg >= argc) { return 0; } else { app->protocol = supper(argv[++nextArg]); } } else if (smatch(argp, "--put")) { app->method = "PUT"; } else if (smatch(argp, "--range")) { if (nextArg >= argc) { return 0; } else { // TODO - should allow multiple ranges if (app->ranges == 0) { app->ranges = sfmt("bytes=%s", argv[++nextArg]); } else { app->ranges = srejoin(app->ranges, ",", argv[++nextArg], NULL); } } } else if (smatch(argp, "--retries") || smatch(argp, "-r")) { if (nextArg >= argc) { return 0; } else { app->retries = atoi(argv[++nextArg]); } } else if (smatch(argp, "--sequence")) { app->sequence++; } else if (smatch(argp, "--showHeaders") || smatch(argp, "--show") || smatch(argp, "-s")) { app->showHeaders++; } else if (smatch(argp, "--showStatus") || smatch(argp, "--showCode")) { app->showStatus++; } else if (smatch(argp, "--single") || smatch(argp, "-s")) { app->singleStep++; } else if (smatch(argp, "--text")) { app->text++; } else if (smatch(argp, "--threads") || smatch(argp, "-t")) { if (nextArg >= argc) { return 0; } else { app->loadThreads = atoi(argv[++nextArg]); } } else if (smatch(argp, "--timeout")) { if (nextArg >= argc) { return 0; } else { app->timeout = atoi(argv[++nextArg]) * MPR_TICKS_PER_SEC; } } else if (smatch(argp, "--upload") || smatch(argp, "-u")) { app->upload++; } else if (smatch(argp, "--user") || smatch(argp, "--username")) { if (nextArg >= argc) { return 0; } else { app->username = argv[++nextArg]; } } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { app->verbose++; } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintfError("%s %s\n" "Copyright (C) Embedthis Software 2003-2012\n" "Copyright (C) Michael O'Brien 2003-2012\n", BIT_NAME, BIT_VERSION); exit(0); } else if (smatch(argp, "--workerTheads") || smatch(argp, "-w")) { if (nextArg >= argc) { return 0; } else { app->workers = atoi(argv[++nextArg]); } setWorkers++; } else if (smatch(argp, "--zero")) { app->zeroOnErrors++; } else if (smatch(argp, "--")) { nextArg++; break; } else if (smatch(argp, "-")) { break; } else { return 0; break; } } if (argc == nextArg) { return 0; } app->nextArg = nextArg; argc = argc - nextArg; argv = &argv[nextArg]; app->target = argv[argc - 1]; app->iterations *= app->loadThreads; if (--argc > 0) { /* Files present on command line */ app->files = mprCreateList(argc, MPR_LIST_STATIC_VALUES); for (i = 0; i < argc; i++) { mprAddItem(app->files, argv[i]); } } if (!setWorkers) { app->workers = app->loadThreads + 2; } if (app->method == 0) { if (app->bodyData || app->formData || app->upload) { app->method = "POST"; } else if (app->files) { app->method = "PUT"; } else { app->method = "GET"; } } return 1; }
MAIN(appweb, int argc, char **argv, char **envp) { Mpr *mpr; cchar *ipAddrPort, *argp, *jail; char *ip, *logSpec; int argind, port, status, verbose; ipAddrPort = 0; ip = 0; jail = 0; port = -1; verbose = 0; logSpec = 0; argv[0] = BLD_APPWEB_PATH; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == NULL) { exit(1); } mprSetAppName(BLD_PRODUCT, BLD_NAME, BLD_VERSION); if ((app = mprAllocObj(App, manageApp)) == NULL) { exit(2); } mprAddRoot(app); mprAddStandardSignals(); #if BLD_FEATURE_ROMFS extern MprRomInode romFiles[]; mprSetRomFileSystem(romFiles); #endif app->mpr = mpr; app->workers = -1; app->configFile = BLD_CONFIG_FILE; app->home = BLD_SERVER_ROOT; app->documents = app->home; argc = mpr->argc; argv = mpr->argv; for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (smatch(argp, "--config") || smatch(argp, "--conf")) { if (argind >= argc) { usageError(); } app->configFile = sclone(argv[++argind]); #if BLD_UNIX_LIKE } else if (smatch(argp, "--chroot")) { if (argind >= argc) { usageError(); } jail = mprGetAbsPath(argv[++argind]); #endif } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--exe")) { if (argind >= argc) { usageError(); } mpr->argv[0] = mprGetAbsPath(argv[++argind]); mprSetAppPath(mpr->argv[0]); mprSetModuleSearchPath(NULL); } else if (smatch(argp, "--home")) { if (argind >= argc) { usageError(); } app->home = mprGetAbsPath(argv[++argind]); #if UNUSED && KEEP if (chdir(app->home) < 0) { mprError("%s: Can't change directory to %s", mprGetAppName(), app->home); exit(4); } #endif } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) { usageError(); } logSpec = argv[++argind]; } else if (smatch(argp, "--name") || smatch(argp, "-n")) { if (argind >= argc) { usageError(); } mprSetAppName(argv[++argind], 0, 0); } else if (smatch(argp, "--threads")) { if (argind >= argc) { usageError(); } app->workers = atoi(argv[++argind]); } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { verbose++; } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s %s-%s\n", mprGetAppTitle(), BLD_VERSION, BLD_NUMBER); exit(0); } else { mprError("Unknown switch \"%s\"", argp); usageError(); exit(5); } } if (logSpec) { mprStartLogging(logSpec, 1); mprSetCmdlineLogging(1); } else if (verbose) { mprStartLogging(sfmt("stderr:%d", verbose + 1), 1); mprSetCmdlineLogging(1); } if (mprStart() < 0) { mprUserError("Can't start MPR for %s", mprGetAppName()); mprDestroy(MPR_EXIT_DEFAULT); return MPR_ERR_CANT_INITIALIZE; } if (checkEnvironment(argv[0]) < 0) { exit(6); } if (argc > argind) { if (argc > (argind + 2)) { usageError(); } ipAddrPort = argv[argind++]; if (argc > argind) { app->documents = sclone(argv[argind++]); } mprParseSocketAddress(ipAddrPort, &ip, &port, HTTP_DEFAULT_PORT); } else if (findConfigFile() < 0) { exit(7); } if (jail && changeRoot(jail) < 0) { exit(8); } if (initialize(ip, port) < 0) { return MPR_ERR_CANT_INITIALIZE; } if (maStartAppweb(app->appweb) < 0) { mprUserError("Can't start HTTP service, exiting."); exit(9); } /* Service I/O events until instructed to exit */ while (!mprIsStopping()) { mprServiceEvents(-1, 0); } status = mprGetExitStatus(); mprLog(1, "Stopping Appweb ..."); maStopAppweb(app->appweb); mprDestroy(MPR_EXIT_DEFAULT); return status; }
MAIN(ejscMain, int argc, char **argv, char **envp) { Mpr *mpr; MprList *args; Ejs *ejs; EcCompiler *cp; char *argp, *searchPath, *outputFile, *outputDir, *certFile, *name, *tok, *modules; int nextArg, err, ejsFlags, ecFlags, bind, debug, doc, merge, modver; int warnLevel, noout, parseOnly, tabWidth, optimizeLevel, strict; /* Initialize the Multithreaded Portable Runtime (MPR) */ mpr = mprCreate(argc, argv, 0); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddStandardSignals(); if (mprStart() < 0) { mprLog("ejsc", 0, "Cannot start mpr services"); return EJS_ERR; } err = 0; searchPath = 0; strict = 0; certFile = 0; ecFlags = 0; bind = 0; debug = 0; doc = 0; merge = 0; modver = 0; noout = 0; parseOnly = 0; tabWidth = 4; warnLevel = 1; outputFile = 0; outputDir = 0; optimizeLevel = 9; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--bind") == 0) { bind = 1; } else if (strcmp(argp, "--debug") == 0) { debug = 1; } else if (strcmp(argp, "--debugger") == 0 || strcmp(argp, "-D") == 0) { mprSetDebugMode(1); } else if (strcmp(argp, "--dir") == 0) { /* Set the output directory for modules */ if (nextArg >= argc) { err++; } else { outputDir = argv[++nextArg]; } } else if (strcmp(argp, "--doc") == 0) { doc = 1; } else if (strcmp(argp, "--log") == 0) { /* Undocumented logging switch */ if (nextArg >= argc) { err++; } else { mprStartLogging(argv[++nextArg], 0); mprSetCmdlineLogging(1); } } else if (strcmp(argp, "--merge") == 0) { merge = 1; } else if (strcmp(argp, "--modver") == 0) { if (nextArg >= argc) { err++; } else { modver = ejsParseModuleVersion(argv[++nextArg]); } } else if (strcmp(argp, "--nobind") == 0) { bind = 0; } else if (strcmp(argp, "--noout") == 0) { noout = 1; } else if (strcmp(argp, "--standard") == 0) { strict = 0; } else if (strcmp(argp, "--strict") == 0) { strict = 1; } else if (strcmp(argp, "--optimize") == 0) { if (nextArg >= argc) { err++; } else { optimizeLevel = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--out") == 0) { /* Create a single output module file containing all modules */ if (nextArg >= argc) { err++; } else { outputFile = argv[++nextArg]; } } else if (strcmp(argp, "--parse") == 0) { parseOnly = 1; } else if (strcmp(argp, "--search") == 0 || strcmp(argp, "--searchpath") == 0) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (strcmp(argp, "--require") == 0) { if (nextArg >= argc) { err++; } else { if (app->modules == 0) { app->modules = mprCreateList(-1, 0); } modules = sclone(argv[++nextArg]); /* Fix for Xcode and Visual Studio */ if (modules[0] == ' ' || scmp(modules, "null") == 0) { modules[0] = '\0'; } name = stok(modules, " \t,", &tok); while (name != NULL) { require(name); name = stok(NULL, " \t", &tok); } } } else if (strcmp(argp, "--sign") == 0) { if (nextArg >= argc) { err++; } else { certFile = argv[++nextArg]; } #if FUTURE } else if (strcmp(argp, "--strip") == 0) { strip = 1; #endif } else if (strcmp(argp, "--tabWidth") == 0) { if (nextArg >= argc) { err++; } else { tabWidth = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--version") == 0 || strcmp(argp, "-V") == 0) { mprPrintf("%s\n", ME_TITLE, EJS_VERSION); return 0; } else if (strcmp(argp, "--warn") == 0) { if (nextArg >= argc) { err++; } else { warnLevel = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--web") == 0) { if (app->modules == 0) { app->modules = mprCreateList(-1, 0); } require("ejs"); require("ejs.unix"); require("ejs.db"); // TODO - decouple and remove this require("ejs.db.mapper"); require("ejs.web"); } else { err++; break; } } if (noout || merge) { bind = 1; } if (outputFile && noout) { mprEprintf("Cannot use --out and --noout\n"); err++; } if (argc == nextArg) { err++; } if (err) { /* Usage Examples: ejsc Person.es User.es Customer.es ejsc --out group.mod Person.es User.es Customer.es ejsc --out group.mod Person.es User.es Customer.es NOTE: bind is deliberately not documented and is for internal use only. */ mprEprintf("Usage: %s [options] files...\n" " Ejscript compiler options:\n" " --debug # Include symbolic debugging information in output\n" " --doc # Include documentation strings in output\n" " --dir directory # Set the output directory for modules (default: \".\")\n" " --merge # Merge dependent input modules into the output\n" " --modver version # Set the default module version\n" " --noout # Do not generate any output\n" " --optimize level # Set optimization level (0-9)\n" " --out filename # Name a single output module (default: \"default.mod\")\n" " --parse # Just parse source. No output\n" " --require 'module ...' # List of required modules to pre-load\n" " --search ejsPath # Module search path\n" " --standard # Default compilation mode to standard (default)\n" " --strict # Default compilation mode to strict\n" #if FUTURE " --sign certFile # Sign the module file (not implemented) \n" " --strip # Strip all symbolic names (Cannot import)\n" " --tabwidth # Tab width for '^' error reporting\n" #endif " --version # Emit the compiler version information\n" " --warn level # Set the warning message level (0-9)\n\n", mpr->name); return -1; } ejsFlags = EJS_FLAG_NO_INIT; if (doc) { ejsFlags |= EJS_FLAG_DOC; } if ((ejs = ejsCreateVM(0, 0, ejsFlags)) == 0) { return MPR_ERR_MEMORY; } #if UNUSED mprRunDispatcher(ejs->dispatcher); #endif if (ejsLoadModules(ejs, searchPath, app->modules) < 0) { return MPR_ERR_CANT_READ; } app->ejs = ejs; ecFlags |= (debug) ? EC_FLAGS_DEBUG: 0; ecFlags |= (merge) ? EC_FLAGS_MERGE: 0; ecFlags |= (bind) ? EC_FLAGS_BIND: 0; ecFlags |= (noout) ? EC_FLAGS_NO_OUT: 0; ecFlags |= (parseOnly) ? EC_FLAGS_PARSE_ONLY: 0; ecFlags |= (doc) ? EC_FLAGS_DOC: 0; cp = app->compiler = ecCreateCompiler(ejs, ecFlags); if (cp == 0) { return MPR_ERR_MEMORY; } cp->require = app->modules; cp->modver = modver; ecSetOptimizeLevel(cp, optimizeLevel); ecSetWarnLevel(cp, warnLevel); ecSetStrictMode(cp, strict); ecSetTabWidth(cp, tabWidth); ecSetOutputDir(cp, outputDir); ecSetOutputFile(cp, outputFile); ecSetCertFile(cp, certFile); if (nextArg < argc) { /* Compile the source files supplied on the command line. This will compile in-memory and optionally also save to module files. */ if ((args = expandWild(ejs, argc - nextArg, &argv[nextArg])) == 0) { err++; } else if (ecCompile(cp, args->length, (char**) args->items) < 0) { err++; } if (cp->warningCount > 0 || cp->errorCount > 0) { mprLog("ejs", 0, "%s\n", cp->errorMsg); } } if (cp->errorCount > 0) { err++; } app->ejs = 0; app->compiler = 0; ejsDestroy(ejs); mprDestroy(); return err; }
MAIN(ejsmodMain, int argc, char **argv, char **envp) { Mpr *mpr; EjsMod *mp; Ejs *ejs; MprList *requiredModules; char *argp, *searchPath, *output, *modules, *name, *tok; int nextArg, err, flags; err = 0; output = searchPath = 0; requiredModules = 0; /* Initialze the Multithreaded Portable Runtime (MPR) */ mpr = mprCreate(argc, argv, 0); mprSetAppName(argv[0], 0, 0); /* Allocate the primary control structure */ if ((mp = mprAllocObj(EjsMod, manageMod)) == NULL) { return MPR_ERR_MEMORY; } mprAddRoot(mp); mp->lstRecords = mprCreateList(0, 0); mp->blocks = mprCreateList(0, 0); mp->docDir = sclone("."); mp->outputDir = sclone("."); for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--cslots") == 0) { mp->cslots = 1; mp->genSlots = 1; } else if (strcmp(argp, "--debugger") == 0 || strcmp(argp, "-D") == 0) { mprSetDebugMode(1); } else if (strcmp(argp, "--depends") == 0) { mp->depends = 1; } else if (strcmp(argp, "--dir") == 0) { if (nextArg >= argc) { err++; } else { mp->outputDir = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--error") == 0) { /* Undocumented switch */ mp->exitOnError++; mp->warnOnError++; } else if (strcmp(argp, "--html") == 0) { if (nextArg >= argc) { err++; } else { mp->docDir = sclone(argv[++nextArg]); mp->html = 1; } } else if (strcmp(argp, "--listing") == 0) { mp->listing = 1; mp->showAsm = 1; } else if (strcmp(argp, "--log") == 0) { /* Undocumented switch */ if (nextArg >= argc) { err++; } else { mprStartLogging(argv[++nextArg], 0); mprSetCmdlineLogging(1); } } else if (strcmp(argp, "--out") == 0) { if (nextArg >= argc) { err++; } else { output = argv[++nextArg]; mp->cslots = 1; mp->genSlots = 1; } } else if (strcmp(argp, "--search") == 0 || strcmp(argp, "--searchpath") == 0) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (strcmp(argp, "--version") == 0 || strcmp(argp, "-V") == 0) { mprPrintfError("%s %s-%s\n", BIT_NAME, BIT_VERSION, BIT_NUMBER); return 0; } else if (strcmp(argp, "--require") == 0) { if (nextArg >= argc) { err++; } else { if (requiredModules == 0) { requiredModules = mprCreateList(-1, 0); } modules = sclone(argv[++nextArg]); /* Fix for Xcode and Visual Studio */ if (modules[0] == ' ' || scmp(modules, "null") == 0) { modules[0] = '\0'; } name = stok(modules, " \t", &tok); while (name != NULL) { require(requiredModules, name); name = stok(NULL, " \t", &tok); } } } else if (strcmp(argp, "--warn") == 0) { mp->warnOnError++; } else if (strcmp(argp, "--xml") == 0) { mp->xml = 1; } else { err++; break; } } if (argc == nextArg) { err++; } if (mp->genSlots == 0 && mp->listing == 0 && mp->html == 0 && mp->xml == 0 && mp->depends == 0) { mp->listing = 1; } if (mp->depends && requiredModules == 0) { requiredModules = mprCreateList(-1, 0); } if (err) { /* Examples: ejsmod file.mod # Defaults to --listing ejsmod --listing embedthis.mod ejsmod --out slots.h embedthis.mod */ mprPrintfError("Usage: %s [options] modules ...\n" " Ejscript module manager options:\n" " --cslots # Generate a C slot definitions file\n" " --html dir # Generate HTML documentation to the specified directory\n" " --listing # Create assembler listing files (default)\n" " --out file # Output file for all C slots (implies --cslots)\n" " --require \"modules\" # List of modules to preload\n" " --search ejsPath # Module file search path\n" " --version # Emit the program version information\n" " --warn # Warn about undocumented methods or parameters in doc\n\n", mpr->name); return -1; } /* Need an interpreter to load modules */ flags = EJS_FLAG_NO_INIT; if (mp->html || mp->xml) { flags |= EJS_FLAG_DOC; } if ((ejs = ejsCreateVM(0, 0, flags)) == 0) { return MPR_ERR_MEMORY; } if (ejsLoadModules(ejs, searchPath, requiredModules) < 0) { return MPR_ERR_CANT_READ; } mp->ejs = ejs; if (nextArg < argc) { if (process(mp, output, argc - nextArg, &argv[nextArg]) < 0) { err++; } } if (mp->errorCount > 0) { err = -1; } mprRemoveRoot(mp); ejsDestroyVM(ejs); mprDestroy(MPR_EXIT_DEFAULT); return err; }
static int parseArgs(int argc, char **argv) { char *argp, *key, *value; int i, setWorkers, nextArg, ssl; setWorkers = 0; ssl = 0; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (smatch(argp, "--auth")) { if (nextArg >= argc) { return showUsage(); } else { app->authType = slower(argv[++nextArg]); } } else if (smatch(argp, "--benchmark") || smatch(argp, "-b")) { app->benchmark++; } else if (smatch(argp, "--ca")) { if (nextArg >= argc) { return showUsage(); } else { app->ca = sclone(argv[++nextArg]); if (!mprPathExists(app->ca, R_OK)) { mprError("Cannot find ca file %s", app->ca); return MPR_ERR_BAD_ARGS; } } ssl = 1; } else if (smatch(argp, "--cert")) { if (nextArg >= argc) { return showUsage(); } else { app->cert = sclone(argv[++nextArg]); if (!mprPathExists(app->cert, R_OK)) { mprError("Cannot find cert file %s", app->cert); return MPR_ERR_BAD_ARGS; } } ssl = 1; } else if (smatch(argp, "--chunk")) { if (nextArg >= argc) { return showUsage(); } else { value = argv[++nextArg]; app->chunkSize = atoi(value); if (app->chunkSize < 0) { mprError("Bad chunksize %d", app->chunkSize); return MPR_ERR_BAD_ARGS; } } } else if (smatch(argp, "--ciphers")) { if (nextArg >= argc) { return showUsage(); } else { app->ciphers = sclone(argv[++nextArg]); } ssl = 1; } else if (smatch(argp, "--continue") || smatch(argp, "-c")) { app->continueOnErrors++; } else if (smatch(argp, "--cookie")) { if (nextArg >= argc) { return showUsage(); } else { mprAddItem(app->headers, mprCreateKeyPair("Cookie", argv[++nextArg], 0)); } } else if (smatch(argp, "--data")) { if (nextArg >= argc) { return showUsage(); } else { if (app->bodyData == 0) { app->bodyData = mprCreateBuf(-1, -1); } mprPutStringToBuf(app->bodyData, argv[++nextArg]); } } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); app->retries = 0; app->timeout = MAXINT; } else if (smatch(argp, "--delete")) { app->method = "DELETE"; } else if (smatch(argp, "--form") || smatch(argp, "-f")) { if (nextArg >= argc) { return showUsage(); } else { if (app->formData == 0) { app->formData = mprCreateList(-1, 0); } addFormVars(argv[++nextArg]); } } else if (smatch(argp, "--header")) { if (nextArg >= argc) { return showUsage(); } else { key = argv[++nextArg]; if ((value = strchr(key, ':')) == 0) { mprError("Bad header format. Must be \"key: value\""); return MPR_ERR_BAD_ARGS; } *value++ = '\0'; while (isspace((uchar) *value)) { value++; } mprAddItem(app->headers, mprCreateKeyPair(key, value, 0)); } } else if (smatch(argp, "--host")) { if (nextArg >= argc) { return showUsage(); } else { app->host = argv[++nextArg]; if (*app->host == ':') { app->host = &app->host[1]; } if (isPort(app->host)) { app->host = sfmt("http://127.0.0.1:%s", app->host); } else { app->host = sclone(app->host); } } } else if (smatch(argp, "--iterations") || smatch(argp, "-i")) { if (nextArg >= argc) { return showUsage(); } else { app->iterations = atoi(argv[++nextArg]); } } else if (smatch(argp, "--key")) { if (nextArg >= argc) { return showUsage(); } else { app->key = sclone(argv[++nextArg]); if (!mprPathExists(app->key, R_OK)) { mprError("Cannot find key file %s", app->key); return MPR_ERR_BAD_ARGS; } } ssl = 1; } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (nextArg >= argc) { return showUsage(); } else { mprStartLogging(argv[++nextArg], 0); } } else if (smatch(argp, "--method") || smatch(argp, "-m")) { if (nextArg >= argc) { return showUsage(); } else { app->method = argv[++nextArg]; } } else if (smatch(argp, "--out") || smatch(argp, "-o")) { if (nextArg >= argc) { return showUsage(); } else { app->outFilename = sclone(argv[++nextArg]); } } else if (smatch(argp, "--noout") || smatch(argp, "-n") || smatch(argp, "--quiet") || smatch(argp, "-q")) { app->noout++; } else if (smatch(argp, "--nofollow")) { app->nofollow++; } else if (smatch(argp, "--password") || smatch(argp, "-p")) { if (nextArg >= argc) { return showUsage(); } else { app->password = sclone(argv[++nextArg]); } } else if (smatch(argp, "--post")) { app->method = "POST"; } else if (smatch(argp, "--printable")) { app->printable++; } else if (smatch(argp, "--protocol")) { if (nextArg >= argc) { return showUsage(); } else { app->protocol = supper(argv[++nextArg]); } } else if (smatch(argp, "--provider")) { /* Undocumented SSL provider selection */ if (nextArg >= argc) { return showUsage(); } else { app->provider = sclone(argv[++nextArg]); } ssl = 1; } else if (smatch(argp, "--put")) { app->method = "PUT"; } else if (smatch(argp, "--range")) { if (nextArg >= argc) { return showUsage(); } else { if (app->ranges == 0) { app->ranges = sfmt("bytes=%s", argv[++nextArg]); } else { app->ranges = srejoin(app->ranges, ",", argv[++nextArg], NULL); } } } else if (smatch(argp, "--retries") || smatch(argp, "-r")) { if (nextArg >= argc) { return showUsage(); } else { app->retries = atoi(argv[++nextArg]); } } else if (smatch(argp, "--self")) { /* Undocumented. Allow self-signed certs. Users should just not set --verify */ app->verifyIssuer = 0; ssl = 1; } else if (smatch(argp, "--sequence")) { app->sequence++; } else if (smatch(argp, "--showHeaders") || smatch(argp, "--show") || smatch(argp, "-s")) { app->showHeaders++; } else if (smatch(argp, "--showStatus") || smatch(argp, "--showCode")) { app->showStatus++; } else if (smatch(argp, "--single") || smatch(argp, "-s")) { app->singleStep++; } else if (smatch(argp, "--text")) { app->text++; } else if (smatch(argp, "--threads") || smatch(argp, "-t")) { if (nextArg >= argc) { return showUsage(); } else { app->loadThreads = atoi(argv[++nextArg]); } } else if (smatch(argp, "--timeout")) { if (nextArg >= argc) { return showUsage(); } else { app->timeout = atoi(argv[++nextArg]) * MPR_TICKS_PER_SEC; } } else if (smatch(argp, "--upload") || smatch(argp, "-u")) { app->upload++; } else if (smatch(argp, "--user") || smatch(argp, "--username")) { if (nextArg >= argc) { return showUsage(); } else { app->username = argv[++nextArg]; } // DEPRECATE validate. Preserve verify. } else if (smatch(argp, "--validate") || smatch(argp, "--verify")) { app->verifyPeer = 1; ssl = 1; } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { app->verbose++; } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprEprintf("%s %s\n" "Copyright (C) Embedthis Software 2003-2013\n" "Copyright (C) Michael O'Brien 2003-2013\n", BIT_TITLE, BIT_VERSION); exit(0); } else if (smatch(argp, "--workerTheads") || smatch(argp, "-w")) { if (nextArg >= argc) { return showUsage(); } else { app->workers = atoi(argv[++nextArg]); } setWorkers++; } else if (smatch(argp, "--zero")) { app->zeroOnErrors++; } else if (smatch(argp, "--")) { nextArg++; break; } else if (smatch(argp, "-")) { break; } else { return showUsage(); } } if (argc == nextArg) { return showUsage(); } app->nextArg = nextArg; argc = argc - nextArg; argv = &argv[nextArg]; app->target = argv[argc - 1]; if (--argc > 0) { /* Files present on command line */ app->files = mprCreateList(argc, MPR_LIST_STATIC_VALUES); for (i = 0; i < argc; i++) { mprAddItem(app->files, argv[i]); } } if (!setWorkers) { app->workers = app->loadThreads + 2; } if (app->method == 0) { if (app->bodyData || app->formData || app->upload) { app->method = "POST"; } else if (app->files) { app->method = "PUT"; } else { app->method = "GET"; } } #if BIT_PACK_SSL { HttpUri *uri = httpCreateUri(app->target, 0); if (uri->secure || ssl) { app->ssl = mprCreateSsl(0); if (app->provider) { mprSetSslProvider(app->ssl, app->provider); } if (app->cert) { if (!app->key) { mprError("Must specify key file"); return 0; } mprSetSslCertFile(app->ssl, app->cert); mprSetSslKeyFile(app->ssl, app->key); } if (app->ca) { mprLog(4, "Using CA: \"%s\"", app->ca); mprSetSslCaFile(app->ssl, app->ca); } if (app->verifyIssuer == -1) { app->verifyIssuer = app->verifyPeer ? 1 : 0; } mprVerifySslPeer(app->ssl, app->verifyPeer); mprVerifySslIssuer(app->ssl, app->verifyIssuer); if (app->ciphers) { mprSetSslCiphers(app->ssl, app->ciphers); } } else { mprVerifySslPeer(NULL, 0); } } #else /* Suppress comp warning */ mprNop(&ssl); #endif return 0; }
MAIN(appweb, int argc, char **argv, char **envp) { Mpr *mpr; cchar *argp, *jail; char *logSpec; int argind, status, verbose; jail = 0; verbose = 0; logSpec = 0; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == NULL) { exit(1); } mprSetAppName(BIT_PRODUCT, BIT_TITLE, BIT_VERSION); /* Allocate the top level application object. ManageApp is the GC manager function and is called by the GC to mark references in the app object. */ if ((app = mprAllocObj(AppwebApp, manageApp)) == NULL) { exit(2); } mprAddRoot(app); mprAddStandardSignals(); app->mpr = mpr; app->configFile = sclone("appweb.conf"); app->home = mprGetCurrentPath(); app->documents = app->home; argc = mpr->argc; argv = (char**) mpr->argv; for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (smatch(argp, "--config") || smatch(argp, "--conf")) { if (argind >= argc) { usageError(); } app->configFile = sclone(argv[++argind]); #if BIT_UNIX_LIKE } else if (smatch(argp, "--chroot")) { if (argind >= argc) { usageError(); } jail = mprGetAbsPath(argv[++argind]); #endif } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--exe")) { if (argind >= argc) { usageError(); } mpr->argv[0] = mprGetAbsPath(argv[++argind]); mprSetAppPath(mpr->argv[0]); mprSetModuleSearchPath(NULL); } else if (smatch(argp, "--home")) { if (argind >= argc) { usageError(); } app->home = mprGetAbsPath(argv[++argind]); if (chdir(app->home) < 0) { mprError("%s: Cannot change directory to %s", mprGetAppName(), app->home); exit(4); } } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) { usageError(); } logSpec = argv[++argind]; } else if (smatch(argp, "--name") || smatch(argp, "-n")) { if (argind >= argc) { usageError(); } mprSetAppName(argv[++argind], 0, 0); } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { verbose++; } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s-%s\n", BIT_VERSION, BIT_BUILD_NUMBER); exit(0); } else { if (!smatch(argp, "?")) { mprError("Unknown switch \"%s\"", argp); } usageError(); exit(5); } } if (logSpec) { mprStartLogging(logSpec, 1); mprSetCmdlineLogging(1); } else if (verbose) { mprStartLogging(sfmt("stderr:%d", verbose + 1), 1); mprSetCmdlineLogging(1); } /* Start the multithreaded portable runtime (MPR) */ if (mprStart() < 0) { mprError("Cannot start MPR for %s", mprGetAppName()); mprDestroy(MPR_EXIT_DEFAULT); return MPR_ERR_CANT_INITIALIZE; } if (checkEnvironment(argv[0]) < 0) { exit(6); } if (findAppwebConf() < 0) { exit(7); } if (jail && changeRoot(jail) < 0) { exit(8); } /* Open the sockets to listen on */ if (createEndpoints(argc - argind, &argv[argind]) < 0) { return MPR_ERR_CANT_INITIALIZE; } /* Start HTTP services */ if (maStartAppweb(app->appweb) < 0) { mprError("Cannot start HTTP service, exiting."); exit(9); } /* Service I/O events until instructed to exit */ while (!mprIsStopping()) { mprServiceEvents(-1, 0); } status = mprGetExitStatus(); mprLog(1, "Stopping Appweb ..."); maStopAppweb(app->appweb); mprDestroy(MPR_EXIT_DEFAULT); return status; }
MAIN(ejsMain, int argc, char **argv, char **envp) { Mpr *mpr; Ejs *ejs; EcCompiler *ec; char *argp, *searchPath, *path, *homeDir; int nextArg, err, flags; /* Initialize Multithreaded Portable Runtime (MPR) */ mpr = mprCreate(argc, argv, 0); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddStandardSignals(); if (mprStart(mpr) < 0) { mprError("Cannot start mpr services"); return EJS_ERR; } err = 0; searchPath = 0; argc = mpr->argc; argv = (char**) mpr->argv; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (smatch(argp, "--chdir") || smatch(argp, "--home") || smatch(argp, "-C")) { if (nextArg >= argc) { err++; } else { homeDir = argv[++nextArg]; if (chdir((char*) homeDir) < 0) { mprError("Cannot change directory to %s", homeDir); } } } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--log")) { if (nextArg >= argc) { err++; } else { mprStartLogging(argv[++nextArg], 0); mprSetCmdlineLogging(1); } } else if (smatch(argp, "--name")) { /* Just ignore. Used to tag commands with a unique command line */ nextArg++; } else if (smatch(argp, "--search") || smatch(argp, "--searchpath")) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { mprStartLogging("stderr:1", 0); mprSetCmdlineLogging(1); } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s-%s\n", BIT_VERSION, BIT_BUILD_NUMBER); return 0; } else { /* Ignore */ } } path = mprJoinPath(mprGetAppDir(), mprGetPathBase(argv[0])); path = mprReplacePathExt(path, ".es"); mprAddRoot(path); argv[0] = path; if ((ejs = ejsCreateVM(argc, (cchar **) &argv[0], 0)) == 0) { return MPR_ERR_MEMORY; } app->ejs = ejs; if (ejsLoadModules(ejs, searchPath, NULL) < 0) { return MPR_ERR_CANT_READ; } mprLog(2, "Load script \"%s\"", path); flags = EC_FLAGS_BIND | EC_FLAGS_DEBUG | EC_FLAGS_NO_OUT | EC_FLAGS_THROW; if ((ec = ecCreateCompiler(ejs, flags)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(ec); ecSetOptimizeLevel(ec, 9); ecSetWarnLevel(ec, 1); if (ecCompile(ec, 1, (char**) &path) < 0) { if (flags & EC_FLAGS_THROW) { ejsThrowSyntaxError(ejs, "%s", ec->errorMsg ? ec->errorMsg : "Cannot parse script"); ejsReportError(ejs, "Error in script"); } err = MPR_ERR; } else { mprRemoveRoot(ec); if (ejsRunProgram(ejs, NULL, NULL) < 0) { ejsReportError(ejs, "Error in script"); err = MPR_ERR; } } if (!err) { err = mpr->exitStatus; } app->ejs = 0; mprTerminate(MPR_EXIT_DEFAULT, err); ejsDestroyVM(ejs); mprDestroy(MPR_EXIT_DEFAULT); return err; }
MAIN(http, int argc, char **argv, char **envp) { Mpr *mpr; cchar *argp; char *logSpec, *traceSpec; int argind; logSpec = 0; traceSpec = 0; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == NULL) { exit(1); } if ((app = mprAllocObj(App, manageApp)) == NULL) { exit(2); } mprAddRoot(app); mprAddStandardSignals(); if (httpCreate(HTTP_CLIENT_SIDE | HTTP_SERVER_SIDE) < 0) { exit(2); } app->mpr = mpr; app->home = mprGetCurrentPath(); argc = mpr->argc; argv = (char**) mpr->argv; for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (smatch(argp, "--config") || smatch(argp, "--conf")) { if (argind >= argc) { usageError(); } app->configFile = sclone(argv[++argind]); } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) { usageError(); } logSpec = argv[++argind]; } else if (smatch(argp, "--name") || smatch(argp, "-n")) { if (argind >= argc) { usageError(); } mprSetAppName(argv[++argind], 0, 0); } else if (smatch(argp, "--show") || smatch(argp, "-s")) { app->show = 1; } else if (smatch(argp, "--trace") || smatch(argp, "-t")) { if (argind >= argc) { usageError(); } traceSpec = argv[++argind]; } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { if (!logSpec) { logSpec = sfmt("stderr:2"); } if (!traceSpec) { traceSpec = sfmt("stderr:2"); } } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s\n", ME_VERSION); exit(0); } else if (*argp == '-' && isdigit((uchar) argp[1])) { if (!logSpec) { logSpec = sfmt("stderr:%d", (int) stoi(&argp[1])); } if (!traceSpec) { traceSpec = sfmt("stderr:%d", (int) stoi(&argp[1])); } } else if (smatch(argp, "-?") || scontains(argp, "-help")) { usageError(); exit(3); } else if (*argp == '-') { mprLog("error http", 0, "Unknown switch \"%s\"", argp); usageError(); exit(4); } } if (logSpec) { mprStartLogging(logSpec, MPR_LOG_CMDLINE); } if (traceSpec) { httpStartTracing(traceSpec); } if (mprStart() < 0) { mprLog("error http", 0, "Cannot start MPR"); mprDestroy(); return MPR_ERR_CANT_INITIALIZE; } if (argc == argind && !app->configFile) { if (findConfig() < 0) { exit(5); } } if (createEndpoints(argc - argind, &argv[argind]) < 0) { return MPR_ERR_CANT_INITIALIZE; } if (httpStartEndpoints() < 0) { mprLog("error http", 0, "Cannot start HTTP service, exiting."); exit(6); } if (app->show) { httpLogRoutes(0, 0); } mprServiceEvents(-1, 0); mprLog("info http", 1, "Stopping http-server ..."); mprDestroy(); return mprGetExitStatus(); }