Subversion Repositories SvarDOS

Rev

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

Rev 517 Rev 527
1
/* This file is part of the SvarCOM project and is published under the terms
1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021 Mateusz Viste
4
 * Copyright (C) 2021 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included in
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
14
 * all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 */
23
 */
24
 
24
 
25
/* entry point for internal commands
25
/* entry point for internal commands
26
 * matches internal commands and executes them
26
 * matches internal commands and executes them
27
 * returns -1 or exit code if processed
27
 * returns -1 or exit code if processed
28
 * returns -2 if command unrecognized
28
 * returns -2 if command unrecognized
29
 */
29
 */
30
 
30
 
31
#include <i86.h>
31
#include <i86.h>
32
#include <stdio.h>
32
#include <stdio.h>
33
#include <stdlib.h>
33
#include <stdlib.h>
34
#include <string.h>
34
#include <string.h>
35
 
35
 
36
#include "doserr.h"
36
#include "doserr.h"
37
#include "env.h"
37
#include "env.h"
38
#include "helpers.h"
38
#include "helpers.h"
39
#include "redir.h"
39
#include "redir.h"
40
#include "rmodinit.h"
40
#include "rmodinit.h"
41
#include "sayonara.h"
41
#include "sayonara.h"
42
 
42
 
43
 
43
 
44
struct cmd_funcparam {
44
struct cmd_funcparam {
45
  int argc;                 /* number of arguments */
45
  int argc;                 /* number of arguments */
46
  const char *argv[128];    /* pointers to each argument */
46
  const char *argv[128];    /* pointers to each argument */
47
  char argvbuf[256];        /* buffer that hold data pointed out by argv[] */
47
  char argvbuf[256];        /* buffer that hold data pointed out by argv[] */
48
  unsigned short env_seg;   /* segment of environment block */
48
  unsigned short env_seg;   /* segment of environment block */
49
  struct rmod_props far *rmod; /* rmod settings */
49
  struct rmod_props far *rmod; /* rmod settings */
50
  unsigned short argoffset; /* offset of cmdline where first argument starts */
50
  unsigned short argoffset; /* offset of cmdline where first argument starts */
51
  const char *cmdline;      /* original cmdline (terminated by a NULL) */
51
  const char *cmdline;      /* original cmdline (terminated by a NULL) */
52
  unsigned short BUFFERSZ;  /* avail space in BUFFER */
52
  unsigned short BUFFERSZ;  /* avail space in BUFFER */
53
  char BUFFER[1];           /* a buffer for whatever is needed (must be last) */
53
  char BUFFER[1];           /* a buffer for whatever is needed (must be last) */
54
};
54
};
55
 
55
 
56
/* scans argv for the presence of a "/?" parameter. returns 1 if found, 0 otherwise */
56
/* scans argv for the presence of a "/?" parameter. returns 1 if found, 0 otherwise */
57
static int cmd_ishlp(const struct cmd_funcparam *p) {
57
static int cmd_ishlp(const struct cmd_funcparam *p) {
58
  int i;
58
  int i;
59
  for (i = 0; i < p->argc; i++) {
59
  for (i = 0; i < p->argc; i++) {
60
    if ((p->argv[i][0] == '/') && (p->argv[i][1] == '?')) return(1);
60
    if ((p->argv[i][0] == '/') && (p->argv[i][1] == '?')) return(1);
61
  }
61
  }
62
  return(0);
62
  return(0);
63
}
63
}
64
 
64
 
65
#include "cmd/_notimpl.c"
65
#include "cmd/_notimpl.c"
66
#include "cmd/break.c"
66
#include "cmd/break.c"
67
#include "cmd/cd.c"
67
#include "cmd/cd.c"
68
#include "cmd/chcp.c"
68
#include "cmd/chcp.c"
69
#include "cmd/cls.c"
69
#include "cmd/cls.c"
70
#include "cmd/copy.c"
70
#include "cmd/copy.c"
71
#include "cmd/date.c"
71
#include "cmd/date.c"
72
#include "cmd/del.c"
72
#include "cmd/del.c"
73
#include "cmd/vol.c"     /* must be included before dir.c due to dependency */
73
#include "cmd/vol.c"     /* must be included before dir.c due to dependency */
74
#include "cmd/dir.c"
74
#include "cmd/dir.c"
75
#include "cmd/echo.c"
75
#include "cmd/echo.c"
76
#include "cmd/exit.c"
76
#include "cmd/exit.c"
77
#include "cmd/mkdir.c"
77
#include "cmd/mkdir.c"
78
#include "cmd/path.c"
78
#include "cmd/path.c"
79
#include "cmd/pause.c"
79
#include "cmd/pause.c"
80
#include "cmd/prompt.c"
80
#include "cmd/prompt.c"
81
#include "cmd/rem.c"
81
#include "cmd/rem.c"
82
#include "cmd/rename.c"
82
#include "cmd/rename.c"
83
#include "cmd/rmdir.c"
83
#include "cmd/rmdir.c"
84
#include "cmd/set.c"
84
#include "cmd/set.c"
85
#include "cmd/shift.c"
85
#include "cmd/shift.c"
86
#include "cmd/time.c"
86
#include "cmd/time.c"
87
#include "cmd/type.c"
87
#include "cmd/type.c"
88
#include "cmd/ver.c"
88
#include "cmd/ver.c"
89
#include "cmd/verify.c"
89
#include "cmd/verify.c"
90
 
90
 
91
#include "cmd.h"
91
#include "cmd.h"
92
 
92
 
93
 
93
 
94
struct CMD_ID {
94
struct CMD_ID {
95
  const char *cmd;
95
  const char *cmd;
96
  int (*func_ptr)(struct cmd_funcparam *); /* pointer to handling function */
96
  int (*func_ptr)(struct cmd_funcparam *); /* pointer to handling function */
97
};
97
};
98
 
98
 
99
const struct CMD_ID INTERNAL_CMDS[] = {
99
const struct CMD_ID INTERNAL_CMDS[] = {
100
  {"BREAK",   cmd_break},
100
  {"BREAK",   cmd_break},
101
  {"CD",      cmd_cd},
101
  {"CD",      cmd_cd},
102
  {"CHCP",    cmd_chcp},
102
  {"CHCP",    cmd_chcp},
103
  {"CHDIR",   cmd_cd},
103
  {"CHDIR",   cmd_cd},
104
  {"CLS",     cmd_cls},
104
  {"CLS",     cmd_cls},
105
  {"COPY",    cmd_copy},
105
  {"COPY",    cmd_copy},
106
  {"CTTY",    cmd_notimpl},
106
  {"CTTY",    cmd_notimpl},
107
  {"DATE",    cmd_date},
107
  {"DATE",    cmd_date},
108
  {"DEL",     cmd_del},
108
  {"DEL",     cmd_del},
109
  {"DIR",     cmd_dir},
109
  {"DIR",     cmd_dir},
110
  {"ECHO",    cmd_echo},
110
  {"ECHO",    cmd_echo},
111
  {"ERASE",   cmd_del},
111
  {"ERASE",   cmd_del},
112
  {"EXIT",    cmd_exit},
112
  {"EXIT",    cmd_exit},
113
  {"LH",      cmd_notimpl},
113
  {"LH",      cmd_notimpl},
114
  {"LOADHIGH",cmd_notimpl},
114
  {"LOADHIGH",cmd_notimpl},
115
  {"MD",      cmd_mkdir},
115
  {"MD",      cmd_mkdir},
116
  {"MKDIR",   cmd_mkdir},
116
  {"MKDIR",   cmd_mkdir},
117
  {"PAUSE",   cmd_pause},
117
  {"PAUSE",   cmd_pause},
118
  {"PATH",    cmd_path},
118
  {"PATH",    cmd_path},
119
  {"PROMPT",  cmd_prompt},
119
  {"PROMPT",  cmd_prompt},
120
  {"RD",      cmd_rmdir},
120
  {"RD",      cmd_rmdir},
121
  {"REM",     cmd_rem},
121
  {"REM",     cmd_rem},
122
  {"REN",     cmd_rename},
122
  {"REN",     cmd_rename},
123
  {"RENAME",  cmd_rename},
123
  {"RENAME",  cmd_rename},
124
  {"RMDIR",   cmd_rmdir},
124
  {"RMDIR",   cmd_rmdir},
125
  {"SET",     cmd_set},
125
  {"SET",     cmd_set},
126
  {"SHIFT",   cmd_shift},
126
  {"SHIFT",   cmd_shift},
127
  {"TIME",    cmd_time},
127
  {"TIME",    cmd_time},
128
  {"TYPE",    cmd_type},
128
  {"TYPE",    cmd_type},
129
  {"VER",     cmd_ver},
129
  {"VER",     cmd_ver},
130
  {"VERIFY",  cmd_verify},
130
  {"VERIFY",  cmd_verify},
131
  {"VOL",     cmd_vol},
131
  {"VOL",     cmd_vol},
132
  {NULL,      NULL}
132
  {NULL,      NULL}
133
};
133
};
134
 
134
 
135
 
135
 
136
/* NULL if cmdline is not matching an internal command, otherwise returns a
136
/* NULL if cmdline is not matching an internal command, otherwise returns a
137
 * pointer to a CMD_ID struct */
137
 * pointer to a CMD_ID struct */
138
static const struct CMD_ID *cmd_match(const char *cmdline, unsigned short *argoffset) {
138
static const struct CMD_ID *cmd_match(const char *cmdline, unsigned short *argoffset) {
139
  unsigned short i;
139
  unsigned short i;
140
  char buff[10];
140
  char buff[10];
141
 
141
 
142
  /* copy command to buffer, until space, NULL, tab, return, dot, slash or backslash */
142
  /* copy command to buffer, until space, NULL, tab, return, dot, slash or backslash */
143
  for (i = 0; i < 9; i++) {
143
  for (i = 0; i < 9; i++) {
144
    if (cmdline[i] == ' ') break;
144
    if (cmdline[i] == ' ') break;
145
    if (cmdline[i] == 0) break;
145
    if (cmdline[i] == 0) break;
146
    if (cmdline[i] == '\t') break;
146
    if (cmdline[i] == '\t') break;
147
    if (cmdline[i] == '\r') break;
147
    if (cmdline[i] == '\r') break;
148
    if (cmdline[i] == '.') break;
148
    if (cmdline[i] == '.') break;
149
    if (cmdline[i] == '/') break;
149
    if (cmdline[i] == '/') break;
150
    if (cmdline[i] == '\\') break;
150
    if (cmdline[i] == '\\') break;
151
    buff[i] = cmdline[i];
151
    buff[i] = cmdline[i];
152
  }
152
  }
153
  buff[i] = 0;
153
  buff[i] = 0;
154
 
154
 
155
  /* advance to nearest non-space to find where arguments start */
155
  /* advance to nearest non-space to find where arguments start */
156
  while (cmdline[i] == ' ') i++;
156
  while (cmdline[i] == ' ') i++;
157
  *argoffset = i;
157
  *argoffset = i;
158
 
158
 
159
  /* try matching an internal command */
159
  /* try matching an internal command */
160
  for (i = 0; INTERNAL_CMDS[i].cmd != NULL; i++) {
160
  for (i = 0; INTERNAL_CMDS[i].cmd != NULL; i++) {
161
    /*printf("imatch(%s,%s)\r\n", buff, INTERNAL_CMDS[i].cmd); */
161
    /*printf("imatch(%s,%s)\r\n", buff, INTERNAL_CMDS[i].cmd); */
162
    if (imatch(buff, INTERNAL_CMDS[i].cmd)) {
162
    if (imatch(buff, INTERNAL_CMDS[i].cmd)) {
163
      /*printf("match cmd i=%u (buff=%s)\r\n", i, buff);*/
163
      /*printf("match cmd i=%u (buff=%s)\r\n", i, buff);*/
164
      return(&(INTERNAL_CMDS[i]));
164
      return(&(INTERNAL_CMDS[i]));
165
    }
165
    }
166
  }
166
  }
167
 
167
 
168
  return(NULL); /* command is not recognized as internal */
168
  return(NULL); /* command is not recognized as internal */
169
}
169
}
170
 
170
 
171
 
171
 
172
/* explodes a command into an array of arguments where last arg is NULL.
172
/* explodes a command into an array of arguments where last arg is NULL.
173
 * if argvlist is not NULL, it will be filled with pointers that point to buff
173
 * if argvlist is not NULL, it will be filled with pointers that point to buff
174
 * locations. buff is filled with all the arguments, each argument being
174
 * locations. buff is filled with all the arguments, each argument being
175
 * zero-separated. buff is terminated with an empty argument to mark the end
175
 * zero-separated. buff is terminated with an empty argument to mark the end
176
 * of arguments.
176
 * of arguments.
177
 * returns number of args */
177
 * returns number of args */
178
unsigned short cmd_explode(char *buff, const char far *s, char const **argvlist) {
178
unsigned short cmd_explode(char *buff, const char far *s, char const **argvlist) {
179
  int si = 0, argc = 0, i = 0;
179
  int si = 0, argc = 0, i = 0;
180
  for (;;) {
180
  for (;;) {
181
    /* skip to next non-space character */
181
    /* skip to next non-space character */
182
    while (s[si] == ' ') si++;
182
    while (s[si] == ' ') si++;
183
    /* end of string? */
183
    /* end of string? */
184
    if (s[si] == 0) break;
184
    if (s[si] == 0) break;
185
    /* set argv ptr */
185
    /* set argv ptr */
186
    if (argvlist) argvlist[argc] = buff + i;
186
    if (argvlist) argvlist[argc] = buff + i;
187
    argc++;
187
    argc++;
188
    /* find next arg delimiter (spc, null, slash or plus) while copying arg to local buffer */
188
    /* find next arg delimiter (spc, null, slash or plus) while copying arg to local buffer */
189
    do {
189
    do {
190
      buff[i++] = s[si++];
190
      buff[i++] = s[si++];
191
    } while (s[si] != ' ' && s[si] != 0 && s[si] != '/' && s[si] != '+');
191
    } while (s[si] != ' ' && s[si] != 0 && s[si] != '/' && s[si] != '+');
192
    buff[i++] = 0;
192
    buff[i++] = 0;
193
    /* is this end of string? */
193
    /* is this end of string? */
194
    if (s[si] == 0) break;
194
    if (s[si] == 0) break;
195
  }
195
  }
196
  buff[i] = 0; /* terminate with one extra zero to tell "this is the end of list" */
196
  buff[i] = 0; /* terminate with one extra zero to tell "this is the end of list" */
197
  if (argvlist) argvlist[argc] = NULL;
197
  if (argvlist) argvlist[argc] = NULL;
198
  return(argc);
198
  return(argc);
199
}
199
}
200
 
200
 
201
 
201
 
202
int cmd_process(struct rmod_props far *rmod, unsigned short env_seg, const char *cmdline, void *BUFFER, unsigned short BUFFERSZ, const struct redir_data *redir) {
202
int cmd_process(struct rmod_props far *rmod, unsigned short env_seg, const char *cmdline, void *BUFFER, unsigned short BUFFERSZ, const struct redir_data *redir) {
203
  const struct CMD_ID *cmdptr;
203
  const struct CMD_ID *cmdptr;
204
  unsigned short argoffset;
204
  unsigned short argoffset;
205
  int cmdres;
205
  int cmdres;
206
  struct cmd_funcparam *p = (void *)BUFFER;
206
  struct cmd_funcparam *p = (void *)BUFFER;
207
  p->BUFFERSZ = BUFFERSZ - sizeof(*p);
207
  p->BUFFERSZ = BUFFERSZ - sizeof(*p);
208
 
208
 
209
  /* special case: is this a drive change? (like "E:") */
209
  /* special case: is this a drive change? (like "E:") */
210
  if ((cmdline[0] != 0) && (cmdline[1] == ':') && ((cmdline[2] == ' ') || (cmdline[2] == 0))) {
210
  if ((cmdline[0] != 0) && (cmdline[1] == ':') && ((cmdline[2] == ' ') || (cmdline[2] == 0))) {
211
    if (((cmdline[0] >= 'a') && (cmdline[0] <= 'z')) || ((cmdline[0] >= 'A') && (cmdline[0] <= 'Z'))) {
211
    if (((cmdline[0] >= 'a') && (cmdline[0] <= 'z')) || ((cmdline[0] >= 'A') && (cmdline[0] <= 'Z'))) {
212
      unsigned char drive = cmdline[0];
212
      unsigned char drive = cmdline[0];
213
      unsigned char curdrive = 0;
213
      unsigned char curdrive = 0;
214
      if (drive >= 'a') {
214
      if (drive >= 'a') {
215
        drive -= 'a';
215
        drive -= 'a';
216
      } else {
216
      } else {
217
        drive -= 'A';
217
        drive -= 'A';
218
      }
218
      }
219
      _asm {
219
      _asm {
220
        push ax
220
        push ax
221
        push dx
221
        push dx
222
        mov ah, 0x0e     /* DOS 1+ - SELECT DEFAULT DRIVE */
222
        mov ah, 0x0e     /* DOS 1+ - SELECT DEFAULT DRIVE */
223
        mov dl, drive    /* DL = new default drive (00h = A:, 01h = B:, etc) */
223
        mov dl, drive    /* DL = new default drive (00h = A:, 01h = B:, etc) */
224
        int 0x21
224
        int 0x21
225
        mov ah, 0x19     /* DOS 1+ - GET CURRENT DRIVE */
225
        mov ah, 0x19     /* DOS 1+ - GET CURRENT DRIVE */
226
        int 0x21
226
        int 0x21
227
        mov curdrive, al /* cur drive (0=A, 1=B, etc) */
227
        mov curdrive, al /* cur drive (0=A, 1=B, etc) */
228
        pop dx
228
        pop dx
229
        pop ax
229
        pop ax
230
      }
230
      }
231
      if (curdrive != drive) puts(doserr(0x0f));
231
      if (curdrive != drive) outputnl(doserr(0x0f));
232
      return(-1);
232
      return(-1);
233
    }
233
    }
234
  }
234
  }
235
 
235
 
236
  /* try matching an internal command */
236
  /* try matching an internal command */
237
  cmdptr = cmd_match(cmdline, &argoffset);
237
  cmdptr = cmd_match(cmdline, &argoffset);
238
  if (cmdptr == NULL) return(-2); /* command is not recognized as internal */
238
  if (cmdptr == NULL) return(-2); /* command is not recognized as internal */
239
 
239
 
240
  /* printf("recognized internal command: '%s', tail of command at offset %u\r\n", cmdptr->cmd, argoffset); */
240
  /* printf("recognized internal command: '%s', tail of command at offset %u\r\n", cmdptr->cmd, argoffset); */
241
 
241
 
242
  /* apply redirections (if any) */
242
  /* apply redirections (if any) */
243
  if (redir_apply(redir) != 0) return(-1);
243
  if (redir_apply(redir) != 0) return(-1);
244
 
244
 
245
  /* prepare function parameters and feed it to the cmd handling function */
245
  /* prepare function parameters and feed it to the cmd handling function */
246
  p->argc = cmd_explode(p->argvbuf, cmdline + argoffset, p->argv);
246
  p->argc = cmd_explode(p->argvbuf, cmdline + argoffset, p->argv);
247
  p->env_seg = env_seg;
247
  p->env_seg = env_seg;
248
  p->rmod = rmod;
248
  p->rmod = rmod;
249
  p->argoffset = argoffset;
249
  p->argoffset = argoffset;
250
  p->cmdline = cmdline;
250
  p->cmdline = cmdline;
251
 
251
 
252
  cmdres = (cmdptr->func_ptr)(p);
252
  cmdres = (cmdptr->func_ptr)(p);
253
 
253
 
254
  /* cancel redirections */
254
  /* cancel redirections */
255
  redir_revert();
255
  redir_revert();
256
 
256
 
257
  return(cmdres);
257
  return(cmdres);
258
}
258
}
259
 
259