Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 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" |
| 17 | |
| 18 | #ifdef Window |
| 19 | # undef Window /* Amiga has its own Window definition */ |
| 20 | #endif |
| 21 | |
| 22 | #ifdef HAVE_FCNTL_H |
| 23 | # include <fcntl.h> |
| 24 | #endif |
| 25 | |
| 26 | #undef TRUE /* will be redefined by exec/types.h */ |
| 27 | #undef FALSE |
| 28 | |
| 29 | #ifndef LATTICE |
| 30 | # include <exec/types.h> |
| 31 | # include <exec/exec.h> |
| 32 | # include <libraries/dos.h> |
| 33 | # include <libraries/dosextens.h> |
| 34 | # include <intuition/intuition.h> |
| 35 | #else |
| 36 | # include <proto/dos.h> |
| 37 | # include <libraries/dosextens.h> |
| 38 | # include <proto/intuition.h> |
| 39 | # include <proto/exec.h> |
| 40 | #endif |
| 41 | |
| 42 | #include <exec/memory.h> |
| 43 | |
| 44 | #include <dos/dostags.h> /* for 2.0 functions */ |
| 45 | #include <dos/dosasl.h> |
| 46 | |
| 47 | #if defined(LATTICE) && !defined(SASC) && defined(FEAT_ARP) |
| 48 | # include <libraries/arp_pragmas.h> |
| 49 | #endif |
| 50 | |
| 51 | /* |
| 52 | * At this point TRUE and FALSE are defined as 1L and 0L, but we want 1 and 0. |
| 53 | */ |
| 54 | #undef TRUE |
| 55 | #define TRUE (1) |
| 56 | #undef FALSE |
| 57 | #define FALSE (0) |
| 58 | |
| 59 | #if !defined(AZTEC_C) && !defined(__AROS__) |
| 60 | static long dos_packet __ARGS((struct MsgPort *, long, long)); |
| 61 | #endif |
| 62 | static int lock2name __ARGS((BPTR lock, char_u *buf, long len)); |
| 63 | static void out_num __ARGS((long n)); |
| 64 | static struct FileInfoBlock *get_fib __ARGS((char_u *)); |
| 65 | static int sortcmp __ARGS((const void *a, const void *b)); |
| 66 | |
| 67 | static BPTR raw_in = (BPTR)NULL; |
| 68 | static BPTR raw_out = (BPTR)NULL; |
| 69 | static int close_win = FALSE; /* set if Vim opened the window */ |
| 70 | |
| 71 | struct IntuitionBase *IntuitionBase = NULL; |
| 72 | #ifdef FEAT_ARP |
| 73 | struct ArpBase *ArpBase = NULL; |
| 74 | #endif |
| 75 | |
| 76 | static struct Window *wb_window; |
| 77 | static char_u *oldwindowtitle = NULL; |
| 78 | |
| 79 | #ifdef FEAT_ARP |
| 80 | int dos2 = FALSE; /* Amiga DOS 2.0x or higher */ |
| 81 | #endif |
| 82 | int size_set = FALSE; /* set to TRUE if window size was set */ |
| 83 | |
| 84 | void |
| 85 | win_resize_on() |
| 86 | { |
| 87 | OUT_STR_NF("\033[12{"); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | win_resize_off() |
| 92 | { |
| 93 | OUT_STR_NF("\033[12}"); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | mch_write(p, len) |
| 98 | char_u *p; |
| 99 | int len; |
| 100 | { |
| 101 | Write(raw_out, (char *)p, (long)len); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * mch_inchar(): low level input funcion. |
| 106 | * Get a characters from the keyboard. |
| 107 | * If time == 0 do not wait for characters. |
| 108 | * If time == n wait a short time for characters. |
| 109 | * If time == -1 wait forever for characters. |
| 110 | * |
| 111 | * Return number of characters read. |
| 112 | */ |
| 113 | int |
| 114 | mch_inchar(buf, maxlen, time, tb_change_cnt) |
| 115 | char_u *buf; |
| 116 | int maxlen; |
| 117 | long time; /* milli seconds */ |
| 118 | int tb_change_cnt; |
| 119 | { |
| 120 | int len; |
| 121 | long utime; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 122 | |
| 123 | if (time >= 0) |
| 124 | { |
| 125 | if (time == 0) |
| 126 | utime = 100L; /* time = 0 causes problems in DOS 1.2 */ |
| 127 | else |
| 128 | utime = time * 1000L; /* convert from milli to micro secs */ |
| 129 | if (WaitForChar(raw_in, utime) == 0) /* no character available */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 130 | return 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | } |
| 132 | else /* time == -1 */ |
| 133 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 134 | /* |
| 135 | * If there is no character available within 2 seconds (default) |
Bram Moolenaar | 5b743bf | 2005-03-15 22:50:43 +0000 | [diff] [blame] | 136 | * write the autoscript file to disk. Or cause the CursorHold event |
| 137 | * to be triggered. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | */ |
Bram Moolenaar | 5b743bf | 2005-03-15 22:50:43 +0000 | [diff] [blame] | 139 | if (WaitForChar(raw_in, p_ut * 1000L) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | { |
| 141 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 142 | if (trigger_cursorhold() && maxlen >= 3) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | { |
Bram Moolenaar | 5b743bf | 2005-03-15 22:50:43 +0000 | [diff] [blame] | 144 | buf[0] = K_SPECIAL; |
| 145 | buf[1] = KS_EXTRA; |
| 146 | buf[2] = (int)KE_CURSORHOLD; |
| 147 | return 3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 148 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 149 | #endif |
Bram Moolenaar | 9a50b1b | 2005-06-27 22:48:21 +0000 | [diff] [blame] | 150 | before_blocking(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | for (;;) /* repeat until we got a character */ |
| 155 | { |
| 156 | # ifdef FEAT_MBYTE |
| 157 | len = Read(raw_in, (char *)buf, (long)maxlen / input_conv.vc_factor); |
| 158 | # else |
| 159 | len = Read(raw_in, (char *)buf, (long)maxlen); |
| 160 | # endif |
| 161 | if (len > 0) |
| 162 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | #ifdef FEAT_MBYTE |
| 164 | /* Convert from 'termencoding' to 'encoding'. */ |
| 165 | if (input_conv.vc_type != CONV_NONE) |
| 166 | len = convert_input(buf, len, maxlen); |
| 167 | #endif |
| 168 | return len; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * return non-zero if a character is available |
| 175 | */ |
| 176 | int |
| 177 | mch_char_avail() |
| 178 | { |
| 179 | return (WaitForChar(raw_in, 100L) != 0); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Return amount of memory still available. |
| 184 | */ |
| 185 | long_u |
| 186 | mch_avail_mem(special) |
| 187 | int special; |
| 188 | { |
| 189 | return (long_u)AvailMem(special ? (long)MEMF_CHIP : (long)MEMF_ANY); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | mch_delay(msec, ignoreinput) |
| 194 | long msec; |
| 195 | int ignoreinput; |
| 196 | { |
| 197 | #ifndef LATTICE /* SAS declares void Delay(UNLONG) */ |
| 198 | void Delay __ARGS((long)); |
| 199 | #endif |
| 200 | |
| 201 | if (msec > 0) |
| 202 | { |
| 203 | if (ignoreinput) |
| 204 | Delay(msec / 20L); /* Delay works with 20 msec intervals */ |
| 205 | else |
| 206 | WaitForChar(raw_in, msec * 1000L); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * We have no job control, fake it by starting a new shell. |
| 212 | */ |
| 213 | void |
| 214 | mch_suspend() |
| 215 | { |
| 216 | suspend_shell(); |
| 217 | } |
| 218 | |
| 219 | #ifndef DOS_LIBRARY |
| 220 | # define DOS_LIBRARY ((UBYTE *)"dos.library") |
| 221 | #endif |
| 222 | |
| 223 | void |
| 224 | mch_init() |
| 225 | { |
| 226 | static char intlibname[] = "intuition.library"; |
| 227 | |
| 228 | #ifdef AZTEC_C |
| 229 | Enable_Abort = 0; /* disallow vim to be aborted */ |
| 230 | #endif |
| 231 | Columns = 80; |
| 232 | Rows = 24; |
| 233 | |
| 234 | /* |
| 235 | * Set input and output channels, unless we have opened our own window |
| 236 | */ |
| 237 | if (raw_in == (BPTR)NULL) |
| 238 | { |
| 239 | raw_in = Input(); |
| 240 | raw_out = Output(); |
| 241 | /* |
| 242 | * If Input() is not interactive, then Output() will be (because of |
| 243 | * check in mch_check_win()). Used for "Vim -". |
| 244 | * Also check the other way around, for "Vim -h | more". |
| 245 | */ |
| 246 | if (!IsInteractive(raw_in)) |
| 247 | raw_in = raw_out; |
| 248 | else if (!IsInteractive(raw_out)) |
| 249 | raw_out = raw_in; |
| 250 | } |
| 251 | |
| 252 | out_flush(); |
| 253 | |
| 254 | wb_window = NULL; |
| 255 | if ((IntuitionBase = (struct IntuitionBase *) |
| 256 | OpenLibrary((UBYTE *)intlibname, 0L)) == NULL) |
| 257 | { |
| 258 | mch_errmsg(_("cannot open ")); |
| 259 | mch_errmsg(intlibname); |
| 260 | mch_errmsg("!?\n"); |
| 261 | mch_exit(3); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | #include <workbench/startup.h> |
| 266 | |
| 267 | /* |
| 268 | * Check_win checks whether we have an interactive window. |
| 269 | * If not, a new window is opened with the newcli command. |
| 270 | * If we would open a window ourselves, the :sh and :! commands would not |
| 271 | * work properly (Why? probably because we are then running in a background |
| 272 | * CLI). This also is the best way to assure proper working in a next |
| 273 | * Workbench release. |
| 274 | * |
| 275 | * For the -f option (foreground mode) we open our own window and disable :sh. |
| 276 | * Otherwise the calling program would never know when editing is finished. |
| 277 | */ |
| 278 | #define BUF2SIZE 320 /* length of buffer for argument with complete path */ |
| 279 | |
| 280 | int |
| 281 | mch_check_win(argc, argv) |
| 282 | int argc; |
| 283 | char **argv; |
| 284 | { |
| 285 | int i; |
| 286 | BPTR nilfh, fh; |
| 287 | char_u buf1[20]; |
| 288 | char_u buf2[BUF2SIZE]; |
| 289 | static char_u *(constrings[3]) = {(char_u *)"con:0/0/662/210/", |
| 290 | (char_u *)"con:0/0/640/200/", |
| 291 | (char_u *)"con:0/0/320/200/"}; |
| 292 | static char_u *winerr = (char_u *)N_("VIM: Can't open window!\n"); |
| 293 | struct WBArg *argp; |
| 294 | int ac; |
| 295 | char *av; |
| 296 | char_u *device = NULL; |
| 297 | int exitval = 4; |
| 298 | struct Library *DosBase; |
| 299 | int usewin = FALSE; |
| 300 | |
| 301 | /* |
| 302 | * check if we are running under DOS 2.0x or higher |
| 303 | */ |
| 304 | DosBase = OpenLibrary(DOS_LIBRARY, 37L); |
| 305 | if (DosBase != NULL) |
| 306 | /* if (((struct Library *)DOSBase)->lib_Version >= 37) */ |
| 307 | { |
| 308 | CloseLibrary(DosBase); |
| 309 | #ifdef FEAT_ARP |
| 310 | dos2 = TRUE; |
| 311 | #endif |
| 312 | } |
| 313 | else /* without arp functions we NEED 2.0 */ |
| 314 | { |
| 315 | #ifndef FEAT_ARP |
| 316 | mch_errmsg(_("Need Amigados version 2.04 or later\n")); |
| 317 | exit(3); |
| 318 | #else |
| 319 | /* need arp functions for dos 1.x */ |
| 320 | if (!(ArpBase = (struct ArpBase *) OpenLibrary((UBYTE *)ArpName, ArpVersion))) |
| 321 | { |
| 322 | fprintf(stderr, _("Need %s version %ld\n"), ArpName, ArpVersion); |
| 323 | exit(3); |
| 324 | } |
| 325 | #endif |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * scan argv[] for the "-f" and "-d" arguments |
| 330 | */ |
| 331 | for (i = 1; i < argc; ++i) |
| 332 | if (argv[i][0] == '-') |
| 333 | { |
| 334 | switch (argv[i][1]) |
| 335 | { |
| 336 | case 'f': |
| 337 | usewin = TRUE; |
| 338 | break; |
| 339 | |
| 340 | case 'd': |
| 341 | if (i < argc - 1 |
| 342 | #ifdef FEAT_DIFF |
| 343 | /* require using "-dev", "-d" means diff mode */ |
| 344 | && argv[i][2] == 'e' && argv[i][3] == 'v' |
| 345 | #endif |
| 346 | ) |
| 347 | device = (char_u *)argv[i + 1]; |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * If we were not started from workbench, do not have a "-d" or "-dev" |
| 354 | * argument and we have been started with an interactive window, use that |
| 355 | * window. |
| 356 | */ |
| 357 | if (argc != 0 |
| 358 | && device == NULL |
| 359 | && (IsInteractive(Input()) || IsInteractive(Output()))) |
| 360 | return OK; |
| 361 | |
| 362 | /* |
| 363 | * When given the "-f" argument, we open our own window. We can't use the |
| 364 | * newcli trick below, because the calling program (mail, rn, etc.) would not |
| 365 | * know when we are finished. |
| 366 | */ |
| 367 | if (usewin) |
| 368 | { |
| 369 | /* |
| 370 | * Try to open a window. First try the specified device. |
| 371 | * Then try a 24 line 80 column window. |
| 372 | * If that fails, try two smaller ones. |
| 373 | */ |
| 374 | for (i = -1; i < 3; ++i) |
| 375 | { |
| 376 | if (i >= 0) |
| 377 | device = constrings[i]; |
| 378 | if (device != NULL && (raw_in = Open((UBYTE *)device, |
| 379 | (long)MODE_NEWFILE)) != (BPTR)NULL) |
| 380 | break; |
| 381 | } |
| 382 | if (raw_in == (BPTR)NULL) /* all three failed */ |
| 383 | { |
| 384 | mch_errmsg(_(winerr)); |
| 385 | goto exit; |
| 386 | } |
| 387 | raw_out = raw_in; |
| 388 | close_win = TRUE; |
| 389 | return OK; |
| 390 | } |
| 391 | |
| 392 | if ((nilfh = Open((UBYTE *)"NIL:", (long)MODE_NEWFILE)) == (BPTR)NULL) |
| 393 | { |
| 394 | mch_errmsg(_("Cannot open NIL:\n")); |
| 395 | goto exit; |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Make a unique name for the temp file (which we will not delete!). |
| 400 | * Use a pointer on the stack (nobody else will be using it). |
| 401 | */ |
| 402 | sprintf((char *)buf1, "t:nc%ld", (long)buf1); |
| 403 | if ((fh = Open((UBYTE *)buf1, (long)MODE_NEWFILE)) == (BPTR)NULL) |
| 404 | { |
| 405 | mch_errmsg(_("Cannot create ")); |
| 406 | mch_errmsg((char *)buf1); |
| 407 | mch_errmsg("\n"); |
| 408 | goto exit; |
| 409 | } |
| 410 | /* |
| 411 | * Write the command into the file, put quotes around the arguments that |
| 412 | * have a space in them. |
| 413 | */ |
| 414 | if (argc == 0) /* run from workbench */ |
| 415 | ac = ((struct WBStartup *)argv)->sm_NumArgs; |
| 416 | else |
| 417 | ac = argc; |
| 418 | for (i = 0; i < ac; ++i) |
| 419 | { |
| 420 | if (argc == 0) |
| 421 | { |
| 422 | *buf2 = NUL; |
| 423 | argp = &(((struct WBStartup *)argv)->sm_ArgList[i]); |
| 424 | if (argp->wa_Lock) |
| 425 | (void)lock2name(argp->wa_Lock, buf2, (long)(BUF2SIZE - 1)); |
| 426 | #ifdef FEAT_ARP |
| 427 | if (dos2) /* use 2.0 function */ |
| 428 | #endif |
| 429 | AddPart((UBYTE *)buf2, (UBYTE *)argp->wa_Name, (long)(BUF2SIZE - 1)); |
| 430 | #ifdef FEAT_ARP |
| 431 | else /* use arp function */ |
| 432 | TackOn((char *)buf2, argp->wa_Name); |
| 433 | #endif |
| 434 | av = (char *)buf2; |
| 435 | } |
| 436 | else |
| 437 | av = argv[i]; |
| 438 | |
| 439 | /* skip '-d' or "-dev" option */ |
| 440 | if (av[0] == '-' && av[1] == 'd' |
| 441 | #ifdef FEAT_DIFF |
| 442 | && av[2] == 'e' && av[3] == 'v' |
| 443 | #endif |
| 444 | ) |
| 445 | { |
| 446 | ++i; |
| 447 | continue; |
| 448 | } |
| 449 | if (vim_strchr((char_u *)av, ' ')) |
| 450 | Write(fh, "\"", 1L); |
| 451 | Write(fh, av, (long)strlen(av)); |
| 452 | if (vim_strchr((char_u *)av, ' ')) |
| 453 | Write(fh, "\"", 1L); |
| 454 | Write(fh, " ", 1L); |
| 455 | } |
| 456 | Write(fh, "\nendcli\n", 8L); |
| 457 | Close(fh); |
| 458 | |
| 459 | /* |
| 460 | * Try to open a new cli in a window. If "-d" or "-dev" argument was given try |
| 461 | * to open the specified device. Then try a 24 line 80 column window. If that |
| 462 | * fails, try two smaller ones. |
| 463 | */ |
| 464 | for (i = -1; i < 3; ++i) |
| 465 | { |
| 466 | if (i >= 0) |
| 467 | device = constrings[i]; |
| 468 | else if (device == NULL) |
| 469 | continue; |
| 470 | sprintf((char *)buf2, "newcli <nil: >nil: %s from %s", (char *)device, (char *)buf1); |
| 471 | #ifdef FEAT_ARP |
| 472 | if (dos2) |
| 473 | { |
| 474 | #endif |
| 475 | if (!SystemTags((UBYTE *)buf2, SYS_UserShell, TRUE, TAG_DONE)) |
| 476 | break; |
| 477 | #ifdef FEAT_ARP |
| 478 | } |
| 479 | else |
| 480 | { |
| 481 | if (Execute((UBYTE *)buf2, nilfh, nilfh)) |
| 482 | break; |
| 483 | } |
| 484 | #endif |
| 485 | } |
| 486 | if (i == 3) /* all three failed */ |
| 487 | { |
| 488 | DeleteFile((UBYTE *)buf1); |
| 489 | mch_errmsg(_(winerr)); |
| 490 | goto exit; |
| 491 | } |
| 492 | exitval = 0; /* The Execute succeeded: exit this program */ |
| 493 | |
| 494 | exit: |
| 495 | #ifdef FEAT_ARP |
| 496 | if (ArpBase) |
| 497 | CloseLibrary((struct Library *) ArpBase); |
| 498 | #endif |
| 499 | exit(exitval); |
| 500 | /* NOTREACHED */ |
| 501 | return FAIL; |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Return TRUE if the input comes from a terminal, FALSE otherwise. |
| 506 | * We fake there is a window, because we can always open one! |
| 507 | */ |
| 508 | int |
| 509 | mch_input_isatty() |
| 510 | { |
| 511 | return TRUE; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * fname_case(): Set the case of the file name, if it already exists. |
| 516 | * This will cause the file name to remain exactly the same. |
| 517 | */ |
| 518 | /*ARGSUSED*/ |
| 519 | void |
| 520 | fname_case(name, len) |
| 521 | char_u *name; |
| 522 | int len; /* buffer size, ignored here */ |
| 523 | { |
| 524 | struct FileInfoBlock *fib; |
| 525 | size_t flen; |
| 526 | |
| 527 | fib = get_fib(name); |
| 528 | if (fib != NULL) |
| 529 | { |
| 530 | flen = STRLEN(name); |
| 531 | if (flen == strlen(fib->fib_FileName)) /* safety check */ |
| 532 | mch_memmove(name, fib->fib_FileName, flen); |
| 533 | vim_free(fib); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Get the FileInfoBlock for file "fname" |
| 539 | * The returned structure has to be free()d. |
| 540 | * Returns NULL on error. |
| 541 | */ |
| 542 | static struct FileInfoBlock * |
| 543 | get_fib(fname) |
| 544 | char_u *fname; |
| 545 | { |
| 546 | BPTR flock; |
| 547 | struct FileInfoBlock *fib; |
| 548 | |
| 549 | if (fname == NULL) /* safety check */ |
| 550 | return NULL; |
| 551 | fib = (struct FileInfoBlock *)malloc(sizeof(struct FileInfoBlock)); |
| 552 | if (fib != NULL) |
| 553 | { |
| 554 | flock = Lock((UBYTE *)fname, (long)ACCESS_READ); |
| 555 | if (flock == (BPTR)NULL || !Examine(flock, fib)) |
| 556 | { |
| 557 | vim_free(fib); /* in case of an error the memory is freed here */ |
| 558 | fib = NULL; |
| 559 | } |
| 560 | if (flock) |
| 561 | UnLock(flock); |
| 562 | } |
| 563 | return fib; |
| 564 | } |
| 565 | |
| 566 | #ifdef FEAT_TITLE |
| 567 | /* |
| 568 | * set the title of our window |
| 569 | * icon name is not set |
| 570 | */ |
| 571 | void |
| 572 | mch_settitle(title, icon) |
| 573 | char_u *title; |
| 574 | char_u *icon; |
| 575 | { |
| 576 | if (wb_window != NULL && title != NULL) |
| 577 | SetWindowTitles(wb_window, (UBYTE *)title, (UBYTE *)-1L); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * Restore the window/icon title. |
| 582 | * which is one of: |
| 583 | * 1 Just restore title |
| 584 | * 2 Just restore icon (which we don't have) |
| 585 | * 3 Restore title and icon (which we don't have) |
| 586 | */ |
| 587 | void |
| 588 | mch_restore_title(which) |
| 589 | int which; |
| 590 | { |
| 591 | if (which & 1) |
| 592 | mch_settitle(oldwindowtitle, NULL); |
| 593 | } |
| 594 | |
| 595 | int |
| 596 | mch_can_restore_title() |
| 597 | { |
| 598 | return (wb_window != NULL); |
| 599 | } |
| 600 | |
| 601 | int |
| 602 | mch_can_restore_icon() |
| 603 | { |
| 604 | return FALSE; |
| 605 | } |
| 606 | #endif |
| 607 | |
| 608 | /* |
| 609 | * Insert user name in s[len]. |
| 610 | */ |
| 611 | int |
| 612 | mch_get_user_name(s, len) |
| 613 | char_u *s; |
| 614 | int len; |
| 615 | { |
| 616 | *s = NUL; |
| 617 | return FAIL; |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Insert host name is s[len]. |
| 622 | */ |
| 623 | void |
| 624 | mch_get_host_name(s, len) |
| 625 | char_u *s; |
| 626 | int len; |
| 627 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 628 | vim_strncpy(s, "Amiga", len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* |
| 632 | * return process ID |
| 633 | */ |
| 634 | long |
| 635 | mch_get_pid() |
| 636 | { |
| 637 | return (long)0; |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * Get name of current directory into buffer 'buf' of length 'len' bytes. |
| 642 | * Return OK for success, FAIL for failure. |
| 643 | */ |
| 644 | int |
| 645 | mch_dirname(buf, len) |
| 646 | char_u *buf; |
| 647 | int len; |
| 648 | { |
| 649 | return mch_FullName((char_u *)"", buf, len, FALSE); |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * get absolute file name into buffer 'buf' of length 'len' bytes |
| 654 | * |
| 655 | * return FAIL for failure, OK otherwise |
| 656 | */ |
| 657 | int |
| 658 | mch_FullName(fname, buf, len, force) |
| 659 | char_u *fname, *buf; |
| 660 | int len; |
| 661 | int force; |
| 662 | { |
| 663 | BPTR l; |
| 664 | int retval = FAIL; |
| 665 | int i; |
| 666 | |
| 667 | /* Lock the file. If it exists, we can get the exact name. */ |
| 668 | if ((l = Lock((UBYTE *)fname, (long)ACCESS_READ)) != (BPTR)0) |
| 669 | { |
| 670 | retval = lock2name(l, buf, (long)len - 1); |
| 671 | UnLock(l); |
| 672 | } |
| 673 | else if (force || !mch_isFullName(fname)) /* not a full path yet */ |
| 674 | { |
| 675 | /* |
| 676 | * If the file cannot be locked (doesn't exist), try to lock the |
| 677 | * current directory and concatenate the file name. |
| 678 | */ |
| 679 | if ((l = Lock((UBYTE *)"", (long)ACCESS_READ)) != (BPTR)NULL) |
| 680 | { |
| 681 | retval = lock2name(l, buf, (long)len); |
| 682 | UnLock(l); |
| 683 | if (retval == OK) |
| 684 | { |
| 685 | i = STRLEN(buf); |
| 686 | /* Concatenate the fname to the directory. Don't add a slash |
| 687 | * if fname is empty, but do change "" to "/". */ |
| 688 | if (i == 0 || *fname != NUL) |
| 689 | { |
| 690 | if (i < len - 1 && (i == 0 || buf[i - 1] != ':')) |
| 691 | buf[i++] = '/'; |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 692 | vim_strncpy(buf + i, fname, len - i - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | if (*buf == 0 || *buf == ':') |
| 698 | retval = FAIL; /* something failed; use the file name */ |
| 699 | return retval; |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Return TRUE if "fname" does not depend on the current directory. |
| 704 | */ |
| 705 | int |
| 706 | mch_isFullName(fname) |
| 707 | char_u *fname; |
| 708 | { |
| 709 | return (vim_strchr(fname, ':') != NULL && *fname != ':'); |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Get the full file name from a lock. Use 2.0 function if possible, because |
| 714 | * the arp function has more restrictions on the path length. |
| 715 | * |
| 716 | * return FAIL for failure, OK otherwise |
| 717 | */ |
| 718 | static int |
| 719 | lock2name(lock, buf, len) |
| 720 | BPTR lock; |
| 721 | char_u *buf; |
| 722 | long len; |
| 723 | { |
| 724 | #ifdef FEAT_ARP |
| 725 | if (dos2) /* use 2.0 function */ |
| 726 | #endif |
| 727 | return ((int)NameFromLock(lock, (UBYTE *)buf, len) ? OK : FAIL); |
| 728 | #ifdef FEAT_ARP |
| 729 | else /* use arp function */ |
| 730 | return ((int)PathName(lock, (char *)buf, (long)(len/32)) ? OK : FAIL); |
| 731 | #endif |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * get file permissions for 'name' |
| 736 | * Returns -1 when it doesn't exist. |
| 737 | */ |
| 738 | long |
| 739 | mch_getperm(name) |
| 740 | char_u *name; |
| 741 | { |
| 742 | struct FileInfoBlock *fib; |
| 743 | long retval = -1; |
| 744 | |
| 745 | fib = get_fib(name); |
| 746 | if (fib != NULL) |
| 747 | { |
| 748 | retval = fib->fib_Protection; |
| 749 | vim_free(fib); |
| 750 | } |
| 751 | return retval; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * set file permission for 'name' to 'perm' |
| 756 | * |
| 757 | * return FAIL for failure, OK otherwise |
| 758 | */ |
| 759 | int |
| 760 | mch_setperm(name, perm) |
| 761 | char_u *name; |
| 762 | long perm; |
| 763 | { |
| 764 | perm &= ~FIBF_ARCHIVE; /* reset archived bit */ |
| 765 | return (SetProtection((UBYTE *)name, (long)perm) ? OK : FAIL); |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Set hidden flag for "name". |
| 770 | */ |
| 771 | void |
| 772 | mch_hide(name) |
| 773 | char_u *name; |
| 774 | { |
| 775 | /* can't hide a file */ |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * return FALSE if "name" is not a directory |
| 780 | * return TRUE if "name" is a directory. |
| 781 | * return FALSE for error. |
| 782 | */ |
| 783 | int |
| 784 | mch_isdir(name) |
| 785 | char_u *name; |
| 786 | { |
| 787 | struct FileInfoBlock *fib; |
| 788 | int retval = FALSE; |
| 789 | |
| 790 | fib = get_fib(name); |
| 791 | if (fib != NULL) |
| 792 | { |
| 793 | retval = ((fib->fib_DirEntryType >= 0) ? TRUE : FALSE); |
| 794 | vim_free(fib); |
| 795 | } |
| 796 | return retval; |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * Create directory "name". |
| 801 | */ |
Bram Moolenaar | e385364 | 2006-09-14 19:36:57 +0000 | [diff] [blame] | 802 | int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 803 | mch_mkdir(name) |
| 804 | char_u *name; |
| 805 | { |
| 806 | BPTR lock; |
| 807 | |
| 808 | lock = CreateDir(name); |
| 809 | if (lock != NULL) |
Bram Moolenaar | e385364 | 2006-09-14 19:36:57 +0000 | [diff] [blame] | 810 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 811 | UnLock(lock); |
Bram Moolenaar | e385364 | 2006-09-14 19:36:57 +0000 | [diff] [blame] | 812 | return 0; |
| 813 | } |
| 814 | return -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | /* |
| 818 | * Return 1 if "name" can be executed, 0 if not. |
| 819 | * Return -1 if unknown. |
| 820 | */ |
| 821 | int |
| 822 | mch_can_exe(name) |
| 823 | char_u *name; |
| 824 | { |
| 825 | /* TODO */ |
| 826 | return -1; |
| 827 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 828 | |
| 829 | /* |
| 830 | * Check what "name" is: |
| 831 | * NODE_NORMAL: file or directory (or doesn't exist) |
| 832 | * NODE_WRITABLE: writable device, socket, fifo, etc. |
| 833 | * NODE_OTHER: non-writable things |
| 834 | */ |
| 835 | int |
| 836 | mch_nodetype(name) |
| 837 | char_u *name; |
| 838 | { |
| 839 | /* TODO */ |
| 840 | return NODE_NORMAL; |
| 841 | } |
| 842 | |
| 843 | void |
| 844 | mch_early_init() |
| 845 | { |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * Careful: mch_exit() may be called before mch_init()! |
| 850 | */ |
| 851 | void |
| 852 | mch_exit(r) |
| 853 | int r; |
| 854 | { |
| 855 | if (raw_in) /* put terminal in 'normal' mode */ |
| 856 | { |
| 857 | settmode(TMODE_COOK); |
| 858 | stoptermcap(); |
| 859 | } |
| 860 | out_char('\n'); |
| 861 | if (raw_out) |
| 862 | { |
| 863 | if (term_console) |
| 864 | { |
| 865 | win_resize_off(); /* window resize events de-activated */ |
| 866 | if (size_set) |
| 867 | OUT_STR("\233t\233u"); /* reset window size (CSI t CSI u) */ |
| 868 | } |
| 869 | out_flush(); |
| 870 | } |
| 871 | |
| 872 | #ifdef FEAT_TITLE |
| 873 | mch_restore_title(3); /* restore window title */ |
| 874 | #endif |
| 875 | |
| 876 | ml_close_all(TRUE); /* remove all memfiles */ |
| 877 | |
| 878 | #ifdef FEAT_ARP |
| 879 | if (ArpBase) |
| 880 | CloseLibrary((struct Library *) ArpBase); |
| 881 | #endif |
| 882 | if (close_win) |
| 883 | Close(raw_in); |
| 884 | if (r) |
| 885 | printf(_("Vim exiting with %d\n"), r); /* somehow this makes :cq work!? */ |
| 886 | exit(r); |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | * This is a routine for setting a given stream to raw or cooked mode on the |
| 891 | * Amiga . This is useful when you are using Lattice C to produce programs |
| 892 | * that want to read single characters with the "getch()" or "fgetc" call. |
| 893 | * |
| 894 | * Written : 18-Jun-87 By Chuck McManis. |
| 895 | */ |
| 896 | |
| 897 | #define MP(xx) ((struct MsgPort *)((struct FileHandle *) (BADDR(xx)))->fh_Type) |
| 898 | |
| 899 | /* |
| 900 | * Function mch_settmode() - Convert the specified file pointer to 'raw' or |
| 901 | * 'cooked' mode. This only works on TTY's. |
| 902 | * |
| 903 | * Raw: keeps DOS from translating keys for you, also (BIG WIN) it means |
| 904 | * getch() will return immediately rather than wait for a return. You |
| 905 | * lose editing features though. |
| 906 | * |
| 907 | * Cooked: This function returns the designate file pointer to it's normal, |
| 908 | * wait for a <CR> mode. This is exactly like raw() except that |
| 909 | * it sends a 0 to the console to make it back into a CON: from a RAW: |
| 910 | */ |
| 911 | void |
| 912 | mch_settmode(tmode) |
| 913 | int tmode; |
| 914 | { |
| 915 | #ifdef __AROS__ |
| 916 | if (!SetMode(raw_in, tmode == TMODE_RAW ? 1 : 0)) |
| 917 | #else |
| 918 | if (dos_packet(MP(raw_in), (long)ACTION_SCREEN_MODE, |
| 919 | tmode == TMODE_RAW ? -1L : 0L) == 0) |
| 920 | #endif |
| 921 | mch_errmsg(_("cannot change console mode ?!\n")); |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * set screen mode, always fails. |
| 926 | */ |
| 927 | int |
| 928 | mch_screenmode(arg) |
| 929 | char_u *arg; |
| 930 | { |
| 931 | EMSG(_(e_screenmode)); |
| 932 | return FAIL; |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * Code for this routine came from the following : |
| 937 | * |
| 938 | * ConPackets.c - C. Scheppner, A. Finkel, P. Lindsay CBM |
| 939 | * DOS packet example |
| 940 | * Requires 1.2 |
| 941 | * |
| 942 | * Found on Fish Disk 56. |
| 943 | * |
| 944 | * Heavely modified by mool. |
| 945 | */ |
| 946 | |
| 947 | #include <devices/conunit.h> |
| 948 | |
| 949 | /* |
| 950 | * try to get the real window size |
| 951 | * return FAIL for failure, OK otherwise |
| 952 | */ |
| 953 | int |
| 954 | mch_get_shellsize() |
| 955 | { |
| 956 | struct ConUnit *conUnit; |
| 957 | char id_a[sizeof(struct InfoData) + 3]; |
| 958 | struct InfoData *id; |
| 959 | |
| 960 | if (!term_console) /* not an amiga window */ |
| 961 | return FAIL; |
| 962 | |
| 963 | /* insure longword alignment */ |
| 964 | id = (struct InfoData *)(((long)id_a + 3L) & ~3L); |
| 965 | |
| 966 | /* |
| 967 | * Should make console aware of real window size, not the one we set. |
| 968 | * Unfortunately, under DOS 2.0x this redraws the window and it |
| 969 | * is rarely needed, so we skip it now, unless we changed the size. |
| 970 | */ |
| 971 | if (size_set) |
| 972 | OUT_STR("\233t\233u"); /* CSI t CSI u */ |
| 973 | out_flush(); |
| 974 | |
| 975 | #ifdef __AROS__ |
| 976 | if (!Info(raw_out, id) |
| 977 | || (wb_window = (struct Window *) id->id_VolumeNode) == NULL) |
| 978 | #else |
| 979 | if (dos_packet(MP(raw_out), (long)ACTION_DISK_INFO, ((ULONG) id) >> 2) == 0 |
| 980 | || (wb_window = (struct Window *)id->id_VolumeNode) == NULL) |
| 981 | #endif |
| 982 | { |
| 983 | /* it's not an amiga window, maybe aux device */ |
| 984 | /* terminal type should be set */ |
| 985 | term_console = FALSE; |
| 986 | return FAIL; |
| 987 | } |
| 988 | if (oldwindowtitle == NULL) |
| 989 | oldwindowtitle = (char_u *)wb_window->Title; |
| 990 | if (id->id_InUse == (BPTR)NULL) |
| 991 | { |
| 992 | mch_errmsg(_("mch_get_shellsize: not a console??\n")); |
| 993 | return FAIL; |
| 994 | } |
| 995 | conUnit = (struct ConUnit *) ((struct IOStdReq *) id->id_InUse)->io_Unit; |
| 996 | |
| 997 | /* get window size */ |
| 998 | Rows = conUnit->cu_YMax + 1; |
| 999 | Columns = conUnit->cu_XMax + 1; |
| 1000 | if (Rows < 0 || Rows > 200) /* cannot be an amiga window */ |
| 1001 | { |
| 1002 | Columns = 80; |
| 1003 | Rows = 24; |
| 1004 | term_console = FALSE; |
| 1005 | return FAIL; |
| 1006 | } |
| 1007 | |
| 1008 | return OK; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * Try to set the real window size to Rows and Columns. |
| 1013 | */ |
| 1014 | void |
| 1015 | mch_set_shellsize() |
| 1016 | { |
| 1017 | if (term_console) |
| 1018 | { |
| 1019 | size_set = TRUE; |
| 1020 | out_char(CSI); |
| 1021 | out_num((long)Rows); |
| 1022 | out_char('t'); |
| 1023 | out_char(CSI); |
| 1024 | out_num((long)Columns); |
| 1025 | out_char('u'); |
| 1026 | out_flush(); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | * Rows and/or Columns has changed. |
| 1032 | */ |
| 1033 | void |
| 1034 | mch_new_shellsize() |
| 1035 | { |
| 1036 | /* Nothing to do. */ |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * out_num - output a (big) number fast |
| 1041 | */ |
| 1042 | static void |
| 1043 | out_num(n) |
| 1044 | long n; |
| 1045 | { |
| 1046 | OUT_STR_NF(tltoa((unsigned long)n)); |
| 1047 | } |
| 1048 | |
| 1049 | #if !defined(AZTEC_C) && !defined(__AROS__) |
| 1050 | /* |
| 1051 | * Sendpacket.c |
| 1052 | * |
| 1053 | * An invaluable addition to your Amiga.lib file. This code sends a packet to |
| 1054 | * the given message port. This makes working around DOS lots easier. |
| 1055 | * |
| 1056 | * Note, I didn't write this, those wonderful folks at CBM did. I do suggest |
| 1057 | * however that you may wish to add it to Amiga.Lib, to do so, compile it and |
| 1058 | * say 'oml lib:amiga.lib -r sendpacket.o' |
| 1059 | */ |
| 1060 | |
| 1061 | /* #include <proto/exec.h> */ |
| 1062 | /* #include <proto/dos.h> */ |
| 1063 | #include <exec/memory.h> |
| 1064 | |
| 1065 | /* |
| 1066 | * Function - dos_packet written by Phil Lindsay, Carolyn Scheppner, and Andy |
| 1067 | * Finkel. This function will send a packet of the given type to the Message |
| 1068 | * Port supplied. |
| 1069 | */ |
| 1070 | |
| 1071 | static long |
| 1072 | dos_packet(pid, action, arg) |
| 1073 | struct MsgPort *pid; /* process indentifier ... (handlers message port) */ |
| 1074 | long action, /* packet type ... (what you want handler to do) */ |
| 1075 | arg; /* single argument */ |
| 1076 | { |
| 1077 | # ifdef FEAT_ARP |
| 1078 | struct MsgPort *replyport; |
| 1079 | struct StandardPacket *packet; |
| 1080 | long res1; |
| 1081 | |
| 1082 | if (dos2) |
| 1083 | # endif |
| 1084 | return DoPkt(pid, action, arg, 0L, 0L, 0L, 0L); /* use 2.0 function */ |
| 1085 | # ifdef FEAT_ARP |
| 1086 | |
| 1087 | replyport = (struct MsgPort *) CreatePort(NULL, 0); /* use arp function */ |
| 1088 | if (!replyport) |
| 1089 | return (0); |
| 1090 | |
| 1091 | /* Allocate space for a packet, make it public and clear it */ |
| 1092 | packet = (struct StandardPacket *) |
| 1093 | AllocMem((long) sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR); |
| 1094 | if (!packet) { |
| 1095 | DeletePort(replyport); |
| 1096 | return (0); |
| 1097 | } |
| 1098 | packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt); |
| 1099 | packet->sp_Pkt.dp_Link = &(packet->sp_Msg); |
| 1100 | packet->sp_Pkt.dp_Port = replyport; |
| 1101 | packet->sp_Pkt.dp_Type = action; |
| 1102 | packet->sp_Pkt.dp_Arg1 = arg; |
| 1103 | |
| 1104 | PutMsg(pid, (struct Message *)packet); /* send packet */ |
| 1105 | |
| 1106 | WaitPort(replyport); |
| 1107 | GetMsg(replyport); |
| 1108 | |
| 1109 | res1 = packet->sp_Pkt.dp_Res1; |
| 1110 | |
| 1111 | FreeMem(packet, (long) sizeof(struct StandardPacket)); |
| 1112 | DeletePort(replyport); |
| 1113 | |
| 1114 | return (res1); |
| 1115 | # endif |
| 1116 | } |
| 1117 | #endif /* !defined(AZTEC_C) && !defined(__AROS__) */ |
| 1118 | |
| 1119 | /* |
| 1120 | * Call shell. |
| 1121 | * Return error number for failure, 0 otherwise |
| 1122 | */ |
| 1123 | int |
| 1124 | mch_call_shell(cmd, options) |
| 1125 | char_u *cmd; |
| 1126 | int options; /* SHELL_*, see vim.h */ |
| 1127 | { |
| 1128 | BPTR mydir; |
| 1129 | int x; |
| 1130 | int tmode = cur_tmode; |
| 1131 | #ifdef AZTEC_C |
| 1132 | int use_execute; |
| 1133 | char_u *shellcmd = NULL; |
| 1134 | char_u *shellarg; |
| 1135 | #endif |
| 1136 | int retval = 0; |
| 1137 | |
| 1138 | if (close_win) |
| 1139 | { |
| 1140 | /* if Vim opened a window: Executing a shell may cause crashes */ |
| 1141 | EMSG(_("E360: Cannot execute shell with -f option")); |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
| 1145 | if (term_console) |
| 1146 | win_resize_off(); /* window resize events de-activated */ |
| 1147 | out_flush(); |
| 1148 | |
| 1149 | if (options & SHELL_COOKED) |
| 1150 | settmode(TMODE_COOK); /* set to normal mode */ |
| 1151 | mydir = Lock((UBYTE *)"", (long)ACCESS_READ); /* remember current dir */ |
| 1152 | |
| 1153 | #if !defined(AZTEC_C) /* not tested very much */ |
| 1154 | if (cmd == NULL) |
| 1155 | { |
| 1156 | # ifdef FEAT_ARP |
| 1157 | if (dos2) |
| 1158 | # endif |
| 1159 | x = SystemTags(p_sh, SYS_UserShell, TRUE, TAG_DONE); |
| 1160 | # ifdef FEAT_ARP |
| 1161 | else |
| 1162 | x = Execute(p_sh, raw_in, raw_out); |
| 1163 | # endif |
| 1164 | } |
| 1165 | else |
| 1166 | { |
| 1167 | # ifdef FEAT_ARP |
| 1168 | if (dos2) |
| 1169 | # endif |
| 1170 | x = SystemTags((char *)cmd, SYS_UserShell, TRUE, TAG_DONE); |
| 1171 | # ifdef FEAT_ARP |
| 1172 | else |
| 1173 | x = Execute((char *)cmd, 0L, raw_out); |
| 1174 | # endif |
| 1175 | } |
| 1176 | # ifdef FEAT_ARP |
| 1177 | if ((dos2 && x < 0) || (!dos2 && !x)) |
| 1178 | # else |
| 1179 | if (x < 0) |
| 1180 | # endif |
| 1181 | { |
| 1182 | MSG_PUTS(_("Cannot execute ")); |
| 1183 | if (cmd == NULL) |
| 1184 | { |
| 1185 | MSG_PUTS(_("shell ")); |
| 1186 | msg_outtrans(p_sh); |
| 1187 | } |
| 1188 | else |
| 1189 | msg_outtrans(cmd); |
| 1190 | msg_putchar('\n'); |
| 1191 | retval = -1; |
| 1192 | } |
| 1193 | # ifdef FEAT_ARP |
| 1194 | else if (!dos2 || x) |
| 1195 | # else |
| 1196 | else if (x) |
| 1197 | # endif |
| 1198 | { |
| 1199 | if ((x = IoErr()) != 0) |
| 1200 | { |
| 1201 | if (!(options & SHELL_SILENT)) |
| 1202 | { |
| 1203 | msg_putchar('\n'); |
| 1204 | msg_outnum((long)x); |
| 1205 | MSG_PUTS(_(" returned\n")); |
| 1206 | } |
| 1207 | retval = x; |
| 1208 | } |
| 1209 | } |
| 1210 | #else /* else part is for AZTEC_C */ |
| 1211 | if (p_st >= 4 || (p_st >= 2 && !(options & SHELL_FILTER))) |
| 1212 | use_execute = 1; |
| 1213 | else |
| 1214 | use_execute = 0; |
| 1215 | if (!use_execute) |
| 1216 | { |
| 1217 | /* |
| 1218 | * separate shell name from argument |
| 1219 | */ |
| 1220 | shellcmd = vim_strsave(p_sh); |
| 1221 | if (shellcmd == NULL) /* out of memory, use Execute */ |
| 1222 | use_execute = 1; |
| 1223 | else |
| 1224 | { |
| 1225 | shellarg = skiptowhite(shellcmd); /* find start of arguments */ |
| 1226 | if (*shellarg != NUL) |
| 1227 | { |
| 1228 | *shellarg++ = NUL; |
| 1229 | shellarg = skipwhite(shellarg); |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | if (cmd == NULL) |
| 1234 | { |
| 1235 | if (use_execute) |
| 1236 | { |
| 1237 | # ifdef FEAT_ARP |
| 1238 | if (dos2) |
| 1239 | # endif |
| 1240 | x = SystemTags((UBYTE *)p_sh, SYS_UserShell, TRUE, TAG_DONE); |
| 1241 | # ifdef FEAT_ARP |
| 1242 | else |
| 1243 | x = !Execute((UBYTE *)p_sh, raw_in, raw_out); |
| 1244 | # endif |
| 1245 | } |
| 1246 | else |
| 1247 | x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, NULL); |
| 1248 | } |
| 1249 | else if (use_execute) |
| 1250 | { |
| 1251 | # ifdef FEAT_ARP |
| 1252 | if (dos2) |
| 1253 | # endif |
| 1254 | x = SystemTags((UBYTE *)cmd, SYS_UserShell, TRUE, TAG_DONE); |
| 1255 | # ifdef FEAT_ARP |
| 1256 | else |
| 1257 | x = !Execute((UBYTE *)cmd, 0L, raw_out); |
| 1258 | # endif |
| 1259 | } |
| 1260 | else if (p_st & 1) |
| 1261 | x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, |
| 1262 | (char *)cmd, NULL); |
| 1263 | else |
| 1264 | x = fexecl((char *)shellcmd, (char *)shellcmd, (char *)shellarg, |
| 1265 | (char *)p_shcf, (char *)cmd, NULL); |
| 1266 | # ifdef FEAT_ARP |
| 1267 | if ((dos2 && x < 0) || (!dos2 && x)) |
| 1268 | # else |
| 1269 | if (x < 0) |
| 1270 | # endif |
| 1271 | { |
| 1272 | MSG_PUTS(_("Cannot execute ")); |
| 1273 | if (use_execute) |
| 1274 | { |
| 1275 | if (cmd == NULL) |
| 1276 | msg_outtrans(p_sh); |
| 1277 | else |
| 1278 | msg_outtrans(cmd); |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | MSG_PUTS(_("shell ")); |
| 1283 | msg_outtrans(shellcmd); |
| 1284 | } |
| 1285 | msg_putchar('\n'); |
| 1286 | retval = -1; |
| 1287 | } |
| 1288 | else |
| 1289 | { |
| 1290 | if (use_execute) |
| 1291 | { |
| 1292 | # ifdef FEAT_ARP |
| 1293 | if (!dos2 || x) |
| 1294 | # else |
| 1295 | if (x) |
| 1296 | # endif |
| 1297 | x = IoErr(); |
| 1298 | } |
| 1299 | else |
| 1300 | x = wait(); |
| 1301 | if (x) |
| 1302 | { |
| 1303 | if (!(options & SHELL_SILENT) && !emsg_silent) |
| 1304 | { |
| 1305 | msg_putchar('\n'); |
| 1306 | msg_outnum((long)x); |
| 1307 | MSG_PUTS(_(" returned\n")); |
| 1308 | } |
| 1309 | retval = x; |
| 1310 | } |
| 1311 | } |
| 1312 | vim_free(shellcmd); |
| 1313 | #endif /* AZTEC_C */ |
| 1314 | |
| 1315 | if ((mydir = CurrentDir(mydir)) != 0) /* make sure we stay in the same directory */ |
| 1316 | UnLock(mydir); |
| 1317 | if (tmode == TMODE_RAW) |
| 1318 | settmode(TMODE_RAW); /* set to raw mode */ |
| 1319 | #ifdef FEAT_TITLE |
| 1320 | resettitle(); |
| 1321 | #endif |
| 1322 | if (term_console) |
| 1323 | win_resize_on(); /* window resize events activated */ |
| 1324 | return retval; |
| 1325 | } |
| 1326 | |
| 1327 | /* |
| 1328 | * check for an "interrupt signal" |
| 1329 | * We only react to a CTRL-C, but also clear the other break signals to avoid |
| 1330 | * trouble with lattice-c programs. |
| 1331 | */ |
| 1332 | void |
| 1333 | mch_breakcheck() |
| 1334 | { |
| 1335 | if (SetSignal(0L, (long)(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F)) & SIGBREAKF_CTRL_C) |
| 1336 | got_int = TRUE; |
| 1337 | } |
| 1338 | |
| 1339 | /* this routine causes manx to use this Chk_Abort() rather than it's own */ |
| 1340 | /* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */ |
| 1341 | /* is zero). Since we want to check for our own ^C's */ |
| 1342 | |
| 1343 | #ifdef _DCC |
| 1344 | #define Chk_Abort chkabort |
| 1345 | #endif |
| 1346 | |
| 1347 | #ifdef LATTICE |
| 1348 | void __regargs __chkabort(void); |
| 1349 | |
| 1350 | void __regargs __chkabort(void) |
| 1351 | {} |
| 1352 | |
| 1353 | #else |
| 1354 | long |
| 1355 | Chk_Abort(void) |
| 1356 | { |
| 1357 | return(0L); |
| 1358 | } |
| 1359 | #endif |
| 1360 | |
| 1361 | /* |
| 1362 | * mch_expandpath() - this code does wild-card pattern matching using the arp |
| 1363 | * routines. |
| 1364 | * |
| 1365 | * "pat" has backslashes before chars that are not to be expanded. |
| 1366 | * Returns the number of matches found. |
| 1367 | * |
| 1368 | * This is based on WildDemo2.c (found in arp1.1 distribution). |
| 1369 | * That code's copyright follows: |
| 1370 | * Copyright (c) 1987, Scott Ballantyne |
| 1371 | * Use and abuse as you please. |
| 1372 | */ |
| 1373 | |
| 1374 | #define ANCHOR_BUF_SIZE (512) |
| 1375 | #define ANCHOR_SIZE (sizeof(struct AnchorPath) + ANCHOR_BUF_SIZE) |
| 1376 | |
| 1377 | int |
| 1378 | mch_expandpath(gap, pat, flags) |
| 1379 | garray_T *gap; |
| 1380 | char_u *pat; |
| 1381 | int flags; /* EW_* flags */ |
| 1382 | { |
| 1383 | struct AnchorPath *Anchor; |
| 1384 | LONG Result; |
| 1385 | char_u *starbuf, *sp, *dp; |
| 1386 | int start_len; |
| 1387 | int matches; |
| 1388 | |
| 1389 | start_len = gap->ga_len; |
| 1390 | |
| 1391 | /* Get our AnchorBase */ |
| 1392 | Anchor = (struct AnchorPath *)alloc_clear((unsigned)ANCHOR_SIZE); |
| 1393 | if (Anchor == NULL) |
| 1394 | return 0; |
| 1395 | |
| 1396 | Anchor->ap_Strlen = ANCHOR_BUF_SIZE; /* ap_Length not supported anymore */ |
| 1397 | #ifdef APF_DODOT |
| 1398 | Anchor->ap_Flags = APF_DODOT | APF_DOWILD; /* allow '.' for current dir */ |
| 1399 | #else |
| 1400 | Anchor->ap_Flags = APF_DoDot | APF_DoWild; /* allow '.' for current dir */ |
| 1401 | #endif |
| 1402 | |
| 1403 | #ifdef FEAT_ARP |
| 1404 | if (dos2) |
| 1405 | { |
| 1406 | #endif |
| 1407 | /* hack to replace '*' by '#?' */ |
| 1408 | starbuf = alloc((unsigned)(2 * STRLEN(pat) + 1)); |
| 1409 | if (starbuf == NULL) |
| 1410 | goto Return; |
| 1411 | for (sp = pat, dp = starbuf; *sp; ++sp) |
| 1412 | { |
| 1413 | if (*sp == '*') |
| 1414 | { |
| 1415 | *dp++ = '#'; |
| 1416 | *dp++ = '?'; |
| 1417 | } |
| 1418 | else |
| 1419 | *dp++ = *sp; |
| 1420 | } |
| 1421 | *dp = NUL; |
| 1422 | Result = MatchFirst((UBYTE *)starbuf, Anchor); |
| 1423 | vim_free(starbuf); |
| 1424 | #ifdef FEAT_ARP |
| 1425 | } |
| 1426 | else |
| 1427 | Result = FindFirst((char *)pat, Anchor); |
| 1428 | #endif |
| 1429 | |
| 1430 | /* |
| 1431 | * Loop to get all matches. |
| 1432 | */ |
| 1433 | while (Result == 0) |
| 1434 | { |
| 1435 | addfile(gap, (char_u *)Anchor->ap_Buf, flags); |
| 1436 | #ifdef FEAT_ARP |
| 1437 | if (dos2) |
| 1438 | #endif |
| 1439 | Result = MatchNext(Anchor); |
| 1440 | #ifdef FEAT_ARP |
| 1441 | else |
| 1442 | Result = FindNext(Anchor); |
| 1443 | #endif |
| 1444 | } |
| 1445 | matches = gap->ga_len - start_len; |
| 1446 | |
| 1447 | if (Result == ERROR_BUFFER_OVERFLOW) |
| 1448 | EMSG(_("ANCHOR_BUF_SIZE too small.")); |
| 1449 | else if (matches == 0 && Result != ERROR_OBJECT_NOT_FOUND |
| 1450 | && Result != ERROR_DEVICE_NOT_MOUNTED |
| 1451 | && Result != ERROR_NO_MORE_ENTRIES) |
| 1452 | EMSG(_("I/O ERROR")); |
| 1453 | |
| 1454 | /* |
| 1455 | * Sort the files for this pattern. |
| 1456 | */ |
| 1457 | if (matches) |
| 1458 | qsort((void *)(((char_u **)gap->ga_data) + start_len), |
| 1459 | (size_t)matches, sizeof(char_u *), sortcmp); |
| 1460 | |
| 1461 | /* Free the wildcard stuff */ |
| 1462 | #ifdef FEAT_ARP |
| 1463 | if (dos2) |
| 1464 | #endif |
| 1465 | MatchEnd(Anchor); |
| 1466 | #ifdef FEAT_ARP |
| 1467 | else |
| 1468 | FreeAnchorChain(Anchor); |
| 1469 | #endif |
| 1470 | |
| 1471 | Return: |
| 1472 | vim_free(Anchor); |
| 1473 | |
| 1474 | return matches; |
| 1475 | } |
| 1476 | |
| 1477 | static int |
| 1478 | sortcmp(a, b) |
| 1479 | const void *a, *b; |
| 1480 | { |
| 1481 | char *s = *(char **)a; |
| 1482 | char *t = *(char **)b; |
| 1483 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1484 | return pathcmp(s, t, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /* |
| 1488 | * Return TRUE if "p" has wildcards that can be expanded by mch_expandpath(). |
| 1489 | */ |
| 1490 | int |
| 1491 | mch_has_exp_wildcard(p) |
| 1492 | char_u *p; |
| 1493 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1494 | for ( ; *p; mb_ptr_adv(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1495 | { |
| 1496 | if (*p == '\\' && p[1] != NUL) |
| 1497 | ++p; |
| 1498 | else if (vim_strchr((char_u *)"*?[(#", *p) != NULL) |
| 1499 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1500 | } |
| 1501 | return FALSE; |
| 1502 | } |
| 1503 | |
| 1504 | int |
| 1505 | mch_has_wildcard(p) |
| 1506 | char_u *p; |
| 1507 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1508 | for ( ; *p; mb_ptr_adv(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1509 | { |
| 1510 | if (*p == '\\' && p[1] != NUL) |
| 1511 | ++p; |
| 1512 | else |
| 1513 | if (vim_strchr((char_u *) |
| 1514 | # ifdef VIM_BACKTICK |
| 1515 | "*?[(#$`" |
| 1516 | # else |
| 1517 | "*?[(#$" |
| 1518 | # endif |
| 1519 | , *p) != NULL |
| 1520 | || (*p == '~' && p[1] != NUL)) |
| 1521 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1522 | } |
| 1523 | return FALSE; |
| 1524 | } |
| 1525 | |
| 1526 | /* |
| 1527 | * With AmigaDOS 2.0 support for reading local environment variables |
| 1528 | * |
| 1529 | * Two buffers are allocated: |
| 1530 | * - A big one to do the expansion into. It is freed before returning. |
| 1531 | * - A small one to hold the return value. It is kept until the next call. |
| 1532 | */ |
| 1533 | char_u * |
| 1534 | mch_getenv(var) |
| 1535 | char_u *var; |
| 1536 | { |
| 1537 | int len; |
| 1538 | UBYTE *buf; /* buffer to expand in */ |
| 1539 | char_u *retval; /* return value */ |
| 1540 | static char_u *alloced = NULL; /* allocated memory */ |
| 1541 | |
| 1542 | #ifdef FEAT_ARP |
| 1543 | if (!dos2) |
| 1544 | retval = (char_u *)getenv((char *)var); |
| 1545 | else |
| 1546 | #endif |
| 1547 | { |
| 1548 | vim_free(alloced); |
| 1549 | alloced = NULL; |
| 1550 | retval = NULL; |
| 1551 | |
| 1552 | buf = alloc(IOSIZE); |
| 1553 | if (buf == NULL) |
| 1554 | return NULL; |
| 1555 | |
| 1556 | len = GetVar((UBYTE *)var, buf, (long)(IOSIZE - 1), (long)0); |
| 1557 | if (len >= 0) |
| 1558 | { |
| 1559 | retval = vim_strsave((char_u *)buf); |
| 1560 | alloced = retval; |
| 1561 | } |
| 1562 | |
| 1563 | vim_free(buf); |
| 1564 | } |
| 1565 | |
| 1566 | /* if $VIM is not defined, use "vim:" instead */ |
| 1567 | if (retval == NULL && STRCMP(var, "VIM") == 0) |
| 1568 | retval = (char_u *)"vim:"; |
| 1569 | |
| 1570 | return retval; |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * Amiga version of setenv() with AmigaDOS 2.0 support. |
| 1575 | */ |
| 1576 | /* ARGSUSED */ |
| 1577 | int |
| 1578 | mch_setenv(var, value, x) |
| 1579 | char *var; |
| 1580 | char *value; |
| 1581 | int x; |
| 1582 | { |
| 1583 | #ifdef FEAT_ARP |
| 1584 | if (!dos2) |
| 1585 | return setenv(var, value); |
| 1586 | #endif |
| 1587 | |
| 1588 | if (SetVar((UBYTE *)var, (UBYTE *)value, (LONG)-1, (ULONG)GVF_LOCAL_ONLY)) |
| 1589 | return 0; /* success */ |
| 1590 | return -1; /* failure */ |
| 1591 | } |