Minimalna implementacija CPPa

Za predprocesiranje ~/.ssh/config fajla koristim CPP (C Preprocessor). Ali na chromebooku nemam instaliran GCC (pa ni CPP), pa sam napisao minimalnu verziju CPPa u perlu.

#!/usr/bin/perl -w
 
use strict;
use Data::Dumper;
 
my $vars = {};
my $state = [];
 
while (<>) {
        if (/^#define\s*([A-Za-z0-9_]*)/) {
                $vars->{$1} = 1;
                next;
        }
        if (/^#ifdef\s*([A-Za-z0-9_]*)/) {
                push(@$state, $1);
                next;
        }
        if (/^#ifndef\s*([A-Za-z0-9_]*)/) {
                push(@$state, '!'.$1);
                next;
        }
        if (/^#else/) {
                my $last = pop(@$state);
                if ($last =~ /^!(.*)$/) { $last = $1; }
                else { $last = '!' . $last; }
                push(@$state, $last);
                next;
        }
        if (/^#endif/) {
                pop(@$state);
                next;
        }
 
        my $cond = 1;
        foreach my $c (@$state) {
                if ($c =~ /^!(.*)$/) {
                        if (defined($vars->{$1})) {
                                $cond = 0;
                                last;
                        }
                } elsif ($c =~ /^(.*)$/) {
                        if (!defined($vars->{$1})) {
                                $cond = 0;
                                last;
                        }
                }
        }
        if ($cond) {
                s,//.*$,,;
                print $_;
        }
}

download pcpp.pl

This entry was posted in admin, chromebook, linux, scripts and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.