Subversion Repositories SvarDOS

Rev

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

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