comp.lang.ada
 help / color / mirror / Atom feed
* Return value of system call (newbie question)
@ 2001-05-16 10:08 Torbjörn Karfunkel
  2001-05-16 15:56 ` Mark Biggar
  2001-05-16 16:08 ` Jeffrey Carter
  0 siblings, 2 replies; 3+ messages in thread
From: Torbjörn Karfunkel @ 2001-05-16 10:08 UTC (permalink / raw)
  To: comp.lang.ada

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?
2) The value of Result is 26624. The program executed, Otter, produces
exit codes, and I would
    like to get access to this exit code in some way instead of systems
return value. How is this
    accomplished? The manual on system says

RETURN VALUE
       The value returned is 127 if the execve() call for /bin/sh
fails,  -1  if
       there was another error and the return code of the command
otherwise.

       If  the  value of string is NULL, system() returns nonzero if the
shell is
       available, and zero if not.

       system() does not affect the wait status of any other children.

Grateful for assistance
/Torbj�rn Karfunkel








^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Return value of system call (newbie question)
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Mark Biggar @ 2001-05-16 15:56 UTC (permalink / raw)




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)
>... 
> 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?

Yes, the code is reusing a memory buffer to construct the argument
string. But, the routine system probably expects a terminating nul
character on it's input string.  Add a '& ASCII.NUL' to your 
argument or use the To_C conversion call from Interfaces.C.

> 2) The value of Result is 26624. The program executed, Otter, produces
> exit codes, and I would
>     like to get access to this exit code in some way instead of systems
> return value. How is this
>     accomplished? The manual on system says

This will be quite a bit more complicated and not as portable as
using "system" as you will have to duplicate what "system" does 
internally and that varies with what operating system you are
running under.  On Unix/Linux you wil have to do 'fork' and 'exec'
calls and then a 'wait' call to await the termination of the program.
the return value you are looking for is embedded the return value
of the 'wait' call as a bit field.  On Windows what you have to do is
completely different.

--
Mark Biggar
mark@biggar.org



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Return value of system call (newbie question)
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Jeffrey Carter @ 2001-05-16 16:08 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-05-16 16:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox