Subversion Repositories SvarDOS

Rev

Rev 371 | Rev 379 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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