File: scan_script.pl

package info (click to toggle)
lintian 1.23.28%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 2,044 kB
  • ctags: 295
  • sloc: perl: 5,038; makefile: 702; python: 431; sh: 329; ansic: 30; tcl: 4; sed: 1
file content (118 lines) | stat: -rw-r--r-- 2,593 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- perl -*-

# Copyright (C) 1998 Richard Braakman
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

# Functions are defined here to read a shell script and return it as
# a list of tokens.

# We do NOT do history expansion, because it's normally turned off in
# shell scripts.  

# Possible tokens:
# literal:
#  <<- << >> && || <> >| >& ;; (( <& >& ( ) < > ; & | -
#
# end of line: EOL

use strict;

sub scan_script {
    my $tokenval = '';
    my @tokens = ();
    my $state = 0; #base
    my $reserved_ok = 1;
    my $line = 1;

    foreach (split(/\n/, $_[0])) {
	if ($state == 0) {  # base
	    s/^\s+//;               # skip leading whitespace
	    if (m/^\#|^$/) {
		# skip blank lines, skip comments till end of line
		push(@tokens, 'EOL');
		$reserved_ok = 1;
		$line++;
		next;
	    }

	    elsif (s/^( <<- | << | >> | <> | >\| | >& )//x) {
		push(@tokens, $1);
		$reserved_ok = 0;
		redo;
	    }

	    elsif (s/^( && | \|\| )//x) {
		push(@tokens, $1);
		$reserved_ok = 1;
		redo;
	    }

	    elsif (s/^ ;; //x) {
		push(@tokens, ';;');
		$state = 1; # case pattern
		$reserved_ok = 1;
		redo;
	    }

	    elsif ($reserved_ok and s/^ \(\( //x) {
		push(@tokens, '((');
		$state = 2; # dparen arithmetic
		redo;
		# XXX parse_arith_cmd
	    }

	    elsif (s/^( <& | >& )//x) {
		push(@tokens, $1);
		# hack <& - and >& - cases.
		# No comments or newlines can appear between the <& and -.
		if (s/^ \s* -//x) {
		    push(@tokens, '-');
		}
		$reserved_ok = 0; 
		redo;
	    }

	    elsif (m/^( <\( | >\( )/x) {
		$state = 3; # word
		$reserved_ok = 0;
		redo;
	    }

	    elsif (s/^( < | > )//x) {
		push (@tokens, $1);
		$reserved_ok = 0;
		redo;
	    }

	    elsif (s/^([();&|])//) {
		push (@tokens, $1);
		$reserved_ok = 1;
		redo;
	    }
	    
	    else {
		$state = 3; # word
		redo;
	    }
	}

    }

    return @tokens;
}