Example #1
0
/*
 *  Called to rotate the access log
 */
void maRotateAccessLog(MaHost *host)
{
    MprFileInfo     info;
    struct tm       tm;
    MprTime         when;
    char            bak[MPR_MAX_FNAME];

    /*
     *  Rotate logs when full
     */
    if (mprGetFileInfo(host, host->logPath, &info) == 0 && info.size > MA_MAX_ACCESS_LOG) {

        when = mprGetTime(host);
        mprGmtime(host, &tm, when);

        mprSprintf(bak, sizeof(bak), "%s-%02d-%02d-%02d-%02d:%02d:%02d", host->logPath, 
            tm.tm_mon, tm.tm_mday, tm.tm_year, tm.tm_hour, tm.tm_min, tm.tm_sec);

        mprFree(host->accessLog);
        rename(host->logPath, bak);
        unlink(host->logPath);

        host->accessLog = mprOpen(host, host->logPath, O_CREAT | O_TRUNC | O_WRONLY | O_TEXT, 0664);
    }
}
Example #2
0
int maWriteUserFile(MaServer *server, MaAuth *auth, char *path)
{
    MprFile         *file;
    MprHash         *hp;
    MaUser          *up;
    char            buf[MA_MAX_PASS * 2];
    char            *tempFile;

    tempFile = mprGetTempPath(auth, NULL);
    if ((file = mprOpen(auth, tempFile, O_CREAT | O_TRUNC | O_WRONLY | O_TEXT, 0444)) == 0) {
        mprError(server, "Can't open %s", tempFile);
        mprFree(tempFile);
        return MPR_ERR_CANT_OPEN;
    }
    mprFree(tempFile);

    hp = mprGetNextHash(auth->users, 0);
    while (hp) {
        up = (MaUser*) hp->data;
        mprSprintf(buf, sizeof(buf), "%d: %s: %s: %s\n", up->enabled, up->name, up->realm, up->password);
        mprWrite(file, buf, (int) strlen(buf));
        hp = mprGetNextHash(auth->users, hp);
    }

    mprFree(file);

    unlink(path);
    if (rename(tempFile, path) < 0) {
        mprError(server, "Can't create new %s", path);
        return MPR_ERR_CANT_WRITE;
    }
    return 0;
}
Example #3
0
static int writeBody(MprHttp *http, MprList *fields, MprList *files)
{
    MprFile     *file;
    char        buf[MPR_HTTP_BUFSIZE], *path, *pair;
    int         bytes, next, count, rc, len;

    rc = 0;

    mprSetSocketBlockingMode(http->sock, 1);
    if (upload) {
        if (mprWriteHttpUploadData(http, files, fields) < 0) {
            mprError(http, "Can't write upload data %s", mprGetHttpError(http));
            mprSetSocketBlockingMode(http->sock, 0);
            return MPR_ERR_CANT_WRITE;
        }
    } else {
        if (fields) {
            count = mprGetListCount(fields);
            for (next = 0; !rc && (pair = mprGetNextItem(fields, &next)) != 0; ) {
                len = (int) strlen(pair);
                if (next < count) {
                    len = (int) strlen(pair);
                    if (mprWriteSocket(http->sock, pair, len) != len || mprWriteSocket(http->sock, "&", 1) != 1) {
                        return MPR_ERR_CANT_WRITE;
                    }
                } else {
                    if (mprWriteSocket(http->sock, pair, len) != len) {
                        return MPR_ERR_CANT_WRITE;
                    }
                }
            }
        }
        if (files) {
            mprAssert(mprGetListCount(files) == 1);
            for (rc = next = 0; !rc && (path = mprGetNextItem(files, &next)) != 0; ) {
                file = mprOpen(http, path, O_RDONLY | O_BINARY, 0);
                if (file == 0) {
                    mprError(http, "Can't open \"%s\"", path);
                    return MPR_ERR_CANT_OPEN;
                }
                if (verbose && upload) {
                    mprPrintf(http, "uploading: %s\n", path);
                }
                while ((bytes = mprRead(file, buf, sizeof(buf))) > 0) {
                    if (mprWriteHttp(http, buf, bytes) != bytes) {
                        mprFree(file);
                        return MPR_ERR_CANT_WRITE;
                    }
                }
                mprFree(file);
            }
        }
        if (mprFinalizeHttpWriting(http) < 0) {
            return MPR_ERR_CANT_WRITE;
        }
    }
    mprSetSocketBlockingMode(http->sock, 0);
    return rc;
}
Example #4
0
int maStartAccessLogging(MaHost *host)
{
#if !BLD_FEATURE_ROMFS
    if (host->logPath) {
        host->accessLog = mprOpen(host, host->logPath, O_CREAT | O_APPEND | O_WRONLY | O_TEXT, 0664);
        if (host->accessLog == 0) {
            mprError(host, "Can't open log file %s", host->logPath);
        }
    }
#endif
    return 0;
}
Example #5
0
/*
 *  Start error and information logging. Note: this is not per-request access logging
 */
int maStartLogging(MprCtx ctx, cchar *logSpec)
{
    Mpr         *mpr;
    MprFile     *file;
    MprTime     now;
    char        *levelSpec, *spec, timeText[80];
    int         level;

    level = 0;
    mpr = mprGetMpr(ctx);

    if (logSpec == 0) {
        logSpec = "stdout:2";
    }
    if (*logSpec && strcmp(logSpec, "none") != 0) {
        spec = mprStrdup(mpr, logSpec);
        if ((levelSpec = strrchr(spec, ':')) != 0 && isdigit((int) levelSpec[1])) {
            *levelSpec++ = '\0';
            level = atoi(levelSpec);
        }

        if (strcmp(spec, "stdout") == 0) {
            file = mpr->fileService->console;

        } else {
            if ((file = mprOpen(mpr, spec, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) {
                mprErrorPrintf(mpr, "Can't open log file %s\n", spec);
                return -1;
            }
        }
        mprSetLogLevel(mpr, level);
        mprSetLogHandler(mpr, logHandler, (void*) file);

        now = mprGetTime(mpr);
        mprCtime(mpr, timeText, sizeof(timeText), now);

        mprLog(mpr, MPR_CONFIG, "Configuration for %s", mprGetAppTitle(mpr));
        mprLog(mpr, MPR_CONFIG, "--------------------------------------------");
        mprLog(mpr, MPR_CONFIG, "Host:               %s", mprGetHostName(mpr));
        mprLog(mpr, MPR_CONFIG, "CPU:                %s", BLD_HOST_CPU);
        mprLog(mpr, MPR_CONFIG, "OS:                 %s", BLD_HOST_OS);
        mprLog(mpr, MPR_CONFIG, "Distribution:       %s %s", BLD_HOST_DIST, BLD_HOST_DIST_VER);
        mprLog(mpr, MPR_CONFIG, "OS:                 %s", BLD_HOST_OS);
        mprLog(mpr, MPR_CONFIG, "Version:            %s.%d", BLD_VERSION, BLD_NUMBER);
        mprLog(mpr, MPR_CONFIG, "BuildType:          %s", BLD_TYPE);
        mprLog(mpr, MPR_CONFIG, "Started at:         %s", timeText);
        mprLog(mpr, MPR_CONFIG, "--------------------------------------------");
    }

    return 0;
}
Example #6
0
/*
 *  Invoked to initialize the send connector for a request
 */
static void sendOpen(MaQueue *q)
{
    MaConn          *conn;
    MaResponse      *resp;

    conn = q->conn;
    resp = conn->response;

    if (!conn->requestFailed && !(resp->flags & MA_RESP_NO_BODY)) {
        resp->file = mprOpen(q, resp->filename, O_RDONLY | O_BINARY, 0);
        if (resp->file == 0) {
            maFailRequest(conn, MPR_HTTP_CODE_NOT_FOUND, "Can't open document: %s", resp->filename);
        }
    }
}
Example #7
0
/*
 *  Start error and information logging. Note: this is not per-request access logging
 */
int maStartLogging(MprCtx ctx, cchar *logSpec)
{
    Mpr         *mpr;
    MprFile     *file;
    char        *levelSpec, *spec;
    int         level;

    level = 0;
    mpr = mprGetMpr(ctx);

    if (logSpec == 0) {
        logSpec = "stdout:0";
    }
    if (*logSpec && strcmp(logSpec, "none") != 0) {
        spec = mprStrdup(mpr, logSpec);
        if ((levelSpec = strrchr(spec, ':')) != 0 && isdigit((int) levelSpec[1])) {
            *levelSpec++ = '\0';
            level = atoi(levelSpec);
        }

        if (strcmp(spec, "stdout") == 0) {
            file = mpr->fileSystem->stdOutput;
        } else {
            if ((file = mprOpen(mpr, spec, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) {
                mprPrintfError(mpr, "Can't open log file %s\n", spec);
                return -1;
            }
        }
        mprSetLogLevel(mpr, level);
        mprSetLogHandler(mpr, logHandler, (void*) file);

        mprLog(mpr, MPR_CONFIG, "Configuration for %s", mprGetAppTitle(mpr));
        mprLog(mpr, MPR_CONFIG, "---------------------------------------------");
        mprLog(mpr, MPR_CONFIG, "Host:               %s", mprGetHostName(mpr));
        mprLog(mpr, MPR_CONFIG, "CPU:                %s", BLD_CPU);
        mprLog(mpr, MPR_CONFIG, "OS:                 %s", BLD_OS);
        if (strcmp(BLD_DIST, "Unknown") != 0) {
            mprLog(mpr, MPR_CONFIG, "Distribution:       %s %s", BLD_DIST, BLD_DIST_VER);
        }
        mprLog(mpr, MPR_CONFIG, "Version:            %s-%s", BLD_VERSION, BLD_NUMBER);
        mprLog(mpr, MPR_CONFIG, "BuildType:          %s", BLD_TYPE);
        mprLog(mpr, MPR_CONFIG, "---------------------------------------------");
    }
    return 0;
}
Example #8
0
/*
 *  Called to rotate the access log
 */
void maRotateAccessLog(MaHost *host)
{
    MprPath         info;
    struct tm       tm;
    MprTime         when;
    char            bak[MPR_MAX_FNAME];

    if (mprGetPathInfo(host, host->logPath, &info) == 0) {
        when = mprGetTime(host);
        mprDecodeUniversalTime(host, &tm, when);
        mprSprintf(bak, sizeof(bak), "%s-%02d-%02d-%02d-%02d:%02d:%02d", host->logPath, 
            tm.tm_mon, tm.tm_mday, tm.tm_year, tm.tm_hour, tm.tm_min, tm.tm_sec);
        mprFree(host->accessLog);
        rename(host->logPath, bak);
        unlink(host->logPath);
        host->accessLog = mprOpen(host, host->logPath, O_CREAT | O_TRUNC | O_WRONLY | O_TEXT, 0664);
    }
}
Example #9
0
int mprMakeTempFileName(MprCtx ctx, char *buf, int bufsize, const char *tempDir)
{
	MprFile		*file;
	MprTime		now;
	char		*dir;
	int 		seed, i;

	if (tempDir == 0) {
#if WIN
		char	*cp;
		dir = mprStrdup(ctx, getenv("TEMP"));
		for (cp = dir; *cp; cp++) {
			if (*cp == '\\') {
				*cp = '/';
			}
		}
#else
		dir = mprStrdup(ctx, "/tmp");
#endif
	} else {
		dir = mprStrdup(ctx, tempDir);
	}

	mprGetTime(ctx, &now);
	seed = now.msec % 64000;
	file = 0;

	for (i = 0; i < 128; i++) {
		mprSprintf(buf, bufsize, "%s/MPR_%d_%d.tmp", dir, getpid(), seed++);
		file = mprOpen(ctx, buf, O_CREAT | O_EXCL | O_BINARY, 0664);
		if (file) {
			break;
		}
	}

	if (file == 0) {
		return MPR_ERR_CANT_CREATE;
	}

	mprClose(file);
	mprFree(dir);

	return 0;
}
Example #10
0
static void copyFile(cchar *url)
{
    MprFile     *file;
    char        path[MPR_MAX_FNAME], buf[MPR_BUFSIZE], *ext;
    int         len;

    ext = strrchr(url, '.');
    if (ext) {
        ++ext;
        if (strcmp(ext, "htm") == 0 || strcmp(ext, "html") == 0) {
            setHeader(NULL, 0, "Content-Type", "text/html");
        } else if (strcmp(ext, "jpg") == 0 || strcmp(ext, "jpeg") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/jpeg");
        } else if (strcmp(ext, "png") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/png");
        } else if (strcmp(ext, "gif") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/gif");
        } else if (strcmp(ext, "tiff") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/tiff");
        } else if (strcmp(ext, "ico") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/x-ico");
        } else if (strcmp(ext, "bmp") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/bmp");
        } else if (strcmp(ext, "pdf") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/pdf");
        } else if (strcmp(ext, "txt") == 0) {
            setHeader(NULL, 0, "Content-Type", "text/plain");
        } else if (strcmp(ext, "css") == 0) {
            setHeader(NULL, 0, "Content-Type", "text/css");
        }
    }
    mprSprintf(path, sizeof(path), "%s/%s", documentRoot, url);
    file = mprOpen(mpr, path, O_RDONLY, 0);
    if (file == 0) {
        error(NULL, 0, "Can't open %s", path);
        return;
    }
    while ((len = mprRead(file, buf, sizeof(buf))) > 0) {
        if (writeBlock(web, buf, len) != len) {
            error(NULL, 0, "Can't write data from %s", path);
            return;
        }
    }
}
Example #11
0
static int load(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	const char	*fileName;
	XmlState	*parser;
	Exml		*xp;
	MprFile		*file;

	if (argc != 1 || !ejsVarIsString(argv[0])) {
		ejsError(ep, EJS_ARG_ERROR, "Bad args. Usage: load(fileName);");
		return -1;
	}
	fileName = argv[0]->string;
	
	/* FUTURE -- not romable 
		Need rom code in MPR not MprServices
	*/
	file = mprOpen(ep, fileName, O_RDONLY, 0664);
	if (file == 0) {
		ejsError(ep, EJS_IO_ERROR, "Can't open: %s", fileName);
		return -1;
	}

	xp = initParser(ep, thisObj, fileName);
	parser = exmlGetParseArg(xp);

	exmlSetInputStream(xp, readFileData, (void*) file);

	if (exmlParse(xp) < 0) {
		if (! ejsGotException(ep)) {
			ejsError(ep, EJS_IO_ERROR, "Can't parse XML file: %s\nDetails %s", 
				fileName, exmlGetErrorMsg(xp));
		}
		termParser(xp);
		mprClose(file);
		return -1;
	}

	ejsSetReturnValue(ep, parser->nodeStack[0].obj);

	termParser(xp);
	mprClose(file);

	return 0;
}
Example #12
0
/*
 *  Read the "fileList" which should be a file containing a list of filenames, and create an MprList of those filenames
 */
static int makeFileList(Mpr *mpr, MprList *files, char *fileList)
{
    MprFile     *file;
    char        path[MPR_MAX_FNAME];
    char        *p;

    if ((file = mprOpen(mpr, fileList, O_RDONLY | O_TEXT, 0)) == NULL) {
        mprError(mpr, "Can't open file list %s", fileList);
        return MPR_ERR_CANT_OPEN;
    }

    while (mprGets(file, path, sizeof(path)) != NULL) {
        if ((p = strchr(path, '\n')) || (p = strchr(path, '\r'))) {
            *p = '\0';
        }
        if (*path == '\0') {
            continue;
        }
        mprAddItem(files, mprStrdup(files, path));
    }
    return 0;
}
Example #13
0
int maReadGroupFile(MaServer *server, MaAuth *auth, char *path)
{
    MprFile     *file;
    MaAcl       acl;
    char        buf[MPR_MAX_STRING];
    char        *users, *group, *enabled, *aclSpec, *tok, *cp;

    mprFree(auth->groupFile);
    auth->groupFile = mprStrdup(server, path);

    if ((file = mprOpen(auth, path, O_RDONLY | O_TEXT, 0444)) == 0) {
        return MPR_ERR_CANT_OPEN;
    }

    while (mprGets(file, buf, sizeof(buf))) {
        enabled = mprStrTok(buf, " :\t", &tok);
        if (!enabled) {
            continue;
        }
        for (cp = enabled; isspace((int) *cp); cp++) {
            ;
        }
        if (*cp == '\0' || *cp == '#') {
            continue;
        }
        aclSpec = mprStrTok(0, " :\t", &tok);
        group = mprStrTok(0, " :\t", &tok);
        users = mprStrTok(0, "\r\n", &tok);

        acl = maParseAcl(auth, aclSpec);
        maAddGroup(auth, group, acl, (*enabled == '0') ? 0 : 1);
        maAddUsersToGroup(auth, group, users);
    }
    mprFree(file);

    maUpdateUserAcls(auth);
    return 0;
}
Example #14
0
int maReadUserFile(MaServer *server, MaAuth *auth, char *path)
{
    MprFile     *file;
    char        buf[MPR_MAX_STRING];
    char        *enabled, *user, *password, *realm, *tok, *cp;

    mprFree(auth->userFile);
    auth->userFile = mprStrdup(auth, path);

    if ((file = mprOpen(auth, path, O_RDONLY | O_TEXT, 0444)) == 0) {
        return MPR_ERR_CANT_OPEN;
    }

    while (mprGets(file, buf, sizeof(buf))) {
        enabled = mprStrTok(buf, " :\t", &tok);
        if (!enabled) {
            continue;
        }
        for (cp = enabled; isspace((int) *cp); cp++) {
            ;
        }
        if (*cp == '\0' || *cp == '#') {
            continue;
        }
        user = mprStrTok(0, ":", &tok);
        realm = mprStrTok(0, ":", &tok);
        password = mprStrTok(0, " \t\r\n", &tok);

        user = trimWhiteSpace(user);
        realm = trimWhiteSpace(realm);
        password = trimWhiteSpace(password);

        maAddUser(auth, realm, user, password, (*enabled == '0' ? 0 : 1));
    }
    mprFree(file);
    maUpdateUserAcls(auth);
    return 0;
}
Example #15
0
int maWriteGroupFile(MaServer *server, MaAuth *auth, char *path)
{
    MprHash         *hp;
    MprFile         *file;
    MaGroup         *gp;
    char            buf[MPR_MAX_STRING], *tempFile, *name;
    int             next;

    tempFile = mprGetTempPath(server, NULL);
    if ((file = mprOpen(auth, tempFile, O_CREAT | O_TRUNC | O_WRONLY | O_TEXT, 0444)) == 0) {
        mprError(server, "Can't open %s", tempFile);
        mprFree(tempFile);
        return MPR_ERR_CANT_OPEN;
    }
    mprFree(tempFile);

    hp = mprGetNextHash(auth->groups, 0);
    while (hp) {
        gp = (MaGroup*) hp->data;
        mprSprintf(buf, sizeof(buf), "%d: %x: %s: ", gp->enabled, gp->acl, gp->name);
        mprWrite(file, buf, (int) strlen(buf));
        for (next = 0; (name = mprGetNextItem(gp->users, &next)) != 0; ) {
            mprWrite(file, name, (int) strlen(name));
        }
        mprWrite(file, "\n", 1);
        hp = mprGetNextHash(auth->groups, hp);
    }
    mprFree(file);

    unlink(path);
    if (rename(tempFile, path) < 0) {
        mprError(server, "Can't create new %s", path);
        return MPR_ERR_CANT_WRITE;
    }
    return 0;
}
Example #16
0
static int startLogging(Mpr *mpr, char *logSpec)
{
    MprFile     *file;
    char        *levelSpec;
    int         level;

    level = 0;

    if ((levelSpec = strchr(logSpec, ':')) != 0) {
        *levelSpec++ = '\0';
        level = atoi(levelSpec);
    }
    if (strcmp(logSpec, "stdout") == 0) {
        file = mpr->fileSystem->stdOutput;
    } else {
        if ((file = mprOpen(mpr, logSpec, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) {
            mprPrintfError(mpr, "Can't open log file %s\n", logSpec);
            return -1;
        }
    }
    mprSetLogLevel(mpr, level);
    mprSetLogHandler(mpr, logHandler, (void*) file);
    return 0;
}
Example #17
0
int ecOpenFileStream(EcLexer *lp, const char *path)
{
    EcFileStream    *fs;
    MprFileInfo     info;
    int             c;

    fs = mprAllocObjZeroed(lp->input, EcFileStream);
    if (fs == 0) {
        return MPR_ERR_NO_MEMORY;
    }

    if ((fs->file = mprOpen(lp, path, O_RDONLY | O_BINARY, 0666)) == 0) {
        mprFree(fs);
        return MPR_ERR_CANT_OPEN;
    }

    if (mprGetFileInfo(fs, path, &info) < 0 || info.size < 0) {
        mprFree(fs);
        return MPR_ERR_CANT_ACCESS;
    }

    /* Sanity check */
    mprAssert(info.size < (10 * 1024 * 1024));
    mprAssert(info.size >= 0);

    fs->stream.buf = (char*) mprAlloc(fs, (int) info.size + 1);
    if (fs->stream.buf == 0) {
        mprFree(fs);
        return MPR_ERR_NO_MEMORY;
    }
    if (mprRead(fs->file, fs->stream.buf, (int) info.size) != (int) info.size) {
        mprFree(fs);
        return MPR_ERR_CANT_READ;
    }

    fs->stream.buf[info.size] = '\0';
    fs->stream.nextChar = fs->stream.buf;
    fs->stream.end = &fs->stream.buf[info.size];
    fs->stream.currentLine = fs->stream.buf;
    fs->stream.lineNumber = 1;
    fs->stream.compiler = lp->compiler;

    fs->stream.name = mprStrdup(lp, path);

    mprFree(lp->input->stream);
    lp->input->stream = (EcStream*) fs;

    lp->input->putBack = 0;
    lp->input->token = 0;
    lp->input->state = 0;
    lp->input->next = 0;

    /*
     *  Initialize the stream line and column data.
     */
    c = getNextChar(&fs->stream);
    putBackChar(&fs->stream, c);



    return 0;
}
Example #18
0
/*
    If the program has a UNIX style "#!/program" string at the start of the file that program will be selected 
    and the original program will be passed as the first arg to that program with argv[] appended after that. If 
    the program is not found, this routine supports a safe intelligent search for the command. If all else fails, 
    we just return in program the fileName we were passed in. script will be set if we are modifying the program 
    to run and we have extracted the name of the file to run as a script.
 */
static void findExecutable(MaConn *conn, char **program, char **script, char **bangScript, char *fileName)
{
    MaRequest       *req;
    MaResponse      *resp;
    MaLocation      *location;
    MprHash         *hp;
    MprFile         *file;
    cchar           *actionProgram, *ext, *cmdShell;
    char            *tok, buf[MPR_MAX_FNAME + 1], *path;

    req = conn->request;
    resp = conn->response;
    location = req->location;

    *bangScript = 0;
    *script = 0;
    *program = 0;
    path = 0;

    actionProgram = maGetMimeActionProgram(conn->host, req->mimeType);
    ext = resp->extension;

    /*
        If not found, go looking for the fileName with the extensions defined in appweb.conf. 
        NOTE: we don't use PATH deliberately!!!
     */
    if (access(fileName, X_OK) < 0 /* && *ext == '\0' */) {
        for (hp = 0; (hp = mprGetNextHash(location->extensions, hp)) != 0; ) {
            path = mprStrcat(resp, -1, fileName, ".", hp->key, NULL);
            if (access(path, X_OK) == 0) {
                break;
            }
            mprFree(path);
            path = 0;
        }
        if (hp) {
            ext = hp->key;
        } else {
            path = fileName;
        }

    } else {
        path = fileName;
    }
    mprAssert(path && *path);

#if BLD_WIN_LIKE
    if (ext && (strcmp(ext, ".bat") == 0 || strcmp(ext, ".cmd") == 0)) {
        /*
            Let a mime action override COMSPEC
         */
        if (actionProgram) {
            cmdShell = actionProgram;
        } else {
            cmdShell = getenv("COMSPEC");
        }
        if (cmdShell == 0) {
            cmdShell = "cmd.exe";
        }
        *script = mprStrdup(resp, path);
        *program = mprStrdup(resp, cmdShell);
        return;
    }
#endif

    if ((file = mprOpen(resp, path, O_RDONLY, 0)) != 0) {
        if (mprRead(file, buf, MPR_MAX_FNAME) > 0) {
            mprFree(file);
            buf[MPR_MAX_FNAME] = '\0';
            if (buf[0] == '#' && buf[1] == '!') {
                cmdShell = mprStrTok(&buf[2], " \t\r\n", &tok);
                if (cmdShell[0] != '/' && (cmdShell[0] != '\0' && cmdShell[1] != ':')) {
                    /*
                        If we can't access the command shell and the command is not an absolute path, 
                        look in the same directory as the script.
                     */
                    if (mprPathExists(resp, cmdShell, X_OK)) {
                        cmdShell = mprJoinPath(resp, mprGetPathDir(resp, path), cmdShell);
                    }
                }
                if (actionProgram) {
                    *program = mprStrdup(resp, actionProgram);
                } else {
                    *program = mprStrdup(resp, cmdShell);
                }
                *bangScript = mprStrdup(resp, path);
                return;
            }
        } else {
            mprFree(file);
        }
    }

    if (actionProgram) {
        *program = mprStrdup(resp, actionProgram);
        *bangScript = mprStrdup(resp, path);
    } else {
        *program = mprStrdup(resp, path);
    }
    return;
}
Example #19
0
MAIN(runProgramMain, int argc, char* argv[])
{
    Mpr     *mpr;
    MprFile *out;
    char    buf[256], *ep;
    int     i, len, exitCode, sofar;

    mpr = mprCreate(argc, argv, NULL);

#if TRACE_PROGRESS
    MprFile *f = mprOpen(mpr, "/tmp/r.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
    mprWriteFormat(f, "runProgram: argc %d\n", argc);
    for (i = 0; i < argc; i++) {
        mprWriteFormat(f, "runProgram: arg[%d] = %s\n", i, argv[i]);
    }
    mprFree(f);
#endif

    if (argc < 2) {
        mprPrintfError(mpr, "Usage: runProgram exitCode args...\n");
        exit(3);
    }
    exitCode = atoi(argv[1]);
    out = mprGetStdout(mpr);

    if (exitCode != 99) {
        /*
         *  Echo the args
         */
        for (i = 2; i < argc; ) {
            mprPuts(out, argv[i]);
            if (++i != argc) {
                mprPutc(out, ' ');
            }
        }
        mprPutc(out, '\n');

        /*
         *  Echo the CMD_ENV environment variable value
         */
        ep = getenv("CMD_ENV");
        if (ep) {
            mprPuts(out, "CMD_ENV=");
            mprPuts(out, ep);
        } else {
            mprPuts(out, "Can't find CMD_ENV");
        }
        mprPutc(out, '\n');
        mprFlush(out);
    }

    /*
     *  Read the input
     */
    sofar = 0;
    while ((len = (int) read(0, buf, sizeof(buf))) > 0) {
        sofar += (int) write(1, buf, len);
    }
    if (exitCode != 99) {
        mprPuts(out, "END");
        mprPutc(out, '\n');
    }
    mprFlush(out);
    return exitCode;
}
Example #20
0
/* 
 *  Encode the files as C code
 */
static int binToC(Mpr *mpr, MprList *files, char *romName, char *prefix)
{
    MprFileInfo     info;
    MprFile         *file;
    char            buf[512];
    char            *filename, *cp, *sl, *p;
    int             next, j, i, len;

    mprPrintf(mpr, "/*\n *  %s -- Compiled Files\n */\n", romName);

    mprPrintf(mpr, "#include \"mpr.h\"\n\n");
    mprPrintf(mpr, "#if BLD_FEATURE_ROMFS\n");

    /*
     *  Open each input file and compile
     */
    for (next = 0; (filename = mprGetNextItem(files, &next)) != 0; ) {
        if (mprGetFileInfo(mpr, filename, &info) == 0 && info.isDir) {
            mprError(mpr, "Skipping directory %s", filename);
            continue;
        } 
        if ((file = mprOpen(mpr, filename, O_RDONLY | O_BINARY, 0666)) < 0) {
            mprError(mpr, "Can't open file %s\n", filename);
            return -1;
        }
        mprPrintf(mpr, "static uchar _file_%d[] = {\n", next);

        while ((len = mprRead(file, buf, sizeof(buf))) > 0) {
            p = buf;
            for (i = 0; i < len; ) {
                mprPrintf(mpr, "    ");
                for (j = 0; p < &buf[len] && j < 16; j++, p++) {
                    mprPrintf(mpr, "%3d,", (unsigned char) *p);
                }
                i += j;
                mprPrintf(mpr, "\n");
            }
        }
        mprPrintf(mpr, "    0 };\n\n");

        mprFree(file);
    }

    /*
     *  Now output the page index
     */ 
    mprPrintf(mpr, "MprRomInode %s[] = {\n", romName);

    for (next = 0; (filename = mprGetNextItem(files, &next)) != 0; ) {
        /*
         *  Replace the prefix with a leading "/"
         */ 
        if (strncmp(filename, prefix, strlen(prefix)) == 0) {
            cp = &filename[strlen(prefix)];
        } else {
            cp = filename;
        }
        while((sl = strchr(filename, '\\')) != NULL) {
            *sl = '/';
        }
        if (*cp == '/') {
            cp++;
        }

        if (*cp == '.' && cp[1] == '\0') {
            cp++;
        }
        if (mprGetFileInfo(mpr, filename, &info) == 0 && info.isDir) {
            mprPrintf(mpr, "    { \"%s\", 0, 0, 0 },\n", cp);
            continue;
        }
        mprPrintf(mpr, "    { \"%s\", _file_%d, %d, %d },\n", cp, next, (int) info.size, next);
    }
    
    mprPrintf(mpr, "    { 0, 0, 0, 0 },\n");
    mprPrintf(mpr, "};\n");

    mprPrintf(mpr, "#endif /* BLD_FEATURE_ROMFS */\n");

    return 0;
}