1#!/usr/bin/perl -w
2
3
4$ident="\$RCSfile:\$ \$Revision:\$ \$Date:\$";
5$right="Copyright (c) 2000, Douglas R. Jerome.";
6
7
8# ******************************************************************************
9#
10# hackerlabs:  man page ripper
11#
12#	----------------------------------------------------------------
13#
14# Copyright (c) 2000, 2001 Douglas R. Jerome, Peoria, AZ USA
15#
16#	This program is free software; you can redistribute it and/or modify
17#	it under the terms of the GNU General Public License as published by
18#	the Free Software Foundation; either version 2 of the License, or
19#	(at your option) any later version.
20#
21#	This program is distributed in the hope that it will be useful,
22#	but WITHOUT ANY WARRANTY; without even the implied warranty of
23#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24#	GNU General Public License for more details.
25#
26#	You should have received a copy of the GNU General Public License
27#	along with this program; if not, write to the Free Software
28#	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29#
30#	----------------------------------------------------------------
31#
32# FILE NAME
33#
34#	$RCSfile:$
35#	$Revision:$
36#	$Date:$
37#
38# PROGRAM INFORMATION
39#
40#	Developed by:	hackerlabs project
41#	Developer:	Douglas R. Jerome, drj, <jerome@primenet.com>
42#
43# FILE DESCRIPTION
44#
45#	[ FIXME ]
46#
47#	exit error codes:
48#
49#		0 - All OK, I hope.
50#		1 - No rip or coalesce file.
51#		2 - Both rip and coalesce file.
52#		3 - No directory.
53#		4 - Can't open file to rip.
54#		5 - Can't open rip output file.
55#		6 - Can't open file to coalesce.
56#
57# CHANGE LOG
58#
59#	27jun01	drj	Created several verbose levels.
60#
61#	13apr00	drj	File generation.
62#
63# ******************************************************************************
64
65
66# *****************************************************************************
67# Library usage.
68# *****************************************************************************
69
70# -- perl/lib
71#
72require "assert.pl";
73require "getopts.pl";
74
75# -- local lib
76#
77#  (None.)
78
79
80# *****************************************************************************
81# Set Some Operational Characteristics
82# *****************************************************************************
83
84select STDOUT;	# Set the default file handle for output operations.
85
86$| = 1;		# Flush output for every print or write, instead of the
87		# default of flushing output on end-of-line.
88
89
90# *****************************************************************************
91# Setup some global variables.
92# *****************************************************************************
93
94# -- Miscellaneous constant values:
95#
96$RIPPER_ERR  = "ripper.pl-ERROR: ";
97$RIPPER_WARN = "ripper.pl-WARNING: ";
98$RIPPER_INFO = "ripper.pl-INFO: ";
99
100# -- Main program variables:
101#
102$date   = `date`;
103# $scalar = "";
104# %array  = ();
105
106# These global variables are not really variables, they're file handles; their
107# usage is documented here.
108#
109#	SOURCE	This is a file handle used to read [FIXME}
110#
111#	DEST	This is a file handle used to write [FIXME}
112#
113#	RIP	This is a file handle used to write [FIXME}
114
115
116# *****************************************************************************
117# Local subroutines.
118# *****************************************************************************
119
120# -------------------------------------------------------------------------
121# Subroutine errorPrint (print a snappy error message)
122# -------------------------------------------------------------------------
123
124sub   errorPrint
125   {
126   local ($arg) = @_;
127   print "$RIPPER_ERR $arg.\n";
128   1;
129   }
130
131# -------------------------------------------------------------------------
132# Subroutine warnPrint (for consistent message format)
133# -------------------------------------------------------------------------
134
135sub   warnPrint
136   {
137   local ($arg) = @_;
138   print "$RIPPER_WARN $arg.\n";
139   1;
140   }
141
142# -------------------------------------------------------------------------
143# Subroutine infoPrint (for consistent message format)
144# -------------------------------------------------------------------------
145
146sub   infoPrint
147   {
148   local ($arg) = @_;
149   print "$RIPPER_INFO $arg.\n";
150   1;
151   }
152
153# -------------------------------------------------------------------------
154# Subroutine usage
155# -------------------------------------------------------------------------
156
157sub   usage
158   {
159   print "\n";
160   print "usage:  mrip  [ -v ]  -r <file>  -d <dir>  -k <keyword>\n";
161   print "        mrip  [ -v ]  -c <file>  -d <dir>  -k <keyword>\n";
162   print "\n";
163   print " \t-v             verbose mode\n";
164   print " \t-d <dir>       directory to rip/coalesce to/from\n";
165   print " \t-r <file>      rip from <file>\n";
166   print " \t-c <file>      coalesce to <file>\n";
167   print " \t-k <keyword>   SUBSYSTEM or SUBROUTINE\n";
168   print "\n";
169   }
170
171# -------------------------------------------------------------------------
172# Subroutine cpState
173# -------------------------------------------------------------------------
174
175sub   cpState
176   {
177   local ($opt_v, $opt_d, $opt_k, $unused, $_) = @_;
178   local ($nextState, $name);
179   $nextState = "cpState";
180   $name = $unused;
181   if (/^<$opt_k NAME=\"(\w+)\">/o)
182      {
183      $name = $1;
184      $nextState = "nameState";
185      }
186   else
187      {
188      print DEST $_;
189      }
190   $nextState, $name;
191   }
192
193# -------------------------------------------------------------------------
194# Subroutine nameState
195# -------------------------------------------------------------------------
196
197sub   nameState
198   {
199   local ($opt_v, $opt_d, $opt_k, $n, $_) = @_;
200   if ($opt_v eq "")  { print "."; }
201   else { if ($opt_v eq "1") { print "\t$n\n"; }
202   else { if ($opt_v eq "2") { print "<$opt_k NAME=\"$n\">\n"; } } }
203   print DEST "<INSERT NAME=\"$n\">\n";
204   if ($opt_k eq "SUBROUTINE")
205      {
206      open RIP, ">$opt_d/$n.3" or &errorPrint ("can't open $opt_d/$n.3 rip");
207      print RIP "$n(3)\n";
208      }
209   else
210      {
211      if ($opt_k eq "SUBSYSTEM")
212         {
213         open RIP, ">$opt_d/$n.l" or &errorPrint ("can't open $opt_d/$n.l rip");
214         print RIP "$n(l)\n";
215         }
216      else
217         {
218         open RIP, ">$opt_d/$n.x" or &errorPrint ("can't open $opt_d/$n.x rip");
219         print RIP "$n(x)\n";
220         }
221      }
222   print RIP "$_";
223   "ripState", "";
224   }
225
226# -------------------------------------------------------------------------
227# Subroutine ripState
228# -------------------------------------------------------------------------
229
230sub   ripState
231   {
232   local ($opt_v, $opt_d, $opt_k, $name, $_) = @_;
233   local ($nextState);
234   $nextState = "ripState";
235   if (/^<\/$opt_k>/o)
236      {
237      $nextState = "cpState";
238      close RIP;
239      }
240   else
241      {
242      print RIP $_;
243      }
244   $nextState, "";
245   }
246
247# -------------------------------------------------------------------------
248# Subroutine ripMe
249# -------------------------------------------------------------------------
250
251sub   ripMe
252   {
253   local ($opt_v, $opt_r, $opt_d, $opt_k) = @_;
254   local ($name) = "";
255   $stateFunc = "cpState";
256   if (open SOURCE, "<$opt_r")
257      {
258      if (open DEST, ">$opt_r.rip")
259         {
260         while (<SOURCE>)
261            {
262$cmd = "(\$stateFunc,\$name)=&$stateFunc(\$opt_v,\$opt_d,\$opt_k,\$name,\$_);";
263eval $cmd;
264            }
265         print "\n" if ($opt_v eq "");
266         close DEST;
267         }
268      else
269         {
270         &errorPrint ("can't open \"$opt_r.rip\" for ripping");
271         exit 5;
272         }
273      close SOURCE;
274      }
275   else
276      {
277      &errorPrint ("can't open \"$opt_r\" for ripping");
278      exit 4;
279      }
280   1;
281   }
282
283# -------------------------------------------------------------------------
284# Subroutine beatMe
285# -------------------------------------------------------------------------
286
287sub   beatMe
288   {
289   local ($opt_v, $opt_c, $opt_d, $opt_k) = @_;
290   local ($name) = "";
291
292   if (open SOURCE, "<$opt_c")
293      {
294      if (open DEST, ">$opt_c.coal")
295         {
296         while (<SOURCE>)
297            {
298            if (/^<INSERT NAME=\"(\w+)\">/o)
299               {
300               $name = $1;
301               if ($opt_k eq "SUBROUTINE")
302                  {
303                  if (! open RIP, "<$opt_d/$name.3")
304                     { $name = ""; }
305                  }
306               else
307                  {
308                  if ($opt_k eq "SUBSYSTEM")
309                     {
310                     if (! open RIP, "<$opt_d/$name.l")
311                        { $name = ""; }
312                     }
313                  else
314                     { $name = ""; }
315                  }
316               if ($name ne "")
317                  {
318                  print DEST "<$opt_k NAME=\"$name\">\n";
319                  <RIP>; # Throw away the first line.
320                  while (<RIP>)
321                     { print DEST; }
322                  close RIP;
323                  print DEST "<\/$opt_k>\n";
324                  }
325               else
326                  {
327                  print DEST;
328                  }
329               }
330            else
331               {
332               print DEST;
333               }
334            }
335         close DEST;
336         }
337      else
338         {
339         &errorPrint ("can't open \"$opt_c.coal\" coalescing");
340         exit 5;
341         }
342      close SOURCE;
343      }
344   else
345      {
346      &errorPrint ("can't open \"$opt_c\" for coalescing");
347      exit 6;
348      }
349
350   1;
351   }
352
353
354# *****************************************************************************
355# Main program.
356# *****************************************************************************
357
358# -- Environment Variables
359#
360# $myEnvVar = $ENV{'MY_ENV_VAR'};	# Location of scripts & tools.
361# $myEnvVar = "." if ! $myEnvVar;	# Current directory, if not defined.
362# if (! $myEnvVar)
363#   { print "$0-WARNING:  no environment variable.\n"; }
364
365# -- Command Line Switches
366#
367# There are some possible arguments to this perl script.  These arguments are
368# NOT expected to be in order, and can be repeated!  The positional description
369# below is recomended.
370#
371# $ARGV[0]     -v                                              (set verbose)
372#       This argument is the "verbose mode".  This perl script will generate
373#       extra output that is (hopefully) usefull for the user.
374#
375# $ARGV[1]     -c
376#       This argument is
377#
378# $ARGV[1]     -d
379#       This argument is
380#
381# $ARGV[1]     -r
382#       This argument is
383#
384# $ARGV[1]     -k
385#       This argument is
386#
387&Getopts ('v:c:d:k:r:');
388 print "No opt_v.\n" if ! defined $opt_v;
389 print "opt_v = $opt_v.\n" if defined $opt_v;
390 print "No opt_c.\n" if ! defined $opt_c;
391 print "opt_c = $opt_c.\n" if defined $opt_c;
392 print "No opt_d.\n" if ! defined $opt_d;
393 print "opt_d = $opt_d.\n" if defined $opt_d;
394 print "No opt_k.\n" if ! defined $opt_k;
395 print "opt_k = $opt_k.\n" if defined $opt_k;
396 print "No opt_r.\n" if ! defined $opt_r;
397 print "opt_r = $opt_r.\n" if defined $opt_r;
398$opt_v = "" unless defined $opt_v && $opt_v ne "";
399$opt_c = "" unless defined $opt_c && $opt_c ne "";
400$opt_d = "" unless defined $opt_d && $opt_d ne "";
401$opt_r = "" unless defined $opt_r && $opt_r ne "";
402$opt_k = "" unless defined $opt_k && $opt_k ne "";
403
404# -- Identify ourself if we are verbose.
405#
406print "\n$date\n$ident\n$right\n\n" if $opt_v eq "2";
407
408# -- Sanity Checkup
409#
410if (($opt_c eq "") && ($opt_r eq ""))
411   {
412   &errorPrint ("no rip/coalesce file");
413   &usage();
414   &infoPrint ("finished executing") if $opt_v eq "2";
415   exit 1;
416   }
417if (($opt_c ne "") && ($opt_r ne ""))
418   {
419   &errorPrint ("can't do both rip and coalesce");
420   &usage();
421   &infoPrint ("finished executing") if $opt_v eq "2";
422   exit 2;
423   }
424if (($opt_d eq "") || (! -d $opt_d) || (! -x $opt_d))
425   {
426   &errorPrint ("no directory (or wrong permissions) $opt_d");
427   &usage();
428   &infoPrint ("finished executing") if $opt_v eq "2";
429   exit 3;
430   }
431
432# -- Rip
433#
434if (($opt_c eq "") && ($opt_r ne ""))
435   {
436   &ripMe ($opt_v, $opt_r, $opt_d, $opt_k);
437   }
438
439# -- Coalesce
440#
441if (($opt_c ne "") && ($opt_r eq ""))
442   {
443   &beatMe ($opt_v, $opt_c, $opt_d, $opt_k);
444   }
445
446
447# *****************************************************************************
448# Exit OK.
449# *****************************************************************************
450
451&infoPrint ("finished executing") if $opt_v eq "2";
452exit 0;
453