Subversion Repositories SvarDOS

Rev

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

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