Subversion Repositories SvarDOS

Rev

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