#!/bin/sed -nf # # This sed script prints the files specified on the command line but removes # the c-style comments and pre-processor directives from them. Note that it # does not evaluate the precprocessor directives. # # Note also that the text line number may be off a little under certain # circumstances -- C style comments that span the end of the line cause # the text from the earlier line to printed on the later line. # y/\f/ /; # eliminate form feed characters by replacing them # with space. This is done because form feeds are # going to be used a delimiters in some regex replaces below. :top # loop to process groups of characters on the current # line (and possibly subsequent lines in the event # of /* comments s/^\(\([^"/]\|'"'\|'\\"'\)\+\)/\1\f/1 # insert a form feed before the first interesting # character. Quoted strings and comments are # intesting characters T handleInterestingText # if there wasn't at least 1 character before # the first comment or quoted string, jump # to the section that handles quotes and comments H # append pattern to hold x # swap pattern and hold s/\f.*$//1 # remove text starting with the separator character x # put the hold buffer back in its place s/^[^\f]*\f//1 # remove the boring text from the current line :handleInterestingText # # at this point, we have either a quoted string in column 1 or # or //.* # or /.* # or /*.* # /^'/{ # the only single character strings of interest are '"' and '\"' H x s/\n^'\(.*'\)[^\n]*$/\n'\1/1 x s/^.//1 b top } /^"/{ # append quoted strings to the end of the hold buffer H # append the while line # then # remove all but the leading quoted string from the # last line in the hold bufffer x s/\n"\(\(\\"\|[^"]\)*\)".*$/"\1"/1 # get the original line back from the hold buffer # and remove the leading quoted string x s/^"\(\(\\"\|[^"]\)*\)"//1 /^"/ { s/"/ /1 # if we get here, we have mismatched quotes } b top } /^\/\// { s%//.*%%1 # remove //.* comments b done } /^\/\*/{ # # handle c style comments: eat tokens # until we find */ -- taking care not # interpret the contents of quoted strings # s/^..//1 :eat1 /\*\// { s%^.*\(\*\/\)%%1 b top } n b eat1 } /^\//{ H x s%\n/.*$%\n/%1 x s/^.//1 b top } :done H # append pattern to hold x # move hold to pattern s/\n//g # replace separators with nothing p