Subversion Repositories SvarDOS

Rev

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

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