Subversion Repositories SvarDOS

Rev

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