/[emacs]/emacs/src/mac.c
ViewVC logotype

Diff of /emacs/src/mac.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by pj, Mon May 6 06:48:57 2002 UTC revision 1.3.2.1 by miles, Fri Apr 4 06:21:01 2003 UTC
# Line 26  Boston, MA 02111-1307, USA.  */ Line 26  Boston, MA 02111-1307, USA.  */
26  #include <errno.h>  #include <errno.h>
27  #include <utime.h>  #include <utime.h>
28  #include <dirent.h>  #include <dirent.h>
29    #include <sys/types.h>
30  #include <sys/stat.h>  #include <sys/stat.h>
31  #include <string.h>  #include <string.h>
32  #include <pwd.h>  #include <pwd.h>
33  #include <sys/param.h>  #include <sys/param.h>
34    #include <stdlib.h>
35    #include <fcntl.h>
36  #if __MWERKS__  #if __MWERKS__
37  #include <unistd.h>  #include <unistd.h>
38  #endif  #endif
# Line 40  Boston, MA 02111-1307, USA.  */ Line 43  Boston, MA 02111-1307, USA.  */
43  #undef free  #undef free
44  #undef malloc  #undef malloc
45  #undef realloc  #undef realloc
46    #undef init_process
47  #include <Carbon/Carbon.h>  #include <Carbon/Carbon.h>
48  #undef free  #undef free
49  #define free unexec_free  #define free unexec_free
# Line 47  Boston, MA 02111-1307, USA.  */ Line 51  Boston, MA 02111-1307, USA.  */
51  #define malloc unexec_malloc  #define malloc unexec_malloc
52  #undef realloc  #undef realloc
53  #define realloc unexec_realloc  #define realloc unexec_realloc
54    #undef init_process
55    #define init_process emacs_init_process
56  #else /* not MAC_OSX */  #else /* not MAC_OSX */
57  #include <Files.h>  #include <Files.h>
58  #include <MacTypes.h>  #include <MacTypes.h>
# Line 86  string_cat_and_replace (char *s1, const Line 92  string_cat_and_replace (char *s1, const
92    int l2 = strlen (s2);    int l2 = strlen (s2);
93    char *p = s1 + l1;    char *p = s1 + l1;
94    int i;    int i;
95      
96    strncat (s1, s2, n);    strncat (s1, s2, n);
97    for (i = 0; i < l2; i++)    for (i = 0; i < l2; i++)
98      {      {
# Line 99  string_cat_and_replace (char *s1, const Line 105  string_cat_and_replace (char *s1, const
105    
106  /* Convert a Mac pathname to Posix form.  A Mac full pathname is one  /* Convert a Mac pathname to Posix form.  A Mac full pathname is one
107     that does not begin with a ':' and contains at least one ':'. A Mac     that does not begin with a ':' and contains at least one ':'. A Mac
108     full pathname causes an '/' to be prepended to the Posix pathname.     full pathname causes a '/' to be prepended to the Posix pathname.
109     The algorithm for the rest of the pathname is as follows:     The algorithm for the rest of the pathname is as follows:
110       For each segment between two ':',       For each segment between two ':',
111         if it is non-null, copy as is and then add a '/' at the end,         if it is non-null, copy as is and then add a '/' at the end,
112         otherwise, insert a "../" into the Posix pathname.         otherwise, insert a "../" into the Posix pathname.
113     Returns 1 if successful; 0 if fails.  */     Returns 1 if successful; 0 if fails.  */
114      
115  int  int
116  mac_to_posix_pathname (const char *mfn, char *ufn, int ufnbuflen)  mac_to_posix_pathname (const char *mfn, char *ufn, int ufnbuflen)
117  {  {
118    const char *p, *q, *pe;    const char *p, *q, *pe;
119            
120    strcpy (ufn, "");    strcpy (ufn, "");
121            
122    if (*mfn == '\0')    if (*mfn == '\0')
123      return 1;      return 1;
124            
125    p = strchr (mfn, ':');    p = strchr (mfn, ':');
126    if (p != 0 && p != mfn)  /* full pathname */    if (p != 0 && p != mfn)  /* full pathname */
127      strcat (ufn, "/");      strcat (ufn, "/");
128                    
129    p = mfn;    p = mfn;
130    if (*p == ':')    if (*p == ':')
131      p++;      p++;
# Line 154  mac_to_posix_pathname (const char *mfn, Line 160  mac_to_posix_pathname (const char *mfn,
160            p = pe;            p = pe;
161          }          }
162      }      }
163            
164    return 1;    return 1;
165  }  }
166    
# Line 164  extern char *get_temp_dir_name (); Line 170  extern char *get_temp_dir_name ();
170    
171  /* Convert a Posix pathname to Mac form.  Approximately reverse of the  /* Convert a Posix pathname to Mac form.  Approximately reverse of the
172     above in algorithm.  */     above in algorithm.  */
173      
174  int  int
175  posix_to_mac_pathname (const char *ufn, char *mfn, int mfnbuflen)  posix_to_mac_pathname (const char *ufn, char *mfn, int mfnbuflen)
176  {  {
177    const char *p, *q, *pe;    const char *p, *q, *pe;
178    char expanded_pathname[MAXPATHLEN+1];    char expanded_pathname[MAXPATHLEN+1];
179            
180    strcpy (mfn, "");    strcpy (mfn, "");
181            
182    if (*ufn == '\0')    if (*ufn == '\0')
183      return 1;      return 1;
184    
185    p = ufn;    p = ufn;
186      
187    /* Check for and handle volume names.  Last comparison: strangely    /* Check for and handle volume names.  Last comparison: strangely
188       somewhere "/.emacs" is passed.  A temporary fix for now.  */       somewhere "/.emacs" is passed.  A temporary fix for now.  */
189    if (*p == '/' && strchr (p+1, '/') == NULL && strcmp (p, "/.emacs") != 0)    if (*p == '/' && strchr (p+1, '/') == NULL && strcmp (p, "/.emacs") != 0)
# Line 211  posix_to_mac_pathname (const char *ufn, Line 217  posix_to_mac_pathname (const char *ufn,
217        strcat (expanded_pathname, p);        strcat (expanded_pathname, p);
218        p = expanded_pathname;        p = expanded_pathname;
219          /* now p points to the pathname with emacs dir prefix */          /* now p points to the pathname with emacs dir prefix */
220      }          }
221    else if (*p != '/')  /* relative pathname */    else if (*p != '/')  /* relative pathname */
222      strcat (mfn, ":");      strcat (mfn, ":");
223                    
224    if (*p == '/')    if (*p == '/')
225      p++;      p++;
226    
# Line 247  posix_to_mac_pathname (const char *ufn, Line 253  posix_to_mac_pathname (const char *ufn,
253            p = pe;            p = pe;
254          }          }
255      }      }
256            
257    return 1;    return 1;
258  }  }
259    
# Line 306  stat_noalias (const char *path, struct s Line 312  stat_noalias (const char *path, struct s
312    cipb.hFileInfo.ioDirID = 0;    cipb.hFileInfo.ioDirID = 0;
313    cipb.hFileInfo.ioFDirIndex = 0;    cipb.hFileInfo.ioFDirIndex = 0;
314      /* set to 0 to get information about specific dir or file */      /* set to 0 to get information about specific dir or file */
315      
316    errno = PBGetCatInfo (&cipb, false);    errno = PBGetCatInfo (&cipb, false);
317    if (errno == -43) /* -43: fnfErr defined in Errors.h */    if (errno == -43) /* -43: fnfErr defined in Errors.h */
318      errno = ENOENT;      errno = ENOENT;
# Line 316  stat_noalias (const char *path, struct s Line 322  stat_noalias (const char *path, struct s
322    if (cipb.hFileInfo.ioFlAttrib & 0x10)  /* bit 4 = 1 for directories */    if (cipb.hFileInfo.ioFlAttrib & 0x10)  /* bit 4 = 1 for directories */
323      {      {
324        buf->st_mode = S_IFDIR | S_IREAD | S_IEXEC;        buf->st_mode = S_IFDIR | S_IREAD | S_IEXEC;
325          
326        if (!(cipb.hFileInfo.ioFlAttrib & 0x1))        if (!(cipb.hFileInfo.ioFlAttrib & 0x1))
327          buf->st_mode |= S_IWRITE;  /* bit 1 = 1 for locked files/directories */          buf->st_mode |= S_IWRITE;  /* bit 1 = 1 for locked files/directories */
328        buf->st_ino = cipb.dirInfo.ioDrDirID;        buf->st_ino = cipb.dirInfo.ioDrDirID;
# Line 372  lstat (const char *path, struct stat *bu Line 378  lstat (const char *path, struct stat *bu
378    
379    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)
380      return -1;      return -1;
381      
382    return stat_noalias (true_pathname, buf);    return stat_noalias (true_pathname, buf);
383  }  }
384    
# Line 381  int Line 387  int
387  stat (const char *path, struct stat *sb)  stat (const char *path, struct stat *sb)
388  {  {
389    int result;    int result;
390    char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];      char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];
391    int len;    int len;
392      
393    if ((result = stat_noalias (path, sb)) >= 0 &&    if ((result = stat_noalias (path, sb)) >= 0 &&
394        ! (sb->st_mode & S_IFLNK))        ! (sb->st_mode & S_IFLNK))
395      return result;      return result;
396    
397    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)
398      return -1;      return -1;
399      
400    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);
401    if (len > -1)    if (len > -1)
402      {      {
# Line 432  mkdir (const char *dirname, int mode) Line 438  mkdir (const char *dirname, int mode)
438    
439    HFileParam hfpb;    HFileParam hfpb;
440    char true_pathname[MAXPATHLEN+1], mac_pathname[MAXPATHLEN+1];    char true_pathname[MAXPATHLEN+1], mac_pathname[MAXPATHLEN+1];
441      
442    if (find_true_pathname (dirname, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (dirname, true_pathname, MAXPATHLEN+1) == -1)
443      return -1;      return -1;
444            
445    if (posix_to_mac_pathname (true_pathname, mac_pathname, MAXPATHLEN+1) == 0)    if (posix_to_mac_pathname (true_pathname, mac_pathname, MAXPATHLEN+1) == 0)
446      return -1;      return -1;
447    
# Line 443  mkdir (const char *dirname, int mode) Line 449  mkdir (const char *dirname, int mode)
449    hfpb.ioNamePtr = mac_pathname;    hfpb.ioNamePtr = mac_pathname;
450    hfpb.ioVRefNum = 0;  /* ignored unless name is invalid */    hfpb.ioVRefNum = 0;  /* ignored unless name is invalid */
451    hfpb.ioDirID = 0;  /* parent is the root */    hfpb.ioDirID = 0;  /* parent is the root */
452      
453    errno = PBDirCreate ((HParmBlkPtr) &hfpb, false);    errno = PBDirCreate ((HParmBlkPtr) &hfpb, false);
454      /* just return the Mac OSErr code for now */      /* just return the Mac OSErr code for now */
455    return errno == noErr ? 0 : -1;    return errno == noErr ? 0 : -1;
# Line 455  sys_rmdir (const char *dirname) Line 461  sys_rmdir (const char *dirname)
461  {  {
462    HFileParam hfpb;    HFileParam hfpb;
463    char mac_pathname[MAXPATHLEN+1];    char mac_pathname[MAXPATHLEN+1];
464            
465    if (posix_to_mac_pathname (dirname, mac_pathname, MAXPATHLEN+1) == 0)    if (posix_to_mac_pathname (dirname, mac_pathname, MAXPATHLEN+1) == 0)
466      return -1;      return -1;
467    
# Line 463  sys_rmdir (const char *dirname) Line 469  sys_rmdir (const char *dirname)
469    hfpb.ioNamePtr = mac_pathname;    hfpb.ioNamePtr = mac_pathname;
470    hfpb.ioVRefNum = 0;  /* ignored unless name is invalid */    hfpb.ioVRefNum = 0;  /* ignored unless name is invalid */
471    hfpb.ioDirID = 0;  /* parent is the root */    hfpb.ioDirID = 0;  /* parent is the root */
472      
473    errno = PBHDelete ((HParmBlkPtr) &hfpb, false);    errno = PBHDelete ((HParmBlkPtr) &hfpb, false);
474    return errno == noErr ? 0 : -1;    return errno == noErr ? 0 : -1;
475  }  }
# Line 482  execvp (const char *path, ...) Line 488  execvp (const char *path, ...)
488  int  int
489  utime (const char *path, const struct utimbuf *times)  utime (const char *path, const struct utimbuf *times)
490  {  {
491    char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];      char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];
492    int len;    int len;
493    char mac_pathname[MAXPATHLEN+1];    char mac_pathname[MAXPATHLEN+1];
494    CInfoPBRec cipb;    CInfoPBRec cipb;
495      
496    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)
497      return -1;      return -1;
498      
499    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);
500    if (len > -1)    if (len > -1)
501      fully_resolved_name[len] = '\0';      fully_resolved_name[len] = '\0';
# Line 503  utime (const char *path, const struct ut Line 509  utime (const char *path, const struct ut
509    cipb.hFileInfo.ioNamePtr = mac_pathname;    cipb.hFileInfo.ioNamePtr = mac_pathname;
510    cipb.hFileInfo.ioVRefNum = 0;    cipb.hFileInfo.ioVRefNum = 0;
511    cipb.hFileInfo.ioDirID = 0;    cipb.hFileInfo.ioDirID = 0;
512    cipb.hFileInfo.ioFDirIndex = 0;    cipb.hFileInfo.ioFDirIndex = 0;
513      /* set to 0 to get information about specific dir or file */      /* set to 0 to get information about specific dir or file */
514      
515    errno = PBGetCatInfo (&cipb, false);    errno = PBGetCatInfo (&cipb, false);
516    if (errno != noErr)    if (errno != noErr)
517      return -1;      return -1;
# Line 544  utime (const char *path, const struct ut Line 550  utime (const char *path, const struct ut
550  int  int
551  access (const char *path, int mode)  access (const char *path, int mode)
552  {  {
553    char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];      char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];
554    int len;    int len;
555    char mac_pathname[MAXPATHLEN+1];    char mac_pathname[MAXPATHLEN+1];
556    CInfoPBRec cipb;    CInfoPBRec cipb;
557      
558    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)
559      return -1;      return -1;
560      
561    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);
562    if (len > -1)    if (len > -1)
563      fully_resolved_name[len] = '\0';      fully_resolved_name[len] = '\0';
# Line 567  access (const char *path, int mode) Line 573  access (const char *path, int mode)
573    cipb.hFileInfo.ioDirID = 0;    cipb.hFileInfo.ioDirID = 0;
574    cipb.hFileInfo.ioFDirIndex = 0;    cipb.hFileInfo.ioFDirIndex = 0;
575      /* set to 0 to get information about specific dir or file */      /* set to 0 to get information about specific dir or file */
576      
577    errno = PBGetCatInfo (&cipb, false);    errno = PBGetCatInfo (&cipb, false);
578    if (errno != noErr)    if (errno != noErr)
579      return -1;      return -1;
# Line 600  access (const char *path, int mode) Line 606  access (const char *path, int mode)
606  int  int
607  sys_open (const char *path, int oflag)  sys_open (const char *path, int oflag)
608  {  {
609    char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];      char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];
610    int len;    int len;
611    char mac_pathname[MAXPATHLEN+1];    char mac_pathname[MAXPATHLEN+1];
612            
613    if (strcmp (path, "/dev/null") == 0)    if (strcmp (path, "/dev/null") == 0)
614      return DEV_NULL_FD;  /* some bogus fd to be ignored in write */      return DEV_NULL_FD;  /* some bogus fd to be ignored in write */
615      
616    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)
617      return -1;      return -1;
618      
619    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);
620    if (len > -1)    if (len > -1)
621      fully_resolved_name[len] = '\0';      fully_resolved_name[len] = '\0';
# Line 637  sys_open (const char *path, int oflag) Line 643  sys_open (const char *path, int oflag)
643  int  int
644  sys_creat (const char *path, mode_t mode)  sys_creat (const char *path, mode_t mode)
645  {  {
646    char true_pathname[MAXPATHLEN+1];      char true_pathname[MAXPATHLEN+1];
647    int len;    int len;
648    char mac_pathname[MAXPATHLEN+1];    char mac_pathname[MAXPATHLEN+1];
649            
650    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)
651      return -1;      return -1;
652    
# Line 663  sys_creat (const char *path, mode_t mode Line 669  sys_creat (const char *path, mode_t mode
669  int  int
670  sys_unlink (const char *path)  sys_unlink (const char *path)
671  {  {
672    char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];      char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];
673    int len;    int len;
674    char mac_pathname[MAXPATHLEN+1];    char mac_pathname[MAXPATHLEN+1];
675            
676    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (path, true_pathname, MAXPATHLEN+1) == -1)
677      return -1;      return -1;
678      
679    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);
680    if (len > -1)    if (len > -1)
681      fully_resolved_name[len] = '\0';      fully_resolved_name[len] = '\0';
# Line 718  int Line 724  int
724  sys_rename (const char * old_name, const char * new_name)  sys_rename (const char * old_name, const char * new_name)
725  {  {
726    char true_old_pathname[MAXPATHLEN+1], true_new_pathname[MAXPATHLEN+1];    char true_old_pathname[MAXPATHLEN+1], true_new_pathname[MAXPATHLEN+1];
727    char fully_resolved_old_name[MAXPATHLEN+1];      char fully_resolved_old_name[MAXPATHLEN+1];
728    int len;    int len;
729    char mac_old_name[MAXPATHLEN+1], mac_new_name[MAXPATHLEN+1];    char mac_old_name[MAXPATHLEN+1], mac_new_name[MAXPATHLEN+1];
730            
731    if (find_true_pathname (old_name, true_old_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (old_name, true_old_pathname, MAXPATHLEN+1) == -1)
732      return -1;      return -1;
733      
734    len = readlink (true_old_pathname, fully_resolved_old_name, MAXPATHLEN);    len = readlink (true_old_pathname, fully_resolved_old_name, MAXPATHLEN);
735    if (len > -1)    if (len > -1)
736      fully_resolved_old_name[len] = '\0';      fully_resolved_old_name[len] = '\0';
# Line 733  sys_rename (const char * old_name, const Line 739  sys_rename (const char * old_name, const
739    
740    if (find_true_pathname (new_name, true_new_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (new_name, true_new_pathname, MAXPATHLEN+1) == -1)
741      return -1;      return -1;
742            
743    if (strcmp (fully_resolved_old_name, true_new_pathname) == 0)    if (strcmp (fully_resolved_old_name, true_new_pathname) == 0)
744      return 0;      return 0;
745    
# Line 741  sys_rename (const char * old_name, const Line 747  sys_rename (const char * old_name, const
747                               mac_old_name,                               mac_old_name,
748                               MAXPATHLEN+1))                               MAXPATHLEN+1))
749      return -1;      return -1;
750                    
751    if (!posix_to_mac_pathname(true_new_pathname, mac_new_name, MAXPATHLEN+1))    if (!posix_to_mac_pathname(true_new_pathname, mac_new_name, MAXPATHLEN+1))
752      return -1;      return -1;
753    
# Line 749  sys_rename (const char * old_name, const Line 755  sys_rename (const char * old_name, const
755       file in Unix.  CW version fails in these situation.  So we add a       file in Unix.  CW version fails in these situation.  So we add a
756       call to unlink here.  */       call to unlink here.  */
757    (void) unlink (mac_new_name);    (void) unlink (mac_new_name);
758      
759    return rename (mac_old_name, mac_new_name);    return rename (mac_old_name, mac_new_name);
760  }  }
761    
# Line 759  extern FILE *fopen (const char *name, co Line 765  extern FILE *fopen (const char *name, co
765  FILE *  FILE *
766  sys_fopen (const char *name, const char *mode)  sys_fopen (const char *name, const char *mode)
767  {  {
768    char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];      char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];
769    int len;    int len;
770    char mac_pathname[MAXPATHLEN+1];    char mac_pathname[MAXPATHLEN+1];
771            
772    if (find_true_pathname (name, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (name, true_pathname, MAXPATHLEN+1) == -1)
773      return 0;      return 0;
774      
775    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);
776    if (len > -1)    if (len > -1)
777      fully_resolved_name[len] = '\0';      fully_resolved_name[len] = '\0';
# Line 838  select (n,  rfds, wfds, efds, timeout) Line 844  select (n,  rfds, wfds, efds, timeout)
844    
845    EMACS_GET_TIME (end_time);    EMACS_GET_TIME (end_time);
846    EMACS_ADD_TIME (end_time, end_time, *timeout);    EMACS_ADD_TIME (end_time, end_time, *timeout);
847      
848    do    do
849      {      {
850        /* Also return true if an event other than a keyDown has        /* Also return true if an event other than a keyDown has
# Line 853  select (n,  rfds, wfds, efds, timeout) Line 859  select (n,  rfds, wfds, efds, timeout)
859        {        {
860          Point mouse_pos;          Point mouse_pos;
861          static Point old_mouse_pos = {-1, -1};          static Point old_mouse_pos = {-1, -1};
862            
863          GetMouse (&mouse_pos);          GetMouse (&mouse_pos);
864          if (!EqualPt (mouse_pos, old_mouse_pos))          if (!EqualPt (mouse_pos, old_mouse_pos))
865            {            {
# Line 861  select (n,  rfds, wfds, efds, timeout) Line 867  select (n,  rfds, wfds, efds, timeout)
867              return 1;              return 1;
868            }            }
869        }        }
870          
871        WaitNextEvent (0, &e, 1UL, NULL); /* Accept no event; wait 1        WaitNextEvent (0, &e, 1UL, NULL); /* Accept no event; wait 1
872                                             tic. by T.I. */                                             tic. by T.I. */
873          
874        EMACS_GET_TIME (now);        EMACS_GET_TIME (now);
875        EMACS_SUB_TIME (now, end_time, now);        EMACS_SUB_TIME (now, end_time, now);
876      }      }
# Line 882  pause () Line 888  pause ()
888  {  {
889    EventRecord e;    EventRecord e;
890    unsigned long tick;    unsigned long tick;
891      
892    if (!target_ticks)  /* no alarm pending */    if (!target_ticks)  /* no alarm pending */
893      return -1;      return -1;
894    
895    if ((tick = TickCount ()) < target_ticks)    if ((tick = TickCount ()) < target_ticks)
896      WaitNextEvent (0, &e, target_ticks - tick, NULL); /* Accept no event;      WaitNextEvent (0, &e, target_ticks - tick, NULL); /* Accept no event;
897                                                           just wait. by T.I. */                                                           just wait. by T.I. */
898      
899    target_ticks = 0;    target_ticks = 0;
900    if (alarm_signal_func)    if (alarm_signal_func)
901      (*alarm_signal_func)(SIGALRM);      (*alarm_signal_func)(SIGALRM);
902      
903    return 0;    return 0;
904  }  }
905    
# Line 902  int Line 908  int
908  alarm (int seconds)  alarm (int seconds)
909  {  {
910    long remaining = target_ticks ? (TickCount () - target_ticks) / 60 : 0;    long remaining = target_ticks ? (TickCount () - target_ticks) / 60 : 0;
911            
912    target_ticks = seconds ? TickCount () + 60 * seconds : 0;    target_ticks = seconds ? TickCount () + 60 * seconds : 0;
913            
914    return (remaining < 0) ? 0 : (unsigned int) remaining;    return (remaining < 0) ? 0 : (unsigned int) remaining;
915  }  }
916    
# Line 927  sys_signal (int signal_num, __signal_fun Line 933  sys_signal (int signal_num, __signal_fun
933    else    else
934      {      {
935  #ifdef __MRC__  #ifdef __MRC__
936        __sigfun old_signal_func;                __sigfun old_signal_func;
937  #elif __MWERKS__  #elif __MWERKS__
938        __signal_func_ptr old_signal_func;                        __signal_func_ptr old_signal_func;
939  #else  #else
940        You lose!!!        You lose!!!
941  #endif  #endif
# Line 974  gettimeofday (tp) Line 980  gettimeofday (tp)
980    
981    /* Get time since boot */    /* Get time since boot */
982    Microseconds (&uw_microseconds);    Microseconds (&uw_microseconds);
983      
984    /* Convert to time since midnight*/    /* Convert to time since midnight*/
985    w_microseconds.hi = uw_microseconds.hi;    w_microseconds.hi = uw_microseconds.hi;
986    w_microseconds.lo = uw_microseconds.lo;    w_microseconds.lo = uw_microseconds.lo;
# Line 1014  struct tm * Line 1020  struct tm *
1020  sys_gmtime (const time_t *timer)  sys_gmtime (const time_t *timer)
1021  {  {
1022    time_t unix_time = *timer + CW_OR_MPW_UNIX_EPOCH_DIFF;    time_t unix_time = *timer + CW_OR_MPW_UNIX_EPOCH_DIFF;
1023      
1024    return gmtime (&unix_time);    return gmtime (&unix_time);
1025  }  }
1026    
# Line 1029  sys_localtime (const time_t *timer) Line 1035  sys_localtime (const time_t *timer)
1035  #else  #else
1036    time_t unix_time = *timer + CW_OR_MPW_UNIX_EPOCH_DIFF;    time_t unix_time = *timer + CW_OR_MPW_UNIX_EPOCH_DIFF;
1037  #endif  #endif
1038      
1039    return localtime (&unix_time);    return localtime (&unix_time);
1040  }  }
1041    
# Line 1044  sys_ctime (const time_t *timer) Line 1050  sys_ctime (const time_t *timer)
1050  #else  #else
1051    time_t unix_time = *timer + CW_OR_MPW_UNIX_EPOCH_DIFF;    time_t unix_time = *timer + CW_OR_MPW_UNIX_EPOCH_DIFF;
1052  #endif  #endif
1053      
1054    return ctime (&unix_time);    return ctime (&unix_time);
1055  }  }
1056    
# Line 1062  sys_time (time_t *timer) Line 1068  sys_time (time_t *timer)
1068    
1069    if (timer)    if (timer)
1070      *timer = mac_time;      *timer = mac_time;
1071        
1072    return mac_time;    return mac_time;
1073  }  }
1074    
# Line 1125  mktemp (char *template) Line 1131  mktemp (char *template)
1131  {  {
1132    int len, k;    int len, k;
1133    static seqnum = 0;    static seqnum = 0;
1134      
1135    len = strlen (template);    len = strlen (template);
1136    k = len - 1;    k = len - 1;
1137    while (k >= 0 && template[k] == 'X')    while (k >= 0 && template[k] == 'X')
1138      k--;      k--;
1139      
1140    k++;  /* make k index of first 'X' */    k++;  /* make k index of first 'X' */
1141      
1142    if (k < len)    if (k < len)
1143      {      {
1144        /* Zero filled, number of digits equal to the number of X's.  */        /* Zero filled, number of digits equal to the number of X's.  */
1145        sprintf (&template[k], "%0*d", len-k, seqnum++);        sprintf (&template[k], "%0*d", len-k, seqnum++);
1146      
1147        return template;        return template;
1148      }      }
1149    else    else
1150      return 0;        return 0;
1151  }  }
1152    
1153    
# Line 1152  mktemp (char *template) Line 1158  mktemp (char *template)
1158  static char my_passwd_name[PASSWD_FIELD_SIZE];  static char my_passwd_name[PASSWD_FIELD_SIZE];
1159  static char my_passwd_dir[MAXPATHLEN+1];  static char my_passwd_dir[MAXPATHLEN+1];
1160    
1161  static struct passwd my_passwd =  static struct passwd my_passwd =
1162  {  {
1163    my_passwd_name,    my_passwd_name,
1164    my_passwd_dir,    my_passwd_dir,
# Line 1196  init_emacs_passwd_dir () Line 1202  init_emacs_passwd_dir ()
1202              }              }
1203          }          }
1204      }      }
1205      
1206    if (!found)    if (!found)
1207      {      {
1208        /* Setting to "/" probably won't work but set it to something        /* Setting to "/" probably won't work but set it to something
# Line 1207  init_emacs_passwd_dir () Line 1213  init_emacs_passwd_dir ()
1213  }  }
1214    
1215    
1216  static struct passwd emacs_passwd =  static struct passwd emacs_passwd =
1217  {  {
1218    "emacs",    "emacs",
1219    emacs_passwd_dir,    emacs_passwd_dir,
# Line 1243  struct passwd * Line 1249  struct passwd *
1249  getpwuid (uid_t uid)  getpwuid (uid_t uid)
1250  {  {
1251    if (!my_passwd_inited)    if (!my_passwd_inited)
1252      {        {
1253        init_my_passwd ();        init_my_passwd ();
1254        my_passwd_inited = 1;        my_passwd_inited = 1;
1255      }      }
1256      
1257    return &my_passwd;    return &my_passwd;
1258  }  }
1259    
# Line 1259  getpwnam (const char *name) Line 1265  getpwnam (const char *name)
1265          return &emacs_passwd;          return &emacs_passwd;
1266    
1267    if (!my_passwd_inited)    if (!my_passwd_inited)
1268      {        {
1269        init_my_passwd ();        init_my_passwd ();
1270        my_passwd_inited = 1;        my_passwd_inited = 1;
1271      }      }
1272      
1273    return &my_passwd;    return &my_passwd;
1274  }  }
1275    
# Line 1305  int Line 1311  int
1311  sigblock (int mask)  sigblock (int mask)
1312  {  {
1313    return 0;    return 0;
1314  }  }
1315    
1316    
1317  void  void
# Line 1387  path_from_vol_dir_name (char *path, int Line 1393  path_from_vol_dir_name (char *path, int
1393        err = PBGetCatInfo (&cipb, false);        err = PBGetCatInfo (&cipb, false);
1394        if (err != noErr)        if (err != noErr)
1395          return 0;          return 0;
1396          
1397        p2cstr (dir_name);        p2cstr (dir_name);
1398        if (strlen (dir_name) + strlen (path) + 1 >= man_path_len)        if (strlen (dir_name) + strlen (path) + 1 >= man_path_len)
1399          return 0;          return 0;
# Line 1399  path_from_vol_dir_name (char *path, int Line 1405  path_from_vol_dir_name (char *path, int
1405      }      }
1406    while (cipb.dirInfo.ioDrDirID != fsRtDirID);    while (cipb.dirInfo.ioDrDirID != fsRtDirID);
1407      /* stop when we see the volume's root directory */      /* stop when we see the volume's root directory */
1408      
1409    return 1;  /* success */    return 1;  /* success */
1410  }  }
1411    
# Line 1463  find_true_pathname (const char *path, ch Line 1469  find_true_pathname (const char *path, ch
1469      return -1;      return -1;
1470    
1471    buf[0] = '\0';    buf[0] = '\0';
1472      
1473    p = path;    p = path;
1474    if (*p == '/')    if (*p == '/')
1475      q = strchr (p + 1, '/');      q = strchr (p + 1, '/');
# Line 1487  find_true_pathname (const char *path, ch Line 1493  find_true_pathname (const char *path, ch
1493        p = q + 1;        p = q + 1;
1494        q = strchr(p, '/');        q = strchr(p, '/');
1495      }      }
1496      
1497    if (len + strlen (p) + 1 >= bufsiz)    if (len + strlen (p) + 1 >= bufsiz)
1498      return -1;      return -1;
1499      
1500    strcat (buf, p);    strcat (buf, p);
1501    return len + strlen (p);    return len + strlen (p);
1502  }  }
# Line 1538  int Line 1544  int
1544  dup2 (int oldd, int newd)  dup2 (int oldd, int newd)
1545  {  {
1546    int fd, ret;    int fd, ret;
1547      
1548    close (newd);    close (newd);
1549    
1550    fd = dup (oldd);    fd = dup (oldd);
# Line 1649  get_temp_dir_name () Line 1655  get_temp_dir_name ()
1655    CInfoPBRec cpb;    CInfoPBRec cpb;
1656    char unix_dir_name[MAXPATHLEN+1];    char unix_dir_name[MAXPATHLEN+1];
1657    DIR *dir;    DIR *dir;
1658      
1659    /* Cache directory name with pointer temp_dir_name.    /* Cache directory name with pointer temp_dir_name.
1660       Look for it only the first time.  */       Look for it only the first time.  */
1661    if (!temp_dir_name)    if (!temp_dir_name)
# Line 1658  get_temp_dir_name () Line 1664  get_temp_dir_name ()
1664                          &vol_ref_num, &dir_id);                          &vol_ref_num, &dir_id);
1665        if (err != noErr)        if (err != noErr)
1666          return NULL;          return NULL;
1667          
1668        if (!path_from_vol_dir_name (full_path, 255, vol_ref_num, dir_id, "\p"))        if (!path_from_vol_dir_name (full_path, 255, vol_ref_num, dir_id, "\p"))
1669          return NULL;          return NULL;
1670    
1671        if (strlen (full_path) + 6 <= MAXPATHLEN)        if (strlen (full_path) + 6 <= MAXPATHLEN)
1672          strcat (full_path, "Emacs:");          strcat (full_path, "Emacs:");
1673        else        else
1674          return NULL;          return NULL;
1675    
1676        if (!mac_to_posix_pathname (full_path, unix_dir_name, MAXPATHLEN+1))        if (!mac_to_posix_pathname (full_path, unix_dir_name, MAXPATHLEN+1))
1677          return NULL;          return NULL;
1678        
1679        dir = opendir (unix_dir_name);  /* check whether temp directory exists */        dir = opendir (unix_dir_name);  /* check whether temp directory exists */
1680        if (dir)        if (dir)
1681          closedir (dir);          closedir (dir);
# Line 1688  get_temp_dir_name () Line 1694  get_temp_dir_name ()
1694  /* Allocate and construct an array of pointers to strings from a list  /* Allocate and construct an array of pointers to strings from a list
1695     of strings stored in a 'STR#' resource.  The returned pointer array     of strings stored in a 'STR#' resource.  The returned pointer array
1696     is stored in the style of argv and environ: if the 'STR#' resource     is stored in the style of argv and environ: if the 'STR#' resource
1697     contains numString strings, an pointer array with numString+1     contains numString strings, a pointer array with numString+1
1698     elements is returned in which the last entry contains a null     elements is returned in which the last entry contains a null
1699     pointer.  The pointer to the pointer array is passed by pointer in     pointer.  The pointer to the pointer array is passed by pointer in
1700     parameter t.  The resource ID of the 'STR#' resource is passed in     parameter t.  The resource ID of the 'STR#' resource is passed in
# Line 1742  get_path_to_system_folder () Line 1748  get_path_to_system_folder ()
1748    CInfoPBRec cpb;    CInfoPBRec cpb;
1749    static char system_folder_unix_name[MAXPATHLEN+1];    static char system_folder_unix_name[MAXPATHLEN+1];
1750    DIR *dir;    DIR *dir;
1751      
1752    err = FindFolder (kOnSystemDisk, kSystemFolderType, kDontCreateFolder,    err = FindFolder (kOnSystemDisk, kSystemFolderType, kDontCreateFolder,
1753                      &vol_ref_num, &dir_id);                      &vol_ref_num, &dir_id);
1754    if (err != noErr)    if (err != noErr)
1755      return NULL;      return NULL;
1756          
1757    if (!path_from_vol_dir_name (full_path, 255, vol_ref_num, dir_id, "\p"))    if (!path_from_vol_dir_name (full_path, 255, vol_ref_num, dir_id, "\p"))
1758      return NULL;      return NULL;
1759    
1760    if (!mac_to_posix_pathname (full_path, system_folder_unix_name,    if (!mac_to_posix_pathname (full_path, system_folder_unix_name,
1761                                MAXPATHLEN+1))                                MAXPATHLEN+1))
1762      return NULL;      return NULL;
1763        
1764    return system_folder_unix_name;    return system_folder_unix_name;
1765  }  }
1766    
# Line 1769  void Line 1775  void
1775  init_environ ()  init_environ ()
1776  {  {
1777    int i;    int i;
1778      
1779    get_string_list (&environ, ENVIRON_STRING_LIST_ID);    get_string_list (&environ, ENVIRON_STRING_LIST_ID);
1780    
1781    i = 0;    i = 0;
# Line 1913  mystrchr (char *s, char c) Line 1919  mystrchr (char *s, char c)
1919    
1920  char *  char *
1921  mystrtok (char *s)  mystrtok (char *s)
1922  {        {
1923    while (*s)    while (*s)
1924      s++;      s++;
1925    
# Line 1969  run_mac_command (argv, workdir, infn, ou Line 1975  run_mac_command (argv, workdir, infn, ou
1975    RgnHandle cursor_region_handle;    RgnHandle cursor_region_handle;
1976    TargetID targ;    TargetID targ;
1977    unsigned long ref_con, len;    unsigned long ref_con, len;
1978            
1979    if (posix_to_mac_pathname (workdir, macworkdir, MAXPATHLEN+1) == 0)    if (posix_to_mac_pathname (workdir, macworkdir, MAXPATHLEN+1) == 0)
1980      return -1;      return -1;
1981    if (posix_to_mac_pathname (infn, macinfn, MAXPATHLEN+1) == 0)    if (posix_to_mac_pathname (infn, macinfn, MAXPATHLEN+1) == 0)
# Line 1978  run_mac_command (argv, workdir, infn, ou Line 1984  run_mac_command (argv, workdir, infn, ou
1984      return -1;      return -1;
1985    if (posix_to_mac_pathname (errfn, macerrfn, MAXPATHLEN+1) == 0)    if (posix_to_mac_pathname (errfn, macerrfn, MAXPATHLEN+1) == 0)
1986      return -1;      return -1;
1987      
1988    paramlen = strlen (macworkdir) + strlen (macinfn) + strlen (macoutfn)    paramlen = strlen (macworkdir) + strlen (macinfn) + strlen (macoutfn)
1989               + strlen (macerrfn) + 4;  /* count nulls at end of strings */               + strlen (macerrfn) + 4;  /* count nulls at end of strings */
1990    
# Line 1997  run_mac_command (argv, workdir, infn, ou Line 2003  run_mac_command (argv, workdir, infn, ou
2003        && argc == 3 && strcmp (argv[1], "-c") == 0)        && argc == 3 && strcmp (argv[1], "-c") == 0)
2004      {      {
2005        char *command, *t, tempmacpathname[MAXPATHLEN+1];        char *command, *t, tempmacpathname[MAXPATHLEN+1];
2006        
2007        /* The arguments for the command in argv[2] are separated by        /* The arguments for the command in argv[2] are separated by
2008           spaces.  Count them and put the count in newargc.  */           spaces.  Count them and put the count in newargc.  */
2009        command = (char *) alloca (strlen (argv[2])+2);        command = (char *) alloca (strlen (argv[2])+2);
2010        strcpy (command, argv[2]);        strcpy (command, argv[2]);
2011        if (command[strlen (command) - 1] != ' ')        if (command[strlen (command) - 1] != ' ')
2012          strcat (command, " ");          strcat (command, " ");
2013        
2014        t = command;        t = command;
2015        newargc = 0;        newargc = 0;
2016        t = mystrchr (t, ' ');        t = mystrchr (t, ' ');
# Line 2013  run_mac_command (argv, workdir, infn, ou Line 2019  run_mac_command (argv, workdir, infn, ou
2019            newargc++;            newargc++;
2020            t = mystrchr (t+1, ' ');            t = mystrchr (t+1, ' ');
2021          }          }
2022        
2023        newargv = (char **) alloca (sizeof (char *) * newargc);        newargv = (char **) alloca (sizeof (char *) * newargc);
2024        
2025        t = command;        t = command;
2026        for (j = 0; j < newargc; j++)        for (j = 0; j < newargc; j++)
2027          {          {
# Line 2025  run_mac_command (argv, workdir, infn, ou Line 2031  run_mac_command (argv, workdir, infn, ou
2031            t = mystrtok (t);            t = mystrtok (t);
2032            paramlen += strlen (newargv[j]) + 1;            paramlen += strlen (newargv[j]) + 1;
2033          }          }
2034        
2035        if (strncmp (newargv[0], "~emacs/", 7) == 0)        if (strncmp (newargv[0], "~emacs/", 7) == 0)
2036          {          {
2037            if (posix_to_mac_pathname (newargv[0], tempmacpathname, MAXPATHLEN+1)            if (posix_to_mac_pathname (newargv[0], tempmacpathname, MAXPATHLEN+1)
# Line 2045  run_mac_command (argv, workdir, infn, ou Line 2051  run_mac_command (argv, workdir, infn, ou
2051    
2052            if (NILP (path))            if (NILP (path))
2053              return -1;              return -1;
2054            if (posix_to_mac_pathname (XSTRING (path)->data, tempmacpathname,            if (posix_to_mac_pathname (SDATA (path), tempmacpathname,
2055                                      MAXPATHLEN+1) == 0)                                      MAXPATHLEN+1) == 0)
2056              return -1;              return -1;
2057          }          }
2058        strcpy (macappname, tempmacpathname);        strcpy (macappname, tempmacpathname);
2059      }      }
2060    else    else
2061      {            {
2062        if (posix_to_mac_pathname (argv[0], macappname, MAXPATHLEN+1) == 0)        if (posix_to_mac_pathname (argv[0], macappname, MAXPATHLEN+1) == 0)
2063          return -1;          return -1;
2064    
2065        newargv = (char **) alloca (sizeof (char *) * argc);        newargv = (char **) alloca (sizeof (char *) * argc);
2066        newargc = argc;          newargc = argc;
2067        for (j = 1; j < argc; j++)        for (j = 1; j < argc; j++)
2068          {          {
2069            if (strncmp (argv[j], "~emacs/", 7) == 0)            if (strncmp (argv[j], "~emacs/", 7) == 0)
# Line 2087  run_mac_command (argv, workdir, infn, ou Line 2093  run_mac_command (argv, workdir, infn, ou
2093                  }                  }
2094              }              }
2095            else            else
2096              newargv[j] = argv[j];                newargv[j] = argv[j];
2097            paramlen += strlen (newargv[j]) + 1;            paramlen += strlen (newargv[j]) + 1;
2098          }          }
2099      }      }
# Line 2108  run_mac_command (argv, workdir, infn, ou Line 2114  run_mac_command (argv, workdir, infn, ou
2114      /* null terminate strings sent so it's possible to use strcpy over there */      /* null terminate strings sent so it's possible to use strcpy over there */
2115    strcpy (p, macinfn);    strcpy (p, macinfn);
2116    p += strlen (macinfn);    p += strlen (macinfn);
2117    *p++ = '\0';      *p++ = '\0';
2118    strcpy (p, macoutfn);    strcpy (p, macoutfn);
2119    p += strlen (macoutfn);    p += strlen (macoutfn);
2120    *p++ = '\0';      *p++ = '\0';
2121    strcpy (p, macerrfn);    strcpy (p, macerrfn);
2122    p += strlen (macerrfn);    p += strlen (macerrfn);
2123    *p++ = '\0';      *p++ = '\0';
2124    for (j = 1; j < newargc; j++)    for (j = 1; j < newargc; j++)
2125      {      {
2126        strcpy (p, newargv[j]);        strcpy (p, newargv[j]);
2127        p += strlen (newargv[j]);        p += strlen (newargv[j]);
2128        *p++ = '\0';          *p++ = '\0';
2129      }      }
2130      
2131    c2pstr (macappname);    c2pstr (macappname);
2132      
2133    iErr = FSMakeFSSpec (0, 0, macappname, &spec);    iErr = FSMakeFSSpec (0, 0, macappname, &spec);
2134      
2135    if (iErr != noErr)    if (iErr != noErr)
2136      {      {
2137        free (param);        free (param);
# Line 2166  run_mac_command (argv, workdir, infn, ou Line 2172  run_mac_command (argv, workdir, infn, ou
2172      }      }
2173    
2174    cursor_region_handle = NewRgn ();    cursor_region_handle = NewRgn ();
2175            
2176    /* Wait for the subprocess to finish, when it will send us a ERPY    /* Wait for the subprocess to finish, when it will send us a ERPY
2177       high level event.  */       high level event.  */
2178    while (1)    while (1)
# Line 2174  run_mac_command (argv, workdir, infn, ou Line 2180  run_mac_command (argv, workdir, infn, ou
2180                         cursor_region_handle)                         cursor_region_handle)
2181          && reply_event.message == kEmacsSubprocessReply)          && reply_event.message == kEmacsSubprocessReply)
2182        break;        break;
2183      
2184    /* The return code is sent through the refCon */    /* The return code is sent through the refCon */
2185    iErr = AcceptHighLevelEvent (&targ, &ref_con, NULL, &len);    iErr = AcceptHighLevelEvent (&targ, &ref_con, NULL, &len);
2186    if (iErr != noErr)    if (iErr != noErr)
# Line 2183  run_mac_command (argv, workdir, infn, ou Line 2189  run_mac_command (argv, workdir, infn, ou
2189        free (param);        free (param);
2190        return -1;        return -1;
2191      }      }
2192      
2193    DisposeHandle ((Handle) cursor_region_handle);    DisposeHandle ((Handle) cursor_region_handle);
2194    free (param);    free (param);
2195    
# Line 2195  run_mac_command (argv, workdir, infn, ou Line 2201  run_mac_command (argv, workdir, infn, ou
2201  DIR *  DIR *
2202  opendir (const char *dirname)  opendir (const char *dirname)
2203  {  {
2204    char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];      char true_pathname[MAXPATHLEN+1], fully_resolved_name[MAXPATHLEN+1];
2205    char mac_pathname[MAXPATHLEN+1], vol_name[MAXPATHLEN+1];    char mac_pathname[MAXPATHLEN+1], vol_name[MAXPATHLEN+1];
2206    DIR *dirp;    DIR *dirp;
2207    CInfoPBRec cipb;    CInfoPBRec cipb;
2208    HVolumeParam vpb;    HVolumeParam vpb;
2209    int len, vol_name_len;    int len, vol_name_len;
2210            
2211    if (find_true_pathname (dirname, true_pathname, MAXPATHLEN+1) == -1)    if (find_true_pathname (dirname, true_pathname, MAXPATHLEN+1) == -1)
2212      return 0;      return 0;
2213      
2214    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);    len = readlink (true_pathname, fully_resolved_name, MAXPATHLEN);
2215    if (len > -1)    if (len > -1)
2216      fully_resolved_name[len] = '\0';      fully_resolved_name[len] = '\0';
# Line 2232  opendir (const char *dirname) Line 2238  opendir (const char *dirname)
2238    len = strlen (mac_pathname);    len = strlen (mac_pathname);
2239    if (mac_pathname[len - 1] != ':' && len < MAXPATHLEN)    if (mac_pathname[len - 1] != ':' && len < MAXPATHLEN)
2240      strcat (mac_pathname, ":");      strcat (mac_pathname, ":");
2241      
2242    /* Extract volume name */    /* Extract volume name */
2243    vol_name_len = strchr (mac_pathname, ':') - mac_pathname;    vol_name_len = strchr (mac_pathname, ':') - mac_pathname;
2244    strncpy (vol_name, mac_pathname, vol_name_len);    strncpy (vol_name, mac_pathname, vol_name_len);
# Line 2246  opendir (const char *dirname) Line 2252  opendir (const char *dirname)
2252    cipb.hFileInfo.ioDirID = 0;    cipb.hFileInfo.ioDirID = 0;
2253    cipb.hFileInfo.ioFDirIndex = 0;    cipb.hFileInfo.ioFDirIndex = 0;
2254      /* set to 0 to get information about specific dir or file */      /* set to 0 to get information about specific dir or file */
2255      
2256    errno = PBGetCatInfo (&cipb, false);    errno = PBGetCatInfo (&cipb, false);
2257    if (errno != noErr)    if (errno != noErr)
2258      {      {
# Line 2274  opendir (const char *dirname) Line 2280  opendir (const char *dirname)
2280      }      }
2281    
2282    dirp->vol_ref_num = vpb.ioVRefNum;    dirp->vol_ref_num = vpb.ioVRefNum;
2283      
2284    return dirp;    return dirp;
2285  }  }
2286    
# Line 2307  readdir (DIR *dp) Line 2313  readdir (DIR *dp)
2313        hpblock.volumeParam.ioNamePtr = s_name;        hpblock.volumeParam.ioNamePtr = s_name;
2314        hpblock.volumeParam.ioVRefNum = 0;        hpblock.volumeParam.ioVRefNum = 0;
2315        hpblock.volumeParam.ioVolIndex = dp->current_index;        hpblock.volumeParam.ioVolIndex = dp->current_index;
2316                    
2317        errno = PBHGetVInfo (&hpblock, false);        errno = PBHGetVInfo (&hpblock, false);
2318        if (errno != noErr)        if (errno != noErr)
2319          {          {
2320            errno = ENOENT;            errno = ENOENT;
2321            return 0;            return 0;
2322          }          }
2323                            
2324        p2cstr (s_name);        p2cstr (s_name);
2325        strcat (s_name, "/");  /* need "/" for stat to work correctly */        strcat (s_name, "/");  /* need "/" for stat to work correctly */
2326    
# Line 2322  readdir (DIR *dp) Line 2328  readdir (DIR *dp)
2328    
2329        s_dirent.d_ino = hpblock.volumeParam.ioVRefNum;        s_dirent.d_ino = hpblock.volumeParam.ioVRefNum;
2330        s_dirent.d_name = s_name;        s_dirent.d_name = s_name;
2331      
2332        return &s_dirent;        return &s_dirent;
2333      }      }
2334    else    else
# Line 2338  readdir (DIR *dp) Line 2344  readdir (DIR *dp)
2344            cipb.hFileInfo.ioDirID = dp->dir_id;            cipb.hFileInfo.ioDirID = dp->dir_id;
2345              /* directory ID found by opendir */              /* directory ID found by opendir */
2346            cipb.hFileInfo.ioFDirIndex = dp->current_index;            cipb.hFileInfo.ioFDirIndex = dp->current_index;
2347              
2348            errno = PBGetCatInfo (&cipb, false);            errno = PBGetCatInfo (&cipb, false);
2349            if (errno != noErr)            if (errno != noErr)
2350              {              {
2351                errno = ENOENT;                errno = ENOENT;
2352                return 0;                return 0;
2353              }              }
2354              
2355            /* insist on an visibile entry */            /* insist on a visible entry */
2356            if (cipb.hFileInfo.ioFlAttrib & 0x10)  /* directory? */            if (cipb.hFileInfo.ioFlAttrib & 0x10)  /* directory? */
2357              done = !(cipb.dirInfo.ioDrUsrWds.frFlags & fInvisible);              done = !(cipb.dirInfo.ioDrUsrWds.frFlags & fInvisible);
2358            else            else
2359              done = !(cipb.hFileInfo.ioFlFndrInfo.fdFlags & fInvisible);              done = !(cipb.hFileInfo.ioFlFndrInfo.fdFlags & fInvisible);
2360              
2361            dp->current_index++;            dp->current_index++;
2362          }          }
2363    
2364        p2cstr (s_name);        p2cstr (s_name);
2365          
2366        p = s_name;        p = s_name;
2367        while (*p)        while (*p)
2368          {          {
# Line 2368  readdir (DIR *dp) Line 2374  readdir (DIR *dp)
2374        s_dirent.d_ino = cipb.dirInfo.ioDrDirID;        s_dirent.d_ino = cipb.dirInfo.ioDrDirID;
2375          /* value unimportant: non-zero for valid file */          /* value unimportant: non-zero for valid file */
2376        s_dirent.d_name = s_name;        s_dirent.d_name = s_name;
2377      
2378        return &s_dirent;        return &s_dirent;
2379      }      }
2380  }  }
# Line 2399  initialize_applescript () Line 2405  initialize_applescript ()
2405  {  {
2406    AEDesc null_desc;    AEDesc null_desc;
2407    OSAError osaerror;    OSAError osaerror;
2408      
2409    /* if open fails, as_scripting_component is set to NULL.  Its    /* if open fails, as_scripting_component is set to NULL.  Its
2410       subsequent use in OSA calls will fail with badComponentInstance       subsequent use in OSA calls will fail with badComponentInstance
2411       error.  */       error.  */
# Line 2443  do_applescript (char *script, char **res Line 2449  do_applescript (char *script, char **res
2449    
2450    *result = 0;    *result = 0;
2451    
2452      if (!as_scripting_component)
2453        initialize_applescript();
2454    
2455    error = AECreateDesc (typeChar, script, strlen(script), &script_desc);    error = AECreateDesc (typeChar, script, strlen(script), &script_desc);
2456    if (error)    if (error)
2457      return error;      return error;
# Line 2499  do_applescript (char *script, char **res Line 2508  do_applescript (char *script, char **res
2508          }          }
2509        HUnlock (result_desc.dataHandle);        HUnlock (result_desc.dataHandle);
2510  #endif /* not TARGET_API_MAC_CARBON */  #endif /* not TARGET_API_MAC_CARBON */
2511          AEDisposeDesc (&result_desc);
2512      }      }
2513    
2514    AEDisposeDesc (&script_desc);    AEDisposeDesc (&script_desc);
   AEDisposeDesc (&result_desc);      
2515    
2516    return osaerror;    return osaerror;
2517  }  }
# Line 2522  component.  */) Line 2531  component.  */)
2531    long status;    long status;
2532    
2533    CHECK_STRING (script);    CHECK_STRING (script);
2534      
2535    status = do_applescript (XSTRING (script)->data, &result);    status = do_applescript (SDATA (script), &result);
2536    if (status)    if (status)
2537      {      {
2538        if (!result)        if (!result)
2539          error ("AppleScript error %ld", status);          error ("AppleScript error %d", status);
2540        else        else
2541          {          {
2542            /* Unfortunately only OSADoScript in do_applescript knows how            /* Unfortunately only OSADoScript in do_applescript knows how
# Line 2559  DEFUN ("mac-file-name-to-posix", Fmac_fi Line 2568  DEFUN ("mac-file-name-to-posix", Fmac_fi
2568    char posix_filename[MAXPATHLEN+1];    char posix_filename[MAXPATHLEN+1];
2569    
2570    CHECK_STRING (mac_filename);    CHECK_STRING (mac_filename);
2571      
2572    if (mac_to_posix_pathname (XSTRING (mac_filename)->data, posix_filename,    if (mac_to_posix_pathname (SDATA (mac_filename), posix_filename,
2573                             MAXPATHLEN))                             MAXPATHLEN))
2574      return build_string (posix_filename);      return build_string (posix_filename);
2575    else    else
# Line 2577  DEFUN ("posix-file-name-to-mac", Fposix_ Line 2586  DEFUN ("posix-file-name-to-mac", Fposix_
2586    char mac_filename[MAXPATHLEN+1];    char mac_filename[MAXPATHLEN+1];
2587    
2588    CHECK_STRING (posix_filename);    CHECK_STRING (posix_filename);
2589      
2590    if (posix_to_mac_pathname (XSTRING (posix_filename)->data, mac_filename,    if (posix_to_mac_pathname (SDATA (posix_filename), mac_filename,
2591                             MAXPATHLEN))                             MAXPATHLEN))
2592      return build_string (mac_filename);      return build_string (mac_filename);
2593    else    else
# Line 2614  DEFUN ("mac-paste-function", Fmac_paste_ Line 2623  DEFUN ("mac-paste-function", Fmac_paste_
2623    if (GetScrapFlavorData (scrap, kScrapFlavorTypeText, &s, data) != noErr    if (GetScrapFlavorData (scrap, kScrapFlavorTypeText, &s, data) != noErr
2624        || s == 0)        || s == 0)
2625      return Qnil;      return Qnil;
2626      
2627    /* Emacs expects clipboard contents have Unix-style eol's */    /* Emacs expects clipboard contents have Unix-style eol's */
2628    for (i = 0; i < s; i++)    for (i = 0; i < s; i++)
2629      if (data[i] == '\r')      if (data[i] == '\r')
# Line 2642  DEFUN ("mac-paste-function", Fmac_paste_ Line 2651  DEFUN ("mac-paste-function", Fmac_paste_
2651    value = make_string (*my_handle, rc);    value = make_string (*my_handle, rc);
2652    
2653    HUnlock (my_handle);    HUnlock (my_handle);
2654      
2655    DisposeHandle (my_handle);    DisposeHandle (my_handle);
2656    
2657    return value;    return value;
# Line 2663  DEFUN ("mac-cut-function", Fmac_cut_func Line 2672  DEFUN ("mac-cut-function", Fmac_cut_func
2672    /* fixme: ignore the push flag for now */    /* fixme: ignore the push flag for now */
2673    
2674    CHECK_STRING (value);    CHECK_STRING (value);
2675      
2676    len = XSTRING (value)->size;    len = SCHARS (value);
2677    buf = (char *) alloca (len+1);    buf = (char *) alloca (len+1);
2678    bcopy (XSTRING (value)->data, buf, len);    bcopy (SDATA (value), buf, len);
2679    buf[len] = '\0';    buf[len] = '\0';
2680      
2681    /* convert to Mac-style eol's before sending to clipboard */    /* convert to Mac-style eol's before sending to clipboard */
2682    for (i = 0; i < len; i++)    for (i = 0; i < len; i++)
2683      if (buf[i] == '\n')      if (buf[i] == '\n')
# Line 2689  DEFUN ("mac-cut-function", Fmac_cut_func Line 2698  DEFUN ("mac-cut-function", Fmac_cut_func
2698    ZeroScrap ();    ZeroScrap ();
2699    PutScrap (len, 'TEXT', buf);    PutScrap (len, 'TEXT', buf);
2700  #endif /* not TARGET_API_MAC_CARBON */  #endif /* not TARGET_API_MAC_CARBON */
2701      
2702    return Qnil;    return Qnil;
2703  }  }
2704    
# Line 2739  and t is the same as `SECONDARY'.  */) Line 2748  and t is the same as `SECONDARY'.  */)
2748    return Qnil;    return Qnil;
2749  }  }
2750    
2751    #ifdef MAC_OSX
2752    #undef select
2753    
2754    extern int inhibit_window_system;
2755    extern int noninteractive;
2756    
2757    /* When Emacs is started from the Finder, SELECT always immediately
2758       returns as if input is present when file descriptor 0 is polled for
2759       input.  Strangely, when Emacs is run as a GUI application from the
2760       command line, it blocks in the same situation.  This `wrapper' of
2761       the system call SELECT corrects this discrepancy.  */
2762    int
2763    sys_select (n, rfds, wfds, efds, timeout)
2764      int n;
2765      SELECT_TYPE *rfds;
2766      SELECT_TYPE *wfds;
2767      SELECT_TYPE *efds;
2768      struct timeval *timeout;
2769    {
2770      if (!inhibit_window_system && rfds && FD_ISSET (0, rfds))
2771        return 1;
2772      else if (inhibit_window_system || noninteractive ||
2773               (timeout && (EMACS_SECS(*timeout)==0) &&
2774                (EMACS_USECS(*timeout)==0)))
2775        return select(n, rfds, wfds, efds, timeout);
2776      else
2777        {
2778          EMACS_TIME end_time, now;
2779    
2780          EMACS_GET_TIME (end_time);
2781          if (timeout)
2782            EMACS_ADD_TIME (end_time, end_time, *timeout);
2783    
2784          do
2785            {
2786              int r;
2787              EMACS_TIME one_second;
2788              SELECT_TYPE orfds;
2789    
2790              FD_ZERO (&orfds);
2791              if (rfds)
2792                {
2793                  orfds = *rfds;
2794                }
2795    
2796              EMACS_SET_SECS (one_second, 1);
2797              EMACS_SET_USECS (one_second, 0);
2798    
2799              if (timeout && EMACS_TIME_LT(*timeout, one_second))
2800                one_second = *timeout;
2801    
2802              if ((r = select (n, &orfds, wfds, efds, &one_second)) > 0)
2803                {
2804                  *rfds = orfds;
2805                  return r;
2806                }
2807    
2808              mac_check_for_quit_char();
2809    
2810              EMACS_GET_TIME (now);
2811              EMACS_SUB_TIME (now, end_time, now);
2812            }
2813          while (!timeout || !EMACS_TIME_NEG_P (now));
2814    
2815          return 0;
2816        }
2817    }
2818    
2819    #undef read
2820    int sys_read (fds, buf, nbyte)
2821         int fds;
2822         char *buf;
2823         unsigned int nbyte;
2824    {
2825      SELECT_TYPE rfds;
2826      EMACS_TIME one_second;
2827      int r;
2828    
2829      /* Use select to block on IO while still checking for quit_char */
2830      if (!inhibit_window_system && !noninteractive &&
2831          ! (fcntl(fds, F_GETFL, 0) & O_NONBLOCK))
2832        {
2833          FD_ZERO (&rfds);
2834          FD_SET (fds, &rfds);
2835          if (sys_select (fds+1, &rfds, 0, 0, NULL) < 0)
2836            return -1;
2837        }
2838    
2839      return read (fds, buf, nbyte);
2840    }
2841    
2842    
2843    /* Set up environment variables so that Emacs can correctly find its
2844       support files when packaged as an application bundle.  Directories
2845       placed in /usr/local/share/emacs/<emacs-version>/, /usr/local/bin,
2846       and /usr/local/libexec/emacs/<emacs-version>/<system-configuration>
2847       by `make install' by default can instead be placed in
2848       .../Emacs.app/Contents/Resources/ and
2849       .../Emacs.app/Contents/MacOS/.  Each of these environment variables
2850       is changed only if it is not already set.  Presumably if the user
2851       sets an environment variable, he will want to use files in his path
2852       instead of ones in the application bundle.  */
2853    void
2854    init_mac_osx_environment ()
2855    {
2856      CFBundleRef bundle;
2857      CFURLRef bundleURL;
2858      CFStringRef cf_app_bundle_pathname;
2859      int app_bundle_pathname_len;
2860      char *app_bundle_pathname;
2861      char *p, *q;
2862      struct stat st;
2863    
2864      /* Fetch the pathname of the application bundle as a C string into
2865         app_bundle_pathname.  */
2866    
2867      bundle = CFBundleGetMainBundle ();
2868      if (!bundle)
2869        return;
2870    
2871      bundleURL = CFBundleCopyBundleURL (bundle);
2872      if (!bundleURL)
2873        return;
2874    
2875      cf_app_bundle_pathname = CFURLCopyFileSystemPath (bundleURL,
2876                                                        kCFURLPOSIXPathStyle);
2877      app_bundle_pathname_len = CFStringGetLength (cf_app_bundle_pathname);
2878      app_bundle_pathname = (char *) alloca (app_bundle_pathname_len + 1);
2879    
2880      if (!CFStringGetCString (cf_app_bundle_pathname,
2881                               app_bundle_pathname,
2882                               app_bundle_pathname_len + 1,
2883                               kCFStringEncodingISOLatin1))
2884        {
2885          CFRelease (cf_app_bundle_pathname);
2886          return;
2887        }
2888    
2889      CFRelease (cf_app_bundle_pathname);
2890    
2891      /* P should have sufficient room for the pathname of the bundle plus
2892         the subpath in it leading to the respective directories.  Q
2893         should have three times that much room because EMACSLOADPATH can
2894         have the value "<path to lisp dir>:<path to leim dir>:<path to
2895         site-lisp dir>".  */
2896      p = (char *) alloca (app_bundle_pathname_len + 50);
2897      q = (char *) alloca (3 * app_bundle_pathname_len + 150);
2898      if (!getenv ("EMACSLOADPATH"))
2899        {
2900          q[0] = '\0';
2901    
2902          strcpy (p, app_bundle_pathname);
2903          strcat (p, "/Contents/Resources/lisp");
2904          if (stat (p, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR)
2905            strcat (q, p);
2906    
2907          strcpy (p, app_bundle_pathname);
2908          strcat (p, "/Contents/Resources/leim");
2909          if (stat (p, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR)
2910            {
2911              if (q[0] != '\0')
2912                strcat (q, ":");
2913              strcat (q, p);
2914            }
2915    
2916          strcpy (p, app_bundle_pathname);
2917          strcat (p, "/Contents/Resources/site-lisp");
2918          if (stat (p, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR)
2919            {
2920              if (q[0] != '\0')
2921                strcat (q, ":");
2922              strcat (q, p);
2923            }
2924    
2925          if (q[0] != '\0')
2926            setenv ("EMACSLOADPATH", q, 1);
2927        }
2928    
2929      if (!getenv ("EMACSPATH"))
2930        {
2931          q[0] = '\0';
2932    
2933          strcpy (p, app_bundle_pathname);
2934          strcat (p, "/Contents/MacOS/bin");
2935          if (stat (p, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR)
2936            strcat (q, p);
2937    
2938          strcpy (p, app_bundle_pathname);
2939          strcat (p, "/Contents/MacOS/libexec");
2940          if (stat (p, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR)
2941            {
2942              if (q[0] != '\0')
2943                strcat (q, ":");
2944              strcat (q, p);
2945            }
2946    
2947          if (q[0] != '\0')
2948            setenv ("EMACSPATH", q, 1);
2949        }
2950    
2951      if (!getenv ("EMACSDATA"))
2952        {
2953          strcpy (p, app_bundle_pathname);
2954          strcat (p, "/Contents/Resources/etc");
2955          if (stat (p, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR)
2956            setenv ("EMACSDATA", p, 1);
2957        }
2958    
2959      if (!getenv ("EMACSDOC"))
2960        {
2961          strcpy (p, app_bundle_pathname);
2962          strcat (p, "/Contents/Resources/etc");
2963          if (stat (p, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR)
2964            setenv ("EMACSDOC", p, 1);
2965        }
2966    
2967      if (!getenv ("INFOPATH"))
2968        {
2969          strcpy (p, app_bundle_pathname);
2970          strcat (p, "/Contents/Resources/info");
2971          if (stat (p, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR)
2972            setenv ("INFOPATH", p, 1);
2973        }
2974    }
2975    #endif /* MAC_OSX */
2976    
2977  void  void
2978  syms_of_mac ()  syms_of_mac ()
2979  {  {
2980    QCLIPBOARD = intern ("CLIPBOARD");    QCLIPBOARD = intern ("CLIPBOARD");
2981    staticpro (&QCLIPBOARD);    staticpro (&QCLIPBOARD);
2982      
2983    defsubr (&Smac_paste_function);    defsubr (&Smac_paste_function);
2984    defsubr (&Smac_cut_function);    defsubr (&Smac_cut_function);
 #if 0  
2985    defsubr (&Sx_selection_exists_p);    defsubr (&Sx_selection_exists_p);
 #endif /* 0 */  
2986    
2987    defsubr (&Sdo_applescript);    defsubr (&Sdo_applescript);
2988    defsubr (&Smac_file_name_to_posix);    defsubr (&Smac_file_name_to_posix);

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.3.2.1

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26