Patch 7.0.105
[MacVim/jjgod.git] / src / os_amiga.c
blob37c5c94010e14724e9dd28054cca7da971e06c13
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * os_amiga.c
13 * Amiga system-dependent routines.
16 #include "vim.h"
18 #ifdef Window
19 # undef Window /* Amiga has its own Window definition */
20 #endif
22 #ifdef HAVE_FCNTL_H
23 # include <fcntl.h>
24 #endif
26 #undef TRUE /* will be redefined by exec/types.h */
27 #undef FALSE
29 #ifndef LATTICE
30 # include <exec/types.h>
31 # include <exec/exec.h>
32 # include <libraries/dos.h>
33 # include <libraries/dosextens.h>
34 # include <intuition/intuition.h>
35 #else
36 # include <proto/dos.h>
37 # include <libraries/dosextens.h>
38 # include <proto/intuition.h>
39 # include <proto/exec.h>
40 #endif
42 #include <exec/memory.h>
44 #include <dos/dostags.h> /* for 2.0 functions */
45 #include <dos/dosasl.h>
47 #if defined(LATTICE) && !defined(SASC) && defined(FEAT_ARP)
48 # include <libraries/arp_pragmas.h>
49 #endif
52 * At this point TRUE and FALSE are defined as 1L and 0L, but we want 1 and 0.
54 #undef TRUE
55 #define TRUE (1)
56 #undef FALSE
57 #define FALSE (0)
59 #if !defined(AZTEC_C) && !defined(__AROS__)
60 static long dos_packet __ARGS((struct MsgPort *, long, long));
61 #endif
62 static int lock2name __ARGS((BPTR lock, char_u *buf, long len));
63 static void out_num __ARGS((long n));
64 static struct FileInfoBlock *get_fib __ARGS((char_u *));
65 static int sortcmp __ARGS((const void *a, const void *b));
67 static BPTR raw_in = (BPTR)NULL;
68 static BPTR raw_out = (BPTR)NULL;
69 static int close_win = FALSE; /* set if Vim opened the window */
71 struct IntuitionBase *IntuitionBase = NULL;
72 #ifdef FEAT_ARP
73 struct ArpBase *ArpBase = NULL;
74 #endif
76 static struct Window *wb_window;
77 static char_u *oldwindowtitle = NULL;
79 #ifdef FEAT_ARP
80 int dos2 = FALSE; /* Amiga DOS 2.0x or higher */
81 #endif
82 int size_set = FALSE; /* set to TRUE if window size was set */
84 void
85 win_resize_on()
87 OUT_STR_NF("\033[12{");
90 void
91 win_resize_off()
93 OUT_STR_NF("\033[12}");
96 void
97 mch_write(p, len)
98 char_u *p;
99 int len;
101 Write(raw_out, (char *)p, (long)len);
105 * mch_inchar(): low level input funcion.
106 * Get a characters from the keyboard.
107 * If time == 0 do not wait for characters.
108 * If time == n wait a short time for characters.
109 * If time == -1 wait forever for characters.
111 * Return number of characters read.
114 mch_inchar(buf, maxlen, time, tb_change_cnt)
115 char_u *buf;
116 int maxlen;
117 long time; /* milli seconds */
118 int tb_change_cnt;
120 int len;
121 long utime;
123 if (time >= 0)
125 if (time == 0)
126 utime = 100L; /* time = 0 causes problems in DOS 1.2 */
127 else
128 utime = time * 1000L; /* convert from milli to micro secs */
129 if (WaitForChar(raw_in, utime) == 0) /* no character available */
130 return 0;
132 else /* time == -1 */
135 * If there is no character available within 2 seconds (default)
136 * write the autoscript file to disk. Or cause the CursorHold event
137 * to be triggered.
139 if (WaitForChar(raw_in, p_ut * 1000L) == 0)
141 #ifdef FEAT_AUTOCMD
142 if (trigger_cursorhold() && maxlen >= 3)
144 buf[0] = K_SPECIAL;
145 buf[1] = KS_EXTRA;
146 buf[2] = (int)KE_CURSORHOLD;
147 return 3;
149 #endif
150 before_blocking();
154 for (;;) /* repeat until we got a character */
156 # ifdef FEAT_MBYTE
157 len = Read(raw_in, (char *)buf, (long)maxlen / input_conv.vc_factor);
158 # else
159 len = Read(raw_in, (char *)buf, (long)maxlen);
160 # endif
161 if (len > 0)
163 #ifdef FEAT_MBYTE
164 /* Convert from 'termencoding' to 'encoding'. */
165 if (input_conv.vc_type != CONV_NONE)
166 len = convert_input(buf, len, maxlen);
167 #endif
168 return len;
174 * return non-zero if a character is available
177 mch_char_avail()
179 return (WaitForChar(raw_in, 100L) != 0);
183 * Return amount of memory still available.
185 long_u
186 mch_avail_mem(special)
187 int special;
189 return (long_u)AvailMem(special ? (long)MEMF_CHIP : (long)MEMF_ANY);
192 void
193 mch_delay(msec, ignoreinput)
194 long msec;
195 int ignoreinput;
197 #ifndef LATTICE /* SAS declares void Delay(UNLONG) */
198 void Delay __ARGS((long));
199 #endif
201 if (msec > 0)
203 if (ignoreinput)
204 Delay(msec / 20L); /* Delay works with 20 msec intervals */
205 else
206 WaitForChar(raw_in, msec * 1000L);
211 * We have no job control, fake it by starting a new shell.
213 void
214 mch_suspend()
216 suspend_shell();
219 #ifndef DOS_LIBRARY
220 # define DOS_LIBRARY ((UBYTE *)"dos.library")
221 #endif
223 void
224 mch_init()
226 static char intlibname[] = "intuition.library";
228 #ifdef AZTEC_C
229 Enable_Abort = 0; /* disallow vim to be aborted */
230 #endif
231 Columns = 80;
232 Rows = 24;
235 * Set input and output channels, unless we have opened our own window
237 if (raw_in == (BPTR)NULL)
239 raw_in = Input();
240 raw_out = Output();
242 * If Input() is not interactive, then Output() will be (because of
243 * check in mch_check_win()). Used for "Vim -".
244 * Also check the other way around, for "Vim -h | more".
246 if (!IsInteractive(raw_in))
247 raw_in = raw_out;
248 else if (!IsInteractive(raw_out))
249 raw_out = raw_in;
252 out_flush();
254 wb_window = NULL;
255 if ((IntuitionBase = (struct IntuitionBase *)
256 OpenLibrary((UBYTE *)intlibname, 0L)) == NULL)
258 mch_errmsg(_("cannot open "));
259 mch_errmsg(intlibname);
260 mch_errmsg("!?\n");
261 mch_exit(3);
265 #include <workbench/startup.h>
268 * Check_win checks whether we have an interactive window.
269 * If not, a new window is opened with the newcli command.
270 * If we would open a window ourselves, the :sh and :! commands would not
271 * work properly (Why? probably because we are then running in a background
272 * CLI). This also is the best way to assure proper working in a next
273 * Workbench release.
275 * For the -f option (foreground mode) we open our own window and disable :sh.
276 * Otherwise the calling program would never know when editing is finished.
278 #define BUF2SIZE 320 /* length of buffer for argument with complete path */
281 mch_check_win(argc, argv)
282 int argc;
283 char **argv;
285 int i;
286 BPTR nilfh, fh;
287 char_u buf1[20];
288 char_u buf2[BUF2SIZE];
289 static char_u *(constrings[3]) = {(char_u *)"con:0/0/662/210/",
290 (char_u *)"con:0/0/640/200/",
291 (char_u *)"con:0/0/320/200/"};
292 static char_u *winerr = (char_u *)N_("VIM: Can't open window!\n");
293 struct WBArg *argp;
294 int ac;
295 char *av;
296 char_u *device = NULL;
297 int exitval = 4;
298 struct Library *DosBase;
299 int usewin = FALSE;
302 * check if we are running under DOS 2.0x or higher
304 DosBase = OpenLibrary(DOS_LIBRARY, 37L);
305 if (DosBase != NULL)
306 /* if (((struct Library *)DOSBase)->lib_Version >= 37) */
308 CloseLibrary(DosBase);
309 #ifdef FEAT_ARP
310 dos2 = TRUE;
311 #endif
313 else /* without arp functions we NEED 2.0 */
315 #ifndef FEAT_ARP
316 mch_errmsg(_("Need Amigados version 2.04 or later\n"));
317 exit(3);
318 #else
319 /* need arp functions for dos 1.x */
320 if (!(ArpBase = (struct ArpBase *) OpenLibrary((UBYTE *)ArpName, ArpVersion)))
322 fprintf(stderr, _("Need %s version %ld\n"), ArpName, ArpVersion);
323 exit(3);
325 #endif
329 * scan argv[] for the "-f" and "-d" arguments
331 for (i = 1; i < argc; ++i)
332 if (argv[i][0] == '-')
334 switch (argv[i][1])
336 case 'f':
337 usewin = TRUE;
338 break;
340 case 'd':
341 if (i < argc - 1
342 #ifdef FEAT_DIFF
343 /* require using "-dev", "-d" means diff mode */
344 && argv[i][2] == 'e' && argv[i][3] == 'v'
345 #endif
347 device = (char_u *)argv[i + 1];
348 break;
353 * If we were not started from workbench, do not have a "-d" or "-dev"
354 * argument and we have been started with an interactive window, use that
355 * window.
357 if (argc != 0
358 && device == NULL
359 && (IsInteractive(Input()) || IsInteractive(Output())))
360 return OK;
363 * When given the "-f" argument, we open our own window. We can't use the
364 * newcli trick below, because the calling program (mail, rn, etc.) would not
365 * know when we are finished.
367 if (usewin)
370 * Try to open a window. First try the specified device.
371 * Then try a 24 line 80 column window.
372 * If that fails, try two smaller ones.
374 for (i = -1; i < 3; ++i)
376 if (i >= 0)
377 device = constrings[i];
378 if (device != NULL && (raw_in = Open((UBYTE *)device,
379 (long)MODE_NEWFILE)) != (BPTR)NULL)
380 break;
382 if (raw_in == (BPTR)NULL) /* all three failed */
384 mch_errmsg(_(winerr));
385 goto exit;
387 raw_out = raw_in;
388 close_win = TRUE;
389 return OK;
392 if ((nilfh = Open((UBYTE *)"NIL:", (long)MODE_NEWFILE)) == (BPTR)NULL)
394 mch_errmsg(_("Cannot open NIL:\n"));
395 goto exit;
399 * Make a unique name for the temp file (which we will not delete!).
400 * Use a pointer on the stack (nobody else will be using it).
402 sprintf((char *)buf1, "t:nc%ld", (long)buf1);
403 if ((fh = Open((UBYTE *)buf1, (long)MODE_NEWFILE)) == (BPTR)NULL)
405 mch_errmsg(_("Cannot create "));
406 mch_errmsg((char *)buf1);
407 mch_errmsg("\n");
408 goto exit;
411 * Write the command into the file, put quotes around the arguments that
412 * have a space in them.
414 if (argc == 0) /* run from workbench */
415 ac = ((struct WBStartup *)argv)->sm_NumArgs;
416 else
417 ac = argc;
418 for (i = 0; i < ac; ++i)
420 if (argc == 0)
422 *buf2 = NUL;
423 argp = &(((struct WBStartup *)argv)->sm_ArgList[i]);
424 if (argp->wa_Lock)
425 (void)lock2name(argp->wa_Lock, buf2, (long)(BUF2SIZE - 1));
426 #ifdef FEAT_ARP
427 if (dos2) /* use 2.0 function */
428 #endif
429 AddPart((UBYTE *)buf2, (UBYTE *)argp->wa_Name, (long)(BUF2SIZE - 1));
430 #ifdef FEAT_ARP
431 else /* use arp function */
432 TackOn((char *)buf2, argp->wa_Name);
433 #endif
434 av = (char *)buf2;
436 else
437 av = argv[i];
439 /* skip '-d' or "-dev" option */
440 if (av[0] == '-' && av[1] == 'd'
441 #ifdef FEAT_DIFF
442 && av[2] == 'e' && av[3] == 'v'
443 #endif
446 ++i;
447 continue;
449 if (vim_strchr((char_u *)av, ' '))
450 Write(fh, "\"", 1L);
451 Write(fh, av, (long)strlen(av));
452 if (vim_strchr((char_u *)av, ' '))
453 Write(fh, "\"", 1L);
454 Write(fh, " ", 1L);
456 Write(fh, "\nendcli\n", 8L);
457 Close(fh);
460 * Try to open a new cli in a window. If "-d" or "-dev" argument was given try
461 * to open the specified device. Then try a 24 line 80 column window. If that
462 * fails, try two smaller ones.
464 for (i = -1; i < 3; ++i)
466 if (i >= 0)
467 device = constrings[i];
468 else if (device == NULL)
469 continue;
470 sprintf((char *)buf2, "newcli <nil: >nil: %s from %s", (char *)device, (char *)buf1);
471 #ifdef FEAT_ARP
472 if (dos2)
474 #endif
475 if (!SystemTags((UBYTE *)buf2, SYS_UserShell, TRUE, TAG_DONE))
476 break;
477 #ifdef FEAT_ARP
479 else
481 if (Execute((UBYTE *)buf2, nilfh, nilfh))
482 break;
484 #endif
486 if (i == 3) /* all three failed */
488 DeleteFile((UBYTE *)buf1);
489 mch_errmsg(_(winerr));
490 goto exit;
492 exitval = 0; /* The Execute succeeded: exit this program */
494 exit:
495 #ifdef FEAT_ARP
496 if (ArpBase)
497 CloseLibrary((struct Library *) ArpBase);
498 #endif
499 exit(exitval);
500 /* NOTREACHED */
501 return FAIL;
505 * Return TRUE if the input comes from a terminal, FALSE otherwise.
506 * We fake there is a window, because we can always open one!
509 mch_input_isatty()
511 return TRUE;
515 * fname_case(): Set the case of the file name, if it already exists.
516 * This will cause the file name to remain exactly the same.
518 /*ARGSUSED*/
519 void
520 fname_case(name, len)
521 char_u *name;
522 int len; /* buffer size, ignored here */
524 struct FileInfoBlock *fib;
525 size_t flen;
527 fib = get_fib(name);
528 if (fib != NULL)
530 flen = STRLEN(name);
531 if (flen == strlen(fib->fib_FileName)) /* safety check */
532 mch_memmove(name, fib->fib_FileName, flen);
533 vim_free(fib);
538 * Get the FileInfoBlock for file "fname"
539 * The returned structure has to be free()d.
540 * Returns NULL on error.
542 static struct FileInfoBlock *
543 get_fib(fname)
544 char_u *fname;
546 BPTR flock;
547 struct FileInfoBlock *fib;
549 if (fname == NULL) /* safety check */
550 return NULL;
551 fib = (struct FileInfoBlock *)malloc(sizeof(struct FileInfoBlock));
552 if (fib != NULL)
554 flock = Lock((UBYTE *)fname, (long)ACCESS_READ);
555 if (flock == (BPTR)NULL || !Examine(flock, fib))
557 vim_free(fib); /* in case of an error the memory is freed here */
558 fib = NULL;
560 if (flock)
561 UnLock(flock);
563 return fib;
566 #ifdef FEAT_TITLE
568 * set the title of our window
569 * icon name is not set
571 void
572 mch_settitle(title, icon)
573 char_u *title;
574 char_u *icon;
576 if (wb_window != NULL && title != NULL)
577 SetWindowTitles(wb_window, (UBYTE *)title, (UBYTE *)-1L);
581 * Restore the window/icon title.
582 * which is one of:
583 * 1 Just restore title
584 * 2 Just restore icon (which we don't have)
585 * 3 Restore title and icon (which we don't have)
587 void
588 mch_restore_title(which)
589 int which;
591 if (which & 1)
592 mch_settitle(oldwindowtitle, NULL);
596 mch_can_restore_title()
598 return (wb_window != NULL);
602 mch_can_restore_icon()
604 return FALSE;
606 #endif
609 * Insert user name in s[len].
612 mch_get_user_name(s, len)
613 char_u *s;
614 int len;
616 *s = NUL;
617 return FAIL;
621 * Insert host name is s[len].
623 void
624 mch_get_host_name(s, len)
625 char_u *s;
626 int len;
628 vim_strncpy(s, "Amiga", len - 1);
632 * return process ID
634 long
635 mch_get_pid()
637 return (long)0;
641 * Get name of current directory into buffer 'buf' of length 'len' bytes.
642 * Return OK for success, FAIL for failure.
645 mch_dirname(buf, len)
646 char_u *buf;
647 int len;
649 return mch_FullName((char_u *)"", buf, len, FALSE);
653 * get absolute file name into buffer 'buf' of length 'len' bytes
655 * return FAIL for failure, OK otherwise
658 mch_FullName(fname, buf, len, force)
659 char_u *fname, *buf;
660 int len;
661 int force;
663 BPTR l;
664 int retval = FAIL;
665 int i;
667 /* Lock the file. If it exists, we can get the exact name. */
668 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
670 retval = lock2name(l, buf, (long)len - 1);
671 UnLock(l);
673 else if (force || !mch_isFullName(fname)) /* not a full path yet */
676 * If the file cannot be locked (doesn't exist), try to lock the
677 * current directory and concatenate the file name.
679 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
681 retval = lock2name(l, buf, (long)len);
682 UnLock(l);
683 if (retval == OK)
685 i = STRLEN(buf);
686 /* Concatenate the fname to the directory. Don't add a slash
687 * if fname is empty, but do change "" to "/". */
688 if (i == 0 || *fname != NUL)
690 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
691 buf[i++] = '/';
692 vim_strncpy(buf + i, fname, len - i - 1);
697 if (*buf == 0 || *buf == ':')
698 retval = FAIL; /* something failed; use the file name */
699 return retval;
703 * Return TRUE if "fname" does not depend on the current directory.
706 mch_isFullName(fname)
707 char_u *fname;
709 return (vim_strchr(fname, ':') != NULL && *fname != ':');
713 * Get the full file name from a lock. Use 2.0 function if possible, because
714 * the arp function has more restrictions on the path length.
716 * return FAIL for failure, OK otherwise
718 static int
719 lock2name(lock, buf, len)
720 BPTR lock;
721 char_u *buf;
722 long len;
724 #ifdef FEAT_ARP
725 if (dos2) /* use 2.0 function */
726 #endif
727 return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL);
728 #ifdef FEAT_ARP
729 else /* use arp function */
730 return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL);
731 #endif
735 * get file permissions for 'name'
736 * Returns -1 when it doesn't exist.
738 long
739 mch_getperm(name)
740 char_u *name;
742 struct FileInfoBlock *fib;
743 long retval = -1;
745 fib = get_fib(name);
746 if (fib != NULL)
748 retval = fib->fib_Protection;
749 vim_free(fib);
751 return retval;
755 * set file permission for 'name' to 'perm'
757 * return FAIL for failure, OK otherwise
760 mch_setperm(name, perm)
761 char_u *name;
762 long perm;
764 perm &= ~FIBF_ARCHIVE; /* reset archived bit */
765 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
769 * Set hidden flag for "name".
771 void
772 mch_hide(name)
773 char_u *name;
775 /* can't hide a file */
779 * return FALSE if "name" is not a directory
780 * return TRUE if "name" is a directory.
781 * return FALSE for error.
784 mch_isdir(name)
785 char_u *name;
787 struct FileInfoBlock *fib;
788 int retval = FALSE;
790 fib = get_fib(name);
791 if (fib != NULL)
793 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
794 vim_free(fib);
796 return retval;
800 * Create directory "name".
802 void
803 mch_mkdir(name)
804 char_u *name;
806 BPTR lock;
808 lock = CreateDir(name);
809 if (lock != NULL)
810 UnLock(lock);
814 * Return 1 if "name" can be executed, 0 if not.
815 * Return -1 if unknown.
818 mch_can_exe(name)
819 char_u *name;
821 /* TODO */
822 return -1;
826 * Check what "name" is:
827 * NODE_NORMAL: file or directory (or doesn't exist)
828 * NODE_WRITABLE: writable device, socket, fifo, etc.
829 * NODE_OTHER: non-writable things
832 mch_nodetype(name)
833 char_u *name;
835 /* TODO */
836 return NODE_NORMAL;
839 void
840 mch_early_init()
845 * Careful: mch_exit() may be called before mch_init()!
847 void
848 mch_exit(r)
849 int r;
851 if (raw_in) /* put terminal in 'normal' mode */
853 settmode(TMODE_COOK);
854 stoptermcap();
856 out_char('\n');
857 if (raw_out)
859 if (term_console)
861 win_resize_off(); /* window resize events de-activated */
862 if (size_set)
863 OUT_STR("\233t\233u"); /* reset window size (CSI t CSI u) */
865 out_flush();
868 #ifdef FEAT_TITLE
869 mch_restore_title(3); /* restore window title */
870 #endif
872 ml_close_all(TRUE); /* remove all memfiles */
874 #ifdef FEAT_ARP
875 if (ArpBase)
876 CloseLibrary((struct Library *) ArpBase);
877 #endif
878 if (close_win)
879 Close(raw_in);
880 if (r)
881 printf(_("Vim exiting with %d\n"), r); /* somehow this makes :cq work!? */
882 exit(r);
886 * This is a routine for setting a given stream to raw or cooked mode on the
887 * Amiga . This is useful when you are using Lattice C to produce programs
888 * that want to read single characters with the "getch()" or "fgetc" call.
890 * Written : 18-Jun-87 By Chuck McManis.
893 #define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
896 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
897 * 'cooked' mode. This only works on TTY's.
899 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
900 * getch() will return immediately rather than wait for a return. You
901 * lose editing features though.
903 * Cooked: This function returns the designate file pointer to it's normal,
904 * wait for a <CR> mode. This is exactly like raw() except that
905 * it sends a 0 to the console to make it back into a CON: from a RAW:
907 void
908 mch_settmode(tmode)
909 int tmode;
911 #ifdef __AROS__
912 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
913 #else
914 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
915 tmode == TMODE_RAW ? -1L : 0L) == 0)
916 #endif
917 mch_errmsg(_("cannot change console mode ?!\n"));
921 * set screen mode, always fails.
924 mch_screenmode(arg)
925 char_u *arg;
927 EMSG(_(e_screenmode));
928 return FAIL;
932 * Code for this routine came from the following :
934 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
935 * DOS packet example
936 * Requires 1.2
938 * Found on Fish Disk 56.
940 * Heavely modified by mool.
943 #include <devices/conunit.h>
946 * try to get the real window size
947 * return FAIL for failure, OK otherwise
950 mch_get_shellsize()
952 struct ConUnit *conUnit;
953 char id_a[sizeof(struct InfoData) + 3];
954 struct InfoData *id;
956 if (!term_console) /* not an amiga window */
957 return FAIL;
959 /* insure longword alignment */
960 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
963 * Should make console aware of real window size, not the one we set.
964 * Unfortunately, under DOS 2.0x this redraws the window and it
965 * is rarely needed, so we skip it now, unless we changed the size.
967 if (size_set)
968 OUT_STR("\233t\233u"); /* CSI t CSI u */
969 out_flush();
971 #ifdef __AROS__
972 if (!Info(raw_out, id)
973 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
974 #else
975 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
976 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
977 #endif
979 /* it's not an amiga window, maybe aux device */
980 /* terminal type should be set */
981 term_console = FALSE;
982 return FAIL;
984 if (oldwindowtitle == NULL)
985 oldwindowtitle = (char_u *)wb_window->Title;
986 if (id->id_InUse == (BPTR)NULL)
988 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
989 return FAIL;
991 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
993 /* get window size */
994 Rows = conUnit->cu_YMax + 1;
995 Columns = conUnit->cu_XMax + 1;
996 if (Rows < 0 || Rows > 200) /* cannot be an amiga window */
998 Columns = 80;
999 Rows = 24;
1000 term_console = FALSE;
1001 return FAIL;
1004 return OK;
1008 * Try to set the real window size to Rows and Columns.
1010 void
1011 mch_set_shellsize()
1013 if (term_console)
1015 size_set = TRUE;
1016 out_char(CSI);
1017 out_num((long)Rows);
1018 out_char('t');
1019 out_char(CSI);
1020 out_num((long)Columns);
1021 out_char('u');
1022 out_flush();
1027 * Rows and/or Columns has changed.
1029 void
1030 mch_new_shellsize()
1032 /* Nothing to do. */
1036 * out_num - output a (big) number fast
1038 static void
1039 out_num(n)
1040 long n;
1042 OUT_STR_NF(tltoa((unsigned long)n));
1045 #if !defined(AZTEC_C) && !defined(__AROS__)
1047 * Sendpacket.c
1049 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1050 * the given message port. This makes working around DOS lots easier.
1052 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1053 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1054 * say 'oml lib:amiga.lib -r sendpacket.o'
1057 /* #include <proto/exec.h> */
1058 /* #include <proto/dos.h> */
1059 #include <exec/memory.h>
1062 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1063 * Finkel. This function will send a packet of the given type to the Message
1064 * Port supplied.
1067 static long
1068 dos_packet(pid, action, arg)
1069 struct MsgPort *pid; /* process indentifier ... (handlers message port) */
1070 long action, /* packet type ... (what you want handler to do) */
1071 arg; /* single argument */
1073 # ifdef FEAT_ARP
1074 struct MsgPort *replyport;
1075 struct StandardPacket *packet;
1076 long res1;
1078 if (dos2)
1079 # endif
1080 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); /* use 2.0 function */
1081 # ifdef FEAT_ARP
1083 replyport = (struct MsgPort *) CreatePort(NULL, 0); /* use arp function */
1084 if (!replyport)
1085 return (0);
1087 /* Allocate space for a packet, make it public and clear it */
1088 packet = (struct StandardPacket *)
1089 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
1090 if (!packet) {
1091 DeletePort(replyport);
1092 return (0);
1094 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1095 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1096 packet->sp_Pkt.dp_Port = replyport;
1097 packet->sp_Pkt.dp_Type = action;
1098 packet->sp_Pkt.dp_Arg1 = arg;
1100 PutMsg(pid, (struct Message *)packet); /* send packet */
1102 WaitPort(replyport);
1103 GetMsg(replyport);
1105 res1 = packet->sp_Pkt.dp_Res1;
1107 FreeMem(packet, (long) sizeof(struct StandardPacket));
1108 DeletePort(replyport);
1110 return (res1);
1111 # endif
1113 #endif /* !defined(AZTEC_C) && !defined(__AROS__) */
1116 * Call shell.
1117 * Return error number for failure, 0 otherwise
1120 mch_call_shell(cmd, options)
1121 char_u *cmd;
1122 int options; /* SHELL_*, see vim.h */
1124 BPTR mydir;
1125 int x;
1126 int tmode = cur_tmode;
1127 #ifdef AZTEC_C
1128 int use_execute;
1129 char_u *shellcmd = NULL;
1130 char_u *shellarg;
1131 #endif
1132 int retval = 0;
1134 if (close_win)
1136 /* if Vim opened a window: Executing a shell may cause crashes */
1137 EMSG(_("E360: Cannot execute shell with -f option"));
1138 return -1;
1141 if (term_console)
1142 win_resize_off(); /* window resize events de-activated */
1143 out_flush();
1145 if (options & SHELL_COOKED)
1146 settmode(TMODE_COOK); /* set to normal mode */
1147 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); /* remember current dir */
1149 #if !defined(AZTEC_C) /* not tested very much */
1150 if (cmd == NULL)
1152 # ifdef FEAT_ARP
1153 if (dos2)
1154 # endif
1155 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1156 # ifdef FEAT_ARP
1157 else
1158 x = Execute(p_sh, raw_in, raw_out);
1159 # endif
1161 else
1163 # ifdef FEAT_ARP
1164 if (dos2)
1165 # endif
1166 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1167 # ifdef FEAT_ARP
1168 else
1169 x = Execute((char *)cmd, 0L, raw_out);
1170 # endif
1172 # ifdef FEAT_ARP
1173 if ((dos2 && x < 0) || (!dos2 && !x))
1174 # else
1175 if (x < 0)
1176 # endif
1178 MSG_PUTS(_("Cannot execute "));
1179 if (cmd == NULL)
1181 MSG_PUTS(_("shell "));
1182 msg_outtrans(p_sh);
1184 else
1185 msg_outtrans(cmd);
1186 msg_putchar('\n');
1187 retval = -1;
1189 # ifdef FEAT_ARP
1190 else if (!dos2 || x)
1191 # else
1192 else if (x)
1193 # endif
1195 if ((x = IoErr()) != 0)
1197 if (!(options & SHELL_SILENT))
1199 msg_putchar('\n');
1200 msg_outnum((long)x);
1201 MSG_PUTS(_(" returned\n"));
1203 retval = x;
1206 #else /* else part is for AZTEC_C */
1207 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1208 use_execute = 1;
1209 else
1210 use_execute = 0;
1211 if (!use_execute)
1214 * separate shell name from argument
1216 shellcmd = vim_strsave(p_sh);
1217 if (shellcmd == NULL) /* out of memory, use Execute */
1218 use_execute = 1;
1219 else
1221 shellarg = skiptowhite(shellcmd); /* find start of arguments */
1222 if (*shellarg != NUL)
1224 *shellarg++ = NUL;
1225 shellarg = skipwhite(shellarg);
1229 if (cmd == NULL)
1231 if (use_execute)
1233 # ifdef FEAT_ARP
1234 if (dos2)
1235 # endif
1236 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1237 # ifdef FEAT_ARP
1238 else
1239 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1240 # endif
1242 else
1243 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1245 else if (use_execute)
1247 # ifdef FEAT_ARP
1248 if (dos2)
1249 # endif
1250 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1251 # ifdef FEAT_ARP
1252 else
1253 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1254 # endif
1256 else if (p_st & 1)
1257 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1258 (char *)cmd, NULL);
1259 else
1260 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1261 (char *)p_shcf, (char *)cmd, NULL);
1262 # ifdef FEAT_ARP
1263 if ((dos2 && x < 0) || (!dos2 && x))
1264 # else
1265 if (x < 0)
1266 # endif
1268 MSG_PUTS(_("Cannot execute "));
1269 if (use_execute)
1271 if (cmd == NULL)
1272 msg_outtrans(p_sh);
1273 else
1274 msg_outtrans(cmd);
1276 else
1278 MSG_PUTS(_("shell "));
1279 msg_outtrans(shellcmd);
1281 msg_putchar('\n');
1282 retval = -1;
1284 else
1286 if (use_execute)
1288 # ifdef FEAT_ARP
1289 if (!dos2 || x)
1290 # else
1291 if (x)
1292 # endif
1293 x = IoErr();
1295 else
1296 x = wait();
1297 if (x)
1299 if (!(options & SHELL_SILENT) && !emsg_silent)
1301 msg_putchar('\n');
1302 msg_outnum((long)x);
1303 MSG_PUTS(_(" returned\n"));
1305 retval = x;
1308 vim_free(shellcmd);
1309 #endif /* AZTEC_C */
1311 if ((mydir = CurrentDir(mydir)) != 0) /* make sure we stay in the same directory */
1312 UnLock(mydir);
1313 if (tmode == TMODE_RAW)
1314 settmode(TMODE_RAW); /* set to raw mode */
1315 #ifdef FEAT_TITLE
1316 resettitle();
1317 #endif
1318 if (term_console)
1319 win_resize_on(); /* window resize events activated */
1320 return retval;
1324 * check for an "interrupt signal"
1325 * We only react to a CTRL-C, but also clear the other break signals to avoid
1326 * trouble with lattice-c programs.
1328 void
1329 mch_breakcheck()
1331 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1332 got_int = TRUE;
1335 /* this routine causes manx to use this Chk_Abort() rather than it's own */
1336 /* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
1337 /* is zero). Since we want to check for our own ^C's */
1339 #ifdef _DCC
1340 #define Chk_Abort chkabort
1341 #endif
1343 #ifdef LATTICE
1344 void __regargs __chkabort(void);
1346 void __regargs __chkabort(void)
1349 #else
1350 long
1351 Chk_Abort(void)
1353 return(0L);
1355 #endif
1358 * mch_expandpath() - this code does wild-card pattern matching using the arp
1359 * routines.
1361 * "pat" has backslashes before chars that are not to be expanded.
1362 * Returns the number of matches found.
1364 * This is based on WildDemo2.c (found in arp1.1 distribution).
1365 * That code's copyright follows:
1366 * Copyright (c) 1987, Scott Ballantyne
1367 * Use and abuse as you please.
1370 #define ANCHOR_BUF_SIZE (512)
1371 #define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1374 mch_expandpath(gap, pat, flags)
1375 garray_T *gap;
1376 char_u *pat;
1377 int flags; /* EW_* flags */
1379 struct AnchorPath *Anchor;
1380 LONG Result;
1381 char_u *starbuf, *sp, *dp;
1382 int start_len;
1383 int matches;
1385 start_len = gap->ga_len;
1387 /* Get our AnchorBase */
1388 Anchor = (struct AnchorPath *)alloc_clear((unsigned)ANCHOR_SIZE);
1389 if (Anchor == NULL)
1390 return 0;
1392 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; /* ap_Length not supported anymore */
1393 #ifdef APF_DODOT
1394 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; /* allow '.' for current dir */
1395 #else
1396 Anchor->ap_Flags = APF_DoDot | APF_DoWild; /* allow '.' for current dir */
1397 #endif
1399 #ifdef FEAT_ARP
1400 if (dos2)
1402 #endif
1403 /* hack to replace '*' by '#?' */
1404 starbuf = alloc((unsigned)(2 * STRLEN(pat) + 1));
1405 if (starbuf == NULL)
1406 goto Return;
1407 for (sp = pat, dp = starbuf; *sp; ++sp)
1409 if (*sp == '*')
1411 *dp++ = '#';
1412 *dp++ = '?';
1414 else
1415 *dp++ = *sp;
1417 *dp = NUL;
1418 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1419 vim_free(starbuf);
1420 #ifdef FEAT_ARP
1422 else
1423 Result = FindFirst((char *)pat, Anchor);
1424 #endif
1427 * Loop to get all matches.
1429 while (Result == 0)
1431 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
1432 #ifdef FEAT_ARP
1433 if (dos2)
1434 #endif
1435 Result = MatchNext(Anchor);
1436 #ifdef FEAT_ARP
1437 else
1438 Result = FindNext(Anchor);
1439 #endif
1441 matches = gap->ga_len - start_len;
1443 if (Result == ERROR_BUFFER_OVERFLOW)
1444 EMSG(_("ANCHOR_BUF_SIZE too small."));
1445 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1446 && Result != ERROR_DEVICE_NOT_MOUNTED
1447 && Result != ERROR_NO_MORE_ENTRIES)
1448 EMSG(_("I/O ERROR"));
1451 * Sort the files for this pattern.
1453 if (matches)
1454 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1455 (size_t)matches, sizeof(char_u *), sortcmp);
1457 /* Free the wildcard stuff */
1458 #ifdef FEAT_ARP
1459 if (dos2)
1460 #endif
1461 MatchEnd(Anchor);
1462 #ifdef FEAT_ARP
1463 else
1464 FreeAnchorChain(Anchor);
1465 #endif
1467 Return:
1468 vim_free(Anchor);
1470 return matches;
1473 static int
1474 sortcmp(a, b)
1475 const void *a, *b;
1477 char *s = *(char **)a;
1478 char *t = *(char **)b;
1480 return pathcmp(s, t, -1);
1484 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1487 mch_has_exp_wildcard(p)
1488 char_u *p;
1490 for ( ; *p; mb_ptr_adv(p))
1492 if (*p == '\\' && p[1] != NUL)
1493 ++p;
1494 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1495 return TRUE;
1497 return FALSE;
1501 mch_has_wildcard(p)
1502 char_u *p;
1504 for ( ; *p; mb_ptr_adv(p))
1506 if (*p == '\\' && p[1] != NUL)
1507 ++p;
1508 else
1509 if (vim_strchr((char_u *)
1510 # ifdef VIM_BACKTICK
1511 "*?[(#$`"
1512 # else
1513 "*?[(#$"
1514 # endif
1515 , *p) != NULL
1516 || (*p == '~' && p[1] != NUL))
1517 return TRUE;
1519 return FALSE;
1523 * With AmigaDOS 2.0 support for reading local environment variables
1525 * Two buffers are allocated:
1526 * - A big one to do the expansion into. It is freed before returning.
1527 * - A small one to hold the return value. It is kept until the next call.
1529 char_u *
1530 mch_getenv(var)
1531 char_u *var;
1533 int len;
1534 UBYTE *buf; /* buffer to expand in */
1535 char_u *retval; /* return value */
1536 static char_u *alloced = NULL; /* allocated memory */
1538 #ifdef FEAT_ARP
1539 if (!dos2)
1540 retval = (char_u *)getenv((char *)var);
1541 else
1542 #endif
1544 vim_free(alloced);
1545 alloced = NULL;
1546 retval = NULL;
1548 buf = alloc(IOSIZE);
1549 if (buf == NULL)
1550 return NULL;
1552 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1553 if (len >= 0)
1555 retval = vim_strsave((char_u *)buf);
1556 alloced = retval;
1559 vim_free(buf);
1562 /* if $VIM is not defined, use "vim:" instead */
1563 if (retval == NULL && STRCMP(var, "VIM") == 0)
1564 retval = (char_u *)"vim:";
1566 return retval;
1570 * Amiga version of setenv() with AmigaDOS 2.0 support.
1572 /* ARGSUSED */
1574 mch_setenv(var, value, x)
1575 char *var;
1576 char *value;
1577 int x;
1579 #ifdef FEAT_ARP
1580 if (!dos2)
1581 return setenv(var, value);
1582 #endif
1584 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
1585 return 0; /* success */
1586 return -1; /* failure */