#!/bin/sh # # This script is similar to uncomment. The difference between the two of them # is that this script reads the entire C source file into memory in order to # remove the c style comments, whereas uncomment reads the file a line at a time # only combines lines in the minimal amount necessary to find the end of comment, # before removing it. # # This script hasn't been tested as much for f in "$@" do sed -n -e ' # # On all lines of the file, eliminate comments that begin and end # on one line. # s%//.*%%1; # cplusplus comments s%/\*.*\*/%%g; # old-style c comments # # concatenate all lines of the file into the hold buffer. # 1h; 1!H; # # On the last line of the file, which by the way has already # been appended to the hold buffer, do the following # ${ # # swap the last line of the file with the hold buffer # (which includes a copy of the last line already) # g; # # across the entire file, remove comments that span # multiple lines (they are the only ones left. # s%/\*.*\*/%%g; # # Remove trailing blanks at the ends of lines # s/ \+\n/\n/g; # # Print the entire file (after substitutions above) # p; } ' "$f" done