blob: 6d444142f272b1bbfe201d36bd41bcb58e3702f4 [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
Bram Moolenaar0f873732019-12-05 20:28:46 +010020# undef Window // Amiga has its own Window definition
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#endif
22
Bram Moolenaar0f873732019-12-05 20:28:46 +010023#undef TRUE // will be redefined by exec/types.h
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#undef FALSE
25
Bram Moolenaar0f873732019-12-05 20:28:46 +010026// cproto fails on missing include files, skip them
Bram Moolenaar82881492012-11-20 16:53:39 +010027#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 Moolenaar0f873732019-12-05 20:28:46 +010036// XXX These are included from os_amiga.h
37// #include <proto/exec.h>
38// #include <proto/dos.h>
39// #include <proto/intuition.h>
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000040
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#include <exec/memory.h>
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000042#include <libraries/dosextens.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
Bram Moolenaar0f873732019-12-05 20:28:46 +010044#include <dos/dostags.h> // for 2.0 functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000045#include <dos/dosasl.h>
46
Bram Moolenaar0f873732019-12-05 20:28:46 +010047// From version 4 of AmigaOS, several system structures must be allocated
48// and freed using system functions. "struct AnchorPath" is one.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000049#ifdef __amigaos4__
50# include <dos/anchorpath.h>
51# define free_fib(x) FreeDosObject(DOS_FIB, x)
52#else
53# define free_fib(x) vim_free(fib)
54#endif
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056#if defined(LATTICE) && !defined(SASC) && defined(FEAT_ARP)
57# include <libraries/arp_pragmas.h>
58#endif
59
Bram Moolenaar0f873732019-12-05 20:28:46 +010060#endif // PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +010061
Bram Moolenaar071d4272004-06-13 20:20:40 +000062/*
Bram Moolenaar9ee3d162019-07-02 23:22:43 +020063 * Set stack size to 1 MiB on NG systems. This should be enough even for
64 * hungry syntax HL / plugin combinations. Leave the stack alone on OS 3
65 * and below, those systems might be low on memory.
66 */
67#if defined(__amigaos4__)
68static const char* __attribute__((used)) stackcookie = "$STACK: 1048576";
69#elif defined(__AROS__) || defined(__MORPHOS__)
70unsigned long __stack = 1048576;
71#endif
72
73/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 * At this point TRUE and FALSE are defined as 1L and 0L, but we want 1 and 0.
75 */
76#undef TRUE
77#define TRUE (1)
78#undef FALSE
79#define FALSE (0)
80
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000081#ifdef __amigaos4__
82# define dos_packet(a, b, c) DoPkt(a, b, c, 0, 0, 0, 0)
83#elif !defined(AZTEC_C) && !defined(__AROS__)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010084static long dos_packet(struct MsgPort *, long, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010086static int lock2name(BPTR lock, char_u *buf, long len);
87static void out_num(long n);
88static struct FileInfoBlock *get_fib(char_u *);
89static int sortcmp(const void *a, const void *b);
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91static BPTR raw_in = (BPTR)NULL;
92static BPTR raw_out = (BPTR)NULL;
Bram Moolenaar0f873732019-12-05 20:28:46 +010093static int close_win = FALSE; // set if Vim opened the window
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
Bram Moolenaar0f873732019-12-05 20:28:46 +010095#ifndef __amigaos4__ // Use autoopen for AmigaOS4
Bram Moolenaar071d4272004-06-13 20:20:40 +000096struct IntuitionBase *IntuitionBase = NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#ifdef FEAT_ARP
99struct ArpBase *ArpBase = NULL;
100#endif
101
102static struct Window *wb_window;
103static char_u *oldwindowtitle = NULL;
104
105#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100106int dos2 = FALSE; // Amiga DOS 2.0x or higher
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100108int size_set = FALSE; // set to TRUE if window size was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109
Bram Moolenaar70576f72019-07-31 20:40:08 +0200110#ifdef __GNUC__
111static char version[] __attribute__((used)) =
112 "\0$VER: Vim "
113 VIM_VERSION_MAJOR_STR "."
114 VIM_VERSION_MINOR_STR
115# ifdef PATCHLEVEL
116 "." PATCHLEVEL
117# endif
118 ;
119#endif
120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100122win_resize_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123{
124 OUT_STR_NF("\033[12{");
125}
126
127 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100128win_resize_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129{
130 OUT_STR_NF("\033[12}");
131}
132
133 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100134mch_write(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135{
136 Write(raw_out, (char *)p, (long)len);
137}
138
139/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200140 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 * Get a characters from the keyboard.
142 * If time == 0 do not wait for characters.
143 * If time == n wait a short time for characters.
144 * If time == -1 wait forever for characters.
145 *
146 * Return number of characters read.
147 */
148 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100149mch_inchar(
150 char_u *buf,
151 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100152 long time, // milli seconds
Bram Moolenaar05540972016-01-30 20:31:25 +0100153 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
155 int len;
156 long utime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
158 if (time >= 0)
159 {
160 if (time == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100161 utime = 100L; // time = 0 causes problems in DOS 1.2
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100163 utime = time * 1000L; // convert from milli to micro secs
164 if (WaitForChar(raw_in, utime) == 0) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100167 else // time == -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 /*
170 * If there is no character available within 2 seconds (default)
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000171 * write the autoscript file to disk. Or cause the CursorHold event
172 * to be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 */
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000174 if (WaitForChar(raw_in, p_ut * 1000L) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000176 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 {
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000178 buf[0] = K_SPECIAL;
179 buf[1] = KS_EXTRA;
180 buf[2] = (int)KE_CURSORHOLD;
181 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000183 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 }
185 }
186
Bram Moolenaar0f873732019-12-05 20:28:46 +0100187 for (;;) // repeat until we got a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 len = Read(raw_in, (char *)buf, (long)maxlen / input_conv.vc_factor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 if (len > 0)
191 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100192 // Convert from 'termencoding' to 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (input_conv.vc_type != CONV_NONE)
194 len = convert_input(buf, len, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 return len;
196 }
197 }
198}
199
200/*
201 * return non-zero if a character is available
202 */
203 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100204mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205{
206 return (WaitForChar(raw_in, 100L) != 0);
207}
208
209/*
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200210 * Return amount of memory still available in Kbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 */
212 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100213mch_avail_mem(int special)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214{
Bram Moolenaara9ab3912019-08-10 14:54:20 +0200215#if defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200216 return (long_u)AvailMem(MEMF_ANY) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000217#else
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200218 return (long_u)(AvailMem(special ? (long)MEMF_CHIP : (long)MEMF_ANY)) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220}
221
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000222/*
223 * Waits a specified amount of time, or until input arrives if
224 * ignoreinput is FALSE.
225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100227mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100229#ifndef LATTICE // SAS declares void Delay(ULONG)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100230 void Delay(long);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#endif
232
233 if (msec > 0)
234 {
235 if (ignoreinput)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100236 Delay(msec / 20L); // Delay works with 20 msec intervals
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 else
238 WaitForChar(raw_in, msec * 1000L);
239 }
240}
241
242/*
243 * We have no job control, fake it by starting a new shell.
244 */
245 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100246mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247{
248 suspend_shell();
249}
250
251#ifndef DOS_LIBRARY
252# define DOS_LIBRARY ((UBYTE *)"dos.library")
253#endif
254
255 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100256mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257{
258 static char intlibname[] = "intuition.library";
259
260#ifdef AZTEC_C
Bram Moolenaar0f873732019-12-05 20:28:46 +0100261 Enable_Abort = 0; // disallow vim to be aborted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#endif
263 Columns = 80;
264 Rows = 24;
265
266 /*
267 * Set input and output channels, unless we have opened our own window
268 */
269 if (raw_in == (BPTR)NULL)
270 {
271 raw_in = Input();
272 raw_out = Output();
273 /*
274 * If Input() is not interactive, then Output() will be (because of
275 * check in mch_check_win()). Used for "Vim -".
276 * Also check the other way around, for "Vim -h | more".
277 */
278 if (!IsInteractive(raw_in))
279 raw_in = raw_out;
280 else if (!IsInteractive(raw_out))
281 raw_out = raw_in;
282 }
283
284 out_flush();
285
286 wb_window = NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000287#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 if ((IntuitionBase = (struct IntuitionBase *)
289 OpenLibrary((UBYTE *)intlibname, 0L)) == NULL)
290 {
291 mch_errmsg(_("cannot open "));
292 mch_errmsg(intlibname);
293 mch_errmsg("!?\n");
294 mch_exit(3);
295 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297}
298
Bram Moolenaar82881492012-11-20 16:53:39 +0100299#ifndef PROTO
300# include <workbench/startup.h>
301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302
303/*
304 * Check_win checks whether we have an interactive window.
305 * If not, a new window is opened with the newcli command.
306 * If we would open a window ourselves, the :sh and :! commands would not
307 * work properly (Why? probably because we are then running in a background
308 * CLI). This also is the best way to assure proper working in a next
309 * Workbench release.
310 *
311 * For the -f option (foreground mode) we open our own window and disable :sh.
312 * Otherwise the calling program would never know when editing is finished.
313 */
Bram Moolenaar0f873732019-12-05 20:28:46 +0100314#define BUF2SIZE 320 // length of buffer for argument with complete path
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315
316 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100317mch_check_win(int argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318{
319 int i;
320 BPTR nilfh, fh;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000321 char_u buf1[24];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 char_u buf2[BUF2SIZE];
323 static char_u *(constrings[3]) = {(char_u *)"con:0/0/662/210/",
324 (char_u *)"con:0/0/640/200/",
325 (char_u *)"con:0/0/320/200/"};
326 static char_u *winerr = (char_u *)N_("VIM: Can't open window!\n");
327 struct WBArg *argp;
328 int ac;
329 char *av;
330 char_u *device = NULL;
331 int exitval = 4;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000332#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 struct Library *DosBase;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 int usewin = FALSE;
336
337/*
338 * check if we are running under DOS 2.0x or higher
339 */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000340#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 DosBase = OpenLibrary(DOS_LIBRARY, 37L);
342 if (DosBase != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100343 // if (((struct Library *)DOSBase)->lib_Version >= 37)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 {
345 CloseLibrary(DosBase);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000346# ifdef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 dos2 = TRUE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100350 else // without arp functions we NEED 2.0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000352# ifndef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 mch_errmsg(_("Need Amigados version 2.04 or later\n"));
354 exit(3);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000355# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100356 // need arp functions for dos 1.x
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 if (!(ArpBase = (struct ArpBase *) OpenLibrary((UBYTE *)ArpName, ArpVersion)))
358 {
359 fprintf(stderr, _("Need %s version %ld\n"), ArpName, ArpVersion);
360 exit(3);
361 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100364#endif // __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
366 /*
367 * scan argv[] for the "-f" and "-d" arguments
368 */
369 for (i = 1; i < argc; ++i)
370 if (argv[i][0] == '-')
371 {
372 switch (argv[i][1])
373 {
374 case 'f':
375 usewin = TRUE;
376 break;
377
378 case 'd':
379 if (i < argc - 1
380#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +0100381 // require using "-dev", "-d" means diff mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 && argv[i][2] == 'e' && argv[i][3] == 'v'
383#endif
384 )
385 device = (char_u *)argv[i + 1];
386 break;
387 }
388 }
389
390/*
391 * If we were not started from workbench, do not have a "-d" or "-dev"
392 * argument and we have been started with an interactive window, use that
393 * window.
394 */
395 if (argc != 0
396 && device == NULL
397 && (IsInteractive(Input()) || IsInteractive(Output())))
398 return OK;
399
400/*
401 * When given the "-f" argument, we open our own window. We can't use the
402 * newcli trick below, because the calling program (mail, rn, etc.) would not
403 * know when we are finished.
404 */
405 if (usewin)
406 {
407 /*
408 * Try to open a window. First try the specified device.
409 * Then try a 24 line 80 column window.
410 * If that fails, try two smaller ones.
411 */
412 for (i = -1; i < 3; ++i)
413 {
414 if (i >= 0)
415 device = constrings[i];
416 if (device != NULL && (raw_in = Open((UBYTE *)device,
417 (long)MODE_NEWFILE)) != (BPTR)NULL)
418 break;
419 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100420 if (raw_in == (BPTR)NULL) // all three failed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {
422 mch_errmsg(_(winerr));
423 goto exit;
424 }
425 raw_out = raw_in;
426 close_win = TRUE;
427 return OK;
428 }
429
430 if ((nilfh = Open((UBYTE *)"NIL:", (long)MODE_NEWFILE)) == (BPTR)NULL)
431 {
432 mch_errmsg(_("Cannot open NIL:\n"));
433 goto exit;
434 }
435
436 /*
437 * Make a unique name for the temp file (which we will not delete!).
438 * Use a pointer on the stack (nobody else will be using it).
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000439 * Under AmigaOS4, this assumption might change in the future, so
440 * we use a pointer to the current task instead. This should be a
441 * shared structure and thus globally unique.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000443#ifdef __amigaos4__
444 sprintf((char *)buf1, "t:nc%p", FindTask(0));
445#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 sprintf((char *)buf1, "t:nc%ld", (long)buf1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 if ((fh = Open((UBYTE *)buf1, (long)MODE_NEWFILE)) == (BPTR)NULL)
449 {
450 mch_errmsg(_("Cannot create "));
451 mch_errmsg((char *)buf1);
452 mch_errmsg("\n");
453 goto exit;
454 }
455 /*
456 * Write the command into the file, put quotes around the arguments that
457 * have a space in them.
458 */
Bram Moolenaar0f873732019-12-05 20:28:46 +0100459 if (argc == 0) // run from workbench
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 ac = ((struct WBStartup *)argv)->sm_NumArgs;
461 else
462 ac = argc;
463 for (i = 0; i < ac; ++i)
464 {
465 if (argc == 0)
466 {
467 *buf2 = NUL;
468 argp = &(((struct WBStartup *)argv)->sm_ArgList[i]);
469 if (argp->wa_Lock)
470 (void)lock2name(argp->wa_Lock, buf2, (long)(BUF2SIZE - 1));
471#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100472 if (dos2) // use 2.0 function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#endif
474 AddPart((UBYTE *)buf2, (UBYTE *)argp->wa_Name, (long)(BUF2SIZE - 1));
475#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100476 else // use arp function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 TackOn((char *)buf2, argp->wa_Name);
478#endif
479 av = (char *)buf2;
480 }
481 else
482 av = argv[i];
483
Bram Moolenaar0f873732019-12-05 20:28:46 +0100484 // skip '-d' or "-dev" option
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 if (av[0] == '-' && av[1] == 'd'
486#ifdef FEAT_DIFF
487 && av[2] == 'e' && av[3] == 'v'
488#endif
489 )
490 {
491 ++i;
492 continue;
493 }
494 if (vim_strchr((char_u *)av, ' '))
495 Write(fh, "\"", 1L);
496 Write(fh, av, (long)strlen(av));
497 if (vim_strchr((char_u *)av, ' '))
498 Write(fh, "\"", 1L);
499 Write(fh, " ", 1L);
500 }
501 Write(fh, "\nendcli\n", 8L);
502 Close(fh);
503
504/*
505 * Try to open a new cli in a window. If "-d" or "-dev" argument was given try
506 * to open the specified device. Then try a 24 line 80 column window. If that
507 * fails, try two smaller ones.
508 */
509 for (i = -1; i < 3; ++i)
510 {
511 if (i >= 0)
512 device = constrings[i];
513 else if (device == NULL)
514 continue;
515 sprintf((char *)buf2, "newcli <nil: >nil: %s from %s", (char *)device, (char *)buf1);
516#ifdef FEAT_ARP
517 if (dos2)
518 {
519#endif
520 if (!SystemTags((UBYTE *)buf2, SYS_UserShell, TRUE, TAG_DONE))
521 break;
522#ifdef FEAT_ARP
523 }
524 else
525 {
526 if (Execute((UBYTE *)buf2, nilfh, nilfh))
527 break;
528 }
529#endif
530 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100531 if (i == 3) // all three failed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {
533 DeleteFile((UBYTE *)buf1);
534 mch_errmsg(_(winerr));
535 goto exit;
536 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100537 exitval = 0; // The Execute succeeded: exit this program
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
539exit:
540#ifdef FEAT_ARP
541 if (ArpBase)
542 CloseLibrary((struct Library *) ArpBase);
543#endif
544 exit(exitval);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100545 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 return FAIL;
547}
548
549/*
550 * Return TRUE if the input comes from a terminal, FALSE otherwise.
551 * We fake there is a window, because we can always open one!
552 */
553 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100554mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 return TRUE;
557}
558
559/*
560 * fname_case(): Set the case of the file name, if it already exists.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000561 * This will cause the file name to remain exactly the same
562 * if the file system ignores, but preserves case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 */
Bram Moolenaar0f873732019-12-05 20:28:46 +0100564//ARGSUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100566fname_case(
567 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100568 int len) // buffer size, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
570 struct FileInfoBlock *fib;
571 size_t flen;
572
573 fib = get_fib(name);
574 if (fib != NULL)
575 {
576 flen = STRLEN(name);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100577 // TODO: Check if this fix applies to AmigaOS < 4 too.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000578#ifdef __amigaos4__
579 if (fib->fib_DirEntryType == ST_ROOT)
580 strcat(fib->fib_FileName, ":");
581#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100582 if (flen == strlen(fib->fib_FileName)) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 mch_memmove(name, fib->fib_FileName, flen);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000584 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 }
586}
587
588/*
589 * Get the FileInfoBlock for file "fname"
590 * The returned structure has to be free()d.
591 * Returns NULL on error.
592 */
593 static struct FileInfoBlock *
Bram Moolenaar05540972016-01-30 20:31:25 +0100594get_fib(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595{
596 BPTR flock;
597 struct FileInfoBlock *fib;
598
Bram Moolenaar0f873732019-12-05 20:28:46 +0100599 if (fname == NULL) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 return NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000601#ifdef __amigaos4__
602 fib = AllocDosObject(DOS_FIB,0);
603#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200604 fib = ALLOC_ONE(struct FileInfoBlock);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (fib != NULL)
607 {
608 flock = Lock((UBYTE *)fname, (long)ACCESS_READ);
609 if (flock == (BPTR)NULL || !Examine(flock, fib))
610 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100611 free_fib(fib); // in case of an error the memory is freed here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 fib = NULL;
613 }
614 if (flock)
615 UnLock(flock);
616 }
617 return fib;
618}
619
620#ifdef FEAT_TITLE
621/*
622 * set the title of our window
623 * icon name is not set
624 */
625 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100626mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
628 if (wb_window != NULL && title != NULL)
629 SetWindowTitles(wb_window, (UBYTE *)title, (UBYTE *)-1L);
630}
631
632/*
633 * Restore the window/icon title.
634 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200635 * SAVE_RESTORE_TITLE Just restore title
636 * SAVE_RESTORE_ICON Just restore icon (which we don't have)
637 * SAVE_RESTORE_BOTH Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 */
639 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100640mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
Bram Moolenaar40385db2018-08-07 22:31:44 +0200642 if (which & SAVE_RESTORE_TITLE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 mch_settitle(oldwindowtitle, NULL);
644}
645
646 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100647mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 return (wb_window != NULL);
650}
651
652 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100653mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 return FALSE;
656}
657#endif
658
Bram Moolenaardfded982019-10-26 21:33:19 +0200659 void
660mch_setmouse(int on UNUSED)
661{
662 // TODO: implement
663}
664
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665/*
666 * Insert user name in s[len].
667 */
668 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100669mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
Bram Moolenaar5e8e9672019-09-27 13:38:56 +0200671#if defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__)
672 struct passwd *pwd = getpwuid(getuid());
673
674 if (pwd != NULL && pwd->pw_name && len > 0)
675 {
676 vim_strncpy(s, (char_u *)pwd->pw_name, len - 1);
677 return OK;
678 }
679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 *s = NUL;
681 return FAIL;
682}
683
684/*
685 * Insert host name is s[len].
686 */
687 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100688mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000690#if defined(__amigaos4__) && defined(__CLIB2__)
691 gethostname(s, len);
692#else
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000693 vim_strncpy(s, "Amiga", len - 1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695}
696
697/*
698 * return process ID
699 */
700 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100701mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000703#ifdef __amigaos4__
Bram Moolenaar0f873732019-12-05 20:28:46 +0100704 // This is as close to a pid as we can come. We could use CLI numbers also,
705 // but then we would have two different types of process identifiers.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000706 return((long)FindTask(0));
707#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 return (long)0;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710}
711
712/*
713 * Get name of current directory into buffer 'buf' of length 'len' bytes.
714 * Return OK for success, FAIL for failure.
715 */
716 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100717mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
719 return mch_FullName((char_u *)"", buf, len, FALSE);
720}
721
722/*
723 * get absolute file name into buffer 'buf' of length 'len' bytes
724 *
725 * return FAIL for failure, OK otherwise
726 */
727 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100728mch_FullName(
729 char_u *fname,
730 char_u *buf,
731 int len,
732 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733{
734 BPTR l;
735 int retval = FAIL;
736 int i;
737
Bram Moolenaar0f873732019-12-05 20:28:46 +0100738 // Lock the file. If it exists, we can get the exact name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
740 {
741 retval = lock2name(l, buf, (long)len - 1);
742 UnLock(l);
743 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100744 else if (force || !mch_isFullName(fname)) // not a full path yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 /*
747 * If the file cannot be locked (doesn't exist), try to lock the
748 * current directory and concatenate the file name.
749 */
750 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
751 {
752 retval = lock2name(l, buf, (long)len);
753 UnLock(l);
754 if (retval == OK)
755 {
756 i = STRLEN(buf);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100757 // Concatenate the fname to the directory. Don't add a slash
758 // if fname is empty, but do change "" to "/".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 if (i == 0 || *fname != NUL)
760 {
761 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
762 buf[i++] = '/';
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000763 vim_strncpy(buf + i, fname, len - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 }
765 }
766 }
767 }
768 if (*buf == 0 || *buf == ':')
Bram Moolenaar0f873732019-12-05 20:28:46 +0100769 retval = FAIL; // something failed; use the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 return retval;
771}
772
773/*
774 * Return TRUE if "fname" does not depend on the current directory.
775 */
776 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100777mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
779 return (vim_strchr(fname, ':') != NULL && *fname != ':');
780}
781
782/*
783 * Get the full file name from a lock. Use 2.0 function if possible, because
784 * the arp function has more restrictions on the path length.
785 *
786 * return FAIL for failure, OK otherwise
787 */
788 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100789lock2name(BPTR lock, char_u *buf, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
791#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100792 if (dos2) // use 2.0 function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793#endif
794 return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL);
795#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100796 else // use arp function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL);
798#endif
799}
800
801/*
802 * get file permissions for 'name'
803 * Returns -1 when it doesn't exist.
804 */
805 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100806mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 struct FileInfoBlock *fib;
809 long retval = -1;
810
811 fib = get_fib(name);
812 if (fib != NULL)
813 {
814 retval = fib->fib_Protection;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000815 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817 return retval;
818}
819
820/*
821 * set file permission for 'name' to 'perm'
822 *
823 * return FAIL for failure, OK otherwise
824 */
825 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100826mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100828 perm &= ~FIBF_ARCHIVE; // reset archived bit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
830}
831
832/*
833 * Set hidden flag for "name".
834 */
835 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100836mch_hide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100838 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840
841/*
842 * return FALSE if "name" is not a directory
843 * return TRUE if "name" is a directory.
844 * return FALSE for error.
845 */
846 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100847mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848{
849 struct FileInfoBlock *fib;
850 int retval = FALSE;
851
852 fib = get_fib(name);
853 if (fib != NULL)
854 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000855#ifdef __amigaos4__
856 retval = (FIB_IS_DRAWER(fib)) ? TRUE : FALSE;
857#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000859#endif
860 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
862 return retval;
863}
864
865/*
866 * Create directory "name".
867 */
Bram Moolenaare3853642006-09-14 19:36:57 +0000868 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100869mch_mkdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 BPTR lock;
872
873 lock = CreateDir(name);
874 if (lock != NULL)
Bram Moolenaare3853642006-09-14 19:36:57 +0000875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 UnLock(lock);
Bram Moolenaare3853642006-09-14 19:36:57 +0000877 return 0;
878 }
879 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880}
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882/*
883 * Return 1 if "name" can be executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +0100884 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * Return -1 if unknown.
886 */
887 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100888mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100890 // TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 return -1;
892}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
894/*
895 * Check what "name" is:
896 * NODE_NORMAL: file or directory (or doesn't exist)
897 * NODE_WRITABLE: writable device, socket, fifo, etc.
898 * NODE_OTHER: non-writable things
899 */
900 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100901mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100903 // TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 return NODE_NORMAL;
905}
906
907 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100908mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
910}
911
912/*
913 * Careful: mch_exit() may be called before mch_init()!
914 */
915 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100916mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100918 exiting = TRUE;
919
Bram Moolenaar0f873732019-12-05 20:28:46 +0100920 if (raw_in) // put terminal in 'normal' mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 {
922 settmode(TMODE_COOK);
923 stoptermcap();
924 }
925 out_char('\n');
926 if (raw_out)
927 {
928 if (term_console)
929 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100930 win_resize_off(); // window resize events de-activated
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (size_set)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100932 OUT_STR("\233t\233u"); // reset window size (CSI t CSI u)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
934 out_flush();
935 }
936
937#ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +0100938 mch_restore_title(SAVE_RESTORE_BOTH); // restore window title
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#endif
940
Bram Moolenaar0f873732019-12-05 20:28:46 +0100941 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943#ifdef FEAT_ARP
944 if (ArpBase)
945 CloseLibrary((struct Library *) ArpBase);
946#endif
947 if (close_win)
948 Close(raw_in);
949 if (r)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100950 printf(_("Vim exiting with %d\n"), r); // somehow this makes :cq work!?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 exit(r);
952}
953
954/*
955 * This is a routine for setting a given stream to raw or cooked mode on the
956 * Amiga . This is useful when you are using Lattice C to produce programs
957 * that want to read single characters with the "getch()" or "fgetc" call.
958 *
959 * Written : 18-Jun-87 By Chuck McManis.
960 */
961
962#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
963
964/*
965 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
966 * 'cooked' mode. This only works on TTY's.
967 *
968 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
969 * getch() will return immediately rather than wait for a return. You
970 * lose editing features though.
971 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100972 * Cooked: This function returns the designate file pointer to its normal,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 * wait for a <CR> mode. This is exactly like raw() except that
974 * it sends a 0 to the console to make it back into a CON: from a RAW:
975 */
976 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100977mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000979#if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
981#else
982 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
983 tmode == TMODE_RAW ? -1L : 0L) == 0)
984#endif
985 mch_errmsg(_("cannot change console mode ?!\n"));
986}
987
988/*
989 * set screen mode, always fails.
990 */
991 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100992mch_screenmode(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100994 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 return FAIL;
996}
997
998/*
999 * Code for this routine came from the following :
1000 *
1001 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
1002 * DOS packet example
1003 * Requires 1.2
1004 *
1005 * Found on Fish Disk 56.
1006 *
1007 * Heavely modified by mool.
1008 */
1009
Bram Moolenaar82881492012-11-20 16:53:39 +01001010#ifndef PROTO
1011# include <devices/conunit.h>
1012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014/*
1015 * try to get the real window size
1016 * return FAIL for failure, OK otherwise
1017 */
1018 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001019mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021 struct ConUnit *conUnit;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001022#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 char id_a[sizeof(struct InfoData) + 3];
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001024#endif
1025 struct InfoData *id=0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
Bram Moolenaar0f873732019-12-05 20:28:46 +01001027 if (!term_console) // not an amiga window
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001028 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
Bram Moolenaar0f873732019-12-05 20:28:46 +01001030 // insure longword alignment
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001031#ifdef __amigaos4__
Bram Moolenaar62dbdc42011-10-20 18:24:22 +02001032 if (!(id = AllocDosObject(DOS_INFODATA, 0)))
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001033 goto out;
1034#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
1038 /*
1039 * Should make console aware of real window size, not the one we set.
1040 * Unfortunately, under DOS 2.0x this redraws the window and it
1041 * is rarely needed, so we skip it now, unless we changed the size.
1042 */
1043 if (size_set)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001044 OUT_STR("\233t\233u"); // CSI t CSI u
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 out_flush();
1046
1047#ifdef __AROS__
1048 if (!Info(raw_out, id)
1049 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
1050#else
1051 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
1052 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
1053#endif
1054 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001055 // it's not an amiga window, maybe aux device
1056 // terminal type should be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 term_console = FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001058 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
1060 if (oldwindowtitle == NULL)
1061 oldwindowtitle = (char_u *)wb_window->Title;
1062 if (id->id_InUse == (BPTR)NULL)
1063 {
1064 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
1065 return FAIL;
1066 }
1067 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
1068
Bram Moolenaar0f873732019-12-05 20:28:46 +01001069 // get window size
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 Rows = conUnit->cu_YMax + 1;
1071 Columns = conUnit->cu_XMax + 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001072 if (Rows < 0 || Rows > 200) // cannot be an amiga window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
1074 Columns = 80;
1075 Rows = 24;
1076 term_console = FALSE;
1077 return FAIL;
1078 }
1079
1080 return OK;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001081out:
1082#ifdef __amigaos4__
Bram Moolenaar0f873732019-12-05 20:28:46 +01001083 FreeDosObject(DOS_INFODATA, id); // Safe to pass NULL
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001084#endif
1085
1086 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087}
1088
1089/*
1090 * Try to set the real window size to Rows and Columns.
1091 */
1092 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001093mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 if (term_console)
1096 {
1097 size_set = TRUE;
1098 out_char(CSI);
1099 out_num((long)Rows);
1100 out_char('t');
1101 out_char(CSI);
1102 out_num((long)Columns);
1103 out_char('u');
1104 out_flush();
1105 }
1106}
1107
1108/*
1109 * Rows and/or Columns has changed.
1110 */
1111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001112mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001114 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115}
1116
1117/*
1118 * out_num - output a (big) number fast
1119 */
1120 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001121out_num(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
1123 OUT_STR_NF(tltoa((unsigned long)n));
1124}
1125
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001126#if !defined(AZTEC_C) && !defined(__AROS__) && !defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127/*
1128 * Sendpacket.c
1129 *
1130 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1131 * the given message port. This makes working around DOS lots easier.
1132 *
1133 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1134 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1135 * say 'oml lib:amiga.lib -r sendpacket.o'
1136 */
1137
Bram Moolenaar82881492012-11-20 16:53:39 +01001138#ifndef PROTO
Bram Moolenaar0f873732019-12-05 20:28:46 +01001139// #include <proto/exec.h>
1140// #include <proto/dos.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01001141# include <exec/memory.h>
1142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
1144/*
1145 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1146 * Finkel. This function will send a packet of the given type to the Message
1147 * Port supplied.
1148 */
1149
1150 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01001151dos_packet(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001152 struct MsgPort *pid, // process identifier ... (handlers message port)
1153 long action, // packet type ... (what you want handler to do)
1154 long arg) // single argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155{
1156# ifdef FEAT_ARP
1157 struct MsgPort *replyport;
1158 struct StandardPacket *packet;
1159 long res1;
1160
1161 if (dos2)
1162# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001163 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); // use 2.0 function
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164# ifdef FEAT_ARP
1165
Bram Moolenaar0f873732019-12-05 20:28:46 +01001166 replyport = (struct MsgPort *) CreatePort(NULL, 0); // use arp function
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (!replyport)
1168 return (0);
1169
Bram Moolenaar0f873732019-12-05 20:28:46 +01001170 // Allocate space for a packet, make it public and clear it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 packet = (struct StandardPacket *)
1172 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
1173 if (!packet) {
1174 DeletePort(replyport);
1175 return (0);
1176 }
1177 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1178 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1179 packet->sp_Pkt.dp_Port = replyport;
1180 packet->sp_Pkt.dp_Type = action;
1181 packet->sp_Pkt.dp_Arg1 = arg;
1182
Bram Moolenaar0f873732019-12-05 20:28:46 +01001183 PutMsg(pid, (struct Message *)packet); // send packet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
1185 WaitPort(replyport);
1186 GetMsg(replyport);
1187
1188 res1 = packet->sp_Pkt.dp_Res1;
1189
1190 FreeMem(packet, (long) sizeof(struct StandardPacket));
1191 DeletePort(replyport);
1192
1193 return (res1);
1194# endif
1195}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001196#endif // !defined(AZTEC_C) && !defined(__AROS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
1198/*
1199 * Call shell.
1200 * Return error number for failure, 0 otherwise
1201 */
1202 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001203mch_call_shell(
1204 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01001205 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
1207 BPTR mydir;
1208 int x;
1209 int tmode = cur_tmode;
1210#ifdef AZTEC_C
1211 int use_execute;
1212 char_u *shellcmd = NULL;
1213 char_u *shellarg;
1214#endif
1215 int retval = 0;
1216
1217 if (close_win)
1218 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001219 // if Vim opened a window: Executing a shell may cause crashes
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001220 emsg(_("E360: Cannot execute shell with -f option"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 return -1;
1222 }
1223
1224 if (term_console)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001225 win_resize_off(); // window resize events de-activated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 out_flush();
1227
1228 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001229 settmode(TMODE_COOK); // set to normal mode
1230 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); // remember current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaar0f873732019-12-05 20:28:46 +01001232#if !defined(AZTEC_C) // not tested very much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (cmd == NULL)
1234 {
1235# ifdef FEAT_ARP
1236 if (dos2)
1237# endif
1238 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1239# ifdef FEAT_ARP
1240 else
1241 x = Execute(p_sh, raw_in, raw_out);
1242# endif
1243 }
1244 else
1245 {
1246# ifdef FEAT_ARP
1247 if (dos2)
1248# endif
1249 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1250# ifdef FEAT_ARP
1251 else
1252 x = Execute((char *)cmd, 0L, raw_out);
1253# endif
1254 }
1255# ifdef FEAT_ARP
1256 if ((dos2 && x < 0) || (!dos2 && !x))
1257# else
1258 if (x < 0)
1259# endif
1260 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001261 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (cmd == NULL)
1263 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001264 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 msg_outtrans(p_sh);
1266 }
1267 else
1268 msg_outtrans(cmd);
1269 msg_putchar('\n');
1270 retval = -1;
1271 }
1272# ifdef FEAT_ARP
1273 else if (!dos2 || x)
1274# else
1275 else if (x)
1276# endif
1277 {
1278 if ((x = IoErr()) != 0)
1279 {
1280 if (!(options & SHELL_SILENT))
1281 {
1282 msg_putchar('\n');
1283 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001284 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 retval = x;
1287 }
1288 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001289#else // else part is for AZTEC_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1291 use_execute = 1;
1292 else
1293 use_execute = 0;
1294 if (!use_execute)
1295 {
1296 /*
1297 * separate shell name from argument
1298 */
1299 shellcmd = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001300 if (shellcmd == NULL) // out of memory, use Execute
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 use_execute = 1;
1302 else
1303 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001304 shellarg = skiptowhite(shellcmd); // find start of arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (*shellarg != NUL)
1306 {
1307 *shellarg++ = NUL;
1308 shellarg = skipwhite(shellarg);
1309 }
1310 }
1311 }
1312 if (cmd == NULL)
1313 {
1314 if (use_execute)
1315 {
1316# ifdef FEAT_ARP
1317 if (dos2)
1318# endif
1319 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1320# ifdef FEAT_ARP
1321 else
1322 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1323# endif
1324 }
1325 else
1326 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1327 }
1328 else if (use_execute)
1329 {
1330# ifdef FEAT_ARP
1331 if (dos2)
1332# endif
1333 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1334# ifdef FEAT_ARP
1335 else
1336 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1337# endif
1338 }
1339 else if (p_st & 1)
1340 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1341 (char *)cmd, NULL);
1342 else
1343 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1344 (char *)p_shcf, (char *)cmd, NULL);
1345# ifdef FEAT_ARP
1346 if ((dos2 && x < 0) || (!dos2 && x))
1347# else
1348 if (x < 0)
1349# endif
1350 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001351 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (use_execute)
1353 {
1354 if (cmd == NULL)
1355 msg_outtrans(p_sh);
1356 else
1357 msg_outtrans(cmd);
1358 }
1359 else
1360 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001361 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 msg_outtrans(shellcmd);
1363 }
1364 msg_putchar('\n');
1365 retval = -1;
1366 }
1367 else
1368 {
1369 if (use_execute)
1370 {
1371# ifdef FEAT_ARP
1372 if (!dos2 || x)
1373# else
1374 if (x)
1375# endif
1376 x = IoErr();
1377 }
1378 else
1379 x = wait();
1380 if (x)
1381 {
1382 if (!(options & SHELL_SILENT) && !emsg_silent)
1383 {
1384 msg_putchar('\n');
1385 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001386 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 retval = x;
1389 }
1390 }
1391 vim_free(shellcmd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001392#endif // AZTEC_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaar0f873732019-12-05 20:28:46 +01001394 if ((mydir = CurrentDir(mydir)) != 0) // make sure we stay in the same directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 UnLock(mydir);
1396 if (tmode == TMODE_RAW)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001397 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_TITLE
1399 resettitle();
1400#endif
1401 if (term_console)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001402 win_resize_on(); // window resize events activated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 return retval;
1404}
1405
1406/*
1407 * check for an "interrupt signal"
1408 * We only react to a CTRL-C, but also clear the other break signals to avoid
1409 * trouble with lattice-c programs.
1410 */
1411 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02001412mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
1414 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1415 got_int = TRUE;
1416}
1417
Bram Moolenaar0f873732019-12-05 20:28:46 +01001418// this routine causes manx to use this Chk_Abort() rather than its own
1419// otherwise it resets our ^C when doing any I/O (even when Enable_Abort
1420// is zero). Since we want to check for our own ^C's
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
1422#ifdef _DCC
1423#define Chk_Abort chkabort
1424#endif
1425
1426#ifdef LATTICE
1427void __regargs __chkabort(void);
1428
1429void __regargs __chkabort(void)
1430{}
1431
1432#else
1433 long
1434Chk_Abort(void)
1435{
1436 return(0L);
1437}
1438#endif
1439
1440/*
1441 * mch_expandpath() - this code does wild-card pattern matching using the arp
1442 * routines.
1443 *
1444 * "pat" has backslashes before chars that are not to be expanded.
1445 * Returns the number of matches found.
1446 *
1447 * This is based on WildDemo2.c (found in arp1.1 distribution).
1448 * That code's copyright follows:
1449 * Copyright (c) 1987, Scott Ballantyne
1450 * Use and abuse as you please.
1451 */
1452
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001453#ifdef __amigaos4__
1454# define ANCHOR_BUF_SIZE 1024
1455#else
1456# define ANCHOR_BUF_SIZE (512)
1457# define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
1460 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001461mch_expandpath(
1462 garray_T *gap,
1463 char_u *pat,
Bram Moolenaar0f873732019-12-05 20:28:46 +01001464 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 struct AnchorPath *Anchor;
1467 LONG Result;
1468 char_u *starbuf, *sp, *dp;
1469 int start_len;
1470 int matches;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001471#ifdef __amigaos4__
1472 struct TagItem AnchorTags[] = {
1473 {ADO_Strlen, ANCHOR_BUF_SIZE},
1474 {ADO_Flags, APF_DODOT|APF_DOWILD|APF_MultiAssigns},
1475 {TAG_DONE, 0L}
1476 };
1477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
1479 start_len = gap->ga_len;
1480
Bram Moolenaar0f873732019-12-05 20:28:46 +01001481 // Get our AnchorBase
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001482#ifdef __amigaos4__
1483 Anchor = AllocDosObject(DOS_ANCHORPATH, AnchorTags);
1484#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001485 Anchor = alloc_clear(ANCHOR_SIZE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (Anchor == NULL)
1488 return 0;
1489
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001490#ifndef __amigaos4__
Bram Moolenaar0f873732019-12-05 20:28:46 +01001491 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; // ap_Length not supported anymore
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001492# ifdef APF_DODOT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001493 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; // allow '.' for current dir
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001494# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001495 Anchor->ap_Flags = APF_DoDot | APF_DoWild; // allow '.' for current dir
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497#endif
1498
1499#ifdef FEAT_ARP
1500 if (dos2)
1501 {
1502#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001503 // hack to replace '*' by '#?'
Bram Moolenaar964b3742019-05-24 18:54:09 +02001504 starbuf = alloc(2 * STRLEN(pat) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (starbuf == NULL)
1506 goto Return;
1507 for (sp = pat, dp = starbuf; *sp; ++sp)
1508 {
1509 if (*sp == '*')
1510 {
1511 *dp++ = '#';
1512 *dp++ = '?';
1513 }
1514 else
1515 *dp++ = *sp;
1516 }
1517 *dp = NUL;
1518 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1519 vim_free(starbuf);
1520#ifdef FEAT_ARP
1521 }
1522 else
1523 Result = FindFirst((char *)pat, Anchor);
1524#endif
1525
1526 /*
1527 * Loop to get all matches.
1528 */
1529 while (Result == 0)
1530 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001531#ifdef __amigaos4__
1532 addfile(gap, (char_u *)Anchor->ap_Buffer, flags);
1533#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#ifdef FEAT_ARP
1537 if (dos2)
1538#endif
1539 Result = MatchNext(Anchor);
1540#ifdef FEAT_ARP
1541 else
1542 Result = FindNext(Anchor);
1543#endif
1544 }
1545 matches = gap->ga_len - start_len;
1546
1547 if (Result == ERROR_BUFFER_OVERFLOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001548 emsg(_("ANCHOR_BUF_SIZE too small."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1550 && Result != ERROR_DEVICE_NOT_MOUNTED
1551 && Result != ERROR_NO_MORE_ENTRIES)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001552 emsg(_("I/O ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554 /*
1555 * Sort the files for this pattern.
1556 */
1557 if (matches)
1558 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1559 (size_t)matches, sizeof(char_u *), sortcmp);
1560
Bram Moolenaar0f873732019-12-05 20:28:46 +01001561 // Free the wildcard stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562#ifdef FEAT_ARP
1563 if (dos2)
1564#endif
1565 MatchEnd(Anchor);
1566#ifdef FEAT_ARP
1567 else
1568 FreeAnchorChain(Anchor);
1569#endif
1570
1571Return:
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001572#ifdef __amigaos4__
1573 FreeDosObject(DOS_ANCHORPATH, Anchor);
1574#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 vim_free(Anchor);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578 return matches;
1579}
1580
1581 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001582sortcmp(const void *a, const void *b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
1584 char *s = *(char **)a;
1585 char *t = *(char **)b;
1586
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001587 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588}
1589
1590/*
1591 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1592 */
1593 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001594mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001596 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
1598 if (*p == '\\' && p[1] != NUL)
1599 ++p;
1600 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1601 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 return FALSE;
1604}
1605
1606 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001607mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001609 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 if (*p == '\\' && p[1] != NUL)
1612 ++p;
1613 else
1614 if (vim_strchr((char_u *)
1615# ifdef VIM_BACKTICK
1616 "*?[(#$`"
1617# else
1618 "*?[(#$"
1619# endif
1620 , *p) != NULL
1621 || (*p == '~' && p[1] != NUL))
1622 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 }
1624 return FALSE;
1625}
1626
1627/*
1628 * With AmigaDOS 2.0 support for reading local environment variables
1629 *
1630 * Two buffers are allocated:
1631 * - A big one to do the expansion into. It is freed before returning.
1632 * - A small one to hold the return value. It is kept until the next call.
1633 */
1634 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001635mch_getenv(char_u *var)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 int len;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001638 UBYTE *buf; // buffer to expand in
1639 char_u *retval; // return value
1640 static char_u *alloced = NULL; // allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
1642#ifdef FEAT_ARP
1643 if (!dos2)
1644 retval = (char_u *)getenv((char *)var);
1645 else
1646#endif
1647 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001648 VIM_CLEAR(alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 retval = NULL;
1650
1651 buf = alloc(IOSIZE);
1652 if (buf == NULL)
1653 return NULL;
1654
1655 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1656 if (len >= 0)
1657 {
1658 retval = vim_strsave((char_u *)buf);
1659 alloced = retval;
1660 }
1661
1662 vim_free(buf);
1663 }
1664
Bram Moolenaar0f873732019-12-05 20:28:46 +01001665 // if $VIM is not defined, use "vim:" instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (retval == NULL && STRCMP(var, "VIM") == 0)
1667 retval = (char_u *)"vim:";
1668
1669 return retval;
1670}
1671
1672/*
1673 * Amiga version of setenv() with AmigaDOS 2.0 support.
1674 */
Bram Moolenaar0f873732019-12-05 20:28:46 +01001675// ARGSUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001677mch_setenv(char *var, char *value, int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679#ifdef FEAT_ARP
1680 if (!dos2)
1681 return setenv(var, value);
1682#endif
1683
1684 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001685 return 0; // success
1686 return -1; // failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687}