Subversion Repositories SvarDOS

Rev

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

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