Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
349 mateuszvis 1
/*
2
 * SvarCOM is a command-line interpreter.
3
 *
4
 * a little memory area is allocated as high as possible. it contains:
5
 *  - a signature (like XMS drivers do)
6
 *  - a routine for exec'ing programs
7
 *  - a "last command" buffer for input history
8
 *
9
 * when svarcom starts, it tries locating the routine in memory.
10
 *
11
 * if found:
12
 *   waits for user input and processes it. if execing something is required, set the "next exec" field in routine's memory and quit.
13
 *
14
 * if not found:
15
 *   installs it by creating a new PSP, set int 22 vector to the routine, set my "parent PSP" to the routine
16
 *   and quit.
17
 *
18
 *
19
 *
20
 * good lecture about PSP and allocating memory
21
 * https://retrocomputing.stackexchange.com/questions/20001/how-much-of-the-program-segment-prefix-area-can-be-reused-by-programs-with-impun/20006#20006
22
 *
23
 * PSP structure
24
 * http://www.piclist.com/techref/dos/psps.htm
25
 *
26
 *
27
 *
28
 * === MCB ===
29
 *
30
 * each time that DOS allocates memory, it prefixes the allocated memory with
31
 * a 16-bytes structure called a "Memory Control Block" (MCB). This control
32
 * block has the following structure:
33
 *
34
 * Offset  Size     Description
35
 *   00h   byte     'M' =  non-last member of the MCB chain
36
 *                  'Z' = indicates last entry in MCB chain
37
 *                  other values cause "Memory Allocation Failure" on exit
38
 *   01h   word     PSP segment address of the owner (Process Id)
39
 *                  possible values:
40
 *                    0 = free
41
 *                    8 = Allocated by DOS before first user pgm loaded
42
 *                    other = Process Id/PSP segment address of owner
43
 *   03h  word      number of paragraphs related to this MCB (excluding MCB)
44
 *   05h  11 bytes  reserved
45
 *   10h  ...       start of actual allocated memory block
46
 */
47
 
48
#include <i86.h>
49
#include <dos.h>
50
#include <stdio.h>
350 mateuszvis 51
#include <stdlib.h>
349 mateuszvis 52
#include <string.h>
53
 
54
#include <process.h>
55
 
352 mateuszvis 56
#include "cmd.h"
366 mateuszvis 57
#include "env.h"
352 mateuszvis 58
#include "helpers.h"
351 mateuszvis 59
#include "rmodinit.h"
349 mateuszvis 60
 
61
struct config {
62
  int locate;
63
  int install;
350 mateuszvis 64
  int envsiz;
349 mateuszvis 65
} cfg;
66
 
67
 
68
static void parse_argv(struct config *cfg, int argc, char **argv) {
69
  int i;
70
  memset(cfg, 0, sizeof(*cfg));
350 mateuszvis 71
 
349 mateuszvis 72
  for (i = 1; i < argc; i++) {
73
    if (strcmp(argv[i], "/locate") == 0) {
74
      cfg->locate = 1;
75
    }
350 mateuszvis 76
    if (strstartswith(argv[i], "/e:") == 0) {
77
      cfg->envsiz = atoi(argv[i]+3);
78
      if (cfg->envsiz < 64) cfg->envsiz = 0;
79
    }
349 mateuszvis 80
  }
81
}
82
 
83
 
354 mateuszvis 84
static void buildprompt(char *s, const char *fmt) {
85
  for (; *fmt != 0; fmt++) {
86
    if (*fmt != '$') {
87
      *s = *fmt;
88
      s++;
89
      continue;
90
    }
91
    /* escape code ($P, etc) */
92
    fmt++;
93
    switch (*fmt) {
94
      case 'Q':  /* $Q = = (equal sign) */
95
      case 'q':
96
        *s = '=';
97
        s++;
98
        break;
99
      case '$':  /* $$ = $ (dollar sign) */
100
        *s = '$';
101
        s++;
102
        break;
103
      case 'T':  /* $t = current time */
104
      case 't':
105
        s += sprintf(s, "00:00"); /* TODO */
106
        break;
107
      case 'D':  /* $D = current date */
108
      case 'd':
109
        s += sprintf(s, "1985-07-29"); /* TODO */
110
        break;
111
      case 'P':  /* $P = current drive and path */
112
      case 'p':
113
        _asm {
114
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
115
          int 0x21
116
          mov bx, s
117
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
118
        }
119
        *s += 'A';
120
        s++;
121
        *s = ':';
122
        s++;
123
        *s = '\\';
124
        s++;
125
        _asm {
126
          mov ah, 0x47    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */
127
          xor dl,dl       /* DL = drive number (00h = default, 01h = A:, etc) */
128
          mov si, s       /* DS:SI -> 64-byte buffer for ASCIZ pathname */
129
          int 0x21
130
        }
131
        while (*s != 0) s++; /* move ptr forward to end of pathname */
132
        break;
133
      case 'V':  /* $V = DOS version number */
134
      case 'v':
135
        s += sprintf(s, "VER"); /* TODO */
136
        break;
137
      case 'N':  /* $N = current drive */
138
      case 'n':
139
        _asm {
140
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
141
          int 0x21
142
          mov bx, s
143
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
144
        }
145
        *s += 'A';
146
        s++;
147
        break;
148
      case 'G':  /* $G = > (greater-than sign) */
149
      case 'g':
150
        *s = '>';
151
        s++;
152
        break;
153
      case 'L':  /* $L = < (less-than sign) */
154
      case 'l':
155
        *s = '<';
156
        s++;
157
        break;
158
      case 'B':  /* $B = | (pipe) */
159
      case 'b':
160
        *s = '|';
161
        s++;
162
        break;
163
      case 'H':  /* $H = backspace (erases previous character) */
164
      case 'h':
165
        *s = '\b';
166
        s++;
167
        break;
168
      case 'E':  /* $E = Escape code (ASCII 27) */
169
      case 'e':
170
        *s = 27;
171
        s++;
172
        break;
173
      case '_':  /* $_ = CR+LF */
174
        *s = '\r';
175
        s++;
176
        *s = '\n';
177
        s++;
178
        break;
179
    }
180
  }
181
  *s = '$';
182
}
349 mateuszvis 183
 
184
 
364 mateuszvis 185
static void run_as_external(const char far *cmdline) {
186
  char buff[256];
187
  char const *argvlist[256];
188
  int i, n;
189
  /* copy buffer to a near var (incl. trailing CR), insert a space before
190
     every slash to make sure arguments are well separated */
191
  n = 0;
192
  i = 0;
193
  for (;;) {
194
    if (cmdline[i] == '/') buff[n++] = ' ';
195
    buff[n++] = cmdline[i++];
196
    if (buff[n] == 0) break;
197
  }
198
 
199
  cmd_explode(buff, cmdline, argvlist);
200
 
201
  /* for (i = 0; argvlist[i] != NULL; i++) printf("arg #%d = '%s'\r\n", i, argvlist[i]); */
202
 
203
  /* must be an external command then. this call should never return, unless
204
   * the other program failed to be executed. */
205
  execvp(argvlist[0], argvlist);
206
}
207
 
208
 
367 mateuszvis 209
static void set_comspec_to_self(unsigned short envseg) {
210
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
211
  char far *myenv = MK_FP(*psp_envseg, 0);
212
  unsigned short varcount;
213
  char buff[256] = "COMSPEC=";
214
  char *buffptr = buff + 8;
215
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
216
  while (*myenv != 0) {
217
    /* consume a NULL-terminated string */
218
    while (*myenv != 0) myenv++;
219
    /* move to next string */
220
    myenv++;
221
  }
222
  /* get next word, if 1 then EXEPATH follows */
223
  myenv++;
224
  varcount = *myenv;
225
  myenv++;
226
  varcount |= (*myenv << 8);
227
  myenv++;
228
  if (varcount != 1) return; /* NO EXEPATH FOUND */
229
  while (*myenv != 0) {
230
    *buffptr = *myenv;
231
    buffptr++;
232
    myenv++;
233
  }
234
  *buffptr = 0;
235
  /* printf("EXEPATH: '%s'\r\n", buff); */
236
  env_setvar(envseg, buff);
237
}
238
 
239
 
349 mateuszvis 240
int main(int argc, char **argv) {
241
  struct config cfg;
350 mateuszvis 242
  unsigned short rmod_seg;
243
  unsigned short far *rmod_envseg;
353 mateuszvis 244
  unsigned short far *lastexitcode;
349 mateuszvis 245
 
246
  parse_argv(&cfg, argc, argv);
247
 
350 mateuszvis 248
  rmod_seg = rmod_find();
349 mateuszvis 249
  if (rmod_seg == 0xffff) {
350 mateuszvis 250
    rmod_seg = rmod_install(cfg.envsiz);
349 mateuszvis 251
    if (rmod_seg == 0xffff) {
350 mateuszvis 252
      puts("ERROR: rmod_install() failed");
349 mateuszvis 253
      return(1);
254
    }
367 mateuszvis 255
    printf("rmod installed at seg 0x%04X\r\n", rmod_seg);
349 mateuszvis 256
  } else {
257
    printf("rmod found at seg 0x%04x\r\n", rmod_seg);
258
  }
259
 
350 mateuszvis 260
  rmod_envseg = MK_FP(rmod_seg, RMOD_OFFSET_ENVSEG);
353 mateuszvis 261
  lastexitcode = MK_FP(rmod_seg, RMOD_OFFSET_LEXITCODE);
350 mateuszvis 262
 
367 mateuszvis 263
  /* make COMPSEC point to myself */
264
  set_comspec_to_self(*rmod_envseg);
265
 
350 mateuszvis 266
  {
267
    unsigned short envsiz;
268
    unsigned short far *sizptr = MK_FP(*rmod_envseg - 1, 3);
269
    envsiz = *sizptr;
270
    envsiz *= 16;
271
    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);
349 mateuszvis 272
  }
273
 
274
  for (;;) {
350 mateuszvis 275
    char far *cmdline = MK_FP(rmod_seg, RMOD_OFFSET_INPBUFF + 2);
349 mateuszvis 276
 
364 mateuszvis 277
    /* revert input history terminator to \r */
278
    if (cmdline[-1] != 0) {
279
      cmdline[(unsigned short)(cmdline[-1])] = '\r';
280
    }
281
 
354 mateuszvis 282
    {
283
      /* print shell prompt */
364 mateuszvis 284
      char buff[256];
354 mateuszvis 285
      char *promptptr = buff;
286
      buildprompt(promptptr, "$p$g"); /* TODO: prompt should be configurable via environment */
287
      _asm {
364 mateuszvis 288
        push ax
354 mateuszvis 289
        push dx
290
        mov ah, 0x09
291
        mov dx, promptptr
292
        int 0x21
293
        pop dx
364 mateuszvis 294
        pop ax
354 mateuszvis 295
      }
296
    }
349 mateuszvis 297
 
298
    /* wait for user input */
299
    _asm {
364 mateuszvis 300
      push ax
301
      push bx
302
      push cx
303
      push dx
349 mateuszvis 304
      push ds
305
 
306
      /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
307
      mov ax, 0x4800
308
      int 0x2f
309
      mov bl, al /* save doskey status in BL */
310
 
311
      /* set up buffered input */
312
      mov ax, rmod_seg
313
      push ax
314
      pop ds
350 mateuszvis 315
      mov dx, RMOD_OFFSET_INPBUFF
349 mateuszvis 316
 
317
      /* execute either DOS input or DOSKEY */
318
      test bl, bl /* zf set if no DOSKEY present */
319
      jnz DOSKEY
320
 
321
      mov ah, 0x0a
322
      int 0x21
323
      jmp short DONE
324
 
325
      DOSKEY:
326
      mov ax, 0x4810
327
      int 0x2f
328
 
329
      DONE:
330
      pop ds
364 mateuszvis 331
      pop dx
332
      pop cx
333
      pop bx
334
      pop ax
349 mateuszvis 335
    }
336
    printf("\r\n");
337
 
338
    /* if nothing entered, loop again */
339
    if (cmdline[-1] == 0) continue;
340
 
364 mateuszvis 341
    /* replace \r by a zero terminator */
342
    cmdline[(unsigned char)(cmdline[-1])] = 0;
349 mateuszvis 343
 
364 mateuszvis 344
    /* move pointer forward to skip over any leading spaces */
345
    while (*cmdline == ' ') cmdline++;
349 mateuszvis 346
 
367 mateuszvis 347
    /* update rmod's ptr to COMPSPEC so it is always up to date */
348
    rmod_updatecomspecptr(rmod_seg, *rmod_envseg);
349
 
364 mateuszvis 350
    /* try matching (and executing) an internal command */
353 mateuszvis 351
    {
364 mateuszvis 352
      int ecode = cmd_process(*rmod_envseg, cmdline);
353 mateuszvis 353
      if (ecode >= 0) *lastexitcode = ecode;
364 mateuszvis 354
      if (ecode >= -1) continue; /* internal command executed */
353 mateuszvis 355
    }
352 mateuszvis 356
 
364 mateuszvis 357
    /* if here, then this was not an internal command */
358
    run_as_external(cmdline);
349 mateuszvis 359
 
360
    /* execvp() replaces the current process by the new one
361
    if I am still alive then external command failed to execute */
362
    puts("Bad command or file name");
363
 
364
  }
365
 
366
  return(0);
367
}