blob: 0b63ea15223e783a4c0d971af6fb6a36cf643e17 [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/*
121 * mch_inchar(): low level input funcion.
122 * 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
887mch_can_exe(name)
888 char_u *name;
889{
890 /* TODO */
891 return -1;
892}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894/*
895 * Check what "name" is:
896 * NODE_NORMAL: file or directory (or doesn't exist)
897 * NODE_WRITABLE: writable device, socket, fifo, etc.
898 * NODE_OTHER: non-writable things
899 */
900 int
901mch_nodetype(name)
902 char_u *name;
903{
904 /* TODO */
905 return NODE_NORMAL;
906}
907
908 void
909mch_early_init()
910{
911}
912
913/*
914 * Careful: mch_exit() may be called before mch_init()!
915 */
916 void
917mch_exit(r)
918 int r;
919{
920 if (raw_in) /* put terminal in 'normal' mode */
921 {
922 settmode(TMODE_COOK);
923 stoptermcap();
924 }
925 out_char('\n');
926 if (raw_out)
927 {
928 if (term_console)
929 {
930 win_resize_off(); /* window resize events de-activated */
931 if (size_set)
932 OUT_STR("\233t\233u"); /* reset window size (CSI t CSI u) */
933 }
934 out_flush();
935 }
936
937#ifdef FEAT_TITLE
938 mch_restore_title(3); /* restore window title */
939#endif
940
941 ml_close_all(TRUE); /* remove all memfiles */
942
943#ifdef FEAT_ARP
944 if (ArpBase)
945 CloseLibrary((struct Library *) ArpBase);
946#endif
947 if (close_win)
948 Close(raw_in);
949 if (r)
950 printf(_("Vim exiting with %d\n"), r); /* somehow this makes :cq work!? */
951 exit(r);
952}
953
954/*
955 * This is a routine for setting a given stream to raw or cooked mode on the
956 * Amiga . This is useful when you are using Lattice C to produce programs
957 * that want to read single characters with the "getch()" or "fgetc" call.
958 *
959 * Written : 18-Jun-87 By Chuck McManis.
960 */
961
962#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
963
964/*
965 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
966 * 'cooked' mode. This only works on TTY's.
967 *
968 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
969 * getch() will return immediately rather than wait for a return. You
970 * lose editing features though.
971 *
972 * Cooked: This function returns the designate file pointer to it's normal,
973 * wait for a <CR> mode. This is exactly like raw() except that
974 * it sends a 0 to the console to make it back into a CON: from a RAW:
975 */
976 void
977mch_settmode(tmode)
978 int tmode;
979{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000980#if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
982#else
983 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
984 tmode == TMODE_RAW ? -1L : 0L) == 0)
985#endif
986 mch_errmsg(_("cannot change console mode ?!\n"));
987}
988
989/*
990 * set screen mode, always fails.
991 */
992 int
993mch_screenmode(arg)
994 char_u *arg;
995{
996 EMSG(_(e_screenmode));
997 return FAIL;
998}
999
1000/*
1001 * Code for this routine came from the following :
1002 *
1003 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
1004 * DOS packet example
1005 * Requires 1.2
1006 *
1007 * Found on Fish Disk 56.
1008 *
1009 * Heavely modified by mool.
1010 */
1011
Bram Moolenaar82881492012-11-20 16:53:39 +01001012#ifndef PROTO
1013# include <devices/conunit.h>
1014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
1016/*
1017 * try to get the real window size
1018 * return FAIL for failure, OK otherwise
1019 */
1020 int
1021mch_get_shellsize()
1022{
1023 struct ConUnit *conUnit;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001024#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 char id_a[sizeof(struct InfoData) + 3];
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001026#endif
1027 struct InfoData *id=0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029 if (!term_console) /* not an amiga window */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001030 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
1032 /* insure longword alignment */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001033#ifdef __amigaos4__
Bram Moolenaar62dbdc42011-10-20 18:24:22 +02001034 if (!(id = AllocDosObject(DOS_INFODATA, 0)))
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001035 goto out;
1036#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040 /*
1041 * Should make console aware of real window size, not the one we set.
1042 * Unfortunately, under DOS 2.0x this redraws the window and it
1043 * is rarely needed, so we skip it now, unless we changed the size.
1044 */
1045 if (size_set)
1046 OUT_STR("\233t\233u"); /* CSI t CSI u */
1047 out_flush();
1048
1049#ifdef __AROS__
1050 if (!Info(raw_out, id)
1051 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
1052#else
1053 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
1054 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
1055#endif
1056 {
1057 /* it's not an amiga window, maybe aux device */
1058 /* terminal type should be set */
1059 term_console = FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001060 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 }
1062 if (oldwindowtitle == NULL)
1063 oldwindowtitle = (char_u *)wb_window->Title;
1064 if (id->id_InUse == (BPTR)NULL)
1065 {
1066 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
1067 return FAIL;
1068 }
1069 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
1070
1071 /* get window size */
1072 Rows = conUnit->cu_YMax + 1;
1073 Columns = conUnit->cu_XMax + 1;
1074 if (Rows < 0 || Rows > 200) /* cannot be an amiga window */
1075 {
1076 Columns = 80;
1077 Rows = 24;
1078 term_console = FALSE;
1079 return FAIL;
1080 }
1081
1082 return OK;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001083out:
1084#ifdef __amigaos4__
1085 FreeDosObject(DOS_INFODATA, id); /* Safe to pass NULL */
1086#endif
1087
1088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089}
1090
1091/*
1092 * Try to set the real window size to Rows and Columns.
1093 */
1094 void
1095mch_set_shellsize()
1096{
1097 if (term_console)
1098 {
1099 size_set = TRUE;
1100 out_char(CSI);
1101 out_num((long)Rows);
1102 out_char('t');
1103 out_char(CSI);
1104 out_num((long)Columns);
1105 out_char('u');
1106 out_flush();
1107 }
1108}
1109
1110/*
1111 * Rows and/or Columns has changed.
1112 */
1113 void
1114mch_new_shellsize()
1115{
1116 /* Nothing to do. */
1117}
1118
1119/*
1120 * out_num - output a (big) number fast
1121 */
1122 static void
1123out_num(n)
1124 long n;
1125{
1126 OUT_STR_NF(tltoa((unsigned long)n));
1127}
1128
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001129#if !defined(AZTEC_C) && !defined(__AROS__) && !defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130/*
1131 * Sendpacket.c
1132 *
1133 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1134 * the given message port. This makes working around DOS lots easier.
1135 *
1136 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1137 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1138 * say 'oml lib:amiga.lib -r sendpacket.o'
1139 */
1140
Bram Moolenaar82881492012-11-20 16:53:39 +01001141#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142/* #include <proto/exec.h> */
1143/* #include <proto/dos.h> */
Bram Moolenaar82881492012-11-20 16:53:39 +01001144# include <exec/memory.h>
1145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
1147/*
1148 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1149 * Finkel. This function will send a packet of the given type to the Message
1150 * Port supplied.
1151 */
1152
1153 static long
1154dos_packet(pid, action, arg)
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001155 struct MsgPort *pid; /* process identifier ... (handlers message port) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 long action, /* packet type ... (what you want handler to do) */
1157 arg; /* single argument */
1158{
1159# ifdef FEAT_ARP
1160 struct MsgPort *replyport;
1161 struct StandardPacket *packet;
1162 long res1;
1163
1164 if (dos2)
1165# endif
1166 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); /* use 2.0 function */
1167# ifdef FEAT_ARP
1168
1169 replyport = (struct MsgPort *) CreatePort(NULL, 0); /* use arp function */
1170 if (!replyport)
1171 return (0);
1172
1173 /* Allocate space for a packet, make it public and clear it */
1174 packet = (struct StandardPacket *)
1175 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
1176 if (!packet) {
1177 DeletePort(replyport);
1178 return (0);
1179 }
1180 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1181 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1182 packet->sp_Pkt.dp_Port = replyport;
1183 packet->sp_Pkt.dp_Type = action;
1184 packet->sp_Pkt.dp_Arg1 = arg;
1185
1186 PutMsg(pid, (struct Message *)packet); /* send packet */
1187
1188 WaitPort(replyport);
1189 GetMsg(replyport);
1190
1191 res1 = packet->sp_Pkt.dp_Res1;
1192
1193 FreeMem(packet, (long) sizeof(struct StandardPacket));
1194 DeletePort(replyport);
1195
1196 return (res1);
1197# endif
1198}
1199#endif /* !defined(AZTEC_C) && !defined(__AROS__) */
1200
1201/*
1202 * Call shell.
1203 * Return error number for failure, 0 otherwise
1204 */
1205 int
1206mch_call_shell(cmd, options)
1207 char_u *cmd;
1208 int options; /* SHELL_*, see vim.h */
1209{
1210 BPTR mydir;
1211 int x;
1212 int tmode = cur_tmode;
1213#ifdef AZTEC_C
1214 int use_execute;
1215 char_u *shellcmd = NULL;
1216 char_u *shellarg;
1217#endif
1218 int retval = 0;
1219
1220 if (close_win)
1221 {
1222 /* if Vim opened a window: Executing a shell may cause crashes */
1223 EMSG(_("E360: Cannot execute shell with -f option"));
1224 return -1;
1225 }
1226
1227 if (term_console)
1228 win_resize_off(); /* window resize events de-activated */
1229 out_flush();
1230
1231 if (options & SHELL_COOKED)
1232 settmode(TMODE_COOK); /* set to normal mode */
1233 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); /* remember current dir */
1234
1235#if !defined(AZTEC_C) /* not tested very much */
1236 if (cmd == NULL)
1237 {
1238# ifdef FEAT_ARP
1239 if (dos2)
1240# endif
1241 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1242# ifdef FEAT_ARP
1243 else
1244 x = Execute(p_sh, raw_in, raw_out);
1245# endif
1246 }
1247 else
1248 {
1249# ifdef FEAT_ARP
1250 if (dos2)
1251# endif
1252 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1253# ifdef FEAT_ARP
1254 else
1255 x = Execute((char *)cmd, 0L, raw_out);
1256# endif
1257 }
1258# ifdef FEAT_ARP
1259 if ((dos2 && x < 0) || (!dos2 && !x))
1260# else
1261 if (x < 0)
1262# endif
1263 {
1264 MSG_PUTS(_("Cannot execute "));
1265 if (cmd == NULL)
1266 {
1267 MSG_PUTS(_("shell "));
1268 msg_outtrans(p_sh);
1269 }
1270 else
1271 msg_outtrans(cmd);
1272 msg_putchar('\n');
1273 retval = -1;
1274 }
1275# ifdef FEAT_ARP
1276 else if (!dos2 || x)
1277# else
1278 else if (x)
1279# endif
1280 {
1281 if ((x = IoErr()) != 0)
1282 {
1283 if (!(options & SHELL_SILENT))
1284 {
1285 msg_putchar('\n');
1286 msg_outnum((long)x);
1287 MSG_PUTS(_(" returned\n"));
1288 }
1289 retval = x;
1290 }
1291 }
1292#else /* else part is for AZTEC_C */
1293 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1294 use_execute = 1;
1295 else
1296 use_execute = 0;
1297 if (!use_execute)
1298 {
1299 /*
1300 * separate shell name from argument
1301 */
1302 shellcmd = vim_strsave(p_sh);
1303 if (shellcmd == NULL) /* out of memory, use Execute */
1304 use_execute = 1;
1305 else
1306 {
1307 shellarg = skiptowhite(shellcmd); /* find start of arguments */
1308 if (*shellarg != NUL)
1309 {
1310 *shellarg++ = NUL;
1311 shellarg = skipwhite(shellarg);
1312 }
1313 }
1314 }
1315 if (cmd == NULL)
1316 {
1317 if (use_execute)
1318 {
1319# ifdef FEAT_ARP
1320 if (dos2)
1321# endif
1322 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1323# ifdef FEAT_ARP
1324 else
1325 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1326# endif
1327 }
1328 else
1329 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1330 }
1331 else if (use_execute)
1332 {
1333# ifdef FEAT_ARP
1334 if (dos2)
1335# endif
1336 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1337# ifdef FEAT_ARP
1338 else
1339 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1340# endif
1341 }
1342 else if (p_st & 1)
1343 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1344 (char *)cmd, NULL);
1345 else
1346 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1347 (char *)p_shcf, (char *)cmd, NULL);
1348# ifdef FEAT_ARP
1349 if ((dos2 && x < 0) || (!dos2 && x))
1350# else
1351 if (x < 0)
1352# endif
1353 {
1354 MSG_PUTS(_("Cannot execute "));
1355 if (use_execute)
1356 {
1357 if (cmd == NULL)
1358 msg_outtrans(p_sh);
1359 else
1360 msg_outtrans(cmd);
1361 }
1362 else
1363 {
1364 MSG_PUTS(_("shell "));
1365 msg_outtrans(shellcmd);
1366 }
1367 msg_putchar('\n');
1368 retval = -1;
1369 }
1370 else
1371 {
1372 if (use_execute)
1373 {
1374# ifdef FEAT_ARP
1375 if (!dos2 || x)
1376# else
1377 if (x)
1378# endif
1379 x = IoErr();
1380 }
1381 else
1382 x = wait();
1383 if (x)
1384 {
1385 if (!(options & SHELL_SILENT) && !emsg_silent)
1386 {
1387 msg_putchar('\n');
1388 msg_outnum((long)x);
1389 MSG_PUTS(_(" returned\n"));
1390 }
1391 retval = x;
1392 }
1393 }
1394 vim_free(shellcmd);
1395#endif /* AZTEC_C */
1396
1397 if ((mydir = CurrentDir(mydir)) != 0) /* make sure we stay in the same directory */
1398 UnLock(mydir);
1399 if (tmode == TMODE_RAW)
1400 settmode(TMODE_RAW); /* set to raw mode */
1401#ifdef FEAT_TITLE
1402 resettitle();
1403#endif
1404 if (term_console)
1405 win_resize_on(); /* window resize events activated */
1406 return retval;
1407}
1408
1409/*
1410 * check for an "interrupt signal"
1411 * We only react to a CTRL-C, but also clear the other break signals to avoid
1412 * trouble with lattice-c programs.
1413 */
1414 void
1415mch_breakcheck()
1416{
1417 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1418 got_int = TRUE;
1419}
1420
1421/* this routine causes manx to use this Chk_Abort() rather than it's own */
1422/* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
1423/* is zero). Since we want to check for our own ^C's */
1424
1425#ifdef _DCC
1426#define Chk_Abort chkabort
1427#endif
1428
1429#ifdef LATTICE
1430void __regargs __chkabort(void);
1431
1432void __regargs __chkabort(void)
1433{}
1434
1435#else
1436 long
1437Chk_Abort(void)
1438{
1439 return(0L);
1440}
1441#endif
1442
1443/*
1444 * mch_expandpath() - this code does wild-card pattern matching using the arp
1445 * routines.
1446 *
1447 * "pat" has backslashes before chars that are not to be expanded.
1448 * Returns the number of matches found.
1449 *
1450 * This is based on WildDemo2.c (found in arp1.1 distribution).
1451 * That code's copyright follows:
1452 * Copyright (c) 1987, Scott Ballantyne
1453 * Use and abuse as you please.
1454 */
1455
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001456#ifdef __amigaos4__
1457# define ANCHOR_BUF_SIZE 1024
1458#else
1459# define ANCHOR_BUF_SIZE (512)
1460# define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
1463 int
1464mch_expandpath(gap, pat, flags)
1465 garray_T *gap;
1466 char_u *pat;
1467 int flags; /* EW_* flags */
1468{
1469 struct AnchorPath *Anchor;
1470 LONG Result;
1471 char_u *starbuf, *sp, *dp;
1472 int start_len;
1473 int matches;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001474#ifdef __amigaos4__
1475 struct TagItem AnchorTags[] = {
1476 {ADO_Strlen, ANCHOR_BUF_SIZE},
1477 {ADO_Flags, APF_DODOT|APF_DOWILD|APF_MultiAssigns},
1478 {TAG_DONE, 0L}
1479 };
1480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
1482 start_len = gap->ga_len;
1483
1484 /* Get our AnchorBase */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001485#ifdef __amigaos4__
1486 Anchor = AllocDosObject(DOS_ANCHORPATH, AnchorTags);
1487#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 Anchor = (struct AnchorPath *)alloc_clear((unsigned)ANCHOR_SIZE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (Anchor == NULL)
1491 return 0;
1492
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001493#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; /* ap_Length not supported anymore */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001495# ifdef APF_DODOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001497# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 Anchor->ap_Flags = APF_DoDot | APF_DoWild; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500#endif
1501
1502#ifdef FEAT_ARP
1503 if (dos2)
1504 {
1505#endif
1506 /* hack to replace '*' by '#?' */
1507 starbuf = alloc((unsigned)(2 * STRLEN(pat) + 1));
1508 if (starbuf == NULL)
1509 goto Return;
1510 for (sp = pat, dp = starbuf; *sp; ++sp)
1511 {
1512 if (*sp == '*')
1513 {
1514 *dp++ = '#';
1515 *dp++ = '?';
1516 }
1517 else
1518 *dp++ = *sp;
1519 }
1520 *dp = NUL;
1521 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1522 vim_free(starbuf);
1523#ifdef FEAT_ARP
1524 }
1525 else
1526 Result = FindFirst((char *)pat, Anchor);
1527#endif
1528
1529 /*
1530 * Loop to get all matches.
1531 */
1532 while (Result == 0)
1533 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001534#ifdef __amigaos4__
1535 addfile(gap, (char_u *)Anchor->ap_Buffer, flags);
1536#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539#ifdef FEAT_ARP
1540 if (dos2)
1541#endif
1542 Result = MatchNext(Anchor);
1543#ifdef FEAT_ARP
1544 else
1545 Result = FindNext(Anchor);
1546#endif
1547 }
1548 matches = gap->ga_len - start_len;
1549
1550 if (Result == ERROR_BUFFER_OVERFLOW)
1551 EMSG(_("ANCHOR_BUF_SIZE too small."));
1552 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1553 && Result != ERROR_DEVICE_NOT_MOUNTED
1554 && Result != ERROR_NO_MORE_ENTRIES)
1555 EMSG(_("I/O ERROR"));
1556
1557 /*
1558 * Sort the files for this pattern.
1559 */
1560 if (matches)
1561 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1562 (size_t)matches, sizeof(char_u *), sortcmp);
1563
1564 /* Free the wildcard stuff */
1565#ifdef FEAT_ARP
1566 if (dos2)
1567#endif
1568 MatchEnd(Anchor);
1569#ifdef FEAT_ARP
1570 else
1571 FreeAnchorChain(Anchor);
1572#endif
1573
1574Return:
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001575#ifdef __amigaos4__
1576 FreeDosObject(DOS_ANCHORPATH, Anchor);
1577#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 vim_free(Anchor);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 return matches;
1582}
1583
1584 static int
1585sortcmp(a, b)
1586 const void *a, *b;
1587{
1588 char *s = *(char **)a;
1589 char *t = *(char **)b;
1590
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001591 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592}
1593
1594/*
1595 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1596 */
1597 int
1598mch_has_exp_wildcard(p)
1599 char_u *p;
1600{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001601 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 if (*p == '\\' && p[1] != NUL)
1604 ++p;
1605 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1606 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 return FALSE;
1609}
1610
1611 int
1612mch_has_wildcard(p)
1613 char_u *p;
1614{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001615 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 if (*p == '\\' && p[1] != NUL)
1618 ++p;
1619 else
1620 if (vim_strchr((char_u *)
1621# ifdef VIM_BACKTICK
1622 "*?[(#$`"
1623# else
1624 "*?[(#$"
1625# endif
1626 , *p) != NULL
1627 || (*p == '~' && p[1] != NUL))
1628 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
1630 return FALSE;
1631}
1632
1633/*
1634 * With AmigaDOS 2.0 support for reading local environment variables
1635 *
1636 * Two buffers are allocated:
1637 * - A big one to do the expansion into. It is freed before returning.
1638 * - A small one to hold the return value. It is kept until the next call.
1639 */
1640 char_u *
1641mch_getenv(var)
1642 char_u *var;
1643{
1644 int len;
1645 UBYTE *buf; /* buffer to expand in */
1646 char_u *retval; /* return value */
1647 static char_u *alloced = NULL; /* allocated memory */
1648
1649#ifdef FEAT_ARP
1650 if (!dos2)
1651 retval = (char_u *)getenv((char *)var);
1652 else
1653#endif
1654 {
1655 vim_free(alloced);
1656 alloced = NULL;
1657 retval = NULL;
1658
1659 buf = alloc(IOSIZE);
1660 if (buf == NULL)
1661 return NULL;
1662
1663 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1664 if (len >= 0)
1665 {
1666 retval = vim_strsave((char_u *)buf);
1667 alloced = retval;
1668 }
1669
1670 vim_free(buf);
1671 }
1672
1673 /* if $VIM is not defined, use "vim:" instead */
1674 if (retval == NULL && STRCMP(var, "VIM") == 0)
1675 retval = (char_u *)"vim:";
1676
1677 return retval;
1678}
1679
1680/*
1681 * Amiga version of setenv() with AmigaDOS 2.0 support.
1682 */
1683/* ARGSUSED */
1684 int
1685mch_setenv(var, value, x)
1686 char *var;
1687 char *value;
1688 int x;
1689{
1690#ifdef FEAT_ARP
1691 if (!dos2)
1692 return setenv(var, value);
1693#endif
1694
1695 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
1696 return 0; /* success */
1697 return -1; /* failure */
1698}