blob: 4c0a55bb1faabe77c717c6a89eba4f70d71547b1 [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
Martin v. Löwis011e8422009-05-05 04:43:17 +000017static wchar_t*
18char2wchar(char* arg)
19{
20 wchar_t *res;
21#ifdef HAVE_BROKEN_MBSTOWCS
22 /* Some platforms have a broken implementation of
23 * mbstowcs which does not count the characters that
24 * would result from conversion. Use an upper bound.
25 */
26 size_t argsize = strlen(arg);
27#else
28 size_t argsize = mbstowcs(NULL, arg, 0);
29#endif
30 size_t count;
31 unsigned char *in;
32 wchar_t *out;
33#ifdef HAVE_MBRTOWC
34 mbstate_t mbs;
35#endif
36 if (argsize != (size_t)-1) {
37 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
38 if (!res)
39 goto oom;
40 count = mbstowcs(res, arg, argsize+1);
41 if (count != (size_t)-1)
42 return res;
43 PyMem_Free(res);
44 }
45 /* Conversion failed. Fall back to escaping with utf8b. */
46#ifdef HAVE_MBRTOWC
47 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
48
49 /* Overallocate; as multi-byte characters are in the argument, the
50 actual output could use less memory. */
51 argsize = strlen(arg) + 1;
52 res = PyMem_Malloc(argsize*sizeof(wchar_t));
53 if (!res) goto oom;
54 in = (unsigned char*)arg;
55 out = res;
56 memset(&mbs, 0, sizeof mbs);
57 while (argsize) {
58 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
59 if (converted == 0)
60 /* Reached end of string; null char stored. */
61 break;
62 if (converted == (size_t)-2) {
63 /* Incomplete character. This should never happen,
64 since we provide everything that we have -
65 unless there is a bug in the C library, or I
66 misunderstood how mbrtowc works. */
67 fprintf(stderr, "unexpected mbrtowc result -2\n");
68 return NULL;
69 }
70 if (converted == (size_t)-1) {
71 /* Conversion error. Escape as UTF-8b, and start over
72 in the initial shift state. */
73 *out++ = 0xdc00 + *in++;
74 argsize--;
75 memset(&mbs, 0, sizeof mbs);
76 continue;
77 }
78 /* successfully converted some bytes */
79 in += converted;
80 argsize -= converted;
81 out++;
82 }
83#else
84 /* Cannot use C locale for escaping; manually escape as if charset
85 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
86 correctly in the locale's charset, which must be an ASCII superset. */
87 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
88 if (!res) goto oom;
89 in = (unsigned char*)arg;
90 out = res;
91 while(*in)
92 if(*in < 128)
93 *out++ = *in++;
94 else
95 *out++ = 0xdc00 + *in++;
96 *out = 0;
97#endif
98 return res;
99oom:
100 fprintf(stderr, "out of memory\n");
101 return NULL;
102}
103
Guido van Rossum7c141031997-08-15 02:52:08 +0000104int
Fredrik Lundhfaa209d62000-07-09 20:35:15 +0000105main(int argc, char **argv)
Guido van Rossum4c04be61997-07-19 19:25:33 +0000106{
Benjamin Peterson08a8f5f2008-10-19 14:15:00 +0000107 wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
Martin v. Löwis790465f2008-04-05 20:41:37 +0000108 /* We need a second copies, as Python might modify the first one. */
Benjamin Peterson08a8f5f2008-10-19 14:15:00 +0000109 wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
Martin v. Löwis790465f2008-04-05 20:41:37 +0000110 int i, res;
111 char *oldloc;
Tim Peters4643bd92002-12-28 21:56:08 +0000112 /* 754 requires that FP exceptions run in "no stop" mode by default,
113 * and until C vendors implement C99's ways to control FP exceptions,
114 * Python requires non-stop mode. Alas, some platforms enable FP
115 * exceptions by default. Here we disable them.
116 */
117#ifdef __FreeBSD__
118 fp_except_t m;
119
120 m = fpgetmask();
121 fpsetmask(m & ~FP_X_OFL);
122#endif
Martin v. Löwis790465f2008-04-05 20:41:37 +0000123 if (!argv_copy || !argv_copy2) {
Amaury Forgeot d'Arcd0ca9552008-10-07 21:06:18 +0000124 fprintf(stderr, "out of memory\n");
Martin v. Löwis790465f2008-04-05 20:41:37 +0000125 return 1;
126 }
Georg Brandl26338d12009-02-27 17:52:38 +0000127 oldloc = strdup(setlocale(LC_ALL, NULL));
Martin v. Löwis790465f2008-04-05 20:41:37 +0000128 setlocale(LC_ALL, "");
129 for (i = 0; i < argc; i++) {
Martin v. Löwis011e8422009-05-05 04:43:17 +0000130 argv_copy2[i] = argv_copy[i] = char2wchar(argv[i]);
131 if (!argv_copy[i])
Martin v. Löwis790465f2008-04-05 20:41:37 +0000132 return 1;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000133 }
134 setlocale(LC_ALL, oldloc);
Georg Brandl26338d12009-02-27 17:52:38 +0000135 free(oldloc);
Martin v. Löwis790465f2008-04-05 20:41:37 +0000136 res = Py_Main(argc, argv_copy);
137 for (i = 0; i < argc; i++) {
138 PyMem_Free(argv_copy2[i]);
139 }
140 PyMem_Free(argv_copy);
141 PyMem_Free(argv_copy2);
142 return res;
Guido van Rossum4c04be61997-07-19 19:25:33 +0000143}
Martin v. Löwis790465f2008-04-05 20:41:37 +0000144#endif