program list(input,output,address_file);
{ this program formats the printing of labels on an epson FX-80
  printer.  The source addresses are in a file that will be prompted
  for if not supplied on the command line.  The addresses are in the
  file with a max. of 4 lines and ONE blank line between them }
const
	numlablesacross = 3;	{ the number of label col. on a sheet }
	firsttab = 2;		{ offset to start first one }
	tabspace = 27;  { 80 div 3 }
	verttab = 6;	{ number of lines from one label to an other }
	characross = 80; { width of printer in chars }
	maxinline = 24;	  { 80 div 3 - 5 }
	maxlinespage = 7;  { the number of label rows on a sheet }
			    { I had to waste the first row to load the sheet }
type
	lineacross = string(characross);  { the line to print }
	inline = string(maxinline);	{ a line of the address }

var	line1,line2,line3,line4: lineacross; { buffers for the printer }
	address_file,printer: text;  	{ the input and output files }
	linecount: integer;		{ used to count the numbers on a page}

procedure blank(var aline: lineacross);
{ clear the buffer line }
var	i: integer;
begin
  for i := 1 to characross do aline[i] := ' ';
end;

procedure asignline(var inl: inline; var aline: lineacross;
					size,index: integer);
{ load the buffer line }
var	i: integer;
begin
  for i := 0 to (size-1) do
    if (index+i) <= characross then aline[index+i] := inl[i+1]
end;

procedure printline(var aline: lineacross);
{ write out a print buffer to the printer }
var	i: integer;
begin
  for i := 1 to	characross do write(printer,aline[i]);
  writeln(printer);
end;

function getline(var line: inline; var size:integer): boolean;
{ read in a source line from the file }
var	i: integer;
begin
  if (not eof(address_file)) and (not eoln(address_file)) then begin
    if address_file^ <> ';' then begin	{address commented out }
      for i := 1 to maxinline do line[i] := ' ';
      getline := true;	i := 1;
      while (not eof(address_file)) and (not eoln(address_file))
       and(i <= maxinline) do begin
        read(address_file,line[i]);
        i := i + 1
      end;
      size := i - 1;
    end else getline := false;
  end else begin
    getline := false;
  end;
  if (not eof(address_file)) then readln(address_file);
end;
procedure skipone;
var	i: integer;
begin
  if not eof(address_file) then
    if address_file^ = ';' then readln(address_file);
  if not eof(address_file) then
    if address_file^ = ';' then readln(address_file);
  if not eof(address_file) then
    if address_file^ = ';' then readln(address_file);
  if not eof(address_file) then
    if address_file^ = ';' then readln(address_file);
end;

procedure dosome;
{ load and print one row of labels across the sheet }
var	i,size: integer;
	ainline: inline;
	outline: lineacross;
begin
  blank(line1);  blank(line2);  blank(line3);  blank(line4);
  for i := 0 to (numlablesacross-1) do begin
    if not eof(address_file) then begin
      while address_file^ = ';' do skipone;
      if eoln(address_file) then readln(address_file); {skip the blank}
      if getline(ainline,size) then begin
        asignline(ainline,line1,size,(firsttab+i*tabspace));
        if getline(ainline,size) then begin
          asignline(ainline,line2,size,(firsttab+i*tabspace));
          if getline(ainline,size) then begin
            asignline(ainline,line3,size,(firsttab+i*tabspace));
            if getline(ainline,size) then begin
              asignline(ainline,line4,size,(firsttab+i*tabspace));
	    end
	  end
	end
      end;
    end
  end;
  printline(line1);
  printline(line2);
  printline(line3);
  printline(line4);
  for i := 5 to verttab do writeln(printer);
end;

begin
  reset(address_file);		{ set the input }
  assign(printer,'PRN:');	{if not ibm-pc then could write to file}
  rewrite(printer);
  linecount := 0;
  while not eof(address_file) do begin
    linecount := linecount + 1;
    dosome;
    if linecount = maxlinespage then begin	{ if end of a sheet }
      writeln('Add new label sheet to printer');
      write('Hit Return when ready');
      readln;	linecount := 0;
    end;
  end;
end.
