comparison src/file-coding.c @ 440:8de8e3f6228a r21-2-28

Import from CVS: tag r21-2-28
author cvs
date Mon, 13 Aug 2007 11:33:38 +0200
parents 84b14dcb0985
children abe6d1db359e
comparison
equal deleted inserted replaced
439:357dd071b03c 440:8de8e3f6228a
28 28
29 #include "buffer.h" 29 #include "buffer.h"
30 #include "elhash.h" 30 #include "elhash.h"
31 #include "insdel.h" 31 #include "insdel.h"
32 #include "lstream.h" 32 #include "lstream.h"
33 #include "opaque.h"
33 #ifdef MULE 34 #ifdef MULE
34 #include "mule-ccl.h" 35 #include "mule-ccl.h"
35 #include "chartab.h" 36 #include "chartab.h"
36 #endif 37 #endif
37 #include "file-coding.h" 38 #include "file-coding.h"
55 56
56 /* Table of all coding categories in decreasing order of priority. 57 /* Table of all coding categories in decreasing order of priority.
57 This describes a permutation of the possible coding categories. */ 58 This describes a permutation of the possible coding categories. */
58 int coding_category_by_priority[CODING_CATEGORY_LAST + 1]; 59 int coding_category_by_priority[CODING_CATEGORY_LAST + 1];
59 60
61 #ifdef MULE
60 Lisp_Object ucs_to_mule_table[65536]; 62 Lisp_Object ucs_to_mule_table[65536];
63 #endif
61 } *fcd; 64 } *fcd;
62 65
63 static const struct lrecord_description fcd_description_1[] = { 66 static const struct lrecord_description fcd_description_1[] = {
64 { XD_LISP_OBJECT, offsetof(struct file_coding_dump, coding_category_system), CODING_CATEGORY_LAST + 1 }, 67 { XD_LISP_OBJECT_ARRAY, offsetof (struct file_coding_dump, coding_category_system), CODING_CATEGORY_LAST + 1 },
65 { XD_LISP_OBJECT, offsetof(struct file_coding_dump, ucs_to_mule_table), 65536 }, 68 #ifdef MULE
69 { XD_LISP_OBJECT_ARRAY, offsetof (struct file_coding_dump, ucs_to_mule_table), 65536 },
70 #endif
66 { XD_END } 71 { XD_END }
67 }; 72 };
68 73
69 static const struct struct_description fcd_description = { 74 static const struct struct_description fcd_description = {
70 sizeof(struct file_coding_dump), 75 sizeof (struct file_coding_dump),
71 fcd_description_1 76 fcd_description_1
72 }; 77 };
73 78
74 Lisp_Object mule_to_ucs_table; 79 Lisp_Object mule_to_ucs_table;
75 80
90 Lisp_Object Qcharset_g0, Qcharset_g1, Qcharset_g2, Qcharset_g3; 95 Lisp_Object Qcharset_g0, Qcharset_g1, Qcharset_g2, Qcharset_g3;
91 Lisp_Object Qforce_g0_on_output, Qforce_g1_on_output; 96 Lisp_Object Qforce_g0_on_output, Qforce_g1_on_output;
92 Lisp_Object Qforce_g2_on_output, Qforce_g3_on_output; 97 Lisp_Object Qforce_g2_on_output, Qforce_g3_on_output;
93 Lisp_Object Qno_iso6429; 98 Lisp_Object Qno_iso6429;
94 Lisp_Object Qinput_charset_conversion, Qoutput_charset_conversion; 99 Lisp_Object Qinput_charset_conversion, Qoutput_charset_conversion;
95 Lisp_Object Qctext, Qescape_quoted; 100 Lisp_Object Qescape_quoted;
96 Lisp_Object Qshort, Qno_ascii_eol, Qno_ascii_cntl, Qseven, Qlock_shift; 101 Lisp_Object Qshort, Qno_ascii_eol, Qno_ascii_cntl, Qseven, Qlock_shift;
97 #endif 102 #endif
98 Lisp_Object Qencode, Qdecode; 103 Lisp_Object Qencode, Qdecode;
99 104
100 Lisp_Object Vcoding_system_hash_table; 105 Lisp_Object Vcoding_system_hash_table;
245 { 250 {
246 Dynarr_declare (codesys_prop); 251 Dynarr_declare (codesys_prop);
247 } codesys_prop_dynarr; 252 } codesys_prop_dynarr;
248 253
249 static const struct lrecord_description codesys_prop_description_1[] = { 254 static const struct lrecord_description codesys_prop_description_1[] = {
250 { XD_LISP_OBJECT, offsetof(codesys_prop, sym), 1 }, 255 { XD_LISP_OBJECT, offsetof (codesys_prop, sym) },
251 { XD_END } 256 { XD_END }
252 }; 257 };
253 258
254 static const struct struct_description codesys_prop_description = { 259 static const struct struct_description codesys_prop_description = {
255 sizeof(codesys_prop), 260 sizeof (codesys_prop),
256 codesys_prop_description_1 261 codesys_prop_description_1
257 }; 262 };
258 263
259 static const struct lrecord_description codesys_prop_dynarr_description_1[] = { 264 static const struct lrecord_description codesys_prop_dynarr_description_1[] = {
260 XD_DYNARR_DESC(codesys_prop_dynarr, &codesys_prop_description), 265 XD_DYNARR_DESC (codesys_prop_dynarr, &codesys_prop_description),
261 { XD_END } 266 { XD_END }
262 }; 267 };
263 268
264 static const struct struct_description codesys_prop_dynarr_description = { 269 static const struct struct_description codesys_prop_dynarr_description = {
265 sizeof(codesys_prop_dynarr), 270 sizeof (codesys_prop_dynarr),
266 codesys_prop_dynarr_description_1 271 codesys_prop_dynarr_description_1
267 }; 272 };
268 273
269 codesys_prop_dynarr *the_codesys_prop_dynarr; 274 codesys_prop_dynarr *the_codesys_prop_dynarr;
270 275
284 static void print_coding_system (Lisp_Object, Lisp_Object, int); 289 static void print_coding_system (Lisp_Object, Lisp_Object, int);
285 static void finalize_coding_system (void *header, int for_disksave); 290 static void finalize_coding_system (void *header, int for_disksave);
286 291
287 #ifdef MULE 292 #ifdef MULE
288 static const struct lrecord_description ccs_description_1[] = { 293 static const struct lrecord_description ccs_description_1[] = {
289 { XD_LISP_OBJECT, offsetof(charset_conversion_spec, from_charset), 2 }, 294 { XD_LISP_OBJECT, offsetof (charset_conversion_spec, from_charset) },
295 { XD_LISP_OBJECT, offsetof (charset_conversion_spec, to_charset) },
290 { XD_END } 296 { XD_END }
291 }; 297 };
292 298
293 static const struct struct_description ccs_description = { 299 static const struct struct_description ccs_description = {
294 sizeof(charset_conversion_spec), 300 sizeof (charset_conversion_spec),
295 ccs_description_1 301 ccs_description_1
296 }; 302 };
297 303
298 static const struct lrecord_description ccsd_description_1[] = { 304 static const struct lrecord_description ccsd_description_1[] = {
299 XD_DYNARR_DESC(charset_conversion_spec_dynarr, &ccs_description), 305 XD_DYNARR_DESC (charset_conversion_spec_dynarr, &ccs_description),
300 { XD_END } 306 { XD_END }
301 }; 307 };
302 308
303 static const struct struct_description ccsd_description = { 309 static const struct struct_description ccsd_description = {
304 sizeof(charset_conversion_spec_dynarr), 310 sizeof (charset_conversion_spec_dynarr),
305 ccsd_description_1 311 ccsd_description_1
306 }; 312 };
307 #endif 313 #endif
308 314
309 static const struct lrecord_description coding_system_description[] = { 315 static const struct lrecord_description coding_system_description[] = {
310 { XD_LISP_OBJECT, offsetof(struct Lisp_Coding_System, name), 2 }, 316 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, name) },
311 { XD_LISP_OBJECT, offsetof(struct Lisp_Coding_System, mnemonic), 3 }, 317 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, doc_string) },
312 { XD_LISP_OBJECT, offsetof(struct Lisp_Coding_System, eol_lf), 3 }, 318 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, mnemonic) },
319 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, post_read_conversion) },
320 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, pre_write_conversion) },
321 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, eol_lf) },
322 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, eol_crlf) },
323 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, eol_cr) },
313 #ifdef MULE 324 #ifdef MULE
314 { XD_LISP_OBJECT, offsetof(struct Lisp_Coding_System, iso2022.initial_charset), 4 }, 325 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Coding_System, iso2022.initial_charset), 4 },
315 { XD_STRUCT_PTR, offsetof(struct Lisp_Coding_System, iso2022.input_conv), 1, &ccsd_description }, 326 { XD_STRUCT_PTR, offsetof (Lisp_Coding_System, iso2022.input_conv), 1, &ccsd_description },
316 { XD_STRUCT_PTR, offsetof(struct Lisp_Coding_System, iso2022.output_conv), 1, &ccsd_description }, 327 { XD_STRUCT_PTR, offsetof (Lisp_Coding_System, iso2022.output_conv), 1, &ccsd_description },
317 { XD_LISP_OBJECT, offsetof(struct Lisp_Coding_System, ccl.decode), 2 }, 328 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, ccl.decode) },
329 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, ccl.encode) },
318 #endif 330 #endif
319 { XD_END } 331 { XD_END }
320 }; 332 };
321 333
322 DEFINE_LRECORD_IMPLEMENTATION ("coding-system", coding_system, 334 DEFINE_LRECORD_IMPLEMENTATION ("coding-system", coding_system,
323 mark_coding_system, print_coding_system, 335 mark_coding_system, print_coding_system,
324 finalize_coding_system, 336 finalize_coding_system,
325 0, 0, coding_system_description, 337 0, 0, coding_system_description,
326 struct Lisp_Coding_System); 338 Lisp_Coding_System);
327 339
328 static Lisp_Object 340 static Lisp_Object
329 mark_coding_system (Lisp_Object obj) 341 mark_coding_system (Lisp_Object obj)
330 { 342 {
331 Lisp_Coding_System *codesys = XCODING_SYSTEM (obj); 343 Lisp_Coding_System *codesys = XCODING_SYSTEM (obj);
527 If there is no such coding system, nil is returned. Otherwise the 539 If there is no such coding system, nil is returned. Otherwise the
528 associated coding system object is returned. 540 associated coding system object is returned.
529 */ 541 */
530 (coding_system_or_name)) 542 (coding_system_or_name))
531 { 543 {
532 if (CODING_SYSTEMP (coding_system_or_name))
533 return coding_system_or_name;
534
535 if (NILP (coding_system_or_name)) 544 if (NILP (coding_system_or_name))
536 coding_system_or_name = Qbinary; 545 coding_system_or_name = Qbinary;
546 else if (CODING_SYSTEMP (coding_system_or_name))
547 return coding_system_or_name;
537 else 548 else
538 CHECK_SYMBOL (coding_system_or_name); 549 CHECK_SYMBOL (coding_system_or_name);
539 550
540 return Fgethash (coding_system_or_name, Vcoding_system_hash_table, Qnil); 551 while (1)
552 {
553 coding_system_or_name =
554 Fgethash (coding_system_or_name, Vcoding_system_hash_table, Qnil);
555
556 if (CODING_SYSTEMP (coding_system_or_name) || NILP (coding_system_or_name))
557 return coding_system_or_name;
558 }
541 } 559 }
542 560
543 DEFUN ("get-coding-system", Fget_coding_system, 1, 1, 0, /* 561 DEFUN ("get-coding-system", Fget_coding_system, 1, 1, 0, /*
544 Retrieve the coding system of the given name. 562 Retrieve the coding system of the given name.
545 Same as `find-coding-system' except that if there is no such 563 Same as `find-coding-system' except that if there is no such
1022 to->name = new_name; 1040 to->name = new_name;
1023 } 1041 }
1024 return new_coding_system; 1042 return new_coding_system;
1025 } 1043 }
1026 1044
1045 DEFUN ("coding-system-canonical-name-p", Fcoding_system_canonical_name_p, 1, 1, 0, /*
1046 Return t if OBJECT names a coding system, and is not a coding system alias.
1047 */
1048 (object))
1049 {
1050 return CODING_SYSTEMP (Fgethash (object, Vcoding_system_hash_table, Qnil))
1051 ? Qt : Qnil;
1052 }
1053
1054 DEFUN ("coding-system-alias-p", Fcoding_system_alias_p, 1, 1, 0, /*
1055 Return t if OBJECT is a coding system alias.
1056 All coding system aliases are created by `define-coding-system-alias'.
1057 */
1058 (object))
1059 {
1060 return SYMBOLP (Fgethash (object, Vcoding_system_hash_table, Qzero))
1061 ? Qt : Qnil;
1062 }
1063
1064 DEFUN ("coding-system-aliasee", Fcoding_system_aliasee, 1, 1, 0, /*
1065 Return the coding-system symbol for which symbol ALIAS is an alias.
1066 */
1067 (alias))
1068 {
1069 Lisp_Object aliasee = Fgethash (alias, Vcoding_system_hash_table, Qnil);
1070 if (SYMBOLP (aliasee))
1071 return aliasee;
1072 else
1073 signal_simple_error ("Symbol is not a coding system alias", alias);
1074 }
1075
1076 static Lisp_Object
1077 append_suffix_to_symbol (Lisp_Object symbol, const char *ascii_string)
1078 {
1079 return Fintern (concat2 (Fsymbol_name (symbol), build_string (ascii_string)),
1080 Qnil);
1081 }
1082
1083 /* A maphash function, for removing dangling coding system aliases. */
1084 static int
1085 dangling_coding_system_alias_p (Lisp_Object alias,
1086 Lisp_Object aliasee,
1087 void *dangling_aliases)
1088 {
1089 if (SYMBOLP (aliasee)
1090 && NILP (Fgethash (aliasee, Vcoding_system_hash_table, Qnil)))
1091 {
1092 (*(int *) dangling_aliases)++;
1093 return 1;
1094 }
1095 else
1096 return 0;
1097 }
1098
1027 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, 2, 2, 0, /* 1099 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, 2, 2, 0, /*
1028 Define symbol ALIAS as an alias for coding system CODING-SYSTEM. 1100 Define symbol ALIAS as an alias for coding system ALIASEE.
1101
1102 You can use this function to redefine an alias that has already been defined,
1103 but you cannot redefine a name which is the canonical name for a coding system.
1104 \(a canonical name of a coding system is what is returned when you call
1105 `coding-system-name' on a coding system).
1106
1107 ALIASEE itself can be an alias, which allows you to define nested aliases.
1108
1109 You are forbidden, however, from creating alias loops or `dangling' aliases.
1110 These will be detected, and an error will be signaled if you attempt to do so.
1111
1112 If ALIASEE is nil, then ALIAS will simply be undefined.
1113
1114 See also `coding-system-alias-p', `coding-system-aliasee',
1115 and `coding-system-canonical-name-p'.
1029 */ 1116 */
1030 (alias, coding_system)) 1117 (alias, aliasee))
1031 { 1118 {
1119 Lisp_Object real_coding_system, probe;
1120
1032 CHECK_SYMBOL (alias); 1121 CHECK_SYMBOL (alias);
1033 if (!NILP (Ffind_coding_system (alias))) 1122
1034 signal_simple_error ("Symbol already names a coding system", alias); 1123 if (!NILP (Fcoding_system_canonical_name_p (alias)))
1035 coding_system = Fget_coding_system (coding_system); 1124 signal_simple_error
1036 Fputhash (alias, coding_system, Vcoding_system_hash_table); 1125 ("Symbol is the canonical name of a coding system and cannot be redefined",
1037 1126 alias);
1038 /* Set up aliases for subsidiaries. */ 1127
1039 if (XCODING_SYSTEM_EOL_TYPE (coding_system) == EOL_AUTODETECT) 1128 if (NILP (aliasee))
1040 { 1129 {
1041 Lisp_Object str; 1130 Lisp_Object subsidiary_unix = append_suffix_to_symbol (alias, "-unix");
1042 XSETSTRING (str, symbol_name (XSYMBOL (alias))); 1131 Lisp_Object subsidiary_dos = append_suffix_to_symbol (alias, "-dos");
1043 #define FROB(type, name) \ 1132 Lisp_Object subsidiary_mac = append_suffix_to_symbol (alias, "-mac");
1044 do { \ 1133
1045 Lisp_Object subsidiary = XCODING_SYSTEM_EOL_##type (coding_system); \ 1134 Fremhash (alias, Vcoding_system_hash_table);
1046 if (!NILP (subsidiary)) \ 1135
1047 Fdefine_coding_system_alias \ 1136 /* Undefine subsidiary aliases,
1048 (Fintern (concat2 (str, build_string (name)), Qnil), subsidiary); \ 1137 presumably created by a previous call to this function */
1049 } while (0) 1138 if (! NILP (Fcoding_system_alias_p (subsidiary_unix)) &&
1050 FROB (LF, "-unix"); 1139 ! NILP (Fcoding_system_alias_p (subsidiary_dos)) &&
1051 FROB (CRLF, "-dos"); 1140 ! NILP (Fcoding_system_alias_p (subsidiary_mac)))
1052 FROB (CR, "-mac"); 1141 {
1053 #undef FROB 1142 Fdefine_coding_system_alias (subsidiary_unix, Qnil);
1054 } 1143 Fdefine_coding_system_alias (subsidiary_dos, Qnil);
1144 Fdefine_coding_system_alias (subsidiary_mac, Qnil);
1145 }
1146
1147 /* Undefine dangling coding system aliases. */
1148 {
1149 int dangling_aliases;
1150
1151 do {
1152 dangling_aliases = 0;
1153 elisp_map_remhash (dangling_coding_system_alias_p,
1154 Vcoding_system_hash_table,
1155 &dangling_aliases);
1156 } while (dangling_aliases > 0);
1157 }
1158
1159 return Qnil;
1160 }
1161
1162 if (CODING_SYSTEMP (aliasee))
1163 aliasee = XCODING_SYSTEM_NAME (aliasee);
1164
1165 /* Checks that aliasee names a coding-system */
1166 real_coding_system = Fget_coding_system (aliasee);
1167
1168 /* Check for coding system alias loops */
1169 if (EQ (alias, aliasee))
1170 alias_loop: signal_simple_error_2
1171 ("Attempt to create a coding system alias loop", alias, aliasee);
1172
1173 for (probe = aliasee;
1174 SYMBOLP (probe);
1175 probe = Fgethash (probe, Vcoding_system_hash_table, Qzero))
1176 {
1177 if (EQ (probe, alias))
1178 goto alias_loop;
1179 }
1180
1181 Fputhash (alias, aliasee, Vcoding_system_hash_table);
1182
1183 /* Set up aliases for subsidiaries.
1184 #### There must be a better way to handle subsidiary coding systems. */
1185 {
1186 static const char *suffixes[] = { "-unix", "-dos", "-mac" };
1187 int i;
1188 for (i = 0; i < countof (suffixes); i++)
1189 {
1190 Lisp_Object alias_subsidiary =
1191 append_suffix_to_symbol (alias, suffixes[i]);
1192 Lisp_Object aliasee_subsidiary =
1193 append_suffix_to_symbol (aliasee, suffixes[i]);
1194
1195 if (! NILP (Ffind_coding_system (aliasee_subsidiary)))
1196 Fdefine_coding_system_alias (alias_subsidiary, aliasee_subsidiary);
1197 }
1198 }
1055 /* FSF return value is a vector of [ALIAS-unix ALIAS-dos ALIAS-mac], 1199 /* FSF return value is a vector of [ALIAS-unix ALIAS-dos ALIAS-mac],
1056 but it doesn't look intentional, so I'd rather return something 1200 but it doesn't look intentional, so I'd rather return something
1057 meaningful or nothing at all. */ 1201 meaningful or nothing at all. */
1058 return Qnil; 1202 return Qnil;
1059 } 1203 }
5403 str->flags = flags; 5547 str->flags = flags;
5404 str->ch = ch; 5548 str->ch = ch;
5405 } 5549 }
5406 5550
5407 5551
5408 /************************************************************************/ 5552
5409 /* Simple internal/external functions */
5410 /************************************************************************/
5411
5412 static Extbyte_dynarr *conversion_out_dynarr;
5413 static Bufbyte_dynarr *conversion_in_dynarr;
5414
5415 /* Determine coding system from coding format */
5416
5417 /* #### not correct for all values of `fmt'! */
5418 static Lisp_Object
5419 external_data_format_to_coding_system (enum external_data_format fmt)
5420 {
5421 switch (fmt)
5422 {
5423 case FORMAT_FILENAME:
5424 case FORMAT_TERMINAL:
5425 if (EQ (Vfile_name_coding_system, Qnil) ||
5426 EQ (Vfile_name_coding_system, Qbinary))
5427 return Qnil;
5428 else
5429 return Fget_coding_system (Vfile_name_coding_system);
5430 #ifdef MULE
5431 case FORMAT_CTEXT:
5432 return Fget_coding_system (Qctext);
5433 #endif
5434 default:
5435 return Qnil;
5436 }
5437 }
5438
5439 Extbyte *
5440 convert_to_external_format (CONST Bufbyte *ptr,
5441 Bytecount len,
5442 Extcount *len_out,
5443 enum external_data_format fmt)
5444 {
5445 Lisp_Object coding_system = external_data_format_to_coding_system (fmt);
5446
5447 if (!conversion_out_dynarr)
5448 conversion_out_dynarr = Dynarr_new (Extbyte);
5449 else
5450 Dynarr_reset (conversion_out_dynarr);
5451
5452 if (NILP (coding_system))
5453 {
5454 CONST Bufbyte *end = ptr + len;
5455
5456 for (; ptr < end;)
5457 {
5458 Bufbyte c =
5459 (BYTE_ASCII_P (*ptr)) ? *ptr :
5460 (*ptr == LEADING_BYTE_CONTROL_1) ? (*(ptr+1) - 0x20) :
5461 (*ptr == LEADING_BYTE_LATIN_ISO8859_1) ? (*(ptr+1)) :
5462 '~';
5463
5464 Dynarr_add (conversion_out_dynarr, (Extbyte) c);
5465 INC_CHARPTR (ptr);
5466 }
5467
5468 #ifdef ERROR_CHECK_BUFPOS
5469 assert (ptr == end);
5470 #endif
5471 }
5472 else
5473 {
5474 Lisp_Object instream, outstream, da_outstream;
5475 Lstream *istr, *ostr;
5476 struct gcpro gcpro1, gcpro2, gcpro3;
5477 char tempbuf[1024]; /* some random amount */
5478
5479 instream = make_fixed_buffer_input_stream ((unsigned char *) ptr, len);
5480 da_outstream = make_dynarr_output_stream
5481 ((unsigned_char_dynarr *) conversion_out_dynarr);
5482 outstream =
5483 make_encoding_output_stream (XLSTREAM (da_outstream), coding_system);
5484 istr = XLSTREAM (instream);
5485 ostr = XLSTREAM (outstream);
5486 GCPRO3 (instream, outstream, da_outstream);
5487 while (1)
5488 {
5489 ssize_t size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
5490 if (!size_in_bytes)
5491 break;
5492 Lstream_write (ostr, tempbuf, size_in_bytes);
5493 }
5494 Lstream_close (istr);
5495 Lstream_close (ostr);
5496 UNGCPRO;
5497 Lstream_delete (istr);
5498 Lstream_delete (ostr);
5499 Lstream_delete (XLSTREAM (da_outstream));
5500 }
5501
5502 *len_out = Dynarr_length (conversion_out_dynarr);
5503 Dynarr_add (conversion_out_dynarr, 0); /* remember to zero-terminate! */
5504 return Dynarr_atp (conversion_out_dynarr, 0);
5505 }
5506
5507 Bufbyte *
5508 convert_from_external_format (CONST Extbyte *ptr,
5509 Extcount len,
5510 Bytecount *len_out,
5511 enum external_data_format fmt)
5512 {
5513 Lisp_Object coding_system = external_data_format_to_coding_system (fmt);
5514
5515 if (!conversion_in_dynarr)
5516 conversion_in_dynarr = Dynarr_new (Bufbyte);
5517 else
5518 Dynarr_reset (conversion_in_dynarr);
5519
5520 if (NILP (coding_system))
5521 {
5522 CONST Extbyte *end = ptr + len;
5523 for (; ptr < end; ptr++)
5524 {
5525 Extbyte c = *ptr;
5526 DECODE_ADD_BINARY_CHAR (c, conversion_in_dynarr);
5527 }
5528 }
5529 else
5530 {
5531 Lisp_Object instream, outstream, da_outstream;
5532 Lstream *istr, *ostr;
5533 struct gcpro gcpro1, gcpro2, gcpro3;
5534 char tempbuf[1024]; /* some random amount */
5535
5536 instream = make_fixed_buffer_input_stream ((unsigned char *) ptr, len);
5537 da_outstream = make_dynarr_output_stream
5538 ((unsigned_char_dynarr *) conversion_in_dynarr);
5539 outstream =
5540 make_decoding_output_stream (XLSTREAM (da_outstream), coding_system);
5541 istr = XLSTREAM (instream);
5542 ostr = XLSTREAM (outstream);
5543 GCPRO3 (instream, outstream, da_outstream);
5544 while (1)
5545 {
5546 ssize_t size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
5547 if (!size_in_bytes)
5548 break;
5549 Lstream_write (ostr, tempbuf, size_in_bytes);
5550 }
5551 Lstream_close (istr);
5552 Lstream_close (ostr);
5553 UNGCPRO;
5554 Lstream_delete (istr);
5555 Lstream_delete (ostr);
5556 Lstream_delete (XLSTREAM (da_outstream));
5557 }
5558
5559 *len_out = Dynarr_length (conversion_in_dynarr);
5560 Dynarr_add (conversion_in_dynarr, 0); /* remember to zero-terminate! */
5561 return Dynarr_atp (conversion_in_dynarr, 0);
5562 }
5563
5564
5565 /************************************************************************/ 5553 /************************************************************************/
5566 /* Initialization */ 5554 /* Initialization */
5567 /************************************************************************/ 5555 /************************************************************************/
5568 5556
5569 void 5557 void
5577 DEFSUBR (Fget_coding_system); 5565 DEFSUBR (Fget_coding_system);
5578 DEFSUBR (Fcoding_system_list); 5566 DEFSUBR (Fcoding_system_list);
5579 DEFSUBR (Fcoding_system_name); 5567 DEFSUBR (Fcoding_system_name);
5580 DEFSUBR (Fmake_coding_system); 5568 DEFSUBR (Fmake_coding_system);
5581 DEFSUBR (Fcopy_coding_system); 5569 DEFSUBR (Fcopy_coding_system);
5570 DEFSUBR (Fcoding_system_canonical_name_p);
5571 DEFSUBR (Fcoding_system_alias_p);
5572 DEFSUBR (Fcoding_system_aliasee);
5582 DEFSUBR (Fdefine_coding_system_alias); 5573 DEFSUBR (Fdefine_coding_system_alias);
5583 DEFSUBR (Fsubsidiary_coding_system); 5574 DEFSUBR (Fsubsidiary_coding_system);
5584 5575
5585 DEFSUBR (Fcoding_system_type); 5576 DEFSUBR (Fcoding_system_type);
5586 DEFSUBR (Fcoding_system_doc_string); 5577 DEFSUBR (Fcoding_system_doc_string);
5652 #endif /* MULE */ 5643 #endif /* MULE */
5653 defsymbol (&Qencode, "encode"); 5644 defsymbol (&Qencode, "encode");
5654 defsymbol (&Qdecode, "decode"); 5645 defsymbol (&Qdecode, "decode");
5655 5646
5656 #ifdef MULE 5647 #ifdef MULE
5657 defsymbol (&Qctext, "ctext");
5658 defsymbol (&coding_category_symbol[CODING_CATEGORY_SHIFT_JIS], 5648 defsymbol (&coding_category_symbol[CODING_CATEGORY_SHIFT_JIS],
5659 "shift-jis"); 5649 "shift-jis");
5660 defsymbol (&coding_category_symbol[CODING_CATEGORY_BIG5], 5650 defsymbol (&coding_category_symbol[CODING_CATEGORY_BIG5],
5661 "big5"); 5651 "big5");
5662 defsymbol (&coding_category_symbol[CODING_CATEGORY_UCS4], 5652 defsymbol (&coding_category_symbol[CODING_CATEGORY_UCS4],
5726 Not used under a windowing system. 5716 Not used under a windowing system.
5727 */ ); 5717 */ );
5728 Vterminal_coding_system = Qnil; 5718 Vterminal_coding_system = Qnil;
5729 5719
5730 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read /* 5720 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read /*
5731 Overriding coding system used when writing a file or process. 5721 Overriding coding system used when reading from a file or process.
5732 You should *bind* this, not set it. If this is non-nil, it specifies 5722 You should bind this variable with `let', but do not set it globally.
5733 the coding system that will be used when a file or process is read 5723 If this is non-nil, it specifies the coding system that will be used
5734 in, and overrides `buffer-file-coding-system-for-read', 5724 to decode input on read operations, such as from a file or process.
5725 It overrides `buffer-file-coding-system-for-read',
5735 `insert-file-contents-pre-hook', etc. Use those variables instead of 5726 `insert-file-contents-pre-hook', etc. Use those variables instead of
5736 this one for permanent changes to the environment. 5727 this one for permanent changes to the environment. */ );
5737 */ );
5738 Vcoding_system_for_read = Qnil; 5728 Vcoding_system_for_read = Qnil;
5739 5729
5740 DEFVAR_LISP ("coding-system-for-write", 5730 DEFVAR_LISP ("coding-system-for-write",
5741 &Vcoding_system_for_write /* 5731 &Vcoding_system_for_write /*
5742 Overriding coding system used when writing a file or process. 5732 Overriding coding system used when writing to a file or process.
5743 You should *bind* this, not set it. If this is non-nil, it specifies 5733 You should bind this variable with `let', but do not set it globally.
5744 the coding system that will be used when a file or process is wrote 5734 If this is non-nil, it specifies the coding system that will be used
5745 in, and overrides `buffer-file-coding-system', 5735 to encode output for write operations, such as to a file or process.
5746 `write-region-pre-hook', etc. Use those variables instead of this one 5736 It overrides `buffer-file-coding-system', `write-region-pre-hook', etc.
5747 for permanent changes to the environment. 5737 Use those variables instead of this one for permanent changes to the
5748 */ ); 5738 environment. */ );
5749 Vcoding_system_for_write = Qnil; 5739 Vcoding_system_for_write = Qnil;
5750 5740
5751 DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system /* 5741 DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system /*
5752 Coding system used to convert pathnames when accessing files. 5742 Coding system used to convert pathnames when accessing files.
5753 */ ); 5743 */ );
5822 list4 (Qeol_type, Qlf, 5812 list4 (Qeol_type, Qlf,
5823 Qmnemonic, build_string ("Binary"))); 5813 Qmnemonic, build_string ("Binary")));
5824 5814
5825 Fdefine_coding_system_alias (Qno_conversion, Qraw_text); 5815 Fdefine_coding_system_alias (Qno_conversion, Qraw_text);
5826 5816
5817 Fdefine_coding_system_alias (Qfile_name, Qbinary);
5818
5819 Fdefine_coding_system_alias (Qterminal, Qbinary);
5820 Fdefine_coding_system_alias (Qkeyboard, Qbinary);
5821
5827 /* Need this for bootstrapping */ 5822 /* Need this for bootstrapping */
5828 fcd->coding_category_system[CODING_CATEGORY_NO_CONVERSION] = 5823 fcd->coding_category_system[CODING_CATEGORY_NO_CONVERSION] =
5829 Fget_coding_system (Qraw_text); 5824 Fget_coding_system (Qraw_text);
5830 5825
5831 #ifdef MULE 5826 #ifdef MULE