PROGRAM NList;

{U+}
{Epson MX80 printer controller ASCII file Print program}
{Entered in the Public Domain by Nathan Liskov}
{Adapted from Turbo Users Group Vol 1 Issue 3}
{Can be invoked with filename as a parameter: nlist filename}
{Feb 10, 1985}

TYPE 
  DateTimeStr = STRING[26];
  OnorOff     = ARRAY[1..2] OF STRING[3];
  parmtype = STRING[127];
  maxspaces = STRING[132];

VAR 
    linecount, n, m, page, doublespace, linelength : integer;
    topspaces, bottomspaces, leftmargin, rightmargin, lm, rm : integer;
    option   : char;
    pagestr  : STRING[3];
    filename : STRING[45];
    temp, lineout     : STRING[255];
    right, left : maxspaces;
    source   : text;
    linemode, double, emphasized, header, automatic, maxline : integer;
    x        : parmtype;
    hellfreezesover : boolean;
    datetimestamp: datetimestr;

  CONST 
    onoff: onoroff = ('On ','Off');

PROCEDURE init;
  BEGIN
    linemode := 80;
    write(lst,chr(18));        {set line mode to 80}
    double   := 2;
    write(lst,chr(27),chr(72)); {set double strike off}
    emphasized := 2;
    write(lst,chr(27),chr(70)); {set emphasized off}
    header   := 1;           {default is header line on}
    doublespace := 2;         {default is single spaces}
    automatic := 2;          {default is zero margins}
    topspaces := 1;
    bottomspaces := 0;
    leftmargin := 0;
    rightmargin := 0;
    right := '';
    left := '';
    IF paramcount<>0
      THEN BEGIN
             filename := paramstr(1);
             assign(source,filename);
          END
      ELSE filename := '';
    hellfreezesover := false;
  END;

FUNCTION DateTime: DateTimeStr;

TYPE 
  regpack = RECORD
              ax,bx,cx,dx,bp,si,ds,es,flags: integer;
            END;
  dayname = STRING[3];

  TYPE monthname = ARRAY[1..12] OF STRING[3];

    CONST mon: monthname = ('Jan','Feb','Mar','Apr','May','Jun',
                            'Jul','Aug','Sep','Oct','Nov','Dec');

VAR 
  recpack:       regpack;                {record for MsDos call}
  day,hours,minutes,seconds:     STRING[2];
  year:          STRING[4];
  month,dx,cx,daynumber,yearnumber:         integer;
  dayoftheweek : dayname;

FUNCTION DayofWeek(juliandate:real): dayname;
{finds day of week for 10 feb 1985 or later}

TYPE  daynames = ARRAY[1..7] OF STRING[3];

  CONST day: daynames = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

VAR   daynumber : real;

BEGIN
  daynumber := (juliandate + 1.5)/7;
  daynumber := daynumber -349444.0;   {sun 10 feb 1985}
  WHILE daynumber > 32000 DO
    daynumber := daynumber - 32000;
  daynumber := (daynumber - trunc(daynumber))*7;
  dayofweek := day[round(daynumber)+1];
END;

FUNCTION juliandate(daynumber, monthnumber, yearnumber:integer): real;

VAR  a,b,c,d : real;

BEGIN
  IF monthnumber < 3
    THEN
      BEGIN
        yearnumber := yearnumber -1;
        monthnumber := monthnumber + 12;
      END;
  a := trunc(yearnumber/100)*1.0;
  b := 2-a+trunc(a/4)*1.0;
  c := 365.0 * yearnumber+trunc(yearnumber/4);
  d := trunc(30.6001*(monthnumber+1));
  juliandate := b+c+d+1720994.5+daynumber;
{  writeln('julian date ',b+c+d+1720994.5+daynumber:10:1);}
END;

BEGIN
  WITH recpack DO
    BEGIN
      ax := $2a shl 8;
    END;
  MsDos(recpack);                        { call function }
  WITH recpack DO
    BEGIN
      str(cx,year);                        {convert to string}
      yearnumber := cx;
      daynumber := dx MOD 256;
      str(daynumber,day);                     { " }
      month := dx shr 8;
    END;
  WITH recpack DO
    BEGIN
      ax := $2c shl 8;
    END;
  MsDos(recpack);
  WITH recpack DO
    BEGIN
      str(cx shr 8,hours);
      IF (cx shr 8)<10
        THEN hours := '0'+hours;
      str(cx MOD 256,minutes);
      IF (cx MOD 256)<10
        THEN minutes := '0'+minutes;
      str(dx shr 8,seconds);
      IF (dx shr 8)<10
        THEN seconds := '0'+seconds;
    END;
  dayoftheweek := (dayofweek(juliandate(daynumber,month,yearnumber)));
  IF daynumber > 9
    THEN
      datetime := dayoftheweek+' '+day+' '+mon[month]+' '+year
                  +'   '+hours+':'+minutes+':'+seconds
    ELSE
      datetime := dayoftheweek+' '+' '+day+' '+mon[month]+' '+year+'   '
                  +hours+':'+minutes+':'+seconds;
END;


PROCEDURE optionline;
  BEGIN
    gotoxy(1,21);
    normvideo;
    writeln('   Enter Option Choice                                                ');
    gotoxy(36,21);
  END;

PROCEDURE menu;  {gives main menu options}
  BEGIN
    clrscr;
    lowvideo;
    writeln('Printer Utility for File Listing on MX-80');
    writeln('____________By Nathan Liskov_____________');
    writeln;
    writeln('   0 := Form Feed');
    writeln('   1 := Line Feed');
    writeln('   2 := Characters/Line.         : ',linemode);
    writeln('   3 := Double Strike            : ',onoff[double]);
    writeln('   4 := Emphasized Mode          : ',onoff[emphasized]);
    writeln('   5 := Header Line              : ',onoff[header]);
    writeln('   6 := Double Spaced            : ',onoff[doublespace]);
    writeln('   7 := Extra Top Blank Lines    : ',topspaces);
    writeln('   8 := Extra Bottom Blank Lines : ',bottomspaces);
    writeln('   9 := Automatic L/R Margins    : ',onoff[automatic]);
    writeln('   L := Extra Left Margin        : ',leftmargin);
    writeln('   R := Extra Right Margin       : ',rightmargin);
    writeln;
    normvideo;
    writeln('   F := File Name                : ',filename);
    writeln;
    writeln('   G := GO         Q := QUIT');
    writeln;
    optionline;
    page := 0;
  END;

PROCEDURE get_file;
  BEGIN
    gotoxy(1,21);
    write('   Enter name of file to list: ');
    readln(filename);
    assign(source,filename);
    gotoxy(36,17);
    write(filename,'                                           ');
    optionline;
  END;

PROCEDURE settopmargin;
  BEGIN
    gotoxy(1,21);
    write('   Enter number of extra top spaces: ');
    readln(topspaces);
    gotoxy(36,11);
    write(topspaces,'            ');
    optionline;
  END;

PROCEDURE setbottommargin;
  BEGIN
    gotoxy(1,21);
    write('   Enter number of extra bottom spaces: ');
    readln(bottomspaces);
    gotoxy(36,12);
    write(bottomspaces,'            ');
    optionline;
  END;

FUNCTION spaces(n:integer): maxspaces;

  VAR 
    tmp: STRING[132];
    m: integer;
  BEGIN
    tmp := '';
    FOR m :=1 TO n DO
      tmp := tmp + ' ';
    spaces := tmp;
  END;

PROCEDURE setleftmargin;
  BEGIN
    gotoxy(1,21);
    write('   Enter number of extra left margin spaces: ');
    readln(leftmargin);
    left := spaces(leftmargin);
    gotoxy(36,14);
    write(leftmargin,'             ');
    optionline;
  END;

PROCEDURE setrightmargin;
  BEGIN
    gotoxy(1,21);
    write('   Enter number of extra right margin spaces: ');
    readln(rightmargin);
    right := spaces(rightmargin);
    gotoxy(36,15);
    write(rightmargin,'             ');
    optionline;
  END;

PROCEDURE title; {prints filename, datetime, and page number on each page}
  BEGIN
    write(lst,chr(27),chr(45),chr(1));      {underline on}
    IF linemode = 80
      THEN
        n := 21 - length(filename)
      ELSE
        n := 47 - length(filename);
    temp := 'File: '+ filename;
    FOR m:=1 TO n DO
      temp := temp + chr(32);
    temp := temp + datetimestamp;
    IF linemode = 80
      THEN
        n := 19
      ELSE
        n := 45;
    FOR m:=1 TO n DO
      temp := temp + chr(32);
    temp := temp + 'Page ';
    str(page:3,pagestr);
    temp := temp + pagestr;
    writeln(lst,temp);
    write(lst,chr(27),chr(45),chr(0));      {underline off}
    write('.');
    linecount := 2;
  END;

PROCEDURE page_feed;
  BEGIN
    writeln(lst,chr(140));
    linecount := 1;
    page := page + 1;
  END;

PROCEDURE insertblankline;
  BEGIN
    writeln(lst);
    write('.');
    linecount := linecount + 1;
  END;

PROCEDURE inserttoplines;
  BEGIN
    FOR n := 1 TO topspaces DO
      insertblankline;
  END;

PROCEDURE composeline;       {inserts left and right margin spaces}
  VAR
    len : integer;
  BEGIN
    len := linemode
     - leftmargin - rightmargin;
    m := (length(temp)-1) DIV len + 1;
   {number of sublines per line of input is m}
    lineout := '';
    FOR n := 1 TO m DO
      lineout := lineout+left+ copy(temp,(n-1)*len+1,len) +right;
    IF length(lineout) > 255
      THEN BEGIN
             writeln;
             writeln('Warning....Line in excess of 255 characters in length.');
        END;
   END;

PROCEDURE automaticmargins;
{sets margins so longest line in file is centered}
  BEGIN
    reset(source);
    lm := leftmargin;
    rm := rightmargin;
    maxline := 0;
    REPEAT
      readln(source,temp);
      m := length(temp);
      IF m > maxline
        THEN maxline := m;
    UNTIL EOF(source);
    close(source);
    leftmargin := (linemode - maxline) DIV 2;
    IF leftmargin < 0
      THEN leftmargin := 0;
    rightmargin := leftmargin;
    right := spaces(rightmargin);
    left := spaces(leftmargin);
    END;

PROCEDURE printfile;
  VAR
      n : integer;
  BEGIN
    datetimestamp := datetime;
    IF automatic = 1
      THEN automaticmargins;
    reset(source);
    page := 1;
    linecount := 1;
    linelength := linemode -rightmargin-leftmargin;
    IF linelength <= 0
      THEN BEGIN
             clrscr;
             writeln('ERROR...Illegal margin size');
             halt;
        END;
    writeln;
    REPEAT
      IF linecount =1
        THEN BEGIN
               writeln;
               write('Page ',page,' ');              {status info to screen}
               IF header = 1
                 THEN title;
               IF topspaces >0
                 THEN inserttoplines;
          END;
      readln(source,temp);                   {read in one line}
      composeline;
      FOR n := 1 TO 1 + (length(lineout)-1) DIV linemode do
        BEGIN
          temp := copy(lineout,(n-1)*linemode+1,linemode);
          writeln(lst,temp);                     {write out one line}
          write('.');
          linecount := linecount + 1;
          IF doublespace = 1
            THEN insertblankline;
          IF linecount > 59 - bottomspaces
            THEN page_feed;
          IF linecount =1
            THEN BEGIN {do header if page ends on a long line}
                   writeln;
                   write('Page ',page,' ');         {status info to screen}
                   IF header = 1
                     THEN title;
                   IF topspaces >0
                     THEN inserttoplines;
              END;
        END;
    UNTIL eof(source);
    close(source);
    IF automatic = 1      {restore margin values}
      THEN BEGIN
             leftmargin := lm;
             left := spaces(leftmargin);
             rightmargin := rm;
             right := spaces(rightmargin);
        END;
    menu;
  END;

PROCEDURE quit;       {restores default conditions on printer}
  BEGIN
    write(lst,chr(18));     {80 char/line}
    write(lst,chr(27),chr(72));  {double strike off}
    clrscr;
    halt;
  END;

PROCEDURE action;
  BEGIN
    CASE option OF
      '0': write(lst,chr(140));
      '1': write(lst,chr(138));
      '2': BEGIN
             IF linemode=80
               THEN BEGIN
                      linemode := 132;
                      write(lst,chr(15));
                 END
               ELSE BEGIN
                      linemode := 80;
                      write(lst,chr(18));
                 END;
             gotoxy(36,6);
             write(linemode,'      ');
             optionline;
           END;
      '3': BEGIN
             IF double = 1
               THEN BEGIN
                      double := 2;
                      write(lst,chr(27),chr(72));  {put double strike off}
                 END
               ELSE BEGIN
                      double := 1;
                      write(lst,chr(27),chr(71));  {double strike on}
                 END;
             gotoxy(36,7);
             write(onoff[double],'    ');
             optionline;
           END;
      '4': BEGIN
             IF emphasized = 1
               THEN BEGIN
                      emphasized := 2;
                      write(lst,chr(27),chr(70));  {emphasized off}
                 END
               ELSE BEGIN
                      emphasized := 1;
                      write(lst,chr(27),chr(71));  {emphasized on}
                 END;
             gotoxy(36,8);
             write(onoff[emphasized],'    ');
             optionline;
           END;
      '5': BEGIN
             IF header=1
               THEN header := 2
               ELSE header := 1;
             gotoxy(36,9);
             write(onoff[header],'      ');
             optionline;
           END;
      '6': BEGIN
             IF doublespace=1
               THEN doublespace := 2
               ELSE doublespace := 1;
             gotoxy(36,10);
             write(onoff[doublespace],'     ');
             optionline;
           END;
      '7': settopmargin;
      '8': setbottommargin;
      '9': BEGIN
             IF automatic=1
               THEN automatic := 2
               ELSE automatic := 1;
             gotoxy(36,13);
             write(onoff[automatic],'    ');
             optionline;
           END;
      'l': setleftmargin;
      'L': setleftmargin;
      'r': setrightmargin;
      'R': setrightmargin;
      'F': get_file;
      'f': get_file;
      'G': IF filename <> ''
             THEN printfile;
      'g': IF filename <> ''
             THEN printfile;
      'Q': quit;
      'q': quit;
    END;
END;

BEGIN
  init;
  menu;
  REPEAT
    gotoxy (35,21);
    REPEAT
      read (kbd,option)
    UNTIL option
               IN ['0','1','2','3','4','5','6','g','G','q','Q','7','8','f','F',
                    'r','R','l','L','9'];
    action;
  UNTIL hellfreezesover = true;
END.
