blob: c1de64a2a4dd9c9f75bad638d0c5e375e1a14e3a [file] [log] [blame]
Guido van Rossum4c04be61997-07-19 19:25:33 +00001/* Minimal main program -- everything is loaded from the library */
2
Guido van Rossumbe10c201998-08-08 20:01:22 +00003#include "Python.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +00004#include <locale.h>
Guido van Rossumbe10c201998-08-08 20:01:22 +00005
Tim Peters4643bd92002-12-28 21:56:08 +00006#ifdef __FreeBSD__
7#include <floatingpoint.h>
8#endif
9
Martin v. Löwis790465f2008-04-05 20:41:37 +000010#ifdef MS_WINDOWS
11int
12wmain(int argc, wchar_t **argv)
13{
14 return Py_Main(argc, argv);
15}
16#else
Guido van Rossum7c141031997-08-15 02:52:08 +000017int
Fredrik Lundhfaa209d62000-07-09 20:35:15 +000018main(int argc, char **argv)
Guido van Rossum4c04be61997-07-19 19:25:33 +000019{
Martin v. Löwis790465f2008-04-05 20:41:37 +000020 wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
21 /* We need a second copies, as Python might modify the first one. */
22 wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
23 int i, res;
24 char *oldloc;
Tim Peters4643bd92002-12-28 21:56:08 +000025 /* 754 requires that FP exceptions run in "no stop" mode by default,
26 * and until C vendors implement C99's ways to control FP exceptions,
27 * Python requires non-stop mode. Alas, some platforms enable FP
28 * exceptions by default. Here we disable them.
29 */
30#ifdef __FreeBSD__
31 fp_except_t m;
32
33 m = fpgetmask();
34 fpsetmask(m & ~FP_X_OFL);
35#endif
Martin v. Löwis790465f2008-04-05 20:41:37 +000036 if (!argv_copy || !argv_copy2) {
37 fprintf(stderr, "out of memory");
38 return 1;
39 }
40 oldloc = setlocale(LC_ALL, NULL);
41 setlocale(LC_ALL, "");
42 for (i = 0; i < argc; i++) {
43 size_t argsize = mbstowcs(NULL, argv[i], 0);
44 if (argsize == (size_t)-1) {
45 fprintf(stderr, "Could not convert argument %d to string", i);
46 return 1;
47 }
48 argv_copy[i] = PyMem_Malloc((argsize+1)*sizeof(wchar_t));
49 argv_copy2[i] = argv_copy[i];
50 if (!argv_copy[i]) {
51 fprintf(stderr, "out of memory");
52 return 1;
53 }
54 mbstowcs(argv_copy[i], argv[i], argsize+1);
55 }
56 setlocale(LC_ALL, oldloc);
57 res = Py_Main(argc, argv_copy);
58 for (i = 0; i < argc; i++) {
59 PyMem_Free(argv_copy2[i]);
60 }
61 PyMem_Free(argv_copy);
62 PyMem_Free(argv_copy2);
63 return res;
Guido van Rossum4c04be61997-07-19 19:25:33 +000064}
Martin v. Löwis790465f2008-04-05 20:41:37 +000065#endif