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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
#!/usr/bin/env perl
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
###############################################
# Please : do not use this functions yet as they need renaming and the order of the
# parameters are going to chamge. Chema.
###############################################
# Functions for tokenizing files
#
# Copyright (C) 2001 Ximian, Inc.
#
# Authors: Chema Celorio <chema@ximian.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
$SCRIPTSDIR = "@scriptsdir@";
if ($SCRIPTSDIR =~ /^@scriptsdir[@]/)
{
$SCRIPTSDIR = ".";
$DOTIN = ".in";
}
require "$SCRIPTSDIR/file.pl$DOTIN";
# -------------------------------- Test ---------------------------
sub gst_tokenizer_test
{
my ($file) = @_;
my $in, $out;
my $token;
$out = \@dummy_arrayt; # Otherwise push @$out doesn't work
$in = &gst_tokenize ($file);
while (defined ($token = &gst_tokenize_get_token ($in, $out))) {
# Replace a token
if ("subnet" eq $token) {
my $subnet = &gst_tokenize_get_token ($in, $out);
&gst_tokenize_replace_token (out, "123\.123\.123\.123");
}
# Append tokens
if ("option" eq $token) {
&gst_tokenize_append_token ($out, " now");
}
# Remove and skip till
if ("range" eq $token) {
&gst_tokenize_remove_till ($out,";");
&gst_tokenize_skip_till ($in, ";");
}
}
&gst_file_buffer_save ($out, $file . ".new");
printf STDERR "Tested tokenizer with $file. Output in $file.new\n";
printf STDERR "You can :\n";
printf STDERR "diff $file $file.new\n";
}
#&gst_tokenizer_test ("/etc/dhcpd.conf");
# -------------------------------- token functions -------------------
sub gst_tokenize_warning
{
my ($mess) = @_;
printf STDERR "Warning ***: $mess\n";
}
sub gst_tokenize_error
{
my ($mess) = @_;
printf STDERR "Error ***: $mess\n";
exit;
}
sub gst_tokenize_verify_token
{
my ($in, $verifyme, $out) = @_;
my $token = &gst_tokenize_get_token ($in, $out);
if ($token ne $verifyme) {
&gst_tokenize_warning ("Expected \"" . $verifyme . "\" found \"" . $token . "\" at line ?");
}
}
sub gst_tokenize_get_token
{
my ($in, $out) = @_;
my $token = "";
while ($token eq "") {
if (not ($token = shift @$in)) {
return undef;
}
if (defined $out) {
push (@$out, $token);
}
$token =~ s/\s//g;
$token =~ s/\#.*//;
}
return $token;
}
sub gst_tokenize_get_token_unclean
{
my ($in, $out) = @_;
my $token = "";
if (not ($token = shift @$in)) {
return undef;
}
if (defined $out) {
push (@$out, $token);
}
return $token;
}
sub gst_tokenize_get_token_till
{
my ($in, $out, $till_me) = @_;
my $resp;
while (defined ($token = &gst_tokenize_get_token ($in, $out)) and
($token ne $till_me)){
if (defined $resp) {
$resp .= " ";
}
$resp .= $token;
}
return $resp;
}
sub gst_tokenize_replace_token
{
my ($out, $new_token) = @_;
my $token = pop (@$out);
my $clean = $token;
$clean =~ s/\s//g;
$clean =~ s/\#.*//;
$token =~ s/$clean/$new_token/;
push (@$out, $token);
}
sub gst_tokenize_append_token
{
my ($out, $token) = @_;
# Fixme, split tokens while adding them so that
# we maintain consistency
push (@$out, $token);
}
sub gst_tokenize_remove_token
{
my ($out, $number) = @_;
if (not defined ($number)) {
$number = 1;
}
while ($number gt 0) {
pop (@$out);
$number --;
}
}
sub gst_tokenize_skip_token
{
my ($in, $number) = @_;
if (not defined ($number)) {
$number = 1;
}
while ($number gt 0) {
shift (@$in);
$number --;
}
}
sub gst_tokenize_undo
{
my ($in, $out, $number) = @_;
if (not defined ($number)) {
$number = 1;
}
while ($number gt 0) {
@$in = (my $token = pop @$out, @$in);
$token =~ s/\s//g;
$token =~ s/\#.*//;
if ($token ne "") {
$number --;
}
}
}
sub gst_tokenize_advance_till
{
my ($in, $till_me, $out) = @_;
while (defined (my $token = &gst_tokenize_get_token ($in, $out))) {
if ($token eq $till_me) {
return;
}
}
}
sub gst_tokenize_skip_till
{
my ($in, $till_me) = @_;
my $token;
while (1) {
$token = &gst_tokenize_get_token ($in, $out);
&gst_tokenize_remove_token ($out);
if ($token eq $till_me) {
return;
}
}
}
sub gst_tokenize_remove_till
{
my ($out, $till_me) = @_;
my @dont_remove;
while (1) {
my $removed = pop (@$out);
$clean = $removed;
$clean =~ s/\s//g;
$clean =~ s/\#.*//;
if ($clean eq "") {
# Don't remove lines we ignore
@dont_remove = ($removed, @dont_remove);
}
if ($clean eq $till_me) {
push (@$out, $removed);
@$out = (@$out, @dont_remove);
return;
}
}
}
# -------------------------------- tokenizer -------------------
sub gst_tokenize
{
my ($file) = @_;
my $buf = &gst_file_buffer_load ($file);
my @out;
my $out_index;
while (@$buf) {
my $line = $$buf[0];
my $i = 0;
if (&gst_ignore_line ($line)) {
$out[$out_index++] = $line;
shift @$buf;
next;
}
# first skip all the white space/tabs
my $line_copy = $line;
$line_copy =~ s/^(\s+)//;
# save what we removed for later
my $leading = $1;
# Now, handle token delimiters
$line_copy =~ s/\;/ \; /g; # ";" is a token by itself
$line_copy =~ s/,/ , /g; # "," is a token by itself
# Remove leading white space again, this time don't save it
# since we added it ourselves
$line_copy =~ s/^(\s+)//;
# Get the first token in the string by splitting it
my @list = split (' ', $line_copy);
my $token = $list[0];
# Add the leading white space that we removed
$token = $leading . $token;
# Now remove our token from the buffer, to "eat" it from it
$$buf[0] =~ s/$token//;
# Tambet had this code, which looks right but when i run it
# it behaves the same as the one above. It escapes the
# pattern before running the s//
if (0) {
my $pattern = $token;
$pattern =~ s/(\W)/\\$1/g;
$$buf[0] =~ s/$pattern//;
}
# This is a special case, if what follows our token is a newline
# we want it to be part of the token so that if we delete it we will
# also delete the newline. If we don't do this, our files can build up
# a lot of newlines. We can't do this above because split will consume
# newlines.
#
# Use the remainder of the last operation where we remove $token from $buf
$_ = $';
# We have 2 matches posible
# a) whitespace + newline and
# b) ws + comment (with an implicit newline)
if ((/^\s*\n/) || (/^\s*\#/)) {
# If it matches, add it to our token and remove it from $buf
$token = $token . $_ ;
$$buf[0] =~ s/$_//;
}
$out[$out_index++] = $token;
# If we removed all of the current line, shift buffer
# so that in the next iteration $line is not empy
if ($$buf[0] eq "") {
shift @$buf;
next;
}
}
#construc the input file to compare it.
if (0) {
my $res = "";
@copy = @out;
while (@copy) {
$res .= $copy[0];
shift @copy;
}
}
return \@out;
}
1;
|