* Program: REPORT * Read stdin and print to printer file * * To compile: * CRTRPGMOD MODULE(xxx/REPORT) SRCFILE(xxx/QRPGLESRC) SRCMBR(REPORT) * CRTPGM PGM(xxx/REPORT) MODULE(REPORT) BNDSRVPGM(STDIO) BNDDIR(QC2LE) H option(*srcstmt: *nodebugio) Fqsysprt o f 198 printer oflind(*inof) usropn /copy prototypes,stdio // ==================================== // Options: // -c : count lines down left side of page // -C : display count of lines at end of report // -d : display output to stdout (in addition to printing) // -h : print page headings // -n : print narrow report -- 132 characters (default) // -w : print wide report -- 198 characters // -x : print extra narrow report -- 80 characters // -1 : single space (default) // -2 : double space // -3 : triple space // ==================================== D Report pr extpgm('REPORT') D Arg01 50a D Arg02 50a D Arg03 50a D Arg04 50a D Arg05 50a D Arg06 50a D Arg07 50a D Arg08 50a D Arg09 50a D Arg10 50a D Report pi D Arg01 50a D Arg02 50a D Arg03 50a D Arg04 50a D Arg05 50a D Arg06 50a D Arg07 50a D Arg08 50a D Arg09 50a D Arg10 50a D AddChar pr D Char 1a const D String 4096a varying D MaxLength 10i 0 const D GetString pr 256 varying D String 50a value D QCmdExc pr extpgm('QCMDEXC') D Cmd 256a const D Length 15p 5 const D sds D time 282 287s 0 * Option flags D EchoOutput s n D NumberLines s n D CountLines s n D PrintPageHdr s n D NoMoreParms s n D datalen s 10i 0 D pssrStatus s n D CurrentChar s 1a D CurrentArg s like(Arg01) D CurrentOption s 1a D PageHeadingTxt s 80a D PrtLine s 198a D Info s 4096a varying D Workfo s 4096a varying D LineLength s 10u 0 inz(132) D LineCounter s 4s 0 D PageCounter s 4s 0 D i s 10u 0 D ix s 10u 0 D Cmd s 256a D CPI s 2a inz('10') D Spacing s 1a inz('1') /free exsr GetArgs; exsr InzReport; datalen = stdin (info); dow datalen > *zero; for i = 1 to datalen; CurrentChar = %subst(info:i:1); select; when CurrentChar = TAB; dou %rem(%len(Workfo): 8) = *zero; AddChar (' ': Workfo: LineLength); enddo; when CurrentChar = CR; // ignore when CurrentChar = LF; LineCounter = LineCounter + 1; if NumberLines; workfo=%editc(LineCounter:'4')+':'+Workfo; endif; exsr PrintDetail; %len(Workfo) = *zero; when CurrentChar >= *blank; AddChar (CurrentChar: Workfo: LineLength); endsl; endfor; datalen = stdin (info); enddo; *inlr = *on; exsr EndReport; exit (0); // ********************************** // // * Print a detail line * // // ********************************** // begsr PrintDetail; exsr OverFlow; if %len(Workfo) <= LineLength; prtline = Workfo; else; prtline = %subst(Workfo:1:LineLength); endif; exsr Output; endsr; // ********************************** // // * Handle page overflow * // // ********************************** // begsr OverFlow; if *inof = *on; except newpage; if PrintPageHdr; PageCounter = PageCounter + 1; prtline = %trimr(PageHeadingTxt) + ' Page ' + %editc(PageCounter:'1'); exsr Output; endif; *inof = *off; endif; endsr; // ********************************** // // * Begin the report * // // ********************************** // begsr InzReport; if LineLength = 198; CPI = '15'; else; CPI = '10'; endif; Cmd = 'OVRPRTF QSYSPRT PAGESIZE(*N ' + %CHAR(LineLength) + ') CPI(' + CPI + ')'; callp QCmdExc (Cmd: %size(Cmd)); open qsysprt; *inof = *on; PageHeadingTxt = %trimr(PageHeadingTxt) + ' ' + %editc(*date: 'Y') + ' ' + %editw(time: ' : : '); endsr; // ************************************** // // * Process the command-line arguments * // // ************************************** // begsr GetArgs; NumberLines = *off; CountLines = *off; if %parms > *zero; CurrentArg = GetString(Arg01); exsr GetOneArg; endif; if %parms > 1; CurrentArg = GetString(Arg02); exsr GetOneArg; endif; if %parms > 2; CurrentArg = GetString(Arg03); exsr GetOneArg; endif; if %parms > 3; CurrentArg = GetString(Arg04); exsr GetOneArg; endif; if %parms > 4; CurrentArg = GetString(Arg05); exsr GetOneArg; endif; if %parms > 5; CurrentArg = GetString(Arg06); exsr GetOneArg; endif; if %parms > 6; CurrentArg = GetString(Arg07); exsr GetOneArg; endif; if %parms > 7; CurrentArg = GetString(Arg08); exsr GetOneArg; endif; if %parms > 8; CurrentArg = GetString(Arg09); exsr GetOneArg; endif; if %parms > 9; CurrentArg = GetString(Arg10); exsr GetOneArg; endif; endsr; // ************************************* // // * Process one command-line argument * // // ************************************* // begsr GetOneArg; if NoMoreParms; exsr ProcessPositionalParm; else; select; when %subst(CurrentArg: 1 : 2) = '--'; NoMoreParms = *on; when %subst(CurrentArg: 1 : 1) = '-'; for ix = 2 to %len(CurrentArg); CurrentOption = %subst(CurrentArg: ix: 1); exsr SetOneOption; endfor; other; exsr ProcessPositionalParm; NoMoreParms = *on; endsl; endif; endsr; // ********************************* // // * Process a non-option argument * // // ********************************* // begsr ProcessPositionalParm; PageHeadingTxt = CurrentArg; endsr; // ******************************* // // * Process one option from the * // // * command-line arguments * // // ******************************* // begsr SetOneOption; select; when CurrentOption = 'c'; NumberLines = *on; when CurrentOption = 'C'; CountLines = *on; when CurrentOption = 'd'; EchoOutput = *on; when CurrentOption = 'x'; LineLength = 80; when CurrentOption = 'n'; LineLength = 132; when CurrentOption = 'w'; LineLength = 198; when CurrentOption = 'h'; PrintPageHdr = *on; when CurrentOption = '1' or CurrentOption = '2' or CurrentOption = '3'; Spacing = CurrentOption; endsl; endsr; // ******************************* // // * End of report * // // ******************************* // begsr EndReport; if CountLines; prtline = 'Total records: ' + %char(LineCounter); exsr Output; endif; endsr; // ********************************** // // * write to QSYSPRT, & optionally * // // * to stdout * // // ********************************** // begsr Output; except pline; if Spacing > '1'; except spacer; endif; if Spacing > '2'; except spacer; endif; if EchoOutput; stdout(prtline + CRLF); endif; endsr; // ********************************** // // * unexpected error routine * // // ********************************** // begsr *pssr; *inlr = *on; if pssrStatus = *off; pssrStatus = *on; else; return; endif; stderr ('Unexpected error.' + CRLF); exit (1); return; endsr; /end-free Oqsysprt e newpage 5 O e pline 1 O prtline O e spacer 1 ********************************************** * Append a character to the end of a string. ********************************************** P AddChar b D pi D Char 1a const D String 4096a varying D MaxLength 10i 0 const /free if %len(String) < MaxLength; String = String + Char; endif; /end-free P e ********************************************** * Extract a null-terminated string from * an input parameter. ********************************************** P GetString b D pi 256 varying D String 50a value D D ndx s 10i 0 D /free ndx = 1; dow '0' = '0'; if ndx > %len(String); leave; endif; if %subst(String: ndx: 1) = x'00'; leave; endif; ndx = ndx + 1; enddo; return (%subst(String: 1: ndx -1)); /end-free P e