Subversion Repositories SvarDOS

Rev

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

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