PROGRAM list_pascal_source_files;

CONST max_lines_per_page = 50;

TYPE command_string = STRING[127];

                   (* Note; This will not compile properly with
                      TURBO Pascal 2.0. Change the definition of
                      "input_line" to a STRING[140] if you are
                      using TURBO Pascal 2.0, since READ and
                      READLN do not work with ARRAY OF CHAR.  *)

VAR input_file      : TEXT;
    input_line      : ARRAY[1..140] OF CHAR;
    line_number     : INTEGER;
    lines_printed   : INTEGER;
    page_no         : INTEGER;
    index           : INTEGER;
    command_in      : command_string ABSOLUTE cseg:$80;
    command_temp    : command_string;
    command         : command_string;

PROCEDURE initialize; (* ****************************** initialize *)
BEGIN
  command := '';
  command_temp := command_in;  (* leave the input area unchanged *)
  WHILE (length(command_temp) > 0) AND (command_temp[1] = ' ') DO
    delete(command_temp,1,1);
  WHILE (length(command_temp) > 0) AND (command_temp[1] <> ' ') DO
    BEGIN
      command := command + command_temp[1];
      delete(command_temp,1,1);
    END;
  ASSIGN(input_file,command);
  RESET(input_file);
  line_number := 1;
  lines_printed := 66; (* This is to force a header immediately *)
  page_no := 1;
END;

PROCEDURE read_a_line; (* **************************** read a line *)
BEGIN
  FOR index := 1 TO 140 DO input_line[index] := ' ';
  READLN(input_file,input_line);
END;

PROCEDURE format_and_display; (* **************** format and display *)

VAR line_length : BYTE;

BEGIN
  WRITE(line_number:6,'  ');
    FOR index := 1 TO 140 DO
    BEGIN
      IF input_line[index] <> ' ' THEN line_length := index;
    END;
  IF line_length <= 70 THEN
    BEGIN               (* line length less than 70 characters *)
      FOR index := 1 TO line_length DO
        WRITE(input_line[index]);
      WRITELN;
    END
  ELSE
    BEGIN               (* line length more than 70 characters *)
      FOR index := 1 TO 70 DO
        WRITE(input_line[index]);
      WRITELN('<');
      WRITE('        ');
      FOR index := 71 TO line_length DO
        WRITE(input_line[index]);
      WRITELN;
    END;
END;

PROCEDURE format_and_print; (* ****************** format and print *)

VAR line_length : BYTE;

BEGIN
  WRITE(lst,line_number:6,'  ');
    FOR index := 1 TO 140 DO
    BEGIN
      IF input_line[index] <> ' ' THEN line_length := index;
    END;
  IF line_length <= 70 THEN
    BEGIN               (* line length less than 70 characters *)
      FOR index := 1 TO line_length DO
        WRITE(lst,input_line[index]);
      WRITELN(lst);
      lines_printed := lines_printed + 1;
    END
  ELSE
    BEGIN               (* line length more than 70 characters *)
      FOR index := 1 TO 70 DO
        WRITE(lst,input_line[index]);
      WRITELN(lst,'<');
      WRITE(lst,'        ');
      FOR index := 71 TO line_length DO
        WRITE(lst,input_line[index]);
      WRITELN(lst);
      lines_printed := lines_printed + 2;
    END;
  line_number := line_number + 1;
END;

PROCEDURE check_for_page; (* ********************** check for page *)
BEGIN
  IF lines_printed > Max_lines_per_page THEN
  BEGIN
    IF page_no > 1 THEN WRITELN(lst,char(12));
    FOR index := 1 TO 3 DO WRITELN(lst);
    WRITE(lst,'     ');
    WRITELN(lst,'Source file ',command,'Page':24,page_no:4);
    page_no := page_no + 1;
    lines_printed := 1;
    WRITELN(lst);
  END;
END;

BEGIN  (* ******************************************* main program *)
  initialize;
  check_for_page;
  REPEAT
    read_a_line;
    format_and_display;
    format_and_print;
    check_for_page;
  UNTIL eof(input_file);
  WRITELN(lst,char(12));
END.  (* of main program *)