blob: 8627cb60af4f7e644284d11af2cc8a6d83710361 [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"
Bram Moolenaar70576f72019-07-31 20:40:08 +020017#include "version.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
19#ifdef Window
20# undef Window /* Amiga has its own Window definition */
21#endif
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#undef TRUE /* will be redefined by exec/types.h */
24#undef FALSE
25
Bram Moolenaar82881492012-11-20 16:53:39 +010026/* cproto fails on missing include files, skip them */
27#ifndef PROTO
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifndef LATTICE
30# include <exec/types.h>
31# include <exec/exec.h>
32# include <libraries/dos.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000033# include <intuition/intuition.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
35
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000036/* XXX These are included from os_amiga.h
37#include <proto/exec.h>
38#include <proto/dos.h>
39#include <proto/intuition.h>
40*/
41
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#include <exec/memory.h>
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000043#include <libraries/dosextens.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45#include <dos/dostags.h> /* for 2.0 functions */
46#include <dos/dosasl.h>
47
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000048/* From version 4 of AmigaOS, several system structures must be allocated
49 * and freed using system functions. "struct AnchorPath" is one.
50 */
51#ifdef __amigaos4__
52# include <dos/anchorpath.h>
53# define free_fib(x) FreeDosObject(DOS_FIB, x)
54#else
55# define free_fib(x) vim_free(fib)
56#endif
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#if defined(LATTICE) && !defined(SASC) && defined(FEAT_ARP)
59# include <libraries/arp_pragmas.h>
60#endif
61
Bram Moolenaar82881492012-11-20 16:53:39 +010062#endif /* PROTO */
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064/*
Bram Moolenaar9ee3d162019-07-02 23:22:43 +020065 * Set stack size to 1 MiB on NG systems. This should be enough even for
66 * hungry syntax HL / plugin combinations. Leave the stack alone on OS 3
67 * and below, those systems might be low on memory.
68 */
69#if defined(__amigaos4__)
70static const char* __attribute__((used)) stackcookie = "$STACK: 1048576";
71#elif defined(__AROS__) || defined(__MORPHOS__)
72unsigned long __stack = 1048576;
73#endif
74
75/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 * At this point TRUE and FALSE are defined as 1L and 0L, but we want 1 and 0.
77 */
78#undef TRUE
79#define TRUE (1)
80#undef FALSE
81#define FALSE (0)
82
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000083#ifdef __amigaos4__
84# define dos_packet(a, b, c) DoPkt(a, b, c, 0, 0, 0, 0)
85#elif !defined(AZTEC_C) && !defined(__AROS__)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010086static long dos_packet(struct MsgPort *, long, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010088static int lock2name(BPTR lock, char_u *buf, long len);
89static void out_num(long n);
90static struct FileInfoBlock *get_fib(char_u *);
91static int sortcmp(const void *a, const void *b);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93static BPTR raw_in = (BPTR)NULL;
94static BPTR raw_out = (BPTR)NULL;
95static int close_win = FALSE; /* set if Vim opened the window */
96
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000097#ifndef __amigaos4__ /* Use autoopen for AmigaOS4 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000098struct IntuitionBase *IntuitionBase = NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#ifdef FEAT_ARP
101struct ArpBase *ArpBase = NULL;
102#endif
103
104static struct Window *wb_window;
105static char_u *oldwindowtitle = NULL;
106
107#ifdef FEAT_ARP
108int dos2 = FALSE; /* Amiga DOS 2.0x or higher */
109#endif
110int size_set = FALSE; /* set to TRUE if window size was set */
111
Bram Moolenaar70576f72019-07-31 20:40:08 +0200112#ifdef __GNUC__
113static char version[] __attribute__((used)) =
114 "\0$VER: Vim "
115 VIM_VERSION_MAJOR_STR "."
116 VIM_VERSION_MINOR_STR
117# ifdef PATCHLEVEL
118 "." PATCHLEVEL
119# endif
120 ;
121#endif
122
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100124win_resize_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 OUT_STR_NF("\033[12{");
127}
128
129 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100130win_resize_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 OUT_STR_NF("\033[12}");
133}
134
135 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100136mch_write(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137{
138 Write(raw_out, (char *)p, (long)len);
139}
140
141/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200142 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 * Get a characters from the keyboard.
144 * If time == 0 do not wait for characters.
145 * If time == n wait a short time for characters.
146 * If time == -1 wait forever for characters.
147 *
148 * Return number of characters read.
149 */
150 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100151mch_inchar(
152 char_u *buf,
153 int maxlen,
154 long time, /* milli seconds */
155 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156{
157 int len;
158 long utime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
160 if (time >= 0)
161 {
162 if (time == 0)
163 utime = 100L; /* time = 0 causes problems in DOS 1.2 */
164 else
165 utime = time * 1000L; /* convert from milli to micro secs */
166 if (WaitForChar(raw_in, utime) == 0) /* no character available */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 }
169 else /* time == -1 */
170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 /*
172 * If there is no character available within 2 seconds (default)
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000173 * write the autoscript file to disk. Or cause the CursorHold event
174 * to be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 */
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000176 if (WaitForChar(raw_in, p_ut * 1000L) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000178 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 {
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000180 buf[0] = K_SPECIAL;
181 buf[1] = KS_EXTRA;
182 buf[2] = (int)KE_CURSORHOLD;
183 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000185 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 }
187 }
188
189 for (;;) /* repeat until we got a character */
190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 len = Read(raw_in, (char *)buf, (long)maxlen / input_conv.vc_factor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 if (len > 0)
193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 /* Convert from 'termencoding' to 'encoding'. */
195 if (input_conv.vc_type != CONV_NONE)
196 len = convert_input(buf, len, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 return len;
198 }
199 }
200}
201
202/*
203 * return non-zero if a character is available
204 */
205 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100206mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207{
208 return (WaitForChar(raw_in, 100L) != 0);
209}
210
211/*
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200212 * Return amount of memory still available in Kbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 */
214 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100215mch_avail_mem(int special)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
Bram Moolenaara9ab3912019-08-10 14:54:20 +0200217#if defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200218 return (long_u)AvailMem(MEMF_ANY) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000219#else
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200220 return (long_u)(AvailMem(special ? (long)MEMF_CHIP : (long)MEMF_ANY)) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222}
223
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000224/*
225 * Waits a specified amount of time, or until input arrives if
226 * ignoreinput is FALSE.
227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100229mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000231#ifndef LATTICE /* SAS declares void Delay(ULONG) */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100232 void Delay(long);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
234
235 if (msec > 0)
236 {
237 if (ignoreinput)
238 Delay(msec / 20L); /* Delay works with 20 msec intervals */
239 else
240 WaitForChar(raw_in, msec * 1000L);
241 }
242}
243
244/*
245 * We have no job control, fake it by starting a new shell.
246 */
247 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100248mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249{
250 suspend_shell();
251}
252
253#ifndef DOS_LIBRARY
254# define DOS_LIBRARY ((UBYTE *)"dos.library")
255#endif
256
257 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100258mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 static char intlibname[] = "intuition.library";
261
262#ifdef AZTEC_C
263 Enable_Abort = 0; /* disallow vim to be aborted */
264#endif
265 Columns = 80;
266 Rows = 24;
267
268 /*
269 * Set input and output channels, unless we have opened our own window
270 */
271 if (raw_in == (BPTR)NULL)
272 {
273 raw_in = Input();
274 raw_out = Output();
275 /*
276 * If Input() is not interactive, then Output() will be (because of
277 * check in mch_check_win()). Used for "Vim -".
278 * Also check the other way around, for "Vim -h | more".
279 */
280 if (!IsInteractive(raw_in))
281 raw_in = raw_out;
282 else if (!IsInteractive(raw_out))
283 raw_out = raw_in;
284 }
285
286 out_flush();
287
288 wb_window = NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000289#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 if ((IntuitionBase = (struct IntuitionBase *)
291 OpenLibrary((UBYTE *)intlibname, 0L)) == NULL)
292 {
293 mch_errmsg(_("cannot open "));
294 mch_errmsg(intlibname);
295 mch_errmsg("!?\n");
296 mch_exit(3);
297 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299}
300
Bram Moolenaar82881492012-11-20 16:53:39 +0100301#ifndef PROTO
302# include <workbench/startup.h>
303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304
305/*
306 * Check_win checks whether we have an interactive window.
307 * If not, a new window is opened with the newcli command.
308 * If we would open a window ourselves, the :sh and :! commands would not
309 * work properly (Why? probably because we are then running in a background
310 * CLI). This also is the best way to assure proper working in a next
311 * Workbench release.
312 *
313 * For the -f option (foreground mode) we open our own window and disable :sh.
314 * Otherwise the calling program would never know when editing is finished.
315 */
316#define BUF2SIZE 320 /* length of buffer for argument with complete path */
317
318 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100319mch_check_win(int argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320{
321 int i;
322 BPTR nilfh, fh;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000323 char_u buf1[24];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 char_u buf2[BUF2SIZE];
325 static char_u *(constrings[3]) = {(char_u *)"con:0/0/662/210/",
326 (char_u *)"con:0/0/640/200/",
327 (char_u *)"con:0/0/320/200/"};
328 static char_u *winerr = (char_u *)N_("VIM: Can't open window!\n");
329 struct WBArg *argp;
330 int ac;
331 char *av;
332 char_u *device = NULL;
333 int exitval = 4;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000334#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 struct Library *DosBase;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 int usewin = FALSE;
338
339/*
340 * check if we are running under DOS 2.0x or higher
341 */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000342#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 DosBase = OpenLibrary(DOS_LIBRARY, 37L);
344 if (DosBase != NULL)
345 /* if (((struct Library *)DOSBase)->lib_Version >= 37) */
346 {
347 CloseLibrary(DosBase);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000348# ifdef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 dos2 = TRUE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 }
352 else /* without arp functions we NEED 2.0 */
353 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000354# ifndef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 mch_errmsg(_("Need Amigados version 2.04 or later\n"));
356 exit(3);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000357# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /* need arp functions for dos 1.x */
359 if (!(ArpBase = (struct ArpBase *) OpenLibrary((UBYTE *)ArpName, ArpVersion)))
360 {
361 fprintf(stderr, _("Need %s version %ld\n"), ArpName, ArpVersion);
362 exit(3);
363 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000366#endif /* __amigaos4__ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367
368 /*
369 * scan argv[] for the "-f" and "-d" arguments
370 */
371 for (i = 1; i < argc; ++i)
372 if (argv[i][0] == '-')
373 {
374 switch (argv[i][1])
375 {
376 case 'f':
377 usewin = TRUE;
378 break;
379
380 case 'd':
381 if (i < argc - 1
382#ifdef FEAT_DIFF
383 /* require using "-dev", "-d" means diff mode */
384 && argv[i][2] == 'e' && argv[i][3] == 'v'
385#endif
386 )
387 device = (char_u *)argv[i + 1];
388 break;
389 }
390 }
391
392/*
393 * If we were not started from workbench, do not have a "-d" or "-dev"
394 * argument and we have been started with an interactive window, use that
395 * window.
396 */
397 if (argc != 0
398 && device == NULL
399 && (IsInteractive(Input()) || IsInteractive(Output())))
400 return OK;
401
402/*
403 * When given the "-f" argument, we open our own window. We can't use the
404 * newcli trick below, because the calling program (mail, rn, etc.) would not
405 * know when we are finished.
406 */
407 if (usewin)
408 {
409 /*
410 * Try to open a window. First try the specified device.
411 * Then try a 24 line 80 column window.
412 * If that fails, try two smaller ones.
413 */
414 for (i = -1; i < 3; ++i)
415 {
416 if (i >= 0)
417 device = constrings[i];
418 if (device != NULL && (raw_in = Open((UBYTE *)device,
419 (long)MODE_NEWFILE)) != (BPTR)NULL)
420 break;
421 }
422 if (raw_in == (BPTR)NULL) /* all three failed */
423 {
424 mch_errmsg(_(winerr));
425 goto exit;
426 }
427 raw_out = raw_in;
428 close_win = TRUE;
429 return OK;
430 }
431
432 if ((nilfh = Open((UBYTE *)"NIL:", (long)MODE_NEWFILE)) == (BPTR)NULL)
433 {
434 mch_errmsg(_("Cannot open NIL:\n"));
435 goto exit;
436 }
437
438 /*
439 * Make a unique name for the temp file (which we will not delete!).
440 * Use a pointer on the stack (nobody else will be using it).
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000441 * Under AmigaOS4, this assumption might change in the future, so
442 * we use a pointer to the current task instead. This should be a
443 * shared structure and thus globally unique.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000445#ifdef __amigaos4__
446 sprintf((char *)buf1, "t:nc%p", FindTask(0));
447#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 sprintf((char *)buf1, "t:nc%ld", (long)buf1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if ((fh = Open((UBYTE *)buf1, (long)MODE_NEWFILE)) == (BPTR)NULL)
451 {
452 mch_errmsg(_("Cannot create "));
453 mch_errmsg((char *)buf1);
454 mch_errmsg("\n");
455 goto exit;
456 }
457 /*
458 * Write the command into the file, put quotes around the arguments that
459 * have a space in them.
460 */
461 if (argc == 0) /* run from workbench */
462 ac = ((struct WBStartup *)argv)->sm_NumArgs;
463 else
464 ac = argc;
465 for (i = 0; i < ac; ++i)
466 {
467 if (argc == 0)
468 {
469 *buf2 = NUL;
470 argp = &(((struct WBStartup *)argv)->sm_ArgList[i]);
471 if (argp->wa_Lock)
472 (void)lock2name(argp->wa_Lock, buf2, (long)(BUF2SIZE - 1));
473#ifdef FEAT_ARP
474 if (dos2) /* use 2.0 function */
475#endif
476 AddPart((UBYTE *)buf2, (UBYTE *)argp->wa_Name, (long)(BUF2SIZE - 1));
477#ifdef FEAT_ARP
478 else /* use arp function */
479 TackOn((char *)buf2, argp->wa_Name);
480#endif
481 av = (char *)buf2;
482 }
483 else
484 av = argv[i];
485
486 /* skip '-d' or "-dev" option */
487 if (av[0] == '-' && av[1] == 'd'
488#ifdef FEAT_DIFF
489 && av[2] == 'e' && av[3] == 'v'
490#endif
491 )
492 {
493 ++i;
494 continue;
495 }
496 if (vim_strchr((char_u *)av, ' '))
497 Write(fh, "\"", 1L);
498 Write(fh, av, (long)strlen(av));
499 if (vim_strchr((char_u *)av, ' '))
500 Write(fh, "\"", 1L);
501 Write(fh, " ", 1L);
502 }
503 Write(fh, "\nendcli\n", 8L);
504 Close(fh);
505
506/*
507 * Try to open a new cli in a window. If "-d" or "-dev" argument was given try
508 * to open the specified device. Then try a 24 line 80 column window. If that
509 * fails, try two smaller ones.
510 */
511 for (i = -1; i < 3; ++i)
512 {
513 if (i >= 0)
514 device = constrings[i];
515 else if (device == NULL)
516 continue;
517 sprintf((char *)buf2, "newcli <nil: >nil: %s from %s", (char *)device, (char *)buf1);
518#ifdef FEAT_ARP
519 if (dos2)
520 {
521#endif
522 if (!SystemTags((UBYTE *)buf2, SYS_UserShell, TRUE, TAG_DONE))
523 break;
524#ifdef FEAT_ARP
525 }
526 else
527 {
528 if (Execute((UBYTE *)buf2, nilfh, nilfh))
529 break;
530 }
531#endif
532 }
533 if (i == 3) /* all three failed */
534 {
535 DeleteFile((UBYTE *)buf1);
536 mch_errmsg(_(winerr));
537 goto exit;
538 }
539 exitval = 0; /* The Execute succeeded: exit this program */
540
541exit:
542#ifdef FEAT_ARP
543 if (ArpBase)
544 CloseLibrary((struct Library *) ArpBase);
545#endif
546 exit(exitval);
547 /* NOTREACHED */
548 return FAIL;
549}
550
551/*
552 * Return TRUE if the input comes from a terminal, FALSE otherwise.
553 * We fake there is a window, because we can always open one!
554 */
555 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100556mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558 return TRUE;
559}
560
561/*
562 * fname_case(): Set the case of the file name, if it already exists.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000563 * This will cause the file name to remain exactly the same
564 * if the file system ignores, but preserves case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 */
566/*ARGSUSED*/
567 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100568fname_case(
569 char_u *name,
570 int len) /* buffer size, ignored here */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571{
572 struct FileInfoBlock *fib;
573 size_t flen;
574
575 fib = get_fib(name);
576 if (fib != NULL)
577 {
578 flen = STRLEN(name);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000579 /* TODO: Check if this fix applies to AmigaOS < 4 too.*/
580#ifdef __amigaos4__
581 if (fib->fib_DirEntryType == ST_ROOT)
582 strcat(fib->fib_FileName, ":");
583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (flen == strlen(fib->fib_FileName)) /* safety check */
585 mch_memmove(name, fib->fib_FileName, flen);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000586 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588}
589
590/*
591 * Get the FileInfoBlock for file "fname"
592 * The returned structure has to be free()d.
593 * Returns NULL on error.
594 */
595 static struct FileInfoBlock *
Bram Moolenaar05540972016-01-30 20:31:25 +0100596get_fib(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 BPTR flock;
599 struct FileInfoBlock *fib;
600
601 if (fname == NULL) /* safety check */
602 return NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000603#ifdef __amigaos4__
604 fib = AllocDosObject(DOS_FIB,0);
605#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200606 fib = ALLOC_ONE(struct FileInfoBlock);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (fib != NULL)
609 {
610 flock = Lock((UBYTE *)fname, (long)ACCESS_READ);
611 if (flock == (BPTR)NULL || !Examine(flock, fib))
612 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000613 free_fib(fib); /* in case of an error the memory is freed here */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 fib = NULL;
615 }
616 if (flock)
617 UnLock(flock);
618 }
619 return fib;
620}
621
622#ifdef FEAT_TITLE
623/*
624 * set the title of our window
625 * icon name is not set
626 */
627 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100628mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
630 if (wb_window != NULL && title != NULL)
631 SetWindowTitles(wb_window, (UBYTE *)title, (UBYTE *)-1L);
632}
633
634/*
635 * Restore the window/icon title.
636 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200637 * SAVE_RESTORE_TITLE Just restore title
638 * SAVE_RESTORE_ICON Just restore icon (which we don't have)
639 * SAVE_RESTORE_BOTH Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 */
641 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100642mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643{
Bram Moolenaar40385db2018-08-07 22:31:44 +0200644 if (which & SAVE_RESTORE_TITLE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 mch_settitle(oldwindowtitle, NULL);
646}
647
648 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100649mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
651 return (wb_window != NULL);
652}
653
654 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100655mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
657 return FALSE;
658}
659#endif
660
Bram Moolenaardfded982019-10-26 21:33:19 +0200661 void
662mch_setmouse(int on UNUSED)
663{
664 // TODO: implement
665}
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667/*
668 * Insert user name in s[len].
669 */
670 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100671mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
Bram Moolenaar5e8e9672019-09-27 13:38:56 +0200673#if defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__)
674 struct passwd *pwd = getpwuid(getuid());
675
676 if (pwd != NULL && pwd->pw_name && len > 0)
677 {
678 vim_strncpy(s, (char_u *)pwd->pw_name, len - 1);
679 return OK;
680 }
681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 *s = NUL;
683 return FAIL;
684}
685
686/*
687 * Insert host name is s[len].
688 */
689 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100690mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000692#if defined(__amigaos4__) && defined(__CLIB2__)
693 gethostname(s, len);
694#else
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000695 vim_strncpy(s, "Amiga", len - 1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
699/*
700 * return process ID
701 */
702 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100703mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000705#ifdef __amigaos4__
706 /* This is as close to a pid as we can come. We could use CLI numbers also,
707 * but then we would have two different types of process identifiers.
708 */
709 return((long)FindTask(0));
710#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return (long)0;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713}
714
715/*
716 * Get name of current directory into buffer 'buf' of length 'len' bytes.
717 * Return OK for success, FAIL for failure.
718 */
719 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100720mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721{
722 return mch_FullName((char_u *)"", buf, len, FALSE);
723}
724
725/*
726 * get absolute file name into buffer 'buf' of length 'len' bytes
727 *
728 * return FAIL for failure, OK otherwise
729 */
730 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100731mch_FullName(
732 char_u *fname,
733 char_u *buf,
734 int len,
735 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 BPTR l;
738 int retval = FAIL;
739 int i;
740
741 /* Lock the file. If it exists, we can get the exact name. */
742 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
743 {
744 retval = lock2name(l, buf, (long)len - 1);
745 UnLock(l);
746 }
747 else if (force || !mch_isFullName(fname)) /* not a full path yet */
748 {
749 /*
750 * If the file cannot be locked (doesn't exist), try to lock the
751 * current directory and concatenate the file name.
752 */
753 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
754 {
755 retval = lock2name(l, buf, (long)len);
756 UnLock(l);
757 if (retval == OK)
758 {
759 i = STRLEN(buf);
760 /* Concatenate the fname to the directory. Don't add a slash
761 * if fname is empty, but do change "" to "/". */
762 if (i == 0 || *fname != NUL)
763 {
764 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
765 buf[i++] = '/';
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000766 vim_strncpy(buf + i, fname, len - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768 }
769 }
770 }
771 if (*buf == 0 || *buf == ':')
772 retval = FAIL; /* something failed; use the file name */
773 return retval;
774}
775
776/*
777 * Return TRUE if "fname" does not depend on the current directory.
778 */
779 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100780mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 return (vim_strchr(fname, ':') != NULL && *fname != ':');
783}
784
785/*
786 * Get the full file name from a lock. Use 2.0 function if possible, because
787 * the arp function has more restrictions on the path length.
788 *
789 * return FAIL for failure, OK otherwise
790 */
791 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100792lock2name(BPTR lock, char_u *buf, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794#ifdef FEAT_ARP
795 if (dos2) /* use 2.0 function */
796#endif
797 return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL);
798#ifdef FEAT_ARP
799 else /* use arp function */
800 return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL);
801#endif
802}
803
804/*
805 * get file permissions for 'name'
806 * Returns -1 when it doesn't exist.
807 */
808 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100809mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 struct FileInfoBlock *fib;
812 long retval = -1;
813
814 fib = get_fib(name);
815 if (fib != NULL)
816 {
817 retval = fib->fib_Protection;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000818 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 }
820 return retval;
821}
822
823/*
824 * set file permission for 'name' to 'perm'
825 *
826 * return FAIL for failure, OK otherwise
827 */
828 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100829mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830{
831 perm &= ~FIBF_ARCHIVE; /* reset archived bit */
832 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
833}
834
835/*
836 * Set hidden flag for "name".
837 */
838 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100839mch_hide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
841 /* can't hide a file */
842}
843
844/*
845 * return FALSE if "name" is not a directory
846 * return TRUE if "name" is a directory.
847 * return FALSE for error.
848 */
849 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100850mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
852 struct FileInfoBlock *fib;
853 int retval = FALSE;
854
855 fib = get_fib(name);
856 if (fib != NULL)
857 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000858#ifdef __amigaos4__
859 retval = (FIB_IS_DRAWER(fib)) ? TRUE : FALSE;
860#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000862#endif
863 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865 return retval;
866}
867
868/*
869 * Create directory "name".
870 */
Bram Moolenaare3853642006-09-14 19:36:57 +0000871 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100872mch_mkdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
874 BPTR lock;
875
876 lock = CreateDir(name);
877 if (lock != NULL)
Bram Moolenaare3853642006-09-14 19:36:57 +0000878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 UnLock(lock);
Bram Moolenaare3853642006-09-14 19:36:57 +0000880 return 0;
881 }
882 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883}
884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885/*
886 * Return 1 if "name" can be executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +0100887 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * Return -1 if unknown.
889 */
890 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100891mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 /* TODO */
894 return -1;
895}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897/*
898 * Check what "name" is:
899 * NODE_NORMAL: file or directory (or doesn't exist)
900 * NODE_WRITABLE: writable device, socket, fifo, etc.
901 * NODE_OTHER: non-writable things
902 */
903 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100904mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
906 /* TODO */
907 return NODE_NORMAL;
908}
909
910 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100911mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
913}
914
915/*
916 * Careful: mch_exit() may be called before mch_init()!
917 */
918 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100919mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100921 exiting = TRUE;
922
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (raw_in) /* put terminal in 'normal' mode */
924 {
925 settmode(TMODE_COOK);
926 stoptermcap();
927 }
928 out_char('\n');
929 if (raw_out)
930 {
931 if (term_console)
932 {
933 win_resize_off(); /* window resize events de-activated */
934 if (size_set)
935 OUT_STR("\233t\233u"); /* reset window size (CSI t CSI u) */
936 }
937 out_flush();
938 }
939
940#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +0200941 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window title */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942#endif
943
944 ml_close_all(TRUE); /* remove all memfiles */
945
946#ifdef FEAT_ARP
947 if (ArpBase)
948 CloseLibrary((struct Library *) ArpBase);
949#endif
950 if (close_win)
951 Close(raw_in);
952 if (r)
953 printf(_("Vim exiting with %d\n"), r); /* somehow this makes :cq work!? */
954 exit(r);
955}
956
957/*
958 * This is a routine for setting a given stream to raw or cooked mode on the
959 * Amiga . This is useful when you are using Lattice C to produce programs
960 * that want to read single characters with the "getch()" or "fgetc" call.
961 *
962 * Written : 18-Jun-87 By Chuck McManis.
963 */
964
965#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
966
967/*
968 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
969 * 'cooked' mode. This only works on TTY's.
970 *
971 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
972 * getch() will return immediately rather than wait for a return. You
973 * lose editing features though.
974 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100975 * Cooked: This function returns the designate file pointer to its normal,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * wait for a <CR> mode. This is exactly like raw() except that
977 * it sends a 0 to the console to make it back into a CON: from a RAW:
978 */
979 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100980mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000982#if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
984#else
985 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
986 tmode == TMODE_RAW ? -1L : 0L) == 0)
987#endif
988 mch_errmsg(_("cannot change console mode ?!\n"));
989}
990
991/*
992 * set screen mode, always fails.
993 */
994 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100995mch_screenmode(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100997 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 return FAIL;
999}
1000
1001/*
1002 * Code for this routine came from the following :
1003 *
1004 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
1005 * DOS packet example
1006 * Requires 1.2
1007 *
1008 * Found on Fish Disk 56.
1009 *
1010 * Heavely modified by mool.
1011 */
1012
Bram Moolenaar82881492012-11-20 16:53:39 +01001013#ifndef PROTO
1014# include <devices/conunit.h>
1015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017/*
1018 * try to get the real window size
1019 * return FAIL for failure, OK otherwise
1020 */
1021 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001022mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
1024 struct ConUnit *conUnit;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001025#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 char id_a[sizeof(struct InfoData) + 3];
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001027#endif
1028 struct InfoData *id=0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 if (!term_console) /* not an amiga window */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001031 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 /* insure longword alignment */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001034#ifdef __amigaos4__
Bram Moolenaar62dbdc42011-10-20 18:24:22 +02001035 if (!(id = AllocDosObject(DOS_INFODATA, 0)))
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001036 goto out;
1037#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
1041 /*
1042 * Should make console aware of real window size, not the one we set.
1043 * Unfortunately, under DOS 2.0x this redraws the window and it
1044 * is rarely needed, so we skip it now, unless we changed the size.
1045 */
1046 if (size_set)
1047 OUT_STR("\233t\233u"); /* CSI t CSI u */
1048 out_flush();
1049
1050#ifdef __AROS__
1051 if (!Info(raw_out, id)
1052 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
1053#else
1054 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
1055 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
1056#endif
1057 {
1058 /* it's not an amiga window, maybe aux device */
1059 /* terminal type should be set */
1060 term_console = FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001061 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
1063 if (oldwindowtitle == NULL)
1064 oldwindowtitle = (char_u *)wb_window->Title;
1065 if (id->id_InUse == (BPTR)NULL)
1066 {
1067 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
1068 return FAIL;
1069 }
1070 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
1071
1072 /* get window size */
1073 Rows = conUnit->cu_YMax + 1;
1074 Columns = conUnit->cu_XMax + 1;
1075 if (Rows < 0 || Rows > 200) /* cannot be an amiga window */
1076 {
1077 Columns = 80;
1078 Rows = 24;
1079 term_console = FALSE;
1080 return FAIL;
1081 }
1082
1083 return OK;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001084out:
1085#ifdef __amigaos4__
1086 FreeDosObject(DOS_INFODATA, id); /* Safe to pass NULL */
1087#endif
1088
1089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090}
1091
1092/*
1093 * Try to set the real window size to Rows and Columns.
1094 */
1095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001096mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
1098 if (term_console)
1099 {
1100 size_set = TRUE;
1101 out_char(CSI);
1102 out_num((long)Rows);
1103 out_char('t');
1104 out_char(CSI);
1105 out_num((long)Columns);
1106 out_char('u');
1107 out_flush();
1108 }
1109}
1110
1111/*
1112 * Rows and/or Columns has changed.
1113 */
1114 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001115mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 /* Nothing to do. */
1118}
1119
1120/*
1121 * out_num - output a (big) number fast
1122 */
1123 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001124out_num(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125{
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
Bram Moolenaar05540972016-01-30 20:31:25 +01001154dos_packet(
1155 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) */
Bram Moolenaard90b6c02016-08-28 18:10:45 +02001157 long arg) /* single argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
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
Bram Moolenaar05540972016-01-30 20:31:25 +01001206mch_call_shell(
1207 char_u *cmd,
1208 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209{
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 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_("E360: Cannot execute shell with -f option"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 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 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001264 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (cmd == NULL)
1266 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001267 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 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);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001287 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
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 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001354 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (use_execute)
1356 {
1357 if (cmd == NULL)
1358 msg_outtrans(p_sh);
1359 else
1360 msg_outtrans(cmd);
1361 }
1362 else
1363 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001364 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 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);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001389 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
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
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02001415mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
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
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01001421/* this routine causes manx to use this Chk_Abort() rather than its own */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422/* 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
Bram Moolenaar05540972016-01-30 20:31:25 +01001464mch_expandpath(
1465 garray_T *gap,
1466 char_u *pat,
1467 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
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 Moolenaarc799fe22019-05-28 23:08:19 +02001488 Anchor = alloc_clear(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 '#?' */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001507 starbuf = alloc(2 * STRLEN(pat) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 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)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001551 emsg(_("ANCHOR_BUF_SIZE too small."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1553 && Result != ERROR_DEVICE_NOT_MOUNTED
1554 && Result != ERROR_NO_MORE_ENTRIES)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001555 emsg(_("I/O ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
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
Bram Moolenaar05540972016-01-30 20:31:25 +01001585sortcmp(const void *a, const void *b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 char *s = *(char **)a;
1588 char *t = *(char **)b;
1589
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001590 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591}
1592
1593/*
1594 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1595 */
1596 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001597mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001599 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
1601 if (*p == '\\' && p[1] != NUL)
1602 ++p;
1603 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1604 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 return FALSE;
1607}
1608
1609 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001610mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001612 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
1614 if (*p == '\\' && p[1] != NUL)
1615 ++p;
1616 else
1617 if (vim_strchr((char_u *)
1618# ifdef VIM_BACKTICK
1619 "*?[(#$`"
1620# else
1621 "*?[(#$"
1622# endif
1623 , *p) != NULL
1624 || (*p == '~' && p[1] != NUL))
1625 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
1627 return FALSE;
1628}
1629
1630/*
1631 * With AmigaDOS 2.0 support for reading local environment variables
1632 *
1633 * Two buffers are allocated:
1634 * - A big one to do the expansion into. It is freed before returning.
1635 * - A small one to hold the return value. It is kept until the next call.
1636 */
1637 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001638mch_getenv(char_u *var)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
1640 int len;
1641 UBYTE *buf; /* buffer to expand in */
1642 char_u *retval; /* return value */
1643 static char_u *alloced = NULL; /* allocated memory */
1644
1645#ifdef FEAT_ARP
1646 if (!dos2)
1647 retval = (char_u *)getenv((char *)var);
1648 else
1649#endif
1650 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001651 VIM_CLEAR(alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 retval = NULL;
1653
1654 buf = alloc(IOSIZE);
1655 if (buf == NULL)
1656 return NULL;
1657
1658 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1659 if (len >= 0)
1660 {
1661 retval = vim_strsave((char_u *)buf);
1662 alloced = retval;
1663 }
1664
1665 vim_free(buf);
1666 }
1667
1668 /* if $VIM is not defined, use "vim:" instead */
1669 if (retval == NULL && STRCMP(var, "VIM") == 0)
1670 retval = (char_u *)"vim:";
1671
1672 return retval;
1673}
1674
1675/*
1676 * Amiga version of setenv() with AmigaDOS 2.0 support.
1677 */
1678/* ARGSUSED */
1679 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001680mch_setenv(char *var, char *value, int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682#ifdef FEAT_ARP
1683 if (!dos2)
1684 return setenv(var, value);
1685#endif
1686
1687 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
1688 return 0; /* success */
1689 return -1; /* failure */
1690}