blob: 17e071e75d6b76843e637e469d74f4968de3c70f [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_amiga.c
12 *
13 * Amiga system-dependent routines.
14 */
15
16#include "vim.h"
17
18#ifdef Window
19# undef Window /* Amiga has its own Window definition */
20#endif
21
22#ifdef HAVE_FCNTL_H
23# include <fcntl.h>
24#endif
25
26#undef TRUE /* will be redefined by exec/types.h */
27#undef FALSE
28
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
41
42#include <exec/memory.h>
43
44#include <dos/dostags.h> /* for 2.0 functions */
45#include <dos/dosasl.h>
46
47#if defined(LATTICE) && !defined(SASC) && defined(FEAT_ARP)
48# include <libraries/arp_pragmas.h>
49#endif
50
51/*
52 * At this point TRUE and FALSE are defined as 1L and 0L, but we want 1 and 0.
53 */
54#undef TRUE
55#define TRUE (1)
56#undef FALSE
57#define FALSE (0)
58
59#if !defined(AZTEC_C) && !defined(__AROS__)
60static long dos_packet __ARGS((struct MsgPort *, long, long));
61#endif
62static int lock2name __ARGS((BPTR lock, char_u *buf, long len));
63static void out_num __ARGS((long n));
64static struct FileInfoBlock *get_fib __ARGS((char_u *));
65static int sortcmp __ARGS((const void *a, const void *b));
66
67static BPTR raw_in = (BPTR)NULL;
68static BPTR raw_out = (BPTR)NULL;
69static int close_win = FALSE; /* set if Vim opened the window */
70
71struct IntuitionBase *IntuitionBase = NULL;
72#ifdef FEAT_ARP
73struct ArpBase *ArpBase = NULL;
74#endif
75
76static struct Window *wb_window;
77static char_u *oldwindowtitle = NULL;
78
79#ifdef FEAT_ARP
80int dos2 = FALSE; /* Amiga DOS 2.0x or higher */
81#endif
82int size_set = FALSE; /* set to TRUE if window size was set */
83
84 void
85win_resize_on()
86{
87 OUT_STR_NF("\033[12{");
88}
89
90 void
91win_resize_off()
92{
93 OUT_STR_NF("\033[12}");
94}
95
96 void
97mch_write(p, len)
98 char_u *p;
99 int len;
100{
101 Write(raw_out, (char *)p, (long)len);
102}
103
104/*
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.
110 *
111 * Return number of characters read.
112 */
113 int
114mch_inchar(buf, maxlen, time, tb_change_cnt)
115 char_u *buf;
116 int maxlen;
117 long time; /* milli seconds */
118 int tb_change_cnt;
119{
120 int len;
121 long utime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123 if (time >= 0)
124 {
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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 }
132 else /* time == -1 */
133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 /*
135 * If there is no character available within 2 seconds (default)
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000136 * write the autoscript file to disk. Or cause the CursorHold event
137 * to be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 */
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000139 if (WaitForChar(raw_in, p_ut * 1000L) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 {
141#ifdef FEAT_AUTOCMD
Bram Moolenaare3226be2005-12-18 22:10:00 +0000142 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 {
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000144 buf[0] = K_SPECIAL;
145 buf[1] = KS_EXTRA;
146 buf[2] = (int)KE_CURSORHOLD;
147 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000150 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 }
152 }
153
154 for (;;) /* repeat until we got a character */
155 {
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)
162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#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;
169 }
170 }
171}
172
173/*
174 * return non-zero if a character is available
175 */
176 int
177mch_char_avail()
178{
179 return (WaitForChar(raw_in, 100L) != 0);
180}
181
182/*
183 * Return amount of memory still available.
184 */
185 long_u
186mch_avail_mem(special)
187 int special;
188{
189 return (long_u)AvailMem(special ? (long)MEMF_CHIP : (long)MEMF_ANY);
190}
191
192 void
193mch_delay(msec, ignoreinput)
194 long msec;
195 int ignoreinput;
196{
197#ifndef LATTICE /* SAS declares void Delay(UNLONG) */
198 void Delay __ARGS((long));
199#endif
200
201 if (msec > 0)
202 {
203 if (ignoreinput)
204 Delay(msec / 20L); /* Delay works with 20 msec intervals */
205 else
206 WaitForChar(raw_in, msec * 1000L);
207 }
208}
209
210/*
211 * We have no job control, fake it by starting a new shell.
212 */
213 void
214mch_suspend()
215{
216 suspend_shell();
217}
218
219#ifndef DOS_LIBRARY
220# define DOS_LIBRARY ((UBYTE *)"dos.library")
221#endif
222
223 void
224mch_init()
225{
226 static char intlibname[] = "intuition.library";
227
228#ifdef AZTEC_C
229 Enable_Abort = 0; /* disallow vim to be aborted */
230#endif
231 Columns = 80;
232 Rows = 24;
233
234 /*
235 * Set input and output channels, unless we have opened our own window
236 */
237 if (raw_in == (BPTR)NULL)
238 {
239 raw_in = Input();
240 raw_out = Output();
241 /*
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".
245 */
246 if (!IsInteractive(raw_in))
247 raw_in = raw_out;
248 else if (!IsInteractive(raw_out))
249 raw_out = raw_in;
250 }
251
252 out_flush();
253
254 wb_window = NULL;
255 if ((IntuitionBase = (struct IntuitionBase *)
256 OpenLibrary((UBYTE *)intlibname, 0L)) == NULL)
257 {
258 mch_errmsg(_("cannot open "));
259 mch_errmsg(intlibname);
260 mch_errmsg("!?\n");
261 mch_exit(3);
262 }
263}
264
265#include <workbench/startup.h>
266
267/*
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.
274 *
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.
277 */
278#define BUF2SIZE 320 /* length of buffer for argument with complete path */
279
280 int
281mch_check_win(argc, argv)
282 int argc;
283 char **argv;
284{
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;
300
301/*
302 * check if we are running under DOS 2.0x or higher
303 */
304 DosBase = OpenLibrary(DOS_LIBRARY, 37L);
305 if (DosBase != NULL)
306 /* if (((struct Library *)DOSBase)->lib_Version >= 37) */
307 {
308 CloseLibrary(DosBase);
309#ifdef FEAT_ARP
310 dos2 = TRUE;
311#endif
312 }
313 else /* without arp functions we NEED 2.0 */
314 {
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)))
321 {
322 fprintf(stderr, _("Need %s version %ld\n"), ArpName, ArpVersion);
323 exit(3);
324 }
325#endif
326 }
327
328 /*
329 * scan argv[] for the "-f" and "-d" arguments
330 */
331 for (i = 1; i < argc; ++i)
332 if (argv[i][0] == '-')
333 {
334 switch (argv[i][1])
335 {
336 case 'f':
337 usewin = TRUE;
338 break;
339
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
346 )
347 device = (char_u *)argv[i + 1];
348 break;
349 }
350 }
351
352/*
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.
356 */
357 if (argc != 0
358 && device == NULL
359 && (IsInteractive(Input()) || IsInteractive(Output())))
360 return OK;
361
362/*
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.
366 */
367 if (usewin)
368 {
369 /*
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.
373 */
374 for (i = -1; i < 3; ++i)
375 {
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;
381 }
382 if (raw_in == (BPTR)NULL) /* all three failed */
383 {
384 mch_errmsg(_(winerr));
385 goto exit;
386 }
387 raw_out = raw_in;
388 close_win = TRUE;
389 return OK;
390 }
391
392 if ((nilfh = Open((UBYTE *)"NIL:", (long)MODE_NEWFILE)) == (BPTR)NULL)
393 {
394 mch_errmsg(_("Cannot open NIL:\n"));
395 goto exit;
396 }
397
398 /*
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).
401 */
402 sprintf((char *)buf1, "t:nc%ld", (long)buf1);
403 if ((fh = Open((UBYTE *)buf1, (long)MODE_NEWFILE)) == (BPTR)NULL)
404 {
405 mch_errmsg(_("Cannot create "));
406 mch_errmsg((char *)buf1);
407 mch_errmsg("\n");
408 goto exit;
409 }
410 /*
411 * Write the command into the file, put quotes around the arguments that
412 * have a space in them.
413 */
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)
419 {
420 if (argc == 0)
421 {
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;
435 }
436 else
437 av = argv[i];
438
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
444 )
445 {
446 ++i;
447 continue;
448 }
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);
455 }
456 Write(fh, "\nendcli\n", 8L);
457 Close(fh);
458
459/*
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.
463 */
464 for (i = -1; i < 3; ++i)
465 {
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)
473 {
474#endif
475 if (!SystemTags((UBYTE *)buf2, SYS_UserShell, TRUE, TAG_DONE))
476 break;
477#ifdef FEAT_ARP
478 }
479 else
480 {
481 if (Execute((UBYTE *)buf2, nilfh, nilfh))
482 break;
483 }
484#endif
485 }
486 if (i == 3) /* all three failed */
487 {
488 DeleteFile((UBYTE *)buf1);
489 mch_errmsg(_(winerr));
490 goto exit;
491 }
492 exitval = 0; /* The Execute succeeded: exit this program */
493
494exit:
495#ifdef FEAT_ARP
496 if (ArpBase)
497 CloseLibrary((struct Library *) ArpBase);
498#endif
499 exit(exitval);
500 /* NOTREACHED */
501 return FAIL;
502}
503
504/*
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!
507 */
508 int
509mch_input_isatty()
510{
511 return TRUE;
512}
513
514/*
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.
517 */
518/*ARGSUSED*/
519 void
520fname_case(name, len)
521 char_u *name;
522 int len; /* buffer size, ignored here */
523{
524 struct FileInfoBlock *fib;
525 size_t flen;
526
527 fib = get_fib(name);
528 if (fib != NULL)
529 {
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);
534 }
535}
536
537/*
538 * Get the FileInfoBlock for file "fname"
539 * The returned structure has to be free()d.
540 * Returns NULL on error.
541 */
542 static struct FileInfoBlock *
543get_fib(fname)
544 char_u *fname;
545{
546 BPTR flock;
547 struct FileInfoBlock *fib;
548
549 if (fname == NULL) /* safety check */
550 return NULL;
551 fib = (struct FileInfoBlock *)malloc(sizeof(struct FileInfoBlock));
552 if (fib != NULL)
553 {
554 flock = Lock((UBYTE *)fname, (long)ACCESS_READ);
555 if (flock == (BPTR)NULL || !Examine(flock, fib))
556 {
557 vim_free(fib); /* in case of an error the memory is freed here */
558 fib = NULL;
559 }
560 if (flock)
561 UnLock(flock);
562 }
563 return fib;
564}
565
566#ifdef FEAT_TITLE
567/*
568 * set the title of our window
569 * icon name is not set
570 */
571 void
572mch_settitle(title, icon)
573 char_u *title;
574 char_u *icon;
575{
576 if (wb_window != NULL && title != NULL)
577 SetWindowTitles(wb_window, (UBYTE *)title, (UBYTE *)-1L);
578}
579
580/*
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)
586 */
587 void
588mch_restore_title(which)
589 int which;
590{
591 if (which & 1)
592 mch_settitle(oldwindowtitle, NULL);
593}
594
595 int
596mch_can_restore_title()
597{
598 return (wb_window != NULL);
599}
600
601 int
602mch_can_restore_icon()
603{
604 return FALSE;
605}
606#endif
607
608/*
609 * Insert user name in s[len].
610 */
611 int
612mch_get_user_name(s, len)
613 char_u *s;
614 int len;
615{
616 *s = NUL;
617 return FAIL;
618}
619
620/*
621 * Insert host name is s[len].
622 */
623 void
624mch_get_host_name(s, len)
625 char_u *s;
626 int len;
627{
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000628 vim_strncpy(s, "Amiga", len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629}
630
631/*
632 * return process ID
633 */
634 long
635mch_get_pid()
636{
637 return (long)0;
638}
639
640/*
641 * Get name of current directory into buffer 'buf' of length 'len' bytes.
642 * Return OK for success, FAIL for failure.
643 */
644 int
645mch_dirname(buf, len)
646 char_u *buf;
647 int len;
648{
649 return mch_FullName((char_u *)"", buf, len, FALSE);
650}
651
652/*
653 * get absolute file name into buffer 'buf' of length 'len' bytes
654 *
655 * return FAIL for failure, OK otherwise
656 */
657 int
658mch_FullName(fname, buf, len, force)
659 char_u *fname, *buf;
660 int len;
661 int force;
662{
663 BPTR l;
664 int retval = FAIL;
665 int i;
666
667 /* Lock the file. If it exists, we can get the exact name. */
668 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
669 {
670 retval = lock2name(l, buf, (long)len - 1);
671 UnLock(l);
672 }
673 else if (force || !mch_isFullName(fname)) /* not a full path yet */
674 {
675 /*
676 * If the file cannot be locked (doesn't exist), try to lock the
677 * current directory and concatenate the file name.
678 */
679 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
680 {
681 retval = lock2name(l, buf, (long)len);
682 UnLock(l);
683 if (retval == OK)
684 {
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)
689 {
690 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
691 buf[i++] = '/';
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000692 vim_strncpy(buf + i, fname, len - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694 }
695 }
696 }
697 if (*buf == 0 || *buf == ':')
698 retval = FAIL; /* something failed; use the file name */
699 return retval;
700}
701
702/*
703 * Return TRUE if "fname" does not depend on the current directory.
704 */
705 int
706mch_isFullName(fname)
707 char_u *fname;
708{
709 return (vim_strchr(fname, ':') != NULL && *fname != ':');
710}
711
712/*
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.
715 *
716 * return FAIL for failure, OK otherwise
717 */
718 static int
719lock2name(lock, buf, len)
720 BPTR lock;
721 char_u *buf;
722 long len;
723{
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
732}
733
734/*
735 * get file permissions for 'name'
736 * Returns -1 when it doesn't exist.
737 */
738 long
739mch_getperm(name)
740 char_u *name;
741{
742 struct FileInfoBlock *fib;
743 long retval = -1;
744
745 fib = get_fib(name);
746 if (fib != NULL)
747 {
748 retval = fib->fib_Protection;
749 vim_free(fib);
750 }
751 return retval;
752}
753
754/*
755 * set file permission for 'name' to 'perm'
756 *
757 * return FAIL for failure, OK otherwise
758 */
759 int
760mch_setperm(name, perm)
761 char_u *name;
762 long perm;
763{
764 perm &= ~FIBF_ARCHIVE; /* reset archived bit */
765 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
766}
767
768/*
769 * Set hidden flag for "name".
770 */
771 void
772mch_hide(name)
773 char_u *name;
774{
775 /* can't hide a file */
776}
777
778/*
779 * return FALSE if "name" is not a directory
780 * return TRUE if "name" is a directory.
781 * return FALSE for error.
782 */
783 int
784mch_isdir(name)
785 char_u *name;
786{
787 struct FileInfoBlock *fib;
788 int retval = FALSE;
789
790 fib = get_fib(name);
791 if (fib != NULL)
792 {
793 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
794 vim_free(fib);
795 }
796 return retval;
797}
798
799/*
800 * Create directory "name".
801 */
802 void
803mch_mkdir(name)
804 char_u *name;
805{
806 BPTR lock;
807
808 lock = CreateDir(name);
809 if (lock != NULL)
810 UnLock(lock);
811}
812
813#if defined(FEAT_EVAL) || defined(PROTO)
814/*
815 * Return 1 if "name" can be executed, 0 if not.
816 * Return -1 if unknown.
817 */
818 int
819mch_can_exe(name)
820 char_u *name;
821{
822 /* TODO */
823 return -1;
824}
825#endif
826
827/*
828 * Check what "name" is:
829 * NODE_NORMAL: file or directory (or doesn't exist)
830 * NODE_WRITABLE: writable device, socket, fifo, etc.
831 * NODE_OTHER: non-writable things
832 */
833 int
834mch_nodetype(name)
835 char_u *name;
836{
837 /* TODO */
838 return NODE_NORMAL;
839}
840
841 void
842mch_early_init()
843{
844}
845
846/*
847 * Careful: mch_exit() may be called before mch_init()!
848 */
849 void
850mch_exit(r)
851 int r;
852{
853 if (raw_in) /* put terminal in 'normal' mode */
854 {
855 settmode(TMODE_COOK);
856 stoptermcap();
857 }
858 out_char('\n');
859 if (raw_out)
860 {
861 if (term_console)
862 {
863 win_resize_off(); /* window resize events de-activated */
864 if (size_set)
865 OUT_STR("\233t\233u"); /* reset window size (CSI t CSI u) */
866 }
867 out_flush();
868 }
869
870#ifdef FEAT_TITLE
871 mch_restore_title(3); /* restore window title */
872#endif
873
874 ml_close_all(TRUE); /* remove all memfiles */
875
876#ifdef FEAT_ARP
877 if (ArpBase)
878 CloseLibrary((struct Library *) ArpBase);
879#endif
880 if (close_win)
881 Close(raw_in);
882 if (r)
883 printf(_("Vim exiting with %d\n"), r); /* somehow this makes :cq work!? */
884 exit(r);
885}
886
887/*
888 * This is a routine for setting a given stream to raw or cooked mode on the
889 * Amiga . This is useful when you are using Lattice C to produce programs
890 * that want to read single characters with the "getch()" or "fgetc" call.
891 *
892 * Written : 18-Jun-87 By Chuck McManis.
893 */
894
895#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
896
897/*
898 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
899 * 'cooked' mode. This only works on TTY's.
900 *
901 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
902 * getch() will return immediately rather than wait for a return. You
903 * lose editing features though.
904 *
905 * Cooked: This function returns the designate file pointer to it's normal,
906 * wait for a <CR> mode. This is exactly like raw() except that
907 * it sends a 0 to the console to make it back into a CON: from a RAW:
908 */
909 void
910mch_settmode(tmode)
911 int tmode;
912{
913#ifdef __AROS__
914 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
915#else
916 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
917 tmode == TMODE_RAW ? -1L : 0L) == 0)
918#endif
919 mch_errmsg(_("cannot change console mode ?!\n"));
920}
921
922/*
923 * set screen mode, always fails.
924 */
925 int
926mch_screenmode(arg)
927 char_u *arg;
928{
929 EMSG(_(e_screenmode));
930 return FAIL;
931}
932
933/*
934 * Code for this routine came from the following :
935 *
936 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
937 * DOS packet example
938 * Requires 1.2
939 *
940 * Found on Fish Disk 56.
941 *
942 * Heavely modified by mool.
943 */
944
945#include <devices/conunit.h>
946
947/*
948 * try to get the real window size
949 * return FAIL for failure, OK otherwise
950 */
951 int
952mch_get_shellsize()
953{
954 struct ConUnit *conUnit;
955 char id_a[sizeof(struct InfoData) + 3];
956 struct InfoData *id;
957
958 if (!term_console) /* not an amiga window */
959 return FAIL;
960
961 /* insure longword alignment */
962 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
963
964 /*
965 * Should make console aware of real window size, not the one we set.
966 * Unfortunately, under DOS 2.0x this redraws the window and it
967 * is rarely needed, so we skip it now, unless we changed the size.
968 */
969 if (size_set)
970 OUT_STR("\233t\233u"); /* CSI t CSI u */
971 out_flush();
972
973#ifdef __AROS__
974 if (!Info(raw_out, id)
975 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
976#else
977 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
978 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
979#endif
980 {
981 /* it's not an amiga window, maybe aux device */
982 /* terminal type should be set */
983 term_console = FALSE;
984 return FAIL;
985 }
986 if (oldwindowtitle == NULL)
987 oldwindowtitle = (char_u *)wb_window->Title;
988 if (id->id_InUse == (BPTR)NULL)
989 {
990 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
991 return FAIL;
992 }
993 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
994
995 /* get window size */
996 Rows = conUnit->cu_YMax + 1;
997 Columns = conUnit->cu_XMax + 1;
998 if (Rows < 0 || Rows > 200) /* cannot be an amiga window */
999 {
1000 Columns = 80;
1001 Rows = 24;
1002 term_console = FALSE;
1003 return FAIL;
1004 }
1005
1006 return OK;
1007}
1008
1009/*
1010 * Try to set the real window size to Rows and Columns.
1011 */
1012 void
1013mch_set_shellsize()
1014{
1015 if (term_console)
1016 {
1017 size_set = TRUE;
1018 out_char(CSI);
1019 out_num((long)Rows);
1020 out_char('t');
1021 out_char(CSI);
1022 out_num((long)Columns);
1023 out_char('u');
1024 out_flush();
1025 }
1026}
1027
1028/*
1029 * Rows and/or Columns has changed.
1030 */
1031 void
1032mch_new_shellsize()
1033{
1034 /* Nothing to do. */
1035}
1036
1037/*
1038 * out_num - output a (big) number fast
1039 */
1040 static void
1041out_num(n)
1042 long n;
1043{
1044 OUT_STR_NF(tltoa((unsigned long)n));
1045}
1046
1047#if !defined(AZTEC_C) && !defined(__AROS__)
1048/*
1049 * Sendpacket.c
1050 *
1051 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1052 * the given message port. This makes working around DOS lots easier.
1053 *
1054 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1055 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1056 * say 'oml lib:amiga.lib -r sendpacket.o'
1057 */
1058
1059/* #include <proto/exec.h> */
1060/* #include <proto/dos.h> */
1061#include <exec/memory.h>
1062
1063/*
1064 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1065 * Finkel. This function will send a packet of the given type to the Message
1066 * Port supplied.
1067 */
1068
1069 static long
1070dos_packet(pid, action, arg)
1071 struct MsgPort *pid; /* process indentifier ... (handlers message port) */
1072 long action, /* packet type ... (what you want handler to do) */
1073 arg; /* single argument */
1074{
1075# ifdef FEAT_ARP
1076 struct MsgPort *replyport;
1077 struct StandardPacket *packet;
1078 long res1;
1079
1080 if (dos2)
1081# endif
1082 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); /* use 2.0 function */
1083# ifdef FEAT_ARP
1084
1085 replyport = (struct MsgPort *) CreatePort(NULL, 0); /* use arp function */
1086 if (!replyport)
1087 return (0);
1088
1089 /* Allocate space for a packet, make it public and clear it */
1090 packet = (struct StandardPacket *)
1091 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
1092 if (!packet) {
1093 DeletePort(replyport);
1094 return (0);
1095 }
1096 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1097 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1098 packet->sp_Pkt.dp_Port = replyport;
1099 packet->sp_Pkt.dp_Type = action;
1100 packet->sp_Pkt.dp_Arg1 = arg;
1101
1102 PutMsg(pid, (struct Message *)packet); /* send packet */
1103
1104 WaitPort(replyport);
1105 GetMsg(replyport);
1106
1107 res1 = packet->sp_Pkt.dp_Res1;
1108
1109 FreeMem(packet, (long) sizeof(struct StandardPacket));
1110 DeletePort(replyport);
1111
1112 return (res1);
1113# endif
1114}
1115#endif /* !defined(AZTEC_C) && !defined(__AROS__) */
1116
1117/*
1118 * Call shell.
1119 * Return error number for failure, 0 otherwise
1120 */
1121 int
1122mch_call_shell(cmd, options)
1123 char_u *cmd;
1124 int options; /* SHELL_*, see vim.h */
1125{
1126 BPTR mydir;
1127 int x;
1128 int tmode = cur_tmode;
1129#ifdef AZTEC_C
1130 int use_execute;
1131 char_u *shellcmd = NULL;
1132 char_u *shellarg;
1133#endif
1134 int retval = 0;
1135
1136 if (close_win)
1137 {
1138 /* if Vim opened a window: Executing a shell may cause crashes */
1139 EMSG(_("E360: Cannot execute shell with -f option"));
1140 return -1;
1141 }
1142
1143 if (term_console)
1144 win_resize_off(); /* window resize events de-activated */
1145 out_flush();
1146
1147 if (options & SHELL_COOKED)
1148 settmode(TMODE_COOK); /* set to normal mode */
1149 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); /* remember current dir */
1150
1151#if !defined(AZTEC_C) /* not tested very much */
1152 if (cmd == NULL)
1153 {
1154# ifdef FEAT_ARP
1155 if (dos2)
1156# endif
1157 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1158# ifdef FEAT_ARP
1159 else
1160 x = Execute(p_sh, raw_in, raw_out);
1161# endif
1162 }
1163 else
1164 {
1165# ifdef FEAT_ARP
1166 if (dos2)
1167# endif
1168 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1169# ifdef FEAT_ARP
1170 else
1171 x = Execute((char *)cmd, 0L, raw_out);
1172# endif
1173 }
1174# ifdef FEAT_ARP
1175 if ((dos2 && x < 0) || (!dos2 && !x))
1176# else
1177 if (x < 0)
1178# endif
1179 {
1180 MSG_PUTS(_("Cannot execute "));
1181 if (cmd == NULL)
1182 {
1183 MSG_PUTS(_("shell "));
1184 msg_outtrans(p_sh);
1185 }
1186 else
1187 msg_outtrans(cmd);
1188 msg_putchar('\n');
1189 retval = -1;
1190 }
1191# ifdef FEAT_ARP
1192 else if (!dos2 || x)
1193# else
1194 else if (x)
1195# endif
1196 {
1197 if ((x = IoErr()) != 0)
1198 {
1199 if (!(options & SHELL_SILENT))
1200 {
1201 msg_putchar('\n');
1202 msg_outnum((long)x);
1203 MSG_PUTS(_(" returned\n"));
1204 }
1205 retval = x;
1206 }
1207 }
1208#else /* else part is for AZTEC_C */
1209 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1210 use_execute = 1;
1211 else
1212 use_execute = 0;
1213 if (!use_execute)
1214 {
1215 /*
1216 * separate shell name from argument
1217 */
1218 shellcmd = vim_strsave(p_sh);
1219 if (shellcmd == NULL) /* out of memory, use Execute */
1220 use_execute = 1;
1221 else
1222 {
1223 shellarg = skiptowhite(shellcmd); /* find start of arguments */
1224 if (*shellarg != NUL)
1225 {
1226 *shellarg++ = NUL;
1227 shellarg = skipwhite(shellarg);
1228 }
1229 }
1230 }
1231 if (cmd == NULL)
1232 {
1233 if (use_execute)
1234 {
1235# ifdef FEAT_ARP
1236 if (dos2)
1237# endif
1238 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1239# ifdef FEAT_ARP
1240 else
1241 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1242# endif
1243 }
1244 else
1245 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1246 }
1247 else if (use_execute)
1248 {
1249# ifdef FEAT_ARP
1250 if (dos2)
1251# endif
1252 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1253# ifdef FEAT_ARP
1254 else
1255 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1256# endif
1257 }
1258 else if (p_st & 1)
1259 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1260 (char *)cmd, NULL);
1261 else
1262 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1263 (char *)p_shcf, (char *)cmd, NULL);
1264# ifdef FEAT_ARP
1265 if ((dos2 && x < 0) || (!dos2 && x))
1266# else
1267 if (x < 0)
1268# endif
1269 {
1270 MSG_PUTS(_("Cannot execute "));
1271 if (use_execute)
1272 {
1273 if (cmd == NULL)
1274 msg_outtrans(p_sh);
1275 else
1276 msg_outtrans(cmd);
1277 }
1278 else
1279 {
1280 MSG_PUTS(_("shell "));
1281 msg_outtrans(shellcmd);
1282 }
1283 msg_putchar('\n');
1284 retval = -1;
1285 }
1286 else
1287 {
1288 if (use_execute)
1289 {
1290# ifdef FEAT_ARP
1291 if (!dos2 || x)
1292# else
1293 if (x)
1294# endif
1295 x = IoErr();
1296 }
1297 else
1298 x = wait();
1299 if (x)
1300 {
1301 if (!(options & SHELL_SILENT) && !emsg_silent)
1302 {
1303 msg_putchar('\n');
1304 msg_outnum((long)x);
1305 MSG_PUTS(_(" returned\n"));
1306 }
1307 retval = x;
1308 }
1309 }
1310 vim_free(shellcmd);
1311#endif /* AZTEC_C */
1312
1313 if ((mydir = CurrentDir(mydir)) != 0) /* make sure we stay in the same directory */
1314 UnLock(mydir);
1315 if (tmode == TMODE_RAW)
1316 settmode(TMODE_RAW); /* set to raw mode */
1317#ifdef FEAT_TITLE
1318 resettitle();
1319#endif
1320 if (term_console)
1321 win_resize_on(); /* window resize events activated */
1322 return retval;
1323}
1324
1325/*
1326 * check for an "interrupt signal"
1327 * We only react to a CTRL-C, but also clear the other break signals to avoid
1328 * trouble with lattice-c programs.
1329 */
1330 void
1331mch_breakcheck()
1332{
1333 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1334 got_int = TRUE;
1335}
1336
1337/* this routine causes manx to use this Chk_Abort() rather than it's own */
1338/* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
1339/* is zero). Since we want to check for our own ^C's */
1340
1341#ifdef _DCC
1342#define Chk_Abort chkabort
1343#endif
1344
1345#ifdef LATTICE
1346void __regargs __chkabort(void);
1347
1348void __regargs __chkabort(void)
1349{}
1350
1351#else
1352 long
1353Chk_Abort(void)
1354{
1355 return(0L);
1356}
1357#endif
1358
1359/*
1360 * mch_expandpath() - this code does wild-card pattern matching using the arp
1361 * routines.
1362 *
1363 * "pat" has backslashes before chars that are not to be expanded.
1364 * Returns the number of matches found.
1365 *
1366 * This is based on WildDemo2.c (found in arp1.1 distribution).
1367 * That code's copyright follows:
1368 * Copyright (c) 1987, Scott Ballantyne
1369 * Use and abuse as you please.
1370 */
1371
1372#define ANCHOR_BUF_SIZE (512)
1373#define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1374
1375 int
1376mch_expandpath(gap, pat, flags)
1377 garray_T *gap;
1378 char_u *pat;
1379 int flags; /* EW_* flags */
1380{
1381 struct AnchorPath *Anchor;
1382 LONG Result;
1383 char_u *starbuf, *sp, *dp;
1384 int start_len;
1385 int matches;
1386
1387 start_len = gap->ga_len;
1388
1389 /* Get our AnchorBase */
1390 Anchor = (struct AnchorPath *)alloc_clear((unsigned)ANCHOR_SIZE);
1391 if (Anchor == NULL)
1392 return 0;
1393
1394 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; /* ap_Length not supported anymore */
1395#ifdef APF_DODOT
1396 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; /* allow '.' for current dir */
1397#else
1398 Anchor->ap_Flags = APF_DoDot | APF_DoWild; /* allow '.' for current dir */
1399#endif
1400
1401#ifdef FEAT_ARP
1402 if (dos2)
1403 {
1404#endif
1405 /* hack to replace '*' by '#?' */
1406 starbuf = alloc((unsigned)(2 * STRLEN(pat) + 1));
1407 if (starbuf == NULL)
1408 goto Return;
1409 for (sp = pat, dp = starbuf; *sp; ++sp)
1410 {
1411 if (*sp == '*')
1412 {
1413 *dp++ = '#';
1414 *dp++ = '?';
1415 }
1416 else
1417 *dp++ = *sp;
1418 }
1419 *dp = NUL;
1420 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1421 vim_free(starbuf);
1422#ifdef FEAT_ARP
1423 }
1424 else
1425 Result = FindFirst((char *)pat, Anchor);
1426#endif
1427
1428 /*
1429 * Loop to get all matches.
1430 */
1431 while (Result == 0)
1432 {
1433 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
1434#ifdef FEAT_ARP
1435 if (dos2)
1436#endif
1437 Result = MatchNext(Anchor);
1438#ifdef FEAT_ARP
1439 else
1440 Result = FindNext(Anchor);
1441#endif
1442 }
1443 matches = gap->ga_len - start_len;
1444
1445 if (Result == ERROR_BUFFER_OVERFLOW)
1446 EMSG(_("ANCHOR_BUF_SIZE too small."));
1447 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1448 && Result != ERROR_DEVICE_NOT_MOUNTED
1449 && Result != ERROR_NO_MORE_ENTRIES)
1450 EMSG(_("I/O ERROR"));
1451
1452 /*
1453 * Sort the files for this pattern.
1454 */
1455 if (matches)
1456 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1457 (size_t)matches, sizeof(char_u *), sortcmp);
1458
1459 /* Free the wildcard stuff */
1460#ifdef FEAT_ARP
1461 if (dos2)
1462#endif
1463 MatchEnd(Anchor);
1464#ifdef FEAT_ARP
1465 else
1466 FreeAnchorChain(Anchor);
1467#endif
1468
1469Return:
1470 vim_free(Anchor);
1471
1472 return matches;
1473}
1474
1475 static int
1476sortcmp(a, b)
1477 const void *a, *b;
1478{
1479 char *s = *(char **)a;
1480 char *t = *(char **)b;
1481
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001482 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483}
1484
1485/*
1486 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1487 */
1488 int
1489mch_has_exp_wildcard(p)
1490 char_u *p;
1491{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001492 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
1494 if (*p == '\\' && p[1] != NUL)
1495 ++p;
1496 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1497 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499 return FALSE;
1500}
1501
1502 int
1503mch_has_wildcard(p)
1504 char_u *p;
1505{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001506 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 if (*p == '\\' && p[1] != NUL)
1509 ++p;
1510 else
1511 if (vim_strchr((char_u *)
1512# ifdef VIM_BACKTICK
1513 "*?[(#$`"
1514# else
1515 "*?[(#$"
1516# endif
1517 , *p) != NULL
1518 || (*p == '~' && p[1] != NUL))
1519 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 }
1521 return FALSE;
1522}
1523
1524/*
1525 * With AmigaDOS 2.0 support for reading local environment variables
1526 *
1527 * Two buffers are allocated:
1528 * - A big one to do the expansion into. It is freed before returning.
1529 * - A small one to hold the return value. It is kept until the next call.
1530 */
1531 char_u *
1532mch_getenv(var)
1533 char_u *var;
1534{
1535 int len;
1536 UBYTE *buf; /* buffer to expand in */
1537 char_u *retval; /* return value */
1538 static char_u *alloced = NULL; /* allocated memory */
1539
1540#ifdef FEAT_ARP
1541 if (!dos2)
1542 retval = (char_u *)getenv((char *)var);
1543 else
1544#endif
1545 {
1546 vim_free(alloced);
1547 alloced = NULL;
1548 retval = NULL;
1549
1550 buf = alloc(IOSIZE);
1551 if (buf == NULL)
1552 return NULL;
1553
1554 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1555 if (len >= 0)
1556 {
1557 retval = vim_strsave((char_u *)buf);
1558 alloced = retval;
1559 }
1560
1561 vim_free(buf);
1562 }
1563
1564 /* if $VIM is not defined, use "vim:" instead */
1565 if (retval == NULL && STRCMP(var, "VIM") == 0)
1566 retval = (char_u *)"vim:";
1567
1568 return retval;
1569}
1570
1571/*
1572 * Amiga version of setenv() with AmigaDOS 2.0 support.
1573 */
1574/* ARGSUSED */
1575 int
1576mch_setenv(var, value, x)
1577 char *var;
1578 char *value;
1579 int x;
1580{
1581#ifdef FEAT_ARP
1582 if (!dos2)
1583 return setenv(var, value);
1584#endif
1585
1586 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
1587 return 0; /* success */
1588 return -1; /* failure */
1589}