Subversion Repositories SvarDOS

Rev

Rev 532 | Rev 538 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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