Subversion Repositories SvarDOS

Rev

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

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