aboutsummaryrefslogtreecommitdiffstats
path: root/src/mac.c
diff options
context:
space:
mode:
authorYAMAMOTO Mitsuharu2005-12-24 02:50:11 +0000
committerYAMAMOTO Mitsuharu2005-12-24 02:50:11 +0000
commit7ce8b6381a456e896cc8f5fbf2ee337b5c1e463e (patch)
tree2aca4e1667267e29a010949af39c28a17533d7f0 /src/mac.c
parent2d22e7edf2d46b0907d88dc8332b21fd3430c6e3 (diff)
downloademacs-7ce8b6381a456e896cc8f5fbf2ee337b5c1e463e.tar.gz
emacs-7ce8b6381a456e896cc8f5fbf2ee337b5c1e463e.zip
(posix_pathname_to_fsspec, fsspec_to_posix_pathname): Add
prototypes. Make static. (mac_aedesc_to_lisp): Initialize err to noErr. (mac_coerce_file_name_ptr, mac_coerce_file_name_desc) (init_coercion_handler): New functions. (Fmac_coerce_ae_data): Use coercion of Apple event data for translation from/to file names.
Diffstat (limited to 'src/mac.c')
-rw-r--r--src/mac.c358
1 files changed, 280 insertions, 78 deletions
diff --git a/src/mac.c b/src/mac.c
index 0e2eac877da..7c3e495f3a9 100644
--- a/src/mac.c
+++ b/src/mac.c
@@ -79,6 +79,8 @@ static ComponentInstance as_scripting_component;
79/* The single script context used for all script executions. */ 79/* The single script context used for all script executions. */
80static OSAID as_script_context; 80static OSAID as_script_context;
81 81
82static OSErr posix_pathname_to_fsspec P_ ((const char *, FSSpec *));
83static OSErr fsspec_to_posix_pathname P_ ((const FSSpec *, char *, int));
82 84
83/* When converting from Mac to Unix pathnames, /'s in folder names are 85/* When converting from Mac to Unix pathnames, /'s in folder names are
84 converted to :'s. This function, used in copying folder names, 86 converted to :'s. This function, used in copying folder names,
@@ -333,7 +335,7 @@ Lisp_Object
333mac_aedesc_to_lisp (desc) 335mac_aedesc_to_lisp (desc)
334 AEDesc *desc; 336 AEDesc *desc;
335{ 337{
336 OSErr err; 338 OSErr err = noErr;
337 DescType desc_type = desc->descriptorType; 339 DescType desc_type = desc->descriptorType;
338 Lisp_Object result; 340 Lisp_Object result;
339 341
@@ -397,6 +399,277 @@ mac_aedesc_to_lisp (desc)
397 return Fcons (make_unibyte_string ((char *) &desc_type, 4), result); 399 return Fcons (make_unibyte_string ((char *) &desc_type, 4), result);
398} 400}
399 401
402static pascal OSErr
403mac_coerce_file_name_ptr (type_code, data_ptr, data_size,
404 to_type, handler_refcon, result)
405 DescType type_code;
406 const void *data_ptr;
407 Size data_size;
408 DescType to_type;
409 long handler_refcon;
410 AEDesc *result;
411{
412 OSErr err;
413
414 if (type_code == TYPE_FILE_NAME)
415 /* Coercion from undecoded file name. */
416 switch (to_type)
417 {
418 case typeAlias:
419 case typeFSS:
420 case typeFSRef:
421#ifdef MAC_OSX
422 case typeFileURL:
423#endif
424 {
425#ifdef MAC_OSX
426 CFStringRef str;
427 CFURLRef url = NULL;
428 CFDataRef data = NULL;
429
430 str = CFStringCreateWithBytes (NULL, data_ptr, data_size,
431 kCFStringEncodingUTF8, false);
432 if (str)
433 {
434 url = CFURLCreateWithFileSystemPath (NULL, str,
435 kCFURLPOSIXPathStyle, false);
436 CFRelease (str);
437 }
438 if (url)
439 {
440 data = CFURLCreateData (NULL, url, kCFStringEncodingUTF8, true);
441 CFRelease (url);
442 }
443 if (data)
444 {
445 err = AECoercePtr (typeFileURL, CFDataGetBytePtr (data),
446 CFDataGetLength (data), to_type, result);
447 CFRelease (data);
448 }
449 else
450 err = memFullErr;
451#else
452 FSSpec fs;
453 char *buf;
454
455 buf = xmalloc (data_size + 1);
456 if (buf)
457 {
458 memcpy (buf, data_ptr, data_size);
459 buf[data_size] = '\0';
460 err = posix_pathname_to_fsspec (buf, &fs);
461 xfree (buf);
462 }
463 else
464 err = memFullErr;
465 if (err == noErr)
466 err = AECoercePtr (typeFSS, &fs, sizeof (FSSpec),
467 to_type, result);
468#endif
469 }
470 break;
471
472 case TYPE_FILE_NAME:
473 case typeWildCard:
474 err = AECreateDesc (TYPE_FILE_NAME, data_ptr, data_size, result);
475 break;
476
477 default:
478 err = errAECoercionFail;
479 break;
480 }
481 else if (to_type == TYPE_FILE_NAME)
482 /* Coercion to undecoded file name. */
483 switch (type_code)
484 {
485 case typeAlias:
486 case typeFSS:
487 case typeFSRef:
488#ifdef MAC_OSX
489 case typeFileURL:
490#endif
491 {
492 AEDesc desc;
493#ifdef MAC_OSX
494 Size size;
495 char *buf;
496 CFURLRef url = NULL;
497 CFStringRef str = NULL;
498 CFDataRef data = NULL;
499
500 err = AECoercePtr (type_code, data_ptr, data_size,
501 typeFileURL, &desc);
502 if (err == noErr)
503 {
504 size = AEGetDescDataSize (&desc);
505 buf = xmalloc (size);
506 if (buf)
507 {
508 err = AEGetDescData (&desc, buf, size);
509 if (err == noErr)
510 url = CFURLCreateWithBytes (NULL, buf, size,
511 kCFStringEncodingUTF8, NULL);
512 xfree (buf);
513 }
514 AEDisposeDesc (&desc);
515 }
516 if (url)
517 {
518 str = CFURLCopyFileSystemPath (url, kCFURLPOSIXPathStyle);
519 CFRelease (url);
520 }
521 if (str)
522 {
523 data =
524 CFStringCreateExternalRepresentation (NULL, str,
525 kCFStringEncodingUTF8,
526 '\0');
527 CFRelease (str);
528 }
529 if (data)
530 {
531 err = AECreateDesc (TYPE_FILE_NAME, CFDataGetBytePtr (data),
532 CFDataGetLength (data), result);
533 CFRelease (data);
534 }
535 else
536 err = memFullErr;
537#else
538 FSSpec fs;
539 char file_name[MAXPATHLEN];
540
541 err = AECoercePtr (type_code, data_ptr, data_size,
542 typeFSS, &desc);
543 if (err == noErr)
544 {
545#if TARGET_API_MAC_CARBON
546 err = AEGetDescData (&desc, &fs, sizeof (FSSpec));
547#else
548 fs = *(FSSpec *)(*(desc.dataHandle));
549#endif
550 if (err == noErr)
551 err = fsspec_to_posix_pathname (&fs, file_name,
552 sizeof (file_name) - 1);
553 if (err == noErr)
554 err = AECreateDesc (TYPE_FILE_NAME, file_name,
555 strlen (file_name), result);
556 AEDisposeDesc (&desc);
557 }
558#endif
559 }
560 break;
561
562 default:
563 err = errAECoercionFail;
564 break;
565 }
566 else
567 abort ();
568
569 if (err != noErr)
570 return errAECoercionFail;
571 return noErr;
572}
573
574static pascal OSErr
575mac_coerce_file_name_desc (from_desc, to_type, handler_refcon, result)
576 const AEDesc *from_desc;
577 DescType to_type;
578 long handler_refcon;
579 AEDesc *result;
580{
581 OSErr err = noErr;
582 DescType from_type = from_desc->descriptorType;
583
584 if (from_type == TYPE_FILE_NAME)
585 {
586 if (to_type != TYPE_FILE_NAME && to_type != typeWildCard
587 && to_type != typeAlias && to_type != typeFSS
588 && to_type != typeFSRef
589#ifdef MAC_OSX
590 && to_type != typeFileURL
591#endif
592 )
593 return errAECoercionFail;
594 }
595 else if (to_type == TYPE_FILE_NAME)
596 {
597 if (from_type != typeAlias && from_type != typeFSS
598 && from_type != typeFSRef
599#ifdef MAC_OSX
600 && from_type != typeFileURL
601#endif
602 )
603 return errAECoercionFail;
604 }
605 else
606 abort ();
607
608 if (from_type == to_type || to_type == typeWildCard)
609 err = AEDuplicateDesc (from_desc, result);
610 else
611 {
612 char *data_ptr;
613 Size data_size;
614
615#if TARGET_API_MAC_CARBON
616 data_size = AEGetDescDataSize (from_desc);
617#else
618 data_size = GetHandleSize (from_desc->dataHandle);
619#endif
620 data_ptr = xmalloc (data_size);
621 if (data_ptr)
622 {
623#if TARGET_API_MAC_CARBON
624 err = AEGetDescData (from_desc, data_ptr, data_size);
625#else
626 memcpy (data_ptr, *(from_desc->dataHandle), data_size);
627#endif
628 if (err == noErr)
629 err = mac_coerce_file_name_ptr (from_type, data_ptr,
630 data_size, to_type,
631 handler_refcon, result);
632 xfree (data_ptr);
633 }
634 else
635 err = memFullErr;
636 }
637
638 if (err != noErr)
639 return errAECoercionFail;
640 return noErr;
641}
642
643OSErr
644init_coercion_handler ()
645{
646 OSErr err;
647
648 static AECoercePtrUPP coerce_file_name_ptrUPP = NULL;
649 static AECoerceDescUPP coerce_file_name_descUPP = NULL;
650
651 if (coerce_file_name_ptrUPP == NULL)
652 {
653 coerce_file_name_ptrUPP = NewAECoercePtrUPP (mac_coerce_file_name_ptr);
654 coerce_file_name_descUPP = NewAECoerceDescUPP (mac_coerce_file_name_desc);
655 }
656
657 err = AEInstallCoercionHandler (TYPE_FILE_NAME, typeWildCard,
658 (AECoercionHandlerUPP)
659 coerce_file_name_ptrUPP, 0, false, false);
660 if (err == noErr)
661 err = AEInstallCoercionHandler (typeWildCard, TYPE_FILE_NAME,
662 (AECoercionHandlerUPP)
663 coerce_file_name_ptrUPP, 0, false, false);
664 if (err == noErr)
665 err = AEInstallCoercionHandler (TYPE_FILE_NAME, typeWildCard,
666 coerce_file_name_descUPP, 0, true, false);
667 if (err == noErr)
668 err = AEInstallCoercionHandler (typeWildCard, TYPE_FILE_NAME,
669 coerce_file_name_descUPP, 0, true, false);
670 return err;
671}
672
400#if TARGET_API_MAC_CARBON 673#if TARGET_API_MAC_CARBON
401OSErr 674OSErr
402create_apple_event_from_event_ref (event, num_params, names, types, result) 675create_apple_event_from_event_ref (event, num_params, names, types, result)
@@ -2602,7 +2875,7 @@ path_from_vol_dir_name (char *path, int man_path_len, short vol_ref_num,
2602} 2875}
2603 2876
2604 2877
2605OSErr 2878static OSErr
2606posix_pathname_to_fsspec (ufn, fs) 2879posix_pathname_to_fsspec (ufn, fs)
2607 const char *ufn; 2880 const char *ufn;
2608 FSSpec *fs; 2881 FSSpec *fs;
@@ -2618,7 +2891,7 @@ posix_pathname_to_fsspec (ufn, fs)
2618 } 2891 }
2619} 2892}
2620 2893
2621OSErr 2894static OSErr
2622fsspec_to_posix_pathname (fs, ufn, ufnbuflen) 2895fsspec_to_posix_pathname (fs, ufn, ufnbuflen)
2623 const FSSpec *fs; 2896 const FSSpec *fs;
2624 char *ufn; 2897 char *ufn;
@@ -4072,92 +4345,21 @@ Each type should be a string of length 4 or the symbol
4072 4345
4073 CHECK_STRING (src_data); 4346 CHECK_STRING (src_data);
4074 if (EQ (src_type, Qundecoded_file_name)) 4347 if (EQ (src_type, Qundecoded_file_name))
4075 { 4348 src_desc_type = TYPE_FILE_NAME;
4076#ifdef MAC_OSX
4077 src_desc_type = typeFileURL;
4078#else
4079 src_desc_type = typeFSS;
4080#endif
4081 }
4082 else 4349 else
4083 src_desc_type = mac_get_code_from_arg (src_type, 0); 4350 src_desc_type = mac_get_code_from_arg (src_type, 0);
4084 4351
4085 if (EQ (dst_type, Qundecoded_file_name)) 4352 if (EQ (dst_type, Qundecoded_file_name))
4086 { 4353 dst_desc_type = TYPE_FILE_NAME;
4087#ifdef MAC_OSX
4088 dst_desc_type = typeFSRef;
4089#else
4090 dst_desc_type = typeFSS;
4091#endif
4092 }
4093 else 4354 else
4094 dst_desc_type = mac_get_code_from_arg (dst_type, 0); 4355 dst_desc_type = mac_get_code_from_arg (dst_type, 0);
4095 4356
4096 BLOCK_INPUT; 4357 BLOCK_INPUT;
4097 if (EQ (src_type, Qundecoded_file_name)) 4358 err = AECoercePtr (src_desc_type, SDATA (src_data), SBYTES (src_data),
4098 {
4099#ifdef MAC_OSX
4100 CFStringRef str;
4101 CFURLRef url = NULL;
4102 CFDataRef data = NULL;
4103
4104 str = cfstring_create_with_utf8_cstring (SDATA (src_data));
4105 if (str)
4106 {
4107 url = CFURLCreateWithFileSystemPath (NULL, str,
4108 kCFURLPOSIXPathStyle, false);
4109 CFRelease (str);
4110 }
4111 if (url)
4112 {
4113 data = CFURLCreateData (NULL, url, kCFStringEncodingUTF8, true);
4114 CFRelease (url);
4115 }
4116 if (data)
4117 {
4118 err = AECoercePtr (src_desc_type, CFDataGetBytePtr (data),
4119 CFDataGetLength (data),
4120 dst_desc_type, &dst_desc);
4121 CFRelease (data);
4122 }
4123 else
4124 err = memFullErr;
4125#else
4126 err = posix_pathname_to_fsspec (SDATA (src_data), &fs);
4127 if (err == noErr)
4128 AECoercePtr (src_desc_type, &fs, sizeof (FSSpec),
4129 dst_desc_type, &dst_desc); 4359 dst_desc_type, &dst_desc);
4130#endif
4131 }
4132 else
4133 err = AECoercePtr (src_desc_type, SDATA (src_data), SBYTES (src_data),
4134 dst_desc_type, &dst_desc);
4135
4136 if (err == noErr) 4360 if (err == noErr)
4137 { 4361 {
4138 if (EQ (dst_type, Qundecoded_file_name)) 4362 result = Fcdr (mac_aedesc_to_lisp (&dst_desc));
4139 {
4140 char file_name[MAXPATHLEN];
4141
4142#ifdef MAC_OSX
4143 err = AEGetDescData (&dst_desc, &fref, sizeof (FSRef));
4144 if (err == noErr)
4145 err = FSRefMakePath (&fref, file_name, sizeof (file_name));
4146#else
4147#if TARGET_API_MAC_CARBON
4148 err = AEGetDescData (&dst_desc, &fs, sizeof (FSSpec));
4149#else
4150 memcpy (&fs, *(dst_desc.dataHandle), sizeof (FSSpec));
4151#endif
4152 if (err == noErr)
4153 err = fsspec_to_posix_pathname (&fs, file_name,
4154 sizeof (file_name) - 1);
4155#endif
4156 if (err == noErr)
4157 result = make_unibyte_string (file_name, strlen (file_name));
4158 }
4159 else
4160 result = Fcdr (mac_aedesc_to_lisp (&dst_desc));
4161 AEDisposeDesc (&dst_desc); 4363 AEDisposeDesc (&dst_desc);
4162 } 4364 }
4163 UNBLOCK_INPUT; 4365 UNBLOCK_INPUT;