blob: 324fc367b7c475e69560d6f7b37cffa8a53e3116 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#undef TRUE /* will be redefined by exec/types.h */
23#undef FALSE
24
Bram Moolenaar82881492012-11-20 16:53:39 +010025/* cproto fails on missing include files, skip them */
26#ifndef PROTO
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#ifndef LATTICE
29# include <exec/types.h>
30# include <exec/exec.h>
31# include <libraries/dos.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# include <intuition/intuition.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#endif
34
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000035/* XXX These are included from os_amiga.h
36#include <proto/exec.h>
37#include <proto/dos.h>
38#include <proto/intuition.h>
39*/
40
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#include <exec/memory.h>
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000042#include <libraries/dosextens.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44#include <dos/dostags.h> /* for 2.0 functions */
45#include <dos/dosasl.h>
46
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000047/* From version 4 of AmigaOS, several system structures must be allocated
48 * and freed using system functions. "struct AnchorPath" is one.
49 */
50#ifdef __amigaos4__
51# include <dos/anchorpath.h>
52# define free_fib(x) FreeDosObject(DOS_FIB, x)
53#else
54# define free_fib(x) vim_free(fib)
55#endif
56
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#if defined(LATTICE) && !defined(SASC) && defined(FEAT_ARP)
58# include <libraries/arp_pragmas.h>
59#endif
60
Bram Moolenaar82881492012-11-20 16:53:39 +010061#endif /* PROTO */
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063/*
64 * At this point TRUE and FALSE are defined as 1L and 0L, but we want 1 and 0.
65 */
66#undef TRUE
67#define TRUE (1)
68#undef FALSE
69#define FALSE (0)
70
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000071#ifdef __amigaos4__
72# define dos_packet(a, b, c) DoPkt(a, b, c, 0, 0, 0, 0)
73#elif !defined(AZTEC_C) && !defined(__AROS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +000074static long dos_packet __ARGS((struct MsgPort *, long, long));
75#endif
76static int lock2name __ARGS((BPTR lock, char_u *buf, long len));
77static void out_num __ARGS((long n));
78static struct FileInfoBlock *get_fib __ARGS((char_u *));
79static int sortcmp __ARGS((const void *a, const void *b));
80
81static BPTR raw_in = (BPTR)NULL;
82static BPTR raw_out = (BPTR)NULL;
83static int close_win = FALSE; /* set if Vim opened the window */
84
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000085#ifndef __amigaos4__ /* Use autoopen for AmigaOS4 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000086struct IntuitionBase *IntuitionBase = NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088#ifdef FEAT_ARP
89struct ArpBase *ArpBase = NULL;
90#endif
91
92static struct Window *wb_window;
93static char_u *oldwindowtitle = NULL;
94
95#ifdef FEAT_ARP
96int dos2 = FALSE; /* Amiga DOS 2.0x or higher */
97#endif
98int size_set = FALSE; /* set to TRUE if window size was set */
99
100 void
101win_resize_on()
102{
103 OUT_STR_NF("\033[12{");
104}
105
106 void
107win_resize_off()
108{
109 OUT_STR_NF("\033[12}");
110}
111
112 void
113mch_write(p, len)
114 char_u *p;
115 int len;
116{
117 Write(raw_out, (char *)p, (long)len);
118}
119
120/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200121 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 * Get a characters from the keyboard.
123 * If time == 0 do not wait for characters.
124 * If time == n wait a short time for characters.
125 * If time == -1 wait forever for characters.
126 *
127 * Return number of characters read.
128 */
129 int
130mch_inchar(buf, maxlen, time, tb_change_cnt)
131 char_u *buf;
132 int maxlen;
133 long time; /* milli seconds */
134 int tb_change_cnt;
135{
136 int len;
137 long utime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139 if (time >= 0)
140 {
141 if (time == 0)
142 utime = 100L; /* time = 0 causes problems in DOS 1.2 */
143 else
144 utime = time * 1000L; /* convert from milli to micro secs */
145 if (WaitForChar(raw_in, utime) == 0) /* no character available */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 }
148 else /* time == -1 */
149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 /*
151 * If there is no character available within 2 seconds (default)
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000152 * write the autoscript file to disk. Or cause the CursorHold event
153 * to be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 */
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000155 if (WaitForChar(raw_in, p_ut * 1000L) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 {
157#ifdef FEAT_AUTOCMD
Bram Moolenaare3226be2005-12-18 22:10:00 +0000158 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 {
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000160 buf[0] = K_SPECIAL;
161 buf[1] = KS_EXTRA;
162 buf[2] = (int)KE_CURSORHOLD;
163 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000166 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 }
168 }
169
170 for (;;) /* repeat until we got a character */
171 {
172# ifdef FEAT_MBYTE
173 len = Read(raw_in, (char *)buf, (long)maxlen / input_conv.vc_factor);
174# else
175 len = Read(raw_in, (char *)buf, (long)maxlen);
176# endif
177 if (len > 0)
178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179#ifdef FEAT_MBYTE
180 /* Convert from 'termencoding' to 'encoding'. */
181 if (input_conv.vc_type != CONV_NONE)
182 len = convert_input(buf, len, maxlen);
183#endif
184 return len;
185 }
186 }
187}
188
189/*
190 * return non-zero if a character is available
191 */
192 int
193mch_char_avail()
194{
195 return (WaitForChar(raw_in, 100L) != 0);
196}
197
198/*
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200199 * Return amount of memory still available in Kbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 */
201 long_u
202mch_avail_mem(special)
203 int special;
204{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000205#ifdef __amigaos4__
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200206 return (long_u)AvailMem(MEMF_ANY) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000207#else
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200208 return (long_u)(AvailMem(special ? (long)MEMF_CHIP : (long)MEMF_ANY)) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210}
211
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000212/*
213 * Waits a specified amount of time, or until input arrives if
214 * ignoreinput is FALSE.
215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 void
217mch_delay(msec, ignoreinput)
218 long msec;
219 int ignoreinput;
220{
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000221#ifndef LATTICE /* SAS declares void Delay(ULONG) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 void Delay __ARGS((long));
223#endif
224
225 if (msec > 0)
226 {
227 if (ignoreinput)
228 Delay(msec / 20L); /* Delay works with 20 msec intervals */
229 else
230 WaitForChar(raw_in, msec * 1000L);
231 }
232}
233
234/*
235 * We have no job control, fake it by starting a new shell.
236 */
237 void
238mch_suspend()
239{
240 suspend_shell();
241}
242
243#ifndef DOS_LIBRARY
244# define DOS_LIBRARY ((UBYTE *)"dos.library")
245#endif
246
247 void
248mch_init()
249{
250 static char intlibname[] = "intuition.library";
251
252#ifdef AZTEC_C
253 Enable_Abort = 0; /* disallow vim to be aborted */
254#endif
255 Columns = 80;
256 Rows = 24;
257
258 /*
259 * Set input and output channels, unless we have opened our own window
260 */
261 if (raw_in == (BPTR)NULL)
262 {
263 raw_in = Input();
264 raw_out = Output();
265 /*
266 * If Input() is not interactive, then Output() will be (because of
267 * check in mch_check_win()). Used for "Vim -".
268 * Also check the other way around, for "Vim -h | more".
269 */
270 if (!IsInteractive(raw_in))
271 raw_in = raw_out;
272 else if (!IsInteractive(raw_out))
273 raw_out = raw_in;
274 }
275
276 out_flush();
277
278 wb_window = NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000279#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 if ((IntuitionBase = (struct IntuitionBase *)
281 OpenLibrary((UBYTE *)intlibname, 0L)) == NULL)
282 {
283 mch_errmsg(_("cannot open "));
284 mch_errmsg(intlibname);
285 mch_errmsg("!?\n");
286 mch_exit(3);
287 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289}
290
Bram Moolenaar82881492012-11-20 16:53:39 +0100291#ifndef PROTO
292# include <workbench/startup.h>
293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294
295/*
296 * Check_win checks whether we have an interactive window.
297 * If not, a new window is opened with the newcli command.
298 * If we would open a window ourselves, the :sh and :! commands would not
299 * work properly (Why? probably because we are then running in a background
300 * CLI). This also is the best way to assure proper working in a next
301 * Workbench release.
302 *
303 * For the -f option (foreground mode) we open our own window and disable :sh.
304 * Otherwise the calling program would never know when editing is finished.
305 */
306#define BUF2SIZE 320 /* length of buffer for argument with complete path */
307
308 int
309mch_check_win(argc, argv)
310 int argc;
311 char **argv;
312{
313 int i;
314 BPTR nilfh, fh;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000315 char_u buf1[24];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 char_u buf2[BUF2SIZE];
317 static char_u *(constrings[3]) = {(char_u *)"con:0/0/662/210/",
318 (char_u *)"con:0/0/640/200/",
319 (char_u *)"con:0/0/320/200/"};
320 static char_u *winerr = (char_u *)N_("VIM: Can't open window!\n");
321 struct WBArg *argp;
322 int ac;
323 char *av;
324 char_u *device = NULL;
325 int exitval = 4;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000326#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 struct Library *DosBase;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 int usewin = FALSE;
330
331/*
332 * check if we are running under DOS 2.0x or higher
333 */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000334#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 DosBase = OpenLibrary(DOS_LIBRARY, 37L);
336 if (DosBase != NULL)
337 /* if (((struct Library *)DOSBase)->lib_Version >= 37) */
338 {
339 CloseLibrary(DosBase);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000340# ifdef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 dos2 = TRUE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000342# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 }
344 else /* without arp functions we NEED 2.0 */
345 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000346# ifndef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 mch_errmsg(_("Need Amigados version 2.04 or later\n"));
348 exit(3);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000349# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 /* need arp functions for dos 1.x */
351 if (!(ArpBase = (struct ArpBase *) OpenLibrary((UBYTE *)ArpName, ArpVersion)))
352 {
353 fprintf(stderr, _("Need %s version %ld\n"), ArpName, ArpVersion);
354 exit(3);
355 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000358#endif /* __amigaos4__ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
360 /*
361 * scan argv[] for the "-f" and "-d" arguments
362 */
363 for (i = 1; i < argc; ++i)
364 if (argv[i][0] == '-')
365 {
366 switch (argv[i][1])
367 {
368 case 'f':
369 usewin = TRUE;
370 break;
371
372 case 'd':
373 if (i < argc - 1
374#ifdef FEAT_DIFF
375 /* require using "-dev", "-d" means diff mode */
376 && argv[i][2] == 'e' && argv[i][3] == 'v'
377#endif
378 )
379 device = (char_u *)argv[i + 1];
380 break;
381 }
382 }
383
384/*
385 * If we were not started from workbench, do not have a "-d" or "-dev"
386 * argument and we have been started with an interactive window, use that
387 * window.
388 */
389 if (argc != 0
390 && device == NULL
391 && (IsInteractive(Input()) || IsInteractive(Output())))
392 return OK;
393
394/*
395 * When given the "-f" argument, we open our own window. We can't use the
396 * newcli trick below, because the calling program (mail, rn, etc.) would not
397 * know when we are finished.
398 */
399 if (usewin)
400 {
401 /*
402 * Try to open a window. First try the specified device.
403 * Then try a 24 line 80 column window.
404 * If that fails, try two smaller ones.
405 */
406 for (i = -1; i < 3; ++i)
407 {
408 if (i >= 0)
409 device = constrings[i];
410 if (device != NULL && (raw_in = Open((UBYTE *)device,
411 (long)MODE_NEWFILE)) != (BPTR)NULL)
412 break;
413 }
414 if (raw_in == (BPTR)NULL) /* all three failed */
415 {
416 mch_errmsg(_(winerr));
417 goto exit;
418 }
419 raw_out = raw_in;
420 close_win = TRUE;
421 return OK;
422 }
423
424 if ((nilfh = Open((UBYTE *)"NIL:", (long)MODE_NEWFILE)) == (BPTR)NULL)
425 {
426 mch_errmsg(_("Cannot open NIL:\n"));
427 goto exit;
428 }
429
430 /*
431 * Make a unique name for the temp file (which we will not delete!).
432 * Use a pointer on the stack (nobody else will be using it).
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000433 * Under AmigaOS4, this assumption might change in the future, so
434 * we use a pointer to the current task instead. This should be a
435 * shared structure and thus globally unique.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000437#ifdef __amigaos4__
438 sprintf((char *)buf1, "t:nc%p", FindTask(0));
439#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 sprintf((char *)buf1, "t:nc%ld", (long)buf1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if ((fh = Open((UBYTE *)buf1, (long)MODE_NEWFILE)) == (BPTR)NULL)
443 {
444 mch_errmsg(_("Cannot create "));
445 mch_errmsg((char *)buf1);
446 mch_errmsg("\n");
447 goto exit;
448 }
449 /*
450 * Write the command into the file, put quotes around the arguments that
451 * have a space in them.
452 */
453 if (argc == 0) /* run from workbench */
454 ac = ((struct WBStartup *)argv)->sm_NumArgs;
455 else
456 ac = argc;
457 for (i = 0; i < ac; ++i)
458 {
459 if (argc == 0)
460 {
461 *buf2 = NUL;
462 argp = &(((struct WBStartup *)argv)->sm_ArgList[i]);
463 if (argp->wa_Lock)
464 (void)lock2name(argp->wa_Lock, buf2, (long)(BUF2SIZE - 1));
465#ifdef FEAT_ARP
466 if (dos2) /* use 2.0 function */
467#endif
468 AddPart((UBYTE *)buf2, (UBYTE *)argp->wa_Name, (long)(BUF2SIZE - 1));
469#ifdef FEAT_ARP
470 else /* use arp function */
471 TackOn((char *)buf2, argp->wa_Name);
472#endif
473 av = (char *)buf2;
474 }
475 else
476 av = argv[i];
477
478 /* skip '-d' or "-dev" option */
479 if (av[0] == '-' && av[1] == 'd'
480#ifdef FEAT_DIFF
481 && av[2] == 'e' && av[3] == 'v'
482#endif
483 )
484 {
485 ++i;
486 continue;
487 }
488 if (vim_strchr((char_u *)av, ' '))
489 Write(fh, "\"", 1L);
490 Write(fh, av, (long)strlen(av));
491 if (vim_strchr((char_u *)av, ' '))
492 Write(fh, "\"", 1L);
493 Write(fh, " ", 1L);
494 }
495 Write(fh, "\nendcli\n", 8L);
496 Close(fh);
497
498/*
499 * Try to open a new cli in a window. If "-d" or "-dev" argument was given try
500 * to open the specified device. Then try a 24 line 80 column window. If that
501 * fails, try two smaller ones.
502 */
503 for (i = -1; i < 3; ++i)
504 {
505 if (i >= 0)
506 device = constrings[i];
507 else if (device == NULL)
508 continue;
509 sprintf((char *)buf2, "newcli <nil: >nil: %s from %s", (char *)device, (char *)buf1);
510#ifdef FEAT_ARP
511 if (dos2)
512 {
513#endif
514 if (!SystemTags((UBYTE *)buf2, SYS_UserShell, TRUE, TAG_DONE))
515 break;
516#ifdef FEAT_ARP
517 }
518 else
519 {
520 if (Execute((UBYTE *)buf2, nilfh, nilfh))
521 break;
522 }
523#endif
524 }
525 if (i == 3) /* all three failed */
526 {
527 DeleteFile((UBYTE *)buf1);
528 mch_errmsg(_(winerr));
529 goto exit;
530 }
531 exitval = 0; /* The Execute succeeded: exit this program */
532
533exit:
534#ifdef FEAT_ARP
535 if (ArpBase)
536 CloseLibrary((struct Library *) ArpBase);
537#endif
538 exit(exitval);
539 /* NOTREACHED */
540 return FAIL;
541}
542
543/*
544 * Return TRUE if the input comes from a terminal, FALSE otherwise.
545 * We fake there is a window, because we can always open one!
546 */
547 int
548mch_input_isatty()
549{
550 return TRUE;
551}
552
553/*
554 * fname_case(): Set the case of the file name, if it already exists.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000555 * This will cause the file name to remain exactly the same
556 * if the file system ignores, but preserves case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 */
558/*ARGSUSED*/
559 void
560fname_case(name, len)
561 char_u *name;
562 int len; /* buffer size, ignored here */
563{
564 struct FileInfoBlock *fib;
565 size_t flen;
566
567 fib = get_fib(name);
568 if (fib != NULL)
569 {
570 flen = STRLEN(name);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000571 /* TODO: Check if this fix applies to AmigaOS < 4 too.*/
572#ifdef __amigaos4__
573 if (fib->fib_DirEntryType == ST_ROOT)
574 strcat(fib->fib_FileName, ":");
575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (flen == strlen(fib->fib_FileName)) /* safety check */
577 mch_memmove(name, fib->fib_FileName, flen);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000578 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580}
581
582/*
583 * Get the FileInfoBlock for file "fname"
584 * The returned structure has to be free()d.
585 * Returns NULL on error.
586 */
587 static struct FileInfoBlock *
588get_fib(fname)
589 char_u *fname;
590{
591 BPTR flock;
592 struct FileInfoBlock *fib;
593
594 if (fname == NULL) /* safety check */
595 return NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000596#ifdef __amigaos4__
597 fib = AllocDosObject(DOS_FIB,0);
598#else
599 fib = (struct FileInfoBlock *)alloc(sizeof(struct FileInfoBlock));
600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 if (fib != NULL)
602 {
603 flock = Lock((UBYTE *)fname, (long)ACCESS_READ);
604 if (flock == (BPTR)NULL || !Examine(flock, fib))
605 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000606 free_fib(fib); /* in case of an error the memory is freed here */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 fib = NULL;
608 }
609 if (flock)
610 UnLock(flock);
611 }
612 return fib;
613}
614
615#ifdef FEAT_TITLE
616/*
617 * set the title of our window
618 * icon name is not set
619 */
620 void
621mch_settitle(title, icon)
622 char_u *title;
623 char_u *icon;
624{
625 if (wb_window != NULL && title != NULL)
626 SetWindowTitles(wb_window, (UBYTE *)title, (UBYTE *)-1L);
627}
628
629/*
630 * Restore the window/icon title.
631 * which is one of:
632 * 1 Just restore title
633 * 2 Just restore icon (which we don't have)
634 * 3 Restore title and icon (which we don't have)
635 */
636 void
637mch_restore_title(which)
638 int which;
639{
640 if (which & 1)
641 mch_settitle(oldwindowtitle, NULL);
642}
643
644 int
645mch_can_restore_title()
646{
647 return (wb_window != NULL);
648}
649
650 int
651mch_can_restore_icon()
652{
653 return FALSE;
654}
655#endif
656
657/*
658 * Insert user name in s[len].
659 */
660 int
661mch_get_user_name(s, len)
662 char_u *s;
663 int len;
664{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000665 /* TODO: Implement this. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 *s = NUL;
667 return FAIL;
668}
669
670/*
671 * Insert host name is s[len].
672 */
673 void
674mch_get_host_name(s, len)
675 char_u *s;
676 int len;
677{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000678#if defined(__amigaos4__) && defined(__CLIB2__)
679 gethostname(s, len);
680#else
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000681 vim_strncpy(s, "Amiga", len - 1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683}
684
685/*
686 * return process ID
687 */
688 long
689mch_get_pid()
690{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000691#ifdef __amigaos4__
692 /* This is as close to a pid as we can come. We could use CLI numbers also,
693 * but then we would have two different types of process identifiers.
694 */
695 return((long)FindTask(0));
696#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 return (long)0;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699}
700
701/*
702 * Get name of current directory into buffer 'buf' of length 'len' bytes.
703 * Return OK for success, FAIL for failure.
704 */
705 int
706mch_dirname(buf, len)
707 char_u *buf;
708 int len;
709{
710 return mch_FullName((char_u *)"", buf, len, FALSE);
711}
712
713/*
714 * get absolute file name into buffer 'buf' of length 'len' bytes
715 *
716 * return FAIL for failure, OK otherwise
717 */
718 int
719mch_FullName(fname, buf, len, force)
720 char_u *fname, *buf;
721 int len;
722 int force;
723{
724 BPTR l;
725 int retval = FAIL;
726 int i;
727
728 /* Lock the file. If it exists, we can get the exact name. */
729 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
730 {
731 retval = lock2name(l, buf, (long)len - 1);
732 UnLock(l);
733 }
734 else if (force || !mch_isFullName(fname)) /* not a full path yet */
735 {
736 /*
737 * If the file cannot be locked (doesn't exist), try to lock the
738 * current directory and concatenate the file name.
739 */
740 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
741 {
742 retval = lock2name(l, buf, (long)len);
743 UnLock(l);
744 if (retval == OK)
745 {
746 i = STRLEN(buf);
747 /* Concatenate the fname to the directory. Don't add a slash
748 * if fname is empty, but do change "" to "/". */
749 if (i == 0 || *fname != NUL)
750 {
751 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
752 buf[i++] = '/';
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000753 vim_strncpy(buf + i, fname, len - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 }
755 }
756 }
757 }
758 if (*buf == 0 || *buf == ':')
759 retval = FAIL; /* something failed; use the file name */
760 return retval;
761}
762
763/*
764 * Return TRUE if "fname" does not depend on the current directory.
765 */
766 int
767mch_isFullName(fname)
768 char_u *fname;
769{
770 return (vim_strchr(fname, ':') != NULL && *fname != ':');
771}
772
773/*
774 * Get the full file name from a lock. Use 2.0 function if possible, because
775 * the arp function has more restrictions on the path length.
776 *
777 * return FAIL for failure, OK otherwise
778 */
779 static int
780lock2name(lock, buf, len)
781 BPTR lock;
782 char_u *buf;
783 long len;
784{
785#ifdef FEAT_ARP
786 if (dos2) /* use 2.0 function */
787#endif
788 return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL);
789#ifdef FEAT_ARP
790 else /* use arp function */
791 return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL);
792#endif
793}
794
795/*
796 * get file permissions for 'name'
797 * Returns -1 when it doesn't exist.
798 */
799 long
800mch_getperm(name)
801 char_u *name;
802{
803 struct FileInfoBlock *fib;
804 long retval = -1;
805
806 fib = get_fib(name);
807 if (fib != NULL)
808 {
809 retval = fib->fib_Protection;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000810 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 return retval;
813}
814
815/*
816 * set file permission for 'name' to 'perm'
817 *
818 * return FAIL for failure, OK otherwise
819 */
820 int
821mch_setperm(name, perm)
822 char_u *name;
823 long perm;
824{
825 perm &= ~FIBF_ARCHIVE; /* reset archived bit */
826 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
827}
828
829/*
830 * Set hidden flag for "name".
831 */
832 void
833mch_hide(name)
834 char_u *name;
835{
836 /* can't hide a file */
837}
838
839/*
840 * return FALSE if "name" is not a directory
841 * return TRUE if "name" is a directory.
842 * return FALSE for error.
843 */
844 int
845mch_isdir(name)
846 char_u *name;
847{
848 struct FileInfoBlock *fib;
849 int retval = FALSE;
850
851 fib = get_fib(name);
852 if (fib != NULL)
853 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000854#ifdef __amigaos4__
855 retval = (FIB_IS_DRAWER(fib)) ? TRUE : FALSE;
856#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000858#endif
859 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 return retval;
862}
863
864/*
865 * Create directory "name".
866 */
Bram Moolenaare3853642006-09-14 19:36:57 +0000867 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868mch_mkdir(name)
869 char_u *name;
870{
871 BPTR lock;
872
873 lock = CreateDir(name);
874 if (lock != NULL)
Bram Moolenaare3853642006-09-14 19:36:57 +0000875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 UnLock(lock);
Bram Moolenaare3853642006-09-14 19:36:57 +0000877 return 0;
878 }
879 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880}
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882/*
883 * Return 1 if "name" can be executed, 0 if not.
884 * Return -1 if unknown.
885 */
886 int
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200887mch_can_exe(name, path)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 char_u *name;
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200889 char_u **path;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890{
891 /* TODO */
892 return -1;
893}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
895/*
896 * Check what "name" is:
897 * NODE_NORMAL: file or directory (or doesn't exist)
898 * NODE_WRITABLE: writable device, socket, fifo, etc.
899 * NODE_OTHER: non-writable things
900 */
901 int
902mch_nodetype(name)
903 char_u *name;
904{
905 /* TODO */
906 return NODE_NORMAL;
907}
908
909 void
910mch_early_init()
911{
912}
913
914/*
915 * Careful: mch_exit() may be called before mch_init()!
916 */
917 void
918mch_exit(r)
919 int r;
920{
921 if (raw_in) /* put terminal in 'normal' mode */
922 {
923 settmode(TMODE_COOK);
924 stoptermcap();
925 }
926 out_char('\n');
927 if (raw_out)
928 {
929 if (term_console)
930 {
931 win_resize_off(); /* window resize events de-activated */
932 if (size_set)
933 OUT_STR("\233t\233u"); /* reset window size (CSI t CSI u) */
934 }
935 out_flush();
936 }
937
938#ifdef FEAT_TITLE
939 mch_restore_title(3); /* restore window title */
940#endif
941
942 ml_close_all(TRUE); /* remove all memfiles */
943
944#ifdef FEAT_ARP
945 if (ArpBase)
946 CloseLibrary((struct Library *) ArpBase);
947#endif
948 if (close_win)
949 Close(raw_in);
950 if (r)
951 printf(_("Vim exiting with %d\n"), r); /* somehow this makes :cq work!? */
952 exit(r);
953}
954
955/*
956 * This is a routine for setting a given stream to raw or cooked mode on the
957 * Amiga . This is useful when you are using Lattice C to produce programs
958 * that want to read single characters with the "getch()" or "fgetc" call.
959 *
960 * Written : 18-Jun-87 By Chuck McManis.
961 */
962
963#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
964
965/*
966 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
967 * 'cooked' mode. This only works on TTY's.
968 *
969 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
970 * getch() will return immediately rather than wait for a return. You
971 * lose editing features though.
972 *
973 * Cooked: This function returns the designate file pointer to it's normal,
974 * wait for a <CR> mode. This is exactly like raw() except that
975 * it sends a 0 to the console to make it back into a CON: from a RAW:
976 */
977 void
978mch_settmode(tmode)
979 int tmode;
980{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000981#if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
983#else
984 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
985 tmode == TMODE_RAW ? -1L : 0L) == 0)
986#endif
987 mch_errmsg(_("cannot change console mode ?!\n"));
988}
989
990/*
991 * set screen mode, always fails.
992 */
993 int
994mch_screenmode(arg)
995 char_u *arg;
996{
997 EMSG(_(e_screenmode));
998 return FAIL;
999}
1000
1001/*
1002 * Code for this routine came from the following :
1003 *
1004 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
1005 * DOS packet example
1006 * Requires 1.2
1007 *
1008 * Found on Fish Disk 56.
1009 *
1010 * Heavely modified by mool.
1011 */
1012
Bram Moolenaar82881492012-11-20 16:53:39 +01001013#ifndef PROTO
1014# include <devices/conunit.h>
1015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017/*
1018 * try to get the real window size
1019 * return FAIL for failure, OK otherwise
1020 */
1021 int
1022mch_get_shellsize()
1023{
1024 struct ConUnit *conUnit;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001025#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 char id_a[sizeof(struct InfoData) + 3];
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001027#endif
1028 struct InfoData *id=0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 if (!term_console) /* not an amiga window */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001031 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 /* insure longword alignment */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001034#ifdef __amigaos4__
Bram Moolenaar62dbdc42011-10-20 18:24:22 +02001035 if (!(id = AllocDosObject(DOS_INFODATA, 0)))
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001036 goto out;
1037#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
1041 /*
1042 * Should make console aware of real window size, not the one we set.
1043 * Unfortunately, under DOS 2.0x this redraws the window and it
1044 * is rarely needed, so we skip it now, unless we changed the size.
1045 */
1046 if (size_set)
1047 OUT_STR("\233t\233u"); /* CSI t CSI u */
1048 out_flush();
1049
1050#ifdef __AROS__
1051 if (!Info(raw_out, id)
1052 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
1053#else
1054 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
1055 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
1056#endif
1057 {
1058 /* it's not an amiga window, maybe aux device */
1059 /* terminal type should be set */
1060 term_console = FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001061 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
1063 if (oldwindowtitle == NULL)
1064 oldwindowtitle = (char_u *)wb_window->Title;
1065 if (id->id_InUse == (BPTR)NULL)
1066 {
1067 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
1068 return FAIL;
1069 }
1070 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
1071
1072 /* get window size */
1073 Rows = conUnit->cu_YMax + 1;
1074 Columns = conUnit->cu_XMax + 1;
1075 if (Rows < 0 || Rows > 200) /* cannot be an amiga window */
1076 {
1077 Columns = 80;
1078 Rows = 24;
1079 term_console = FALSE;
1080 return FAIL;
1081 }
1082
1083 return OK;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001084out:
1085#ifdef __amigaos4__
1086 FreeDosObject(DOS_INFODATA, id); /* Safe to pass NULL */
1087#endif
1088
1089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090}
1091
1092/*
1093 * Try to set the real window size to Rows and Columns.
1094 */
1095 void
1096mch_set_shellsize()
1097{
1098 if (term_console)
1099 {
1100 size_set = TRUE;
1101 out_char(CSI);
1102 out_num((long)Rows);
1103 out_char('t');
1104 out_char(CSI);
1105 out_num((long)Columns);
1106 out_char('u');
1107 out_flush();
1108 }
1109}
1110
1111/*
1112 * Rows and/or Columns has changed.
1113 */
1114 void
1115mch_new_shellsize()
1116{
1117 /* Nothing to do. */
1118}
1119
1120/*
1121 * out_num - output a (big) number fast
1122 */
1123 static void
1124out_num(n)
1125 long n;
1126{
1127 OUT_STR_NF(tltoa((unsigned long)n));
1128}
1129
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001130#if !defined(AZTEC_C) && !defined(__AROS__) && !defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131/*
1132 * Sendpacket.c
1133 *
1134 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1135 * the given message port. This makes working around DOS lots easier.
1136 *
1137 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1138 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1139 * say 'oml lib:amiga.lib -r sendpacket.o'
1140 */
1141
Bram Moolenaar82881492012-11-20 16:53:39 +01001142#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143/* #include <proto/exec.h> */
1144/* #include <proto/dos.h> */
Bram Moolenaar82881492012-11-20 16:53:39 +01001145# include <exec/memory.h>
1146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
1148/*
1149 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1150 * Finkel. This function will send a packet of the given type to the Message
1151 * Port supplied.
1152 */
1153
1154 static long
1155dos_packet(pid, action, arg)
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001156 struct MsgPort *pid; /* process identifier ... (handlers message port) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 long action, /* packet type ... (what you want handler to do) */
1158 arg; /* single argument */
1159{
1160# ifdef FEAT_ARP
1161 struct MsgPort *replyport;
1162 struct StandardPacket *packet;
1163 long res1;
1164
1165 if (dos2)
1166# endif
1167 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); /* use 2.0 function */
1168# ifdef FEAT_ARP
1169
1170 replyport = (struct MsgPort *) CreatePort(NULL, 0); /* use arp function */
1171 if (!replyport)
1172 return (0);
1173
1174 /* Allocate space for a packet, make it public and clear it */
1175 packet = (struct StandardPacket *)
1176 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
1177 if (!packet) {
1178 DeletePort(replyport);
1179 return (0);
1180 }
1181 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1182 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1183 packet->sp_Pkt.dp_Port = replyport;
1184 packet->sp_Pkt.dp_Type = action;
1185 packet->sp_Pkt.dp_Arg1 = arg;
1186
1187 PutMsg(pid, (struct Message *)packet); /* send packet */
1188
1189 WaitPort(replyport);
1190 GetMsg(replyport);
1191
1192 res1 = packet->sp_Pkt.dp_Res1;
1193
1194 FreeMem(packet, (long) sizeof(struct StandardPacket));
1195 DeletePort(replyport);
1196
1197 return (res1);
1198# endif
1199}
1200#endif /* !defined(AZTEC_C) && !defined(__AROS__) */
1201
1202/*
1203 * Call shell.
1204 * Return error number for failure, 0 otherwise
1205 */
1206 int
1207mch_call_shell(cmd, options)
1208 char_u *cmd;
1209 int options; /* SHELL_*, see vim.h */
1210{
1211 BPTR mydir;
1212 int x;
1213 int tmode = cur_tmode;
1214#ifdef AZTEC_C
1215 int use_execute;
1216 char_u *shellcmd = NULL;
1217 char_u *shellarg;
1218#endif
1219 int retval = 0;
1220
1221 if (close_win)
1222 {
1223 /* if Vim opened a window: Executing a shell may cause crashes */
1224 EMSG(_("E360: Cannot execute shell with -f option"));
1225 return -1;
1226 }
1227
1228 if (term_console)
1229 win_resize_off(); /* window resize events de-activated */
1230 out_flush();
1231
1232 if (options & SHELL_COOKED)
1233 settmode(TMODE_COOK); /* set to normal mode */
1234 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); /* remember current dir */
1235
1236#if !defined(AZTEC_C) /* not tested very much */
1237 if (cmd == NULL)
1238 {
1239# ifdef FEAT_ARP
1240 if (dos2)
1241# endif
1242 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1243# ifdef FEAT_ARP
1244 else
1245 x = Execute(p_sh, raw_in, raw_out);
1246# endif
1247 }
1248 else
1249 {
1250# ifdef FEAT_ARP
1251 if (dos2)
1252# endif
1253 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1254# ifdef FEAT_ARP
1255 else
1256 x = Execute((char *)cmd, 0L, raw_out);
1257# endif
1258 }
1259# ifdef FEAT_ARP
1260 if ((dos2 && x < 0) || (!dos2 && !x))
1261# else
1262 if (x < 0)
1263# endif
1264 {
1265 MSG_PUTS(_("Cannot execute "));
1266 if (cmd == NULL)
1267 {
1268 MSG_PUTS(_("shell "));
1269 msg_outtrans(p_sh);
1270 }
1271 else
1272 msg_outtrans(cmd);
1273 msg_putchar('\n');
1274 retval = -1;
1275 }
1276# ifdef FEAT_ARP
1277 else if (!dos2 || x)
1278# else
1279 else if (x)
1280# endif
1281 {
1282 if ((x = IoErr()) != 0)
1283 {
1284 if (!(options & SHELL_SILENT))
1285 {
1286 msg_putchar('\n');
1287 msg_outnum((long)x);
1288 MSG_PUTS(_(" returned\n"));
1289 }
1290 retval = x;
1291 }
1292 }
1293#else /* else part is for AZTEC_C */
1294 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1295 use_execute = 1;
1296 else
1297 use_execute = 0;
1298 if (!use_execute)
1299 {
1300 /*
1301 * separate shell name from argument
1302 */
1303 shellcmd = vim_strsave(p_sh);
1304 if (shellcmd == NULL) /* out of memory, use Execute */
1305 use_execute = 1;
1306 else
1307 {
1308 shellarg = skiptowhite(shellcmd); /* find start of arguments */
1309 if (*shellarg != NUL)
1310 {
1311 *shellarg++ = NUL;
1312 shellarg = skipwhite(shellarg);
1313 }
1314 }
1315 }
1316 if (cmd == NULL)
1317 {
1318 if (use_execute)
1319 {
1320# ifdef FEAT_ARP
1321 if (dos2)
1322# endif
1323 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1324# ifdef FEAT_ARP
1325 else
1326 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1327# endif
1328 }
1329 else
1330 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1331 }
1332 else if (use_execute)
1333 {
1334# ifdef FEAT_ARP
1335 if (dos2)
1336# endif
1337 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1338# ifdef FEAT_ARP
1339 else
1340 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1341# endif
1342 }
1343 else if (p_st & 1)
1344 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1345 (char *)cmd, NULL);
1346 else
1347 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1348 (char *)p_shcf, (char *)cmd, NULL);
1349# ifdef FEAT_ARP
1350 if ((dos2 && x < 0) || (!dos2 && x))
1351# else
1352 if (x < 0)
1353# endif
1354 {
1355 MSG_PUTS(_("Cannot execute "));
1356 if (use_execute)
1357 {
1358 if (cmd == NULL)
1359 msg_outtrans(p_sh);
1360 else
1361 msg_outtrans(cmd);
1362 }
1363 else
1364 {
1365 MSG_PUTS(_("shell "));
1366 msg_outtrans(shellcmd);
1367 }
1368 msg_putchar('\n');
1369 retval = -1;
1370 }
1371 else
1372 {
1373 if (use_execute)
1374 {
1375# ifdef FEAT_ARP
1376 if (!dos2 || x)
1377# else
1378 if (x)
1379# endif
1380 x = IoErr();
1381 }
1382 else
1383 x = wait();
1384 if (x)
1385 {
1386 if (!(options & SHELL_SILENT) && !emsg_silent)
1387 {
1388 msg_putchar('\n');
1389 msg_outnum((long)x);
1390 MSG_PUTS(_(" returned\n"));
1391 }
1392 retval = x;
1393 }
1394 }
1395 vim_free(shellcmd);
1396#endif /* AZTEC_C */
1397
1398 if ((mydir = CurrentDir(mydir)) != 0) /* make sure we stay in the same directory */
1399 UnLock(mydir);
1400 if (tmode == TMODE_RAW)
1401 settmode(TMODE_RAW); /* set to raw mode */
1402#ifdef FEAT_TITLE
1403 resettitle();
1404#endif
1405 if (term_console)
1406 win_resize_on(); /* window resize events activated */
1407 return retval;
1408}
1409
1410/*
1411 * check for an "interrupt signal"
1412 * We only react to a CTRL-C, but also clear the other break signals to avoid
1413 * trouble with lattice-c programs.
1414 */
1415 void
1416mch_breakcheck()
1417{
1418 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1419 got_int = TRUE;
1420}
1421
1422/* this routine causes manx to use this Chk_Abort() rather than it's own */
1423/* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
1424/* is zero). Since we want to check for our own ^C's */
1425
1426#ifdef _DCC
1427#define Chk_Abort chkabort
1428#endif
1429
1430#ifdef LATTICE
1431void __regargs __chkabort(void);
1432
1433void __regargs __chkabort(void)
1434{}
1435
1436#else
1437 long
1438Chk_Abort(void)
1439{
1440 return(0L);
1441}
1442#endif
1443
1444/*
1445 * mch_expandpath() - this code does wild-card pattern matching using the arp
1446 * routines.
1447 *
1448 * "pat" has backslashes before chars that are not to be expanded.
1449 * Returns the number of matches found.
1450 *
1451 * This is based on WildDemo2.c (found in arp1.1 distribution).
1452 * That code's copyright follows:
1453 * Copyright (c) 1987, Scott Ballantyne
1454 * Use and abuse as you please.
1455 */
1456
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001457#ifdef __amigaos4__
1458# define ANCHOR_BUF_SIZE 1024
1459#else
1460# define ANCHOR_BUF_SIZE (512)
1461# define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
1464 int
1465mch_expandpath(gap, pat, flags)
1466 garray_T *gap;
1467 char_u *pat;
1468 int flags; /* EW_* flags */
1469{
1470 struct AnchorPath *Anchor;
1471 LONG Result;
1472 char_u *starbuf, *sp, *dp;
1473 int start_len;
1474 int matches;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001475#ifdef __amigaos4__
1476 struct TagItem AnchorTags[] = {
1477 {ADO_Strlen, ANCHOR_BUF_SIZE},
1478 {ADO_Flags, APF_DODOT|APF_DOWILD|APF_MultiAssigns},
1479 {TAG_DONE, 0L}
1480 };
1481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 start_len = gap->ga_len;
1484
1485 /* Get our AnchorBase */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001486#ifdef __amigaos4__
1487 Anchor = AllocDosObject(DOS_ANCHORPATH, AnchorTags);
1488#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 Anchor = (struct AnchorPath *)alloc_clear((unsigned)ANCHOR_SIZE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (Anchor == NULL)
1492 return 0;
1493
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001494#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; /* ap_Length not supported anymore */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001496# ifdef APF_DODOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001498# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 Anchor->ap_Flags = APF_DoDot | APF_DoWild; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001500# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501#endif
1502
1503#ifdef FEAT_ARP
1504 if (dos2)
1505 {
1506#endif
1507 /* hack to replace '*' by '#?' */
1508 starbuf = alloc((unsigned)(2 * STRLEN(pat) + 1));
1509 if (starbuf == NULL)
1510 goto Return;
1511 for (sp = pat, dp = starbuf; *sp; ++sp)
1512 {
1513 if (*sp == '*')
1514 {
1515 *dp++ = '#';
1516 *dp++ = '?';
1517 }
1518 else
1519 *dp++ = *sp;
1520 }
1521 *dp = NUL;
1522 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1523 vim_free(starbuf);
1524#ifdef FEAT_ARP
1525 }
1526 else
1527 Result = FindFirst((char *)pat, Anchor);
1528#endif
1529
1530 /*
1531 * Loop to get all matches.
1532 */
1533 while (Result == 0)
1534 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001535#ifdef __amigaos4__
1536 addfile(gap, (char_u *)Anchor->ap_Buffer, flags);
1537#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#ifdef FEAT_ARP
1541 if (dos2)
1542#endif
1543 Result = MatchNext(Anchor);
1544#ifdef FEAT_ARP
1545 else
1546 Result = FindNext(Anchor);
1547#endif
1548 }
1549 matches = gap->ga_len - start_len;
1550
1551 if (Result == ERROR_BUFFER_OVERFLOW)
1552 EMSG(_("ANCHOR_BUF_SIZE too small."));
1553 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1554 && Result != ERROR_DEVICE_NOT_MOUNTED
1555 && Result != ERROR_NO_MORE_ENTRIES)
1556 EMSG(_("I/O ERROR"));
1557
1558 /*
1559 * Sort the files for this pattern.
1560 */
1561 if (matches)
1562 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1563 (size_t)matches, sizeof(char_u *), sortcmp);
1564
1565 /* Free the wildcard stuff */
1566#ifdef FEAT_ARP
1567 if (dos2)
1568#endif
1569 MatchEnd(Anchor);
1570#ifdef FEAT_ARP
1571 else
1572 FreeAnchorChain(Anchor);
1573#endif
1574
1575Return:
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001576#ifdef __amigaos4__
1577 FreeDosObject(DOS_ANCHORPATH, Anchor);
1578#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 vim_free(Anchor);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 return matches;
1583}
1584
1585 static int
1586sortcmp(a, b)
1587 const void *a, *b;
1588{
1589 char *s = *(char **)a;
1590 char *t = *(char **)b;
1591
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001592 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593}
1594
1595/*
1596 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1597 */
1598 int
1599mch_has_exp_wildcard(p)
1600 char_u *p;
1601{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001602 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 if (*p == '\\' && p[1] != NUL)
1605 ++p;
1606 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1607 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 return FALSE;
1610}
1611
1612 int
1613mch_has_wildcard(p)
1614 char_u *p;
1615{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001616 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 {
1618 if (*p == '\\' && p[1] != NUL)
1619 ++p;
1620 else
1621 if (vim_strchr((char_u *)
1622# ifdef VIM_BACKTICK
1623 "*?[(#$`"
1624# else
1625 "*?[(#$"
1626# endif
1627 , *p) != NULL
1628 || (*p == '~' && p[1] != NUL))
1629 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 }
1631 return FALSE;
1632}
1633
1634/*
1635 * With AmigaDOS 2.0 support for reading local environment variables
1636 *
1637 * Two buffers are allocated:
1638 * - A big one to do the expansion into. It is freed before returning.
1639 * - A small one to hold the return value. It is kept until the next call.
1640 */
1641 char_u *
1642mch_getenv(var)
1643 char_u *var;
1644{
1645 int len;
1646 UBYTE *buf; /* buffer to expand in */
1647 char_u *retval; /* return value */
1648 static char_u *alloced = NULL; /* allocated memory */
1649
1650#ifdef FEAT_ARP
1651 if (!dos2)
1652 retval = (char_u *)getenv((char *)var);
1653 else
1654#endif
1655 {
1656 vim_free(alloced);
1657 alloced = NULL;
1658 retval = NULL;
1659
1660 buf = alloc(IOSIZE);
1661 if (buf == NULL)
1662 return NULL;
1663
1664 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1665 if (len >= 0)
1666 {
1667 retval = vim_strsave((char_u *)buf);
1668 alloced = retval;
1669 }
1670
1671 vim_free(buf);
1672 }
1673
1674 /* if $VIM is not defined, use "vim:" instead */
1675 if (retval == NULL && STRCMP(var, "VIM") == 0)
1676 retval = (char_u *)"vim:";
1677
1678 return retval;
1679}
1680
1681/*
1682 * Amiga version of setenv() with AmigaDOS 2.0 support.
1683 */
1684/* ARGSUSED */
1685 int
1686mch_setenv(var, value, x)
1687 char *var;
1688 char *value;
1689 int x;
1690{
1691#ifdef FEAT_ARP
1692 if (!dos2)
1693 return setenv(var, value);
1694#endif
1695
1696 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
1697 return 0; /* success */
1698 return -1; /* failure */
1699}