3 # $Header: perldb,v 2.0 88/06/05 00:09:45 root Exp $
6 # Revision 2.0 88/06/05 00:09:45 root
7 # Baseline version 2.0.
11 $tmp = "/tmp/pdb$$"; # default temporary file, -o overrides.
15 while ($ARGV[0] =~ /^-/) {
17 /^-o$/ && ($tmp = shift,next);
18 die "Unrecognized switch: $_";
22 die "Usage: perldb [-o output] scriptname arguments" unless $filename;
24 open(script,$filename) || die "Can't find $filename";
26 open(tmp, ">$tmp") || die "Can't make temp script";
28 $perl = '/usr/bin/perl';
32 # now translate script to contain DB calls at the appropriate places
37 if (/^#! *([^ \t]*) (-[^ \t]*)/) {
41 elsif (/^#! *([^ \t]*)/) {
46 push(@script,$_); # remember line for DBinit
48 next if /^$/; # blank lines are uninteresting
49 next if /^[ \t]*#/; # likewise comment lines
51 print tmp "do DBinit($.);"; $init = '';
53 if ($inform) { # skip formats
60 if (/^[ \t]*format /) {
64 if ($state eq 'statement' &&
65 !/^[ \t]*}|^[ \t]*else|^[ \t]*continue|^[ \t]*elsif/) {
66 if (s/^([ \t]*[A-Za-z_0-9]+:)//) {
72 $line = $label . "do DB($.); " . $_; # all that work for this line
75 $script[$#script - 1] .= ' '; # mark line as having continuation
77 do parse(); # set $state to correct eol value
83 # now put out our debugging subroutines. First the one that's called all over.
87 push(@DB,$. ,$@, $!, $[, $,, $/, $\ );
88 $[ = 0; $, = ""; $/ = "\n"; $\ = "";
90 if ($DBsingle || $DBstop[$DBline] || $DBtrace) {
91 print "$DBline:\t",$DBline[$DBline],"\n";
92 for ($DBi = $DBline; $DBline[$DBi++] =~ / $/; ) {
93 print "$DBi:\t",$DBline[$DBi],"\n";
96 if ($DBaction[$DBline]) {
97 eval $DBaction[$DBline]; print $@;
99 if ($DBstop[$DBline] || $DBsingle) {
103 last if $DBcmd =~ /^$/;
104 if ($DBcmd =~ /^q$/) {
107 if ($DBcmd =~ /^h$/) {
111 <CR> Repeat last s or c.
112 l min-max List lines.
114 l List the whole program.
117 b line Set breakpoint.
118 d line Delete breakpoint.
119 d Delete breakpoint at this line.
120 a line command Set an action for this line.
122 command Execute as a perl statement.
127 if ($DBcmd =~ /^t$/) {
128 $DBtrace = !$DBtrace;
129 print "Trace = $DBtrace\n";
132 if ($DBcmd =~ /^l (.*)[-,](.*)/) {
133 for ($DBi = $1; $DBi <= $2; $DBi++) {
134 print "$DBi:\t", $DBline[$DBi], "\n";
138 if ($DBcmd =~ /^l (.*)/) {
139 print "$1:\t", $DBline[$1], "\n";
142 if ($DBcmd =~ /^l$/) {
143 for ($DBi = 1; $DBi <= $DBmax ; $DBi++) {
144 print "$DBi:\t", $DBline[$DBi], "\n";
148 if ($DBcmd =~ /^L$/) {
149 for ($DBi = 1; $DBi <= $DBmax ; $DBi++) {
150 print "$DBi:\t", $DBline[$DBi], "\n" if $DBstop[$DBi];
154 if ($DBcmd =~ /^b (.*)/) {
156 if ($DBline[$DBi-1] =~ / $/) {
157 print "Line $DBi not breakable.\n";
164 if ($DBcmd =~ /^d (.*)/) {
168 if ($DBcmd =~ /^d$/) {
169 $DBstop[$DBline] = 0;
172 if ($DBcmd =~ /^a ([0-9]+)[ \t]+(.*)/) {
175 $DBaction .= ";" unless $DBaction =~ /[;}]$/;
176 $DBaction[$DBi] = $DBaction;
179 if ($DBcmd =~ /^s$/) {
183 if ($DBcmd =~ /^c$/) {
188 $DBcmd .= ";" unless $DBcmd =~ /[;}]$/;
205 print tmp " \$0 = '$script';\n";
206 print tmp " \$DBmax = $.;\n";
207 print tmp " unlink '/tmp/pdb$$';\n"; # expected to fail on -o.
208 for ($i = 1; $#script >= 0; $i++) {
211 print tmp " \$DBline[$i] = '$_';\n";
218 # prepare to run the new script
221 unshift(@ARGV,$switch) if $switch;
222 unshift(@ARGV,$perl);
225 # This routine tokenizes one perl line good enough to tell what state we are
226 # in by the end of the line, so we can tell if the next line should contain
227 # a call to DB or not.
233 if ($quote == $ord) {
239 $state = 'term' unless $quoting;
243 do quote(ord($1),1), next if s/^m\b(.)//;
244 do quote(ord($1),2), next if s/^s\b(.)//;
245 do quote(ord($1),2), next if s/^y\b(.)//;
246 do quote(ord($1),2), next if s/^tr\b(.)//;
247 do quote($ord,1), next if s/^`//;
248 next if s/^[A-Za-z_][A-Za-z_0-9]*://;
249 $state = 'term', next if s/^eof\b//;
250 $state = 'term', next if s/^shift\b//;
251 $state = 'term', next if s/^split\b//;
252 $state = 'term', next if s/^tell\b//;
253 $state = 'term', next if s/^write\b//;
254 $state = 'operator', next if s/^[A-Za-z_][A-Za-z_0-9]*//;
255 $state = 'operator', next if s/^[~^|]+//;
256 $state = 'statement', next if s/^{//;
257 $state = 'statement', next if s/^}[ \t]*$//;
258 $state = 'statement', next if s/^}[ \t]*#/#/;
259 $state = 'term', next if s/^}//;
260 $state = 'operator', next if s/^\[//;
261 $state = 'term', next if s/^]//;
262 die "Illegal character $_";
265 next if s/[ \t\n\f]+//;
266 die "Illegal character $_";
269 $state = 'statement', next if s/^;//;
270 $state = 'term', next if s/^\.[0-9eE]+//;
271 $state = 'term', next if s/^[0-9][0-9xaAbBcCddeEfF.]*//;
272 $state = 'term', next if s/^\$[A-Za-z_][A-Za-z_0-9]*//;
273 $state = 'term', next if s/^\$.//;
274 $state = 'term', next if s/^@[A-Za-z_][A-Za-z_0-9]*//;
275 $state = 'term', next if s/^@.//;
276 $state = 'term', next if s/^<[A-Za-z_0-9]*>//;
279 $state = 'operator', next if s/^[-(!%&*=+:,.<>]//;
280 $state = 'term', next if s/^\)+//;
281 do quote($ord,1), next if s/^'//;
282 do quote($ord,1), next if s/^"//;
284 if ($state =~ /stat|oper/) {
286 do quote($ord,1), next;
288 $state = 'operator', next;
296 ($quote,$quoting) = @_;