Subversion Repositories SvarDOS

Rev

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