blob: cc656f061e462472e993846f15d9285bd2f1162f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * os_mac.c -- code for the MacOS
12 *
13 * This file is mainly based on os_unix.c.
14 */
15
16#include "vim.h"
17
18#if defined(__MRC__) || defined(__SC__) /* for Apple MPW Compilers */
19
20#include "StandardFile.h"
21
22/*
23 * Implements the corresponding unix function
24 */
25 int
26stat(
27 char *p,
28 struct stat *p_st)
29{
30 /*
31 TODO: Use functions which fill the FileParam struct (Files.h)
32 and copy these contents to our self-defined stat struct
33 */
34 return 0;
35}
36#endif
37
38/*
39 * change the current working directory
40 */
41 int
42mch_chdir(char *p_name)
43{
44#if defined(__MRC__) || defined(__SC__) /* for Apple MPW Compilers */
45 /* TODO */
46 return FAIL;
47#else
48 return chdir(p_name);
49#endif
50}
51
52
53/*
54 * Recursively build up a list of files in "gap" matching the first wildcard
55 * in `path'. Called by mch_expandpath().
56 * "path" has backslashes before chars that are not to be expanded.
57 */
58 int
59mac_expandpath(
60 garray_T *gap,
61 char_u *path,
62 int flags, /* EW_* flags */
63 short start_at,
64 short as_full)
65{
66 /*
67 * TODO:
68 * +Get Volumes (when looking for files in current dir)
69 * +Make it work when working dir not on select volume
70 * +Cleanup
71 */
72 short index = 1;
73 OSErr gErr;
74 char_u dirname[256];
75 char_u cfilename[256];
76 long dirID;
77 char_u *new_name;
78 CInfoPBRec gMyCPB;
79 HParamBlockRec gMyHPBlock;
80 FSSpec usedDir;
81
82 char_u *buf;
83 char_u *p, *s, *e, dany;
84 int start_len, c;
85 char_u *pat;
86 regmatch_T regmatch;
87
88 start_len = gap->ga_len;
89 buf = alloc(STRLEN(path) + BASENAMELEN + 5);/* make room for file name */
90 if (buf == NULL)
91 return 0;
92
93/*
94 * Find the first part in the path name that contains a wildcard.
95 * Copy it into buf, including the preceding characters.
96 */
97 p = buf;
98 s = buf;
99 e = NULL;
100#if 1
101 STRNCPY(buf, path, start_at);
102 p += start_at;
103 path += start_at;
104#endif
105
106 while (*path)
107 {
108 if (*path == ':')
109 {
110 if (e)
111 break;
112 else
113 s = p + 1;
114 }
115 /* should use WILCARDLIST but what about ` */
116 /* if (vim_strchr((char_u *)"*?[{~$", *path) != NULL)*/
117 else if (vim_strchr((char_u *)WILDCHAR_LIST, *path) != NULL)
118 e = p;
119#ifdef FEAT_MBYTE
120 if (has_mbyte)
121 {
122 int len = (*mb_ptr2len_check)(path);
123
124 STRNCPY(p, path, len);
125 p += len;
126 path += len;
127 }
128 else
129#endif
130 *p++ = *path++;
131 }
132 e = p;
133
134 /* now we have one wildcard component between s and e */
135 *e = NUL;
136
137#if 1
138 dany = *s;
139 *s = NUL;
140 backslash_halve(buf);
141 *s = dany;
142#endif
143
144 /* convert the file pattern to a regexp pattern */
145 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
146 if (pat == NULL)
147 {
148 vim_free(buf);
149 return 0;
150 }
151
152 /* compile the regexp into a program */
153 regmatch.rm_ic = FALSE; /* Don't ever ignore case */
154 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
155 vim_free(pat);
156
157 if (regmatch.regprog == NULL)
158 {
159 vim_free(buf);
160 return 0;
161 }
162
163 /* open the directory for scanning */
164 c = *s;
165 *s = NUL;
166
167 if (*buf == NUL)
168 {
169 as_full = TRUE;
170#if 0
171 (void) mch_dirname (&dirname[1], 254);
172 dirname[0] = STRLEN(&dirname[1]);
173#endif
174 }
175 else
176 {
177 if (*buf == ':') /* relative path */
178 {
179 (void)mch_dirname(&dirname[1], 254);
180 new_name = concat_fnames(&dirname[1], buf+1, TRUE);
181 STRCPY(&dirname[1], new_name);
182 dirname[0] = STRLEN(new_name);
183 vim_free(new_name);
184 }
185 else
186 {
187 STRCPY(&dirname[1], buf);
188 backslash_halve(&dirname[1]);
189 dirname[0] = STRLEN(buf);
190 }
191 }
192 *s = c;
193
194 FSMakeFSSpec (0, 0, dirname, &usedDir);
195
196 gMyCPB.dirInfo.ioNamePtr = dirname;
197 gMyCPB.dirInfo.ioVRefNum = usedDir.vRefNum;
198 gMyCPB.dirInfo.ioFDirIndex = 0;
199 gMyCPB.dirInfo.ioDrDirID = 0;
200
201 gErr = PBGetCatInfo(&gMyCPB, false);
202
203 gMyCPB.dirInfo.ioCompletion = NULL;
204 dirID = gMyCPB.dirInfo.ioDrDirID;
205 do
206 {
207 gMyCPB.hFileInfo.ioFDirIndex = index;
208 gMyCPB.hFileInfo.ioDirID = dirID;
209
210 gErr = PBGetCatInfo(&gMyCPB,false);
211
212 if (gErr == noErr)
213 {
214 STRNCPY (cfilename, &dirname[1], dirname[0]);
215 cfilename[dirname[0]] = 0;
216 if (vim_regexec(&regmatch, cfilename, (colnr_T)0))
217 {
218 if (s[-1] != ':')
219 {
220 /* TODO: need to copy with cleaned name */
221 STRCPY(s+1, cfilename);
222 s[0] = ':';
223 }
224 else
225 { /* TODO: need to copy with cleeaned name */
226 STRCPY(s, cfilename);
227 }
228 start_at = STRLEN(buf);
229 STRCAT(buf, path);
230 if (mch_has_exp_wildcard(path)) /* handle more wildcards */
231 (void)mac_expandpath(gap, buf, flags, start_at, FALSE);
232 else
233 {
234#ifdef DONT_ADD_PATHSEP_TO_DIR
235 if ((gMyCPB.hFileInfo.ioFlAttrib & ioDirMask) !=0 )
236 STRCAT(buf, PATHSEPSTR);
237#endif
238 addfile(gap, buf, flags);
239 }
240 }
241 if ((gMyCPB.hFileInfo.ioFlAttrib & ioDirMask) !=0 )
242 {
243 }
244 else
245 {
246 }
247 }
248 index++;
249 }
250 while (gErr == noErr);
251
252 if (as_full)
253 {
254 index = 1;
255 do
256 {
257 gMyHPBlock.volumeParam.ioNamePtr = (char_u *) dirname;
258 gMyHPBlock.volumeParam.ioVRefNum =0;
259 gMyHPBlock.volumeParam.ioVolIndex = index;
260
261 gErr = PBHGetVInfo (&gMyHPBlock,false);
262 if (gErr == noErr)
263 {
264 STRNCPY (cfilename, &dirname[1], dirname[0]);
265 cfilename[dirname[0]] = 0;
266 if (vim_regexec(&regmatch, cfilename, (colnr_T)0))
267 {
268 STRCPY(s, cfilename);
269 STRCAT(buf, path);
270 if (mch_has_exp_wildcard(path)) /* handle more wildcards */
271 (void)mac_expandpath(gap, s, flags, 0, FALSE);
272 else
273 {
274#ifdef DONT_ADD_PATHSEP_TO_DIR
275/* if ((gMyCPB.hFileInfo.ioFlAttrib & ioDirMask) !=0 )
276*/ STRCAT(buf, PATHSEPSTR);
277#endif
278 addfile(gap, s, flags);
279 }
280#if 0
281 STRCAT(cfilename, PATHSEPSTR);
282 addfile (gap, cfilename, flags);
283#endif
284 }
285 }
286 index++;
287 }
288 while (gErr == noErr);
289 }
290
291 vim_free(regmatch.regprog);
292
293 return gap->ga_len - start_len;
294}
295
296
297#ifdef USE_UNIXFILENAME
298 static int
299pstrcmp(a, b)
300 const void *a, *b;
301{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000302 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303}
304
305 static int
306unix_expandpath(gap, path, wildoff, flags)
307 garray_T *gap;
308 char_u *path;
309 int wildoff;
310 int flags; /* EW_* flags */
311{
312 char_u *buf;
313 char_u *path_end;
314 char_u *p, *s, *e;
315 int start_len, c;
316 char_u *pat;
317 DIR *dirp;
318 regmatch_T regmatch;
319 struct dirent *dp;
320 int starts_with_dot;
321 int matches;
322 int len;
323
324 start_len = gap->ga_len;
325 buf = alloc(STRLEN(path) + BASENAMELEN + 5);/* make room for file name */
326 if (buf == NULL)
327 return 0;
328
329/*
330 * Find the first part in the path name that contains a wildcard.
331 * Copy it into buf, including the preceding characters.
332 */
333 p = buf;
334 s = buf;
335 e = NULL;
336 path_end = path;
337 while (*path_end)
338 {
339 /* May ignore a wildcard that has a backslash before it */
340 if (path_end >= path + wildoff && rem_backslash(path_end))
341 *p++ = *path_end++;
342 else if (*path_end == '/')
343 {
344 if (e != NULL)
345 break;
346 else
347 s = p + 1;
348 }
349 else if (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL)
350 e = p;
351#ifdef FEAT_MBYTE
352 if (has_mbyte)
353 {
354 len = (*mb_ptr2len_check)(path_end);
355 STRNCPY(p, path_end, len);
356 p += len;
357 path_end += len;
358 }
359 else
360#endif
361 *p++ = *path_end++;
362 }
363 e = p;
364 *e = NUL;
365
366 /* now we have one wildcard component between s and e */
367 /* Remove backslashes between "wildoff" and the start of the wildcard
368 * component. */
369 for (p = buf + wildoff; p < s; ++p)
370 if (rem_backslash(p))
371 {
372 STRCPY(p, p + 1);
373 --e;
374 --s;
375 }
376
377 /* convert the file pattern to a regexp pattern */
378 starts_with_dot = (*s == '.');
379 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
380 if (pat == NULL)
381 {
382 vim_free(buf);
383 return 0;
384 }
385
386 /* compile the regexp into a program */
387#ifdef MACOS_X
388 /* We want to behave like Terminal.app */
389 regmatch.rm_ic = TRUE;
390#else
391 regmatch.rm_ic = FALSE; /* Don't ever ignore case */
392#endif
393 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
394 vim_free(pat);
395
396 if (regmatch.regprog == NULL)
397 {
398 vim_free(buf);
399 return 0;
400 }
401
402 /* open the directory for scanning */
403 c = *s;
404 *s = NUL;
405 dirp = opendir(*buf == NUL ? "." : (char *)buf);
406 *s = c;
407
408 /* Find all matching entries */
409 if (dirp != NULL)
410 {
411 for (;;)
412 {
413 dp = readdir(dirp);
414 if (dp == NULL)
415 break;
416 if ((dp->d_name[0] != '.' || starts_with_dot)
417 && vim_regexec(&regmatch, (char_u *)dp->d_name, (colnr_T)0))
418 {
419 STRCPY(s, dp->d_name);
420 len = STRLEN(buf);
421 STRCPY(buf + len, path_end);
422 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
423 {
424 /* need to expand another component of the path */
425 /* remove backslashes for the remaining components only */
426 (void)unix_expandpath(gap, buf, len + 1, flags);
427 }
428 else
429 {
430 /* no more wildcards, check if there is a match */
431 /* remove backslashes for the remaining components only */
432 if (*path_end)
433 backslash_halve(buf + len + 1);
434 if (mch_getperm(buf) >= 0) /* add existing file */
435 addfile(gap, buf, flags);
436 }
437 }
438 }
439
440 closedir(dirp);
441 }
442
443 vim_free(buf);
444 vim_free(regmatch.regprog);
445
446 matches = gap->ga_len - start_len;
447 if (matches)
448 qsort(((char_u **)gap->ga_data) + start_len, matches,
449 sizeof(char_u *), pstrcmp);
450 return matches;
451}
452#endif
453
454/*
455 * Recursively build up a list of files in "gap" matching the first wildcard
456 * in `path'. Called by expand_wildcards().
457 * "pat" has backslashes before chars that are not to be expanded.
458 */
459 int
460mch_expandpath(
461 garray_T *gap,
462 char_u *path,
463 int flags) /* EW_* flags */
464{
465#ifdef USE_UNIXFILENAME
466 return unix_expandpath(gap, path, 0, flags);
467#else
468 char_u first = *path;
469 short scan_volume;
470
471 slash_n_colon_adjust(path);
472
473 scan_volume = (first != *path);
474
475 return mac_expandpath(gap, path, flags, 0, scan_volume);
476#endif
477}
478
479 void
480fname_case(name, len)
481 char_u *name;
482 int len; /* buffer size, ignored here */
483{
484 /*
485 * TODO: get the real casing for the file
486 * make it called
487 * with USE_FNAME_CASE & USE_LONG_FNAME
488 * CASE_INSENSITIVE_FILENAME
489 * within setfname, fix_fname, do_ecmd
490 */
491#ifdef USE_UNIXFILENAME
492 OSStatus status;
493 FSRef refFile;
494 UInt32 pathSize = STRLEN(name) + 1;
495 char_u *path;
496 Boolean isDirectory;
497
498 path = alloc(pathSize);
499 if (path == NULL)
500 return;
501
502 status = FSPathMakeRef((UInt8 *)name, &refFile, &isDirectory);
503 if (status)
504 return;
505
506 status = FSRefMakePath(&refFile, (UInt8 *)path, pathSize);
507 if (status)
508 return;
509
510 /* Paranoid: Update the name if only the casing differ.*/
511 if (STRICMP(name, path) == 0)
512 STRCPY(name, path);
513#endif
514}
515static char_u *oldtitle = (char_u *) "gVim";
516
517/*
518 * check for an "interrupt signal": CTRL-break or CTRL-C
519 */
520 void
521mch_breakcheck()
522{
523 /*
524 * TODO: Scan event for a CTRL-C or COMMAND-. and do: got_int=TRUE;
525 * or only go proccess event?
526 * or do nothing
527 */
528 EventRecord theEvent;
529
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000530 if (EventAvail(keyDownMask, &theEvent))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 if ((theEvent.message & charCodeMask) == Ctrl_C && ctrl_c_interrupts)
532 got_int = TRUE;
533#if 0
534 short i = 0;
535 Boolean found = false;
536 EventRecord theEvent;
537
538 while ((i < 10) && (!found))
539 {
540 found = EventAvail (keyDownMask, &theEvent);
541 if (found)
542 {
543 if ((theEvent.modifiers & controlKey) != 0)
544 found = false;
545 if ((theEvent.what == keyDown))
546 found = false;
547 if ((theEvent.message & charCodeMask) == Ctrl_C)
548 {
549 found = false;
550 got_int = TRUE;
551 }
552 }
553 i++;
554 }
555#endif
556
557}
558
559/*
560 * Return amount of memory currently available.
561 */
562 long_u
563mch_avail_mem(special)
564 int special;
565{
566 /*
567 * TODO: Use MaxBlock, FreeMeM, PurgeSpace, MaxBlockSys FAQ-266
568 * figure out what the special is for
569 *
570 * FreeMem -> returns all avail memory is application heap
571 * MaxBlock -> returns the biggest contigeous block in application heap
572 * PurgeSpace ->
573 */
574 return MaxBlock();
575}
576
577 void
578mch_delay(msec, ignoreinput)
579 long msec;
580 int ignoreinput;
581{
582#if (defined(__MWERKS__) && __MWERKS__ >= 0x2000) \
583 || defined(__MRC__) || defined(__SC__)
584 unsigned
585#endif
586 long finalTick;
587
588 if (ignoreinput)
589 Delay (60*msec/1000, &finalTick);
590 else
591 /* even thougth we should call gui stuff from here
592 it the simplest way to be safe */
593 gui_mch_wait_for_chars(msec);
594}
595
596 void
597mch_init()
598{
599 /*
600 * TODO: Verify if needed, or override later.
601 */
602 Columns = 80;
603 Rows = 24;
604}
605
606/*
607 * Check_win checks whether we have an interactive stdout.
608 */
609 int
610mch_check_win(argc, argv)
611 int argc;
612 char **argv;
613{
614 /*
615 * TODO: Maybe to be remove through NO_CONSOLE
616 */
617 return OK;
618}
619
620/*
621 * Return TRUE if the input comes from a terminal, FALSE otherwise.
622 */
623 int
624mch_input_isatty()
625{
626 /*
627 * TODO: Maybe to be remove through NO_CONSOLE
628 */
629 return OK;
630}
631
632#ifdef FEAT_TITLE
633/*
634 * Set the window title and icon.
635 * (The icon is not taken care of).
636 */
637 void
638mch_settitle(title, icon)
639 char_u *title;
640 char_u *icon;
641{
642 gui_mch_settitle(title, icon);
643}
644
645/*
646 * Restore the window/icon title.
647 * which is one of:
648 * 1 Just restore title
649 * 2 Just restore icon
650 * 3 Restore title and icon
651 * but don't care about the icon.
652 */
653 void
654mch_restore_title(which)
655 int which;
656{
657 mch_settitle((which & 1) ? oldtitle : NULL, NULL);
658}
659#endif
660
661/*
662 * Insert user name in s[len].
663 * Return OK if a name found.
664 */
665 int
666mch_get_user_name(s, len)
667 char_u *s;
668 int len;
669{
670#if !(defined(__MRC__) || defined(__SC__)) /* No solution yet */
671 /*
672 * TODO: clean up and try getlogin ()
673 */
674#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
675 struct passwd *pw;
676#endif
677 uid_t uid;
678
679 uid = getuid();
680#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
681 if ((pw = getpwuid(uid)) != NULL
682 && pw->pw_name != NULL && *(pw->pw_name) != NUL)
683 {
684 STRNCPY(s, pw->pw_name, len);
685 return OK;
686 }
687#endif
688 sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
689#endif
690 return FAIL; /* a number is not a name */
691}
692
693/*
694 * Copy host name into s[len].
695 */
696 void
697mch_get_host_name(s, len)
698 char_u *s;
699 int len;
700{
701#if defined(__MRC__) || defined(__SC__) || defined(__APPLE_CC__)
702 STRNCPY(s, "Mac", len); /* TODO: use Gestalt information */
703#else
704 struct utsname vutsname;
705
706 if (uname(&vutsname) < 0)
707 *s = NUL;
708 else
709 STRNCPY(s, vutsname.nodename, len);
710#endif
711 s[len - 1] = NUL; /* make sure it's terminated */
712}
713
714/*
715 * return process ID
716 */
717 long
718mch_get_pid()
719{
720 return (long)getpid();
721}
722
723/*
724 * Get name of current directory into buffer 'buf' of length 'len' bytes.
725 * Return OK for success, FAIL for failure.
726 */
727 int
728mch_dirname(buf, len)
729 char_u *buf;
730 int len;
731{
732#if defined(__MRC__) || defined(__SC__)
733 return FAIL; /* No solution yet */
734#else
735 /* The last : is already put by getcwd */
736 if (getcwd((char *)buf, len) == NULL)
737 {
738 STRCPY(buf, strerror(errno));
739 return FAIL;
740 }
741# ifndef USE_UNIXFILENAME
742 else if (*buf != NUL && buf[STRLEN(buf) - 1] == ':')
743 buf[STRLEN(buf) - 1] = NUL; /* remove trailing ':' */
744# endif
745 return OK;
746#endif
747}
748
749 void
750slash_to_colon(p)
751 char_u *p;
752{
753 for ( ; *p; ++p)
754 if (*p == '/')
755 *p = ':';
756}
757
758 char_u *
759slash_to_colon_save (p)
760 char_u *p;
761{
762 char_u *res;
763
764 res = vim_strsave(p);
765 if (res == NULL)
766 return p;
767 slash_to_colon(res);
768 return res;
769}
770
771 void
772slash_n_colon_adjust (buf)
773 char_u *buf;
774{
775 /*
776 * TODO: Make it faster
777 */
778#ifndef USE_UNIXFILENAME
779 char_u temp[MAXPATHL];
780 char_u *first_colon = vim_strchr(buf, ':');
781 char_u *first_slash = vim_strchr(buf, '/');
782 int full = TRUE;
783 char_u *scanning;
784 char_u *filling;
785 char_u last_copied = NUL;
786
787 if (*buf == NUL)
788 return ;
789
790 if ((first_colon == NULL) && (first_slash == NULL))
791 full = FALSE;
792 if ((first_slash == NULL) && (first_colon != NULL))
793 full = TRUE;
794 if ((first_colon == NULL) && (first_slash != NULL))
795 full = FALSE;
796 if ((first_slash < first_colon) && (first_slash != NULL))
797 full = FALSE;
798 if ((first_colon < first_slash) && (first_colon != NULL))
799 full = TRUE;
800 if (first_slash == buf)
801 full = TRUE;
802 if (first_colon == buf)
803 full = FALSE;
804
805 scanning = buf;
806 filling = temp;
807
808 while (*scanning != NUL)
809 {
810 if (*scanning == '/')
811 {
812 if ((scanning[1] != '/') && (scanning[-1] != ':'))
813 {
814 *filling++ = ':';
815 scanning++;
816 }
817 else
818 scanning++;
819 }
820 else if (*scanning == '.')
821 {
822 if ((scanning[1] == NUL) || scanning[1] == '/')
823 {
824 if (scanning[1] == NUL)
825 scanning += 1;
826 else
827 scanning += 2;
828 }
829 else if (scanning[1] == '.')
830 {
831 if ((scanning[2] == NUL) || scanning[2] == '/')
832 {
833 *filling++ = ':';
834 if (scanning[2] == NUL)
835 scanning +=2;
836 else
837 scanning += 3;
838 }
839 else
840 {
841 *filling++ = *scanning++;
842 }
843 }
844 else
845 {
846 *filling++ = *scanning++;
847 }
848
849 }
850 else
851 {
852 *filling++ = *scanning++;
853 }
854
855 }
856
857 *filling = 0;
858 filling = temp;
859
860 if (!full)
861 {
862 if (buf[0] != ':')
863 {
864 buf[0] = ':';
865 buf[1] = NUL;
866 }
867 else
868 buf[0] = NUL;
869 }
870 else
871 {
872 buf[0] = NUL;
873 if (filling[0] == ':')
874 filling++;
875 }
876
877 STRCAT (buf, filling);
878#endif
879}
880
881/*
882 * Get absolute filename into buffer 'buf' of length 'len' bytes.
883 *
884 * return FAIL for failure, OK for success
885 */
886 int
887mch_FullName(fname, buf, len, force)
888 char_u *fname, *buf;
889 int len;
890 int force; /* also expand when already absolute path name */
891{
892 /*
893 * TODO: Find what TODO
894 */
895 int l;
896 char_u olddir[MAXPATHL];
897 char_u newdir[MAXPATHL];
898 char_u *p;
899 char_u c;
900 int retval = OK;
901
902 if (force || !mch_isFullName(fname))
903 {
904 /*
905 * Forced or not an absolute path.
906 * If the file name has a path, change to that directory for a moment,
907 * and then do the getwd() (and get back to where we were).
908 * This will get the correct path name with "../" things.
909 */
910 if ((p = vim_strrchr(fname, ':')) != NULL)
911 {
912 p++;
913 if (mch_dirname(olddir, MAXPATHL) == FAIL)
914 {
915 p = NULL; /* can't get current dir: don't chdir */
916 retval = FAIL;
917 }
918 else
919 {
920 c = *p;
921 *p = NUL;
922 if (mch_chdir((char *)fname))
923 retval = FAIL;
924 else
925 fname = p; /* + 1;*/
926 *p = c;
927 }
928 }
929 if (mch_dirname(buf, len) == FAIL)
930 {
931 retval = FAIL;
932 *newdir = NUL;
933 }
934 l = STRLEN(buf);
935 if (STRCMP(fname, ".") != 0)
936 {
937#ifdef USE_UNIXFILENAME
938 if (l > 0 && buf[l - 1] != '/' && *fname != NUL)
939 STRCAT(buf, "/");
940#else
941 if (l > 0 && buf[l - 1] != ':' && *fname != NUL)
942 STRCAT(buf, ":");
943#endif
944 }
945 if (p != NULL)
946 mch_chdir((char *)olddir);
947 if (STRCMP(fname, ".") != 0)
948 STRCAT(buf, fname);
949 }
950 else
951 {
952 STRNCPY(buf, fname, len);
953 buf[len - 1] = NUL;
954 slash_n_colon_adjust(buf);
955 }
956
957 return retval;
958}
959
960/*
961 * Return TRUE if "fname" does not depend on the current directory.
962 */
963 int
964mch_isFullName(fname)
965 char_u *fname;
966{
967#ifdef USE_UNIXFILENAME
968 return ((fname[0] == '/') || (fname[0] == '~'));
969#else
970 /*
971 * TODO: Make sure fname is always of mac still
972 * i.e: passed throught slash_n_colon_adjust
973 */
974 char_u *first_colon = vim_strchr(fname, ':');
975 char_u *first_slash = vim_strchr(fname, '/');
976
977 if (first_colon == fname)
978 return FALSE;
979 if (first_slash == fname)
980 return TRUE;
981 if ((first_colon < first_slash) && (first_colon != NULL))
982 return TRUE;
983 if ((first_slash < first_colon) && (first_slash != NULL))
984 return FALSE;
985 if ((first_colon == NULL) && (first_slash != NULL))
986 return FALSE;
987 if ((first_slash == NULL) && (first_colon != NULL))
988 return TRUE;
989 if ((first_colon == NULL) && (first_slash == NULL))
990 return FALSE;
991 return TRUE;
992#endif
993}
994
995/*
996 * Replace all slashes by colons.
997 */
998 void
999slash_adjust(p)
1000 char_u *p;
1001{
1002#ifndef USE_UNIXFILENAME
1003 /*
1004 * TODO: keep escaped '/'
1005 */
1006
1007 while (*p)
1008 {
1009 if (*p == '/')
1010 *p = ':';
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001011 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013#endif
1014}
1015
1016/*
1017 * Get file permissions for 'name'.
1018 * Returns -1 when it doesn't exist.
1019 */
1020 long
1021mch_getperm(name)
1022 char_u *name;
1023{
1024 /*
1025 * TODO: Maybe use AppleShare info??
1026 * Use locked for non writable
1027 */
1028
1029 struct stat statb;
1030
1031 if (stat((char *)name, &statb))
1032 return -1;
1033 return statb.st_mode;
1034}
1035
1036/*
1037 * set file permission for 'name' to 'perm'
1038 *
1039 * return FAIL for failure, OK otherwise
1040 */
1041 int
1042mch_setperm(name, perm)
1043 char_u *name;
1044 long perm;
1045{
1046 /*
1047 * TODO: Maybe use AppleShare info??
1048 * Use locked for non writable
1049 */
1050 return (OK);
1051}
1052
1053/*
1054 * Set hidden flag for "name".
1055 */
1056 void
1057mch_hide(name)
1058 char_u *name;
1059{
1060 /*
1061 * TODO: Hide the file throught FileManager FAQ 8-34
1062 *
1063 * *name is mac style start with : for relative
1064 */
1065}
1066
1067
1068/*
1069 * return TRUE if "name" is a directory
1070 * return FALSE if "name" is not a directory
1071 * return FALSE for error
1072 */
1073 int
1074mch_isdir(name)
1075 char_u *name;
1076{
1077 /*
1078 * TODO: Find out by FileManager calls ...
1079 */
1080 struct stat statb;
1081
1082#if defined(TARGET_API_MAC_CARBON) && TARGET_API_MAC_CARBON
1083 /* For some reason the name is sometimes empty,
1084 * (such as for a not yet named file). An empty
1085 * filename is interpreted by the MacOS version
1086 * of stat (at least under Codewarrior) as the
1087 * current directory.
1088 */
1089 /* AK 20020413
1090 * This is required for Carbon but breaks the
1091 * explorer plugin in Classic
1092 */
1093 if (name[0] == NULL)
1094 return FALSE;
1095#endif
1096
1097 if (stat((char *)name, &statb))
1098 return FALSE;
1099#if defined(__MRC__) || defined(__SC__)
1100 return FALSE; /* definitely TODO */
1101#else
1102 return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
1103#endif
1104}
1105
1106#if defined(FEAT_EVAL) || defined(PROTO)
1107/*
1108 * Return 1 if "name" can be executed, 0 if not.
1109 * Return -1 if unknown.
1110 */
1111 int
1112mch_can_exe(name)
1113 char_u *name;
1114{
1115 /* TODO */
1116 return -1;
1117}
1118#endif
1119
1120/*
1121 * Check what "name" is:
1122 * NODE_NORMAL: file or directory (or doesn't exist)
1123 * NODE_WRITABLE: writable device, socket, fifo, etc.
1124 * NODE_OTHER: non-writable things
1125 */
1126 int
1127mch_nodetype(name)
1128 char_u *name;
1129{
1130 /* TODO */
1131 return NODE_NORMAL;
1132}
1133
1134 void
1135mch_early_init()
1136{
1137}
1138
1139 void
1140mch_exit(r)
1141 int r;
1142{
1143 display_errors();
1144
1145 ml_close_all(TRUE); /* remove all memfiles */
1146 exit(r);
1147}
1148
1149
1150 void
1151mch_settmode(tmode)
1152 int tmode;
1153{
1154 /*
1155 * TODO: remove the needs of it.
1156 */
1157}
1158
1159#ifdef FEAT_MOUSE
1160/*
1161 * set mouse clicks on or off (only works for xterms)
1162 */
1163 void
1164mch_setmouse(on)
1165 int on;
1166{
1167 /*
1168 * TODO: remove the needs of it.
1169 */
1170}
1171#endif
1172
1173/*
1174 * set screen mode, always fails.
1175 */
1176 int
1177mch_screenmode(arg)
1178 char_u *arg;
1179{
1180 EMSG(_(e_screenmode));
1181 return FAIL;
1182}
1183
1184 int
1185mch_call_shell(cmd, options)
1186 char_u *cmd;
1187 int options; /* SHELL_*, see vim.h */
1188{
1189 /*
1190 * TODO: find a shell or pseudo-shell to call
1191 * for some simple useful command
1192 */
1193
1194 return (-1);
1195}
1196
1197/*
1198 * Return TRUE if "p" contains a wildcard that can be expanded by
1199 * mch_expandpath().
1200 */
1201 int
1202mch_has_exp_wildcard(p)
1203 char_u *p;
1204{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001205 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
1207 if (*p == '\\' && p[1] != NUL)
1208 ++p;
1209 else if (vim_strchr((char_u *)WILDCHAR_LIST, *p) != NULL)
1210 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212 return FALSE;
1213}
1214
1215 int
1216mch_has_wildcard(p)
1217 char_u *p;
1218{
1219#ifdef USE_UNIXFILENAME
1220 if (*p == '~' && p[1] != NUL)
1221 return TRUE;
1222#endif
1223 return mch_has_exp_wildcard(p);
1224}
1225
1226
1227/*
1228 * This procedure duplicate a file, it is used in order to keep
1229 * the footprint of the previous file, when some info can be easily
1230 * restored with set_perm().
1231 *
1232 * Return -1 for failure, 0 for success.
1233 */
1234 int
1235mch_copy_file(from, to)
1236 char_u *from;
1237 char_u *to;
1238{
1239 char_u from_str[256];
1240 char_u to_str[256];
1241 char_u to_name[256];
1242
1243 HParamBlockRec paramBlock;
1244 char_u *char_ptr;
1245 int len;
1246
1247 /*
1248 * Convert C string to Pascal string
1249 */
1250 char_ptr = from;
1251 len = 1;
1252 for (; (*char_ptr != 0) && (len < 255); len++, char_ptr++)
1253 from_str[len] = *char_ptr;
1254 from_str[0] = len-1;
1255
1256 char_ptr = to;
1257 len = 1;
1258 for (; (*char_ptr != 0) && (len < 255); len++, char_ptr++)
1259 to_str[len] = *char_ptr;
1260 to_str[0] = len-1;
1261
1262 paramBlock.copyParam.ioCompletion = NULL;
1263 paramBlock.copyParam.ioNamePtr = from_str;
1264 /* paramBlock.copyParam.ioVRefnum = overided by ioFilename; */
1265 /* paramBlock.copyParam.ioDirI = overided by ioFilename; */
1266
1267 paramBlock.copyParam.ioNewName = to_str;
1268 paramBlock.copyParam.ioCopyName = to_name; /* NIL */
1269 /* paramBlock.copyParam.ioDstVRefNum = overided by ioNewName; */
1270 /* paramBlock.copyParam.ioNewDirID = overided by ioNewName; */
1271
1272
1273
1274 /*
1275 * First delete the "to" file, this is required on some systems to make
1276 * the rename() work, on other systems it makes sure that we don't have
1277 * two files when the rename() fails.
1278 */
1279 mch_remove(to);
1280
1281 /*
1282 * First try a normal rename, return if it works.
1283 */
1284 (void) PBHCopyFile(&paramBlock, false);
1285 return 0;
1286
1287}
1288
1289
1290 int
1291mch_copy_file_attribute(from, to)
1292 char_u *from;
1293 char_u *to;
1294{
Bram Moolenaar8299df92004-07-10 09:47:34 +00001295 FSSpec frFSSpec;
1296 FSSpec toFSSpec;
1297 FInfo fndrInfo;
1298 Str255 name;
1299 ResType type;
1300 ResType sink;
1301 Handle resource;
1302 short idxTypes;
1303 short nbTypes;
1304 short idxResources;
1305 short nbResources;
1306 short ID;
1307 short frRFid;
1308 short toRFid;
1309 short attrs_orig;
1310 short attrs_copy;
1311 short temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
1313 /* TODO: Handle error */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001314 (void)GetFSSpecFromPath(from, &frFSSpec);
1315 (void)GetFSSpecFromPath(to , &toFSSpec);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317 /* Copy resource fork */
1318 temp = 0;
1319
1320#if 1
1321 frRFid = FSpOpenResFile (&frFSSpec, fsCurPerm);
1322
1323 if (frRFid != -1)
1324 {
1325 FSpCreateResFile(&toFSSpec, 'TEXT', UNKNOWN_CREATOR, 0);
Bram Moolenaar8299df92004-07-10 09:47:34 +00001326 toRFid = FSpOpenResFile(&toFSSpec, fsRdWrPerm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
Bram Moolenaar8299df92004-07-10 09:47:34 +00001328 UseResFile(frRFid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
1330 nbTypes = Count1Types();
1331
1332 for (idxTypes = 1; idxTypes <= nbTypes; idxTypes++)
1333 {
Bram Moolenaar8299df92004-07-10 09:47:34 +00001334 Get1IndType(&type, idxTypes);
1335 nbResources = Count1Resources(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaar8299df92004-07-10 09:47:34 +00001337 for (idxResources = 1; idxResources <= nbResources; idxResources++)
1338 {
1339 attrs_orig = 0; /* in case GetRes fails */
1340 attrs_copy = 0; /* in case GetRes fails */
1341 resource = Get1IndResource(type, idxResources);
1342 GetResInfo(resource, &ID, &sink, name);
1343 HLock(resource);
1344 attrs_orig = GetResAttrs(resource);
1345 DetachResource(resource);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
1347
Bram Moolenaar8299df92004-07-10 09:47:34 +00001348 UseResFile(toRFid);
1349 AddResource(resource, type, ID, name);
1350 attrs_copy = GetResAttrs(resource);
1351 attrs_copy = (attrs_copy & 0x2) | (attrs_orig & 0xFD);
1352 SetResAttrs(resource, attrs_copy);
1353 WriteResource(resource);
1354 UpdateResFile(toRFid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar8299df92004-07-10 09:47:34 +00001356 temp = GetResAttrs(resource);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaar8299df92004-07-10 09:47:34 +00001358 /*SetResAttrs (resource, 0);*/
1359 HUnlock(resource);
1360 ReleaseResource(resource);
1361 UseResFile(frRFid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 }
Bram Moolenaar8299df92004-07-10 09:47:34 +00001364 CloseResFile(toRFid);
1365 CloseResFile(frRFid);
1366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#endif
1368 /* Copy Finder Info */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001369 (void)FSpGetFInfo(&frFSSpec, &fndrInfo);
1370 (void)FSpSetFInfo(&toFSSpec, &fndrInfo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
1372 return (temp == attrs_copy);
1373}
1374
1375 int
1376mch_has_resource_fork (file)
1377 char_u *file;
1378{
1379 FSSpec fileFSSpec;
Bram Moolenaar8299df92004-07-10 09:47:34 +00001380 short fileRFid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382 /* TODO: Handle error */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001383 (void)GetFSSpecFromPath(file, &fileFSSpec);
1384 fileRFid = FSpOpenResFile(&fileFSSpec, fsCurPerm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (fileRFid != -1)
Bram Moolenaar8299df92004-07-10 09:47:34 +00001386 CloseResFile(fileRFid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
1388 return (fileRFid != -1);
1389}
1390
1391 int
1392mch_get_shellsize(void)
1393{
1394 /* never used */
1395 return OK;
1396}
1397
1398 void
1399mch_set_shellsize(void)
1400{
1401 /* never used */
1402}
1403
1404/*
1405 * Rows and/or Columns has changed.
1406 */
1407 void
1408mch_new_shellsize(void)
1409{
1410 /* never used */
1411}
1412
1413/*
1414 * Those function were set as #define before, but in order
1415 * to allow an easier us of os_unix.c for the MacOS X port,
1416 * they are change to procedure. Thec ompile whould optimize
1417 * them out.
1418 */
1419
1420 int
1421mch_can_restore_title()
1422{
1423 return TRUE;
1424}
1425
1426 int
1427mch_can_restore_icon()
1428{
1429 return TRUE;
1430}
1431
1432/*
1433 * If the machine has job control, use it to suspend the program,
1434 * otherwise fake it by starting a new shell.
1435 */
1436 void
1437mch_suspend()
1438{
1439 /* TODO: get calle in #ifndef NO_CONSOLE */
1440 gui_mch_iconify();
1441};
1442