{$symtab-,$linesize:131,$pagesize:86,
$title:'ARGLIST.PAS -- "argc/argv" for PASCAL'}
{	COPYRIGHT @ 1982
	Jim Holtman and Eric Holtman
	35 Dogwood Trail
	Randolph, NJ 07869
	(201) 361-3395
}
{$include:'arglist.inc'}
implementation of arglist;
const
    BLANK = ' ';
    QUOTE = '''';
var
    argvlist : array[1..20] of ^lstring;
    newparm, parmlist : lstring(100);
    i,j : integer;
    state : (leading_ws,non_quote,quoted,end_quote);
    inchar : char;
function ppmuqq(u1 : word; u2 : adrmem; var dest : lstring) : word;
    external;

procedure argv;
begin
    if (i > 0) and (i <= argc) then copylst(argvlist[i]^,a)
    else copylst(null,a)
end;

procedure saveparm;
begin
    argc := argc+1;
    new(argvlist[argc],ord(newparm.len));
    copylst(newparm,argvlist[argc]^);
    newparm.len := 0;
end;

begin
    argc := 0;
    eval(ppmuqq(0,adr null,parmlist));
    if parmlist.len = 0 then return;
    concat(parmlist,BLANK);
    state := leading_ws;
    newparm.len := 0;
    for i := 1 to ord(parmlist.len) do begin
	inchar := parmlist[i];
	case state of

	leading_ws:
	    if inchar = QUOTE then state := quoted
	    else if inchar <> BLANK then begin
		copylst(inchar,newparm);
		state := non_quote
	    end;

	non_quote:
	    if inchar = BLANK then begin
		saveparm;
		state := leading_ws
	    end
	    else concat(newparm,inchar);

	quoted:
	    if inchar = QUOTE then state := end_quote
	    else concat(newparm,inchar);

	end_quote:
	    if inchar = QUOTE then begin
		concat(newparm,inchar);
		state := quoted
	    end
	    else if inchar <> BLANK then begin
		concat(newparm,inchar);
		state := non_quote
	    end
	    else begin
		saveparm;
		state := leading_ws
	    end;

	end;
    end;
end.
