From: Jeffrey Carter <jeffrey.carter@boeing.com>
Subject: Re: Return value of system call (newbie question)
Date: Wed, 16 May 2001 16:08:40 GMT
Date: 2001-05-16T16:08:40+00:00 [thread overview]
Message-ID: <3B02A608.EAC44C76@boeing.com> (raw)
In-Reply-To: mailman.990008355.31624.comp.lang.ada@ada.eu.org
Torbj�rn Karfunkel wrote:
>
> I'm executing an external program from within an Ada program by using
>
> function OtterCall(Value : String) return Integer;
> pragma Import(C, OtterCall, "system");
>
> The program that is called upon, Otter, is called with the syntax
>
> otter <input-file> output-file
>
> and the calls look like this (several calls to each are made)
>
> Result := OtterCall(Value =>
> ("/home/toka/otter/otter-3.0.6/source/otter " &
> "<
> /home/toka/exjobb/model_checker/nyare/" &
> ("sat" &
> Natural'Image(K)(2..Natural'Image(K)'Last) & ".in") &
> " >
> /home/toka/exjobb/model_checker/nyare/" &
> ("satresult" &
> Natural'Image(K)(2..Natural'Image(K)'Last) & ".oout")));
>
> Result := OtterCall(Value =>
> ("/home/toka/otter/otter-3.0.6/source/otter " &
> "<
> /home/toka/exjobb/model_checker/nyare/" &
> ("taut" &
> Natural'Image(K)(2..Natural'Image(K)'Last) & ".in") &
> " >
> /home/toka/exjobb/model_checker/nyare/" &
> ("tautresult" &
> Natural'Image(K)(2..Natural'Image(K)'Last) & ".oout")));
>
> Two problems have arisen:
> 1) The first call to otter was executed normally and produced the
> outfile satresult0.oout,
> but the second call, which I thought would produce the outfile
> tautresult0.oout, produced
> an outfile named tautresult0.oout0.oout.
> It seems that some part of the Value string from the previous call
> is appended to the end
> of the next. I solved this problem by appending a sequence of
> blanks to the end of the Value
> strings, but this seems unnecessary. Could anyone give an
> explanation to this behavior?
I suspect this is caused because you are not NUL terminating your
strings. The C "system" function takes a C string (a pointer to the 1st
character of a NUL-terminated string). Pragma Import will take care of
providing the pointer to C, but will not add the NUL.
If we simplify your code a bit:
Program_Path : constant String :=
"/home/toka/otter/otter-3.0.6/source/otter";
File_Path : constant String :=
"/home/toka/exjobb/model_checker/nyare/";
function Image_NLB (Value : Natural) return String is
Image : constant String := Natural'Image (Value);
begin -- Image_NLB
return Image (Image'First + 1 .. Image'Last);
end Image_NLB;
function In_Name (Prefix : String; Number : Natural) return String is
-- null;
begin -- In_Name
return File_Path & Prefix & Image_NLB (Number) & ".in";
end In_Name;
function Out_Name (Prefix : String; Number : Natural) return String is
-- null;
begin -- Out_Name
return File_Path & Prefix & Image_NLB (Number) & ".oout";
end Out_Name;
Result := Ottercall (Program_Path &
" < " &
In_Name ("sat", K) &
" > " &
Out_Name ("satresult", K) &
Ada.Characters.Latin_1.NUL);
This should correct the problem. You could also use
"Interfaces.C.Char_Array" in place of "String" in your specification of
Ottercall, and convert your strings using function Interfaces.C.To_C:
Result := Ottercall (Interfaces.C.To_C (Program_Path &
" < " &
In_Name ("sat", K) &
" > " &
Out_Name ("satresult", K) ) );
--
Jeffrey Carter
prev parent reply other threads:[~2001-05-16 16:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-05-16 10:08 Return value of system call (newbie question) Torbjörn Karfunkel
2001-05-16 15:56 ` Mark Biggar
2001-05-16 16:08 ` Jeffrey Carter [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox