Subversion Repositories SvarDOS

Rev

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

Rev 572 Rev 576
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-2022 Mateusz Viste
4
 * Copyright (C) 2021-2022 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
#include <i86.h>
25
#include <i86.h>
26
#include <dos.h>
26
#include <dos.h>
27
#include <stdio.h>
27
#include <stdio.h>
28
#include <stdlib.h>
28
#include <stdlib.h>
29
#include <string.h>
29
#include <string.h>
30
 
30
 
31
#include <process.h>
31
#include <process.h>
32
 
32
 
33
#include "cmd.h"
33
#include "cmd.h"
34
#include "env.h"
34
#include "env.h"
35
#include "helpers.h"
35
#include "helpers.h"
36
#include "redir.h"
36
#include "redir.h"
37
#include "rmodinit.h"
37
#include "rmodinit.h"
38
#include "sayonara.h"
38
#include "sayonara.h"
39
 
39
 
40
#include "rmodcore.h" /* rmod binary inside a BUFFER array */
40
#include "rmodcore.h" /* rmod binary inside a BUFFER array */
41
 
41
 
42
/* this version byte is used to tag RMOD so I can easily make sure that
42
/* this version byte is used to tag RMOD so I can easily make sure that
43
 * the RMOD struct I find in memory is one that I know. Should the version
43
 * the RMOD struct I find in memory is one that I know. Should the version
44
 * mismatch, then it would likely mean that SvarCOM has been upgraded and
44
 * mismatch, then it would likely mean that SvarCOM has been upgraded and
45
 * RMOD should not be accessed as its structure might no longer be in sync
45
 * RMOD should not be accessed as its structure might no longer be in sync
46
 * with what I think it is.
46
 * with what I think it is.
47
 *          *** INCREMENT THIS AT EACH NEW SVARCOM RELEASE! ***          */
47
 *          *** INCREMENT THIS AT EACH NEW SVARCOM RELEASE! ***          */
48
#define BYTE_VERSION 3
48
#define BYTE_VERSION 3
49
 
49
 
50
 
50
 
51
struct config {
51
struct config {
52
  unsigned char flags; /* command.com flags, as defined in rmodinit.h */
52
  unsigned char flags; /* command.com flags, as defined in rmodinit.h */
53
  char *execcmd;
53
  char *execcmd;
54
  unsigned short envsiz;
54
  unsigned short envsiz;
55
};
55
};
56
 
56
 
57
/* max length of the cmdline storage (bytes) - includes also max length of
57
/* max length of the cmdline storage (bytes) - includes also max length of
58
 * line loaded from a BAT file (no more than 255 bytes!) */
58
 * line loaded from a BAT file (no more than 255 bytes!) */
59
#define CMDLINE_MAXLEN 255
59
#define CMDLINE_MAXLEN 255
60
 
60
 
61
 
61
 
62
/* sets guard values at a few places in memory for later detection of
62
/* sets guard values at a few places in memory for later detection of
63
 * overflows via memguard_check() */
63
 * overflows via memguard_check() */
64
static void memguard_set(char *cmdlinebuf) {
64
static void memguard_set(char *cmdlinebuf) {
65
  BUFFER[sizeof(BUFFER) - 1] = 0xC7;
65
  BUFFER[sizeof(BUFFER) - 1] = 0xC7;
66
  cmdlinebuf[CMDLINE_MAXLEN] = 0xC7;
66
  cmdlinebuf[CMDLINE_MAXLEN] = 0xC7;
67
}
67
}
68
 
68
 
69
 
69
 
70
/* checks for valguards at specific memory locations, returns 0 on success */
70
/* checks for valguards at specific memory locations, returns 0 on success */
71
static int memguard_check(unsigned short rmodseg, char *cmdlinebuf) {
71
static int memguard_check(unsigned short rmodseg, char *cmdlinebuf) {
72
  /* check RMOD signature (would be overwritten in case of stack overflow */
72
  /* check RMOD signature (would be overwritten in case of stack overflow */
73
  static char msg[] = "!! MEMORY CORRUPTION ## DETECTED !!";
73
  static char msg[] = "!! MEMORY CORRUPTION ## DETECTED !!";
74
  unsigned short far *rmodsig = MK_FP(rmodseg, 0x100 + 6);
74
  unsigned short far *rmodsig = MK_FP(rmodseg, 0x100 + 6);
75
  unsigned char far *rmod = MK_FP(rmodseg, 0);
75
  unsigned char far *rmod = MK_FP(rmodseg, 0);
76
 
76
 
77
  if (*rmodsig != 0x2019) {
77
  if (*rmodsig != 0x2019) {
78
    msg[22] = '1';
78
    msg[22] = '1';
79
    goto FAIL;
79
    goto FAIL;
80
  }
80
  }
81
 
81
 
82
  /* check last BUFFER byte */
82
  /* check last BUFFER byte */
83
  if (BUFFER[sizeof(BUFFER) - 1] != 0xC7) {
83
  if (BUFFER[sizeof(BUFFER) - 1] != 0xC7) {
84
    msg[22] = '2';
84
    msg[22] = '2';
85
    goto FAIL;
85
    goto FAIL;
86
  }
86
  }
87
 
87
 
88
  /* check last cmdlinebuf byte */
88
  /* check last cmdlinebuf byte */
89
  if (cmdlinebuf[CMDLINE_MAXLEN] != 0xC7) {
89
  if (cmdlinebuf[CMDLINE_MAXLEN] != 0xC7) {
90
    msg[22] = '3';
90
    msg[22] = '3';
91
    goto FAIL;
91
    goto FAIL;
92
  }
92
  }
93
 
93
 
94
  /* check rmod exec buf */
94
  /* check rmod exec buf */
95
  if (rmod[RMOD_OFFSET_EXECPROG + 127] != 0) {
95
  if (rmod[RMOD_OFFSET_EXECPROG + 127] != 0) {
96
    msg[22] = '4';
96
    msg[22] = '4';
97
    goto FAIL;
97
    goto FAIL;
98
  }
98
  }
99
 
99
 
100
  /* check rmod exec stdin buf */
100
  /* check rmod exec stdin buf */
101
  if (rmod[RMOD_OFFSET_STDINFILE + 127] != 0) {
101
  if (rmod[RMOD_OFFSET_STDINFILE + 127] != 0) {
102
    msg[22] = '5';
102
    msg[22] = '5';
103
    goto FAIL;
103
    goto FAIL;
104
  }
104
  }
105
 
105
 
106
  /* check rmod exec stdout buf */
106
  /* check rmod exec stdout buf */
107
  if (rmod[RMOD_OFFSET_STDOUTFILE + 127] != 0) {
107
  if (rmod[RMOD_OFFSET_STDOUTFILE + 127] != 0) {
108
    msg[22] = '6';
108
    msg[22] = '6';
109
    goto FAIL;
109
    goto FAIL;
110
  }
110
  }
111
 
111
 
112
  /* else all good */
112
  /* else all good */
113
  return(0);
113
  return(0);
114
 
114
 
115
  /* error handling */
115
  /* error handling */
116
  FAIL:
116
  FAIL:
117
  outputnl(msg);
117
  outputnl(msg);
118
  return(1);
118
  return(1);
119
}
119
}
120
 
120
 
121
 
121
 
122
/* parses command line the hard way (directly from PSP) */
122
/* parses command line the hard way (directly from PSP) */
123
static void parse_argv(struct config *cfg) {
123
static void parse_argv(struct config *cfg) {
124
  const unsigned char *cmdlinelen = (void *)0x80;
124
  const unsigned char *cmdlinelen = (void *)0x80;
125
  char *cmdline = (void *)0x81;
125
  char *cmdline = (void *)0x81;
126
 
126
 
127
  memset(cfg, 0, sizeof(*cfg));
127
  memset(cfg, 0, sizeof(*cfg));
128
 
128
 
129
  /* set a NULL terminator on cmdline */
129
  /* set a NULL terminator on cmdline */
130
  cmdline[*cmdlinelen] = 0;
130
  cmdline[*cmdlinelen] = 0;
131
 
131
 
132
  while (*cmdline != 0) {
132
  while (*cmdline != 0) {
133
 
133
 
134
    /* skip over any leading spaces */
134
    /* skip over any leading spaces */
135
    if (*cmdline == ' ') {
135
    if (*cmdline == ' ') {
136
      cmdline++;
136
      cmdline++;
137
      continue;
137
      continue;
138
    }
138
    }
139
 
139
 
140
    if (*cmdline != '/') {
140
    if (*cmdline != '/') {
141
      output("Invalid parameter: ");
141
      output("Invalid parameter: ");
142
      outputnl(cmdline);
142
      outputnl(cmdline);
143
      goto SKIP_TO_NEXT_ARG;
143
      goto SKIP_TO_NEXT_ARG;
144
    }
144
    }
145
 
145
 
146
    /* got a slash */
146
    /* got a slash */
147
    cmdline++;  /* skip the slash */
147
    cmdline++;  /* skip the slash */
148
    switch (*cmdline) {
148
    switch (*cmdline) {
149
      case 'c': /* /C = execute command and quit */
149
      case 'c': /* /C = execute command and quit */
150
      case 'C':
150
      case 'C':
151
        cfg->flags |= FLAG_EXEC_AND_QUIT;
151
        cfg->flags |= FLAG_EXEC_AND_QUIT;
152
        /* FALLTHRU */
152
        /* FALLTHRU */
153
      case 'k': /* /K = execute command and keep running */
153
      case 'k': /* /K = execute command and keep running */
154
      case 'K':
154
      case 'K':
155
        cfg->execcmd = cmdline + 1;
155
        cfg->execcmd = cmdline + 1;
156
        return;
156
        return;
157
 
157
 
158
      case 'd': /* /D = skip autoexec.bat processing */
158
      case 'd': /* /D = skip autoexec.bat processing */
159
      case 'D':
159
      case 'D':
160
        cfg->flags |= FLAG_SKIP_AUTOEXEC;
160
        cfg->flags |= FLAG_SKIP_AUTOEXEC;
161
        break;
161
        break;
162
 
162
 
163
      case 'e': /* preset the initial size of the environment block */
163
      case 'e': /* preset the initial size of the environment block */
164
      case 'E':
164
      case 'E':
165
        cmdline++;
165
        cmdline++;
166
        if (*cmdline == ':') cmdline++; /* could be /E:size */
166
        if (*cmdline == ':') cmdline++; /* could be /E:size */
167
        atous(&(cfg->envsiz), cmdline);
167
        atous(&(cfg->envsiz), cmdline);
168
        if (cfg->envsiz < 64) cfg->envsiz = 0;
168
        if (cfg->envsiz < 64) cfg->envsiz = 0;
169
        break;
169
        break;
170
 
170
 
171
      case 'p': /* permanent shell (can't exit + run autoexec.bat) */
171
      case 'p': /* permanent shell (can't exit + run autoexec.bat) */
172
      case 'P':
172
      case 'P':
173
        cfg->flags |= FLAG_PERMANENT;
173
        cfg->flags |= FLAG_PERMANENT;
174
        break;
174
        break;
175
 
175
 
176
      case '?':
176
      case '?':
177
        outputnl("Starts the SvarCOM command interpreter");
177
        outputnl("Starts the SvarCOM command interpreter");
178
        outputnl("");
178
        outputnl("");
179
        outputnl("COMMAND /E:nnn [/[C|K] [/P] [/D] command]");
179
        outputnl("COMMAND /E:nnn [/[C|K] [/P] [/D] command]");
180
        outputnl("");
180
        outputnl("");
181
        outputnl("/D      Skip AUTOEXEC.BAT processing (makes sense only with /P)");
181
        outputnl("/D      Skip AUTOEXEC.BAT processing (makes sense only with /P)");
182
        outputnl("/E:nnn  Sets the environment size to nnn bytes");
182
        outputnl("/E:nnn  Sets the environment size to nnn bytes");
183
        outputnl("/P      Makes the new command interpreter permanent and run AUTOEXEC.BAT");
183
        outputnl("/P      Makes the new command interpreter permanent and run AUTOEXEC.BAT");
184
        outputnl("/C      Executes the specified command and returns");
184
        outputnl("/C      Executes the specified command and returns");
185
        outputnl("/K      Executes the specified command and continues running");
185
        outputnl("/K      Executes the specified command and continues running");
186
        exit(1);
186
        exit(1);
187
        break;
187
        break;
188
 
188
 
189
      default:
189
      default:
190
        output("Invalid switch: /");
190
        output("Invalid switch: /");
191
        outputnl(cmdline);
191
        outputnl(cmdline);
192
        break;
192
        break;
193
    }
193
    }
194
 
194
 
195
    /* move to next argument or quit processing if end of cmdline */
195
    /* move to next argument or quit processing if end of cmdline */
196
    SKIP_TO_NEXT_ARG:
196
    SKIP_TO_NEXT_ARG:
197
    while ((*cmdline != 0) && (*cmdline != ' ') && (*cmdline != '/')) cmdline++;
197
    while ((*cmdline != 0) && (*cmdline != ' ') && (*cmdline != '/')) cmdline++;
198
  }
198
  }
199
}
199
}
200
 
200
 
201
 
201
 
202
/* builds the prompt string and displays it. buff is filled with a zero-terminated copy of the prompt. */
202
/* builds the prompt string and displays it. buff is filled with a zero-terminated copy of the prompt. */
203
static void build_and_display_prompt(char *buff, unsigned short envseg) {
203
static void build_and_display_prompt(char *buff, unsigned short envseg) {
204
  char *s = buff;
204
  char *s = buff;
205
  /* locate the prompt variable or use the default pattern */
205
  /* locate the prompt variable or use the default pattern */
206
  const char far *fmt = env_lookup_val(envseg, "PROMPT");
206
  const char far *fmt = env_lookup_val(envseg, "PROMPT");
207
  if ((fmt == NULL) || (*fmt == 0)) fmt = "$p$g"; /* fallback to default if empty */
207
  if ((fmt == NULL) || (*fmt == 0)) fmt = "$p$g"; /* fallback to default if empty */
208
  /* build the prompt string based on pattern */
208
  /* build the prompt string based on pattern */
209
  for (; *fmt != 0; fmt++) {
209
  for (; *fmt != 0; fmt++) {
210
    if (*fmt != '$') {
210
    if (*fmt != '$') {
211
      *s = *fmt;
211
      *s = *fmt;
212
      s++;
212
      s++;
213
      continue;
213
      continue;
214
    }
214
    }
215
    /* escape code ($P, etc) */
215
    /* escape code ($P, etc) */
216
    fmt++;
216
    fmt++;
217
    switch (*fmt) {
217
    switch (*fmt) {
218
      case 'Q':  /* $Q = = (equal sign) */
218
      case 'Q':  /* $Q = = (equal sign) */
219
      case 'q':
219
      case 'q':
220
        *s = '=';
220
        *s = '=';
221
        s++;
221
        s++;
222
        break;
222
        break;
223
      case '$':  /* $$ = $ (dollar sign) */
223
      case '$':  /* $$ = $ (dollar sign) */
224
        *s = '$';
224
        *s = '$';
225
        s++;
225
        s++;
226
        break;
226
        break;
227
      case 'T':  /* $t = current time */
227
      case 'T':  /* $t = current time */
228
      case 't':
228
      case 't':
229
        s += sprintf(s, "00:00"); /* TODO */
229
        s += sprintf(s, "00:00"); /* TODO */
230
        break;
230
        break;
231
      case 'D':  /* $D = current date */
231
      case 'D':  /* $D = current date */
232
      case 'd':
232
      case 'd':
233
        s += sprintf(s, "1985-07-29"); /* TODO */
233
        s += sprintf(s, "1985-07-29"); /* TODO */
234
        break;
234
        break;
235
      case 'P':  /* $P = current drive and path */
235
      case 'P':  /* $P = current drive and path */
236
      case 'p':
236
      case 'p':
237
        _asm {
237
        _asm {
238
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
238
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
239
          int 0x21
239
          int 0x21
240
          mov bx, s
240
          mov bx, s
241
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
241
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
242
        }
242
        }
243
        *s += 'A';
243
        *s += 'A';
244
        s++;
244
        s++;
245
        *s = ':';
245
        *s = ':';
246
        s++;
246
        s++;
247
        *s = '\\';
247
        *s = '\\';
248
        s++;
248
        s++;
249
        _asm {
249
        _asm {
250
          mov ah, 0x47    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */
250
          mov ah, 0x47    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */
251
          xor dl,dl       /* DL = drive number (00h = default, 01h = A:, etc) */
251
          xor dl,dl       /* DL = drive number (00h = default, 01h = A:, etc) */
252
          mov si, s       /* DS:SI -> 64-byte buffer for ASCIZ pathname */
252
          mov si, s       /* DS:SI -> 64-byte buffer for ASCIZ pathname */
253
          int 0x21
253
          int 0x21
254
          jc DONE         /* leave path empty on error */
254
          jc DONE         /* leave path empty on error */
255
          /* move s ptr forward to end (0-termintor) of pathname */
255
          /* move s ptr forward to end (0-termintor) of pathname */
256
          NEXTBYTE:
256
          NEXTBYTE:
257
          mov si, s
257
          mov si, s
258
          cmp byte ptr [si], 0
258
          cmp byte ptr [si], 0
259
          je DONE
259
          je DONE
260
          inc s
260
          inc s
261
          jmp NEXTBYTE
261
          jmp NEXTBYTE
262
          DONE:
262
          DONE:
263
        }
263
        }
264
        break;
264
        break;
265
      case 'V':  /* $V = DOS version number */
265
      case 'V':  /* $V = DOS version number */
266
      case 'v':
266
      case 'v':
267
        s += sprintf(s, "VER"); /* TODO */
267
        s += sprintf(s, "VER"); /* TODO */
268
        break;
268
        break;
269
      case 'N':  /* $N = current drive */
269
      case 'N':  /* $N = current drive */
270
      case 'n':
270
      case 'n':
271
        _asm {
271
        _asm {
272
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
272
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
273
          int 0x21
273
          int 0x21
274
          mov bx, s
274
          mov bx, s
275
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
275
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
276
        }
276
        }
277
        *s += 'A';
277
        *s += 'A';
278
        s++;
278
        s++;
279
        break;
279
        break;
280
      case 'G':  /* $G = > (greater-than sign) */
280
      case 'G':  /* $G = > (greater-than sign) */
281
      case 'g':
281
      case 'g':
282
        *s = '>';
282
        *s = '>';
283
        s++;
283
        s++;
284
        break;
284
        break;
285
      case 'L':  /* $L = < (less-than sign) */
285
      case 'L':  /* $L = < (less-than sign) */
286
      case 'l':
286
      case 'l':
287
        *s = '<';
287
        *s = '<';
288
        s++;
288
        s++;
289
        break;
289
        break;
290
      case 'B':  /* $B = | (pipe) */
290
      case 'B':  /* $B = | (pipe) */
291
      case 'b':
291
      case 'b':
292
        *s = '|';
292
        *s = '|';
293
        s++;
293
        s++;
294
        break;
294
        break;
295
      case 'H':  /* $H = backspace (erases previous character) */
295
      case 'H':  /* $H = backspace (erases previous character) */
296
      case 'h':
296
      case 'h':
297
        *s = '\b';
297
        *s = '\b';
298
        s++;
298
        s++;
299
        break;
299
        break;
300
      case 'E':  /* $E = Escape code (ASCII 27) */
300
      case 'E':  /* $E = Escape code (ASCII 27) */
301
      case 'e':
301
      case 'e':
302
        *s = 27;
302
        *s = 27;
303
        s++;
303
        s++;
304
        break;
304
        break;
305
      case '_':  /* $_ = CR+LF */
305
      case '_':  /* $_ = CR+LF */
306
        *s = '\r';
306
        *s = '\r';
307
        s++;
307
        s++;
308
        *s = '\n';
308
        *s = '\n';
309
        s++;
309
        s++;
310
        break;
310
        break;
311
    }
311
    }
312
  }
312
  }
313
  *s = 0;
313
  *s = 0;
314
  output(buff);
314
  output(buff);
315
}
315
}
316
 
316
 
317
 
317
 
318
static void run_as_external(char *buff, const char *cmdline, unsigned short envseg, struct rmod_props far *rmod, struct redir_data *redir) {
318
static void run_as_external(char *buff, const char *cmdline, unsigned short envseg, struct rmod_props far *rmod, struct redir_data *redir, unsigned char delete_stdin_file) {
319
  char *cmdfile = buff + 512;
319
  char *cmdfile = buff + 512;
320
  const char far *pathptr;
320
  const char far *pathptr;
321
  int lookup;
321
  int lookup;
322
  unsigned short i;
322
  unsigned short i;
323
  const char *ext;
323
  const char *ext;
324
  char *cmd = buff + 1024;
324
  char *cmd = buff + 1024;
325
  const char *cmdtail;
325
  const char *cmdtail;
326
  char far *rmod_execprog = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPROG);
326
  char far *rmod_execprog = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPROG);
327
  char far *rmod_cmdtail = MK_FP(rmod->rmodseg, 0x81);
327
  char far *rmod_cmdtail = MK_FP(rmod->rmodseg, 0x81);
328
  _Packed struct {
328
  _Packed struct {
329
    unsigned short envseg;
329
    unsigned short envseg;
330
    unsigned long cmdtail;
330
    unsigned long cmdtail;
331
    unsigned long fcb1;
331
    unsigned long fcb1;
332
    unsigned long fcb2;
332
    unsigned long fcb2;
333
  } far *ExecParam = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPARAM);
333
  } far *ExecParam = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPARAM);
334
 
334
 
335
  /* find cmd and cmdtail */
335
  /* find cmd and cmdtail */
336
  i = 0;
336
  i = 0;
337
  cmdtail = cmdline;
337
  cmdtail = cmdline;
338
  while (*cmdtail == ' ') cmdtail++; /* skip any leading spaces */
338
  while (*cmdtail == ' ') cmdtail++; /* skip any leading spaces */
339
  while ((*cmdtail != ' ') && (*cmdtail != '/') && (*cmdtail != '+') && (*cmdtail != 0)) {
339
  while ((*cmdtail != ' ') && (*cmdtail != '/') && (*cmdtail != '+') && (*cmdtail != 0)) {
340
    cmd[i++] = *cmdtail;
340
    cmd[i++] = *cmdtail;
341
    cmdtail++;
341
    cmdtail++;
342
  }
342
  }
343
  cmd[i] = 0;
343
  cmd[i] = 0;
344
 
344
 
345
  /* is this a command in curdir? */
345
  /* is this a command in curdir? */
346
  lookup = lookup_cmd(cmdfile, cmd, NULL, &ext);
346
  lookup = lookup_cmd(cmdfile, cmd, NULL, &ext);
347
  if (lookup == 0) {
347
  if (lookup == 0) {
348
    /* printf("FOUND LOCAL EXEC FILE: '%s'\r\n", cmdfile); */
348
    /* printf("FOUND LOCAL EXEC FILE: '%s'\r\n", cmdfile); */
349
    goto RUNCMDFILE;
349
    goto RUNCMDFILE;
350
  } else if (lookup == -2) {
350
  } else if (lookup == -2) {
351
    /* puts("NOT FOUND"); */
351
    /* puts("NOT FOUND"); */
352
    return;
352
    return;
353
  }
353
  }
354
 
354
 
355
  /* try matching something in PATH */
355
  /* try matching something in PATH */
356
  pathptr = env_lookup_val(envseg, "PATH");
356
  pathptr = env_lookup_val(envseg, "PATH");
357
 
357
 
358
  /* try each path in %PATH% */
358
  /* try each path in %PATH% */
359
  while (pathptr) {
359
  while (pathptr) {
360
    for (i = 0;; i++) {
360
    for (i = 0;; i++) {
361
      buff[i] = *pathptr;
361
      buff[i] = *pathptr;
362
      if ((buff[i] == 0) || (buff[i] == ';')) break;
362
      if ((buff[i] == 0) || (buff[i] == ';')) break;
363
      pathptr++;
363
      pathptr++;
364
    }
364
    }
365
    buff[i] = 0;
365
    buff[i] = 0;
366
    lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
366
    lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
367
    if (lookup == 0) goto RUNCMDFILE;
367
    if (lookup == 0) goto RUNCMDFILE;
368
    if (lookup == -2) return;
368
    if (lookup == -2) return;
369
    if (*pathptr == ';') {
369
    if (*pathptr == ';') {
370
      pathptr++;
370
      pathptr++;
371
    } else {
371
    } else {
372
      break;
372
      break;
373
    }
373
    }
374
  }
374
  }
375
 
375
 
376
  /* last chance: is it an executable link? (trim extension from cmd first) */
376
  /* last chance: is it an executable link? (trim extension from cmd first) */
377
  for (i = 0; (cmd[i] != 0) && (cmd[i] != '.') && (i < 9); i++) buff[128 + i] = cmd[i];
377
  for (i = 0; (cmd[i] != 0) && (cmd[i] != '.') && (i < 9); i++) buff[128 + i] = cmd[i];
378
  buff[128 + i] = 0;
378
  buff[128 + i] = 0;
379
  if ((i < 9) && (link_computefname(buff, buff + 128, envseg) == 0)) {
379
  if ((i < 9) && (link_computefname(buff, buff + 128, envseg) == 0)) {
380
    /* try opening the link file (if it exists) and read it into buff */
380
    /* try opening the link file (if it exists) and read it into buff */
381
    i = 0;
381
    i = 0;
382
    _asm {
382
    _asm {
383
      push ax
383
      push ax
384
      push bx
384
      push bx
385
      push cx
385
      push cx
386
      push dx
386
      push dx
387
 
387
 
388
      mov ax, 0x3d00  /* DOS 2+ - OPEN EXISTING FILE, READ-ONLY */
388
      mov ax, 0x3d00  /* DOS 2+ - OPEN EXISTING FILE, READ-ONLY */
389
      mov dx, buff    /* file name */
389
      mov dx, buff    /* file name */
390
      int 0x21
390
      int 0x21
391
      jc ERR_FOPEN
391
      jc ERR_FOPEN
392
      /* file handle in AX, read from file now */
392
      /* file handle in AX, read from file now */
393
      mov bx, ax      /* file handle */
393
      mov bx, ax      /* file handle */
394
      mov ah, 0x3f    /* Read from file via handle bx */
394
      mov ah, 0x3f    /* Read from file via handle bx */
395
      mov cx, 128     /* up to 128 bytes */
395
      mov cx, 128     /* up to 128 bytes */
396
      /* mov dx, buff */ /* dest buffer (already set) */
396
      /* mov dx, buff */ /* dest buffer (already set) */
397
      int 0x21        /* read up to 256 bytes from file and write to buff */
397
      int 0x21        /* read up to 256 bytes from file and write to buff */
398
      jc ERR_READ
398
      jc ERR_READ
399
      mov i, ax
399
      mov i, ax
400
      ERR_READ:
400
      ERR_READ:
401
      mov ah, 0x3e    /* close file handle in BX */
401
      mov ah, 0x3e    /* close file handle in BX */
402
      int 0x21
402
      int 0x21
403
      ERR_FOPEN:
403
      ERR_FOPEN:
404
 
404
 
405
      pop dx
405
      pop dx
406
      pop cx
406
      pop cx
407
      pop bx
407
      pop bx
408
      pop ax
408
      pop ax
409
    }
409
    }
410
 
410
 
411
    /* did I read anything? */
411
    /* did I read anything? */
412
    if (i != 0) {
412
    if (i != 0) {
413
      buff[i] = 0;
413
      buff[i] = 0;
414
      /* trim buff at first \n or \r, just in case someone fiddled with the
414
      /* trim buff at first \n or \r, just in case someone fiddled with the
415
       * link file using a text editor */
415
       * link file using a text editor */
416
      for (i = 0; (buff[i] != 0) && (buff[i] != '\r') && (buff[i] != '\n'); i++);
416
      for (i = 0; (buff[i] != 0) && (buff[i] != '\r') && (buff[i] != '\n'); i++);
417
      buff[i] = 0;
417
      buff[i] = 0;
418
      /* lookup check */
418
      /* lookup check */
419
      if (buff[0] != 0) {
419
      if (buff[0] != 0) {
420
        lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
420
        lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
421
        if (lookup == 0) goto RUNCMDFILE;
421
        if (lookup == 0) goto RUNCMDFILE;
422
      }
422
      }
423
    }
423
    }
424
  }
424
  }
425
 
425
 
426
  /* all failed (ie. executable file not found) */
426
  /* all failed (ie. executable file not found) */
427
  return;
427
  return;
428
 
428
 
429
  RUNCMDFILE:
429
  RUNCMDFILE:
430
 
430
 
431
  /* special handling of batch files */
431
  /* special handling of batch files */
432
  if ((ext != NULL) && (imatch(ext, "bat"))) {
432
  if ((ext != NULL) && (imatch(ext, "bat"))) {
433
    /* copy truename of the bat file to rmod buff */
433
    /* copy truename of the bat file to rmod buff */
434
    _fstrcpy(rmod->batfile, cmdfile);
434
    _fstrcpy(rmod->batfile, cmdfile);
435
 
435
 
436
    /* explode args of the bat file and store them in rmod buff */
436
    /* explode args of the bat file and store them in rmod buff */
437
    cmd_explode(buff, cmdline, NULL);
437
    cmd_explode(buff, cmdline, NULL);
438
    _fmemcpy(rmod->batargv, buff, sizeof(rmod->batargv));
438
    _fmemcpy(rmod->batargv, buff, sizeof(rmod->batargv));
439
 
439
 
440
    /* reset the 'next line to execute' counter */
440
    /* reset the 'next line to execute' counter */
441
    rmod->batnextline = 0;
441
    rmod->batnextline = 0;
442
    /* remember the echo flag (in case bat file disables echo) */
442
    /* remember the echo flag (in case bat file disables echo) */
443
    rmod->flags &= ~FLAG_ECHO_BEFORE_BAT;
443
    rmod->flags &= ~FLAG_ECHO_BEFORE_BAT;
444
    if (rmod->flags & FLAG_ECHOFLAG) rmod->flags |= FLAG_ECHO_BEFORE_BAT;
444
    if (rmod->flags & FLAG_ECHOFLAG) rmod->flags |= FLAG_ECHO_BEFORE_BAT;
445
    return;
445
    return;
446
  }
446
  }
447
 
447
 
448
  /* copy full filename to execute, along with redirected files (if any) */
448
  /* copy full filename to execute, along with redirected files (if any) */
449
  _fstrcpy(rmod_execprog, cmdfile);
449
  _fstrcpy(rmod_execprog, cmdfile);
450
 
450
 
451
  /* copy stdin file if a redirection is needed */
451
  /* copy stdin file if a redirection is needed */
452
  if (redir->stdinfile) {
452
  if (redir->stdinfile) {
453
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDINFILE);
453
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDINFILE);
-
 
454
    char far *delstdin = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDIN_DEL);
454
    _fstrcpy(farptr, redir->stdinfile);
455
    _fstrcpy(farptr, redir->stdinfile);
-
 
456
    if (delete_stdin_file) {
-
 
457
      *delstdin = redir->stdinfile[0];
-
 
458
    } else {
-
 
459
      *delstdin = 0;
-
 
460
    }
455
  }
461
  }
456
 
462
 
457
  /* same for stdout file */
463
  /* same for stdout file */
458
  if (redir->stdoutfile) {
464
  if (redir->stdoutfile) {
459
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTFILE);
465
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTFILE);
460
    unsigned short far *farptr16 = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTAPP);
466
    unsigned short far *farptr16 = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTAPP);
461
    _fstrcpy(farptr, redir->stdoutfile);
467
    _fstrcpy(farptr, redir->stdoutfile);
462
    /* openflag */
468
    /* openflag */
463
    *farptr16 = redir->stdout_openflag;
469
    *farptr16 = redir->stdout_openflag;
464
  }
470
  }
465
 
471
 
466
  /* copy cmdtail to rmod's PSP and compute its len */
472
  /* copy cmdtail to rmod's PSP and compute its len */
467
  for (i = 0; cmdtail[i] != 0; i++) rmod_cmdtail[i] = cmdtail[i];
473
  for (i = 0; cmdtail[i] != 0; i++) rmod_cmdtail[i] = cmdtail[i];
468
  rmod_cmdtail[i] = '\r';
474
  rmod_cmdtail[i] = '\r';
469
  rmod_cmdtail[-1] = i;
475
  rmod_cmdtail[-1] = i;
470
 
476
 
471
  /* set up rmod to execute the command */
477
  /* set up rmod to execute the command */
472
 
478
 
473
  ExecParam->envseg = envseg;
479
  ExecParam->envseg = envseg;
474
  ExecParam->cmdtail = (unsigned long)MK_FP(rmod->rmodseg, 0x80); /* farptr, must be in PSP format (lenbyte args \r) */
480
  ExecParam->cmdtail = (unsigned long)MK_FP(rmod->rmodseg, 0x80); /* farptr, must be in PSP format (lenbyte args \r) */
475
  ExecParam->fcb1 = 0; /* TODO farptr */
481
  ExecParam->fcb1 = 0; /* TODO farptr */
476
  ExecParam->fcb2 = 0; /* TODO farptr */
482
  ExecParam->fcb2 = 0; /* TODO farptr */
477
  exit(0); /* let rmod do the job now */
483
  exit(0); /* let rmod do the job now */
478
}
484
}
479
 
485
 
480
 
486
 
481
static void set_comspec_to_self(unsigned short envseg) {
487
static void set_comspec_to_self(unsigned short envseg) {
482
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
488
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
483
  char far *myenv = MK_FP(*psp_envseg, 0);
489
  char far *myenv = MK_FP(*psp_envseg, 0);
484
  unsigned short varcount;
490
  unsigned short varcount;
485
  char buff[256] = "COMSPEC=";
491
  char buff[256] = "COMSPEC=";
486
  char *buffptr = buff + 8;
492
  char *buffptr = buff + 8;
487
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
493
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
488
  while (*myenv != 0) {
494
  while (*myenv != 0) {
489
    /* consume a NULL-terminated string */
495
    /* consume a NULL-terminated string */
490
    while (*myenv != 0) myenv++;
496
    while (*myenv != 0) myenv++;
491
    /* move to next string */
497
    /* move to next string */
492
    myenv++;
498
    myenv++;
493
  }
499
  }
494
  /* get next word, if 1 then EXEPATH follows */
500
  /* get next word, if 1 then EXEPATH follows */
495
  myenv++;
501
  myenv++;
496
  varcount = *myenv;
502
  varcount = *myenv;
497
  myenv++;
503
  myenv++;
498
  varcount |= (*myenv << 8);
504
  varcount |= (*myenv << 8);
499
  myenv++;
505
  myenv++;
500
  if (varcount != 1) return; /* NO EXEPATH FOUND */
506
  if (varcount != 1) return; /* NO EXEPATH FOUND */
501
  while (*myenv != 0) {
507
  while (*myenv != 0) {
502
    *buffptr = *myenv;
508
    *buffptr = *myenv;
503
    buffptr++;
509
    buffptr++;
504
    myenv++;
510
    myenv++;
505
  }
511
  }
506
  *buffptr = 0;
512
  *buffptr = 0;
507
  /* printf("EXEPATH: '%s'\r\n", buff); */
513
  /* printf("EXEPATH: '%s'\r\n", buff); */
508
  env_setvar(envseg, buff);
514
  env_setvar(envseg, buff);
509
}
515
}
510
 
516
 
511
 
517
 
512
/* wait for user input */
518
/* wait for user input */
513
static void cmdline_getinput(unsigned short inpseg, unsigned short inpoff) {
519
static void cmdline_getinput(unsigned short inpseg, unsigned short inpoff) {
514
  _asm {
520
  _asm {
515
    push ax
521
    push ax
516
    push bx
522
    push bx
517
    push cx
523
    push cx
518
    push dx
524
    push dx
519
    push ds
525
    push ds
520
 
526
 
521
    /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
527
    /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
522
    mov ax, 0x4800
528
    mov ax, 0x4800
523
    int 0x2f
529
    int 0x2f
524
    mov bl, al /* save doskey status in BL */
530
    mov bl, al /* save doskey status in BL */
525
 
531
 
526
    /* set up buffered input to inpseg:inpoff */
532
    /* set up buffered input to inpseg:inpoff */
527
    mov ax, inpseg
533
    mov ax, inpseg
528
    push ax
534
    push ax
529
    pop ds
535
    pop ds
530
    mov dx, inpoff
536
    mov dx, inpoff
531
 
537
 
532
    /* execute either DOS input or DOSKEY */
538
    /* execute either DOS input or DOSKEY */
533
    test bl, bl /* zf set if no DOSKEY present */
539
    test bl, bl /* zf set if no DOSKEY present */
534
    jnz DOSKEY
540
    jnz DOSKEY
535
 
541
 
536
    mov ah, 0x0a
542
    mov ah, 0x0a
537
    int 0x21
543
    int 0x21
538
    jmp short DONE
544
    jmp short DONE
539
 
545
 
540
    DOSKEY:
546
    DOSKEY:
541
    mov ax, 0x4810
547
    mov ax, 0x4810
542
    int 0x2f
548
    int 0x2f
543
 
549
 
544
    DONE:
550
    DONE:
545
    /* terminate command with a CR/LF */
551
    /* terminate command with a CR/LF */
546
    mov ah, 0x02 /* display character in dl */
552
    mov ah, 0x02 /* display character in dl */
547
    mov dl, 0x0d
553
    mov dl, 0x0d
548
    int 0x21
554
    int 0x21
549
    mov dl, 0x0a
555
    mov dl, 0x0a
550
    int 0x21
556
    int 0x21
551
 
557
 
552
    pop ds
558
    pop ds
553
    pop dx
559
    pop dx
554
    pop cx
560
    pop cx
555
    pop bx
561
    pop bx
556
    pop ax
562
    pop ax
557
  }
563
  }
558
}
564
}
559
 
565
 
560
 
566
 
561
/* fetches a line from batch file and write it to buff (NULL-terminated),
567
/* fetches a line from batch file and write it to buff (NULL-terminated),
562
 * increments rmod counter and returns 0 on success. */
568
 * increments rmod counter and returns 0 on success. */
563
static int getbatcmd(char *buff, unsigned char buffmaxlen, struct rmod_props far *rmod) {
569
static int getbatcmd(char *buff, unsigned char buffmaxlen, struct rmod_props far *rmod) {
564
  unsigned short i;
570
  unsigned short i;
565
  unsigned short batname_seg = FP_SEG(rmod->batfile);
571
  unsigned short batname_seg = FP_SEG(rmod->batfile);
566
  unsigned short batname_off = FP_OFF(rmod->batfile);
572
  unsigned short batname_off = FP_OFF(rmod->batfile);
567
  unsigned short filepos_cx = rmod->batnextline >> 16;
573
  unsigned short filepos_cx = rmod->batnextline >> 16;
568
  unsigned short filepos_dx = rmod->batnextline & 0xffff;
574
  unsigned short filepos_dx = rmod->batnextline & 0xffff;
569
  unsigned char blen = 0;
575
  unsigned char blen = 0;
570
  unsigned short errv = 0;
576
  unsigned short errv = 0;
571
 
577
 
572
  /* open file, jump to offset filpos, and read data into buff.
578
  /* open file, jump to offset filpos, and read data into buff.
573
   * result in blen (unchanged if EOF or failure). */
579
   * result in blen (unchanged if EOF or failure). */
574
  _asm {
580
  _asm {
575
    push ax
581
    push ax
576
    push bx
582
    push bx
577
    push cx
583
    push cx
578
    push dx
584
    push dx
579
 
585
 
580
    /* open file (read-only) */
586
    /* open file (read-only) */
581
    mov bx, 0xffff        /* preset BX to 0xffff to detect error conditions */
587
    mov bx, 0xffff        /* preset BX to 0xffff to detect error conditions */
582
    mov dx, batname_off
588
    mov dx, batname_off
583
    mov ax, batname_seg
589
    mov ax, batname_seg
584
    push ds     /* save DS */
590
    push ds     /* save DS */
585
    mov ds, ax
591
    mov ds, ax
586
    mov ax, 0x3d00
592
    mov ax, 0x3d00
587
    int 0x21    /* handle in ax on success */
593
    int 0x21    /* handle in ax on success */
588
    pop ds      /* restore DS */
594
    pop ds      /* restore DS */
589
    jc ERR
595
    jc ERR
590
    mov bx, ax  /* save handle to bx */
596
    mov bx, ax  /* save handle to bx */
591
 
597
 
592
    /* jump to file offset CX:DX */
598
    /* jump to file offset CX:DX */
593
    mov ax, 0x4200
599
    mov ax, 0x4200
594
    mov cx, filepos_cx
600
    mov cx, filepos_cx
595
    mov dx, filepos_dx
601
    mov dx, filepos_dx
596
    int 0x21  /* CF clear on success, DX:AX set to cur pos */
602
    int 0x21  /* CF clear on success, DX:AX set to cur pos */
597
    jc ERR
603
    jc ERR
598
 
604
 
599
    /* read the line into buff */
605
    /* read the line into buff */
600
    mov ah, 0x3f
606
    mov ah, 0x3f
601
    xor ch, ch
607
    xor ch, ch
602
    mov cl, buffmaxlen
608
    mov cl, buffmaxlen
603
    mov dx, buff
609
    mov dx, buff
604
    int 0x21 /* CF clear on success, AX=number of bytes read */
610
    int 0x21 /* CF clear on success, AX=number of bytes read */
605
    jc ERR
611
    jc ERR
606
    mov blen, al
612
    mov blen, al
607
    jmp CLOSEANDQUIT
613
    jmp CLOSEANDQUIT
608
 
614
 
609
    ERR:
615
    ERR:
610
    mov errv, ax
616
    mov errv, ax
611
 
617
 
612
    CLOSEANDQUIT:
618
    CLOSEANDQUIT:
613
    /* close file (if bx contains a handle) */
619
    /* close file (if bx contains a handle) */
614
    cmp bx, 0xffff
620
    cmp bx, 0xffff
615
    je DONE
621
    je DONE
616
    mov ah, 0x3e
622
    mov ah, 0x3e
617
    int 0x21
623
    int 0x21
618
 
624
 
619
    DONE:
625
    DONE:
620
    pop dx
626
    pop dx
621
    pop cx
627
    pop cx
622
    pop bx
628
    pop bx
623
    pop ax
629
    pop ax
624
  }
630
  }
625
 
631
 
626
  /* printf("blen=%u filepos_cx=%u filepos_dx=%u\r\n", blen, filepos_cx, filepos_dx); */
632
  /* printf("blen=%u filepos_cx=%u filepos_dx=%u\r\n", blen, filepos_cx, filepos_dx); */
627
 
633
 
628
  if (errv != 0) nls_outputnl_doserr(errv);
634
  if (errv != 0) nls_outputnl_doserr(errv);
629
 
635
 
630
  /* on EOF - abort processing the bat file */
636
  /* on EOF - abort processing the bat file */
631
  if (blen == 0) goto OOPS;
637
  if (blen == 0) goto OOPS;
632
 
638
 
633
  /* find nearest \n to inc batch offset and replace \r by NULL terminator
639
  /* find nearest \n to inc batch offset and replace \r by NULL terminator
634
   * I support all CR/LF, CR- and LF-terminated batch files */
640
   * I support all CR/LF, CR- and LF-terminated batch files */
635
  for (i = 0; i < blen; i++) {
641
  for (i = 0; i < blen; i++) {
636
    if ((buff[i] == '\r') || (buff[i] == '\n')) {
642
    if ((buff[i] == '\r') || (buff[i] == '\n')) {
637
      if ((buff[i] == '\r') && ((i+1) < blen) && (buff[i+1] == '\n')) rmod->batnextline += 1;
643
      if ((buff[i] == '\r') && ((i+1) < blen) && (buff[i+1] == '\n')) rmod->batnextline += 1;
638
      break;
644
      break;
639
    }
645
    }
640
  }
646
  }
641
  buff[i] = 0;
647
  buff[i] = 0;
642
  rmod->batnextline += i + 1;
648
  rmod->batnextline += i + 1;
643
 
649
 
644
  return(0);
650
  return(0);
645
 
651
 
646
  OOPS:
652
  OOPS:
647
  rmod->batfile[0] = 0;
653
  rmod->batfile[0] = 0;
648
  rmod->batnextline = 0;
654
  rmod->batnextline = 0;
649
  return(-1);
655
  return(-1);
650
}
656
}
651
 
657
 
652
 
658
 
653
/* replaces %-variables in a BAT line with resolved values:
659
/* replaces %-variables in a BAT line with resolved values:
654
 * %PATH%       -> replaced by the contend of the PATH env variable
660
 * %PATH%       -> replaced by the contend of the PATH env variable
655
 * %UNDEFINED%  -> undefined variables are replaced by nothing ("")
661
 * %UNDEFINED%  -> undefined variables are replaced by nothing ("")
656
 * %NOTCLOSED   -> NOTCLOSED
662
 * %NOTCLOSED   -> NOTCLOSED
657
 * %1           -> first argument of the batch file (or nothing if no arg) */
663
 * %1           -> first argument of the batch file (or nothing if no arg) */
658
static void batpercrepl(char *res, unsigned short ressz, const char *line, const struct rmod_props far *rmod, unsigned short envseg) {
664
static void batpercrepl(char *res, unsigned short ressz, const char *line, const struct rmod_props far *rmod, unsigned short envseg) {
659
  unsigned short lastperc = 0xffff;
665
  unsigned short lastperc = 0xffff;
660
  unsigned short reslen = 0;
666
  unsigned short reslen = 0;
661
 
667
 
662
  if (ressz == 0) return;
668
  if (ressz == 0) return;
663
  ressz--; /* reserve one byte for the NULL terminator */
669
  ressz--; /* reserve one byte for the NULL terminator */
664
 
670
 
665
  for (; (reslen < ressz) && (*line != 0); line++) {
671
  for (; (reslen < ressz) && (*line != 0); line++) {
666
    /* if not a percent, I don't care */
672
    /* if not a percent, I don't care */
667
    if (*line != '%') {
673
    if (*line != '%') {
668
      res[reslen++] = *line;
674
      res[reslen++] = *line;
669
      continue;
675
      continue;
670
    }
676
    }
671
 
677
 
672
    /* *** perc char handling *** */
678
    /* *** perc char handling *** */
673
 
679
 
674
    /* closing perc? */
680
    /* closing perc? */
675
    if (lastperc != 0xffff) {
681
    if (lastperc != 0xffff) {
676
      /* %% is '%' */
682
      /* %% is '%' */
677
      if (lastperc == reslen) {
683
      if (lastperc == reslen) {
678
        res[reslen++] = '%';
684
        res[reslen++] = '%';
679
      } else {   /* otherwise variable name */
685
      } else {   /* otherwise variable name */
680
        const char far *ptr;
686
        const char far *ptr;
681
        res[reslen] = 0;
687
        res[reslen] = 0;
682
        reslen = lastperc;
688
        reslen = lastperc;
683
        ptr = env_lookup_val(envseg, res + reslen);
689
        ptr = env_lookup_val(envseg, res + reslen);
684
        if (ptr != NULL) {
690
        if (ptr != NULL) {
685
          while ((*ptr != 0) && (reslen < ressz)) {
691
          while ((*ptr != 0) && (reslen < ressz)) {
686
            res[reslen++] = *ptr;
692
            res[reslen++] = *ptr;
687
            ptr++;
693
            ptr++;
688
          }
694
          }
689
        }
695
        }
690
      }
696
      }
691
      lastperc = 0xffff;
697
      lastperc = 0xffff;
692
      continue;
698
      continue;
693
    }
699
    }
694
 
700
 
695
    /* digit? (bat arg) */
701
    /* digit? (bat arg) */
696
    if ((line[1] >= '0') && (line[1] <= '9')) {
702
    if ((line[1] >= '0') && (line[1] <= '9')) {
697
      unsigned short argid = line[1] - '0';
703
      unsigned short argid = line[1] - '0';
698
      unsigned short i;
704
      unsigned short i;
699
      const char far *argv = rmod->batargv;
705
      const char far *argv = rmod->batargv;
700
 
706
 
701
      /* locate the proper arg */
707
      /* locate the proper arg */
702
      for (i = 0; i != argid; i++) {
708
      for (i = 0; i != argid; i++) {
703
        /* if string is 0, then end of list reached */
709
        /* if string is 0, then end of list reached */
704
        if (*argv == 0) break;
710
        if (*argv == 0) break;
705
        /* jump to next arg */
711
        /* jump to next arg */
706
        while (*argv != 0) argv++;
712
        while (*argv != 0) argv++;
707
        argv++;
713
        argv++;
708
      }
714
      }
709
 
715
 
710
      /* copy the arg to result */
716
      /* copy the arg to result */
711
      for (i = 0; (argv[i] != 0) && (reslen < ressz); i++) {
717
      for (i = 0; (argv[i] != 0) && (reslen < ressz); i++) {
712
        res[reslen++] = argv[i];
718
        res[reslen++] = argv[i];
713
      }
719
      }
714
      line++;  /* skip the digit */
720
      line++;  /* skip the digit */
715
      continue;
721
      continue;
716
    }
722
    }
717
 
723
 
718
    /* opening perc */
724
    /* opening perc */
719
    lastperc = reslen;
725
    lastperc = reslen;
720
 
726
 
721
  }
727
  }
722
 
728
 
723
  res[reslen] = 0;
729
  res[reslen] = 0;
724
}
730
}
725
 
731
 
726
 
732
 
727
int main(void) {
733
int main(void) {
728
  static struct config cfg;
734
  static struct config cfg;
729
  static unsigned short far *rmod_envseg;
735
  static unsigned short far *rmod_envseg;
730
  static unsigned short far *lastexitcode;
736
  static unsigned short far *lastexitcode;
731
  static struct rmod_props far *rmod;
737
  static struct rmod_props far *rmod;
732
  static char cmdlinebuf[CMDLINE_MAXLEN + 2]; /* 1 extra byte for 0-terminator and another for memguard */
738
  static char cmdlinebuf[CMDLINE_MAXLEN + 2]; /* 1 extra byte for 0-terminator and another for memguard */
733
  static char *cmdline;
739
  static char *cmdline;
734
  static struct redir_data redirprops;
740
  static struct redir_data redirprops;
735
  static enum cmd_result cmdres;
741
  static enum cmd_result cmdres;
736
  static unsigned short i; /* general-purpose variable for short-lived things */
742
  static unsigned short i; /* general-purpose variable for short-lived things */
-
 
743
  static unsigned char delete_stdin_file;
737
 
744
 
738
  rmod = rmod_find(BUFFER_len);
745
  rmod = rmod_find(BUFFER_len);
739
  if (rmod == NULL) {
746
  if (rmod == NULL) {
740
    /* look at command line parameters (in case env size if set there) */
747
    /* look at command line parameters (in case env size if set there) */
741
    parse_argv(&cfg);
748
    parse_argv(&cfg);
742
    rmod = rmod_install(cfg.envsiz, BUFFER, BUFFER_len);
749
    rmod = rmod_install(cfg.envsiz, BUFFER, BUFFER_len);
743
    if (rmod == NULL) {
750
    if (rmod == NULL) {
744
      outputnl("ERROR: rmod_install() failed");
751
      outputnl("ERROR: rmod_install() failed");
745
      return(1);
752
      return(1);
746
    }
753
    }
747
    /* copy flags to rmod's storage (and enable ECHO) */
754
    /* copy flags to rmod's storage (and enable ECHO) */
748
    rmod->flags = cfg.flags | FLAG_ECHOFLAG;
755
    rmod->flags = cfg.flags | FLAG_ECHOFLAG;
749
    /* printf("rmod installed at %Fp\r\n", rmod); */
756
    /* printf("rmod installed at %Fp\r\n", rmod); */
750
    rmod->version = BYTE_VERSION;
757
    rmod->version = BYTE_VERSION;
751
  } else {
758
  } else {
752
    /* printf("rmod found at %Fp\r\n", rmod); */
759
    /* printf("rmod found at %Fp\r\n", rmod); */
753
    /* if I was spawned by rmod and FLAG_EXEC_AND_QUIT is set, then I should
760
    /* if I was spawned by rmod and FLAG_EXEC_AND_QUIT is set, then I should
754
     * die asap, because the command has been executed already, so I no longer
761
     * die asap, because the command has been executed already, so I no longer
755
     * have a purpose in life */
762
     * have a purpose in life */
756
    if (rmod->flags & FLAG_EXEC_AND_QUIT) sayonara(rmod);
763
    if (rmod->flags & FLAG_EXEC_AND_QUIT) sayonara(rmod);
757
    /* */
764
    /* */
758
    if (rmod->version != BYTE_VERSION) {
765
    if (rmod->version != BYTE_VERSION) {
759
      outputnl("SVARCOM VERSION CHANGED. SYSTEM HALTED. PLEASE REBOOT YOUR COMPUTER.");
766
      outputnl("SVARCOM VERSION CHANGED. SYSTEM HALTED. PLEASE REBOOT YOUR COMPUTER.");
760
      _asm {
767
      _asm {
761
        HALT:
768
        HALT:
762
        hlt
769
        hlt
763
        jmp HALT
770
        jmp HALT
764
      }
771
      }
765
    }
772
    }
766
  }
773
  }
767
 
774
 
768
  /* install a few guardvals in memory to detect some cases of overflows */
775
  /* install a few guardvals in memory to detect some cases of overflows */
769
  memguard_set(cmdlinebuf);
776
  memguard_set(cmdlinebuf);
770
 
777
 
771
  rmod_envseg = MK_FP(rmod->rmodseg, RMOD_OFFSET_ENVSEG);
778
  rmod_envseg = MK_FP(rmod->rmodseg, RMOD_OFFSET_ENVSEG);
772
  lastexitcode = MK_FP(rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
779
  lastexitcode = MK_FP(rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
773
 
780
 
774
  /* make COMPSEC point to myself */
781
  /* make COMPSEC point to myself */
775
  set_comspec_to_self(*rmod_envseg);
782
  set_comspec_to_self(*rmod_envseg);
776
 
783
 
777
/*  {
784
/*  {
778
    unsigned short envsiz;
785
    unsigned short envsiz;
779
    unsigned short far *sizptr = MK_FP(*rmod_envseg - 1, 3);
786
    unsigned short far *sizptr = MK_FP(*rmod_envseg - 1, 3);
780
    envsiz = *sizptr;
787
    envsiz = *sizptr;
781
    envsiz *= 16;
788
    envsiz *= 16;
782
    printf("rmod_inpbuff at %04X:%04X, env_seg at %04X:0000 (env_size = %u bytes)\r\n", rmod->rmodseg, RMOD_OFFSET_INPBUFF, *rmod_envseg, envsiz);
789
    printf("rmod_inpbuff at %04X:%04X, env_seg at %04X:0000 (env_size = %u bytes)\r\n", rmod->rmodseg, RMOD_OFFSET_INPBUFF, *rmod_envseg, envsiz);
783
  }*/
790
  }*/
784
 
791
 
785
  /* on /P check for the presence of AUTOEXEC.BAT and execute it if found,
792
  /* on /P check for the presence of AUTOEXEC.BAT and execute it if found,
786
   * but skip this check if /D was also passed */
793
   * but skip this check if /D was also passed */
787
  if ((cfg.flags & (FLAG_PERMANENT | FLAG_SKIP_AUTOEXEC)) == FLAG_PERMANENT) {
794
  if ((cfg.flags & (FLAG_PERMANENT | FLAG_SKIP_AUTOEXEC)) == FLAG_PERMANENT) {
788
    if (file_getattr("AUTOEXEC.BAT") >= 0) cfg.execcmd = "AUTOEXEC.BAT";
795
    if (file_getattr("AUTOEXEC.BAT") >= 0) cfg.execcmd = "AUTOEXEC.BAT";
789
  }
796
  }
790
 
797
 
791
  do {
798
  do {
792
    /* terminate previous command with a CR/LF if ECHO ON (but not during BAT processing) */
799
    /* terminate previous command with a CR/LF if ECHO ON (but not during BAT processing) */
793
    if ((rmod->flags & FLAG_ECHOFLAG) && (rmod->batfile[0] == 0)) outputnl("");
800
    if ((rmod->flags & FLAG_ECHOFLAG) && (rmod->batfile[0] == 0)) outputnl("");
794
 
801
 
795
    SKIP_NEWLINE:
802
    SKIP_NEWLINE:
796
 
803
 
797
    /* cancel any redirections that may have been set up before */
-
 
798
    redir_revert();
-
 
799
 
-
 
800
    /* memory check */
804
    /* memory check */
801
    memguard_check(rmod->rmodseg, cmdlinebuf);
805
    memguard_check(rmod->rmodseg, cmdlinebuf);
802
 
806
 
803
    /* preset cmdline to point at the dedicated buffer */
807
    /* preset cmdline to point at the dedicated buffer */
804
    cmdline = cmdlinebuf;
808
    cmdline = cmdlinebuf;
805
 
809
 
806
    /* (re)load translation strings if needed */
810
    /* (re)load translation strings if needed */
807
    nls_langreload(BUFFER, *rmod_envseg);
811
    nls_langreload(BUFFER, *rmod_envseg);
808
 
812
 
809
    /* load awaiting command, if any (used to run piped commands) */
813
    /* load awaiting command, if any (used to run piped commands) */
810
    if (rmod->awaitingcmd[0] != 0) {
814
    if (rmod->awaitingcmd[0] != 0) {
811
      _fstrcpy(cmdline, rmod->awaitingcmd);
815
      _fstrcpy(cmdline, rmod->awaitingcmd);
812
      rmod->awaitingcmd[0] = 0;
816
      rmod->awaitingcmd[0] = 0;
-
 
817
      delete_stdin_file = 1;
813
      goto EXEC_CMDLINE;
818
      goto EXEC_CMDLINE;
-
 
819
    } else {
-
 
820
      delete_stdin_file = 0;
814
    }
821
    }
815
 
822
 
816
    /* skip user input if I have a command to exec (/C or /K) */
823
    /* skip user input if I have a command to exec (/C or /K) */
817
    if (cfg.execcmd != NULL) {
824
    if (cfg.execcmd != NULL) {
818
      cmdline = cfg.execcmd;
825
      cmdline = cfg.execcmd;
819
      cfg.execcmd = NULL;
826
      cfg.execcmd = NULL;
820
      goto EXEC_CMDLINE;
827
      goto EXEC_CMDLINE;
821
    }
828
    }
822
 
829
 
823
    /* if batch file is being executed -> fetch next line */
830
    /* if batch file is being executed -> fetch next line */
824
    if (rmod->batfile[0] != 0) {
831
    if (rmod->batfile[0] != 0) {
825
      if (getbatcmd(BUFFER, CMDLINE_MAXLEN, rmod) != 0) { /* end of batch */
832
      if (getbatcmd(BUFFER, CMDLINE_MAXLEN, rmod) != 0) { /* end of batch */
826
        /* restore echo flag as it was before running the bat file */
833
        /* restore echo flag as it was before running the bat file */
827
        rmod->flags &= ~FLAG_ECHOFLAG;
834
        rmod->flags &= ~FLAG_ECHOFLAG;
828
        if (rmod->flags & FLAG_ECHO_BEFORE_BAT) rmod->flags |= FLAG_ECHOFLAG;
835
        if (rmod->flags & FLAG_ECHO_BEFORE_BAT) rmod->flags |= FLAG_ECHOFLAG;
829
        continue;
836
        continue;
830
      }
837
      }
831
      /* %-decoding of variables (%PATH%, %1, %%...), result in cmdline */
838
      /* %-decoding of variables (%PATH%, %1, %%...), result in cmdline */
832
      batpercrepl(cmdline, CMDLINE_MAXLEN, BUFFER, rmod, *rmod_envseg);
839
      batpercrepl(cmdline, CMDLINE_MAXLEN, BUFFER, rmod, *rmod_envseg);
833
      /* skip any leading spaces */
840
      /* skip any leading spaces */
834
      while (*cmdline == ' ') cmdline++;
841
      while (*cmdline == ' ') cmdline++;
835
      /* output prompt and command on screen if echo on and command is not
842
      /* output prompt and command on screen if echo on and command is not
836
       * inhibiting it with the @ prefix */
843
       * inhibiting it with the @ prefix */
837
      if ((rmod->flags & FLAG_ECHOFLAG) && (cmdline[0] != '@')) {
844
      if ((rmod->flags & FLAG_ECHOFLAG) && (cmdline[0] != '@')) {
838
        build_and_display_prompt(BUFFER, *rmod_envseg);
845
        build_and_display_prompt(BUFFER, *rmod_envseg);
839
        outputnl(cmdline);
846
        outputnl(cmdline);
840
      }
847
      }
841
      /* skip the @ prefix if present, it is no longer useful */
848
      /* skip the @ prefix if present, it is no longer useful */
842
      if (cmdline[0] == '@') cmdline++;
849
      if (cmdline[0] == '@') cmdline++;
843
    } else {
850
    } else {
844
      /* interactive mode: display prompt (if echo enabled) and wait for user
851
      /* interactive mode: display prompt (if echo enabled) and wait for user
845
       * command line */
852
       * command line */
846
      if (rmod->flags & FLAG_ECHOFLAG) build_and_display_prompt(BUFFER, *rmod_envseg);
853
      if (rmod->flags & FLAG_ECHOFLAG) build_and_display_prompt(BUFFER, *rmod_envseg);
847
      /* collect user input */
854
      /* collect user input */
848
      cmdline_getinput(FP_SEG(rmod->inputbuf), FP_OFF(rmod->inputbuf));
855
      cmdline_getinput(FP_SEG(rmod->inputbuf), FP_OFF(rmod->inputbuf));
849
      /* copy it to local cmdline */
856
      /* copy it to local cmdline */
850
      if (rmod->inputbuf[1] != 0) _fmemcpy(cmdline, rmod->inputbuf + 2, rmod->inputbuf[1]);
857
      if (rmod->inputbuf[1] != 0) _fmemcpy(cmdline, rmod->inputbuf + 2, rmod->inputbuf[1]);
851
      cmdline[(unsigned)(rmod->inputbuf[1])] = 0; /* zero-terminate local buff (oriignal is '\r'-terminated) */
858
      cmdline[(unsigned)(rmod->inputbuf[1])] = 0; /* zero-terminate local buff (oriignal is '\r'-terminated) */
852
    }
859
    }
853
 
860
 
854
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
861
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
855
    if (cmdline[0] == 0) goto SKIP_NEWLINE;
862
    if (cmdline[0] == 0) goto SKIP_NEWLINE;
856
 
863
 
857
    /* I jump here when I need to exec an initial command (/C or /K) */
864
    /* I jump here when I need to exec an initial command (/C or /K) */
858
    EXEC_CMDLINE:
865
    EXEC_CMDLINE:
859
 
866
 
860
    /* move pointer forward to skip over any leading spaces */
867
    /* move pointer forward to skip over any leading spaces */
861
    while (*cmdline == ' ') cmdline++;
868
    while (*cmdline == ' ') cmdline++;
862
 
869
 
863
    /* update rmod's ptr to COMPSPEC so it is always up to date */
870
    /* update rmod's ptr to COMPSPEC so it is always up to date */
864
    rmod_updatecomspecptr(rmod->rmodseg, *rmod_envseg);
871
    rmod_updatecomspecptr(rmod->rmodseg, *rmod_envseg);
865
 
872
 
866
    /* handle redirections (if any) */
873
    /* handle redirections (if any) */
867
    i = redir_parsecmd(&redirprops, cmdline, rmod->awaitingcmd);
874
    i = redir_parsecmd(&redirprops, cmdline, rmod->awaitingcmd);
868
    if (i != 0) {
875
    if (i != 0) {
869
      nls_outputnl_doserr(i);
876
      nls_outputnl_doserr(i);
870
      rmod->awaitingcmd[0] = 0;
877
      rmod->awaitingcmd[0] = 0;
871
      continue;
878
      continue;
872
    }
879
    }
873
 
880
 
874
    /* try matching (and executing) an internal command */
881
    /* try matching (and executing) an internal command */
875
    cmdres = cmd_process(rmod, *rmod_envseg, cmdline, BUFFER, sizeof(BUFFER), &redirprops);
882
    cmdres = cmd_process(rmod, *rmod_envseg, cmdline, BUFFER, sizeof(BUFFER), &redirprops, delete_stdin_file);
876
    if ((cmdres == CMD_OK) || (cmdres == CMD_FAIL)) {
883
    if ((cmdres == CMD_OK) || (cmdres == CMD_FAIL)) {
877
      /* internal command executed */
884
      /* internal command executed */
878
      continue;
885
      continue;
879
    } else if (cmdres == CMD_CHANGED) { /* cmdline changed, needs to be reprocessed */
886
    } else if (cmdres == CMD_CHANGED) { /* cmdline changed, needs to be reprocessed */
880
      goto EXEC_CMDLINE;
887
      goto EXEC_CMDLINE;
881
    } else if (cmdres == CMD_NOTFOUND) {
888
    } else if (cmdres == CMD_NOTFOUND) {
882
      /* this was not an internal command, try matching an external command */
889
      /* this was not an internal command, try matching an external command */
883
      run_as_external(BUFFER, cmdline, *rmod_envseg, rmod, &redirprops);
890
      run_as_external(BUFFER, cmdline, *rmod_envseg, rmod, &redirprops, delete_stdin_file);
884
      /* perhaps this is a newly launched BAT file */
891
      /* perhaps this is a newly launched BAT file */
885
      if ((rmod->batfile[0] != 0) && (rmod->batnextline == 0)) goto SKIP_NEWLINE;
892
      if ((rmod->batfile[0] != 0) && (rmod->batnextline == 0)) goto SKIP_NEWLINE;
886
      /* run_as_external() does not return on success, if I am still alive then
893
      /* run_as_external() does not return on success, if I am still alive then
887
       * external command failed to execute */
894
       * external command failed to execute */
888
      outputnl("Bad command or file name");
895
      outputnl("Bad command or file name");
889
      continue;
896
      continue;
890
    }
897
    }
891
 
898
 
892
    /* I should never ever land here */
899
    /* I should never ever land here */
893
    outputnl("INTERNAL ERR: INVALID CMDRES");
900
    outputnl("INTERNAL ERR: INVALID CMDRES");
894
 
901
 
895
  } while ((rmod->flags & FLAG_EXEC_AND_QUIT) == 0);
902
  } while ((rmod->flags & FLAG_EXEC_AND_QUIT) == 0);
896
 
903
 
897
  sayonara(rmod);
904
  sayonara(rmod);
898
  return(0);
905
  return(0);
899
}
906
}
900
 
907