!Bug reporting/Suggestion Utility.  Run with option -b for BUG, -s for SUGGEST
!J. Butler November 1984

include  "inc:util.imp"
include  "inc:fs.imp"
include  "inc:fsutil.imp"
include  "managr:addefs.inc"
include  "managr:admin.inc"

begin 

constinteger  sugg = 0, bug = 1, fix = 2, bbc = 3, comment = 4, titlelen = 26
ownbyte  suggbug = bug
ownstring  (255) suggbug file = "view:0bugs"
owninteger  i=0, pub=0, instream=0, outstream=0
recordformat  cell fm(record  (cell fm) name  link, string  (127) datum)
record  (cell fm) head
record  (cell fm) name  tail, old, line
record  (admin fm) a
string  (255) data, user, datime, s, t, uname, infile

routine  heapfile(string  (255) s)
   on  3,9 start ; -> eof; finish 

   openinput(3, s); selectinput(3)
   cycle 
      readline(s); length(s)=127 if  length(s)>127
      t=s
      tail_link==new(head); tail==tail_link; tail_datum=t; tail_link==nil
   repeat 

eof:
   close input
   selectinput(instream)
end 


routine  heapline
   string  (255) s
   string  (127) t
   readline(s)
   length(s) = 127 if  length(s) > 127
   if  length(s)>=2 and  charno(s,1)='@' start 
      t = substring(s,2,length(s))
      if  exists(t) then  heapfile(t) and  return 
   finish 
   t = s
   tail_link == new(head)
   tail == tail_link
   tail_datum = t
   tail_link == nil
end 

on  0,3,9 start 
   if  event_event = 0 start 
      -> abort
   elseif  event_event = 3
      -> no file
   else 
      ->eof
   finish 
finish 

head = 0; tail == head
infile = ":"; suggbug = bug
define param("INput file", infile, 0)
define enum param("Suggestion,Bug,Fix,Xper,Comment",suggbug,0)
!xper currently used for BBC s/w
process parameters(cliparam)

user = current user; datime = date." ".time
user = "ANON" if  user = ""
uname=user
if  infile # "" and  infile # ":" and  exists(infile) start 
   openinput(2, infile); selectinput(2); instream = 2
finishelse  instream=0

if  suggbug = bug start 
  printline("APM Bug reporting service: ".uname." reporting at ".datime)
elseif  suggbug = fix
  printline("APM Fix-reporting: You are ".uname)
elseif  suggbug = bbc
  printline("BBC swap-shop: You are ".uname)
  suggbug file = "view:bbc"
elseif  suggbug = comment
  printline("APM gossip column: You are ".uname)
else 
  printline("APM Suggestions box: You are ".uname)
finish 

if  instream = 0 start 
   if  suggbug = bug start 
     printline("Please give a general description of the bug, including what system")
     printline("components are involved, (especially which operating system), whether")
     printline("it is repeatable, and where documentary evidence (if any) may be found.")
     printline("Please also give an opinion as to how severe the bug is.") 
   else 
     printstring("OK, ")
   finish 
   printline("Finish with a Control-Z.")
finish 
   
! Read in the text, saving it on the heap.

prompt("Heading:")
heapline

if  user = "ANON" or  user = "PUB" start 
   prompt("Name please (you're anonymous):")
   heapline
finish 

prompt(":")

cycle 
   heapline
repeat 

eof:
!He's finished with a ctrl-Z. Do the next bit as quickly as possible.
!Open the bug file and write the header line.
line == head_link
stop  if  line == nil
open append(1, suggbug file); outstream = 1; selectoutput(1)
printstring("!<")

t = line_datum
t = t." " while  length(t) < titlelen
length(t) = titlelen
if  charno(t, length(t)) = ' ' then  t = t."  " else  t = t.".."
printstring(uname); spaces(11-length(uname))
if  suggbug = bug start 
   printstring("Bug Report:")
elseif  suggbug = fix
   printstring("Bug Fix:   ")
elseif  suggbug = bbc
   printstring("Item:      ")
elseif  suggbug = comment
   printstring("Comment:   ")
else 
   printstring("Suggestion:")
finish 
printstring(" ".t." "); phex2(etherstation); printline(" ".datime)

newline

!Dump the text.
while  line ## nil cycle 
   printline(line_datum)
   line == line_link
repeat 

Abort:
!If he control-Y'ed, come in here.  
!If we opened the file and dumped header, now dump trailer and close the file
if  outstream # 0 start 
   printline("!> *********************************************************************")
   close append
   selectoutput(0)
   printstring("Thanks.")
   if  suggbug = bug then  printline(" The bug will be ignored indefintely.")
finish 

no file:
selectinput(0)

!Panic over. Now unravel the heap.
tail == head_link
while  tail ## nil cycle 
   old == tail; tail == tail_link; dispose(old)
repeat 

endofprogram