Subversion Repositories SvarDOS

Rev

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