Subversion Repositories SvarDOS

Rev

Rev 491 | Rev 494 | 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
 *
4
 * Copyright (C) 2021 Mateusz Viste
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
 
31
#include <process.h>
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
 
349 mateuszvis 42
struct config {
449 mateuszvis 43
  unsigned char flags; /* command.com flags, as defined in rmodinit.h */
443 mateuszvis 44
  char *execcmd;
410 mateuszvis 45
  unsigned short envsiz;
443 mateuszvis 46
};
349 mateuszvis 47
 
490 mateuszvis 48
/* max length of the cmdline storage (bytes) - includes also max length of
49
 * line loaded from a BAT file (no more than 255 bytes!) */
50
#define CMDLINE_MAXLEN 255
349 mateuszvis 51
 
490 mateuszvis 52
 
53
/* sets guard values at a few places in memory for later detection of
54
 * overflows via memguard_check() */
55
static void memguard_set(void) {
56
  BUFFER[sizeof(BUFFER) - 1] = 0xC7;
57
  BUFFER[sizeof(BUFFER) - (CMDLINE_MAXLEN + 3)] = 0xC7;
58
}
59
 
60
 
61
/* checks for valguards at specific memory locations, returns 0 on success */
62
static int memguard_check(unsigned short rmodseg) {
63
  /* check RMOD signature (would be overwritten in case of stack overflow */
64
  static char msg[] = "!! MEMORY CORRUPTION ## DETECTED !!";
65
  unsigned short far *rmodsig = MK_FP(rmodseg, 0x100 + 6);
66
  if (*rmodsig != 0x2019) {
67
    msg[22] = '1';
68
    outputnl(msg);
69
    printf("rmodseg = %04X ; *rmodsig = %04X\r\n", rmodseg, *rmodsig);
70
    return(1);
71
  }
72
  /* check last BUFFER byte (could be overwritten by cmdline) */
73
  if (BUFFER[sizeof(BUFFER) - 1] != 0xC7) {
74
    msg[22] = '2';
75
    outputnl(msg);
76
    return(2);
77
  }
78
  /* check that cmdline BUFFER's end hasn't been touched by something else */
79
  if (BUFFER[sizeof(BUFFER) - (CMDLINE_MAXLEN + 3)] != 0xC7) {
80
    msg[22] = '3';
81
    outputnl(msg);
82
    return(3);
83
  }
84
  /* all good */
85
  return(0);
86
}
87
 
88
 
443 mateuszvis 89
/* parses command line the hard way (directly from PSP) */
90
static void parse_argv(struct config *cfg) {
491 mateuszvis 91
  const unsigned char *cmdlinelen = (void *)0x80;
92
  char *cmdline = (void *)0x81;
443 mateuszvis 93
 
349 mateuszvis 94
  memset(cfg, 0, sizeof(*cfg));
350 mateuszvis 95
 
443 mateuszvis 96
  /* set a NULL terminator on cmdline */
97
  cmdline[*cmdlinelen] = 0;
98
 
491 mateuszvis 99
  while (*cmdline != 0) {
443 mateuszvis 100
 
101
    /* skip over any leading spaces */
491 mateuszvis 102
    if (*cmdline == ' ') {
103
      cmdline++;
104
      continue;
349 mateuszvis 105
    }
443 mateuszvis 106
 
491 mateuszvis 107
    if (*cmdline != '/') {
443 mateuszvis 108
      output("Invalid parameter: ");
491 mateuszvis 109
      outputnl(cmdline);
110
      goto SKIP_TO_NEXT_ARG;
111
    }
443 mateuszvis 112
 
491 mateuszvis 113
    /* got a slash */
114
    cmdline++;  /* skip the slash */
115
    switch (*cmdline) {
116
      case 'c': /* /C = execute command and quit */
117
      case 'C':
118
        cfg->flags |= FLAG_EXEC_AND_QUIT;
119
        /* FALLTHRU */
120
      case 'k': /* /K = execute command and keep running */
121
      case 'K':
122
        cfg->execcmd = cmdline + 1;
123
        return;
443 mateuszvis 124
 
491 mateuszvis 125
      case 'e': /* preset the initial size of the environment block */
126
      case 'E':
127
        cmdline++;
128
        if (*cmdline == ':') cmdline++; /* could be /E:size */
129
        atous(&(cfg->envsiz), cmdline);
130
        if (cfg->envsiz < 64) cfg->envsiz = 0;
131
        break;
449 mateuszvis 132
 
491 mateuszvis 133
      case 'p': /* permanent shell (can't exit + run autoexec.bat) */
134
      case 'P':
135
        cfg->flags |= FLAG_PERMANENT;
136
        break;
444 mateuszvis 137
 
491 mateuszvis 138
      case '?':
139
        outputnl("Starts the SvarCOM command interpreter");
140
        outputnl("");
141
        outputnl("COMMAND /E:nnn [/[C|K] command]");
142
        outputnl("");
143
        outputnl("/E:nnn     Sets the environment size to nnn bytes");
144
        outputnl("/P         Makes the new command interpreter permanent (can't exit)");
145
        outputnl("/C         Executes the specified command and returns");
146
        outputnl("/K         Executes the specified command and continues running");
147
        exit(1);
148
        break;
149
 
150
      default:
151
        output("Invalid switch:");
152
        output(" ");
153
        outputnl(cmdline);
154
        break;
350 mateuszvis 155
    }
443 mateuszvis 156
 
157
    /* move to next argument or quit processing if end of cmdline */
491 mateuszvis 158
    SKIP_TO_NEXT_ARG:
159
    while ((*cmdline != 0) && (*cmdline != ' ') && (*cmdline != '/')) cmdline++;
349 mateuszvis 160
  }
161
}
162
 
163
 
474 mateuszvis 164
/* builds the prompt string and displays it. buff is filled with a zero-terminated copy of the prompt. */
165
static void build_and_display_prompt(char *buff, unsigned short envseg) {
166
  char *s = buff;
370 mateuszvis 167
  /* locate the prompt variable or use the default pattern */
438 mateuszvis 168
  const char far *fmt = env_lookup_val(envseg, "PROMPT");
370 mateuszvis 169
  if ((fmt == NULL) || (*fmt == 0)) fmt = "$p$g"; /* fallback to default if empty */
170
  /* build the prompt string based on pattern */
354 mateuszvis 171
  for (; *fmt != 0; fmt++) {
172
    if (*fmt != '$') {
173
      *s = *fmt;
174
      s++;
175
      continue;
176
    }
177
    /* escape code ($P, etc) */
178
    fmt++;
179
    switch (*fmt) {
180
      case 'Q':  /* $Q = = (equal sign) */
181
      case 'q':
182
        *s = '=';
183
        s++;
184
        break;
185
      case '$':  /* $$ = $ (dollar sign) */
186
        *s = '$';
187
        s++;
188
        break;
189
      case 'T':  /* $t = current time */
190
      case 't':
191
        s += sprintf(s, "00:00"); /* TODO */
192
        break;
193
      case 'D':  /* $D = current date */
194
      case 'd':
195
        s += sprintf(s, "1985-07-29"); /* TODO */
196
        break;
197
      case 'P':  /* $P = current drive and path */
198
      case 'p':
199
        _asm {
200
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
201
          int 0x21
202
          mov bx, s
203
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
204
        }
205
        *s += 'A';
206
        s++;
207
        *s = ':';
208
        s++;
209
        *s = '\\';
210
        s++;
211
        _asm {
212
          mov ah, 0x47    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */
213
          xor dl,dl       /* DL = drive number (00h = default, 01h = A:, etc) */
214
          mov si, s       /* DS:SI -> 64-byte buffer for ASCIZ pathname */
215
          int 0x21
216
        }
217
        while (*s != 0) s++; /* move ptr forward to end of pathname */
218
        break;
219
      case 'V':  /* $V = DOS version number */
220
      case 'v':
221
        s += sprintf(s, "VER"); /* TODO */
222
        break;
223
      case 'N':  /* $N = current drive */
224
      case 'n':
225
        _asm {
226
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
227
          int 0x21
228
          mov bx, s
229
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
230
        }
231
        *s += 'A';
232
        s++;
233
        break;
234
      case 'G':  /* $G = > (greater-than sign) */
235
      case 'g':
236
        *s = '>';
237
        s++;
238
        break;
239
      case 'L':  /* $L = < (less-than sign) */
240
      case 'l':
241
        *s = '<';
242
        s++;
243
        break;
244
      case 'B':  /* $B = | (pipe) */
245
      case 'b':
246
        *s = '|';
247
        s++;
248
        break;
249
      case 'H':  /* $H = backspace (erases previous character) */
250
      case 'h':
251
        *s = '\b';
252
        s++;
253
        break;
254
      case 'E':  /* $E = Escape code (ASCII 27) */
255
      case 'e':
256
        *s = 27;
257
        s++;
258
        break;
259
      case '_':  /* $_ = CR+LF */
260
        *s = '\r';
261
        s++;
262
        *s = '\n';
263
        s++;
264
        break;
265
    }
266
  }
474 mateuszvis 267
  *s = 0;
268
  output(buff);
354 mateuszvis 269
}
349 mateuszvis 270
 
271
 
458 mateuszvis 272
/* tries locating executable fname in path and fille res with result. returns 0 on success,
273
 * -1 on failed match and -2 on failed match + "don't even try with other paths"
274
 * format is filled the offset where extension starts in fname (-1 if not found) */
275
int lookup_cmd(char *res, const char *fname, const char *path, const char **extptr) {
276
  unsigned short lastbslash = 0xffff;
277
  unsigned short i, len;
278
  unsigned char explicitpath = 0;
279
 
280
  /* does the original fname had an explicit path prefix or explicit ext? */
281
  *extptr = NULL;
282
  for (i = 0; fname[i] != 0; i++) {
283
    switch (fname[i]) {
284
      case ':':
285
      case '\\':
286
        explicitpath = 1;
287
        *extptr = NULL; /* extension is the last dot AFTER all path delimiters */
288
        break;
289
      case '.':
290
        *extptr = fname + i + 1;
291
        break;
292
    }
293
  }
294
 
295
  /* normalize filename */
296
  if (file_truename(fname, res) != 0) return(-2);
297
 
298
  /* printf("truename: %s\r\n", res); */
299
 
300
  /* figure out where the command starts and if it has an explicit extension */
301
  for (len = 0; res[len] != 0; len++) {
302
    switch (res[len]) {
303
      case '?':   /* abort on any wildcard character */
304
      case '*':
305
        return(-2);
306
      case '\\':
307
        lastbslash = len;
308
        break;
309
    }
310
  }
311
 
312
  /* printf("lastbslash=%u\r\n", lastbslash); */
313
 
314
  /* if no path prefix in fname (':' or backslash), then assemble path+filename */
315
  if (!explicitpath) {
316
    if (path != NULL) {
317
      i = strlen(path);
318
    } else {
319
      i = 0;
320
    }
321
    if ((i != 0) && (path[i - 1] != '\\')) i++; /* add a byte for inserting a bkslash after path */
322
    memmove(res + i, res + lastbslash + 1, len - lastbslash);
323
    if (i != 0) {
324
      memmove(res, path, i);
325
      res[i - 1] = '\\';
326
    }
327
  }
328
 
329
  /* if no extension was initially provided, try matching COM, EXE, BAT */
330
  if (*extptr == NULL) {
331
    const char *ext[] = {".COM", ".EXE", ".BAT", NULL};
332
    len = strlen(res);
333
    for (i = 0; ext[i] != NULL; i++) {
334
      strcpy(res + len, ext[i]);
335
      /* printf("? '%s'\r\n", res); */
336
      *extptr = ext[i] + 1;
337
      if (file_getattr(res) >= 0) return(0);
338
    }
339
  } else { /* try finding it as-is */
340
    /* printf("? '%s'\r\n", res); */
341
    if (file_getattr(res) >= 0) return(0);
342
  }
343
 
344
  /* not found */
345
  if (explicitpath) return(-2); /* don't bother trying other paths, the caller had its own path preset anyway */
346
  return(-1);
347
}
348
 
349
 
479 mateuszvis 350
static void run_as_external(char *buff, const char *cmdline, unsigned short envseg, struct rmod_props far *rmod) {
472 mateuszvis 351
  char *cmdfile = buff + 512;
458 mateuszvis 352
  const char far *pathptr;
353
  int lookup;
354
  unsigned short i;
355
  const char *ext;
472 mateuszvis 356
  char *cmd = buff + 256;
479 mateuszvis 357
  const char *cmdtail;
461 mateuszvis 358
  char far *rmod_execprog = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPROG);
359
  char far *rmod_cmdtail = MK_FP(rmod->rmodseg, 0x81);
360
  _Packed struct {
361
    unsigned short envseg;
362
    unsigned long cmdtail;
363
    unsigned long fcb1;
364
    unsigned long fcb2;
365
  } far *ExecParam = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPARAM);
364 mateuszvis 366
 
472 mateuszvis 367
  /* find cmd and cmdtail */
368
  i = 0;
369
  cmdtail = cmdline;
370
  while (*cmdtail == ' ') cmdtail++; /* skip any leading spaces */
371
  while ((*cmdtail != ' ') && (*cmdtail != '/') && (*cmdtail != '+') && (*cmdtail != 0)) {
372
    cmd[i++] = *cmdtail;
373
    cmdtail++;
374
  }
375
  cmd[i] = 0;
364 mateuszvis 376
 
458 mateuszvis 377
  /* is this a command in curdir? */
472 mateuszvis 378
  lookup = lookup_cmd(cmdfile, cmd, NULL, &ext);
458 mateuszvis 379
  if (lookup == 0) {
380
    /* printf("FOUND LOCAL EXEC FILE: '%s'\r\n", cmdfile); */
381
    goto RUNCMDFILE;
382
  } else if (lookup == -2) {
383
    /* puts("NOT FOUND"); */
384
    return;
385
  }
386
 
387
  /* try matching something in PATH */
388
  pathptr = env_lookup_val(envseg, "PATH");
389
  if (pathptr == NULL) return;
390
 
391
  /* try each path in %PATH% */
392
  for (;;) {
393
    for (i = 0;; i++) {
394
      buff[i] = *pathptr;
395
      if ((buff[i] == 0) || (buff[i] == ';')) break;
396
      pathptr++;
397
    }
398
    buff[i] = 0;
472 mateuszvis 399
    lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
458 mateuszvis 400
    if (lookup == 0) break;
401
    if (lookup == -2) return;
402
    if (*pathptr == ';') {
403
      pathptr++;
404
    } else {
405
      return;
406
    }
407
  }
408
 
409
  RUNCMDFILE:
410
 
469 mateuszvis 411
  /* special handling of batch files */
412
  if ((ext != NULL) && (imatch(ext, "bat"))) {
413
    /* copy truename of the bat file to rmod buff */
414
    for (i = 0; cmdfile[i] != 0; i++) rmod->batfile[i] = cmdfile[i];
415
    rmod->batfile[i] = 0;
416
    /* copy args of the bat file to rmod buff */
417
    for (i = 0; cmdtail[i] != 0; i++) rmod->batargs[i] = cmdtail[i];
418
    /* reset the 'next line to execute' counter */
419
    rmod->batnextline = 0;
474 mateuszvis 420
    /* remember the echo flag (in case bat file disables echo) */
421
    rmod->flags &= ~FLAG_ECHO_BEFORE_BAT;
475 mateuszvis 422
    if (rmod->flags & FLAG_ECHOFLAG) rmod->flags |= FLAG_ECHO_BEFORE_BAT;
469 mateuszvis 423
    return;
424
  }
425
 
461 mateuszvis 426
  /* copy full filename to execute */
427
  for (i = 0; cmdfile[i] != 0; i++) rmod_execprog[i] = cmdfile[i];
428
  rmod_execprog[i] = 0;
429
 
430
  /* copy cmdtail to rmod's PSP and compute its len */
431
  for (i = 0; cmdtail[i] != 0; i++) rmod_cmdtail[i] = cmdtail[i];
432
  rmod_cmdtail[i] = '\r';
433
  rmod_cmdtail[-1] = i;
434
 
435
  /* set up rmod to execute the command */
436
 
464 mateuszvis 437
  ExecParam->envseg = envseg;
461 mateuszvis 438
  ExecParam->cmdtail = (unsigned long)MK_FP(rmod->rmodseg, 0x80); /* farptr, must be in PSP format (lenbyte args \r) */
439
  ExecParam->fcb1 = 0; /* TODO farptr */
440
  ExecParam->fcb2 = 0; /* TODO farptr */
441
  exit(0); /* let rmod do the job now */
364 mateuszvis 442
}
443
 
444
 
367 mateuszvis 445
static void set_comspec_to_self(unsigned short envseg) {
446
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
447
  char far *myenv = MK_FP(*psp_envseg, 0);
448
  unsigned short varcount;
449
  char buff[256] = "COMSPEC=";
450
  char *buffptr = buff + 8;
451
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
452
  while (*myenv != 0) {
453
    /* consume a NULL-terminated string */
454
    while (*myenv != 0) myenv++;
455
    /* move to next string */
456
    myenv++;
457
  }
458
  /* get next word, if 1 then EXEPATH follows */
459
  myenv++;
460
  varcount = *myenv;
461
  myenv++;
462
  varcount |= (*myenv << 8);
463
  myenv++;
464
  if (varcount != 1) return; /* NO EXEPATH FOUND */
465
  while (*myenv != 0) {
466
    *buffptr = *myenv;
467
    buffptr++;
468
    myenv++;
469
  }
470
  *buffptr = 0;
471
  /* printf("EXEPATH: '%s'\r\n", buff); */
472
  env_setvar(envseg, buff);
473
}
474
 
475
 
450 mateuszvis 476
/* wait for user input */
477
static void cmdline_getinput(unsigned short inpseg, unsigned short inpoff) {
478
  _asm {
479
    push ax
480
    push bx
481
    push cx
482
    push dx
483
    push ds
484
 
485
    /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
486
    mov ax, 0x4800
487
    int 0x2f
488
    mov bl, al /* save doskey status in BL */
489
 
490
    /* set up buffered input to inpseg:inpoff */
491
    mov ax, inpseg
492
    push ax
493
    pop ds
494
    mov dx, inpoff
495
 
496
    /* execute either DOS input or DOSKEY */
497
    test bl, bl /* zf set if no DOSKEY present */
498
    jnz DOSKEY
499
 
500
    mov ah, 0x0a
501
    int 0x21
502
    jmp short DONE
503
 
504
    DOSKEY:
505
    mov ax, 0x4810
506
    int 0x2f
507
 
508
    DONE:
509
    /* terminate command with a CR/LF */
510
    mov ah, 0x02 /* display character in dl */
511
    mov dl, 0x0d
512
    int 0x21
513
    mov dl, 0x0a
514
    int 0x21
515
 
516
    pop ds
517
    pop dx
518
    pop cx
519
    pop bx
520
    pop ax
521
  }
522
}
523
 
524
 
479 mateuszvis 525
/* fetches a line from batch file and write it to buff (NULL-terminated),
526
 * increments rmod counter and returns 0 on success. */
484 mateuszvis 527
static int getbatcmd(char *buff, unsigned char buffmaxlen, struct rmod_props far *rmod) {
469 mateuszvis 528
  unsigned short i;
474 mateuszvis 529
  unsigned short batname_seg = FP_SEG(rmod->batfile);
530
  unsigned short batname_off = FP_OFF(rmod->batfile);
531
  unsigned short filepos_cx = rmod->batnextline >> 16;
532
  unsigned short filepos_dx = rmod->batnextline & 0xffff;
533
  unsigned char blen = 0;
534
 
535
  /* open file, jump to offset filpos, and read data into buff.
536
   * result in blen (unchanged if EOF or failure). */
537
  _asm {
538
    push ax
539
    push bx
540
    push cx
541
    push dx
542
 
543
    /* open file (read-only) */
544
    mov dx, batname_off
545
    mov ax, batname_seg
546
    push ds     /* save DS */
547
    mov ds, ax
548
    mov ax, 0x3d00
549
    int 0x21    /* handle in ax on success */
550
    pop ds      /* restore DS */
551
    jc DONE
552
    mov bx, ax  /* save handle to bx */
553
 
554
    /* jump to file offset CX:DX */
555
    mov ax, 0x4200
556
    mov cx, filepos_cx
557
    mov dx, filepos_dx
558
    int 0x21  /* CF clear on success, DX:AX set to cur pos */
559
    jc CLOSEANDQUIT
560
 
561
    /* read the line into buff */
562
    mov ah, 0x3f
484 mateuszvis 563
    xor ch, ch
564
    mov cl, buffmaxlen
474 mateuszvis 565
    mov dx, buff
566
    int 0x21 /* CF clear on success, AX=number of bytes read */
567
    jc CLOSEANDQUIT
568
    mov blen, al
569
 
570
    CLOSEANDQUIT:
571
    /* close file (handle in bx) */
572
    mov ah, 0x3e
573
    int 0x21
574
 
575
    DONE:
576
    pop dx
577
    pop cx
578
    pop bx
579
    pop ax
469 mateuszvis 580
  }
470 mateuszvis 581
 
474 mateuszvis 582
  /* printf("blen=%u filepos_cx=%u filepos_dx=%u\r\n", blen, filepos_cx, filepos_dx); */
470 mateuszvis 583
 
474 mateuszvis 584
  /* on EOF - abort processing the bat file */
585
  if (blen == 0) goto OOPS;
586
 
587
  /* find nearest \n to inc batch offset and replace \r by NULL terminator
588
   * I support all CR/LF, CR- and LF-terminated batch files */
589
  for (i = 0; i < blen; i++) {
590
    if ((buff[i] == '\r') || (buff[i] == '\n')) {
591
      if ((buff[i] == '\r') && ((i+1) < blen) && (buff[i+1] == '\n')) rmod->batnextline += 1;
592
      break;
593
    }
594
  }
595
  buff[i] = 0;
596
  rmod->batnextline += i + 1;
597
 
598
  return(0);
599
 
600
  OOPS:
601
  rmod->batfile[0] = 0;
602
  rmod->batnextline = 0;
603
  return(-1);
469 mateuszvis 604
}
605
 
606
 
443 mateuszvis 607
int main(void) {
372 mateuszvis 608
  static struct config cfg;
609
  static unsigned short far *rmod_envseg;
610
  static unsigned short far *lastexitcode;
449 mateuszvis 611
  static struct rmod_props far *rmod;
479 mateuszvis 612
  static char *cmdline;
349 mateuszvis 613
 
479 mateuszvis 614
  rmod = rmod_find(BUFFER_len);
449 mateuszvis 615
  if (rmod == NULL) {
485 mateuszvis 616
    /* look at command line parameters (in case env size if set there) */
617
    parse_argv(&cfg);
479 mateuszvis 618
    rmod = rmod_install(cfg.envsiz, BUFFER, BUFFER_len);
449 mateuszvis 619
    if (rmod == NULL) {
369 mateuszvis 620
      outputnl("ERROR: rmod_install() failed");
349 mateuszvis 621
      return(1);
622
    }
475 mateuszvis 623
    /* copy flags to rmod's storage (and enable ECHO) */
624
    rmod->flags = cfg.flags | FLAG_ECHOFLAG;
465 mateuszvis 625
    /* printf("rmod installed at %Fp\r\n", rmod); */
349 mateuszvis 626
  } else {
465 mateuszvis 627
    /* printf("rmod found at %Fp\r\n", rmod); */
628
    /* if I was spawned by rmod and FLAG_EXEC_AND_QUIT is set, then I should
629
     * die asap, because the command has been executed already, so I no longer
630
     * have a purpose in life */
469 mateuszvis 631
    if (rmod->flags & FLAG_EXEC_AND_QUIT) sayonara(rmod);
349 mateuszvis 632
  }
633
 
490 mateuszvis 634
  /* install a few guardvals in memory to detect some cases of overflows */
635
  memguard_set();
636
 
465 mateuszvis 637
  rmod_envseg = MK_FP(rmod->rmodseg, RMOD_OFFSET_ENVSEG);
638
  lastexitcode = MK_FP(rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
350 mateuszvis 639
 
367 mateuszvis 640
  /* make COMPSEC point to myself */
641
  set_comspec_to_self(*rmod_envseg);
642
 
369 mateuszvis 643
/*  {
350 mateuszvis 644
    unsigned short envsiz;
645
    unsigned short far *sizptr = MK_FP(*rmod_envseg - 1, 3);
646
    envsiz = *sizptr;
647
    envsiz *= 16;
465 mateuszvis 648
    printf("rmod_inpbuff at %04X:%04X, env_seg at %04X:0000 (env_size = %u bytes)\r\n", rmod->rmodseg, RMOD_OFFSET_INPBUFF, *rmod_envseg, envsiz);
369 mateuszvis 649
  }*/
349 mateuszvis 650
 
483 mateuszvis 651
  /* on /P check for the presence of AUTOEXEC.BAT and execute it if found */
652
  if (cfg.flags & FLAG_PERMANENT) {
653
    if (file_getattr("AUTOEXEC.BAT") >= 0) cfg.execcmd = "AUTOEXEC.BAT";
654
  }
655
 
443 mateuszvis 656
  do {
480 mateuszvis 657
    /* terminate previous command with a CR/LF if ECHO ON (but not during BAT processing) */
483 mateuszvis 658
    if ((rmod->flags & FLAG_ECHOFLAG) && (rmod->batfile[0] == 0)) outputnl("");
474 mateuszvis 659
 
660
    SKIP_NEWLINE:
661
 
662
    /* cancel any redirections that may have been set up before */
663
    redir_revert();
664
 
490 mateuszvis 665
    /* memory check */
666
    memguard_check(rmod->rmodseg);
474 mateuszvis 667
 
490 mateuszvis 668
    /* preset cmdline to point at the end of my general-purpose buffer (with
669
     * one extra byte for the NULL terminator and another for memguard val) */
670
    cmdline = BUFFER + sizeof(BUFFER) - (CMDLINE_MAXLEN + 2);
671
 
437 mateuszvis 672
    /* (re)load translation strings if needed */
673
    nls_langreload(BUFFER, *rmod_envseg);
674
 
443 mateuszvis 675
    /* skip user input if I have a command to exec (/C or /K) */
676
    if (cfg.execcmd != NULL) {
677
      cmdline = cfg.execcmd;
678
      cfg.execcmd = NULL;
679
      goto EXEC_CMDLINE;
680
    }
681
 
469 mateuszvis 682
    /* if batch file is being executed -> fetch next line */
683
    if (rmod->batfile[0] != 0) {
484 mateuszvis 684
      if (getbatcmd(cmdline, CMDLINE_MAXLEN, rmod) != 0) { /* end of batch */
474 mateuszvis 685
        /* restore echo flag as it was before running the bat file */
475 mateuszvis 686
        rmod->flags &= ~FLAG_ECHOFLAG;
687
        if (rmod->flags & FLAG_ECHO_BEFORE_BAT) rmod->flags |= FLAG_ECHOFLAG;
474 mateuszvis 688
        continue;
689
      }
480 mateuszvis 690
      /* skip any leading spaces */
691
      while (*cmdline == ' ') cmdline++;
474 mateuszvis 692
      /* output prompt and command on screen if echo on and command is not
693
       * inhibiting it with the @ prefix */
479 mateuszvis 694
      if ((rmod->flags & FLAG_ECHOFLAG) && (cmdline[0] != '@')) {
474 mateuszvis 695
        build_and_display_prompt(BUFFER, *rmod_envseg);
479 mateuszvis 696
        outputnl(cmdline);
474 mateuszvis 697
      }
479 mateuszvis 698
      /* skip the @ prefix if present, it is no longer useful */
699
      if (cmdline[0] == '@') cmdline++;
469 mateuszvis 700
    } else {
474 mateuszvis 701
      /* interactive mode: display prompt (if echo enabled) and wait for user
702
       * command line */
475 mateuszvis 703
      if (rmod->flags & FLAG_ECHOFLAG) build_and_display_prompt(BUFFER, *rmod_envseg);
474 mateuszvis 704
      /* collect user input */
469 mateuszvis 705
      cmdline_getinput(FP_SEG(rmod->inputbuf), FP_OFF(rmod->inputbuf));
479 mateuszvis 706
      /* copy it to local cmdline */
707
      if (rmod->inputbuf[1] != 0) _fmemcpy(cmdline, rmod->inputbuf + 2, rmod->inputbuf[1]);
708
      cmdline[(unsigned)(rmod->inputbuf[1])] = 0; /* zero-terminate local buff (oriignal is '\r'-terminated) */
469 mateuszvis 709
    }
349 mateuszvis 710
 
405 mateuszvis 711
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
479 mateuszvis 712
    if (cmdline[0] == 0) goto SKIP_NEWLINE;
349 mateuszvis 713
 
443 mateuszvis 714
    /* I jump here when I need to exec an initial command (/C or /K) */
715
    EXEC_CMDLINE:
716
 
364 mateuszvis 717
    /* move pointer forward to skip over any leading spaces */
718
    while (*cmdline == ' ') cmdline++;
349 mateuszvis 719
 
367 mateuszvis 720
    /* update rmod's ptr to COMPSPEC so it is always up to date */
465 mateuszvis 721
    rmod_updatecomspecptr(rmod->rmodseg, *rmod_envseg);
367 mateuszvis 722
 
402 mateuszvis 723
    /* handle redirections (if any) */
724
    if (redir_parsecmd(cmdline, BUFFER) != 0) {
725
      outputnl("");
726
      continue;
727
    }
728
 
364 mateuszvis 729
    /* try matching (and executing) an internal command */
449 mateuszvis 730
    if (cmd_process(rmod, *rmod_envseg, cmdline, BUFFER) >= -1) {
443 mateuszvis 731
      /* internal command executed */
732
      redir_revert(); /* revert stdout (in case it was redirected) */
733
      continue;
353 mateuszvis 734
    }
352 mateuszvis 735
 
364 mateuszvis 736
    /* if here, then this was not an internal command */
461 mateuszvis 737
    run_as_external(BUFFER, cmdline, *rmod_envseg, rmod);
469 mateuszvis 738
    /* perhaps this is a newly launched BAT file */
739
    if ((rmod->batfile[0] != 0) && (rmod->batnextline == 0)) goto SKIP_NEWLINE;
349 mateuszvis 740
 
474 mateuszvis 741
    /* revert stdout (so the err msg is not redirected) */
402 mateuszvis 742
    redir_revert();
743
 
465 mateuszvis 744
    /* run_as_external() does not return on success, if I am still alive then
745
     * external command failed to execute */
369 mateuszvis 746
    outputnl("Bad command or file name");
349 mateuszvis 747
 
449 mateuszvis 748
  } while ((rmod->flags & FLAG_EXEC_AND_QUIT) == 0);
349 mateuszvis 749
 
449 mateuszvis 750
  sayonara(rmod);
349 mateuszvis 751
  return(0);
752
}