Subversion Repositories SvarDOS

Rev

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