Subversion Repositories SvarDOS

Rev

Rev 421 | Rev 437 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 421 Rev 426
1
/* This file is part of the SvarCOM project and is published under the terms
1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021 Mateusz Viste
4
 * Copyright (C) 2021 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included in
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
14
 * all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
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
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
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
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 */
23
 */
24
 
24
 
25
/*
25
/*
26
 * SvarCOM is a command-line interpreter.
26
 * SvarCOM is a command-line interpreter.
27
 *
27
 *
28
 * a little memory area is allocated as high as possible. it contains:
28
 * a little memory area is allocated as high as possible. it contains:
29
 *  - a signature (like XMS drivers do)
29
 *  - a signature (like XMS drivers do)
30
 *  - a routine for exec'ing programs
30
 *  - a routine for exec'ing programs
31
 *  - a "last command" buffer for input history
31
 *  - a "last command" buffer for input history
32
 *
32
 *
33
 * when svarcom starts, it tries locating the routine in memory.
33
 * when svarcom starts, it tries locating the routine in memory.
34
 *
34
 *
35
 * if found:
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.
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
 *
37
 *
38
 * if not found:
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
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.
40
 *   and quit.
41
 *
41
 *
42
 * PSP structure
42
 * PSP structure
43
 * http://www.piclist.com/techref/dos/psps.htm
43
 * http://www.piclist.com/techref/dos/psps.htm
44
 *
44
 *
45
 *
45
 *
46
 *
46
 *
47
 * === MCB ===
47
 * === MCB ===
48
 *
48
 *
49
 * each time that DOS allocates memory, it prefixes the allocated memory with
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
50
 * a 16-bytes structure called a "Memory Control Block" (MCB). This control
51
 * block has the following structure:
51
 * block has the following structure:
52
 *
52
 *
53
 * Offset  Size     Description
53
 * Offset  Size     Description
54
 *   00h   byte     'M' =  non-last member of the MCB chain
54
 *   00h   byte     'M' =  non-last member of the MCB chain
55
 *                  'Z' = indicates last entry in MCB chain
55
 *                  'Z' = indicates last entry in MCB chain
56
 *                  other values cause "Memory Allocation Failure" on exit
56
 *                  other values cause "Memory Allocation Failure" on exit
57
 *   01h   word     PSP segment address of the owner (Process Id)
57
 *   01h   word     PSP segment address of the owner (Process Id)
58
 *                  possible values:
58
 *                  possible values:
59
 *                    0 = free
59
 *                    0 = free
60
 *                    8 = Allocated by DOS before first user pgm loaded
60
 *                    8 = Allocated by DOS before first user pgm loaded
61
 *                    other = Process Id/PSP segment address of owner
61
 *                    other = Process Id/PSP segment address of owner
62
 *   03h  word      number of paragraphs related to this MCB (excluding MCB)
62
 *   03h  word      number of paragraphs related to this MCB (excluding MCB)
63
 *   05h  11 bytes  reserved
63
 *   05h  11 bytes  reserved
64
 *   10h  ...       start of actual allocated memory block
64
 *   10h  ...       start of actual allocated memory block
65
 */
65
 */
66
 
66
 
67
#include <i86.h>
67
#include <i86.h>
68
#include <dos.h>
68
#include <dos.h>
69
#include <stdio.h>
69
#include <stdio.h>
70
#include <stdlib.h>
70
#include <stdlib.h>
71
#include <string.h>
71
#include <string.h>
72
 
72
 
73
#include <process.h>
73
#include <process.h>
74
 
74
 
75
#include "cmd.h"
75
#include "cmd.h"
76
#include "env.h"
76
#include "env.h"
77
#include "helpers.h"
77
#include "helpers.h"
78
#include "redir.h"
78
#include "redir.h"
79
#include "rmodinit.h"
79
#include "rmodinit.h"
80
 
80
 
81
struct config {
81
struct config {
82
  int locate;
82
  int locate;
83
  int install;
83
  int install;
84
  unsigned short envsiz;
84
  unsigned short envsiz;
85
} cfg;
85
} cfg;
86
 
86
 
87
 
87
 
88
static void parse_argv(struct config *cfg, int argc, char **argv) {
88
static void parse_argv(struct config *cfg, int argc, char **argv) {
89
  int i;
89
  int i;
90
  memset(cfg, 0, sizeof(*cfg));
90
  memset(cfg, 0, sizeof(*cfg));
91
 
91
 
92
  for (i = 1; i < argc; i++) {
92
  for (i = 1; i < argc; i++) {
93
    if (strcmp(argv[i], "/locate") == 0) {
93
    if (strcmp(argv[i], "/locate") == 0) {
94
      cfg->locate = 1;
94
      cfg->locate = 1;
95
    }
95
    }
96
    if (strstartswith(argv[i], "/e:") == 0) {
96
    if (strstartswith(argv[i], "/e:") == 0) {
97
      if ((atouns(&(cfg->envsiz), argv[i] + 3) != 0) || (cfg->envsiz < 64)) {
97
      if ((atous(&(cfg->envsiz), argv[i] + 3) != 0) || (cfg->envsiz < 64)) {
98
        cfg->envsiz = 0;
98
        cfg->envsiz = 0;
99
      }
99
      }
100
    }
100
    }
101
  }
101
  }
102
}
102
}
103
 
103
 
104
 
104
 
105
static void buildprompt(char *s, unsigned short envseg) {
105
static void buildprompt(char *s, unsigned short envseg) {
106
  /* locate the prompt variable or use the default pattern */
106
  /* locate the prompt variable or use the default pattern */
107
  const char far *fmt = env_lookup(envseg, "PROMPT");
107
  const char far *fmt = env_lookup(envseg, "PROMPT");
108
  while ((fmt != NULL) && (*fmt != 0)) {
108
  while ((fmt != NULL) && (*fmt != 0)) {
109
    fmt++;
109
    fmt++;
110
    if (fmt[-1] == '=') break;
110
    if (fmt[-1] == '=') break;
111
  }
111
  }
112
  if ((fmt == NULL) || (*fmt == 0)) fmt = "$p$g"; /* fallback to default if empty */
112
  if ((fmt == NULL) || (*fmt == 0)) fmt = "$p$g"; /* fallback to default if empty */
113
  /* build the prompt string based on pattern */
113
  /* build the prompt string based on pattern */
114
  for (; *fmt != 0; fmt++) {
114
  for (; *fmt != 0; fmt++) {
115
    if (*fmt != '$') {
115
    if (*fmt != '$') {
116
      *s = *fmt;
116
      *s = *fmt;
117
      s++;
117
      s++;
118
      continue;
118
      continue;
119
    }
119
    }
120
    /* escape code ($P, etc) */
120
    /* escape code ($P, etc) */
121
    fmt++;
121
    fmt++;
122
    switch (*fmt) {
122
    switch (*fmt) {
123
      case 'Q':  /* $Q = = (equal sign) */
123
      case 'Q':  /* $Q = = (equal sign) */
124
      case 'q':
124
      case 'q':
125
        *s = '=';
125
        *s = '=';
126
        s++;
126
        s++;
127
        break;
127
        break;
128
      case '$':  /* $$ = $ (dollar sign) */
128
      case '$':  /* $$ = $ (dollar sign) */
129
        *s = '$';
129
        *s = '$';
130
        s++;
130
        s++;
131
        break;
131
        break;
132
      case 'T':  /* $t = current time */
132
      case 'T':  /* $t = current time */
133
      case 't':
133
      case 't':
134
        s += sprintf(s, "00:00"); /* TODO */
134
        s += sprintf(s, "00:00"); /* TODO */
135
        break;
135
        break;
136
      case 'D':  /* $D = current date */
136
      case 'D':  /* $D = current date */
137
      case 'd':
137
      case 'd':
138
        s += sprintf(s, "1985-07-29"); /* TODO */
138
        s += sprintf(s, "1985-07-29"); /* TODO */
139
        break;
139
        break;
140
      case 'P':  /* $P = current drive and path */
140
      case 'P':  /* $P = current drive and path */
141
      case 'p':
141
      case 'p':
142
        _asm {
142
        _asm {
143
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
143
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
144
          int 0x21
144
          int 0x21
145
          mov bx, s
145
          mov bx, s
146
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
146
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
147
        }
147
        }
148
        *s += 'A';
148
        *s += 'A';
149
        s++;
149
        s++;
150
        *s = ':';
150
        *s = ':';
151
        s++;
151
        s++;
152
        *s = '\\';
152
        *s = '\\';
153
        s++;
153
        s++;
154
        _asm {
154
        _asm {
155
          mov ah, 0x47    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */
155
          mov ah, 0x47    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */
156
          xor dl,dl       /* DL = drive number (00h = default, 01h = A:, etc) */
156
          xor dl,dl       /* DL = drive number (00h = default, 01h = A:, etc) */
157
          mov si, s       /* DS:SI -> 64-byte buffer for ASCIZ pathname */
157
          mov si, s       /* DS:SI -> 64-byte buffer for ASCIZ pathname */
158
          int 0x21
158
          int 0x21
159
        }
159
        }
160
        while (*s != 0) s++; /* move ptr forward to end of pathname */
160
        while (*s != 0) s++; /* move ptr forward to end of pathname */
161
        break;
161
        break;
162
      case 'V':  /* $V = DOS version number */
162
      case 'V':  /* $V = DOS version number */
163
      case 'v':
163
      case 'v':
164
        s += sprintf(s, "VER"); /* TODO */
164
        s += sprintf(s, "VER"); /* TODO */
165
        break;
165
        break;
166
      case 'N':  /* $N = current drive */
166
      case 'N':  /* $N = current drive */
167
      case 'n':
167
      case 'n':
168
        _asm {
168
        _asm {
169
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
169
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
170
          int 0x21
170
          int 0x21
171
          mov bx, s
171
          mov bx, s
172
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
172
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
173
        }
173
        }
174
        *s += 'A';
174
        *s += 'A';
175
        s++;
175
        s++;
176
        break;
176
        break;
177
      case 'G':  /* $G = > (greater-than sign) */
177
      case 'G':  /* $G = > (greater-than sign) */
178
      case 'g':
178
      case 'g':
179
        *s = '>';
179
        *s = '>';
180
        s++;
180
        s++;
181
        break;
181
        break;
182
      case 'L':  /* $L = < (less-than sign) */
182
      case 'L':  /* $L = < (less-than sign) */
183
      case 'l':
183
      case 'l':
184
        *s = '<';
184
        *s = '<';
185
        s++;
185
        s++;
186
        break;
186
        break;
187
      case 'B':  /* $B = | (pipe) */
187
      case 'B':  /* $B = | (pipe) */
188
      case 'b':
188
      case 'b':
189
        *s = '|';
189
        *s = '|';
190
        s++;
190
        s++;
191
        break;
191
        break;
192
      case 'H':  /* $H = backspace (erases previous character) */
192
      case 'H':  /* $H = backspace (erases previous character) */
193
      case 'h':
193
      case 'h':
194
        *s = '\b';
194
        *s = '\b';
195
        s++;
195
        s++;
196
        break;
196
        break;
197
      case 'E':  /* $E = Escape code (ASCII 27) */
197
      case 'E':  /* $E = Escape code (ASCII 27) */
198
      case 'e':
198
      case 'e':
199
        *s = 27;
199
        *s = 27;
200
        s++;
200
        s++;
201
        break;
201
        break;
202
      case '_':  /* $_ = CR+LF */
202
      case '_':  /* $_ = CR+LF */
203
        *s = '\r';
203
        *s = '\r';
204
        s++;
204
        s++;
205
        *s = '\n';
205
        *s = '\n';
206
        s++;
206
        s++;
207
        break;
207
        break;
208
    }
208
    }
209
  }
209
  }
210
  *s = '$';
210
  *s = '$';
211
}
211
}
212
 
212
 
213
 
213
 
214
static void run_as_external(const char far *cmdline) {
214
static void run_as_external(const char far *cmdline) {
215
  char buff[256];
215
  char buff[256];
216
  char const *argvlist[256];
216
  char const *argvlist[256];
217
  int i, n;
217
  int i, n;
218
  /* copy buffer to a near var (incl. trailing CR), insert a space before
218
  /* copy buffer to a near var (incl. trailing CR), insert a space before
219
     every slash to make sure arguments are well separated */
219
     every slash to make sure arguments are well separated */
220
  n = 0;
220
  n = 0;
221
  i = 0;
221
  i = 0;
222
  for (;;) {
222
  for (;;) {
223
    if (cmdline[i] == '/') buff[n++] = ' ';
223
    if (cmdline[i] == '/') buff[n++] = ' ';
224
    buff[n++] = cmdline[i++];
224
    buff[n++] = cmdline[i++];
225
    if (buff[n] == 0) break;
225
    if (buff[n] == 0) break;
226
  }
226
  }
227
 
227
 
228
  cmd_explode(buff, cmdline, argvlist);
228
  cmd_explode(buff, cmdline, argvlist);
229
 
229
 
230
  /* for (i = 0; argvlist[i] != NULL; i++) printf("arg #%d = '%s'\r\n", i, argvlist[i]); */
230
  /* for (i = 0; argvlist[i] != NULL; i++) printf("arg #%d = '%s'\r\n", i, argvlist[i]); */
231
 
231
 
232
  /* must be an external command then. this call should never return, unless
232
  /* must be an external command then. this call should never return, unless
233
   * the other program failed to be executed. */
233
   * the other program failed to be executed. */
234
  execvp(argvlist[0], argvlist);
234
  execvp(argvlist[0], argvlist);
235
}
235
}
236
 
236
 
237
 
237
 
238
static void set_comspec_to_self(unsigned short envseg) {
238
static void set_comspec_to_self(unsigned short envseg) {
239
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
239
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
240
  char far *myenv = MK_FP(*psp_envseg, 0);
240
  char far *myenv = MK_FP(*psp_envseg, 0);
241
  unsigned short varcount;
241
  unsigned short varcount;
242
  char buff[256] = "COMSPEC=";
242
  char buff[256] = "COMSPEC=";
243
  char *buffptr = buff + 8;
243
  char *buffptr = buff + 8;
244
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
244
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
245
  while (*myenv != 0) {
245
  while (*myenv != 0) {
246
    /* consume a NULL-terminated string */
246
    /* consume a NULL-terminated string */
247
    while (*myenv != 0) myenv++;
247
    while (*myenv != 0) myenv++;
248
    /* move to next string */
248
    /* move to next string */
249
    myenv++;
249
    myenv++;
250
  }
250
  }
251
  /* get next word, if 1 then EXEPATH follows */
251
  /* get next word, if 1 then EXEPATH follows */
252
  myenv++;
252
  myenv++;
253
  varcount = *myenv;
253
  varcount = *myenv;
254
  myenv++;
254
  myenv++;
255
  varcount |= (*myenv << 8);
255
  varcount |= (*myenv << 8);
256
  myenv++;
256
  myenv++;
257
  if (varcount != 1) return; /* NO EXEPATH FOUND */
257
  if (varcount != 1) return; /* NO EXEPATH FOUND */
258
  while (*myenv != 0) {
258
  while (*myenv != 0) {
259
    *buffptr = *myenv;
259
    *buffptr = *myenv;
260
    buffptr++;
260
    buffptr++;
261
    myenv++;
261
    myenv++;
262
  }
262
  }
263
  *buffptr = 0;
263
  *buffptr = 0;
264
  /* printf("EXEPATH: '%s'\r\n", buff); */
264
  /* printf("EXEPATH: '%s'\r\n", buff); */
265
  env_setvar(envseg, buff);
265
  env_setvar(envseg, buff);
266
}
266
}
267
 
267
 
268
 
268
 
269
int main(int argc, char **argv) {
269
int main(int argc, char **argv) {
270
  static struct config cfg;
270
  static struct config cfg;
271
  static unsigned short rmod_seg;
271
  static unsigned short rmod_seg;
272
  static unsigned short far *rmod_envseg;
272
  static unsigned short far *rmod_envseg;
273
  static unsigned short far *lastexitcode;
273
  static unsigned short far *lastexitcode;
274
  static unsigned char BUFFER[4096];
274
  static unsigned char BUFFER[4096];
275
 
275
 
276
  parse_argv(&cfg, argc, argv);
276
  parse_argv(&cfg, argc, argv);
277
 
277
 
278
  rmod_seg = rmod_find();
278
  rmod_seg = rmod_find();
279
  if (rmod_seg == 0xffff) {
279
  if (rmod_seg == 0xffff) {
280
    rmod_seg = rmod_install(cfg.envsiz);
280
    rmod_seg = rmod_install(cfg.envsiz);
281
    if (rmod_seg == 0xffff) {
281
    if (rmod_seg == 0xffff) {
282
      outputnl("ERROR: rmod_install() failed");
282
      outputnl("ERROR: rmod_install() failed");
283
      return(1);
283
      return(1);
284
    }
284
    }
285
/*    printf("rmod installed at seg 0x%04X\r\n", rmod_seg); */
285
/*    printf("rmod installed at seg 0x%04X\r\n", rmod_seg); */
286
  } else {
286
  } else {
287
/*    printf("rmod found at seg 0x%04x\r\n", rmod_seg); */
287
/*    printf("rmod found at seg 0x%04x\r\n", rmod_seg); */
288
  }
288
  }
289
 
289
 
290
  rmod_envseg = MK_FP(rmod_seg, RMOD_OFFSET_ENVSEG);
290
  rmod_envseg = MK_FP(rmod_seg, RMOD_OFFSET_ENVSEG);
291
  lastexitcode = MK_FP(rmod_seg, RMOD_OFFSET_LEXITCODE);
291
  lastexitcode = MK_FP(rmod_seg, RMOD_OFFSET_LEXITCODE);
292
 
292
 
293
  /* make COMPSEC point to myself */
293
  /* make COMPSEC point to myself */
294
  set_comspec_to_self(*rmod_envseg);
294
  set_comspec_to_self(*rmod_envseg);
295
 
295
 
296
/*  {
296
/*  {
297
    unsigned short envsiz;
297
    unsigned short envsiz;
298
    unsigned short far *sizptr = MK_FP(*rmod_envseg - 1, 3);
298
    unsigned short far *sizptr = MK_FP(*rmod_envseg - 1, 3);
299
    envsiz = *sizptr;
299
    envsiz = *sizptr;
300
    envsiz *= 16;
300
    envsiz *= 16;
301
    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);
301
    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);
302
  }*/
302
  }*/
303
 
303
 
304
  for (;;) {
304
  for (;;) {
305
    char far *cmdline = MK_FP(rmod_seg, RMOD_OFFSET_INPBUFF + 2);
305
    char far *cmdline = MK_FP(rmod_seg, RMOD_OFFSET_INPBUFF + 2);
306
    unsigned char far *echostatus = MK_FP(rmod_seg, RMOD_OFFSET_ECHOFLAG);
306
    unsigned char far *echostatus = MK_FP(rmod_seg, RMOD_OFFSET_ECHOFLAG);
307
 
307
 
308
    if (*echostatus != 0) outputnl(""); /* terminate the previous command with a CR/LF */
308
    if (*echostatus != 0) outputnl(""); /* terminate the previous command with a CR/LF */
309
 
309
 
310
    SKIP_NEWLINE:
310
    SKIP_NEWLINE:
311
 
311
 
312
    /* print shell prompt (only if ECHO is enabled) */
312
    /* print shell prompt (only if ECHO is enabled) */
313
    if (*echostatus != 0) {
313
    if (*echostatus != 0) {
314
      char *promptptr = BUFFER;
314
      char *promptptr = BUFFER;
315
      buildprompt(promptptr, *rmod_envseg);
315
      buildprompt(promptptr, *rmod_envseg);
316
      _asm {
316
      _asm {
317
        push ax
317
        push ax
318
        push dx
318
        push dx
319
        mov ah, 0x09
319
        mov ah, 0x09
320
        mov dx, promptptr
320
        mov dx, promptptr
321
        int 0x21
321
        int 0x21
322
        pop dx
322
        pop dx
323
        pop ax
323
        pop ax
324
      }
324
      }
325
    }
325
    }
326
 
326
 
327
    /* revert input history terminator to \r */
327
    /* revert input history terminator to \r */
328
    if (cmdline[-1] != 0) {
328
    if (cmdline[-1] != 0) {
329
      cmdline[(unsigned short)(cmdline[-1])] = '\r';
329
      cmdline[(unsigned short)(cmdline[-1])] = '\r';
330
    }
330
    }
331
 
331
 
332
    /* wait for user input */
332
    /* wait for user input */
333
    _asm {
333
    _asm {
334
      push ax
334
      push ax
335
      push bx
335
      push bx
336
      push cx
336
      push cx
337
      push dx
337
      push dx
338
      push ds
338
      push ds
339
 
339
 
340
      /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
340
      /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
341
      mov ax, 0x4800
341
      mov ax, 0x4800
342
      int 0x2f
342
      int 0x2f
343
      mov bl, al /* save doskey status in BL */
343
      mov bl, al /* save doskey status in BL */
344
 
344
 
345
      /* set up buffered input */
345
      /* set up buffered input */
346
      mov ax, rmod_seg
346
      mov ax, rmod_seg
347
      push ax
347
      push ax
348
      pop ds
348
      pop ds
349
      mov dx, RMOD_OFFSET_INPBUFF
349
      mov dx, RMOD_OFFSET_INPBUFF
350
 
350
 
351
      /* execute either DOS input or DOSKEY */
351
      /* execute either DOS input or DOSKEY */
352
      test bl, bl /* zf set if no DOSKEY present */
352
      test bl, bl /* zf set if no DOSKEY present */
353
      jnz DOSKEY
353
      jnz DOSKEY
354
 
354
 
355
      mov ah, 0x0a
355
      mov ah, 0x0a
356
      int 0x21
356
      int 0x21
357
      jmp short DONE
357
      jmp short DONE
358
 
358
 
359
      DOSKEY:
359
      DOSKEY:
360
      mov ax, 0x4810
360
      mov ax, 0x4810
361
      int 0x2f
361
      int 0x2f
362
 
362
 
363
      DONE:
363
      DONE:
364
      /* terminate command with a CR/LF */
364
      /* terminate command with a CR/LF */
365
      mov ah, 0x02 /* display character in dl */
365
      mov ah, 0x02 /* display character in dl */
366
      mov dl, 0x0d
366
      mov dl, 0x0d
367
      int 0x21
367
      int 0x21
368
      mov dl, 0x0a
368
      mov dl, 0x0a
369
      int 0x21
369
      int 0x21
370
 
370
 
371
      pop ds
371
      pop ds
372
      pop dx
372
      pop dx
373
      pop cx
373
      pop cx
374
      pop bx
374
      pop bx
375
      pop ax
375
      pop ax
376
    }
376
    }
377
 
377
 
378
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
378
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
379
    if (cmdline[-1] == 0) goto SKIP_NEWLINE;
379
    if (cmdline[-1] == 0) goto SKIP_NEWLINE;
380
 
380
 
381
    /* replace \r by a zero terminator */
381
    /* replace \r by a zero terminator */
382
    cmdline[(unsigned char)(cmdline[-1])] = 0;
382
    cmdline[(unsigned char)(cmdline[-1])] = 0;
383
 
383
 
384
    /* move pointer forward to skip over any leading spaces */
384
    /* move pointer forward to skip over any leading spaces */
385
    while (*cmdline == ' ') cmdline++;
385
    while (*cmdline == ' ') cmdline++;
386
 
386
 
387
    /* update rmod's ptr to COMPSPEC so it is always up to date */
387
    /* update rmod's ptr to COMPSPEC so it is always up to date */
388
    rmod_updatecomspecptr(rmod_seg, *rmod_envseg);
388
    rmod_updatecomspecptr(rmod_seg, *rmod_envseg);
389
 
389
 
390
    /* handle redirections (if any) */
390
    /* handle redirections (if any) */
391
    if (redir_parsecmd(cmdline, BUFFER) != 0) {
391
    if (redir_parsecmd(cmdline, BUFFER) != 0) {
392
      outputnl("");
392
      outputnl("");
393
      continue;
393
      continue;
394
    }
394
    }
395
 
395
 
396
    /* try matching (and executing) an internal command */
396
    /* try matching (and executing) an internal command */
397
    {
397
    {
398
      int ecode = cmd_process(rmod_seg, *rmod_envseg, cmdline, BUFFER);
398
      int ecode = cmd_process(rmod_seg, *rmod_envseg, cmdline, BUFFER);
399
      if (ecode >= 0) *lastexitcode = ecode;
399
      if (ecode >= 0) *lastexitcode = ecode;
400
      if (ecode >= -1) { /* internal command executed */
400
      if (ecode >= -1) { /* internal command executed */
401
        redir_revert(); /* revert stdout (in case it was redirected) */
401
        redir_revert(); /* revert stdout (in case it was redirected) */
402
        continue;
402
        continue;
403
      }
403
      }
404
    }
404
    }
405
 
405
 
406
    /* if here, then this was not an internal command */
406
    /* if here, then this was not an internal command */
407
    run_as_external(cmdline);
407
    run_as_external(cmdline);
408
 
408
 
409
    /* revert stdout (in case it was redirected) */
409
    /* revert stdout (in case it was redirected) */
410
    redir_revert();
410
    redir_revert();
411
 
411
 
412
    /* execvp() replaces the current process by the new one
412
    /* execvp() replaces the current process by the new one
413
    if I am still alive then external command failed to execute */
413
    if I am still alive then external command failed to execute */
414
    outputnl("Bad command or file name");
414
    outputnl("Bad command or file name");
415
 
415
 
416
  }
416
  }
417
 
417
 
418
  return(0);
418
  return(0);
419
}
419
}
420
 
420