Subversion Repositories SvarDOS

Rev

Rev 364 | Rev 367 | 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
 
349 mateuszvis 209
int main(int argc, char **argv) {
210
  struct config cfg;
350 mateuszvis 211
  unsigned short rmod_seg;
212
  unsigned short far *rmod_envseg;
353 mateuszvis 213
  unsigned short far *lastexitcode;
349 mateuszvis 214
 
215
  parse_argv(&cfg, argc, argv);
216
 
350 mateuszvis 217
  rmod_seg = rmod_find();
349 mateuszvis 218
  if (rmod_seg == 0xffff) {
350 mateuszvis 219
    rmod_seg = rmod_install(cfg.envsiz);
349 mateuszvis 220
    if (rmod_seg == 0xffff) {
350 mateuszvis 221
      puts("ERROR: rmod_install() failed");
349 mateuszvis 222
      return(1);
223
    } else {
224
      printf("rmod installed at seg 0x%04X\r\n", rmod_seg);
225
    }
226
  } else {
227
    printf("rmod found at seg 0x%04x\r\n", rmod_seg);
228
  }
229
 
350 mateuszvis 230
  rmod_envseg = MK_FP(rmod_seg, RMOD_OFFSET_ENVSEG);
353 mateuszvis 231
  lastexitcode = MK_FP(rmod_seg, RMOD_OFFSET_LEXITCODE);
350 mateuszvis 232
 
233
  {
234
    unsigned short envsiz;
235
    unsigned short far *sizptr = MK_FP(*rmod_envseg - 1, 3);
236
    envsiz = *sizptr;
237
    envsiz *= 16;
238
    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 239
  }
240
 
241
  for (;;) {
350 mateuszvis 242
    char far *cmdline = MK_FP(rmod_seg, RMOD_OFFSET_INPBUFF + 2);
349 mateuszvis 243
 
364 mateuszvis 244
    /* revert input history terminator to \r */
245
    if (cmdline[-1] != 0) {
246
      cmdline[(unsigned short)(cmdline[-1])] = '\r';
247
    }
248
 
354 mateuszvis 249
    {
250
      /* print shell prompt */
364 mateuszvis 251
      char buff[256];
354 mateuszvis 252
      char *promptptr = buff;
253
      buildprompt(promptptr, "$p$g"); /* TODO: prompt should be configurable via environment */
254
      _asm {
364 mateuszvis 255
        push ax
354 mateuszvis 256
        push dx
257
        mov ah, 0x09
258
        mov dx, promptptr
259
        int 0x21
260
        pop dx
364 mateuszvis 261
        pop ax
354 mateuszvis 262
      }
263
    }
349 mateuszvis 264
 
265
    /* wait for user input */
266
    _asm {
364 mateuszvis 267
      push ax
268
      push bx
269
      push cx
270
      push dx
349 mateuszvis 271
      push ds
272
 
273
      /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
274
      mov ax, 0x4800
275
      int 0x2f
276
      mov bl, al /* save doskey status in BL */
277
 
278
      /* set up buffered input */
279
      mov ax, rmod_seg
280
      push ax
281
      pop ds
350 mateuszvis 282
      mov dx, RMOD_OFFSET_INPBUFF
349 mateuszvis 283
 
284
      /* execute either DOS input or DOSKEY */
285
      test bl, bl /* zf set if no DOSKEY present */
286
      jnz DOSKEY
287
 
288
      mov ah, 0x0a
289
      int 0x21
290
      jmp short DONE
291
 
292
      DOSKEY:
293
      mov ax, 0x4810
294
      int 0x2f
295
 
296
      DONE:
297
      pop ds
364 mateuszvis 298
      pop dx
299
      pop cx
300
      pop bx
301
      pop ax
349 mateuszvis 302
    }
303
    printf("\r\n");
304
 
305
    /* if nothing entered, loop again */
306
    if (cmdline[-1] == 0) continue;
307
 
364 mateuszvis 308
    /* replace \r by a zero terminator */
309
    cmdline[(unsigned char)(cmdline[-1])] = 0;
349 mateuszvis 310
 
364 mateuszvis 311
    /* move pointer forward to skip over any leading spaces */
312
    while (*cmdline == ' ') cmdline++;
349 mateuszvis 313
 
364 mateuszvis 314
    /* try matching (and executing) an internal command */
353 mateuszvis 315
    {
364 mateuszvis 316
      int ecode = cmd_process(*rmod_envseg, cmdline);
353 mateuszvis 317
      if (ecode >= 0) *lastexitcode = ecode;
366 mateuszvis 318
      /* update rmod's ptr to COMPSPEC, in case it changed */
319
      {
320
        unsigned short far *comspecptr = MK_FP(rmod_seg, RMOD_OFFSET_COMSPECPTR);
321
        char far *comspecfp = env_lookup(*rmod_envseg, "COMSPEC");
322
        if (comspecfp != NULL) {
323
          *comspecptr = FP_OFF(comspecfp) + 8; /* +8 to skip the "COMSPEC=" prefix */
324
        } else {
325
          *comspecptr = 0;
326
        }
327
      }
364 mateuszvis 328
      if (ecode >= -1) continue; /* internal command executed */
353 mateuszvis 329
    }
352 mateuszvis 330
 
364 mateuszvis 331
    /* if here, then this was not an internal command */
332
    run_as_external(cmdline);
349 mateuszvis 333
 
334
    /* execvp() replaces the current process by the new one
335
    if I am still alive then external command failed to execute */
336
    puts("Bad command or file name");
337
 
338
  }
339
 
340
  return(0);
341
}