Subversion Repositories SvarDOS

Rev

Rev 382 | Rev 386 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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