Subversion Repositories SvarDOS

Rev

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

Rev 508 Rev 514
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 "rmodinit.h"
39
#include "rmodinit.h"
40
#include "sayonara.h"
40
#include "sayonara.h"
41
 
41
 
42
 
42
 
43
struct cmd_funcparam {
43
struct cmd_funcparam {
44
  int argc;                 /* number of arguments */
44
  int argc;                 /* number of arguments */
45
  const char *argv[128];    /* pointers to each argument */
45
  const char *argv[128];    /* pointers to each argument */
46
  char argvbuf[256];        /* buffer that hold data pointed out by argv[] */
46
  char argvbuf[256];        /* buffer that hold data pointed out by argv[] */
47
  unsigned short env_seg;   /* segment of environment block */
47
  unsigned short env_seg;   /* segment of environment block */
48
  struct rmod_props far *rmod; /* rmod settings */
48
  struct rmod_props far *rmod; /* rmod settings */
49
  unsigned short argoffset; /* offset of cmdline where first argument starts */
49
  unsigned short argoffset; /* offset of cmdline where first argument starts */
50
  const char *cmdline;      /* original cmdline (terminated by a NULL) */
50
  const char *cmdline;      /* original cmdline (terminated by a NULL) */
51
  unsigned short BUFFERSZ;  /* avail space in BUFFER */
51
  unsigned short BUFFERSZ;  /* avail space in BUFFER */
52
  char BUFFER[1];           /* a buffer for whatever is needed (must be last) */
52
  char BUFFER[1];           /* a buffer for whatever is needed (must be last) */
53
};
53
};
54
 
54
 
55
/* scans argv for the presence of a "/?" parameter. returns 1 if found, 0 otherwise */
55
/* scans argv for the presence of a "/?" parameter. returns 1 if found, 0 otherwise */
56
static int cmd_ishlp(const struct cmd_funcparam *p) {
56
static int cmd_ishlp(const struct cmd_funcparam *p) {
57
  int i;
57
  int i;
58
  for (i = 0; i < p->argc; i++) {
58
  for (i = 0; i < p->argc; i++) {
59
    if ((p->argv[i][0] == '/') && (p->argv[i][1] == '?')) return(1);
59
    if ((p->argv[i][0] == '/') && (p->argv[i][1] == '?')) return(1);
60
  }
60
  }
61
  return(0);
61
  return(0);
62
}
62
}
63
 
63
 
64
#include "cmd/_notimpl.c"
64
#include "cmd/_notimpl.c"
65
#include "cmd/break.c"
65
#include "cmd/break.c"
66
#include "cmd/cd.c"
66
#include "cmd/cd.c"
67
#include "cmd/chcp.c"
67
#include "cmd/chcp.c"
68
#include "cmd/cls.c"
68
#include "cmd/cls.c"
69
#include "cmd/copy.c"
69
#include "cmd/copy.c"
70
#include "cmd/date.c"
70
#include "cmd/date.c"
71
#include "cmd/del.c"
71
#include "cmd/del.c"
72
#include "cmd/vol.c"     /* must be included before dir.c due to dependency */
72
#include "cmd/vol.c"     /* must be included before dir.c due to dependency */
73
#include "cmd/dir.c"
73
#include "cmd/dir.c"
74
#include "cmd/echo.c"
74
#include "cmd/echo.c"
75
#include "cmd/exit.c"
75
#include "cmd/exit.c"
76
#include "cmd/mkdir.c"
76
#include "cmd/mkdir.c"
77
#include "cmd/path.c"
77
#include "cmd/path.c"
78
#include "cmd/pause.c"
78
#include "cmd/pause.c"
79
#include "cmd/prompt.c"
79
#include "cmd/prompt.c"
80
#include "cmd/rem.c"
80
#include "cmd/rem.c"
81
#include "cmd/rename.c"
81
#include "cmd/rename.c"
82
#include "cmd/rmdir.c"
82
#include "cmd/rmdir.c"
83
#include "cmd/set.c"
83
#include "cmd/set.c"
-
 
84
#include "cmd/shift.c"
84
#include "cmd/time.c"
85
#include "cmd/time.c"
85
#include "cmd/type.c"
86
#include "cmd/type.c"
86
#include "cmd/ver.c"
87
#include "cmd/ver.c"
87
#include "cmd/verify.c"
88
#include "cmd/verify.c"
88
 
89
 
89
#include "cmd.h"
90
#include "cmd.h"
90
 
91
 
91
 
92
 
92
struct CMD_ID {
93
struct CMD_ID {
93
  const char *cmd;
94
  const char *cmd;
94
  int (*func_ptr)(struct cmd_funcparam *); /* pointer to handling function */
95
  int (*func_ptr)(struct cmd_funcparam *); /* pointer to handling function */
95
};
96
};
96
 
97
 
97
const struct CMD_ID INTERNAL_CMDS[] = {
98
const struct CMD_ID INTERNAL_CMDS[] = {
98
  {"BREAK",   cmd_break},
99
  {"BREAK",   cmd_break},
99
  {"CD",      cmd_cd},
100
  {"CD",      cmd_cd},
100
  {"CHCP",    cmd_chcp},
101
  {"CHCP",    cmd_chcp},
101
  {"CHDIR",   cmd_cd},
102
  {"CHDIR",   cmd_cd},
102
  {"CLS",     cmd_cls},
103
  {"CLS",     cmd_cls},
103
  {"COPY",    cmd_copy},
104
  {"COPY",    cmd_copy},
104
  {"CTTY",    cmd_notimpl},
105
  {"CTTY",    cmd_notimpl},
105
  {"DATE",    cmd_date},
106
  {"DATE",    cmd_date},
106
  {"DEL",     cmd_del},
107
  {"DEL",     cmd_del},
107
  {"DIR",     cmd_dir},
108
  {"DIR",     cmd_dir},
108
  {"ECHO",    cmd_echo},
109
  {"ECHO",    cmd_echo},
109
  {"ERASE",   cmd_del},
110
  {"ERASE",   cmd_del},
110
  {"EXIT",    cmd_exit},
111
  {"EXIT",    cmd_exit},
111
  {"LH",      cmd_notimpl},
112
  {"LH",      cmd_notimpl},
112
  {"LOADHIGH",cmd_notimpl},
113
  {"LOADHIGH",cmd_notimpl},
113
  {"MD",      cmd_mkdir},
114
  {"MD",      cmd_mkdir},
114
  {"MKDIR",   cmd_mkdir},
115
  {"MKDIR",   cmd_mkdir},
115
  {"PAUSE",   cmd_pause},
116
  {"PAUSE",   cmd_pause},
116
  {"PATH",    cmd_path},
117
  {"PATH",    cmd_path},
117
  {"PROMPT",  cmd_prompt},
118
  {"PROMPT",  cmd_prompt},
118
  {"RD",      cmd_rmdir},
119
  {"RD",      cmd_rmdir},
119
  {"REM",     cmd_rem},
120
  {"REM",     cmd_rem},
120
  {"REN",     cmd_rename},
121
  {"REN",     cmd_rename},
121
  {"RENAME",  cmd_rename},
122
  {"RENAME",  cmd_rename},
122
  {"RMDIR",   cmd_rmdir},
123
  {"RMDIR",   cmd_rmdir},
123
  {"SET",     cmd_set},
124
  {"SET",     cmd_set},
-
 
125
  {"SHIFT",   cmd_shift},
124
  {"TIME",    cmd_time},
126
  {"TIME",    cmd_time},
125
  {"TYPE",    cmd_type},
127
  {"TYPE",    cmd_type},
126
  {"VER",     cmd_ver},
128
  {"VER",     cmd_ver},
127
  {"VERIFY",  cmd_verify},
129
  {"VERIFY",  cmd_verify},
128
  {"VOL",     cmd_vol},
130
  {"VOL",     cmd_vol},
129
  {NULL,      NULL}
131
  {NULL,      NULL}
130
};
132
};
131
 
133
 
132
 
134
 
133
/* NULL if cmdline is not matching an internal command, otherwise returns a
135
/* NULL if cmdline is not matching an internal command, otherwise returns a
134
 * pointer to a CMD_ID struct */
136
 * pointer to a CMD_ID struct */
135
static const struct CMD_ID *cmd_match(const char *cmdline, unsigned short *argoffset) {
137
static const struct CMD_ID *cmd_match(const char *cmdline, unsigned short *argoffset) {
136
  unsigned short i;
138
  unsigned short i;
137
  char buff[10];
139
  char buff[10];
138
 
140
 
139
  /* copy command to buffer, until space, NULL, tab, return, dot, slash or backslash */
141
  /* copy command to buffer, until space, NULL, tab, return, dot, slash or backslash */
140
  for (i = 0; i < 9; i++) {
142
  for (i = 0; i < 9; i++) {
141
    if (cmdline[i] == ' ') break;
143
    if (cmdline[i] == ' ') break;
142
    if (cmdline[i] == 0) break;
144
    if (cmdline[i] == 0) break;
143
    if (cmdline[i] == '\t') break;
145
    if (cmdline[i] == '\t') break;
144
    if (cmdline[i] == '\r') break;
146
    if (cmdline[i] == '\r') break;
145
    if (cmdline[i] == '.') break;
147
    if (cmdline[i] == '.') break;
146
    if (cmdline[i] == '/') break;
148
    if (cmdline[i] == '/') break;
147
    if (cmdline[i] == '\\') break;
149
    if (cmdline[i] == '\\') break;
148
    buff[i] = cmdline[i];
150
    buff[i] = cmdline[i];
149
  }
151
  }
150
  buff[i] = 0;
152
  buff[i] = 0;
151
 
153
 
152
  /* advance to nearest non-space to find where arguments start */
154
  /* advance to nearest non-space to find where arguments start */
153
  while (cmdline[i] == ' ') i++;
155
  while (cmdline[i] == ' ') i++;
154
  *argoffset = i;
156
  *argoffset = i;
155
 
157
 
156
  /* try matching an internal command */
158
  /* try matching an internal command */
157
  for (i = 0; INTERNAL_CMDS[i].cmd != NULL; i++) {
159
  for (i = 0; INTERNAL_CMDS[i].cmd != NULL; i++) {
158
    /*printf("imatch(%s,%s)\r\n", buff, INTERNAL_CMDS[i].cmd); */
160
    /*printf("imatch(%s,%s)\r\n", buff, INTERNAL_CMDS[i].cmd); */
159
    if (imatch(buff, INTERNAL_CMDS[i].cmd)) {
161
    if (imatch(buff, INTERNAL_CMDS[i].cmd)) {
160
      /*printf("match cmd i=%u (buff=%s)\r\n", i, buff);*/
162
      /*printf("match cmd i=%u (buff=%s)\r\n", i, buff);*/
161
      return(&(INTERNAL_CMDS[i]));
163
      return(&(INTERNAL_CMDS[i]));
162
    }
164
    }
163
  }
165
  }
164
 
166
 
165
  return(NULL); /* command is not recognized as internal */
167
  return(NULL); /* command is not recognized as internal */
166
}
168
}
167
 
169
 
168
 
170
 
169
/* explodes a command into an array of arguments where last arg is NULL.
171
/* explodes a command into an array of arguments where last arg is NULL.
170
 * if argvlist is not NULL, it will be filled with pointers that point to buff
172
 * if argvlist is not NULL, it will be filled with pointers that point to buff
171
 * locations. buff is filled with all the arguments, each argument being
173
 * locations. buff is filled with all the arguments, each argument being
172
 * zero-separated. buff is terminated with an empty argument to mark the end
174
 * zero-separated. buff is terminated with an empty argument to mark the end
173
 * of arguments.
175
 * of arguments.
174
 * returns number of args */
176
 * returns number of args */
175
unsigned short cmd_explode(char *buff, const char far *s, char const **argvlist) {
177
unsigned short cmd_explode(char *buff, const char far *s, char const **argvlist) {
176
  int si = 0, argc = 0, i = 0;
178
  int si = 0, argc = 0, i = 0;
177
  for (;;) {
179
  for (;;) {
178
    /* skip to next non-space character */
180
    /* skip to next non-space character */
179
    while (s[si] == ' ') si++;
181
    while (s[si] == ' ') si++;
180
    /* end of string? */
182
    /* end of string? */
181
    if (s[si] == 0) break;
183
    if (s[si] == 0) break;
182
    /* set argv ptr */
184
    /* set argv ptr */
183
    if (argvlist) argvlist[argc] = buff + i;
185
    if (argvlist) argvlist[argc] = buff + i;
184
    argc++;
186
    argc++;
185
    /* find next arg delimiter (spc, null, slash or plus) while copying arg to local buffer */
187
    /* find next arg delimiter (spc, null, slash or plus) while copying arg to local buffer */
186
    do {
188
    do {
187
      buff[i++] = s[si++];
189
      buff[i++] = s[si++];
188
    } while (s[si] != ' ' && s[si] != 0 && s[si] != '/' && s[si] != '+');
190
    } while (s[si] != ' ' && s[si] != 0 && s[si] != '/' && s[si] != '+');
189
    buff[i++] = 0;
191
    buff[i++] = 0;
190
    /* is this end of string? */
192
    /* is this end of string? */
191
    if (s[si] == 0) break;
193
    if (s[si] == 0) break;
192
  }
194
  }
193
  buff[i] = 0; /* terminate with one extra zero to tell "this is the end of list" */
195
  buff[i] = 0; /* terminate with one extra zero to tell "this is the end of list" */
194
  if (argvlist) argvlist[argc] = NULL;
196
  if (argvlist) argvlist[argc] = NULL;
195
  return(argc);
197
  return(argc);
196
}
198
}
197
 
199
 
198
 
200
 
199
int cmd_process(struct rmod_props far *rmod, unsigned short env_seg, const char *cmdline, void *BUFFER, unsigned short BUFFERSZ) {
201
int cmd_process(struct rmod_props far *rmod, unsigned short env_seg, const char *cmdline, void *BUFFER, unsigned short BUFFERSZ) {
200
  const struct CMD_ID *cmdptr;
202
  const struct CMD_ID *cmdptr;
201
  unsigned short argoffset;
203
  unsigned short argoffset;
202
  struct cmd_funcparam *p = (void *)BUFFER;
204
  struct cmd_funcparam *p = (void *)BUFFER;
203
  p->BUFFERSZ = BUFFERSZ - sizeof(*p);
205
  p->BUFFERSZ = BUFFERSZ - sizeof(*p);
204
 
206
 
205
  /* special case: is this a drive change? (like "E:") */
207
  /* special case: is this a drive change? (like "E:") */
206
  if ((cmdline[0] != 0) && (cmdline[1] == ':') && ((cmdline[2] == ' ') || (cmdline[2] == 0))) {
208
  if ((cmdline[0] != 0) && (cmdline[1] == ':') && ((cmdline[2] == ' ') || (cmdline[2] == 0))) {
207
    if (((cmdline[0] >= 'a') && (cmdline[0] <= 'z')) || ((cmdline[0] >= 'A') && (cmdline[0] <= 'Z'))) {
209
    if (((cmdline[0] >= 'a') && (cmdline[0] <= 'z')) || ((cmdline[0] >= 'A') && (cmdline[0] <= 'Z'))) {
208
      unsigned char drive = cmdline[0];
210
      unsigned char drive = cmdline[0];
209
      unsigned char curdrive = 0;
211
      unsigned char curdrive = 0;
210
      if (drive >= 'a') {
212
      if (drive >= 'a') {
211
        drive -= 'a';
213
        drive -= 'a';
212
      } else {
214
      } else {
213
        drive -= 'A';
215
        drive -= 'A';
214
      }
216
      }
215
      _asm {
217
      _asm {
216
        push ax
218
        push ax
217
        push dx
219
        push dx
218
        mov ah, 0x0e     /* DOS 1+ - SELECT DEFAULT DRIVE */
220
        mov ah, 0x0e     /* DOS 1+ - SELECT DEFAULT DRIVE */
219
        mov dl, drive    /* DL = new default drive (00h = A:, 01h = B:, etc) */
221
        mov dl, drive    /* DL = new default drive (00h = A:, 01h = B:, etc) */
220
        int 0x21
222
        int 0x21
221
        mov ah, 0x19     /* DOS 1+ - GET CURRENT DRIVE */
223
        mov ah, 0x19     /* DOS 1+ - GET CURRENT DRIVE */
222
        int 0x21
224
        int 0x21
223
        mov curdrive, al /* cur drive (0=A, 1=B, etc) */
225
        mov curdrive, al /* cur drive (0=A, 1=B, etc) */
224
        pop dx
226
        pop dx
225
        pop ax
227
        pop ax
226
      }
228
      }
227
      if (curdrive != drive) puts(doserr(0x0f));
229
      if (curdrive != drive) puts(doserr(0x0f));
228
      return(-1);
230
      return(-1);
229
    }
231
    }
230
  }
232
  }
231
 
233
 
232
  /* try matching an internal command */
234
  /* try matching an internal command */
233
  cmdptr = cmd_match(cmdline, &argoffset);
235
  cmdptr = cmd_match(cmdline, &argoffset);
234
  if (cmdptr == NULL) return(-2); /* command is not recognized as internal */
236
  if (cmdptr == NULL) return(-2); /* command is not recognized as internal */
235
 
237
 
236
  /* printf("recognized internal command: '%s', tail of command at offset %u\r\n", cmdptr->cmd, argoffset); */
238
  /* printf("recognized internal command: '%s', tail of command at offset %u\r\n", cmdptr->cmd, argoffset); */
237
 
239
 
238
  /* prepare function parameters and feed it to the cmd handling function */
240
  /* prepare function parameters and feed it to the cmd handling function */
239
  p->argc = cmd_explode(p->argvbuf, cmdline + argoffset, p->argv);
241
  p->argc = cmd_explode(p->argvbuf, cmdline + argoffset, p->argv);
240
  p->env_seg = env_seg;
242
  p->env_seg = env_seg;
241
  p->rmod = rmod;
243
  p->rmod = rmod;
242
  p->argoffset = argoffset;
244
  p->argoffset = argoffset;
243
  p->cmdline = cmdline;
245
  p->cmdline = cmdline;
244
 
246
 
245
  return((cmdptr->func_ptr)(p));
247
  return((cmdptr->func_ptr)(p));
246
}
248
}
247
 
249