home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 18 REXX
/
18-REXX.zip
/
getopt.zip
/
rpr.cmd
< prev
next >
Wrap
OS/2 REXX Batch file
|
1994-04-20
|
11KB
|
405 lines
/*-------------------------------------------------------------------------
* rpr - display and format files
*
* Copyright (c) 1994 Lawrence R Buchanan. ALL RIGHTS RESERVED.
*
* This program is free software; you are free to do whatever you
* want with it. The only requirement is that if you use these
* subroutines in code that you distribute, that you leave the
* copyright messages that appear in the headers of the GetOpt and
* SetupArg subroutines.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* Usage: rpr [-dFnt] [-h header] [-l lines] [-w width] file ...
*
* ------------------- R E V I S I O N H I S T O R Y -------------------
*
* See end of file.
*
-------------------------------------------------------------------------*/
/* Check for uninitialized variables. */
signal on NOVALUE name SIG_NoValue
/*-------------------------------------------------------------------------
Setup GetOpt. stem variable for GetOpt subroutine.
These two statements MUST appear at the beginning of any program
that uses GetOpt.
-------------------------------------------------------------------------*/
parse arg args
call SetupArg args
/* If no options issue usage message and exit. */
if GetOpt.0 = 0 then do
call Usage
exit 1
end
/* Setup program defaults. */
length = 66 /* Page length (in lines). */
width = 72 /* Page width (in characters). */
dflg = 0 /* 0 = single space, 1 = double space. */
Fflg = 0 /* 1 = use FF to separate pages. */
hflg = 0 /* 1 = print custom header. */
nflg = 0 /* 1 = Print line numbers. */
tflg = 0 /* 1 = do not print header/trailer. */
/* Get the option flags and arguments and set up the program environment. */
call DecodeSwitches
/* If no files are given to be printed issue message and exit. */
if GetOpt._optind > GetOpt.0 then do
say GetOpt._program ': No files to print'
exit 2
end
/* Don't print header/trailer if pagelength <= 10. */
if length <= 10 then
tflg = 1
if tflg then
lpp = length
else
lpp = length - 5 - 5 /* Allow for 5-line header/5-line trailer.*/
do i = GetOpt._optind to GetOpt.0
call PrintFile GetOpt.i
end
exit
/* End of main program */
/*-------------------------------------------------------------------------
DecodeSwitches - decodes command-line options
-------------------------------------------------------------------------*/
DecodeSwitches: procedure expose GetOpt. length width custom_header dflg,
Fflg hflg nflg tflg
errflg = 0
optstr = 'Fdh:l:ntw:'
c = GetOpt(optstr)
do while c <> -1
select
when c = 'F' then
Fflg = 1
when c = 'd' then
dflg = 1
when c = 'h' then do
hflg = 1
custom_header = GetOpt._optarg
end
when c = 'l' then
if datatype(GetOpt._optarg, 'N') then
length = trunc(GetOpt._optarg)
else
errflg = 1
when c = 'n' then
nflg = 1
when c = 't' then
tflg = 1
when c = 'w' then
if datatype(GetOpt._optarg, 'N') then
width = trunc(GetOpt._optarg)
else
errflg = 1
otherwise
do
call Usage
exit 2
end
end /* select */
if errflg then do
say GetOpt._program ': Invalid argument for option' c
exit 2
end
c = GetOpt(optstr)
end /* while */
return
/* End of DecodeSwitches */
/*-------------------------------------------------------------------------
PrintFile - Print file
-------------------------------------------------------------------------*/
PrintFile: procedure expose dflg Fflg hflg nflg tflg custom_header,
length lpp width
parse arg file
if stream(file, 'C', 'QUERY EXISTS') = 'NOTREADY:' then
return
header = MakeHeader(file)
pagno = 0
lineno = 0
prtlines = 0
do while lines(file)
if \tflg then do
if prtlines >= lpp then do
if Fflg then
call charout , '0C'x
else
do i = prtlines+5 to length-1
say
end
prtlines = 0
end
if prtlines = 0 then
call PrintHeader header
end
printline = linein(file)
if nflg then
printline = format(lineno,5,0) printline
say strip(left(printline, width), 'T')
lineno = lineno + 1
if dflg then do
say
prtlines = prtlines + 2
end
else
prtlines = prtlines + 1
end
if \tflg & lineno > 0 then
if Fflg then
call charout , '0C'x
else
do i = prtlines+5 to length-1
say
end
return
/*-------------------------------------------------------------------------
PrintHeader - Print header on report
-------------------------------------------------------------------------*/
PrintHeader: procedure expose pagno
parse arg header
pagno = pagno + 1
say
say
say header format(pagno, 4, 0)
say
say
return
/*-------------------------------------------------------------------------
MakeHeader - Make header to print on reports
-------------------------------------------------------------------------*/
MakeHeader: Procedure expose hflg custom_header width
parse arg file
timestamp = stream(file, 'C', 'QUERY DATETIME')
if \hflg then
custom_header = file
header = right('Page', width-5)
custom_header = strip(center(custom_header, width), 'T')
header = overlay(custom_header, header)
return overlay(timestamp, header)
/*-------------------------------------------------------------------------
Usage - Print usage message.
-------------------------------------------------------------------------*/
Usage:
say GetOpt._program 'version 1.0, $Revision: 1.2 $'
say 'Copyright (c) 1994 Lawrence R Buchanan.'
say
say 'Usage:' GetOpt._program '[-dFnt] [-h header] [-l lines] [-w width] file ...'
return
/*-------------------------------------------------------------------------
GetOpt - parse options from REXX program command line
Copyright (c) 1994 Lawrence R Buchanan. ALL RIGHTS RESERVED.
-------------------------------------------------------------------------*/
GetOpt: procedure expose GetOpt.
parse arg optstr
i = GetOpt._optind
if GetOpt._sp = 1 then do
if GetOpt._optind > GetOpt.0 | ,
substr(GetOpt.i, 1, 1, '00'x) <> '-' | ,
substr(GetOpt.i, 2, 1, '00'x) = '00'x then
return -1
else
if GetOpt.i = '--' then do
GetOpt._optind = GetOpt._optind + 1
return -1
end
end
c = substr(GetOpt.i, GetOpt._sp+1, 1, '00'x)
GetOpt._optopt = c
cp = pos(c, optstr)
if c = ':' | cp = 0 then do
if GetOpt._opterr = 1 then
say GetOpt._program ': illegal option --' c
GetOpt._sp = GetOpt._sp + 1
if substr(GetOpt.i, GetOpt._sp+1, 1, '00'x) = '00'x then do
GetOpt._optind = GetOpt._optind + 1
GetOpt._sp = 1
end
return '?'
end
cp = cp + 1
if substr(optstr, cp, 1, '00'x) = ':' then do
if substr(GetOpt.i, GetOpt._sp+2, 1, '00'x) <> '00'x then do
GetOpt._optarg = substr(GetOpt.i, GetOpt._sp+2)
GetOpt._optind = GetOpt._optind + 1
end
else do
GetOpt._optind = GetOpt._optind + 1
i = GetOpt._optind
if GetOpt._optind > GetOpt.0 then do
if GetOpt._opterr = 1 then
say GetOpt._program ': option requires an argument --' c
GetOpt._sp = 1
return '?'
end
else do
GetOpt._optarg = GetOpt.i
GetOpt._optind = GetOpt._optind + 1
end
end
GetOpt._sp = 1
end
else do
GetOpt._sp = GetOpt._sp + 1
if substr(GetOpt.i, GetOpt._sp+1, 1, '00'x) = '00'x then do
GetOpt._sp = 1
GetOpt._optind = GetOpt._optind + 1
end
GetOpt._optarg = ''
end
return c
/* End of GetOpt */
/*-------------------------------------------------------------------------
SetupArg - Parse command-line arguments and store in stem GetOpt.
Copyright (c) 1994 Lawrence R Buchanan. ALL RIGHTS RESERVED.
-------------------------------------------------------------------------*/
SetupArg: procedure expose GetOpt.
parse arg arglist
/* Initialize variables used in GetOpt subroutine. */
GetOpt. = ''
GetOpt._opterr = 1
GetOpt._optind = 1
GetOpt._sp = 1
/* Place program name in GetOpt._program. */
parse source os . GetOpt._program .
if os = 'OS/2' then do
GetOpt._program = filespec('N', GetOpt._program)
GetOpt._program = delstr(GetOpt._program, lastpos('.', GetOpt._program))
end
/* Make sure the command-line contains an even number of
quotation characters. If it doesn't, I can't continue. */
if __SetupArg_CntQuo(arglist) // 2 then do
say GetOpt._program ': Unbalanced quotation marks in command-line'
exit 255
end
i = 0
/* Load command-line options into GetOpt.1 through GetOpt.n. */
do while arglist <> ''
i = i + 1
parse var arglist GetOpt.i arglist
/* If quoted argument, make sure we get it all from command-line. */
if pos('"', GetOpt.i) > 0 then do
cnt = __SetupArg_CntQuo(GetOpt.i)
parse var GetOpt.i opt '"' tmparg
GetOpt.i = opt || strip(tmparg, 'T', '"')
if cnt = 1 then do
parse var arglist remarg '"' arglist
GetOpt.i = GetOpt.i remarg
end
end
end
GetOpt.0 = i
return GetOpt.0
/* End of SetupArg */
/*-------------------------------------------------------------------------
__SetupArg_CntQuo - Count number of occurrences of '"' in str
Copyright (c) 1994 Lawrence R Buchanan. ALL RIGHTS RESERVED.
-------------------------------------------------------------------------*/
__SetupArg_CntQuo: procedure
parse arg str
cnt = 0
pos = pos('"', str)
do while pos > 0
cnt = cnt + 1
pos = pos('"', str, pos+1)
end
return cnt
/* End of __SetupArg_CntQuo */
/*-------------------------------------------------------------------------
This subroutine, in conjunction with a SIGNAL ON NOVALUE statement,
will display an error message (in sort-of Microsoft format) if the
program encounters an uninitialized variable.
-------------------------------------------------------------------------*/
SIG_NoValue:
parse source . . source_file .
say argv.0 '(' || sigl || '): Error: Variable' condition('D'),
'was not initialized prior to use'
exit
/*-------------------- R E V I S I O N H I S T O R Y --------------------
*
* $Log: D:\u\src\rexx\getopt\vcs\rpr.cmv $
*
* Rev 1.2 20 Apr 1994 16:14:30 rodb
* CHG: Renamed stem names by prepending a '_' (underscore), in an attempt to
* protect them from inadvertent use. (i.e., GetOpt.program is now
* GetOpt._program.)
*
* Rev 1.1 20 Apr 1994 12:08:54 rodb
* CHG: Major changes to GetOpt. stem variable. Done to make code more
* "in tune" with standard REXX stem variable behavior.
*
* Rev 1.0 16 Mar 1994 09:21:08 rodb
* Initial revision.
*
-------------------------------------------------------------------------*/