/* Create a simple stand-alone web server */ int main(int argc, char **argv, char **envp) { Mpr *mpr; MaAppweb *appweb; MaServer *server; int rc; rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD)) == 0) { mprError("Cannot create the web server runtime"); return -1; } mprStart(); appweb = maCreateAppweb(mpr); mprAddRoot(appweb); server = maCreateServer(appweb, 0); if (maParseConfig(server, "appweb.conf", 0) < 0) { mprError("Cannot parse the config file %s", "appweb.conf"); return -1; } httpDefineAction("/action/myaction", myaction); if (maStartServer(server) < 0) { mprError("Cannot start the web server"); return -1; } mprServiceEvents(-1, 0); maStopServer(server); mprRemoveRoot(appweb); mprDestroy(MPR_EXIT_DEFAULT); return 0; }
/* * Create a web server described by a config file. */ MaHttp *maCreateWebServer(cchar *configFile) { Mpr *mpr; MaHttp *http; MaServer *server; /* * Initialize and start the portable runtime services. */ if ((mpr = mprCreate(0, NULL, NULL)) == 0) { mprError(mpr, "Can't create the web server runtime"); return 0; } if (mprStart(mpr, 0) < 0) { mprError(mpr, "Can't start the web server runtime"); return 0; } http = maCreateHttp(mpr); if ((server = maCreateServer(http, configFile, NULL, NULL, -1)) == 0) { mprError(mpr, "Can't create the web server"); return 0; } if (maParseConfig(server, configFile) < 0) { mprError(mpr, "Can't parse the config file %s", configFile); return 0; } return http; }
/* Run the web client to retrieve a URI This will create the MPR and Http service on demand. As such, it is not the most efficient way to run a web request. @return HTTP status code or negative MPR error code. Returns a malloc string in response. */ PUBLIC int maRunWebClient(cchar *method, cchar *uri, cchar *data, char **response, char **err) { Mpr *mpr; HttpConn *conn; int status; if (err) { *err = 0; } if (response) { *response = 0; } if ((mpr = mprCreate(0, NULL, 0)) == 0) { mprLog("error appweb", 0, "Cannot create the MPR runtime"); return MPR_ERR_CANT_CREATE; } if (mprStart() < 0) { mprLog("error appweb", 0, "Cannot start the web server runtime"); return MPR_ERR_CANT_INITIALIZE; } httpCreate(HTTP_CLIENT_SIDE); conn = httpRequest(method, uri, data, err); status = httpGetStatus(conn); if (response) { *response = httpReadString(conn); } mprDestroy(); return status; }
/* Create a simple stand-alone web server */ int main(int argc, char **argv, char **envp) { Mpr *mpr; int rc; rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD)) == 0) { mprError("Cannot create runtime"); return -1; } if (httpCreate(HTTP_CLIENT_SIDE | HTTP_SERVER_SIDE) == 0) { mprError("Cannot create the HTTP services"); return -1; } mprStart(); if (maParseConfig("appweb.conf") < 0) { mprError("Cannot parse the config file %s", "appweb.conf"); return -1; } httpDefineAction("/action/myaction", myaction); if (httpStartEndpoints() < 0) { mprError("Cannot start the web server"); return -1; } mprServiceEvents(-1, 0); mprDestroy(); return 0; }
MAIN(simpleClient, int argc, char** argv) { Mpr *mpr; App *app; cchar *content; int code; /* Create the Multithreaded Portable Runtime */ mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD); if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); mprAddStandardSignals(app); /* Start the Multithreaded Portable Runtime */ mprStart(); /* Create an Http service object */ app->http = httpCreate(mpr); /* Get a client http object to work with. We can issue multiple requests with this one object. */ app->conn = httpCreateConn(app->http, NULL, NULL); /* Get a URL */ if (httpConnect(app->conn, "GET", "http://www.embedthis.com/index.html") < 0) { mprError("Can't get URL"); exit(2); } /* Examine the HTTP response HTTP code. 200 is success. */ code = httpGetStatus(app->conn); if (code != 200) { mprError("Server responded with code %d\n", code); exit(1); } /* Get the actual response content */ content = httpReadString(app->conn); if (content) { mprPrintf("Server responded with: %s\n", content); } mprDestroy(MPR_EXIT_DEFAULT); return 0; }
MAIN(simpleClient, int argc, char **argv, char **envp) { Http *http; HttpConn *conn; cchar *content; int code; /* Create the Multithreaded Portable Runtime and start it. */ mprCreate(argc, argv, 0); mprStart(); /* Get a client http object to work with. We can issue multiple requests with this one object. Add the conn as a root object so the GC won't collect it while we are using it. */ http = httpCreate(HTTP_CLIENT_SIDE); conn = httpCreateConn(http, NULL, NULL); mprAddRoot(conn); /* Open a connection to issue the GET. Then finalize the request output - this forces the request out. */ if (httpConnect(conn, "GET", "http://www.embedthis.com/index.html", NULL) < 0) { mprError("Can't get URL"); exit(2); } httpFinalizeOutput(conn); /* Wait for a response */ if (httpWait(conn, HTTP_STATE_PARSED, 10000) < 0) { mprError("No response"); exit(2); } /* Examine the HTTP response HTTP code. 200 is success. */ code = httpGetStatus(conn); if (code != 200) { mprError("Server responded with code %d\n", code); exit(1); } /* Get the actual response content */ content = httpReadString(conn); if (content) { mprPrintf("Server responded with: %s\n", content); } mprDestroy(MPR_EXIT_DEFAULT); return 0; }
MAIN(benchMpr, int argc, char **argv, char **envp) { MprThread *thread; Mpr *mpr; char *argp; int err, nextArg; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == 0) { return MPR_ERR_MEMORY; } if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); app->mutex = mprCreateLock(mpr); app->complete = mprCreateCond(); app->iterations = 5; err = 0; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--iterations") == 0 || strcmp(argp, "-i") == 0) { if (nextArg >= argc) { err++; } else { app->iterations = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--alloc") == 0 || strcmp(argp, "-a") == 0) { app->testAllocOnly++; } else { err++; } } if (err) { mprEprintf("usage: bench [-a] [-i iterations]\n"); mprEprintf("usage: %s [options]\n" " -a # Alloc test only\n" " --iterations count # Number of iterations to run the test\n", mprGetAppName(mpr)); exit(2); } mprStart(mpr); thread = mprCreateThread("bench", (MprThreadProc) doBenchmark, (void*) MPR, 0); mprStartThread(thread); while (!testComplete) { mprServiceEvents(250, 0); } mprPrintMem("Memory Report", 0); mprDestroy(); return 0; }
MAIN(httpMain, int argc, char *argv[]) { MprTime start; double elapsed; if (mprCreate(argc, argv, MPR_USER_EVENTS_THREAD) == 0) { return MPR_ERR_MEMORY; } if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); mprAddStandardSignals(); initSettings(); if (!parseArgs(argc, argv)) { showUsage(); return MPR_ERR_BAD_ARGS; } mprSetMaxWorkers(app->workers); #if BLD_FEATURE_SSL if (!mprLoadSsl(1)) { mprError("Can't load SSL"); exit(1); } #endif if (mprStart() < 0) { mprError("Can't start MPR for %s", mprGetAppTitle()); exit(2); } start = mprGetTime(); app->http = httpCreate(); httpEaseLimits(app->http->clientLimits); processing(); mprServiceEvents(-1, 0); if (app->benchmark) { elapsed = (double) (mprGetTime() - start); if (app->fetchCount == 0) { elapsed = 0; app->fetchCount = 1; } mprPrintf("\nRequest Count: %13d\n", app->fetchCount); mprPrintf("Time elapsed: %13.4f sec\n", elapsed / 1000.0); mprPrintf("Time per request: %13.4f sec\n", elapsed / 1000.0 / app->fetchCount); mprPrintf("Requests per second: %13.4f\n", app->fetchCount * 1.0 / (elapsed / 1000.0)); mprPrintf("Load threads: %13d\n", app->loadThreads); mprPrintf("Worker threads: %13d\n", app->workers); } if (!app->success && app->verbose) { mprError("Request failed"); } return (app->success) ? 0 : 255; }
MAIN(testMain, int argc, char *argv[]) { Mpr *mpr; MprTestService *ts; MprTestGroup *gp; int rc; mpr = mprCreate(argc, argv, 0); #if VXWORKS || WINCE /* * These platforms pass an arg string in via the argc value. Assumes 32-bit. */ mprMakeArgv(mpr, "testMpr", (char*) argc, &argc, &argv); #endif ts = mprCreateTestService(mpr); if (ts == 0) { mprError(mpr, "Can't create test service"); exit(2); } if (mprParseTestArgs(ts, argc, argv) < 0) { mprFree(mpr); exit(3); } gp = mprAddTestGroup(ts, &master); if (gp == 0) { exit(4); } #if BLD_FEATURE_SSL && (BLD_FEATURE_MATRIXSSL || BLD_FEATURE_OPENSSL) if (!mprLoadSsl(mpr, 0)) { exit(5); } #endif /* * Need a background event thread as we use the main thread to run the tests. */ if (mprStart(mpr, 0)) { mprError(mpr, "Can't start mpr services"); exit(4); } /* * Run the tests and return zero if 100% success */ rc = mprRunTests(ts); mprReportTestResults(ts); return (rc == 0) ? 0 : 6; }
MAIN(httpMain, int argc, char **argv, char **envp) { MprTime start; double elapsed; if (mprCreate(argc, argv, MPR_USER_EVENTS_THREAD) == 0) { return MPR_ERR_MEMORY; } if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); mprAddStandardSignals(); initSettings(); if (parseArgs(argc, argv) < 0) { return MPR_ERR_BAD_ARGS; } mprSetMaxWorkers(app->workers); if (mprStart() < 0) { mprError("Cannot start MPR for %s", mprGetAppTitle()); exit(2); } start = mprGetTime(); app->http = httpCreate(HTTP_CLIENT_SIDE); httpEaseLimits(app->http->clientLimits); #if BIT_STATIC && BIT_PACK_SSL extern MprModuleEntry mprSslInit; mprNop(mprSslInit); #endif processing(); mprServiceEvents(-1, 0); if (app->benchmark) { elapsed = (double) (mprGetTime() - start); if (app->fetchCount == 0) { elapsed = 0; app->fetchCount = 1; } mprPrintf("\nRequest Count: %13d\n", app->fetchCount); mprPrintf("Time elapsed: %13.4f sec\n", elapsed / 1000.0); mprPrintf("Time per request: %13.4f sec\n", elapsed / 1000.0 / app->fetchCount); mprPrintf("Requests per second: %13.4f\n", app->fetchCount * 1.0 / (elapsed / 1000.0)); mprPrintf("Load threads: %13d\n", app->loadThreads); mprPrintf("Worker threads: %13d\n", app->workers); } if (!app->success && app->verbose) { mprError("Request failed"); } mprDestroy(MPR_EXIT_DEFAULT); return (app->success) ? 0 : 255; }
MAIN(httpMain, int argc, char **argv, char **envp) { MprTime start; double elapsed; int success; if (mprCreate(argc, argv, MPR_USER_EVENTS_THREAD) == 0) { return MPR_ERR_MEMORY; } if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); mprAddStandardSignals(); initSettings(); if ((app->http = httpCreate(HTTP_CLIENT_SIDE)) == 0) { return MPR_ERR_MEMORY; } if (parseArgs(argc, argv) < 0) { return MPR_ERR_BAD_ARGS; } mprSetMaxWorkers(app->workers); if (mprStart() < 0) { mprLog("error http", 0, "Cannot start MPR for %s", mprGetAppTitle()); exit(2); } start = mprGetTime(); processing(); mprServiceEvents(-1, 0); if (app->benchmark) { elapsed = (double) (mprGetTime() - start); if (app->fetchCount == 0) { elapsed = 0; app->fetchCount = 1; } mprPrintf("\nRequest Count: %13d\n", app->fetchCount); mprPrintf("Time elapsed: %13.4f sec\n", elapsed / 1000.0); mprPrintf("Time per request: %13.4f sec\n", elapsed / 1000.0 / app->fetchCount); mprPrintf("Requests per second: %13.4f\n", app->fetchCount * 1.0 / (elapsed / 1000.0)); mprPrintf("Load threads: %13d\n", app->loadThreads); mprPrintf("Worker threads: %13d\n", app->workers); } if (!app->success && app->verbose) { mprLog("error http", 0, "Request failed"); } success = app->success; mprDestroy(); return success ? 0 : 255; }
/* Create a web server described by a config file. */ PUBLIC int maRunWebServer(cchar *configFile) { Mpr *mpr; int rc; if ((mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD)) == 0) { mprLog("error appweb", 0, "Cannot create the web server runtime"); return MPR_ERR_CANT_CREATE; } rc = runServer(configFile, 0, 0, 0, 0); mprDestroy(); return rc; }
/* Run a web server not based on a config file. */ PUBLIC int maRunSimpleWebServer(cchar *ip, int port, cchar *home, cchar *documents) { Mpr *mpr; int rc; if ((mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD)) == 0) { mprLog("error appweb", 0, "Cannot create the web server runtime"); return MPR_ERR_CANT_CREATE; } rc = runServer(0, ip, port, home, documents); mprDestroy(); return rc; }
int main(int argc, char **argv) { Mpr *mpr; MprList *files; char *argp, *prefix, *romName; int nextArg, err; mpr = mprCreate(argc, argv, 0); err = 0; prefix = ""; romName = "romFiles"; files = mprCreateList(-1, 0); for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--prefix") == 0) { if (nextArg >= argc) { err++; } else { prefix = argv[++nextArg]; } } else if (strcmp(argp, "--name") == 0) { if (nextArg >= argc) { err++; } else { romName = argv[++nextArg]; } } } if (nextArg >= argc) { err++; } if (err) { printUsage(mpr); exit(2); } while (nextArg < argc) { mprAddItem(files, argv[nextArg++]); } if (binToC(files, romName, prefix) < 0) { return MPR_ERR; } return 0; }
int main(int argc, char *argv[]) { Mpr *mpr; int rc; mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD); mprAddStandardSignals(); app = mprAllocObj(App, manageApp); mprAddRoot(app); app->host = sclone("127.0.0.1"); app->port = 4100; if ((ts = mprCreateTestService(mpr)) == 0) { mprLog("appweb test", 0, "Cannot create test service"); exit(2); } if (mprParseTestArgs(ts, argc, argv, parseArgs) < 0) { mprEprintf("\n" " Commands specifically for %s\n" " --host ip:port # Set the default host address for testing\n\n", mprGetAppName(mpr)); exit(3); } if (mprAddTestGroup(ts, &master) == 0) { exit(4); } #if BIT_PACK_SSL if (mprLoadSsl(0) < 0) { return 0; } #endif /* Need a background event thread as we use the main thread to run the tests. */ if (mprStart(mpr)) { mprLog("appweb test", 0, "Cannot start mpr services"); exit(5); } /* Run the tests and return zero if 100% success */ rc = mprRunTests(ts); mprReportTestResults(ts); mprDestroy(); return (rc == 0) ? 0 : -1; }
int main(int argc, char **argv) { Mpr *mpr; cchar *dir; mpr = mprCreate(argc, argv, NULL); if (mprStart(mpr) < 0) { mprError("genbyte", "Cannot start mpr services"); return MPR_ERR; } dir = (argc == 2) ? argv[1] : "."; genByteCodeHeader(mpr, dir); genByteGotoHeader(mpr, dir); mprDestroy(mpr); return 0; }
int maRunSimpleWebServer(cchar *ipAddr, int port, cchar *docRoot) { Mpr *mpr; MaHttp *http; MaServer *server; /* * Initialize and start the portable runtime services. */ if ((mpr = mprCreate(0, NULL, NULL)) == 0) { mprError(mpr, "Can't create the web server runtime"); return MPR_ERR_CANT_CREATE; } if (mprStart(mpr, 0) < 0) { mprError(mpr, "Can't start the web server runtime"); return MPR_ERR_CANT_INITIALIZE; } /* * Create the HTTP object. */ if ((http = maCreateHttp(mpr)) == 0) { mprError(mpr, "Can't create the web server http services"); return MPR_ERR_CANT_INITIALIZE; } /* * Create and start the HTTP server. Give the server a name of "default" and define "." as * the default serverRoot, ie. the directory with the server configuration files. */ server = maCreateServer(http, ipAddr, ".", ipAddr, port); if (server == 0) { mprError(mpr, "Can't create the web server"); return MPR_ERR_CANT_CREATE; } maSetDocumentRoot(server->defaultHost, docRoot); if (maStartHttp(http) < 0) { mprError(mpr, "Can't start the web server"); return MPR_ERR_CANT_CREATE; } mprServiceEvents(mprGetDispatcher(mpr), -1, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); maStopHttp(http); mprFree(mpr); return 0; }
int main(int argc, char *argv[]) { uchar flags; uint c; mprCreate(argc, argv, 0); mprPrintf("static uchar charMatch[256] = {\n\t0x00,"); for (c = 1; c < 256; ++c) { flags = 0; if (c % 16 == 0) mprPrintf("\n\t"); #if BIT_WIN_LIKE if (strchr("&;`'\"|*?~<>^()[]{}$\\\n\r%", c)) { flags |= MPR_ENCODE_SHELL; } #else if (strchr("&;`\'\"|*?~<>^()[]{}$\\\n", c)) { flags |= MPR_ENCODE_SHELL; } #endif if (isalnum((uchar) c) || strchr("-_.~", c)) { /* Acceptable */ } else if (strchr("+", c)) { flags |= MPR_ENCODE_URI_COMPONENT | MPR_ENCODE_JS_URI_COMPONENT; } else if (strchr("#;,/?:@&=+$", c)) { flags |= MPR_ENCODE_URI_COMPONENT | MPR_ENCODE_JS_URI_COMPONENT; } else if (strchr("!'()*", c)) { flags |= MPR_ENCODE_URI | MPR_ENCODE_URI_COMPONENT; } else if (strchr("[]", c)) { flags |= MPR_ENCODE_JS_URI | MPR_ENCODE_URI_COMPONENT | MPR_ENCODE_JS_URI_COMPONENT; } else { flags |= MPR_ENCODE_URI | MPR_ENCODE_URI_COMPONENT | MPR_ENCODE_JS_URI | MPR_ENCODE_JS_URI_COMPONENT; } if (strchr("<>&\"'", c) != 0) { flags |= MPR_ENCODE_HTML; } mprPrintf("0x%02x%c", flags, (c < 255) ? ',' : ' '); } mprPrintf("\n};\n"); return 0; }
/* Run a web server not based on a config file. */ int maRunSimpleWebServer(cchar *ip, int port, cchar *home, cchar *documents) { Mpr *mpr; MaServer *server; MaAppweb *appweb; int rc; /* Initialize and start the portable runtime services. */ rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, 0)) == 0) { mprError("Can't create the web server runtime"); } else { if (mprStart(mpr) < 0) { mprError("Can't start the web server runtime"); } else { if ((appweb = maCreateAppweb(mpr)) == 0) { mprError("Can't create the web server http services"); } else { mprAddRoot(appweb); if ((server = maCreateServer(appweb, 0)) == 0) { mprError("Can't create the web server"); } else { if (maConfigureServer(server, 0, home, documents, ip, port) < 0) { mprError("Can't create the web server"); } else { if (maStartServer(server) < 0) { mprError("Can't start the web server"); } else { mprServiceEvents(-1, 0); rc = 0; } maStopServer(server); } } mprRemoveRoot(appweb); } } mprDestroy(MPR_EXIT_DEFAULT); } return rc; }
/* Create a web server described by a config file. */ int maRunWebServer(cchar *configFile) { Mpr *mpr; MaAppweb *appweb; MaServer *server; int rc; rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, 0)) == 0) { mprError("Can't create the web server runtime"); } else { if (mprStart() < 0) { mprError("Can't start the web server runtime"); } else { if ((appweb = maCreateAppweb(mpr)) == 0) { mprError("Can't create appweb object"); } else { mprAddRoot(appweb); if ((server = maCreateServer(appweb, 0)) == 0) { mprError("Can't create the web server"); } else { if (maParseConfig(server, configFile, 0) < 0) { mprError("Can't parse the config file %s", configFile); } else { if (maStartServer(server) < 0) { mprError("Can't start the web server"); } else { mprServiceEvents(-1, 0); rc = 0; } maStopServer(server); } } mprRemoveRoot(appweb); } } } mprDestroy(MPR_EXIT_DEFAULT); return rc; }
/* One-line embedding. Evaluate a file. This will compile and interpret the given Ejscript source file. */ int ejsEvalFile(cchar *path) { Ejs *ejs; mprCreate(0, 0, 0); if ((ejs = ejsCreateVM(0, 0, 0)) == 0) { mprDestroy(0); return MPR_ERR_MEMORY; } mprAddRoot(ejs); if (ejsLoadModules(ejs, 0, 0) < 0) { mprDestroy(0); return MPR_ERR_CANT_READ; } if (ejsLoadScriptFile(ejs, path, NULL, EC_FLAGS_NO_OUT | EC_FLAGS_DEBUG) < 0) { ejsReportError(ejs, "Error in program"); mprDestroy(0); return MPR_ERR; } mprDestroy(MPR_EXIT_DEFAULT); return 0; }
/* One-line embedding. Evaluate a script. This will compile and interpret the given script. */ int ejsEvalScript(cchar *script) { Ejs *ejs; mprCreate(0, 0, 0); if ((ejs = ejsCreateVM(0, 0, 0)) == 0) { mprDestroy(0); return MPR_ERR_MEMORY; } mprAddRoot(ejs); if (ejsLoadModules(ejs, 0, 0) < 0) { mprDestroy(0); return MPR_ERR_CANT_READ; } if (ejsLoadScriptLiteral(ejs, ejsCreateStringFromAsc(ejs, script), NULL, EC_FLAGS_NO_OUT | EC_FLAGS_DEBUG) < 0) { ejsReportError(ejs, "Error in program"); mprDestroy(0); return MPR_ERR; } mprDestroy(MPR_EXIT_DEFAULT); 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; }
int APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *args, int junk2) { char **argv, *argp; int argc, err, nextArg, manage, stop; mpr = mprCreate(0, NULL, NULL); err = 0; manage = 0; appInst = inst; stop = 0; serviceName = BLD_COMPANY "-" BLD_PRODUCT; serviceTitle = BLD_NAME; serviceWindowName = BLD_PRODUCT "Angel"; serviceWindowTitle = BLD_NAME "Angel"; mprSetAppName(mpr, BLD_PRODUCT "Monitor", BLD_NAME " Monitor", BLD_VERSION); appName = mprGetAppName(mpr); appTitle = mprGetAppTitle(mpr); mprSetLogHandler(mpr, logHandler, NULL); chdir(mprGetAppDir(mpr)); /* * Parse command line arguments */ if (mprMakeArgv(mpr, "", args, &argc, &argv) < 0) { return FALSE; } for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--manage") == 0) { manage++; } else if (strcmp(argp, "--stop") == 0) { stop++; } else { err++; } if (err) { mprUserError(mpr, "Bad command line: %s\n" " Usage: %s [options]\n" " Switches:\n" " --manage # Launch browser to manage", " --stop # Stop a running monitor", args, appName); return -1; } } if (stop) { stopMonitor(); return 0; } if (findInstance(mpr)) { mprUserError(mpr, "Application %s is already active.", mprGetAppTitle(mpr)); return MPR_ERR_BUSY; } /* * Create the window */ if (initWindow(mpr) < 0) { mprUserError(mpr, "Can't initialize application Window"); return MPR_ERR_CANT_INITIALIZE; } if (manage) { /* * Launch the browser */ runBrowser("/index.html"); } else { if (openMonitorIcon() < 0) { mprUserError(mpr, "Can't open %s tray", appName); } else { eventLoop(); closeMonitorIcon(); } } // mprFree(mpr); return 0; }
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 main(int argc, char **argv) { Mpr *mpr; EcCompiler *cp; EjsService *vmService; Ejs *ejs; MprList *useModules, *files; cchar *cmd, *className, *methodName; char *argp, *searchPath, *modules, *name, *tok, *extraFiles, *spec; int nextArg, err, ejsFlags, ecFlags, stats, run, merge, bind, noout, debug, optimizeLevel, nobind, warnLevel; int compilerMode, lang; /* * Create the Embedthis Portable Runtime (MPR) and setup a memory failure handler */ mpr = mprCreate(argc, argv, ejsMemoryFailure); mprSetAppName(mpr, argv[0], 0, 0); if (mprStart(mpr, 0) < 0) { mprError(mpr, "Can't start mpr services"); return EJS_ERR; } err = 0; className = 0; cmd = 0; methodName = 0; searchPath = 0; stats = 0; run = 1; merge = 0; bind = 1; nobind = 0; noout = 1; debug = 1; warnLevel = 1; optimizeLevel = 9; compilerMode = PRAGMA_MODE_STANDARD; lang = BLD_FEATURE_EJS_LANG; useModules = mprCreateList(mpr); files = mprCreateList(mpr); ejsFlags = EJS_FLAG_COMPILER; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--bind") == 0) { /* Ignore. Not required and only here for compatibility with ec */ } else if (strcmp(argp, "--class") == 0) { if (nextArg >= argc) { err++; } else { className = argv[++nextArg]; } } else if (strcmp(argp, "--cmd") == 0 || strcmp(argp, "-c") == 0) { if (nextArg >= argc) { err++; } else { cmd = argv[++nextArg]; } } else if (strcmp(argp, "--debug") == 0) { debug = 1; } else if (strcmp(argp, "--lang") == 0) { if (nextArg >= argc) { err++; } else { spec = argv[++nextArg]; if (mprStrcmpAnyCase(spec, "ecma") == 0) { lang = EJS_SPEC_ECMA; } else if (mprStrcmpAnyCase(spec, "plus") == 0) { lang = EJS_SPEC_PLUS; } else if (mprStrcmpAnyCase(spec, "fixed") == 0) { lang = EJS_SPEC_FIXED; } } } else if (strcmp(argp, "--files") == 0 || strcmp(argp, "-f") == 0) { if (nextArg >= argc) { err++; } else { extraFiles = mprStrdup(mpr, argv[++nextArg]); name = mprStrTok(extraFiles, " \t", &tok); while (name != NULL) { mprAddItem(files, name); name = mprStrTok(NULL, " \t", &tok); } } } else if (strcmp(argp, "--log") == 0) { if (nextArg >= argc) { err++; } else { ejsStartLogging(mpr, argv[++nextArg]); } } else if (strcmp(argp, "--method") == 0) { if (nextArg >= argc) { err++; } else { methodName = argv[++nextArg]; } } else if (strcmp(argp, "--nobind") == 0) { /* * This is a hidden switch just for the compiler developers */ nobind = 1; bind = 0; } else if (strcmp(argp, "--nodebug") == 0) { debug = 0; } else if (strcmp(argp, "--optimize") == 0) { if (nextArg >= argc) { err++; } else { optimizeLevel = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--searchpath") == 0) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (strcmp(argp, "--standard") == 0) { compilerMode = PRAGMA_MODE_STANDARD; } else if (strcmp(argp, "--stats") == 0) { stats = 1; } else if (strcmp(argp, "--strict") == 0) { compilerMode = PRAGMA_MODE_STRICT; } else if (strcmp(argp, "--use") == 0) { if (nextArg >= argc) { err++; } else { modules = mprStrdup(mpr, argv[++nextArg]); name = mprStrTok(modules, " \t", &tok); while (name != NULL) { mprAddItem(useModules, name); name = mprStrTok(NULL, " \t", &tok); } } } else if (strcmp(argp, "--version") == 0 || strcmp(argp, "-V") == 0) { mprErrorPrintf(mpr, "%s %s\n" "Copyright (C) Embedthis Software 2003-2009\n" "Copyright (C) Michael O'Brien 2003-2009\n", BLD_NAME, BLD_VERSION); exit(0); } else if (strcmp(argp, "--warn") == 0) { 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 */ mprErrorPrintf(mpr, "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" " --debug # Include symbolic debugging information in output\n" " --lang # Language compliance (ecma|plus|fixed)\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 in output\n" " --optimize level # Set the optimization level (0-9 default is 9)\n" " --searchpath ejsPath # Module search path\n" " --standard # Default compilation mode to standard (default)\n" " --stats # Print stats on exit\n" " --strict # Default compilation mode to strict\n" " --use 'module, ...' # List of modules to pre-load\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; } vmService = ejsCreateService(mpr); if (vmService == 0) { return MPR_ERR_NO_MEMORY; } if (searchPath) { ejsSetSearchPath(vmService, searchPath); } ejs = ejsCreate(vmService, 0, ejsFlags); if (ejs == 0) { return MPR_ERR_NO_MEMORY; } ecFlags = 0; ecFlags |= (run) ? EC_FLAGS_RUN: 0; ecFlags |= (merge) ? EC_FLAGS_MERGE: 0; ecFlags |= (bind) ? EC_FLAGS_BIND_GLOBALS: 0; ecFlags |= (nobind) ? EC_FLAGS_NO_BIND: 0; ecFlags |= (noout) ? EC_FLAGS_NO_OUT: 0; ecFlags |= (debug) ? EC_FLAGS_DEBUG: 0; cp = ecCreateCompiler(ejs, ecFlags, lang); if (cp == 0) { return MPR_ERR_NO_MEMORY; } ecSetOptimizeLevel(cp, optimizeLevel); ecSetWarnLevel(cp, warnLevel); ecSetDefaultMode(cp, compilerMode); if (preloadModules(cp, useModules) < 0) { return EJS_ERR; } if (cmd) { if (interpretCommands(cp, cmd) < 0) { err++; } } else if (nextArg < argc) { name = argv[nextArg]; mprAddItem(files, argv[nextArg]); if (interpretFiles(cp, files, argc - nextArg, &argv[nextArg], className, methodName) < 0) { err++; } } else { /* * No args - run as an interactive shell */ if (interpretCommands(cp, NULL) < 0) { err++; } } #if BLD_DEBUG if (stats) { mprSetLogLevel(ejs, 1); ejsPrintAllocReport(ejs); } #endif mprFree(mpr); return err; }
MAIN(appweb, int argc, char **argv) { Mpr *mpr; MaHttp *http; cchar *ipAddrPort, *documentRoot, *argp, *logSpec; char *configFile, *ipAddr, *homeDir, *timeText, *ejsPrefix, *ejsPath, *changeRoot; int workers, outputVersion, argind, port; documentRoot = 0; changeRoot = ejsPrefix = ejsPath = 0; ipAddrPort = 0; ipAddr = 0; port = -1; logSpec = 0; server = 0; outputVersion = 0; workers = -1; configFile = BLD_FEATURE_CONFIG_FILE; homeDir = BLD_FEATURE_SERVER_ROOT; mpr = mprCreate(argc, argv, memoryFailure); argc = mpr->argc; argv = mpr->argv; #if BLD_FEATURE_ROMFS extern MprRomInode romFiles[]; mprSetRomFileSystem(mpr, romFiles); #endif if (osInit(mpr) < 0) { exit(2); } if (mprStart(mpr, 0) < 0) { mprUserError(mpr, "Can't start MPR for %s", mprGetAppName(mpr)); mprFree(mpr); return MPR_ERR_CANT_INITIALIZE; } for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (strcmp(argp, "--config") == 0) { if (argind >= argc) { return printUsage(mpr); } configFile = argv[++argind]; #if BLD_UNIX_LIKE } else if (strcmp(argp, "--chroot") == 0) { if (argind >= argc) { return printUsage(mpr); } changeRoot = mprGetAbsPath(mpr, argv[++argind]); #endif } else if (strcmp(argp, "--debug") == 0 || strcmp(argp, "-D") == 0 || strcmp(argp, "-d") == 0 || strcmp(argp, "--debugger") == 0) { mprSetDebugMode(mpr, 1); } else if (strcmp(argp, "--ejs") == 0) { if (argind >= argc) { return printUsage(mpr); } ejsPrefix = mprStrdup(mpr, argv[++argind]); if ((ejsPath = strchr(ejsPrefix, ':')) != 0) { *ejsPath++ = '\0'; } ejsPath = mprGetAbsPath(mpr, ejsPath); } else if (strcmp(argp, "--home") == 0) { if (argind >= argc) { return printUsage(mpr); } homeDir = mprGetAbsPath(mpr, argv[++argind]); if (chdir((char*) homeDir) < 0) { mprPrintfError(mpr, "%s: Can't change directory to %s\n", mprGetAppName(mpr), homeDir); exit(2); } } else if (strcmp(argp, "--log") == 0 || strcmp(argp, "-l") == 0) { if (argind >= argc) { return printUsage(mpr); } logSpec = argv[++argind]; maStartLogging(mpr, logSpec); } else if (strcmp(argp, "--name") == 0 || strcmp(argp, "-n") == 0) { if (argind >= argc) { return printUsage(mpr); } mprSetAppName(mpr, argv[++argind], 0, 0); } else if (strcmp(argp, "--threads") == 0) { if (argind >= argc) { return printUsage(mpr); } workers = atoi(argv[++argind]); } else if (strcmp(argp, "--verbose") == 0 || strcmp(argp, "-v") == 0) { maStartLogging(mpr, "stdout:2"); } else if (strcmp(argp, "--version") == 0 || strcmp(argp, "-V") == 0) { outputVersion++; } else { mprPrintfError(mpr, "Unknown switch \"%s\"\n", argp); printUsage(mpr); exit(2); } } if (argc > argind) { if (argc > (argind + 2)) { return printUsage(mpr); } ipAddrPort = argv[argind++]; if (argc > argind) { documentRoot = argv[argind++]; } else { documentRoot = "."; } } if (outputVersion) { mprPrintf(mpr, "%s %s-%s\n", mprGetAppTitle(mpr), BLD_VERSION, BLD_NUMBER); exit(0); } if (ipAddrPort) { mprParseIp(mpr, ipAddrPort, &ipAddr, &port, MA_SERVER_DEFAULT_PORT_NUM); } else { #if BLD_FEATURE_CONFIG_PARSE if (configFile == 0) { configFile = mprStrcat(mpr, -1, mprGetAppName(mpr), ".conf", NULL); } if (!mprPathExists(mpr, configFile, R_OK)) { mprPrintfError(mpr, "Can't open config file %s\n", configFile); exit(2); } #else return printUsage(mpr); #endif #if !BLD_FEATURE_ROMFS if (homeDir == 0) { homeDir = mprGetPathParent(mpr, configFile); if (chdir((char*) homeDir) < 0) { mprPrintfError(mpr, "%s: Can't change directory to %s\n", mprGetAppName(mpr), homeDir); exit(2); } } #endif } homeDir = mprGetCurrentPath(mpr); if (checkEnvironment(mpr, argv[0], homeDir) < 0) { exit(3); } #if BLD_UNIX_LIKE if (changeRoot) { homeDir = mprGetAbsPath(mpr, changeRoot); if (chdir(homeDir) < 0) { mprError(mpr, "%s: Can't change directory to %s", homeDir); exit(4); } if (chroot(homeDir) < 0) { if (errno == EPERM) { mprError(mpr, "%s: Must be super user to use the --chroot option", mprGetAppName(mpr)); } else { mprError(mpr, "%s: Can't change change root directory to %s, errno %d", mprGetAppName(mpr), homeDir, errno); } exit(5); } } #endif /* * Create the top level HTTP service and default HTTP server. Set the initial server root to "." */ http = maCreateHttp(mpr); if (http == 0) { mprUserError(mpr, "Can't create HTTP service for %s", mprGetAppName(mpr)); return MPR_ERR_CANT_INITIALIZE; } server = maCreateServer(http, "default", ".", 0, -1); if (server == 0) { mprUserError(mpr, "Can't create HTTP server for %s", mprGetAppName(mpr)); return MPR_ERR_CANT_INITIALIZE; } if (maConfigureServer(mpr, http, server, configFile, ipAddr, port, documentRoot) < 0) { /* mprUserError(mpr, "Can't configure the server, exiting."); */ exit(6); } if (mpr->ipAddr) { mprLog(mpr, 2, "Server IP address %s", mpr->ipAddr); } timeText = mprFormatLocalTime(mpr, mprGetTime(mpr)); mprLog(mpr, 1, "Started at %s", timeText); mprFree(timeText); #if BLD_FEATURE_EJS if (ejsPrefix) { createEjsAlias(mpr, http, server, ejsPrefix, ejsPath); } #endif #if BLD_FEATURE_MULTITHREAD if (workers >= 0) { mprSetMaxWorkers(http, workers); } #endif #if BLD_WIN_LIKE if (!ejsPrefix) { writePort(server->defaultHost); } #endif if (maStartHttp(http) < 0) { mprUserError(mpr, "Can't start HTTP service, exiting."); exit(7); } #if BLD_FEATURE_MULTITHREAD mprLog(mpr, 1, "HTTP services are ready with max %d worker threads", mprGetMaxWorkers(mpr)); #else mprLog(mpr, 1, "HTTP services are ready (single-threaded)"); #endif /* * Service I/O events until instructed to exit */ while (!mprIsExiting(mpr)) { mprServiceEvents(mpr->dispatcher, -1, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); } /* * Signal a graceful shutdown */ mprLog(http, 1, "Exiting ..."); maStopHttp(http); mprLog(http, 1, "Exit complete"); #if VXWORKS if (mprStop(mpr)) { mprFree(mpr); } #endif return 0; }
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; }
int main(int argc, char **argv) { cchar *searchPath, *argp, *program; int nextArg, err; /* * Create the Embedthis Portable Runtime (MPR) and setup a memory failure handler */ mpr = mprCreate(argc, argv, ejsMemoryFailure); program = mprGetBaseName(argv[0]); mprSetAppName(mpr, program, 0, 0); if (strcmp(program, "ejscgi-debug") == 0) { debug++; } if (mprStart(mpr, 0) < 0) { mprError(mpr, "Can't start mpr services"); return EJS_ERR; } for (err = 0, nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--debug") == 0 || strcmp(argp, "-d") == 0) { debug++; #if BLD_DEBUG } else if (strcmp(argp, "--dummy") == 0) { dummy++; #endif } else if (strcmp(argp, "--log") == 0 || strcmp(argp, "-l") == 0) { if (nextArg >= argc) { err++; } else { ejsStartLogging(mpr, argv[++nextArg]); } } else if (strcmp(argp, "--searchpath") == 0) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (strcmp(argp, "--version") == 0 || strcmp(argp, "-V") == 0) { mprErrorPrintf(mpr, "%s %s\n" "Copyright (C) Embedthis Software 2003-2009\n" "Copyright (C) Michael O'Brien 2003-2009\n", BLD_NAME, BLD_VERSION); exit(0); } else { err++; break; } } if (err) { mprErrorPrintf(mpr, "Usage: %s [options]\n" " Options:\n" " --log logSpec # Diagnostic trace\n" " --searchpath ejsPath # Module search path\n" " --version # Emit the program version information\n\n", mpr->name); return -1; } if (initControlBlock() < 0) { error(NULL, 0, "Can't initialize control block"); exit(1); } // FAST CGI must update this currentDate = getDateString(0); if (getRequest() < 0) { error(NULL, 0, "Can't get request"); } else { processRequest(); } if (responseCode && responseMsg) { fprintf(stderr, "ejscgi: ERROR: %s\n", responseMsg); } return 0; }