Subversion Repositories SvarDOS

Rev

Rev 443 | Rev 448 | 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"
349 mateuszvis 80
 
443 mateuszvis 81
#define FLAG_EXEC_AND_QUIT 1
82
 
349 mateuszvis 83
struct config {
443 mateuszvis 84
  unsigned char flags;
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
 
444 mateuszvis 133
        case '?':
134
          outputnl("Starts the SvarCOM command interpreter");
135
          outputnl("");
136
          outputnl("COMMAND /E:nnn [/[C|K] command]");
137
          outputnl("");
138
          outputnl("/E:nnn     Sets the environment size to nnn bytes");
139
          outputnl("/C         Executes the specified command and returns");
140
          outputnl("/K         Executes the specified command and continues running");
141
          exit(1);
142
          break;
143
 
443 mateuszvis 144
        default:
145
          output("Invalid switch:");
146
          output(" ");
147
          outputnl(cmdline + i);
148
          exit(1);
149
          break;
410 mateuszvis 150
      }
350 mateuszvis 151
    }
443 mateuszvis 152
 
153
    /* move to next argument or quit processing if end of cmdline */
154
    for (i++; (cmdline[i] != 0) && (cmdline[i] != ' ') && (cmdline[i] != '/'); i++);
155
 
349 mateuszvis 156
  }
157
}
158
 
159
 
370 mateuszvis 160
static void buildprompt(char *s, unsigned short envseg) {
161
  /* locate the prompt variable or use the default pattern */
438 mateuszvis 162
  const char far *fmt = env_lookup_val(envseg, "PROMPT");
370 mateuszvis 163
  if ((fmt == NULL) || (*fmt == 0)) fmt = "$p$g"; /* fallback to default if empty */
164
  /* build the prompt string based on pattern */
354 mateuszvis 165
  for (; *fmt != 0; fmt++) {
166
    if (*fmt != '$') {
167
      *s = *fmt;
168
      s++;
169
      continue;
170
    }
171
    /* escape code ($P, etc) */
172
    fmt++;
173
    switch (*fmt) {
174
      case 'Q':  /* $Q = = (equal sign) */
175
      case 'q':
176
        *s = '=';
177
        s++;
178
        break;
179
      case '$':  /* $$ = $ (dollar sign) */
180
        *s = '$';
181
        s++;
182
        break;
183
      case 'T':  /* $t = current time */
184
      case 't':
185
        s += sprintf(s, "00:00"); /* TODO */
186
        break;
187
      case 'D':  /* $D = current date */
188
      case 'd':
189
        s += sprintf(s, "1985-07-29"); /* TODO */
190
        break;
191
      case 'P':  /* $P = current drive and path */
192
      case 'p':
193
        _asm {
194
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
195
          int 0x21
196
          mov bx, s
197
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
198
        }
199
        *s += 'A';
200
        s++;
201
        *s = ':';
202
        s++;
203
        *s = '\\';
204
        s++;
205
        _asm {
206
          mov ah, 0x47    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */
207
          xor dl,dl       /* DL = drive number (00h = default, 01h = A:, etc) */
208
          mov si, s       /* DS:SI -> 64-byte buffer for ASCIZ pathname */
209
          int 0x21
210
        }
211
        while (*s != 0) s++; /* move ptr forward to end of pathname */
212
        break;
213
      case 'V':  /* $V = DOS version number */
214
      case 'v':
215
        s += sprintf(s, "VER"); /* TODO */
216
        break;
217
      case 'N':  /* $N = current drive */
218
      case 'n':
219
        _asm {
220
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
221
          int 0x21
222
          mov bx, s
223
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
224
        }
225
        *s += 'A';
226
        s++;
227
        break;
228
      case 'G':  /* $G = > (greater-than sign) */
229
      case 'g':
230
        *s = '>';
231
        s++;
232
        break;
233
      case 'L':  /* $L = < (less-than sign) */
234
      case 'l':
235
        *s = '<';
236
        s++;
237
        break;
238
      case 'B':  /* $B = | (pipe) */
239
      case 'b':
240
        *s = '|';
241
        s++;
242
        break;
243
      case 'H':  /* $H = backspace (erases previous character) */
244
      case 'h':
245
        *s = '\b';
246
        s++;
247
        break;
248
      case 'E':  /* $E = Escape code (ASCII 27) */
249
      case 'e':
250
        *s = 27;
251
        s++;
252
        break;
253
      case '_':  /* $_ = CR+LF */
254
        *s = '\r';
255
        s++;
256
        *s = '\n';
257
        s++;
258
        break;
259
    }
260
  }
261
  *s = '$';
262
}
349 mateuszvis 263
 
264
 
364 mateuszvis 265
static void run_as_external(const char far *cmdline) {
266
  char buff[256];
267
  char const *argvlist[256];
268
  int i, n;
269
  /* copy buffer to a near var (incl. trailing CR), insert a space before
270
     every slash to make sure arguments are well separated */
271
  n = 0;
272
  i = 0;
273
  for (;;) {
274
    if (cmdline[i] == '/') buff[n++] = ' ';
275
    buff[n++] = cmdline[i++];
276
    if (buff[n] == 0) break;
277
  }
278
 
279
  cmd_explode(buff, cmdline, argvlist);
280
 
281
  /* for (i = 0; argvlist[i] != NULL; i++) printf("arg #%d = '%s'\r\n", i, argvlist[i]); */
282
 
283
  /* must be an external command then. this call should never return, unless
284
   * the other program failed to be executed. */
285
  execvp(argvlist[0], argvlist);
286
}
287
 
288
 
367 mateuszvis 289
static void set_comspec_to_self(unsigned short envseg) {
290
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
291
  char far *myenv = MK_FP(*psp_envseg, 0);
292
  unsigned short varcount;
293
  char buff[256] = "COMSPEC=";
294
  char *buffptr = buff + 8;
295
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
296
  while (*myenv != 0) {
297
    /* consume a NULL-terminated string */
298
    while (*myenv != 0) myenv++;
299
    /* move to next string */
300
    myenv++;
301
  }
302
  /* get next word, if 1 then EXEPATH follows */
303
  myenv++;
304
  varcount = *myenv;
305
  myenv++;
306
  varcount |= (*myenv << 8);
307
  myenv++;
308
  if (varcount != 1) return; /* NO EXEPATH FOUND */
309
  while (*myenv != 0) {
310
    *buffptr = *myenv;
311
    buffptr++;
312
    myenv++;
313
  }
314
  *buffptr = 0;
315
  /* printf("EXEPATH: '%s'\r\n", buff); */
316
  env_setvar(envseg, buff);
317
}
318
 
319
 
443 mateuszvis 320
int main(void) {
372 mateuszvis 321
  static struct config cfg;
322
  static unsigned short rmod_seg;
323
  static unsigned short far *rmod_envseg;
324
  static unsigned short far *lastexitcode;
325
  static unsigned char BUFFER[4096];
349 mateuszvis 326
 
443 mateuszvis 327
  parse_argv(&cfg);
349 mateuszvis 328
 
350 mateuszvis 329
  rmod_seg = rmod_find();
349 mateuszvis 330
  if (rmod_seg == 0xffff) {
350 mateuszvis 331
    rmod_seg = rmod_install(cfg.envsiz);
349 mateuszvis 332
    if (rmod_seg == 0xffff) {
369 mateuszvis 333
      outputnl("ERROR: rmod_install() failed");
349 mateuszvis 334
      return(1);
335
    }
369 mateuszvis 336
/*    printf("rmod installed at seg 0x%04X\r\n", rmod_seg); */
349 mateuszvis 337
  } else {
369 mateuszvis 338
/*    printf("rmod found at seg 0x%04x\r\n", rmod_seg); */
349 mateuszvis 339
  }
340
 
350 mateuszvis 341
  rmod_envseg = MK_FP(rmod_seg, RMOD_OFFSET_ENVSEG);
353 mateuszvis 342
  lastexitcode = MK_FP(rmod_seg, RMOD_OFFSET_LEXITCODE);
350 mateuszvis 343
 
367 mateuszvis 344
  /* make COMPSEC point to myself */
345
  set_comspec_to_self(*rmod_envseg);
346
 
369 mateuszvis 347
/*  {
350 mateuszvis 348
    unsigned short envsiz;
349
    unsigned short far *sizptr = MK_FP(*rmod_envseg - 1, 3);
350
    envsiz = *sizptr;
351
    envsiz *= 16;
352
    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 353
  }*/
349 mateuszvis 354
 
443 mateuszvis 355
  do {
350 mateuszvis 356
    char far *cmdline = MK_FP(rmod_seg, RMOD_OFFSET_INPBUFF + 2);
405 mateuszvis 357
    unsigned char far *echostatus = MK_FP(rmod_seg, RMOD_OFFSET_ECHOFLAG);
349 mateuszvis 358
 
437 mateuszvis 359
    /* (re)load translation strings if needed */
360
    nls_langreload(BUFFER, *rmod_envseg);
361
 
443 mateuszvis 362
    /* skip user input if I have a command to exec (/C or /K) */
363
    if (cfg.execcmd != NULL) {
364
      cmdline = cfg.execcmd;
365
      cfg.execcmd = NULL;
366
      goto EXEC_CMDLINE;
367
    }
368
 
369
    if (*echostatus != 0) outputnl(""); /* terminate the previous command with a CR/LF */
370
 
405 mateuszvis 371
    SKIP_NEWLINE:
372
 
373
    /* print shell prompt (only if ECHO is enabled) */
374
    if (*echostatus != 0) {
372 mateuszvis 375
      char *promptptr = BUFFER;
370 mateuszvis 376
      buildprompt(promptptr, *rmod_envseg);
354 mateuszvis 377
      _asm {
364 mateuszvis 378
        push ax
354 mateuszvis 379
        push dx
380
        mov ah, 0x09
381
        mov dx, promptptr
382
        int 0x21
383
        pop dx
364 mateuszvis 384
        pop ax
354 mateuszvis 385
      }
386
    }
349 mateuszvis 387
 
405 mateuszvis 388
    /* revert input history terminator to \r */
389
    if (cmdline[-1] != 0) {
390
      cmdline[(unsigned short)(cmdline[-1])] = '\r';
391
    }
392
 
349 mateuszvis 393
    /* wait for user input */
394
    _asm {
364 mateuszvis 395
      push ax
396
      push bx
397
      push cx
398
      push dx
349 mateuszvis 399
      push ds
400
 
401
      /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
402
      mov ax, 0x4800
403
      int 0x2f
404
      mov bl, al /* save doskey status in BL */
405
 
406
      /* set up buffered input */
407
      mov ax, rmod_seg
408
      push ax
409
      pop ds
350 mateuszvis 410
      mov dx, RMOD_OFFSET_INPBUFF
349 mateuszvis 411
 
412
      /* execute either DOS input or DOSKEY */
413
      test bl, bl /* zf set if no DOSKEY present */
414
      jnz DOSKEY
415
 
416
      mov ah, 0x0a
417
      int 0x21
418
      jmp short DONE
419
 
420
      DOSKEY:
421
      mov ax, 0x4810
422
      int 0x2f
423
 
424
      DONE:
405 mateuszvis 425
      /* terminate command with a CR/LF */
426
      mov ah, 0x02 /* display character in dl */
427
      mov dl, 0x0d
428
      int 0x21
429
      mov dl, 0x0a
430
      int 0x21
431
 
349 mateuszvis 432
      pop ds
364 mateuszvis 433
      pop dx
434
      pop cx
435
      pop bx
436
      pop ax
349 mateuszvis 437
    }
438
 
405 mateuszvis 439
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
440
    if (cmdline[-1] == 0) goto SKIP_NEWLINE;
349 mateuszvis 441
 
364 mateuszvis 442
    /* replace \r by a zero terminator */
443
    cmdline[(unsigned char)(cmdline[-1])] = 0;
349 mateuszvis 444
 
443 mateuszvis 445
    /* I jump here when I need to exec an initial command (/C or /K) */
446
    EXEC_CMDLINE:
447
 
364 mateuszvis 448
    /* move pointer forward to skip over any leading spaces */
449
    while (*cmdline == ' ') cmdline++;
349 mateuszvis 450
 
367 mateuszvis 451
    /* update rmod's ptr to COMPSPEC so it is always up to date */
452
    rmod_updatecomspecptr(rmod_seg, *rmod_envseg);
453
 
402 mateuszvis 454
    /* handle redirections (if any) */
455
    if (redir_parsecmd(cmdline, BUFFER) != 0) {
456
      outputnl("");
457
      continue;
458
    }
459
 
364 mateuszvis 460
    /* try matching (and executing) an internal command */
443 mateuszvis 461
    if (cmd_process(rmod_seg, *rmod_envseg, cmdline, BUFFER) >= -1) {
462
      /* internal command executed */
463
      redir_revert(); /* revert stdout (in case it was redirected) */
464
      continue;
353 mateuszvis 465
    }
352 mateuszvis 466
 
364 mateuszvis 467
    /* if here, then this was not an internal command */
468
    run_as_external(cmdline);
349 mateuszvis 469
 
402 mateuszvis 470
    /* revert stdout (in case it was redirected) */
471
    redir_revert();
472
 
349 mateuszvis 473
    /* execvp() replaces the current process by the new one
474
    if I am still alive then external command failed to execute */
369 mateuszvis 475
    outputnl("Bad command or file name");
349 mateuszvis 476
 
443 mateuszvis 477
  } while ((cfg.flags & FLAG_EXEC_AND_QUIT) == 0);
349 mateuszvis 478
 
479
  return(0);
480
}