Subversion Repositories SvarDOS

Rev

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

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