Subversion Repositories SvarDOS

Rev

Rev 458 | Rev 464 | 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
/*
26
 * SvarCOM is a command-line interpreter.
27
 *
28
 * a little memory area is allocated as high as possible. it contains:
29
 *  - a signature (like XMS drivers do)
30
 *  - a routine for exec'ing programs
31
 *  - a "last command" buffer for input history
32
 *
33
 * when svarcom starts, it tries locating the routine in memory.
34
 *
35
 * if found:
36
 *   waits for user input and processes it. if execing something is required, set the "next exec" field in routine's memory and quit.
37
 *
38
 * if not found:
39
 *   installs it by creating a new PSP, set int 22 vector to the routine, set my "parent PSP" to the routine
40
 *   and quit.
41
 *
42
 * PSP structure
43
 * http://www.piclist.com/techref/dos/psps.htm
44
 *
45
 *
46
 *
47
 * === MCB ===
48
 *
49
 * each time that DOS allocates memory, it prefixes the allocated memory with
50
 * a 16-bytes structure called a "Memory Control Block" (MCB). This control
51
 * block has the following structure:
52
 *
53
 * Offset  Size     Description
54
 *   00h   byte     'M' =  non-last member of the MCB chain
55
 *                  'Z' = indicates last entry in MCB chain
56
 *                  other values cause "Memory Allocation Failure" on exit
57
 *   01h   word     PSP segment address of the owner (Process Id)
58
 *                  possible values:
59
 *                    0 = free
60
 *                    8 = Allocated by DOS before first user pgm loaded
61
 *                    other = Process Id/PSP segment address of owner
62
 *   03h  word      number of paragraphs related to this MCB (excluding MCB)
63
 *   05h  11 bytes  reserved
64
 *   10h  ...       start of actual allocated memory block
65
 */
66
 
67
#include <i86.h>
68
#include <dos.h>
69
#include <stdio.h>
350 mateuszvis 70
#include <stdlib.h>
349 mateuszvis 71
#include <string.h>
72
 
73
#include <process.h>
74
 
352 mateuszvis 75
#include "cmd.h"
366 mateuszvis 76
#include "env.h"
352 mateuszvis 77
#include "helpers.h"
402 mateuszvis 78
#include "redir.h"
351 mateuszvis 79
#include "rmodinit.h"
448 mateuszvis 80
#include "sayonara.h"
349 mateuszvis 81
 
443 mateuszvis 82
 
349 mateuszvis 83
struct config {
449 mateuszvis 84
  unsigned char flags; /* command.com flags, as defined in rmodinit.h */
443 mateuszvis 85
  char *execcmd;
410 mateuszvis 86
  unsigned short envsiz;
443 mateuszvis 87
};
349 mateuszvis 88
 
89
 
443 mateuszvis 90
/* parses command line the hard way (directly from PSP) */
91
static void parse_argv(struct config *cfg) {
92
  unsigned short i;
93
  const unsigned char *cmdlinelen = (unsigned char *)0x80;
94
  char *cmdline = (char *)0x81;
95
 
349 mateuszvis 96
  memset(cfg, 0, sizeof(*cfg));
350 mateuszvis 97
 
443 mateuszvis 98
  /* set a NULL terminator on cmdline */
99
  cmdline[*cmdlinelen] = 0;
100
 
101
  for (i = 0;;) {
102
 
103
    /* skip over any leading spaces */
104
    for (;; i++) {
105
      if (cmdline[i] == 0) return;
106
      if (cmdline[i] != ' ') break;
349 mateuszvis 107
    }
443 mateuszvis 108
 
109
    if (cmdline[i] != '/') {
110
      output("Invalid parameter: ");
111
      outputnl(cmdline + i);
112
      /* exit(1); */
113
    } else {
114
      i++;        /* skip the slash */
115
      switch (cmdline[i]) {
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 + i + 1;
123
          return;
124
 
125
        case 'e': /* preset the initial size of the environment block */
126
        case 'E':
127
          i++;
128
          if (cmdline[i] == ':') i++; /* could be /E:size */
129
          atous(&(cfg->envsiz), cmdline + i);
130
          if (cfg->envsiz < 64) cfg->envsiz = 0;
131
          break;
132
 
449 mateuszvis 133
        case 'p': /* permanent shell (can't exit) */
134
        case 'P':
135
          cfg->flags |= FLAG_PERMANENT;
136
          break;
137
 
444 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");
449 mateuszvis 144
          outputnl("/P         Makes the new command interpreter permanent (can't exit)");
444 mateuszvis 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
 
443 mateuszvis 150
        default:
151
          output("Invalid switch:");
152
          output(" ");
153
          outputnl(cmdline + i);
154
          exit(1);
155
          break;
410 mateuszvis 156
      }
350 mateuszvis 157
    }
443 mateuszvis 158
 
159
    /* move to next argument or quit processing if end of cmdline */
160
    for (i++; (cmdline[i] != 0) && (cmdline[i] != ' ') && (cmdline[i] != '/'); i++);
161
 
349 mateuszvis 162
  }
163
}
164
 
165
 
370 mateuszvis 166
static void buildprompt(char *s, unsigned short envseg) {
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
  }
267
  *s = '$';
268
}
349 mateuszvis 269
 
270
 
458 mateuszvis 271
/* tries locating executable fname in path and fille res with result. returns 0 on success,
272
 * -1 on failed match and -2 on failed match + "don't even try with other paths"
273
 * format is filled the offset where extension starts in fname (-1 if not found) */
274
int lookup_cmd(char *res, const char *fname, const char *path, const char **extptr) {
275
  unsigned short lastbslash = 0xffff;
276
  unsigned short i, len;
277
  unsigned char explicitpath = 0;
278
 
279
  /* does the original fname had an explicit path prefix or explicit ext? */
280
  *extptr = NULL;
281
  for (i = 0; fname[i] != 0; i++) {
282
    switch (fname[i]) {
283
      case ':':
284
      case '\\':
285
        explicitpath = 1;
286
        *extptr = NULL; /* extension is the last dot AFTER all path delimiters */
287
        break;
288
      case '.':
289
        *extptr = fname + i + 1;
290
        break;
291
    }
292
  }
293
 
294
  /* normalize filename */
295
  if (file_truename(fname, res) != 0) return(-2);
296
 
297
  /* printf("truename: %s\r\n", res); */
298
 
299
  /* figure out where the command starts and if it has an explicit extension */
300
  for (len = 0; res[len] != 0; len++) {
301
    switch (res[len]) {
302
      case '?':   /* abort on any wildcard character */
303
      case '*':
304
        return(-2);
305
      case '\\':
306
        lastbslash = len;
307
        break;
308
    }
309
  }
310
 
311
  /* printf("lastbslash=%u\r\n", lastbslash); */
312
 
313
  /* if no path prefix in fname (':' or backslash), then assemble path+filename */
314
  if (!explicitpath) {
315
    if (path != NULL) {
316
      i = strlen(path);
317
    } else {
318
      i = 0;
319
    }
320
    if ((i != 0) && (path[i - 1] != '\\')) i++; /* add a byte for inserting a bkslash after path */
321
    memmove(res + i, res + lastbslash + 1, len - lastbslash);
322
    if (i != 0) {
323
      memmove(res, path, i);
324
      res[i - 1] = '\\';
325
    }
326
  }
327
 
328
  /* if no extension was initially provided, try matching COM, EXE, BAT */
329
  if (*extptr == NULL) {
330
    const char *ext[] = {".COM", ".EXE", ".BAT", NULL};
331
    len = strlen(res);
332
    for (i = 0; ext[i] != NULL; i++) {
333
      strcpy(res + len, ext[i]);
334
      /* printf("? '%s'\r\n", res); */
335
      *extptr = ext[i] + 1;
336
      if (file_getattr(res) >= 0) return(0);
337
    }
338
  } else { /* try finding it as-is */
339
    /* printf("? '%s'\r\n", res); */
340
    if (file_getattr(res) >= 0) return(0);
341
  }
342
 
343
  /* not found */
344
  if (explicitpath) return(-2); /* don't bother trying other paths, the caller had its own path preset anyway */
345
  return(-1);
346
}
347
 
348
 
461 mateuszvis 349
static void run_as_external(char *buff, const char far *cmdline, unsigned short envseg, struct rmod_props far *rmod) {
457 mateuszvis 350
  char const **argvlist = (void *)(buff + 512);
458 mateuszvis 351
  char *cmdfile = buff + 1024;
352
  const char far *pathptr;
353
  int lookup;
354
  unsigned short i;
355
  const char *ext;
461 mateuszvis 356
  const char far *cmdtail;
357
  char far *rmod_execprog = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPROG);
358
  char far *rmod_cmdtail = MK_FP(rmod->rmodseg, 0x81);
359
  _Packed struct {
360
    unsigned short envseg;
361
    unsigned long cmdtail;
362
    unsigned long fcb1;
363
    unsigned long fcb2;
364
  } far *ExecParam = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPARAM);
364 mateuszvis 365
 
458 mateuszvis 366
  cmd_explode(buff + 2048, cmdline, argvlist);
364 mateuszvis 367
 
368
  /* for (i = 0; argvlist[i] != NULL; i++) printf("arg #%d = '%s'\r\n", i, argvlist[i]); */
369
 
458 mateuszvis 370
  /* is this a command in curdir? */
371
  lookup = lookup_cmd(cmdfile, argvlist[0], NULL, &ext);
372
  if (lookup == 0) {
373
    /* printf("FOUND LOCAL EXEC FILE: '%s'\r\n", cmdfile); */
374
    goto RUNCMDFILE;
375
  } else if (lookup == -2) {
376
    /* puts("NOT FOUND"); */
377
    return;
378
  }
379
 
380
  /* try matching something in PATH */
381
  pathptr = env_lookup_val(envseg, "PATH");
382
  if (pathptr == NULL) return;
383
 
384
  /* try each path in %PATH% */
385
  for (;;) {
386
    for (i = 0;; i++) {
387
      buff[i] = *pathptr;
388
      if ((buff[i] == 0) || (buff[i] == ';')) break;
389
      pathptr++;
390
    }
391
    buff[i] = 0;
392
    lookup = lookup_cmd(cmdfile, argvlist[0], buff, &ext);
393
    if (lookup == 0) break;
394
    if (lookup == -2) return;
395
    if (*pathptr == ';') {
396
      pathptr++;
397
    } else {
398
      return;
399
    }
400
  }
401
 
402
  RUNCMDFILE:
403
  /* TODO run command through INT 0x21 */
404
  /* TODO copy environment and append full exec path */
405
  /* TODO special handling of batch files */
406
  if ((ext != NULL) && (imatch(ext, "bat"))) {
407
    outputnl("batch processing not supported yet");
408
    return;
409
  }
410
 
411
  /* printf("Exec: '%s'\r\n", cmdfile); */
412
 
461 mateuszvis 413
  /* find cmdtail */
414
  cmdtail = cmdline;
415
  while (*cmdtail == ' ') cmdtail++;
416
  while ((*cmdtail != ' ') && (*cmdtail != '/') && (*cmdtail != '+') && (*cmdtail != 0)) {
417
    cmdtail++;
418
  }
419
 
420
  /* copy full filename to execute */
421
  for (i = 0; cmdfile[i] != 0; i++) rmod_execprog[i] = cmdfile[i];
422
  rmod_execprog[i] = 0;
423
 
424
  /* copy cmdtail to rmod's PSP and compute its len */
425
  for (i = 0; cmdtail[i] != 0; i++) rmod_cmdtail[i] = cmdtail[i];
426
  rmod_cmdtail[i] = '\r';
427
  rmod_cmdtail[-1] = i;
428
 
429
  printf("Exec: '");
430
  for (i = 0; rmod_execprog[i] != 0; i++) {
431
    printf("%c", rmod_execprog[i]);
432
  }
433
  printf("'\r\n");
434
 
435
  /* set up rmod to execute the command */
436
 
437
  ExecParam->envseg = 0; /* 0 = use parent's env segment */
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
 
443 mateuszvis 525
int main(void) {
372 mateuszvis 526
  static struct config cfg;
527
  static unsigned short rmod_seg;
528
  static unsigned short far *rmod_envseg;
529
  static unsigned short far *lastexitcode;
530
  static unsigned char BUFFER[4096];
449 mateuszvis 531
  static struct rmod_props far *rmod;
349 mateuszvis 532
 
443 mateuszvis 533
  parse_argv(&cfg);
349 mateuszvis 534
 
449 mateuszvis 535
  rmod = rmod_find();
536
  if (rmod == NULL) {
537
    rmod = rmod_install(cfg.envsiz);
538
    if (rmod == NULL) {
369 mateuszvis 539
      outputnl("ERROR: rmod_install() failed");
349 mateuszvis 540
      return(1);
541
    }
449 mateuszvis 542
    /* copy flags to rmod's storage */
543
    rmod->flags = cfg.flags;
369 mateuszvis 544
/*    printf("rmod installed at seg 0x%04X\r\n", rmod_seg); */
349 mateuszvis 545
  } else {
369 mateuszvis 546
/*    printf("rmod found at seg 0x%04x\r\n", rmod_seg); */
349 mateuszvis 547
  }
548
 
449 mateuszvis 549
  rmod_seg = rmod->rmodseg;
350 mateuszvis 550
  rmod_envseg = MK_FP(rmod_seg, RMOD_OFFSET_ENVSEG);
353 mateuszvis 551
  lastexitcode = MK_FP(rmod_seg, RMOD_OFFSET_LEXITCODE);
350 mateuszvis 552
 
367 mateuszvis 553
  /* make COMPSEC point to myself */
554
  set_comspec_to_self(*rmod_envseg);
555
 
369 mateuszvis 556
/*  {
350 mateuszvis 557
    unsigned short envsiz;
558
    unsigned short far *sizptr = MK_FP(*rmod_envseg - 1, 3);
559
    envsiz = *sizptr;
560
    envsiz *= 16;
561
    printf("rmod_inpbuff at %04X:%04X, env_seg at %04X:0000 (env_size = %u bytes)\r\n", rmod_seg, RMOD_OFFSET_INPBUFF, *rmod_envseg, envsiz);
369 mateuszvis 562
  }*/
349 mateuszvis 563
 
443 mateuszvis 564
  do {
450 mateuszvis 565
    char far *cmdline = rmod->inputbuf + 2;
349 mateuszvis 566
 
437 mateuszvis 567
    /* (re)load translation strings if needed */
568
    nls_langreload(BUFFER, *rmod_envseg);
569
 
443 mateuszvis 570
    /* skip user input if I have a command to exec (/C or /K) */
571
    if (cfg.execcmd != NULL) {
572
      cmdline = cfg.execcmd;
573
      cfg.execcmd = NULL;
574
      goto EXEC_CMDLINE;
575
    }
576
 
450 mateuszvis 577
    if (rmod->echoflag != 0) outputnl(""); /* terminate the previous command with a CR/LF */
443 mateuszvis 578
 
405 mateuszvis 579
    SKIP_NEWLINE:
580
 
581
    /* print shell prompt (only if ECHO is enabled) */
450 mateuszvis 582
    if (rmod->echoflag != 0) {
372 mateuszvis 583
      char *promptptr = BUFFER;
370 mateuszvis 584
      buildprompt(promptptr, *rmod_envseg);
354 mateuszvis 585
      _asm {
364 mateuszvis 586
        push ax
354 mateuszvis 587
        push dx
588
        mov ah, 0x09
589
        mov dx, promptptr
590
        int 0x21
591
        pop dx
364 mateuszvis 592
        pop ax
354 mateuszvis 593
      }
594
    }
349 mateuszvis 595
 
405 mateuszvis 596
    /* revert input history terminator to \r */
597
    if (cmdline[-1] != 0) {
598
      cmdline[(unsigned short)(cmdline[-1])] = '\r';
599
    }
600
 
450 mateuszvis 601
    /* wait for user command line */
602
    cmdline_getinput(FP_SEG(rmod->inputbuf), FP_OFF(rmod->inputbuf));
349 mateuszvis 603
 
405 mateuszvis 604
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
605
    if (cmdline[-1] == 0) goto SKIP_NEWLINE;
349 mateuszvis 606
 
364 mateuszvis 607
    /* replace \r by a zero terminator */
608
    cmdline[(unsigned char)(cmdline[-1])] = 0;
349 mateuszvis 609
 
443 mateuszvis 610
    /* I jump here when I need to exec an initial command (/C or /K) */
611
    EXEC_CMDLINE:
612
 
364 mateuszvis 613
    /* move pointer forward to skip over any leading spaces */
614
    while (*cmdline == ' ') cmdline++;
349 mateuszvis 615
 
367 mateuszvis 616
    /* update rmod's ptr to COMPSPEC so it is always up to date */
617
    rmod_updatecomspecptr(rmod_seg, *rmod_envseg);
618
 
402 mateuszvis 619
    /* handle redirections (if any) */
620
    if (redir_parsecmd(cmdline, BUFFER) != 0) {
621
      outputnl("");
622
      continue;
623
    }
624
 
364 mateuszvis 625
    /* try matching (and executing) an internal command */
449 mateuszvis 626
    if (cmd_process(rmod, *rmod_envseg, cmdline, BUFFER) >= -1) {
443 mateuszvis 627
      /* internal command executed */
628
      redir_revert(); /* revert stdout (in case it was redirected) */
629
      continue;
353 mateuszvis 630
    }
352 mateuszvis 631
 
364 mateuszvis 632
    /* if here, then this was not an internal command */
461 mateuszvis 633
    run_as_external(BUFFER, cmdline, *rmod_envseg, rmod);
349 mateuszvis 634
 
402 mateuszvis 635
    /* revert stdout (in case it was redirected) */
636
    redir_revert();
637
 
349 mateuszvis 638
    /* execvp() replaces the current process by the new one
639
    if I am still alive then external command failed to execute */
369 mateuszvis 640
    outputnl("Bad command or file name");
349 mateuszvis 641
 
449 mateuszvis 642
  } while ((rmod->flags & FLAG_EXEC_AND_QUIT) == 0);
349 mateuszvis 643
 
449 mateuszvis 644
  sayonara(rmod);
349 mateuszvis 645
  return(0);
646
}