Operation System Tools

Site in transition, you might want to look at the old version

File utilities

Most of the file manipulations are implemented using calls to external program's. See below to see how this is done.

Copy Files

Copy Files. You will need a Unix style “cp” command to use this package.

Sort Text Files

Copy Files. You will need a Unix style “sort” command to use this package. On Windows and OS/2 you need to make a copy of “sort.exe” named “gsort.exe” because both operating systems come with a rather stupid sort.

Rename and Move Files

Renames or moves files. Uses GNAT.Os_Lib.

Delete Files

Delete files. Uses GNAT.Os_Lib.

Convert End of Line Marker

Changes files end of line markers. You will need a Unix style “dos2unix” and “dos2unix” command to use this package. On OS/2 the commands are calls “unix2os2” and “os22unix”.

Mount File systems

Only available for Linux.

Manipulate Extended Attributes

Currently only available for OS/2.

Execution of external Programs

One of the most Important features for Scripting Languages is the easy execution of external program's. In vain I have been looking at the ability of using pipes '|'<A CLASS="sdfootnoteanc" NAME="sdfootnote1anc" HREF="#sdfootnote1sym"><SUP>1</SUP></A> of normal shells. Or even cooler the $(command)<A CLASS="sdfootnoteanc" NAME="sdfootnote2anc" HREF="#sdfootnote2sym"><SUP>2</SUP></A> option of “bash”.

The AdaCL.OS Package gives you all you need for executing other program's. For this AdaCL offers three differed kind of execution modes:

Synchronous Execution

This is the easiest case. All you have to do is crate an new AdaCL.OS.Command Object and call Start on it:

 Test1 : declare
    Command :  AdaCL.OS.Command.Object
            := AdaCL.OS.Command.New_Object (
                   Command   => "ls",
                   Arguments => "-la",
                   ExecMode  => AdaCL.OS.Command.Sync);
 begin
    AdaCL.OS.Command.Start (Command);
 exception
    when AnException : others =>
        AdaCL.Trace.Write_Error (AnException, AdaCL.Trace.Entity, AdaCL.Trace.Source);
 end Test1;

Asynchronous Execution

This too is quite easy. Again you create an AdaCL.OS.Command.Object and call Start on it. Then however you can do something else. Later you should call Wait to wait for the end of your program.

 Test2 : declare
    Command :  AdaCL.OS.Command.Object
            := AdaCL.OS.Command.New_Object (
                   Command   => "ls",
                   Arguments => "-a",
                   ExecMode  => AdaCL.OS.Command.Async);
 begin
    AdaCL.OS.Command.Start (Command);
    --
    --  Do something else.
    --
    AdaCL.OS.Command.Wait  (Command);
 exception
    when AnException : others =>
        AdaCL.Trace.Write_Error (AnException, AdaCL.Trace.Entity, AdaCL.Trace.Source);
 end Test2;

In/Out Execution

After starting you program as before you can attach Ada.Text_IO.File_Type to the standard input, standard output and standard error pipes. You can then use normal Ada.Text_IO to read and write to your program. The following demo uses the external command “§n§cat”§§ to add line numbers to some text.

 Test3 : declare
    Command     :  AdaCL.OS.Command.Object
                := AdaCL.OS.Command.New_Object (
                       Command   => "cat",
                       Arguments => "--number -"
                       ExecMode  => AdaCL.OS.Command.In_Out);
    Command_In  : Ada.Text_IO.File_Type;
    Command_Out : Ada.Text_IO.File_Type;
 begin
    AdaCL.OS.Command.Start (Command);

    AdaCL.OS.Command.Text_IO.Attach_In  (Command, Command_In);
    AdaCL.OS.Command.Text_IO.Attach_Out (Command, Command_Out);

    Ada.Text_IO.Put_Line (Command_In, "First Line");
    Ada.Text_IO.Put_Line (Command_In, "Middle Line");
    Ada.Text_IO.Put_Line (Command_In, "Last Line");
    Ada.Text_IO.Flush    (Command_In);

    Ada.Text_IO.Put_Line (AdaCL.Strings.IO.Get_Line (Command_Out));
    Ada.Text_IO.Put_Line (AdaCL.Strings.IO.Get_Line (Command_Out));
    Ada.Text_IO.Put_Line (AdaCL.Strings.IO.Get_Line (Command_Out));

    Ada.Text_IO.Close (Command_In);
    Ada.Text_IO.Close (Command_Out);

    AdaCL.OS.Command.Wait (Command);
 exception
    when AnException : others =>
        AdaCL.Trace.Write_Error (AnException, AdaCL.Trace.Entity, AdaCL.Trace.Source);
 end Test3;

Please note that Tasks will block when a process is waiting for input data to arrive or output data to be processed. Execution is therefore depended on OS Buffering (i.e.: Linux only 4Kb). You are best off to always attach to all streams. The AdaCL.OS.Command.Tee package provide some useful null streams.

Text IO on Commands

The AdaCL.OS.Command.Text_IO package allows you to attach the IO Streams of your Program to a Ada.Text_IO.File_Type.

Sequential IO on Command

The AdaCL.OS.Command.Sequential_IO package allows you to attach the IO Streams of your Program to a Ada.Sequential_IO.File_Type.

Pipe from one Command to the next.

The package AdaCL.OS.Command.Pipe allows pipe simple from one command to the next. The streams not needed are attached to null streams.

Discard unneeded stream data

The package AdaCL.OS.Command.Tee allows to discard unneeded stream data so the process will not block because the IO Buffer filled up. If trace is on data will be traced. If verbose is on the data is copied to standard output. Works only when data is Text<A CLASS="sdfootnoteanc" NAME="sdfootnote3anc" HREF="#sdfootnote3sym"><SUP>3</SUP></A>.

Recording to CD's

The package AdaCL.OS.CD_Record contains a binding to “cdrecord”, “eject”, “mkisofs” and other tools needed for working with a CD recorder.

More Examples

You find some more examples with the following files. The links should take to “§n§gnathtml”§§ generated web sides.

<DIV ALIGN=RIGHT>

	<TABLE WIDTH=100% BORDER=1 CELLPADDING=6 CELLSPACING=5 BGCOLOR="FloralWhite" STYLE="page-break-before: always">
		<COL WIDTH=84*>
		<COL WIDTH=84*>
		<COL WIDTH=87*>
		<THEAD>
			<TR BGCOLOR="OldLace" VALIGN=TOP>
				<TH WIDTH=33% BGCOLOR="OldLace">

Examples for Synchronous Execution

				</TH>
				<TH WIDTH=33% BGCOLOR="OldLace">

Examples for Asynchronous Execution

				</TH>
				<TH WIDTH=34% BGCOLOR="OldLace">

Examples for In/Out Execution

				</TH>
			</TR>
		</THEAD>
		<TBODY>
			<TR BGCOLOR="FloralWhite" VALIGN=TOP>
				<TD WIDTH=33% BGCOLOR="FloralWhite">

				</TD>
				<TD WIDTH=33% BGCOLOR="FloralWhite">

				</TD>
				<TD WIDTH=34%>

				</TD>
			</TR>
		</TBODY>
	</TABLE>

</DIV>


# Execute two programs and send the output of the first program as input into the second program. # Execute a program and return the output as a string variable. # Programs producing binary data do so for a reason. But if, against all odds, you do need to discard binary data it should not be to difficult to create a new package to do so.


Ada programming, © 2005,2006 the Authors, Content is available under GNU Free Documentation License.