From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!attcan!uunet!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!usc!ucsd!ucsdhub!hp-sdd!hplabs!hpda!hpcuhb!hpcllla!hpclisp!defaria@hpclapd.HP.COM From: defaria@hpclapd.HP.COM (Andy DeFaria) Newsgroups: comp.lang.ada Subject: Re: Unix (HP-UX) system calls from ADA Message-ID: <920020@hpclapd.HP.COM> Date: 21 Mar 90 17:09:34 GMT References: <1587@krafla.rhi.hi.is> Organization: Hewlett-Packard Calif. Language Lab List-Id: >/ 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 ===============================================================================