You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.7 KiB

  1. /* Minimal main program -- everything is loaded from the library */
  2. #include "Python.h"
  3. #include <locale.h>
  4. #ifdef __FreeBSD__
  5. #include <floatingpoint.h>
  6. #endif
  7. #ifdef MS_WINDOWS
  8. int
  9. wmain(int argc, wchar_t **argv)
  10. {
  11. return Py_Main(argc, argv);
  12. }
  13. #else
  14. static wchar_t*
  15. char2wchar(char* arg)
  16. {
  17. wchar_t *res;
  18. #ifdef HAVE_BROKEN_MBSTOWCS
  19. /* Some platforms have a broken implementation of
  20. * mbstowcs which does not count the characters that
  21. * would result from conversion. Use an upper bound.
  22. */
  23. size_t argsize = strlen(arg);
  24. #else
  25. size_t argsize = mbstowcs(NULL, arg, 0);
  26. #endif
  27. size_t count;
  28. unsigned char *in;
  29. wchar_t *out;
  30. #ifdef HAVE_MBRTOWC
  31. mbstate_t mbs;
  32. #endif
  33. if (argsize != (size_t)-1) {
  34. res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
  35. if (!res)
  36. goto oom;
  37. count = mbstowcs(res, arg, argsize+1);
  38. if (count != (size_t)-1) {
  39. wchar_t *tmp;
  40. /* Only use the result if it contains no
  41. surrogate characters. */
  42. for (tmp = res; *tmp != 0 &&
  43. (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
  44. ;
  45. if (*tmp == 0)
  46. return res;
  47. }
  48. PyMem_Free(res);
  49. }
  50. /* Conversion failed. Fall back to escaping with surrogateescape. */
  51. #ifdef HAVE_MBRTOWC
  52. /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
  53. /* Overallocate; as multi-byte characters are in the argument, the
  54. actual output could use less memory. */
  55. argsize = strlen(arg) + 1;
  56. res = PyMem_Malloc(argsize*sizeof(wchar_t));
  57. if (!res) goto oom;
  58. in = (unsigned char*)arg;
  59. out = res;
  60. memset(&mbs, 0, sizeof mbs);
  61. while (argsize) {
  62. size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
  63. if (converted == 0)
  64. /* Reached end of string; null char stored. */
  65. break;
  66. if (converted == (size_t)-2) {
  67. /* Incomplete character. This should never happen,
  68. since we provide everything that we have -
  69. unless there is a bug in the C library, or I
  70. misunderstood how mbrtowc works. */
  71. fprintf(stderr, "unexpected mbrtowc result -2\n");
  72. return NULL;
  73. }
  74. if (converted == (size_t)-1) {
  75. /* Conversion error. Escape as UTF-8b, and start over
  76. in the initial shift state. */
  77. *out++ = 0xdc00 + *in++;
  78. argsize--;
  79. memset(&mbs, 0, sizeof mbs);
  80. continue;
  81. }
  82. if (*out >= 0xd800 && *out <= 0xdfff) {
  83. /* Surrogate character. Escape the original
  84. byte sequence with surrogateescape. */
  85. argsize -= converted;
  86. while (converted--)
  87. *out++ = 0xdc00 + *in++;
  88. continue;
  89. }
  90. /* successfully converted some bytes */
  91. in += converted;
  92. argsize -= converted;
  93. out++;
  94. }
  95. #else
  96. /* Cannot use C locale for escaping; manually escape as if charset
  97. is ASCII (i.e. escape all bytes > 128. This will still roundtrip
  98. correctly in the locale's charset, which must be an ASCII superset. */
  99. res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
  100. if (!res) goto oom;
  101. in = (unsigned char*)arg;
  102. out = res;
  103. while(*in)
  104. if(*in < 128)
  105. *out++ = *in++;
  106. else
  107. *out++ = 0xdc00 + *in++;
  108. *out = 0;
  109. #endif
  110. return res;
  111. oom:
  112. fprintf(stderr, "out of memory\n");
  113. return NULL;
  114. }
  115. int
  116. main(int argc, char **argv)
  117. {
  118. wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
  119. /* We need a second copies, as Python might modify the first one. */
  120. wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
  121. int i, res;
  122. char *oldloc;
  123. /* 754 requires that FP exceptions run in "no stop" mode by default,
  124. * and until C vendors implement C99's ways to control FP exceptions,
  125. * Python requires non-stop mode. Alas, some platforms enable FP
  126. * exceptions by default. Here we disable them.
  127. */
  128. #ifdef __FreeBSD__
  129. fp_except_t m;
  130. m = fpgetmask();
  131. fpsetmask(m & ~FP_X_OFL);
  132. #endif
  133. if (!argv_copy || !argv_copy2) {
  134. fprintf(stderr, "out of memory\n");
  135. return 1;
  136. }
  137. oldloc = strdup(setlocale(LC_ALL, NULL));
  138. setlocale(LC_ALL, "");
  139. for (i = 0; i < argc; i++) {
  140. argv_copy2[i] = argv_copy[i] = char2wchar(argv[i]);
  141. if (!argv_copy[i])
  142. return 1;
  143. }
  144. setlocale(LC_ALL, oldloc);
  145. free(oldloc);
  146. res = Py_Main(argc, argv_copy);
  147. for (i = 0; i < argc; i++) {
  148. PyMem_Free(argv_copy2[i]);
  149. }
  150. PyMem_Free(argv_copy);
  151. PyMem_Free(argv_copy2);
  152. return res;
  153. }
  154. #endif