Subversion Repositories SvarDOS

Rev

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

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