Unit argx;

INTERFACE

function arg_count: integer;
function arg_string(ordinal: integer): string;

IMPLEMENTATION

Uses Dos;

var
	regs: Registers;
	tail: ComStr;
	PSP: ^ComStr;
	PSP_seg, PSP_Ofs: integer;

	test_ix: integer;

procedure get_tail;
begin
	regs.AH := 98;
	Msdos(regs);
	PSP_seg := regs.BX;
	PSP_Ofs := 128;
	PSP := Ptr(PSP_Seg,PSP_Ofs);
	tail := PSP^
end;


function arg_count;

var
	i0, temp: integer;
	at_end: boolean;

begin
	get_tail;
	i0 := 1;
	temp := 0;
	at_end := false;
	repeat
		while (i0 <= length(tail)) and
		   (tail[i0] = ' ') do
			i0 := i0 + 1;
		if i0 <= length(tail) then begin
			temp := temp + 1;
			while (i0 <= length(tail)) and
			   (tail[i0] <> ' ') do
				i0 := i0 + 1;
			if i0 > length(tail) then
				at_end := true
			end
		else at_end := true
	until at_end;
	arg_count := temp
end;

function arg_string;
var
	i0, temp, start_arg, end_arg: integer;
	at_end: boolean;
begin
	if (ordinal < 1) or
	   (ordinal > arg_count) then
		arg_string := ''
	else begin
		i0 := 1;
		at_end := false;
		temp := 0;
		repeat
			while (i0 <= length(tail)) and
			  (tail[i0] = ' ') do
				i0 := i0 + 1;
			if i0 <= length(tail) then begin
				temp := temp + 1;
				start_arg := i0;
				while (i0 <= length(tail)) and
				   (tail[i0] <> ' ') do
					i0 := i0 + 1;
				end_arg := i0 - 1;
				if temp = ordinal then begin
					at_end := true;
					arg_string := copy(tail,start_arg,
					   (end_arg - start_arg + 1))
					end
				end
			else begin
				arg_string := '';
				at_end := true
				end
		until at_end
		end
end;


end.
