blob: dfc805374ff5271f828b3e4d3b4bd0f9b162fd61 [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 Moolenaar5a6404c2006-11-01 17:12:57 +0000667 /* TODO: Implement this. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 *s = NUL;
669 return FAIL;
670}
671
672/*
673 * Insert host name is s[len].
674 */
675 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100676mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000678#if defined(__amigaos4__) && defined(__CLIB2__)
679 gethostname(s, len);
680#else
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000681 vim_strncpy(s, "Amiga", len - 1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683}
684
685/*
686 * return process ID
687 */
688 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100689mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000691#ifdef __amigaos4__
692 /* This is as close to a pid as we can come. We could use CLI numbers also,
693 * but then we would have two different types of process identifiers.
694 */
695 return((long)FindTask(0));
696#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 return (long)0;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699}
700
701/*
702 * Get name of current directory into buffer 'buf' of length 'len' bytes.
703 * Return OK for success, FAIL for failure.
704 */
705 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100706mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707{
708 return mch_FullName((char_u *)"", buf, len, FALSE);
709}
710
711/*
712 * get absolute file name into buffer 'buf' of length 'len' bytes
713 *
714 * return FAIL for failure, OK otherwise
715 */
716 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100717mch_FullName(
718 char_u *fname,
719 char_u *buf,
720 int len,
721 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 BPTR l;
724 int retval = FAIL;
725 int i;
726
727 /* Lock the file. If it exists, we can get the exact name. */
728 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
729 {
730 retval = lock2name(l, buf, (long)len - 1);
731 UnLock(l);
732 }
733 else if (force || !mch_isFullName(fname)) /* not a full path yet */
734 {
735 /*
736 * If the file cannot be locked (doesn't exist), try to lock the
737 * current directory and concatenate the file name.
738 */
739 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
740 {
741 retval = lock2name(l, buf, (long)len);
742 UnLock(l);
743 if (retval == OK)
744 {
745 i = STRLEN(buf);
746 /* Concatenate the fname to the directory. Don't add a slash
747 * if fname is empty, but do change "" to "/". */
748 if (i == 0 || *fname != NUL)
749 {
750 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
751 buf[i++] = '/';
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000752 vim_strncpy(buf + i, fname, len - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754 }
755 }
756 }
757 if (*buf == 0 || *buf == ':')
758 retval = FAIL; /* something failed; use the file name */
759 return retval;
760}
761
762/*
763 * Return TRUE if "fname" does not depend on the current directory.
764 */
765 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100766mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768 return (vim_strchr(fname, ':') != NULL && *fname != ':');
769}
770
771/*
772 * Get the full file name from a lock. Use 2.0 function if possible, because
773 * the arp function has more restrictions on the path length.
774 *
775 * return FAIL for failure, OK otherwise
776 */
777 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100778lock2name(BPTR lock, char_u *buf, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
780#ifdef FEAT_ARP
781 if (dos2) /* use 2.0 function */
782#endif
783 return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL);
784#ifdef FEAT_ARP
785 else /* use arp function */
786 return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL);
787#endif
788}
789
790/*
791 * get file permissions for 'name'
792 * Returns -1 when it doesn't exist.
793 */
794 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100795mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 struct FileInfoBlock *fib;
798 long retval = -1;
799
800 fib = get_fib(name);
801 if (fib != NULL)
802 {
803 retval = fib->fib_Protection;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000804 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 return retval;
807}
808
809/*
810 * set file permission for 'name' to 'perm'
811 *
812 * return FAIL for failure, OK otherwise
813 */
814 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100815mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 perm &= ~FIBF_ARCHIVE; /* reset archived bit */
818 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
819}
820
821/*
822 * Set hidden flag for "name".
823 */
824 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100825mch_hide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
827 /* can't hide a file */
828}
829
830/*
831 * return FALSE if "name" is not a directory
832 * return TRUE if "name" is a directory.
833 * return FALSE for error.
834 */
835 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100836mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 struct FileInfoBlock *fib;
839 int retval = FALSE;
840
841 fib = get_fib(name);
842 if (fib != NULL)
843 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000844#ifdef __amigaos4__
845 retval = (FIB_IS_DRAWER(fib)) ? TRUE : FALSE;
846#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000848#endif
849 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 return retval;
852}
853
854/*
855 * Create directory "name".
856 */
Bram Moolenaare3853642006-09-14 19:36:57 +0000857 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100858mch_mkdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859{
860 BPTR lock;
861
862 lock = CreateDir(name);
863 if (lock != NULL)
Bram Moolenaare3853642006-09-14 19:36:57 +0000864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 UnLock(lock);
Bram Moolenaare3853642006-09-14 19:36:57 +0000866 return 0;
867 }
868 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869}
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871/*
872 * Return 1 if "name" can be executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +0100873 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 * Return -1 if unknown.
875 */
876 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100877mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878{
879 /* TODO */
880 return -1;
881}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
883/*
884 * Check what "name" is:
885 * NODE_NORMAL: file or directory (or doesn't exist)
886 * NODE_WRITABLE: writable device, socket, fifo, etc.
887 * NODE_OTHER: non-writable things
888 */
889 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100890mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 /* TODO */
893 return NODE_NORMAL;
894}
895
896 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100897mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898{
899}
900
901/*
902 * Careful: mch_exit() may be called before mch_init()!
903 */
904 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100905mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100907 exiting = TRUE;
908
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (raw_in) /* put terminal in 'normal' mode */
910 {
911 settmode(TMODE_COOK);
912 stoptermcap();
913 }
914 out_char('\n');
915 if (raw_out)
916 {
917 if (term_console)
918 {
919 win_resize_off(); /* window resize events de-activated */
920 if (size_set)
921 OUT_STR("\233t\233u"); /* reset window size (CSI t CSI u) */
922 }
923 out_flush();
924 }
925
926#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +0200927 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window title */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928#endif
929
930 ml_close_all(TRUE); /* remove all memfiles */
931
932#ifdef FEAT_ARP
933 if (ArpBase)
934 CloseLibrary((struct Library *) ArpBase);
935#endif
936 if (close_win)
937 Close(raw_in);
938 if (r)
939 printf(_("Vim exiting with %d\n"), r); /* somehow this makes :cq work!? */
940 exit(r);
941}
942
943/*
944 * This is a routine for setting a given stream to raw or cooked mode on the
945 * Amiga . This is useful when you are using Lattice C to produce programs
946 * that want to read single characters with the "getch()" or "fgetc" call.
947 *
948 * Written : 18-Jun-87 By Chuck McManis.
949 */
950
951#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
952
953/*
954 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
955 * 'cooked' mode. This only works on TTY's.
956 *
957 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
958 * getch() will return immediately rather than wait for a return. You
959 * lose editing features though.
960 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100961 * Cooked: This function returns the designate file pointer to its normal,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 * wait for a <CR> mode. This is exactly like raw() except that
963 * it sends a 0 to the console to make it back into a CON: from a RAW:
964 */
965 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100966mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000968#if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
970#else
971 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
972 tmode == TMODE_RAW ? -1L : 0L) == 0)
973#endif
974 mch_errmsg(_("cannot change console mode ?!\n"));
975}
976
977/*
978 * set screen mode, always fails.
979 */
980 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100981mch_screenmode(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 return FAIL;
985}
986
987/*
988 * Code for this routine came from the following :
989 *
990 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
991 * DOS packet example
992 * Requires 1.2
993 *
994 * Found on Fish Disk 56.
995 *
996 * Heavely modified by mool.
997 */
998
Bram Moolenaar82881492012-11-20 16:53:39 +0100999#ifndef PROTO
1000# include <devices/conunit.h>
1001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
1003/*
1004 * try to get the real window size
1005 * return FAIL for failure, OK otherwise
1006 */
1007 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001008mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010 struct ConUnit *conUnit;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001011#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 char id_a[sizeof(struct InfoData) + 3];
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001013#endif
1014 struct InfoData *id=0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
1016 if (!term_console) /* not an amiga window */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001017 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
1019 /* insure longword alignment */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001020#ifdef __amigaos4__
Bram Moolenaar62dbdc42011-10-20 18:24:22 +02001021 if (!(id = AllocDosObject(DOS_INFODATA, 0)))
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001022 goto out;
1023#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 /*
1028 * Should make console aware of real window size, not the one we set.
1029 * Unfortunately, under DOS 2.0x this redraws the window and it
1030 * is rarely needed, so we skip it now, unless we changed the size.
1031 */
1032 if (size_set)
1033 OUT_STR("\233t\233u"); /* CSI t CSI u */
1034 out_flush();
1035
1036#ifdef __AROS__
1037 if (!Info(raw_out, id)
1038 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
1039#else
1040 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
1041 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
1042#endif
1043 {
1044 /* it's not an amiga window, maybe aux device */
1045 /* terminal type should be set */
1046 term_console = FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001047 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049 if (oldwindowtitle == NULL)
1050 oldwindowtitle = (char_u *)wb_window->Title;
1051 if (id->id_InUse == (BPTR)NULL)
1052 {
1053 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
1054 return FAIL;
1055 }
1056 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
1057
1058 /* get window size */
1059 Rows = conUnit->cu_YMax + 1;
1060 Columns = conUnit->cu_XMax + 1;
1061 if (Rows < 0 || Rows > 200) /* cannot be an amiga window */
1062 {
1063 Columns = 80;
1064 Rows = 24;
1065 term_console = FALSE;
1066 return FAIL;
1067 }
1068
1069 return OK;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001070out:
1071#ifdef __amigaos4__
1072 FreeDosObject(DOS_INFODATA, id); /* Safe to pass NULL */
1073#endif
1074
1075 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076}
1077
1078/*
1079 * Try to set the real window size to Rows and Columns.
1080 */
1081 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001082mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 if (term_console)
1085 {
1086 size_set = TRUE;
1087 out_char(CSI);
1088 out_num((long)Rows);
1089 out_char('t');
1090 out_char(CSI);
1091 out_num((long)Columns);
1092 out_char('u');
1093 out_flush();
1094 }
1095}
1096
1097/*
1098 * Rows and/or Columns has changed.
1099 */
1100 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001101mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 /* Nothing to do. */
1104}
1105
1106/*
1107 * out_num - output a (big) number fast
1108 */
1109 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001110out_num(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112 OUT_STR_NF(tltoa((unsigned long)n));
1113}
1114
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001115#if !defined(AZTEC_C) && !defined(__AROS__) && !defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116/*
1117 * Sendpacket.c
1118 *
1119 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1120 * the given message port. This makes working around DOS lots easier.
1121 *
1122 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1123 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1124 * say 'oml lib:amiga.lib -r sendpacket.o'
1125 */
1126
Bram Moolenaar82881492012-11-20 16:53:39 +01001127#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128/* #include <proto/exec.h> */
1129/* #include <proto/dos.h> */
Bram Moolenaar82881492012-11-20 16:53:39 +01001130# include <exec/memory.h>
1131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132
1133/*
1134 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1135 * Finkel. This function will send a packet of the given type to the Message
1136 * Port supplied.
1137 */
1138
1139 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01001140dos_packet(
1141 struct MsgPort *pid, /* process identifier ... (handlers message port) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 long action, /* packet type ... (what you want handler to do) */
Bram Moolenaard90b6c02016-08-28 18:10:45 +02001143 long arg) /* single argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144{
1145# ifdef FEAT_ARP
1146 struct MsgPort *replyport;
1147 struct StandardPacket *packet;
1148 long res1;
1149
1150 if (dos2)
1151# endif
1152 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); /* use 2.0 function */
1153# ifdef FEAT_ARP
1154
1155 replyport = (struct MsgPort *) CreatePort(NULL, 0); /* use arp function */
1156 if (!replyport)
1157 return (0);
1158
1159 /* Allocate space for a packet, make it public and clear it */
1160 packet = (struct StandardPacket *)
1161 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
1162 if (!packet) {
1163 DeletePort(replyport);
1164 return (0);
1165 }
1166 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1167 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1168 packet->sp_Pkt.dp_Port = replyport;
1169 packet->sp_Pkt.dp_Type = action;
1170 packet->sp_Pkt.dp_Arg1 = arg;
1171
1172 PutMsg(pid, (struct Message *)packet); /* send packet */
1173
1174 WaitPort(replyport);
1175 GetMsg(replyport);
1176
1177 res1 = packet->sp_Pkt.dp_Res1;
1178
1179 FreeMem(packet, (long) sizeof(struct StandardPacket));
1180 DeletePort(replyport);
1181
1182 return (res1);
1183# endif
1184}
1185#endif /* !defined(AZTEC_C) && !defined(__AROS__) */
1186
1187/*
1188 * Call shell.
1189 * Return error number for failure, 0 otherwise
1190 */
1191 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001192mch_call_shell(
1193 char_u *cmd,
1194 int options) /* SHELL_*, see vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
1196 BPTR mydir;
1197 int x;
1198 int tmode = cur_tmode;
1199#ifdef AZTEC_C
1200 int use_execute;
1201 char_u *shellcmd = NULL;
1202 char_u *shellarg;
1203#endif
1204 int retval = 0;
1205
1206 if (close_win)
1207 {
1208 /* if Vim opened a window: Executing a shell may cause crashes */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001209 emsg(_("E360: Cannot execute shell with -f option"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 return -1;
1211 }
1212
1213 if (term_console)
1214 win_resize_off(); /* window resize events de-activated */
1215 out_flush();
1216
1217 if (options & SHELL_COOKED)
1218 settmode(TMODE_COOK); /* set to normal mode */
1219 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); /* remember current dir */
1220
1221#if !defined(AZTEC_C) /* not tested very much */
1222 if (cmd == NULL)
1223 {
1224# ifdef FEAT_ARP
1225 if (dos2)
1226# endif
1227 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1228# ifdef FEAT_ARP
1229 else
1230 x = Execute(p_sh, raw_in, raw_out);
1231# endif
1232 }
1233 else
1234 {
1235# ifdef FEAT_ARP
1236 if (dos2)
1237# endif
1238 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1239# ifdef FEAT_ARP
1240 else
1241 x = Execute((char *)cmd, 0L, raw_out);
1242# endif
1243 }
1244# ifdef FEAT_ARP
1245 if ((dos2 && x < 0) || (!dos2 && !x))
1246# else
1247 if (x < 0)
1248# endif
1249 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001250 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 if (cmd == NULL)
1252 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001253 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 msg_outtrans(p_sh);
1255 }
1256 else
1257 msg_outtrans(cmd);
1258 msg_putchar('\n');
1259 retval = -1;
1260 }
1261# ifdef FEAT_ARP
1262 else if (!dos2 || x)
1263# else
1264 else if (x)
1265# endif
1266 {
1267 if ((x = IoErr()) != 0)
1268 {
1269 if (!(options & SHELL_SILENT))
1270 {
1271 msg_putchar('\n');
1272 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001273 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 retval = x;
1276 }
1277 }
1278#else /* else part is for AZTEC_C */
1279 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1280 use_execute = 1;
1281 else
1282 use_execute = 0;
1283 if (!use_execute)
1284 {
1285 /*
1286 * separate shell name from argument
1287 */
1288 shellcmd = vim_strsave(p_sh);
1289 if (shellcmd == NULL) /* out of memory, use Execute */
1290 use_execute = 1;
1291 else
1292 {
1293 shellarg = skiptowhite(shellcmd); /* find start of arguments */
1294 if (*shellarg != NUL)
1295 {
1296 *shellarg++ = NUL;
1297 shellarg = skipwhite(shellarg);
1298 }
1299 }
1300 }
1301 if (cmd == NULL)
1302 {
1303 if (use_execute)
1304 {
1305# ifdef FEAT_ARP
1306 if (dos2)
1307# endif
1308 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1309# ifdef FEAT_ARP
1310 else
1311 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1312# endif
1313 }
1314 else
1315 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1316 }
1317 else if (use_execute)
1318 {
1319# ifdef FEAT_ARP
1320 if (dos2)
1321# endif
1322 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1323# ifdef FEAT_ARP
1324 else
1325 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1326# endif
1327 }
1328 else if (p_st & 1)
1329 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1330 (char *)cmd, NULL);
1331 else
1332 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1333 (char *)p_shcf, (char *)cmd, NULL);
1334# ifdef FEAT_ARP
1335 if ((dos2 && x < 0) || (!dos2 && x))
1336# else
1337 if (x < 0)
1338# endif
1339 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001340 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (use_execute)
1342 {
1343 if (cmd == NULL)
1344 msg_outtrans(p_sh);
1345 else
1346 msg_outtrans(cmd);
1347 }
1348 else
1349 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001350 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 msg_outtrans(shellcmd);
1352 }
1353 msg_putchar('\n');
1354 retval = -1;
1355 }
1356 else
1357 {
1358 if (use_execute)
1359 {
1360# ifdef FEAT_ARP
1361 if (!dos2 || x)
1362# else
1363 if (x)
1364# endif
1365 x = IoErr();
1366 }
1367 else
1368 x = wait();
1369 if (x)
1370 {
1371 if (!(options & SHELL_SILENT) && !emsg_silent)
1372 {
1373 msg_putchar('\n');
1374 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001375 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377 retval = x;
1378 }
1379 }
1380 vim_free(shellcmd);
1381#endif /* AZTEC_C */
1382
1383 if ((mydir = CurrentDir(mydir)) != 0) /* make sure we stay in the same directory */
1384 UnLock(mydir);
1385 if (tmode == TMODE_RAW)
1386 settmode(TMODE_RAW); /* set to raw mode */
1387#ifdef FEAT_TITLE
1388 resettitle();
1389#endif
1390 if (term_console)
1391 win_resize_on(); /* window resize events activated */
1392 return retval;
1393}
1394
1395/*
1396 * check for an "interrupt signal"
1397 * We only react to a CTRL-C, but also clear the other break signals to avoid
1398 * trouble with lattice-c programs.
1399 */
1400 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02001401mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1404 got_int = TRUE;
1405}
1406
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01001407/* this routine causes manx to use this Chk_Abort() rather than its own */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408/* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
1409/* is zero). Since we want to check for our own ^C's */
1410
1411#ifdef _DCC
1412#define Chk_Abort chkabort
1413#endif
1414
1415#ifdef LATTICE
1416void __regargs __chkabort(void);
1417
1418void __regargs __chkabort(void)
1419{}
1420
1421#else
1422 long
1423Chk_Abort(void)
1424{
1425 return(0L);
1426}
1427#endif
1428
1429/*
1430 * mch_expandpath() - this code does wild-card pattern matching using the arp
1431 * routines.
1432 *
1433 * "pat" has backslashes before chars that are not to be expanded.
1434 * Returns the number of matches found.
1435 *
1436 * This is based on WildDemo2.c (found in arp1.1 distribution).
1437 * That code's copyright follows:
1438 * Copyright (c) 1987, Scott Ballantyne
1439 * Use and abuse as you please.
1440 */
1441
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001442#ifdef __amigaos4__
1443# define ANCHOR_BUF_SIZE 1024
1444#else
1445# define ANCHOR_BUF_SIZE (512)
1446# define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001450mch_expandpath(
1451 garray_T *gap,
1452 char_u *pat,
1453 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 struct AnchorPath *Anchor;
1456 LONG Result;
1457 char_u *starbuf, *sp, *dp;
1458 int start_len;
1459 int matches;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001460#ifdef __amigaos4__
1461 struct TagItem AnchorTags[] = {
1462 {ADO_Strlen, ANCHOR_BUF_SIZE},
1463 {ADO_Flags, APF_DODOT|APF_DOWILD|APF_MultiAssigns},
1464 {TAG_DONE, 0L}
1465 };
1466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
1468 start_len = gap->ga_len;
1469
1470 /* Get our AnchorBase */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001471#ifdef __amigaos4__
1472 Anchor = AllocDosObject(DOS_ANCHORPATH, AnchorTags);
1473#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001474 Anchor = alloc_clear(ANCHOR_SIZE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (Anchor == NULL)
1477 return 0;
1478
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001479#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; /* ap_Length not supported anymore */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001481# ifdef APF_DODOT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001483# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 Anchor->ap_Flags = APF_DoDot | APF_DoWild; /* allow '.' for current dir */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486#endif
1487
1488#ifdef FEAT_ARP
1489 if (dos2)
1490 {
1491#endif
1492 /* hack to replace '*' by '#?' */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001493 starbuf = alloc(2 * STRLEN(pat) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 if (starbuf == NULL)
1495 goto Return;
1496 for (sp = pat, dp = starbuf; *sp; ++sp)
1497 {
1498 if (*sp == '*')
1499 {
1500 *dp++ = '#';
1501 *dp++ = '?';
1502 }
1503 else
1504 *dp++ = *sp;
1505 }
1506 *dp = NUL;
1507 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1508 vim_free(starbuf);
1509#ifdef FEAT_ARP
1510 }
1511 else
1512 Result = FindFirst((char *)pat, Anchor);
1513#endif
1514
1515 /*
1516 * Loop to get all matches.
1517 */
1518 while (Result == 0)
1519 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001520#ifdef __amigaos4__
1521 addfile(gap, (char_u *)Anchor->ap_Buffer, flags);
1522#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525#ifdef FEAT_ARP
1526 if (dos2)
1527#endif
1528 Result = MatchNext(Anchor);
1529#ifdef FEAT_ARP
1530 else
1531 Result = FindNext(Anchor);
1532#endif
1533 }
1534 matches = gap->ga_len - start_len;
1535
1536 if (Result == ERROR_BUFFER_OVERFLOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001537 emsg(_("ANCHOR_BUF_SIZE too small."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1539 && Result != ERROR_DEVICE_NOT_MOUNTED
1540 && Result != ERROR_NO_MORE_ENTRIES)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001541 emsg(_("I/O ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
1543 /*
1544 * Sort the files for this pattern.
1545 */
1546 if (matches)
1547 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1548 (size_t)matches, sizeof(char_u *), sortcmp);
1549
1550 /* Free the wildcard stuff */
1551#ifdef FEAT_ARP
1552 if (dos2)
1553#endif
1554 MatchEnd(Anchor);
1555#ifdef FEAT_ARP
1556 else
1557 FreeAnchorChain(Anchor);
1558#endif
1559
1560Return:
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001561#ifdef __amigaos4__
1562 FreeDosObject(DOS_ANCHORPATH, Anchor);
1563#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 vim_free(Anchor);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 return matches;
1568}
1569
1570 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001571sortcmp(const void *a, const void *b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573 char *s = *(char **)a;
1574 char *t = *(char **)b;
1575
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001576 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577}
1578
1579/*
1580 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1581 */
1582 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001583mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001585 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
1587 if (*p == '\\' && p[1] != NUL)
1588 ++p;
1589 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1590 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592 return FALSE;
1593}
1594
1595 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001596mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001598 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 if (*p == '\\' && p[1] != NUL)
1601 ++p;
1602 else
1603 if (vim_strchr((char_u *)
1604# ifdef VIM_BACKTICK
1605 "*?[(#$`"
1606# else
1607 "*?[(#$"
1608# endif
1609 , *p) != NULL
1610 || (*p == '~' && p[1] != NUL))
1611 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 return FALSE;
1614}
1615
1616/*
1617 * With AmigaDOS 2.0 support for reading local environment variables
1618 *
1619 * Two buffers are allocated:
1620 * - A big one to do the expansion into. It is freed before returning.
1621 * - A small one to hold the return value. It is kept until the next call.
1622 */
1623 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001624mch_getenv(char_u *var)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 int len;
1627 UBYTE *buf; /* buffer to expand in */
1628 char_u *retval; /* return value */
1629 static char_u *alloced = NULL; /* allocated memory */
1630
1631#ifdef FEAT_ARP
1632 if (!dos2)
1633 retval = (char_u *)getenv((char *)var);
1634 else
1635#endif
1636 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001637 VIM_CLEAR(alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 retval = NULL;
1639
1640 buf = alloc(IOSIZE);
1641 if (buf == NULL)
1642 return NULL;
1643
1644 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1645 if (len >= 0)
1646 {
1647 retval = vim_strsave((char_u *)buf);
1648 alloced = retval;
1649 }
1650
1651 vim_free(buf);
1652 }
1653
1654 /* if $VIM is not defined, use "vim:" instead */
1655 if (retval == NULL && STRCMP(var, "VIM") == 0)
1656 retval = (char_u *)"vim:";
1657
1658 return retval;
1659}
1660
1661/*
1662 * Amiga version of setenv() with AmigaDOS 2.0 support.
1663 */
1664/* ARGSUSED */
1665 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001666mch_setenv(char *var, char *value, int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
1668#ifdef FEAT_ARP
1669 if (!dos2)
1670 return setenv(var, value);
1671#endif
1672
1673 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
1674 return 0; /* success */
1675 return -1; /* failure */
1676}