Subversion Repositories SvarDOS

Rev

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

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