Subversion Repositories SvarDOS

Rev

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

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