blob: aff24c5674ee97dce9a4ff3ff09b6dbca10fca7e [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
661/*
662 * Insert user name in s[len].
663 */
664 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100665mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
Bram Moolenaar5e8e9672019-09-27 13:38:56 +0200667#if defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__)
668 struct passwd *pwd = getpwuid(getuid());
669
670 if (pwd != NULL && pwd->pw_name && len > 0)
671 {
672 vim_strncpy(s, (char_u *)pwd->pw_name, len - 1);
673 return OK;
674 }
675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 *s = NUL;
677 return FAIL;
678}
679
680/*
681 * Insert host name is s[len].
682 */
683 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100684mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000686#if defined(__amigaos4__) && defined(__CLIB2__)
687 gethostname(s, len);
688#else
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000689 vim_strncpy(s, "Amiga", len - 1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691}
692
693/*
694 * return process ID
695 */
696 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100697mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000699#ifdef __amigaos4__
700 /* This is as close to a pid as we can come. We could use CLI numbers also,
701 * but then we would have two different types of process identifiers.
702 */
703 return((long)FindTask(0));
704#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 return (long)0;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707}
708
709/*
710 * Get name of current directory into buffer 'buf' of length 'len' bytes.
711 * Return OK for success, FAIL for failure.
712 */
713 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100714mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
716 return mch_FullName((char_u *)"", buf, len, FALSE);
717}
718
719/*
720 * get absolute file name into buffer 'buf' of length 'len' bytes
721 *
722 * return FAIL for failure, OK otherwise
723 */
724 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100725mch_FullName(
726 char_u *fname,
727 char_u *buf,
728 int len,
729 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730{
731 BPTR l;
732 int retval = FAIL;
733 int i;
734
735 /* Lock the file. If it exists, we can get the exact name. */
736 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
737 {
738 retval = lock2name(l, buf, (long)len - 1);
739 UnLock(l);
740 }
741 else if (force || !mch_isFullName(fname)) /* not a full path yet */
742 {
743 /*
744 * If the file cannot be locked (doesn't exist), try to lock the
745 * current directory and concatenate the file name.
746 */
747 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
748 {
749 retval = lock2name(l, buf, (long)len);
750 UnLock(l);
751 if (retval == OK)
752 {
753 i = STRLEN(buf);
754 /* Concatenate the fname to the directory. Don't add a slash
755 * if fname is empty, but do change "" to "/". */
756 if (i == 0 || *fname != NUL)
757 {
758 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
759 buf[i++] = '/';
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000760 vim_strncpy(buf + i, fname, len - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762 }
763 }
764 }
765 if (*buf == 0 || *buf == ':')
766 retval = FAIL; /* something failed; use the file name */
767 return retval;
768}
769
770/*
771 * Return TRUE if "fname" does not depend on the current directory.
772 */
773 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100774mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
776 return (vim_strchr(fname, ':') != NULL && *fname != ':');
777}
778
779/*
780 * Get the full file name from a lock. Use 2.0 function if possible, because
781 * the arp function has more restrictions on the path length.
782 *
783 * return FAIL for failure, OK otherwise
784 */
785 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100786lock2name(BPTR lock, char_u *buf, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
788#ifdef FEAT_ARP
789 if (dos2) /* use 2.0 function */
790#endif
791 return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL);
792#ifdef FEAT_ARP
793 else /* use arp function */
794 return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL);
795#endif
796}
797
798/*
799 * get file permissions for 'name'
800 * Returns -1 when it doesn't exist.
801 */
802 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100803mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 struct FileInfoBlock *fib;
806 long retval = -1;
807
808 fib = get_fib(name);
809 if (fib != NULL)
810 {
811 retval = fib->fib_Protection;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000812 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 }
814 return retval;
815}
816
817/*
818 * set file permission for 'name' to 'perm'
819 *
820 * return FAIL for failure, OK otherwise
821 */
822 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100823mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 perm &= ~FIBF_ARCHIVE; /* reset archived bit */
826 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
827}
828
829/*
830 * Set hidden flag for "name".
831 */
832 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100833mch_hide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 /* can't hide a file */
836}
837
838/*
839 * return FALSE if "name" is not a directory
840 * return TRUE if "name" is a directory.
841 * return FALSE for error.
842 */
843 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100844mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845{
846 struct FileInfoBlock *fib;
847 int retval = FALSE;
848
849 fib = get_fib(name);
850 if (fib != NULL)
851 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000852#ifdef __amigaos4__
853 retval = (FIB_IS_DRAWER(fib)) ? TRUE : FALSE;
854#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000856#endif
857 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 return retval;
860}
861
862/*
863 * Create directory "name".
864 */
Bram Moolenaare3853642006-09-14 19:36:57 +0000865 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100866mch_mkdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
868 BPTR lock;
869
870 lock = CreateDir(name);
871 if (lock != NULL)
Bram Moolenaare3853642006-09-14 19:36:57 +0000872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 UnLock(lock);
Bram Moolenaare3853642006-09-14 19:36:57 +0000874 return 0;
875 }
876 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877}
878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879/*
880 * Return 1 if "name" can be executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +0100881 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 * Return -1 if unknown.
883 */
884 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100885mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886{
887 /* TODO */
888 return -1;
889}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891/*
892 * Check what "name" is:
893 * NODE_NORMAL: file or directory (or doesn't exist)
894 * NODE_WRITABLE: writable device, socket, fifo, etc.
895 * NODE_OTHER: non-writable things
896 */
897 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100898mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
900 /* TODO */
901 return NODE_NORMAL;
902}
903
904 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100905mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
907}
908
909/*
910 * Careful: mch_exit() may be called before mch_init()!
911 */
912 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100913mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100915 exiting = TRUE;
916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (raw_in) /* put terminal in 'normal' mode */
918 {
919 settmode(TMODE_COOK);
920 stoptermcap();
921 }
922 out_char('\n');
923 if (raw_out)
924 {
925 if (term_console)
926 {
927 win_resize_off(); /* window resize events de-activated */
928 if (size_set)
929 OUT_STR("\233t\233u"); /* reset window size (CSI t CSI u) */
930 }
931 out_flush();
932 }
933
934#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +0200935 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window title */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936#endif
937
938 ml_close_all(TRUE); /* remove all memfiles */
939
940#ifdef FEAT_ARP
941 if (ArpBase)
942 CloseLibrary((struct Library *) ArpBase);
943#endif
944 if (close_win)
945 Close(raw_in);
946 if (r)
947 printf(_("Vim exiting with %d\n"), r); /* somehow this makes :cq work!? */
948 exit(r);
949}
950
951/*
952 * This is a routine for setting a given stream to raw or cooked mode on the
953 * Amiga . This is useful when you are using Lattice C to produce programs
954 * that want to read single characters with the "getch()" or "fgetc" call.
955 *
956 * Written : 18-Jun-87 By Chuck McManis.
957 */
958
959#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
960
961/*
962 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
963 * 'cooked' mode. This only works on TTY's.
964 *
965 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
966 * getch() will return immediately rather than wait for a return. You
967 * lose editing features though.
968 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100969 * Cooked: This function returns the designate file pointer to its normal,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 * wait for a <CR> mode. This is exactly like raw() except that
971 * it sends a 0 to the console to make it back into a CON: from a RAW:
972 */
973 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100974mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000976#if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
978#else
979 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
980 tmode == TMODE_RAW ? -1L : 0L) == 0)
981#endif
982 mch_errmsg(_("cannot change console mode ?!\n"));
983}
984
985/*
986 * set screen mode, always fails.
987 */
988 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100989mch_screenmode(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100991 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 return FAIL;
993}
994
995/*
996 * Code for this routine came from the following :
997 *
998 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
999 * DOS packet example
1000 * Requires 1.2
1001 *
1002 * Found on Fish Disk 56.
1003 *
1004 * Heavely modified by mool.
1005 */
1006
Bram Moolenaar82881492012-11-20 16:53:39 +01001007#ifndef PROTO
1008# include <devices/conunit.h>
1009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011/*
1012 * try to get the real window size
1013 * return FAIL for failure, OK otherwise
1014 */
1015 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001016mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017{
1018 struct ConUnit *conUnit;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001019#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 char id_a[sizeof(struct InfoData) + 3];
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001021#endif
1022 struct InfoData *id=0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 if (!term_console) /* not an amiga window */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001025 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 /* insure longword alignment */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001028#ifdef __amigaos4__
Bram Moolenaar62dbdc42011-10-20 18:24:22 +02001029 if (!(id = AllocDosObject(DOS_INFODATA, 0)))
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001030 goto out;
1031#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 /*
1036 * Should make console aware of real window size, not the one we set.
1037 * Unfortunately, under DOS 2.0x this redraws the window and it
1038 * is rarely needed, so we skip it now, unless we changed the size.
1039 */
1040 if (size_set)
1041 OUT_STR("\233t\233u"); /* CSI t CSI u */
1042 out_flush();
1043
1044#ifdef __AROS__
1045 if (!Info(raw_out, id)
1046 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
1047#else
1048 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
1049 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
1050#endif
1051 {
1052 /* it's not an amiga window, maybe aux device */
1053 /* terminal type should be set */
1054 term_console = FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001055 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057 if (oldwindowtitle == NULL)
1058 oldwindowtitle = (char_u *)wb_window->Title;
1059 if (id->id_InUse == (BPTR)NULL)
1060 {
1061 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
1062 return FAIL;
1063 }
1064 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
1065
1066 /* get window size */
1067 Rows = conUnit->cu_YMax + 1;
1068 Columns = conUnit->cu_XMax + 1;
1069 if (Rows < 0 || Rows > 200) /* cannot be an amiga window */
1070 {
1071 Columns = 80;
1072 Rows = 24;
1073 term_console = FALSE;
1074 return FAIL;
1075 }
1076
1077 return OK;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001078out:
1079#ifdef __amigaos4__
1080 FreeDosObject(DOS_INFODATA, id); /* Safe to pass NULL */
1081#endif
1082
1083 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084}
1085
1086/*
1087 * Try to set the real window size to Rows and Columns.
1088 */
1089 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001090mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 if (term_console)
1093 {
1094 size_set = TRUE;
1095 out_char(CSI);
1096 out_num((long)Rows);
1097 out_char('t');
1098 out_char(CSI);
1099 out_num((long)Columns);
1100 out_char('u');
1101 out_flush();
1102 }
1103}
1104
1105/*
1106 * Rows and/or Columns has changed.
1107 */
1108 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001109mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 /* Nothing to do. */
1112}
1113
1114/*
1115 * out_num - output a (big) number fast
1116 */
1117 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001118out_num(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
1120 OUT_STR_NF(tltoa((unsigned long)n));
1121}
1122
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001123#if !defined(AZTEC_C) && !defined(__AROS__) && !defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124/*
1125 * Sendpacket.c
1126 *
1127 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1128 * the given message port. This makes working around DOS lots easier.
1129 *
1130 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1131 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1132 * say 'oml lib:amiga.lib -r sendpacket.o'
1133 */
1134
Bram Moolenaar82881492012-11-20 16:53:39 +01001135#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136/* #include <proto/exec.h> */
1137/* #include <proto/dos.h> */
Bram Moolenaar82881492012-11-20 16:53:39 +01001138# include <exec/memory.h>
1139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
1141/*
1142 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1143 * Finkel. This function will send a packet of the given type to the Message
1144 * Port supplied.
1145 */
1146
1147 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01001148dos_packet(
1149 struct MsgPort *pid, /* process identifier ... (handlers message port) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 long action, /* packet type ... (what you want handler to do) */
Bram Moolenaard90b6c02016-08-28 18:10:45 +02001151 long arg) /* single argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152{
1153# ifdef FEAT_ARP
1154 struct MsgPort *replyport;
1155 struct StandardPacket *packet;
1156 long res1;
1157
1158 if (dos2)
1159# endif
1160 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); /* use 2.0 function */
1161# ifdef FEAT_ARP
1162
1163 replyport = (struct MsgPort *) CreatePort(NULL, 0); /* use arp function */
1164 if (!replyport)
1165 return (0);
1166
1167 /* Allocate space for a packet, make it public and clear it */
1168 packet = (struct StandardPacket *)
1169 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
1170 if (!packet) {
1171 DeletePort(replyport);
1172 return (0);
1173 }
1174 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1175 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1176 packet->sp_Pkt.dp_Port = replyport;
1177 packet->sp_Pkt.dp_Type = action;
1178 packet->sp_Pkt.dp_Arg1 = arg;
1179
1180 PutMsg(pid, (struct Message *)packet); /* send packet */
1181
1182 WaitPort(replyport);
1183 GetMsg(replyport);
1184
1185 res1 = packet->sp_Pkt.dp_Res1;
1186
1187 FreeMem(packet, (long) sizeof(struct StandardPacket));
1188 DeletePort(replyport);
1189
1190 return (res1);
1191# endif
1192}
1193#endif /* !defined(AZTEC_C) && !defined(__AROS__) */
1194
1195/*
1196 * Call shell.
1197 * Return error number for failure, 0 otherwise
1198 */
1199 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001200mch_call_shell(
1201 char_u *cmd,
1202 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203{
1204 BPTR mydir;
1205 int x;
1206 int tmode = cur_tmode;
1207#ifdef AZTEC_C
1208 int use_execute;
1209 char_u *shellcmd = NULL;
1210 char_u *shellarg;
1211#endif
1212 int retval = 0;
1213
1214 if (close_win)
1215 {
1216 /* if Vim opened a window: Executing a shell may cause crashes */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 emsg(_("E360: Cannot execute shell with -f option"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 return -1;
1219 }
1220
1221 if (term_console)
1222 win_resize_off(); /* window resize events de-activated */
1223 out_flush();
1224
1225 if (options & SHELL_COOKED)
1226 settmode(TMODE_COOK); /* set to normal mode */
1227 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); /* remember current dir */
1228
1229#if !defined(AZTEC_C) /* not tested very much */
1230 if (cmd == NULL)
1231 {
1232# ifdef FEAT_ARP
1233 if (dos2)
1234# endif
1235 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1236# ifdef FEAT_ARP
1237 else
1238 x = Execute(p_sh, raw_in, raw_out);
1239# endif
1240 }
1241 else
1242 {
1243# ifdef FEAT_ARP
1244 if (dos2)
1245# endif
1246 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1247# ifdef FEAT_ARP
1248 else
1249 x = Execute((char *)cmd, 0L, raw_out);
1250# endif
1251 }
1252# ifdef FEAT_ARP
1253 if ((dos2 && x < 0) || (!dos2 && !x))
1254# else
1255 if (x < 0)
1256# endif
1257 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001258 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (cmd == NULL)
1260 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001261 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 msg_outtrans(p_sh);
1263 }
1264 else
1265 msg_outtrans(cmd);
1266 msg_putchar('\n');
1267 retval = -1;
1268 }
1269# ifdef FEAT_ARP
1270 else if (!dos2 || x)
1271# else
1272 else if (x)
1273# endif
1274 {
1275 if ((x = IoErr()) != 0)
1276 {
1277 if (!(options & SHELL_SILENT))
1278 {
1279 msg_putchar('\n');
1280 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001281 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 retval = x;
1284 }
1285 }
1286#else /* else part is for AZTEC_C */
1287 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1288 use_execute = 1;
1289 else
1290 use_execute = 0;
1291 if (!use_execute)
1292 {
1293 /*
1294 * separate shell name from argument
1295 */
1296 shellcmd = vim_strsave(p_sh);
1297 if (shellcmd == NULL) /* out of memory, use Execute */
1298 use_execute = 1;
1299 else
1300 {
1301 shellarg = skiptowhite(shellcmd); /* find start of arguments */
1302 if (*shellarg != NUL)
1303 {
1304 *shellarg++ = NUL;
1305 shellarg = skipwhite(shellarg);
1306 }
1307 }
1308 }
1309 if (cmd == NULL)
1310 {
1311 if (use_execute)
1312 {
1313# ifdef FEAT_ARP
1314 if (dos2)
1315# endif
1316 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1317# ifdef FEAT_ARP
1318 else
1319 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1320# endif
1321 }
1322 else
1323 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1324 }
1325 else if (use_execute)
1326 {
1327# ifdef FEAT_ARP
1328 if (dos2)
1329# endif
1330 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1331# ifdef FEAT_ARP
1332 else
1333 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1334# endif
1335 }
1336 else if (p_st & 1)
1337 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1338 (char *)cmd, NULL);
1339 else
1340 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1341 (char *)p_shcf, (char *)cmd, NULL);
1342# ifdef FEAT_ARP
1343 if ((dos2 && x < 0) || (!dos2 && x))
1344# else
1345 if (x < 0)
1346# endif
1347 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001348 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (use_execute)
1350 {
1351 if (cmd == NULL)
1352 msg_outtrans(p_sh);
1353 else
1354 msg_outtrans(cmd);
1355 }
1356 else
1357 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001358 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 msg_outtrans(shellcmd);
1360 }
1361 msg_putchar('\n');
1362 retval = -1;
1363 }
1364 else
1365 {
1366 if (use_execute)
1367 {
1368# ifdef FEAT_ARP
1369 if (!dos2 || x)
1370# else
1371 if (x)
1372# endif
1373 x = IoErr();
1374 }
1375 else
1376 x = wait();
1377 if (x)
1378 {
1379 if (!(options & SHELL_SILENT) && !emsg_silent)
1380 {
1381 msg_putchar('\n');
1382 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001383 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385 retval = x;
1386 }
1387 }
1388 vim_free(shellcmd);
1389#endif /* AZTEC_C */
1390
1391 if ((mydir = CurrentDir(mydir)) != 0) /* make sure we stay in the same directory */
1392 UnLock(mydir);
1393 if (tmode == TMODE_RAW)
1394 settmode(TMODE_RAW); /* set to raw mode */
1395#ifdef FEAT_TITLE
1396 resettitle();
1397#endif
1398 if (term_console)
1399 win_resize_on(); /* window resize events activated */
1400 return retval;
1401}
1402
1403/*
1404 * check for an "interrupt signal"
1405 * We only react to a CTRL-C, but also clear the other break signals to avoid
1406 * trouble with lattice-c programs.
1407 */
1408 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02001409mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1412 got_int = TRUE;
1413}
1414
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01001415/* this routine causes manx to use this Chk_Abort() rather than its own */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416/* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
1417/* is zero). Since we want to check for our own ^C's */
1418
1419#ifdef _DCC
1420#define Chk_Abort chkabort
1421#endif
1422
1423#ifdef LATTICE
1424void __regargs __chkabort(void);
1425
1426void __regargs __chkabort(void)
1427{}
1428
1429#else
1430 long
1431Chk_Abort(void)
1432{
1433 return(0L);
1434}
1435#endif
1436
1437/*
1438 * mch_expandpath() - this code does wild-card pattern matching using the arp
1439 * routines.
1440 *
1441 * "pat" has backslashes before chars that are not to be expanded.
1442 * Returns the number of matches found.
1443 *
1444 * This is based on WildDemo2.c (found in arp1.1 distribution).
1445 * That code's copyright follows:
1446 * Copyright (c) 1987, Scott Ballantyne
1447 * Use and abuse as you please.
1448 */
1449
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001450#ifdef __amigaos4__
1451# define ANCHOR_BUF_SIZE 1024
1452#else
1453# define ANCHOR_BUF_SIZE (512)
1454# define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001458mch_expandpath(
1459 garray_T *gap,
1460 char_u *pat,
1461 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 struct AnchorPath *Anchor;
1464 LONG Result;
1465 char_u *starbuf, *sp, *dp;
1466 int start_len;
1467 int matches;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001468#ifdef __amigaos4__
1469 struct TagItem AnchorTags[] = {
1470 {ADO_Strlen, ANCHOR_BUF_SIZE},
1471 {ADO_Flags, APF_DODOT|APF_DOWILD|APF_MultiAssigns},
1472 {TAG_DONE, 0L}
1473 };
1474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476 start_len = gap->ga_len;
1477
1478 /* Get our AnchorBase */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001479#ifdef __amigaos4__
1480 Anchor = AllocDosObject(DOS_ANCHORPATH, AnchorTags);
1481#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001482 Anchor = alloc_clear(ANCHOR_SIZE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (Anchor == NULL)
1485 return 0;
1486
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001487#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; /* ap_Length not supported anymore */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001489# ifdef APF_DODOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001491# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 Anchor->ap_Flags = APF_DoDot | APF_DoWild; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494#endif
1495
1496#ifdef FEAT_ARP
1497 if (dos2)
1498 {
1499#endif
1500 /* hack to replace '*' by '#?' */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001501 starbuf = alloc(2 * STRLEN(pat) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 if (starbuf == NULL)
1503 goto Return;
1504 for (sp = pat, dp = starbuf; *sp; ++sp)
1505 {
1506 if (*sp == '*')
1507 {
1508 *dp++ = '#';
1509 *dp++ = '?';
1510 }
1511 else
1512 *dp++ = *sp;
1513 }
1514 *dp = NUL;
1515 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1516 vim_free(starbuf);
1517#ifdef FEAT_ARP
1518 }
1519 else
1520 Result = FindFirst((char *)pat, Anchor);
1521#endif
1522
1523 /*
1524 * Loop to get all matches.
1525 */
1526 while (Result == 0)
1527 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001528#ifdef __amigaos4__
1529 addfile(gap, (char_u *)Anchor->ap_Buffer, flags);
1530#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#ifdef FEAT_ARP
1534 if (dos2)
1535#endif
1536 Result = MatchNext(Anchor);
1537#ifdef FEAT_ARP
1538 else
1539 Result = FindNext(Anchor);
1540#endif
1541 }
1542 matches = gap->ga_len - start_len;
1543
1544 if (Result == ERROR_BUFFER_OVERFLOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001545 emsg(_("ANCHOR_BUF_SIZE too small."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1547 && Result != ERROR_DEVICE_NOT_MOUNTED
1548 && Result != ERROR_NO_MORE_ENTRIES)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001549 emsg(_("I/O ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
1551 /*
1552 * Sort the files for this pattern.
1553 */
1554 if (matches)
1555 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1556 (size_t)matches, sizeof(char_u *), sortcmp);
1557
1558 /* Free the wildcard stuff */
1559#ifdef FEAT_ARP
1560 if (dos2)
1561#endif
1562 MatchEnd(Anchor);
1563#ifdef FEAT_ARP
1564 else
1565 FreeAnchorChain(Anchor);
1566#endif
1567
1568Return:
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001569#ifdef __amigaos4__
1570 FreeDosObject(DOS_ANCHORPATH, Anchor);
1571#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 vim_free(Anchor);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 return matches;
1576}
1577
1578 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001579sortcmp(const void *a, const void *b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 char *s = *(char **)a;
1582 char *t = *(char **)b;
1583
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001584 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585}
1586
1587/*
1588 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1589 */
1590 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001591mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001593 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 if (*p == '\\' && p[1] != NUL)
1596 ++p;
1597 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1598 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 return FALSE;
1601}
1602
1603 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001604mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001606 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 if (*p == '\\' && p[1] != NUL)
1609 ++p;
1610 else
1611 if (vim_strchr((char_u *)
1612# ifdef VIM_BACKTICK
1613 "*?[(#$`"
1614# else
1615 "*?[(#$"
1616# endif
1617 , *p) != NULL
1618 || (*p == '~' && p[1] != NUL))
1619 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 return FALSE;
1622}
1623
1624/*
1625 * With AmigaDOS 2.0 support for reading local environment variables
1626 *
1627 * Two buffers are allocated:
1628 * - A big one to do the expansion into. It is freed before returning.
1629 * - A small one to hold the return value. It is kept until the next call.
1630 */
1631 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001632mch_getenv(char_u *var)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 int len;
1635 UBYTE *buf; /* buffer to expand in */
1636 char_u *retval; /* return value */
1637 static char_u *alloced = NULL; /* allocated memory */
1638
1639#ifdef FEAT_ARP
1640 if (!dos2)
1641 retval = (char_u *)getenv((char *)var);
1642 else
1643#endif
1644 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001645 VIM_CLEAR(alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 retval = NULL;
1647
1648 buf = alloc(IOSIZE);
1649 if (buf == NULL)
1650 return NULL;
1651
1652 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1653 if (len >= 0)
1654 {
1655 retval = vim_strsave((char_u *)buf);
1656 alloced = retval;
1657 }
1658
1659 vim_free(buf);
1660 }
1661
1662 /* if $VIM is not defined, use "vim:" instead */
1663 if (retval == NULL && STRCMP(var, "VIM") == 0)
1664 retval = (char_u *)"vim:";
1665
1666 return retval;
1667}
1668
1669/*
1670 * Amiga version of setenv() with AmigaDOS 2.0 support.
1671 */
1672/* ARGSUSED */
1673 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001674mch_setenv(char *var, char *value, int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676#ifdef FEAT_ARP
1677 if (!dos2)
1678 return setenv(var, value);
1679#endif
1680
1681 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
1682 return 0; /* success */
1683 return -1; /* failure */
1684}