blob: 50ec5f02191e13f5d4fd7653d074da33cc7a99a0 [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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000014 return Py_Main(argc, argv);
Martin v. Löwis790465f2008-04-05 20:41:37 +000015}
16#else
Martin v. Löwis011e8422009-05-05 04:43:17 +000017static wchar_t*
18char2wchar(char* arg)
19{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000020 wchar_t *res;
Martin v. Löwis011e8422009-05-05 04:43:17 +000021#ifdef HAVE_BROKEN_MBSTOWCS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000022 /* 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);
Martin v. Löwis011e8422009-05-05 04:43:17 +000027#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000028 size_t argsize = mbstowcs(NULL, arg, 0);
Martin v. Löwis011e8422009-05-05 04:43:17 +000029#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000030 size_t count;
31 unsigned char *in;
32 wchar_t *out;
Martin v. Löwis011e8422009-05-05 04:43:17 +000033#ifdef HAVE_MBRTOWC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000034 mbstate_t mbs;
Martin v. Löwis011e8422009-05-05 04:43:17 +000035#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000036 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 wchar_t *tmp;
43 /* Only use the result if it contains no
44 surrogate characters. */
45 for (tmp = res; *tmp != 0 &&
46 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
47 ;
48 if (*tmp == 0)
49 return res;
50 }
51 PyMem_Free(res);
52 }
53 /* Conversion failed. Fall back to escaping with surrogateescape. */
Martin v. Löwis011e8422009-05-05 04:43:17 +000054#ifdef HAVE_MBRTOWC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000055 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
56
57 /* Overallocate; as multi-byte characters are in the argument, the
58 actual output could use less memory. */
59 argsize = strlen(arg) + 1;
60 res = PyMem_Malloc(argsize*sizeof(wchar_t));
61 if (!res) goto oom;
62 in = (unsigned char*)arg;
63 out = res;
64 memset(&mbs, 0, sizeof mbs);
65 while (argsize) {
66 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
67 if (converted == 0)
68 /* Reached end of string; null char stored. */
69 break;
70 if (converted == (size_t)-2) {
71 /* Incomplete character. This should never happen,
72 since we provide everything that we have -
73 unless there is a bug in the C library, or I
74 misunderstood how mbrtowc works. */
75 fprintf(stderr, "unexpected mbrtowc result -2\n");
76 return NULL;
77 }
78 if (converted == (size_t)-1) {
79 /* Conversion error. Escape as UTF-8b, and start over
80 in the initial shift state. */
81 *out++ = 0xdc00 + *in++;
82 argsize--;
83 memset(&mbs, 0, sizeof mbs);
84 continue;
85 }
86 if (*out >= 0xd800 && *out <= 0xdfff) {
87 /* Surrogate character. Escape the original
88 byte sequence with surrogateescape. */
89 argsize -= converted;
90 while (converted--)
91 *out++ = 0xdc00 + *in++;
92 continue;
93 }
94 /* successfully converted some bytes */
95 in += converted;
96 argsize -= converted;
97 out++;
98 }
Martin v. Löwis011e8422009-05-05 04:43:17 +000099#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000100 /* Cannot use C locale for escaping; manually escape as if charset
101 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
102 correctly in the locale's charset, which must be an ASCII superset. */
103 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
104 if (!res) goto oom;
105 in = (unsigned char*)arg;
106 out = res;
107 while(*in)
108 if(*in < 128)
109 *out++ = *in++;
110 else
111 *out++ = 0xdc00 + *in++;
112 *out = 0;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000113#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000114 return res;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000115oom:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000116 fprintf(stderr, "out of memory\n");
117 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000118}
119
Guido van Rossum7c141031997-08-15 02:52:08 +0000120int
Fredrik Lundhfaa209d62000-07-09 20:35:15 +0000121main(int argc, char **argv)
Guido van Rossum4c04be61997-07-19 19:25:33 +0000122{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000123 wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
124 /* We need a second copies, as Python might modify the first one. */
125 wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
126 int i, res;
127 char *oldloc;
128 /* 754 requires that FP exceptions run in "no stop" mode by default,
129 * and until C vendors implement C99's ways to control FP exceptions,
130 * Python requires non-stop mode. Alas, some platforms enable FP
131 * exceptions by default. Here we disable them.
132 */
Tim Peters4643bd92002-12-28 21:56:08 +0000133#ifdef __FreeBSD__
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000134 fp_except_t m;
Tim Peters4643bd92002-12-28 21:56:08 +0000135
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000136 m = fpgetmask();
137 fpsetmask(m & ~FP_X_OFL);
Tim Peters4643bd92002-12-28 21:56:08 +0000138#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000139 if (!argv_copy || !argv_copy2) {
140 fprintf(stderr, "out of memory\n");
141 return 1;
142 }
143 oldloc = strdup(setlocale(LC_ALL, NULL));
144 setlocale(LC_ALL, "");
145 for (i = 0; i < argc; i++) {
146 argv_copy2[i] = argv_copy[i] = char2wchar(argv[i]);
147 if (!argv_copy[i])
148 return 1;
149 }
150 setlocale(LC_ALL, oldloc);
151 free(oldloc);
152 res = Py_Main(argc, argv_copy);
153 for (i = 0; i < argc; i++) {
154 PyMem_Free(argv_copy2[i]);
155 }
156 PyMem_Free(argv_copy);
157 PyMem_Free(argv_copy2);
158 return res;
Guido van Rossum4c04be61997-07-19 19:25:33 +0000159}
Martin v. Löwis790465f2008-04-05 20:41:37 +0000160#endif