Subversion Repositories SvarDOS

Rev

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

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