Subversion Repositories SvarDOS

Rev

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