Subversion Repositories SvarDOS

Rev

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

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