blob: bc4ecc9610190863fe657c7c38d3d5af1e6c7fff [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000030# include <exec/exec.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000031# include <intuition/intuition.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar0f873732019-12-05 20:28:46 +010034// XXX These are included from os_amiga.h
=?UTF-8?q?Ola=20S=C3=B6der?=663ee882023-03-07 15:30:50 +000035// #include <exec/types.h>
36// #include <libraries/dos.h>
37// #include <libraries/dosextens.h>
Bram Moolenaar0f873732019-12-05 20:28:46 +010038// #include <proto/exec.h>
39// #include <proto/dos.h>
40// #include <proto/intuition.h>
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000041
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#include <exec/memory.h>
43
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
ola.soder@axis.comcc650402021-06-23 15:52:46 +0200119# ifdef BUILDDATE
120 " (" BUILDDATE ")"
121# endif
Bram Moolenaar70576f72019-07-31 20:40:08 +0200122 ;
123#endif
124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100126win_resize_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127{
128 OUT_STR_NF("\033[12{");
129}
130
131 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100132win_resize_off(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
134 OUT_STR_NF("\033[12}");
135}
136
137 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100138mch_write(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 Write(raw_out, (char *)p, (long)len);
141}
142
143/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200144 * mch_inchar(): low level input function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 * Get a characters from the keyboard.
146 * If time == 0 do not wait for characters.
147 * If time == n wait a short time for characters.
148 * If time == -1 wait forever for characters.
149 *
150 * Return number of characters read.
151 */
152 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100153mch_inchar(
154 char_u *buf,
155 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000156 long time, // milliseconds
ola.soder@axis.come1314962022-02-13 12:13:38 +0000157 int tb_change_cnt UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158{
159 int len;
160 long utime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
162 if (time >= 0)
163 {
164 if (time == 0)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100165 utime = 100L; // time = 0 causes problems in DOS 1.2
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100167 utime = time * 1000L; // convert from milli to micro secs
168 if (WaitForChar(raw_in, utime) == 0) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100171 else // time == -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 /*
174 * If there is no character available within 2 seconds (default)
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000175 * write the autoscript file to disk. Or cause the CursorHold event
176 * to be triggered.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 */
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000178 if (WaitForChar(raw_in, p_ut * 1000L) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 {
Bram Moolenaare3226be2005-12-18 22:10:00 +0000180 if (trigger_cursorhold() && maxlen >= 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 {
Bram Moolenaar5b743bf2005-03-15 22:50:43 +0000182 buf[0] = K_SPECIAL;
183 buf[1] = KS_EXTRA;
184 buf[2] = (int)KE_CURSORHOLD;
185 return 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000187 before_blocking();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 }
189 }
190
Bram Moolenaar0f873732019-12-05 20:28:46 +0100191 for (;;) // repeat until we got a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 len = Read(raw_in, (char *)buf, (long)maxlen / input_conv.vc_factor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 if (len > 0)
195 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100196 // Convert from 'termencoding' to 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 if (input_conv.vc_type != CONV_NONE)
198 len = convert_input(buf, len, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 return len;
200 }
201 }
202}
203
204/*
205 * return non-zero if a character is available
206 */
207 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100208mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209{
210 return (WaitForChar(raw_in, 100L) != 0);
211}
212
213/*
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200214 * Return amount of memory still available in Kbyte.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 */
216 long_u
Bram Moolenaar05540972016-01-30 20:31:25 +0100217mch_avail_mem(int special)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
Bram Moolenaara9ab3912019-08-10 14:54:20 +0200219#if defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__)
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200220 return (long_u)AvailMem(MEMF_ANY) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000221#else
Bram Moolenaar11b73d62012-06-29 15:51:30 +0200222 return (long_u)(AvailMem(special ? (long)MEMF_CHIP : (long)MEMF_ANY)) >> 10;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224}
225
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000226/*
227 * Waits a specified amount of time, or until input arrives if
Bram Moolenaar0981c872020-08-23 14:28:37 +0200228 * flags does not have MCH_DELAY_IGNOREINPUT.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 void
Bram Moolenaar0981c872020-08-23 14:28:37 +0200231mch_delay(long msec, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100233#ifndef LATTICE // SAS declares void Delay(ULONG)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100234 void Delay(long);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235#endif
236
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000237 if (msec <= 0)
238 return;
239
240 if (flags & MCH_DELAY_IGNOREINPUT)
241 Delay(msec / 20L); // Delay works with 20 msec intervals
242 else
243 WaitForChar(raw_in, msec * 1000L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244}
245
246/*
247 * We have no job control, fake it by starting a new shell.
248 */
249 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100250mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251{
252 suspend_shell();
253}
254
255#ifndef DOS_LIBRARY
256# define DOS_LIBRARY ((UBYTE *)"dos.library")
257#endif
258
259 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100260mch_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261{
Bram Moolenaardba7c852019-12-30 22:33:17 +0100262#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 static char intlibname[] = "intuition.library";
Bram Moolenaardba7c852019-12-30 22:33:17 +0100264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266#ifdef AZTEC_C
Bram Moolenaar0f873732019-12-05 20:28:46 +0100267 Enable_Abort = 0; // disallow vim to be aborted
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
269 Columns = 80;
270 Rows = 24;
271
272 /*
273 * Set input and output channels, unless we have opened our own window
274 */
275 if (raw_in == (BPTR)NULL)
276 {
277 raw_in = Input();
278 raw_out = Output();
279 /*
280 * If Input() is not interactive, then Output() will be (because of
281 * check in mch_check_win()). Used for "Vim -".
282 * Also check the other way around, for "Vim -h | more".
283 */
284 if (!IsInteractive(raw_in))
285 raw_in = raw_out;
286 else if (!IsInteractive(raw_out))
287 raw_out = raw_in;
288 }
289
290 out_flush();
291
292 wb_window = NULL;
Bram Moolenaardba7c852019-12-30 22:33:17 +0100293#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 if ((IntuitionBase = (struct IntuitionBase *)
295 OpenLibrary((UBYTE *)intlibname, 0L)) == NULL)
296 {
297 mch_errmsg(_("cannot open "));
298 mch_errmsg(intlibname);
299 mch_errmsg("!?\n");
300 mch_exit(3);
301 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303}
304
Bram Moolenaar82881492012-11-20 16:53:39 +0100305#ifndef PROTO
306# include <workbench/startup.h>
307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308
309/*
310 * Check_win checks whether we have an interactive window.
311 * If not, a new window is opened with the newcli command.
312 * If we would open a window ourselves, the :sh and :! commands would not
313 * work properly (Why? probably because we are then running in a background
314 * CLI). This also is the best way to assure proper working in a next
315 * Workbench release.
316 *
317 * For the -f option (foreground mode) we open our own window and disable :sh.
318 * Otherwise the calling program would never know when editing is finished.
319 */
Bram Moolenaar0f873732019-12-05 20:28:46 +0100320#define BUF2SIZE 320 // length of buffer for argument with complete path
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
322 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100323mch_check_win(int argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324{
325 int i;
326 BPTR nilfh, fh;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000327 char_u buf1[24];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 char_u buf2[BUF2SIZE];
329 static char_u *(constrings[3]) = {(char_u *)"con:0/0/662/210/",
330 (char_u *)"con:0/0/640/200/",
331 (char_u *)"con:0/0/320/200/"};
332 static char_u *winerr = (char_u *)N_("VIM: Can't open window!\n");
333 struct WBArg *argp;
334 int ac;
335 char *av;
336 char_u *device = NULL;
337 int exitval = 4;
Bram Moolenaardba7c852019-12-30 22:33:17 +0100338#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 struct Library *DosBase;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 int usewin = FALSE;
342
343/*
344 * check if we are running under DOS 2.0x or higher
345 */
Bram Moolenaardba7c852019-12-30 22:33:17 +0100346#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 DosBase = OpenLibrary(DOS_LIBRARY, 37L);
348 if (DosBase != NULL)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100349 // if (((struct Library *)DOSBase)->lib_Version >= 37)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 {
351 CloseLibrary(DosBase);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000352# ifdef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 dos2 = TRUE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100356 else // without arp functions we NEED 2.0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000358# ifndef FEAT_ARP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 mch_errmsg(_("Need Amigados version 2.04 or later\n"));
360 exit(3);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000361# else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100362 // need arp functions for dos 1.x
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (!(ArpBase = (struct ArpBase *) OpenLibrary((UBYTE *)ArpName, ArpVersion)))
364 {
365 fprintf(stderr, _("Need %s version %ld\n"), ArpName, ArpVersion);
366 exit(3);
367 }
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 }
Bram Moolenaardba7c852019-12-30 22:33:17 +0100370#endif /* __amigaos4__ __AROS__ __MORPHOS__ */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371
372 /*
373 * scan argv[] for the "-f" and "-d" arguments
374 */
375 for (i = 1; i < argc; ++i)
376 if (argv[i][0] == '-')
377 {
378 switch (argv[i][1])
379 {
380 case 'f':
381 usewin = TRUE;
382 break;
383
384 case 'd':
385 if (i < argc - 1
386#ifdef FEAT_DIFF
Bram Moolenaar0f873732019-12-05 20:28:46 +0100387 // require using "-dev", "-d" means diff mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 && argv[i][2] == 'e' && argv[i][3] == 'v'
389#endif
390 )
391 device = (char_u *)argv[i + 1];
392 break;
393 }
394 }
395
396/*
397 * If we were not started from workbench, do not have a "-d" or "-dev"
398 * argument and we have been started with an interactive window, use that
399 * window.
400 */
401 if (argc != 0
402 && device == NULL
403 && (IsInteractive(Input()) || IsInteractive(Output())))
404 return OK;
405
406/*
407 * When given the "-f" argument, we open our own window. We can't use the
408 * newcli trick below, because the calling program (mail, rn, etc.) would not
409 * know when we are finished.
410 */
411 if (usewin)
412 {
413 /*
414 * Try to open a window. First try the specified device.
415 * Then try a 24 line 80 column window.
416 * If that fails, try two smaller ones.
417 */
418 for (i = -1; i < 3; ++i)
419 {
420 if (i >= 0)
421 device = constrings[i];
422 if (device != NULL && (raw_in = Open((UBYTE *)device,
423 (long)MODE_NEWFILE)) != (BPTR)NULL)
424 break;
425 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100426 if (raw_in == (BPTR)NULL) // all three failed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 {
428 mch_errmsg(_(winerr));
429 goto exit;
430 }
431 raw_out = raw_in;
432 close_win = TRUE;
433 return OK;
434 }
435
436 if ((nilfh = Open((UBYTE *)"NIL:", (long)MODE_NEWFILE)) == (BPTR)NULL)
437 {
438 mch_errmsg(_("Cannot open NIL:\n"));
439 goto exit;
440 }
441
442 /*
443 * Make a unique name for the temp file (which we will not delete!).
444 * Use a pointer on the stack (nobody else will be using it).
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000445 * Under AmigaOS4, this assumption might change in the future, so
446 * we use a pointer to the current task instead. This should be a
447 * shared structure and thus globally unique.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 */
ola.soder@axis.com3a62b142021-06-24 18:50:30 +0200449#if !defined(__amigaos4__) && !defined(__AROS__) && !defined(__MORPHOS__)
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000450 sprintf((char *)buf1, "t:nc%p", FindTask(0));
451#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 sprintf((char *)buf1, "t:nc%ld", (long)buf1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if ((fh = Open((UBYTE *)buf1, (long)MODE_NEWFILE)) == (BPTR)NULL)
455 {
456 mch_errmsg(_("Cannot create "));
457 mch_errmsg((char *)buf1);
458 mch_errmsg("\n");
459 goto exit;
460 }
461 /*
462 * Write the command into the file, put quotes around the arguments that
463 * have a space in them.
464 */
Bram Moolenaar0f873732019-12-05 20:28:46 +0100465 if (argc == 0) // run from workbench
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 ac = ((struct WBStartup *)argv)->sm_NumArgs;
467 else
468 ac = argc;
469 for (i = 0; i < ac; ++i)
470 {
471 if (argc == 0)
472 {
473 *buf2 = NUL;
474 argp = &(((struct WBStartup *)argv)->sm_ArgList[i]);
475 if (argp->wa_Lock)
476 (void)lock2name(argp->wa_Lock, buf2, (long)(BUF2SIZE - 1));
477#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100478 if (dos2) // use 2.0 function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479#endif
480 AddPart((UBYTE *)buf2, (UBYTE *)argp->wa_Name, (long)(BUF2SIZE - 1));
481#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100482 else // use arp function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 TackOn((char *)buf2, argp->wa_Name);
484#endif
485 av = (char *)buf2;
486 }
487 else
488 av = argv[i];
489
Bram Moolenaar0f873732019-12-05 20:28:46 +0100490 // skip '-d' or "-dev" option
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 if (av[0] == '-' && av[1] == 'd'
492#ifdef FEAT_DIFF
493 && av[2] == 'e' && av[3] == 'v'
494#endif
495 )
496 {
497 ++i;
498 continue;
499 }
500 if (vim_strchr((char_u *)av, ' '))
501 Write(fh, "\"", 1L);
502 Write(fh, av, (long)strlen(av));
503 if (vim_strchr((char_u *)av, ' '))
504 Write(fh, "\"", 1L);
505 Write(fh, " ", 1L);
506 }
507 Write(fh, "\nendcli\n", 8L);
508 Close(fh);
509
510/*
511 * Try to open a new cli in a window. If "-d" or "-dev" argument was given try
512 * to open the specified device. Then try a 24 line 80 column window. If that
513 * fails, try two smaller ones.
514 */
515 for (i = -1; i < 3; ++i)
516 {
517 if (i >= 0)
518 device = constrings[i];
519 else if (device == NULL)
520 continue;
521 sprintf((char *)buf2, "newcli <nil: >nil: %s from %s", (char *)device, (char *)buf1);
522#ifdef FEAT_ARP
523 if (dos2)
524 {
525#endif
526 if (!SystemTags((UBYTE *)buf2, SYS_UserShell, TRUE, TAG_DONE))
527 break;
528#ifdef FEAT_ARP
529 }
530 else
531 {
532 if (Execute((UBYTE *)buf2, nilfh, nilfh))
533 break;
534 }
535#endif
536 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100537 if (i == 3) // all three failed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 {
539 DeleteFile((UBYTE *)buf1);
540 mch_errmsg(_(winerr));
541 goto exit;
542 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100543 exitval = 0; // The Execute succeeded: exit this program
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
545exit:
546#ifdef FEAT_ARP
547 if (ArpBase)
548 CloseLibrary((struct Library *) ArpBase);
549#endif
550 exit(exitval);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100551 // NOTREACHED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 return FAIL;
553}
554
555/*
556 * Return TRUE if the input comes from a terminal, FALSE otherwise.
557 * We fake there is a window, because we can always open one!
558 */
559 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100560mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
562 return TRUE;
563}
564
565/*
566 * fname_case(): Set the case of the file name, if it already exists.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000567 * This will cause the file name to remain exactly the same
568 * if the file system ignores, but preserves case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 */
Bram Moolenaar0f873732019-12-05 20:28:46 +0100570//ARGSUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100572fname_case(
573 char_u *name,
ola.soder@axis.come1314962022-02-13 12:13:38 +0000574 int len UNUSED) // buffer size, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
576 struct FileInfoBlock *fib;
577 size_t flen;
578
579 fib = get_fib(name);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000580 if (fib == NULL)
581 return;
582
583 flen = STRLEN(name);
584 // TODO: Check if this fix applies to AmigaOS < 4 too.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000585#ifdef __amigaos4__
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000586 if (fib->fib_DirEntryType == ST_ROOT)
587 strcat(fib->fib_FileName, ":");
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000588#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000589 if (flen == strlen(fib->fib_FileName)) // safety check
590 mch_memmove(name, fib->fib_FileName, flen);
591 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592}
593
594/*
595 * Get the FileInfoBlock for file "fname"
596 * The returned structure has to be free()d.
597 * Returns NULL on error.
598 */
599 static struct FileInfoBlock *
Bram Moolenaar05540972016-01-30 20:31:25 +0100600get_fib(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 BPTR flock;
603 struct FileInfoBlock *fib;
604
Bram Moolenaar0f873732019-12-05 20:28:46 +0100605 if (fname == NULL) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 return NULL;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000607#ifdef __amigaos4__
608 fib = AllocDosObject(DOS_FIB,0);
609#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200610 fib = ALLOC_ONE(struct FileInfoBlock);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000611#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000612 if (fib == NULL)
613 return;
614
615 flock = Lock((UBYTE *)fname, (long)ACCESS_READ);
616 if (flock == (BPTR)NULL || !Examine(flock, fib))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000618 free_fib(fib); // in case of an error the memory is freed here
619 fib = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000621 if (flock)
622 UnLock(flock);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 return fib;
624}
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626/*
627 * set the title of our window
628 * icon name is not set
629 */
630 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100631mch_settitle(char_u *title, char_u *icon)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 if (wb_window != NULL && title != NULL)
634 SetWindowTitles(wb_window, (UBYTE *)title, (UBYTE *)-1L);
635}
636
637/*
638 * Restore the window/icon title.
639 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200640 * SAVE_RESTORE_TITLE Just restore title
641 * SAVE_RESTORE_ICON Just restore icon (which we don't have)
642 * SAVE_RESTORE_BOTH Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 */
644 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100645mch_restore_title(int which)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
Bram Moolenaar40385db2018-08-07 22:31:44 +0200647 if (which & SAVE_RESTORE_TITLE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 mch_settitle(oldwindowtitle, NULL);
649}
650
651 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100652mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 return (wb_window != NULL);
655}
656
657 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100658mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
660 return FALSE;
661}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662
Bram Moolenaardfded982019-10-26 21:33:19 +0200663 void
664mch_setmouse(int on UNUSED)
665{
666 // TODO: implement
667}
668
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669/*
670 * Insert user name in s[len].
671 */
672 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100673mch_get_user_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674{
Bram Moolenaar5e8e9672019-09-27 13:38:56 +0200675#if defined(__amigaos4__) || defined(__AROS__) || defined(__MORPHOS__)
676 struct passwd *pwd = getpwuid(getuid());
677
678 if (pwd != NULL && pwd->pw_name && len > 0)
679 {
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100680 vim_strncpy(s, (char_u *)pwd->pw_name, len - 1);
681 return OK;
Bram Moolenaar5e8e9672019-09-27 13:38:56 +0200682 }
683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 *s = NUL;
685 return FAIL;
686}
687
688/*
689 * Insert host name is s[len].
690 */
691 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100692mch_get_host_name(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693{
=?UTF-8?q?Ola=20S=C3=B6der?=f06c4a72023-03-06 20:36:55 +0000694#if !defined(__AROS__)
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000695 gethostname(s, len);
696#else
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000697 vim_strncpy(s, "Amiga", len - 1);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699}
700
701/*
702 * return process ID
703 */
704 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100705mch_get_pid(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
=?UTF-8?q?Ola=20S=C3=B6der?=b7e07dc2022-09-25 20:12:21 +0100707#if defined(__amigaos4__)
708 return (long) getpid();
709#elif defined(__AROS__) || defined(__MORPHOS__)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100710 // This is as close to a pid as we can come. We could use CLI numbers also,
711 // but then we would have two different types of process identifiers.
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000712 return((long)FindTask(0));
713#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 return (long)0;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716}
717
718/*
719 * Get name of current directory into buffer 'buf' of length 'len' bytes.
720 * Return OK for success, FAIL for failure.
721 */
722 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100723mch_dirname(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
725 return mch_FullName((char_u *)"", buf, len, FALSE);
726}
727
728/*
729 * get absolute file name into buffer 'buf' of length 'len' bytes
730 *
731 * return FAIL for failure, OK otherwise
732 */
733 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100734mch_FullName(
735 char_u *fname,
736 char_u *buf,
737 int len,
738 int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
740 BPTR l;
741 int retval = FAIL;
742 int i;
743
Bram Moolenaar0f873732019-12-05 20:28:46 +0100744 // Lock the file. If it exists, we can get the exact name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0)
746 {
747 retval = lock2name(l, buf, (long)len - 1);
748 UnLock(l);
749 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100750 else if (force || !mch_isFullName(fname)) // not a full path yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
752 /*
753 * If the file cannot be locked (doesn't exist), try to lock the
754 * current directory and concatenate the file name.
755 */
756 if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL)
757 {
758 retval = lock2name(l, buf, (long)len);
759 UnLock(l);
760 if (retval == OK)
761 {
762 i = STRLEN(buf);
Bram Moolenaar0f873732019-12-05 20:28:46 +0100763 // Concatenate the fname to the directory. Don't add a slash
764 // if fname is empty, but do change "" to "/".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (i == 0 || *fname != NUL)
766 {
767 if (i < len - 1 && (i == 0 || buf[i - 1] != ':'))
768 buf[i++] = '/';
Bram Moolenaarce0842a2005-07-18 21:58:11 +0000769 vim_strncpy(buf + i, fname, len - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
771 }
772 }
773 }
774 if (*buf == 0 || *buf == ':')
Bram Moolenaar0f873732019-12-05 20:28:46 +0100775 retval = FAIL; // something failed; use the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 return retval;
777}
778
779/*
780 * Return TRUE if "fname" does not depend on the current directory.
781 */
782 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100783mch_isFullName(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
785 return (vim_strchr(fname, ':') != NULL && *fname != ':');
786}
787
788/*
789 * Get the full file name from a lock. Use 2.0 function if possible, because
790 * the arp function has more restrictions on the path length.
791 *
792 * return FAIL for failure, OK otherwise
793 */
794 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100795lock2name(BPTR lock, char_u *buf, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100798 if (dos2) // use 2.0 function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799#endif
800 return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL);
801#ifdef FEAT_ARP
Bram Moolenaar0f873732019-12-05 20:28:46 +0100802 else // use arp function
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL);
804#endif
805}
806
807/*
808 * get file permissions for 'name'
809 * Returns -1 when it doesn't exist.
810 */
811 long
Bram Moolenaar05540972016-01-30 20:31:25 +0100812mch_getperm(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 struct FileInfoBlock *fib;
815 long retval = -1;
816
817 fib = get_fib(name);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000818 if (fib == NULL)
819 return -1;
820
821 retval = fib->fib_Protection;
822 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 return retval;
824}
825
826/*
827 * set file permission for 'name' to 'perm'
828 *
829 * return FAIL for failure, OK otherwise
830 */
831 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100832mch_setperm(char_u *name, long perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100834 perm &= ~FIBF_ARCHIVE; // reset archived bit
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL);
836}
837
838/*
839 * Set hidden flag for "name".
840 */
841 void
ola.soder@axis.come1314962022-02-13 12:13:38 +0000842mch_hide(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100844 // can't hide a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845}
846
847/*
848 * return FALSE if "name" is not a directory
849 * return TRUE if "name" is a directory.
850 * return FALSE for error.
851 */
852 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100853mch_isdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854{
855 struct FileInfoBlock *fib;
856 int retval = FALSE;
857
858 fib = get_fib(name);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000859 if (fib == NULL)
860 return FALSE;
861
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000862#ifdef __amigaos4__
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000863 retval = (FIB_IS_DRAWER(fib)) ? TRUE : FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000864#else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000865 retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000866#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000867 free_fib(fib);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 return retval;
869}
870
871/*
872 * Create directory "name".
873 */
Bram Moolenaare3853642006-09-14 19:36:57 +0000874 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100875mch_mkdir(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
877 BPTR lock;
878
879 lock = CreateDir(name);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000880 if (lock == NULL)
881 return -1;
882
883 UnLock(lock);
884 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885}
886
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887/*
888 * Return 1 if "name" can be executed, 0 if not.
Bram Moolenaarb5971142015-03-21 17:32:19 +0100889 * If "use_path" is FALSE only check if "name" is executable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * Return -1 if unknown.
891 */
892 int
ola.soder@axis.come1314962022-02-13 12:13:38 +0000893mch_can_exe(char_u *name, char_u **path UNUSED, int use_path)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
ola.soder@axis.com949b35d2022-02-10 20:21:04 +0000895 int exe = -1;
896#ifdef __amigaos4__
897 // Load file sections using elf.library or hunk.library.
898 BPTR seg = LoadSeg(name);
899
900 if (seg && GetSegListInfoTags(seg, GSLI_Native, NULL, TAG_DONE) !=
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100901 GetSegListInfoTags(seg, GSLI_68KHUNK, NULL, TAG_DONE))
ola.soder@axis.com949b35d2022-02-10 20:21:04 +0000902 {
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100903 // Test if file permissions allow execution.
904 struct ExamineData *exd = ExamineObjectTags(EX_StringNameInput, name);
ola.soder@axis.com949b35d2022-02-10 20:21:04 +0000905
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100906 exe = (exd && !(exd->Protection & EXDF_NO_EXECUTE)) ? 1 : 0;
907 FreeDosObject(DOS_EXAMINEDATA, exd);
ola.soder@axis.com949b35d2022-02-10 20:21:04 +0000908 }
909 else
910 {
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100911 exe = 0;
ola.soder@axis.com949b35d2022-02-10 20:21:04 +0000912 }
913
914 UnLoadSeg(seg);
915
916 // Search for executable in path if applicable.
917 if (!exe && use_path)
918 {
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100919 // Save current working dir.
920 BPTR cwd = GetCurrentDir();
921 struct PathNode *head = DupCmdPathList(NULL);
ola.soder@axis.com949b35d2022-02-10 20:21:04 +0000922
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100923 // For each entry, recur to check for executable.
924 for(struct PathNode *tail = head; !exe && tail;
925 tail = (struct PathNode *) BADDR(tail->pn_Next))
926 {
927 SetCurrentDir(tail->pn_Lock);
928 exe = mch_can_exe(name, path, 0);
929 }
ola.soder@axis.com949b35d2022-02-10 20:21:04 +0000930
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100931 // Go back to where we were.
932 FreeCmdPathList(head);
933 SetCurrentDir(cwd);
ola.soder@axis.com949b35d2022-02-10 20:21:04 +0000934 }
935#endif
936 return exe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939/*
940 * Check what "name" is:
941 * NODE_NORMAL: file or directory (or doesn't exist)
942 * NODE_WRITABLE: writable device, socket, fifo, etc.
943 * NODE_OTHER: non-writable things
944 */
945 int
ola.soder@axis.come1314962022-02-13 12:13:38 +0000946mch_nodetype(char_u *name UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100948 // TODO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 return NODE_NORMAL;
950}
951
952 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100953mch_early_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954{
955}
956
957/*
958 * Careful: mch_exit() may be called before mch_init()!
959 */
960 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100961mch_exit(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100963 exiting = TRUE;
964
Bram Moolenaar0f873732019-12-05 20:28:46 +0100965 if (raw_in) // put terminal in 'normal' mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
967 settmode(TMODE_COOK);
968 stoptermcap();
969 }
970 out_char('\n');
971 if (raw_out)
972 {
973 if (term_console)
974 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100975 win_resize_off(); // window resize events de-activated
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (size_set)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100977 OUT_STR("\233t\233u"); // reset window size (CSI t CSI u)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
979 out_flush();
980 }
981
Bram Moolenaar0f873732019-12-05 20:28:46 +0100982 mch_restore_title(SAVE_RESTORE_BOTH); // restore window title
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983
Bram Moolenaar0f873732019-12-05 20:28:46 +0100984 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986#ifdef FEAT_ARP
987 if (ArpBase)
988 CloseLibrary((struct Library *) ArpBase);
989#endif
990 if (close_win)
991 Close(raw_in);
992 if (r)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100993 printf(_("Vim exiting with %d\n"), r); // somehow this makes :cq work!?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 exit(r);
995}
996
997/*
998 * This is a routine for setting a given stream to raw or cooked mode on the
999 * Amiga . This is useful when you are using Lattice C to produce programs
1000 * that want to read single characters with the "getch()" or "fgetc" call.
1001 *
1002 * Written : 18-Jun-87 By Chuck McManis.
1003 */
1004
1005#define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type)
1006
1007/*
1008 * Function mch_settmode() - Convert the specified file pointer to 'raw' or
1009 * 'cooked' mode. This only works on TTY's.
1010 *
1011 * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means
1012 * getch() will return immediately rather than wait for a return. You
1013 * lose editing features though.
1014 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01001015 * Cooked: This function returns the designate file pointer to its normal,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 * wait for a <CR> mode. This is exactly like raw() except that
1017 * it sends a 0 to the console to make it back into a CON: from a RAW:
1018 */
1019 void
Bram Moolenaar26e86442020-05-17 14:06:16 +02001020mch_settmode(tmode_T tmode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
ola.soder@axis.comb420ac92021-06-24 21:43:31 +02001022#if defined(__AROS__) || defined(__amigaos4__) || defined(__MORPHOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0))
1024#else
1025 if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE,
1026 tmode == TMODE_RAW ? -1L : 0L) == 0)
1027#endif
1028 mch_errmsg(_("cannot change console mode ?!\n"));
1029}
1030
1031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 * Code for this routine came from the following :
1033 *
1034 * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM
1035 * DOS packet example
1036 * Requires 1.2
1037 *
1038 * Found on Fish Disk 56.
1039 *
1040 * Heavely modified by mool.
1041 */
1042
Bram Moolenaar82881492012-11-20 16:53:39 +01001043#ifndef PROTO
1044# include <devices/conunit.h>
1045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
1047/*
ola.soder@axis.comd415d262021-06-23 22:05:27 +02001048 * Get console size in a system friendly way on AROS and MorphOS.
1049 * Return FAIL for failure, OK otherwise
1050 */
1051#if defined(__AROS__) || defined(__MORPHOS__)
1052 int
1053mch_get_shellsize(void)
1054{
1055 if (!term_console)
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01001056 return FAIL;
ola.soder@axis.comd415d262021-06-23 22:05:27 +02001057
1058 if (raw_in && raw_out)
1059 {
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01001060 // Save current console mode.
1061 int old_tmode = cur_tmode;
1062 char ctrl[] = "\x9b""0 q";
ola.soder@axis.comd415d262021-06-23 22:05:27 +02001063
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01001064 // Set RAW mode.
1065 mch_settmode(TMODE_RAW);
ola.soder@axis.comd415d262021-06-23 22:05:27 +02001066
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01001067 // Write control sequence to console.
1068 if (Write(raw_out, ctrl, sizeof(ctrl)) == sizeof(ctrl))
1069 {
1070 char scan[] = "\x9b""1;1;%d;%d r",
1071 answ[sizeof(scan) + 8] = { '\0' };
ola.soder@axis.comd415d262021-06-23 22:05:27 +02001072
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01001073 // Read return sequence from input.
1074 if (Read(raw_in, answ, sizeof(answ) - 1) > 0)
1075 {
1076 // Parse result and set Vim globals.
1077 if (sscanf(answ, scan, &Rows, &Columns) == 2)
1078 {
1079 // Restore console mode.
1080 mch_settmode(old_tmode);
1081 return OK;
1082 }
1083 }
1084 }
ola.soder@axis.comd415d262021-06-23 22:05:27 +02001085
Bram Moolenaar9f1983d2022-05-12 20:35:35 +01001086 // Restore console mode.
1087 mch_settmode(old_tmode);
ola.soder@axis.comd415d262021-06-23 22:05:27 +02001088 }
1089
1090 // I/O error. Default size fallback.
1091 term_console = FALSE;
1092 Columns = 80;
1093 Rows = 24;
1094
1095 return FAIL;
1096}
1097#else
1098/*
1099 * Try to get the real window size,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 * return FAIL for failure, OK otherwise
1101 */
1102 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001103mch_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 struct ConUnit *conUnit;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001106#ifndef __amigaos4__
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 char id_a[sizeof(struct InfoData) + 3];
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001108#endif
1109 struct InfoData *id=0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
Bram Moolenaar0f873732019-12-05 20:28:46 +01001111 if (!term_console) // not an amiga window
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001112 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
Bram Moolenaar0f873732019-12-05 20:28:46 +01001114 // insure longword alignment
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001115#ifdef __amigaos4__
Bram Moolenaar62dbdc42011-10-20 18:24:22 +02001116 if (!(id = AllocDosObject(DOS_INFODATA, 0)))
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001117 goto out;
1118#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 id = (struct InfoData *)(((long)id_a + 3L) & ~3L);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
1122 /*
1123 * Should make console aware of real window size, not the one we set.
1124 * Unfortunately, under DOS 2.0x this redraws the window and it
1125 * is rarely needed, so we skip it now, unless we changed the size.
1126 */
1127 if (size_set)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001128 OUT_STR("\233t\233u"); // CSI t CSI u
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 out_flush();
1130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0
1132 || (wb_window = (struct Window *)id->id_VolumeNode) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001134 // it's not an amiga window, maybe aux device
1135 // terminal type should be set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 term_console = FALSE;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001137 goto out;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 if (oldwindowtitle == NULL)
1140 oldwindowtitle = (char_u *)wb_window->Title;
1141 if (id->id_InUse == (BPTR)NULL)
1142 {
1143 mch_errmsg(_("mch_get_shellsize: not a console??\n"));
1144 return FAIL;
1145 }
1146 conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit;
1147
Bram Moolenaar0f873732019-12-05 20:28:46 +01001148 // get window size
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 Rows = conUnit->cu_YMax + 1;
1150 Columns = conUnit->cu_XMax + 1;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001151 if (Rows < 0 || Rows > 200) // cannot be an amiga window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {
1153 Columns = 80;
1154 Rows = 24;
1155 term_console = FALSE;
1156 return FAIL;
1157 }
1158
1159 return OK;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001160out:
1161#ifdef __amigaos4__
Bram Moolenaar0f873732019-12-05 20:28:46 +01001162 FreeDosObject(DOS_INFODATA, id); // Safe to pass NULL
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001163#endif
1164
1165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166}
ola.soder@axis.comd415d262021-06-23 22:05:27 +02001167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168
1169/*
1170 * Try to set the real window size to Rows and Columns.
1171 */
1172 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001173mch_set_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001175 if (!term_console)
1176 return;
1177
1178 size_set = TRUE;
1179 out_char(CSI);
1180 out_num((long)Rows);
1181 out_char('t');
1182 out_char(CSI);
1183 out_num((long)Columns);
1184 out_char('u');
1185 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186}
1187
1188/*
1189 * Rows and/or Columns has changed.
1190 */
1191 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001192mch_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001194 // Nothing to do.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195}
1196
1197/*
1198 * out_num - output a (big) number fast
1199 */
1200 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001201out_num(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
1203 OUT_STR_NF(tltoa((unsigned long)n));
1204}
1205
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001206#if !defined(AZTEC_C) && !defined(__AROS__) && !defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207/*
1208 * Sendpacket.c
1209 *
1210 * An invaluable addition to your Amiga.lib file. This code sends a packet to
1211 * the given message port. This makes working around DOS lots easier.
1212 *
1213 * Note, I didn't write this, those wonderful folks at CBM did. I do suggest
1214 * however that you may wish to add it to Amiga.Lib, to do so, compile it and
1215 * say 'oml lib:amiga.lib -r sendpacket.o'
1216 */
1217
Bram Moolenaar82881492012-11-20 16:53:39 +01001218#ifndef PROTO
Bram Moolenaar0f873732019-12-05 20:28:46 +01001219// #include <proto/exec.h>
1220// #include <proto/dos.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01001221# include <exec/memory.h>
1222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
1224/*
1225 * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy
1226 * Finkel. This function will send a packet of the given type to the Message
1227 * Port supplied.
1228 */
1229
1230 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01001231dos_packet(
Bram Moolenaar0f873732019-12-05 20:28:46 +01001232 struct MsgPort *pid, // process identifier ... (handlers message port)
1233 long action, // packet type ... (what you want handler to do)
1234 long arg) // single argument
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
1236# ifdef FEAT_ARP
1237 struct MsgPort *replyport;
1238 struct StandardPacket *packet;
1239 long res1;
1240
1241 if (dos2)
1242# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001243 return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); // use 2.0 function
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244# ifdef FEAT_ARP
1245
Bram Moolenaar0f873732019-12-05 20:28:46 +01001246 replyport = (struct MsgPort *) CreatePort(NULL, 0); // use arp function
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (!replyport)
1248 return (0);
1249
Bram Moolenaar0f873732019-12-05 20:28:46 +01001250 // Allocate space for a packet, make it public and clear it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 packet = (struct StandardPacket *)
1252 AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001253 if (!packet)
1254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 DeletePort(replyport);
1256 return (0);
1257 }
1258 packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
1259 packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
1260 packet->sp_Pkt.dp_Port = replyport;
1261 packet->sp_Pkt.dp_Type = action;
1262 packet->sp_Pkt.dp_Arg1 = arg;
1263
Bram Moolenaar0f873732019-12-05 20:28:46 +01001264 PutMsg(pid, (struct Message *)packet); // send packet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 WaitPort(replyport);
1267 GetMsg(replyport);
1268
1269 res1 = packet->sp_Pkt.dp_Res1;
1270
1271 FreeMem(packet, (long) sizeof(struct StandardPacket));
1272 DeletePort(replyport);
1273
1274 return (res1);
1275# endif
1276}
Bram Moolenaar0f873732019-12-05 20:28:46 +01001277#endif // !defined(AZTEC_C) && !defined(__AROS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278
1279/*
1280 * Call shell.
1281 * Return error number for failure, 0 otherwise
1282 */
1283 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001284mch_call_shell(
1285 char_u *cmd,
Bram Moolenaar0f873732019-12-05 20:28:46 +01001286 int options) // SHELL_*, see vim.h
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
1288 BPTR mydir;
1289 int x;
1290 int tmode = cur_tmode;
1291#ifdef AZTEC_C
1292 int use_execute;
1293 char_u *shellcmd = NULL;
1294 char_u *shellarg;
1295#endif
1296 int retval = 0;
1297
1298 if (close_win)
1299 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001300 // if Vim opened a window: Executing a shell may cause crashes
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001301 emsg(_(e_cannot_execute_shell_with_f_option));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 return -1;
1303 }
1304
1305 if (term_console)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001306 win_resize_off(); // window resize events de-activated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 out_flush();
1308
1309 if (options & SHELL_COOKED)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001310 settmode(TMODE_COOK); // set to normal mode
1311 mydir = Lock((UBYTE *)"", (long)ACCESS_READ); // remember current dir
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
Bram Moolenaar0f873732019-12-05 20:28:46 +01001313#if !defined(AZTEC_C) // not tested very much
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (cmd == NULL)
1315 {
1316# ifdef FEAT_ARP
1317 if (dos2)
1318# endif
1319 x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE);
1320# ifdef FEAT_ARP
1321 else
1322 x = Execute(p_sh, raw_in, raw_out);
1323# endif
1324 }
1325 else
1326 {
1327# ifdef FEAT_ARP
1328 if (dos2)
1329# endif
1330 x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1331# ifdef FEAT_ARP
1332 else
1333 x = Execute((char *)cmd, 0L, raw_out);
1334# endif
1335 }
1336# ifdef FEAT_ARP
1337 if ((dos2 && x < 0) || (!dos2 && !x))
1338# else
1339 if (x < 0)
1340# endif
1341 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001342 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (cmd == NULL)
1344 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001345 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 msg_outtrans(p_sh);
1347 }
1348 else
1349 msg_outtrans(cmd);
1350 msg_putchar('\n');
1351 retval = -1;
1352 }
1353# ifdef FEAT_ARP
1354 else if (!dos2 || x)
1355# else
1356 else if (x)
1357# endif
1358 {
1359 if ((x = IoErr()) != 0)
1360 {
1361 if (!(options & SHELL_SILENT))
1362 {
1363 msg_putchar('\n');
1364 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001365 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367 retval = x;
1368 }
1369 }
Bram Moolenaar0f873732019-12-05 20:28:46 +01001370#else // else part is for AZTEC_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER)))
1372 use_execute = 1;
1373 else
1374 use_execute = 0;
1375 if (!use_execute)
1376 {
1377 /*
1378 * separate shell name from argument
1379 */
1380 shellcmd = vim_strsave(p_sh);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001381 if (shellcmd == NULL) // out of memory, use Execute
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 use_execute = 1;
1383 else
1384 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001385 shellarg = skiptowhite(shellcmd); // find start of arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 if (*shellarg != NUL)
1387 {
1388 *shellarg++ = NUL;
1389 shellarg = skipwhite(shellarg);
1390 }
1391 }
1392 }
1393 if (cmd == NULL)
1394 {
1395 if (use_execute)
1396 {
1397# ifdef FEAT_ARP
1398 if (dos2)
1399# endif
1400 x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE);
1401# ifdef FEAT_ARP
1402 else
1403 x = !Execute((UBYTE *)p_sh, raw_in, raw_out);
1404# endif
1405 }
1406 else
1407 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL);
1408 }
1409 else if (use_execute)
1410 {
1411# ifdef FEAT_ARP
1412 if (dos2)
1413# endif
1414 x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE);
1415# ifdef FEAT_ARP
1416 else
1417 x = !Execute((UBYTE *)cmd, 0L, raw_out);
1418# endif
1419 }
1420 else if (p_st & 1)
1421 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1422 (char *)cmd, NULL);
1423 else
1424 x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg,
1425 (char *)p_shcf, (char *)cmd, NULL);
1426# ifdef FEAT_ARP
1427 if ((dos2 && x < 0) || (!dos2 && x))
1428# else
1429 if (x < 0)
1430# endif
1431 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001432 msg_puts(_("Cannot execute "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 if (use_execute)
1434 {
1435 if (cmd == NULL)
1436 msg_outtrans(p_sh);
1437 else
1438 msg_outtrans(cmd);
1439 }
1440 else
1441 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001442 msg_puts(_("shell "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 msg_outtrans(shellcmd);
1444 }
1445 msg_putchar('\n');
1446 retval = -1;
1447 }
1448 else
1449 {
1450 if (use_execute)
1451 {
1452# ifdef FEAT_ARP
1453 if (!dos2 || x)
1454# else
1455 if (x)
1456# endif
1457 x = IoErr();
1458 }
1459 else
1460 x = wait();
1461 if (x)
1462 {
1463 if (!(options & SHELL_SILENT) && !emsg_silent)
1464 {
1465 msg_putchar('\n');
1466 msg_outnum((long)x);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001467 msg_puts(_(" returned\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
1469 retval = x;
1470 }
1471 }
1472 vim_free(shellcmd);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001473#endif // AZTEC_C
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
Bram Moolenaar0f873732019-12-05 20:28:46 +01001475 if ((mydir = CurrentDir(mydir)) != 0) // make sure we stay in the same directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 UnLock(mydir);
1477 if (tmode == TMODE_RAW)
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02001478 {
1479 // The shell may have messed with the mode, always set it.
1480 cur_tmode = TMODE_UNKNOWN;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001481 settmode(TMODE_RAW); // set to raw mode
Bram Moolenaar3b1f18f2020-05-16 23:15:08 +02001482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 resettitle();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (term_console)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001485 win_resize_on(); // window resize events activated
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 return retval;
1487}
1488
1489/*
1490 * check for an "interrupt signal"
1491 * We only react to a CTRL-C, but also clear the other break signals to avoid
1492 * trouble with lattice-c programs.
1493 */
1494 void
ola.soder@axis.come1314962022-02-13 12:13:38 +00001495mch_breakcheck(int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C)
1498 got_int = TRUE;
1499}
1500
Bram Moolenaar0f873732019-12-05 20:28:46 +01001501// this routine causes manx to use this Chk_Abort() rather than its own
1502// otherwise it resets our ^C when doing any I/O (even when Enable_Abort
1503// is zero). Since we want to check for our own ^C's
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505#ifdef _DCC
1506#define Chk_Abort chkabort
1507#endif
1508
1509#ifdef LATTICE
1510void __regargs __chkabort(void);
1511
1512void __regargs __chkabort(void)
1513{}
1514
1515#else
1516 long
1517Chk_Abort(void)
1518{
1519 return(0L);
1520}
1521#endif
1522
1523/*
1524 * mch_expandpath() - this code does wild-card pattern matching using the arp
1525 * routines.
1526 *
1527 * "pat" has backslashes before chars that are not to be expanded.
1528 * Returns the number of matches found.
1529 *
1530 * This is based on WildDemo2.c (found in arp1.1 distribution).
1531 * That code's copyright follows:
1532 * Copyright (c) 1987, Scott Ballantyne
1533 * Use and abuse as you please.
1534 */
1535
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001536#ifdef __amigaos4__
1537# define ANCHOR_BUF_SIZE 1024
1538#else
1539# define ANCHOR_BUF_SIZE (512)
1540# define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE)
1541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
1543 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001544mch_expandpath(
1545 garray_T *gap,
1546 char_u *pat,
Bram Moolenaar0f873732019-12-05 20:28:46 +01001547 int flags) // EW_* flags
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 struct AnchorPath *Anchor;
1550 LONG Result;
1551 char_u *starbuf, *sp, *dp;
1552 int start_len;
1553 int matches;
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001554#ifdef __amigaos4__
1555 struct TagItem AnchorTags[] = {
1556 {ADO_Strlen, ANCHOR_BUF_SIZE},
1557 {ADO_Flags, APF_DODOT|APF_DOWILD|APF_MultiAssigns},
1558 {TAG_DONE, 0L}
1559 };
1560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
1562 start_len = gap->ga_len;
1563
Bram Moolenaar0f873732019-12-05 20:28:46 +01001564 // Get our AnchorBase
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001565#ifdef __amigaos4__
1566 Anchor = AllocDosObject(DOS_ANCHORPATH, AnchorTags);
1567#else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001568 Anchor = alloc_clear(ANCHOR_SIZE);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (Anchor == NULL)
1571 return 0;
1572
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001573#ifndef __amigaos4__
Bram Moolenaar0f873732019-12-05 20:28:46 +01001574 Anchor->ap_Strlen = ANCHOR_BUF_SIZE; // ap_Length not supported anymore
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001575# ifdef APF_DODOT
Bram Moolenaar0f873732019-12-05 20:28:46 +01001576 Anchor->ap_Flags = APF_DODOT | APF_DOWILD; // allow '.' for current dir
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001577# else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001578 Anchor->ap_Flags = APF_DoDot | APF_DoWild; // allow '.' for current dir
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580#endif
1581
1582#ifdef FEAT_ARP
1583 if (dos2)
1584 {
1585#endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001586 // hack to replace '*' by '#?'
Bram Moolenaar964b3742019-05-24 18:54:09 +02001587 starbuf = alloc(2 * STRLEN(pat) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (starbuf == NULL)
1589 goto Return;
1590 for (sp = pat, dp = starbuf; *sp; ++sp)
1591 {
1592 if (*sp == '*')
1593 {
1594 *dp++ = '#';
1595 *dp++ = '?';
1596 }
1597 else
1598 *dp++ = *sp;
1599 }
1600 *dp = NUL;
1601 Result = MatchFirst((UBYTE *)starbuf, Anchor);
1602 vim_free(starbuf);
1603#ifdef FEAT_ARP
1604 }
1605 else
1606 Result = FindFirst((char *)pat, Anchor);
1607#endif
1608
1609 /*
1610 * Loop to get all matches.
1611 */
1612 while (Result == 0)
1613 {
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001614#ifdef __amigaos4__
1615 addfile(gap, (char_u *)Anchor->ap_Buffer, flags);
1616#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 addfile(gap, (char_u *)Anchor->ap_Buf, flags);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619#ifdef FEAT_ARP
1620 if (dos2)
1621#endif
1622 Result = MatchNext(Anchor);
1623#ifdef FEAT_ARP
1624 else
1625 Result = FindNext(Anchor);
1626#endif
1627 }
1628 matches = gap->ga_len - start_len;
1629
1630 if (Result == ERROR_BUFFER_OVERFLOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001631 emsg(_("ANCHOR_BUF_SIZE too small."));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND
1633 && Result != ERROR_DEVICE_NOT_MOUNTED
1634 && Result != ERROR_NO_MORE_ENTRIES)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001635 emsg(_("I/O ERROR"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 /*
1638 * Sort the files for this pattern.
1639 */
1640 if (matches)
1641 qsort((void *)(((char_u **)gap->ga_data) + start_len),
1642 (size_t)matches, sizeof(char_u *), sortcmp);
1643
Bram Moolenaar0f873732019-12-05 20:28:46 +01001644 // Free the wildcard stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#ifdef FEAT_ARP
1646 if (dos2)
1647#endif
1648 MatchEnd(Anchor);
1649#ifdef FEAT_ARP
1650 else
1651 FreeAnchorChain(Anchor);
1652#endif
1653
1654Return:
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001655#ifdef __amigaos4__
1656 FreeDosObject(DOS_ANCHORPATH, Anchor);
1657#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 vim_free(Anchor);
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00001659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
1661 return matches;
1662}
1663
1664 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001665sortcmp(const void *a, const void *b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 char *s = *(char **)a;
1668 char *t = *(char **)b;
1669
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001670 return pathcmp(s, t, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671}
1672
1673/*
1674 * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath().
1675 */
1676 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001677mch_has_exp_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001679 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 {
1681 if (*p == '\\' && p[1] != NUL)
1682 ++p;
1683 else if (vim_strchr((char_u *)"*?[(#", *p) != NULL)
1684 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
1686 return FALSE;
1687}
1688
1689 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001690mch_has_wildcard(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001692 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 if (*p == '\\' && p[1] != NUL)
1695 ++p;
1696 else
1697 if (vim_strchr((char_u *)
1698# ifdef VIM_BACKTICK
1699 "*?[(#$`"
1700# else
1701 "*?[(#$"
1702# endif
1703 , *p) != NULL
1704 || (*p == '~' && p[1] != NUL))
1705 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
1707 return FALSE;
1708}
1709
1710/*
1711 * With AmigaDOS 2.0 support for reading local environment variables
1712 *
1713 * Two buffers are allocated:
1714 * - A big one to do the expansion into. It is freed before returning.
1715 * - A small one to hold the return value. It is kept until the next call.
1716 */
1717 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001718mch_getenv(char_u *var)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 int len;
Bram Moolenaar0f873732019-12-05 20:28:46 +01001721 UBYTE *buf; // buffer to expand in
1722 char_u *retval; // return value
1723 static char_u *alloced = NULL; // allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
1725#ifdef FEAT_ARP
1726 if (!dos2)
1727 retval = (char_u *)getenv((char *)var);
1728 else
1729#endif
1730 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001731 VIM_CLEAR(alloced);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 retval = NULL;
1733
1734 buf = alloc(IOSIZE);
1735 if (buf == NULL)
1736 return NULL;
1737
1738 len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0);
1739 if (len >= 0)
1740 {
1741 retval = vim_strsave((char_u *)buf);
1742 alloced = retval;
1743 }
1744
1745 vim_free(buf);
1746 }
1747
Bram Moolenaar0f873732019-12-05 20:28:46 +01001748 // if $VIM is not defined, use "vim:" instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (retval == NULL && STRCMP(var, "VIM") == 0)
1750 retval = (char_u *)"vim:";
1751
1752 return retval;
1753}
1754
1755/*
1756 * Amiga version of setenv() with AmigaDOS 2.0 support.
1757 */
Bram Moolenaar0f873732019-12-05 20:28:46 +01001758// ARGSUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 int
ola.soder@axis.come1314962022-02-13 12:13:38 +00001760mch_setenv(char *var, char *value, int x UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762#ifdef FEAT_ARP
1763 if (!dos2)
1764 return setenv(var, value);
1765#endif
1766
1767 if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY))
Bram Moolenaar0f873732019-12-05 20:28:46 +01001768 return 0; // success
1769 return -1; // failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770}