Subversion Repositories SvarDOS

Rev

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

Rev 520 Rev 538
1
/* This file is part of the SvarCOM project and is published under the terms
1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021 Mateusz Viste
4
 * Copyright (C) 2021 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included in
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
14
 * all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
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() */
25
#include <string.h> /* memset() */
26
 
26
 
27
#include "doserr.h"
-
 
28
#include "helpers.h"
27
#include "helpers.h"
29
#include "rmodinit.h"
28
#include "rmodinit.h"
30
 
29
 
31
#include "redir.h"
30
#include "redir.h"
32
 
31
 
33
static unsigned short oldstdout = 0xffff;
32
static unsigned short oldstdout = 0xffff;
34
 
33
 
35
 
34
 
36
/* parse commandline and performs necessary redirections. cmdline is
35
/* parse commandline and performs necessary redirections. cmdline is
37
 * modified so all redirections are cut out. */
36
 * modified so all redirections are cut out. */
38
void redir_parsecmd(struct redir_data *d, char *cmdline) {
37
void redir_parsecmd(struct redir_data *d, char *cmdline) {
39
  unsigned short i;
38
  unsigned short i;
40
  unsigned short pipescount = 0;
39
  unsigned short pipescount = 0;
41
 
40
 
42
  /* NOTE:
41
  /* NOTE:
43
   *
42
   *
44
   * 1. while it is possible to type a command with multiple
43
   * 1. while it is possible to type a command with multiple
45
   *    redirections, MSDOS executes only the last redirection.
44
   *    redirections, MSDOS executes only the last redirection.
46
   *
45
   *
47
   * 2. the order in which >, < and | are provided on the command line does
46
   * 2. the order in which >, < and | are provided on the command line does
48
   *    not seem to matter for MSDOS. piped commands are executed first (in
47
   *    not seem to matter for MSDOS. piped commands are executed first (in
49
   *    the order they appear) and the result of the last one is redirected to
48
   *    the order they appear) and the result of the last one is redirected to
50
   *    whenever the last > points at.
49
   *    whenever the last > points at.
51
   */
50
   */
52
 
51
 
53
  /* preset oldstdout to 0xffff in case no redirection is required */
52
  /* preset oldstdout to 0xffff in case no redirection is required */
54
  oldstdout = 0xffff;
53
  oldstdout = 0xffff;
55
 
54
 
56
  /* clear out the redir_data struct */
55
  /* clear out the redir_data struct */
57
  memset(d, 0, sizeof(*d));
56
  memset(d, 0, sizeof(*d));
58
 
57
 
59
  /* parse the command line and fill struct with pointers */
58
  /* parse the command line and fill struct with pointers */
60
  for (i = 0;; i++) {
59
  for (i = 0;; i++) {
61
    if (cmdline[i] == '>') {
60
    if (cmdline[i] == '>') {
62
      cmdline[i] = 0;
61
      cmdline[i] = 0;
63
      if (cmdline[i + 1] == '>') {
62
      if (cmdline[i + 1] == '>') {
64
        i++;
63
        i++;
65
        d->stdout_openflag = 0x11;  /* used during int 21h,AH=6C */
64
        d->stdout_openflag = 0x11;  /* used during int 21h,AH=6C */
66
      } else {
65
      } else {
67
        d->stdout_openflag = 0x12;
66
        d->stdout_openflag = 0x12;
68
      }
67
      }
69
      d->stdoutfile = cmdline + i + 1;
68
      d->stdoutfile = cmdline + i + 1;
70
      while (d->stdoutfile[0] == ' ') d->stdoutfile++;
69
      while (d->stdoutfile[0] == ' ') d->stdoutfile++;
71
    } else if (cmdline[i] == '<') {
70
    } else if (cmdline[i] == '<') {
72
      cmdline[i] = 0;
71
      cmdline[i] = 0;
73
      d->stdinfile = cmdline + i + 1;
72
      d->stdinfile = cmdline + i + 1;
74
      while (d->stdinfile[0] == ' ') d->stdinfile++;
73
      while (d->stdinfile[0] == ' ') d->stdinfile++;
75
    } else if (cmdline[i] == '|') {
74
    } else if (cmdline[i] == '|') {
76
      cmdline[i] = 0;
75
      cmdline[i] = 0;
77
      if (pipescount < REDIR_MAX_PIPES) {
76
      if (pipescount < REDIR_MAX_PIPES) {
78
        d->pipes[pipescount++] = cmdline + i + 1;
77
        d->pipes[pipescount++] = cmdline + i + 1;
79
        while (d->pipes[pipescount][0] == ' ') d->pipes[pipescount]++;
78
        while (d->pipes[pipescount][0] == ' ') d->pipes[pipescount]++;
80
      }
79
      }
81
    } else if (cmdline[i] == 0) {
80
    } else if (cmdline[i] == 0) {
82
      break;
81
      break;
83
    }
82
    }
84
  }
83
  }
85
}
84
}
86
 
85
 
87
 
86
 
88
/* apply stdin/stdout redirections defined in redir_data, returns 0 on success */
87
/* apply stdin/stdout redirections defined in redir_data, returns 0 on success */
89
int redir_apply(const struct redir_data *d) {
88
int redir_apply(const struct redir_data *d) {
90
  if (d->stdinfile != NULL) {
89
  if (d->stdinfile != NULL) {
91
    outputnl("ERROR: stdin redirection is not supported yet");
90
    outputnl("ERROR: stdin redirection is not supported yet");
92
    return(-1);
91
    return(-1);
93
  }
92
  }
94
 
93
 
95
  if (d->stdoutfile != NULL) {
94
  if (d->stdoutfile != NULL) {
96
    unsigned short openflag = d->stdout_openflag;
95
    unsigned short openflag = d->stdout_openflag;
97
    unsigned short errcode = 0;
96
    unsigned short errcode = 0;
98
    unsigned short handle = 0;
97
    unsigned short handle = 0;
99
    const char *myptr = d->stdoutfile;
98
    const char *myptr = d->stdoutfile;
100
 
99
 
101
    /* */
100
    /* */
102
    _asm {
101
    _asm {
103
      push ax
102
      push ax
104
      push bx
103
      push bx
105
      push cx
104
      push cx
106
      push dx
105
      push dx
107
      push si
106
      push si
108
      mov ax, 0x6c00     /* Extended Open/Create */
107
      mov ax, 0x6c00     /* Extended Open/Create */
109
      mov bx, 1          /* access mode (0=read, 1=write, 2=r+w */
108
      mov bx, 1          /* access mode (0=read, 1=write, 2=r+w */
110
      xor cx, cx         /* attributes when(if) creating the file (0=normal) */
109
      xor cx, cx         /* attributes when(if) creating the file (0=normal) */
111
      mov dx, [openflag] /* action if file exists (0x11=open, 0x12=truncate)*/
110
      mov dx, [openflag] /* action if file exists (0x11=open, 0x12=truncate)*/
112
      mov si, myptr      /* ASCIIZ filename */
111
      mov si, myptr      /* ASCIIZ filename */
113
      int 0x21           /* AX=handle on success (CF clear), otherwise dos err */
112
      int 0x21           /* AX=handle on success (CF clear), otherwise dos err */
114
      mov handle, ax     /* save the file handler */
113
      mov handle, ax     /* save the file handler */
115
      jnc JMPEOF
114
      jnc JMPEOF
116
      mov errcode, ax
115
      mov errcode, ax
117
      jmp DONE
116
      jmp DONE
118
 
117
 
119
      JMPEOF:
118
      JMPEOF:
120
      cmp openflag, word ptr 0x11
119
      cmp openflag, word ptr 0x11
121
      jne DUPSTDOUT
120
      jne DUPSTDOUT
122
      /* jump to the end of the file (required for >> redirections) */
121
      /* jump to the end of the file (required for >> redirections) */
123
      mov ax, 0x4202     /* jump to position EOF - CX:DX in handle BX */
122
      mov ax, 0x4202     /* jump to position EOF - CX:DX in handle BX */
124
      mov bx, handle
123
      mov bx, handle
125
      xor cx, cx
124
      xor cx, cx
126
      xor dx, dx
125
      xor dx, dx
127
      int 0x21
126
      int 0x21
128
 
127
 
129
      /* save (duplicate) current stdout so I can revert it later */
128
      /* save (duplicate) current stdout so I can revert it later */
130
      DUPSTDOUT:
129
      DUPSTDOUT:
131
      mov ah, 0x45       /* duplicate file handle */
130
      mov ah, 0x45       /* duplicate file handle */
132
      mov bx, 1          /* handle to dup (1=stdout) */
131
      mov bx, 1          /* handle to dup (1=stdout) */
133
      int 0x21           /* ax = new file handle */
132
      int 0x21           /* ax = new file handle */
134
      mov oldstdout, ax
133
      mov oldstdout, ax
135
      /* redirect the stdout handle */
134
      /* redirect the stdout handle */
136
      mov bx, handle     /* dst handle */
135
      mov bx, handle     /* dst handle */
137
      mov cx, 1          /* src handle (1=stdout) */
136
      mov cx, 1          /* src handle (1=stdout) */
138
      mov ah, 0x46       /* redirect a handle */
137
      mov ah, 0x46       /* redirect a handle */
139
      int 0x21
138
      int 0x21
140
      /* close the original file handle (no longer needed) */
139
      /* close the original file handle (no longer needed) */
141
      mov ah, 0x3e       /* close a file handle (handle in BX) */
140
      mov ah, 0x3e       /* close a file handle (handle in BX) */
142
      int 0x21
141
      int 0x21
143
      DONE:
142
      DONE:
144
      pop si
143
      pop si
145
      pop dx
144
      pop dx
146
      pop cx
145
      pop cx
147
      pop bx
146
      pop bx
148
      pop ax
147
      pop ax
149
    }
148
    }
150
    if (errcode != 0) {
149
    if (errcode != 0) {
151
      outputnl(doserr(errcode));
150
      nls_outputnl_doserr(errcode);
152
      return(-1);
151
      return(-1);
153
    }
152
    }
154
  }
153
  }
155
 
154
 
156
  return(0);
155
  return(0);
157
}
156
}
158
 
157
 
159
 
158
 
160
/* restores previous stdout/stdin handlers if they have been redirected */
159
/* restores previous stdout/stdin handlers if they have been redirected */
161
void redir_revert(void) {
160
void redir_revert(void) {
162
  _asm {
161
  _asm {
163
    /* if oldstdout is 0xffff then not redirected */
162
    /* if oldstdout is 0xffff then not redirected */
164
    cmp word ptr [oldstdout], 0xffff
163
    cmp word ptr [oldstdout], 0xffff
165
    je DONE
164
    je DONE
166
    /* redirect the stdout handle */
165
    /* redirect the stdout handle */
167
    mov bx, [oldstdout] /* dst handle */
166
    mov bx, [oldstdout] /* dst handle */
168
    mov cx, 1           /* src handle (1=stdout) */
167
    mov cx, 1           /* src handle (1=stdout) */
169
    mov ah, 0x46        /* redirect a handle */
168
    mov ah, 0x46        /* redirect a handle */
170
    int 0x21
169
    int 0x21
171
    /* close old handle (in bx already) */
170
    /* close old handle (in bx already) */
172
    mov ah, 0x3e
171
    mov ah, 0x3e
173
    int 0x21
172
    int 0x21
174
    mov [oldstdout], 0xffff
173
    mov [oldstdout], 0xffff
175
    DONE:
174
    DONE:
176
  }
175
  }
177
}
176
}
178
 
177