Subversion Repositories SvarDOS

Rev

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

Rev 1070 Rev 1138
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 "svarlang.lib/svarlang.h"
31
#include "svarlang.lib/svarlang.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 4
48
#define BYTE_VERSION 4
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
      nls_output(0,6); /* "Invalid parameter" */
141
      nls_output(0,6); /* "Invalid parameter" */
142
      output(": ");
142
      output(": ");
143
      outputnl(cmdline);
143
      outputnl(cmdline);
144
      goto SKIP_TO_NEXT_ARG;
144
      goto SKIP_TO_NEXT_ARG;
145
    }
145
    }
146
 
146
 
147
    /* got a slash */
147
    /* got a slash */
148
    cmdline++;  /* skip the slash */
148
    cmdline++;  /* skip the slash */
149
    switch (*cmdline) {
149
    switch (*cmdline) {
150
      case 'c': /* /C = execute command and quit */
150
      case 'c': /* /C = execute command and quit */
151
      case 'C':
151
      case 'C':
152
        cfg->flags |= FLAG_EXEC_AND_QUIT;
152
        cfg->flags |= FLAG_EXEC_AND_QUIT;
153
        /* FALLTHRU */
153
        /* FALLTHRU */
154
      case 'k': /* /K = execute command and keep running */
154
      case 'k': /* /K = execute command and keep running */
155
      case 'K':
155
      case 'K':
156
        cmdline++;
156
        cmdline++;
157
        cfg->execcmd = cmdline;
157
        cfg->execcmd = cmdline;
158
        return; /* further arguments are for the executed program, not for me */
158
        return; /* further arguments are for the executed program, not for me */
159
 
159
 
160
      case 'y': /* /Y = execute batch file step-by-step (with /P, /K or /C) */
160
      case 'y': /* /Y = execute batch file step-by-step (with /P, /K or /C) */
161
      case 'Y':
161
      case 'Y':
162
        cfg->flags |= FLAG_STEPBYSTEP;
162
        cfg->flags |= FLAG_STEPBYSTEP;
163
        break;
163
        break;
164
 
164
 
165
      case 'd': /* /D = skip autoexec.bat processing */
165
      case 'd': /* /D = skip autoexec.bat processing */
166
      case 'D':
166
      case 'D':
167
        cfg->flags |= FLAG_SKIP_AUTOEXEC;
167
        cfg->flags |= FLAG_SKIP_AUTOEXEC;
168
        break;
168
        break;
169
 
169
 
170
      case 'e': /* preset the initial size of the environment block */
170
      case 'e': /* preset the initial size of the environment block */
171
      case 'E':
171
      case 'E':
172
        cmdline++;
172
        cmdline++;
173
        if (*cmdline == ':') cmdline++; /* could be /E:size */
173
        if (*cmdline == ':') cmdline++; /* could be /E:size */
174
        atous(&(cfg->envsiz), cmdline);
174
        atous(&(cfg->envsiz), cmdline);
175
        if (cfg->envsiz < 64) cfg->envsiz = 0;
175
        if (cfg->envsiz < 64) cfg->envsiz = 0;
176
        break;
176
        break;
177
 
177
 
178
      case 'p': /* permanent shell (can't exit + run autoexec.bat) */
178
      case 'p': /* permanent shell (can't exit + run autoexec.bat) */
179
      case 'P':
179
      case 'P':
180
        cfg->flags |= FLAG_PERMANENT;
180
        cfg->flags |= FLAG_PERMANENT;
181
        break;
181
        break;
182
 
182
 
183
      case '?':
183
      case '?':
184
        nls_outputnl(1,0); /* "Starts the SvarCOM command interpreter" */
184
        nls_outputnl(1,0); /* "Starts the SvarCOM command interpreter" */
185
        outputnl("");
185
        outputnl("");
186
        nls_outputnl(1,1); /* "COMMAND /E:nnn [/[C|K] [/P] [/D] command]" */
186
        nls_outputnl(1,1); /* "COMMAND /E:nnn [/[C|K] [/P] [/D] command]" */
187
        outputnl("");
187
        outputnl("");
188
        nls_outputnl(1,2); /* "/D      Skip AUTOEXEC.BAT processing (makes sense only with /P)" */
188
        nls_outputnl(1,2); /* "/D      Skip AUTOEXEC.BAT processing (makes sense only with /P)" */
189
        nls_outputnl(1,3); /* "/E:nnn  Sets the environment size to nnn bytes" */
189
        nls_outputnl(1,3); /* "/E:nnn  Sets the environment size to nnn bytes" */
190
        nls_outputnl(1,4); /* "/P      Makes the new command interpreter permanent and run AUTOEXEC.BAT" */
190
        nls_outputnl(1,4); /* "/P      Makes the new command interpreter permanent and run AUTOEXEC.BAT" */
191
        nls_outputnl(1,5); /* "/C      Executes the specified command and returns" */
191
        nls_outputnl(1,5); /* "/C      Executes the specified command and returns" */
192
        nls_outputnl(1,6); /* "/K      Executes the specified command and continues running" */
192
        nls_outputnl(1,6); /* "/K      Executes the specified command and continues running" */
193
        nls_outputnl(1,7); /* "/Y      Executes the batch program step by step" */
193
        nls_outputnl(1,7); /* "/Y      Executes the batch program step by step" */
194
        exit(1);
194
        exit(1);
195
        break;
195
        break;
196
 
196
 
197
      default:
197
      default:
198
        nls_output(0,2); /* invalid switch */
198
        nls_output(0,2); /* invalid switch */
199
        output(": /");
199
        output(": /");
200
        outputnl(cmdline);
200
        outputnl(cmdline);
201
        break;
201
        break;
202
    }
202
    }
203
 
203
 
204
    /* move to next argument or quit processing if end of cmdline */
204
    /* move to next argument or quit processing if end of cmdline */
205
    SKIP_TO_NEXT_ARG:
205
    SKIP_TO_NEXT_ARG:
206
    while ((*cmdline != 0) && (*cmdline != ' ') && (*cmdline != '/')) cmdline++;
206
    while ((*cmdline != 0) && (*cmdline != ' ') && (*cmdline != '/')) cmdline++;
207
  }
207
  }
208
}
208
}
209
 
209
 
210
 
210
 
211
/* builds the prompt string and displays it. buff is filled with a zero-terminated copy of the prompt. */
211
/* builds the prompt string and displays it. buff is filled with a zero-terminated copy of the prompt. */
212
static void build_and_display_prompt(char *buff, unsigned short envseg) {
212
static void build_and_display_prompt(char *buff, unsigned short envseg) {
213
  char *s = buff;
213
  char *s = buff;
214
  /* locate the prompt variable or use the default pattern */
214
  /* locate the prompt variable or use the default pattern */
215
  const char far *fmt = env_lookup_val(envseg, "PROMPT");
215
  const char far *fmt = env_lookup_val(envseg, "PROMPT");
216
  if ((fmt == NULL) || (*fmt == 0)) fmt = "$p$g"; /* fallback to default if empty */
216
  if ((fmt == NULL) || (*fmt == 0)) fmt = "$p$g"; /* fallback to default if empty */
217
  /* build the prompt string based on pattern */
217
  /* build the prompt string based on pattern */
218
  for (; *fmt != 0; fmt++) {
218
  for (; *fmt != 0; fmt++) {
219
    if (*fmt != '$') {
219
    if (*fmt != '$') {
220
      *s = *fmt;
220
      *s = *fmt;
221
      s++;
221
      s++;
222
      continue;
222
      continue;
223
    }
223
    }
224
    /* escape code ($P, etc) */
224
    /* escape code ($P, etc) */
225
    fmt++;
225
    fmt++;
226
    switch (*fmt) {
226
    switch (*fmt) {
227
      case 'Q':  /* $Q = = (equal sign) */
227
      case 'Q':  /* $Q = = (equal sign) */
228
      case 'q':
228
      case 'q':
229
        *s = '=';
229
        *s = '=';
230
        s++;
230
        s++;
231
        break;
231
        break;
232
      case '$':  /* $$ = $ (dollar sign) */
232
      case '$':  /* $$ = $ (dollar sign) */
233
        *s = '$';
233
        *s = '$';
234
        s++;
234
        s++;
235
        break;
235
        break;
236
      case 'T':  /* $t = current time */
236
      case 'T':  /* $t = current time */
237
      case 't':
237
      case 't':
238
        s += sprintf(s, "00:00"); /* TODO */
238
        s += sprintf(s, "00:00"); /* TODO */
239
        break;
239
        break;
240
      case 'D':  /* $D = current date */
240
      case 'D':  /* $D = current date */
241
      case 'd':
241
      case 'd':
242
        s += sprintf(s, "1985-07-29"); /* TODO */
242
        s += sprintf(s, "1985-07-29"); /* TODO */
243
        break;
243
        break;
244
      case 'P':  /* $P = current drive and path */
244
      case 'P':  /* $P = current drive and path */
245
      case 'p':
245
      case 'p':
246
        _asm {
246
        _asm {
247
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
247
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
248
          int 0x21
248
          int 0x21
249
          mov bx, s
249
          mov bx, s
250
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
250
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
251
        }
251
        }
252
        *s += 'A';
252
        *s += 'A';
253
        s++;
253
        s++;
254
        *s = ':';
254
        *s = ':';
255
        s++;
255
        s++;
256
        *s = '\\';
256
        *s = '\\';
257
        s++;
257
        s++;
258
        _asm {
258
        _asm {
259
          mov ah, 0x47    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */
259
          mov ah, 0x47    /* DOS 2+ - CWD - GET CURRENT DIRECTORY */
260
          xor dl,dl       /* DL = drive number (00h = default, 01h = A:, etc) */
260
          xor dl,dl       /* DL = drive number (00h = default, 01h = A:, etc) */
261
          mov si, s       /* DS:SI -> 64-byte buffer for ASCIZ pathname */
261
          mov si, s       /* DS:SI -> 64-byte buffer for ASCIZ pathname */
262
          int 0x21
262
          int 0x21
263
          jc DONE         /* leave path empty on error */
263
          jc DONE         /* leave path empty on error */
264
          /* move s ptr forward to end (0-termintor) of pathname */
264
          /* move s ptr forward to end (0-termintor) of pathname */
265
          NEXTBYTE:
265
          NEXTBYTE:
266
          mov si, s
266
          mov si, s
267
          cmp byte ptr [si], 0
267
          cmp byte ptr [si], 0
268
          je DONE
268
          je DONE
269
          inc s
269
          inc s
270
          jmp NEXTBYTE
270
          jmp NEXTBYTE
271
          DONE:
271
          DONE:
272
        }
272
        }
273
        break;
273
        break;
274
      case 'V':  /* $V = DOS version number */
274
      case 'V':  /* $V = DOS version number */
275
      case 'v':
275
      case 'v':
276
        s += sprintf(s, "VER"); /* TODO */
276
        s += sprintf(s, "VER"); /* TODO */
277
        break;
277
        break;
278
      case 'N':  /* $N = current drive */
278
      case 'N':  /* $N = current drive */
279
      case 'n':
279
      case 'n':
280
        _asm {
280
        _asm {
281
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
281
          mov ah, 0x19    /* DOS 1+ - GET CURRENT DRIVE */
282
          int 0x21
282
          int 0x21
283
          mov bx, s
283
          mov bx, s
284
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
284
          mov [bx], al  /* AL = drive (00 = A:, 01 = B:, etc */
285
        }
285
        }
286
        *s += 'A';
286
        *s += 'A';
287
        s++;
287
        s++;
288
        break;
288
        break;
289
      case 'G':  /* $G = > (greater-than sign) */
289
      case 'G':  /* $G = > (greater-than sign) */
290
      case 'g':
290
      case 'g':
291
        *s = '>';
291
        *s = '>';
292
        s++;
292
        s++;
293
        break;
293
        break;
294
      case 'L':  /* $L = < (less-than sign) */
294
      case 'L':  /* $L = < (less-than sign) */
295
      case 'l':
295
      case 'l':
296
        *s = '<';
296
        *s = '<';
297
        s++;
297
        s++;
298
        break;
298
        break;
299
      case 'B':  /* $B = | (pipe) */
299
      case 'B':  /* $B = | (pipe) */
300
      case 'b':
300
      case 'b':
301
        *s = '|';
301
        *s = '|';
302
        s++;
302
        s++;
303
        break;
303
        break;
304
      case 'H':  /* $H = backspace (erases previous character) */
304
      case 'H':  /* $H = backspace (erases previous character) */
305
      case 'h':
305
      case 'h':
306
        *s = '\b';
306
        *s = '\b';
307
        s++;
307
        s++;
308
        break;
308
        break;
309
      case 'E':  /* $E = Escape code (ASCII 27) */
309
      case 'E':  /* $E = Escape code (ASCII 27) */
310
      case 'e':
310
      case 'e':
311
        *s = 27;
311
        *s = 27;
312
        s++;
312
        s++;
313
        break;
313
        break;
314
      case '_':  /* $_ = CR+LF */
314
      case '_':  /* $_ = CR+LF */
315
        *s = '\r';
315
        *s = '\r';
316
        s++;
316
        s++;
317
        *s = '\n';
317
        *s = '\n';
318
        s++;
318
        s++;
319
        break;
319
        break;
320
    }
320
    }
321
  }
321
  }
322
  *s = 0;
322
  *s = 0;
323
  output(buff);
323
  output(buff);
324
}
324
}
325
 
325
 
326
 
326
 
327
/* a few internal flags */
327
/* a few internal flags */
328
#define DELETE_STDIN_FILE 1
328
#define DELETE_STDIN_FILE 1
329
#define CALL_FLAG         2
329
#define CALL_FLAG         2
330
 
330
 
331
static void run_as_external(char *buff, const char *cmdline, unsigned short envseg, struct rmod_props far *rmod, struct redir_data *redir, unsigned char flags) {
331
static void run_as_external(char *buff, const char *cmdline, unsigned short envseg, struct rmod_props far *rmod, struct redir_data *redir, unsigned char flags) {
332
  char *cmdfile = buff + 512;
332
  char *cmdfile = buff + 512;
333
  const char far *pathptr;
333
  const char far *pathptr;
334
  int lookup;
334
  int lookup;
335
  unsigned short i;
335
  unsigned short i;
336
  const char *ext;
336
  const char *ext;
337
  char *cmd = buff + 1024;
337
  char *cmd = buff + 1024;
338
  const char *cmdtail;
338
  const char *cmdtail;
339
  char far *rmod_execprog = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPROG);
339
  char far *rmod_execprog = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPROG);
340
  char far *rmod_cmdtail = MK_FP(rmod->rmodseg, 0x81);
340
  char far *rmod_cmdtail = MK_FP(rmod->rmodseg, 0x81);
341
  _Packed struct {
341
  _Packed struct {
342
    unsigned short envseg;
342
    unsigned short envseg;
343
    unsigned long cmdtail;
343
    unsigned long cmdtail;
344
    unsigned long fcb1;
344
    unsigned long fcb1;
345
    unsigned long fcb2;
345
    unsigned long fcb2;
346
  } far *ExecParam = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPARAM);
346
  } far *ExecParam = MK_FP(rmod->rmodseg, RMOD_OFFSET_EXECPARAM);
347
 
347
 
348
  /* find cmd and cmdtail */
348
  /* find cmd and cmdtail */
349
  i = 0;
349
  i = 0;
350
  cmdtail = cmdline;
350
  cmdtail = cmdline;
351
  while (*cmdtail == ' ') cmdtail++; /* skip any leading spaces */
351
  while (*cmdtail == ' ') cmdtail++; /* skip any leading spaces */
352
  while ((*cmdtail != ' ') && (*cmdtail != '/') && (*cmdtail != '+') && (*cmdtail != 0)) {
352
  while ((*cmdtail != ' ') && (*cmdtail != '/') && (*cmdtail != '+') && (*cmdtail != 0)) {
353
    cmd[i++] = *cmdtail;
353
    cmd[i++] = *cmdtail;
354
    cmdtail++;
354
    cmdtail++;
355
  }
355
  }
356
  cmd[i] = 0;
356
  cmd[i] = 0;
357
 
357
 
358
  /* is this a command in curdir? */
358
  /* is this a command in curdir? */
359
  lookup = lookup_cmd(cmdfile, cmd, NULL, &ext);
359
  lookup = lookup_cmd(cmdfile, cmd, NULL, &ext);
360
  if (lookup == 0) {
360
  if (lookup == 0) {
361
    /* printf("FOUND LOCAL EXEC FILE: '%s'\r\n", cmdfile); */
361
    /* printf("FOUND LOCAL EXEC FILE: '%s'\r\n", cmdfile); */
362
    goto RUNCMDFILE;
362
    goto RUNCMDFILE;
363
  } else if (lookup == -2) {
363
  } else if (lookup == -2) {
364
    /* puts("NOT FOUND"); */
364
    /* puts("NOT FOUND"); */
365
    return;
365
    return;
366
  }
366
  }
367
 
367
 
368
  /* try matching something in PATH */
368
  /* try matching something in PATH */
369
  pathptr = env_lookup_val(envseg, "PATH");
369
  pathptr = env_lookup_val(envseg, "PATH");
370
 
370
 
371
  /* try each path in %PATH% */
371
  /* try each path in %PATH% */
372
  while (pathptr) {
372
  while (pathptr) {
373
    for (i = 0;; i++) {
373
    for (i = 0;; i++) {
374
      buff[i] = *pathptr;
374
      buff[i] = *pathptr;
375
      if ((buff[i] == 0) || (buff[i] == ';')) break;
375
      if ((buff[i] == 0) || (buff[i] == ';')) break;
376
      pathptr++;
376
      pathptr++;
377
    }
377
    }
378
    buff[i] = 0;
378
    buff[i] = 0;
379
    lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
379
    lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
380
    if (lookup == 0) goto RUNCMDFILE;
380
    if (lookup == 0) goto RUNCMDFILE;
381
    if (lookup == -2) return;
381
    if (lookup == -2) return;
382
    if (*pathptr == ';') {
382
    if (*pathptr == ';') {
383
      pathptr++;
383
      pathptr++;
384
    } else {
384
    } else {
385
      break;
385
      break;
386
    }
386
    }
387
  }
387
  }
388
 
388
 
389
  /* last chance: is it an executable link? (trim extension from cmd first) */
389
  /* last chance: is it an executable link? (trim extension from cmd first) */
390
  for (i = 0; (cmd[i] != 0) && (cmd[i] != '.') && (i < 9); i++) buff[128 + i] = cmd[i];
390
  for (i = 0; (cmd[i] != 0) && (cmd[i] != '.') && (i < 9); i++) buff[128 + i] = cmd[i];
391
  buff[128 + i] = 0;
391
  buff[128 + i] = 0;
392
  if ((i < 9) && (link_computefname(buff, buff + 128, envseg) == 0)) {
392
  if ((i < 9) && (link_computefname(buff, buff + 128, envseg) == 0)) {
393
    /* try opening the link file (if it exists) and read it into buff */
393
    /* try opening the link file (if it exists) and read it into buff */
394
    i = 0;
394
    i = 0;
395
    _asm {
395
    _asm {
396
      push ax
396
      push ax
397
      push bx
397
      push bx
398
      push cx
398
      push cx
399
      push dx
399
      push dx
400
 
400
 
401
      mov ax, 0x3d00  /* DOS 2+ - OPEN EXISTING FILE, READ-ONLY */
401
      mov ax, 0x3d00  /* DOS 2+ - OPEN EXISTING FILE, READ-ONLY */
402
      mov dx, buff    /* file name */
402
      mov dx, buff    /* file name */
403
      int 0x21
403
      int 0x21
404
      jc ERR_FOPEN
404
      jc ERR_FOPEN
405
      /* file handle in AX, read from file now */
405
      /* file handle in AX, read from file now */
406
      mov bx, ax      /* file handle */
406
      mov bx, ax      /* file handle */
407
      mov ah, 0x3f    /* Read from file via handle bx */
407
      mov ah, 0x3f    /* Read from file via handle bx */
408
      mov cx, 128     /* up to 128 bytes */
408
      mov cx, 128     /* up to 128 bytes */
409
      /* mov dx, buff */ /* dest buffer (already set) */
409
      /* mov dx, buff */ /* dest buffer (already set) */
410
      int 0x21        /* read up to 256 bytes from file and write to buff */
410
      int 0x21        /* read up to 256 bytes from file and write to buff */
411
      jc ERR_READ
411
      jc ERR_READ
412
      mov i, ax
412
      mov i, ax
413
      ERR_READ:
413
      ERR_READ:
414
      mov ah, 0x3e    /* close file handle in BX */
414
      mov ah, 0x3e    /* close file handle in BX */
415
      int 0x21
415
      int 0x21
416
      ERR_FOPEN:
416
      ERR_FOPEN:
417
 
417
 
418
      pop dx
418
      pop dx
419
      pop cx
419
      pop cx
420
      pop bx
420
      pop bx
421
      pop ax
421
      pop ax
422
    }
422
    }
423
 
423
 
424
    /* did I read anything? */
424
    /* did I read anything? */
425
    if (i != 0) {
425
    if (i != 0) {
426
      buff[i] = 0;
426
      buff[i] = 0;
427
      /* trim buff at first \n or \r, just in case someone fiddled with the
427
      /* trim buff at first \n or \r, just in case someone fiddled with the
428
       * link file using a text editor */
428
       * link file using a text editor */
429
      for (i = 0; (buff[i] != 0) && (buff[i] != '\r') && (buff[i] != '\n'); i++);
429
      for (i = 0; (buff[i] != 0) && (buff[i] != '\r') && (buff[i] != '\n'); i++);
430
      buff[i] = 0;
430
      buff[i] = 0;
431
      /* lookup check */
431
      /* lookup check */
432
      if (buff[0] != 0) {
432
      if (buff[0] != 0) {
433
        lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
433
        lookup = lookup_cmd(cmdfile, cmd, buff, &ext);
434
        if (lookup == 0) goto RUNCMDFILE;
434
        if (lookup == 0) goto RUNCMDFILE;
435
      }
435
      }
436
    }
436
    }
437
  }
437
  }
438
 
438
 
439
  /* all failed (ie. executable file not found) */
439
  /* all failed (ie. executable file not found) */
440
  return;
440
  return;
441
 
441
 
442
  RUNCMDFILE:
442
  RUNCMDFILE:
443
 
443
 
444
  /* special handling of batch files */
444
  /* special handling of batch files */
445
  if ((ext != NULL) && (imatch(ext, "bat"))) {
445
  if ((ext != NULL) && (imatch(ext, "bat"))) {
446
    struct batctx far *newbat;
446
    struct batctx far *newbat;
447
 
447
 
448
    /* remember the echo flag (in case bat file disables echo, only when starting first bat) */
448
    /* remember the echo flag (in case bat file disables echo, only when starting first bat) */
449
    if (rmod->bat == NULL) {
449
    if (rmod->bat == NULL) {
450
      rmod->flags &= ~FLAG_ECHO_BEFORE_BAT;
450
      rmod->flags &= ~FLAG_ECHO_BEFORE_BAT;
451
      if (rmod->flags & FLAG_ECHOFLAG) rmod->flags |= FLAG_ECHO_BEFORE_BAT;
451
      if (rmod->flags & FLAG_ECHOFLAG) rmod->flags |= FLAG_ECHO_BEFORE_BAT;
452
    }
452
    }
453
 
453
 
454
    /* if bat is not called via a CALL, then free the bat-context linked list */
454
    /* if bat is not called via a CALL, then free the bat-context linked list */
455
    if ((flags & CALL_FLAG) == 0) rmod_free_bat_llist(rmod);
455
    if ((flags & CALL_FLAG) == 0) rmod_free_bat_llist(rmod);
456
 
456
 
457
    /* allocate a new bat context */
457
    /* allocate a new bat context */
458
    newbat = rmod_fcalloc(sizeof(struct batctx), rmod->rmodseg, "SVBATCTX");
458
    newbat = rmod_fcalloc(sizeof(struct batctx), rmod->rmodseg, "SVBATCTX");
459
    if (newbat == NULL) {
459
    if (newbat == NULL) {
460
      nls_outputnl_doserr(8); /* insufficient memory */
460
      nls_outputnl_doserr(8); /* insufficient memory */
461
      return;
461
      return;
462
    }
462
    }
463
 
463
 
464
    /* fill the newly allocated batctx structure */
464
    /* fill the newly allocated batctx structure */
465
    _fstrcpy(newbat->fname, cmdfile); /* truename of the BAT file */
465
    _fstrcpy(newbat->fname, cmdfile); /* truename of the BAT file */
466
    newbat->flags = flags & FLAG_STEPBYSTEP;
466
    newbat->flags = flags & FLAG_STEPBYSTEP;
467
    /* explode args of the bat file and store them in rmod buff */
467
    /* explode args of the bat file and store them in rmod buff */
468
    cmd_explode(buff, cmdline, NULL);
468
    cmd_explode(buff, cmdline, NULL);
469
    _fmemcpy(newbat->argv, buff, sizeof(newbat->argv));
469
    _fmemcpy(newbat->argv, buff, sizeof(newbat->argv));
470
 
470
 
471
    /* push the new bat to the top of rmod's linked list */
471
    /* push the new bat to the top of rmod's linked list */
472
    newbat->parent = rmod->bat;
472
    newbat->parent = rmod->bat;
473
    rmod->bat = newbat;
473
    rmod->bat = newbat;
474
 
474
 
475
    return;
475
    return;
476
  }
476
  }
477
 
477
 
478
  /* copy full filename to execute, along with redirected files (if any) */
478
  /* copy full filename to execute, along with redirected files (if any) */
479
  _fstrcpy(rmod_execprog, cmdfile);
479
  _fstrcpy(rmod_execprog, cmdfile);
480
 
480
 
481
  /* copy stdin file if a redirection is needed */
481
  /* copy stdin file if a redirection is needed */
482
  if (redir->stdinfile) {
482
  if (redir->stdinfile) {
483
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDINFILE);
483
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDINFILE);
484
    char far *delstdin = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDIN_DEL);
484
    char far *delstdin = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDIN_DEL);
485
    _fstrcpy(farptr, redir->stdinfile);
485
    _fstrcpy(farptr, redir->stdinfile);
486
    if (flags & DELETE_STDIN_FILE) {
486
    if (flags & DELETE_STDIN_FILE) {
487
      *delstdin = redir->stdinfile[0];
487
      *delstdin = redir->stdinfile[0];
488
    } else {
488
    } else {
489
      *delstdin = 0;
489
      *delstdin = 0;
490
    }
490
    }
491
  }
491
  }
492
 
492
 
493
  /* same for stdout file */
493
  /* same for stdout file */
494
  if (redir->stdoutfile) {
494
  if (redir->stdoutfile) {
495
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTFILE);
495
    char far *farptr = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTFILE);
496
    unsigned short far *farptr16 = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTAPP);
496
    unsigned short far *farptr16 = MK_FP(rmod->rmodseg, RMOD_OFFSET_STDOUTAPP);
497
    _fstrcpy(farptr, redir->stdoutfile);
497
    _fstrcpy(farptr, redir->stdoutfile);
498
    /* openflag */
498
    /* openflag */
499
    *farptr16 = redir->stdout_openflag;
499
    *farptr16 = redir->stdout_openflag;
500
  }
500
  }
501
 
501
 
502
  /* copy cmdtail to rmod's PSP and compute its len */
502
  /* copy cmdtail to rmod's PSP and compute its len */
503
  for (i = 0; cmdtail[i] != 0; i++) rmod_cmdtail[i] = cmdtail[i];
503
  for (i = 0; cmdtail[i] != 0; i++) rmod_cmdtail[i] = cmdtail[i];
504
  rmod_cmdtail[i] = '\r';
504
  rmod_cmdtail[i] = '\r';
505
  rmod_cmdtail[-1] = i;
505
  rmod_cmdtail[-1] = i;
506
 
506
 
507
  /* set up rmod to execute the command */
507
  /* set up rmod to execute the command */
508
 
508
 
509
  ExecParam->envseg = envseg;
509
  ExecParam->envseg = envseg;
510
  ExecParam->cmdtail = (unsigned long)MK_FP(rmod->rmodseg, 0x80); /* farptr, must be in PSP format (lenbyte args \r) */
510
  ExecParam->cmdtail = (unsigned long)MK_FP(rmod->rmodseg, 0x80); /* farptr, must be in PSP format (lenbyte args \r) */
511
  ExecParam->fcb1 = 0; /* TODO farptr */
511
  ExecParam->fcb1 = 0; /* TODO farptr */
512
  ExecParam->fcb2 = 0; /* TODO farptr */
512
  ExecParam->fcb2 = 0; /* TODO farptr */
513
  exit(0); /* let rmod do the job now */
513
  exit(0); /* let rmod do the job now */
514
}
514
}
515
 
515
 
516
 
516
 
517
static void set_comspec_to_self(unsigned short envseg) {
517
static void set_comspec_to_self(unsigned short envseg) {
518
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
518
  unsigned short *psp_envseg = (void *)(0x2c); /* pointer to my env segment field in the PSP */
519
  char far *myenv = MK_FP(*psp_envseg, 0);
519
  char far *myenv = MK_FP(*psp_envseg, 0);
520
  unsigned short varcount;
520
  unsigned short varcount;
521
  char buff[256] = "COMSPEC=";
521
  char buff[256] = "COMSPEC=";
522
  char *buffptr = buff + 8;
522
  char *buffptr = buff + 8;
523
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
523
  /* who am i? look into my own environment, at the end of it should be my EXEPATH string */
524
  while (*myenv != 0) {
524
  while (*myenv != 0) {
525
    /* consume a NULL-terminated string */
525
    /* consume a NULL-terminated string */
526
    while (*myenv != 0) myenv++;
526
    while (*myenv != 0) myenv++;
527
    /* move to next string */
527
    /* move to next string */
528
    myenv++;
528
    myenv++;
529
  }
529
  }
530
  /* get next word, if 1 then EXEPATH follows */
530
  /* get next word, if 1 then EXEPATH follows */
531
  myenv++;
531
  myenv++;
532
  varcount = *myenv;
532
  varcount = *myenv;
533
  myenv++;
533
  myenv++;
534
  varcount |= (*myenv << 8);
534
  varcount |= (*myenv << 8);
535
  myenv++;
535
  myenv++;
536
  if (varcount != 1) return; /* NO EXEPATH FOUND */
536
  if (varcount != 1) return; /* NO EXEPATH FOUND */
537
  while (*myenv != 0) {
537
  while (*myenv != 0) {
538
    *buffptr = *myenv;
538
    *buffptr = *myenv;
539
    buffptr++;
539
    buffptr++;
540
    myenv++;
540
    myenv++;
541
  }
541
  }
542
  *buffptr = 0;
542
  *buffptr = 0;
543
  /* printf("EXEPATH: '%s'\r\n", buff); */
543
  /* printf("EXEPATH: '%s'\r\n", buff); */
544
  env_setvar(envseg, buff);
544
  env_setvar(envseg, buff);
545
}
545
}
546
 
546
 
547
 
547
 
548
/* wait for user input */
548
/* wait for user input */
549
static void cmdline_getinput(unsigned short inpseg, unsigned short inpoff) {
549
static void cmdline_getinput(unsigned short inpseg, unsigned short inpoff) {
550
  _asm {
550
  _asm {
551
    push ax
551
    push ax
552
    push bx
552
    push bx
553
    push cx
553
    push cx
554
    push dx
554
    push dx
555
    push ds
555
    push ds
556
 
556
 
557
    /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
557
    /* is DOSKEY support present? (INT 2Fh, AX=4800h, returns non-zero in AL if present) */
558
    mov ax, 0x4800
558
    mov ax, 0x4800
559
    int 0x2f
559
    int 0x2f
560
    mov bl, al /* save doskey status in BL */
560
    mov bl, al /* save doskey status in BL */
561
 
561
 
562
    /* set up buffered input to inpseg:inpoff */
562
    /* set up buffered input to inpseg:inpoff */
563
    mov ax, inpseg
563
    mov ax, inpseg
564
    push ax
564
    push ax
565
    pop ds
565
    pop ds
566
    mov dx, inpoff
566
    mov dx, inpoff
567
 
567
 
568
    /* execute either DOS input or DOSKEY */
568
    /* execute either DOS input or DOSKEY */
569
    test bl, bl /* zf set if no DOSKEY present */
569
    test bl, bl /* zf set if no DOSKEY present */
570
    jnz DOSKEY
570
    jnz DOSKEY
571
 
571
 
572
    mov ah, 0x0a
572
    mov ah, 0x0a
573
    int 0x21
573
    int 0x21
574
    jmp short DONE
574
    jmp short DONE
575
 
575
 
576
    DOSKEY:
576
    DOSKEY:
577
    mov ax, 0x4810
577
    mov ax, 0x4810
578
    int 0x2f
578
    int 0x2f
579
 
579
 
580
    DONE:
580
    DONE:
581
    /* terminate command with a CR/LF */
581
    /* terminate command with a CR/LF */
582
    mov ah, 0x02 /* display character in dl */
582
    mov ah, 0x02 /* display character in dl */
583
    mov dl, 0x0d
583
    mov dl, 0x0d
584
    int 0x21
584
    int 0x21
585
    mov dl, 0x0a
585
    mov dl, 0x0a
586
    int 0x21
586
    int 0x21
587
 
587
 
588
    pop ds
588
    pop ds
589
    pop dx
589
    pop dx
590
    pop cx
590
    pop cx
591
    pop bx
591
    pop bx
592
    pop ax
592
    pop ax
593
  }
593
  }
594
}
594
}
595
 
595
 
596
 
596
 
597
/* fetches a line from batch file and write it to buff (NULL-terminated),
597
/* fetches a line from batch file and write it to buff (NULL-terminated),
598
 * increments rmod counter and returns 0 on success. */
598
 * increments rmod counter and returns 0 on success. */
599
static int getbatcmd(char *buff, unsigned char buffmaxlen, struct rmod_props far *rmod) {
599
static int getbatcmd(char *buff, unsigned char buffmaxlen, struct rmod_props far *rmod) {
600
  unsigned short i;
600
  unsigned short i;
601
  unsigned short batname_seg = FP_SEG(rmod->bat->fname);
601
  unsigned short batname_seg = FP_SEG(rmod->bat->fname);
602
  unsigned short batname_off = FP_OFF(rmod->bat->fname);
602
  unsigned short batname_off = FP_OFF(rmod->bat->fname);
603
  unsigned short filepos_cx = rmod->bat->nextline >> 16;
603
  unsigned short filepos_cx = rmod->bat->nextline >> 16;
604
  unsigned short filepos_dx = rmod->bat->nextline & 0xffff;
604
  unsigned short filepos_dx = rmod->bat->nextline & 0xffff;
605
  unsigned char blen = 0;
605
  unsigned char blen = 0;
606
  unsigned short errv = 0;
606
  unsigned short errv = 0;
607
 
607
 
608
  /* open file, jump to offset filpos, and read data into buff.
608
  /* open file, jump to offset filpos, and read data into buff.
609
   * result in blen (unchanged if EOF or failure). */
609
   * result in blen (unchanged if EOF or failure). */
610
  _asm {
610
  _asm {
611
    push ax
611
    push ax
612
    push bx
612
    push bx
613
    push cx
613
    push cx
614
    push dx
614
    push dx
615
 
615
 
616
    /* open file (read-only) */
616
    /* open file (read-only) */
617
    mov bx, 0xffff        /* preset BX to 0xffff to detect error conditions */
617
    mov bx, 0xffff        /* preset BX to 0xffff to detect error conditions */
618
    mov dx, batname_off
618
    mov dx, batname_off
619
    mov ax, batname_seg
619
    mov ax, batname_seg
620
    push ds     /* save DS */
620
    push ds     /* save DS */
621
    mov ds, ax
621
    mov ds, ax
622
    mov ax, 0x3d00
622
    mov ax, 0x3d00
623
    int 0x21    /* handle in ax on success */
623
    int 0x21    /* handle in ax on success */
624
    pop ds      /* restore DS */
624
    pop ds      /* restore DS */
625
    jc ERR
625
    jc ERR
626
    mov bx, ax  /* save handle to bx */
626
    mov bx, ax  /* save handle to bx */
627
 
627
 
628
    /* jump to file offset CX:DX */
628
    /* jump to file offset CX:DX */
629
    mov ax, 0x4200
629
    mov ax, 0x4200
630
    mov cx, filepos_cx
630
    mov cx, filepos_cx
631
    mov dx, filepos_dx
631
    mov dx, filepos_dx
632
    int 0x21  /* CF clear on success, DX:AX set to cur pos */
632
    int 0x21  /* CF clear on success, DX:AX set to cur pos */
633
    jc ERR
633
    jc ERR
634
 
634
 
635
    /* read the line into buff */
635
    /* read the line into buff */
636
    mov ah, 0x3f
636
    mov ah, 0x3f
637
    xor ch, ch
637
    xor ch, ch
638
    mov cl, buffmaxlen
638
    mov cl, buffmaxlen
639
    mov dx, buff
639
    mov dx, buff
640
    int 0x21 /* CF clear on success, AX=number of bytes read */
640
    int 0x21 /* CF clear on success, AX=number of bytes read */
641
    jc ERR
641
    jc ERR
642
    mov blen, al
642
    mov blen, al
643
    jmp CLOSEANDQUIT
643
    jmp CLOSEANDQUIT
644
 
644
 
645
    ERR:
645
    ERR:
646
    mov errv, ax
646
    mov errv, ax
647
 
647
 
648
    CLOSEANDQUIT:
648
    CLOSEANDQUIT:
649
    /* close file (if bx contains a handle) */
649
    /* close file (if bx contains a handle) */
650
    cmp bx, 0xffff
650
    cmp bx, 0xffff
651
    je DONE
651
    je DONE
652
    mov ah, 0x3e
652
    mov ah, 0x3e
653
    int 0x21
653
    int 0x21
654
 
654
 
655
    DONE:
655
    DONE:
656
    pop dx
656
    pop dx
657
    pop cx
657
    pop cx
658
    pop bx
658
    pop bx
659
    pop ax
659
    pop ax
660
  }
660
  }
661
 
661
 
662
  /* printf("blen=%u filepos_cx=%u filepos_dx=%u\r\n", blen, filepos_cx, filepos_dx); */
662
  /* printf("blen=%u filepos_cx=%u filepos_dx=%u\r\n", blen, filepos_cx, filepos_dx); */
663
 
663
 
664
  if (errv != 0) nls_outputnl_doserr(errv);
664
  if (errv != 0) nls_outputnl_doserr(errv);
665
 
665
 
666
  /* on EOF - abort processing the bat file */
666
  /* on EOF - abort processing the bat file */
667
  if (blen == 0) goto OOPS;
667
  if (blen == 0) goto OOPS;
668
 
668
 
669
  /* find nearest \n to inc batch offset and replace \r by NULL terminator
669
  /* find nearest \n to inc batch offset and replace \r by NULL terminator
670
   * I support all CR/LF, CR- and LF-terminated batch files */
670
   * I support all CR/LF, CR- and LF-terminated batch files */
671
  for (i = 0; i < blen; i++) {
671
  for (i = 0; i < blen; i++) {
672
    if ((buff[i] == '\r') || (buff[i] == '\n')) {
672
    if ((buff[i] == '\r') || (buff[i] == '\n')) {
673
      if ((buff[i] == '\r') && ((i+1) < blen) && (buff[i+1] == '\n')) rmod->bat->nextline += 1;
673
      if ((buff[i] == '\r') && ((i+1) < blen) && (buff[i+1] == '\n')) rmod->bat->nextline += 1;
674
      break;
674
      break;
675
    }
675
    }
676
  }
676
  }
677
  buff[i] = 0;
677
  buff[i] = 0;
678
  rmod->bat->nextline += i + 1;
678
  rmod->bat->nextline += i + 1;
679
 
679
 
680
  return(0);
680
  return(0);
681
 
681
 
682
  OOPS:
682
  OOPS:
683
  rmod->bat->fname[0] = 0;
683
  rmod->bat->fname[0] = 0;
684
  rmod->bat->nextline = 0;
684
  rmod->bat->nextline = 0;
685
  return(-1);
685
  return(-1);
686
}
686
}
687
 
687
 
688
 
688
 
689
/* replaces %-variables in a BAT line with resolved values:
689
/* replaces %-variables in a BAT line with resolved values:
690
 * %PATH%       -> replaced by the contend of the PATH env variable
690
 * %PATH%       -> replaced by the contend of the PATH env variable
691
 * %UNDEFINED%  -> undefined variables are replaced by nothing ("")
691
 * %UNDEFINED%  -> undefined variables are replaced by nothing ("")
692
 * %NOTCLOSED   -> NOTCLOSED
692
 * %NOTCLOSED   -> NOTCLOSED
693
 * %1           -> first argument of the batch file (or nothing if no arg) */
693
 * %1           -> first argument of the batch file (or nothing if no arg) */
694
static void batpercrepl(char *res, unsigned short ressz, const char *line, const struct rmod_props far *rmod, unsigned short envseg) {
694
static void batpercrepl(char *res, unsigned short ressz, const char *line, const struct rmod_props far *rmod, unsigned short envseg) {
695
  unsigned short lastperc = 0xffff;
695
  unsigned short lastperc = 0xffff;
696
  unsigned short reslen = 0;
696
  unsigned short reslen = 0;
697
 
697
 
698
  if (ressz == 0) return;
698
  if (ressz == 0) return;
699
  ressz--; /* reserve one byte for the NULL terminator */
699
  ressz--; /* reserve one byte for the NULL terminator */
700
 
700
 
701
  for (; (reslen < ressz) && (*line != 0); line++) {
701
  for (; (reslen < ressz) && (*line != 0); line++) {
702
    /* if not a percent, I don't care */
702
    /* if not a percent, I don't care */
703
    if (*line != '%') {
703
    if (*line != '%') {
704
      res[reslen++] = *line;
704
      res[reslen++] = *line;
705
      continue;
705
      continue;
706
    }
706
    }
707
 
707
 
708
    /* *** perc char handling *** */
708
    /* *** perc char handling *** */
709
 
709
 
710
    /* closing perc? */
710
    /* closing perc? */
711
    if (lastperc != 0xffff) {
711
    if (lastperc != 0xffff) {
712
      /* %% is '%' */
712
      /* %% is '%' */
713
      if (lastperc == reslen) {
713
      if (lastperc == reslen) {
714
        res[reslen++] = '%';
714
        res[reslen++] = '%';
715
      } else {   /* otherwise variable name */
715
      } else {   /* otherwise variable name */
716
        const char far *ptr;
716
        const char far *ptr;
717
        res[reslen] = 0;
717
        res[reslen] = 0;
718
        reslen = lastperc;
718
        reslen = lastperc;
719
        strupr(res + reslen); /* turn varname uppercase before lookup */
719
        strupr(res + reslen); /* turn varname uppercase before lookup */
720
        ptr = env_lookup_val(envseg, res + reslen);
720
        ptr = env_lookup_val(envseg, res + reslen);
721
        if (ptr != NULL) {
721
        if (ptr != NULL) {
722
          while ((*ptr != 0) && (reslen < ressz)) {
722
          while ((*ptr != 0) && (reslen < ressz)) {
723
            res[reslen++] = *ptr;
723
            res[reslen++] = *ptr;
724
            ptr++;
724
            ptr++;
725
          }
725
          }
726
        }
726
        }
727
      }
727
      }
728
      lastperc = 0xffff;
728
      lastperc = 0xffff;
729
      continue;
729
      continue;
730
    }
730
    }
731
 
731
 
732
    /* digit? (bat arg) */
732
    /* digit? (bat arg) */
733
    if ((line[1] >= '0') && (line[1] <= '9')) {
733
    if ((line[1] >= '0') && (line[1] <= '9')) {
734
      unsigned short argid = line[1] - '0';
734
      unsigned short argid = line[1] - '0';
735
      unsigned short i;
735
      unsigned short i;
736
      const char far *argv = "";
736
      const char far *argv = "";
737
      if ((rmod != NULL) && (rmod->bat != NULL)) argv = rmod->bat->argv;
737
      if ((rmod != NULL) && (rmod->bat != NULL)) argv = rmod->bat->argv;
738
 
738
 
739
      /* locate the proper arg */
739
      /* locate the proper arg */
740
      for (i = 0; i != argid; i++) {
740
      for (i = 0; i != argid; i++) {
741
        /* if string is 0, then end of list reached */
741
        /* if string is 0, then end of list reached */
742
        if (*argv == 0) break;
742
        if (*argv == 0) break;
743
        /* jump to next arg */
743
        /* jump to next arg */
744
        while (*argv != 0) argv++;
744
        while (*argv != 0) argv++;
745
        argv++;
745
        argv++;
746
      }
746
      }
747
 
747
 
748
      /* copy the arg to result */
748
      /* copy the arg to result */
749
      for (i = 0; (argv[i] != 0) && (reslen < ressz); i++) {
749
      for (i = 0; (argv[i] != 0) && (reslen < ressz); i++) {
750
        res[reslen++] = argv[i];
750
        res[reslen++] = argv[i];
751
      }
751
      }
752
      line++;  /* skip the digit */
752
      line++;  /* skip the digit */
753
      continue;
753
      continue;
754
    }
754
    }
755
 
755
 
756
    /* opening perc */
756
    /* opening perc */
757
    lastperc = reslen;
757
    lastperc = reslen;
758
 
758
 
759
  }
759
  }
760
 
760
 
761
  res[reslen] = 0;
761
  res[reslen] = 0;
762
}
762
}
763
 
763
 
764
 
764
 
765
/* process the ongoing forloop, returns 0 on success, non-zero otherwise (no
765
/* process the ongoing forloop, returns 0 on success, non-zero otherwise (no
766
   more things to process) */
766
   more things to process) */
767
static int forloop_process(char *res, struct forctx far *forloop) {
767
static int forloop_process(char *res, struct forctx far *forloop) {
768
  unsigned short i, t;
768
  unsigned short i, t;
769
  struct DTA *dta = (void *)0x80; /* default DTA at 80h in PSP */
769
  struct DTA *dta = (void *)0x80; /* default DTA at 80h in PSP */
770
  char *fnameptr = dta->fname;
770
  char *fnameptr = dta->fname;
771
  char *pathprefix = BUFFER + 256;
771
  char *pathprefix = BUFFER + 256;
772
 
772
 
773
  *pathprefix = 0;
773
  *pathprefix = 0;
774
 
774
 
775
  TRYAGAIN:
775
  TRYAGAIN:
776
 
776
 
777
  /* dta_inited: FindFirst() or FindNext()? */
777
  /* dta_inited: FindFirst() or FindNext()? */
778
  if (forloop->dta_inited == 0) {
778
  if (forloop->dta_inited == 0) {
779
 
779
 
780
    /* copy next awaiting pattern to BUFFER (and skip all delimiters until
780
    /* copy next awaiting pattern to BUFFER (and skip all delimiters until
781
     * next pattern or end of list) */
781
     * next pattern or end of list) */
782
    t = 0;
782
    t = 0;
783
    for (i = 0;; i++) {
783
    for (i = 0;; i++) {
784
      BUFFER[i] = forloop->cmd[forloop->nextpat + i];
784
      BUFFER[i] = forloop->cmd[forloop->nextpat + i];
785
      /* is this a delimiter? (all delimiters are already normalized to a space here) */
785
      /* is this a delimiter? (all delimiters are already normalized to a space here) */
786
      if (BUFFER[i] == ' ') {
786
      if (BUFFER[i] == ' ') {
787
        BUFFER[i] = 0;
787
        BUFFER[i] = 0;
788
        t = 1;
788
        t = 1;
789
      } else if (BUFFER[i] == 0) {
789
      } else if (BUFFER[i] == 0) {
790
        /* end of patterns list */
790
        /* end of patterns list */
791
        break;
791
        break;
792
      } else {
792
      } else {
793
        /* quit if I got a pattern already */
793
        /* quit if I got a pattern already */
794
        if (t == 1) break;
794
        if (t == 1) break;
795
      }
795
      }
796
    }
796
    }
797
 
797
 
798
    if (i == 0) return(-1);
798
    if (i == 0) return(-1);
799
 
799
 
800
    /* remember position of current pattern */
800
    /* remember position of current pattern */
801
    forloop->curpat = forloop->nextpat;
801
    forloop->curpat = forloop->nextpat;
802
 
802
 
803
    /* move nextpat forward to next pattern */
803
    /* move nextpat forward to next pattern */
804
    i += forloop->nextpat;
804
    i += forloop->nextpat;
805
    forloop->nextpat = i;
805
    forloop->nextpat = i;
806
 
806
 
807
    /* if this is a string and not a pattern, skip all the FindFirst business
807
    /* if this is a string and not a pattern, skip all the FindFirst business
808
     * a file pattern has a wildcard (* or ?), a message doesn't */
808
     * a file pattern has a wildcard (* or ?), a message doesn't */
809
    for (i = 0; (BUFFER[i] != 0) && (BUFFER[i] != '?') && (BUFFER[i] != '*'); i++);
809
    for (i = 0; (BUFFER[i] != 0) && (BUFFER[i] != '?') && (BUFFER[i] != '*'); i++);
810
    if (BUFFER[i] == 0) {
810
    if (BUFFER[i] == 0) {
811
      fnameptr = BUFFER;
811
      fnameptr = BUFFER;
812
      goto SKIP_DTA;
812
      goto SKIP_DTA;
813
    }
813
    }
814
 
814
 
815
    /* FOR in MSDOS 6 includes hidden and system files, but not directories nor volumes */
815
    /* FOR in MSDOS 6 includes hidden and system files, but not directories nor volumes */
816
    if (findfirst(dta, BUFFER, DOS_ATTR_RO | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_ARC) != 0) {
816
    if (findfirst(dta, BUFFER, DOS_ATTR_RO | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_ARC) != 0) {
817
      goto TRYAGAIN;
817
      goto TRYAGAIN;
818
    }
818
    }
819
    forloop->dta_inited = 1;
819
    forloop->dta_inited = 1;
820
  } else { /* dta in progress */
820
  } else { /* dta in progress */
821
 
821
 
822
    /* copy forloop DTA to my local copy */
822
    /* copy forloop DTA to my local copy */
823
    _fmemcpy(dta, &(forloop->dta), sizeof(*dta));
823
    _fmemcpy(dta, &(forloop->dta), sizeof(*dta));
824
 
824
 
825
    /* findnext() call */
825
    /* findnext() call */
826
    if (findnext(dta) != 0) {
826
    if (findnext(dta) != 0) {
827
      forloop->dta_inited = 0;
827
      forloop->dta_inited = 0;
828
      goto TRYAGAIN;
828
      goto TRYAGAIN;
829
    }
829
    }
830
  }
830
  }
831
 
831
 
832
  /* copy updated DTA to rmod */
832
  /* copy updated DTA to rmod */
833
  _fmemcpy(&(forloop->dta), dta, sizeof(*dta));
833
  _fmemcpy(&(forloop->dta), dta, sizeof(*dta));
834
 
834
 
835
  /* prefill pathprefix with the prefix (path) of the files */
835
  /* prefill pathprefix with the prefix (path) of the files */
836
  {
836
  {
837
    short lastbk = -1;
837
    short lastbk = -1;
838
    char far *c = forloop->cmd + forloop->curpat;
838
    char far *c = forloop->cmd + forloop->curpat;
839
    for (i = 0;; i++) {
839
    for (i = 0;; i++) {
840
      pathprefix[i] = c[i];
840
      pathprefix[i] = c[i];
841
      if (pathprefix[i] == '\\') lastbk = i;
841
      if (pathprefix[i] == '\\') lastbk = i;
842
      if ((pathprefix[i] == ' ') || (pathprefix[i] == 0)) break;
842
      if ((pathprefix[i] == ' ') || (pathprefix[i] == 0)) break;
843
    }
843
    }
844
    pathprefix[lastbk+1] = 0;
844
    pathprefix[lastbk+1] = 0;
845
  }
845
  }
846
 
846
 
847
  SKIP_DTA:
847
  SKIP_DTA:
848
 
848
 
849
  /* fill res with command, replacing varname by actual filename */
849
  /* fill res with command, replacing varname by actual filename */
850
  /* full filename is to be built with path of curpat and fname from dta */
850
  /* full filename is to be built with path of curpat and fname from dta */
851
  t = 0;
851
  t = 0;
852
  i = 0;
852
  i = 0;
853
  for (;;) {
853
  for (;;) {
854
    if ((forloop->cmd[forloop->exec + t] == '%') && (forloop->cmd[forloop->exec + t + 1] == forloop->varname)) {
854
    if ((forloop->cmd[forloop->exec + t] == '%') && (forloop->cmd[forloop->exec + t + 1] == forloop->varname)) {
855
      strcpy(res + i, pathprefix);
855
      strcpy(res + i, pathprefix);
856
      strcat(res + i, fnameptr);
856
      strcat(res + i, fnameptr);
857
      for (; res[i] != 0; i++);
857
      for (; res[i] != 0; i++);
858
      t += 2;
858
      t += 2;
859
    } else {
859
    } else {
860
      res[i] = forloop->cmd[forloop->exec + t];
860
      res[i] = forloop->cmd[forloop->exec + t];
861
      t++;
861
      t++;
862
      if (res[i++] == 0) break;
862
      if (res[i++] == 0) break;
863
    }
863
    }
864
  }
864
  }
865
 
865
 
866
  return(0);
866
  return(0);
867
}
867
}
868
 
868
 
869
 
869
 
870
int main(void) {
870
int main(void) {
871
  static struct config cfg;
871
  static struct config cfg;
872
  static unsigned short far *rmod_envseg;
872
  static unsigned short far *rmod_envseg;
873
  static unsigned short far *lastexitcode;
873
  static unsigned short far *lastexitcode;
874
  static struct rmod_props far *rmod;
874
  static struct rmod_props far *rmod;
875
  static char cmdlinebuf[CMDLINE_MAXLEN + 2]; /* 1 extra byte for 0-terminator and another for memguard */
875
  static char cmdlinebuf[CMDLINE_MAXLEN + 2]; /* 1 extra byte for 0-terminator and another for memguard */
876
  static char *cmdline;
876
  static char *cmdline;
877
  static struct redir_data redirprops;
877
  static struct redir_data redirprops;
878
  static enum cmd_result cmdres;
878
  static enum cmd_result cmdres;
879
  static unsigned short i; /* general-purpose variable for short-lived things */
879
  static unsigned short i; /* general-purpose variable for short-lived things */
880
  static unsigned char flags;
880
  static unsigned char flags;
881
 
881
 
882
  rmod = rmod_find(BUFFER_len);
882
  rmod = rmod_find(BUFFER_len);
883
  if (rmod == NULL) {
883
  if (rmod == NULL) {
884
    /* look at command line parameters (in case env size if set there) */
884
    /* look at command line parameters (in case env size if set there) */
885
    parse_argv(&cfg);
885
    parse_argv(&cfg);
886
    rmod = rmod_install(cfg.envsiz, BUFFER, BUFFER_len);
886
    rmod = rmod_install(cfg.envsiz, BUFFER, BUFFER_len);
887
    if (rmod == NULL) {
887
    if (rmod == NULL) {
888
      nls_outputnl_err(2,1); /* "FATAL ERROR: rmod_install() failed" */
888
      nls_outputnl_err(2,1); /* "FATAL ERROR: rmod_install() failed" */
889
      return(1);
889
      return(1);
890
    }
890
    }
891
    /* copy flags to rmod's storage (and enable ECHO) */
891
    /* copy flags to rmod's storage (and enable ECHO) */
892
    rmod->flags = cfg.flags | FLAG_ECHOFLAG;
892
    rmod->flags = cfg.flags | FLAG_ECHOFLAG;
893
    /* printf("rmod installed at %Fp\r\n", rmod); */
893
    /* printf("rmod installed at %Fp\r\n", rmod); */
894
    rmod->version = BYTE_VERSION;
894
    rmod->version = BYTE_VERSION;
895
  } else {
895
  } else {
896
    /* printf("rmod found at %Fp\r\n", rmod); */
896
    /* printf("rmod found at %Fp\r\n", rmod); */
897
    /* if I was spawned by rmod and FLAG_EXEC_AND_QUIT is set, then I should
897
    /* if I was spawned by rmod and FLAG_EXEC_AND_QUIT is set, then I should
898
     * die asap, because the command has been executed already, so I no longer
898
     * die asap, because the command has been executed already, so I no longer
899
     * have a purpose in life */
899
     * have a purpose in life */
900
    if (rmod->flags & FLAG_EXEC_AND_QUIT) sayonara(rmod);
900
    if (rmod->flags & FLAG_EXEC_AND_QUIT) sayonara(rmod);
901
    /* */
901
    /* */
902
    if (rmod->version != BYTE_VERSION) {
902
    if (rmod->version != BYTE_VERSION) {
903
      nls_outputnl_err(2,0);
903
      nls_outputnl_err(2,0);
904
      _asm {
904
      _asm {
905
        HALT:
905
        HALT:
906
        hlt
906
        hlt
907
        jmp HALT
907
        jmp HALT
908
      }
908
      }
909
    }
909
    }
910
  }
910
  }
911
 
911
 
912
  /* install a few guardvals in memory to detect some cases of overflows */
912
  /* install a few guardvals in memory to detect some cases of overflows */
913
  memguard_set(cmdlinebuf);
913
  memguard_set(cmdlinebuf);
914
 
914
 
915
  rmod_envseg = MK_FP(rmod->rmodseg, RMOD_OFFSET_ENVSEG);
915
  rmod_envseg = MK_FP(rmod->rmodseg, RMOD_OFFSET_ENVSEG);
916
  lastexitcode = MK_FP(rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
916
  lastexitcode = MK_FP(rmod->rmodseg, RMOD_OFFSET_LEXITCODE);
917
 
917
 
918
  /* make COMPSEC point to myself */
918
  /* make COMPSEC point to myself */
919
  set_comspec_to_self(*rmod_envseg);
919
  set_comspec_to_self(*rmod_envseg);
920
 
920
 
921
  /* on /P check for the presence of AUTOEXEC.BAT and execute it if found,
921
  /* on /P check for the presence of AUTOEXEC.BAT and execute it if found,
922
   * but skip this check if /D was also passed */
922
   * but skip this check if /D was also passed */
923
  if ((cfg.flags & (FLAG_PERMANENT | FLAG_SKIP_AUTOEXEC)) == FLAG_PERMANENT) {
923
  if ((cfg.flags & (FLAG_PERMANENT | FLAG_SKIP_AUTOEXEC)) == FLAG_PERMANENT) {
924
    if (file_getattr("AUTOEXEC.BAT") >= 0) cfg.execcmd = "AUTOEXEC.BAT";
924
    if (file_getattr("AUTOEXEC.BAT") >= 0) cfg.execcmd = "AUTOEXEC.BAT";
925
  }
925
  }
926
 
926
 
927
  do {
927
  do {
928
 
928
 
929
    /* terminate previous command with a CR/LF if ECHO ON (but not during BAT processing) */
929
    /* terminate previous command with a CR/LF if ECHO ON (but not during BAT processing) */
930
    if ((rmod->flags & FLAG_ECHOFLAG) && (rmod->bat == NULL)) outputnl("");
930
    if ((rmod->flags & FLAG_ECHOFLAG) && (rmod->bat == NULL)) outputnl("");
931
 
931
 
932
    SKIP_NEWLINE:
932
    SKIP_NEWLINE:
933
 
933
 
934
    /* memory check */
934
    /* memory check */
935
    memguard_check(rmod->rmodseg, cmdlinebuf);
935
    memguard_check(rmod->rmodseg, cmdlinebuf);
936
 
936
 
937
    /* preset cmdline to point at the dedicated buffer */
937
    /* preset cmdline to point at the dedicated buffer */
938
    cmdline = cmdlinebuf;
938
    cmdline = cmdlinebuf;
939
 
939
 
940
    /* (re)load translation strings if needed */
940
    /* (re)load translation strings if needed */
941
    nls_langreload(BUFFER, *rmod_envseg);
941
    nls_langreload(BUFFER, *rmod_envseg);
942
 
942
 
943
    /* am I inside a FOR loop? */
943
    /* am I inside a FOR loop? */
944
    if (rmod->forloop) {
944
    if (rmod->forloop) {
945
      if (forloop_process(cmdlinebuf, rmod->forloop) != 0) {
945
      if (forloop_process(cmdlinebuf, rmod->forloop) != 0) {
946
        rmod_ffree(rmod->forloop);
946
        rmod_ffree(rmod->forloop);
947
        rmod->forloop = NULL;
947
        rmod->forloop = NULL;
948
      } else {
948
      } else {
949
        /* output prompt and command on screen if echo on and command is not
949
        /* output prompt and command on screen if echo on and command is not
950
         * inhibiting it with the @ prefix */
950
         * inhibiting it with the @ prefix */
951
        if (rmod->flags & FLAG_ECHOFLAG) {
951
        if (rmod->flags & FLAG_ECHOFLAG) {
952
          build_and_display_prompt(BUFFER, *rmod_envseg);
952
          build_and_display_prompt(BUFFER, *rmod_envseg);
953
          outputnl(cmdline);
953
          outputnl(cmdline);
954
        }
954
        }
955
        /* jump to command processing */
955
        /* jump to command processing */
956
        goto EXEC_CMDLINE;
956
        goto EXEC_CMDLINE;
957
      }
957
      }
958
    }
958
    }
959
 
959
 
960
    /* load awaiting command, if any (used to run piped commands) */
960
    /* load awaiting command, if any (used to run piped commands) */
961
    if (rmod->awaitingcmd[0] != 0) {
961
    if (rmod->awaitingcmd[0] != 0) {
962
      _fstrcpy(cmdline, rmod->awaitingcmd);
962
      _fstrcpy(cmdline, rmod->awaitingcmd);
963
      rmod->awaitingcmd[0] = 0;
963
      rmod->awaitingcmd[0] = 0;
964
      flags |= DELETE_STDIN_FILE;
964
      flags |= DELETE_STDIN_FILE;
965
      goto EXEC_CMDLINE;
965
      goto EXEC_CMDLINE;
966
    } else {
966
    } else {
967
      flags &= ~DELETE_STDIN_FILE;
967
      flags &= ~DELETE_STDIN_FILE;
968
    }
968
    }
969
 
969
 
970
    /* skip user input if I have a command to exec (/C or /K or /P) */
970
    /* skip user input if I have a command to exec (/C or /K or /P) */
971
    if (cfg.execcmd != NULL) {
971
    if (cfg.execcmd != NULL) {
972
      cmdline = cfg.execcmd;
972
      cmdline = cfg.execcmd;
973
      cfg.execcmd = NULL;
973
      cfg.execcmd = NULL;
974
      /* */
974
      /* */
975
      if (cfg.flags & FLAG_STEPBYSTEP) flags |= FLAG_STEPBYSTEP;
975
      if (cfg.flags & FLAG_STEPBYSTEP) flags |= FLAG_STEPBYSTEP;
976
      goto EXEC_CMDLINE;
976
      goto EXEC_CMDLINE;
977
    }
977
    }
978
 
978
 
979
    /* if batch file is being executed -> fetch next line */
979
    /* if batch file is being executed -> fetch next line */
980
    if (rmod->bat != NULL) {
980
    if (rmod->bat != NULL) {
981
      if (getbatcmd(BUFFER, CMDLINE_MAXLEN, rmod) != 0) { /* end of batch */
981
      if (getbatcmd(BUFFER, CMDLINE_MAXLEN, rmod) != 0) { /* end of batch */
982
        struct batctx far *victim = rmod->bat;
982
        struct batctx far *victim = rmod->bat;
983
        rmod->bat = rmod->bat->parent;
983
        rmod->bat = rmod->bat->parent;
984
        rmod_ffree(victim);
984
        rmod_ffree(victim);
985
        /* end of batch? then restore echo flag as it was before running the (first) bat file */
985
        /* end of batch? then restore echo flag as it was before running the (first) bat file */
986
        if (rmod->bat == NULL) {
986
        if (rmod->bat == NULL) {
987
          rmod->flags &= ~FLAG_ECHOFLAG;
987
          rmod->flags &= ~FLAG_ECHOFLAG;
988
          if (rmod->flags & FLAG_ECHO_BEFORE_BAT) rmod->flags |= FLAG_ECHOFLAG;
988
          if (rmod->flags & FLAG_ECHO_BEFORE_BAT) rmod->flags |= FLAG_ECHOFLAG;
989
        }
989
        }
990
        continue;
990
        continue;
991
      }
991
      }
992
      /* %-decoding of variables (%PATH%, %1, %%...), result in cmdline */
992
      /* %-decoding of variables (%PATH%, %1, %%...), result in cmdline */
993
      batpercrepl(cmdline, CMDLINE_MAXLEN, BUFFER, rmod, *rmod_envseg);
993
      batpercrepl(cmdline, CMDLINE_MAXLEN, BUFFER, rmod, *rmod_envseg);
994
      /* skip any leading spaces */
994
      /* skip any leading spaces */
995
      while (*cmdline == ' ') cmdline++;
995
      while (*cmdline == ' ') cmdline++;
996
      /* skip batch labels */
996
      /* skip batch labels */
997
      if (*cmdline == ':') continue;
997
      if (*cmdline == ':') continue;
998
      /* step-by-step execution? */
998
      /* step-by-step execution? */
999
      if (rmod->bat->flags & FLAG_STEPBYSTEP) {
999
      if (rmod->bat->flags & FLAG_STEPBYSTEP) {
1000
        if (*cmdline == 0) continue; /* skip empty lines */
1000
        if (*cmdline == 0) continue; /* skip empty lines */
1001
        if (askchoice(cmdline, svarlang_str(0,10)) != 0) continue;
1001
        if (askchoice(cmdline, svarlang_str(0,10)) != 0) continue;
1002
      }
1002
      }
1003
      /* output prompt and command on screen if echo on and command is not
1003
      /* output prompt and command on screen if echo on and command is not
1004
       * inhibiting it with the @ prefix */
1004
       * inhibiting it with the @ prefix */
1005
      if ((rmod->flags & FLAG_ECHOFLAG) && (cmdline[0] != '@')) {
1005
      if ((rmod->flags & FLAG_ECHOFLAG) && (cmdline[0] != '@')) {
1006
        build_and_display_prompt(BUFFER, *rmod_envseg);
1006
        build_and_display_prompt(BUFFER, *rmod_envseg);
1007
        outputnl(cmdline);
1007
        outputnl(cmdline);
1008
      }
1008
      }
1009
      /* skip the @ prefix if present, it is no longer useful */
1009
      /* skip the @ prefix if present, it is no longer useful */
1010
      if (cmdline[0] == '@') cmdline++;
1010
      if (cmdline[0] == '@') cmdline++;
1011
    } else {
1011
    } else {
1012
      unsigned char far *rmod_inputbuf = MK_FP(rmod->rmodseg, RMOD_OFFSET_INPUTBUF);
1012
      unsigned char far *rmod_inputbuf = MK_FP(rmod->rmodseg, RMOD_OFFSET_INPUTBUF);
1013
      /* invalidate input history if it appears to be damaged (could occur
1013
      /* invalidate input history if it appears to be damaged (could occur
1014
       * because of a stack overflow, for example if some stack-hungry TSR is
1014
       * because of a stack overflow, for example if some stack-hungry TSR is
1015
       * being used) */
1015
       * being used) */
1016
      if ((rmod_inputbuf[0] != 128) || (rmod_inputbuf[rmod_inputbuf[1] + 2] != '\r') || (rmod_inputbuf[rmod_inputbuf[1] + 3] != 0xCA) || (rmod_inputbuf[rmod_inputbuf[1] + 4] != 0xFE)) {
1016
      if ((rmod_inputbuf[0] != 128) || (rmod_inputbuf[rmod_inputbuf[1] + 2] != '\r') || (rmod_inputbuf[rmod_inputbuf[1] + 3] != 0xCA) || (rmod_inputbuf[rmod_inputbuf[1] + 4] != 0xFE)) {
1017
        rmod_inputbuf[0] = 128;  /* max allowed input length */
1017
        rmod_inputbuf[0] = 128;  /* max allowed input length */
1018
        rmod_inputbuf[1] = 0;    /* string len stored in buffer */
1018
        rmod_inputbuf[1] = 0;    /* string len stored in buffer */
1019
        rmod_inputbuf[2] = '\r'; /* string terminator */
1019
        rmod_inputbuf[2] = '\r'; /* string terminator */
1020
        rmod_inputbuf[3] = 0xCA; /* trailing signature */
1020
        rmod_inputbuf[3] = 0xCA; /* trailing signature */
1021
        rmod_inputbuf[4] = 0xFE; /* trailing signature */
1021
        rmod_inputbuf[4] = 0xFE; /* trailing signature */
1022
        nls_outputnl_err(2,2); /* "stack overflow detected, command history flushed" */
1022
        nls_outputnl_err(2,2); /* "stack overflow detected, command history flushed" */
1023
      }
1023
      }
1024
      /* interactive mode: display prompt (if echo enabled) and wait for user
1024
      /* interactive mode: display prompt (if echo enabled) and wait for user
1025
       * command line */
1025
       * command line */
1026
      if (rmod->flags & FLAG_ECHOFLAG) build_and_display_prompt(BUFFER, *rmod_envseg);
1026
      if (rmod->flags & FLAG_ECHOFLAG) build_and_display_prompt(BUFFER, *rmod_envseg);
1027
      /* collect user input */
1027
      /* collect user input */
1028
      cmdline_getinput(rmod->rmodseg, RMOD_OFFSET_INPUTBUF);
1028
      cmdline_getinput(rmod->rmodseg, RMOD_OFFSET_INPUTBUF);
1029
      /* append stack-overflow detection signature to the end of the input buffer */
1029
      /* append stack-overflow detection signature to the end of the input buffer */
1030
      rmod_inputbuf[rmod_inputbuf[1] + 3] = 0xCA; /* trailing signature */
1030
      rmod_inputbuf[rmod_inputbuf[1] + 3] = 0xCA; /* trailing signature */
1031
      rmod_inputbuf[rmod_inputbuf[1] + 4] = 0xFE; /* trailing signature */
1031
      rmod_inputbuf[rmod_inputbuf[1] + 4] = 0xFE; /* trailing signature */
1032
      /* copy it to local cmdline */
1032
      /* copy it to local cmdline */
1033
      if (rmod_inputbuf[1] != 0) _fmemcpy(cmdline, rmod_inputbuf + 2, rmod_inputbuf[1]);
1033
      if (rmod_inputbuf[1] != 0) _fmemcpy(cmdline, rmod_inputbuf + 2, rmod_inputbuf[1]);
1034
      cmdline[rmod_inputbuf[1]] = 0; /* zero-terminate local buff (original is '\r'-terminated) */
1034
      cmdline[rmod_inputbuf[1]] = 0; /* zero-terminate local buff (original is '\r'-terminated) */
1035
    }
1035
    }
1036
 
1036
 
1037
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
1037
    /* if nothing entered, loop again (but without appending an extra CR/LF) */
1038
    if (cmdline[0] == 0) goto SKIP_NEWLINE;
1038
    if (cmdline[0] == 0) goto SKIP_NEWLINE;
1039
 
1039
 
1040
    /* I jump here when I need to exec an initial command (/C or /K) */
1040
    /* I jump here when I need to exec an initial command (/C or /K) */
1041
    EXEC_CMDLINE:
1041
    EXEC_CMDLINE:
1042
 
1042
 
1043
    /* move pointer forward to skip over any leading spaces */
1043
    /* move pointer forward to skip over any leading spaces */
1044
    while (*cmdline == ' ') cmdline++;
1044
    while (*cmdline == ' ') cmdline++;
1045
 
1045
 
-
 
1046
    /* sanitize separators into spaces */
-
 
1047
    for (i = 0; cmdline[i] != 0; i++) {
-
 
1048
      switch (cmdline[i]) {
-
 
1049
        case '\t':
-
 
1050
          cmdline[i] = ' ';
-
 
1051
      }
-
 
1052
    }
-
 
1053
 
1046
    /* update rmod's ptr to COMPSPEC so it is always up to date */
1054
    /* update rmod's ptr to COMPSPEC so it is always up to date */
1047
    rmod_updatecomspecptr(rmod->rmodseg, *rmod_envseg);
1055
    rmod_updatecomspecptr(rmod->rmodseg, *rmod_envseg);
1048
 
1056
 
1049
    /* handle redirections (if any) */
1057
    /* handle redirections (if any) */
1050
    i = redir_parsecmd(&redirprops, cmdline, rmod->awaitingcmd, *rmod_envseg);
1058
    i = redir_parsecmd(&redirprops, cmdline, rmod->awaitingcmd, *rmod_envseg);
1051
    if (i != 0) {
1059
    if (i != 0) {
1052
      nls_outputnl_doserr(i);
1060
      nls_outputnl_doserr(i);
1053
      rmod->awaitingcmd[0] = 0;
1061
      rmod->awaitingcmd[0] = 0;
1054
      continue;
1062
      continue;
1055
    }
1063
    }
1056
 
1064
 
1057
    /* try matching (and executing) an internal command */
1065
    /* try matching (and executing) an internal command */
1058
    cmdres = cmd_process(rmod, *rmod_envseg, cmdline, BUFFER, sizeof(BUFFER), &redirprops, flags & DELETE_STDIN_FILE);
1066
    cmdres = cmd_process(rmod, *rmod_envseg, cmdline, BUFFER, sizeof(BUFFER), &redirprops, flags & DELETE_STDIN_FILE);
1059
    if ((cmdres == CMD_OK) || (cmdres == CMD_FAIL)) {
1067
    if ((cmdres == CMD_OK) || (cmdres == CMD_FAIL)) {
1060
      /* internal command executed */
1068
      /* internal command executed */
1061
    } else if (cmdres == CMD_CHANGED) { /* cmdline changed, needs to be reprocessed */
1069
    } else if (cmdres == CMD_CHANGED) { /* cmdline changed, needs to be reprocessed */
1062
      goto EXEC_CMDLINE;
1070
      goto EXEC_CMDLINE;
1063
    } else if (cmdres == CMD_CHANGED_BY_CALL) { /* cmdline changed *specifically* by CALL */
1071
    } else if (cmdres == CMD_CHANGED_BY_CALL) { /* cmdline changed *specifically* by CALL */
1064
      /* the distinction is important since it changes the way batch files are processed */
1072
      /* the distinction is important since it changes the way batch files are processed */
1065
      flags |= CALL_FLAG;
1073
      flags |= CALL_FLAG;
1066
      goto EXEC_CMDLINE;
1074
      goto EXEC_CMDLINE;
1067
    } else if (cmdres == CMD_NOTFOUND) {
1075
    } else if (cmdres == CMD_NOTFOUND) {
1068
      /* this was not an internal command, try matching an external command */
1076
      /* this was not an internal command, try matching an external command */
1069
      run_as_external(BUFFER, cmdline, *rmod_envseg, rmod, &redirprops, flags);
1077
      run_as_external(BUFFER, cmdline, *rmod_envseg, rmod, &redirprops, flags);
1070
 
1078
 
1071
      /* is it a newly launched BAT file? */
1079
      /* is it a newly launched BAT file? */
1072
      if ((rmod->bat != NULL) && (rmod->bat->nextline == 0)) goto SKIP_NEWLINE;
1080
      if ((rmod->bat != NULL) && (rmod->bat->nextline == 0)) goto SKIP_NEWLINE;
1073
      /* run_as_external() does not return on success, if I am still alive then
1081
      /* run_as_external() does not return on success, if I am still alive then
1074
       * external command failed to execute */
1082
       * external command failed to execute */
1075
      nls_outputnl(0,5); /* "Bad command or file name" */
1083
      nls_outputnl(0,5); /* "Bad command or file name" */
1076
    } else {
1084
    } else {
1077
      /* I should never ever land here */
1085
      /* I should never ever land here */
1078
      outputnl("INTERNAL ERR: INVALID CMDRES");
1086
      outputnl("INTERNAL ERR: INVALID CMDRES");
1079
    }
1087
    }
1080
 
1088
 
1081
    /* reset one-time only flags */
1089
    /* reset one-time only flags */
1082
    flags &= ~CALL_FLAG;
1090
    flags &= ~CALL_FLAG;
1083
    flags &= ~FLAG_STEPBYSTEP;
1091
    flags &= ~FLAG_STEPBYSTEP;
1084
 
1092
 
1085
    /* repeat unless /C was asked - but always finish running an ongoing batch
1093
    /* repeat unless /C was asked - but always finish running an ongoing batch
1086
     * file (otherwise only first BAT command would be executed with /C) */
1094
     * file (otherwise only first BAT command would be executed with /C) */
1087
  } while (((rmod->flags & FLAG_EXEC_AND_QUIT) == 0) || (rmod->bat != NULL) || (rmod->forloop != NULL));
1095
  } while (((rmod->flags & FLAG_EXEC_AND_QUIT) == 0) || (rmod->bat != NULL) || (rmod->forloop != NULL));
1088
 
1096
 
1089
  sayonara(rmod);
1097
  sayonara(rmod);
1090
  return(0);
1098
  return(0);
1091
}
1099
}
1092
 
1100