blob: 9e7888999f491df4266dce9824a085bba18cc30c [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{
Benjamin Peterson08a8f5f2008-10-19 14:15:00 +000020 wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
Martin v. Löwis790465f2008-04-05 20:41:37 +000021 /* We need a second copies, as Python might modify the first one. */
Benjamin Peterson08a8f5f2008-10-19 14:15:00 +000022 wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
Martin v. Löwis790465f2008-04-05 20:41:37 +000023 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) {
Amaury Forgeot d'Arcd0ca9552008-10-07 21:06:18 +000037 fprintf(stderr, "out of memory\n");
Martin v. Löwis790465f2008-04-05 20:41:37 +000038 return 1;
39 }
40 oldloc = setlocale(LC_ALL, NULL);
41 setlocale(LC_ALL, "");
42 for (i = 0; i < argc; i++) {
Antoine Pitroufff95302008-09-03 18:58:51 +000043#ifdef HAVE_BROKEN_MBSTOWCS
44 /* Some platforms have a broken implementation of
45 * mbstowcs which does not count the characters that
46 * would result from conversion. Use an upper bound.
47 */
48 size_t argsize = strlen(argv[i]);
49#else
Martin v. Löwis790465f2008-04-05 20:41:37 +000050 size_t argsize = mbstowcs(NULL, argv[i], 0);
Antoine Pitroufff95302008-09-03 18:58:51 +000051#endif
52 size_t count;
Martin v. Löwis790465f2008-04-05 20:41:37 +000053 if (argsize == (size_t)-1) {
Amaury Forgeot d'Arcd0ca9552008-10-07 21:06:18 +000054 fprintf(stderr, "Could not convert argument %d to string\n", i);
Martin v. Löwis790465f2008-04-05 20:41:37 +000055 return 1;
56 }
Benjamin Peterson08a8f5f2008-10-19 14:15:00 +000057 argv_copy[i] = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
Martin v. Löwis790465f2008-04-05 20:41:37 +000058 argv_copy2[i] = argv_copy[i];
59 if (!argv_copy[i]) {
Amaury Forgeot d'Arcd0ca9552008-10-07 21:06:18 +000060 fprintf(stderr, "out of memory\n");
Martin v. Löwis790465f2008-04-05 20:41:37 +000061 return 1;
62 }
Antoine Pitroufff95302008-09-03 18:58:51 +000063 count = mbstowcs(argv_copy[i], argv[i], argsize+1);
64 if (count == (size_t)-1) {
Amaury Forgeot d'Arcd0ca9552008-10-07 21:06:18 +000065 fprintf(stderr, "Could not convert argument %d to string\n", i);
Antoine Pitroufff95302008-09-03 18:58:51 +000066 return 1;
67 }
Martin v. Löwis790465f2008-04-05 20:41:37 +000068 }
69 setlocale(LC_ALL, oldloc);
70 res = Py_Main(argc, argv_copy);
71 for (i = 0; i < argc; i++) {
72 PyMem_Free(argv_copy2[i]);
73 }
74 PyMem_Free(argv_copy);
75 PyMem_Free(argv_copy2);
76 return res;
Guido van Rossum4c04be61997-07-19 19:25:33 +000077}
Martin v. Löwis790465f2008-04-05 20:41:37 +000078#endif