sanitize.pl revision 761a1c3761dc4e83a5d11765a42fab2970c5352b
9e6de65c57eddc3790badaad3b9481aaaed18e03Brian Wellington#!/usr/bin/perl
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence#
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# Copyright (C) 1999, 2000 Internet Software Consortium.
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence#
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# Permission to use, copy, modify, and distribute this software for any
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# purpose with or without fee is hereby granted, provided that the above
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# copyright notice and this permission notice appear in all copies.
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence#
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# SOFTWARE.
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
9e6de65c57eddc3790badaad3b9481aaaed18e03Brian Wellington# $Id: sanitize.pl,v 1.1 2000/07/26 18:47:43 mws Exp $
d09197467bbb156dccf0cbe72bb5c63480d5cfdcDavid Lawrence
d09197467bbb156dccf0cbe72bb5c63480d5cfdcDavid Lawrence# Don't try and sanitize this file: NOMINUM_IGNORE
d09197467bbb156dccf0cbe72bb5c63480d5cfdcDavid Lawrence
64ba6e4cc3a0ccf8c8c6349fa75b937ca9bad9a6Michael Graff# Go through the directory tree and make sure that all of the files are
64ba6e4cc3a0ccf8c8c6349fa75b937ca9bad9a6Michael Graff# sanitized.
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence#
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# In normal mode, check file, removing code between
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# #ifndef NOMINUM_PUBLIC
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# and the accompanying #else or #endif. Similarly, code in an #else
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# clause after an #ifndef test will be removed. The #else or #endif's
94231e22882a98a3805060b0a1322e96d8f57b41David Lawrence# must appear as:
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# #else /* NOMINUM_PUBLIC */
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# #endif /* NOMINUM_PUBLIC */
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence# Balance is tested.
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# Non-.c/.h files are tested for the existance of NOMINUM_PUBLIC anywhere
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence# in the file, and a warning is generated, unless the string
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence# NOMINUM_IGNORE appears before NOMINUM_PUBLIC.
194f54f4da604a6601edefa2175d48412e01bf2fDavid Lawrence
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# Usage:
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# ./sanitize.pl -c - Check syntax only, don't change anything
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# ./sanitize.pl -i - Reverse sense of sanitize.
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence# ./sanitize.pl - - Work as a pipe, sanitizing stdin to stdout.
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence# ./sanitize.pl file - Sanitize the specified file.
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence$makechange = 1;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence$state = 0;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence$showon = 1;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence$debug = 0;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence
c4c843edb3f4c609e3552ee77a43400852400467David Lawrence# States:
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley# 0 - Outside of test, include code
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley# 1 - Inside NOMINUM_PUBLIC
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence# 2 - Inside !NOMINUM_PUBLIC
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrenceforeach $arg (@ARGV) {
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence $_ = $arg;
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence if (/^-c$/i) {
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence $makechange = 0;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence }
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence elsif (/^-i$/i) {
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence $showon = 2;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence }
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence elsif (/^-$/i) {
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence &runfile("-","-");
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence }
da1eb108c623ef351c4275251e8a906138da85b9David Lawrence# elsif (/^-a$/i) {
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence# &rundir();
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence# }
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence elsif (/^-d$/i) {
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence $debug = 1;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence }
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence else {
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence &runfile($arg, $arg.".sanitize");
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence }
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence}
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrenceexit(0);
593d951248f799e90d30bc69bb89342000b012cdBob Halley
d65db52903505c1bcbc6dd1651e9f9f347e48217David Lawrence
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrencesub runfile($) {
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence $state = 0;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence open(INFILE, $_[0]) || die ("$_[0]");
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence open(OUTFILE, ">$_[1]") || die ("$_[1]")
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence if ($makechange);
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence while (<INFILE>) {
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence if (/NOMINUM_IGNORE/) {
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence close(INFILE);
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence close(OUTFILE);
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence unlink($_[1]);
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence break;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence }
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence if (/\#ifdef.+NOMINUM_PUBLIC/) {
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence if ($state != 0) {
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence print(STDERR "*** ERROR in file $_[0]: ".
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence "Found #ifdef without matching ".
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence "#endif.\n");
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence close(INFILE);
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence close(OUTFILE) if ($makechange);
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence unlink($_[1]);
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence break;
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence }
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence $state = 1;
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence }
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence elsif (/\#ifndef.+NOMINUM_PUBLIC/) {
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence if ($state != 0) {
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence print(STDERR "*** ERROR in file $_[0]: ".
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence "Found #ifndef without matching ".
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence "#endif.\n");
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence close(INFILE);
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence close(OUTFILE) if ($makechange);
ec80744ad68b97f15657b1fdf5591c30b559b57dDavid Lawrence unlink($_[1]);
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence break;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence }
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley $state = 2;
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley }
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley elsif (/\#else.+NOMINUM_PUBLIC/) {
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley if ($state == 0) {
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley print(STDERR "*** ERROR in file $_[0]: ".
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley "Found #else without matching ".
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley "#if[n]def.\n");
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley close(INFILE);
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley close(OUTFILE) if ($makechange);
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence unlink($_[1]);
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence break;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence }
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence if ($state == 1) {
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence $state = 2;
f036af2c718147408d738081cdb0a564b981b4cdDavid Lawrence } else {
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley $state = 1;
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley }
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley }
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley elsif (/\#endif.+NOMINUM_PUBLIC/) {
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley if ($state == 0) {
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley print(STDERR "*** ERROR in file $_[0]: ".
1630fce031f7a3e33f0579e477a3e17d1993e1f9Bob Halley "Found #else without matching ".
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence "#if[n]def.\n");
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence close(INFILE);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence close(OUTFILE) if ($makechange);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence unlink($_[1]);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence break;
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence }
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence $state = 0;
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence }
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence elsif (/NOMINUM_PUBLIC/) {
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence print(STDERR "*** WARNING in file $_[0]: ".
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence "Found NOMINUM_PUBLIC outside of ".
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence "#ifdef/#else/#endif.\n");
33950f0a0262f4d49528c4adcf8be42807fa2576David Lawrence }
84f8cc7e22cccde283627f6df6b8b74ec473cdc9David Lawrence else {
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence if (($state == 0) || ($state == $showon)) {
bd1190c84b08e61a12789c54f083318c36449e5eDavid Lawrence print(OUTFILE) if ($makechange);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence }
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence }
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence }
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence if ($state != 0) {
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence print(STDERR "*** ERROR in file $_[0]: ".
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence "File ended with unterminated test.\n");
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence } else {
5f50687f61057f7f0acb161d5803f4a48e40b3a8David Lawrence close(INFILE);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence close(OUTFILE) if ($makechange);
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence if (($_[0] ne "-") && ($makechange)) {
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence unlink($_[0]) || die "Unlink $_[0]:";
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence rename($_[1], $_[0]) || die "Rename $_[1] to $_[0]:";
73d62a89f1493865c33c689b3ee3de91c74ad58eDavid Lawrence }
f389bc2c9e9e434380e10221778b7b548612a67fDavid Lawrence }
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence}
0f5962ac3e4ef336faff68f1cb838505e64665e5David Lawrence