Subversion Repositories SvarDOS

Rev

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

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