(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
[inherit('sys$library:starlet')]

program p3z(input,output);

const
   blocksize = 8192;
   blocks = 10;
   shrmemsiz = blocks * blocksize;
   maxmsgsiz = 100;
   maxmsgs = shrmemsiz div maxmsgsiz;
   name = 'M';

type
   msgtype = packed array [1..maxmsgsiz] of char;
   shrmemtype = record
                   case boolean of
                      true : ( dummy : packed array [1..shrmemsiz] of char; );
                      false : ( msg : packed array [1..maxmsgs] of msgtype; );
                end;

var
   shrmem : ^shrmemtype;
   range : array [1..2] of pointer;
   stat, i, msglen : integer;

begin
   (* map global section *)
   range[1] := nil;
   range[2] := nil;
   stat := $mgblsc(inadr := range, retadr := range, flags := SEC$M_EXPREG + SEC$M_WRT, gsdnam := name);
   if not odd(stat) then writeln('sys$mgblsc stat=', stat:1);
   shrmem := range[1];
   (* write to global section *)   
   i := 1;
   while shrmem^.msg[i][1] <> chr(0) do i := i + 1;
   shrmem^.msg[i] := 'Hi from Pascal (alt)!' + chr(0);
   (* dump global section *)
   for i := 1 to maxmsgs do begin
      if shrmem^.msg[i][1] <> chr(0) then begin
         msglen := index(shrmem^.msg[i], chr(0));
         writeln('|' + substr(shrmem^.msg[i], 1, msglen) + '|');
      end;
   end;
end.
