comp.lang.ada
 help / color / mirror / Atom feed
From: defaria@hpclapd.HP.COM (Andy DeFaria)
Subject: Re: Unix (HP-UX) system calls from ADA
Date: 21 Mar 90 17:09:34 GMT	[thread overview]
Message-ID: <920020@hpclapd.HP.COM> (raw)
In-Reply-To: 1587@krafla.rhi.hi.is

>/ hpclapd:comp.lang.ada / valdi@rhi.hi.is (Thorvaldur Sigurdsson) /  7:57 am  Mar 20, 1990 /

>Problem       : Making unix system calls within Ada code
>Background    : HP-UX 6.5, HP 9000/300 series, Alsys 4.35 Ada compiler
>Error message : 
>		ld: Undefined external -
>		      _ll
>		      _clear
>		ld: output file still contains undefined symbols
>		ld: (Warning) did not generate an output file
>		ada: Errors detected

Your problem is that ll and clear are not system calls  but  commands.  If you
want to execute commands  then you want to call  the system(3C) system call
which can be accomplished like this:

package UNIX_SYSTEM_CALL is
 
  procedure EXECUTE_UNIX_COMMAND (COMMAND : STRING);

end UNIX_SYSTEM_CALL;

package body UNIX_SYSTEM_CALL is

   type C_STRING is access CHARACTER;
   type A_STRING is access STRING;
   type STATUS_T is new INTEGER;

   -- Note that SYSTEM is a reserved word in Ada because of the package
   -- named SYSTEM therefore we'll change it to C_SYSTEM.  This then requires
   -- that we use pragma INTERFACE_NAME so that the linker will search for the
   -- proper external.
   function C_SYSTEM (COMMAND : C_STRING) return STATUS_T;
   pragma INTERFACE (C, C_SYSTEM);
   pragma INTERFACE_NAME (C_SYSTEM, "system");

   function CONVERT_TO_C (SRC : STRING) return C_STRING is

      function C_STRING_CONVERT is new UNCHECKED_CONVERSION
         (SOURCE => SYSTEM'ADDRESS, TARGET => C_STRING);

      C_STORAGE : A_STRING := new STRING'(SRC & ASCII.NUL);

   begin -- CONVERT_TO_C
     return C_STRING_CONVERT (C_STORAGE.all (C_STORAGE.all'FIRST)'ADDRESS);

   end CONVERT_TO_C;

   procedure EXECUTE_UNIX_COMMAND (COMMAND : STRING) is

   begin -- EXECUTE_UNIX_COMMAND
      STATUS := C_SYSTEM (CONVERT_TO_C ("clear"));

      if STATUS = -1 then 
         raise PROGRAM_ERROR;
      end if;

   end EXECUTE_UNIX_COMMAND;

end UNIX_SYSTEM_CALL;  -- package specification

Notes:
	HP_UX system calls are written in  C and thereby expect  strings to
	be in "C" format (null  terminated).  This is accomplished with the
	CONVERT_TO_C routine which allocates a new string, null terminated,
	then UNCHECKED_CONVERSION's it to a type called C_STRING; The above
	implementation  hides this  from the Ada user  by only requiring an
	Ada string  on the  EXECUTE_UNIX_COMMAND  call and  performing  the
	conversion in the package body.

	Since SYSTEM  is a  reserved word  in Ada C_SYSTEM is used instead,
	requiring that we inform the  linker of the real external  function
	name via a pragma INTERFACE_NAME.

	No extra libraries  need to be specified  for the link  phase for a
	system(3C) call.  If, however, you tried to  call a curses(3X) call
	then you  would  need to specify the  curses library for  the link.
	This is no different than what the C programmer would have to do.

	Most system calls  return a status  that should be checked.  Here I
	simply raised PROGRAM_ERROR if the  system(3C) call fails.  You may
	want to return the error or do something else.

Rather  than write to  specific calls  for two  different  HP_UX commands I
choose to code a more general call that  executes the HP_UX command that is
passed in.  Your main program can now call these two commands, or any other
HP_UX commands, like so:

procedure MAIN is

begin 
   UNIX_SYSTEM_CALL.EXECUTE_UNIX_COMMAND ("clear");
   UNIX_SYSTEM_CALL.EXECURE_UNIX_COMMAND ("ll");

end MAIN;

Hope this was helpful.

===============================================================================
I know you believe you understand what you think I   | Andrew DeFaria
said but I'm not sure you realize that what you heard| Hewlett Packard
is not what I meant!                                 | California Langauge Labs
===============================================================================

  parent reply	other threads:[~1990-03-21 17:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1990-03-20 15:57 Unix (HP-UX) system calls from ADA Thorvaldur Sigurdsson
1990-03-20 21:22 ` Karl A. Nyberg
1990-03-21 17:09 ` Andy DeFaria [this message]
1990-03-21 18:46 ` Andy DeFaria
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox