Subversion Repositories SvarDOS

Rev

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

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