Subversion Repositories SvarDOS

Rev

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