Subversion Repositories SvarDOS

Rev

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

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