Example #1
0
int
main(int argc, char** argv)
{
    int eargv_size;
    int eargc_base;		/* How many arguments in the base of eargv. */
    char* emulator;
    char *env;

    emulator = env = get_env("ESCRIPT_EMULATOR");
    if (emulator == NULL) {
	emulator = get_default_emulator(argv[0]);
    }

    /*
     * Allocate the argv vector to be used for arguments to Erlang.
     * Arrange for starting to pushing information in the middle of
     * the array, to allow easy addition of commands in the beginning.
     */

    eargv_size = argc*4+100;
    eargv_base = (char **) emalloc(eargv_size*sizeof(char*));
    eargv = eargv_base;
    eargc = 0;
    push_words(emulator);
    eargc_base = eargc;
    eargv = eargv + eargv_size/2;
    eargc = 0;

    free_env_val(env);

    /*
     * Push initial arguments.
     */

    PUSH("+B");
    PUSH2("-boot", "start_clean");
    PUSH("-noshell");
    PUSH3("-run", "escript", "start");

    /*
     * Push all options (without the hyphen) before the script name.
     */

    while(argc > 1 && argv[1][0] == '-') {
	PUSH(argv[1]+1);
	argc--, argv++;
    }

    /*
     * Push the script name and everything following it as extra arguments.
     */

    PUSH("-extra");
    while (argc > 1) {
	PUSH(argv[1]);
	argc--, argv++;
    }

    /*
     * Move up the commands for invoking the emulator and adjust eargv
     * accordingly.
     */

    while (--eargc_base >= 0) {
	UNSHIFT(eargv_base[eargc_base]);
    }
    
    /*
     * Invoke Erlang with the collected options.
     */

    PUSH(NULL);
    return run_erlang(eargv[0], eargv);
}
Example #2
0
int
main(int argc, char** argv)
{
    int eargv_size;
    int eargc_base;		/* How many arguments in the base of eargv. */
    char* emulator;
    char* env;
    char* basename;
    char* absname;
    char scriptname[PMAX];
    char** last_opt;
    char** first_opt;

    emulator = env = get_env("ESCRIPT_EMULATOR");
    if (emulator == NULL) {
	emulator = get_default_emulator(argv[0]);
    }

    if (strlen(emulator) >= PMAX)
        error("Value of environment variable ESCRIPT_EMULATOR is too large");

    /*
     * Allocate the argv vector to be used for arguments to Erlang.
     * Arrange for starting to pushing information in the middle of
     * the array, to allow easy addition of commands in the beginning.
     */

    eargv_size = argc*4+1000+LINEBUFSZ/2;
    eargv_base = (char **) emalloc(eargv_size*sizeof(char*));
    eargv = eargv_base;
    eargc = 0;
    push_words(emulator);
    eargc_base = eargc;
    eargv = eargv + eargv_size/2;
    eargc = 0;

    free_env_val(env);

    /*
     * Push initial arguments.
     */

    PUSH("+B");
    PUSH2("-boot", "start_clean");
    PUSH("-noshell");

    /* Determine basename of the executable */
    for (basename = argv[0]+strlen(argv[0]);
	 basename > argv[0] && !(IS_DIRSEP(basename[-1]));
	 --basename)
	;
    
    first_opt = argv;
    last_opt = argv;

#ifdef __WIN32__
    if ( (_stricmp(basename, "escript.exe") == 0)
       ||(_stricmp(basename, "escript") == 0)) {
#else
    if (strcmp(basename, "escript") == 0) {
#endif
	/*
	 * Locate all options before the script name.
	 */
	
	while (argc > 1 && argv[1][0] == '-') {
	    argc--;
	    argv++;
	    last_opt = argv;
	}
	
	if (argc <= 1) {
	    error("Missing filename\n");
	}
	strncpy(scriptname, argv[1], sizeof(scriptname));
	scriptname[sizeof(scriptname)-1] = '\0';
	argc--;
	argv++;
    } else {
#ifdef __WIN32__
	int len;
#endif
	absname = find_prog(argv[0]);
#ifdef __WIN32__
	len = strlen(absname);
	if (len >= 4 && _stricmp(absname+len-4, ".exe") == 0) {
	    absname[len-4] = '\0';
	}
#endif

	erts_snprintf(scriptname, sizeof(scriptname), "%s.escript",
        absname);
	efree(absname);

    }

    /*
     * Read options from the %%! row in the script and add them as args
     */

    append_shebang_args(scriptname);

    /*
     * Push the script name and everything following it as extra arguments.
     */

    PUSH3("-run", "escript", "start");

    /*
     * Push all options before the script name. But omit the leading hyphens.
     */
    
    while (first_opt != last_opt) {
	PUSH(first_opt[1]+1);
	first_opt++;
    }

    PUSH("-extra");
    PUSH(scriptname);
    while (argc > 1) {
	PUSH(argv[1]);
	argc--, argv++;
    }

    /*
     * Move up the commands for invoking the emulator and adjust eargv
     * accordingly.
     */

    while (--eargc_base >= 0) {
	UNSHIFT(eargv_base[eargc_base]);
    }
    
    /*
     * Invoke Erlang with the collected options.
     */

    PUSH(NULL);
    return run_erlang(eargv[0], eargv);
}

static void
push_words(char* src)
{
    char sbuf[PMAX];
    char* dst;

    dst = sbuf;
    while ((*dst++ = *src++) != '\0') {
	if (isspace((int)*src)) {
	    *dst = '\0';
	    PUSH(strsave(sbuf));
	    dst = sbuf;
	    do {
		src++;
	    } while (isspace((int)*src));
	}
    }
    if (sbuf[0])
	PUSH(strsave(sbuf));
}
Example #3
0
File: erlc.c Project: Dasudian/otp
int wmain(int argc, wchar_t **wcargv)
{
    char** argv;
#else
int main(int argc, char** argv)
{
#endif
    int eargv_size;
    int eargc_base;		/* How many arguments in the base of eargv. */
    char* emulator;
    char *env;

#ifdef __WIN32__
    int i;
    int len;
    /* Convert argv to utf8 */
    argv = malloc((argc+1) * sizeof(char*));
    for (i=0; i<argc; i++) {
	len = WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, NULL, 0, NULL, NULL);
	argv[i] = malloc(len*sizeof(char));
	WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, argv[i], len, NULL, NULL);
    }
    argv[argc] = NULL;
#endif

    env = get_env("ERLC_EMULATOR");
    emulator = env ? env : get_default_emulator(argv[0]);

    if (strlen(emulator) >= MAXPATHLEN)
        error("Value of environment variable ERLC_EMULATOR is too large");

    /*
     * Allocate the argv vector to be used for arguments to Erlang.
     * Arrange for starting to pushing information in the middle of
     * the array, to allow easy adding of emulator options (like -pa)
     * before '-s erlcompile compile_cmdline...'.
     *
     * Oh, by the way, we will push the compiler command in the
     * base of the eargv vector, and move it up later.
     */

    eargv_size = argc*6+100;
    eargv_base = (char **) emalloc(eargv_size*sizeof(char*));
    eargv = eargv_base;
    eargc = 0;
    push_words(emulator);
    eargc_base = eargc;
    eargv = eargv + eargv_size/2;
    eargc = 0;

    free_env_val(env);

    /*
     * Push initial arguments.
     */

    PUSH("+sbtu");
    PUSH("+A0");
    PUSH("-noinput");
    PUSH2("-mode", "minimal");
    PUSH2("-boot", "start_clean");
    PUSH3("-s", "erl_compile", "compile_cmdline");
    PUSH("-extra");

    /*
     * Push standard arguments to Erlang.
     */

    /*
     * Parse all command line switches.
     */

    while (argc > 1) {

	/*
	 * Options starting with '+' are passed on to Erlang.
	 */

	switch (argv[1][0]) {
	case '+':
	    PUSH(argv[1]);
	    break;
	case '-':
	    switch (argv[1][1]) {
	    case 'd':
		if (argv[1][2] == '\0') {
		    debug = 1;
		} else {
		    PUSH(argv[1]);
		}
		break;
	    case 'p':
		{
		    int c = argv[1][2];
		    
		    if (c != 'a' && c != 'z') {
			PUSH(argv[1]);
#ifdef __WIN32__
		    } else if (strcmp(argv[1], "-pause") == 0) {
			pause_after_execution = 1;
#endif
		    } else {
			char option[4];

			UNSHIFT(process_opt(&argc, &argv, 1));
			option[0] = '-';
			option[1] = 'p';
			option[2] = c;
			option[3] = '\0';
			UNSHIFT(strsave(option));
		    }
		}
		break;
	    case 's':
		if (strcmp(argv[1], "-smp") == 0) {
		    UNSHIFT(argv[1]);
		} else {
		    PUSH(argv[1]);
		}
		break;
	    default:
		PUSH(argv[1]);
		break;
	    }
	    break;
	default:
	    PUSH(argv[1]);
	    break;
	}
	argc--, argv++;
    }

    /*
     * Move up the commands for invoking the emulator and adjust eargv
     * accordingly.
     */

    while (--eargc_base >= 0) {
	UNSHIFT(eargv_base[eargc_base]);
    }
    
    /*
     * Invoke Erlang with the collected options.
     */

    PUSH(NULL);
    return run_erlang(eargv[0], eargv);
}
Example #4
0
int wmain(int argc, wchar_t **wcargv)
{
    char** argv;
#else
int main(int argc, char** argv)
{
#endif
    int eargv_size;
    int eargc_base;		/* How many arguments in the base of eargv. */
    char* emulator;
    char *env;
    int i;
    int need_shell = 0;

#ifdef __WIN32__
    int len;
    /* Convert argv to utf8 */
    argv = emalloc((argc+1) * sizeof(char*));
    for (i=0; i<argc; i++) {
	len = WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, NULL, 0, NULL, NULL);
	argv[i] = emalloc(len*sizeof(char));
	WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, argv[i], len, NULL, NULL);
    }
    argv[argc] = NULL;
#endif

    env = get_env("DIALYZER_EMULATOR");
    emulator = env ? env : get_default_emulator(argv[0]);

    if (strlen(emulator) >= MAXPATHLEN)
        error("Value of environment variable DIALYZER_EMULATOR is too large");

    /*
     * Allocate the argv vector to be used for arguments to Erlang.
     * Arrange for starting to pushing information in the middle of
     * the array, to allow easy addition of commands in the beginning.
     */

    eargv_size = argc*4+100;
    eargv_base = (char **) emalloc(eargv_size*sizeof(char*));
    eargv = eargv_base;
    eargc = 0;
    push_words(emulator);
    eargc_base = eargc;
    eargv = eargv + eargv_size/2;
    eargc = 0;

    free_env_val(env);

    /*
     * Push initial arguments.
     */

    for (i = 1; i < argc; i++) {
       if (strcmp(argv[i], "--wx") == 0) {
           PUSH("-smp"); /* wx currently requires SMP enabled */
           break;
       }
    }

    if (argc > 1 && strcmp(argv[1], "-smp") == 0) {
	PUSH("-smpauto");
	argc--, argv++;
    }

    if (argc > 2 && strcmp(argv[1], "+S") == 0) {
	PUSH3("-smp", "+S", argv[2]);
	argc--, argv++;
	argc--, argv++;
    }

    if (argc > 2 && strcmp(argv[1], "+P") == 0) {
	PUSH2("+P", argv[2]);
	argc--, argv++;
	argc--, argv++;
    } else PUSH2("+P", "1000000");

    if (argc > 2 && strcmp(argv[1], "+sbt") == 0) {
	PUSH2("+sbt", argv[2]);
	argc--, argv++;
	argc--, argv++;
    }

    PUSH("+B");
    PUSH2("-boot", "start_clean");
    PUSH3("-run", "dialyzer", "plain_cl");
    PUSH("-extra");

    /*
     * Push everything except --shell.
     */

    while (argc > 1) {
	if (strcmp(argv[1], "--shell") == 0) {
	    need_shell = 1;
	} else {
	    PUSH(argv[1]);
	}
	argc--, argv++;
    }

    if (!need_shell) {
	UNSHIFT("-noinput");
    }

    /*
     * Move up the commands for invoking the emulator and adjust eargv
     * accordingly.
     */

    while (--eargc_base >= 0) {
	UNSHIFT(eargv_base[eargc_base]);
    }
    
    /*
     * Invoke Erlang with the collected options.
     */

    PUSH(NULL);
    return run_erlang(eargv[0], eargv);
}
Example #5
0
int wmain(int argc, wchar_t **wcargv)
{
    char** argv;
#else
int
main(int argc, char** argv)
{
#endif
    int eargv_size;
    int eargc_base;		/* How many arguments in the base of eargv. */
    char* emulator;
    int need_shell = 0;

#ifdef __WIN32__
    int i;
    int len;
    /* Convert argv to utf8 */
    argv = malloc((argc+1) * sizeof(char*));
    for (i=0; i<argc; i++) {
        len = WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, NULL, 0, NULL, NULL);
        argv[i] = malloc(len*sizeof(char));
        WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, argv[i], len, NULL, NULL);
    }
    argv[argc] = NULL;
#endif

    emulator = get_default_emulator(argv[0]);

    if (strlen(emulator) >= MAXPATHLEN)
        error("Emulator path length is too large");

    /*
     * Allocate the argv vector to be used for arguments to Erlang.
     * Arrange for starting to pushing information in the middle of
     * the array, to allow easy addition of commands in the beginning.
     */

    eargv_size = argc*4+100;
    eargv_base = (char **) emalloc(eargv_size*sizeof(char*));
    eargv = eargv_base;
    eargc = 0;
    PUSH(strsave(emulator));
    eargc_base = eargc;
    eargv = eargv + eargv_size/2;
    eargc = 0;

    /*
     * Push initial arguments.
     */

    if (argc > 1 && strcmp(argv[1], "-smp") == 0) {
        PUSH("-smpauto");
        argc--, argv++;
    }

    PUSH("+B");
    PUSH2("-boot", "start_clean");
    PUSH3("-run", "typer", "start");
    PUSH("-extra");

    /*
     * Push everything except --shell.
     */

    while (argc > 1) {
        if (strcmp(argv[1], "--shell") == 0) {
            need_shell = 1;
        } else {
            PUSH(argv[1]);
        }
        argc--, argv++;
    }

    if (!need_shell) {
        UNSHIFT("-noinput");
    }

    /*
     * Move up the commands for invoking the emulator and adjust eargv
     * accordingly.
     */

    while (--eargc_base >= 0) {
        UNSHIFT(eargv_base[eargc_base]);
    }

    /*
     * Invoke Erlang with the collected options.
     */

    PUSH(NULL);
    return run_erlang(eargv[0], eargv);
}
Example #6
0
int wmain(int argc, wchar_t **wcargv)
{
    char** argv;
#else
int main(int argc, char** argv)
{
#endif
    int eargv_size;
    int eargc_base;		/* How many arguments in the base of eargv. */
    char* emulator;
    char nodename[100];
    char browser[100];
    int ct_mode;
    int dist_mode;
    int cnt;
    int erl_args;
    char** argv0;

#ifdef __WIN32__
    int i;
    int len;
    /* Convert argv to utf8 */
    argv = malloc((argc+1) * sizeof(char*));
    for (i=0; i<argc; i++) {
        len = WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, NULL, 0, NULL, NULL);
        argv[i] = malloc(len*sizeof(char));
        WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, argv[i], len, NULL, NULL);
    }
    argv[argc] = NULL;
#endif
    argv0 = argv;

    emulator = get_default_emulator(argv[0]);
    if (strlen(emulator) >= MAXPATHLEN)
        error("Emulator path length is too large");

    /*
     * Allocate the argv vector to be used for arguments to Erlang.
     * Arrange for starting to pushing information in the middle of
     * the array, to allow easy addition of commands in the beginning.
     */

    eargv_size = argc*4+100;
    eargv_base = (char **) emalloc(eargv_size*sizeof(char*));
    eargv = eargv_base;
    eargc = 0;
    PUSH(strsave(emulator));
    eargc_base = eargc;
    eargv = eargv + eargv_size/2;
    eargc = 0;

    strcpy(nodename, "ct");
    dist_mode = SHORT_NAME;
    browser[0] = '\0';
    ct_mode = NORMAL_MODE;
    erl_args = argc;
    cnt = 1;

    /*
     * Check various flags before building command line
     */

    while (cnt < argc) {
        if (strcmp(argv[1], "-erl_args") == 0) {
            erl_args = cnt;
        }
        else if (strcmp(argv[1], "-sname") == 0) {
            strncpy(nodename, argv[2], sizeof(nodename));
            nodename[sizeof(nodename)-1] = '\0';
            cnt++, argv++;
        }
        else if (strcmp(argv[1], "-name") == 0) {
            strncpy(nodename, argv[2], sizeof(nodename));
            nodename[sizeof(nodename)-1] = '\0';
            dist_mode = FULL_NAME;
            cnt++, argv++;
        }
        else {
            if (cnt < erl_args) {
                if (strcmp(argv[1], "-vts") == 0) {
                    ct_mode = VTS_MODE;
                }
                else if (strcmp(argv[1], "-browser") == 0) {
                    strncpy(browser, argv[2], sizeof(browser));
                    browser[sizeof(browser)-1] = '\0';
                    cnt++, argv++;
                }
                else if (strcmp(argv[1], "-shell") == 0) {
                    ct_mode = CT_SHELL_MODE;
                }
                else if (strcmp(argv[1], "-ctmaster") == 0) {
                    strcpy(nodename, "ct_master");
                    ct_mode = MASTER_MODE;
                }
                else if (strcmp(argv[1], "-ctname") == 0) {
                    strncpy(nodename, argv[2], sizeof(nodename));
                    nodename[sizeof(nodename)-1] = '\0';
                    ct_mode = ERL_SHELL_MODE;
                    cnt++, argv++;
                }
            }
        }
        cnt++, argv++;
    }

    argv = argv0;

    /*
     * Push initial arguments.
     */

    if (dist_mode == FULL_NAME) {
        PUSH2("-name", nodename);
    }
    else {
        PUSH2("-sname", nodename);
    }

    /*
     * Push everything else
     */

    if (ct_mode == VTS_MODE) {
        PUSH4("-s", "ct_webtool", "script_start", "vts");
        if (browser[0] != '\0') PUSH(browser);
        PUSH3("-s", "ct_run", "script_start");
    }
    else if (ct_mode == CT_SHELL_MODE) {
        PUSH3("-s", "ct_run", "script_start");
    }
    else if (ct_mode == NORMAL_MODE) {
        PUSH3("-s", "ct_run", "script_start");
        PUSH3("-s", "erlang", "halt");
    }

    cnt = 1;
    while (cnt < argc) {
        if (strcmp(argv[1], "-erl_args") == 0) {
            PUSH("-ct_erl_args");
        }
        else if ((strcmp(argv[1], "-sname") == 0) || (strcmp(argv[1], "-name") == 0)) {
            cnt++, argv++;
        }
        else if (cnt < erl_args) {
            if (strcmp(argv[1], "-config") == 0)
                PUSH("-ct_config");
            else if (strcmp(argv[1], "-decrypt_key") == 0)
                PUSH("-ct_decrypt_key");
            else if (strcmp(argv[1], "-decrypt_file") == 0)
                PUSH("-ct_decrypt_file");
            else
                PUSH(argv[1]);
        }
        else {
            PUSH(argv[1]);
        }
        cnt++, argv++;
    }

    /*
     * Move up the commands for invoking the emulator and adjust eargv
     * accordingly.
     */

    while (--eargc_base >= 0) {
        UNSHIFT(eargv_base[eargc_base]);
    }

    /*
     * Invoke Erlang with the collected options.
     */

    PUSH(NULL);

    return run_erlang(eargv[0], eargv);
}
Example #7
0
int
main(int argc, char** argv)
{
    int eargv_size;
    int eargc_base;		/* How many arguments in the base of eargv. */
    char* emulator;
    char nodename[100];
    char browser[100];
    int ct_mode;
    int dist_mode;
    int cnt;
    int erl_args;
    char** argv0 = argv;

    emulator = get_default_emulator(argv[0]);

    /*
     * Allocate the argv vector to be used for arguments to Erlang.
     * Arrange for starting to pushing information in the middle of
     * the array, to allow easy addition of commands in the beginning.
     */

    eargv_size = argc*4+100;
    eargv_base = (char **) emalloc(eargv_size*sizeof(char*));
    eargv = eargv_base;
    eargc = 0;
    push_words(emulator);
    eargc_base = eargc;
    eargv = eargv + eargv_size/2;
    eargc = 0;

    strcpy(nodename, "ct");
    dist_mode = SHORT_NAME;
    browser[0] = '\0';
    ct_mode = NORMAL_MODE;
    erl_args = argc;
    cnt = 1;

    /*
     * Check various flags before building command line
     */

    while (cnt < argc) {
	if (strcmp(argv[1], "-erl_args") == 0) {
	    erl_args = cnt;
	}
	else if (strcmp(argv[1], "-sname") == 0) {
	    strcpy(nodename, argv[2]);
	    cnt++, argv++;
	}
	else if (strcmp(argv[1], "-name") == 0) {
	    strcpy(nodename, argv[2]);
	    dist_mode = FULL_NAME;
	    cnt++, argv++;
	}
	else {
	    if (cnt < erl_args) {
		if (strcmp(argv[1], "-vts") == 0) {
		    ct_mode = VTS_MODE;
		}
		else if (strcmp(argv[1], "-browser") == 0) {
		    strcpy(browser, argv[2]);
		    cnt++, argv++;
		}
		else if (strcmp(argv[1], "-shell") == 0) {
		    ct_mode = CT_SHELL_MODE;
		}
		else if (strcmp(argv[1], "-ctmaster") == 0) {
		    strcpy(nodename, "ct_master");
		    ct_mode = MASTER_MODE;
		}
		else if (strcmp(argv[1], "-ctname") == 0) {
		    strcpy(nodename, argv[2]);
		    ct_mode = ERL_SHELL_MODE;
		    cnt++, argv++;
		}
	    }
	}
	cnt++, argv++;
    }

    argv = argv0;

    /*
     * Push initial arguments.
     */

    if (dist_mode == FULL_NAME) {
	PUSH2("-name", nodename);
    }
    else {
	PUSH2("-sname", nodename);
    }

    /*
     * Push everything else
     */

    if (ct_mode == VTS_MODE) {
	PUSH4("-s", "webtool", "script_start", "vts");
	if (browser[0] != '\0') PUSH(browser);
	PUSH3("-s", "ct_run", "script_start");
    }
    else if (ct_mode == CT_SHELL_MODE) {
	PUSH3("-s", "ct_run", "script_start");
    }
    else if (ct_mode == NORMAL_MODE) {
	PUSH3("-s", "ct_run", "script_start");
	PUSH3("-s", "erlang", "halt");
    }

    cnt = 1;
    while (cnt < argc) {
	if (strcmp(argv[1], "-erl_args") == 0) {
	    PUSH("-ct_erl_args");
	}
	else if ((strcmp(argv[1], "-sname") == 0) || (strcmp(argv[1], "-name") == 0)) {
	    cnt++, argv++;
	}
	else if (cnt < erl_args) {
	    if (strcmp(argv[1], "-config") == 0)
		PUSH("-ct_config");
	    else if (strcmp(argv[1], "-decrypt_key") == 0)
		PUSH("-ct_decrypt_key");
	    else if (strcmp(argv[1], "-decrypt_file") == 0)
		PUSH("-ct_decrypt_file");
	    else
		PUSH(argv[1]);
	}
	else {
	    PUSH(argv[1]);
	}
	cnt++, argv++;
    }

    /*
     * Move up the commands for invoking the emulator and adjust eargv
     * accordingly.
     */

    while (--eargc_base >= 0) {
	UNSHIFT(eargv_base[eargc_base]);
    }

    /*
     * Invoke Erlang with the collected options.
     */

    PUSH(NULL);

    return run_erlang(eargv[0], eargv);
}
Example #8
0
File: escript.c Project: HansN/otp
int wmain(int argc, wchar_t **wcargv)
{
    char** argv;
#else
int
main(int argc, char** argv)
{
#endif
    int eargv_size;
    int eargc_base;		/* How many arguments in the base of eargv. */
    char* emulator;
    char* basename;
    char* def_emu_lookup_path;
    char scriptname[PMAX];
    char** last_opt;
    char** first_opt;
    
#ifdef __WIN32__
    int i;
    int len;
    /* Convert argv to utf8 */
    argv = emalloc((argc+1) * sizeof(char*));
    for (i=0; i<argc; i++) {
	len = WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, NULL, 0, NULL, NULL);
	argv[i] = emalloc(len*sizeof(char));
	WideCharToMultiByte(CP_UTF8, 0, wcargv[i], -1, argv[i], len, NULL, NULL);
    }
    argv[argc] = NULL;
#endif

    /*
     * Allocate the argv vector to be used for arguments to Erlang.
     * Arrange for starting to pushing information in the middle of
     * the array, to allow easy addition of commands in the beginning.
     */

    eargv_size = argc*4+1000+LINEBUFSZ/2;
    eargv_base = (char **) emalloc(eargv_size*sizeof(char*));
    eargv = eargv_base;
    eargc = 0;
    eargc_base = eargc;
    eargv = eargv + eargv_size/2;
    eargc = 0;

    /* Determine basename of the executable */
    for (basename = argv[0]+strlen(argv[0]);
	 basename > argv[0] && !(IS_DIRSEP(basename[-1]));
	 --basename)
	;
    
    first_opt = argv;
    last_opt = argv;

#ifdef __WIN32__
    if ( (_stricmp(basename, "escript.exe") == 0)
       ||(_stricmp(basename, "escript") == 0)) {
#else
    if (strcmp(basename, "escript") == 0) {
#endif
        def_emu_lookup_path = argv[0];
	/*
	 * Locate all options before the script name.
	 */
	
	while (argc > 1 && argv[1][0] == '-') {
	    argc--;
	    argv++;
	    last_opt = argv;
	}
	
	if (argc <= 1) {
	    error("Missing filename\n");
	}
	strncpy(scriptname, argv[1], sizeof(scriptname));
	scriptname[sizeof(scriptname)-1] = '\0';
	argc--;
	argv++;
    } else {
        char *absname = find_prog(argv[0]);
#ifdef __WIN32__
	int len = strlen(absname);
	if (len >= 4 && _stricmp(absname+len-4, ".exe") == 0) {
	    absname[len-4] = '\0';
	}
#endif
	erts_snprintf(scriptname, sizeof(scriptname), "%s.escript",
		      absname);
        efree(absname);
        def_emu_lookup_path = scriptname;
    }

    /* Determine path to emulator */
    emulator = get_env("ESCRIPT_EMULATOR");

    if (emulator == NULL) {
	emulator = get_default_emulator(def_emu_lookup_path);
    }

    if (strlen(emulator) >= PMAX)
        error("Value of environment variable ESCRIPT_EMULATOR is too large");

    /*
     * Push initial arguments.
     */

    PUSH(emulator);

    PUSH("+B");
    PUSH2("-boot", "no_dot_erlang");
    PUSH("-noshell");

    /*
     * Read options from the %%! row in the script and add them as args
     */

    append_shebang_args(scriptname);

    /*
     * Push the script name and everything following it as extra arguments.
     */

    PUSH3("-run", "escript", "start");

    /*
     * Push all options before the script name. But omit the leading hyphens.
     */
    
    while (first_opt != last_opt) {
	PUSH(first_opt[1]+1);
	first_opt++;
    }

    PUSH("-extra");
    PUSH(scriptname);
    while (argc > 1) {
	PUSH(argv[1]);
	argc--, argv++;
    }

    /*
     * Move up the commands for invoking the emulator and adjust eargv
     * accordingly.
     */

    while (--eargc_base >= 0) {
	UNSHIFT(eargv_base[eargc_base]);
    }

    /*
     * Add scriptname to env
     */
    set_env("ESCRIPT_NAME", scriptname);

    /*
     * Invoke Erlang with the collected options.
     */

    PUSH(NULL);
    return run_erlang(eargv[0], eargv);
}

#ifdef __WIN32__
wchar_t *make_commandline(char **argv)
{
    static wchar_t *buff = NULL;
    static int siz = 0;
    int num = 0, len;
    char **arg;
    wchar_t *p;

    if (*argv == NULL) { 
	return L"";
    }
    for (arg = argv; *arg != NULL; ++arg) {
	num += strlen(*arg)+1;
    }
    if (!siz) {
	siz = num;
	buff = (wchar_t *) emalloc(siz*sizeof(wchar_t));
    } else if (siz < num) {
	siz = num;
	buff = (wchar_t *) erealloc(buff,siz*sizeof(wchar_t));
    }
    p = buff;
    num=0;
    for (arg = argv; *arg != NULL; ++arg) {
	len = MultiByteToWideChar(CP_UTF8, 0, *arg, -1, p, siz);
	p+=(len-1);
	*p++=L' ';
    }
    *(--p) = L'\0';

    if (debug) {
	printf("Processed command line:%S\n",buff);
    }
    return buff;
}

int my_spawnvp(char **argv)
{
    STARTUPINFOW siStartInfo;
    PROCESS_INFORMATION piProcInfo;
    DWORD ec;

    memset(&siStartInfo,0,sizeof(STARTUPINFOW));
    siStartInfo.cb = sizeof(STARTUPINFOW); 
    siStartInfo.dwFlags = STARTF_USESTDHANDLES;
    siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);

    if (!CreateProcessW(NULL, 
			make_commandline(argv),
			NULL, 
			NULL, 
			TRUE, 
			0,
			NULL, 
			NULL, 
			&siStartInfo, 
			&piProcInfo)) {
	return -1;
    }
    CloseHandle(piProcInfo.hThread);

    WaitForSingleObject(piProcInfo.hProcess,INFINITE);
    if (!GetExitCodeProcess(piProcInfo.hProcess,&ec)) {
	return 0;
    }
    return (int) ec;
}