Subversion Repositories SvarDOS

Rev

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