blob: 58e462d5cbeaa2e4e2128c34d6a7948faa82e560 [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 Moolenaardba7c852019-12-30 22:33:17 +010095/* Use autoopen for AmigaOS4, AROS and MorphOS */
96#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097struct IntuitionBase *IntuitionBase = NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#ifdef FEAT_ARP
100struct ArpBase *ArpBase = NULL;
101#endif
102
103static struct Window *wb_window;
104static char_u *oldwindowtitle = NULL;
105
106#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100107int dos2 = FALSE; // Amiga DOS 2.0x or higher
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100109int size_set = FALSE; // set to TRUE if window size was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
Bram Moolenaar70576f72019-07-31 20:40:08 +0200111#ifdef __GNUC__
112static char version[] __attribute__((used)) =
113 "\0$VER: Vim "
114 VIM_VERSION_MAJOR_STR "."
115 VIM_VERSION_MINOR_STR
116# ifdef PATCHLEVEL
117 "." PATCHLEVEL
118# endif
119 ;
120#endif
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100123win_resize_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
125 OUT_STR_NF("\033[12{");
126}
127
128 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100129win_resize_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130{
131 OUT_STR_NF("\033[12}");
132}
133
134 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100135mch_write(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136{
137 Write(raw_out, (char *)p, (long)len);
138}
139
140/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200141 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 * Get a characters from the keyboard.
143 * If time == 0 do not wait for characters.
144 * If time == n wait a short time for characters.
145 * If time == -1 wait forever for characters.
146 *
147 * Return number of characters read.
148 */
149 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100150mch_inchar(
151 char_u *buf,
152 int maxlen,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100153 long time, // milli seconds
Bram Moolenaar05540972016-01-30 20:31:25 +0100154 int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155{
156 int len;
157 long utime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
159 if (time >= 0)
160 {
161 if (time == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100162 utime = 100L; // time = 0 causes problems in DOS 1.2
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100164 utime = time * 1000L; // convert from milli to micro secs
165 if (WaitForChar(raw_in, utime) == 0) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100168 else // time == -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 /*
171 * If there is no character available within 2 seconds (default)
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000172 * write the autoscript file to disk. Or cause the CursorHold event
173 * to be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 */
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000175 if (WaitForChar(raw_in, p_ut * 1000L) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000177 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 {
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000179 buf[0] = K_SPECIAL;
180 buf[1] = KS_EXTRA;
181 buf[2] = (int)KE_CURSORHOLD;
182 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000184 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 }
186 }
187
Bram Moolenaar0f873732019-12-05 20:28:46 +0100188 for (;;) // repeat until we got a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 len = Read(raw_in, (char *)buf, (long)maxlen / input_conv.vc_factor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 if (len > 0)
192 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100193 // Convert from 'termencoding' to 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 if (input_conv.vc_type != CONV_NONE)
195 len = convert_input(buf, len, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 return len;
197 }
198 }
199}
200
201/*
202 * return non-zero if a character is available
203 */
204 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100205mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206{
207 return (WaitForChar(raw_in, 100L) != 0);
208}
209
210/*
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200211 * Return amount of memory still available in Kbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 */
213 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100214mch_avail_mem(int special)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
Bram Moolenaara9ab3912019-08-10 14:54:20 +0200216#if defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200217 return (long_u)AvailMem(MEMF_ANY) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000218#else
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200219 return (long_u)(AvailMem(special ? (long)MEMF_CHIP : (long)MEMF_ANY)) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221}
222
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000223/*
224 * Waits a specified amount of time, or until input arrives if
225 * ignoreinput is FALSE.
226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100228mch_delay(long msec, int ignoreinput)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100230#ifndef LATTICE // SAS declares void Delay(ULONG)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100231 void Delay(long);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif
233
234 if (msec > 0)
235 {
236 if (ignoreinput)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100237 Delay(msec / 20L); // Delay works with 20 msec intervals
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 else
239 WaitForChar(raw_in, msec * 1000L);
240 }
241}
242
243/*
244 * We have no job control, fake it by starting a new shell.
245 */
246 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100247mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 suspend_shell();
250}
251
252#ifndef DOS_LIBRARY
253# define DOS_LIBRARY ((UBYTE *)"dos.library")
254#endif
255
256 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100257mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
Bram Moolenaardba7c852019-12-30 22:33:17 +0100259#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 static char intlibname[] = "intuition.library";
Bram Moolenaardba7c852019-12-30 22:33:17 +0100261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263#ifdef AZTEC_C
Bram Moolenaar0f873732019-12-05 20:28:46 +0100264 Enable_Abort = 0; // disallow vim to be aborted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#endif
266 Columns = 80;
267 Rows = 24;
268
269 /*
270 * Set input and output channels, unless we have opened our own window
271 */
272 if (raw_in == (BPTR)NULL)
273 {
274 raw_in = Input();
275 raw_out = Output();
276 /*
277 * If Input() is not interactive, then Output() will be (because of
278 * check in mch_check_win()). Used for "Vim -".
279 * Also check the other way around, for "Vim -h | more".
280 */
281 if (!IsInteractive(raw_in))
282 raw_in = raw_out;
283 else if (!IsInteractive(raw_out))
284 raw_out = raw_in;
285 }
286
287 out_flush();
288
289 wb_window = NULL;
Bram Moolenaardba7c852019-12-30 22:33:17 +0100290#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if ((IntuitionBase = (struct IntuitionBase *)
292 OpenLibrary((UBYTE *)intlibname, 0L)) == NULL)
293 {
294 mch_errmsg(_("cannot open "));
295 mch_errmsg(intlibname);
296 mch_errmsg("!?\n");
297 mch_exit(3);
298 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300}
301
Bram Moolenaar82881492012-11-20 16:53:39 +0100302#ifndef PROTO
303# include <workbench/startup.h>
304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
306/*
307 * Check_win checks whether we have an interactive window.
308 * If not, a new window is opened with the newcli command.
309 * If we would open a window ourselves, the :sh and :! commands would not
310 * work properly (Why? probably because we are then running in a background
311 * CLI). This also is the best way to assure proper working in a next
312 * Workbench release.
313 *
314 * For the -f option (foreground mode) we open our own window and disable :sh.
315 * Otherwise the calling program would never know when editing is finished.
316 */
Bram Moolenaar0f873732019-12-05 20:28:46 +0100317#define BUF2SIZE 320 // length of buffer for argument with complete path
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318
319 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100320mch_check_win(int argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321{
322 int i;
323 BPTR nilfh, fh;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000324 char_u buf1[24];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 char_u buf2[BUF2SIZE];
326 static char_u *(constrings[3]) = {(char_u *)"con:0/0/662/210/",
327 (char_u *)"con:0/0/640/200/",
328 (char_u *)"con:0/0/320/200/"};
329 static char_u *winerr = (char_u *)N_("VIM: Can't open window!\n");
330 struct WBArg *argp;
331 int ac;
332 char *av;
333 char_u *device = NULL;
334 int exitval = 4;
Bram Moolenaardba7c852019-12-30 22:33:17 +0100335#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 struct Library *DosBase;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 int usewin = FALSE;
339
340/*
341 * check if we are running under DOS 2.0x or higher
342 */
Bram Moolenaardba7c852019-12-30 22:33:17 +0100343#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 DosBase = OpenLibrary(DOS_LIBRARY, 37L);
345 if (DosBase != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100346 // if (((struct Library *)DOSBase)->lib_Version >= 37)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 {
348 CloseLibrary(DosBase);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000349# ifdef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 dos2 = TRUE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100353 else // without arp functions we NEED 2.0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000355# ifndef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 mch_errmsg(_("Need Amigados version 2.04 or later\n"));
357 exit(3);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000358# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100359 // need arp functions for dos 1.x
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 if (!(ArpBase = (struct ArpBase *) OpenLibrary((UBYTE *)ArpName, ArpVersion)))
361 {
362 fprintf(stderr, _("Need %s version %ld\n"), ArpName, ArpVersion);
363 exit(3);
364 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 }
Bram Moolenaardba7c852019-12-30 22:33:17 +0100367#endif /* __amigaos4__ __AROS__ __MORPHOS__ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
369 /*
370 * scan argv[] for the "-f" and "-d" arguments
371 */
372 for (i = 1; i < argc; ++i)
373 if (argv[i][0] == '-')
374 {
375 switch (argv[i][1])
376 {
377 case 'f':
378 usewin = TRUE;
379 break;
380
381 case 'd':
382 if (i < argc - 1
383#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +0100384 // require using "-dev", "-d" means diff mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 && argv[i][2] == 'e' && argv[i][3] == 'v'
386#endif
387 )
388 device = (char_u *)argv[i + 1];
389 break;
390 }
391 }
392
393/*
394 * If we were not started from workbench, do not have a "-d" or "-dev"
395 * argument and we have been started with an interactive window, use that
396 * window.
397 */
398 if (argc != 0
399 && device == NULL
400 && (IsInteractive(Input()) || IsInteractive(Output())))
401 return OK;
402
403/*
404 * When given the "-f" argument, we open our own window. We can't use the
405 * newcli trick below, because the calling program (mail, rn, etc.) would not
406 * know when we are finished.
407 */
408 if (usewin)
409 {
410 /*
411 * Try to open a window. First try the specified device.
412 * Then try a 24 line 80 column window.
413 * If that fails, try two smaller ones.
414 */
415 for (i = -1; i < 3; ++i)
416 {
417 if (i >= 0)
418 device = constrings[i];
419 if (device != NULL && (raw_in = Open((UBYTE *)device,
420 (long)MODE_NEWFILE)) != (BPTR)NULL)
421 break;
422 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100423 if (raw_in == (BPTR)NULL) // all three failed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {
425 mch_errmsg(_(winerr));
426 goto exit;
427 }
428 raw_out = raw_in;
429 close_win = TRUE;
430 return OK;
431 }
432
433 if ((nilfh = Open((UBYTE *)"NIL:", (long)MODE_NEWFILE)) == (BPTR)NULL)
434 {
435 mch_errmsg(_("Cannot open NIL:\n"));
436 goto exit;
437 }
438
439 /*
440 * Make a unique name for the temp file (which we will not delete!).
441 * Use a pointer on the stack (nobody else will be using it).
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000442 * Under AmigaOS4, this assumption might change in the future, so
443 * we use a pointer to the current task instead. This should be a
444 * shared structure and thus globally unique.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 */
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000446#ifdef __amigaos4__
447 sprintf((char *)buf1, "t:nc%p", FindTask(0));
448#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 sprintf((char *)buf1, "t:nc%ld", (long)buf1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 if ((fh = Open((UBYTE *)buf1, (long)MODE_NEWFILE)) == (BPTR)NULL)
452 {
453 mch_errmsg(_("Cannot create "));
454 mch_errmsg((char *)buf1);
455 mch_errmsg("\n");
456 goto exit;
457 }
458 /*
459 * Write the command into the file, put quotes around the arguments that
460 * have a space in them.
461 */
Bram Moolenaar0f873732019-12-05 20:28:46 +0100462 if (argc == 0) // run from workbench
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 ac = ((struct WBStartup *)argv)->sm_NumArgs;
464 else
465 ac = argc;
466 for (i = 0; i < ac; ++i)
467 {
468 if (argc == 0)
469 {
470 *buf2 = NUL;
471 argp = &(((struct WBStartup *)argv)->sm_ArgList[i]);
472 if (argp->wa_Lock)
473 (void)lock2name(argp->wa_Lock, buf2, (long)(BUF2SIZE - 1));
474#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100475 if (dos2) // use 2.0 function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476#endif
477 AddPart((UBYTE *)buf2, (UBYTE *)argp->wa_Name, (long)(BUF2SIZE - 1));
478#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100479 else // use arp function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 TackOn((char *)buf2, argp->wa_Name);
481#endif
482 av = (char *)buf2;
483 }
484 else
485 av = argv[i];
486
Bram Moolenaar0f873732019-12-05 20:28:46 +0100487 // skip '-d' or "-dev" option
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 if (av[0] == '-' && av[1] == 'd'
489#ifdef FEAT_DIFF
490 && av[2] == 'e' && av[3] == 'v'
491#endif
492 )
493 {
494 ++i;
495 continue;
496 }
497 if (vim_strchr((char_u *)av, ' '))
498 Write(fh, "\"", 1L);
499 Write(fh, av, (long)strlen(av));
500 if (vim_strchr((char_u *)av, ' '))
501 Write(fh, "\"", 1L);
502 Write(fh, " ", 1L);
503 }
504 Write(fh, "\nendcli\n", 8L);
505 Close(fh);
506
507/*
508 * Try to open a new cli in a window. If "-d" or "-dev" argument was given try
509 * to open the specified device. Then try a 24 line 80 column window. If that
510 * fails, try two smaller ones.
511 */
512 for (i = -1; i < 3; ++i)
513 {
514 if (i >= 0)
515 device = constrings[i];
516 else if (device == NULL)
517 continue;
518 sprintf((char *)buf2, "newcli <nil: >nil: %s from %s", (char *)device, (char *)buf1);
519#ifdef FEAT_ARP
520 if (dos2)
521 {
522#endif
523 if (!SystemTags((UBYTE *)buf2, SYS_UserShell, TRUE, TAG_DONE))
524 break;
525#ifdef FEAT_ARP
526 }
527 else
528 {
529 if (Execute((UBYTE *)buf2, nilfh, nilfh))
530 break;
531 }
532#endif
533 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100534 if (i == 3) // all three failed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 {
536 DeleteFile((UBYTE *)buf1);
537 mch_errmsg(_(winerr));
538 goto exit;
539 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100540 exitval = 0; // The Execute succeeded: exit this program
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542exit:
543#ifdef FEAT_ARP
544 if (ArpBase)
545 CloseLibrary((struct Library *) ArpBase);
546#endif
547 exit(exitval);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100548 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 return FAIL;
550}
551
552/*
553 * Return TRUE if the input comes from a terminal, FALSE otherwise.
554 * We fake there is a window, because we can always open one!
555 */
556 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100557mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
559 return TRUE;
560}
561
562/*
563 * fname_case(): Set the case of the file name, if it already exists.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000564 * This will cause the file name to remain exactly the same
565 * if the file system ignores, but preserves case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 */
Bram Moolenaar0f873732019-12-05 20:28:46 +0100567//ARGSUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100569fname_case(
570 char_u *name,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100571 int len) // buffer size, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572{
573 struct FileInfoBlock *fib;
574 size_t flen;
575
576 fib = get_fib(name);
577 if (fib != NULL)
578 {
579 flen = STRLEN(name);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100580 // TODO: Check if this fix applies to AmigaOS < 4 too.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000581#ifdef __amigaos4__
582 if (fib->fib_DirEntryType == ST_ROOT)
583 strcat(fib->fib_FileName, ":");
584#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100585 if (flen == strlen(fib->fib_FileName)) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 mch_memmove(name, fib->fib_FileName, flen);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000587 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
589}
590
591/*
592 * Get the FileInfoBlock for file "fname"
593 * The returned structure has to be free()d.
594 * Returns NULL on error.
595 */
596 static struct FileInfoBlock *
Bram Moolenaar05540972016-01-30 20:31:25 +0100597get_fib(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 BPTR flock;
600 struct FileInfoBlock *fib;
601
Bram Moolenaar0f873732019-12-05 20:28:46 +0100602 if (fname == NULL) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 return NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000604#ifdef __amigaos4__
605 fib = AllocDosObject(DOS_FIB,0);
606#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200607 fib = ALLOC_ONE(struct FileInfoBlock);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 if (fib != NULL)
610 {
611 flock = Lock((UBYTE *)fname, (long)ACCESS_READ);
612 if (flock == (BPTR)NULL || !Examine(flock, fib))
613 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100614 free_fib(fib); // in case of an error the memory is freed here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 fib = NULL;
616 }
617 if (flock)
618 UnLock(flock);
619 }
620 return fib;
621}
622
623#ifdef FEAT_TITLE
624/*
625 * set the title of our window
626 * icon name is not set
627 */
628 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100629mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
631 if (wb_window != NULL && title != NULL)
632 SetWindowTitles(wb_window, (UBYTE *)title, (UBYTE *)-1L);
633}
634
635/*
636 * Restore the window/icon title.
637 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200638 * SAVE_RESTORE_TITLE Just restore title
639 * SAVE_RESTORE_ICON Just restore icon (which we don't have)
640 * SAVE_RESTORE_BOTH Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 */
642 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100643mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
Bram Moolenaar40385db2018-08-07 22:31:44 +0200645 if (which & SAVE_RESTORE_TITLE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 mch_settitle(oldwindowtitle, NULL);
647}
648
649 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100650mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 return (wb_window != NULL);
653}
654
655 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100656mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
658 return FALSE;
659}
660#endif
661
Bram Moolenaardfded982019-10-26 21:33:19 +0200662 void
663mch_setmouse(int on UNUSED)
664{
665 // TODO: implement
666}
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668/*
669 * Insert user name in s[len].
670 */
671 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100672mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar5e8e9672019-09-27 13:38:56 +0200674#if defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__)
675 struct passwd *pwd = getpwuid(getuid());
676
677 if (pwd != NULL && pwd->pw_name && len > 0)
678 {
679 vim_strncpy(s, (char_u *)pwd->pw_name, len - 1);
680 return OK;
681 }
682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 *s = NUL;
684 return FAIL;
685}
686
687/*
688 * Insert host name is s[len].
689 */
690 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100691mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000693#if defined(__amigaos4__) && defined(__CLIB2__)
694 gethostname(s, len);
695#else
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000696 vim_strncpy(s, "Amiga", len - 1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698}
699
700/*
701 * return process ID
702 */
703 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100704mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000706#ifdef __amigaos4__
Bram Moolenaar0f873732019-12-05 20:28:46 +0100707 // This is as close to a pid as we can come. We could use CLI numbers also,
708 // but then we would have two different types of process identifiers.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000709 return((long)FindTask(0));
710#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return (long)0;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713}
714
715/*
716 * Get name of current directory into buffer 'buf' of length 'len' bytes.
717 * Return OK for success, FAIL for failure.
718 */
719 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100720mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721{
722 return mch_FullName((char_u *)"", buf, len, FALSE);
723}
724
725/*
726 * get absolute file name into buffer 'buf' of length 'len' bytes
727 *
728 * return FAIL for failure, OK otherwise
729 */
730 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100731mch_FullName(
732 char_u *fname,
733 char_u *buf,
734 int len,
735 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 BPTR l;
738 int retval = FAIL;
739 int i;
740
Bram Moolenaar0f873732019-12-05 20:28:46 +0100741 // Lock the file. If it exists, we can get the exact name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
743 {
744 retval = lock2name(l, buf, (long)len - 1);
745 UnLock(l);
746 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100747 else if (force || !mch_isFullName(fname)) // not a full path yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
749 /*
750 * If the file cannot be locked (doesn't exist), try to lock the
751 * current directory and concatenate the file name.
752 */
753 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
754 {
755 retval = lock2name(l, buf, (long)len);
756 UnLock(l);
757 if (retval == OK)
758 {
759 i = STRLEN(buf);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100760 // Concatenate the fname to the directory. Don't add a slash
761 // if fname is empty, but do change "" to "/".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if (i == 0 || *fname != NUL)
763 {
764 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
765 buf[i++] = '/';
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000766 vim_strncpy(buf + i, fname, len - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768 }
769 }
770 }
771 if (*buf == 0 || *buf == ':')
Bram Moolenaar0f873732019-12-05 20:28:46 +0100772 retval = FAIL; // something failed; use the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 return retval;
774}
775
776/*
777 * Return TRUE if "fname" does not depend on the current directory.
778 */
779 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100780mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 return (vim_strchr(fname, ':') != NULL && *fname != ':');
783}
784
785/*
786 * Get the full file name from a lock. Use 2.0 function if possible, because
787 * the arp function has more restrictions on the path length.
788 *
789 * return FAIL for failure, OK otherwise
790 */
791 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100792lock2name(BPTR lock, char_u *buf, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793{
794#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100795 if (dos2) // use 2.0 function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796#endif
797 return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL);
798#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100799 else // use arp function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL);
801#endif
802}
803
804/*
805 * get file permissions for 'name'
806 * Returns -1 when it doesn't exist.
807 */
808 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100809mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 struct FileInfoBlock *fib;
812 long retval = -1;
813
814 fib = get_fib(name);
815 if (fib != NULL)
816 {
817 retval = fib->fib_Protection;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000818 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 }
820 return retval;
821}
822
823/*
824 * set file permission for 'name' to 'perm'
825 *
826 * return FAIL for failure, OK otherwise
827 */
828 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100829mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100831 perm &= ~FIBF_ARCHIVE; // reset archived bit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
833}
834
835/*
836 * Set hidden flag for "name".
837 */
838 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100839mch_hide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100841 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/*
845 * return FALSE if "name" is not a directory
846 * return TRUE if "name" is a directory.
847 * return FALSE for error.
848 */
849 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100850mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
852 struct FileInfoBlock *fib;
853 int retval = FALSE;
854
855 fib = get_fib(name);
856 if (fib != NULL)
857 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000858#ifdef __amigaos4__
859 retval = (FIB_IS_DRAWER(fib)) ? TRUE : FALSE;
860#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000862#endif
863 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865 return retval;
866}
867
868/*
869 * Create directory "name".
870 */
Bram Moolenaare3853642006-09-14 19:36:57 +0000871 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100872mch_mkdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873{
874 BPTR lock;
875
876 lock = CreateDir(name);
877 if (lock != NULL)
Bram Moolenaare3853642006-09-14 19:36:57 +0000878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 UnLock(lock);
Bram Moolenaare3853642006-09-14 19:36:57 +0000880 return 0;
881 }
882 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883}
884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885/*
886 * Return 1 if "name" can be executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +0100887 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * Return -1 if unknown.
889 */
890 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100891mch_can_exe(char_u *name, char_u **path, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100893 // TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 return -1;
895}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897/*
898 * Check what "name" is:
899 * NODE_NORMAL: file or directory (or doesn't exist)
900 * NODE_WRITABLE: writable device, socket, fifo, etc.
901 * NODE_OTHER: non-writable things
902 */
903 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100904mch_nodetype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100906 // TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 return NODE_NORMAL;
908}
909
910 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100911mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
913}
914
915/*
916 * Careful: mch_exit() may be called before mch_init()!
917 */
918 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100919mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100921 exiting = TRUE;
922
Bram Moolenaar0f873732019-12-05 20:28:46 +0100923 if (raw_in) // put terminal in 'normal' mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 {
925 settmode(TMODE_COOK);
926 stoptermcap();
927 }
928 out_char('\n');
929 if (raw_out)
930 {
931 if (term_console)
932 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100933 win_resize_off(); // window resize events de-activated
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (size_set)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100935 OUT_STR("\233t\233u"); // reset window size (CSI t CSI u)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937 out_flush();
938 }
939
940#ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +0100941 mch_restore_title(SAVE_RESTORE_BOTH); // restore window title
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942#endif
943
Bram Moolenaar0f873732019-12-05 20:28:46 +0100944 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946#ifdef FEAT_ARP
947 if (ArpBase)
948 CloseLibrary((struct Library *) ArpBase);
949#endif
950 if (close_win)
951 Close(raw_in);
952 if (r)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100953 printf(_("Vim exiting with %d\n"), r); // somehow this makes :cq work!?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 exit(r);
955}
956
957/*
958 * This is a routine for setting a given stream to raw or cooked mode on the
959 * Amiga . This is useful when you are using Lattice C to produce programs
960 * that want to read single characters with the "getch()" or "fgetc" call.
961 *
962 * Written : 18-Jun-87 By Chuck McManis.
963 */
964
965#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
966
967/*
968 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
969 * 'cooked' mode. This only works on TTY's.
970 *
971 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
972 * getch() will return immediately rather than wait for a return. You
973 * lose editing features though.
974 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100975 * Cooked: This function returns the designate file pointer to its normal,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * wait for a <CR> mode. This is exactly like raw() except that
977 * it sends a 0 to the console to make it back into a CON: from a RAW:
978 */
979 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100980mch_settmode(int tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981{
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000982#if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
984#else
985 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
986 tmode == TMODE_RAW ? -1L : 0L) == 0)
987#endif
988 mch_errmsg(_("cannot change console mode ?!\n"));
989}
990
991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 * Code for this routine came from the following :
993 *
994 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
995 * DOS packet example
996 * Requires 1.2
997 *
998 * Found on Fish Disk 56.
999 *
1000 * Heavely modified by mool.
1001 */
1002
Bram Moolenaar82881492012-11-20 16:53:39 +01001003#ifndef PROTO
1004# include <devices/conunit.h>
1005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007/*
1008 * try to get the real window size
1009 * return FAIL for failure, OK otherwise
1010 */
1011 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001012mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
1014 struct ConUnit *conUnit;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001015#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 char id_a[sizeof(struct InfoData) + 3];
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001017#endif
1018 struct InfoData *id=0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
Bram Moolenaar0f873732019-12-05 20:28:46 +01001020 if (!term_console) // not an amiga window
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001021 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
Bram Moolenaar0f873732019-12-05 20:28:46 +01001023 // insure longword alignment
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001024#ifdef __amigaos4__
Bram Moolenaar62dbdc42011-10-20 18:24:22 +02001025 if (!(id = AllocDosObject(DOS_INFODATA, 0)))
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001026 goto out;
1027#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
1031 /*
1032 * Should make console aware of real window size, not the one we set.
1033 * Unfortunately, under DOS 2.0x this redraws the window and it
1034 * is rarely needed, so we skip it now, unless we changed the size.
1035 */
1036 if (size_set)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001037 OUT_STR("\233t\233u"); // CSI t CSI u
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 out_flush();
1039
1040#ifdef __AROS__
1041 if (!Info(raw_out, id)
1042 || (wb_window = (struct Window *) id->id_VolumeNode) == NULL)
1043#else
1044 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
1045 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
1046#endif
1047 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001048 // it's not an amiga window, maybe aux device
1049 // terminal type should be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 term_console = FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001051 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 }
1053 if (oldwindowtitle == NULL)
1054 oldwindowtitle = (char_u *)wb_window->Title;
1055 if (id->id_InUse == (BPTR)NULL)
1056 {
1057 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
1058 return FAIL;
1059 }
1060 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
1061
Bram Moolenaar0f873732019-12-05 20:28:46 +01001062 // get window size
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 Rows = conUnit->cu_YMax + 1;
1064 Columns = conUnit->cu_XMax + 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001065 if (Rows < 0 || Rows > 200) // cannot be an amiga window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
1067 Columns = 80;
1068 Rows = 24;
1069 term_console = FALSE;
1070 return FAIL;
1071 }
1072
1073 return OK;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001074out:
1075#ifdef __amigaos4__
Bram Moolenaar0f873732019-12-05 20:28:46 +01001076 FreeDosObject(DOS_INFODATA, id); // Safe to pass NULL
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001077#endif
1078
1079 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/*
1083 * Try to set the real window size to Rows and Columns.
1084 */
1085 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001086mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
1088 if (term_console)
1089 {
1090 size_set = TRUE;
1091 out_char(CSI);
1092 out_num((long)Rows);
1093 out_char('t');
1094 out_char(CSI);
1095 out_num((long)Columns);
1096 out_char('u');
1097 out_flush();
1098 }
1099}
1100
1101/*
1102 * Rows and/or Columns has changed.
1103 */
1104 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001105mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001107 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108}
1109
1110/*
1111 * out_num - output a (big) number fast
1112 */
1113 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001114out_num(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 OUT_STR_NF(tltoa((unsigned long)n));
1117}
1118
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001119#if !defined(AZTEC_C) && !defined(__AROS__) && !defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120/*
1121 * Sendpacket.c
1122 *
1123 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1124 * the given message port. This makes working around DOS lots easier.
1125 *
1126 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1127 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1128 * say 'oml lib:amiga.lib -r sendpacket.o'
1129 */
1130
Bram Moolenaar82881492012-11-20 16:53:39 +01001131#ifndef PROTO
Bram Moolenaar0f873732019-12-05 20:28:46 +01001132// #include <proto/exec.h>
1133// #include <proto/dos.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01001134# include <exec/memory.h>
1135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137/*
1138 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1139 * Finkel. This function will send a packet of the given type to the Message
1140 * Port supplied.
1141 */
1142
1143 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01001144dos_packet(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001145 struct MsgPort *pid, // process identifier ... (handlers message port)
1146 long action, // packet type ... (what you want handler to do)
1147 long arg) // single argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148{
1149# ifdef FEAT_ARP
1150 struct MsgPort *replyport;
1151 struct StandardPacket *packet;
1152 long res1;
1153
1154 if (dos2)
1155# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001156 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); // use 2.0 function
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157# ifdef FEAT_ARP
1158
Bram Moolenaar0f873732019-12-05 20:28:46 +01001159 replyport = (struct MsgPort *) CreatePort(NULL, 0); // use arp function
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (!replyport)
1161 return (0);
1162
Bram Moolenaar0f873732019-12-05 20:28:46 +01001163 // Allocate space for a packet, make it public and clear it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 packet = (struct StandardPacket *)
1165 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
1166 if (!packet) {
1167 DeletePort(replyport);
1168 return (0);
1169 }
1170 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1171 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1172 packet->sp_Pkt.dp_Port = replyport;
1173 packet->sp_Pkt.dp_Type = action;
1174 packet->sp_Pkt.dp_Arg1 = arg;
1175
Bram Moolenaar0f873732019-12-05 20:28:46 +01001176 PutMsg(pid, (struct Message *)packet); // send packet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 WaitPort(replyport);
1179 GetMsg(replyport);
1180
1181 res1 = packet->sp_Pkt.dp_Res1;
1182
1183 FreeMem(packet, (long) sizeof(struct StandardPacket));
1184 DeletePort(replyport);
1185
1186 return (res1);
1187# endif
1188}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001189#endif // !defined(AZTEC_C) && !defined(__AROS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
1191/*
1192 * Call shell.
1193 * Return error number for failure, 0 otherwise
1194 */
1195 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001196mch_call_shell(
1197 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01001198 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
1200 BPTR mydir;
1201 int x;
1202 int tmode = cur_tmode;
1203#ifdef AZTEC_C
1204 int use_execute;
1205 char_u *shellcmd = NULL;
1206 char_u *shellarg;
1207#endif
1208 int retval = 0;
1209
1210 if (close_win)
1211 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001212 // if Vim opened a window: Executing a shell may cause crashes
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001213 emsg(_("E360: Cannot execute shell with -f option"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 return -1;
1215 }
1216
1217 if (term_console)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001218 win_resize_off(); // window resize events de-activated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 out_flush();
1220
1221 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001222 settmode(TMODE_COOK); // set to normal mode
1223 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); // remember current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar0f873732019-12-05 20:28:46 +01001225#if !defined(AZTEC_C) // not tested very much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (cmd == NULL)
1227 {
1228# ifdef FEAT_ARP
1229 if (dos2)
1230# endif
1231 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1232# ifdef FEAT_ARP
1233 else
1234 x = Execute(p_sh, raw_in, raw_out);
1235# endif
1236 }
1237 else
1238 {
1239# ifdef FEAT_ARP
1240 if (dos2)
1241# endif
1242 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1243# ifdef FEAT_ARP
1244 else
1245 x = Execute((char *)cmd, 0L, raw_out);
1246# endif
1247 }
1248# ifdef FEAT_ARP
1249 if ((dos2 && x < 0) || (!dos2 && !x))
1250# else
1251 if (x < 0)
1252# endif
1253 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001254 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (cmd == NULL)
1256 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001257 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 msg_outtrans(p_sh);
1259 }
1260 else
1261 msg_outtrans(cmd);
1262 msg_putchar('\n');
1263 retval = -1;
1264 }
1265# ifdef FEAT_ARP
1266 else if (!dos2 || x)
1267# else
1268 else if (x)
1269# endif
1270 {
1271 if ((x = IoErr()) != 0)
1272 {
1273 if (!(options & SHELL_SILENT))
1274 {
1275 msg_putchar('\n');
1276 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001277 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 retval = x;
1280 }
1281 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001282#else // else part is for AZTEC_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1284 use_execute = 1;
1285 else
1286 use_execute = 0;
1287 if (!use_execute)
1288 {
1289 /*
1290 * separate shell name from argument
1291 */
1292 shellcmd = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001293 if (shellcmd == NULL) // out of memory, use Execute
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 use_execute = 1;
1295 else
1296 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001297 shellarg = skiptowhite(shellcmd); // find start of arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (*shellarg != NUL)
1299 {
1300 *shellarg++ = NUL;
1301 shellarg = skipwhite(shellarg);
1302 }
1303 }
1304 }
1305 if (cmd == NULL)
1306 {
1307 if (use_execute)
1308 {
1309# ifdef FEAT_ARP
1310 if (dos2)
1311# endif
1312 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1313# ifdef FEAT_ARP
1314 else
1315 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1316# endif
1317 }
1318 else
1319 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1320 }
1321 else if (use_execute)
1322 {
1323# ifdef FEAT_ARP
1324 if (dos2)
1325# endif
1326 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1327# ifdef FEAT_ARP
1328 else
1329 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1330# endif
1331 }
1332 else if (p_st & 1)
1333 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1334 (char *)cmd, NULL);
1335 else
1336 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1337 (char *)p_shcf, (char *)cmd, NULL);
1338# ifdef FEAT_ARP
1339 if ((dos2 && x < 0) || (!dos2 && x))
1340# else
1341 if (x < 0)
1342# endif
1343 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001344 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (use_execute)
1346 {
1347 if (cmd == NULL)
1348 msg_outtrans(p_sh);
1349 else
1350 msg_outtrans(cmd);
1351 }
1352 else
1353 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001354 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 msg_outtrans(shellcmd);
1356 }
1357 msg_putchar('\n');
1358 retval = -1;
1359 }
1360 else
1361 {
1362 if (use_execute)
1363 {
1364# ifdef FEAT_ARP
1365 if (!dos2 || x)
1366# else
1367 if (x)
1368# endif
1369 x = IoErr();
1370 }
1371 else
1372 x = wait();
1373 if (x)
1374 {
1375 if (!(options & SHELL_SILENT) && !emsg_silent)
1376 {
1377 msg_putchar('\n');
1378 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001379 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 retval = x;
1382 }
1383 }
1384 vim_free(shellcmd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001385#endif // AZTEC_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaar0f873732019-12-05 20:28:46 +01001387 if ((mydir = CurrentDir(mydir)) != 0) // make sure we stay in the same directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 UnLock(mydir);
1389 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02001390 {
1391 // The shell may have messed with the mode, always set it.
1392 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001393 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02001394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#ifdef FEAT_TITLE
1396 resettitle();
1397#endif
1398 if (term_console)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001399 win_resize_on(); // window resize events activated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 return retval;
1401}
1402
1403/*
1404 * check for an "interrupt signal"
1405 * We only react to a CTRL-C, but also clear the other break signals to avoid
1406 * trouble with lattice-c programs.
1407 */
1408 void
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02001409mch_breakcheck(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1412 got_int = TRUE;
1413}
1414
Bram Moolenaar0f873732019-12-05 20:28:46 +01001415// this routine causes manx to use this Chk_Abort() rather than its own
1416// otherwise it resets our ^C when doing any I/O (even when Enable_Abort
1417// is zero). Since we want to check for our own ^C's
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
1419#ifdef _DCC
1420#define Chk_Abort chkabort
1421#endif
1422
1423#ifdef LATTICE
1424void __regargs __chkabort(void);
1425
1426void __regargs __chkabort(void)
1427{}
1428
1429#else
1430 long
1431Chk_Abort(void)
1432{
1433 return(0L);
1434}
1435#endif
1436
1437/*
1438 * mch_expandpath() - this code does wild-card pattern matching using the arp
1439 * routines.
1440 *
1441 * "pat" has backslashes before chars that are not to be expanded.
1442 * Returns the number of matches found.
1443 *
1444 * This is based on WildDemo2.c (found in arp1.1 distribution).
1445 * That code's copyright follows:
1446 * Copyright (c) 1987, Scott Ballantyne
1447 * Use and abuse as you please.
1448 */
1449
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001450#ifdef __amigaos4__
1451# define ANCHOR_BUF_SIZE 1024
1452#else
1453# define ANCHOR_BUF_SIZE (512)
1454# define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001458mch_expandpath(
1459 garray_T *gap,
1460 char_u *pat,
Bram Moolenaar0f873732019-12-05 20:28:46 +01001461 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 struct AnchorPath *Anchor;
1464 LONG Result;
1465 char_u *starbuf, *sp, *dp;
1466 int start_len;
1467 int matches;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001468#ifdef __amigaos4__
1469 struct TagItem AnchorTags[] = {
1470 {ADO_Strlen, ANCHOR_BUF_SIZE},
1471 {ADO_Flags, APF_DODOT|APF_DOWILD|APF_MultiAssigns},
1472 {TAG_DONE, 0L}
1473 };
1474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476 start_len = gap->ga_len;
1477
Bram Moolenaar0f873732019-12-05 20:28:46 +01001478 // Get our AnchorBase
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001479#ifdef __amigaos4__
1480 Anchor = AllocDosObject(DOS_ANCHORPATH, AnchorTags);
1481#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001482 Anchor = alloc_clear(ANCHOR_SIZE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (Anchor == NULL)
1485 return 0;
1486
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001487#ifndef __amigaos4__
Bram Moolenaar0f873732019-12-05 20:28:46 +01001488 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; // ap_Length not supported anymore
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001489# ifdef APF_DODOT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001490 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; // allow '.' for current dir
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001491# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001492 Anchor->ap_Flags = APF_DoDot | APF_DoWild; // allow '.' for current dir
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494#endif
1495
1496#ifdef FEAT_ARP
1497 if (dos2)
1498 {
1499#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001500 // hack to replace '*' by '#?'
Bram Moolenaar964b3742019-05-24 18:54:09 +02001501 starbuf = alloc(2 * STRLEN(pat) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 if (starbuf == NULL)
1503 goto Return;
1504 for (sp = pat, dp = starbuf; *sp; ++sp)
1505 {
1506 if (*sp == '*')
1507 {
1508 *dp++ = '#';
1509 *dp++ = '?';
1510 }
1511 else
1512 *dp++ = *sp;
1513 }
1514 *dp = NUL;
1515 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1516 vim_free(starbuf);
1517#ifdef FEAT_ARP
1518 }
1519 else
1520 Result = FindFirst((char *)pat, Anchor);
1521#endif
1522
1523 /*
1524 * Loop to get all matches.
1525 */
1526 while (Result == 0)
1527 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001528#ifdef __amigaos4__
1529 addfile(gap, (char_u *)Anchor->ap_Buffer, flags);
1530#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#ifdef FEAT_ARP
1534 if (dos2)
1535#endif
1536 Result = MatchNext(Anchor);
1537#ifdef FEAT_ARP
1538 else
1539 Result = FindNext(Anchor);
1540#endif
1541 }
1542 matches = gap->ga_len - start_len;
1543
1544 if (Result == ERROR_BUFFER_OVERFLOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001545 emsg(_("ANCHOR_BUF_SIZE too small."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1547 && Result != ERROR_DEVICE_NOT_MOUNTED
1548 && Result != ERROR_NO_MORE_ENTRIES)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001549 emsg(_("I/O ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
1551 /*
1552 * Sort the files for this pattern.
1553 */
1554 if (matches)
1555 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1556 (size_t)matches, sizeof(char_u *), sortcmp);
1557
Bram Moolenaar0f873732019-12-05 20:28:46 +01001558 // Free the wildcard stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559#ifdef FEAT_ARP
1560 if (dos2)
1561#endif
1562 MatchEnd(Anchor);
1563#ifdef FEAT_ARP
1564 else
1565 FreeAnchorChain(Anchor);
1566#endif
1567
1568Return:
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001569#ifdef __amigaos4__
1570 FreeDosObject(DOS_ANCHORPATH, Anchor);
1571#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 vim_free(Anchor);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 return matches;
1576}
1577
1578 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001579sortcmp(const void *a, const void *b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 char *s = *(char **)a;
1582 char *t = *(char **)b;
1583
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001584 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585}
1586
1587/*
1588 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1589 */
1590 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001591mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001593 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 if (*p == '\\' && p[1] != NUL)
1596 ++p;
1597 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1598 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 return FALSE;
1601}
1602
1603 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001604mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001606 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 if (*p == '\\' && p[1] != NUL)
1609 ++p;
1610 else
1611 if (vim_strchr((char_u *)
1612# ifdef VIM_BACKTICK
1613 "*?[(#$`"
1614# else
1615 "*?[(#$"
1616# endif
1617 , *p) != NULL
1618 || (*p == '~' && p[1] != NUL))
1619 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 return FALSE;
1622}
1623
1624/*
1625 * With AmigaDOS 2.0 support for reading local environment variables
1626 *
1627 * Two buffers are allocated:
1628 * - A big one to do the expansion into. It is freed before returning.
1629 * - A small one to hold the return value. It is kept until the next call.
1630 */
1631 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001632mch_getenv(char_u *var)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 int len;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001635 UBYTE *buf; // buffer to expand in
1636 char_u *retval; // return value
1637 static char_u *alloced = NULL; // allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639#ifdef FEAT_ARP
1640 if (!dos2)
1641 retval = (char_u *)getenv((char *)var);
1642 else
1643#endif
1644 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001645 VIM_CLEAR(alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 retval = NULL;
1647
1648 buf = alloc(IOSIZE);
1649 if (buf == NULL)
1650 return NULL;
1651
1652 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1653 if (len >= 0)
1654 {
1655 retval = vim_strsave((char_u *)buf);
1656 alloced = retval;
1657 }
1658
1659 vim_free(buf);
1660 }
1661
Bram Moolenaar0f873732019-12-05 20:28:46 +01001662 // if $VIM is not defined, use "vim:" instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (retval == NULL && STRCMP(var, "VIM") == 0)
1664 retval = (char_u *)"vim:";
1665
1666 return retval;
1667}
1668
1669/*
1670 * Amiga version of setenv() with AmigaDOS 2.0 support.
1671 */
Bram Moolenaar0f873732019-12-05 20:28:46 +01001672// ARGSUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001674mch_setenv(char *var, char *value, int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676#ifdef FEAT_ARP
1677 if (!dos2)
1678 return setenv(var, value);
1679#endif
1680
1681 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001682 return 0; // success
1683 return -1; // failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684}