blob: 94355b81c75ebb14d1d83d9e45d8e51338eeefc6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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/*
Bram Moolenaar9ee3d162019-07-02 23:22:43 +020064 * Set stack size to 1 MiB on NG systems. This should be enough even for
65 * hungry syntax HL / plugin combinations. Leave the stack alone on OS 3
66 * and below, those systems might be low on memory.
67 */
68#if defined(__amigaos4__)
69static const char* __attribute__((used)) stackcookie = "$STACK: 1048576";
70#elif defined(__AROS__) || defined(__MORPHOS__)
71unsigned long __stack = 1048576;
72#endif
73
74/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 * At this point TRUE and FALSE are defined as 1L and 0L, but we want 1 and 0.
76 */
77#undef TRUE
78#define TRUE (1)
79#undef FALSE
80#define FALSE (0)
81
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000082#ifdef __amigaos4__
83# define dos_packet(a, b, c) DoPkt(a, b, c, 0, 0, 0, 0)
84#elif !defined(AZTEC_C) && !defined(__AROS__)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010085static long dos_packet(struct MsgPort *, long, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010087static int lock2name(BPTR lock, char_u *buf, long len);
88static void out_num(long n);
89static struct FileInfoBlock *get_fib(char_u *);
90static int sortcmp(const void *a, const void *b);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92static BPTR raw_in = (BPTR)NULL;
93static BPTR raw_out = (BPTR)NULL;
94static int close_win = FALSE; /* set if Vim opened the window */
95
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000096#ifndef __amigaos4__ /* Use autoopen for AmigaOS4 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000097struct IntuitionBase *IntuitionBase = NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#ifdef FEAT_ARP
100struct ArpBase *ArpBase = NULL;
101#endif
102
103static struct Window *wb_window;
104static char_u *oldwindowtitle = NULL;
105
106#ifdef FEAT_ARP
107int dos2 = FALSE; /* Amiga DOS 2.0x or higher */
108#endif
109int size_set = FALSE; /* set to TRUE if window size was set */
110
111 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100112win_resize_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113{
114 OUT_STR_NF("\033[12{");
115}
116
117 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100118win_resize_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
120 OUT_STR_NF("\033[12}");
121}
122
123 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100124mch_write(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 Write(raw_out, (char *)p, (long)len);
127}
128
129/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200130 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 * Get a characters from the keyboard.
132 * If time == 0 do not wait for characters.
133 * If time == n wait a short time for characters.
134 * If time == -1 wait forever for characters.
135 *
136 * Return number of characters read.
137 */
138 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100139mch_inchar(
140 char_u *buf,
141 int maxlen,
142 long time, /* milli seconds */
143 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145 int len;
146 long utime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
148 if (time >= 0)
149 {
150 if (time == 0)
151 utime = 100L; /* time = 0 causes problems in DOS 1.2 */
152 else
153 utime = time * 1000L; /* convert from milli to micro secs */
154 if (WaitForChar(raw_in, utime) == 0) /* no character available */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 }
157 else /* time == -1 */
158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 /*
160 * If there is no character available within 2 seconds (default)
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000161 * write the autoscript file to disk. Or cause the CursorHold event
162 * to be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 */
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000164 if (WaitForChar(raw_in, p_ut * 1000L) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000166 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 {
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000168 buf[0] = K_SPECIAL;
169 buf[1] = KS_EXTRA;
170 buf[2] = (int)KE_CURSORHOLD;
171 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000173 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 }
175 }
176
177 for (;;) /* repeat until we got a character */
178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 len = Read(raw_in, (char *)buf, (long)maxlen / input_conv.vc_factor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 if (len > 0)
181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 /* Convert from 'termencoding' to 'encoding'. */
183 if (input_conv.vc_type != CONV_NONE)
184 len = convert_input(buf, len, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 return len;
186 }
187 }
188}
189
190/*
191 * return non-zero if a character is available
192 */
193 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100194mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 return (WaitForChar(raw_in, 100L) != 0);
197}
198
199/*
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200200 * Return amount of memory still available in Kbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 */
202 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100203mch_avail_mem(int special)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204{
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
Bram Moolenaar05540972016-01-30 20:31:25 +0100217mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000219#ifndef LATTICE /* SAS declares void Delay(ULONG) */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100220 void Delay(long);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#endif
222
223 if (msec > 0)
224 {
225 if (ignoreinput)
226 Delay(msec / 20L); /* Delay works with 20 msec intervals */
227 else
228 WaitForChar(raw_in, msec * 1000L);
229 }
230}
231
232/*
233 * We have no job control, fake it by starting a new shell.
234 */
235 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100236mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237{
238 suspend_shell();
239}
240
241#ifndef DOS_LIBRARY
242# define DOS_LIBRARY ((UBYTE *)"dos.library")
243#endif
244
245 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100246mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247{
248 static char intlibname[] = "intuition.library";
249
250#ifdef AZTEC_C
251 Enable_Abort = 0; /* disallow vim to be aborted */
252#endif
253 Columns = 80;
254 Rows = 24;
255
256 /*
257 * Set input and output channels, unless we have opened our own window
258 */
259 if (raw_in == (BPTR)NULL)
260 {
261 raw_in = Input();
262 raw_out = Output();
263 /*
264 * If Input() is not interactive, then Output() will be (because of
265 * check in mch_check_win()). Used for "Vim -".
266 * Also check the other way around, for "Vim -h | more".
267 */
268 if (!IsInteractive(raw_in))
269 raw_in = raw_out;
270 else if (!IsInteractive(raw_out))
271 raw_out = raw_in;
272 }
273
274 out_flush();
275
276 wb_window = NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000277#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 if ((IntuitionBase = (struct IntuitionBase *)
279 OpenLibrary((UBYTE *)intlibname, 0L)) == NULL)
280 {
281 mch_errmsg(_("cannot open "));
282 mch_errmsg(intlibname);
283 mch_errmsg("!?\n");
284 mch_exit(3);
285 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287}
288
Bram Moolenaar82881492012-11-20 16:53:39 +0100289#ifndef PROTO
290# include <workbench/startup.h>
291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
293/*
294 * Check_win checks whether we have an interactive window.
295 * If not, a new window is opened with the newcli command.
296 * If we would open a window ourselves, the :sh and :! commands would not
297 * work properly (Why? probably because we are then running in a background
298 * CLI). This also is the best way to assure proper working in a next
299 * Workbench release.
300 *
301 * For the -f option (foreground mode) we open our own window and disable :sh.
302 * Otherwise the calling program would never know when editing is finished.
303 */
304#define BUF2SIZE 320 /* length of buffer for argument with complete path */
305
306 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100307mch_check_win(int argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308{
309 int i;
310 BPTR nilfh, fh;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000311 char_u buf1[24];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 char_u buf2[BUF2SIZE];
313 static char_u *(constrings[3]) = {(char_u *)"con:0/0/662/210/",
314 (char_u *)"con:0/0/640/200/",
315 (char_u *)"con:0/0/320/200/"};
316 static char_u *winerr = (char_u *)N_("VIM: Can't open window!\n");
317 struct WBArg *argp;
318 int ac;
319 char *av;
320 char_u *device = NULL;
321 int exitval = 4;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000322#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 struct Library *DosBase;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 int usewin = FALSE;
326
327/*
328 * check if we are running under DOS 2.0x or higher
329 */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000330#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 DosBase = OpenLibrary(DOS_LIBRARY, 37L);
332 if (DosBase != NULL)
333 /* if (((struct Library *)DOSBase)->lib_Version >= 37) */
334 {
335 CloseLibrary(DosBase);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000336# ifdef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 dos2 = TRUE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000338# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 }
340 else /* without arp functions we NEED 2.0 */
341 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000342# ifndef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 mch_errmsg(_("Need Amigados version 2.04 or later\n"));
344 exit(3);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000345# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 /* need arp functions for dos 1.x */
347 if (!(ArpBase = (struct ArpBase *) OpenLibrary((UBYTE *)ArpName, ArpVersion)))
348 {
349 fprintf(stderr, _("Need %s version %ld\n"), ArpName, ArpVersion);
350 exit(3);
351 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000354#endif /* __amigaos4__ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
356 /*
357 * scan argv[] for the "-f" and "-d" arguments
358 */
359 for (i = 1; i < argc; ++i)
360 if (argv[i][0] == '-')
361 {
362 switch (argv[i][1])
363 {
364 case 'f':
365 usewin = TRUE;
366 break;
367
368 case 'd':
369 if (i < argc - 1
370#ifdef FEAT_DIFF
371 /* require using "-dev", "-d" means diff mode */
372 && argv[i][2] == 'e' && argv[i][3] == 'v'
373#endif
374 )
375 device = (char_u *)argv[i + 1];
376 break;
377 }
378 }
379
380/*
381 * If we were not started from workbench, do not have a "-d" or "-dev"
382 * argument and we have been started with an interactive window, use that
383 * window.
384 */
385 if (argc != 0
386 && device == NULL
387 && (IsInteractive(Input()) || IsInteractive(Output())))
388 return OK;
389
390/*
391 * When given the "-f" argument, we open our own window. We can't use the
392 * newcli trick below, because the calling program (mail, rn, etc.) would not
393 * know when we are finished.
394 */
395 if (usewin)
396 {
397 /*
398 * Try to open a window. First try the specified device.
399 * Then try a 24 line 80 column window.
400 * If that fails, try two smaller ones.
401 */
402 for (i = -1; i < 3; ++i)
403 {
404 if (i >= 0)
405 device = constrings[i];
406 if (device != NULL && (raw_in = Open((UBYTE *)device,
407 (long)MODE_NEWFILE)) != (BPTR)NULL)
408 break;
409 }
410 if (raw_in == (BPTR)NULL) /* all three failed */
411 {
412 mch_errmsg(_(winerr));
413 goto exit;
414 }
415 raw_out = raw_in;
416 close_win = TRUE;
417 return OK;
418 }
419
420 if ((nilfh = Open((UBYTE *)"NIL:", (long)MODE_NEWFILE)) == (BPTR)NULL)
421 {
422 mch_errmsg(_("Cannot open NIL:\n"));
423 goto exit;
424 }
425
426 /*
427 * Make a unique name for the temp file (which we will not delete!).
428 * Use a pointer on the stack (nobody else will be using it).
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000429 * Under AmigaOS4, this assumption might change in the future, so
430 * we use a pointer to the current task instead. This should be a
431 * shared structure and thus globally unique.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000433#ifdef __amigaos4__
434 sprintf((char *)buf1, "t:nc%p", FindTask(0));
435#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 sprintf((char *)buf1, "t:nc%ld", (long)buf1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 if ((fh = Open((UBYTE *)buf1, (long)MODE_NEWFILE)) == (BPTR)NULL)
439 {
440 mch_errmsg(_("Cannot create "));
441 mch_errmsg((char *)buf1);
442 mch_errmsg("\n");
443 goto exit;
444 }
445 /*
446 * Write the command into the file, put quotes around the arguments that
447 * have a space in them.
448 */
449 if (argc == 0) /* run from workbench */
450 ac = ((struct WBStartup *)argv)->sm_NumArgs;
451 else
452 ac = argc;
453 for (i = 0; i < ac; ++i)
454 {
455 if (argc == 0)
456 {
457 *buf2 = NUL;
458 argp = &(((struct WBStartup *)argv)->sm_ArgList[i]);
459 if (argp->wa_Lock)
460 (void)lock2name(argp->wa_Lock, buf2, (long)(BUF2SIZE - 1));
461#ifdef FEAT_ARP
462 if (dos2) /* use 2.0 function */
463#endif
464 AddPart((UBYTE *)buf2, (UBYTE *)argp->wa_Name, (long)(BUF2SIZE - 1));
465#ifdef FEAT_ARP
466 else /* use arp function */
467 TackOn((char *)buf2, argp->wa_Name);
468#endif
469 av = (char *)buf2;
470 }
471 else
472 av = argv[i];
473
474 /* skip '-d' or "-dev" option */
475 if (av[0] == '-' && av[1] == 'd'
476#ifdef FEAT_DIFF
477 && av[2] == 'e' && av[3] == 'v'
478#endif
479 )
480 {
481 ++i;
482 continue;
483 }
484 if (vim_strchr((char_u *)av, ' '))
485 Write(fh, "\"", 1L);
486 Write(fh, av, (long)strlen(av));
487 if (vim_strchr((char_u *)av, ' '))
488 Write(fh, "\"", 1L);
489 Write(fh, " ", 1L);
490 }
491 Write(fh, "\nendcli\n", 8L);
492 Close(fh);
493
494/*
495 * Try to open a new cli in a window. If "-d" or "-dev" argument was given try
496 * to open the specified device. Then try a 24 line 80 column window. If that
497 * fails, try two smaller ones.
498 */
499 for (i = -1; i < 3; ++i)
500 {
501 if (i >= 0)
502 device = constrings[i];
503 else if (device == NULL)
504 continue;
505 sprintf((char *)buf2, "newcli <nil: >nil: %s from %s", (char *)device, (char *)buf1);
506#ifdef FEAT_ARP
507 if (dos2)
508 {
509#endif
510 if (!SystemTags((UBYTE *)buf2, SYS_UserShell, TRUE, TAG_DONE))
511 break;
512#ifdef FEAT_ARP
513 }
514 else
515 {
516 if (Execute((UBYTE *)buf2, nilfh, nilfh))
517 break;
518 }
519#endif
520 }
521 if (i == 3) /* all three failed */
522 {
523 DeleteFile((UBYTE *)buf1);
524 mch_errmsg(_(winerr));
525 goto exit;
526 }
527 exitval = 0; /* The Execute succeeded: exit this program */
528
529exit:
530#ifdef FEAT_ARP
531 if (ArpBase)
532 CloseLibrary((struct Library *) ArpBase);
533#endif
534 exit(exitval);
535 /* NOTREACHED */
536 return FAIL;
537}
538
539/*
540 * Return TRUE if the input comes from a terminal, FALSE otherwise.
541 * We fake there is a window, because we can always open one!
542 */
543 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100544mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 return TRUE;
547}
548
549/*
550 * fname_case(): Set the case of the file name, if it already exists.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000551 * This will cause the file name to remain exactly the same
552 * if the file system ignores, but preserves case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 */
554/*ARGSUSED*/
555 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100556fname_case(
557 char_u *name,
558 int len) /* buffer size, ignored here */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
560 struct FileInfoBlock *fib;
561 size_t flen;
562
563 fib = get_fib(name);
564 if (fib != NULL)
565 {
566 flen = STRLEN(name);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000567 /* TODO: Check if this fix applies to AmigaOS < 4 too.*/
568#ifdef __amigaos4__
569 if (fib->fib_DirEntryType == ST_ROOT)
570 strcat(fib->fib_FileName, ":");
571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (flen == strlen(fib->fib_FileName)) /* safety check */
573 mch_memmove(name, fib->fib_FileName, flen);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000574 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 }
576}
577
578/*
579 * Get the FileInfoBlock for file "fname"
580 * The returned structure has to be free()d.
581 * Returns NULL on error.
582 */
583 static struct FileInfoBlock *
Bram Moolenaar05540972016-01-30 20:31:25 +0100584get_fib(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 BPTR flock;
587 struct FileInfoBlock *fib;
588
589 if (fname == NULL) /* safety check */
590 return NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000591#ifdef __amigaos4__
592 fib = AllocDosObject(DOS_FIB,0);
593#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200594 fib = ALLOC_ONE(struct FileInfoBlock);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 if (fib != NULL)
597 {
598 flock = Lock((UBYTE *)fname, (long)ACCESS_READ);
599 if (flock == (BPTR)NULL || !Examine(flock, fib))
600 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000601 free_fib(fib); /* in case of an error the memory is freed here */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 fib = NULL;
603 }
604 if (flock)
605 UnLock(flock);
606 }
607 return fib;
608}
609
610#ifdef FEAT_TITLE
611/*
612 * set the title of our window
613 * icon name is not set
614 */
615 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100616mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 if (wb_window != NULL && title != NULL)
619 SetWindowTitles(wb_window, (UBYTE *)title, (UBYTE *)-1L);
620}
621
622/*
623 * Restore the window/icon title.
624 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200625 * SAVE_RESTORE_TITLE Just restore title
626 * SAVE_RESTORE_ICON Just restore icon (which we don't have)
627 * SAVE_RESTORE_BOTH Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 */
629 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100630mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
Bram Moolenaar40385db2018-08-07 22:31:44 +0200632 if (which & SAVE_RESTORE_TITLE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 mch_settitle(oldwindowtitle, NULL);
634}
635
636 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100637mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
639 return (wb_window != NULL);
640}
641
642 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100643mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 return FALSE;
646}
647#endif
648
649/*
650 * Insert user name in s[len].
651 */
652 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100653mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000655 /* TODO: Implement this. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 *s = NUL;
657 return FAIL;
658}
659
660/*
661 * Insert host name is s[len].
662 */
663 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100664mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000666#if defined(__amigaos4__) && defined(__CLIB2__)
667 gethostname(s, len);
668#else
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000669 vim_strncpy(s, "Amiga", len - 1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671}
672
673/*
674 * return process ID
675 */
676 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100677mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000679#ifdef __amigaos4__
680 /* This is as close to a pid as we can come. We could use CLI numbers also,
681 * but then we would have two different types of process identifiers.
682 */
683 return((long)FindTask(0));
684#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 return (long)0;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687}
688
689/*
690 * Get name of current directory into buffer 'buf' of length 'len' bytes.
691 * Return OK for success, FAIL for failure.
692 */
693 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100694mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
696 return mch_FullName((char_u *)"", buf, len, FALSE);
697}
698
699/*
700 * get absolute file name into buffer 'buf' of length 'len' bytes
701 *
702 * return FAIL for failure, OK otherwise
703 */
704 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100705mch_FullName(
706 char_u *fname,
707 char_u *buf,
708 int len,
709 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
711 BPTR l;
712 int retval = FAIL;
713 int i;
714
715 /* Lock the file. If it exists, we can get the exact name. */
716 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
717 {
718 retval = lock2name(l, buf, (long)len - 1);
719 UnLock(l);
720 }
721 else if (force || !mch_isFullName(fname)) /* not a full path yet */
722 {
723 /*
724 * If the file cannot be locked (doesn't exist), try to lock the
725 * current directory and concatenate the file name.
726 */
727 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
728 {
729 retval = lock2name(l, buf, (long)len);
730 UnLock(l);
731 if (retval == OK)
732 {
733 i = STRLEN(buf);
734 /* Concatenate the fname to the directory. Don't add a slash
735 * if fname is empty, but do change "" to "/". */
736 if (i == 0 || *fname != NUL)
737 {
738 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
739 buf[i++] = '/';
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000740 vim_strncpy(buf + i, fname, len - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 }
742 }
743 }
744 }
745 if (*buf == 0 || *buf == ':')
746 retval = FAIL; /* something failed; use the file name */
747 return retval;
748}
749
750/*
751 * Return TRUE if "fname" does not depend on the current directory.
752 */
753 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100754mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755{
756 return (vim_strchr(fname, ':') != NULL && *fname != ':');
757}
758
759/*
760 * Get the full file name from a lock. Use 2.0 function if possible, because
761 * the arp function has more restrictions on the path length.
762 *
763 * return FAIL for failure, OK otherwise
764 */
765 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100766lock2name(BPTR lock, char_u *buf, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768#ifdef FEAT_ARP
769 if (dos2) /* use 2.0 function */
770#endif
771 return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL);
772#ifdef FEAT_ARP
773 else /* use arp function */
774 return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL);
775#endif
776}
777
778/*
779 * get file permissions for 'name'
780 * Returns -1 when it doesn't exist.
781 */
782 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100783mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
785 struct FileInfoBlock *fib;
786 long retval = -1;
787
788 fib = get_fib(name);
789 if (fib != NULL)
790 {
791 retval = fib->fib_Protection;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000792 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794 return retval;
795}
796
797/*
798 * set file permission for 'name' to 'perm'
799 *
800 * return FAIL for failure, OK otherwise
801 */
802 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100803mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 perm &= ~FIBF_ARCHIVE; /* reset archived bit */
806 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
807}
808
809/*
810 * Set hidden flag for "name".
811 */
812 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100813mch_hide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 /* can't hide a file */
816}
817
818/*
819 * return FALSE if "name" is not a directory
820 * return TRUE if "name" is a directory.
821 * return FALSE for error.
822 */
823 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100824mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825{
826 struct FileInfoBlock *fib;
827 int retval = FALSE;
828
829 fib = get_fib(name);
830 if (fib != NULL)
831 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000832#ifdef __amigaos4__
833 retval = (FIB_IS_DRAWER(fib)) ? TRUE : FALSE;
834#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000836#endif
837 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 }
839 return retval;
840}
841
842/*
843 * Create directory "name".
844 */
Bram Moolenaare3853642006-09-14 19:36:57 +0000845 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100846mch_mkdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
848 BPTR lock;
849
850 lock = CreateDir(name);
851 if (lock != NULL)
Bram Moolenaare3853642006-09-14 19:36:57 +0000852 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 UnLock(lock);
Bram Moolenaare3853642006-09-14 19:36:57 +0000854 return 0;
855 }
856 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859/*
860 * Return 1 if "name" can be executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +0100861 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * Return -1 if unknown.
863 */
864 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100865mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866{
867 /* TODO */
868 return -1;
869}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
871/*
872 * Check what "name" is:
873 * NODE_NORMAL: file or directory (or doesn't exist)
874 * NODE_WRITABLE: writable device, socket, fifo, etc.
875 * NODE_OTHER: non-writable things
876 */
877 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100878mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
880 /* TODO */
881 return NODE_NORMAL;
882}
883
884 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100885mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886{
887}
888
889/*
890 * Careful: mch_exit() may be called before mch_init()!
891 */
892 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100893mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100895 exiting = TRUE;
896
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (raw_in) /* put terminal in 'normal' mode */
898 {
899 settmode(TMODE_COOK);
900 stoptermcap();
901 }
902 out_char('\n');
903 if (raw_out)
904 {
905 if (term_console)
906 {
907 win_resize_off(); /* window resize events de-activated */
908 if (size_set)
909 OUT_STR("\233t\233u"); /* reset window size (CSI t CSI u) */
910 }
911 out_flush();
912 }
913
914#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +0200915 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window title */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916#endif
917
918 ml_close_all(TRUE); /* remove all memfiles */
919
920#ifdef FEAT_ARP
921 if (ArpBase)
922 CloseLibrary((struct Library *) ArpBase);
923#endif
924 if (close_win)
925 Close(raw_in);
926 if (r)
927 printf(_("Vim exiting with %d\n"), r); /* somehow this makes :cq work!? */
928 exit(r);
929}
930
931/*
932 * This is a routine for setting a given stream to raw or cooked mode on the
933 * Amiga . This is useful when you are using Lattice C to produce programs
934 * that want to read single characters with the "getch()" or "fgetc" call.
935 *
936 * Written : 18-Jun-87 By Chuck McManis.
937 */
938
939#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
940
941/*
942 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
943 * 'cooked' mode. This only works on TTY's.
944 *
945 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
946 * getch() will return immediately rather than wait for a return. You
947 * lose editing features though.
948 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100949 * Cooked: This function returns the designate file pointer to its normal,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 * wait for a <CR> mode. This is exactly like raw() except that
951 * it sends a 0 to the console to make it back into a CON: from a RAW:
952 */
953 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100954mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000956#if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
958#else
959 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
960 tmode == TMODE_RAW ? -1L : 0L) == 0)
961#endif
962 mch_errmsg(_("cannot change console mode ?!\n"));
963}
964
965/*
966 * set screen mode, always fails.
967 */
968 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100969mch_screenmode(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100971 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 return FAIL;
973}
974
975/*
976 * Code for this routine came from the following :
977 *
978 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
979 * DOS packet example
980 * Requires 1.2
981 *
982 * Found on Fish Disk 56.
983 *
984 * Heavely modified by mool.
985 */
986
Bram Moolenaar82881492012-11-20 16:53:39 +0100987#ifndef PROTO
988# include <devices/conunit.h>
989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
991/*
992 * try to get the real window size
993 * return FAIL for failure, OK otherwise
994 */
995 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100996mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
998 struct ConUnit *conUnit;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000999#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 char id_a[sizeof(struct InfoData) + 3];
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001001#endif
1002 struct InfoData *id=0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 if (!term_console) /* not an amiga window */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001005 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 /* insure longword alignment */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001008#ifdef __amigaos4__
Bram Moolenaar62dbdc42011-10-20 18:24:22 +02001009 if (!(id = AllocDosObject(DOS_INFODATA, 0)))
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001010 goto out;
1011#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015 /*
1016 * Should make console aware of real window size, not the one we set.
1017 * Unfortunately, under DOS 2.0x this redraws the window and it
1018 * is rarely needed, so we skip it now, unless we changed the size.
1019 */
1020 if (size_set)
1021 OUT_STR("\233t\233u"); /* CSI t CSI u */
1022 out_flush();
1023
1024#ifdef __AROS__
1025 if (!Info(raw_out, id)
1026 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
1027#else
1028 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
1029 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
1030#endif
1031 {
1032 /* it's not an amiga window, maybe aux device */
1033 /* terminal type should be set */
1034 term_console = FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001035 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
1037 if (oldwindowtitle == NULL)
1038 oldwindowtitle = (char_u *)wb_window->Title;
1039 if (id->id_InUse == (BPTR)NULL)
1040 {
1041 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
1042 return FAIL;
1043 }
1044 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
1045
1046 /* get window size */
1047 Rows = conUnit->cu_YMax + 1;
1048 Columns = conUnit->cu_XMax + 1;
1049 if (Rows < 0 || Rows > 200) /* cannot be an amiga window */
1050 {
1051 Columns = 80;
1052 Rows = 24;
1053 term_console = FALSE;
1054 return FAIL;
1055 }
1056
1057 return OK;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001058out:
1059#ifdef __amigaos4__
1060 FreeDosObject(DOS_INFODATA, id); /* Safe to pass NULL */
1061#endif
1062
1063 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064}
1065
1066/*
1067 * Try to set the real window size to Rows and Columns.
1068 */
1069 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001070mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 if (term_console)
1073 {
1074 size_set = TRUE;
1075 out_char(CSI);
1076 out_num((long)Rows);
1077 out_char('t');
1078 out_char(CSI);
1079 out_num((long)Columns);
1080 out_char('u');
1081 out_flush();
1082 }
1083}
1084
1085/*
1086 * Rows and/or Columns has changed.
1087 */
1088 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001089mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 /* Nothing to do. */
1092}
1093
1094/*
1095 * out_num - output a (big) number fast
1096 */
1097 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001098out_num(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
1100 OUT_STR_NF(tltoa((unsigned long)n));
1101}
1102
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001103#if !defined(AZTEC_C) && !defined(__AROS__) && !defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104/*
1105 * Sendpacket.c
1106 *
1107 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1108 * the given message port. This makes working around DOS lots easier.
1109 *
1110 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1111 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1112 * say 'oml lib:amiga.lib -r sendpacket.o'
1113 */
1114
Bram Moolenaar82881492012-11-20 16:53:39 +01001115#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116/* #include <proto/exec.h> */
1117/* #include <proto/dos.h> */
Bram Moolenaar82881492012-11-20 16:53:39 +01001118# include <exec/memory.h>
1119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
1121/*
1122 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1123 * Finkel. This function will send a packet of the given type to the Message
1124 * Port supplied.
1125 */
1126
1127 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01001128dos_packet(
1129 struct MsgPort *pid, /* process identifier ... (handlers message port) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 long action, /* packet type ... (what you want handler to do) */
Bram Moolenaard90b6c02016-08-28 18:10:45 +02001131 long arg) /* single argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133# ifdef FEAT_ARP
1134 struct MsgPort *replyport;
1135 struct StandardPacket *packet;
1136 long res1;
1137
1138 if (dos2)
1139# endif
1140 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); /* use 2.0 function */
1141# ifdef FEAT_ARP
1142
1143 replyport = (struct MsgPort *) CreatePort(NULL, 0); /* use arp function */
1144 if (!replyport)
1145 return (0);
1146
1147 /* Allocate space for a packet, make it public and clear it */
1148 packet = (struct StandardPacket *)
1149 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
1150 if (!packet) {
1151 DeletePort(replyport);
1152 return (0);
1153 }
1154 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1155 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1156 packet->sp_Pkt.dp_Port = replyport;
1157 packet->sp_Pkt.dp_Type = action;
1158 packet->sp_Pkt.dp_Arg1 = arg;
1159
1160 PutMsg(pid, (struct Message *)packet); /* send packet */
1161
1162 WaitPort(replyport);
1163 GetMsg(replyport);
1164
1165 res1 = packet->sp_Pkt.dp_Res1;
1166
1167 FreeMem(packet, (long) sizeof(struct StandardPacket));
1168 DeletePort(replyport);
1169
1170 return (res1);
1171# endif
1172}
1173#endif /* !defined(AZTEC_C) && !defined(__AROS__) */
1174
1175/*
1176 * Call shell.
1177 * Return error number for failure, 0 otherwise
1178 */
1179 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001180mch_call_shell(
1181 char_u *cmd,
1182 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 BPTR mydir;
1185 int x;
1186 int tmode = cur_tmode;
1187#ifdef AZTEC_C
1188 int use_execute;
1189 char_u *shellcmd = NULL;
1190 char_u *shellarg;
1191#endif
1192 int retval = 0;
1193
1194 if (close_win)
1195 {
1196 /* if Vim opened a window: Executing a shell may cause crashes */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_("E360: Cannot execute shell with -f option"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 return -1;
1199 }
1200
1201 if (term_console)
1202 win_resize_off(); /* window resize events de-activated */
1203 out_flush();
1204
1205 if (options & SHELL_COOKED)
1206 settmode(TMODE_COOK); /* set to normal mode */
1207 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); /* remember current dir */
1208
1209#if !defined(AZTEC_C) /* not tested very much */
1210 if (cmd == NULL)
1211 {
1212# ifdef FEAT_ARP
1213 if (dos2)
1214# endif
1215 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1216# ifdef FEAT_ARP
1217 else
1218 x = Execute(p_sh, raw_in, raw_out);
1219# endif
1220 }
1221 else
1222 {
1223# ifdef FEAT_ARP
1224 if (dos2)
1225# endif
1226 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1227# ifdef FEAT_ARP
1228 else
1229 x = Execute((char *)cmd, 0L, raw_out);
1230# endif
1231 }
1232# ifdef FEAT_ARP
1233 if ((dos2 && x < 0) || (!dos2 && !x))
1234# else
1235 if (x < 0)
1236# endif
1237 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001238 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (cmd == NULL)
1240 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001241 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 msg_outtrans(p_sh);
1243 }
1244 else
1245 msg_outtrans(cmd);
1246 msg_putchar('\n');
1247 retval = -1;
1248 }
1249# ifdef FEAT_ARP
1250 else if (!dos2 || x)
1251# else
1252 else if (x)
1253# endif
1254 {
1255 if ((x = IoErr()) != 0)
1256 {
1257 if (!(options & SHELL_SILENT))
1258 {
1259 msg_putchar('\n');
1260 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001261 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263 retval = x;
1264 }
1265 }
1266#else /* else part is for AZTEC_C */
1267 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1268 use_execute = 1;
1269 else
1270 use_execute = 0;
1271 if (!use_execute)
1272 {
1273 /*
1274 * separate shell name from argument
1275 */
1276 shellcmd = vim_strsave(p_sh);
1277 if (shellcmd == NULL) /* out of memory, use Execute */
1278 use_execute = 1;
1279 else
1280 {
1281 shellarg = skiptowhite(shellcmd); /* find start of arguments */
1282 if (*shellarg != NUL)
1283 {
1284 *shellarg++ = NUL;
1285 shellarg = skipwhite(shellarg);
1286 }
1287 }
1288 }
1289 if (cmd == NULL)
1290 {
1291 if (use_execute)
1292 {
1293# ifdef FEAT_ARP
1294 if (dos2)
1295# endif
1296 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1297# ifdef FEAT_ARP
1298 else
1299 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1300# endif
1301 }
1302 else
1303 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1304 }
1305 else if (use_execute)
1306 {
1307# ifdef FEAT_ARP
1308 if (dos2)
1309# endif
1310 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1311# ifdef FEAT_ARP
1312 else
1313 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1314# endif
1315 }
1316 else if (p_st & 1)
1317 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1318 (char *)cmd, NULL);
1319 else
1320 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1321 (char *)p_shcf, (char *)cmd, NULL);
1322# ifdef FEAT_ARP
1323 if ((dos2 && x < 0) || (!dos2 && x))
1324# else
1325 if (x < 0)
1326# endif
1327 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001328 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (use_execute)
1330 {
1331 if (cmd == NULL)
1332 msg_outtrans(p_sh);
1333 else
1334 msg_outtrans(cmd);
1335 }
1336 else
1337 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001338 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 msg_outtrans(shellcmd);
1340 }
1341 msg_putchar('\n');
1342 retval = -1;
1343 }
1344 else
1345 {
1346 if (use_execute)
1347 {
1348# ifdef FEAT_ARP
1349 if (!dos2 || x)
1350# else
1351 if (x)
1352# endif
1353 x = IoErr();
1354 }
1355 else
1356 x = wait();
1357 if (x)
1358 {
1359 if (!(options & SHELL_SILENT) && !emsg_silent)
1360 {
1361 msg_putchar('\n');
1362 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001363 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365 retval = x;
1366 }
1367 }
1368 vim_free(shellcmd);
1369#endif /* AZTEC_C */
1370
1371 if ((mydir = CurrentDir(mydir)) != 0) /* make sure we stay in the same directory */
1372 UnLock(mydir);
1373 if (tmode == TMODE_RAW)
1374 settmode(TMODE_RAW); /* set to raw mode */
1375#ifdef FEAT_TITLE
1376 resettitle();
1377#endif
1378 if (term_console)
1379 win_resize_on(); /* window resize events activated */
1380 return retval;
1381}
1382
1383/*
1384 * check for an "interrupt signal"
1385 * We only react to a CTRL-C, but also clear the other break signals to avoid
1386 * trouble with lattice-c programs.
1387 */
1388 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02001389mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1392 got_int = TRUE;
1393}
1394
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01001395/* this routine causes manx to use this Chk_Abort() rather than its own */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396/* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
1397/* is zero). Since we want to check for our own ^C's */
1398
1399#ifdef _DCC
1400#define Chk_Abort chkabort
1401#endif
1402
1403#ifdef LATTICE
1404void __regargs __chkabort(void);
1405
1406void __regargs __chkabort(void)
1407{}
1408
1409#else
1410 long
1411Chk_Abort(void)
1412{
1413 return(0L);
1414}
1415#endif
1416
1417/*
1418 * mch_expandpath() - this code does wild-card pattern matching using the arp
1419 * routines.
1420 *
1421 * "pat" has backslashes before chars that are not to be expanded.
1422 * Returns the number of matches found.
1423 *
1424 * This is based on WildDemo2.c (found in arp1.1 distribution).
1425 * That code's copyright follows:
1426 * Copyright (c) 1987, Scott Ballantyne
1427 * Use and abuse as you please.
1428 */
1429
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001430#ifdef __amigaos4__
1431# define ANCHOR_BUF_SIZE 1024
1432#else
1433# define ANCHOR_BUF_SIZE (512)
1434# define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001438mch_expandpath(
1439 garray_T *gap,
1440 char_u *pat,
1441 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
1443 struct AnchorPath *Anchor;
1444 LONG Result;
1445 char_u *starbuf, *sp, *dp;
1446 int start_len;
1447 int matches;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001448#ifdef __amigaos4__
1449 struct TagItem AnchorTags[] = {
1450 {ADO_Strlen, ANCHOR_BUF_SIZE},
1451 {ADO_Flags, APF_DODOT|APF_DOWILD|APF_MultiAssigns},
1452 {TAG_DONE, 0L}
1453 };
1454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
1456 start_len = gap->ga_len;
1457
1458 /* Get our AnchorBase */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001459#ifdef __amigaos4__
1460 Anchor = AllocDosObject(DOS_ANCHORPATH, AnchorTags);
1461#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001462 Anchor = alloc_clear(ANCHOR_SIZE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (Anchor == NULL)
1465 return 0;
1466
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001467#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; /* ap_Length not supported anymore */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001469# ifdef APF_DODOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001471# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 Anchor->ap_Flags = APF_DoDot | APF_DoWild; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#endif
1475
1476#ifdef FEAT_ARP
1477 if (dos2)
1478 {
1479#endif
1480 /* hack to replace '*' by '#?' */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001481 starbuf = alloc(2 * STRLEN(pat) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (starbuf == NULL)
1483 goto Return;
1484 for (sp = pat, dp = starbuf; *sp; ++sp)
1485 {
1486 if (*sp == '*')
1487 {
1488 *dp++ = '#';
1489 *dp++ = '?';
1490 }
1491 else
1492 *dp++ = *sp;
1493 }
1494 *dp = NUL;
1495 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1496 vim_free(starbuf);
1497#ifdef FEAT_ARP
1498 }
1499 else
1500 Result = FindFirst((char *)pat, Anchor);
1501#endif
1502
1503 /*
1504 * Loop to get all matches.
1505 */
1506 while (Result == 0)
1507 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001508#ifdef __amigaos4__
1509 addfile(gap, (char_u *)Anchor->ap_Buffer, flags);
1510#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513#ifdef FEAT_ARP
1514 if (dos2)
1515#endif
1516 Result = MatchNext(Anchor);
1517#ifdef FEAT_ARP
1518 else
1519 Result = FindNext(Anchor);
1520#endif
1521 }
1522 matches = gap->ga_len - start_len;
1523
1524 if (Result == ERROR_BUFFER_OVERFLOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001525 emsg(_("ANCHOR_BUF_SIZE too small."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1527 && Result != ERROR_DEVICE_NOT_MOUNTED
1528 && Result != ERROR_NO_MORE_ENTRIES)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001529 emsg(_("I/O ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
1531 /*
1532 * Sort the files for this pattern.
1533 */
1534 if (matches)
1535 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1536 (size_t)matches, sizeof(char_u *), sortcmp);
1537
1538 /* Free the wildcard stuff */
1539#ifdef FEAT_ARP
1540 if (dos2)
1541#endif
1542 MatchEnd(Anchor);
1543#ifdef FEAT_ARP
1544 else
1545 FreeAnchorChain(Anchor);
1546#endif
1547
1548Return:
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001549#ifdef __amigaos4__
1550 FreeDosObject(DOS_ANCHORPATH, Anchor);
1551#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 vim_free(Anchor);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
1555 return matches;
1556}
1557
1558 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001559sortcmp(const void *a, const void *b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 char *s = *(char **)a;
1562 char *t = *(char **)b;
1563
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001564 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565}
1566
1567/*
1568 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1569 */
1570 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001571mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001573 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 {
1575 if (*p == '\\' && p[1] != NUL)
1576 ++p;
1577 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1578 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 return FALSE;
1581}
1582
1583 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001584mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001586 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588 if (*p == '\\' && p[1] != NUL)
1589 ++p;
1590 else
1591 if (vim_strchr((char_u *)
1592# ifdef VIM_BACKTICK
1593 "*?[(#$`"
1594# else
1595 "*?[(#$"
1596# endif
1597 , *p) != NULL
1598 || (*p == '~' && p[1] != NUL))
1599 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 return FALSE;
1602}
1603
1604/*
1605 * With AmigaDOS 2.0 support for reading local environment variables
1606 *
1607 * Two buffers are allocated:
1608 * - A big one to do the expansion into. It is freed before returning.
1609 * - A small one to hold the return value. It is kept until the next call.
1610 */
1611 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001612mch_getenv(char_u *var)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 int len;
1615 UBYTE *buf; /* buffer to expand in */
1616 char_u *retval; /* return value */
1617 static char_u *alloced = NULL; /* allocated memory */
1618
1619#ifdef FEAT_ARP
1620 if (!dos2)
1621 retval = (char_u *)getenv((char *)var);
1622 else
1623#endif
1624 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001625 VIM_CLEAR(alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 retval = NULL;
1627
1628 buf = alloc(IOSIZE);
1629 if (buf == NULL)
1630 return NULL;
1631
1632 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1633 if (len >= 0)
1634 {
1635 retval = vim_strsave((char_u *)buf);
1636 alloced = retval;
1637 }
1638
1639 vim_free(buf);
1640 }
1641
1642 /* if $VIM is not defined, use "vim:" instead */
1643 if (retval == NULL && STRCMP(var, "VIM") == 0)
1644 retval = (char_u *)"vim:";
1645
1646 return retval;
1647}
1648
1649/*
1650 * Amiga version of setenv() with AmigaDOS 2.0 support.
1651 */
1652/* ARGSUSED */
1653 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001654mch_setenv(char *var, char *value, int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
1656#ifdef FEAT_ARP
1657 if (!dos2)
1658 return setenv(var, value);
1659#endif
1660
1661 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
1662 return 0; /* success */
1663 return -1; /* failure */
1664}