Subversion Repositories SvarDOS

Rev

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

Rev 397 Rev 421
-
 
1
/* This file is part of the SvarCOM project and is published under the terms
-
 
2
 * of the MIT license.
-
 
3
 *
-
 
4
 * Copyright (C) 2021 Mateusz Viste
-
 
5
 *
-
 
6
 * Permission is hereby granted, free of charge, to any person obtaining a
-
 
7
 * copy of this software and associated documentation files (the "Software"),
-
 
8
 * to deal in the Software without restriction, including without limitation
-
 
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
-
 
11
 * Software is furnished to do so, subject to the following conditions:
-
 
12
 *
-
 
13
 * The above copyright notice and this permission notice shall be included in
-
 
14
 * all copies or substantial portions of the Software.
-
 
15
 *
-
 
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,
-
 
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
-
 
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
-
 
22
 * DEALINGS IN THE SOFTWARE.
-
 
23
 */
-
 
24
 
1
/*
25
/*
2
 * type
26
 * type
3
 */
27
 */
4
 
28
 
5
static int cmd_type(struct cmd_funcparam *p) {
29
static int cmd_type(struct cmd_funcparam *p) {
6
  char *buff = p->BUFFER;
30
  char *buff = p->BUFFER;
7
  const char *fname = p->argv[0];
31
  const char *fname = p->argv[0];
8
  unsigned short err = 0;
32
  unsigned short err = 0;
9
 
33
 
10
  if (cmd_ishlp(p)) {
34
  if (cmd_ishlp(p)) {
11
    outputnl("Displays the contents of a text file.");
35
    outputnl("Displays the contents of a text file.");
12
    outputnl("");
36
    outputnl("");
13
    outputnl("TYPE [drive:][path]filename");
37
    outputnl("TYPE [drive:][path]filename");
14
    return(-1);
38
    return(-1);
15
  }
39
  }
16
 
40
 
17
  if (p->argc == 0) {
41
  if (p->argc == 0) {
18
    outputnl("Required parameter missing");
42
    outputnl("Required parameter missing");
19
    return(-1);
43
    return(-1);
20
  }
44
  }
21
 
45
 
22
  if (p->argc > 1) {
46
  if (p->argc > 1) {
23
    outputnl("Too many parameters");
47
    outputnl("Too many parameters");
24
    return(-1);
48
    return(-1);
25
  }
49
  }
26
 
50
 
27
  /* if here then display the file */
51
  /* if here then display the file */
28
  _asm {
52
  _asm {
29
    push ax
53
    push ax
30
    push bx
54
    push bx
31
    push cx
55
    push cx
32
    push dx
56
    push dx
33
    push si
57
    push si
34
 
58
 
35
    mov ax, 0x3d00 /* open file via handle, access mode in AL (0 = read) */
59
    mov ax, 0x3d00 /* open file via handle, access mode in AL (0 = read) */
36
    mov dx, fname
60
    mov dx, fname
37
    int 0x21       /* file handle in ax on success (CF clear) */
61
    int 0x21       /* file handle in ax on success (CF clear) */
38
    jnc FILE_OPEN_OK
62
    jnc FILE_OPEN_OK
39
    mov [err], ax  /* on error AX contains the DOS err code */
63
    mov [err], ax  /* on error AX contains the DOS err code */
40
    jmp FOPENFAIL
64
    jmp FOPENFAIL
41
    FILE_OPEN_OK:
65
    FILE_OPEN_OK:
42
    /* copy obtained file handle to BX */
66
    /* copy obtained file handle to BX */
43
    mov bx, ax
67
    mov bx, ax
44
 
68
 
45
    READNEXTBLOCK:
69
    READNEXTBLOCK:
46
    /* read file block by block */
70
    /* read file block by block */
47
    mov cx, 1024   /* read 1K at a time */
71
    mov cx, 1024   /* read 1K at a time */
48
    mov dx, buff
72
    mov dx, buff
49
    mov ah, 0x3f   /* read CX bytes from file handle in BX and write to DS:DX */
73
    mov ah, 0x3f   /* read CX bytes from file handle in BX and write to DS:DX */
50
    int 0x21       /* CF set on error, AX=errno or AX=number of bytes read */
74
    int 0x21       /* CF set on error, AX=errno or AX=number of bytes read */
51
    jc GOTERROR    /* abort on error */
75
    jc GOTERROR    /* abort on error */
52
    test ax, ax    /* EOF? */
76
    test ax, ax    /* EOF? */
53
    jz ENDFILE
77
    jz ENDFILE
54
    /* display read block (AX=len) */
78
    /* display read block (AX=len) */
55
    mov si, dx     /* preset DS:SI to DS:DX (DL will be reused soon) */
79
    mov si, dx     /* preset DS:SI to DS:DX (DL will be reused soon) */
56
    mov cx, ax     /* set loop count to CX */
80
    mov cx, ax     /* set loop count to CX */
57
    mov ah, 0x02   /* write character in DL to stdout */
81
    mov ah, 0x02   /* write character in DL to stdout */
58
    NEXTCHAR:
82
    NEXTCHAR:
59
    mov dl, [si]
83
    mov dl, [si]
60
    inc si
84
    inc si
61
    int 0x21
85
    int 0x21
62
    loopnz NEXTCHAR /* CX-- ; jnz NEXTCHAR (display CX characters) */
86
    loopnz NEXTCHAR /* CX-- ; jnz NEXTCHAR (display CX characters) */
63
    /* read (and display) next block */
87
    /* read (and display) next block */
64
    jmp READNEXTBLOCK
88
    jmp READNEXTBLOCK
65
 
89
 
66
    GOTERROR:
90
    GOTERROR:
67
    mov [err], ax
91
    mov [err], ax
68
 
92
 
69
    ENDFILE:
93
    ENDFILE:
70
    /* close file */
94
    /* close file */
71
    mov ah, 0x3e   /* close file handle (file handle already in BX) */
95
    mov ah, 0x3e   /* close file handle (file handle already in BX) */
72
    int 0x21
96
    int 0x21
73
 
97
 
74
    FOPENFAIL:
98
    FOPENFAIL:
75
 
99
 
76
    pop si
100
    pop si
77
    pop dx
101
    pop dx
78
    pop cx
102
    pop cx
79
    pop bx
103
    pop bx
80
    pop ax
104
    pop ax
81
  }
105
  }
82
 
106
 
83
  if (err != 0) outputnl(doserr(err));
107
  if (err != 0) outputnl(doserr(err));
84
 
108
 
85
  return(-1);
109
  return(-1);
86
}
110
}
87
 
111