perm filename OLDRPT.SAI[USE,CSR]4 blob sn#286559 filedate 1977-06-02 generic text, type C, neo UTF8
C00001 00001
C00004 00002	Declarations
C00011 00003	I/O: ttin,lookupfail,enterfail,inuse,inscan,resp,ynresp,cresp,textinfail,invout
C00021 00004	Procedures onhandin, onhandout, new_report for onhand file input/output
C00025 00005	Procedure rdaddr, buildtree, and addfilin for address file input
C00031 00006	Procedures wraddr, untree, addfilout for address file output
C00034 00007	binary search tree maintenance routines: search,insert,delete
C00038 00008	Procedures to access the address file: unpack,display,find
C00042 00009	Sub-procedures for update actions: zipcheck,gethash
C00045 00010	procedures for update actions: look,ins,mfy,del,update
C00050 00011	The procedure which records orders received
C00058 00012	The `receive' procedure, which handles virtual money
C00064 00013	Procedures for making labels: lab,emitlab,endlab
C00067 00014	The MAIL procedure and its subprocedures abst,invo,inv,status,scanorders
C00081 00015	The `send' procedure, for isolated orders
C00097 00016	The `adjust' procedure, to adjust the inventory
C00102 00017	The president (chief executive)
C00105 00018	The program starts here (sets string constants, including HELPs)
C00115 00019	Set breaks, open channels, call main procedure, end gracefully
C00118 ENDMK
comment Declarations;
begin comment This is the CS report system coded by D. Knuth, October 1976;


require 200 system_pdl;
require 30000 string_space;

require "⊂⊃" delimiters;
define # = ⊂;comment⊃;
define crlf = ⊂('15&'12)⊃;
define crlf2 = ⊂('15&'12&'12)⊃;
define icr = ⊂'15⊃;
define ialt = ⊂'175⊃;
define iff = ⊂'14⊃;
define asize = ⊂1600⊃ # maximum number of addresses in the mailing list;
define logasize = ⊂11⊃ # must be equal to 1 + floor(lg asize);
define bsize = ⊂1000⊃ # maxmimum number of old reports in backorder file;

define cvc(i) = ⊂(if i<10 then "0"+i else ("A"-10)+i)⊃ # encodes
			small integer as a single character;

define btt = ⊂1⊃ # breaktable for ttin;
define bfflf = ⊂2⊃ # break on formfeed (end page) or linefeed (end line);
define blf = ⊂3⊃ # break on linefeed;
define babs = ⊂4⊃ # break on |;
define bast = ⊂5⊃ # break on *;
define bff = ⊂6⊃ # break on formfeed;
define bpar = ⊂7⊃ # break on );
define bsp = ⊂8⊃ # break on space (or tab);
define bch = ⊂9⊃ # break on non-blank;
define bvar = ⊂10⊃ # variable break table, set dynamically;

boolean eof # end of file indicator;
integer brchar # break character;
integer flag # input-output flag;
integer ichan, iichan # channels for character input;
integer ochan, oochan # channels for character output;
integer lchan # channel for mailing label output;

integer c,d,i,j,k,m,p,q,t # miscellaneous temporary integers, pointers, etc.;
string s,st,stt,str # miscellaneous temporary strings;

string typein # input returned by ttin, ends with cr;
integer scale # scale factor returned by inscan;
integer nl # number of lines returned by unpack;
string array lne[0:6] # individual lines of an address;

integer array llink,rlink,key,balance[0:asize] # binary search tree structure;
	comment the address file is organized as a binary tree.
	key[p] is the hashcode, in binary form, of the addressee whose
	serial number is p. balance[p] is the number of pennies he owes.
	Unused positions of the table are doubly-linked into an AVAIL list
	whose head is at position 0. Such entries have key=0;
string array nmline,lines[0:asize] # nmline[p] is line 1 of an address,
	ending with crlf. lines[p] contains the rest of the address information,
	as follows: Let s=lines[p], then
		s[1 to 1] is the mailing category ("C","F","A","N",or "M")
		s[2 to 6] is the zip code or country
		s[7 to 18] is the activity code for last 12 mailings
			(0,1,...,9,A,B,... for 0, 1, ..., 9, 10, 11, ...
			orders, or Z if there were back-orders)
		s[19 to ∞] is lines 2,3, etc. of the address, including
			carriage returns and line feeds but not US zip code;
integer troot # the root of the main binary search tree (contains the 
		addresses on the mailing list);

integer recd # total money (in cents) received in today's transactions;
integer fixd # total accounting adjustments in today's transactions;
integer chgd # total money charged to accounts in today's transactions;
integer calrecd # total amount of receipts from California residents;

boolean mailed # has MAIL already set up output to be spooled?;
boolean sended # has SEND already set up output to be spooled?;
boolean afchanged # should ADDFIL.DSK be written out after processing?;
string date # today's date in form dd MON 19yy;
string mon # month whose orders are being processed (3-letter abbr);

string lf,ff,tab,cshelp,findhelp1,findhelp2,codehelp,updhelp,ordhelp1,ordhelp2,
comment constant strings, see page 16;

string array canned[0:19] # text used to write invoices;

preload_with "JAN","FEB","MAR","APR","MAY","JUN",
	"JUL","AUG","SEP","OCT","NOV","DEC"; string array months[1:12];
preload_with "Y","N"; string array yesnoopts[1:2];
preload_with ""; string array nullopt[1:1];
preload_with "UPD","ORD","REC","MAI","SEN","ADJ"; string array csopts[1:6];
preload_with "C","F","N","M","A"; string array codeopts[1:5];
preload_with "INS","DEL","MOD","LOOK"; string array updopts[1:4];
preload_with "AVER","CHES";string array labelopts[1:2];
preload_with "ADD","CHA"; string array ordopts[1:2];
preload_with "ABS", "REP"; string array mailopts[1:2];
comment I/O: ttin,lookupfail,enterfail,inuse,inscan,resp,ynresp,cresp,textinfail,invout;

procedure ttin;
begin comment sets typein to the line typed in and echoes it also on
	the PRINT file, then gets rid of leading blanks;
integer i;
typein←inchwl&icr; comment alternate for  ttyin(btt,brchar);
setprint(null,"I"); print(typein,lf); setprint(null,"C");
while typein = " " do i←lop(typein);

boolean procedure lookupfail(integer chan; string file);
begin close(chan); lookup(chan,file,flag);
if flag then print(crlf,"Whoa, I can't find ",file,", so I'm stuck.",crlf);

boolean procedure enterfail(integer chan; string file);
begin close(chan); enter(chan,file,flag);
if flag then print(crlf,"Whoa, system error trying to enter ",file,
	", so I'm stuck.",crlf);

boolean procedure inuse(string file);
comment a kludge -- checks to see if file is in use. 'oochan' is used as a temp channel;
begin lookup(oochan,file,flag); enter(oochan,file,flag);
if (flag land '000000777777)=3 then
     print(crlf,"Someone else is using the program, so I'm stuck.",crlf);

integer procedure inscan;
begin comment returns integer contents of typein, ignoring nondigits;
comment sets brchar to last nondigit, scale to no. of digits after ".";
integer t,d; 
t←scale←0; brchar←0;
while typein≠icr do
	begin d←lop(typein);
	if d≥"0" and d≤"9" then
		begin t←10*t+d-"0";
		if brchar="." then scale←scale+1;
	else brchar←d;

integer procedure resp(string q; reference string h; string array opts);
begin comment q is the question asked of the user, h is the HELP string, and opts lists
	the initial characters of allowable responses;
comment the output is 0 if the response was <cr>, otherwise it is the
	index of the option typed;
integer i;
while true do
	begin print(q); ttin;
	if typein=icr then return(0);
	if typein≠"?" then
		begin if equ(typein[1 to 4],"HELP") then
			begin print(crlf,h,crlf,crlf);continue;
		for i←1 step 1 until arrinfo(opts,2) do
			if equ(typein[1 to length(opts[i])],opts[i])
			then return(i);
	print("?The responses I can understand at this point are:",crlf);
	for i←1 step 1 until arrinfo(opts,2) do
	print(crlf,"or <cr> (to get out of this loop),",crlf);
	print("or HELP<cr> (for more information).",crlf);

integer procedure ynresp(string q);
return(resp(q&" (Y or N) ",yesnohelp,yesnoopts));

integer procedure cresp(string q);
begin comment q asks for a response in dollars and cents;
comment this procedure returns the amount in cents, or -1 if response is just <cr>;
comment also brchar is set to the last nondigit typed;
integer c;
while true do
	begin print(q,"$"); ttin;
	if typein=icr then return(-1);
	if scale=2 then return(c);
	print("?I wanted you to type a dollars-and-cents number like 3.14<cr>",
	"----",crlf,"Please try again, or just type <cr> to get out of this.",crlf);

boolean procedure textinfail;
begin comment the canned text for invoices is read into memory;
if lookupfail(ichan,"FORM.DAT") then return(true);
do st←input(ichan,bfflf) until equ(st[1 to 7],"INVOICE") ∨ eof # bypass directory;
if eof then
	begin print("Whoa, file FORM.DAT has been clobbered, so I'm stuck.",crlf);
for i←0 step 1 until 19 do canned[i]←input(ichan,bast);
comment for the desired form of FORM.DAT, see the example in the
	user manual and/or the procedure invout below;

procedure invout(reference string send,sorry,name,addrlabel;
	integer oldbal,charges; boolean Calif);
begin comment outputs an invoice to ochan, and a compressed version to oochan;

   string procedure short (value string reptlist);
   comment returns a compressed version of the report list, with the names deleted;
   begin "short"
	   string shortlist, name;
	   while brchar do
		   if ¬equ(name[5 to 12],"Handling") then begin
		   end else scan(name,blf,brchar);
   end "short";

integer newbal; string str;
out(ochan,canned[0]&date&canned[1]&name&canned[2]) # heading, salutation, ret. addr;
out(oochan,date&crlf2&(if ¬equ(addrlabel[45 to 50],"      ")
			 then addrlabel[1 to 50] else addrlabel)&crlf2);
if sorry≠0 then begin "sorry list" 
	if equ(sorry[1 to 2],crlf) then out(ochan,canned[19])
	    # crlf at the beginning means that one order had to be split -- say so;
	out(oochan,canned[4]&crlf&short(sorry)&crlf); end "sorry list";
if send≠0 then begin "send list"
	if Calif and charges>0 then out(ochan,canned[15]&crlf);
newbal←oldbal+charges; str←cvf(abs(newbal)/100);
if charges>0 then comment print account status, dep on old and current balance;
	begin if oldbal=0 then out(ochan,canned[8]&str&canned[9])
	else if newbal>0 then out(ochan,canned[10]&str&canned[11])
	else if newbal<0 then out(ochan,canned[12]&str&canned[13])
	else out(ochan,canned[14])
else if oldbal>0 then out(ochan,canned[6]&str&canned[7]);
if newbal>0 then out(ochan,crlf&canned[16]&str&canned[17]&addrlabel
	&canned[18]) comment print invoices for this set of reports;
else out(ochan,crlf&lf&lf&addrlabel&ff);
comment Procedures onhandin, onhandout, new_report for onhand file input/output;

boolean procedure onhandin (string array oldrep,title; integer array onhandh,onhandm,cost;
			reference integer imax);
comment reads from the ONHAND.DSK file, and sets up the arrays and count;
begin "read onhand"
string st;
if lookupfail(ichan,"ONHAND.DSK") then return (false);
for i←0 step 1 until bsize-1 do
	begin do st←input(ichan,bfflf) until lop(st)="*" or eof;
	if eof then done;
	if equ(st[1 to 4],"SAME") then title[i]←"" else
		begin title[i]←scan(st,babs,brchar);
		typein←scan(st,babs,brchar)&icr; onhandh[i]←inscan;
		typein←scan(st,babs,brchar)&icr; onhandm[i]←inscan;
		if st=0 then
			begin print("Bad entry in ONHAND.DSK file for ",
			oldrep[i],crlf); st←"$.00"&icr;
		typein←st; cost[i]←inscan;
print("I have found ",imax+1," records about old reports in file ONHAND.DSK.",crlf);
return (true);
end "read onhand";

boolean procedure onhandout(string array oldrep,title; integer array onhandh,onhandm,cost;
			value integer imax);
begin "write onhand"
if enterfail(ochan,"ONHAND.DSK") then return (false);
for i←0 step 1 until imax do
	begin out(ochan,"*"&oldrep[i]&"|");
	if title[i]=0 then out(ochan,"SAME"&crlf)
	else out(ochan,title[i]&"|"&cvs(onhandh[i])&"|"&
	if i mod 55 = 54 then out(ochan,ff);
return (true);
end "write onhand";

boolean procedure new_report(string name; string array oldrep, title;
			 integer array onhandh, onhandm, cost; reference integer imax);
comment Adds a new report title to the ONHAND.DSK file;
begin "new report"
	k←imax+1; oldrep[k]←st;
	cost[k]←cresp("What is the price of hardcopy? ");
	if cost[k]<0 then return (false);
	print("How many hard copies are on hand? "); ttin;
	if typein=icr then return(false) else onhandh[k]←inscan;
	print("How many microfiche copies are on hand? ");
	ttin; if typein=icr then return(false) else onhandm[k]←inscan;
	return (true);
end "new report";
comment Procedure rdaddr, buildtree, and addfilin for address file input;

integer prevk # previous key read by rdaddr;

integer procedure rdaddr;
begin comment reads and stores the next address from ADDFIL.DSK,
	returning the serial number;
comment returns 0 if end of file sensed;
comment during this procedure, st represents the file line most recently
	read but not yet digested;
comment The ADDFIL.DSK contains up to twenty entries per page.  Each entry begins
	with a line in the format
	where C=category, ZZZZZ=zipcode, AAAAAAAAAAAA=activity codes,
	HHHHH=hashcode, SSSSS=serial number, BAL=dollar balance due
	(preceded by - if negative). Then comes 2 to 5 lines of the
	address, each of which should be at most 34 characters wide
	in most cases;
comment most of this code is devoted to simple error checking;
string ent,name,addr; integer loc,k; label start;
key[0]←1; nmline[0]←"Listhead"&crlf;
comment if the file was in E editor format, pass over the index page;
while st ≠ "*" do
	if eof then return(0) else st←input(iichan,bfflf);
st←addr[1 to 1];
comment now ent,name,addr are the first three address lines;
while st ≠ "*" and st ≠ "#" do
	begin if length(st)>2 then addr←addr&st;
	if eof then done;
loc←cvd(ent[27 to 31]);
if loc>asize then
	begin print(crlf,"ADDFIL.DSK error, serial number too big...
	    the following name has been deleted from the file:",crlf,name,crlf,
	    "since it had a serial number of ",loc,".",crlf,
	    "...The rest of the deleted file entry was:",crlf,ent,addr);
	go to start;
if key[loc]≠0 then
	begin print(crlf,"ADDFIL.DSK error, two people with same serial number...
	    the following name has been deleted from the file:",crlf,name,
	    "since it had the same serial number as:",crlf,nmline[loc],
	    "...The rest of the deleted file entry was:",crlf,ent,addr); 
	go to start;
comment convert the hash key to internal format (integer);
k←cvasc(ent[22 to 26]);
if k ≤ prevk then
	begin print(crlf,"ADDFIL.DSK error, hash codes not increasing...
	    the following name has been deleted from the file:",crlf,name,
	    "since its hash code was not greater than the preceding one.",crlf,
	    "...The rest of the deleted file entry was:",crlf,ent,addr);
	go to start;
key[loc]←k; prevk←k;
rlink[llink[loc]]←rlink[loc];llink[rlink[loc]]←llink[loc] # remove from AVAIL;
nmline[loc]←name;lines[loc]←ent[2 to 7]&ent[9 to 20]&addr;
typein←ent[33 to ∞]; balance[loc]←inscan;
if ent[33 to 33]="-" then balance[loc]←-balance[loc];

recursive integer procedure buildtree(integer m);
begin comment builds a somewhat balanced binary search tree of up to
		2↑m-1 nodes, returning a pointer to the root;
integer root,subtree;
if m=0 then return(0) else
	begin subtree←buildtree(m-1);
	if root=0 then return(subtree) else
		begin llink[root]←subtree;

procedure addfilin;
begin comment inputs the address file, assuming that it is on iichan;
for i←1 step 1 until asize-1 do
	begin key[i]←0; llink[i]←i-1; rlink[i]←i+1;
st←""; prevk←'400000000000;
print(crlf,"The address file contains a total of ",i," entries.",crlf);
comment Procedures wraddr, untree, addfilout for address file output;

integer totbal # total balance from all accounts in the file;
integer kf,kn,km,ka # total number of entries of various categories;

procedure wraddr(integer p);
begin comment appends the address for serial number p to current output page,
	and outputs if the page is full);
comment also gathers statistics about the file;
string s,t;
out(oochan,"*"); out(oochan,t[1 to 6]); out(oochan,"|"); out(oochan,t[7 to 18]);
out(oochan,"#"); out(oochan,cvstr(key[p]));
setformat(5,2); out(oochan,cvs(p)); setformat(0,2); comment for serial number;
out(oochan,"$"); out(oochan,cvf(balance[p]/100)); out(oochan,crlf);
out(oochan,nmline[p]); out(oochan,t[19 to ∞]); comment output address;
if t≠"C" then
	begin if t="F" then kf←kf+1
	else if t="A" then ka←ka+1
	else if t="N" then kn←kn+1
	else if t="M" then km←km+1;
if k mod 20 = 0 then out(oochan,ff);

recursive procedure untree(integer p);
begin comment outputs the binary search tree rooted at p in order by key;
if p≠0 then
	begin untree(llink[p]);

procedure addfilout;
begin comment outputs the entire address file to oochan;
print(crlf,"The address file now contains a total of ",k," entries,
including the following special categories:
F = ",kf," A = ",ka," N = ",kn," M = ",km,crlf,
"and the total balance outstanding is $",cvf(totbal/100),".",crlf);
if asize-k<50 then print("I am currently programmed to handle up to ",
	asize," entries maximum.",crlf);
comment binary search tree maintenance routines: search,insert,delete;

integer lp # last position unsuccessfully probed in search routine;

integer procedure search(integer k);
begin comment returns serial number of addressee having key k,
	or 0 if not in the table;
integer p;
p←troot; lp←0; key[0]←k;
while k≠key[p] do
	begin lp←p;
	if k<key[p] then p←llink[p] else p←rlink[p];

integer procedure insert(reference string name,ent; string hash; integer bal);
begin comment inserts new address file entry into an available place
	and returns the value of this place (i.e. the serial number);
integer p,k;
p←rlink[0] # get available location;
if p=0 then
	begin print("The mailing list is now completely full, so I can't ",
	    "insert the entry for the",crlf," following name: ",name,
	    "To increase the table size one may simply recompile CSREPT",crlf,
	    "with asize and logasize defined larger. 
	    (But do we really want such a big mailing list?)",crlf);
rlink[0]←rlink[p]; llink[rlink[p]]←0 # remove from AVAIL list;
nmline[p]←name; lines[p]←ent; key[p]←k; balance[p]←bal;
if k<key[lp] then llink[lp]←p else rlink[lp]←p;

procedure delete(integer k);
begin comment deletes entry with key k from its place in the address file,
	using the standard algorithm;
integer p,q,r;
if p=0 then
	begin print("Hmm... Something went wrong, I just attempted to ",
	"delete a nonexistent key.",crlf); return;
comment new delete p from its subtree, yielding a subtree with root q;
if llink[p]=0 then q←rlink[p]
else if rlink[p]=0 then q←llink[p]
else	begin q←rlink[p];
	if llink[q]=0 then llink[q]←llink[p]
	else	begin do q←llink[r←q] until llink[q]=0;
		llink[r]←rlink[q]; llink[q]←llink[p]; rlink[q]←rlink[p];
comment now adjust the upper part of the tree and the AVAIL list;
if lp=0 then troot←q
else if k<key[lp] then llink[lp]←q else rlink[lp]←q;
q←rlink[0]; rlink[p]←q; llink[q]←p; llink[p]←0; rlink[0]←p;
comment the next insert will go into location p again (this property
	is used in the update "mfy" routine);
key[p]←0; nmline[p]←lines[p]←"";
fixd←fixd+balance[p]; balance[p]←0;
comment Procedures to access the address file: unpack,display,find;

procedure unpack(integer p);
begin comment takes entry from address file position p and stores it
	in lne[0], lne[1], ..., lne[nl];
string ent,zip;
lne[0]←lines[p][1 to 18];
ent←lines[p][19 to ∞];
for j←2 step 1 until 6 do
	begin lne[j]←scan(ent,blf,brchar);
	if ent=0 then
		begin nl←j; done;
zip←lne[0][2 to 6];
if zip≤"9" then lne[nl]←lne[nl][1 to ∞-2]&"  "&zip&crlf;

procedure display(integer p);
begin comment types an address entry;
string ent,s;
if equ(lne[0][2 to 6],"IDMAI") then lne[nl]←lne[nl][0 to ∞-2]&" IDMAIL"&crlf;
for j←1 step 1 until nl do
	print("LINE ",j,": ",lne[j]);
print("hashcode=#",cvstr(key[p]),",   category=",lne[0][1 to 1],
	",   serial=",p,
	if equ(lne[0][2 to 6],"IDMAI") then ",  IDMAIL," else ",",
	crlf,"ordering history=",lne[0][7 to 18],
	",   current balance=$",cvf(balance[p]/100),crlf);

integer procedure find(string s);
begin comment interactive specification of a table entry,
	where s is part of the prompting message;
integer k,p,c,d;
while true do
	begin if resp("Type hashcode "&s&": #",findhelp1,nullopt) = 0 then return(0);
	if (p←search(cvasc(typein[1 to 5])))≠0 then
	case ynresp("Is the name "&nmline[p][1 to ∞-2]&"?") of
		begin continue; return(p); ;
	else	begin if resp("Sorry, that hashcode isn't in the file."
		&" What is the name? ",findhelp2,nullopt)=0 then continue;
		typein←typein[1 to ∞-1]; d←length(typein);
		c←typein; setbreak(bvar,c,null,"IR");
		print("Here are all the entries matching that name:",crlf);
		for i ← 1 step 1 until asize do if key[i]≠0 then
			begin stt←nmline[i];
			while true do
				begin scan(stt,bvar,brchar);
				if brchar=0 then done;
				if equ(stt[1 to d],typein) then
					begin print("#",cvstr(key[i]),": ",nmline[i]);
					end else k←lop(stt);

comment Sub-procedures for update actions: zipcheck,gethash;

string zip,hash # returned by zipcheck and gethash;

boolean procedure zipcheck (boolean newzip);
begin comment before writing an address into the file, we need to check its
	zip code for validity: the first three characters of the hash and the
	zip should agree;
comment this procedure set zip to the desired zip code and sets typein to
	the classification category, or returns false if the user wishes
	to flush the address;
integer i,k;
stt←lne[nl][1 to ∞-2]&"    "; k←length(stt)-5;
while k>0 and stt[k to k]=" " do k←k-1;
comment find the five characters after the rightmost blank;
while k>0 and stt[k to k]≠" " do k←k-1;
zip←stt[k+1 to k+5];
while k>0 and stt[k to k]=" " do k←k-1;
if newzip then
	print("I deduce that the ZIP code or country is ",zip,";",crlf,
 		" if not, please reject this and try again.",crlf);
case resp("Type the classification (C,F,N,M, or A), or type <cr> to reject "
	&"this entry: ",codehelp,codeopts) of begin
	if zip≤"9" or equ(zip,"IDMAI") then lne[nl]←stt[1 to k]&crlf # C;
	if zip≤"9" or equ(zip,"IDMAI") then lne[nl]←stt[1 to k]&crlf # F;
	zip←"ONRXX" # N;
	zip←"DARPA" # M;
	zip←"AUTOM" # A;

procedure gethash;
begin comment sets hash to a hashcode not already in the table,
	beginning with the first three characters of zip;
integer j,k,c,d;
k←length(lne[1]); j←k div 3; k←2*j;
do	begin c←lne[1][j to j]; j←j-1;
	end until (c≥"A" and c≤"Z") or j=0;
if c<"A" or c>"Z" then c←"X";
do	begin d←lne[1][k to k]; k←k-1;
	end until (d≥"A" and d≤"Z") or k=0;
if k=0 then d←"J";
while true do
	begin hash←zip[1 to 3]&c&d; k←cvasc(hash);
	if search(k)=0 then done;
	if d≠"Z" then d←d+1
	else	begin d←"A";
		if c≠"Z" then c←c+1 else c←"A";
	end # will loop forever if 676 people with same zip[1 to 3];
comment procedures for update actions: look,ins,mfy,del,update;

procedure look;
if(p←find("of entry to be displayed"))=0 then return
else display(p);

procedure shorten(integer d);
print("That line was ",d," character", if d=1 then "" else "s",
      " too long for our mailing labels.
       Please shorten it.",crlf);

procedure ins;
begin comment interactive insertion of new address;
integer i,c,p; string ent;
print("Type the new address, two to five lines long:",crlf);
nl←0; for i←1 step 1 until 5 do
	begin label prompt;
prompt:	print("Line ",i,": "); ttin;
	if typein=icr then done;
	if length(typein)>35 then
		begin shorten(length(typein)-35);
		go to prompt;
	lne[i]←typein&lf; nl←i;
if nl=0 then return;
if nl=1 then
	begin print("You need another line; try again.",crlf); return;
if not zipcheck(true) then return;
c←lop(typein) # C, F, N, M, or A;
for i←2 step 1 until nl do ent←ent&lne[i];
if(p←insert(lne[1],ent,hash,0))=0 then return;
print("OK, I've inserted it; hash code is #",hash,", serial number is ",p,crlf);

procedure mfy;
begin comment interactive modification of an address;
boolean zch # if zipcode could not have changed, avoids a typeout;
string ent;
integer b,j,jmax,p,k;
if (p←find("of entry to be modified"))=0 then return;
while true do
	begin jmax←nl+1; if jmax>5 then jmax←5;
	print("Type number of a line to be changed (1 to ",jmax,"),",crlf,		
		"or <cr> if modifications are complete: "); ttin;
	if typein = icr then done;
	if j≤0 or j>jmax then print("Invalid line number.",crlf)
	else	begin label prompt;
prompt:		print("New line ",j,": "); ttin;
		if typein=icr then
			begin nl←j-1; zch←true;
		else if length(typein)>35 then
			begin shorten(length(typein)-35);
			go to prompt;
		if j≥nl then zch←true;
		if j>nl then nl←nl+1;
if not zipcheck(zch) then return;
ent←lop(typein)&zip&lne[0][7 to 18];
for j←2 step 1 until nl do ent←ent&lne[j];
if not equ(zip[1 to 3], lne[0][2 to 4]) then
	begin delete(key[p]);
	print("Hashcode changed from #",cvstr(k)," to #",hash,".",crlf);
	insert(lne[1],ent,hash,b) # it goes into location p again but relinked;
	end else
	begin comment hashcode did not change;
	nmline[p]←lne[1]; lines[p]←ent;
print("OK, the modification has been made.",crlf);

procedure del;
begin comment interactive deletion of a table entry;
integer j,p;
if(p←find("of entry to be deleted"))=0 then return;
j←ynresp("Do you really want to delete this?");
if j≠1 then return else
	begin delete(key[p]);
	print("OK, I did it.",crlf);

procedure update # main control routine for update loop;
begin comment when debugging, call bail here;
while true do
	case resp("UPDATE: INS, DEL, MOD, or LOOK? ",updhelp,updopts) of
		begin done;ins;del;mfy;look;
comment The procedure which records orders received;

procedure orders;
begin "orders"
	comment the files ORDERS.XXX, where XXX is a month,
	    consist of a number of lines of the form 
	    where HHHHH is the hashcode (ignored in the processing),
	    SSSSS is the serial number right-justified to seven digits,
	    DDDD is a variable-length list of report-order digits 
	    0,...,9,A,B,..., and DATE is the date of recording this order in the file;
integer flag,j,p;

   procedure add # used to put new orders into the file;
   begin "add"
       close(ichan); lookup(ichan,"orders."&mon,flag);
       if enterfail(ochan,"orders."&mon) then return;
       if flag then
	   print("No orders on file for ",mon," I will create a new file.",crlf)
       else begin 
          print("I will append to existing orders recorded on file ORDERS.",mon,".",crlf);
	   if equ(stt[1 to 10],"COMMENT ⊗ ") then
		   begin while brchar≠iff do stt←input(ichan,bff) # skip directory page;
	   do      begin out(ochan,stt); stt←input(ichan,bff)
		   end until stt=0;
       while true do begin "next customer"
	       if(p←find("of person ordering"))=0 then done;
	       if resp("Reports ordered: ",ordhelp2,nullopt)=0 then continue;
	       out(ochan,"#"&cvstr(key[p])&","&cvs(p)&":"&typein[1 to ∞-1]
	       end "next customer";
       print(j," new orders written onto ORDERS.",mon,".",crlf);
   end "add";

   procedure chg # used to modify the order for one of the customers already in the file;
   begin "change"
       label restart;
       integer imax # number of entries in the file (modulo deletions);
       integer array sernum[1:700]; string array tkey,reports,dte[1:700];
       close(ichan); lookup(ichan,"orders."&mon,flag);
       if flag then begin
	   print("No orders on file for ",mon," so I can't change anything.",crlf);
	   return; end;
       if enterfail(ochan,"orders."&mon) then return;
       comment allow the user to modify the order file;
       comment first, read in the file, and store it in the arrays;
       if equ(stt[1 to 10],"COMMENT ⊗ ") then begin
	       while brchar≠iff do stt←input(ichan,bff) # skip directory page;
       for i←1 step 1 until 700 do begin "read orders file"
	   if brchar=0 then done;
	   tkey[i]←stt[2 to 6];
	   stt←stt[8 to ∞];
	   reports[i]←scan(stt,bsp,brchar)[2 to ∞];
	   dte[i]←stt[1 to ∞-2];
       end "read orders file";
       if ¬eof then begin
	   print("Whoops. The order file is too big for me to modify.",crlf,
		   "You'll have to use E.",crlf);  return;  end;
       print(imax," entries found in ORDERS file.",crlf);
       comment have read in the file, now let the user modify it;
       j←0; k←0; comment number of reports changed and deleted, respectively;
       while true do begin "next customer"
		string code; label found;
		if resp("Enter hash code of customer to be modified: #",
			   ordhelp2,nullopt)=0        then done;
   	 	code←typein[1 to 5];
		for i←1 step 1 until imax do 
		    if equ(tkey[i],code) then goto found;
		print("That code is not in the file. Try again.",crlf);
	found:	if ynresp("Is the name "&nmline[sernum[i]][1 TO ∞-2]&"?")≠1 then continue;
		print("Current list of reports ordered: ",reports[i],".",crlf);
		if resp("Enter corrected list: ",ordhelp2,nullopt)=0 then begin
			reports[i]←null; k←k+1; end
		else begin reports[i]←typein[1 to ∞-1]; dte[i]←date; j←j+1; end;
       end "next customer";
       print(j," records changed in ORDERS file.",crlf);
       if k≠0 then print(k," records deleted.",crlf);
       comment now write the file back out again;
       for i←1 step 1 until imax do
	   if reports[i]≠null then
   end "change";

j←resp("For which month? ",ordhelp1,months);
if j=0 then return else mon←months[j];
while true do
	case resp("ORDER: ADD or CHANGE? ",ordhelp2,ordopts) of begin
end "orders";
comment The `receive' procedure, which handles virtual money;

procedure receive;
begin comment interactive processing of receipts;
while true do
	begin label prompt; integer amt;
	p←find("of account to credit (or 99999)");
	if p=0 then done;
prompt:	print("Amount rec'd (or amount + or -, if accounting adjustment)? $");ttin;
	if scale≠2 then
		begin print("Type amount followed by <cr>, e.g., 5.20<cr>,",crlf,
		    "if $5.20 has been received in payment for this account.",crlf,
		    "Type amount followed by -<cr> if the account balance is to",crlf,
		    "decrease by this amount but no payment has been received.",crlf,
		    "Type amount followed by +<cr> if the account balance is to",crlf,
		    "increase by this amount. Just type <cr> to leave the account",crlf,
		    "unchanged. People not on the mailing list have hash code #99999.",crlf);
		go to prompt;
	if brchar="-" then
		begin fixd←fixd+amt; balance[p]←balance[p]-amt;
	else if brchar="+" then
		begin fixd←fixd-amt; balance[p]←balance[p]+amt;
	else	begin label notax; if brchar≠"." then
			begin print("Incorrect form, try again.",crlf);
			go to prompt;
		if key[p]=cvasc("99999") then
			begin if ynresp("California resident?")≠1 then go to notax;
		else if key[p]<cvasc("90000") or key[p]≥cvasc("96700") then
			go to notax;
		comment We must pay tax on California residents, the tax was
			included in the purchase price;
notax:		balance[p]←balance[p]-amt; recd←recd+amt;
comment Procedures for making labels: lab,emitlab,endlab;

integer ltype # 0 for AVERY labels, 1 for CHESHIRE;
integer lct # mod 3 counter for CHESHIRE label output;
string array blne[1:5] # CHESHIRE label buffer;

string procedure lab(integer p,w; boolean lvunpacked,free);
begin comment makes a 5-line label, w characters wide, for addressee at
	serial number p, either leaving the result in lne[1] thru lne[5]
	(if lvunpacked is true) or delivering it as a string.
	If free=true, the word "(FREE)" is inserted on the second line
	when appropriate;
for i←1 step 1 until nl do
	begin stt←lne[i][1 to ∞-2]&blanks;
	lne[i]←stt[1 to w-6]&
	(if i=1 then "#"&cvstr(key[p])
	else if free and i=2 and lne[0]≠"C" then "(FREE)"
	else stt[w-5 to w])&crlf;
for i←nl+1 step 1 until 5 do lne[i]←
	if lvunpacked then blanks[1 to w]&crlf else crlf;
if lvunpacked then return("")
else return(lne[1]&lne[2]&lne[3]&lne[4]&lne[5]&crlf);
comment note that a sixth blank line was returned;

procedure emitlab(integer p,free) # outputs one label;
case ltype of
	begin   out(lchan,lab(p,34,false,free)) # AVERY label;
		begin # CHESHIRE label;
		    for i←1 step 1 until 5 do
			case lct of
				blne[i]←lne[i][1 to 34]&" " # lct=0;
				blne[i]←blne[i]&lne[i][1 to 34]&" " # lct=1;
				out(lchan,blne[i]&lne[i]) # lct=2;
			lct←(lct+1)mod 3; if lct=0 then out(lchan,crlf);

procedure endlab;
begin comment outputs the last labels, if any;
if ltype = 1 and lct≠0 then out(lchan,blne[1]&crlf&blne[2]&crlf&blne[3]&crlf
print("The mailing labels have been written onto file LABELS.TMP. 
To print them, see instructions in the user manual; be sure to delete
this file afterwards.",crlf);
comment The MAIL procedure and its subprocedures abst,invo,inv,status,scanorders;

procedure mail;
begin comment takes care of abstract and invoice mailings;
comment The following arrays are allocated only within MAIL;
integer array send,sorry[0:asize] # record of orders that can and can't be filled;
integer array msk[0:42] # the bit corresponding to a report, if that report
	appears on the current month's list (send and sorry use these bit codes);
string array starname,reptname[0:42] # identifies a report;
integer array stock,filled,unf[0:42] # on hand, requests filled, requests unfilled;
integer array reptcost[0:42] # price of report in pennies;
integer imax # maximum report number;

   recursive procedure abst(integer p) # emits mailing labels in symmetric
	   order (i.e., in order by hashcode) for tree rooted at p;
   if p≠0 then
	   begin abst(llink[p]);
	   if j="C" or j="F" then emitlab(p,true);

   integer procedure status;
   begin comment prints a status report for the user, and returns 0,1,2 according as
       the verdict is to start over, go ahead with shifting, go ahead without shifting;
   print("I have read through all the orders, and here is how things stand:",crlf,
     "(hardcopy)      To be   Unfillable      (microfiche)    To be   Unfillable",crlf,
     "Cost  On hand   sent    requests              On hand   sent    requests",crlf);
   for i←1 step 2 until imax do
	   begin print(starname[i]);
	   j←cvc(i)-"1"; print("$",cvf(reptcost[j]/100),tab,stock[j],tab,
	   j←cvc(i+1)-"1"; print(tab,tab,stock[j],tab,filled[j],tab,unf[j],crlf);
   print("Please check this carefully. If there has been some error,",crlf,
     "type <cr> to exit; but if it's all right to go ahead and print the invoices,",crlf,
     "type Y<cr> and I will prepare them: "); ttin;
   if typein = "Y" then
   begin j←resp("OK, I will begin to work on the invoices."&crlf&"Do you want the"&
		   "activity codes to be shifted? (Y or N) ",acthelp,yesnoopts);
   end else
	   begin print("OK, I will not print those invoices, please try again.",crlf);

   procedure scanorders;
   begin "scanorders" comment read through all orders, and set up the `send' & `sorry' arrays;
   if equ(stt[1 to 10],"COMMENT ⊗ ") then
	   begin while brchar≠iff do stt←input(ichan,bff) # skip directory page;
   for i←1 step 1 until asize do send[i]←0;
   while true do
	   begin label nextline; integer sendp,sorryp;
	   if eof then done;
	   if stt="#" then
		   begin p←cvd(stt[8 to 14]) # get the serial number for this order;
		   if p>asize or key[p]=0 then
			   begin print("I ignored the order ",stt,
			   "since that serial number is no longer in the file.",crlf);
			   go to nextline;
		   st←stt[16 to ∞];
		   sendp←send[p]; sorryp←sorry[p];
		   while st≥"1" do
			   begin j←lop(st)-"1";
			   if j>42 or msk[j]=0 then
				   begin print("I ignored the invalid report code "&
				   (j+"1")," which appears in the following order:",
			   else if((sendp lor sorryp)land msk[j])=0 then
				   begin if stock[j]>filled[j] then
					   begin filled[j]←filled[j]+1;
					   sendp←sendp lor msk[j];
				   else    begin unf[j]←unf[j]+1;
					   sorryp←sorryp lor msk[j];
   end "scanorders";

   procedure inv(integer p; boolean shift);
   begin comment processes (makes up invoices for) the addressee with serial number p;
   integer reps; reps←0;
   if lines[p]≠"C" and lines[p]≠"F" and shift then emitlab(p,false)
   else if send[p]≠0∨sorry[p]≠0∨(shift∧balance[p]>0∧equ(lines[p][17 to 18],"00")) then
	   begin comment set up the list of reports that we're going to send;
	   string addrlab,sends,sorrys; integer t,j,tbal;
	   sends←sorrys←""; tbal←reps←0;
	   if send[p]≠0 then
		   begin t←send[p];
		   for j←0 step 1 until 42 do if msk[j] land t ≠ 0 then
			   begin reps←reps+1;
			   if lines[p]≠"C" or reptcost[j]=0 then
			   else    begin tbal←tbal+reptcost[j];
				   sends←sends&"    $"&cvf(reptcost[j]/100)&crlf;
			   if(t←t xor msk[j])=0 then done;
	   if sorry[p]≠0 then comment set up the list of reports that we're out of;
		   begin t←sorry[p];
		   for j←0 step 1 until 42 do if msk[j] land t ≠ 0 then
			   begin reps←reps+1;
			   if(t←t xor msk[j])=0 then done;
		   key[p]≥cvasc("90000") and key[p]<cvasc("96700"));
   if shift then lines[p]←st[1 to 6]&st[8 to 18]&cvc(reps)&st[19 to ∞]
   else if reps > 0 then
	   begin t←st[18 to 18]-"0";
	   if t>9 then t←t-7;
	   lines[p]←st[1 to 17]&cvc(t+reps)&st[19 to ∞];

   recursive procedure invo(integer p;boolean shift);
   begin comment calls inv for all addresses in p's subtree, symmetric order (inorder);
   if p≠0 then
	   begin invo(llink[p],shift);inv(p,shift);invo(rlink[p],shift);

comment The MAIL procedure really starts here;
if mailed then
	begin print("Sorry, but you can't use MAIL again at this session;",crlf,
	            "   you have to spool the output from this session first.",crlf);
if enterfail(lchan,"LABELS.TMP") then return;
if (ltype←resp("MAIL subsystem: AVERY or CHESHIRE labels? ",mailhelp,labelopts)-1)
	< 0 then return;
case resp("MAIL subsystem: Sending abstracts or reports? ",mailhelp,mailopts) of begin
	begin # abstracts;
	    print("OK, I'm making the labels for you...",crlf);
	begin # reports;
	    if textinfail then return;
	    j←resp("For which month? ",mailhelp,months);
	    if j=0 then return else mon←months[j];
	    if lookupfail(ichan,"ORDERS."&mon) then return;
	    if enterfail(ochan,"INVOIC.TMP") then return;
	    if enterfail(oochan,"INVOI$.TMP") then return;
	    comment now get report data;
	    print("I need to know some things from that abstract list.",crlf);
	    for j←0 step 1 until 42 do msk[j]←0;
	    for i←1 step 2 until 35 do
		    begin label restart;
  restart:          if resp("Please enter STAN- or AIM- or HPP- number of reports "
			      &cvc(i)&" and "&cvc(i+1)&","&crlf&" followed by *AUTHOR,TITLE"&
			      " (or <cr> if done, QUIT<cr> to abort):"&crlf,
			    rhelp,nullopt) = 0 then done;
		    if equ(typein[1 to 4],"QUIT") then return;
		    j←cvc(i)-"1"; k←cvc(i+1)-"1";
		    reptname[j]←st&"(hardcopy) ";
		    reptname[k]←st&"(microfiche) ";
		    if(reptcost[j]←cresp("What is the cost of hardcopy? "&
			 "(If unavailable, say anything.) "))<0 then go to restart;
		    for p←j,k do
			    begin print("How many copies of ",reptname[p],
			    crlf,"are available for distribution? "); ttin;
			    if typein=icr then go to restart;
		    reptname[j]←reptname[j]&str[1 to ∞-2];
		    reptname[k]←reptname[k]&str[1 to ∞-2];
		    if ynresp("Thanks. Can I assume that the information you just gave"&
			      "for this report"&crlf&"is correct and complete?")≠1 then
			    begin print("Then let's try again.",crlf);
			    go to restart;
		    else    begin msk[j]←1 lsh(i-1); msk[k]←1 lsh i;
	    print("OK, now I'm looking at the orders...",crlf);
	    case status of
		    begin return;invo(troot,true);invo(troot,false);
	    close(ochan); afchanged←true;
	    print("The invoices, bills of lading, and dunning letters have ",
		"been written",crlf,"onto file INVOIC.TMP. To print them, do",
		crlf,tab,tab,"XS INVOIC.TMP/NOHEAD",crlf,
		"and after successful completion of that do",crlf,tab,tab,
		"DEL INVOIC.TMP.",crlf,"The files ORDERS.",mon," and INVOI$.TMP ",
		"contain shorter records of",crlf,"the orders requested and ",
		"the invoices actually sent (for your permanent",crlf,
		"records). These should also be XSpooled and then DELeted.",crlf);
comment The `send' procedure, for isolated orders;

procedure send;
begin "send" 
string array oldrep,title[0:bsize] # contain the abbreviation and title of each rept;
integer array onhandh,onhandm,cost[0:bsize] # quantities on hand, and cost of pc;
comment if the file ONHAND.DSK contains a line like this:
    *CS249|STAN-CS-74-249*KNUTH,HOW NOT TO RUN A COMMITTEE|22|0|$3.50
    then the internal representation has oldrep[i]="CS249", title[i]=
    "STAN...TEE", onhandh[i]=22 (hardcopy on hand), onhandm[i]=0,
    cost[i]=350. If the line on the file is "*AIM123|SAME" then
    oldrep[i]="AIM123", title[i]="", and it means the same as report i-1;
integer imax,tbal,amt,copies; boolean fiche;
integer array bufh,bufm,counth,countm [0:30] # places to update onhandh, onhandm arrays;
integer ph,pm # stack pointers for buf and count arrays;
label restart;

string procedure multiple(integer copies);
        return(crlf&"   ("&cvs(copies)&" copies)");

simple procedure order(integer array buf, count; reference integer ptr; integer quant);
     	comment to add an order to the temporary list (may be cancelled by the user);
        begin buf[ptr]←k; count[ptr]←quant; ptr←ptr+1; end;

procedure bulkrate (integer copies);
comment This makes adjustments necessary for volume deals -- reducing
	the price, or adding a handling charge -- and calculates the totals;
begin "bulkrate"
	integer handling, cost1;
	case ynresp("We are sending "&cvs(copies)&" copies of "&oldrep[k]&"."&crlf&
	                "Do you wish to change the price per copy?") of begin
		goto restart;
		cost1←cresp("List price is $"&cvf(cost[k]/100)&"."&crlf&
				"What is the actual price to be charged? ");
	if cost1<0 then return;
	st←st&" @ $"&cvf(cost1/100)&":   $"&cvf(amt/100);
	case ynresp("Do you wish to add a handling charge?") of begin
		goto restart;
		    handling←cresp("What is the handling charge? ");
		    if handling<0 then goto restart
		    else begin
			    st←st&crlf&"   Handling               $"&cvf(handling/100);
		end; ;
end "bulkrate";

if sended then
	begin print("Sorry, but you can't use SEND again at this session;",crlf,
		     "you have to spool the output from this session first.",crlf);
if textinfail then return # read canned text for invoice forms;
if enterfail(ochan,"BILLS.TMP") then return;
if enterfail(oochan,"BILLS$.TMP")  then return;
if ¬onhandin(oldrep,title,onhandh,onhandm,cost,imax) then return; # read the file;

while true do
	begin "next user" 
	string name,addr,sends,sorrys,thenext;
	integer reps; boolean free;
        if(p←find("(or 99999) for person requesting old reports"))=0 then done;
	if key[p]=cvasc("99999") then
		begin print("Type the name and address of customer, ",
		"followed by a blank line:",crlf); ttin;
		if typein=icr then continue else name←typein&lf;
		ttin; if typein=icr then continue else addr←name&typein&lf;
		while true do
			begin ttin; if typein=icr then done else addr←addr&typein&lf;
		free←ynresp("Should this customer get the reports free of charge?")=1;
	else free←lines[p]≠"C";
	thenext←sends←sorrys←""; tbal←reps←0;
	while true do
		begin "next report" 
		label found,notfound;
restart:	if resp("Type short name of "&thenext&"report requested: ",
		 	    sendhelp,nullopt) = 0 then done;
		if brchar≠"*" then begin st←st[1 to ∞-1]; copies←1; end
		else copies ← intscan(typein,brchar);
 		if st[∞ for 1] = "F" then
			begin st←st[1 to ∞-1]; fiche←true; end
		else fiche←false;
		for i←imax step -1 until 0 do if equ(oldrep[i],st) then
			begin k←i; while title[k]=0 do k←k-1;
			go to found;
notfound:	print("I couldn't find that one in the file.",crlf);
		if resp("Enter its specs in the form STAN- or AIM- or HPP-number "&
		   "followed by *AUTHOR,TITLE:"&crlf,rhelp,nullopt)=0 then continue;
		k←bsize; title[k]←typein[1 to ∞-1];
		if imax=bsize-1 then j←2 else
		j←ynresp("Do you want to enter it into the file?");
		case j of begin
		  	continue # go on to next report;
			if ¬new_report(title[k],oldrep,title,onhandh,onhandm,cost,imax)
				then continue  # add this report to the file;
			begin comment no new entry;
				j←ynresp("Do you have "&(if copies=1 then "a copy"
					   else "enough copies")&" on hand?");
				case j of begin
					goto restart;
				if (not fiche) and (not free) and (j=1) then begin
					cost[k]←cresp("What does a copy cost? ");
					if cost[k]<0 then goto restart;
found:		str←title[k]; st←scan(str,bast,brchar);
		if fiche then
			begin amt←0; st←st&"(microfiche) "&str;
			if j≥0 and k<bsize then order(bufm,countm,pm,copies);
		end else begin
			amt←cost[k]*copies; st←st&"(hardcopy) "&str;
			if j≥0 and k<bsize then order(bufh,counth,ph,copies);
		if copies=1 then
			if j≥0 then begin
				if (not free) ∧ (amt>0) then begin
					st←st&"  $"&cvf(cost[k]/100);
			end else sorrys←sorrys&st&crlf
		else comment multiple copies requested;
		    if j≥0 then begin comment we can fill the order;
			    if ¬free ∧ amt>0 then bulkrate(copies);
		    end else begin "split" 
			comment not enough to fill a complete request for
			    multiple copies. See if we should send what's available;
			integer i;
			j←-j # number of copies we're short;
			if j=copies then i←2 comment no copies left, so don't ask;
			else begin
			    print("The customer has requested "&cvs(copies)&" copies ",
				    "of this report, but we have only "&cvs(copies-j),
				    crlf,"    copies on hand.",crlf);
			    i←resp("Should I send the ones we have? (Y or N) ",
			case i of begin "send any?"
			    begin comment split the request, and send what we have;
				comment first set the sorry list;
				comment now calculate the cost, if any;
				if ¬free ∧ amt>0 then bulkrate(copies-j);
				comment now set the list of reports to send;
				if fiche then order(bufm,countm,pm,copies-j)
				else order(bufh,counth,ph,copies-j);
			    begin comment don't send anything (wait for more on hand);
			end "send any?";
		    end "split";
		thenext←"the next ";
		end "next report";
	if ynresp("Before I make up the invoice, you'd better doublecheck the above."&crlf&
	    (if sends≠0 then "We will be sending"&crlf&sends else "")&
	   (if sorrys≠0 then "We will say that we are unable to send"&crlf&sorrys else "")&
	    "Is it all right to make up the invoice?") ≠ 1   then
		begin print("OK, please try again.",crlf);continue;end;
	print("OK, I am making an invoice for this customer.",crlf);
	while pm>0 do
		begin pm←pm-1; k←bufm[pm];
	while ph>0 do
		begin ph←ph-1; k←bufh[ph];
	if key[p]=cvasc("99999") then
	else	begin addr←lab(p,50,false,false);
			key[p]≥cvasc("90000")and key[p]<cvasc("96700"));
		t←lines[p][18 to 18]-"0"; if t>9 then t←t-7;
		lines[p]←lines[p][1 to 17]&cvc(reps+t)&lines[p][19 to ∞];
	end "next user";
if sended then
	print("I wrote the invoices onto file BILLS.TMP. ",crlf,
	    "To print them, do",crlf,
   	    "	    XS BILLS.TMP/NOHEAD",crlf,
	    "and after successful completion don't forget to DEL BILLS.TMP.",crlf,
	    "The file BILLS$.TMP contains a list of the invoices sent (for your",crlf,
	    "permanent record).  You should XSpool it and then DELete it.",crlf);
comment now rewrite the ONHAND.DSK file;
if ¬onhandout(oldrep,title,onhandh,onhandm,cost,imax) then return;
end "send";
comment The `adjust' procedure, to adjust the inventory;

procedure adjust;
begin "onhand"
comment The format of the onhand.dsk file is described in the `send' procedure;

string thenext;
integer imax;
string array oldrep,title[0:bsize];
integer array onhandh,onhandm,cost[0:bsize];

comment first load the current ONHAND.DSK file, into the arrays;
if ¬onhandin(oldrep,title,onhandh,onhandm,cost,imax) then return;
comment adjust one report at a time;
while true do
	begin boolean found, fiche;
	if resp("Type short name of "&thenext&"report to be adjusted: ",
	    onhandhelp,nullopt)=0 then done;
	st←typein[1 to ∞-1];
	if st[∞ for 1]="F" then begin st←st[1 to ∞-1]; fiche←true; end;
	thenext←"the next ";
	for i←imax step -1 until 0 do
		if equ(oldrep[i],st) then begin
			k←i; while title[k]=0 do k←k-1; found←true; done;
	if found then begin
	        comment we've located the entry specified. Now give the user a
		    chance to adjust the quantity;
		if fiche then begin
			print("Number of microfiche copies on hand = ",onhandm[k],crlf,
			      "Enter new quantity of microfiche copies: ");
			ttin; if typein≠icr then onhandm[k]←inscan;
		end else begin
			print("Number of hard copies on hand = ",onhandh[k],crlf,
			      "Enter new quantity of hard copies: ");
			ttin; if typein≠icr then onhandh[k]←inscan;
	end else begin
		comment adding a new entry to the file;
         	print("I couldn't find that one in the file.",crlf);
		if imax=bsize-1 then j←2 
		else j←ynresp("Do you want to enter it into the file?");
		case j of begin
		    begin comment adding a new entry to the file;
			if resp("Enter its specs in the form STAN- or AIM-  or HPP- number"
			      &crlf&"followed by *AUTHOR,TITLE:"&crlf,rhelp,nullopt)=0 
			    then continue;
			new_report(typein[1 to ∞-1],oldrep,title,onhandh,onhandm,cost,imax)
				    # add this report to the file;
		     continue # don't add an entry -- user changed his mind ;

comment have made all changes to the ONHAND file. Now write it out;
if ¬onhandout(oldrep,title,onhandh,onhandm,cost,imax) then  return;

end "onhand";
comment The president (chief executive);

procedure the_president;
begin comment The main control routine for CSREPORT system functions;
if inuse("ADDFIL.DSK") then return;
if lookupfail(iichan,"ADDFIL.DSK") then return;
print("Hello! Please wait a minute while I read in the address file....",crlf);
comment the file is left open for input. This should prevent another user from
	running the same program, and at the same time guard against disk crashes;
while true do begin
case resp(crlf&"  CSREPORT system:  What can I do for you? ",cshelp,csopts) of
		begin done;
if afchanged and ynresp("May I record all of today's transactions permanently"&
	" on file ADDFIL.DSK?")=1 then
	if enterfail(oochan,"ADDFIL.DSK") then return;
else print("No changes made to ADDFIL.DSK this time.",crlf);
if recd+calrecd+abs(fixd)+chgd>0 then
print(crlf,"SUMMARY of today's financial transactions:
    $",cvf(recd/100)," received in payments,
    $",cvf(calrecd/100)," of which was from residents of California.
    $",cvf(fixd/100)," was subtracted from accounts due to adjustments or
	    deletions from the mailing list.
    $",cvf(chgd/100)," new charges were sent out on invoices.",crlf);
comment The program starts here (sets string constants, including HELPs);


setformat(0,2) # format should always be returned to this if changed;

d←t mod 31 + 1;
m←(t←t div 31) mod 12 +1;
date←cvs(d)&" "&months[m]&" "&cvs(1964+ t div 12);

blanks←"                              ";

cshelp←"Hello, this is your friendly CSREPORT system.
There are six kinds of things I am programmed to do for you:
	UPDATE	Look at and perhaps change the mailing list.
	ORDER	File away any orders that have been received
		for a given month's list.
	RECEIVE	Record payments received, or adjust accounts.
	MAIL	Prepare mailing labels and/or invoices and
		bills of lading for everyone that has ordered
		reports from a given month's list.
	SEND	Prepare invoices for isolated back-order requests,
		including orders from people not on the mailing list.
	ADJUST  Change the records in the inventory list, to reflect
		changes in the number of copies on hand.
When I ask, `What can I do for you?', just type the first three letters
of one of these functions and hit carriage RETURN <cr>. (You can also
type more than three letters if you want to.) The user's manual, which
contains more information, is file REPORT.TXT[DOC,CSR].";

findhelp1←"I am going to try to identify an addressee for you. Type the
five-character hash code if you know it, or type 99999 if the
addressee is not on our mailing list and not being inserted into it.
If you don't know the hash code, type XXXXX and I will try a name search
of the whole file. If you type just <cr> now, I will go on to something
else. Since hash codes sometimes change, you should doublecheck the
addressee name I find in case it is the wrong person.";

findhelp2←"I am trying to identify an addressee for you. Type the name
or any part of the name, and I will show you all name lines in the file 
which contain that sequence of characters (including blank spaces in
the middle of the sequence, if you use them). Note that I will search
only the first line of each address on the mailing list.";

codehelp←"Type <cr> to reject this entry and flush it; or type C<cr> for
normal entry, F<cr> for the free list, N<cr> for the ONR list, M<cr> for
the ARPA list, A<cr> for the `automatic' list.";

updhelp←"The UPDATE routine should be used to make all changes to the
address file, since editing with E is risky. To insert a new entry,
type INS and follow instructions. To modify an existing entry, type
MOD and see what happens.  To delete an entry, type DEL (but don't delete
anybody who has orders outstanding on some ORDERS file -- it's best to
delete only after MAILing all orders).  To simply look at an entry,
type LOOK.  Type only <cr> when you want to quit updating.";

ordhelp1←"The ORDERS subsystem is used to record orders received from a
given month's mailing list. The information you enter is recorded in
the file ORDERS.XXX where XXX is JAN,FEB, ..., or DEC.";

ordhelp2←"In ADD mode, you can add new orders to the file. In CHANGE, you
modify a previous entry for a prticular customer, adding or deleting
reports from his list.  You identify a person by his hashcode,
and then say which reports he has ordered.  For example, if he 
wants reports 1,9,A, and G, you can type 19AG or 1AG9, etc.";

mailhelp←"The MAIL subsystem is used to prepare mailing labels and/or
invoices for monthly report distribution.  Two kinds of mailing labels
are presently provided for: AVERY (34 characters wide, one printed
at a time) and CHESHIRE (34 characters wide and three printed at once).
When mailing an abstract list, type ABS<cr> and I will prepare the file
LABELS.TMP containing mailing labels for everyone on the mailing list
except codes N, M, or A.  When mailing invoices and reports, type
REP<cr> and give the necessary information about the relevant month's
reports.  The ORDERS.XXX file for that month will be used to specify
all orders, and the activity records for all customers are shifted
left one position unless you request otherwise.";

acthelp←"Each addressee has activity codes representing the number of orders
he made during the last 12 mailing periods. If you type Y<cr>, the
present mailing is considered a new mailing period.  If you type
N<cr> or <cr>, the present mailing is considered to be combined with
the previous mailing period.";

sendhelp←"Give the short name of a report requested, e.g. CS287 or AIM239 or HPP772,
followed by F if it is microfiche, e.g. CS287F.  But if no more 
reports are requested by this customer, just type <cr>.";

splithelp←"If you type Y, I will send all the copies of this report which we have
on hand, with a note mentioning that we don't have the rest. If you type N
I won't fill any of the order (i.e., as if we were completely out of it),
and I'll send a sorry letter saying so.";

onhandhelp←"The ADJUST routine should be used to make all changes to the
inventory file, since editing with E is risky. To change the quantity
for a report, just enter its short name, e.g. CS287 or AIM 239F
or HPP772. You will be prompted for the new total of copies on hand.
If no more reports are to be updated, just type <cr>.";

yesnohelp←"Answer YES,SIR<cr> or NO,SIR<cr> or some abbreviation.";

rhelp←"At this point I need to know the names and numbers of the reports,
in order to identify them meaningfully on the invoices to
be written. Here are three examples of the form I want you to type:
Note that there should be an asterisk (and no space) between the report
number and the author name(s). The title has to be abbreviated so that
everything fits on one line, even when I substitute the word `(microfiche) '
for the asterisk. If the report is a thesis, follow the title by `(THESIS)'.";
comment Set breaks, open channels, call main procedure, end gracefully;

setbreak(btt,lf,null,"ISK") # for ttin, translates lower case to upper;
setbreak(bfflf,ff&lf,null,"IA") # for scanning lines of character files;
setbreak(bff,ff,null,"IA") # for scanning pages quickly but carefully;
setbreak(blf,lf,null,"IA") # for scanning lines within a page;
setbreak(babs,"|",null,"IS") # for separating substrings delimited by |;
setbreak(bast,"*",null,"IS") # for separating substrings delimited by *;
setbreak(bpar,")",null,"IA") # for substrings ending with );
setbreak(bsp," "&tab,null,"IS") # for scanning to blank or tab;
setbreak(bch," "&tab,null,"XR") # for scanning past blanks and tabs;

open(ichan←getchan,"DSK",0,19,0,450,brchar,eof) # channel for character input;
open(iichan←getchan,"DSK",0,19,0,450,brchar,eof) # channel for ADDFIL input;
open(ochan←getchan,"DSK",0,0,19,0,0,eof) # channel for character output;
open(oochan←getchan,"DSK",0,0,19,0,0,eof) # for ADDFIL output, & backup invoices;
open(lchan←getchan,"DSK",0,0,19,0,0,eof) # channel for mailing label output;




print(crlf,"See you later. Be sure to xspool a copy of DIALOG.TMP,",crlf,
       "which records what happened today.");

setprint(null,"N"); comment closes the dialog file;

close(ichan); close(iichan); 
close(ochan); close(oochan); close(lchan) # just in case I forgot;