Subversion Repositories SvarDOS

Rev

Rev 1797 | Rev 1822 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
421 mateuszvis 1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
3
 *
1714 mateusz.vi 4
 * Copyright (C) 2021-2024 Mateusz Viste
421 mateuszvis 5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
23
 */
24
 
349 mateuszvis 25
#include <i86.h>
26
#include <dos.h>
27
#include <stdio.h>
350 mateuszvis 28
#include <stdlib.h>
349 mateuszvis 29
#include <string.h>
30
 
1001 mateusz.vi 31
#include "svarlang.lib/svarlang.h"
349 mateuszvis 32
 
352 mateuszvis 33
#include "cmd.h"
366 mateuszvis 34
#include "env.h"
352 mateuszvis 35
#include "helpers.h"
402 mateuszvis 36
#include "redir.h"
351 mateuszvis 37
#include "rmodinit.h"
448 mateuszvis 38
#include "sayonara.h"
349 mateuszvis 39
 
479 mateuszvis 40
#include "rmodcore.h" /* rmod binary inside a BUFFER array */
443 mateuszvis 41
 
572 mateuszvis 42
/* this version byte is used to tag RMOD so I can easily make sure that
43
 * the RMOD struct I find in memory is one that I know. Should the version
44
 * mismatch, then it would likely mean that SvarCOM has been upgraded and
45
 * RMOD should not be accessed as its structure might no longer be in sync
46
 * with what I think it is.
1715 mateusz.vi 47
 *          *** INCREMENT THIS AT EACH NEW SVARCOM RELEASE! ***
48
 *            (or at least whenever RMOD's struct is changed)            */
1730 mateusz.vi 49
#define BYTE_VERSION 5
572 mateuszvis 50
 
51
 
349 mateuszvis 52
struct config {
449 mateuszvis 53
  unsigned char flags; /* command.com flags, as defined in rmodinit.h */
443 mateuszvis 54
  char *execcmd;
410 mateuszvis 55
  unsigned short envsiz;
443 mateuszvis 56
};
349 mateuszvis 57
 
490 mateuszvis 58
/* max length of the cmdline storage (bytes) - includes also max length of
59
 * line loaded from a BAT file (no more than 255 bytes!) */
60
#define CMDLINE_MAXLEN 255
349 mateuszvis 61
 
490 mateuszvis 62
 
63
/* sets guard values at a few places in memory for later detection of
64
 * overflows via memguard_check() */
500 mateuszvis 65
static void memguard_set(char *cmdlinebuf) {
490 mateuszvis 66
  BUFFER[sizeof(BUFFER) - 1] = 0xC7;
500 mateuszvis 67
  cmdlinebuf[CMDLINE_MAXLEN] = 0xC7;
490 mateuszvis 68
}
69
 
70
 
71
/* checks for valguards at specific memory locations, returns 0 on success */
500 mateuszvis 72
static int memguard_check(unsigned short rmodseg, char *cmdlinebuf) {
490 mateuszvis 73
  /* check RMOD signature (would be overwritten in case of stack overflow */
74
  static char msg[] = "!! MEMORY CORRUPTION ## DETECTED !!";
75
  unsigned short far *rmodsig = MK_FP(rmodseg, 0x100 + 6);
548 mateuszvis 76
  unsigned char far *rmod = MK_FP(rmodseg, 0);
77
 
490 mateuszvis 78
  if (*rmodsig != 0x2019) {
79
    msg[22] = '1';
548 mateuszvis 80
    goto FAIL;
490 mateuszvis 81
  }
548 mateuszvis 82
 
500 mateuszvis 83
  /* check last BUFFER byte */
490 mateuszvis 84
  if (BUFFER[sizeof(BUFFER) - 1] != 0xC7) {
85
    msg[22] = '2';
548 mateuszvis 86
    goto FAIL;
490 mateuszvis 87
  }
548 mateuszvis 88
 
500 mateuszvis 89
  /* check last cmdlinebuf byte */
90
  if (cmdlinebuf[CMDLINE_MAXLEN] != 0xC7) {
490 mateuszvis 91
    msg[22] = '3';
548 mateuszvis 92
    goto FAIL;
490 mateuszvis 93
  }
548 mateuszvis 94
 
95
  /* check rmod exec buf */
96
  if (rmod[RMOD_OFFSET_EXECPROG + 127] != 0) {
97
    msg[22] = '4';
98
    goto FAIL;
99
  }
100
 
101
  /* check rmod exec stdin buf */
102
  if (rmod[RMOD_OFFSET_STDINFILE + 127] != 0) {
103
    msg[22] = '5';
104
    goto FAIL;
105
  }
106
 
107
  /* check rmod exec stdout buf */
108
  if (rmod[RMOD_OFFSET_STDOUTFILE + 127] != 0) {
109
    msg[22] = '6';
110
    goto FAIL;
111
  }
112
 
113
  /* else all good */
490 mateuszvis 114
  return(0);
548 mateuszvis 115
 
116
  /* error handling */
117
  FAIL:
118
  outputnl(msg);
119
  return(1);
490 mateuszvis 120
}
121
 
122
 
443 mateuszvis 123
/* parses command line the hard way (directly from PSP) */
124
static void parse_argv(struct config *cfg) {
1715 mateusz.vi 125
  unsigned char *cmdlinelen = (void *)0x80;
491 mateuszvis 126
  char *cmdline = (void *)0x81;
443 mateuszvis 127
 
1715 mateusz.vi 128
  /* The arg tail at [81h] needs some care when being processed.
129
   *
130
   * Its length should be provided in [80h], but it is not always exact:
131
   * https://github.com/SvarDOS/bugz/issues/67
132
   *
133
   * The tail string itself is usually terminated by a CR character. But
134
   * sometimes it might be terminated by a nul. Or by nothing at all.
135
   *
136
   * The cautious approach is therefore to read the tail up until the length
137
   * mentionned at [80h] or to first CR or nul, whichever comes first.
138
   */
139
 
349 mateuszvis 140
  memset(cfg, 0, sizeof(*cfg));
350 mateuszvis 141
 
1715 mateusz.vi 142
  /* Make sure that the advertised cmdline length is no more than 126 bytes
143
   * because the PSP ends at [0xff] and there ought to be at least 1 byte of
144
   * room for the CR-terminator.
145
   * According to Matthias Paul cmdlines longer than 126 (and even longer than
146
   * 127) might happen with some buggy implementations. */
147
  if (*cmdlinelen > 126) *cmdlinelen = 126;
443 mateuszvis 148
 
1715 mateusz.vi 149
  /* trim out any trailing CR garbage (see the issue 67 mentioned above) */
150
  while ((*cmdlinelen > 0) && (cmdline[*cmdlinelen - 1] == '\r')) (*cmdlinelen)--;
151
 
152
  /* normalize the cmd so it is nul-terminated - this is expected later in a
153
   * few places in the codeflow, among others in run_as_external() */
154
  cmdline[*cmdlinelen] = 0;
155
 
156
  /* process the parameters given to COMMAND.COM */
157
  while (*cmdline != 0) {
158
 
443 mateuszvis 159
    /* skip over any leading spaces */
491 mateuszvis 160
    if (*cmdline == ' ') {
161
      cmdline++;
162
      continue;
349 mateuszvis 163
    }
443 mateuszvis 164
 
491 mateuszvis 165
    if (*cmdline != '/') {
989 mateusz.vi 166
      nls_output(0,6); /* "Invalid parameter" */
167
      output(": ");
491 mateuszvis 168
      outputnl(cmdline);
169
      goto SKIP_TO_NEXT_ARG;
170
    }
443 mateuszvis 171
 
491 mateuszvis 172
    /* got a slash */
173
    cmdline++;  /* skip the slash */
174
    switch (*cmdline) {
175
      case 'c': /* /C = execute command and quit */
176
      case 'C':
177
        cfg->flags |= FLAG_EXEC_AND_QUIT;
178
        /* FALLTHRU */
179
      case 'k': /* /K = execute command and keep running */
180
      case 'K':
1001 mateusz.vi 181
        cmdline++;
182
        cfg->execcmd = cmdline;
1715 mateusz.vi 183
        return; /* further arguments are for the executed program, not for me */
443 mateuszvis 184
 
1001 mateusz.vi 185
      case 'y': /* /Y = execute batch file step-by-step (with /P, /K or /C) */
186
      case 'Y':
187
        cfg->flags |= FLAG_STEPBYSTEP;
188
        break;
189
 
494 mateuszvis 190
      case 'd': /* /D = skip autoexec.bat processing */
191
      case 'D':
192
        cfg->flags |= FLAG_SKIP_AUTOEXEC;
193
        break;
194
 
491 mateuszvis 195
      case 'e': /* preset the initial size of the environment block */
196
      case 'E':
197
        cmdline++;
198
        if (*cmdline == ':') cmdline++; /* could be /E:size */
199
        atous(&(cfg->envsiz), cmdline);
200
        if (cfg->envsiz < 64) cfg->envsiz = 0;
201
        break;
449 mateuszvis 202
 
491 mateuszvis 203
      case 'p': /* permanent shell (can't exit + run autoexec.bat) */
204
      case 'P':
205
        cfg->flags |= FLAG_PERMANENT;
206
        break;
444 mateuszvis 207
 
491 mateuszvis 208
      case '?':
989 mateusz.vi 209
        nls_outputnl(1,0); /* "Starts the SvarCOM command interpreter" */
491 mateuszvis 210
        outputnl("");
989 mateusz.vi 211
        nls_outputnl(1,1); /* "COMMAND /E:nnn [/[C|K] [/P] [/D] command]" */
491 mateuszvis 212
        outputnl("");
989 mateusz.vi 213
        nls_outputnl(1,2); /* "/D      Skip AUTOEXEC.BAT processing (makes sense only with /P)" */
214
        nls_outputnl(1,3); /* "/E:nnn  Sets the environment size to nnn bytes" */
215
        nls_outputnl(1,4); /* "/P      Makes the new command interpreter permanent and run AUTOEXEC.BAT" */
216
        nls_outputnl(1,5); /* "/C      Executes the specified command and returns" */
217
        nls_outputnl(1,6); /* "/K      Executes the specified command and continues running" */
1001 mateusz.vi 218
        nls_outputnl(1,7); /* "/Y      Executes the batch program step by step" */
491 mateuszvis 219
        exit(1);
220
        break;
221
 
222
      default:
989 mateusz.vi 223
        nls_output(0,2); /* invalid switch */
224
        output(": /");
491 mateuszvis 225
        outputnl(cmdline);
226
        break;
350 mateuszvis 227
    }
443 mateuszvis 228
 
229
    /* move to next argument or quit processing if end of cmdline */
491 mateuszvis 230
    SKIP_TO_NEXT_ARG:
1715 mateusz.vi 231
    while ((*cmdline != 0) && (*cmdline != ' ') && (*cmdline != '/')) cmdline++;
349 mateuszvis 232
  }
233
}
234
 
235
 
1798 mateusz.vi 236
/* returns current DOS drive (0 = A: ; 1 = B: etc) */
237
static unsigned char _dosgetcurdrive(void);
238
#pragma aux _dosgetcurdrive = \
239
"mov ah, 0x19"    /* DOS 1+ - GET CURRENT DRIVE */ \
240
"int 0x21" \
241
modify [ah] \
242
value [al]
243
 
244
 
245
static void _dosgetcurdir(char near *s);
246
#pragma aux _dosgetcurdir = \
247
"mov ah, 0x47"    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */ \
248
"xor dl, dl"      /* DL = drive number (00h = default, 01h = A:, etc) */ \
249
"int 0x21" \
250
parm [si] \
251
modify [ax dl]
252
 
253
 
474 mateuszvis 254
/* builds the prompt string and displays it. buff is filled with a zero-terminated copy of the prompt. */
255
static void build_and_display_prompt(char *buff, unsigned short envseg) {
256
  char *s = buff;
370 mateuszvis 257
  /* locate the prompt variable or use the default pattern */
438 mateuszvis 258
  const char far *fmt = env_lookup_val(envseg, "PROMPT");
370 mateuszvis 259
  if ((fmt == NULL) || (*fmt == 0)) fmt = "$p$g"; /* fallback to default if empty */
260
  /* build the prompt string based on pattern */
354 mateuszvis 261
  for (; *fmt != 0; fmt++) {
262
    if (*fmt != '$') {
263
      *s = *fmt;
264
      s++;
265
      continue;
266
    }
267
    /* escape code ($P, etc) */
268
    fmt++;
269
    switch (*fmt) {
270
      case 'Q':  /* $Q = = (equal sign) */
271
      case 'q':
272
        *s = '=';
273
        s++;
274
        break;
275
      case '$':  /* $$ = $ (dollar sign) */
276
        *s = '$';
277
        s++;
278
        break;
279
      case 'T':  /* $t = current time */
280
      case 't':
281
        s += sprintf(s, "00:00"); /* TODO */
282
        break;
283
      case 'D':  /* $D = current date */
284
      case 'd':
285
        s += sprintf(s, "1985-07-29"); /* TODO */
286
        break;
287
      case 'P':  /* $P = current drive and path */
288
      case 'p':
1798 mateusz.vi 289
        *s = _dosgetcurdrive() + 'A';
354 mateuszvis 290
        s++;
291
        *s = ':';
292
        s++;
293
        *s = '\\';
294
        s++;
1798 mateusz.vi 295
        _dosgetcurdir(s);
296
        /* move s ptr forward to end (0-termintor) of pathname */
297
        while (*s != 0) s++;
354 mateuszvis 298
        break;
299
      case 'V':  /* $V = DOS version number */
300
      case 'v':
301
        s += sprintf(s, "VER"); /* TODO */
302
        break;
303
      case 'N':  /* $N = current drive */
304
      case 'n':
1798 mateusz.vi 305
        *s = _dosgetcurdrive() + 'A';
354 mateuszvis 306
        s++;
307
        break;
308
      case 'G':  /* $G = > (greater-than sign) */
309
      case 'g':
310
        *s = '>';
311
        s++;
312
        break;
313
      case 'L':  /* $L = < (less-than sign) */
314
      case 'l':
315
        *s = '<';
316
        s++;
317
        break;
318
      case 'B':  /* $B = | (pipe) */
319
      case 'b':
320
        *s = '|';
321
        s++;
322
        break;
323
      case 'H':  /* $H = backspace (erases previous character) */
324
      case 'h':
325
        *s = '\b';
326
        s++;
327
        break;
328
      case 'E':  /* $E = Escape code (ASCII 27) */
329
      case 'e':
330
        *s = 27;
331
        s++;
332
        break;
333
      case '_':  /* $_ = CR+LF */
334
        *s = '\r';
335
        s++;
336
        *s = '\n';
337
        s++;
338
        break;
339
    }
340
  }
474 mateuszvis 341
  *s = 0;
342
  output(buff);
354 mateuszvis 343
}
349 mateuszvis 344
 
345
 
1797 mateusz.vi 346
static void dos_fname2fcb(char far *fcb, const char near *cmd);
347
#pragma aux dos_fname2fcb = \
348
"mov ax, 0x2900"   /* DOS 1+ - parse filename into FCB (DS:SI=fname, ES:DI=FCB) */ \
349
"int 0x21" \
350
parm [es di] [si] \
351
modify [ax si]
1156 mateusz.vi 352
 
353
 
354
/* parses cmdtail and fills fcb1 and fcb2 with first and second arguments,
355
 * respectively. an FCB is 12 bytes long:
356
 * drive (0=default, 1=A, 2=B, etc)
357
 * fname (8 chars, blank-padded)
358
 * fext (3 chars, blank-padded) */
359
static void cmdtail_to_fcb(char far *fcb1, char far *fcb2, const char *cmdtail) {
360
 
361
  /* skip any leading spaces */
362
  while (*cmdtail == ' ') cmdtail++;
363
 
364
  /* convert first arg */
365
  dos_fname2fcb(fcb1, cmdtail);
366
 
367
  /* skip to next arg */
368
  while ((*cmdtail != ' ') && (*cmdtail != 0)) cmdtail++;
369
  while (*cmdtail == ' ') cmdtail++;
370
 
371
  /* convert second arg */
372
  dos_fname2fcb(fcb2, cmdtail);
373
}
374
 
375
 
957 mateusz.vi 376
/* a few internal flags */
377
#define DELETE_STDIN_FILE 1
378
#define CALL_FLAG         2
1730 mateusz.vi 379
#define LOADHIGH_FLAG     4
957 mateusz.vi 380
 
381
static void run_as_external(char *buff, const char *cmdline, unsigned short envseg, struct rmod_props far *rmod, struct redir_data *redir, unsigned char flags) {
472 mateuszvis 382
  char *cmdfile = buff + 512;
458 mateuszvis 383
  const char far *pathptr;
384
  int lookup;
385
  unsigned short i;
386
  const char *ext;
508 mateuszvis 387
  char *cmd = buff + 1024;
479 mateuszvis 388
  const char *cmdtail;
461 mateuszvis 389
  char far *rmod_execprog = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPROG);
390
  char far *rmod_cmdtail = MK_FP(rmod->rmodseg, 0x81);
391
  _Packed struct {
392
    unsigned short envseg;
393
    unsigned long cmdtail;
394
    unsigned long fcb1;
395
    unsigned long fcb2;
396
  } far *ExecParam = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPARAM);
364 mateuszvis 397
 
472 mateuszvis 398
  /* find cmd and cmdtail */
399
  i = 0;
400
  cmdtail = cmdline;
401
  while (*cmdtail == ' ') cmdtail++; /* skip any leading spaces */
402
  while ((*cmdtail != ' ') && (*cmdtail != '/') && (*cmdtail != '+') && (*cmdtail != 0)) {
403
    cmd[i++] = *cmdtail;
404
    cmdtail++;
405
  }
406
  cmd[i] = 0;
364 mateuszvis 407
 
458 mateuszvis 408
  /* is this a command in curdir? */
472 mateuszvis 409
  lookup = lookup_cmd(cmdfile, cmd, NULL, &ext);
458 mateuszvis 410
  if (lookup == 0) {
411
    /* printf("FOUND LOCAL EXEC FILE: '%s'\r\n", cmdfile); */
412
    goto RUNCMDFILE;
413
  } else if (lookup == -2) {
414
    /* puts("NOT FOUND"); */
415
    return;
416
  }
417
 
418
  /* try matching something in PATH */
419
  pathptr = env_lookup_val(envseg, "PATH");
420
 
421
  /* try each path in %PATH% */
571 mateuszvis 422
  while (pathptr) {
458 mateuszvis 423
    for (i = 0;; i++) {
424
      buff[i] = *pathptr;
425
      if ((buff[i] == 0) || (buff[i] == ';')) break;
426
      pathptr++;
427
    }
428
    buff[i] = 0;
472 mateuszvis 429
    lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
571 mateuszvis 430
    if (lookup == 0) goto RUNCMDFILE;
458 mateuszvis 431
    if (lookup == -2) return;
432
    if (*pathptr == ';') {
433
      pathptr++;
434
    } else {
571 mateuszvis 435
      break;
458 mateuszvis 436
    }
437
  }
438
 
571 mateuszvis 439
  /* last chance: is it an executable link? (trim extension from cmd first) */
440
  for (i = 0; (cmd[i] != 0) && (cmd[i] != '.') && (i < 9); i++) buff[128 + i] = cmd[i];
441
  buff[128 + i] = 0;
442
  if ((i < 9) && (link_computefname(buff, buff + 128, envseg) == 0)) {
443
    /* try opening the link file (if it exists) and read it into buff */
444
    i = 0;
445
    _asm {
446
      push ax
447
      push bx
448
      push cx
449
      push dx
450
 
451
      mov ax, 0x3d00  /* DOS 2+ - OPEN EXISTING FILE, READ-ONLY */
452
      mov dx, buff    /* file name */
453
      int 0x21
454
      jc ERR_FOPEN
455
      /* file handle in AX, read from file now */
456
      mov bx, ax      /* file handle */
457
      mov ah, 0x3f    /* Read from file via handle bx */
458
      mov cx, 128     /* up to 128 bytes */
459
      /* mov dx, buff */ /* dest buffer (already set) */
460
      int 0x21        /* read up to 256 bytes from file and write to buff */
461
      jc ERR_READ
462
      mov i, ax
463
      ERR_READ:
464
      mov ah, 0x3e    /* close file handle in BX */
465
      int 0x21
466
      ERR_FOPEN:
467
 
468
      pop dx
469
      pop cx
470
      pop bx
471
      pop ax
472
    }
473
 
474
    /* did I read anything? */
475
    if (i != 0) {
476
      buff[i] = 0;
477
      /* trim buff at first \n or \r, just in case someone fiddled with the
478
       * link file using a text editor */
479
      for (i = 0; (buff[i] != 0) && (buff[i] != '\r') && (buff[i] != '\n'); i++);
480
      buff[i] = 0;
481
      /* lookup check */
482
      if (buff[0] != 0) {
483
        lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
484
        if (lookup == 0) goto RUNCMDFILE;
485
      }
486
    }
487
  }
488
 
489
  /* all failed (ie. executable file not found) */
490
  return;
491
 
458 mateuszvis 492
  RUNCMDFILE:
493
 
469 mateuszvis 494
  /* special handling of batch files */
495
  if ((ext != NULL) && (imatch(ext, "bat"))) {
957 mateusz.vi 496
    struct batctx far *newbat;
497
 
498
    /* remember the echo flag (in case bat file disables echo, only when starting first bat) */
499
    if (rmod->bat == NULL) {
500
      rmod->flags &= ~FLAG_ECHO_BEFORE_BAT;
501
      if (rmod->flags & FLAG_ECHOFLAG) rmod->flags |= FLAG_ECHO_BEFORE_BAT;
949 mateusz.vi 502
    }
957 mateusz.vi 503
 
504
    /* if bat is not called via a CALL, then free the bat-context linked list */
963 mateusz.vi 505
    if ((flags & CALL_FLAG) == 0) rmod_free_bat_llist(rmod);
506
 
957 mateusz.vi 507
    /* allocate a new bat context */
508
    newbat = rmod_fcalloc(sizeof(struct batctx), rmod->rmodseg, "SVBATCTX");
509
    if (newbat == NULL) {
510
      nls_outputnl_doserr(8); /* insufficient memory */
949 mateusz.vi 511
      return;
512
    }
513
 
957 mateusz.vi 514
    /* fill the newly allocated batctx structure */
515
    _fstrcpy(newbat->fname, cmdfile); /* truename of the BAT file */
1001 mateusz.vi 516
    newbat->flags = flags & FLAG_STEPBYSTEP;
508 mateuszvis 517
    /* explode args of the bat file and store them in rmod buff */
518
    cmd_explode(buff, cmdline, NULL);
957 mateusz.vi 519
    _fmemcpy(newbat->argv, buff, sizeof(newbat->argv));
508 mateuszvis 520
 
957 mateusz.vi 521
    /* push the new bat to the top of rmod's linked list */
522
    newbat->parent = rmod->bat;
523
    rmod->bat = newbat;
524
 
469 mateuszvis 525
    return;
526
  }
527
 
517 mateuszvis 528
  /* copy full filename to execute, along with redirected files (if any) */
548 mateuszvis 529
  _fstrcpy(rmod_execprog, cmdfile);
530
 
531
  /* copy stdin file if a redirection is needed */
517 mateuszvis 532
  if (redir->stdinfile) {
548 mateuszvis 533
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDINFILE);
576 mateuszvis 534
    char far *delstdin = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDIN_DEL);
548 mateuszvis 535
    _fstrcpy(farptr, redir->stdinfile);
957 mateusz.vi 536
    if (flags & DELETE_STDIN_FILE) {
576 mateuszvis 537
      *delstdin = redir->stdinfile[0];
538
    } else {
539
      *delstdin = 0;
540
    }
517 mateuszvis 541
  }
548 mateuszvis 542
 
543
  /* same for stdout file */
517 mateuszvis 544
  if (redir->stdoutfile) {
548 mateuszvis 545
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTFILE);
546
    unsigned short far *farptr16 = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTAPP);
547
    _fstrcpy(farptr, redir->stdoutfile);
517 mateuszvis 548
    /* openflag */
548 mateuszvis 549
    *farptr16 = redir->stdout_openflag;
517 mateuszvis 550
  }
461 mateuszvis 551
 
552
  /* copy cmdtail to rmod's PSP and compute its len */
553
  for (i = 0; cmdtail[i] != 0; i++) rmod_cmdtail[i] = cmdtail[i];
554
  rmod_cmdtail[i] = '\r';
555
  rmod_cmdtail[-1] = i;
556
 
557
  /* set up rmod to execute the command */
558
 
1730 mateusz.vi 559
  /* loadhigh? */
560
  if (flags & LOADHIGH_FLAG) {
561
    unsigned char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXEC_LH);
562
    *farptr = 1;
563
  }
564
 
464 mateuszvis 565
  ExecParam->envseg = envseg;
461 mateuszvis 566
  ExecParam->cmdtail = (unsigned long)MK_FP(rmod->rmodseg, 0x80); /* farptr, must be in PSP format (lenbyte args \r) */
1156 mateusz.vi 567
  /* far pointers to unopened FCB entries (stored in RMOD's own PSP) */
568
  {
569
    char far *farptr;
570
    /* prep the unopened FCBs */
571
    farptr = MK_FP(rmod->rmodseg, 0x5C);
572
    _fmemset(farptr, 0, 36); /* first FCB is 16 bytes long, second is 20 bytes long */
573
    cmdtail_to_fcb(farptr, farptr + 16, cmdtail);
574
    /* set (far) pointers in the ExecParam block */
575
    ExecParam->fcb1 = (unsigned long)MK_FP(rmod->rmodseg, 0x5C);
576
    ExecParam->fcb2 = (unsigned long)MK_FP(rmod->rmodseg, 0x6C);
577
  }
461 mateuszvis 578
  exit(0); /* let rmod do the job now */
364 mateuszvis 579
}
580
 
581
 
367 mateuszvis 582
static void set_comspec_to_self(unsigned short envseg) {
583
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
584
  char far *myenv = MK_FP(*psp_envseg, 0);
585
  unsigned short varcount;
586
  char buff[256] = "COMSPEC=";
587
  char *buffptr = buff + 8;
588
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
589
  while (*myenv != 0) {
590
    /* consume a NULL-terminated string */
591
    while (*myenv != 0) myenv++;
592
    /* move to next string */
593
    myenv++;
594
  }
595
  /* get next word, if 1 then EXEPATH follows */
596
  myenv++;
597
  varcount = *myenv;
598
  myenv++;
599
  varcount |= (*myenv << 8);
600
  myenv++;
601
  if (varcount != 1) return; /* NO EXEPATH FOUND */
602
  while (*myenv != 0) {
603
    *buffptr = *myenv;
604
    buffptr++;
605
    myenv++;
606
  }
607
  *buffptr = 0;
608
  /* printf("EXEPATH: '%s'\r\n", buff); */
609
  env_setvar(envseg, buff);
610
}
611
 
612
 
450 mateuszvis 613
/* wait for user input */
1797 mateusz.vi 614
static void cmdline_getinput(unsigned short inpseg, unsigned short inpoff);
615
#pragma aux cmdline_getinput = \
616
"push ds" \
617
/* set up buffered input to inpseg:inpoff */ \
618
"push ax" \
619
"pop ds" \
620
\
621
/* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */ \
622
"mov ax, 0x4800" \
623
"int 0x2f" \
624
\
625
/* execute either DOS input or DOSKEY */ \
626
"test al, al" /* al=0 if no DOSKEY present */ \
627
"jnz DOSKEY" \
628
\
629
/* buffered string input */ \
630
"mov ah, 0x0a" \
631
"int 0x21" \
632
"jmp short DONE" \
633
\
634
"DOSKEY:" \
635
"mov ax, 0x4810" \
636
"int 0x2f" \
637
\
638
"DONE:" \
639
/* terminate command with a CR/LF */ \
640
"mov ah, 0x02" /* display character in dl */ \
641
"mov dl, 0x0d" \
642
"int 0x21" \
643
"mov dl, 0x0a" \
644
"int 0x21" \
645
"pop ds" \
646
parm [ax] [dx] \
647
modify [ax dl]
450 mateuszvis 648
 
649
 
479 mateuszvis 650
/* fetches a line from batch file and write it to buff (NULL-terminated),
651
 * increments rmod counter and returns 0 on success. */
484 mateuszvis 652
static int getbatcmd(char *buff, unsigned char buffmaxlen, struct rmod_props far *rmod) {
469 mateuszvis 653
  unsigned short i;
949 mateusz.vi 654
  unsigned short batname_seg = FP_SEG(rmod->bat->fname);
655
  unsigned short batname_off = FP_OFF(rmod->bat->fname);
656
  unsigned short filepos_cx = rmod->bat->nextline >> 16;
657
  unsigned short filepos_dx = rmod->bat->nextline & 0xffff;
474 mateuszvis 658
  unsigned char blen = 0;
505 mateuszvis 659
  unsigned short errv = 0;
474 mateuszvis 660
 
661
  /* open file, jump to offset filpos, and read data into buff.
662
   * result in blen (unchanged if EOF or failure). */
663
  _asm {
664
    push ax
665
    push bx
666
    push cx
667
    push dx
668
 
669
    /* open file (read-only) */
505 mateuszvis 670
    mov bx, 0xffff        /* preset BX to 0xffff to detect error conditions */
474 mateuszvis 671
    mov dx, batname_off
672
    mov ax, batname_seg
673
    push ds     /* save DS */
674
    mov ds, ax
675
    mov ax, 0x3d00
676
    int 0x21    /* handle in ax on success */
677
    pop ds      /* restore DS */
505 mateuszvis 678
    jc ERR
474 mateuszvis 679
    mov bx, ax  /* save handle to bx */
680
 
681
    /* jump to file offset CX:DX */
682
    mov ax, 0x4200
683
    mov cx, filepos_cx
684
    mov dx, filepos_dx
685
    int 0x21  /* CF clear on success, DX:AX set to cur pos */
505 mateuszvis 686
    jc ERR
474 mateuszvis 687
 
688
    /* read the line into buff */
689
    mov ah, 0x3f
484 mateuszvis 690
    xor ch, ch
691
    mov cl, buffmaxlen
474 mateuszvis 692
    mov dx, buff
693
    int 0x21 /* CF clear on success, AX=number of bytes read */
505 mateuszvis 694
    jc ERR
474 mateuszvis 695
    mov blen, al
505 mateuszvis 696
    jmp CLOSEANDQUIT
474 mateuszvis 697
 
505 mateuszvis 698
    ERR:
699
    mov errv, ax
700
 
474 mateuszvis 701
    CLOSEANDQUIT:
505 mateuszvis 702
    /* close file (if bx contains a handle) */
703
    cmp bx, 0xffff
704
    je DONE
474 mateuszvis 705
    mov ah, 0x3e
706
    int 0x21
707
 
708
    DONE:
709
    pop dx
710
    pop cx
711
    pop bx
712
    pop ax
469 mateuszvis 713
  }
470 mateuszvis 714
 
474 mateuszvis 715
  /* printf("blen=%u filepos_cx=%u filepos_dx=%u\r\n", blen, filepos_cx, filepos_dx); */
470 mateuszvis 716
 
538 mateuszvis 717
  if (errv != 0) nls_outputnl_doserr(errv);
505 mateuszvis 718
 
474 mateuszvis 719
  /* on EOF - abort processing the bat file */
720
  if (blen == 0) goto OOPS;
721
 
722
  /* find nearest \n to inc batch offset and replace \r by NULL terminator
723
   * I support all CR/LF, CR- and LF-terminated batch files */
724
  for (i = 0; i < blen; i++) {
725
    if ((buff[i] == '\r') || (buff[i] == '\n')) {
949 mateusz.vi 726
      if ((buff[i] == '\r') && ((i+1) < blen) && (buff[i+1] == '\n')) rmod->bat->nextline += 1;
474 mateuszvis 727
      break;
728
    }
729
  }
730
  buff[i] = 0;
949 mateusz.vi 731
  rmod->bat->nextline += i + 1;
474 mateuszvis 732
 
733
  return(0);
734
 
735
  OOPS:
949 mateusz.vi 736
  rmod->bat->fname[0] = 0;
737
  rmod->bat->nextline = 0;
474 mateuszvis 738
  return(-1);
469 mateuszvis 739
}
740
 
741
 
507 mateuszvis 742
/* replaces %-variables in a BAT line with resolved values:
743
 * %PATH%       -> replaced by the contend of the PATH env variable
744
 * %UNDEFINED%  -> undefined variables are replaced by nothing ("")
745
 * %NOTCLOSED   -> NOTCLOSED
746
 * %1           -> first argument of the batch file (or nothing if no arg) */
747
static void batpercrepl(char *res, unsigned short ressz, const char *line, const struct rmod_props far *rmod, unsigned short envseg) {
748
  unsigned short lastperc = 0xffff;
749
  unsigned short reslen = 0;
750
 
751
  if (ressz == 0) return;
752
  ressz--; /* reserve one byte for the NULL terminator */
753
 
754
  for (; (reslen < ressz) && (*line != 0); line++) {
755
    /* if not a percent, I don't care */
756
    if (*line != '%') {
757
      res[reslen++] = *line;
758
      continue;
759
    }
760
 
761
    /* *** perc char handling *** */
762
 
763
    /* closing perc? */
764
    if (lastperc != 0xffff) {
765
      /* %% is '%' */
766
      if (lastperc == reslen) {
767
        res[reslen++] = '%';
768
      } else {   /* otherwise variable name */
769
        const char far *ptr;
770
        res[reslen] = 0;
771
        reslen = lastperc;
1139 mateusz.vi 772
        nls_strtoup(res + reslen); /* turn varname uppercase before lookup */
507 mateuszvis 773
        ptr = env_lookup_val(envseg, res + reslen);
774
        if (ptr != NULL) {
775
          while ((*ptr != 0) && (reslen < ressz)) {
776
            res[reslen++] = *ptr;
777
            ptr++;
778
          }
779
        }
780
      }
781
      lastperc = 0xffff;
782
      continue;
783
    }
784
 
785
    /* digit? (bat arg) */
786
    if ((line[1] >= '0') && (line[1] <= '9')) {
508 mateuszvis 787
      unsigned short argid = line[1] - '0';
788
      unsigned short i;
949 mateusz.vi 789
      const char far *argv = "";
790
      if ((rmod != NULL) && (rmod->bat != NULL)) argv = rmod->bat->argv;
508 mateuszvis 791
 
792
      /* locate the proper arg */
793
      for (i = 0; i != argid; i++) {
794
        /* if string is 0, then end of list reached */
795
        if (*argv == 0) break;
796
        /* jump to next arg */
797
        while (*argv != 0) argv++;
798
        argv++;
799
      }
800
 
801
      /* copy the arg to result */
802
      for (i = 0; (argv[i] != 0) && (reslen < ressz); i++) {
803
        res[reslen++] = argv[i];
804
      }
507 mateuszvis 805
      line++;  /* skip the digit */
806
      continue;
807
    }
808
 
809
    /* opening perc */
810
    lastperc = reslen;
811
 
812
  }
813
 
814
  res[reslen] = 0;
815
}
816
 
817
 
1024 mateusz.vi 818
/* process the ongoing forloop, returns 0 on success, non-zero otherwise (no
819
   more things to process) */
820
static int forloop_process(char *res, struct forctx far *forloop) {
821
  unsigned short i, t;
822
  struct DTA *dta = (void *)0x80; /* default DTA at 80h in PSP */
1055 mateusz.vi 823
  char *fnameptr = dta->fname;
1070 mateusz.vi 824
  char *pathprefix = BUFFER + 256;
957 mateusz.vi 825
 
1070 mateusz.vi 826
  *pathprefix = 0;
827
 
1024 mateusz.vi 828
  TRYAGAIN:
829
 
830
  /* dta_inited: FindFirst() or FindNext()? */
831
  if (forloop->dta_inited == 0) {
832
 
1065 bttr 833
    /* copy next awaiting pattern to BUFFER (and skip all delimiters until
1054 mateusz.vi 834
     * next pattern or end of list) */
835
    t = 0;
1024 mateusz.vi 836
    for (i = 0;; i++) {
837
      BUFFER[i] = forloop->cmd[forloop->nextpat + i];
1070 mateusz.vi 838
      /* is this a delimiter? (all delimiters are already normalized to a space here) */
839
      if (BUFFER[i] == ' ') {
840
        BUFFER[i] = 0;
841
        t = 1;
842
      } else if (BUFFER[i] == 0) {
843
        /* end of patterns list */
844
        break;
845
      } else {
846
        /* quit if I got a pattern already */
847
        if (t == 1) break;
1024 mateusz.vi 848
      }
849
    }
850
 
851
    if (i == 0) return(-1);
852
 
853
    /* remember position of current pattern */
854
    forloop->curpat = forloop->nextpat;
855
 
856
    /* move nextpat forward to next pattern */
857
    i += forloop->nextpat;
858
    forloop->nextpat = i;
859
 
1055 mateusz.vi 860
    /* if this is a string and not a pattern, skip all the FindFirst business
861
     * a file pattern has a wildcard (* or ?), a message doesn't */
862
    for (i = 0; (BUFFER[i] != 0) && (BUFFER[i] != '?') && (BUFFER[i] != '*'); i++);
863
    if (BUFFER[i] == 0) {
864
      fnameptr = BUFFER;
865
      goto SKIP_DTA;
866
    }
867
 
1024 mateusz.vi 868
    /* FOR in MSDOS 6 includes hidden and system files, but not directories nor volumes */
869
    if (findfirst(dta, BUFFER, DOS_ATTR_RO | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_ARC) != 0) {
870
      goto TRYAGAIN;
871
    }
872
    forloop->dta_inited = 1;
873
  } else { /* dta in progress */
874
 
875
    /* copy forloop DTA to my local copy */
876
    _fmemcpy(dta, &(forloop->dta), sizeof(*dta));
877
 
878
    /* findnext() call */
879
    if (findnext(dta) != 0) {
880
      forloop->dta_inited = 0;
881
      goto TRYAGAIN;
882
    }
883
  }
884
 
885
  /* copy updated DTA to rmod */
886
  _fmemcpy(&(forloop->dta), dta, sizeof(*dta));
887
 
1070 mateusz.vi 888
  /* prefill pathprefix with the prefix (path) of the files */
889
  {
890
    short lastbk = -1;
891
    char far *c = forloop->cmd + forloop->curpat;
892
    for (i = 0;; i++) {
893
      pathprefix[i] = c[i];
894
      if (pathprefix[i] == '\\') lastbk = i;
895
      if ((pathprefix[i] == ' ') || (pathprefix[i] == 0)) break;
896
    }
897
    pathprefix[lastbk+1] = 0;
898
  }
899
 
1055 mateusz.vi 900
  SKIP_DTA:
901
 
1024 mateusz.vi 902
  /* fill res with command, replacing varname by actual filename */
903
  /* full filename is to be built with path of curpat and fname from dta */
904
  t = 0;
905
  i = 0;
906
  for (;;) {
907
    if ((forloop->cmd[forloop->exec + t] == '%') && (forloop->cmd[forloop->exec + t + 1] == forloop->varname)) {
1070 mateusz.vi 908
      strcpy(res + i, pathprefix);
909
      strcat(res + i, fnameptr);
910
      for (; res[i] != 0; i++);
1024 mateusz.vi 911
      t += 2;
912
    } else {
913
      res[i] = forloop->cmd[forloop->exec + t];
914
      t++;
915
      if (res[i++] == 0) break;
916
    }
917
  }
918
 
919
  return(0);
920
}
921
 
922
 
443 mateuszvis 923
int main(void) {
372 mateuszvis 924
  static struct config cfg;
925
  static unsigned short far *rmod_envseg;
926
  static unsigned short far *lastexitcode;
449 mateuszvis 927
  static struct rmod_props far *rmod;
500 mateuszvis 928
  static char cmdlinebuf[CMDLINE_MAXLEN + 2]; /* 1 extra byte for 0-terminator and another for memguard */
479 mateuszvis 929
  static char *cmdline;
517 mateuszvis 930
  static struct redir_data redirprops;
533 mateuszvis 931
  static enum cmd_result cmdres;
543 mateuszvis 932
  static unsigned short i; /* general-purpose variable for short-lived things */
957 mateusz.vi 933
  static unsigned char flags;
349 mateuszvis 934
 
479 mateuszvis 935
  rmod = rmod_find(BUFFER_len);
449 mateuszvis 936
  if (rmod == NULL) {
485 mateuszvis 937
    /* look at command line parameters (in case env size if set there) */
938
    parse_argv(&cfg);
479 mateuszvis 939
    rmod = rmod_install(cfg.envsiz, BUFFER, BUFFER_len);
449 mateuszvis 940
    if (rmod == NULL) {
989 mateusz.vi 941
      nls_outputnl_err(2,1); /* "FATAL ERROR: rmod_install() failed" */
349 mateuszvis 942
      return(1);
943
    }
475 mateuszvis 944
    /* copy flags to rmod's storage (and enable ECHO) */
945
    rmod->flags = cfg.flags | FLAG_ECHOFLAG;
465 mateuszvis 946
    /* printf("rmod installed at %Fp\r\n", rmod); */
572 mateuszvis 947
    rmod->version = BYTE_VERSION;
349 mateuszvis 948
  } else {
465 mateuszvis 949
    /* printf("rmod found at %Fp\r\n", rmod); */
950
    /* if I was spawned by rmod and FLAG_EXEC_AND_QUIT is set, then I should
951
     * die asap, because the command has been executed already, so I no longer
952
     * have a purpose in life */
469 mateuszvis 953
    if (rmod->flags & FLAG_EXEC_AND_QUIT) sayonara(rmod);
572 mateuszvis 954
    /* */
955
    if (rmod->version != BYTE_VERSION) {
989 mateusz.vi 956
      nls_outputnl_err(2,0);
572 mateuszvis 957
      _asm {
958
        HALT:
959
        hlt
960
        jmp HALT
961
      }
962
    }
349 mateuszvis 963
  }
964
 
490 mateuszvis 965
  /* install a few guardvals in memory to detect some cases of overflows */
500 mateuszvis 966
  memguard_set(cmdlinebuf);
490 mateuszvis 967
 
465 mateuszvis 968
  rmod_envseg = MK_FP(rmod->rmodseg, RMOD_OFFSET_ENVSEG);
969
  lastexitcode = MK_FP(rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
350 mateuszvis 970
 
1713 bttr 971
  /* make COMSPEC point to myself */
367 mateuszvis 972
  set_comspec_to_self(*rmod_envseg);
973
 
494 mateuszvis 974
  /* on /P check for the presence of AUTOEXEC.BAT and execute it if found,
975
   * but skip this check if /D was also passed */
976
  if ((cfg.flags & (FLAG_PERMANENT | FLAG_SKIP_AUTOEXEC)) == FLAG_PERMANENT) {
483 mateuszvis 977
    if (file_getattr("AUTOEXEC.BAT") >= 0) cfg.execcmd = "AUTOEXEC.BAT";
978
  }
979
 
443 mateuszvis 980
  do {
1023 mateusz.vi 981
 
480 mateuszvis 982
    /* terminate previous command with a CR/LF if ECHO ON (but not during BAT processing) */
949 mateusz.vi 983
    if ((rmod->flags & FLAG_ECHOFLAG) && (rmod->bat == NULL)) outputnl("");
474 mateuszvis 984
 
985
    SKIP_NEWLINE:
986
 
490 mateuszvis 987
    /* memory check */
500 mateuszvis 988
    memguard_check(rmod->rmodseg, cmdlinebuf);
474 mateuszvis 989
 
500 mateuszvis 990
    /* preset cmdline to point at the dedicated buffer */
991
    cmdline = cmdlinebuf;
490 mateuszvis 992
 
437 mateuszvis 993
    /* (re)load translation strings if needed */
994
    nls_langreload(BUFFER, *rmod_envseg);
995
 
1024 mateusz.vi 996
    /* am I inside a FOR loop? */
997
    if (rmod->forloop) {
998
      if (forloop_process(cmdlinebuf, rmod->forloop) != 0) {
999
        rmod_ffree(rmod->forloop);
1000
        rmod->forloop = NULL;
1001
      } else {
1002
        /* output prompt and command on screen if echo on and command is not
1003
         * inhibiting it with the @ prefix */
1004
        if (rmod->flags & FLAG_ECHOFLAG) {
1005
          build_and_display_prompt(BUFFER, *rmod_envseg);
1006
          outputnl(cmdline);
1007
        }
1008
        /* jump to command processing */
1009
        goto EXEC_CMDLINE;
1010
      }
1011
    }
1012
 
543 mateuszvis 1013
    /* load awaiting command, if any (used to run piped commands) */
1014
    if (rmod->awaitingcmd[0] != 0) {
1015
      _fstrcpy(cmdline, rmod->awaitingcmd);
1016
      rmod->awaitingcmd[0] = 0;
957 mateusz.vi 1017
      flags |= DELETE_STDIN_FILE;
543 mateuszvis 1018
      goto EXEC_CMDLINE;
576 mateuszvis 1019
    } else {
957 mateusz.vi 1020
      flags &= ~DELETE_STDIN_FILE;
543 mateuszvis 1021
    }
1022
 
1001 mateusz.vi 1023
    /* skip user input if I have a command to exec (/C or /K or /P) */
443 mateuszvis 1024
    if (cfg.execcmd != NULL) {
1025
      cmdline = cfg.execcmd;
1026
      cfg.execcmd = NULL;
1001 mateusz.vi 1027
      /* */
1028
      if (cfg.flags & FLAG_STEPBYSTEP) flags |= FLAG_STEPBYSTEP;
443 mateuszvis 1029
      goto EXEC_CMDLINE;
1030
    }
1031
 
469 mateuszvis 1032
    /* if batch file is being executed -> fetch next line */
949 mateusz.vi 1033
    if (rmod->bat != NULL) {
507 mateuszvis 1034
      if (getbatcmd(BUFFER, CMDLINE_MAXLEN, rmod) != 0) { /* end of batch */
949 mateusz.vi 1035
        struct batctx far *victim = rmod->bat;
1036
        rmod->bat = rmod->bat->parent;
1037
        rmod_ffree(victim);
957 mateusz.vi 1038
        /* end of batch? then restore echo flag as it was before running the (first) bat file */
949 mateusz.vi 1039
        if (rmod->bat == NULL) {
1040
          rmod->flags &= ~FLAG_ECHOFLAG;
1041
          if (rmod->flags & FLAG_ECHO_BEFORE_BAT) rmod->flags |= FLAG_ECHOFLAG;
1042
        }
474 mateuszvis 1043
        continue;
1044
      }
507 mateuszvis 1045
      /* %-decoding of variables (%PATH%, %1, %%...), result in cmdline */
1046
      batpercrepl(cmdline, CMDLINE_MAXLEN, BUFFER, rmod, *rmod_envseg);
480 mateuszvis 1047
      /* skip any leading spaces */
1048
      while (*cmdline == ' ') cmdline++;
960 mateusz.vi 1049
      /* skip batch labels */
1050
      if (*cmdline == ':') continue;
1001 mateusz.vi 1051
      /* step-by-step execution? */
1052
      if (rmod->bat->flags & FLAG_STEPBYSTEP) {
1053
        if (*cmdline == 0) continue; /* skip empty lines */
1054
        if (askchoice(cmdline, svarlang_str(0,10)) != 0) continue;
1055
      }
474 mateuszvis 1056
      /* output prompt and command on screen if echo on and command is not
1057
       * inhibiting it with the @ prefix */
479 mateuszvis 1058
      if ((rmod->flags & FLAG_ECHOFLAG) && (cmdline[0] != '@')) {
474 mateuszvis 1059
        build_and_display_prompt(BUFFER, *rmod_envseg);
479 mateuszvis 1060
        outputnl(cmdline);
474 mateuszvis 1061
      }
479 mateuszvis 1062
      /* skip the @ prefix if present, it is no longer useful */
1063
      if (cmdline[0] == '@') cmdline++;
469 mateuszvis 1064
    } else {
983 mateusz.vi 1065
      unsigned char far *rmod_inputbuf = MK_FP(rmod->rmodseg, RMOD_OFFSET_INPUTBUF);
1066
      /* invalidate input history if it appears to be damaged (could occur
1067
       * because of a stack overflow, for example if some stack-hungry TSR is
1068
       * being used) */
987 mateusz.vi 1069
      if ((rmod_inputbuf[0] != 128) || (rmod_inputbuf[rmod_inputbuf[1] + 2] != '\r') || (rmod_inputbuf[rmod_inputbuf[1] + 3] != 0xCA) || (rmod_inputbuf[rmod_inputbuf[1] + 4] != 0xFE)) {
1070
        rmod_inputbuf[0] = 128;  /* max allowed input length */
1071
        rmod_inputbuf[1] = 0;    /* string len stored in buffer */
1072
        rmod_inputbuf[2] = '\r'; /* string terminator */
1073
        rmod_inputbuf[3] = 0xCA; /* trailing signature */
1074
        rmod_inputbuf[4] = 0xFE; /* trailing signature */
989 mateusz.vi 1075
        nls_outputnl_err(2,2); /* "stack overflow detected, command history flushed" */
983 mateusz.vi 1076
      }
474 mateuszvis 1077
      /* interactive mode: display prompt (if echo enabled) and wait for user
1078
       * command line */
475 mateuszvis 1079
      if (rmod->flags & FLAG_ECHOFLAG) build_and_display_prompt(BUFFER, *rmod_envseg);
474 mateuszvis 1080
      /* collect user input */
983 mateusz.vi 1081
      cmdline_getinput(rmod->rmodseg, RMOD_OFFSET_INPUTBUF);
987 mateusz.vi 1082
      /* append stack-overflow detection signature to the end of the input buffer */
1083
      rmod_inputbuf[rmod_inputbuf[1] + 3] = 0xCA; /* trailing signature */
1084
      rmod_inputbuf[rmod_inputbuf[1] + 4] = 0xFE; /* trailing signature */
479 mateuszvis 1085
      /* copy it to local cmdline */
983 mateusz.vi 1086
      if (rmod_inputbuf[1] != 0) _fmemcpy(cmdline, rmod_inputbuf + 2, rmod_inputbuf[1]);
1087
      cmdline[rmod_inputbuf[1]] = 0; /* zero-terminate local buff (original is '\r'-terminated) */
469 mateuszvis 1088
    }
349 mateuszvis 1089
 
405 mateuszvis 1090
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
479 mateuszvis 1091
    if (cmdline[0] == 0) goto SKIP_NEWLINE;
349 mateuszvis 1092
 
443 mateuszvis 1093
    /* I jump here when I need to exec an initial command (/C or /K) */
1094
    EXEC_CMDLINE:
1095
 
364 mateuszvis 1096
    /* move pointer forward to skip over any leading spaces */
1097
    while (*cmdline == ' ') cmdline++;
349 mateuszvis 1098
 
1138 mateusz.vi 1099
    /* sanitize separators into spaces */
1100
    for (i = 0; cmdline[i] != 0; i++) {
1101
      switch (cmdline[i]) {
1102
        case '\t':
1103
          cmdline[i] = ' ';
1104
      }
1105
    }
1106
 
1713 bttr 1107
    /* update rmod's ptr to COMSPEC so it is always up to date */
465 mateuszvis 1108
    rmod_updatecomspecptr(rmod->rmodseg, *rmod_envseg);
367 mateuszvis 1109
 
402 mateuszvis 1110
    /* handle redirections (if any) */
577 mateuszvis 1111
    i = redir_parsecmd(&redirprops, cmdline, rmod->awaitingcmd, *rmod_envseg);
543 mateuszvis 1112
    if (i != 0) {
1113
      nls_outputnl_doserr(i);
1114
      rmod->awaitingcmd[0] = 0;
1115
      continue;
1116
    }
402 mateuszvis 1117
 
364 mateuszvis 1118
    /* try matching (and executing) an internal command */
957 mateusz.vi 1119
    cmdres = cmd_process(rmod, *rmod_envseg, cmdline, BUFFER, sizeof(BUFFER), &redirprops, flags & DELETE_STDIN_FILE);
533 mateuszvis 1120
    if ((cmdres == CMD_OK) || (cmdres == CMD_FAIL)) {
443 mateuszvis 1121
      /* internal command executed */
533 mateuszvis 1122
    } else if (cmdres == CMD_CHANGED) { /* cmdline changed, needs to be reprocessed */
1123
      goto EXEC_CMDLINE;
957 mateusz.vi 1124
    } else if (cmdres == CMD_CHANGED_BY_CALL) { /* cmdline changed *specifically* by CALL */
1125
      /* the distinction is important since it changes the way batch files are processed */
1126
      flags |= CALL_FLAG;
1127
      goto EXEC_CMDLINE;
1730 mateusz.vi 1128
    } else if (cmdres == CMD_CHANGED_BY_LH) { /* cmdline changed *specifically* by LH */
1129
      flags |= LOADHIGH_FLAG;
1130
      goto EXEC_CMDLINE;
533 mateuszvis 1131
    } else if (cmdres == CMD_NOTFOUND) {
1132
      /* this was not an internal command, try matching an external command */
957 mateusz.vi 1133
      run_as_external(BUFFER, cmdline, *rmod_envseg, rmod, &redirprops, flags);
1134
 
1135
      /* is it a newly launched BAT file? */
949 mateusz.vi 1136
      if ((rmod->bat != NULL) && (rmod->bat->nextline == 0)) goto SKIP_NEWLINE;
533 mateuszvis 1137
      /* run_as_external() does not return on success, if I am still alive then
1138
       * external command failed to execute */
989 mateusz.vi 1139
      nls_outputnl(0,5); /* "Bad command or file name" */
1001 mateusz.vi 1140
    } else {
1141
      /* I should never ever land here */
1142
      outputnl("INTERNAL ERR: INVALID CMDRES");
353 mateuszvis 1143
    }
352 mateuszvis 1144
 
1001 mateusz.vi 1145
    /* reset one-time only flags */
1146
    flags &= ~CALL_FLAG;
1147
    flags &= ~FLAG_STEPBYSTEP;
1730 mateusz.vi 1148
    flags &= ~LOADHIGH_FLAG;
349 mateuszvis 1149
 
958 mateusz.vi 1150
    /* repeat unless /C was asked - but always finish running an ongoing batch
1151
     * file (otherwise only first BAT command would be executed with /C) */
1024 mateusz.vi 1152
  } while (((rmod->flags & FLAG_EXEC_AND_QUIT) == 0) || (rmod->bat != NULL) || (rmod->forloop != NULL));
349 mateuszvis 1153
 
449 mateuszvis 1154
  sayonara(rmod);
349 mateuszvis 1155
  return(0);
1156
}