home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / LIST01.ZIP / LIST01.PAS
Encoding:
Pascal/Delphi Source File  |  1980-01-01  |  9.9 KB  |  339 lines

  1. PROGRAM LIST; {12/27/84}
  2.  
  3. { Revised version 6/9/85}
  4.  
  5. { Compile with TURBO PASCAL.
  6.  
  7. LIST -  Program source code listing utility.
  8.  
  9. Will format and print standard ASCII files:
  10.   -Allows up to 20 files to be specified to be printed batch style.
  11.   -Paginates (standard 8 1/2 X 11 paper, 80-column printer).
  12.   -Correct pagination even if some lines exceed right margin and printer
  13.       "wraps" to next line.
  14.   -Allows you to specify a left margin (default is 5), so listing can
  15.       be inserted in loose-leaf binder.
  16.   -Prints header with file name, system date, and page number.
  17.  
  18. Usage:
  19.  
  20. For printing several files (up to 20):
  21.   Have printer ready, then type LIST <Enter> at DOS prompt and respond
  22.   to prompts in program.  Accepts drive designator for non-default drive,
  23.   but not DOS pathnames.  Begins printing immediately after left margin
  24.   offset is entered.
  25.  
  26. For printing a single file:
  27.   Issue command at DOS prompt as
  28.  
  29.           LIST filename.ext [left margin offset, default 5]
  30.           e.g., LIST FOO.PAS 8
  31.  
  32. Author:
  33.   Frank L. Eskridge
  34.   2895 Hill Park Court
  35.   Marietta, GA  30062
  36.   (404) 973-1714                                                         }
  37.  
  38. { Revision notice 6/9/85:
  39.  
  40.   1.  Added routine to automatically pick up system date (MS-DOS) and
  41.       print as header.
  42.   2.  Removed option to add user's header (I never used it except to
  43.       put in the date).
  44.   3.  Added option to enter at command line with this syntax:
  45.  
  46.               LIST [<filename>] [<left margin offset>]
  47.  
  48.               e.g.,  LIST FOO.PAS 12
  49.  
  50.        Default left margin is 5.                                        }
  51.  
  52.  
  53. TYPE  namestring=STRING[14];
  54.  
  55. VAR
  56.    input_file            :TEXT;
  57.    filename              :ARRAY [1..20] OF NAMESTRING;
  58.    header                :STRING[50];
  59.    line                  :STRING[255];
  60.    header_length,
  61.    offset,i,c,y          :INTEGER;
  62.    ok                    :BOOLEAN;
  63.  
  64. {The following routine reads the system date and is called by simply
  65. using DATE where you wnat the date to appear.  This was copied from
  66. a public domain program called DATETI.PAS, no credit given.
  67. It is completely unchanged except I left off the Time function.
  68.                                                                     }
  69. type
  70.   datetimetype = string[8];
  71.   regtype = record
  72.       ax,bx,cx,dx,bp,si,di,ds,es,flags: integer;
  73.     end;
  74.  
  75. function date: datetimetype;
  76.   var
  77.     reg: regtype;
  78.     y,m,d,w: datetimetype;
  79.     i: integer;
  80.   begin
  81.     reg.ax:=$2A00;
  82.     intr($21,reg);
  83.     str(reg.cx:4,y);
  84.     delete(y,1,2);
  85.     str(hi(reg.dx):2,m);
  86.     str(lo(reg.dx):2,d);
  87.     w:=m+'/'+d+'/'+y;
  88.     for i:=1 to length(w) do if w[i]=' ' then w[i]:='0';
  89.     date:=w;
  90.   end;
  91. {End of DATE function.}
  92.  
  93. { The next function is from a program by Jim Holtman of Randloph,
  94.   New Jersey, called ARGLIST.PAS.  I cleaned it up a little in order
  95.   to understand it myself, but basically it is unchanged.  The next
  96.   paragraph is Holtman's.
  97.  
  98.   This provides capabilities similar to argc/argv in C. You can now
  99.   read the argument list from your TURBO Pascal program. `argc' is
  100.   actually a function that returns the number of parameters on the
  101.   command line. Spaces are the separators between parameters. If the
  102.   parameter is enclosed in quotes ('), then any characters can appear.
  103.   If you want a quote, put two in the command line parameter.
  104.                                                                         }
  105.  
  106.  
  107. TYPE
  108.     arglist_string = string[80];
  109. CONST
  110.     arglist_max = 20;
  111.     arglist_number : integer = -1;
  112. VAR
  113.    argvlist : array[1..arglist_max] of ^arglist_string;
  114.  
  115. FUNCTION argv ( num : integer ) : arglist_string;
  116.    VAR
  117.       argument : arglist_string absolute cseg:$80;
  118.       newparm,parmline : arglist_string;
  119.       i,j : INTEGER;
  120.       state : (leading_ws, non_quote, quoted, end_quote);
  121.       inchar : CHAR;
  122.  
  123.       PROCEDURE saveparm;
  124.          BEGIN
  125.             IF arglist_number < arglist_max THEN
  126.                BEGIN
  127.                   arglist_number := arglist_number+1;
  128.                   new(argvlist[arglist_number]);
  129.                   argvlist[arglist_number]^ := newparm;
  130.                   newparm := '';
  131.                END;
  132.          END;
  133.  
  134.    BEGIN {function argv}
  135.       IF arglist_number = -1 THEN
  136.          BEGIN
  137.             arglist_number := 0;
  138.             parmline := argument+' ';
  139.             state := leading_ws;
  140.             newparm := '';
  141.             FOR i := 1 to length(parmline) DO
  142.                BEGIN
  143.                   inchar := parmline[i];
  144.                   CASE state OF
  145.                      leading_ws:
  146.                         BEGIN
  147.                            IF inchar = '''' THEN state := quoted
  148.                            ELSE IF inchar <> ' ' THEN
  149.                               BEGIN
  150.                                  newparm := newparm+inchar;
  151.                                  state := non_quote;
  152.                               END;
  153.                         END;
  154.  
  155.                      non_quote:
  156.                         BEGIN
  157.                            IF inchar = ' ' THEN
  158.                               BEGIN
  159.                                  saveparm;
  160.                                  state := leading_ws;
  161.                               END
  162.                            ELSE newparm := newparm+inchar;
  163.                         END;
  164.  
  165.                      quoted:
  166.                         BEGIN
  167.                            IF inchar = '''' THEN state := end_quote
  168.                            ELSE newparm := newparm+inchar;
  169.                         END;
  170.  
  171.                      end_quote:
  172.                         BEGIN
  173.                            IF inchar = '''' THEN
  174.                               BEGIN
  175.                                  newparm := newparm+inchar;
  176.                                  state := quoted;
  177.                               END
  178.                            ELSE IF inchar <> ' ' THEN
  179.                               BEGIN
  180.                                  newparm := newparm+inchar;
  181.                                  state := non_quote;
  182.                               END
  183.                            ELSE
  184.                               BEGIN
  185.                                  saveparm;
  186.                                  state := leading_ws;
  187.                               END;
  188.                         END;
  189.                   END; {case}
  190.                END;
  191.          END;
  192.  
  193.          IF (num > 0) AND (num <= arglist_number) THEN
  194.             argv := argvlist[num]^
  195.          ELSE argv := '';
  196.    END; {function argv}
  197.  
  198.  
  199. FUNCTION argc:integer;
  200.   VAR
  201.      dummy : arglist_string;
  202.   BEGIN {function argc}
  203.      IF arglist_number = -1 THEN
  204.      BEGIN
  205.         dummy := argv(1); {force evaluation}
  206.         argc := arglist_number;
  207.      END
  208.      ELSE
  209.         argc := arglist_number;
  210.   END;
  211.  
  212. {End of command line argument functions}
  213.  
  214. PROCEDURE SPACE(number:INTEGER);
  215. VAR x : INTEGER;
  216. BEGIN
  217.    FOR x := 1 TO number DO
  218.       WRITE(lst,' ');
  219. END;
  220.  
  221.  
  222. PROCEDURE LINE_FEED;
  223. BEGIN
  224.    WRITELN(lst,'');
  225. END;
  226.  
  227.  
  228. PROCEDURE CONVERT_TO_UPPER(VAR allcaps:namestring);
  229. VAR x       :INTEGER;
  230.     ch      :CHAR;
  231.     newword :NAMESTRING;
  232. BEGIN
  233.    newword := '';
  234.    FOR x := 1 TO LENGTH(allcaps) DO
  235.       BEGIN
  236.          ch := allcaps[x];
  237.          newword := newword + upcase(ch);
  238.       END;
  239.       allcaps := newword;
  240. END;
  241.  
  242.  
  243. PROCEDURE GET_FILENAMES;
  244. VAR ch :CHAR;
  245. BEGIN
  246.    i := 1;
  247.    REPEAT
  248.       WRITE('Name of file to list on printer (CR to end): ');
  249.       READLN(filename[i]);
  250.       CONVERT_TO_UPPER(filename[i]);
  251.       i := i+1;
  252.    UNTIL filename[i-1] = '';
  253. END;
  254.  
  255.  
  256. PROCEDURE GET_OFFSET;
  257. VAR cnum:   STRING[2];
  258.     code:   INTEGER;
  259. BEGIN
  260.    REPEAT
  261.       WRITE('Number of columns to offset left margin [5]: ');
  262.       READLN(cnum);
  263.       IF cnum = ''THEN cnum:='5';
  264.       VAL(cnum,offset,code);
  265.       IF (offset<0) OR (offset>50) THEN
  266.          WRITELN(#7+'Please enter a number between 0 and 50...');
  267.    UNTIL (offset>=0) AND (offset<51);
  268. END;
  269.  
  270.  
  271. PROCEDURE OPEN(name:namestring);
  272. BEGIN
  273.    ASSIGN(input_file,filename[c]);
  274.    {$I-}RESET(input_file) {$I+};
  275.    ok := (IOResult=0);
  276.    IF NOT ok THEN WRITELN(#7+' ----> Invalid filename--ignoring.');
  277. END;
  278.  
  279.  
  280. PROCEDURE PRINT_FILE(name:namestring);
  281. VAR page,ln : INTEGER;
  282. BEGIN
  283.    page := 1;
  284.    header_length := LENGTH(filename[c])+offset+10;
  285.    WHILE NOT EOF(input_file) DO
  286.       BEGIN
  287.          SPACE(offset);
  288.          WRITE(lst,filename[c]+'  '+date);
  289.          SPACE(65-header_length);
  290.          WRITE(lst,'Page');
  291.          WRITELN(lst,page:3);
  292.          LINE_FEED;LINE_FEED;
  293.          LN := 5;
  294.          WHILE (LN < 60) AND (NOT EOF(input_file)) DO
  295.             BEGIN
  296.                READLN(input_file,line);
  297.                SPACE(offset);
  298.                WRITELN(lst,line);
  299.                IF LENGTH(line) > 80-offset THEN LN := LN+1;
  300.                LN := LN+1;
  301.             END;
  302.          WRITE(lst,^L);
  303.          page := page + 1;
  304.       END;
  305. END;
  306.  
  307.  
  308. BEGIN {main program}
  309.    IF argc = 0 THEN BEGIN
  310.       WRITELN('LIST --  Formats and prints up to 20 ASCII files.');
  311.       WRITELN('(Single file: use command-line entry--syntax: ');
  312.       WRITELN('  LIST filename.ext [left margin, default 5] ');
  313.       WRITELN('         Example:  LIST FOO.PAS 8             )');
  314.       WRITELN('-------------------------------------------------');
  315.       GET_FILENAMES;
  316.       GET_OFFSET;
  317.       WRITELN;
  318.    END;
  319.    IF argc > 0 THEN BEGIN
  320.       filename[1] := argv(1);
  321.       CONVERT_TO_UPPER(filename[1]);
  322.       IF argc = 2 THEN VAL(argv(2),offset,y) ELSE offset := 5;
  323.       i := 3;
  324.       WRITELN;WRITELN;WRITELN;
  325.    END;
  326.  
  327.    FOR c := 1 TO (i-2) DO
  328.       BEGIN
  329.          WRITE('Printing ---> '+filename[c]);
  330.          OPEN(filename[c]);
  331.          IF ok THEN
  332.             BEGIN
  333.                PRINT_FILE(filename[c]);
  334.                CLOSE(input_file);
  335.                WRITELN(' ----> Done');
  336.             END;
  337.       END;
  338. END.
  339.