Subversion Repositories SvarDOS

Rev

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