421 |
mateuszvis |
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 |
|
382 |
mateuszvis |
25 |
/*
|
|
|
26 |
* type
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
static int cmd_type(struct cmd_funcparam *p) {
|
|
|
30 |
char *buff = p->BUFFER;
|
|
|
31 |
const char *fname = p->argv[0];
|
|
|
32 |
unsigned short err = 0;
|
|
|
33 |
|
387 |
mateuszvis |
34 |
if (cmd_ishlp(p)) {
|
382 |
mateuszvis |
35 |
outputnl("Displays the contents of a text file.");
|
|
|
36 |
outputnl("");
|
|
|
37 |
outputnl("TYPE [drive:][path]filename");
|
|
|
38 |
return(-1);
|
|
|
39 |
}
|
|
|
40 |
|
387 |
mateuszvis |
41 |
if (p->argc == 0) {
|
|
|
42 |
outputnl("Required parameter missing");
|
|
|
43 |
return(-1);
|
|
|
44 |
}
|
|
|
45 |
|
382 |
mateuszvis |
46 |
if (p->argc > 1) {
|
|
|
47 |
outputnl("Too many parameters");
|
|
|
48 |
return(-1);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/* if here then display the file */
|
|
|
52 |
_asm {
|
|
|
53 |
push ax
|
|
|
54 |
push bx
|
|
|
55 |
push cx
|
|
|
56 |
push dx
|
|
|
57 |
push si
|
|
|
58 |
|
|
|
59 |
mov ax, 0x3d00 /* open file via handle, access mode in AL (0 = read) */
|
|
|
60 |
mov dx, fname
|
|
|
61 |
int 0x21 /* file handle in ax on success (CF clear) */
|
|
|
62 |
jnc FILE_OPEN_OK
|
|
|
63 |
mov [err], ax /* on error AX contains the DOS err code */
|
|
|
64 |
jmp FOPENFAIL
|
|
|
65 |
FILE_OPEN_OK:
|
|
|
66 |
/* copy obtained file handle to BX */
|
|
|
67 |
mov bx, ax
|
|
|
68 |
|
|
|
69 |
READNEXTBLOCK:
|
|
|
70 |
/* read file block by block */
|
|
|
71 |
mov cx, 1024 /* read 1K at a time */
|
|
|
72 |
mov dx, buff
|
|
|
73 |
mov ah, 0x3f /* read CX bytes from file handle in BX and write to DS:DX */
|
|
|
74 |
int 0x21 /* CF set on error, AX=errno or AX=number of bytes read */
|
|
|
75 |
jc GOTERROR /* abort on error */
|
|
|
76 |
test ax, ax /* EOF? */
|
|
|
77 |
jz ENDFILE
|
|
|
78 |
/* display read block (AX=len) */
|
|
|
79 |
mov si, dx /* preset DS:SI to DS:DX (DL will be reused soon) */
|
|
|
80 |
mov cx, ax /* set loop count to CX */
|
|
|
81 |
mov ah, 0x02 /* write character in DL to stdout */
|
|
|
82 |
NEXTCHAR:
|
|
|
83 |
mov dl, [si]
|
|
|
84 |
inc si
|
|
|
85 |
int 0x21
|
|
|
86 |
loopnz NEXTCHAR /* CX-- ; jnz NEXTCHAR (display CX characters) */
|
|
|
87 |
/* read (and display) next block */
|
|
|
88 |
jmp READNEXTBLOCK
|
|
|
89 |
|
|
|
90 |
GOTERROR:
|
|
|
91 |
mov [err], ax
|
|
|
92 |
|
|
|
93 |
ENDFILE:
|
|
|
94 |
/* close file */
|
|
|
95 |
mov ah, 0x3e /* close file handle (file handle already in BX) */
|
|
|
96 |
int 0x21
|
|
|
97 |
|
|
|
98 |
FOPENFAIL:
|
|
|
99 |
|
|
|
100 |
pop si
|
|
|
101 |
pop dx
|
|
|
102 |
pop cx
|
|
|
103 |
pop bx
|
|
|
104 |
pop ax
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
if (err != 0) outputnl(doserr(err));
|
|
|
108 |
|
|
|
109 |
return(-1);
|
|
|
110 |
}
|