findPort : a new Unix filter

In an upcoming Tcl/Tk program I need a series of small Unix filter style programs. The program is fed with an argument and it looks up the answer in a system resource. The answer is in the form of an ASCII string.

In this case, I need a filter that looks up the I/O address of a given portname. In DOS, the port addresses had fixed locations in the BIOS area at 0000:0400 and up. In Linux there is a better way: a text device located at /proc/ioports. Just issue the following command from a console:

	$ cat /proc/ioports
   
There are lots of ports active, as you can see. And findPort just taps into this resource.

findPort : the source

Below is the source for the findPort program:

MODULE findPort;

IMPORT	Arguments, InOut, Strings, TextIO;

TYPE	NumberString		= ARRAY [0..7] OF CHAR;

VAR	Options			: Arguments.ArgTable;
	count			: SHORTCARD;
	inFile, outFile		: TextIO.File;
	char			: CHAR;
	i			: CARDINAL;
	String			: NumberString;
	parameter, portname	: ARRAY [0..15] OF CHAR;


PROCEDURE GetChar;

BEGIN
   TextIO.GetChar (inFile, char);
   IF  TextIO.EOF (inFile)  THEN
      InOut.WriteString ("-Error: Portname not found.");
      InOut.WriteLn;
      HALT
   END
END GetChar;


BEGIN
   Arguments.GetArgs (count, Options);
   Strings.Assign (portname, Options^[1]^);
(*
	InOut.WriteString (portname);
	InOut.WriteString ('  .............');
	InOut.WriteLn;
*)
   TextIO.OpenInput (inFile, '/proc/ioports');
   IF  NOT TextIO.Done ()  THEN
      InOut.WriteString ("Your system has no support for '/proc/ioports'. Aborting.");
      InOut.WriteLn;
      HALT
   END;
   LOOP
      i := 0;
      REPEAT  GetChar  UNTIL  char > ' ';
      REPEAT
	 String [i] := char;
	 INC (i);
	 GetChar
      UNTIL  (char = '-') OR (char <= ' ');
      String [i] := 0C;
      REPEAT  GetChar  UNTIL  char = ':';
      i := 0;
      REPEAT  GetChar  UNTIL  char > ' ';
      REPEAT
	 parameter [i] := char;
	 INC (i);
         GetChar
      UNTIL  char = 12C;
      parameter [i] := 0C; 
      IF  Strings.StrEq (parameter, portname)  THEN
	 InOut.WriteString (String);
	 InOut.WriteLn;
	 HALT
      END;
   END
END findPort.
   

findPort : eating the pudding

As always, the proof of the eating, is in the pudding! So here we go and have a ball!

jan@beryllium:~/modula/TCL$ ./findPort fruttPort
-Error: Portname not found.
jan@beryllium:~/modula/TCL$ ./findPort parport0
0378
jan@beryllium:~/modula/TCL$ ./findPort parport1
eff0
jan@beryllium:~/modula/TCL$ ./findPort PCI\ CardBus\ #03
4000
jan@beryllium:~/modula/TCL$ findPort pnp\ 00:09
0290

0378-037a : parport0
0290-0297 : pnp 00:09
4000-40ff : PCI CardBus #03
  eff0-eff2 : parport1
   
As the latter examples show, you can use escape sequences to include special characters as part of a port descriptor.

Page created 5 May 2008,