Subversion Repositories SvarDOS

Rev

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

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