PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/racket/src/racket/src/string.c

http://github.com/plt/racket
C | 5610 lines | 4599 code | 714 blank | 297 comment | 1058 complexity | 5a88f05530bc3f678e0a97f47d3bb508 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, BSD-3-Clause, CC-BY-SA-3.0
  1. #include "schpriv.h"
  2. #include "schvers.h"
  3. #include "schrktio.h"
  4. #include <string.h>
  5. #include <ctype.h>
  6. #ifdef NO_ERRNO_GLOBAL
  7. # define errno -1
  8. #else
  9. # include <errno.h>
  10. #endif
  11. #ifndef SCHEME_PLATFORM_LIBRARY_SUBPATH
  12. # include "schsys.h"
  13. #endif
  14. #ifndef SPLS_SUFFIX
  15. # define SPLS_SUFFIX ""
  16. #endif
  17. #include "schustr.inc"
  18. #ifdef MACOS_UNICODE_SUPPORT
  19. # define mzLOCALE_IS_UTF_8(s) (!s || !(*s))
  20. #endif
  21. #ifdef WINDOWS_UNICODE_SUPPORT
  22. # define mzLOCALE_IS_UTF_8(s) (!s || !(*s))
  23. #endif
  24. #ifndef mzLOCALE_IS_UTF_8
  25. # define mzLOCALE_IS_UTF_8(s) (!(rktio_convert_properties(scheme_rktio) & RKTIO_CONVERTER_SUPPORTED))
  26. #endif
  27. #define mzICONV_KIND 0
  28. #define mzUTF8_KIND 1
  29. #define mzUTF8_TO_UTF16_KIND 2
  30. #define mzUTF16_TO_UTF8_KIND 3
  31. typedef struct Scheme_Converter {
  32. Scheme_Object so;
  33. short closed;
  34. short kind;
  35. rktio_converter_t *cd;
  36. int permissive;
  37. Scheme_Custodian_Reference *mref;
  38. } Scheme_Converter;
  39. Scheme_Object *scheme_system_type_proc;
  40. static Scheme_Object *make_string (int argc, Scheme_Object *argv[]);
  41. static Scheme_Object *string (int argc, Scheme_Object *argv[]);
  42. static Scheme_Object *string_p (int argc, Scheme_Object *argv[]);
  43. static Scheme_Object *string_length (int argc, Scheme_Object *argv[]);
  44. static Scheme_Object *string_eq (int argc, Scheme_Object *argv[]);
  45. static Scheme_Object *string_locale_eq (int argc, Scheme_Object *argv[]);
  46. static Scheme_Object *string_ci_eq (int argc, Scheme_Object *argv[]);
  47. static Scheme_Object *string_locale_ci_eq (int argc, Scheme_Object *argv[]);
  48. static Scheme_Object *string_lt (int argc, Scheme_Object *argv[]);
  49. static Scheme_Object *string_locale_lt (int argc, Scheme_Object *argv[]);
  50. static Scheme_Object *string_gt (int argc, Scheme_Object *argv[]);
  51. static Scheme_Object *string_locale_gt (int argc, Scheme_Object *argv[]);
  52. static Scheme_Object *string_lt_eq (int argc, Scheme_Object *argv[]);
  53. static Scheme_Object *string_gt_eq (int argc, Scheme_Object *argv[]);
  54. static Scheme_Object *string_ci_lt (int argc, Scheme_Object *argv[]);
  55. static Scheme_Object *string_locale_ci_lt (int argc, Scheme_Object *argv[]);
  56. static Scheme_Object *string_ci_gt (int argc, Scheme_Object *argv[]);
  57. static Scheme_Object *string_locale_ci_gt (int argc, Scheme_Object *argv[]);
  58. static Scheme_Object *string_ci_lt_eq (int argc, Scheme_Object *argv[]);
  59. static Scheme_Object *string_ci_gt_eq (int argc, Scheme_Object *argv[]);
  60. static Scheme_Object *string_upcase (int argc, Scheme_Object *argv[]);
  61. static Scheme_Object *string_downcase (int argc, Scheme_Object *argv[]);
  62. static Scheme_Object *string_titlecase (int argc, Scheme_Object *argv[]);
  63. static Scheme_Object *string_foldcase (int argc, Scheme_Object *argv[]);
  64. static Scheme_Object *string_locale_upcase (int argc, Scheme_Object *argv[]);
  65. static Scheme_Object *string_locale_downcase (int argc, Scheme_Object *argv[]);
  66. static Scheme_Object *substring (int argc, Scheme_Object *argv[]);
  67. static Scheme_Object *string_append (int argc, Scheme_Object *argv[]);
  68. static Scheme_Object *string_append_immutable (int argc, Scheme_Object *argv[]);
  69. static Scheme_Object *string_to_list (int argc, Scheme_Object *argv[]);
  70. static Scheme_Object *list_to_string (int argc, Scheme_Object *argv[]);
  71. static Scheme_Object *string_copy (int argc, Scheme_Object *argv[]);
  72. static Scheme_Object *string_copy_bang (int argc, Scheme_Object *argv[]);
  73. static Scheme_Object *string_fill (int argc, Scheme_Object *argv[]);
  74. static Scheme_Object *string_to_immutable (int argc, Scheme_Object *argv[]);
  75. static Scheme_Object *string_normalize_c (int argc, Scheme_Object *argv[]);
  76. static Scheme_Object *string_normalize_kc (int argc, Scheme_Object *argv[]);
  77. static Scheme_Object *string_normalize_d (int argc, Scheme_Object *argv[]);
  78. static Scheme_Object *string_normalize_kd (int argc, Scheme_Object *argv[]);
  79. static Scheme_Object *make_shared_byte_string (int argc, Scheme_Object *argv[]);
  80. static Scheme_Object *shared_byte_string (int argc, Scheme_Object *argv[]);
  81. static Scheme_Object *make_byte_string (int argc, Scheme_Object *argv[]);
  82. static Scheme_Object *byte_string (int argc, Scheme_Object *argv[]);
  83. static Scheme_Object *byte_p (int argc, Scheme_Object *argv[]);
  84. static Scheme_Object *byte_string_p (int argc, Scheme_Object *argv[]);
  85. static Scheme_Object *byte_string_length (int argc, Scheme_Object *argv[]);
  86. static Scheme_Object *byte_string_eq (int argc, Scheme_Object *argv[]);
  87. static Scheme_Object *byte_string_lt (int argc, Scheme_Object *argv[]);
  88. static Scheme_Object *byte_string_gt (int argc, Scheme_Object *argv[]);
  89. static Scheme_Object *byte_substring (int argc, Scheme_Object *argv[]);
  90. static Scheme_Object *byte_string_append (int argc, Scheme_Object *argv[]);
  91. static Scheme_Object *byte_string_to_list (int argc, Scheme_Object *argv[]);
  92. static Scheme_Object *list_to_byte_string (int argc, Scheme_Object *argv[]);
  93. static Scheme_Object *byte_string_copy (int argc, Scheme_Object *argv[]);
  94. static Scheme_Object *byte_string_copy_bang (int argc, Scheme_Object *argv[]);
  95. static Scheme_Object *byte_string_fill (int argc, Scheme_Object *argv[]);
  96. static Scheme_Object *byte_string_to_immutable (int argc, Scheme_Object *argv[]);
  97. static Scheme_Object *byte_string_utf8_index (int argc, Scheme_Object *argv[]);
  98. static Scheme_Object *byte_string_utf8_ref (int argc, Scheme_Object *argv[]);
  99. static Scheme_Object *byte_string_utf8_length (int argc, Scheme_Object *argv[]);
  100. static Scheme_Object *byte_string_to_char_string (int argc, Scheme_Object *argv[]);
  101. static Scheme_Object *byte_string_to_char_string_locale (int argc, Scheme_Object *argv[]);
  102. static Scheme_Object *byte_string_to_char_string_latin1 (int argc, Scheme_Object *argv[]);
  103. static Scheme_Object *char_string_to_byte_string (int argc, Scheme_Object *argv[]);
  104. static Scheme_Object *char_string_to_byte_string_locale (int argc, Scheme_Object *argv[]);
  105. static Scheme_Object *char_string_to_byte_string_latin1 (int argc, Scheme_Object *argv[]);
  106. static Scheme_Object *char_string_utf8_length (int argc, Scheme_Object *argv[]);
  107. static Scheme_Object *version(int argc, Scheme_Object *argv[]);
  108. static Scheme_Object *format(int argc, Scheme_Object *argv[]);
  109. static Scheme_Object *sch_printf(int argc, Scheme_Object *argv[]);
  110. static Scheme_Object *sch_eprintf(int argc, Scheme_Object *argv[]);
  111. static Scheme_Object *sch_fprintf(int argc, Scheme_Object *argv[]);
  112. static Scheme_Object *banner(int argc, Scheme_Object *argv[]);
  113. static Scheme_Object *env_p(int argc, Scheme_Object *argv[]);
  114. static Scheme_Object *sch_getenv(int argc, Scheme_Object *argv[]);
  115. static Scheme_Object *sch_getenv_names(int argc, Scheme_Object *argv[]);
  116. static Scheme_Object *sch_putenv(int argc, Scheme_Object *argv[]);
  117. static Scheme_Object *env_copy(int argc, Scheme_Object *argv[]);
  118. static Scheme_Object *env_make(int argc, Scheme_Object *argv[]);
  119. static Scheme_Object *current_environment_variables(int argc, Scheme_Object *argv[]);
  120. static Scheme_Object *system_type(int argc, Scheme_Object *argv[]);
  121. static Scheme_Object *system_library_subpath(int argc, Scheme_Object *argv[]);
  122. static Scheme_Object *cmdline_args(int argc, Scheme_Object *argv[]);
  123. static Scheme_Object *current_locale(int argc, Scheme_Object *argv[]);
  124. static Scheme_Object *locale_string_encoding(int argc, Scheme_Object *argv[]);
  125. static Scheme_Object *system_language_country(int argc, Scheme_Object *argv[]);
  126. static Scheme_Object *byte_string_open_converter(int argc, Scheme_Object *argv[]);
  127. static Scheme_Object *byte_string_close_converter(int argc, Scheme_Object *argv[]);
  128. static Scheme_Object *byte_string_convert(int argc, Scheme_Object *argv[]);
  129. static Scheme_Object *byte_string_convert_end(int argc, Scheme_Object *argv[]);
  130. static Scheme_Object *byte_converter_p(int argc, Scheme_Object *argv[]);
  131. static Scheme_Object *path_lt (int argc, Scheme_Object *argv[]);
  132. #ifdef MZ_PRECISE_GC
  133. static void register_traversers(void);
  134. #endif
  135. static int mz_char_strcmp(const char *who, const mzchar *str1, intptr_t l1, const mzchar *str2, intptr_t l2, int locale, int size_shortcut);
  136. static int mz_char_strcmp_ci(const char *who, const mzchar *str1, intptr_t l1, const mzchar *str2, intptr_t l2, int locale, int size_shortcut);
  137. static int mz_strcmp(const char *who, unsigned char *str1, intptr_t l1, unsigned char *str2, intptr_t l2);
  138. XFORM_NONGCING static intptr_t utf8_decode_x(const unsigned char *s, intptr_t start, intptr_t end,
  139. unsigned int *us, intptr_t dstart, intptr_t dend,
  140. intptr_t *ipos, intptr_t *jpos,
  141. char compact, char utf16,
  142. int *state, int might_continue, int permissive);
  143. XFORM_NONGCING static intptr_t utf8_encode_x(const unsigned int *us, intptr_t start, intptr_t end,
  144. unsigned char *s, intptr_t dstart, intptr_t dend,
  145. intptr_t *_ipos, intptr_t *_opos, char utf16);
  146. static char *string_to_from_locale(int to_bytes,
  147. char *in, intptr_t delta, intptr_t len,
  148. intptr_t *olen, int perm,
  149. int *no_cvt);
  150. static void cache_locale_or_close(int to_bytes, rktio_converter_t *cd, char *le);
  151. #define portable_isspace(x) (((x) < 128) && isspace(x))
  152. ROSYM static Scheme_Object *sys_symbol;
  153. ROSYM static Scheme_Object *link_symbol, *machine_symbol, *vm_symbol, *gc_symbol;
  154. ROSYM static Scheme_Object *so_suffix_symbol, *so_mode_symbol, *word_symbol;
  155. ROSYM static Scheme_Object *os_symbol, *fs_change_symbol, *target_machine_symbol, *cross_symbol;
  156. ROSYM static Scheme_Object *racket_symbol, *cgc_symbol, *_3m_symbol, *cs_symbol;
  157. ROSYM static Scheme_Object *force_symbol, *infer_symbol;
  158. ROSYM static Scheme_Object *platform_3m_path, *platform_cgc_path, *platform_cs_path;
  159. READ_ONLY static Scheme_Object *zero_length_char_string;
  160. READ_ONLY static Scheme_Object *zero_length_char_immutable_string;
  161. READ_ONLY static Scheme_Object *zero_length_byte_string;
  162. SHARED_OK static char *embedding_banner;
  163. SHARED_OK static Scheme_Object *vers_str;
  164. SHARED_OK static Scheme_Object *banner_str;
  165. THREAD_LOCAL_DECL(static Scheme_Object *fs_change_props);
  166. THREAD_LOCAL_DECL(static char *cached_locale_encoding_name);
  167. THREAD_LOCAL_DECL(struct rktio_converter_t *cached_locale_to_converter);
  168. THREAD_LOCAL_DECL(struct rktio_converter_t *cached_locale_from_converter);
  169. READ_ONLY static Scheme_Object *complete_symbol, *continues_symbol, *aborts_symbol, *error_symbol;
  170. READ_ONLY Scheme_Object *scheme_string_p_proc;
  171. READ_ONLY Scheme_Object *scheme_byte_string_p_proc;
  172. READ_ONLY static int cross_compile_mode;
  173. /* These two locale variables are only valid when reset_locale()
  174. is called after continuation marks (and hence parameterization)
  175. may have changed. Similarly, setlocale() is only up-to-date
  176. when reset_locale() has been called. */
  177. THREAD_LOCAL_DECL(static int locale_on);
  178. THREAD_LOCAL_DECL(static void *current_locale_name_ptr);
  179. static void reset_locale(void);
  180. #define current_locale_name ((const mzchar *)current_locale_name_ptr)
  181. static const mzchar empty_char_string[1] = { 0 };
  182. static const mzchar xes_char_string[2] = { 0x78787878, 0 };
  183. void
  184. scheme_init_string (Scheme_Startup_Env *env)
  185. {
  186. Scheme_Object *p;
  187. REGISTER_SO(sys_symbol);
  188. sys_symbol = scheme_intern_symbol(SYSTEM_TYPE_NAME);
  189. REGISTER_SO(link_symbol);
  190. REGISTER_SO(machine_symbol);
  191. REGISTER_SO(gc_symbol);
  192. REGISTER_SO(vm_symbol);
  193. REGISTER_SO(so_suffix_symbol);
  194. REGISTER_SO(so_mode_symbol);
  195. REGISTER_SO(word_symbol);
  196. REGISTER_SO(os_symbol);
  197. REGISTER_SO(fs_change_symbol);
  198. REGISTER_SO(target_machine_symbol);
  199. REGISTER_SO(cross_symbol);
  200. link_symbol = scheme_intern_symbol("link");
  201. machine_symbol = scheme_intern_symbol("machine");
  202. vm_symbol = scheme_intern_symbol("vm");
  203. gc_symbol = scheme_intern_symbol("gc");
  204. so_suffix_symbol = scheme_intern_symbol("so-suffix");
  205. so_mode_symbol = scheme_intern_symbol("so-mode");
  206. word_symbol = scheme_intern_symbol("word");
  207. os_symbol = scheme_intern_symbol("os");
  208. fs_change_symbol = scheme_intern_symbol("fs-change");
  209. target_machine_symbol = scheme_intern_symbol("target-machine");
  210. cross_symbol = scheme_intern_symbol("cross");
  211. REGISTER_SO(racket_symbol);
  212. REGISTER_SO(cgc_symbol);
  213. REGISTER_SO(_3m_symbol);
  214. REGISTER_SO(cs_symbol);
  215. racket_symbol = scheme_intern_symbol("racket");
  216. cgc_symbol = scheme_intern_symbol("cgc");
  217. _3m_symbol = scheme_intern_symbol("3m");
  218. cs_symbol = scheme_intern_symbol("cs");
  219. REGISTER_SO(force_symbol);
  220. REGISTER_SO(infer_symbol);
  221. force_symbol = scheme_intern_symbol("force");
  222. infer_symbol = scheme_intern_symbol("infer");
  223. REGISTER_SO(zero_length_char_string);
  224. REGISTER_SO(zero_length_char_immutable_string);
  225. REGISTER_SO(zero_length_byte_string);
  226. zero_length_char_string = scheme_alloc_char_string(0, 0);
  227. zero_length_char_immutable_string = scheme_alloc_char_string(0, 0);
  228. SCHEME_SET_CHAR_STRING_IMMUTABLE(zero_length_char_immutable_string);
  229. zero_length_byte_string = scheme_alloc_byte_string(0, 0);
  230. REGISTER_SO(complete_symbol);
  231. REGISTER_SO(continues_symbol);
  232. REGISTER_SO(aborts_symbol);
  233. REGISTER_SO(error_symbol);
  234. complete_symbol = scheme_intern_symbol("complete");
  235. continues_symbol = scheme_intern_symbol("continues");
  236. aborts_symbol = scheme_intern_symbol("aborts");
  237. error_symbol = scheme_intern_symbol("error");
  238. REGISTER_SO(platform_3m_path);
  239. # ifdef DOS_FILE_SYSTEM
  240. # define MZ3M_SUBDIR "\\3m"
  241. # define MZCS_SUBDIR "\\cs"
  242. # else
  243. # define MZ3M_SUBDIR "/3m"
  244. # define MZCS_SUBDIR "/cs"
  245. #endif
  246. REGISTER_SO(platform_3m_path);
  247. REGISTER_SO(platform_cgc_path);
  248. REGISTER_SO(platform_cs_path);
  249. platform_cgc_path = scheme_make_path(SCHEME_PLATFORM_LIBRARY_SUBPATH SPLS_SUFFIX);
  250. platform_3m_path = scheme_make_path(SCHEME_PLATFORM_LIBRARY_SUBPATH SPLS_SUFFIX MZ3M_SUBDIR);
  251. platform_cs_path = scheme_make_path(SCHEME_PLATFORM_LIBRARY_SUBPATH SPLS_SUFFIX MZCS_SUBDIR);
  252. REGISTER_SO(embedding_banner);
  253. REGISTER_SO(vers_str);
  254. REGISTER_SO(banner_str);
  255. vers_str = scheme_make_utf8_string(scheme_version());
  256. SCHEME_SET_CHAR_STRING_IMMUTABLE(vers_str);
  257. banner_str = scheme_make_utf8_string(scheme_banner());
  258. SCHEME_SET_CHAR_STRING_IMMUTABLE(banner_str);
  259. REGISTER_SO(scheme_string_p_proc);
  260. p = scheme_make_folding_prim(string_p, "string?", 1, 1, 1);
  261. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_UNARY_INLINED
  262. | SCHEME_PRIM_IS_OMITABLE
  263. | SCHEME_PRIM_PRODUCES_BOOL);
  264. scheme_addto_prim_instance("string?", p, env);
  265. scheme_string_p_proc = p;
  266. scheme_addto_prim_instance("make-string",
  267. scheme_make_immed_prim(make_string,
  268. "make-string",
  269. 1, 2),
  270. env);
  271. scheme_addto_prim_instance("string",
  272. scheme_make_immed_prim(string,
  273. "string",
  274. 0, -1),
  275. env);
  276. p = scheme_make_folding_prim(string_length, "string-length", 1, 1, 1);
  277. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_UNARY_INLINED
  278. |SCHEME_PRIM_PRODUCES_FIXNUM
  279. | SCHEME_PRIM_AD_HOC_OPT);
  280. scheme_addto_prim_instance("string-length", p,
  281. env);
  282. p = scheme_make_immed_prim(scheme_checked_string_ref, "string-ref", 2, 2);
  283. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_BINARY_INLINED
  284. | SCHEME_PRIM_AD_HOC_OPT);
  285. scheme_addto_prim_instance("string-ref", p, env);
  286. p = scheme_make_immed_prim(scheme_checked_string_set, "string-set!", 3, 3);
  287. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_NARY_INLINED
  288. | SCHEME_PRIM_AD_HOC_OPT);
  289. scheme_addto_prim_instance("string-set!", p, env);
  290. p = scheme_make_immed_prim(string_eq, "string=?", 1, -1);
  291. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_BINARY_INLINED
  292. | SCHEME_PRIM_PRODUCES_BOOL
  293. | SCHEME_PRIM_PRODUCES_BOOL);
  294. scheme_addto_prim_instance("string=?", p, env);
  295. scheme_addto_prim_instance("string-locale=?",
  296. scheme_make_immed_prim(string_locale_eq,
  297. "string-locale=?",
  298. 1, -1),
  299. env);
  300. scheme_addto_prim_instance("string-ci=?",
  301. scheme_make_immed_prim(string_ci_eq,
  302. "string-ci=?",
  303. 1, -1),
  304. env);
  305. scheme_addto_prim_instance("string-locale-ci=?",
  306. scheme_make_immed_prim(string_locale_ci_eq,
  307. "string-locale-ci=?",
  308. 1, -1),
  309. env);
  310. scheme_addto_prim_instance("string<?",
  311. scheme_make_immed_prim(string_lt,
  312. "string<?",
  313. 1, -1),
  314. env);
  315. scheme_addto_prim_instance("string-locale<?",
  316. scheme_make_immed_prim(string_locale_lt,
  317. "string-locale<?",
  318. 1, -1),
  319. env);
  320. scheme_addto_prim_instance("string>?",
  321. scheme_make_immed_prim(string_gt,
  322. "string>?",
  323. 1, -1),
  324. env);
  325. scheme_addto_prim_instance("string-locale>?",
  326. scheme_make_immed_prim(string_locale_gt,
  327. "string-locale>?",
  328. 1, -1),
  329. env);
  330. scheme_addto_prim_instance("string<=?",
  331. scheme_make_immed_prim(string_lt_eq,
  332. "string<=?",
  333. 1, -1),
  334. env);
  335. scheme_addto_prim_instance("string>=?",
  336. scheme_make_immed_prim(string_gt_eq,
  337. "string>=?",
  338. 1, -1),
  339. env);
  340. scheme_addto_prim_instance("string-ci<?",
  341. scheme_make_immed_prim(string_ci_lt,
  342. "string-ci<?",
  343. 1, -1),
  344. env);
  345. scheme_addto_prim_instance("string-locale-ci<?",
  346. scheme_make_immed_prim(string_locale_ci_lt,
  347. "string-locale-ci<?",
  348. 1, -1),
  349. env);
  350. scheme_addto_prim_instance("string-ci>?",
  351. scheme_make_immed_prim(string_ci_gt,
  352. "string-ci>?",
  353. 1, -1),
  354. env);
  355. scheme_addto_prim_instance("string-locale-ci>?",
  356. scheme_make_immed_prim(string_locale_ci_gt,
  357. "string-locale-ci>?",
  358. 1, -1),
  359. env);
  360. scheme_addto_prim_instance("string-ci<=?",
  361. scheme_make_immed_prim(string_ci_lt_eq,
  362. "string-ci<=?",
  363. 1, -1),
  364. env);
  365. scheme_addto_prim_instance("string-ci>=?",
  366. scheme_make_immed_prim(string_ci_gt_eq,
  367. "string-ci>=?",
  368. 1, -1),
  369. env);
  370. scheme_addto_prim_instance("substring",
  371. scheme_make_immed_prim(substring,
  372. "substring",
  373. 2, 3),
  374. env);
  375. p = scheme_make_immed_prim(string_append, "string-append", 0, -1);
  376. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_AD_HOC_OPT);
  377. scheme_addto_prim_instance("string-append", p, env);
  378. p = scheme_make_immed_prim(string_append_immutable, "string-append-immutable", 0, -1);
  379. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_AD_HOC_OPT);
  380. scheme_addto_prim_instance("string-append-immutable", p, env);
  381. scheme_addto_prim_instance("string->list",
  382. scheme_make_immed_prim(string_to_list,
  383. "string->list",
  384. 1, 1),
  385. env);
  386. scheme_addto_prim_instance("list->string",
  387. scheme_make_immed_prim(list_to_string,
  388. "list->string",
  389. 1, 1),
  390. env);
  391. scheme_addto_prim_instance("string-copy",
  392. scheme_make_immed_prim(string_copy,
  393. "string-copy",
  394. 1, 1),
  395. env);
  396. scheme_addto_prim_instance("string-copy!",
  397. scheme_make_immed_prim(string_copy_bang,
  398. "string-copy!",
  399. 3, 5),
  400. env);
  401. scheme_addto_prim_instance("string-fill!",
  402. scheme_make_immed_prim(string_fill,
  403. "string-fill!",
  404. 2, 2),
  405. env);
  406. p = scheme_make_immed_prim(string_to_immutable, "string->immutable-string", 1, 1);
  407. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_AD_HOC_OPT);
  408. scheme_addto_prim_instance("string->immutable-string", p, env);
  409. scheme_addto_prim_instance("string-normalize-nfc",
  410. scheme_make_immed_prim(string_normalize_c,
  411. "string-normalize-nfc",
  412. 1, 1),
  413. env);
  414. scheme_addto_prim_instance("string-normalize-nfkc",
  415. scheme_make_immed_prim(string_normalize_kc,
  416. "string-normalize-nfkc",
  417. 1, 1),
  418. env);
  419. scheme_addto_prim_instance("string-normalize-nfd",
  420. scheme_make_immed_prim(string_normalize_d,
  421. "string-normalize-nfd",
  422. 1, 1),
  423. env);
  424. scheme_addto_prim_instance("string-normalize-nfkd",
  425. scheme_make_immed_prim(string_normalize_kd,
  426. "string-normalize-nfkd",
  427. 1, 1),
  428. env);
  429. scheme_addto_prim_instance("string-upcase",
  430. scheme_make_immed_prim(string_upcase,
  431. "string-upcase",
  432. 1, 1),
  433. env);
  434. scheme_addto_prim_instance("string-downcase",
  435. scheme_make_immed_prim(string_downcase,
  436. "string-downcase",
  437. 1, 1),
  438. env);
  439. scheme_addto_prim_instance("string-titlecase",
  440. scheme_make_immed_prim(string_titlecase,
  441. "string-titlecase",
  442. 1, 1),
  443. env);
  444. scheme_addto_prim_instance("string-foldcase",
  445. scheme_make_immed_prim(string_foldcase,
  446. "string-foldcase",
  447. 1, 1),
  448. env);
  449. scheme_addto_prim_instance("string-locale-upcase",
  450. scheme_make_immed_prim(string_locale_upcase,
  451. "string-locale-upcase",
  452. 1, 1),
  453. env);
  454. scheme_addto_prim_instance("string-locale-downcase",
  455. scheme_make_immed_prim(string_locale_downcase,
  456. "string-locale-downcase",
  457. 1, 1),
  458. env);
  459. scheme_addto_prim_instance("current-locale",
  460. scheme_register_parameter(current_locale,
  461. "current-locale",
  462. MZCONFIG_LOCALE),
  463. env);
  464. scheme_addto_prim_instance("locale-string-encoding",
  465. scheme_make_immed_prim(locale_string_encoding,
  466. "locale-string-encoding",
  467. 0, 0),
  468. env);
  469. scheme_addto_prim_instance("system-language+country",
  470. scheme_make_immed_prim(system_language_country,
  471. "system-language+country",
  472. 0, 0),
  473. env);
  474. scheme_addto_prim_instance("bytes-converter?",
  475. scheme_make_immed_prim(byte_converter_p,
  476. "bytes-converter?",
  477. 1, 1),
  478. env);
  479. scheme_addto_prim_instance("bytes-convert",
  480. scheme_make_prim_w_arity2(byte_string_convert,
  481. "bytes-convert",
  482. 2, 7,
  483. 3, 3),
  484. env);
  485. scheme_addto_prim_instance("bytes-convert-end",
  486. scheme_make_prim_w_arity2(byte_string_convert_end,
  487. "bytes-convert-end",
  488. 1, 4,
  489. 2, 2),
  490. env);
  491. scheme_addto_prim_instance("bytes-open-converter",
  492. scheme_make_immed_prim(byte_string_open_converter,
  493. "bytes-open-converter",
  494. 2, 2),
  495. env);
  496. scheme_addto_prim_instance("bytes-close-converter",
  497. scheme_make_immed_prim(byte_string_close_converter,
  498. "bytes-close-converter",
  499. 1, 1),
  500. env);
  501. scheme_addto_prim_instance("format",
  502. scheme_make_noncm_prim(format,
  503. "format",
  504. 1, -1),
  505. env);
  506. scheme_addto_prim_instance("printf",
  507. scheme_make_noncm_prim(sch_printf,
  508. "printf",
  509. 1, -1),
  510. env);
  511. scheme_addto_prim_instance("eprintf",
  512. scheme_make_noncm_prim(sch_eprintf,
  513. "eprintf",
  514. 1, -1),
  515. env);
  516. scheme_addto_prim_instance("fprintf",
  517. scheme_make_noncm_prim(sch_fprintf,
  518. "fprintf",
  519. 2, -1),
  520. env);
  521. scheme_addto_prim_instance("byte?",
  522. scheme_make_folding_prim(byte_p,
  523. "byte?",
  524. 1, 1, 1),
  525. env);
  526. REGISTER_SO(scheme_byte_string_p_proc);
  527. p = scheme_make_folding_prim(byte_string_p, "bytes?", 1, 1, 1);
  528. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_UNARY_INLINED
  529. | SCHEME_PRIM_IS_OMITABLE
  530. | SCHEME_PRIM_PRODUCES_BOOL);
  531. scheme_addto_prim_instance("bytes?", p, env);
  532. scheme_byte_string_p_proc = p;
  533. scheme_addto_prim_instance("make-bytes",
  534. scheme_make_immed_prim(make_byte_string,
  535. "make-bytes",
  536. 1, 2),
  537. env);
  538. scheme_addto_prim_instance("bytes",
  539. scheme_make_immed_prim(byte_string,
  540. "bytes",
  541. 0, -1),
  542. env);
  543. ADD_PRIM_W_ARITY("make-shared-bytes", make_shared_byte_string, 1, 2, env);
  544. ADD_PRIM_W_ARITY("shared-bytes", shared_byte_string, 0, -1, env);
  545. p = scheme_make_folding_prim(byte_string_length, "bytes-length", 1, 1, 1);
  546. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_UNARY_INLINED
  547. | SCHEME_PRIM_PRODUCES_FIXNUM
  548. | SCHEME_PRIM_AD_HOC_OPT);
  549. scheme_addto_prim_instance("bytes-length", p, env);
  550. p = scheme_make_immed_prim(scheme_checked_byte_string_ref, "bytes-ref", 2, 2);
  551. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_BINARY_INLINED
  552. | SCHEME_PRIM_PRODUCES_FIXNUM
  553. | SCHEME_PRIM_AD_HOC_OPT);
  554. scheme_addto_prim_instance("bytes-ref", p, env);
  555. p = scheme_make_immed_prim(scheme_checked_byte_string_set, "bytes-set!", 3, 3);
  556. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_NARY_INLINED
  557. | SCHEME_PRIM_AD_HOC_OPT);
  558. scheme_addto_prim_instance("bytes-set!", p, env);
  559. p = scheme_make_immed_prim(byte_string_eq, "bytes=?", 1, -1);
  560. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_IS_BINARY_INLINED
  561. | SCHEME_PRIM_PRODUCES_BOOL);
  562. scheme_addto_prim_instance("bytes=?", p, env);
  563. scheme_addto_prim_instance("bytes<?",
  564. scheme_make_immed_prim(byte_string_lt,
  565. "bytes<?",
  566. 1, -1),
  567. env);
  568. scheme_addto_prim_instance("bytes>?",
  569. scheme_make_immed_prim(byte_string_gt,
  570. "bytes>?",
  571. 1, -1),
  572. env);
  573. scheme_addto_prim_instance("subbytes",
  574. scheme_make_immed_prim(byte_substring,
  575. "subbytes",
  576. 2, 3),
  577. env);
  578. p = scheme_make_immed_prim(byte_string_append, "bytes-append", 0, -1);
  579. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_AD_HOC_OPT);
  580. scheme_addto_prim_instance("bytes-append", p, env);
  581. scheme_addto_prim_instance("bytes->list",
  582. scheme_make_immed_prim(byte_string_to_list,
  583. "bytes->list",
  584. 1, 1),
  585. env);
  586. scheme_addto_prim_instance("list->bytes",
  587. scheme_make_immed_prim(list_to_byte_string,
  588. "list->bytes",
  589. 1, 1),
  590. env);
  591. scheme_addto_prim_instance("bytes-copy",
  592. scheme_make_immed_prim(byte_string_copy,
  593. "bytes-copy",
  594. 1, 1),
  595. env);
  596. scheme_addto_prim_instance("bytes-copy!",
  597. scheme_make_immed_prim(byte_string_copy_bang,
  598. "bytes-copy!",
  599. 3, 5),
  600. env);
  601. scheme_addto_prim_instance("bytes-fill!",
  602. scheme_make_immed_prim(byte_string_fill,
  603. "bytes-fill!",
  604. 2, 2),
  605. env);
  606. p = scheme_make_immed_prim(byte_string_to_immutable, "bytes->immutable-bytes", 1, 1);
  607. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_AD_HOC_OPT);
  608. scheme_addto_prim_instance("bytes->immutable-bytes", p, env);
  609. p = scheme_make_immed_prim(byte_string_utf8_index, "bytes-utf-8-index", 2, 5);
  610. /* Incorrect, since the result can be #f:
  611. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_PRODUCES_FIXNUM); */
  612. scheme_addto_prim_instance("bytes-utf-8-index", p, env);
  613. p = scheme_make_immed_prim(byte_string_utf8_length, "bytes-utf-8-length", 1, 4);
  614. /* Incorrect, since the result can be #f:
  615. SCHEME_PRIM_PROC_FLAGS(p) |= scheme_intern_prim_opt_flags(SCHEME_PRIM_PRODUCES_FIXNUM); */
  616. scheme_addto_prim_instance("bytes-utf-8-length", p, env);
  617. scheme_addto_prim_instance("bytes-utf-8-ref",
  618. scheme_make_immed_prim(byte_string_utf8_ref,
  619. "bytes-utf-8-ref",
  620. 2, 5),
  621. env);
  622. scheme_addto_prim_instance("bytes->string/utf-8",
  623. scheme_make_immed_prim(byte_string_to_char_string,
  624. "bytes->string/utf-8",
  625. 1, 4),
  626. env);
  627. scheme_addto_prim_instance("bytes->string/locale",
  628. scheme_make_immed_prim(byte_string_to_char_string_locale,
  629. "bytes->string/locale",
  630. 1, 4),
  631. env);
  632. scheme_addto_prim_instance("bytes->string/latin-1",
  633. scheme_make_immed_prim(byte_string_to_char_string_latin1,
  634. "bytes->string/latin-1",
  635. 1, 4),
  636. env);
  637. scheme_addto_prim_instance("string->bytes/utf-8",
  638. scheme_make_immed_prim(char_string_to_byte_string,
  639. "string->bytes/utf-8",
  640. 1, 4),
  641. env);
  642. scheme_addto_prim_instance("string->bytes/locale",
  643. scheme_make_immed_prim(char_string_to_byte_string_locale,
  644. "string->bytes/locale",
  645. 1, 4),
  646. env);
  647. scheme_addto_prim_instance("string->bytes/latin-1",
  648. scheme_make_immed_prim(char_string_to_byte_string_latin1,
  649. "string->bytes/latin-1",
  650. 1, 4),
  651. env);
  652. scheme_addto_prim_instance("string-utf-8-length",
  653. scheme_make_immed_prim(char_string_utf8_length,
  654. "string-utf-8-length",
  655. 1, 3),
  656. env);
  657. /* In principle, `version' could be foldable, but it invites
  658. more problems than it solves... */
  659. scheme_addto_prim_instance("version",
  660. scheme_make_immed_prim(version,
  661. "version",
  662. 0, 0),
  663. env);
  664. scheme_addto_prim_instance("banner",
  665. scheme_make_immed_prim(banner,
  666. "banner",
  667. 0, 0),
  668. env);
  669. /* Environment variables */
  670. scheme_addto_prim_instance("environment-variables?",
  671. scheme_make_folding_prim(env_p,
  672. "environment-variables?",
  673. 1, 1, 1),
  674. env);
  675. scheme_addto_prim_instance("current-environment-variables",
  676. scheme_register_parameter(current_environment_variables,
  677. "current-environment-variables",
  678. MZCONFIG_CURRENT_ENV_VARS),
  679. env);
  680. scheme_addto_prim_instance("environment-variables-ref",
  681. scheme_make_immed_prim(sch_getenv,
  682. "environment-variables-ref",
  683. 2, 2),
  684. env);
  685. scheme_addto_prim_instance("environment-variables-set!",
  686. scheme_make_prim_w_arity(sch_putenv,
  687. "environment-variables-set!",
  688. 3, 4),
  689. env);
  690. scheme_addto_prim_instance("environment-variables-names",
  691. scheme_make_immed_prim(sch_getenv_names,
  692. "environment-variables-names",
  693. 1, 1),
  694. env);
  695. scheme_addto_prim_instance("environment-variables-copy",
  696. scheme_make_immed_prim(env_copy,
  697. "environment-variables-copy",
  698. 1, 1),
  699. env);
  700. scheme_addto_prim_instance("make-environment-variables",
  701. scheme_make_immed_prim(env_make,
  702. "make-environment-variables",
  703. 0, -1),
  704. env);
  705. /* Don't make these folding, since they're platform-specific: */
  706. REGISTER_SO(scheme_system_type_proc);
  707. scheme_system_type_proc = scheme_make_immed_prim(system_type,
  708. "system-type",
  709. 0, 1);
  710. scheme_addto_prim_instance("system-type", scheme_system_type_proc, env);
  711. scheme_addto_prim_instance("system-library-subpath",
  712. scheme_make_immed_prim(system_library_subpath,
  713. "system-library-subpath",
  714. 0, 1),
  715. env);
  716. scheme_addto_prim_instance("current-command-line-arguments",
  717. scheme_register_parameter(cmdline_args,
  718. "current-command-line-arguments",
  719. MZCONFIG_CMDLINE_ARGS),
  720. env);
  721. scheme_addto_prim_instance("path<?",
  722. scheme_make_immed_prim(path_lt,
  723. "path<?",
  724. 1, -1),
  725. env);
  726. #ifdef MZ_PRECISE_GC
  727. register_traversers();
  728. #endif
  729. }
  730. void scheme_init_string_places(void) {
  731. REGISTER_SO(current_locale_name_ptr);
  732. current_locale_name_ptr = (void *)xes_char_string;
  733. REGISTER_SO(fs_change_props);
  734. {
  735. int supported, scalable, low_latency, file_level;
  736. Scheme_Object *s;
  737. scheme_fs_change_properties(&supported, &scalable, &low_latency, &file_level);
  738. fs_change_props = scheme_make_vector(4, scheme_false);
  739. if (supported) {
  740. s = scheme_intern_symbol("supported");
  741. SCHEME_VEC_ELS(fs_change_props)[0] = s;
  742. }
  743. if (scalable) {
  744. s = scheme_intern_symbol("scalable");
  745. SCHEME_VEC_ELS(fs_change_props)[1] = s;
  746. }
  747. if (low_latency) {
  748. s = scheme_intern_symbol("low-latency");
  749. SCHEME_VEC_ELS(fs_change_props)[2] = s;
  750. }
  751. if (file_level) {
  752. s = scheme_intern_symbol("file-level");
  753. SCHEME_VEC_ELS(fs_change_props)[3] = s;
  754. }
  755. SCHEME_SET_IMMUTABLE(fs_change_props);
  756. }
  757. }
  758. /**********************************************************************/
  759. /* UTF-8 char constructors */
  760. /**********************************************************************/
  761. Scheme_Object *scheme_make_sized_offset_utf8_string(char *chars, intptr_t d, intptr_t len)
  762. {
  763. intptr_t ulen;
  764. mzchar *us;
  765. if (len) {
  766. ulen = scheme_utf8_decode((unsigned char *)chars, d, d + len,
  767. NULL, 0, -1,
  768. NULL, 0 /* not UTF-16 */, 0xFFFD);
  769. us = scheme_malloc_atomic(sizeof(mzchar) * (ulen + 1));
  770. scheme_utf8_decode((unsigned char *)chars, d, d + len,
  771. us, 0, -1,
  772. NULL, 0 /* not UTF-16 */, 0xFFFD);
  773. us[ulen] = 0;
  774. } else {
  775. us = (mzchar *)empty_char_string;
  776. ulen = 0;
  777. }
  778. return scheme_make_sized_offset_char_string(us, 0, ulen, 0);
  779. }
  780. Scheme_Object *
  781. scheme_make_sized_utf8_string(char *chars, intptr_t len)
  782. {
  783. return scheme_make_sized_offset_utf8_string(chars, 0, len);
  784. }
  785. Scheme_Object *
  786. scheme_make_immutable_sized_utf8_string(char *chars, intptr_t len)
  787. {
  788. Scheme_Object *s;
  789. s = scheme_make_sized_offset_utf8_string(chars, 0, len);
  790. if (len)
  791. SCHEME_SET_CHAR_STRING_IMMUTABLE(s);
  792. return s;
  793. }
  794. Scheme_Object *
  795. scheme_make_utf8_string(const char *chars)
  796. {
  797. return scheme_make_sized_offset_utf8_string((char *)chars, 0, -1);
  798. }
  799. Scheme_Object *
  800. scheme_make_locale_string(const char *chars)
  801. {
  802. return scheme_byte_string_to_char_string_locale(scheme_make_byte_string((char *)chars));
  803. }
  804. Scheme_Object *scheme_append_strings(Scheme_Object *s1, Scheme_Object *s2)
  805. {
  806. Scheme_Object *a[2];
  807. a[0] = s1;
  808. a[1] = s2;
  809. return string_append(2, a);
  810. }
  811. /**********************************************************************/
  812. /* index helpers */
  813. /**********************************************************************/
  814. intptr_t scheme_extract_index(const char *name, int pos, int argc, Scheme_Object **argv, intptr_t top, int false_ok)
  815. {
  816. intptr_t i;
  817. int is_top = 0;
  818. if (SCHEME_INTP(argv[pos])) {
  819. i = SCHEME_INT_VAL(argv[pos]);
  820. } else if (SCHEME_BIGNUMP(argv[pos])) {
  821. if (SCHEME_BIGPOS(argv[pos])) {
  822. i = top; /* out-of-bounds */
  823. is_top = 1;
  824. } else
  825. i = -1; /* negative */
  826. } else
  827. i = -1;
  828. if (!is_top && (i < 0))
  829. scheme_wrong_contract(name,
  830. (false_ok ? "(or/c exact-nonnegative-integer? #f)" : "exact-nonnegative-integer?"),
  831. pos, argc, argv);
  832. return i;
  833. }
  834. void scheme_get_substring_indices(const char *name, Scheme_Object *str,
  835. int argc, Scheme_Object **argv,
  836. int spos, int fpos, intptr_t *_start, intptr_t *_finish)
  837. {
  838. intptr_t len;
  839. intptr_t start, finish;
  840. if (SCHEME_CHAPERONE_VECTORP(str))
  841. len = SCHEME_CHAPERONE_VEC_SIZE(str);
  842. else if (SCHEME_CHAR_STRINGP(str))
  843. len = SCHEME_CHAR_STRTAG_VAL(str);
  844. else
  845. len = SCHEME_BYTE_STRTAG_VAL(str);
  846. if (argc > spos)
  847. start = scheme_extract_index(name, spos, argc, argv, len + 1, 0);
  848. else
  849. start = 0;
  850. if (argc > fpos)
  851. finish = scheme_extract_index(name, fpos, argc, argv, len + 1, 0);
  852. else
  853. finish = len;
  854. if (!(start <= len)) {
  855. scheme_out_of_range(name, NULL, (fpos < 100) ? "starting " : "", argv[spos], str, 0, len);
  856. }
  857. if (!(finish >= start && finish <= len)) {
  858. scheme_out_of_range(name, NULL, "ending ", argv[fpos], str, start, len);
  859. }
  860. *_start = start;
  861. *_finish = finish;
  862. }
  863. void scheme_do_get_substring_indices(const char *name, Scheme_Object *str,
  864. int argc, Scheme_Object **argv,
  865. int spos, int fpos, intptr_t *_start, intptr_t *_finish, intptr_t len)
  866. {
  867. if (argc > spos) {
  868. if (SCHEME_INTP(argv[spos])) {
  869. intptr_t start = SCHEME_INT_VAL(argv[spos]);
  870. if ((start >= 0) && (start < len)) {
  871. *_start = start;
  872. if (argc > fpos) {
  873. intptr_t finish = SCHEME_INT_VAL(argv[fpos]);
  874. if ((finish >= start) && (finish <= len)) {
  875. *_finish = finish;
  876. return;
  877. }
  878. } else {
  879. *_finish = len;
  880. return;
  881. }
  882. }
  883. }
  884. } else {
  885. *_start = 0;
  886. *_finish = len;
  887. return;
  888. }
  889. scheme_get_substring_indices(name, str, argc, argv, spos, fpos, _start, _finish);
  890. }
  891. /**********************************************************************/
  892. /* char strings */
  893. /**********************************************************************/
  894. #define SCHEME_X_STR_VAL(x) SCHEME_CHAR_STR_VAL(x)
  895. #define SCHEME_X_STRTAG_VAL(x) SCHEME_CHAR_STRTAG_VAL(x)
  896. #define SCHEME_X_STRINGP(x) SCHEME_CHAR_STRINGP(x)
  897. #define SCHEME_MUTABLE_X_STRINGP(x) SCHEME_MUTABLE_CHAR_STRINGP(x)
  898. #define SCHEME_SET_X_STRING_IMMUTABLE(x) SCHEME_SET_CHAR_STRING_IMMUTABLE(x)
  899. #define scheme_x_string_type scheme_char_string_type
  900. #define X(a, b) a##_char##b
  901. #define X_(a, b) a##_##b
  902. #define X__(a) a
  903. #define EMPTY (mzchar *)"\0\0\0"
  904. #define Xchar mzchar
  905. #define uXchar mzchar
  906. #define XSTR ""
  907. #define IS_STR "string?"
  908. #define XSTRINGSTR "string"
  909. #define SUBXSTR "substring"
  910. #define CHARP(x) SCHEME_CHARP(x)
  911. #define CHAR_VAL(x) SCHEME_CHAR_VAL(x)
  912. #define CHAR_STR "char?"
  913. #define MAKE_CHAR(x) _scheme_make_char(x)
  914. #define xstrlen scheme_char_strlen
  915. #include "strops.inc"
  916. #define GEN_STRING_COMP(name, scheme_name, comp, op, ul, size_shortcut) \
  917. static Scheme_Object * name (int argc, Scheme_Object *argv[]) \
  918. { mzchar *s, *prev; int i, sl, pl; int falz = 0;\
  919. if (!SCHEME_CHAR_STRINGP(argv[0])) \
  920. scheme_wrong_contract(scheme_name, "string?", 0, argc, argv); \
  921. prev = SCHEME_CHAR_STR_VAL(argv[0]); pl = SCHEME_CHAR_STRTAG_VAL(argv[0]); \
  922. for (i = 1; i < argc; i++) { \
  923. if (!SCHEME_CHAR_STRINGP(argv[i])) \
  924. scheme_wrong_contract(scheme_name, "string?", i, argc, argv); \
  925. s = SCHEME_CHAR_STR_VAL(argv[i]); sl = SCHEME_CHAR_STRTAG_VAL(argv[i]); \
  926. if (!falz) if (!(comp(scheme_name, \
  927. prev, pl, \
  928. s, sl, ul, size_shortcut) op 0)) falz = 1; \
  929. prev = s; pl = sl; \
  930. } \
  931. return falz ? scheme_false : scheme_true; \
  932. }
  933. GEN_STRING_COMP(string_eq, "string=?", mz_char_strcmp, ==, 0, 1)
  934. GEN_STRING_COMP(string_lt, "string<?", mz_char_strcmp, <, 0, 0)
  935. GEN_STRING_COMP(string_gt, "string>?", mz_char_strcmp, >, 0, 0)
  936. GEN_STRING_COMP(string_lt_eq, "string<=?", mz_char_strcmp, <=, 0, 0)
  937. GEN_STRING_COMP(string_gt_eq, "string>=?", mz_char_strcmp, >=, 0, 0)
  938. GEN_STRING_COMP(string_ci_eq, "string-ci=?", mz_char_strcmp_ci, ==, 0, 0)
  939. GEN_STRING_COMP(string_ci_lt, "string-ci<?", mz_char_strcmp_ci, <, 0, 0)
  940. GEN_STRING_COMP(string_ci_gt, "string-ci>?", mz_char_strcmp_ci, >, 0, 0)
  941. GEN_STRING_COMP(string_ci_lt_eq, "string-ci<=?", mz_char_strcmp_ci, <=, 0, 0)
  942. GEN_STRING_COMP(string_ci_gt_eq, "string-ci>=?", mz_char_strcmp_ci, >=, 0, 0)
  943. GEN_STRING_COMP(string_locale_eq, "string-locale=?", mz_char_strcmp, ==, 1, 0)
  944. GEN_STRING_COMP(string_locale_lt, "string-locale<?", mz_char_strcmp, <, 1, 0)
  945. GEN_STRING_COMP(string_locale_gt, "string-locale>?", mz_char_strcmp, >, 1, 0)
  946. GEN_STRING_COMP(string_locale_ci_eq, "string-locale-ci=?", mz_char_strcmp_ci, ==, 1, 0)
  947. GEN_STRING_COMP(string_locale_ci_lt, "string-locale-ci<?", mz_char_strcmp_ci, <, 1, 0)
  948. GEN_STRING_COMP(string_locale_ci_gt, "string-locale-ci>?", mz_char_strcmp_ci, >, 1, 0)
  949. Scheme_Object *scheme_string_eq_2(Scheme_Object *str1, Scheme_Object *str2)
  950. {
  951. Scheme_Object *a[2];
  952. a[0] = str1;
  953. a[1] = str2;
  954. return string_eq(2, a);
  955. }
  956. Scheme_Object *string_append_immutable(int argc, Scheme_Object *argv[])
  957. {
  958. Scheme_Object *r;
  959. r = do_string_append("string-append-immutable", argc, argv);
  960. if (r == zero_length_char_string)
  961. return zero_length_char_immutable_string;
  962. SCHEME_SET_CHAR_STRING_IMMUTABLE(r);
  963. return r;
  964. }
  965. /**********************************************************************/
  966. /* byte strings */
  967. /**********************************************************************/
  968. #define SCHEME_BYTEP(x) ((SCHEME_INTP(x)) && (SCHEME_INT_VAL(x) >= 0) && (SCHEME_INT_VAL(x) <= 255))
  969. static Scheme_Object *
  970. byte_p(int argc, Scheme_Object *argv[])
  971. {
  972. return (SCHEME_BYTEP(argv[0]) ? scheme_true : scheme_false);
  973. }
  974. #define SCHEME_X_STR_VAL(x) SCHEME_BYTE_STR_VAL(x)
  975. #define SCHEME_X_STRTAG_VAL(x) SCHEME_BYTE_STRTAG_VAL(x)
  976. #define SCHEME_X_STRINGP(x) SCHEME_BYTE_STRINGP(x)
  977. #define SCHEME_MUTABLE_X_STRINGP(x) SCHEME_MUTABLE_BYTE_STRINGP(x)
  978. #define SCHEME_SET_X_STRING_IMMUTABLE(x) SCHEME_SET_BYTE_STRING_IMMUTABLE(x)
  979. #define scheme_x_string_type scheme_byte_string_type
  980. #define X(a, b) a##_byte##b
  981. #define X_(a, b) a##_byte_##b
  982. #define X__(a) byte_##a
  983. #define EMPTY ""
  984. #define Xchar char
  985. #define uXchar unsigned char
  986. #define XSTR "byte "
  987. #define IS_STR "bytes?"
  988. #define XSTRINGSTR "bytes"
  989. #define SUBXSTR "subbytes"
  990. #define CHARP(x) SCHEME_BYTEP(x)
  991. #define CHAR_VAL(x) SCHEME_INT_VAL(x)
  992. #define CHAR_STR "byte?"
  993. #define MAKE_CHAR(x) scheme_make_integer_value(x)
  994. #define xstrlen strlen
  995. #define GENERATING_BYTE
  996. #include "strops.inc"
  997. #undef GENERATING_BYTE
  998. /* comparisons */
  999. #define GEN_BYTE_STRING_PATH_COMP(name, scheme_name, comp, op, PRED, contract) \
  1000. static Scheme_Object * name (int argc, Scheme_Object *argv[]) \
  1001. { char *s, *prev; int i, sl, pl; int falz = 0;\
  1002. if (!PRED(argv[0])) \
  1003. scheme_wrong_contract(scheme_name, contract, 0, argc, argv); \
  1004. prev = SCHEME_BYTE_STR_VAL(argv[0]); pl = SCHEME_BYTE_STRTAG_VAL(argv[0]); \
  1005. for (i = 1; i < argc; i++) { \
  1006. if (!PRED(argv[i])) \
  1007. scheme_wrong_contract(scheme_name, contract, i, argc, argv); \
  1008. s = SCHEME_BYTE_STR_VAL(argv[i]); sl = SCHEME_BYTE_STRTAG_VAL(argv[i]); \
  1009. if (!falz) if (!(comp(scheme_name, \
  1010. (unsigned char *)prev, pl, \
  1011. (unsigned char *)s, sl) op 0)) falz = 1; \
  1012. prev = s; pl = sl; \
  1013. } \
  1014. return falz ? scheme_false : scheme_true; \
  1015. }
  1016. #define GEN_BYTE_STRING_COMP(name, scheme_name, comp, op) \
  1017. GEN_BYTE_STRING_PATH_COMP(name, scheme_name, comp, op, SCHEME_BYTE_STRINGP, "bytes?") \
  1018. GEN_BYTE_STRING_COMP(byte_string_eq, "bytes=?", mz_strcmp, ==)
  1019. GEN_BYTE_STRING_COMP(byte_string_lt, "bytes<?", mz_strcmp, <)
  1020. GEN_BYTE_STRING_COMP(byte_string_gt, "bytes>?", mz_strcmp, >)
  1021. GEN_BYTE_STRING_PATH_COMP(path_lt, "path<?", mz_strcmp, <, SCHEME_PATHP, "path?")
  1022. Scheme_Object *scheme_byte_string_eq_2(Scheme_Object *str1, Scheme_Object *str2)
  1023. {
  1024. Scheme_Object *a[2];
  1025. a[0] = str1;
  1026. a[1] = str2;
  1027. return byte_string_eq(2, a);
  1028. }
  1029. /**********************************************************************/
  1030. /* byte string <-> char string */
  1031. /**********************************************************************/
  1032. /************************* bytes->string *************************/
  1033. static Scheme_Object *
  1034. do_byte_string_to_char_string(const char *who,
  1035. Scheme_Object *bstr,
  1036. intptr_t istart, intptr_t ifinish,
  1037. int perm, int as_locale)
  1038. {
  1039. int i, ulen;
  1040. char *chars;
  1041. unsigned int *v;
  1042. chars = SCHEME_BYTE_STR_VAL(bstr);
  1043. ulen = utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1044. NULL, 0, -1,
  1045. NULL, NULL, 0, 0,
  1046. NULL, 0,
  1047. (perm > -1) ? 0xD800 : 0);
  1048. if (ulen < 0) {
  1049. scheme_contract_error(who,
  1050. "string is not a well-formed UTF-8 encoding",
  1051. "string", 1, bstr,
  1052. NULL);
  1053. }
  1054. v = (unsigned int *)scheme_malloc_atomic((ulen + 1) * sizeof(unsigned int));
  1055. utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1056. v, 0, -1,
  1057. NULL, NULL, 0, 0,
  1058. NULL, 0,
  1059. (perm > -1) ? 0xD800 : 0);
  1060. if (perm > -1) {
  1061. for (i = 0; i < ulen; i++) {
  1062. if (v[i] == 0xD800)
  1063. v[i] = perm;
  1064. }
  1065. }
  1066. v[ulen] = 0;
  1067. return scheme_make_sized_char_string(v, ulen, 0);
  1068. }
  1069. static Scheme_Object *
  1070. do_byte_string_to_char_string_locale(const char *who,
  1071. Scheme_Object *bstr,
  1072. intptr_t istart, intptr_t ifinish,
  1073. int perm)
  1074. {
  1075. char *us;
  1076. intptr_t olen;
  1077. reset_locale();
  1078. if (mzLOCALE_IS_UTF_8(current_locale_name) || !locale_on
  1079. || !(rktio_convert_properties(scheme_rktio) & RKTIO_CONVERTER_SUPPORTED))
  1080. return do_byte_string_to_char_string(who, bstr, istart, ifinish, perm, 1);
  1081. if (istart < ifinish) {
  1082. int no_cvt;
  1083. us = string_to_from_locale(0, SCHEME_BYTE_STR_VAL(bstr),
  1084. istart, ifinish - istart,
  1085. &olen, perm, &no_cvt);
  1086. if (!us) {
  1087. if (no_cvt) {
  1088. return do_byte_string_to_char_string(who, bstr, istart, ifinish, perm, 1);
  1089. } else {
  1090. scheme_contract_error(who,
  1091. "byte string is not a valid encoding for the current locale",
  1092. "byte string", 1, bstr,
  1093. NULL);
  1094. }
  1095. }
  1096. ((mzchar *)us)[olen] = 0;
  1097. } else {
  1098. us = "\0\0\0";
  1099. olen = 0;
  1100. }
  1101. return scheme_make_sized_char_string((mzchar *)us, olen, 0);
  1102. }
  1103. static Scheme_Object *
  1104. do_string_to_vector(const char *who, int mode, int argc, Scheme_Object *argv[])
  1105. {
  1106. int permc;
  1107. intptr_t istart, ifinish;
  1108. if (!SCHEME_BYTE_STRINGP(argv[0]))
  1109. scheme_wrong_contract(who, "bytes?", 0, argc, argv);
  1110. if ((argc < 2) || SCHEME_FALSEP(argv[1]))
  1111. permc = -1;
  1112. else {
  1113. if (!SCHEME_CHARP(argv[1]))
  1114. scheme_wrong_contract(who, "(or/c char? #f)", 1, argc, argv);
  1115. permc = SCHEME_CHAR_VAL(argv[1]);
  1116. }
  1117. scheme_get_substring_indices(who, argv[0], argc, argv,
  1118. 2, 3,
  1119. &istart, &ifinish);
  1120. if (mode == 0)
  1121. return do_byte_string_to_char_string(who, argv[0], istart, ifinish, permc, 0);
  1122. else if (mode == 1)
  1123. return do_byte_string_to_char_string_locale(who, argv[0], istart, ifinish, permc);
  1124. else {
  1125. /* Latin-1 */
  1126. mzchar *us;
  1127. unsigned char *s;
  1128. intptr_t i, len;
  1129. len = ifinish - istart;
  1130. s = (unsigned char *)SCHEME_BYTE_STR_VAL(argv[0]);
  1131. us = (mzchar *)scheme_malloc_atomic((len + 1) * sizeof(mzchar));
  1132. for (i = istart; i < ifinish; i++) {
  1133. us[i - istart] = s[i];
  1134. }
  1135. us[len] = 0;
  1136. return scheme_make_sized_char_string(us, len, 0);
  1137. }
  1138. }
  1139. static Scheme_Object *
  1140. byte_string_to_char_string (int argc, Scheme_Object *argv[])
  1141. {
  1142. return do_string_to_vector("bytes->string/utf-8", 0, argc, argv);
  1143. }
  1144. static Scheme_Object *
  1145. byte_string_to_char_string_locale (int argc, Scheme_Object *argv[])
  1146. {
  1147. return do_string_to_vector("bytes->string/locale", 1, argc, argv);
  1148. }
  1149. static Scheme_Object *
  1150. byte_string_to_char_string_latin1 (int argc, Scheme_Object *argv[])
  1151. {
  1152. return do_string_to_vector("bytes->string/latin-1", 2, argc, argv);
  1153. }
  1154. Scheme_Object *scheme_byte_string_to_char_string(Scheme_Object *o)
  1155. {
  1156. return do_byte_string_to_char_string("s->s", o, 0, SCHEME_BYTE_STRLEN_VAL(o), 0xFFFD, 0);
  1157. }
  1158. Scheme_Object *scheme_byte_string_to_char_string_locale(Scheme_Object *o)
  1159. {
  1160. return do_byte_string_to_char_string_locale("s->s", o, 0, SCHEME_BYTE_STRLEN_VAL(o), 0xFFFD);
  1161. }
  1162. /************************* string->bytes *************************/
  1163. static Scheme_Object *do_char_string_to_byte_string(Scheme_Object *s, intptr_t istart, intptr_t ifinish,
  1164. int as_locale)
  1165. {
  1166. char *bs;
  1167. int slen;
  1168. slen = scheme_utf8_encode(SCHEME_CHAR_STR_VAL(s), istart, ifinish,
  1169. NULL, 0,
  1170. 0 /* UTF-16 */);
  1171. bs = (char *)scheme_malloc_atomic(slen + 1);
  1172. scheme_utf8_encode(SCHEME_CHAR_STR_VAL(s), istart, ifinish,
  1173. (unsigned char *)bs, 0,
  1174. 0 /* UTF-16 */);
  1175. bs[slen] = 0;
  1176. return scheme_make_sized_byte_string(bs, slen, 0);
  1177. }
  1178. static Scheme_Object *
  1179. do_char_string_to_byte_string_locale(const char *who,
  1180. Scheme_Object *cstr,
  1181. intptr_t istart, intptr_t ifinish,
  1182. int perm)
  1183. {
  1184. char *s;
  1185. intptr_t olen;
  1186. reset_locale();
  1187. if (mzLOCALE_IS_UTF_8(current_locale_name) || !locale_on
  1188. || !(rktio_convert_properties(scheme_rktio) & RKTIO_CONVERTER_SUPPORTED))
  1189. return do_char_string_to_byte_string(cstr, istart, ifinish, 1);
  1190. if (istart < ifinish) {
  1191. int no_cvt;
  1192. s = string_to_from_locale(1, (char *)SCHEME_CHAR_STR_VAL(cstr),
  1193. istart, ifinish - istart,
  1194. &olen, perm, &no_cvt);
  1195. if (!s) {
  1196. if (no_cvt) {
  1197. return do_char_string_to_byte_string(cstr, istart, ifinish, 1);
  1198. } else {
  1199. scheme_contract_error(who,
  1200. "string cannot be encoded for the current locale",
  1201. "string", 1, cstr,
  1202. NULL);
  1203. }
  1204. }
  1205. s[olen] = 0;
  1206. } else {
  1207. s = "";
  1208. olen = 0;
  1209. }
  1210. return scheme_make_sized_byte_string(s, olen, 0);
  1211. }
  1212. Scheme_Object *scheme_char_string_to_byte_string(Scheme_Object *s)
  1213. {
  1214. return do_char_string_to_byte_string(s, 0, SCHEME_CHAR_STRLEN_VAL(s), 0);
  1215. }
  1216. Scheme_Object *scheme_char_string_to_byte_string_locale(Scheme_Object *s)
  1217. {
  1218. return do_char_string_to_byte_string_locale("s->s", s, 0, SCHEME_CHAR_STRLEN_VAL(s), '?');
  1219. }
  1220. static Scheme_Object *do_chars_to_bytes(const char *who, int mode,
  1221. int argc, Scheme_Object *argv[])
  1222. {
  1223. intptr_t istart, ifinish;
  1224. int permc;
  1225. if (!SCHEME_CHAR_STRINGP(argv[0]))
  1226. scheme_wrong_contract(who, "string?", 0, argc, argv);
  1227. if ((argc < 2) || SCHEME_FALSEP(argv[1]))
  1228. permc = -1;
  1229. else {
  1230. if (!SCHEME_BYTEP(argv[1]))
  1231. scheme_wrong_contract(who, "(or/c byte? #f)", 1, argc, argv);
  1232. permc = SCHEME_INT_VAL(argv[1]);
  1233. }
  1234. scheme_get_substring_indices(who, argv[0], argc, argv,
  1235. 2, 3, &istart, &ifinish);
  1236. if (mode == 1)
  1237. return do_char_string_to_byte_string_locale(who, argv[0], istart, ifinish, permc);
  1238. else if (mode == 0)
  1239. return do_char_string_to_byte_string(argv[0], istart, ifinish, 0);
  1240. else {
  1241. /* Latin-1 */
  1242. mzchar *us;
  1243. unsigned char *s;
  1244. intptr_t i, len;
  1245. len = ifinish - istart;
  1246. us = SCHEME_CHAR_STR_VAL(argv[0]);
  1247. s = (unsigned char *)scheme_malloc_atomic(len + 1);
  1248. for (i = istart; i < ifinish; i++) {
  1249. if (us[i] < 256)
  1250. s[i - istart] = us[i];
  1251. else if (permc >= 0) {
  1252. s[i - istart] = permc;
  1253. } else {
  1254. scheme_contract_error(who,
  1255. "string cannot be encoded in Latin-1",
  1256. "string", 1, argv[0],
  1257. NULL);
  1258. }
  1259. }
  1260. s[len] = 0;
  1261. return scheme_make_sized_byte_string((char *)s, len, 0);
  1262. }
  1263. }
  1264. static Scheme_Object *char_string_to_byte_string(int argc, Scheme_Object *argv[])
  1265. {
  1266. return do_chars_to_bytes("string->bytes/utf-8", 0, argc, argv);
  1267. }
  1268. static Scheme_Object *char_string_to_byte_string_locale(int argc, Scheme_Object *argv[])
  1269. {
  1270. return do_chars_to_bytes("string->bytes/locale", 1, argc, argv);
  1271. }
  1272. static Scheme_Object *char_string_to_byte_string_latin1(int argc, Scheme_Object *argv[])
  1273. {
  1274. return do_chars_to_bytes("string->bytes/latin-1", 2, argc, argv);
  1275. }
  1276. /************************* Other *************************/
  1277. static Scheme_Object *char_string_utf8_length (int argc, Scheme_Object *argv[])
  1278. {
  1279. intptr_t istart, ifinish, len;
  1280. if (!SCHEME_CHAR_STRINGP(argv[0]))
  1281. scheme_wrong_contract("string-utf-8-length", "string?", 0, argc, argv);
  1282. scheme_get_substring_indices("string-utf-8-length", argv[0], argc, argv,
  1283. 1, 2, &istart, &ifinish);
  1284. len = scheme_utf8_encode(SCHEME_CHAR_STR_VAL(argv[0]), istart, ifinish,
  1285. NULL, 0, 0);
  1286. return scheme_make_integer(len);
  1287. }
  1288. static Scheme_Object *
  1289. byte_string_utf8_length (int argc, Scheme_Object *argv[])
  1290. {
  1291. int len, perm;
  1292. intptr_t istart, ifinish;
  1293. char *chars;
  1294. if (!SCHEME_BYTE_STRINGP(argv[0]))
  1295. scheme_wrong_contract("bytes-utf-8-length", "string?", 0, argc, argv);
  1296. chars = SCHEME_BYTE_STR_VAL(argv[0]);
  1297. if ((argc > 1) && !SCHEME_FALSEP(argv[1])) {
  1298. if (!SCHEME_CHARP(argv[1]))
  1299. scheme_wrong_contract("bytes-utf-8-length", "(or/c char? #f)", 1, argc, argv);
  1300. perm = 1;
  1301. } else
  1302. perm = 0;
  1303. scheme_get_substring_indices("bytes-utf-8-length", argv[0], argc, argv,
  1304. 2, 3,
  1305. &istart, &ifinish);
  1306. len = scheme_utf8_decode((unsigned char *)chars, istart, ifinish,
  1307. NULL, 0, -1,
  1308. NULL, 0, perm);
  1309. if (len < 0)
  1310. return scheme_false;
  1311. else
  1312. return scheme_make_integer(len);
  1313. }
  1314. static Scheme_Object *
  1315. byte_string_utf8_index(int argc, Scheme_Object *argv[])
  1316. {
  1317. intptr_t istart, ifinish, pos = -1, opos, ipos;
  1318. int result, perm;
  1319. char *chars;
  1320. if (!SCHEME_BYTE_STRINGP(argv[0]))
  1321. scheme_wrong_contract("bytes-utf-8-index", "bytes?", 0, argc, argv);
  1322. chars = SCHEME_BYTE_STR_VAL(argv[0]);
  1323. if (SCHEME_INTP(argv[1])) {
  1324. pos = SCHEME_INT_VAL(argv[1]);
  1325. } else if (SCHEME_BIGNUMP(argv[1])) {
  1326. if (SCHEME_BIGPOS(argv[1]))
  1327. pos = 0x7FFFFFFF;
  1328. }
  1329. if (pos < 0) {
  1330. scheme_wrong_contract("bytes-utf-8-index", "exact-nonnegative-integer?", 1, argc, argv);
  1331. }
  1332. if ((argc > 2) && !SCHEME_FALSEP(argv[2])) {
  1333. if (!SCHEME_CHARP(argv[2]))
  1334. scheme_wrong_contract("bytes-utf-8-index", "(or/c char? #f)", 1, argc, argv);
  1335. perm = 1;
  1336. } else
  1337. perm = 0;
  1338. scheme_get_substring_indices("bytes-utf-8-index", argv[0], argc, argv,
  1339. 3, 4,
  1340. &istart, &ifinish);
  1341. result = utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1342. NULL, 0, pos,
  1343. &ipos, &opos,
  1344. 0, 0, NULL, 0, perm ? 1 : 0);
  1345. if (((result < 0) && (result != -3))
  1346. || ((ipos == ifinish) && (opos <= pos)))
  1347. return scheme_false;
  1348. else
  1349. return scheme_make_integer(ipos);
  1350. }
  1351. static Scheme_Object *
  1352. byte_string_utf8_ref(int argc, Scheme_Object *argv[])
  1353. {
  1354. intptr_t istart, ifinish, pos = -1, opos, ipos;
  1355. char *chars;
  1356. unsigned int us[1];
  1357. Scheme_Object *perm;
  1358. if (!SCHEME_BYTE_STRINGP(argv[0]))
  1359. scheme_wrong_contract("bytes-utf-8-ref", "bytes?", 0, argc, argv);
  1360. chars = SCHEME_BYTE_STR_VAL(argv[0]);
  1361. if (SCHEME_INTP(argv[1])) {
  1362. pos = SCHEME_INT_VAL(argv[1]);
  1363. } else if (SCHEME_BIGNUMP(argv[1])) {
  1364. if (SCHEME_BIGPOS(argv[1]))
  1365. pos = 0x7FFFFFFF;
  1366. }
  1367. if (pos < 0) {
  1368. scheme_wrong_contract("bytes-utf-8-ref", "exact-nonnegative-integer?", 1, argc, argv);
  1369. }
  1370. if ((argc > 2) && !SCHEME_FALSEP(argv[2])) {
  1371. if (!SCHEME_CHARP(argv[2]))
  1372. scheme_wrong_contract("bytes-utf-8-ref", "(or/c char? #f)", 1, argc, argv);
  1373. perm = argv[2];
  1374. } else
  1375. perm = 0;
  1376. scheme_get_substring_indices("bytes-utf-8-ref", argv[0], argc, argv,
  1377. 3, 4,
  1378. &istart, &ifinish);
  1379. if (pos > 0) {
  1380. utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1381. NULL, 0, pos,
  1382. &ipos, &opos,
  1383. 0, 0, NULL, 0, perm ? 1 : 0);
  1384. if (opos < pos)
  1385. return scheme_false;
  1386. istart = ipos;
  1387. }
  1388. utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1389. us, 0, 1,
  1390. &ipos, &opos,
  1391. 0, 0, NULL, 0, perm ? 0xFFFFFF : 0);
  1392. if (opos < 1)
  1393. return scheme_false;
  1394. else if (us[0] == 0xFFFFFF)
  1395. return perm;
  1396. else
  1397. return scheme_make_character(us[0]);
  1398. }
  1399. /********************************************************************/
  1400. /* format */
  1401. /********************************************************************/
  1402. void scheme_do_format(const char *procname, Scheme_Object *port,
  1403. const mzchar *format, int flen,
  1404. int fpos, int offset, int argc, Scheme_Object **argv)
  1405. {
  1406. int i, start, end;
  1407. int used = offset;
  1408. int num_err = 0, char_err = 0, end_ok = 0;
  1409. Scheme_Object *a[2];
  1410. if (!format) {
  1411. if (!SCHEME_CHAR_STRINGP(argv[fpos])) {
  1412. scheme_wrong_contract(procname, "string?", fpos, argc, argv);
  1413. return;
  1414. }
  1415. format = SCHEME_CHAR_STR_VAL(argv[fpos]);
  1416. flen = SCHEME_CHAR_STRTAG_VAL(argv[fpos]);
  1417. } else if (flen == -1)
  1418. flen = strlen((char *)format);
  1419. /* Check string first: */
  1420. end = flen - 1;
  1421. for (i = 0; i < end; i++) {
  1422. if (format[i] == '~') {
  1423. i++;
  1424. if (scheme_isspace(format[i])) {
  1425. /* skip spaces... */
  1426. } else switch (format[i]) {
  1427. case '~':
  1428. if (i == end)
  1429. end_ok = 1;
  1430. break;
  1431. case '%':
  1432. case 'n':
  1433. case 'N':
  1434. break;
  1435. case 'a':
  1436. case 'A':
  1437. case 's':
  1438. case 'S':
  1439. case 'v':
  1440. case 'V':
  1441. case 'e':
  1442. case 'E':
  1443. used++;
  1444. break;
  1445. case '.':
  1446. switch (format[i+1]) {
  1447. case 'a':
  1448. case 'A':
  1449. case 's':
  1450. case 'S':
  1451. case 'v':
  1452. case 'V':
  1453. break;
  1454. default:
  1455. scheme_contract_error(procname,
  1456. "ill-formed pattern string",
  1457. "explanation", 0, "tag `~.' not followed by `a', `s', or `v'",
  1458. "pattern string", 1, argv[fpos],
  1459. NULL);
  1460. break;
  1461. }
  1462. used++;
  1463. break;
  1464. case 'x':
  1465. case 'X':
  1466. case 'o':
  1467. case 'O':
  1468. case 'b':
  1469. case 'B':
  1470. if (!num_err && !char_err && (used < argc)) {
  1471. Scheme_Object *o = argv[used];
  1472. if (!SCHEME_EXACT_REALP(o)
  1473. && (!SCHEME_COMPLEXP(o)
  1474. || !SCHEME_EXACT_REALP(scheme_complex_real_part(o))))
  1475. num_err = used + 1;
  1476. }
  1477. used++;
  1478. break;
  1479. case 'c':
  1480. case 'C':
  1481. if (!num_err && !char_err && (used < argc)) {
  1482. if (!SCHEME_CHARP(argv[used]))
  1483. char_err = used + 1;
  1484. }
  1485. used++;
  1486. break;
  1487. default:
  1488. {
  1489. char buffer[64];
  1490. sprintf(buffer, "tag `~%c' not allowed", format[i]);
  1491. scheme_contract_error(procname,
  1492. "ill-formed pattern string",
  1493. "explanation", 0, buffer,
  1494. "pattern string", 1, argv[fpos],
  1495. NULL);
  1496. return;
  1497. }
  1498. }
  1499. }
  1500. }
  1501. if ((format[end] == '~') && !end_ok) {
  1502. scheme_contract_error(procname,
  1503. "ill-formed pattern string",
  1504. "explanation", 0, "cannot end in `~'",
  1505. "pattern string", 1, argv[fpos],
  1506. NULL);
  1507. return;
  1508. }
  1509. if (used != argc) {
  1510. char *args;
  1511. intptr_t alen;
  1512. args = scheme_make_args_string("", -1, argc, argv, &alen);
  1513. if (used > argc) {
  1514. scheme_raise_exn(MZEXN_FAIL_CONTRACT,
  1515. "%s: format string requires %d arguments, given %d%t",
  1516. procname, used - offset, argc - offset, args, alen);
  1517. } else {
  1518. scheme_raise_exn(MZEXN_FAIL_CONTRACT,
  1519. "%s: format string requires %d arguments, given %d%t",
  1520. procname, used - offset, argc - offset, args, alen);
  1521. }
  1522. return;
  1523. }
  1524. if (num_err || char_err) {
  1525. int pos = (num_err ? num_err : char_err) - 1;
  1526. char *args, *bstr;
  1527. intptr_t alen;
  1528. intptr_t blen;
  1529. char *type = (num_err ? "exact-number" : "character");
  1530. Scheme_Object *bad = argv[pos];
  1531. args = scheme_make_args_string("other ", pos, argc, argv, &alen);
  1532. bstr = scheme_make_provided_string(bad, 1, &blen);
  1533. scheme_raise_exn(MZEXN_FAIL_CONTRACT,
  1534. "%s: format string requires argument of type <%s>, given %t%t",
  1535. procname, type,
  1536. bstr, blen,
  1537. args, alen);
  1538. return;
  1539. }
  1540. for (used = offset, i = start = 0; i < flen; i++) {
  1541. if (format[i] == '~') {
  1542. if (start < i) {
  1543. (void)scheme_put_char_string(procname, port, format, start, i - start);
  1544. }
  1545. i++;
  1546. if (scheme_isspace(format[i])) {
  1547. /* skip spaces (at most one newline) */
  1548. do {
  1549. if ((format[i] == '\n') || (format[i] == '\r')) {
  1550. /* got one */
  1551. if ((format[i] == '\r') && (format[i + 1] == '\n'))
  1552. i++; /* Windows-style CR-NL */
  1553. i++;
  1554. while (portable_isspace(format[i])
  1555. && !((format[i] == '\n') || (format[i] == '\r'))) {
  1556. i++;
  1557. }
  1558. break;
  1559. } else
  1560. i++;
  1561. } while (scheme_isspace(format[i]));
  1562. --i; /* back up over something */
  1563. } else switch (format[i]) {
  1564. case '~':
  1565. scheme_write_byte_string("~", 1, port);
  1566. break;
  1567. case '%':
  1568. case 'n':
  1569. case 'N':
  1570. scheme_write_byte_string("\n", 1, port);
  1571. break;
  1572. case 'c':
  1573. case 'C':
  1574. case 'a':
  1575. case 'A':
  1576. a[0] = argv[used++];
  1577. a[1] = port;
  1578. _scheme_apply(scheme_display_proc, 2, a);
  1579. break;
  1580. case 's':
  1581. case 'S':
  1582. a[0] = argv[used++];
  1583. a[1] = port;
  1584. _scheme_apply(scheme_write_proc, 2, a);
  1585. break;
  1586. case 'v':
  1587. case 'V':
  1588. a[0] = argv[used++];
  1589. a[1] = port;
  1590. _scheme_apply(scheme_print_proc, 2, a);
  1591. break;
  1592. case 'e':
  1593. case 'E':
  1594. {
  1595. intptr_t len;
  1596. char *s;
  1597. s = scheme_make_provided_string(argv[used++], 0, &len);
  1598. scheme_write_byte_string(s, len, port);
  1599. }
  1600. break;
  1601. case '.':
  1602. {
  1603. intptr_t len;
  1604. char *s;
  1605. len = scheme_get_print_width();
  1606. i++;
  1607. switch (format[i]) {
  1608. case 'a':
  1609. case 'A':
  1610. s = scheme_display_to_string_w_max(argv[used++], &len, len);
  1611. break;
  1612. case 's':
  1613. case 'S':
  1614. s = scheme_write_to_string_w_max(argv[used++], &len, len);
  1615. break;
  1616. case 'v':
  1617. case 'V':
  1618. s = scheme_print_to_string_w_max(argv[used++], &len, len);
  1619. break;
  1620. default:
  1621. s = "???";
  1622. len = 3;
  1623. }
  1624. scheme_write_byte_string(s, len, port);
  1625. }
  1626. break;
  1627. case 'x':
  1628. case 'X':
  1629. case 'o':
  1630. case 'O':
  1631. case 'b':
  1632. case 'B':
  1633. {
  1634. char *s;
  1635. int radix;
  1636. switch(format[i]) {
  1637. case 'x':
  1638. case 'X':
  1639. radix = 16;
  1640. break;
  1641. case 'o':
  1642. case 'O':
  1643. radix = 8;
  1644. break;
  1645. default:
  1646. case 'b':
  1647. case 'B':
  1648. radix = 2;
  1649. break;
  1650. }
  1651. s = scheme_number_to_string(radix, argv[used++]);
  1652. scheme_write_byte_string(s, strlen(s), port);
  1653. }
  1654. break;
  1655. }
  1656. SCHEME_USE_FUEL(1);
  1657. start = i + 1;
  1658. }
  1659. }
  1660. SCHEME_USE_FUEL(flen);
  1661. if (start < i) {
  1662. (void)scheme_put_char_string(procname, port, format, start, i - start);
  1663. }
  1664. }
  1665. char *scheme_format(mzchar *format, int flen, int argc, Scheme_Object **argv, intptr_t *rlen)
  1666. {
  1667. Scheme_Object *port;
  1668. port = scheme_make_byte_string_output_port();
  1669. scheme_do_format("format", port, format, flen, 0, 0, argc, argv);
  1670. return scheme_get_sized_byte_string_output(port, rlen);
  1671. }
  1672. void scheme_printf(mzchar *format, int flen, int argc, Scheme_Object **argv)
  1673. {
  1674. scheme_do_format("printf", scheme_get_param(scheme_current_config(), MZCONFIG_OUTPUT_PORT),
  1675. format, flen, 0, 0, argc, argv);
  1676. }
  1677. char *scheme_format_utf8(char *format, int flen, int argc, Scheme_Object **argv, intptr_t *rlen)
  1678. {
  1679. mzchar *s;
  1680. intptr_t srlen;
  1681. if (flen == -1)
  1682. flen = strlen(format);
  1683. s = scheme_utf8_decode_to_buffer_len((unsigned char *)format, flen, NULL, 0, &srlen);
  1684. if (s)
  1685. return scheme_format(s, srlen, argc, argv, rlen);
  1686. else
  1687. return "";
  1688. }
  1689. void scheme_printf_utf8(char *format, int flen, int argc, Scheme_Object **argv)
  1690. {
  1691. mzchar *s;
  1692. intptr_t srlen;
  1693. if (flen == -1)
  1694. flen = strlen(format);
  1695. s = scheme_utf8_decode_to_buffer_len((unsigned char *)format, flen, NULL, 0, &srlen);
  1696. if (s)
  1697. scheme_printf(s, srlen, argc, argv);
  1698. }
  1699. static Scheme_Object *
  1700. format(int argc, Scheme_Object *argv[])
  1701. {
  1702. Scheme_Object *port;
  1703. char *s;
  1704. intptr_t len;
  1705. port = scheme_make_byte_string_output_port();
  1706. scheme_do_format("format", port, NULL, 0, 0, 1, argc, argv);
  1707. s = scheme_get_sized_byte_string_output(port, &len);
  1708. return scheme_make_sized_utf8_string(s, len);
  1709. }
  1710. #ifdef INSTRUMENT_PRIMITIVES
  1711. extern int g_print_prims;
  1712. #endif
  1713. static Scheme_Object *
  1714. sch_printf(int argc, Scheme_Object *argv[])
  1715. {
  1716. scheme_do_format("printf", scheme_get_param(scheme_current_config(), MZCONFIG_OUTPUT_PORT),
  1717. NULL, 0, 0, 1, argc, argv);
  1718. return scheme_void;
  1719. }
  1720. static Scheme_Object *
  1721. sch_eprintf(int argc, Scheme_Object *argv[])
  1722. {
  1723. scheme_do_format("eprintf", scheme_get_param(scheme_current_config(), MZCONFIG_ERROR_PORT),
  1724. NULL, 0, 0, 1, argc, argv);
  1725. return scheme_void;
  1726. }
  1727. static Scheme_Object *
  1728. sch_fprintf(int argc, Scheme_Object *argv[])
  1729. {
  1730. if (!SCHEME_OUTPUT_PORTP(argv[0]))
  1731. scheme_wrong_contract("fprintf", "output-port?", 0, argc, argv);
  1732. scheme_do_format("fprintf", argv[0], NULL, 0, 1, 2, argc, argv);
  1733. return scheme_void;
  1734. }
  1735. /********************************************************************/
  1736. /* misc */
  1737. /********************************************************************/
  1738. static Scheme_Object *
  1739. version(int argc, Scheme_Object *argv[])
  1740. {
  1741. return vers_str;
  1742. }
  1743. static Scheme_Object *
  1744. banner(int argc, Scheme_Object *argv[])
  1745. {
  1746. return banner_str;
  1747. }
  1748. char *scheme_version(void)
  1749. {
  1750. return MZSCHEME_VERSION;
  1751. }
  1752. #ifdef MZ_PRECISE_GC
  1753. /* don't print " [3m]", which is the default: */
  1754. # define VERSION_SUFFIX ""
  1755. #else
  1756. # ifdef USE_SENORA_GC
  1757. # define VERSION_SUFFIX " [cgc]"
  1758. # else
  1759. # define VERSION_SUFFIX " [cgc/b]"
  1760. # endif
  1761. #endif
  1762. char *scheme_banner(void)
  1763. {
  1764. if (embedding_banner)
  1765. return embedding_banner;
  1766. else
  1767. return ("Welcome to Racket"
  1768. " v" MZSCHEME_VERSION VERSION_SUFFIX
  1769. ".\n");
  1770. }
  1771. void scheme_set_banner(char *s)
  1772. {
  1773. embedding_banner = s;
  1774. }
  1775. int scheme_byte_string_has_null(Scheme_Object *o)
  1776. {
  1777. const char *s = SCHEME_BYTE_STR_VAL(o);
  1778. int i = SCHEME_BYTE_STRTAG_VAL(o);
  1779. while (i--) {
  1780. if (!s[i])
  1781. return 1;
  1782. }
  1783. return 0;
  1784. }
  1785. int scheme_any_string_has_null(Scheme_Object *o)
  1786. {
  1787. if (SCHEME_BYTE_STRINGP(o))
  1788. return scheme_byte_string_has_null(o);
  1789. else {
  1790. const mzchar *s = SCHEME_CHAR_STR_VAL(o);
  1791. int i = SCHEME_CHAR_STRTAG_VAL(o);
  1792. while (i--) {
  1793. if (!s[i])
  1794. return 1;
  1795. }
  1796. return 0;
  1797. }
  1798. }
  1799. /***********************************************************************/
  1800. /* Environment Variables */
  1801. /***********************************************************************/
  1802. /* A `scheme_environment_variables_type` record wraps a hash table
  1803. that maps normalized keys to (cons <key> <val>), where the key
  1804. in the pair preserves its original case. */
  1805. #define SCHEME_ENVVARS_TABLE(ev) ((Scheme_Hash_Tree *)SCHEME_PTR_VAL(ev))
  1806. Scheme_Object *scheme_make_environment_variables(Scheme_Hash_Tree *ht)
  1807. {
  1808. Scheme_Object *ev;
  1809. ev = scheme_alloc_small_object();
  1810. ev->type = scheme_environment_variables_type;
  1811. SCHEME_PTR_VAL(ev) = (Scheme_Object *)ht;
  1812. return ev;
  1813. }
  1814. static Scheme_Object *env_p(int argc, Scheme_Object *argv[])
  1815. {
  1816. return (SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_environment_variables_type)
  1817. ? scheme_true
  1818. : scheme_false);
  1819. }
  1820. static Scheme_Object *current_environment_variables(int argc, Scheme_Object *argv[])
  1821. {
  1822. Scheme_Object *v;
  1823. v = scheme_param_config2("current-environment-variables",
  1824. scheme_make_integer(MZCONFIG_CURRENT_ENV_VARS),
  1825. argc, argv,
  1826. -1, env_p, "environment-variables?", 0);
  1827. return v;
  1828. }
  1829. static int sch_bool_getenv(const char* name);
  1830. void scheme_init_getenv(void)
  1831. {
  1832. if (sch_bool_getenv("PLTNOMZJIT")) {
  1833. scheme_set_startup_use_jit(0);
  1834. }
  1835. }
  1836. static int sch_bool_getenv(const char* name)
  1837. {
  1838. if (rktio_getenv(scheme_rktio, name))
  1839. return 1;
  1840. else
  1841. return 0;
  1842. }
  1843. int byte_string_ok_name(Scheme_Object *o)
  1844. {
  1845. const char *s = SCHEME_BYTE_STR_VAL(o);
  1846. int i = SCHEME_BYTE_STRTAG_VAL(o);
  1847. while (i--) {
  1848. if (!s[i])
  1849. return 0;
  1850. }
  1851. return rktio_is_ok_envvar_name(scheme_rktio, s);
  1852. return 1;
  1853. }
  1854. static Scheme_Object *normalize_env_case(Scheme_Object *bs)
  1855. {
  1856. if (rktio_are_envvar_names_case_insensitive(scheme_rktio)) {
  1857. bs = scheme_byte_string_to_char_string(bs);
  1858. bs = string_locale_downcase(1, &bs);
  1859. bs = scheme_char_string_to_byte_string(bs);
  1860. }
  1861. return bs;
  1862. }
  1863. char *scheme_getenv(char *name)
  1864. {
  1865. char *s;
  1866. s = rktio_getenv(scheme_rktio, name);
  1867. if (s)
  1868. return scheme_strdup_and_free(s);
  1869. else
  1870. return NULL;
  1871. }
  1872. static Scheme_Object *sch_getenv(int argc, Scheme_Object *argv[])
  1873. {
  1874. char *name;
  1875. char *value;
  1876. Scheme_Object *bs, *ev, *val;
  1877. Scheme_Hash_Tree *ht;
  1878. if (!SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_environment_variables_type))
  1879. scheme_wrong_contract("environment-variables-ref", "environment-variables?", 0, argc, argv);
  1880. bs = argv[1];
  1881. if (!SCHEME_BYTE_STRINGP(bs)
  1882. || !byte_string_ok_name(bs))
  1883. scheme_wrong_contract("environment-variables-ref", "bytes-environment-variable-name?", 1, argc, argv);
  1884. ev = argv[0];
  1885. ht = SCHEME_ENVVARS_TABLE(ev);
  1886. if (!ht) {
  1887. name = SCHEME_BYTE_STR_VAL(bs);
  1888. value = rktio_getenv(scheme_rktio, name);
  1889. if (value) {
  1890. val = scheme_make_byte_string(value);
  1891. free(value);
  1892. } else
  1893. val = scheme_false;
  1894. return val;
  1895. } else {
  1896. bs = normalize_env_case(bs);
  1897. val = scheme_hash_tree_get(ht, bs);
  1898. return val ? SCHEME_CDR(val) : scheme_false;
  1899. }
  1900. }
  1901. static Scheme_Object *sch_putenv(int argc, Scheme_Object *argv[])
  1902. {
  1903. Scheme_Object *varbs, *valbs, *norm_varbs, *ev;
  1904. Scheme_Hash_Tree *ht;
  1905. char *var;
  1906. char *val;
  1907. if (!SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_environment_variables_type))
  1908. scheme_wrong_contract("environment-variables-set!", "environment-variables?", 0, argc, argv);
  1909. varbs = argv[1];
  1910. if (!SCHEME_BYTE_STRINGP(varbs)
  1911. || !byte_string_ok_name(varbs))
  1912. scheme_wrong_contract("environment-variables-set!", "bytes-environment-variable-name?", 1, argc, argv);
  1913. valbs = argv[2];
  1914. if (!SCHEME_FALSEP(valbs)
  1915. && (!SCHEME_BYTE_STRINGP(valbs)
  1916. || scheme_byte_string_has_null(valbs)))
  1917. scheme_wrong_contract("environment-variables-set!", "(or/c bytes-no-nuls? #f)", 2, argc, argv);
  1918. if (argc > 3)
  1919. scheme_check_proc_arity("environment-variables-set!", 0, 3, argc, argv);
  1920. ev = argv[0];
  1921. ht = SCHEME_ENVVARS_TABLE(ev);
  1922. if (ht) {
  1923. norm_varbs = normalize_env_case(varbs);
  1924. if (SCHEME_FALSEP(valbs)) {
  1925. ht = scheme_hash_tree_set(ht, norm_varbs, NULL);
  1926. } else {
  1927. if (SAME_OBJ(varbs, norm_varbs)) {
  1928. varbs = byte_string_to_immutable(1, &varbs);
  1929. norm_varbs = varbs;
  1930. } else {
  1931. varbs = byte_string_to_immutable(1, &varbs);
  1932. norm_varbs = byte_string_to_immutable(1, &norm_varbs);
  1933. }
  1934. valbs = byte_string_to_immutable(1, &valbs);
  1935. ht = scheme_hash_tree_set(ht, norm_varbs, scheme_make_pair(varbs, valbs));
  1936. }
  1937. SCHEME_PTR_VAL(ev) = (Scheme_Object *)ht;
  1938. return scheme_void;
  1939. } else {
  1940. var = SCHEME_BYTE_STR_VAL(varbs);
  1941. if (SCHEME_FALSEP(valbs)) {
  1942. val = NULL;
  1943. } else {
  1944. val = SCHEME_BYTE_STR_VAL(valbs);
  1945. }
  1946. if (!rktio_setenv(scheme_rktio, var, val)) {
  1947. if (argc > 3)
  1948. return _scheme_tail_apply(argv[3], 0, NULL);
  1949. else {
  1950. scheme_raise_exn(MZEXN_FAIL,
  1951. "environment-variables-set!: change failed\n"
  1952. " system error: %R");
  1953. }
  1954. }
  1955. return scheme_void;
  1956. }
  1957. }
  1958. static Scheme_Object *env_copy(int argc, Scheme_Object *argv[])
  1959. {
  1960. Scheme_Hash_Tree *ht;
  1961. if (!SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_environment_variables_type))
  1962. scheme_wrong_contract("environment-variables-copy", "environment-variables?", 0, argc, argv);
  1963. ht = SCHEME_ENVVARS_TABLE(argv[0]);
  1964. if (ht)
  1965. return scheme_make_environment_variables(ht);
  1966. /* copy system environment variables into a hash table: */
  1967. ht = scheme_make_hash_tree(SCHEME_hashtr_equal);
  1968. {
  1969. intptr_t i;
  1970. rktio_envvars_t *envvars;
  1971. Scheme_Object *var, *val, *norm_var;
  1972. envvars = rktio_envvars(scheme_rktio);
  1973. for (i = rktio_envvars_count(scheme_rktio, envvars); i--; ) {
  1974. var = scheme_make_immutable_sized_byte_string(rktio_envvars_name_ref(scheme_rktio, envvars, i), -1, 1);
  1975. val = scheme_make_immutable_sized_byte_string(rktio_envvars_value_ref(scheme_rktio, envvars, i), -1, 1);
  1976. norm_var = normalize_env_case(var);
  1977. if (!SAME_OBJ(var, norm_var))
  1978. norm_var = byte_string_to_immutable(1, &norm_var);
  1979. ht = scheme_hash_tree_set(ht, norm_var, scheme_make_pair(var, val));
  1980. }
  1981. rktio_envvars_free(scheme_rktio, envvars);
  1982. }
  1983. return scheme_make_environment_variables(ht);
  1984. }
  1985. static Scheme_Object *env_make(int argc, Scheme_Object *argv[])
  1986. {
  1987. Scheme_Hash_Tree *ht;
  1988. Scheme_Object *varbs, *valbs, *norm_varbs;
  1989. int i;
  1990. ht = scheme_make_hash_tree(SCHEME_hashtr_equal);
  1991. for (i = 0; i < argc; i += 2) {
  1992. varbs = argv[i];
  1993. if (!SCHEME_BYTE_STRINGP(varbs)
  1994. || !byte_string_ok_name(varbs))
  1995. scheme_wrong_contract("make-environment-variables", "bytes-environment-variable-name?", i, argc, argv);
  1996. if (i+1 >= argc) {
  1997. scheme_contract_error("make-environment-variables",
  1998. "key does not have a value (i.e., an odd number of arguments were provided)",
  1999. "key", 1, argv[i],
  2000. NULL);
  2001. return NULL;
  2002. }
  2003. valbs = argv[i+1];
  2004. if (!SCHEME_FALSEP(valbs)
  2005. && (!SCHEME_BYTE_STRINGP(valbs)
  2006. || scheme_byte_string_has_null(valbs)))
  2007. scheme_wrong_contract("make-environment-variables", "(or/c bytes-no-nuls? #f)", i+1, argc, argv);
  2008. varbs = byte_string_to_immutable(1, &varbs);
  2009. valbs = byte_string_to_immutable(1, &valbs);
  2010. norm_varbs = normalize_env_case(varbs);
  2011. if (!SAME_OBJ(varbs, norm_varbs))
  2012. norm_varbs = byte_string_to_immutable(1, &norm_varbs);
  2013. ht = scheme_hash_tree_set(ht, norm_varbs, scheme_make_pair(varbs, valbs));
  2014. }
  2015. return scheme_make_environment_variables(ht);
  2016. }
  2017. static Scheme_Object *sch_getenv_names(int argc, Scheme_Object *argv[])
  2018. {
  2019. Scheme_Object *ev, *r = scheme_null, *key, *val;
  2020. Scheme_Hash_Tree *ht;
  2021. mzlonglong i;
  2022. ev = argv[0];
  2023. if (!SAME_TYPE(SCHEME_TYPE(ev), scheme_environment_variables_type))
  2024. scheme_wrong_contract("environment-variables-names", "environment-variables?", 0, argc, argv);
  2025. ht = SCHEME_ENVVARS_TABLE(ev);
  2026. if (!ht) {
  2027. ev = env_copy(1, argv);
  2028. ht = SCHEME_ENVVARS_TABLE(ev);
  2029. }
  2030. for (i = scheme_hash_tree_next(ht, -1); i != -1; i = scheme_hash_tree_next(ht, i)) {
  2031. scheme_hash_tree_index(ht, i, &key, &val);
  2032. r = scheme_make_pair(SCHEME_CAR(val), r);
  2033. }
  2034. return r;
  2035. }
  2036. rktio_envvars_t *scheme_environment_variables_to_envvars(Scheme_Object *ev)
  2037. {
  2038. Scheme_Hash_Tree *ht = SCHEME_ENVVARS_TABLE(ev);
  2039. rktio_envvars_t *envvars;
  2040. mzlonglong i;
  2041. Scheme_Object *key, *val;
  2042. if (!ht)
  2043. return NULL;
  2044. envvars = rktio_empty_envvars(scheme_rktio);
  2045. for (i = scheme_hash_tree_next(ht, -1); i != -1; i = scheme_hash_tree_next(ht, i)) {
  2046. scheme_hash_tree_index(ht, i, &key, &val);
  2047. rktio_envvars_set(scheme_rktio,
  2048. envvars,
  2049. SCHEME_BYTE_STR_VAL(SCHEME_CAR(val)),
  2050. SCHEME_BYTE_STR_VAL(SCHEME_CDR(val)));
  2051. }
  2052. return envvars;
  2053. }
  2054. /***********************************************************************/
  2055. /* End Environment Variables */
  2056. /***********************************************************************/
  2057. void scheme_set_cross_compile_mode(int v)
  2058. {
  2059. cross_compile_mode = v;
  2060. }
  2061. static void machine_details(char *s);
  2062. #include "systype.inc"
  2063. static Scheme_Object *system_type(int argc, Scheme_Object *argv[])
  2064. {
  2065. if (argc) {
  2066. if (SAME_OBJ(argv[0], link_symbol)) {
  2067. return scheme_intern_symbol(MZ_SYSTEM_TYPE_LINK);
  2068. }
  2069. if (SAME_OBJ(argv[0], machine_symbol)) {
  2070. char buff[1024];
  2071. machine_details(buff);
  2072. return scheme_make_utf8_string(buff);
  2073. }
  2074. if (SAME_OBJ(argv[0], gc_symbol)) {
  2075. #ifdef MZ_PRECISE_GC
  2076. return _3m_symbol;
  2077. #else
  2078. return cgc_symbol;
  2079. #endif
  2080. }
  2081. if (SAME_OBJ(argv[0], vm_symbol)) {
  2082. return racket_symbol;
  2083. }
  2084. if (SAME_OBJ(argv[0], so_suffix_symbol)) {
  2085. return scheme_make_byte_string(MZ_SYSTEM_TYPE_SO_SUFFIX);
  2086. }
  2087. if (SAME_OBJ(argv[0], so_mode_symbol)) {
  2088. return scheme_intern_symbol(MZ_SYSTEM_TYPE_SO_MODE);
  2089. }
  2090. if (SAME_OBJ(argv[0], word_symbol)) {
  2091. return scheme_make_integer(sizeof(void*)*8);
  2092. }
  2093. if (SAME_OBJ(argv[0], fs_change_symbol)) {
  2094. return fs_change_props;
  2095. }
  2096. if (SAME_OBJ(argv[0], target_machine_symbol)) {
  2097. return racket_symbol;
  2098. }
  2099. if (SAME_OBJ(argv[0], cross_symbol)) {
  2100. return (cross_compile_mode ? force_symbol : infer_symbol);
  2101. }
  2102. if (!SAME_OBJ(argv[0], os_symbol)) {
  2103. scheme_wrong_contract("system-type",
  2104. ("(or/c 'os 'word 'link 'machine 'target-machine\n"
  2105. " 'vm 'gc 'so-suffix 'so-mode 'word 'fs-change 'cross)"),
  2106. 0, argc, argv);
  2107. return NULL;
  2108. }
  2109. }
  2110. return sys_symbol;
  2111. }
  2112. static Scheme_Object *system_library_subpath(int argc, Scheme_Object *argv[])
  2113. {
  2114. if (argc > 0) {
  2115. if (SCHEME_FALSEP(argv[0]))
  2116. return platform_cgc_path;
  2117. if (SAME_OBJ(cgc_symbol, argv[0]))
  2118. return platform_cgc_path;
  2119. if (SAME_OBJ(_3m_symbol, argv[0]))
  2120. return platform_3m_path;
  2121. if (SAME_OBJ(cs_symbol, argv[0]))
  2122. return platform_cs_path;
  2123. scheme_wrong_contract("system-library-subpath", "(or/c 'cgc '3m 'cs #f)", 0, argc, argv);
  2124. return NULL;
  2125. } else {
  2126. #ifdef MZ_PRECISE_GC
  2127. return platform_3m_path;
  2128. #else
  2129. return platform_cgc_path;
  2130. #endif
  2131. }
  2132. }
  2133. const char *scheme_system_library_subpath()
  2134. {
  2135. return SCHEME_PLATFORM_LIBRARY_SUBPATH SPLS_SUFFIX;
  2136. }
  2137. /* Our own strncpy - which would be really stupid, except the one for
  2138. the implementation in Solaris 2.6 is broken (it doesn't always stop
  2139. at the null terminator). */
  2140. int scheme_strncmp(const char *a, const char *b, int len)
  2141. {
  2142. while (len-- && (*a == *b) && *a) {
  2143. a++;
  2144. b++;
  2145. }
  2146. if (len < 0)
  2147. return 0;
  2148. else
  2149. return *a - *b;
  2150. }
  2151. static Scheme_Object *ok_cmdline(int argc, Scheme_Object **argv)
  2152. {
  2153. if (SCHEME_CHAPERONE_VECTORP(argv[0])) {
  2154. Scheme_Object *vec = argv[0], *vec2, *str;
  2155. int i, size = SCHEME_CHAPERONE_VEC_SIZE(vec);
  2156. if (!size)
  2157. return vec;
  2158. /* Make sure vector and strings are immutable: */
  2159. vec2 = scheme_make_vector(size, NULL);
  2160. if (size)
  2161. SCHEME_SET_VECTOR_IMMUTABLE(vec2);
  2162. for (i = 0; i < size; i++) {
  2163. if (SCHEME_VECTORP(vec))
  2164. str = SCHEME_VEC_ELS(vec)[i];
  2165. else
  2166. str = scheme_chaperone_vector_ref(vec, i);
  2167. if (!SCHEME_CHAR_STRINGP(str))
  2168. return NULL;
  2169. if (!SCHEME_IMMUTABLE_CHAR_STRINGP(str)) {
  2170. str = scheme_make_sized_char_string(SCHEME_CHAR_STR_VAL(str), SCHEME_CHAR_STRLEN_VAL(str), 0);
  2171. SCHEME_SET_CHAR_STRING_IMMUTABLE(str);
  2172. }
  2173. SCHEME_VEC_ELS(vec2)[i] = str;
  2174. }
  2175. return vec2;
  2176. }
  2177. return NULL;
  2178. }
  2179. static Scheme_Object *cmdline_args(int argc, Scheme_Object *argv[])
  2180. {
  2181. return scheme_param_config2("current-command-line-arguments",
  2182. scheme_make_integer(MZCONFIG_CMDLINE_ARGS),
  2183. argc, argv,
  2184. -1, ok_cmdline, "(vectorof string?)", 1);
  2185. }
  2186. /**********************************************************************/
  2187. /* locale ops */
  2188. /**********************************************************************/
  2189. static Scheme_Object *ok_locale(int argc, Scheme_Object **argv)
  2190. {
  2191. if (SCHEME_FALSEP(argv[0]))
  2192. return argv[0];
  2193. else if (SCHEME_CHAR_STRINGP(argv[0])) {
  2194. if (SCHEME_IMMUTABLEP(argv[0]))
  2195. return argv[0];
  2196. else {
  2197. Scheme_Object *str = argv[0];
  2198. str = scheme_make_immutable_sized_char_string(SCHEME_CHAR_STR_VAL(str), SCHEME_CHAR_STRLEN_VAL(str), 1);
  2199. return str;
  2200. }
  2201. }
  2202. return NULL;
  2203. }
  2204. static Scheme_Object *current_locale(int argc, Scheme_Object *argv[])
  2205. {
  2206. Scheme_Object *v;
  2207. v = scheme_param_config2("current-locale",
  2208. scheme_make_integer(MZCONFIG_LOCALE),
  2209. argc, argv,
  2210. -1, ok_locale, "(or/c #f string?)", 1);
  2211. return v;
  2212. }
  2213. static Scheme_Object *locale_string_encoding(int argc, Scheme_Object *argv[])
  2214. {
  2215. char *enc;
  2216. Scheme_Object *s;
  2217. reset_locale();
  2218. if (mzLOCALE_IS_UTF_8(current_locale_name) || !locale_on)
  2219. return scheme_make_utf8_string("UTF-8");
  2220. enc = rktio_locale_encoding(scheme_rktio);
  2221. s = scheme_make_utf8_string(enc);
  2222. free(enc);
  2223. return s;
  2224. }
  2225. static Scheme_Object *system_language_country(int argc, Scheme_Object *argv[])
  2226. {
  2227. char *lc;
  2228. Scheme_Object *s;
  2229. lc = rktio_system_language_country(scheme_rktio);
  2230. s = scheme_make_utf8_string(lc);
  2231. free(lc);
  2232. return s;
  2233. }
  2234. static void do_convert_close(rktio_converter_t *cd, int cache_mode, const char *to_e, const char *from_e)
  2235. /* If `cache_mode` is -1, then `to_e` needs to be freed (or cached).
  2236. If `cache_mode` is 1, then `from_e` needs to be freed (or cached). */
  2237. {
  2238. if (cache_mode == -1)
  2239. cache_locale_or_close(1, cd, (char *)to_e);
  2240. else if (cache_mode == 1)
  2241. cache_locale_or_close(0, cd, (char *)from_e);
  2242. else if (!cache_mode)
  2243. rktio_converter_close(scheme_rktio, cd);
  2244. }
  2245. static char *do_convert(rktio_converter_t *cd,
  2246. /* if !cd and either from_e or to_e can be NULL, then
  2247. reset_locale() must have been called */
  2248. const char *from_e, const char *to_e,
  2249. /* 1 => UCS-4 -> UTF-8; 2 => UTF-8 -> UCS-4; 0 => other */
  2250. int to_from_utf8,
  2251. /* in can be NULL to output just a shift; in that case,
  2252. id should be 0, too */
  2253. char *in, int id, int iilen,
  2254. char *out, int od, int iolen,
  2255. /* if grow, then reallocate when out isn't big enough */
  2256. int grow,
  2257. /* if add_end_shift, add a shift sequence to the end;
  2258. not useful if in is already NULL to indicate a shift */
  2259. int add_end_shift,
  2260. /* extra specifies the length of a terminator,
  2261. not included in iolen or *oolen */
  2262. int extra,
  2263. /* these two report actual read/wrote sizes: */
  2264. intptr_t *oilen, intptr_t *oolen,
  2265. /* status is set to
  2266. 0 for complete,
  2267. -1 for partial input,
  2268. -2 for error,
  2269. 1 for more avail */
  2270. int *status)
  2271. {
  2272. int dip, dop, close_it = 0, cache_mode = 0, mz_utf8 = 0;
  2273. intptr_t il, ol, r;
  2274. GC_CAN_IGNORE char *ip, *op;
  2275. /* Defaults: */
  2276. *status = -1;
  2277. if (oilen)
  2278. *oilen = 0;
  2279. *oolen = 0;
  2280. if (!cd) {
  2281. if (rktio_convert_properties(scheme_rktio) & RKTIO_CONVERTER_SUPPORTED) {
  2282. char *tmp_from_e = NULL, *tmp_to_e = NULL;
  2283. if (!to_e && !strcmp(from_e, MZ_UCS4_NAME))
  2284. cache_mode = -1;
  2285. else if (!from_e && !strcmp(to_e, MZ_UCS4_NAME))
  2286. cache_mode = 1;
  2287. if (!from_e) {
  2288. tmp_from_e = rktio_locale_encoding(scheme_rktio);
  2289. from_e = tmp_from_e;
  2290. }
  2291. if (!to_e) {
  2292. tmp_to_e = rktio_locale_encoding(scheme_rktio);
  2293. to_e = tmp_to_e;
  2294. }
  2295. if ((cache_mode == -1)
  2296. && cached_locale_to_converter
  2297. && !strcmp(to_e, cached_locale_encoding_name)) {
  2298. cd = cached_locale_to_converter;
  2299. cached_locale_to_converter = NULL;
  2300. } else if ((cache_mode == 1)
  2301. && cached_locale_from_converter
  2302. && !strcmp(from_e, cached_locale_encoding_name)) {
  2303. cd = cached_locale_from_converter;
  2304. cached_locale_from_converter = NULL;
  2305. } else {
  2306. cd = rktio_converter_open(scheme_rktio, to_e, from_e);
  2307. }
  2308. close_it = 1;
  2309. if (tmp_from_e && ((cache_mode != 1) || !cd)) free(tmp_from_e);
  2310. if (tmp_to_e && ((cache_mode != -1) || !cd)) free(tmp_to_e);
  2311. } else if (to_from_utf8) {
  2312. /* Assume UTF-8 */
  2313. mz_utf8 = 1;
  2314. }
  2315. }
  2316. if (!cd && !mz_utf8) {
  2317. if (out) {
  2318. while (extra--) {
  2319. out[extra] = 0;
  2320. }
  2321. }
  2322. return out;
  2323. }
  2324. /* The converter is ready. Allocate out space, if necessary */
  2325. if (!out) {
  2326. if (iolen <= 0)
  2327. iolen = iilen;
  2328. out = (char *)scheme_malloc_atomic(iolen + extra);
  2329. od = 0;
  2330. }
  2331. /* il and ol are the number of available chars */
  2332. il = iilen;
  2333. ol = iolen;
  2334. /* dip and dop are the number of characters read so far;
  2335. we use these and NULL out the ip and op pointers
  2336. for the sake of precise GC */
  2337. dip = 0;
  2338. dop = 0;
  2339. if (!in)
  2340. add_end_shift = 0;
  2341. while (1) {
  2342. int icerr;
  2343. if (mz_utf8) {
  2344. /* Use our UTF-8 routines as if they were iconv */
  2345. if (to_from_utf8 == 1) {
  2346. /* UCS-4 -> UTF-8 */
  2347. /* We assume that in + id and iilen are mzchar-aligned */
  2348. int opos, uid, uilen;
  2349. uid = (id + dip) >> 2;
  2350. uilen = (iilen - dip) >> 2;
  2351. opos = scheme_utf8_encode((const unsigned int *)in, uid, uilen,
  2352. NULL, 0,
  2353. 0);
  2354. if (opos <= iolen) {
  2355. opos = scheme_utf8_encode((const unsigned int *)in, uid, uilen,
  2356. (unsigned char *)out, od + dop,
  2357. 0);
  2358. dop += opos;
  2359. dip += iilen;
  2360. icerr = 0;
  2361. r = (size_t)opos;
  2362. } else {
  2363. icerr = E2BIG;
  2364. r = (size_t)-1;
  2365. }
  2366. } else {
  2367. /* UTF-8 -> UCS-4 */
  2368. /* We assume that out + od is mzchar-aligned */
  2369. intptr_t ipos, opos;
  2370. r = utf8_decode_x((unsigned char *)in, id + dip, iilen,
  2371. (unsigned int *)out, (od + dop) >> 2, iolen >> 2,
  2372. &ipos, &opos,
  2373. 0, 0, NULL, 0, 0);
  2374. opos <<= 2;
  2375. dop = (opos - od);
  2376. dip = (ipos - id);
  2377. if ((r == -1) || (r == -2)) {
  2378. r = (size_t)-1;
  2379. icerr = RKTIO_ERROR_CONVERT_BAD_SEQUENCE;
  2380. } else if (r == -3) {
  2381. icerr = RKTIO_ERROR_CONVERT_NOT_ENOUGH_SPACE;
  2382. r = (size_t)-1;
  2383. } else
  2384. icerr = 0;
  2385. }
  2386. } else {
  2387. ip = in XFORM_OK_PLUS id + dip;
  2388. op = out XFORM_OK_PLUS od + dop;
  2389. r = rktio_convert(scheme_rktio, cd, &ip, &il, &op, &ol);
  2390. dip = ip - (in XFORM_OK_PLUS id);
  2391. dop = op - (out XFORM_OK_PLUS od);
  2392. ip = op = NULL;
  2393. icerr = rktio_get_last_error(scheme_rktio);
  2394. }
  2395. /* Record how many chars processed, now */
  2396. if (oilen)
  2397. *oilen = dip;
  2398. *oolen = dop;
  2399. /* Got all the chars? */
  2400. if (r == RKTIO_CONVERT_ERROR) {
  2401. if (icerr == RKTIO_ERROR_CONVERT_NOT_ENOUGH_SPACE) {
  2402. if (grow) {
  2403. /* Double the string size and try again */
  2404. char *naya;
  2405. naya = (char *)scheme_malloc_atomic((iolen * 2) + extra);
  2406. memcpy(naya, out + od, *oolen);
  2407. ol += iolen;
  2408. iolen += iolen;
  2409. out = naya;
  2410. od = 0;
  2411. } else {
  2412. *status = 1;
  2413. if (close_it)
  2414. do_convert_close(cd, cache_mode, to_e, from_e);
  2415. while (extra--) {
  2416. out[od + dop + extra] = 0;
  2417. }
  2418. return out;
  2419. }
  2420. } else {
  2421. /* Either EINVAL (premature end) or EILSEQ (bad sequence) */
  2422. if (icerr == RKTIO_ERROR_CONVERT_BAD_SEQUENCE)
  2423. *status = -2;
  2424. if (close_it)
  2425. do_convert_close(cd, cache_mode, to_e, from_e);
  2426. while (extra--) {
  2427. out[od + dop + extra] = 0;
  2428. }
  2429. return out;
  2430. }
  2431. } else {
  2432. /* All done... */
  2433. if (add_end_shift) {
  2434. add_end_shift = 0;
  2435. in = NULL;
  2436. dip = 0;
  2437. id = 0;
  2438. il = 0; /* should be redundant */
  2439. oilen = NULL; /* so it doesn't get set to 0 */
  2440. } else {
  2441. *status = 0;
  2442. if (close_it)
  2443. do_convert_close(cd, cache_mode, to_e, from_e);
  2444. while (extra--) {
  2445. out[od + dop + extra] = 0;
  2446. }
  2447. return out;
  2448. }
  2449. }
  2450. }
  2451. }
  2452. #define MZ_SC_BUF_SIZE 32
  2453. static char *string_to_from_locale(int to_bytes,
  2454. char *in, intptr_t delta, intptr_t len,
  2455. intptr_t *olen, int perm,
  2456. int *no_cvt)
  2457. /* Call this function only when iconv is available, and only when
  2458. reset_locale() has been called */
  2459. {
  2460. Scheme_Object *parts = scheme_null, *one;
  2461. char *c, *le;
  2462. intptr_t clen, used;
  2463. int status;
  2464. rktio_converter_t *cd;
  2465. le = rktio_locale_encoding(scheme_rktio);
  2466. if (cached_locale_encoding_name
  2467. && !strcmp(le, cached_locale_encoding_name)
  2468. && (to_bytes ? cached_locale_to_converter : cached_locale_from_converter)) {
  2469. if (to_bytes) {
  2470. cd = cached_locale_to_converter;
  2471. cached_locale_to_converter = NULL;
  2472. } else {
  2473. cd = cached_locale_from_converter;
  2474. cached_locale_from_converter = NULL;
  2475. }
  2476. } else {
  2477. if (to_bytes)
  2478. cd = rktio_converter_open(scheme_rktio, le, MZ_UCS4_NAME);
  2479. else
  2480. cd = rktio_converter_open(scheme_rktio, MZ_UCS4_NAME, le);
  2481. }
  2482. if (!cd) {
  2483. free(le);
  2484. *no_cvt = 1;
  2485. return NULL;
  2486. }
  2487. *no_cvt = 0;
  2488. status = 0;
  2489. while (len) {
  2490. /* We might have conversion errors... */
  2491. c = do_convert(cd, NULL, NULL, 0,
  2492. (char *)in, (to_bytes ? 4 : 1) * delta, (to_bytes ? 4 : 1) * len,
  2493. NULL, 0, (to_bytes ? 1 : 4) * (len + 1),
  2494. 1 /* grow */, 1, (to_bytes ? 1 : 4) /* terminator size */,
  2495. &used, &clen,
  2496. &status);
  2497. if (to_bytes)
  2498. used >>= 2;
  2499. if ((perm < 0) && (used < len)) {
  2500. rktio_converter_close(scheme_rktio, cd);
  2501. free(le);
  2502. return NULL;
  2503. }
  2504. delta += used;
  2505. len -= used;
  2506. if (!len && SCHEME_NULLP(parts)) {
  2507. if (to_bytes) {
  2508. *olen = clen;
  2509. c[*olen] = 0;
  2510. } else {
  2511. *olen = (clen >> 2);
  2512. ((mzchar *)c)[*olen] = 0;
  2513. }
  2514. cache_locale_or_close(to_bytes, cd, le);
  2515. return c;
  2516. }
  2517. /* We can get here if there was some conversion error at some
  2518. point. We're building up a list of parts. */
  2519. if (to_bytes) {
  2520. one = scheme_make_sized_byte_string(c, clen, 0);
  2521. } else {
  2522. one = scheme_make_sized_char_string((mzchar *)c, clen >> 2, 0);
  2523. }
  2524. parts = scheme_make_pair(one, parts);
  2525. if (len) {
  2526. /* Conversion error, so skip one char. */
  2527. if (to_bytes) {
  2528. char bc[1];
  2529. bc[0] = perm;
  2530. one = scheme_make_sized_byte_string(bc, 1, 1);
  2531. } else {
  2532. mzchar bc[1];
  2533. bc[0] = perm;
  2534. one = scheme_make_sized_char_string(bc, 1, 1);
  2535. }
  2536. parts = scheme_make_pair(one, parts);
  2537. delta += 1;
  2538. len -= 1;
  2539. }
  2540. }
  2541. cache_locale_or_close(to_bytes, cd, le);
  2542. if (to_bytes) {
  2543. parts = append_all_byte_strings_backwards(parts);
  2544. *olen = SCHEME_BYTE_STRTAG_VAL(parts);
  2545. return SCHEME_BYTE_STR_VAL(parts);
  2546. } else {
  2547. parts = append_all_strings_backwards(parts);
  2548. *olen = SCHEME_CHAR_STRTAG_VAL(parts);
  2549. return (char *)SCHEME_CHAR_STR_VAL(parts);
  2550. }
  2551. }
  2552. void cache_locale_or_close(int to_bytes, rktio_converter_t *cd, char *le)
  2553. {
  2554. if (to_bytes ? cached_locale_to_converter : cached_locale_from_converter) {
  2555. rktio_converter_close(scheme_rktio, cd);
  2556. free(le);
  2557. } else {
  2558. if (!cached_locale_encoding_name || strcmp(le, cached_locale_encoding_name)) {
  2559. scheme_clear_locale_cache();
  2560. cached_locale_encoding_name = le;
  2561. } else
  2562. free(le);
  2563. rktio_convert_reset(scheme_rktio, cd);
  2564. if (to_bytes)
  2565. cached_locale_to_converter = cd;
  2566. else
  2567. cached_locale_from_converter = cd;
  2568. }
  2569. }
  2570. void scheme_clear_locale_cache(void)
  2571. {
  2572. if (cached_locale_encoding_name) {
  2573. if (cached_locale_to_converter) {
  2574. rktio_converter_close(scheme_rktio, cached_locale_to_converter);
  2575. cached_locale_to_converter = NULL;
  2576. }
  2577. if (cached_locale_from_converter) {
  2578. rktio_converter_close(scheme_rktio, cached_locale_from_converter);
  2579. cached_locale_from_converter = NULL;
  2580. }
  2581. free(cached_locale_encoding_name);
  2582. cached_locale_encoding_name = NULL;
  2583. }
  2584. }
  2585. static char *locale_recase(int to_up,
  2586. /* in must be null-terminated, iilen doesn't include it */
  2587. char *in, int id, int iilen,
  2588. /* iolen, in contrast, includes the terminator */
  2589. char *out, int od, int iolen,
  2590. intptr_t *oolen)
  2591. /* Assumes that reset_locale() has been called */
  2592. {
  2593. char *s, *s2;
  2594. intptr_t len;
  2595. s = rktio_locale_recase(scheme_rktio, to_up, in XFORM_OK_PLUS id);
  2596. len = strlen(s);
  2597. if ((len+1) < iolen) {
  2598. memcpy(out XFORM_OK_PLUS od, s, len+1);
  2599. s2 = out;
  2600. } else {
  2601. s2 = scheme_malloc_atomic(len+1);
  2602. memcpy(s2, s, len+1);
  2603. }
  2604. free(s);
  2605. *oolen = len;
  2606. return s2;
  2607. }
  2608. int mz_locale_strcoll(char *s1, int d1, int l1, char *s2, int d2, int l2, int cvt_case)
  2609. /* The s1 and s2 arguments are actually UCS-4.
  2610. Assumes that reset_locale() has been called. */
  2611. {
  2612. intptr_t clen1, clen2, used1, used2, origl1, origl2;
  2613. char *c1, *c2, buf1[MZ_SC_BUF_SIZE], buf2[MZ_SC_BUF_SIZE];
  2614. char case_buf1[MZ_SC_BUF_SIZE], case_buf2[MZ_SC_BUF_SIZE];
  2615. int status, got_more;
  2616. /* First, convert UCS-4 to locale-specific encoding. If some
  2617. characters don't fit into the encoding, then we'll have leftover
  2618. characters. Count unconvertable charc as greater than anything
  2619. that can be converted */
  2620. origl1 = l1;
  2621. origl2 = l2;
  2622. /* Loop to check both convertable and unconvertable parts */
  2623. while (1) {
  2624. if (!origl1 && !origl2)
  2625. return 0;
  2626. if (!origl1)
  2627. return -1;
  2628. if (!origl2)
  2629. return 1;
  2630. /* Loop to get consistent parts of the wto strings, in case
  2631. a conversion fails. */
  2632. got_more = 0;
  2633. l1 = origl1;
  2634. l2 = origl2;
  2635. while (1) {
  2636. c1 = do_convert(NULL, MZ_UCS4_NAME, NULL, 1,
  2637. s1, d1 * 4, 4 * l1,
  2638. buf1, 0, MZ_SC_BUF_SIZE - 1,
  2639. 1 /* grow */, 0, 1 /* terminator size */,
  2640. &used1, &clen1,
  2641. &status);
  2642. c2 = do_convert(NULL, MZ_UCS4_NAME, NULL, 1,
  2643. s2, d2 * 4, 4 * l2,
  2644. buf2, 0, MZ_SC_BUF_SIZE - 1,
  2645. 1 /* grow */, 0, 1 /* terminator size */,
  2646. &used2, &clen2,
  2647. &status);
  2648. if ((used1 < 4 * l1) || (used2 < 4 * l2)) {
  2649. if (got_more) {
  2650. /* Something went wrong. We've already tried to
  2651. even out the parts that work. Let's give up
  2652. on the first characters */
  2653. clen1 = clen2 = 0;
  2654. break;
  2655. } else if (used1 == used2) {
  2656. /* Not everything, but both ended at the same point */
  2657. break;
  2658. } else {
  2659. /* Pick the smallest */
  2660. if (used2 < used1) {
  2661. used1 = used2;
  2662. got_more = 1;
  2663. } else
  2664. got_more = 2;
  2665. l2 = (used1 >> 2);
  2666. l1 = (used1 >> 2);
  2667. if (!l1) {
  2668. /* Nothing to get this time. */
  2669. clen1 = clen2 = 0;
  2670. c1 = c2 = "";
  2671. used1 = used2 = 0;
  2672. break;
  2673. }
  2674. }
  2675. } else
  2676. /* Got all that we wanted */
  2677. break;
  2678. }
  2679. if (cvt_case) {
  2680. if (clen1)
  2681. c1 = locale_recase(0, c1, 0, clen1,
  2682. case_buf1, 0, MZ_SC_BUF_SIZE - 1,
  2683. &clen1);
  2684. else
  2685. c1 = NULL;
  2686. if (clen2)
  2687. c2 = locale_recase(0, c2, 0, clen2,
  2688. case_buf2, 0, MZ_SC_BUF_SIZE - 1,
  2689. &clen2);
  2690. else
  2691. c2 = NULL;
  2692. /* There shouldn't have been conversion errors, but just in
  2693. case, care of NULL. */
  2694. if (!c1) c1 = "";
  2695. if (!c2) c2 = "";
  2696. }
  2697. /* Collate, finally. */
  2698. status = rktio_locale_strcoll(scheme_rktio, c1, c2);
  2699. /* If one is bigger than the other, we're done. */
  2700. if (status)
  2701. return status;
  2702. /* Otherwise, is there more to check? */
  2703. origl1 -= (used1 >> 2);
  2704. origl2 -= (used2 >> 2);
  2705. d1 += (used1 >> 2);
  2706. d2 += (used2 >> 2);
  2707. if (!origl1 && !origl2)
  2708. return 0;
  2709. /* There's more. It must be that the next character wasn't
  2710. convertable in one of the encodings. */
  2711. if (got_more)
  2712. return ((got_more == 2) ? 1 : -1);
  2713. if (!origl1)
  2714. return -1;
  2715. /* Compare an unconverable character directly. No case conversions
  2716. if it's outside the locale. */
  2717. if (((unsigned int *)s1)[d1] > ((unsigned int *)s2)[d2])
  2718. return 1;
  2719. else if (((unsigned int *)s1)[d1] < ((unsigned int *)s2)[d2])
  2720. return -1;
  2721. else {
  2722. /* We've skipped one unconvertable char, and they still look the
  2723. same. Now try again. */
  2724. origl1 -= 1;
  2725. origl2 -= 1;
  2726. d1 += 1;
  2727. d2 += 1;
  2728. }
  2729. }
  2730. }
  2731. int mz_native_strcoll(char *s1, int d1, int l1, char *s2, int d2, int l2, int cvt_case)
  2732. /* The s1 and s2 arguments are actually UTF-16. */
  2733. {
  2734. return rktio_strcoll_utf16(scheme_rktio,
  2735. (rktio_char16_t *)s1 XFORM_OK_PLUS d1, l1,
  2736. (rktio_char16_t *)s2 XFORM_OK_PLUS d2, l2,
  2737. cvt_case);
  2738. }
  2739. typedef int (*strcoll_proc)(char *s1, int d1, int l1, char *s2, int d2, int l2, int cvt_case);
  2740. int do_locale_comp(const char *who, const mzchar *us1, intptr_t ul1, const mzchar *us2, intptr_t ul2, int cvt_case)
  2741. {
  2742. int xl1;
  2743. int v, endres, utf16 = 0;
  2744. GC_CAN_IGNORE strcoll_proc mz_strcoll = mz_locale_strcoll;
  2745. if (current_locale_name
  2746. && !*current_locale_name
  2747. && (rktio_convert_properties(scheme_rktio) & RKTIO_CONVERT_STRCOLL_UTF16)) {
  2748. utf16 = 1;
  2749. mz_strcoll = mz_native_strcoll;
  2750. }
  2751. if (utf16) {
  2752. us1 = (mzchar *)scheme_ucs4_to_utf16(us1, 0, ul1, NULL, 0, &ul1, 1);
  2753. us2 = (mzchar *)scheme_ucs4_to_utf16(us2, 0, ul2, NULL, 0, &ul2, 1);
  2754. ((short *)us1)[ul1] = 0;
  2755. ((short *)us2)[ul2] = 0;
  2756. }
  2757. if (ul1 > ul2) {
  2758. ul1 = ul2;
  2759. endres = 1;
  2760. } else {
  2761. if (ul2 > ul1)
  2762. endres = -1;
  2763. else
  2764. endres = 0;
  2765. }
  2766. /* Walk back through the strings looking for nul characters. If we
  2767. find one, compare the part after the null character to update
  2768. endres, then continue. Unfortunately, we do too much work if an
  2769. earlier part of the string (tested later) determines the result,
  2770. but hopefully nul characters are rare. */
  2771. xl1 = 0;
  2772. while (ul1--) {
  2773. if ((utf16 && (!(((short *)us1)[ul1]) || !(((short *)us2)[ul1])))
  2774. || (!utf16 && (!(us1[ul1]) || !(us2[ul1])))) {
  2775. if (utf16) {
  2776. if (((short *)us1)[ul1])
  2777. endres = 1;
  2778. else if (((short *)us2)[ul1])
  2779. endres = -1;
  2780. } else {
  2781. if (us1[ul1])
  2782. endres = 1;
  2783. else if (us2[ul1])
  2784. endres = -1;
  2785. }
  2786. if (xl1)
  2787. v = mz_strcoll((char *)us1, ul1 + 1, xl1, (char *)us2, ul1 + 1, xl1, cvt_case);
  2788. else
  2789. v = 0;
  2790. if (v)
  2791. endres = v;
  2792. xl1 = 0;
  2793. } else {
  2794. xl1++;
  2795. }
  2796. }
  2797. v = mz_strcoll((char *)us1, 0, xl1, (char *)us2, 0, xl1, cvt_case);
  2798. if (v)
  2799. endres = v;
  2800. return endres;
  2801. }
  2802. mzchar *do_locale_recase(int to_up, mzchar *in, int delta, int len, intptr_t *olen)
  2803. {
  2804. Scheme_Object *parts = scheme_null;
  2805. char *c, buf[MZ_SC_BUF_SIZE], case_buf[MZ_SC_BUF_SIZE];
  2806. intptr_t clen, used;
  2807. int status;
  2808. while (len) {
  2809. /* We might have conversion errors... */
  2810. c = do_convert(NULL, MZ_UCS4_NAME, NULL, 1,
  2811. (char *)in, 4 * delta, 4 * len,
  2812. buf, 0, MZ_SC_BUF_SIZE - 1,
  2813. 1 /* grow */, 0, 1 /* terminator size */,
  2814. &used, &clen,
  2815. &status);
  2816. used >>= 2;
  2817. delta += used;
  2818. len -= used;
  2819. c = locale_recase(to_up, c, 0, clen,
  2820. case_buf, 0, MZ_SC_BUF_SIZE - 1,
  2821. &clen);
  2822. if (!c)
  2823. clen = 0;
  2824. c = do_convert(NULL, NULL, MZ_UCS4_NAME, 2,
  2825. c, 0, clen,
  2826. NULL, 0, 0,
  2827. 1 /* grow */, 0, sizeof(mzchar) /* terminator size */,
  2828. &used, &clen,
  2829. &status);
  2830. if (!len && SCHEME_NULLP(parts)) {
  2831. *olen = (clen >> 2);
  2832. ((mzchar *)c)[*olen] = 0;
  2833. return (mzchar *)c;
  2834. }
  2835. /* We can get here if there was some conversion error at some
  2836. point. We're building up a list of parts. */
  2837. parts = scheme_make_pair(scheme_make_sized_char_string((mzchar *)c, clen >> 2, 0),
  2838. parts);
  2839. if (len) {
  2840. /* Conversion error, so skip one char. */
  2841. parts = scheme_make_pair(scheme_make_sized_offset_char_string(in, delta, 1, 1),
  2842. parts);
  2843. delta += 1;
  2844. len -= 1;
  2845. }
  2846. }
  2847. parts = append_all_strings_backwards(parts);
  2848. *olen = SCHEME_CHAR_STRTAG_VAL(parts);
  2849. return SCHEME_CHAR_STR_VAL(parts);
  2850. }
  2851. mzchar *do_native_recase(int to_up, mzchar *in, int delta, int len, intptr_t *olen)
  2852. /* The in argument is actually UTF-16. */
  2853. {
  2854. rktio_char16_t *s, *s2;
  2855. intptr_t ol;
  2856. s = rktio_recase_utf16(scheme_rktio, to_up, (rktio_char16_t *)in XFORM_OK_PLUS delta, len, &ol);
  2857. s2 = scheme_malloc_atomic(sizeof(rktio_char16_t) * (ol+1));
  2858. memcpy(s2, s, sizeof(rktio_char16_t) * (ol+1));
  2859. free(s);
  2860. *olen = ol;
  2861. return (mzchar *)s2;
  2862. }
  2863. typedef mzchar *(*recase_proc)(int to_up, mzchar *in, int delta, int len, intptr_t *olen);
  2864. static Scheme_Object *mz_recase(const char *who, int to_up, mzchar *us, intptr_t ulen)
  2865. {
  2866. intptr_t ulen1;
  2867. int utf16 = 0, i, delta = 0;
  2868. mzchar *us1;
  2869. recase_proc mz_do_recase = do_locale_recase;
  2870. Scheme_Object *s, *parts = scheme_null;
  2871. reset_locale();
  2872. if (current_locale_name
  2873. && !*current_locale_name
  2874. && (rktio_convert_properties(scheme_rktio) & RKTIO_CONVERT_RECASE_UTF16)) {
  2875. utf16 = 1;
  2876. mz_do_recase = do_native_recase;
  2877. }
  2878. if (utf16) {
  2879. us = (mzchar *)scheme_ucs4_to_utf16(us, 0, ulen, NULL, 0, &ulen, 1);
  2880. ((short *)us)[ulen] = 0;
  2881. }
  2882. /* If there are nuls in the string, then we have to make multiple
  2883. calls to mz_do_recase */
  2884. i = 0;
  2885. while (1) {
  2886. for (; i < ulen; i++) {
  2887. if (utf16) {
  2888. if (!((short *)us)[i])
  2889. break;
  2890. } else if (!us[i])
  2891. break;
  2892. }
  2893. us1 = mz_do_recase(to_up, us, delta, i - delta, &ulen1);
  2894. if (utf16) {
  2895. us1 = scheme_utf16_to_ucs4((unsigned short *)us1, 0, ulen1, NULL, 0, &ulen1, 1);
  2896. us1[ulen1] = 0;
  2897. }
  2898. s = scheme_make_sized_char_string((mzchar *)us1, ulen1, 0);
  2899. if (SCHEME_NULLP(parts) && (i == ulen))
  2900. return s;
  2901. parts = scheme_make_pair(s, parts);
  2902. if (i == ulen)
  2903. break;
  2904. /* upcasing and encoding a nul char is easy: */
  2905. s = scheme_make_sized_char_string((mzchar *)"\0\0\0\0", 1, 0);
  2906. parts = scheme_make_pair(s, parts);
  2907. i++;
  2908. delta = i;
  2909. if (i == ulen)
  2910. break;
  2911. }
  2912. return append_all_strings_backwards(parts);
  2913. }
  2914. static Scheme_Object *
  2915. unicode_recase(const char *who, int to_up, int argc, Scheme_Object *argv[])
  2916. {
  2917. intptr_t len;
  2918. mzchar *chars;
  2919. if (!SCHEME_CHAR_STRINGP(argv[0]))
  2920. scheme_wrong_contract(who, "string?", 0, argc, argv);
  2921. chars = SCHEME_CHAR_STR_VAL(argv[0]);
  2922. len = SCHEME_CHAR_STRTAG_VAL(argv[0]);
  2923. return mz_recase(who, to_up, chars, len);
  2924. }
  2925. static Scheme_Object *
  2926. string_locale_upcase(int argc, Scheme_Object *argv[])
  2927. {
  2928. return unicode_recase("string-locale-upcase", 1, argc, argv);
  2929. }
  2930. static Scheme_Object *
  2931. string_locale_downcase(int argc, Scheme_Object *argv[])
  2932. {
  2933. return unicode_recase("string-locale-downcase", 0, argc, argv);
  2934. }
  2935. static void reset_locale(void)
  2936. {
  2937. Scheme_Object *v;
  2938. const mzchar *name;
  2939. /* This function needs to work before threads are set up: */
  2940. if (scheme_current_thread) {
  2941. v = scheme_get_param(scheme_current_config(), MZCONFIG_LOCALE);
  2942. } else {
  2943. v = scheme_make_immutable_sized_utf8_string("", 0);
  2944. }
  2945. locale_on = SCHEME_TRUEP(v);
  2946. if (locale_on) {
  2947. name = SCHEME_CHAR_STR_VAL(v);
  2948. if ((current_locale_name != name)
  2949. && (!current_locale_name
  2950. || mz_char_strcmp("result-locale",
  2951. current_locale_name, scheme_char_strlen(current_locale_name),
  2952. name, SCHEME_CHAR_STRLEN_VAL(v),
  2953. 0, 1))) {
  2954. char *n, buf[32];
  2955. n = scheme_utf8_encode_to_buffer(name, SCHEME_CHAR_STRLEN_VAL(v), buf, 32);
  2956. rktio_set_locale(scheme_rktio, n);
  2957. }
  2958. current_locale_name_ptr = (void *)name;
  2959. }
  2960. }
  2961. char *scheme_push_c_numeric_locale()
  2962. {
  2963. return rktio_push_c_numeric_locale(scheme_rktio);
  2964. }
  2965. void scheme_pop_c_numeric_locale(char *prev)
  2966. {
  2967. rktio_pop_c_numeric_locale(scheme_rktio, prev);
  2968. }
  2969. static int find_special_casing(int ch)
  2970. {
  2971. /* Binary search */
  2972. int i, lo, hi, j;
  2973. i = NUM_SPECIAL_CASINGS >> 1;
  2974. lo = i;
  2975. hi = NUM_SPECIAL_CASINGS - i - 1;
  2976. while (1) {
  2977. if (uchar_special_casings[i * 10] == ch)
  2978. return i * 10;
  2979. if (uchar_special_casings[i * 10] > ch) {
  2980. j = i - lo;
  2981. i = j + (lo >> 1);
  2982. hi = lo - (i - j) - 1;
  2983. lo = i - j;
  2984. } else {
  2985. j = i + 1;
  2986. i = j + (hi >> 1);
  2987. lo = i - j;
  2988. hi = hi - (i - j) - 1;
  2989. }
  2990. }
  2991. }
  2992. static int is_final_sigma(int mode, mzchar *s, int d, int i, int len)
  2993. {
  2994. int j;
  2995. if (mode == 3)
  2996. return 1;
  2997. /* find a cased char before, skipping case-ignorable: */
  2998. for (j = i - 1; j >= d; j--) {
  2999. if (!scheme_iscaseignorable(s[j])) {
  3000. if (scheme_iscased(s[j]))
  3001. break;
  3002. else
  3003. return 0;
  3004. }
  3005. }
  3006. if (j < d)
  3007. return 0;
  3008. /* next non-case-ignorable must not be cased: */
  3009. for (j = i + 1; j < d + len; j++) {
  3010. if (!scheme_iscaseignorable(s[j])) {
  3011. return !scheme_iscased(s[j]);
  3012. }
  3013. }
  3014. return 1;
  3015. }
  3016. mzchar *scheme_string_recase(mzchar *s, int d, int len, int mode, int inplace, int *_len)
  3017. {
  3018. mzchar *t;
  3019. int i, extra = 0, pos, special = 0, td, prev_was_cased = 0, xmode = mode;
  3020. for (i = 0; i < len; i++) {
  3021. if (scheme_isspecialcasing(s[d+i])) {
  3022. pos = find_special_casing(s[d+i]);
  3023. if (!uchar_special_casings[pos + 9] || is_final_sigma(xmode, s, d, i, len)) {
  3024. special = 1;
  3025. extra += (uchar_special_casings[pos + 1 + (xmode << 1)] - 1);
  3026. }
  3027. }
  3028. if (mode == 2) {
  3029. if (!scheme_iscaseignorable(s[d+i]))
  3030. prev_was_cased = scheme_iscased(s[d+i]);
  3031. xmode = (prev_was_cased ? 0 : 2);
  3032. }
  3033. }
  3034. if (_len)
  3035. *_len = len + extra;
  3036. if (!extra && inplace) {
  3037. t = s;
  3038. td = d;
  3039. } else {
  3040. t = scheme_malloc_atomic(sizeof(mzchar) * (len + extra + 1));
  3041. td = 0;
  3042. }
  3043. if (!special) {
  3044. if (mode == 0) {
  3045. for (i = 0; i < len; i++) {
  3046. t[i+td] = scheme_tolower(s[i+d]);
  3047. }
  3048. } else if (mode == 1) {
  3049. for (i = 0; i < len; i++) {
  3050. t[i+td] = scheme_toupper(s[i+d]);
  3051. }
  3052. } else if (mode == 2) {
  3053. prev_was_cased = 0;
  3054. for (i = 0; i < len; i++) {
  3055. if (!prev_was_cased)
  3056. t[i+td] = scheme_totitle(s[i+d]);
  3057. else
  3058. t[i+td] = scheme_tolower(s[i+d]);
  3059. if (!scheme_iscaseignorable(s[i+d]))
  3060. prev_was_cased = scheme_iscased(s[i+d]);
  3061. }
  3062. } else /* if (mode == 3) */ {
  3063. for (i = 0; i < len; i++) {
  3064. t[i+td] = scheme_tofold(s[i+d]);
  3065. }
  3066. }
  3067. } else {
  3068. int j = 0, c;
  3069. prev_was_cased = 0;
  3070. for (i = 0; i < len; i++) {
  3071. if (mode == 0) {
  3072. t[j+td] = scheme_tolower(s[i+d]);
  3073. } else if (mode == 1) {
  3074. t[j+td] = scheme_toupper(s[i+d]);
  3075. } else if (mode == 2) {
  3076. if (!prev_was_cased) {
  3077. xmode = 2;
  3078. t[j+td] = scheme_totitle(s[i+d]);
  3079. } else {
  3080. xmode = 0;
  3081. t[j+td] = scheme_tolower(s[i+d]);
  3082. }
  3083. if (!scheme_iscaseignorable(s[i+d]))
  3084. prev_was_cased = scheme_iscased(s[i+d]);
  3085. } else /* if (mode == 3) */ {
  3086. t[j+td] = scheme_tofold(s[i+d]);
  3087. }
  3088. if (scheme_isspecialcasing(s[i+d])) {
  3089. pos = find_special_casing(s[i+d]);
  3090. if (!uchar_special_casings[pos + 9] || is_final_sigma(xmode, s, d, i, len)) {
  3091. c = uchar_special_casings[pos + 1 + (xmode << 1)];
  3092. pos = uchar_special_casings[pos + 2 + (xmode << 1)];
  3093. while (c--) {
  3094. t[(j++)+td] = uchar_special_casing_data[pos++];
  3095. }
  3096. } else
  3097. j++;
  3098. } else
  3099. j++;
  3100. }
  3101. }
  3102. t[len+extra+td] = 0;
  3103. return t;
  3104. }
  3105. static Scheme_Object *string_recase (const char *name, int argc, Scheme_Object *argv[], int mode)
  3106. {
  3107. mzchar *s;
  3108. int len;
  3109. if (!SCHEME_CHAR_STRINGP(argv[0]))
  3110. scheme_wrong_contract(name, "string?", 0, argc, argv);
  3111. s = SCHEME_CHAR_STR_VAL(argv[0]);
  3112. len = SCHEME_CHAR_STRLEN_VAL(argv[0]);
  3113. s = scheme_string_recase(s, 0, len, mode, 0, &len);
  3114. return scheme_make_sized_char_string(s, len, 0);
  3115. }
  3116. static Scheme_Object *string_upcase (int argc, Scheme_Object *argv[])
  3117. {
  3118. return string_recase("string-upcase", argc, argv, 1);
  3119. }
  3120. static Scheme_Object *string_downcase (int argc, Scheme_Object *argv[])
  3121. {
  3122. return string_recase("string-downcase", argc, argv, 0);
  3123. }
  3124. static Scheme_Object *string_titlecase (int argc, Scheme_Object *argv[])
  3125. {
  3126. return string_recase("string-titlecase", argc, argv, 2);
  3127. }
  3128. static Scheme_Object *string_foldcase (int argc, Scheme_Object *argv[])
  3129. {
  3130. return string_recase("string-foldcase", argc, argv, 3);
  3131. }
  3132. /**********************************************************************/
  3133. /* normalization */
  3134. /**********************************************************************/
  3135. #define MZ_JAMO_INITIAL_CONSONANT_START 0x1100
  3136. #define MZ_JAMO_INITIAL_CONSONANT_COUNT 19
  3137. #define MZ_JAMO_INITIAL_CONSONANT_END (MZ_JAMO_INITIAL_CONSONANT_START + MZ_JAMO_INITIAL_CONSONANT_COUNT - 1)
  3138. #define MZ_JAMO_VOWEL_START 0x1161
  3139. #define MZ_JAMO_VOWEL_COUNT 21
  3140. #define MZ_JAMO_VOWEL_END (MZ_JAMO_VOWEL_START + MZ_JAMO_VOWEL_COUNT - 1)
  3141. /* First in this range is not actually a consonant, but a placeholder for "no consonant" */
  3142. #define MZ_JAMO_TRAILING_CONSONANT_START 0x11A7
  3143. #define MZ_JAMO_TRAILING_CONSONANT_COUNT 28
  3144. #define MZ_JAMO_TRAILING_CONSONANT_END (MZ_JAMO_TRAILING_CONSONANT_START + MZ_JAMO_TRAILING_CONSONANT_COUNT - 1)
  3145. #define MZ_JAMO_SYLLABLE_START 0xAC00
  3146. #define MZ_JAMO_SYLLABLE_END (MZ_JAMO_SYLLABLE_START + 11171)
  3147. XFORM_NONGCING static mzchar get_composition(mzchar a, mzchar b)
  3148. {
  3149. if ((a > 0xFFFF) || (b > 0xFFFF)) {
  3150. /* Look in long-composes table. */
  3151. mzlonglong key = ((((mzlonglong)a & 0x1F0000) << 21)
  3152. | (((mzlonglong)a & 0xFFFF) << 16)
  3153. | (((mzlonglong)b & 0x1F0000) << 16)
  3154. | ((mzlonglong)b & 0xFFFF));
  3155. int pos = (LONG_COMPOSE_TABLE_SIZE >> 1), new_pos;
  3156. int below_len = pos;
  3157. int above_len = (LONG_COMPOSE_TABLE_SIZE - pos - 1);
  3158. /* Binary search: */
  3159. while (key != utable_canon_compose_long_pairs[pos]) {
  3160. if (key > utable_canon_compose_long_pairs[pos]) {
  3161. if (!above_len)
  3162. return 0;
  3163. new_pos = pos + (above_len >> 1) + 1;
  3164. below_len = (new_pos - pos - 1);
  3165. above_len = (above_len - below_len - 1);
  3166. pos = new_pos;
  3167. } else if (key < utable_canon_compose_long_pairs[pos]) {
  3168. if (!below_len)
  3169. return 0;
  3170. new_pos = pos - ((below_len >> 1) + 1);
  3171. above_len = (pos - new_pos - 1);
  3172. below_len = (below_len - above_len - 1);
  3173. pos = new_pos;
  3174. }
  3175. }
  3176. return utable_canon_compose_long_result[pos];
  3177. } else {
  3178. uintptr_t key = (a << 16) | b;
  3179. int pos = (COMPOSE_TABLE_SIZE >> 1), new_pos;
  3180. int below_len = pos;
  3181. int above_len = (COMPOSE_TABLE_SIZE - pos - 1);
  3182. /* Binary search: */
  3183. while (key != utable_compose_pairs[pos]) {
  3184. if (key > utable_compose_pairs[pos]) {
  3185. if (!above_len)
  3186. return 0;
  3187. new_pos = pos + (above_len >> 1) + 1;
  3188. below_len = (new_pos - pos - 1);
  3189. above_len = (above_len - below_len - 1);
  3190. pos = new_pos;
  3191. } else if (key < utable_compose_pairs[pos]) {
  3192. if (!below_len)
  3193. return 0;
  3194. new_pos = pos - ((below_len >> 1) + 1);
  3195. above_len = (pos - new_pos - 1);
  3196. below_len = (below_len - above_len - 1);
  3197. pos = new_pos;
  3198. }
  3199. }
  3200. return utable_compose_result[pos];
  3201. }
  3202. }
  3203. XFORM_NONGCING mzchar get_canon_decomposition(mzchar key, mzchar *b)
  3204. {
  3205. int pos = (DECOMPOSE_TABLE_SIZE >> 1), new_pos;
  3206. int below_len = pos;
  3207. int above_len = (DECOMPOSE_TABLE_SIZE - pos - 1);
  3208. /* Binary search: */
  3209. while (key != utable_decomp_keys[pos]) {
  3210. if (key > utable_decomp_keys[pos]) {
  3211. if (!above_len)
  3212. return 0;
  3213. new_pos = pos + (above_len >> 1) + 1;
  3214. below_len = (new_pos - pos - 1);
  3215. above_len = (above_len - below_len - 1);
  3216. pos = new_pos;
  3217. } else if (key < utable_decomp_keys[pos]) {
  3218. if (!below_len)
  3219. return 0;
  3220. new_pos = pos - ((below_len >> 1) + 1);
  3221. above_len = (pos - new_pos - 1);
  3222. below_len = (below_len - above_len - 1);
  3223. pos = new_pos;
  3224. }
  3225. }
  3226. pos = utable_decomp_indices[pos];
  3227. if (pos < 0) {
  3228. pos = -(pos + 1);
  3229. pos <<= 1;
  3230. *b = utable_compose_long_pairs[pos + 1];
  3231. return utable_compose_long_pairs[pos];
  3232. } else {
  3233. key = utable_compose_pairs[pos];
  3234. *b = (key & 0xFFFF);
  3235. return (key >> 16);
  3236. }
  3237. }
  3238. XFORM_NONGCING int get_kompat_decomposition(mzchar key, unsigned short **chars)
  3239. {
  3240. int pos = (KOMPAT_DECOMPOSE_TABLE_SIZE >> 1), new_pos;
  3241. int below_len = pos;
  3242. int above_len = (KOMPAT_DECOMPOSE_TABLE_SIZE - pos - 1);
  3243. /* Binary search: */
  3244. while (key != utable_kompat_decomp_keys[pos]) {
  3245. if (key > utable_kompat_decomp_keys[pos]) {
  3246. if (!above_len)
  3247. return 0;
  3248. new_pos = pos + (above_len >> 1) + 1;
  3249. below_len = (new_pos - pos - 1);
  3250. above_len = (above_len - below_len - 1);
  3251. pos = new_pos;
  3252. } else if (key < utable_kompat_decomp_keys[pos]) {
  3253. if (!below_len)
  3254. return 0;
  3255. new_pos = pos - ((below_len >> 1) + 1);
  3256. above_len = (pos - new_pos - 1);
  3257. below_len = (below_len - above_len - 1);
  3258. pos = new_pos;
  3259. }
  3260. }
  3261. *chars = utable_kompat_decomp_strs XFORM_OK_PLUS utable_kompat_decomp_indices[pos];
  3262. return utable_kompat_decomp_lens[pos];
  3263. }
  3264. static Scheme_Object *normalize_c(Scheme_Object *o)
  3265. /* Assumes then given string is in normal form D */
  3266. {
  3267. mzchar *s, *s2, tmp, last_c0 = 0;
  3268. int len, i, j = 0, last_c0_pos = 0, last_cc = 0;
  3269. s = SCHEME_CHAR_STR_VAL(o);
  3270. len = SCHEME_CHAR_STRLEN_VAL(o);
  3271. s2 = (mzchar *)scheme_malloc_atomic((len + 1) * sizeof(mzchar));
  3272. memcpy(s2, s, len * sizeof(mzchar));
  3273. for (i = 0; i < len; i++) {
  3274. if ((i + 1 < len)
  3275. && (s2[i] >= MZ_JAMO_INITIAL_CONSONANT_START)
  3276. && (s2[i] <= MZ_JAMO_INITIAL_CONSONANT_END)
  3277. && (s2[i+1] >= MZ_JAMO_VOWEL_START)
  3278. && (s2[i+1] <= MZ_JAMO_VOWEL_END)) {
  3279. /* Need Hangul composition */
  3280. if ((i + 2 < len)
  3281. && (s2[i+2] > MZ_JAMO_TRAILING_CONSONANT_START)
  3282. && (s2[i+2] <= MZ_JAMO_TRAILING_CONSONANT_END)) {
  3283. /* 3-char composition */
  3284. tmp = (MZ_JAMO_SYLLABLE_START
  3285. + ((s2[i] - MZ_JAMO_INITIAL_CONSONANT_START)
  3286. * MZ_JAMO_VOWEL_COUNT * MZ_JAMO_TRAILING_CONSONANT_COUNT)
  3287. + ((s2[i+1] - MZ_JAMO_VOWEL_START)
  3288. * MZ_JAMO_TRAILING_CONSONANT_COUNT)
  3289. + (s2[i+2] - MZ_JAMO_TRAILING_CONSONANT_START));
  3290. i += 2;
  3291. } else {
  3292. /* 2-char composition */
  3293. tmp = (MZ_JAMO_SYLLABLE_START
  3294. + ((s2[i] - MZ_JAMO_INITIAL_CONSONANT_START)
  3295. * MZ_JAMO_VOWEL_COUNT * MZ_JAMO_TRAILING_CONSONANT_COUNT)
  3296. + ((s2[i+1] - MZ_JAMO_VOWEL_START)
  3297. * MZ_JAMO_TRAILING_CONSONANT_COUNT));
  3298. i++;
  3299. }
  3300. last_c0 = tmp;
  3301. last_c0_pos = j;
  3302. last_cc = 0;
  3303. s2[j++] = tmp;
  3304. } else {
  3305. int cc;
  3306. cc = scheme_combining_class(s2[i]);
  3307. if (last_c0 && (cc > last_cc))
  3308. tmp = get_composition(last_c0, s2[i]);
  3309. else
  3310. tmp = 0;
  3311. if (tmp) {
  3312. /* Need to compose */
  3313. s2[last_c0_pos] = tmp;
  3314. last_c0 = tmp;
  3315. } else if (!cc) {
  3316. /* Reset last_c0... */
  3317. tmp = s2[i];
  3318. if (scheme_needs_maybe_compose(tmp)) {
  3319. last_c0 = tmp;
  3320. last_c0_pos = j;
  3321. } else {
  3322. last_c0 = 0;
  3323. }
  3324. last_cc = -1;
  3325. s2[j++] = tmp;
  3326. } else {
  3327. s2[j++] = s2[i];
  3328. last_cc = cc;
  3329. }
  3330. }
  3331. }
  3332. s2[j] = 0;
  3333. if (len - j > 16) {
  3334. s = (mzchar *)scheme_malloc_atomic((j + 1) * sizeof(mzchar));
  3335. memcpy(s, s2, (j + 1) * sizeof(mzchar));
  3336. s2 = s;
  3337. }
  3338. return scheme_make_sized_char_string(s2, j, 0);
  3339. }
  3340. static Scheme_Object *normalize_d(Scheme_Object *o, int kompat)
  3341. {
  3342. mzchar *s, tmp, *s2;
  3343. int len, i, delta, j, swapped;
  3344. s = SCHEME_CHAR_STR_VAL(o);
  3345. len = SCHEME_CHAR_STRLEN_VAL(o);
  3346. /* Run through string list to predict expansion: */
  3347. delta = 0;
  3348. for (i = 0; i < len; i++) {
  3349. if (scheme_needs_decompose(s[i])) {
  3350. int klen;
  3351. mzchar snd;
  3352. GC_CAN_IGNORE unsigned short *start;
  3353. tmp = s[i];
  3354. while (scheme_needs_decompose(tmp)) {
  3355. if (kompat)
  3356. klen = get_kompat_decomposition(tmp, &start);
  3357. else
  3358. klen = 0;
  3359. if (klen) {
  3360. delta += (klen - 1);
  3361. break;
  3362. } else {
  3363. tmp = get_canon_decomposition(tmp, &snd);
  3364. if (tmp) {
  3365. if (snd) {
  3366. delta++;
  3367. if (kompat) {
  3368. klen = get_kompat_decomposition(snd, &start);
  3369. if (klen)
  3370. delta += (klen - 1);
  3371. }
  3372. }
  3373. } else
  3374. break;
  3375. }
  3376. }
  3377. } else if ((s[i] >= MZ_JAMO_SYLLABLE_START)
  3378. && (s[i] <= MZ_JAMO_SYLLABLE_END)) {
  3379. tmp = s[i];
  3380. tmp -= MZ_JAMO_SYLLABLE_START;
  3381. if (tmp % MZ_JAMO_TRAILING_CONSONANT_COUNT)
  3382. delta += 2;
  3383. else
  3384. delta += 1;
  3385. }
  3386. }
  3387. s2 = (mzchar *)scheme_malloc_atomic((len + delta + 1) * sizeof(mzchar));
  3388. j = 0;
  3389. for (i = 0; i < len; i++) {
  3390. if (scheme_needs_decompose(s[i])) {
  3391. mzchar snd, tmp2;
  3392. int snds = 0, klen = 0, k;
  3393. GC_CAN_IGNORE unsigned short*start;
  3394. tmp = s[i];
  3395. while (scheme_needs_decompose(tmp)) {
  3396. if (kompat)
  3397. klen = get_kompat_decomposition(tmp, &start);
  3398. else
  3399. klen = 0;
  3400. if (klen) {
  3401. for (k = 0; k < klen; k++) {
  3402. s2[j++] = start[k];
  3403. }
  3404. break;
  3405. } else {
  3406. tmp2 = get_canon_decomposition(tmp, &snd);
  3407. if (tmp2) {
  3408. tmp = tmp2;
  3409. if (snd) {
  3410. if (kompat)
  3411. klen = get_kompat_decomposition(snd, &start);
  3412. else
  3413. klen = 0;
  3414. if (klen) {
  3415. snds += klen;
  3416. for (k = 0; k < klen; k++) {
  3417. s2[len + delta - snds + k] = start[k];
  3418. }
  3419. klen = 0;
  3420. } else {
  3421. snds++;
  3422. s2[len + delta - snds] = snd;
  3423. }
  3424. }
  3425. } else
  3426. break;
  3427. }
  3428. }
  3429. if (!klen)
  3430. s2[j++] = tmp;
  3431. memmove(s2 + j, s2 + len + delta - snds, snds * sizeof(mzchar));
  3432. j += snds;
  3433. } else if ((s[i] >= MZ_JAMO_SYLLABLE_START)
  3434. && (s[i] <= MZ_JAMO_SYLLABLE_END)) {
  3435. int l, v, t;
  3436. tmp = s[i];
  3437. tmp -= MZ_JAMO_SYLLABLE_START;
  3438. l = tmp / (MZ_JAMO_VOWEL_COUNT * MZ_JAMO_TRAILING_CONSONANT_COUNT);
  3439. v = (tmp % (MZ_JAMO_VOWEL_COUNT * MZ_JAMO_TRAILING_CONSONANT_COUNT)) / MZ_JAMO_TRAILING_CONSONANT_COUNT;
  3440. t = tmp % MZ_JAMO_TRAILING_CONSONANT_COUNT;
  3441. s2[j++] = MZ_JAMO_INITIAL_CONSONANT_START + l;
  3442. s2[j++] = MZ_JAMO_VOWEL_START + v;
  3443. if (t) {
  3444. s2[j++] = MZ_JAMO_TRAILING_CONSONANT_START + t;
  3445. }
  3446. } else {
  3447. s2[j++] = s[i];
  3448. }
  3449. }
  3450. s2[j] = 0;
  3451. len += delta;
  3452. /* Reorder pass: */
  3453. do {
  3454. swapped = 0;
  3455. for (i = 0; i < len; i++) {
  3456. if ((i + 1 < len)
  3457. && scheme_combining_class(s2[i])
  3458. && scheme_combining_class(s2[i+1])
  3459. && (scheme_combining_class(s2[i+1]) < scheme_combining_class(s2[i]))) {
  3460. /* Reorder and try again: */
  3461. tmp = s2[i + 1];
  3462. s2[i + 1] = s2[i];
  3463. s2[i] = tmp;
  3464. i--;
  3465. swapped = 1;
  3466. }
  3467. }
  3468. } while (swapped);
  3469. return scheme_make_sized_char_string(s2, len, 0);
  3470. }
  3471. static Scheme_Object *do_string_normalize_c (const char *who, int argc, Scheme_Object *argv[], int kompat)
  3472. {
  3473. Scheme_Object *o;
  3474. mzchar *s, last_c0 = 0, snd;
  3475. int len, i, last_cc = 0;
  3476. o = argv[0];
  3477. if (!SCHEME_CHAR_STRINGP(o))
  3478. scheme_wrong_contract(who, "string?", 0, argc, argv);
  3479. s = SCHEME_CHAR_STR_VAL(o);
  3480. len = SCHEME_CHAR_STRLEN_VAL(o);
  3481. for (i = 0; i < len; i++) {
  3482. if (scheme_needs_decompose(s[i])
  3483. && (kompat || get_canon_decomposition(s[i], &snd))) {
  3484. /* Decomposition may expose a different composition */
  3485. break;
  3486. } else if ((i + 1 < len)
  3487. && scheme_combining_class(s[i])
  3488. && scheme_combining_class(s[i+1])
  3489. && (scheme_combining_class(s[i+1]) < scheme_combining_class(s[i]))) {
  3490. /* Need to reorder */
  3491. break;
  3492. } else if ((s[i] >= MZ_JAMO_INITIAL_CONSONANT_START)
  3493. && (s[i] <= MZ_JAMO_INITIAL_CONSONANT_END)
  3494. && (s[i+1] >= MZ_JAMO_VOWEL_START)
  3495. && (s[i+1] <= MZ_JAMO_VOWEL_END)) {
  3496. /* Need Hangul composition */
  3497. break;
  3498. } else if (last_c0
  3499. && get_composition(last_c0, s[i])
  3500. && (scheme_combining_class(s[i]) > last_cc)) {
  3501. /* Need to compose */
  3502. break;
  3503. } else {
  3504. int cc;
  3505. cc = scheme_combining_class(s[i]);
  3506. if (!cc) {
  3507. if (scheme_needs_maybe_compose(s[i]))
  3508. last_c0 = s[i];
  3509. else
  3510. last_c0 = 0;
  3511. last_cc = -1;
  3512. } else
  3513. last_cc = cc;
  3514. }
  3515. }
  3516. if (i < len) {
  3517. o = normalize_c(normalize_d(o, kompat));
  3518. }
  3519. return o;
  3520. }
  3521. static Scheme_Object *string_normalize_c (int argc, Scheme_Object *argv[])
  3522. {
  3523. return do_string_normalize_c("string-normalize-nfc", argc, argv, 0);
  3524. }
  3525. static Scheme_Object *string_normalize_kc (int argc, Scheme_Object *argv[])
  3526. {
  3527. return do_string_normalize_c("string-normalize-nfkc", argc, argv, 1);
  3528. }
  3529. static Scheme_Object *do_string_normalize_d (const char *who, int argc, Scheme_Object *argv[], int kompat)
  3530. {
  3531. Scheme_Object *o;
  3532. mzchar *s;
  3533. int len, i;
  3534. o = argv[0];
  3535. if (!SCHEME_CHAR_STRINGP(o))
  3536. scheme_wrong_contract(who, "string?", 0, argc, argv);
  3537. s = SCHEME_CHAR_STR_VAL(o);
  3538. len = SCHEME_CHAR_STRLEN_VAL(o);
  3539. for (i = len; i--; ) {
  3540. if (scheme_needs_decompose(s[i])) {
  3541. /* Need to decompose */
  3542. mzchar snd;
  3543. if (kompat || get_canon_decomposition(s[i], &snd))
  3544. break;
  3545. } else if ((i + 1 < len)
  3546. && scheme_combining_class(s[i])
  3547. && scheme_combining_class(s[i+1])
  3548. && (scheme_combining_class(s[i+1]) < scheme_combining_class(s[i]))) {
  3549. /* Need to reorder */
  3550. break;
  3551. } else if ((s[i] >= MZ_JAMO_SYLLABLE_START)
  3552. && (s[i] <= MZ_JAMO_SYLLABLE_END)) {
  3553. /* Need Hangul decomposition */
  3554. break;
  3555. }
  3556. }
  3557. if (i >= 0) {
  3558. o = normalize_d(o, kompat);
  3559. }
  3560. return o;
  3561. }
  3562. static Scheme_Object *string_normalize_d (int argc, Scheme_Object *argv[])
  3563. {
  3564. return do_string_normalize_d("string-normalize-nfd", argc, argv, 0);
  3565. }
  3566. static Scheme_Object *string_normalize_kd (int argc, Scheme_Object *argv[])
  3567. {
  3568. return do_string_normalize_d("string-normalize-nfkd", argc, argv, 1);
  3569. }
  3570. /**********************************************************************/
  3571. /* strcmps */
  3572. /**********************************************************************/
  3573. intptr_t scheme_char_strlen(const mzchar *s)
  3574. {
  3575. intptr_t i;
  3576. if ((intptr_t)s & 0x3)
  3577. abort();
  3578. for (i = 0; s[i]; i++) {
  3579. }
  3580. return i;
  3581. }
  3582. static int mz_char_strcmp(const char *who, const mzchar *str1, intptr_t l1, const mzchar *str2, intptr_t l2,
  3583. int use_locale, int size_shortcut)
  3584. {
  3585. intptr_t endres;
  3586. if (size_shortcut && (l1 != l2))
  3587. return 1;
  3588. if (use_locale) {
  3589. reset_locale();
  3590. if (locale_on) {
  3591. return do_locale_comp(who, str1, l1, str2, l2, 0);
  3592. }
  3593. }
  3594. if (l1 > l2) {
  3595. l1 = l2;
  3596. endres = 1;
  3597. } else {
  3598. if (l2 > l1)
  3599. endres = -1;
  3600. else
  3601. endres = 0;
  3602. }
  3603. while (l1--) {
  3604. unsigned int a, b;
  3605. a = *(str1++);
  3606. b = *(str2++);
  3607. a = a - b;
  3608. if (a)
  3609. return a;
  3610. }
  3611. return endres;
  3612. }
  3613. static int mz_char_strcmp_ci(const char *who, const mzchar *str1, intptr_t l1, const mzchar *str2, intptr_t l2,
  3614. int use_locale, int size_shortcut)
  3615. {
  3616. intptr_t p1, p2, sp1, sp2, a, b;
  3617. mzchar spec1[SPECIAL_CASE_FOLD_MAX], spec2[SPECIAL_CASE_FOLD_MAX];
  3618. if (size_shortcut && (l1 != l2))
  3619. return 1;
  3620. if (use_locale) {
  3621. reset_locale();
  3622. if (locale_on) {
  3623. return do_locale_comp(who, str1, l1, str2, l2, 1);
  3624. }
  3625. }
  3626. p1 = sp1 = 0;
  3627. p2 = sp2 = 0;
  3628. while (((p1 < l1) || sp1) && ((p2 < l2) || sp2)) {
  3629. if (sp1) {
  3630. a = spec1[--sp1];
  3631. } else {
  3632. a = str1[p1];
  3633. if (scheme_isspecialcasing(a)) {
  3634. int pos, i;
  3635. pos = find_special_casing(a);
  3636. sp1 = uchar_special_casings[pos + 7];
  3637. pos = uchar_special_casings[pos + 8];
  3638. for (i = sp1; i--; pos++) {
  3639. spec1[i] = uchar_special_casing_data[pos];
  3640. }
  3641. a = spec1[--sp1];
  3642. } else {
  3643. a = scheme_tofold(a);
  3644. }
  3645. p1++;
  3646. }
  3647. if (sp2) {
  3648. b = spec2[--sp2];
  3649. } else {
  3650. b = str2[p2];
  3651. if (scheme_isspecialcasing(b)) {
  3652. int pos, i;
  3653. pos = find_special_casing(b);
  3654. sp2 = uchar_special_casings[pos + 7];
  3655. pos = uchar_special_casings[pos + 8];
  3656. for (i = sp2; i--; pos++) {
  3657. spec2[i] = uchar_special_casing_data[pos];
  3658. }
  3659. b = spec2[--sp2];
  3660. } else {
  3661. b = scheme_tofold(b);
  3662. }
  3663. p2++;
  3664. }
  3665. a = a - b;
  3666. if (a)
  3667. return a;
  3668. }
  3669. return ((p1 < l1) || sp1) - ((p2 < l2) || sp2);
  3670. }
  3671. static int mz_strcmp(const char *who, unsigned char *str1, intptr_t l1, unsigned char *str2, intptr_t l2)
  3672. {
  3673. intptr_t endres;
  3674. if (l1 > l2) {
  3675. l1 = l2;
  3676. endres = 1;
  3677. } else {
  3678. if (l2 > l1)
  3679. endres = -1;
  3680. else
  3681. endres = 0;
  3682. }
  3683. while (l1--) {
  3684. unsigned int a, b;
  3685. a = *(str1++);
  3686. b = *(str2++);
  3687. a = a - b;
  3688. if (a)
  3689. return a;
  3690. }
  3691. return endres;
  3692. }
  3693. int scheme_string_compare(Scheme_Object *a, Scheme_Object *b)
  3694. {
  3695. return mz_char_strcmp(NULL,
  3696. SCHEME_CHAR_STR_VAL(a), SCHEME_CHAR_STRTAG_VAL(a),
  3697. SCHEME_CHAR_STR_VAL(b), SCHEME_CHAR_STRTAG_VAL(b),
  3698. 0, 0);
  3699. }
  3700. int scheme_bytes_compare(Scheme_Object *a, Scheme_Object *b)
  3701. {
  3702. return mz_strcmp(NULL,
  3703. (unsigned char *)SCHEME_BYTE_STR_VAL(a), SCHEME_BYTE_STRTAG_VAL(a),
  3704. (unsigned char *)SCHEME_BYTE_STR_VAL(b), SCHEME_BYTE_STRTAG_VAL(b));
  3705. }
  3706. /**********************************************************************/
  3707. /* byte string conversion */
  3708. /**********************************************************************/
  3709. static void close_converter(Scheme_Object *o, void *data)
  3710. {
  3711. Scheme_Converter *c = (Scheme_Converter *)o;
  3712. if (!c->closed) {
  3713. c->closed = 1;
  3714. if (c->kind == mzICONV_KIND) {
  3715. rktio_converter_close(scheme_rktio, c->cd);
  3716. c->cd = NULL;
  3717. }
  3718. if (c->mref) {
  3719. scheme_remove_managed(c->mref, (Scheme_Object *)c);
  3720. c->mref = NULL;
  3721. }
  3722. }
  3723. }
  3724. Scheme_Object *scheme_open_converter(const char *from_e, const char *to_e)
  3725. {
  3726. Scheme_Converter *c;
  3727. rktio_converter_t *cd;
  3728. int kind;
  3729. int permissive;
  3730. int need_regis = 1;
  3731. Scheme_Custodian_Reference *mref;
  3732. if (!*to_e || !*from_e)
  3733. reset_locale();
  3734. if ((!strcmp(from_e, "UTF-8")
  3735. || !strcmp(from_e, "UTF-8-permissive")
  3736. || (!*from_e && mzLOCALE_IS_UTF_8(current_locale_name)))
  3737. && (!strcmp(to_e, "UTF-8")
  3738. || (!*to_e && mzLOCALE_IS_UTF_8(current_locale_name)))) {
  3739. /* Use the built-in UTF-8<->UTF-8 converter: */
  3740. kind = mzUTF8_KIND;
  3741. if (!strcmp(from_e, "UTF-8-permissive"))
  3742. permissive = 0xFFFD;
  3743. else
  3744. permissive = 0;
  3745. cd = NULL;
  3746. need_regis = 0;
  3747. } else if ((!strcmp(from_e, "platform-UTF-8")
  3748. || !strcmp(from_e, "platform-UTF-8-permissive"))
  3749. && !strcmp(to_e, "platform-UTF-16")) {
  3750. kind = mzUTF8_TO_UTF16_KIND;
  3751. if (!strcmp(from_e, "platform-UTF-8-permissive"))
  3752. permissive = 0xFFFD;
  3753. else
  3754. permissive = 0;
  3755. cd = NULL;
  3756. need_regis = 0;
  3757. } else if (!strcmp(from_e, "platform-UTF-16")
  3758. && !strcmp(to_e, "platform-UTF-8")) {
  3759. kind = mzUTF16_TO_UTF8_KIND;
  3760. permissive = 0;
  3761. cd = NULL;
  3762. need_regis = 0;
  3763. } else {
  3764. char *tmp_from_e = NULL, *tmp_to_e = NULL;
  3765. if (!(rktio_convert_properties(scheme_rktio) & RKTIO_CONVERTER_SUPPORTED))
  3766. return scheme_false;
  3767. if (!*from_e || !*to_e)
  3768. reset_locale();
  3769. if (!*from_e) {
  3770. tmp_from_e = rktio_locale_encoding(scheme_rktio);
  3771. from_e = tmp_from_e;
  3772. }
  3773. if (!*to_e) {
  3774. tmp_to_e = rktio_locale_encoding(scheme_rktio);
  3775. to_e = tmp_to_e;
  3776. }
  3777. cd = rktio_converter_open(scheme_rktio, to_e, from_e);
  3778. if (tmp_from_e) free(tmp_from_e);
  3779. if (tmp_to_e) free(tmp_to_e);
  3780. if (!cd)
  3781. return scheme_false;
  3782. kind = mzICONV_KIND;
  3783. permissive = 0;
  3784. }
  3785. c = MALLOC_ONE_TAGGED(Scheme_Converter);
  3786. c->so.type = scheme_string_converter_type;
  3787. c->closed = 0;
  3788. c->kind = kind;
  3789. c->permissive = permissive;
  3790. c->cd = cd;
  3791. if (!need_regis)
  3792. mref = NULL;
  3793. else
  3794. mref = scheme_add_managed(NULL,
  3795. (Scheme_Object *)c,
  3796. close_converter,
  3797. NULL, 1);
  3798. c->mref = mref;
  3799. return (Scheme_Object *)c;
  3800. }
  3801. static Scheme_Object *byte_string_open_converter(int argc, Scheme_Object **argv)
  3802. {
  3803. Scheme_Object *s1, *s2;
  3804. char *from_e, *to_e;
  3805. if (!SCHEME_CHAR_STRINGP(argv[0]))
  3806. scheme_wrong_contract("bytes-open-converter", "bytes?", 0, argc, argv);
  3807. if (!SCHEME_CHAR_STRINGP(argv[1]))
  3808. scheme_wrong_contract("bytes-open-converter", "bytes?", 1, argc, argv);
  3809. scheme_custodian_check_available(NULL, "bytes-open-converter", "converter");
  3810. s1 = scheme_char_string_to_byte_string(argv[0]);
  3811. s2 = scheme_char_string_to_byte_string(argv[1]);
  3812. if (scheme_byte_string_has_null(s1))
  3813. return scheme_false;
  3814. if (scheme_byte_string_has_null(s2))
  3815. return scheme_false;
  3816. from_e = SCHEME_BYTE_STR_VAL(s1);
  3817. to_e = SCHEME_BYTE_STR_VAL(s2);
  3818. return scheme_open_converter(from_e, to_e);
  3819. }
  3820. static Scheme_Object *convert_one(const char *who, int opos, int argc, Scheme_Object *argv[])
  3821. {
  3822. char *r, *instr;
  3823. int status;
  3824. intptr_t amt_read, amt_wrote;
  3825. intptr_t istart, ifinish, ostart, ofinish;
  3826. Scheme_Object *a[3], *status_sym;
  3827. Scheme_Converter *c;
  3828. if (!SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_string_converter_type))
  3829. scheme_wrong_contract(who, "bytes-converter?", 0, argc, argv);
  3830. if (opos > 1) {
  3831. if (!SCHEME_BYTE_STRINGP(argv[1]))
  3832. scheme_wrong_contract(who, "bytes?", 1, argc, argv);
  3833. scheme_get_substring_indices(who, argv[1], argc, argv, 2, 3, &istart, &ifinish);
  3834. } else {
  3835. istart = 0;
  3836. ifinish = 0;
  3837. }
  3838. if (argc > opos) {
  3839. if (SCHEME_TRUEP(argv[opos])) {
  3840. if (!SCHEME_MUTABLE_BYTE_STRINGP(argv[opos]))
  3841. scheme_wrong_contract(who, "(and/c bytes? (not/c immutable?))", opos, argc, argv);
  3842. r = SCHEME_BYTE_STR_VAL(argv[opos]);
  3843. scheme_get_substring_indices(who, argv[opos], argc, argv, opos + 1, opos + 2, &ostart, &ofinish);
  3844. } else {
  3845. int ip;
  3846. r = NULL;
  3847. for (ip = opos + 1; ip <= opos + 2; ip++) {
  3848. if (argc > ip) {
  3849. int ok = 0;
  3850. if (SCHEME_INTP(argv[ip]))
  3851. ok = SCHEME_INT_VAL(argv[ip]) >= 0;
  3852. else if (SCHEME_BIGNUMP(argv[ip]))
  3853. ok = SCHEME_BIGPOS(argv[ip]);
  3854. else if ((ip == opos + 2) && SCHEME_FALSEP(argv[ip]))
  3855. ok = 1;
  3856. if (!ok)
  3857. scheme_wrong_contract(who,
  3858. ((ip == opos + 2)
  3859. ? "(or/c exact-nonnegative-integer? #f)"
  3860. : "exact-nonnegative-integer?"),
  3861. ip, argc, argv);
  3862. }
  3863. }
  3864. if ((argc > opos + 2) && SCHEME_TRUEP(argv[opos + 2])) {
  3865. Scheme_Object *delta;
  3866. if (scheme_bin_lt(argv[opos + 2], argv[opos + 1])) {
  3867. scheme_contract_error(who,
  3868. "ending index is less than the starting index",
  3869. "staring index", 1, argv[opos + 1],
  3870. "ending index", 1, argv[opos + 2],
  3871. NULL);
  3872. }
  3873. delta = scheme_bin_minus(argv[opos + 2], argv[opos + 1]);
  3874. if (SCHEME_BIGNUMP(delta))
  3875. ofinish = -1;
  3876. else
  3877. ofinish = SCHEME_INT_VAL(delta);
  3878. ostart = 0;
  3879. } else {
  3880. ostart = 0;
  3881. ofinish = -1;
  3882. }
  3883. }
  3884. } else {
  3885. r = NULL;
  3886. ostart = 0;
  3887. ofinish = -1;
  3888. }
  3889. c = (Scheme_Converter *)argv[0];
  3890. if (c->closed)
  3891. scheme_contract_error(who, "converter is closed",
  3892. "converter", 1, argv[0],
  3893. NULL);
  3894. instr = ((opos > 1) ? SCHEME_BYTE_STR_VAL(argv[1]) : NULL);
  3895. if (c->kind == mzUTF16_TO_UTF8_KIND) {
  3896. if (istart & 0x1) {
  3897. /* Copy to word-align */
  3898. char *c2;
  3899. c2 = (char *)scheme_malloc_atomic(ifinish - istart);
  3900. memcpy(c2, instr XFORM_OK_PLUS istart, ifinish - istart);
  3901. ifinish = ifinish - istart;
  3902. istart = 0;
  3903. instr = c2;
  3904. }
  3905. status = utf8_encode_x((const unsigned int *)instr, istart >> 1, ifinish >> 1,
  3906. (unsigned char *)r, ostart, ofinish,
  3907. &amt_read, &amt_wrote, 1);
  3908. amt_read -= (istart >> 1);
  3909. if (amt_read) {
  3910. if (!r) {
  3911. /* Need to allocate, then do it again: */
  3912. r = (char *)scheme_malloc_atomic(amt_wrote + 1);
  3913. utf8_encode_x((const unsigned int *)instr, istart >> 1, ifinish >> 1,
  3914. (unsigned char *)r, ostart, ofinish,
  3915. NULL, NULL, 1);
  3916. r[amt_wrote] = 0;
  3917. }
  3918. amt_read <<= 1;
  3919. }
  3920. /* We might get a -1 result because the input has an odd number of
  3921. bytes, and 2nd+next-to-last bytes form an unpaired
  3922. surrogate. In that case, the transformer normally needs one
  3923. more byte: Windows is little-endian, so we need the byte to
  3924. tell whether the surrogate is paired, and for all other
  3925. platforms (where we assume that surrogates are paired), we need
  3926. the byte to generate output. Technically, on a big-endian
  3927. non-Windows machine, we could generate the first byte of UTF-8
  3928. output and keep the byte as state, but we don't. */
  3929. if (status != -1) {
  3930. if (amt_read < ((ifinish - istart) & ~0x1)) {
  3931. /* Must have run out of output space */
  3932. status = 1;
  3933. } else {
  3934. /* Read all of input --- but it wasn't really all if there
  3935. was an odd number of bytes. */
  3936. if ((ifinish - istart) & 0x1)
  3937. status = -1;
  3938. else
  3939. status = 0;
  3940. }
  3941. }
  3942. } else if (c->kind != mzICONV_KIND) {
  3943. /* UTF-8 -> UTF-{8,16} "identity" converter, but maybe permissive */
  3944. if (instr) {
  3945. intptr_t _ostart, _ofinish;
  3946. int utf16;
  3947. if (c->kind == mzUTF8_TO_UTF16_KIND) {
  3948. _ostart = ostart;
  3949. _ofinish = ofinish;
  3950. if (_ostart & 0x1)
  3951. _ostart++;
  3952. _ostart >>= 1;
  3953. if (_ofinish > 0)
  3954. _ofinish >>= 1;
  3955. utf16 = 1;
  3956. } else {
  3957. _ostart = ostart;
  3958. _ofinish = ofinish;
  3959. utf16 = 0;
  3960. }
  3961. status = utf8_decode_x((unsigned char *)instr, istart, ifinish,
  3962. (unsigned int *)r, _ostart, _ofinish,
  3963. &amt_read, &amt_wrote,
  3964. 1, utf16, NULL, 1, c->permissive);
  3965. if (utf16) {
  3966. _ostart <<= 1;
  3967. amt_wrote <<= 1;
  3968. if ((ostart & 0x1) && (amt_wrote > _ostart)) {
  3969. /* Shift down one byte: */
  3970. memmove(r XFORM_OK_PLUS ostart, r XFORM_OK_PLUS _ostart, amt_wrote - _ostart);
  3971. }
  3972. }
  3973. amt_read -= istart;
  3974. amt_wrote -= _ostart;
  3975. if (status == -3) {
  3976. /* r is not NULL; ran out of room */
  3977. status = 1;
  3978. } else {
  3979. if (amt_wrote) {
  3980. if (!r) {
  3981. /* Need to allocate, then do it again: */
  3982. r = (char *)scheme_malloc_atomic(amt_wrote + 1);
  3983. utf8_decode_x((unsigned char *)instr, istart, ifinish,
  3984. (unsigned int *)r, ostart, _ofinish,
  3985. NULL, NULL,
  3986. 1, utf16, NULL, 1, c->permissive);
  3987. r[amt_wrote] = 0;
  3988. }
  3989. } else if (!r)
  3990. r = "";
  3991. if (status > 0)
  3992. status = 0;
  3993. }
  3994. } else {
  3995. r = "";
  3996. status = 0;
  3997. amt_read = 0;
  3998. amt_wrote = 0;
  3999. }
  4000. } else {
  4001. r = do_convert(c->cd, NULL, NULL, 0,
  4002. instr, istart, ifinish-istart,
  4003. r, ostart, ofinish-ostart,
  4004. !r, /* grow? */
  4005. 0,
  4006. (r ? 0 : 1), /* terminator */
  4007. &amt_read, &amt_wrote,
  4008. &status);
  4009. }
  4010. if (status == 0) {
  4011. /* Converted all input without error */
  4012. status_sym = complete_symbol;
  4013. } else if (status == 1) {
  4014. /* Filled output, more input ready */
  4015. status_sym = continues_symbol;
  4016. } else if (status == -1) {
  4017. /* Input ends in the middle of an encoding */
  4018. status_sym = aborts_symbol;
  4019. } else {
  4020. /* Assert: status == -2 */
  4021. /* Input has error (that won't be fixed by
  4022. adding more characters */
  4023. status_sym = error_symbol;
  4024. }
  4025. if (argc <= opos) {
  4026. a[0] = scheme_make_sized_byte_string(r, amt_wrote, 0);
  4027. } else {
  4028. a[0] = scheme_make_integer(amt_wrote);
  4029. }
  4030. if (opos > 1) {
  4031. a[1] = scheme_make_integer(amt_read);
  4032. a[2] = status_sym;
  4033. return scheme_values(3, a);
  4034. } else {
  4035. a[1] = status_sym;
  4036. return scheme_values(2, a);
  4037. }
  4038. }
  4039. static Scheme_Object *byte_string_convert(int argc, Scheme_Object *argv[])
  4040. {
  4041. return convert_one("bytes-convert", 4, argc, argv);
  4042. }
  4043. static Scheme_Object *byte_string_convert_end(int argc, Scheme_Object *argv[])
  4044. {
  4045. return convert_one("bytes-convert-end", 1, argc, argv);
  4046. }
  4047. void scheme_close_converter(Scheme_Object *conv)
  4048. {
  4049. close_converter(conv, NULL);
  4050. }
  4051. static Scheme_Object *byte_string_close_converter(int argc, Scheme_Object **argv)
  4052. {
  4053. if (!SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_string_converter_type))
  4054. scheme_wrong_contract("bytes-close-converter", "bytes-converter?", 0, argc, argv);
  4055. scheme_close_converter(argv[0]);
  4056. return scheme_void;
  4057. }
  4058. static Scheme_Object *
  4059. byte_converter_p(int argc, Scheme_Object *argv[])
  4060. {
  4061. return (SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_string_converter_type)
  4062. ? scheme_true
  4063. : scheme_false);
  4064. }
  4065. /**********************************************************************/
  4066. /* utf8 converter */
  4067. /**********************************************************************/
  4068. static intptr_t utf8_decode_x(const unsigned char *s, intptr_t start, intptr_t end,
  4069. unsigned int *us, intptr_t dstart, intptr_t dend,
  4070. intptr_t *ipos, intptr_t *jpos,
  4071. char compact, char utf16, int *_state,
  4072. int might_continue, int permissive)
  4073. /* Results:
  4074. non-negative => translation complete, = number of produced chars
  4075. -1 => input ended in middle of encoding (only if might_continue)
  4076. -2 => encoding error (only if permissive is 0)
  4077. -3 => not enough output room
  4078. ipos & jpos are filled with ending positions (between [d]start
  4079. and [d]end) before return, unless they are NULL.
  4080. compact => UTF-8 to UTF-8 or UTF-16 --- the latter if utf16
  4081. for Windows for utf16, decode extended UTF-8 that allows surrogates
  4082. _state provides initial state and is filled with ending state;
  4083. when it's not NULL, the us must be NULL
  4084. might_continue => allows -1 result without consuming characters
  4085. permissive is non-zero => use permissive as value for bad byte
  4086. sequences. When generating UTF-8, this must be an ASCII character
  4087. or U+FFFD. */
  4088. {
  4089. intptr_t i, j, oki;
  4090. int failmode = -3, state;
  4091. int init_doki;
  4092. int nextbits, v;
  4093. unsigned int sc;
  4094. # ifdef WINDOWS_UNICODE_SUPPORT
  4095. int pending_surrogate = 0;
  4096. # endif
  4097. if (_state) {
  4098. state = (*_state) & 0x7;
  4099. init_doki = (((*_state) >> 3) & 0x7);
  4100. nextbits = ((((*_state) >> 6) & 0xF) << 2);
  4101. /* Need v to detect 0xD800 through 0xDFFF
  4102. Note that we have 22 bits to work with, which is
  4103. is enough to detect > 0x10FFFF */
  4104. v = ((*_state) >> 10);
  4105. } else {
  4106. state = 0;
  4107. init_doki = 0;
  4108. nextbits = 0;
  4109. v = 0;
  4110. }
  4111. /* In non-permissive mode, a negative result means ill-formed input.
  4112. Permissive mode accepts anything and tries to convert it. In
  4113. that case, the strategy for illegal sequences is to convert
  4114. anything bad to the given "permissive" value. */
  4115. if (end < 0)
  4116. end = strlen((char *)s);
  4117. if (dend < 0)
  4118. dend = 0x7FFFFFFF;
  4119. # define ENCFAIL i = oki; failmode = -2; break
  4120. oki = start;
  4121. j = dstart;
  4122. i = start;
  4123. if (j < dend) {
  4124. while (i < end) {
  4125. sc = s[i];
  4126. if (sc < 0x80) {
  4127. if (state) {
  4128. /* In a sequence, but didn't continue */
  4129. state = 0;
  4130. nextbits = 0;
  4131. if (permissive) {
  4132. v = permissive;
  4133. i = oki;
  4134. j += init_doki;
  4135. } else {
  4136. ENCFAIL;
  4137. }
  4138. } else {
  4139. v = sc;
  4140. }
  4141. } else if ((sc & 0xC0) == 0x80) {
  4142. /* Continues a sequence ... */
  4143. if (state) {
  4144. /* ... and we're in one ... */
  4145. if (!nextbits || (sc & nextbits)) {
  4146. /* and we have required bits. */
  4147. v = (v << 6) + (sc & 0x3F);
  4148. nextbits = 0;
  4149. --state;
  4150. if (state) {
  4151. i++;
  4152. continue;
  4153. }
  4154. /* We finished. One last check: */
  4155. if ((((v >= 0xD800) && (v <= 0xDFFF))
  4156. || (v > 0x10FFFF))
  4157. # ifdef WINDOWS_UNICODE_SUPPORT
  4158. && (!utf16
  4159. /* If UTF-16 for Windows, just apply upper-limit check */
  4160. || (v > 0x10FFFF))
  4161. # endif
  4162. ) {
  4163. /* UTF-16 surrogates or other illegal code units */
  4164. if (permissive) {
  4165. v = permissive;
  4166. j += init_doki;
  4167. i = oki;
  4168. } else {
  4169. ENCFAIL;
  4170. }
  4171. }
  4172. } else {
  4173. /* ... but we're missing required bits. */
  4174. state = 0;
  4175. nextbits = 0;
  4176. if (permissive) {
  4177. v = permissive;
  4178. j += init_doki;
  4179. i = oki;
  4180. } else {
  4181. ENCFAIL;
  4182. }
  4183. }
  4184. } else {
  4185. /* ... but we're not in one */
  4186. if (permissive) {
  4187. v = permissive;
  4188. } else {
  4189. ENCFAIL;
  4190. }
  4191. }
  4192. } else if (state) {
  4193. /* bad: already in a sequence */
  4194. state = 0;
  4195. if (permissive) {
  4196. v = permissive;
  4197. i = oki;
  4198. j += init_doki;
  4199. } else {
  4200. ENCFAIL;
  4201. }
  4202. } else {
  4203. if ((sc & 0xE0) == 0xC0) {
  4204. if (sc & 0x1E) {
  4205. state = 1;
  4206. v = (sc & 0x1F);
  4207. i++;
  4208. continue;
  4209. }
  4210. /* else too small */
  4211. } else if ((sc & 0xF0) == 0xE0) {
  4212. state = 2;
  4213. v = (sc & 0xF);
  4214. if (!v)
  4215. nextbits = 0x20;
  4216. i++;
  4217. continue;
  4218. } else if ((sc & 0xF8) == 0xF0) {
  4219. v = (sc & 0x7);
  4220. if (v <= 4) {
  4221. state = 3;
  4222. if (!v)
  4223. nextbits = 0x30;
  4224. i++;
  4225. continue;
  4226. }
  4227. /* Else will be larger than 0x10FFFF, so fail */
  4228. }
  4229. /* Too small, or 0xFF or 0xFe, or start of a 5- or 6-byte sequence */
  4230. if (permissive) {
  4231. v = permissive;
  4232. } else {
  4233. ENCFAIL;
  4234. }
  4235. }
  4236. /* If we get here, we're supposed to output v */
  4237. if (compact) {
  4238. if (utf16) {
  4239. if (v > 0xFFFF) {
  4240. # ifdef WINDOWS_UNICODE_SUPPORT
  4241. if (pending_surrogate) {
  4242. if (us)
  4243. ((unsigned short *)us)[j] = pending_surrogate;
  4244. j++; /* Accept previously written unpaired surrogate */
  4245. pending_surrogate = 0;
  4246. }
  4247. # endif
  4248. if (j + 1 >= dend)
  4249. break;
  4250. if (us) {
  4251. v -= 0x10000;
  4252. ((unsigned short *)us)[j] = 0xD800 | ((v >> 10) & 0x3FF);
  4253. ((unsigned short *)us)[j+1] = 0xDC00 | (v & 0x3FF);
  4254. }
  4255. j++;
  4256. } else {
  4257. # ifdef WINDOWS_UNICODE_SUPPORT
  4258. /* We allow a surrogate by itself, but don't allow
  4259. a 0xDC00 after a 0xD800, otherwise multiple encodings can
  4260. map to the same thing. */
  4261. if ((v >= 0xD800) && (v <= 0xDFFF)) {
  4262. if (pending_surrogate && ((v & 0xDC00) == 0xDC00)) {
  4263. /* This looks like a surrogate pair, so disallow it. */
  4264. if (permissive) {
  4265. /* We need to fill in 6 permissive substitutions,
  4266. one for each input byte. If we can't put all 6,
  4267. then don't use any input. */
  4268. if (j + 5 >= dend) {
  4269. break;
  4270. } else {
  4271. int p;
  4272. if (us) {
  4273. for (p = 0; p < 5; p++) {
  4274. if (j + p >= dend)
  4275. break;
  4276. ((unsigned short *)us)[j+p] = permissive;
  4277. }
  4278. }
  4279. j += 5;
  4280. v = permissive;
  4281. }
  4282. } else {
  4283. ENCFAIL;
  4284. }
  4285. pending_surrogate = 0;
  4286. } else {
  4287. if (pending_surrogate) {
  4288. if (us)
  4289. ((unsigned short *)us)[j] = pending_surrogate;
  4290. j++; /* Accept previousy written unpaired surrogate */
  4291. pending_surrogate = 0;
  4292. if (j >= dend)
  4293. break;
  4294. }
  4295. if ((v & 0xDC00) == 0xD800)
  4296. pending_surrogate = v;
  4297. else
  4298. pending_surrogate = 0;
  4299. }
  4300. } else {
  4301. if (pending_surrogate) {
  4302. if (us)
  4303. ((unsigned short *)us)[j] = pending_surrogate;
  4304. j++; /* Accept previousy written unpaired surrogate */
  4305. pending_surrogate = 0;
  4306. if (j >= dend)
  4307. break;
  4308. }
  4309. }
  4310. if (pending_surrogate)
  4311. --j; /* don't accept unpaired surrogate, yet */
  4312. else if (us)
  4313. ((unsigned short *)us)[j] = v;
  4314. # else
  4315. if (us)
  4316. ((unsigned short *)us)[j] = v;
  4317. # endif
  4318. }
  4319. } else {
  4320. intptr_t delta;
  4321. delta = (i - oki);
  4322. if (delta) {
  4323. if (j + delta + 1 < dend) {
  4324. if (us)
  4325. memcpy(((char *)us) + j, s + oki, delta + 1);
  4326. j += delta;
  4327. } else
  4328. break;
  4329. } else if (v == 0xFFFD) {
  4330. if (j + 3 < dend) {
  4331. if (us) {
  4332. ((unsigned char *)us)[j] = 0xEF;
  4333. ((unsigned char *)us)[j+1] = 0xBF;
  4334. ((unsigned char *)us)[j+2] = 0xBD;
  4335. }
  4336. j += 2;
  4337. } else
  4338. break;
  4339. } else if (us) {
  4340. ((unsigned char *)us)[j] = v;
  4341. }
  4342. }
  4343. } else if (us) {
  4344. us[j] = v;
  4345. }
  4346. j++;
  4347. i++;
  4348. oki = i;
  4349. init_doki = 0;
  4350. if (j >= dend)
  4351. break;
  4352. }
  4353. }
  4354. if (_state) {
  4355. if (!state)
  4356. *_state = 0;
  4357. else
  4358. *_state = (state
  4359. | (((end - oki) + init_doki) << 3)
  4360. | ((nextbits >> 2) << 6)
  4361. | (v << 10));
  4362. } else if (state) {
  4363. if (might_continue || !permissive) {
  4364. failmode = -1;
  4365. i = end - 1; /* to ensure that failmode is returned */
  4366. } else if (permissive) {
  4367. # ifdef WINDOWS_UNICODE_SUPPORT
  4368. if (pending_surrogate) {
  4369. /* Unpaired surrogate before permissive replacements */
  4370. if (utf16 && (j < dend)) {
  4371. if (us)
  4372. ((unsigned short *)us)[j] = pending_surrogate;
  4373. j++;
  4374. }
  4375. pending_surrogate = 0;
  4376. }
  4377. #endif
  4378. for (i = oki; i < end; i++) {
  4379. if (j < dend) {
  4380. if (us) {
  4381. if (compact) {
  4382. if (utf16)
  4383. ((unsigned short *)us)[j] = permissive;
  4384. else
  4385. ((unsigned char *)us)[j] = permissive;
  4386. } else
  4387. us[j] = permissive;
  4388. }
  4389. j++;
  4390. } else
  4391. break;
  4392. }
  4393. oki = i;
  4394. }
  4395. }
  4396. # ifdef WINDOWS_UNICODE_SUPPORT
  4397. if (pending_surrogate) {
  4398. if (!might_continue) {
  4399. /* Accept unpaired surrogate at end of input */
  4400. if (j < dend) {
  4401. if (us)
  4402. ((unsigned short *)us)[j] = pending_surrogate;
  4403. j++;
  4404. }
  4405. } else {
  4406. oki -= 3;
  4407. }
  4408. }
  4409. #endif
  4410. if (ipos)
  4411. *ipos = oki;
  4412. if (jpos)
  4413. *jpos = j;
  4414. if (i < end)
  4415. return failmode;
  4416. # ifdef WINDOWS_UNICODE_SUPPORT
  4417. if (pending_surrogate) {
  4418. /* input must have ended right after surrogate */
  4419. return -1;
  4420. }
  4421. #endif
  4422. return j - dstart;
  4423. }
  4424. intptr_t scheme_utf8_decode(const unsigned char *s, intptr_t start, intptr_t end,
  4425. unsigned int *us, intptr_t dstart, intptr_t dend,
  4426. intptr_t *ipos, char utf16, int permissive)
  4427. {
  4428. return utf8_decode_x(s, start, end, us, dstart, dend,
  4429. ipos, NULL, utf16, utf16, NULL, 0, permissive);
  4430. }
  4431. intptr_t scheme_utf8_decode_offset_prefix(const unsigned char *s, intptr_t start, intptr_t end,
  4432. unsigned int *us, intptr_t dstart, intptr_t dend,
  4433. intptr_t *ipos, char utf16, int permissive)
  4434. {
  4435. return utf8_decode_x(s, start, end, us, dstart, dend,
  4436. ipos, NULL, utf16, utf16, NULL, 1, permissive);
  4437. }
  4438. intptr_t scheme_utf8_decode_as_prefix(const unsigned char *s, intptr_t start, intptr_t end,
  4439. unsigned int *us, intptr_t dstart, intptr_t dend,
  4440. intptr_t *ipos, char utf16, int permissive)
  4441. /* Always returns number of read characters, not error codes. */
  4442. {
  4443. intptr_t opos;
  4444. utf8_decode_x(s, start, end, us, dstart, dend,
  4445. ipos, &opos, utf16, utf16, NULL, 1, permissive);
  4446. return opos - dstart;
  4447. }
  4448. intptr_t scheme_utf8_decode_all(const unsigned char *s, intptr_t len, unsigned int *us, int permissive)
  4449. {
  4450. return utf8_decode_x(s, 0, len, us, 0, -1, NULL, NULL, 0, 0, NULL, 0, permissive);
  4451. }
  4452. intptr_t scheme_utf8_decode_prefix(const unsigned char *s, intptr_t len, unsigned int *us, int permissive)
  4453. /* us != NULL */
  4454. {
  4455. {
  4456. /* Try fast path (all ASCII) */
  4457. intptr_t i;
  4458. for (i = 0; i < len; i++) {
  4459. if (s[i] < 128)
  4460. us[i] = s[i];
  4461. else
  4462. break;
  4463. }
  4464. if (i == len)
  4465. return len;
  4466. }
  4467. return utf8_decode_x(s, 0, len, us, 0, -1, NULL, NULL, 0, 0, NULL, 1, permissive);
  4468. }
  4469. mzchar *scheme_utf8_decode_to_buffer_len(const unsigned char *s, intptr_t len,
  4470. mzchar *buf, intptr_t blen, intptr_t *_ulen)
  4471. {
  4472. intptr_t ulen;
  4473. ulen = utf8_decode_x(s, 0, len, NULL, 0, -1,
  4474. NULL, NULL, 0, 0,
  4475. NULL, 0, 0);
  4476. if (ulen < 0)
  4477. return NULL;
  4478. if (ulen + 1 > blen) {
  4479. buf = (mzchar *)scheme_malloc_atomic((ulen + 1) * sizeof(mzchar));
  4480. }
  4481. utf8_decode_x(s, 0, len, buf, 0, -1,
  4482. NULL, NULL, 0, 0,
  4483. NULL, 0, 0);
  4484. buf[ulen] = 0;
  4485. *_ulen = ulen;
  4486. return buf;
  4487. }
  4488. mzchar *scheme_utf8_decode_to_buffer(const unsigned char *s, intptr_t len,
  4489. mzchar *buf, intptr_t blen)
  4490. {
  4491. intptr_t ulen;
  4492. return scheme_utf8_decode_to_buffer_len(s, len, buf, blen, &ulen);
  4493. }
  4494. intptr_t scheme_utf8_decode_count(const unsigned char *s, intptr_t start, intptr_t end,
  4495. int *_state, int might_continue, int permissive)
  4496. {
  4497. intptr_t pos = 0;
  4498. if (!_state || !*_state) {
  4499. /* Try fast path (all ASCII): */
  4500. intptr_t i;
  4501. for (i = start; i < end; i++) {
  4502. if (s[i] > 127)
  4503. break;
  4504. }
  4505. if (i == end)
  4506. return end - start;
  4507. }
  4508. utf8_decode_x(s, start, end,
  4509. NULL, 0, -1,
  4510. NULL, &pos,
  4511. 0, 0, _state,
  4512. might_continue, permissive);
  4513. return pos;
  4514. }
  4515. static intptr_t utf8_encode_x(const unsigned int *us, intptr_t start, intptr_t end,
  4516. unsigned char *s, intptr_t dstart, intptr_t dend,
  4517. intptr_t *_ipos, intptr_t *_opos, char utf16)
  4518. /* Results:
  4519. -1 => input ended in the middle of an encoding - only when utf16 and _opos
  4520. non-negative => reports number of bytes/code-units produced */
  4521. {
  4522. intptr_t i, j, done = start;
  4523. if (dend < 0)
  4524. dend = 0x7FFFFFFF;
  4525. if (!s) {
  4526. unsigned int wc;
  4527. j = 0;
  4528. for (i = start; i < end; i++) {
  4529. if (utf16) {
  4530. wc = ((unsigned short *)us)[i];
  4531. if ((wc & 0xF800) == 0xD800) {
  4532. /* Unparse surrogates. We assume that the surrogates are
  4533. well formed, unless this is Windows or if we're at the
  4534. end and _opos is 0. The well-formedness assumption was
  4535. probably not a good idea, but note that it's explicitly
  4536. documented to behave that way. */
  4537. # ifdef WINDOWS_UNICODE_SUPPORT
  4538. # define UNPAIRED_MASK 0xFC00
  4539. # else
  4540. # define UNPAIRED_MASK 0xF800
  4541. # endif
  4542. if (((i + 1) == end) && ((wc & UNPAIRED_MASK) == 0xD800) && _opos) {
  4543. /* Ended in the middle of a surrogate pair */
  4544. *_opos = j;
  4545. if (_ipos)
  4546. *_ipos = i;
  4547. return -1;
  4548. }
  4549. # ifdef WINDOWS_UNICODE_SUPPORT
  4550. if ((wc & 0xFC00) != 0xD800) {
  4551. /* Count as one */
  4552. } else if ((i + 1 >= end)
  4553. || (((((unsigned short *)us)[i+1]) & 0xFC00) != 0xDC00)) {
  4554. } else
  4555. # endif
  4556. {
  4557. i++;
  4558. wc = ((wc & 0x3FF) << 10) + ((((unsigned short *)us)[i]) & 0x3FF);
  4559. wc += 0x10000;
  4560. }
  4561. }
  4562. } else {
  4563. wc = us[i];
  4564. }
  4565. if (wc < 0x80) {
  4566. j += 1;
  4567. } else if (wc < 0x800) {
  4568. j += 2;
  4569. } else if (wc < 0x10000) {
  4570. j += 3;
  4571. } else if (wc < 0x200000) {
  4572. j += 4;
  4573. } else if (wc < 0x4000000) {
  4574. j += 5;
  4575. } else {
  4576. j += 6;
  4577. }
  4578. }
  4579. if (_ipos)
  4580. *_ipos = i;
  4581. if (_opos)
  4582. *_opos = j + dstart;
  4583. return j;
  4584. } else {
  4585. unsigned int wc;
  4586. j = dstart;
  4587. for (i = start; i < end; i++) {
  4588. if (utf16) {
  4589. wc = ((unsigned short *)us)[i];
  4590. if ((wc & 0xF800) == 0xD800) {
  4591. /* Unparse surrogates. We assume that the surrogates are
  4592. well formed on non-Windows platforms, but when _opos,
  4593. we detect ending in the middle of an surrogate pair. */
  4594. if (((i + 1) == end) && ((wc & UNPAIRED_MASK) == 0xD800) && _opos) {
  4595. /* Ended in the middle of a surrogate pair */
  4596. *_opos = j;
  4597. if (_ipos)
  4598. *_ipos = i;
  4599. return -1;
  4600. }
  4601. # ifdef WINDOWS_UNICODE_SUPPORT
  4602. if ((wc & 0xFC00) != 0xD800) {
  4603. /* Let the misplaced surrogate through */
  4604. } else if ((i + 1 >= end)
  4605. || (((((unsigned short *)us)[i+1]) & 0xFC00) != 0xDC00)) {
  4606. /* Let the misplaced surrogate through */
  4607. } else
  4608. # endif
  4609. {
  4610. i++;
  4611. wc = ((wc & 0x3FF) << 10) + ((((unsigned short *)us)[i]) & 0x3FF);
  4612. wc += 0x10000;
  4613. }
  4614. }
  4615. } else {
  4616. wc = us[i];
  4617. }
  4618. if (wc < 0x80) {
  4619. if (j + 1 > dend)
  4620. break;
  4621. s[j++] = wc;
  4622. } else if (wc < 0x800) {
  4623. if (j + 2 > dend)
  4624. break;
  4625. s[j++] = 0xC0 | ((wc & 0x7C0) >> 6);
  4626. s[j++] = 0x80 | (wc & 0x3F);
  4627. } else if (wc < 0x10000) {
  4628. if (j + 3 > dend)
  4629. break;
  4630. s[j++] = 0xE0 | ((wc & 0xF000) >> 12);
  4631. s[j++] = 0x80 | ((wc & 0x0FC0) >> 6);
  4632. s[j++] = 0x80 | (wc & 0x3F);
  4633. } else if (wc < 0x200000) {
  4634. if (j + 4 > dend)
  4635. break;
  4636. s[j++] = 0xF0 | ((wc & 0x1C0000) >> 18);
  4637. s[j++] = 0x80 | ((wc & 0x03F000) >> 12);
  4638. s[j++] = 0x80 | ((wc & 0x000FC0) >> 6);
  4639. s[j++] = 0x80 | (wc & 0x3F);
  4640. } else if (wc < 0x4000000) {
  4641. if (j + 5 > dend)
  4642. break;
  4643. s[j++] = 0xF8 | ((wc & 0x3000000) >> 24);
  4644. s[j++] = 0x80 | ((wc & 0x0FC0000) >> 18);
  4645. s[j++] = 0x80 | ((wc & 0x003F000) >> 12);
  4646. s[j++] = 0x80 | ((wc & 0x0000FC0) >> 6);
  4647. s[j++] = 0x80 | (wc & 0x3F);
  4648. } else {
  4649. if (j + 6 > dend)
  4650. break;
  4651. s[j++] = 0xFC | ((wc & 0x40000000) >> 30);
  4652. s[j++] = 0x80 | ((wc & 0x3F000000) >> 24);
  4653. s[j++] = 0x80 | ((wc & 0x00FC0000) >> 18);
  4654. s[j++] = 0x80 | ((wc & 0x0003F000) >> 12);
  4655. s[j++] = 0x80 | ((wc & 0x00000FC0) >> 6);
  4656. s[j++] = 0x80 | (wc & 0x3F);
  4657. }
  4658. done = i;
  4659. }
  4660. if (_ipos)
  4661. *_ipos = done;
  4662. if (_opos)
  4663. *_opos = j;
  4664. return j - dstart;
  4665. }
  4666. }
  4667. intptr_t scheme_utf8_encode(const unsigned int *us, intptr_t start, intptr_t end,
  4668. unsigned char *s, intptr_t dstart,
  4669. char utf16)
  4670. {
  4671. return utf8_encode_x(us, start, end,
  4672. s, dstart, -1,
  4673. NULL, NULL, utf16);
  4674. }
  4675. intptr_t scheme_utf8_encode_all(const unsigned int *us, intptr_t len, unsigned char *s)
  4676. {
  4677. return utf8_encode_x(us, 0, len, s, 0, -1, NULL, NULL, 0 /* utf16 */);
  4678. }
  4679. char *scheme_utf8_encode_to_buffer_len(const mzchar *s, intptr_t len,
  4680. char *buf, intptr_t blen,
  4681. intptr_t *_slen)
  4682. {
  4683. intptr_t slen;
  4684. /* ASCII with len < blen is a common case: */
  4685. if (len < blen) {
  4686. for (slen = 0; slen < len; slen++) {
  4687. if (s[slen] > 127)
  4688. break;
  4689. else
  4690. buf[slen] = s[slen];
  4691. }
  4692. if (slen == len) {
  4693. buf[slen] = 0;
  4694. *_slen = slen;
  4695. return buf;
  4696. }
  4697. }
  4698. slen = utf8_encode_x(s, 0, len, NULL, 0, -1, NULL, NULL, 0);
  4699. if (slen + 1 > blen) {
  4700. buf = (char *)scheme_malloc_atomic(slen + 1);
  4701. }
  4702. utf8_encode_x(s, 0, len, (unsigned char *)buf, 0, -1, NULL, NULL, 0);
  4703. buf[slen] = 0;
  4704. *_slen = slen;
  4705. return buf;
  4706. }
  4707. char *scheme_utf8_encode_to_buffer(const mzchar *s, intptr_t len,
  4708. char *buf, intptr_t blen)
  4709. {
  4710. intptr_t slen;
  4711. return scheme_utf8_encode_to_buffer_len(s, len, buf, blen, &slen);
  4712. }
  4713. unsigned short *scheme_ucs4_to_utf16(const mzchar *text, intptr_t start, intptr_t end,
  4714. unsigned short *buf, intptr_t bufsize,
  4715. intptr_t *ulen, intptr_t term_size)
  4716. {
  4717. mzchar v;
  4718. intptr_t extra, i, j;
  4719. unsigned short *utf16;
  4720. /* Count characters that fall outside UCS-2: */
  4721. for (i = start, extra = 0; i < end; i++) {
  4722. if (text[i] > 0xFFFF)
  4723. extra++;
  4724. }
  4725. if ((end - start) + extra + term_size < bufsize)
  4726. utf16 = buf;
  4727. else
  4728. utf16 = (unsigned short *)scheme_malloc_atomic(sizeof(unsigned short) * ((end - start) + extra + term_size));
  4729. for (i = start, j = 0; i < end; i++) {
  4730. v = text[i];
  4731. if (v > 0xFFFF) {
  4732. v -= 0x10000;
  4733. utf16[j++] = 0xD800 | ((v >> 10) & 0x3FF);
  4734. utf16[j++] = 0xDC00 | (v & 0x3FF);
  4735. } else
  4736. utf16[j++] = v;
  4737. }
  4738. *ulen = j;
  4739. return utf16;
  4740. }
  4741. mzchar *scheme_utf16_to_ucs4(const unsigned short *text, intptr_t start, intptr_t end,
  4742. mzchar *buf, intptr_t bufsize,
  4743. intptr_t *ulen, intptr_t term_size)
  4744. {
  4745. int wc;
  4746. intptr_t i, j;
  4747. for (i = start, j = 0; i < end; i++) {
  4748. wc = text[i];
  4749. if ((wc & 0xF800) == 0xD800) {
  4750. i++;
  4751. }
  4752. j++;
  4753. }
  4754. if (j + term_size >= bufsize)
  4755. buf = (mzchar *)scheme_malloc_atomic((j + term_size) * sizeof(mzchar));
  4756. for (i = start, j = 0; i < end; i++) {
  4757. wc = text[i];
  4758. if ((wc & 0xF800) == 0xD800) {
  4759. i++;
  4760. wc = ((wc & 0x3FF) << 10) + ((((unsigned short *)text)[i]) & 0x3FF);
  4761. wc += 0x10000;
  4762. }
  4763. buf[j++] = wc;
  4764. }
  4765. *ulen = j;
  4766. return buf;
  4767. }
  4768. /**********************************************************************/
  4769. /* machine type details */
  4770. /**********************************************************************/
  4771. /*************************** Windows **********************************/
  4772. #ifdef DOS_FILE_SYSTEM
  4773. # include <windows.h>
  4774. void machine_details(char *buff)
  4775. {
  4776. OSVERSIONINFO info;
  4777. BOOL hasInfo;
  4778. char *p;
  4779. info.dwOSVersionInfoSize = sizeof(info);
  4780. GetVersionEx(&info);
  4781. hasInfo = FALSE;
  4782. p = info.szCSDVersion;
  4783. while (p < info.szCSDVersion + sizeof(info.szCSDVersion) &&
  4784. *p) {
  4785. if (*p != ' ') {
  4786. hasInfo = TRUE;
  4787. break;
  4788. }
  4789. p = p XFORM_OK_PLUS 1;
  4790. }
  4791. sprintf(buff,"Windows %s %ld.%ld (Build %ld)%s%s",
  4792. (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) ?
  4793. "9x" :
  4794. (info.dwPlatformId == VER_PLATFORM_WIN32_NT) ?
  4795. "NT" : "Unknown platform",
  4796. info.dwMajorVersion,info.dwMinorVersion,
  4797. (info.dwPlatformId == VER_PLATFORM_WIN32_NT) ?
  4798. info.dwBuildNumber :
  4799. info.dwBuildNumber & 0xFFFF,
  4800. hasInfo ? " " : "",hasInfo ? info.szCSDVersion : "");
  4801. }
  4802. #endif
  4803. /***************************** Unix ***********************************/
  4804. #if !defined(DOS_FILE_SYSTEM)
  4805. READ_ONLY static char *uname_locations[] = { "/bin/uname",
  4806. "/usr/bin/uname",
  4807. /* The above should cover everything, but
  4808. just in case... */
  4809. "/sbin/uname",
  4810. "/usr/sbin/uname",
  4811. "/usr/local/bin/uname",
  4812. "/usr/local/uname",
  4813. NULL };
  4814. static int try_subproc(Scheme_Object *subprocess_proc, char *prog)
  4815. {
  4816. Scheme_Object *a[5];
  4817. mz_jmp_buf * volatile savebuf, newbuf;
  4818. savebuf = scheme_current_thread->error_buf;
  4819. scheme_current_thread->error_buf = &newbuf;
  4820. if (!scheme_setjmp(newbuf)) {
  4821. a[0] = scheme_false;
  4822. a[1] = scheme_false;
  4823. a[2] = scheme_false;
  4824. a[3] = scheme_make_locale_string(prog);
  4825. a[4] = scheme_make_locale_string("-a");
  4826. _scheme_apply_multi(subprocess_proc, 5, a);
  4827. scheme_current_thread->error_buf = savebuf;
  4828. return 1;
  4829. } else {
  4830. scheme_clear_escape();
  4831. scheme_current_thread->error_buf = savebuf;
  4832. return 0;
  4833. }
  4834. }
  4835. void machine_details(char *buff)
  4836. {
  4837. Scheme_Object *subprocess_proc;
  4838. int i;
  4839. Scheme_Config *config;
  4840. Scheme_Security_Guard *sg;
  4841. Scheme_Cont_Frame_Data cframe;
  4842. /* Use the root security guard so we can test for and run
  4843. executables. */
  4844. config = scheme_current_config();
  4845. sg = (Scheme_Security_Guard *)scheme_get_param(config, MZCONFIG_SECURITY_GUARD);
  4846. while (sg->parent) { sg = sg->parent; }
  4847. config = scheme_extend_config(config, MZCONFIG_SECURITY_GUARD, (Scheme_Object *)sg);
  4848. scheme_push_continuation_frame(&cframe);
  4849. scheme_install_config(config);
  4850. subprocess_proc = scheme_builtin_value("subprocess");
  4851. for (i = 0; uname_locations[i]; i++) {
  4852. if (scheme_file_exists(uname_locations[i])) {
  4853. /* Try running it. */
  4854. if (try_subproc(subprocess_proc, uname_locations[i])) {
  4855. Scheme_Object *sout, *sin, *serr;
  4856. intptr_t c;
  4857. sout = scheme_current_thread->ku.multiple.array[1];
  4858. sin = scheme_current_thread->ku.multiple.array[2];
  4859. serr = scheme_current_thread->ku.multiple.array[3];
  4860. scheme_close_output_port(sin);
  4861. scheme_close_input_port(serr);
  4862. /* Read result: */
  4863. strcpy(buff, "<unknown machine>");
  4864. c = scheme_get_bytes(sout, 1023, buff, 0);
  4865. buff[c] = 0;
  4866. scheme_close_input_port(sout);
  4867. /* Remove trailing whitespace (especially newlines) */
  4868. while (c && portable_isspace(((unsigned char *)buff)[c - 1])) {
  4869. buff[--c] = 0;
  4870. }
  4871. scheme_pop_continuation_frame(&cframe);
  4872. return;
  4873. }
  4874. }
  4875. }
  4876. strcpy(buff, "<unknown machine>");
  4877. scheme_pop_continuation_frame(&cframe);
  4878. }
  4879. #endif
  4880. /**********************************************************************/
  4881. /* Precise GC */
  4882. /**********************************************************************/
  4883. #ifdef MZ_PRECISE_GC
  4884. START_XFORM_SKIP;
  4885. #include "mzmark_string.inc"
  4886. static void register_traversers(void)
  4887. {
  4888. GC_REG_TRAV(scheme_string_converter_type, mark_string_convert);
  4889. }
  4890. END_XFORM_SKIP;
  4891. #endif