home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PPOS2.ZIP / TYPEFILE.ASM < prev   
Assembly Source File  |  1989-03-16  |  6KB  |  169 lines

  1.         title   TYPEFILE -- demo of RLINE subroutine
  2.         page    55,132
  3.     .286
  4.  
  5. ;  TYPEFILE.ASM -- Simple demo of RLINE subroutine (OS/2 version).  
  6. ;                  Displays text file on standard output device.
  7. ;  by Ray Duncan, (C) 1988 Ziff Davis Communications
  8. ;  Requires: RLINE.ASM, STRINGS1.ASM, ARGV.ASM, ARGC.ASM.
  9. ;
  10. ;  Build:       MASM TYPEFILE;
  11. ;               MASM RLINE;
  12. ;               MASM STRINGS1;
  13. ;               MASM ARGC;
  14. ;               MASM ARGV;
  15. ;               LINK TYPEFILE+RLINE+STRINGS1+ARGC+ARGV,,,OS2,TYPEFILE.DEF
  16. ;
  17. ;  Usage:       TYPEFILE filename.ext
  18.  
  19. cr      equ     0dh             ; ASCII carriage return
  20. lf      equ     0ah             ; ASCII line feed
  21. blank   equ     20h             ; ASCII space code
  22.  
  23. blksize equ     256             ; size of input file buffer     
  24.  
  25. stdin   equ     0               ; standard input handle
  26. stdout  equ     1               ; standard output handle
  27. stderr  equ     2               ; standard error handle
  28.  
  29.     extrn    DosClose:far    ; references to API services
  30.     extrn    DosExit:far
  31.     extrn    DosOpen:far    
  32.         extrn    DosWrite:far
  33.  
  34. DGROUP  group   _DATA
  35.  
  36. _TEXT   segment word public 'CODE'
  37.  
  38.         extrn   argc:near           ; external subroutines
  39.         extrn   argv:near
  40.         extrn   rline:near
  41.  
  42.         assume  cs:_TEXT,ds:DGROUP,es:NOTHING
  43.  
  44. main    proc    far                 ; entry point from OS/2
  45.  
  46.                                     ; check if filename present
  47.         call    argc                   ; count command arguments
  48.         cmp     ax,2                ; are there 2 arguments?
  49.         je      main1               ; yes, proceed
  50.  
  51.                                     ; missing filename...
  52.         mov     dx,offset DGROUP:msg2      ; DS:DX = error message
  53.         mov     cx,msg2_len         ; CX = message length
  54.         jmp     main5               ; go display it and exit
  55.  
  56. main1:                              ; get address of filename
  57.         mov     ax,1                ; AX = argument number
  58.         call    argv                ; returns ES:BX = address,
  59.                                     ; and AX = length
  60.  
  61.         mov     di,offset DGROUP:fname     ; copy filename to buffer
  62.         mov     cx,ax               ; let CX = length
  63.  
  64. main2:  mov     al,es:[bx]          ; copy one byte
  65.         mov     [di],al
  66.         inc     bx                  ; bump string pointers
  67.         inc     di
  68.         loop    main2               ; loop until string done
  69.         mov     byte ptr [di],0     ; add terminal null byte
  70.  
  71.         push    ds                  ; set ES = DGROUP
  72.         pop     es
  73.         assume  es:DGROUP
  74.  
  75.                                     ; now open the file...
  76.     push    ds            ; address of filename    
  77.         push    offset DGROUP:fname
  78.         push    ds                ; receives file handle
  79.         push    offset DGROUP:fhandle
  80.         push    ds                ; receives DosOpen action
  81.         push    offset DGROUP:faction
  82.         push    0            ; file allocation (N/A)
  83.         push    0
  84.         push    0            ; attribute (N/A)
  85.         push    1            ; open but do not create
  86.         push    40h            ; read-only, deny none
  87.     push    0            ; reserved DWORD 0
  88.         push    0    
  89.     call    DosOpen            ; transfer to OS/2
  90.         or    ax,ax            ; open successful?
  91.         jz    main3            ; yes, jump
  92.  
  93.                                     ; file doesn't exist...
  94.         mov     dx,offset DGROUP:msg1      ; DS:DX = error message
  95.         mov     cx,msg1_len         ; CX = message length
  96.         jmp     main5               ; go display and exit
  97.  
  98. main3:                              ; read line from file
  99.         mov     bx,fhandle          ; BX = file handle
  100.         mov     cx,blksize          ; CX = buffer length
  101.         mov     dx,offset DGROUP:fbuff     ; DS:DX = buffer
  102.         call    rline
  103.         or      ax,ax               ; reached end of file?
  104.         jz      main4               ; yes, exit
  105.  
  106.                                     ; otherwise display line
  107.     push    stdout            ; standard output handle
  108.         push    ds            ; address of text
  109.         push    dx
  110.         push    ax            ; length of text
  111.         push    ds                ; receives bytes written
  112.         push    offset DGROUP:wlen
  113.     call    DosWrite        ; transfer to OS/2    
  114.  
  115.         jmp     main3               ; get another line
  116.  
  117. main4:                              ; success exit point
  118.     push    fhandle            ; file handle
  119.         call    DosClose        ; transfer to OS/2
  120.  
  121.         push    1            ; terminate process
  122.         push    0            ; with return code = 0
  123.         call    DosExit            ; transfer to OS/2
  124.  
  125. main5:                              ; common error exit
  126.                                     ; DS:DX = message address
  127.                                     ; CX = message length
  128.     push    stderr          ; standard error handle    
  129.         push    ds                ; address of error message
  130.         push    dx
  131.         push    cx                ; length of error message
  132.         push    ds                ; receives bytes written
  133.         push    offset DGROUP:wlen
  134.         call    DosWrite            ; transfer to OS/2
  135.  
  136.         push    1            ; terminate process
  137.         push    1            ; with return code = 1
  138.         call    DosExit            ; transfer to OS/2
  139.  
  140. main    endp
  141.  
  142. _TEXT    ends
  143.  
  144.  
  145. _DATA   segment word public 'DATA'
  146.  
  147. fname   db      64 dup (0)          ; buffer for input filespec
  148. fhandle dw      0                   ; input file handle
  149. faction    dw    0            ; receives DosOpen action
  150.  
  151. fbuff   db      blksize dup (?)     ; data from input file
  152.  
  153. wlen    dw    0            ; receives bytes written
  154.  
  155. msg1    db      cr,lf
  156.         db      'typefile: file not found'
  157.         db      cr,lf
  158. msg1_len equ    $-msg1
  159.  
  160. msg2    db      cr,lf
  161.         db      'typefile: missing filename'
  162.         db      cr,lf
  163. msg2_len equ    $-msg2
  164.  
  165. _DATA   ends    
  166.  
  167.         end     main
  168.  
  169.