{$X+}
program jeeves;

include {NOLIST} 'domain:jsys.def';
include {NOLIST} 'domain:mdep.def';
include {NOLIST} 'domain:master.def';
include {NOLIST} 'domain:lparse.def';
include {NOLIST} 'domain:irdata.hdr';
include {NOLIST} 'domain:dump.hdr';
include {NOLIST} 'domain:pp.hdr';
include {NOLIST} 'domain:mdep.hdr';
include {NOLIST} 'domain:rsolve.hdr';
include {NOLIST} 'domain:iomsg.hdr';

type	string5=packed array[1..5] of char;

var	master,slave:master_block_pointer;
	i:integer;
	myfbp:file_blk_ptr;
procedure cproc(    loadfile:filename; (* filename containing process *)
		  var handle:integer); (* handle of created process *)

{ CPROC creates a subfork from the specified file, mapps the database into
  it, and starts the process.  Note that no capabilities are enabled,
  though all are passed to the subfork }

var	ac1,rval,jfn:integer;
	myfbp:file_blk_ptr;

	procedure cpjerr(str:string5);

        begin
	    myfbp:=ofile(fatl);
	    write(myfbp^.fident,str,' failure during fork creation for ');
	    d_file(myfbp^.fident,loadfile);
	    writeln(myfbp^.fident);
	    pheader(fatl,myfbp);
	    jsys_err(noabort,-1,myfbp);
	    cfile(myfbp);

	    myfbp:=ofile(err); (* repeat with error log instead *)
	    write(myfbp^.fident,str,' failure during fork creation for ');
	    d_file(myfbp^.fident,loadfile);
	    writeln(myfbp^.fident);
	    pheader(err,myfbp);
	    jsys_err(noabort,-1,myfbp);
	    cfile(myfbp);
	end; (* cpjerr *)
begin
jsys(cfork,-2,rval; (* create subfork *)
     [1],0;
     ac1);
if rval=3
then cpjerr('CFORK')
else begin (* normal return from cfork *)
	 handle:=ac1;
	 jsys(gtjfn,-2,rval; (* get JFN for EXE file *)
	      [gjold],loadfile;
	      ac1);
	 if rval=3
	 then cpjerr('GTJFN')
	 else begin (* normal return from GTJFN *)
		  jfn:=ac1;
		  jsys(xget,-2,rval; (* load the fork *)
		       handle:jfn);
		  if rval=3
		  then cpjerr('GET  ')
		  else begin (* normal return from GET *)
			   jsys(pmap,-2,rval; (* map database into fork *)
				fhslf:db_first_page, (* from this fork *)
				handle:db_first_page, (* to slave fork *)
				[pmcnt+18,pmrd+18,pmwr+18]:
					  db_last_page-db_first_page+1);
			   if rval=3
			   then cpjerr('PMAP ')
			   else begin (* normal PMAP, database mapped *)
				    jsys(sfrkv,-2,rval;	(* start process *)
					 handle,0);
				    if rval=3
				    then cpjerr('SFRKV')
				    else begin (* normal return *)
					 end
				end
		       end
	      end
     end
	 
end; (* cproc *)

begin { main }

{ low-level initialization }
irinit;	(* initialize encoded value and format descriptors *)
pp_init;

{ initialize database }
initmaster; (* set master and slave pointers to NIL *)
mapmaster(true,true,true,not test_version);
gcmaster;
logmaster;
mapslave;

master:=getmaster;
slave:=getslave;

{ remove comment characters to force cache dump on initialization }

{if master^.cache_pointer<>NIL
then begin
	 myfbp:=ofile(log);
	 dmpzone(myfbp^.fident,master^.cache_pointer^);
	 cfile(myfbp)
     end;}

if (slave^.dirty<>0)
    OR
   (master^.cupdate>slave^.cupdate)
then cpymaster;

{ start up the procedures }

{ the name server }
cproc(nuforkfile,i);

{ the resolver }

rsolve;
end.
