Subversion Repositories SvarDOS

Rev

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

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