home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / ZCPR33 / A-R / FOR-NXT2.LBR / NEXT.ZZ0 / NEXT.Z80
Text File  |  2000-06-30  |  11KB  |  472 lines

  1. ; NEXT.Z80
  2. ;            A ZCPR3 Utility
  3. ;
  4. ; This program reads one line from the beginning of the file
  5. ; 'FORFILES.SYS' and stores that line in the shell variable
  6. ; specified as the first parameter.  The 'FORFILES.SYS' file is 
  7. ; written out without the first line.
  8. ;
  9. ; Invocation syntax is:
  10. ;
  11. ;        NEXT <varname>
  12. ;
  13. ; The input file 'FORFILES.SYS' is looked for only in the current directory.
  14. ; The shell variable file is looked for first in any directory named 'ROOT'
  15. ; and, if not found there, in the directory at the root of the path.
  16. ;
  17. ; The only option is help, which is exclusive.
  18. ;
  19. ; Error codes:  If the program cannot find the file 'FORFILES.SYS' it will
  20. ; return an error code corresponding to 'file not found.'  Note that after
  21. ; the last line of the file has been read and stored in a variable, when
  22. ; an attempt is made to read the file again, it will fail.  The shell
  23. ; variable will be set to null (that is, the name will exist, but will 
  24. ; evaluate to nothing).  Thus, when used in a loop, as it is intended, the 
  25. ; possible exit conditions for the loop are:
  26. ;    -  absence of the file-list (IF ~EXIST FORFILES.SYS),
  27. ;    -  a program error (IF ERROR),
  28. ;    -  an empty variable name (IF NUL <varname>).
  29. ;
  30. ; This program must be linked with Z3VARLIB.REL.
  31. ; The libraries Z3LIB.REL and SYSLIB.REL must be searched.
  32. ;
  33. ; Author:  Dreas Nielsen
  34. ; History:
  35. ;   4/18/86    1.0    Created.  RDN
  36. ;   7/12/86        Modified to use 'Z3VARLIB' routines.  RDN
  37. ;   5/25/87    1.1    Modified to set variable to null if file not found. RDN
  38. ;
  39. ;================[  Equates and External Routines  ]================
  40. ;
  41. VERS    EQU    11
  42. ;
  43. FALSE    EQU    0
  44. TRUE    EQU    NOT FALSE
  45. ;
  46. DEBUG    EQU    FALSE
  47. ;
  48. CR    EQU    0DH
  49. LF    EQU    0AH
  50. BELL    EQU    7
  51. CTRLZ    EQU    1AH
  52. EOF    EQU    1AH
  53. BDOS    EQU    5
  54. INFERR    EQU    1    ;error code for "can't open input file"
  55. OFERR    EQU    2    ;error code for "can't open output file"
  56. RDERR    EQU    3    ;error code for "can't read input file"
  57. WRTERR    EQU    4    ;error code for "can't write output file"
  58. CLZERR    EQU    5    ;error code for "can't close output file"
  59. PARMFL    EQU    '/'
  60. CMDLIN    EQU    080H
  61. SYSFCB    EQU    05CH
  62. BUFSIZ    EQU    32    ;32 128-byte sectors (4k) for I/O buffers
  63. FCBLEN    EQU    36
  64. BSZOFF    EQU    0    ;offset of buffer size indicator from I/O ctl block
  65. BADOFF    EQU    6    ;offset of buffer addr indic. from I/O ctl block start
  66. FCBOFF    EQU    8    ;offset of fcb from I/O ctl block start
  67. ;
  68.     EXT    CODEND,QPRINT,PRINT,SKSP,PUTER2,PUTUD,FXI$OPEN,FXO$OPEN
  69.     EXT    FX$GET,FX$PUT,FXI$CLOSE,FXO$CLOSE,Z3INIT
  70.     EXT    ROOT,LOGUD,INITFCB,F$MAKE,GETUD,PFN2,CAPS,GETFN1
  71.     EXT    F$OPEN,F$WRITE,F$CLOSE,F$DELETE,F$RENAME,SFA
  72. ;
  73.     EXT    VARLOAD,ADDVAR,WRTVAR,VARDEF
  74. ;
  75.     IF    DEBUG
  76.     EXT    PHL4HC
  77.     ENDIF
  78. ;
  79. ;================[  Beginning of Program  ]================
  80. ;
  81.     DB    'Z3ENV'
  82.     DB    1
  83. Z3ENV:    DW    00
  84.     DB    VERS
  85. ;
  86. START:    LD    HL,(Z3ENV)
  87.     CALL    Z3INIT
  88. ;
  89. ; Reset error flag
  90.     XOR    A
  91.     CALL    PUTER2
  92. ;
  93. ; Save stack and set a new one
  94.     LD    (SAVESP),SP
  95.     LD    HL,STK
  96.     LD    SP,HL
  97. ;
  98. ; Print signon message
  99.     CALL    QPRINT
  100.     DB    'NEXT  v. ',[VERS / 10] + '0','.',[VERS MOD 10] + '0',CR,LF,0
  101. ;
  102. ; Check for no parameters or help
  103.     LD    A,(SYSFCB+1)
  104.     CP    ' '
  105.     JP    Z,HELP
  106.     LD    HL,CMDLIN
  107.     LD    A,(HL)        ;store null at end of cmdline
  108.     INC    A
  109.     LD    E,A
  110.     XOR    A
  111.     LD    D,A
  112.     ADD    HL,DE
  113.     LD    (HL),A
  114.     LD    HL,CMDLIN+1    ;find 1st char
  115.     CALL    SKSP
  116.     LD    A,(HL)
  117.     CP    '?'
  118.     JP    Z,HELP
  119.     CP    PARMFL
  120.     JP    Z,HELP
  121. ;
  122. ; Save currently logged DU
  123.     CALL    PUTUD
  124. ;
  125. ; Allocate buffers for byte-oriented file I/O, first line of file, and
  126. ; shell variable name.
  127. SETBUFS:
  128.     CALL    CODEND
  129.     LD    (FCBOUT),HL    ;output fcb
  130.     LD    DE,36
  131.     ADD    HL,DE
  132.     LD    (FIRSTLIN),HL
  133.     LD    DE,300H        ;should be enough room for a filename
  134.     ADD    HL,DE
  135.     LD    (INPLOC),HL
  136.     LD    B,BUFSIZ
  137.     CALL    FBINIT        ;allocate I/O ctl buffer for input
  138.     LD    (OUTPLOC),HL
  139.     CALL    FBINIT        ;allocate I/O ctl buffer for output
  140. ;
  141. ; Initialize the I/O control buffers with filenames
  142.     LD    HL,(INPLOC)
  143.     LD    DE,INFNAM
  144.     CALL    INITNAM
  145.     LD    HL,(OUTPLOC)
  146.     LD    DE,OFNAM
  147.     CALL    INITNAM
  148. ;
  149. ; Place Shell Variable into Buffer
  150. ;
  151.     LD    HL,SHVAR    ;first, clear buffer to blanks
  152.     LD    A,' '
  153.     LD    B,8
  154. FILLB:    LD    (HL),A
  155.     INC    HL
  156.     DJNZ    FILLB
  157. ;
  158.     LD    HL,CMDLIN+1    ;point to shell var name
  159.     CALL    SKSP
  160.     LD    DE,SHVAR    ;pt to shell variable buffer
  161.     LD    B,8        ;8 chars max
  162. EXPV1:
  163.     LD    A,(HL)        ;get char
  164.     CALL    DELCK        ;check for delimiter
  165.     JR    Z,EXPV3        ;done if delimiter
  166.     LD    (DE),A        ;save char
  167.     INC    HL        ;pt to next
  168.     INC    DE
  169.     DJNZ    EXPV1
  170. ;
  171. ; Flush Overflow of Shell Variable
  172. ;
  173. EXPV2:
  174.     LD    A,(HL)        ;get char
  175.     INC    HL        ;pt to next
  176.     CALL    DELCK        ;check for delimiter
  177.     JR    NZ,EXPV2
  178.     DEC    HL        ;pt to delimiter
  179. EXPV3:       ;end of routine
  180. ;
  181. ;
  182. ; Now...do the real work.  Try to open the input file.  If it can't be
  183. ; opened, we're done.  Otherwise try to open the output file.  If this
  184. ; can't be done an error code must be returned, and the user may be in
  185. ; trouble if he/she can't distinguish between "can't open input file" and
  186. ; "can't open output file" error codes.  If no errors have occurred, read
  187. ; in the first line of the input file.  Write the rest of the input file
  188. ; to the output file.  Do no format checking on the line read in (i.e., it
  189. ; may not conform to a filename; this leaves an opportunity for other creative
  190. ; uses of 'NEXT').  Then assign the input line to the appropriate shell
  191. ; variable, creating the shell variable file if necessary.
  192. ;
  193. ; Try to open input file
  194.     LD    DE,(INPLOC)
  195.     CALL    FXI$OPEN
  196.     JR    NZ,OPN2
  197. NOINPT:    LD    A,INFERR    ;set "input file error" code
  198.     CALL    PUTER2        ;...this will be "normal" operation sometimes
  199.     LD    HL,(FIRSTLIN)
  200.     XOR    A        ;Create null variable definition.
  201.     LD    (HL),A
  202.     IF    DEBUG
  203.     CALL    ERRADR
  204.     ENDIF
  205.     JP    VARFIL
  206. ;
  207. ; Try to open output file
  208. OPN2:    LD    DE,(OUTPLOC)
  209.     CALL    FXO$OPEN
  210.     JR    NZ,READNAM
  211.     LD    A,OFERR
  212.     CALL    PUTER2
  213.     CALL    PRINT
  214.     DB    'Can''t open output file',CR,LF,0
  215.     IF    DEBUG
  216.     CALL    ERRADR
  217.     ENDIF
  218.     JP    EXIT
  219. ;
  220. ; Read in first filename (or whatever it is)
  221. READNAM:
  222.     LD    HL,(FIRSTLIN)    ;place to put characters
  223.     LD    DE,(INPLOC)
  224. RD3:    CALL    FX$GET        ;see if file empty or leading eoln chars
  225.     JR    NZ,RD4        ;read error must be physical eof
  226.     LD    A,INFERR    ;set "input file error" code
  227. RD5:    CALL    PUTER2        ;...this will be "normal" operation sometimes
  228.     CALL    FXI$CLOSE
  229.     CALL    DELINF        ;delete the input file
  230.     IF    DEBUG
  231.     CALL    ERRADR
  232.     ENDIF
  233.     JP    EXIT
  234. RD4:    CALL    EOLN        ;if it's the end of the line...
  235.     JR    Z,RD3        ;...get another
  236.     CP    EOF        ;if it's the end of the file...
  237.     JR    Z,RD5        ;...quit and clean up
  238.     LD    (HL),A
  239.     INC    HL
  240. RD1:    CALL    FX$GET        ;now get rest of line
  241.     JR    Z,RD2        ;physical eof, but we've gotten at least 1 char
  242.     CALL    EOLN        ;CR or LF?
  243.     JR    Z,RD2
  244.     CP    EOF        ;...or EOF?
  245.     JR    Z,RD2        ;if so, quit
  246.     LD    (HL),A
  247.     INC    HL        ;else point to addr for next char
  248.     JR    RD1        ;and get it
  249. ;
  250. RD2:    XOR    A        ;terminate the line
  251.     LD    (HL),A
  252. ;
  253. ; Copy remainder of input file to output file.
  254. ;  First flush any remaining eoln chars from input file.
  255. COPY:    CALL    FX$GET
  256.     JR    Z,NOCHARS    ;no more chars
  257.     CP    EOF
  258.     JR    Z,NOCHARS
  259.     CALL    EOLN
  260.     JR    Z,COPY
  261. ;                ;there's a legit char in A
  262.     LD    HL,(OUTPLOC)    ;write it out
  263.     EX    DE,HL        ;swap buffer pointers
  264.     CALL    FX$PUT
  265.     JR    Z,OUTERR    ;can't write output file
  266. ;                ;now write the rest of the chars
  267. COPY1:    EX    DE,HL        ;input ptr in DE
  268.     CALL    FX$GET
  269.     JR    Z,CLOSEM    ;done
  270.     EX    DE,HL        ;output ptr in DE
  271.     CALL    FX$PUT
  272.     JR    NZ,COPY1
  273. ;
  274. OUTERR:    LD    A,WRTERR    ;do this if error in writing
  275.     CALL    PUTER2
  276.     IF    DEBUG
  277.     CALL    ERRADR
  278.     ENDIF
  279.     CALL    PRINT
  280.     DB    'Can''t write output file',CR,LF,0
  281. ;
  282. CLOSEM:    LD    DE,(INPLOC)    ;do this if any chars written to output
  283.     CALL    FXI$CLOSE
  284.     LD    DE,(OUTPLOC)
  285.     CALL    FXO$CLOSE
  286.     JR    NZ,CHGNAM    ;continue if no error
  287.     LD    A,CLZERR
  288.     CALL    PUTER2        ;if can't close output file, set flag...
  289.     IF    DEBUG
  290.     CALL    ERRADR
  291.     ENDIF
  292.     CALL    PRINT        ;...print err msg...
  293.     DB    'Can''t close output file',CR,LF,0
  294.     CALL    DELINF        ;...delete input file,
  295.     JR    VARFIL        ;and go try to change shell var
  296. ;
  297. ; If no chars written to output file, close just the input file
  298. ; and erase it
  299. NOCHARS:
  300.     LD    DE,(INPLOC)
  301.     CALL    FXI$CLOSE
  302.     CALL    DELINF
  303.     JR    VARFIL        ;go add shell variable
  304. ;
  305. ; Delete input file and rename output file to infilename
  306. CHGNAM:    CALL    DELINF
  307.     PUSH    DE        ;save input fcb addr
  308.     LD    HL,(OUTPLOC)
  309.     LD    DE,FCBOFF
  310.     ADD    HL,DE
  311.     POP    DE
  312.     EX    DE,HL
  313.     CALL    F$RENAME
  314. ;
  315. ;
  316. ; Modify shell variable file appropriately (using external routines)
  317. ;
  318. VARFIL:    LD    HL,(VARLIST)    ;point to location of beginning of list
  319.     CALL    VARLOAD
  320.     LD    HL,SHVAR    ;point to variable name
  321.     LD    DE,(FIRSTLIN)    ;point to variable definition
  322.     CALL    ADDVAR
  323.     CALL    WRTVAR        ;done!
  324. ;
  325. ; Return to ZCPR3
  326. ;
  327. EXIT:
  328.     CALL    GETUD
  329.     LD    HL,(SAVESP)
  330.     LD    SP,HL
  331.     RET
  332. ;
  333. ; Print help message and exit
  334. HELP:    CALL    PRINT
  335.     DB    'Syntax:',CR,LF
  336.     DB    '          NEXT <varname>',CR,LF
  337.     DB    'The shell variable named will contain the next filename read',CR,LF
  338.     DB    'from the file FORFILES.SYS in the current directory',CR,LF,0
  339.     JP    EXIT
  340. ;
  341. ;
  342. ;================[  Subroutines  ]================
  343. ;
  344. ; Initialize file I/O control buffers.
  345. ; Enter with  HL = first free address in memory
  346. ;             B  = number of 128-byte sectors for the file buffer
  347. ; Return:     HL = first free address after buffer
  348. ;
  349. FBINIT:
  350.     PUSH    DE
  351.     PUSH    BC
  352.     LD    (HL),B
  353.     LD    DE,BADOFF    ;loc of buf addr in I/O ctl block
  354.     ADD    HL,DE
  355.     PUSH    HL
  356.     LD    DE,[FCBLEN + FCBOFF - BADOFF]
  357.     ADD    HL,DE
  358.     EX    DE,HL
  359.     POP    HL
  360.     LD    (HL),E
  361.     INC    HL
  362.     LD    (HL),D
  363.     EX    DE,HL        ;get buf start addr in HL
  364.     LD    DE,128        ;incr HL by buf len in bytes
  365. FBINI1:    ADD    HL,DE
  366.     DJNZ    FBINI1
  367.     POP    BC
  368.     POP    DE
  369.     RET
  370. ;
  371. ;----------------
  372. ; Move filename into fcb of I/O ctl block.  Enter with HL = I/O ctl blk addr,
  373. ; DE = addr of string to move.  Drive is set to current.
  374. INITNAM:
  375.     PUSH    BC
  376.     PUSH    DE        ;save while adding fcb offset
  377.     LD    DE,FCBOFF
  378.     ADD    HL,DE        ;point to input fcb
  379.     XOR    A        ;set current drive
  380.     LD    (HL),A
  381.     INC    HL        ;point to name field of fcb
  382.     POP    DE        ;get source addr
  383.     EX    DE,HL        ;put dest addr in DE, source in HL
  384.     LD    BC,11
  385.     LDIR
  386.     POP    BC        ;restore original contents
  387.     RET
  388. ;
  389. ;----------------
  390. ; Delete input file (file pointed to by INPLOC)
  391. DELINF:    LD    HL,(INPLOC)
  392.     LD    DE,FCBOFF
  393.     ADD    HL,DE
  394.     EX    DE,HL
  395.     CALL    F$DELETE
  396.     RET
  397. ;
  398. ;----------------
  399. ; Check to see if char in A is a delimiter
  400. ;    Return with Z if so
  401. ;
  402. DELCK:
  403.     PUSH    HL        ;pt to table
  404.     PUSH    BC        ;save BC
  405.     CALL    CAPS        ;capitalize char
  406.     LD    B,A        ;char in B
  407.     LD    HL,DTABLE    ;pt to delimiter table
  408. DELCK1:
  409.     LD    A,(HL)        ;get delimiter
  410.     OR    A        ;done?
  411.     JR    Z,NOTDEL
  412.     CP    B        ;compare
  413.     JR    Z,YESDEL
  414.     INC    HL        ;pt to next
  415.     JR    DELCK1
  416. NOTDEL:
  417.     LD    A,B        ;get char
  418.     OR    A        ;set Z if null, else NZ
  419. YESDEL:
  420.     LD    A,B        ;restore char
  421.     POP    BC        ;restore regs
  422.     POP    HL
  423.     RET    
  424. ;
  425. ; Delimiter Table
  426. ;
  427. DTABLE:
  428.     DB    '<>;:,.=-_ ',0
  429. ;
  430. ;----------------
  431. ; Check for end of line -- CR or LF.  Return Z if true.
  432. EOLN:    CP    CR
  433.     RET    Z
  434.     CP    LF
  435.     RET
  436. ;
  437. ;----------------
  438.     IF    DEBUG
  439. ;
  440. ; Write the return address on the console
  441. ;
  442. ERRADR:    CALL    PRINT
  443.     DB    CR,LF,'Error at ',0
  444.     EX    (SP),HL
  445.     CALL    PHL4HC
  446.     EX    (SP),HL
  447.     RET
  448. ;
  449.     ENDIF
  450. ;
  451. ;================[  Buffers  ]================
  452. ;
  453. SAVESP:    DS    2
  454. FIRSTLIN:
  455.     DS    2
  456. FCBOUT:
  457.     DS    2
  458. VARLIST:            ;list read in over I/O ctl buffers
  459. INPLOC:
  460.     DS    2
  461. OUTPLOC:
  462.     DS    2
  463. ;
  464. SHVAR:    DB    '        '
  465. INFNAM:    DB    'FORFILESS','Y'+80H,'S'        ;file should be system
  466. OFNAM:    DB    'FORFILES'
  467. OUTTYP:    DB    '$$$'
  468. STKBOT:    DS    36
  469. STK:    DS    2
  470. ;
  471.     END    START
  472.