home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch16 / execsort.asm < prev    next >
Encoding:
Assembly Source File  |  1988-12-12  |  7.9 KB  |  222 lines

  1.     title      EXECSORT -- Run SORT.EXE as Child
  2.         page      55,132
  3.         .286
  4.     .sall
  5.  
  6. ;
  7. ; EXECSORT.ASM
  8. ;
  9. ; Demonstration of use of DosExecPgm to run the OS/2 filter SORT.EXE
  10. ; as a child process, redirecting its input to MYFILE.DAT and its
  11. ; output to MYFILE.SRT.
  12. ;
  13. ; Assemble with:  C> masm execsort.asm;
  14. ; Link with:  C> link execsort,,,os2,execsort
  15. ;
  16. ; Usage is:  C> execsort
  17. ;
  18. ; Copyright (C) 1988 Ray Duncan
  19. ;
  20.  
  21. stdin   equ     0               ; standard input device
  22. stdout  equ     1               ; standard output device        
  23. stderr  equ     2               ; standard error device
  24.  
  25. cr      equ     0dh             ; ASCII carriage return
  26. lf      equ     0ah             ; ASCII linefeed
  27.  
  28.         extrn   DosClose:far
  29.         extrn   DosDupHandle:far
  30.         extrn   DosExecPgm:far
  31.         extrn   DosExit:far
  32.         extrn   DosOpen:far
  33.         extrn   DosWrite:far
  34.  
  35. jerr    macro    target        ;; Macro to test AX
  36.     local    zero        ;; and jump if AX nonzero
  37.         or      ax,ax
  38.     jz    zero        ;; Uses JMP DISP16 to avoid
  39.     jmp    target        ;; branch out of range errors
  40. zero:
  41.         endm
  42.  
  43.  
  44. DGROUP  group   _DATA
  45.  
  46. _DATA   segment word public 'DATA'
  47.  
  48. iname   db      'MYFILE.DAT',0  ; name of input file
  49. oname   db      'MYFILE.SRT',0  ; name of output file
  50.  
  51. ihandle dw      ?               ; handle for input file
  52. ohandle dw      ?               ; handle for output file
  53.  
  54. action  dw      ?               ; receives DosOpen action
  55.  
  56. oldin   dw      -1              ; dup of old stdin handle
  57. oldout  dw      -1              ; dup of old stdout handle      
  58.  
  59. newin   dw      stdin           ; forced to track ihandle
  60. newout  dw      stdout          ; forced to track ohandle
  61.  
  62. pname   db      'SORT.EXE',0    ; pathname of SORT filter
  63.  
  64. objbuff db      64              ; receives failing dynlink
  65. objbuff_len equ $-objbuff
  66.  
  67. pcodes  dw      0,0             ; PID, exit code of child
  68.  
  69. msg     db      cr,lf,'SORT was executed as child.',cr,lf
  70. msg_len equ $-msg 
  71.  
  72. _DATA   ends
  73.  
  74.  
  75. _TEXT   segment word public 'CODE'
  76.  
  77.         assume  cs:_TEXT,ds:DGROUP
  78.  
  79. main    proc    far             ; entry point from OS/2
  80.  
  81.                                 ; prepare stdin and stdout
  82.                                 ; handles for child SORT...
  83.  
  84.                                 ; dup handle for stdin...
  85.         push    stdin           ; standard input handle
  86.         push    ds              ; receives new handle
  87.         push    offset DGROUP:oldin
  88.         call    DosDupHandle    ; transfer to OS/2
  89.         jerr    main1           ; exit if error
  90.  
  91.                                 ; dup handle for stdout...
  92.         push    stdout          ; standard output handle
  93.         push    ds              ; receives new handle
  94.         push    offset DGROUP:oldout    
  95.         call    DosDupHandle    ; transfer to OS/2
  96.         jerr    main1           ; exit if error
  97.  
  98.                                 ; open input file...
  99.         push    ds              ; address of filename
  100.         push    offset DGROUP:iname
  101.         push    ds              ; receives file handle
  102.         push    offset DGROUP:ihandle
  103.         push    ds              ; receives DosOpen action
  104.         push    offset DGROUP:action
  105.     push    0        ; file size (not used)
  106.         push    0               
  107.         push    0               ; attribute (not used)
  108.         push    1               ; action: open if exists
  109.                                 ;         fail if doesn't
  110.         push    40h             ; access: read-only
  111.         push    0               ; reserved DWORD 0
  112.         push    0
  113.         call    DosOpen         ; transfer to OS/2
  114.         jerr    main1           ; exit if error
  115.         
  116.                                 ; create output file...
  117.         push    ds              ; address of filename
  118.         push    offset DGROUP:oname
  119.         push    ds              ; receives file handle
  120.         push    offset DGROUP:ohandle
  121.         push    ds              ; receives DOSOPEN action
  122.         push    offset DGROUP:action
  123.     push    0        ; initial file size
  124.         push    0               
  125.         push    0               ; attribute = normal 
  126.         push    12h             ; action: create/replace
  127.         push    41h             ; access: write-only
  128.         push    0               ; reserved DWORD 0
  129.         push    0
  130.         call    DosOpen         ; transfer to OS/2
  131.         jerr    main1           ; exit if error
  132.  
  133.                 ; make stdin track
  134.                 ; input file handle...
  135.         push    ihandle         ; handle from DOSOPEN
  136.         push    ds              ; standard input handle
  137.         push    offset DGROUP:newin
  138.         call    DosDupHandle    ; transfer to OS/2
  139.         jerr    main1           ; exit if error
  140.  
  141.                 ; make stdout track
  142.                 ; output file handle...
  143.         push    ohandle         ; handle from DOSOPEN
  144.         push    ds              ; standard output handle
  145.         push    offset DGROUP:newout
  146.         call    DosDupHandle    ; transfer to OS/2
  147.         jerr    main1           ; exit if error
  148.  
  149.                                 ; run SORT.EXE as child...
  150.         push    ds              ; receives failing dynlink
  151.         push    offset DGROUP:objbuff
  152.         push    objbuff_len     ; length of buffer
  153.         push    0               ; 0 = synchronous execution
  154.         push    0               ; argument strings ptr
  155.         push    0
  156.         push    0               ; environment pointer
  157.         push    0
  158.         push    ds              ; receives PID, exit code
  159.         push    offset DGROUP:pcodes
  160.         push    ds              ; child program pathname
  161.         push    offset DGROUP:pname
  162.         call    DosExecPgm      ; transfer to OS/2
  163.         jerr    main1           ; exit if error
  164.  
  165.                 ; restore stdin handle
  166.                 ; to original meaning...
  167.         push    oldin           ; dup of original stdin
  168.         push    ds              ; standard input handle
  169.         push    offset DGROUP:newin
  170.         call    DosDupHandle    ; transfer to OS/2
  171.         jerr    main1           ; exit if error
  172.  
  173.                 ; restore stdout handle
  174.                 ; to original meaning...
  175.         push    oldout          ; dup of original stdout
  176.         push    ds              ; standard output handle
  177.         push    offset DGROUP:newout
  178.         call    DosDupHandle    ; transfer to OS/2
  179.         jerr    main1           ; exit if error
  180.  
  181.         push    oldin           ; close dup of stdin
  182.         call    DosClose        ; transfer to OS/2
  183.         jerr    main1           ; exit if error
  184.  
  185.         push    oldout          ; close dup of stdout
  186.         call    DosClose        ; transfer to OS/2
  187.         jerr    main1           ; exit if error
  188.  
  189.         push    ihandle         ; close input file 
  190.         call    DosClose        ; transfer to OS/2
  191.         jerr    main1           ; exit if error
  192.  
  193.         push    ohandle         ; close output file
  194.         call    DosClose        ; transfer to OS/2
  195.         jerr    main1           ; exit if error
  196.  
  197.                                 ; display success message...
  198.         push    stdout          ; standard output handle
  199.         push    ds              ; address of message
  200.         push    offset DGROUP:msg
  201.         push    msg_len         ; length of message
  202.         push    ds              ; receives bytes written
  203.         push    offset DGROUP:action
  204.         call    DosWrite        ; transfer to OS/2
  205.         jerr    main1           ; exit if error
  206.  
  207.                                 ; exit point if no errors
  208.         push    1               ; terminate all threads
  209.         push    0               ; exit code = 0 (success)
  210.         call    DosExit         ; transfer to OS/2
  211.  
  212. main1:                          ; exit point if error
  213.         push    1               ; terminate all threads
  214.         push    1               ; exit code = 1 (error)
  215.         call    DosExit
  216.  
  217. main    endp                    
  218.  
  219. _TEXT   ends
  220.  
  221.         end     main            ; defines entry point
  222.