home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / psc1.zip / PSC.CMD
OS/2 REXX Batch file  |  1991-01-03  |  9KB  |  177 lines

  1. /* --------------------------------------------------------------- */
  2. /* -- PSC      .CMD          (PrintSourceCode)                  -- */
  3. /* --------------------------------------------------------------- */
  4. /* --  Prints a text file on the default printer with a header  -- */
  5. /* --  showing filename, date, time, page- and line-numbers.    -- */
  6. /* --                                                           -- */
  7. /* --  The file is printed in NLQ, 15 cpi, 58 lines per page,   -- */
  8. /* --  and  with 1" margin (using a Panasonic/Epson printer).   -- */
  9. /* --                                                           -- */
  10. /* --  Embedded page breaks are handled, as well as the page-   -- */
  11. /* --  break comment string 'c1 <f> c2', where c1 and c2 are    -- */
  12. /* --  beginning and ending comment delimiters respectivly.     -- */
  13. /* --  These can be:                                            -- */
  14. /* --    c1='/*' c2='*/' for SAS and REXX programs              -- */
  15. /* --    c1='(*' c2='*)' for Modula-2 programs                  -- */
  16. /* --  (This is a leftover from source code prepared for use    -- */
  17. /* --   with SourcePrint, an old PC-DOS program I have used.)   -- */
  18. /* --                                                           -- */
  19. /* --------------------------------------------------------------- */
  20. /* --  This program comes with no guarantees of any kind.  It   -- */
  21. /* --  was developed for my own use, and as an illustration of  -- */
  22. /* --  what is possible to with REXX under OS/2.                -- */
  23. /* --                                                           -- */
  24. /* --  If you have any comments, you can reach me with e-mail   -- */
  25. /* --  at the following addresses:                              -- */
  26. /* --   BitNet: berglano@orstvm.ucs.orst.edu                    -- */
  27. /* --   BIX:    obergland                                       -- */
  28. /* --                                                           -- */
  29. /* --------------------------------------------------------------- */
  30. /* -- History:                                                  -- */
  31. /* --                                                           -- */
  32. /* -- 02.09.90 Olvar Bergland                           REXX/2  -- */
  33. /* --------------------------------------------------------------- */
  34.  
  35. /* --- check for correct syntax                                    */
  36. IF ARG() = 0 THEN SIGNAL syntax
  37.  
  38. /* --- initalize variables                                         */
  39. esc      = "1B"x                   /* -- ESC character             */
  40. ff       = "0C"x                   /* -- page break char           */
  41. cr       = "0D"x                   /* -- carriage return           */
  42. zero     = "00"x                   /* -- zero character            */
  43. linesize = 112                     /* -- length of a line          */
  44. pagesize =  58                     /* -- length of a page          */
  45. pageno   =   1                     /* -- page number               */
  46. sourceno =   1                     /* -- source line number        */
  47. lineno   =   1                     /* -- page line number          */
  48. newpage  = "<f>"                   /* -- page break string         */
  49. pbrk1    = "/* "||newpage||" */"   /* -- page break comment        */
  50. pbrk2    = "(* "||newpage||" *)"   /* -- page break comment        */
  51.  
  52. topline  = COPIES('─', linesize)
  53. contline = OVERLAY(' continued '  , '─', linesize - 25, 26, '─')
  54. eofline  = OVERLAY(' end-of-file ', '─', linesize - 26, 27, '─')
  55. dateline = STRIP(CENTER(DATE() TIME('C'),linesize),"t")
  56.  
  57. /* --- check for command line options                              */
  58. ARG fileid                        /* get filename from cmdline     */
  59.  
  60. /* <f> */
  61. /* --- does the file exist ?                                       */
  62. CALL STREAM fileid, C, "query exists"       /* check for existence */
  63. IF RESULT = ""                              /* check the result    */
  64. THEN DO
  65.   SAY ""
  66.   SAY "Couldn't find the specified file:" fileid
  67.   EXIT
  68.   END
  69. filename = RESULT                           /* full filename       */
  70. SAY "File found:" filename " (FileSize:" ,
  71.     STREAM(fileid,C,'query size') "bytes.)"
  72.  
  73. /* --- check the file name size                                    */
  74. IF LENGTH(filename) + 3 > (linesize - LENGTH(STRIP(dateline)))/2
  75. THEN DO
  76.   /* the filename is a little bit too long */
  77.   indx = POS('\',filename)
  78.   filename = DELSTR(filename,indx+1,LASTPOS('\',filename)-indx-1)
  79.   filename = INSERT('..',filename,indx)
  80.   END
  81.  
  82. /* --- set printer options (for Panasonic)                         */
  83. CALL CHAROUT prn,esc||'m'||'02'x            /* set IBM graphics I  */
  84. CALL CHAROUT prn,esc||"n"                   /* set NLQ             */
  85. CALL CHAROUT prn,esc||"w"||'02'x            /* set 15 cpi          */
  86. CALL CHAROUT prn,esc||"l"||'08'x            /* set margin          */
  87.   /* could use cmdline switches for these and other options        */
  88.  
  89. /* <f> */
  90. CALL newpage                                /* write first header  */
  91. /* --- start main program loop */
  92. DO WHILE LINES(fileid) > 0                  /* repeat until eof    */
  93.    IF lineno > pagesize                     /* at bottom of page   */
  94.    THEN DO
  95.       CALL newpage
  96.       END
  97.    nextline = STRIP(LINEIN(fileid),"t")     /* read next line      */
  98.  
  99.    /* --- check for embedded page breaks                           */
  100.    IF (POS(pbrk1,nextline) <> 0) |,         /* page break comment  */
  101.       (POS(pbrk2,nextline) <> 0)            /* page break comment  */
  102.    THEN DO
  103.       sourceno = sourceno + 1
  104.       CALL newpage
  105.       END
  106.    ELSE DO
  107.       /* --- make sure there are no hidden page breaks             */
  108.       IF SUBSTR(nextline,1,1) = ff          /* page break char     */
  109.       THEN DO
  110.          CALL newpage
  111.          nextline = SUBSTR(nextline,2,LENGTH(nextline)-1)
  112.          END
  113.  
  114.       CALL LINEOUT prn, RIGHT(sourceno,6) ": " nextline /* print  */
  115.       sourceno = sourceno + 1
  116.       lineno = lineno + 1
  117.       END
  118.    END                                      /* end of WHILE loop   */
  119.  
  120. /* --- write end of file comments to printer                       */
  121. CALL LINEOUT prn,""                         /* extra line          */
  122. DO WHILE lineno <= pagesize
  123.    CALL LINEOUT prn,""                      /* extra line          */
  124.    lineno = lineno + 1
  125.    END
  126. CALL LINEOUT prn, eofline                   /* continuation line   */
  127.  
  128. /* --- clean up & reset the printer                                */
  129. CALL CHAROUT prn,esc||"l"||zero             /* reset margin        */
  130. CALL CHAROUT prn,esc||"P"                   /* select draft mode   */
  131. CALL CHAROUT prn,esc||"w"||zero             /* select 10 cpi       */
  132. CALL CHAROUT prn,esc||'m'||zero             /* select standard mode*/
  133. CALL CHAROUT prn,ff                         /* top of next page    */
  134. CALL CHAROUT prn,cr                         /* empty print buffer  */
  135.  
  136. CALL LINEOUT fileid                         /* close input file    */
  137. CALL LINEOUT prn                            /* close printer       */
  138.  
  139. EXIT                              /* --  end of main            -- */
  140.  
  141. /* <f> */
  142. /* --- subroutine for newpage                                   -- */
  143. newpage:
  144.  
  145.    /* -- use ANSI sequence to stay at the same location            */
  146.    CALL CHAROUT con, esc||'[s'              /* report progress     */
  147.    CALL CHAROUT con, "Printing Page" pageno /*                     */
  148.    CALL CHAROUT con, esc||'[u'
  149.  
  150.    IF pageno \= 1
  151.    THEN DO
  152.      CALL LINEOUT prn,""                    /* extra line          */
  153.      DO WHILE lineno <= pagesize
  154.        CALL LINEOUT prn,""                  /* extra line          */
  155.        lineno = lineno + 1
  156.        END
  157.      CALL LINEOUT prn, contline             /* continuation line   */
  158.      CALL CHAROUT prn,ff                    /* form-feed char      */
  159.      END
  160.    CALL LINEOUT prn,""                      /* extra line          */
  161.    pageline  = RIGHT("Page" FORMAT(pageno,3),linesize)
  162.    finalline = OVERLAY(filename,OVERLAY(dateline,pageline))
  163.    CALL LINEOUT prn,finalline
  164.    CALL LINEOUT prn,topline
  165.    CALL LINEOUT prn,""
  166.    pageno = pageno + 1
  167.    lineno = 1
  168.    RETURN                         /* --  end of subroutine      -- */
  169.  
  170.  
  171. /* --- incorrect syntax                                         -- */
  172. syntax:
  173.    SAY ""
  174.    SAY "Usage: PSC filename"
  175.    EXIT
  176.    END                            /* --  end of subroutine      -- */
  177.