mercurial/exewrapper.c
author Matt Harbison <matt_harbison@yahoo.com>
Fri, 19 Oct 2018 18:32:13 -0400
changeset 40396 973ff03d9bc0
parent 40395 aca727359ec5
child 40407 36ba91e06948
permissions -rw-r--r--
exewrapper: convert to _tcsxxx functions for Unicode compatability This fixes more than 50 tests on py3 on Windows when enabled, mostly hooks and such that invoked `hg` directly. 187 left to go. I skipped doing the abort printing with Unicode because of apparent issues with MinGW [1]. It may be moot though, as MinGW isn't listed as a supported compiler after 3.4 [2]. [1] https://stackoverflow.com/questions/17700797/printf-wprintf-s-s-ls-char-and-wchar-errors-not-announced-by-a-compil [2] https://wiki.python.org/moin/WindowsCompilers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     1
/*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     2
 exewrapper.c - wrapper for calling a python script on Windows
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     3
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     4
 Copyright 2012 Adrian Buehlmann <adrian@cadifra.com> and others
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     5
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     6
 This software may be used and distributed according to the terms of the
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     7
 GNU General Public License version 2 or any later version.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     8
*/
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     9
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    10
#include <stdio.h>
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    11
#include <tchar.h>
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    12
#include <windows.h>
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    13
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    14
#include "hgpythonlib.h"
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    15
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    16
#ifdef __GNUC__
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    17
int strcat_s(char *d, size_t n, const char *s)
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    18
{
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    19
	return !strncat(d, s, n);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    20
}
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    21
int strcpy_s(char *d, size_t n, const char *s)
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    22
{
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    23
	return !strncpy(d, s, n);
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    24
}
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    25
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    26
#define _tcscpy_s strcpy_s
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    27
#define _tcscat_s strcat_s
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    28
#define _countof(array) (sizeof(array)/sizeof(array[0]))
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    29
#endif
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    30
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    31
static TCHAR pyscript[MAX_PATH + 10];
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    32
static TCHAR pyhome[MAX_PATH + 10];
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    33
static TCHAR pydllfile[MAX_PATH + 10];
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    34
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    35
int _tmain(int argc, TCHAR *argv[])
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    36
{
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    37
	TCHAR *p;
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    38
	int ret;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    39
	int i;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    40
	int n;
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    41
	TCHAR **pyargv;
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    42
	WIN32_FIND_DATA fdata;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    43
	HANDLE hfind;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    44
	const char *err;
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    45
	HMODULE pydll;
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    46
	void(__cdecl * Py_SetPythonHome)(TCHAR *home);
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    47
	int(__cdecl * Py_Main)(int argc, TCHAR *argv[]);
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    48
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    49
	if (GetModuleFileName(NULL, pyscript, _countof(pyscript)) == 0) {
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    50
		err = "GetModuleFileName failed";
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    51
		goto bail;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    52
	}
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    53
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    54
	p = _tcsrchr(pyscript, '.');
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    55
	if (p == NULL) {
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    56
		err = "malformed module filename";
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    57
		goto bail;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    58
	}
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    59
	*p = 0; /* cut trailing ".exe" */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    60
	_tcscpy_s(pyhome, _countof(pyhome), pyscript);
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    61
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    62
	hfind = FindFirstFile(pyscript, &fdata);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    63
	if (hfind != INVALID_HANDLE_VALUE) {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    64
		/* pyscript exists, close handle */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    65
		FindClose(hfind);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    66
	} else {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    67
		/* file pyscript isn't there, take <pyscript>exe.py */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    68
		_tcscat_s(pyscript, _countof(pyscript), _T("exe.py"));
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    69
	}
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    70
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    71
	pydll = NULL;
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    72
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    73
	p = _tcsrchr(pyhome, _T('\\'));
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    74
	if (p == NULL) {
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    75
		err = "can't find backslash in module filename";
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    76
		goto bail;
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    77
	}
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    78
	*p = 0; /* cut at directory */
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    79
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    80
	/* check for private Python of HackableMercurial */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    81
	_tcscat_s(pyhome, _countof(pyhome), _T("\\hg-python"));
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    82
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    83
	hfind = FindFirstFile(pyhome, &fdata);
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    84
	if (hfind != INVALID_HANDLE_VALUE) {
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    85
		/* Path .\hg-python exists. We are probably in HackableMercurial
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    86
		scenario, so let's load python dll from this dir. */
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    87
		FindClose(hfind);
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    88
		_tcscpy_s(pydllfile, _countof(pydllfile), pyhome);
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    89
		_tcscat_s(pydllfile, _countof(pydllfile), _T("\\") _T(HGPYTHONLIB)
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    90
					_T(".dll"));
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    91
		pydll = LoadLibrary(pydllfile);
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    92
		if (pydll == NULL) {
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
    93
			err = "failed to load private Python DLL " HGPYTHONLIB ".dll";
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    94
			goto bail;
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
    95
		}
34636
31c6c4d27be7 exewrapper: format with clang-format
Augie Fackler <augie@google.com>
parents: 31452
diff changeset
    96
		Py_SetPythonHome =
31c6c4d27be7 exewrapper: format with clang-format
Augie Fackler <augie@google.com>
parents: 31452
diff changeset
    97
		    (void *)GetProcAddress(pydll, "Py_SetPythonHome");
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    98
		if (Py_SetPythonHome == NULL) {
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
    99
			err = "failed to get Py_SetPythonHome";
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
   100
			goto bail;
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   101
		}
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
   102
		Py_SetPythonHome(pyhome);
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   103
	}
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   104
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   105
	if (pydll == NULL) {
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
   106
		pydll = LoadLibrary(_T(HGPYTHONLIB) _T(".dll"));
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   107
		if (pydll == NULL) {
26662
d215def59c3b exewrapper: report name of failed DLL in error message
Adrian Buehlmann <adrian@cadifra.com>
parents: 17732
diff changeset
   108
			err = "failed to load Python DLL " HGPYTHONLIB ".dll";
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   109
			goto bail;
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   110
		}
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   111
	}
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   112
34636
31c6c4d27be7 exewrapper: format with clang-format
Augie Fackler <augie@google.com>
parents: 31452
diff changeset
   113
	Py_Main = (void *)GetProcAddress(pydll, "Py_Main");
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   114
	if (Py_Main == NULL) {
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   115
		err = "failed to get Py_Main";
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   116
		goto bail;
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   117
	}
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
   118
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   119
	/*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   120
	Only add the pyscript to the args, if it's not already there. It may
17063
3fbc6e3abdbd exewrapper: use generic term script
Adrian Buehlmann <adrian@cadifra.com>
parents: 17058
diff changeset
   121
	already be there, if the script spawned a child process of itself, in
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   122
	the same way as it got called, that is, with the pyscript already in
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   123
	place. So we optionally accept the pyscript as the first argument
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   124
	(argv[1]), letting our exe taking the role of the python interpreter.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   125
	*/
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
   126
	if (argc >= 2 && _tcscmp(argv[1], pyscript) == 0) {
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   127
		/*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   128
		pyscript is already in the args, so there is no need to copy
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   129
		the args and we can directly call the python interpreter with
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   130
		the original args.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   131
		*/
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   132
		return Py_Main(argc, argv);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   133
	}
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   134
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   135
	/*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   136
	Start assembling the args for the Python interpreter call. We put the
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   137
	name of our exe (argv[0]) in the position where the python.exe
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   138
	canonically is, and insert the pyscript next.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   139
	*/
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
   140
	pyargv = malloc((argc + 5) * sizeof(TCHAR *));
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   141
	if (pyargv == NULL) {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   142
		err = "not enough memory";
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   143
		goto bail;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   144
	}
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   145
	n = 0;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   146
	pyargv[n++] = argv[0];
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   147
	pyargv[n++] = pyscript;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   148
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   149
	/* copy remaining args from the command line */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   150
	for (i = 1; i < argc; i++)
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   151
		pyargv[n++] = argv[i];
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   152
	/* argv[argc] is guaranteed to be NULL, so we forward that guarantee */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   153
	pyargv[n] = NULL;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   154
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   155
	ret = Py_Main(n, pyargv); /* The Python interpreter call */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   156
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   157
	free(pyargv);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   158
	return ret;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   159
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   160
bail:
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   161
	fprintf(stderr, "abort: %s\n", err);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   162
	return 255;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   163
}