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