Subversion Repositories SvarDOS

Rev

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

Rev 408 Rev 421
-
 
1
/* This file is part of the SvarCOM project and is published under the terms
-
 
2
 * of the MIT license.
-
 
3
 *
-
 
4
 * Copyright (C) 2021 Mateusz Viste
-
 
5
 *
-
 
6
 * Permission is hereby granted, free of charge, to any person obtaining a
-
 
7
 * copy of this software and associated documentation files (the "Software"),
-
 
8
 * to deal in the Software without restriction, including without limitation
-
 
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
-
 
11
 * Software is furnished to do so, subject to the following conditions:
-
 
12
 *
-
 
13
 * The above copyright notice and this permission notice shall be included in
-
 
14
 * all copies or substantial portions of the Software.
-
 
15
 *
-
 
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,
-
 
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
-
 
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
-
 
22
 * DEALINGS IN THE SOFTWARE.
-
 
23
 */
-
 
24
 
1
/* entry point for internal commands
25
/* entry point for internal commands
2
 * matches internal commands and executes them
26
 * matches internal commands and executes them
3
 * returns -1 or exit code if processed
27
 * returns -1 or exit code if processed
4
 * returns -2 if command unrecognized */
28
 * returns -2 if command unrecognized
-
 
29
 */
5
 
30
 
6
#include <i86.h>
31
#include <i86.h>
7
#include <stdio.h>
32
#include <stdio.h>
8
#include <stdlib.h>
33
#include <stdlib.h>
9
#include <string.h>
34
#include <string.h>
10
 
35
 
11
#include "doserr.h"
36
#include "doserr.h"
12
#include "env.h"
37
#include "env.h"
13
#include "helpers.h"
38
#include "helpers.h"
14
#include "rmodinit.h"
39
#include "rmodinit.h"
15
 
40
 
16
#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 */
17
 
42
 
18
struct cmd_funcparam {
43
struct cmd_funcparam {
19
  int argc;                 /* number of arguments */
44
  int argc;                 /* number of arguments */
20
  const char *argv[256];    /* pointers to each argument */
45
  const char *argv[256];    /* pointers to each argument */
21
  unsigned short env_seg;   /* segment of environment block */
46
  unsigned short env_seg;   /* segment of environment block */
22
  unsigned short rmod_seg;  /* segment of the resident module */
47
  unsigned short rmod_seg;  /* segment of the resident module */
23
  unsigned short argoffset; /* offset of cmdline where first argument starts */
48
  unsigned short argoffset; /* offset of cmdline where first argument starts */
24
  const char far *cmdline;  /* original cmdline (terminated by a NULL) */
49
  const char far *cmdline;  /* original cmdline (terminated by a NULL) */
25
  char BUFFER[BUFFER_SIZE]; /* a buffer for whatever is needed */
50
  char BUFFER[BUFFER_SIZE]; /* a buffer for whatever is needed */
26
};
51
};
27
 
52
 
28
/* 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 */
29
static int cmd_ishlp(const struct cmd_funcparam *p) {
54
static int cmd_ishlp(const struct cmd_funcparam *p) {
30
  int i;
55
  int i;
31
  for (i = 0; i < p->argc; i++) {
56
  for (i = 0; i < p->argc; i++) {
32
    if ((p->argv[i][0] == '/') && (p->argv[i][1] == '?')) return(1);
57
    if ((p->argv[i][0] == '/') && (p->argv[i][1] == '?')) return(1);
33
  }
58
  }
34
  return(0);
59
  return(0);
35
}
60
}
36
 
61
 
37
#include "cmd/_notimpl.c"
62
#include "cmd/_notimpl.c"
38
#include "cmd/break.c"
63
#include "cmd/break.c"
39
#include "cmd/cd.c"
64
#include "cmd/cd.c"
40
#include "cmd/chcp.c"
65
#include "cmd/chcp.c"
41
#include "cmd/cls.c"
66
#include "cmd/cls.c"
42
#include "cmd/copy.c"
67
#include "cmd/copy.c"
43
#include "cmd/del.c"
68
#include "cmd/del.c"
44
#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 */
45
#include "cmd/dir.c"
70
#include "cmd/dir.c"
46
#include "cmd/echo.c"
71
#include "cmd/echo.c"
47
#include "cmd/exit.c"
72
#include "cmd/exit.c"
48
#include "cmd/mkdir.c"
73
#include "cmd/mkdir.c"
49
#include "cmd/path.c"
74
#include "cmd/path.c"
50
#include "cmd/pause.c"
75
#include "cmd/pause.c"
51
#include "cmd/prompt.c"
76
#include "cmd/prompt.c"
52
#include "cmd/rem.c"
77
#include "cmd/rem.c"
53
#include "cmd/rename.c"
78
#include "cmd/rename.c"
54
#include "cmd/rmdir.c"
79
#include "cmd/rmdir.c"
55
#include "cmd/set.c"
80
#include "cmd/set.c"
56
#include "cmd/type.c"
81
#include "cmd/type.c"
57
#include "cmd/ver.c"
82
#include "cmd/ver.c"
58
#include "cmd/verify.c"
83
#include "cmd/verify.c"
59
 
84
 
60
#include "cmd.h"
85
#include "cmd.h"
61
 
86
 
62
 
87
 
63
struct CMD_ID {
88
struct CMD_ID {
64
  const char *cmd;
89
  const char *cmd;
65
  int (*func_ptr)(struct cmd_funcparam *); /* pointer to handling function */
90
  int (*func_ptr)(struct cmd_funcparam *); /* pointer to handling function */
66
};
91
};
67
 
92
 
68
const struct CMD_ID INTERNAL_CMDS[] = {
93
const struct CMD_ID INTERNAL_CMDS[] = {
69
  {"BREAK",   cmd_break},
94
  {"BREAK",   cmd_break},
70
  {"CD",      cmd_cd},
95
  {"CD",      cmd_cd},
71
  {"CHCP",    cmd_chcp},
96
  {"CHCP",    cmd_chcp},
72
  {"CHDIR",   cmd_cd},
97
  {"CHDIR",   cmd_cd},
73
  {"CLS",     cmd_cls},
98
  {"CLS",     cmd_cls},
74
  {"COPY",    cmd_copy},
99
  {"COPY",    cmd_copy},
75
  {"CTTY",    cmd_notimpl},
100
  {"CTTY",    cmd_notimpl},
76
  {"DATE",    cmd_notimpl},
101
  {"DATE",    cmd_notimpl},
77
  {"DEL",     cmd_del},
102
  {"DEL",     cmd_del},
78
  {"DIR",     cmd_dir},
103
  {"DIR",     cmd_dir},
79
  {"ECHO",    cmd_echo},
104
  {"ECHO",    cmd_echo},
80
  {"ERASE",   cmd_del},
105
  {"ERASE",   cmd_del},
81
  {"EXIT",    cmd_exit},
106
  {"EXIT",    cmd_exit},
82
  {"LH",      cmd_notimpl},
107
  {"LH",      cmd_notimpl},
83
  {"LOADHIGH",cmd_notimpl},
108
  {"LOADHIGH",cmd_notimpl},
84
  {"MD",      cmd_mkdir},
109
  {"MD",      cmd_mkdir},
85
  {"MKDIR",   cmd_mkdir},
110
  {"MKDIR",   cmd_mkdir},
86
  {"PAUSE",   cmd_pause},
111
  {"PAUSE",   cmd_pause},
87
  {"PATH",    cmd_path},
112
  {"PATH",    cmd_path},
88
  {"PROMPT",  cmd_prompt},
113
  {"PROMPT",  cmd_prompt},
89
  {"RD",      cmd_rmdir},
114
  {"RD",      cmd_rmdir},
90
  {"REM",     cmd_rem},
115
  {"REM",     cmd_rem},
91
  {"REN",     cmd_rename},
116
  {"REN",     cmd_rename},
92
  {"RENAME",  cmd_rename},
117
  {"RENAME",  cmd_rename},
93
  {"RMDIR",   cmd_rmdir},
118
  {"RMDIR",   cmd_rmdir},
94
  {"SET",     cmd_set},
119
  {"SET",     cmd_set},
95
  {"TIME",    cmd_notimpl},
120
  {"TIME",    cmd_notimpl},
96
  {"TYPE",    cmd_type},
121
  {"TYPE",    cmd_type},
97
  {"VER",     cmd_ver},
122
  {"VER",     cmd_ver},
98
  {"VERIFY",  cmd_verify},
123
  {"VERIFY",  cmd_verify},
99
  {"VOL",     cmd_vol},
124
  {"VOL",     cmd_vol},
100
  {NULL,      NULL}
125
  {NULL,      NULL}
101
};
126
};
102
 
127
 
103
 
128
 
104
/* NULL if cmdline is not matching an internal command, otherwise returns a
129
/* NULL if cmdline is not matching an internal command, otherwise returns a
105
 * pointer to a CMD_ID struct */
130
 * pointer to a CMD_ID struct */
106
static const struct CMD_ID *cmd_match(const char far *cmdline, unsigned short *argoffset) {
131
static const struct CMD_ID *cmd_match(const char far *cmdline, unsigned short *argoffset) {
107
  unsigned short i;
132
  unsigned short i;
108
  char buff[10];
133
  char buff[10];
109
 
134
 
110
  /* copy command to buffer, until space, NULL, tab, return, dot, slash or backslash */
135
  /* copy command to buffer, until space, NULL, tab, return, dot, slash or backslash */
111
  for (i = 0; i < 9; i++) {
136
  for (i = 0; i < 9; i++) {
112
    if (cmdline[i] == ' ') break;
137
    if (cmdline[i] == ' ') break;
113
    if (cmdline[i] == 0) break;
138
    if (cmdline[i] == 0) break;
114
    if (cmdline[i] == '\t') break;
139
    if (cmdline[i] == '\t') break;
115
    if (cmdline[i] == '\r') break;
140
    if (cmdline[i] == '\r') break;
116
    if (cmdline[i] == '.') break;
141
    if (cmdline[i] == '.') break;
117
    if (cmdline[i] == '/') break;
142
    if (cmdline[i] == '/') break;
118
    if (cmdline[i] == '\\') break;
143
    if (cmdline[i] == '\\') break;
119
    buff[i] = cmdline[i];
144
    buff[i] = cmdline[i];
120
  }
145
  }
121
  buff[i] = 0;
146
  buff[i] = 0;
122
 
147
 
123
  /* advance to nearest non-space to find where arguments start */
148
  /* advance to nearest non-space to find where arguments start */
124
  while (cmdline[i] == ' ') i++;
149
  while (cmdline[i] == ' ') i++;
125
  *argoffset = i;
150
  *argoffset = i;
126
 
151
 
127
  /* try matching an internal command */
152
  /* try matching an internal command */
128
  for (i = 0; INTERNAL_CMDS[i].cmd != NULL; i++) {
153
  for (i = 0; INTERNAL_CMDS[i].cmd != NULL; i++) {
129
    /*printf("imatch(%s,%s)\r\n", buff, INTERNAL_CMDS[i].cmd); */
154
    /*printf("imatch(%s,%s)\r\n", buff, INTERNAL_CMDS[i].cmd); */
130
    if (imatch(buff, INTERNAL_CMDS[i].cmd)) {
155
    if (imatch(buff, INTERNAL_CMDS[i].cmd)) {
131
      /*printf("match cmd i=%u (buff=%s)\r\n", i, buff);*/
156
      /*printf("match cmd i=%u (buff=%s)\r\n", i, buff);*/
132
      return(&(INTERNAL_CMDS[i]));
157
      return(&(INTERNAL_CMDS[i]));
133
    }
158
    }
134
  }
159
  }
135
 
160
 
136
  return(NULL); /* command is not recognized as internal */
161
  return(NULL); /* command is not recognized as internal */
137
}
162
}
138
 
163
 
139
 
164
 
140
/* explodes a command into an array of arguments where last arg is NULL
165
/* explodes a command into an array of arguments where last arg is NULL
141
 * returns number of args */
166
 * returns number of args */
142
unsigned short cmd_explode(char *buff, const char far *s, char const **argvlist) {
167
unsigned short cmd_explode(char *buff, const char far *s, char const **argvlist) {
143
  int si = 0, argc = 0, i = 0;
168
  int si = 0, argc = 0, i = 0;
144
  for (;;) {
169
  for (;;) {
145
    /* skip to next non-space character */
170
    /* skip to next non-space character */
146
    while (s[si] == ' ') si++;
171
    while (s[si] == ' ') si++;
147
    /* end of string? */
172
    /* end of string? */
148
    if (s[si] == 0) break;
173
    if (s[si] == 0) break;
149
    /* set argv ptr */
174
    /* set argv ptr */
150
    argvlist[argc++] = buff + i;
175
    argvlist[argc++] = buff + i;
151
    /* find next arg delimiter (spc, null, slash or plus) while copying arg to local buffer */
176
    /* find next arg delimiter (spc, null, slash or plus) while copying arg to local buffer */
152
    do {
177
    do {
153
      buff[i++] = s[si++];
178
      buff[i++] = s[si++];
154
    } while (s[si] != ' ' && s[si] != 0 && s[si] != '/' && s[si] != '+');
179
    } while (s[si] != ' ' && s[si] != 0 && s[si] != '/' && s[si] != '+');
155
    buff[i++] = 0;
180
    buff[i++] = 0;
156
    /* is this end of string? */
181
    /* is this end of string? */
157
    if (s[si] == 0) break;
182
    if (s[si] == 0) break;
158
  }
183
  }
159
  argvlist[argc] = NULL;
184
  argvlist[argc] = NULL;
160
  return(argc);
185
  return(argc);
161
}
186
}
162
 
187
 
163
 
188
 
164
int cmd_process(unsigned short rmod_seg, unsigned short env_seg, const char far *cmdline, char *BUFFER) {
189
int cmd_process(unsigned short rmod_seg, unsigned short env_seg, const char far *cmdline, char *BUFFER) {
165
  const struct CMD_ID *cmdptr;
190
  const struct CMD_ID *cmdptr;
166
  unsigned short argoffset;
191
  unsigned short argoffset;
167
  struct cmd_funcparam *p = (void *)BUFFER;
192
  struct cmd_funcparam *p = (void *)BUFFER;
168
 
193
 
169
  /* special case: is this a drive change? (like "E:") */
194
  /* special case: is this a drive change? (like "E:") */
170
  if ((cmdline[0] != 0) && (cmdline[1] == ':') && ((cmdline[2] == ' ') || (cmdline[2] == 0))) {
195
  if ((cmdline[0] != 0) && (cmdline[1] == ':') && ((cmdline[2] == ' ') || (cmdline[2] == 0))) {
171
    if (((cmdline[0] >= 'a') && (cmdline[0] <= 'z')) || ((cmdline[0] >= 'A') && (cmdline[0] <= 'Z'))) {
196
    if (((cmdline[0] >= 'a') && (cmdline[0] <= 'z')) || ((cmdline[0] >= 'A') && (cmdline[0] <= 'Z'))) {
172
      unsigned char drive = cmdline[0];
197
      unsigned char drive = cmdline[0];
173
      unsigned char curdrive = 0;
198
      unsigned char curdrive = 0;
174
      if (drive >= 'a') {
199
      if (drive >= 'a') {
175
        drive -= 'a';
200
        drive -= 'a';
176
      } else {
201
      } else {
177
        drive -= 'A';
202
        drive -= 'A';
178
      }
203
      }
179
      _asm {
204
      _asm {
180
        push ax
205
        push ax
181
        push dx
206
        push dx
182
        mov ah, 0x0e     /* DOS 1+ - SELECT DEFAULT DRIVE */
207
        mov ah, 0x0e     /* DOS 1+ - SELECT DEFAULT DRIVE */
183
        mov dl, drive    /* DL = new default drive (00h = A:, 01h = B:, etc) */
208
        mov dl, drive    /* DL = new default drive (00h = A:, 01h = B:, etc) */
184
        int 0x21
209
        int 0x21
185
        mov ah, 0x19     /* DOS 1+ - GET CURRENT DRIVE */
210
        mov ah, 0x19     /* DOS 1+ - GET CURRENT DRIVE */
186
        int 0x21
211
        int 0x21
187
        mov curdrive, al /* cur drive (0=A, 1=B, etc) */
212
        mov curdrive, al /* cur drive (0=A, 1=B, etc) */
188
        pop dx
213
        pop dx
189
        pop ax
214
        pop ax
190
      }
215
      }
191
      if (curdrive != drive) puts(doserr(0x0f));
216
      if (curdrive != drive) puts(doserr(0x0f));
192
      return(-1);
217
      return(-1);
193
    }
218
    }
194
  }
219
  }
195
 
220
 
196
  /* try matching an internal command */
221
  /* try matching an internal command */
197
  cmdptr = cmd_match(cmdline, &argoffset);
222
  cmdptr = cmd_match(cmdline, &argoffset);
198
  if (cmdptr == NULL) return(-2); /* command is not recognized as internal */
223
  if (cmdptr == NULL) return(-2); /* command is not recognized as internal */
199
 
224
 
200
  /* printf("recognized internal command: '%s', tail of command at offset %u\r\n", cmdptr->cmd, argoffset); */
225
  /* printf("recognized internal command: '%s', tail of command at offset %u\r\n", cmdptr->cmd, argoffset); */
201
 
226
 
202
  /* prepare function parameters and feed it to the cmd handling function */
227
  /* prepare function parameters and feed it to the cmd handling function */
203
  p->argc = cmd_explode(BUFFER + sizeof(*p), cmdline + argoffset, p->argv);
228
  p->argc = cmd_explode(BUFFER + sizeof(*p), cmdline + argoffset, p->argv);
204
  p->env_seg = env_seg;
229
  p->env_seg = env_seg;
205
  p->rmod_seg = rmod_seg;
230
  p->rmod_seg = rmod_seg;
206
  p->argoffset = argoffset;
231
  p->argoffset = argoffset;
207
  p->cmdline = cmdline;
232
  p->cmdline = cmdline;
208
 
233
 
209
  return((cmdptr->func_ptr)(p));
234
  return((cmdptr->func_ptr)(p));
210
}
235
}
211
 
236