Ejemplo n.º 1
0
*/	void *Alloc_Mem(size_t size)
/*
**		Main memory allocation wrapper function.
**
***********************************************************************/
{
	void *ptr;

	if (!(ptr = malloc(size))) return 0;
	PG_Mem_Usage += size;
	if (PG_Mem_Limit != 0 && (PG_Mem_Usage > PG_Mem_Limit)) {
		Check_Security(SYM_MEMORY, POL_EXEC, 0);
	}
	CLEAR(ptr, size);

	return ptr;
}
Ejemplo n.º 2
0
//
//  Call_Core: C
//
// flags:
//     1: wait, is implied when I/O redirection is enabled
//     2: console
//     4: shell
//     8: info
//     16: show
//
// Return -1 on error, otherwise the process return code.
//
// POSIX previous simple version was just 'return system(call);'
// This uses 'execvp' which is "POSIX.1 conforming, UNIX compatible"
//
REB_R Call_Core(REBFRM *frame_) {
    PROCESS_INCLUDE_PARAMS_OF_CALL_INTERNAL_P;

    UNUSED(REF(console));  // !!! actually not paid attention to, why?

    // SECURE was never actually done for R3-Alpha
    //
    Check_Security(Canon(SYM_CALL), POL_EXEC, ARG(command));

    // Make sure that if the output or error series are STRING! or BINARY!,
    // they are not read-only, before we try appending to them.
    //
    if (IS_TEXT(ARG(output)) or IS_BINARY(ARG(output)))
        FAIL_IF_READ_ONLY(ARG(output));
    if (IS_TEXT(ARG(error)) or IS_BINARY(ARG(error)))
        FAIL_IF_READ_ONLY(ARG(error));

    char *inbuf;
    size_t inbuf_size;

    if (not REF(input)) {
      null_input_buffer:
        inbuf = nullptr;
        inbuf_size = 0;
    }
    else switch (VAL_TYPE(ARG(input))) {
      case REB_LOGIC:
        goto null_input_buffer;

      case REB_TEXT: {
        inbuf_size = rebSpellIntoQ(nullptr, 0, ARG(input), rebEND);
        inbuf = rebAllocN(char, inbuf_size);
        size_t check;
        check = rebSpellIntoQ(inbuf, inbuf_size, ARG(input), rebEND);
        UNUSED(check);
        break; }

      case REB_FILE: {
        size_t size;
        inbuf = s_cast(rebBytes(  // !!! why fileNAME size passed in???
            &size,
            "file-to-local", ARG(input),
            rebEND
        ));
        inbuf_size = size;
        break; }

      case REB_BINARY: {
        inbuf = s_cast(rebBytes(&inbuf_size, ARG(input), rebEND));
        break; }

      default:
        panic (ARG(input));  // typechecking should not have allowed it
    }

    bool flag_wait;
    if (
        REF(wait)
        or (
            IS_TEXT(ARG(input)) or IS_BINARY(ARG(input))
            or IS_TEXT(ARG(output)) or IS_BINARY(ARG(output))
            or IS_TEXT(ARG(error)) or IS_BINARY(ARG(error))
        ) // I/O redirection implies /WAIT
    ){
        flag_wait = true;
    }
    else
        flag_wait = false;

    // We synthesize the argc and argv from the "command", and in the process
    // we do dynamic allocations of argc strings through the API.  These need
    // to be freed before we return.
    //
    char *cmd;
    int argc;
    const char **argv;

    if (IS_TEXT(ARG(command))) {
        //
        // !!! POSIX does not offer the ability to take a single command
        // line string when invoking a process.  You have to use an argv[]
        // array.  The only workaround to this is to run through a shell--
        // but that would give you a new environment.  We only parse the
        // command line if forced (Windows can call with a single command
        // line, but has the reverse problem: it has to make the command
        // line out of argv[] parts if you pass an array).
        //
        if (not REF(shell)) {
            REBVAL *block = rebValue(
                "parse-command-to-argv*", ARG(command), rebEND
            );
            Move_Value(ARG(command), block);
            rebRelease(block);
            goto block_command;
        }

        cmd = rebSpell(ARG(command), rebEND);

        argc = 1;
        argv = rebAllocN(const char*, (argc + 1));

        // !!! Make two copies because it frees cmd and all the argv.  Review.
        //
        argv[0] = rebSpell(ARG(command), rebEND);
        argv[1] = nullptr;
    }
    else if (IS_BLOCK(ARG(command))) {