Subversion Repositories SvarDOS

Rev

Rev 479 | Rev 519 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 479 Rev 517
Line 20... Line 20...
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 */
23
 */
24
 
24
 
-
 
25
#include <string.h> /* memset() */
-
 
26
 
25
#include "doserr.h"
27
#include "doserr.h"
26
#include "helpers.h"
28
#include "helpers.h"
-
 
29
#include "rmodinit.h"
27
 
30
 
28
#include "redir.h"
31
#include "redir.h"
29
 
32
 
30
static unsigned short oldstdout = 0xffff;
33
static unsigned short oldstdout = 0xffff;
31
 
34
 
-
 
35
 
32
/* parse commandline and performs necessary redirections. cmdline is
36
/* parse commandline and performs necessary redirections. cmdline is
33
 * modified so all redirections are cut out.
37
 * modified so all redirections are cut out. */
34
 * returns 0 on success, non-zero otherwise */
-
 
35
int redir_parsecmd(char *cmdline, char *BUFFER) {
38
void redir_parsecmd(struct redir_data *d, char *cmdline) {
36
  unsigned short i;
39
  unsigned short i;
37
  unsigned short rediroffset_stdin = 0;
-
 
38
  unsigned short rediroffset_stdout = 0;
-
 
39
  unsigned short pipesoffsets[16];
-
 
40
  unsigned short pipescount = 0;
40
  unsigned short pipescount = 0;
41
 
41
 
42
  /* NOTE:
42
  /* NOTE:
43
   *
43
   *
44
   * 1. while it is possible to type a command with multiple
44
   * 1. while it is possible to type a command with multiple
Line 51... Line 51...
51
   */
51
   */
52
 
52
 
53
  /* preset oldstdout to 0xffff in case no redirection is required */
53
  /* preset oldstdout to 0xffff in case no redirection is required */
54
  oldstdout = 0xffff;
54
  oldstdout = 0xffff;
55
 
55
 
-
 
56
  /* clear out the redir_data struct */
-
 
57
  memset(d, 0, sizeof(*d));
-
 
58
 
-
 
59
  /* parse the command line and fill struct with pointers */
56
  for (i = 0;; i++) {
60
  for (i = 0;; i++) {
57
    if (cmdline[i] == '>') {
61
    if (cmdline[i] == '>') {
58
      cmdline[i] = 0;
62
      cmdline[i] = 0;
-
 
63
      if (cmdline[i + 1] == '>') {
-
 
64
        i++;
-
 
65
        d->stdout_openflag = 0x11;  /* used during int 21h,AH=6C */
-
 
66
      } else {
-
 
67
        d->stdout_openflag = 0x12;
-
 
68
      }
59
      rediroffset_stdout = i + 1;
69
      d->stdoutfile = cmdline + i + 1;
-
 
70
      while (d->stdoutfile[0] == ' ') d->stdoutfile++;
60
    } else if (cmdline[i] == '<') {
71
    } else if (cmdline[i] == '<') {
61
      cmdline[i] = 0;
72
      cmdline[i] = 0;
62
      rediroffset_stdin = i + 1;
73
      d->stdinfile = cmdline + i + 1;
-
 
74
      while (d->stdinfile[0] == ' ') d->stdinfile++;
63
    } else if (cmdline[i] == '|') {
75
    } else if (cmdline[i] == '|') {
64
      cmdline[i] = 0;
76
      cmdline[i] = 0;
-
 
77
      if (pipescount < REDIR_MAX_PIPES) {
65
      pipesoffsets[pipescount++] = i + 1;
78
        d->pipes[pipescount++] = cmdline + i + 1;
-
 
79
        while (d->pipes[pipescount][0] == ' ') d->pipes[pipescount]++;
-
 
80
      }
66
    } else if (cmdline[i] == 0) {
81
    } else if (cmdline[i] == 0) {
67
      break;
82
      break;
68
    }
83
    }
69
  }
84
  }
-
 
85
}
70
 
86
 
71
  if (rediroffset_stdin != 0) {
-
 
72
    outputnl("ERROR: stdin redirection is not supported yet");
-
 
73
    return(-1);
-
 
74
  }
-
 
75
 
87
 
-
 
88
/* apply stdin/stdout redirections defined in redir_data, returns 0 on success */
-
 
89
int redir_apply(const struct redir_data *d) {
76
  if (pipescount != 0) {
90
  if (d->stdinfile != NULL) {
77
    outputnl("ERROR: pipe redirections are not supported yet");
91
    outputnl("ERROR: stdin redirection is not supported yet");
78
    return(-1);
92
    return(-1);
79
  }
93
  }
80
 
94
 
81
  if (rediroffset_stdout != 0) {
95
  if (d->stdoutfile != NULL) {
82
    unsigned short openflag = 0x12;  /* used during the int 21h,ah=6c call */
96
    unsigned short openflag = d->stdout_openflag;
83
    unsigned short errcode = 0;
97
    unsigned short errcode = 0;
84
    unsigned short handle = 0;
98
    unsigned short handle = 0;
85
    char *ptr;
-
 
86
    /* append? */
-
 
87
    if (cmdline[rediroffset_stdout] == '>') {
-
 
88
      openflag = 0x11;
-
 
89
      rediroffset_stdout++;
-
 
90
    }
-
 
91
 
-
 
92
    /* copy dst file to BUFFER */
-
 
93
    ptr = cmdline + rediroffset_stdout;
99
    const char *myptr = d->stdoutfile;
94
    while (*ptr == ' ') ptr++; /* skip leading white spaces */
-
 
95
    for (i = 0;; i++) {
-
 
96
      BUFFER[i] = ptr[i];
-
 
97
      if ((BUFFER[i] == ' ') || (BUFFER[i] == 0)) break;
-
 
98
    }
-
 
99
    BUFFER[i] = 0;
-
 
100
 
100
 
101
    /* */
101
    /* */
102
    _asm {
102
    _asm {
103
      push ax
103
      push ax
104
      push bx
104
      push bx
Line 107... Line 107...
107
      push si
107
      push si
108
      mov ax, 0x6c00     /* Extended Open/Create */
108
      mov ax, 0x6c00     /* Extended Open/Create */
109
      mov bx, 1          /* access mode (0=read, 1=write, 2=r+w */
109
      mov bx, 1          /* access mode (0=read, 1=write, 2=r+w */
110
      xor cx, cx         /* attributes when(if) creating the file (0=normal) */
110
      xor cx, cx         /* attributes when(if) creating the file (0=normal) */
111
      mov dx, [openflag] /* action if file exists (0x11=open, 0x12=truncate)*/
111
      mov dx, [openflag] /* action if file exists (0x11=open, 0x12=truncate)*/
112
      mov si, BUFFER     /* ASCIIZ filename */
112
      mov si, myptr      /* ASCIIZ filename */
113
      int 0x21           /* AX=handle on success (CF clear), otherwise dos err */
113
      int 0x21           /* AX=handle on success (CF clear), otherwise dos err */
114
      mov [handle], ax   /* save the file handler */
114
      mov [handle], ax   /* save the file handler */
115
      jnc DUPSTDOUT
115
      jnc DUPSTDOUT
116
      mov [errcode], ax
116
      mov [errcode], ax
117
      jmp DONE
117
      jmp DONE