#!/bin/ksh ## ## SCRIPT NAME: COMPRESS_SCRIPT ## ## Where: in $FEDIR/tkGUIs and/or in $FEDIR/scripts ## ## where FEDIR=/apps/nns_com/fea ## ############################################################################# ## PURPOSE: To 'COMPRESS' Tcl-Tk scripts (or shell scripts). ## ## This script removes essentially all comment lines, blank ('white-space') ## lines, and combines continuation lines onto their parent lines. ## ## A caveat: In some quoted strings of text, if there were leading spaces ## and #'s (or trailing spaces up to a back-slash), ## the intended formatting of the string may be compromised. ## However, this seems to be a rare occurrence. ############################################################################# ## CALLED BY: manually, in a script directory (or wherever) ## with a script filename as input. ############################################################################# ## CALL FORMAT: ./COMPRESS_SCRIPT ## where the filename is of a Tcl-Tk (or Unix shell) script. ## This filename is available as the $1 var in this script. ############################################################################# ## EXAMPLE USE: ## ./COMPRESS_SCRIPT > _cleaned ## chmod 755 _cleaned ## ./_cleaned ## ## May need to provide some inputs for the last execution line. ########################################################################### ## STRUCTURE OF THIS CODE: ## The file ($1) is passed through the following filters: ## ## 0) 'sed' to ## a) replace any tab character by one space, ## AND b) remove blank lines (lines with zero or more spaces), ## ## 1) 'egrep' to remove comment lines --- more specifically: ## a) lines starting with zero or more spaces and '##' ## b) lines starting with zero or more spaces and '# '. ## ## [To handle cases where someone might add comment lines with a ## character starting immediately after a single '#", we would like ## to remove all lines starting with zero or more spaces and '#', ## followed by any character except '!' -- to keep a top line like ## #!/usr/bin/wish. ## ## Unfortunately we were thwarted by running across cases where ## colors were specified by a string like #FF00FF. Some of these ## valid code lines were cast out, resulting in execution-time errors ## like 'missing-closing-brace'. ## ## The above implementation (see statements a & b) seems to be 'safe', ## however, a comment line with text starting immediately after a # ## would slip through and appear in the output of this code compactor. ## ## 2) 'sed' to ## a) replace any string of LEADING spaces ## (that start from the beginning of the line) by one space. ## AND b) replace 'multiple-spaces-and-a-back-slash' at the ## end of a line by 'one-space-and-a-back-slash'. ## ## 3) 'awk' to ## a) print the lines that end in back-slash --- without ## the back-slash and with a space instead of a line-feed. ## AND ## b) print the lines that do NOT end in back-slash as-is ## (with their line-feed). ########################################################################### ## NOTE: You can run this script with itself as input to get the ## 'compressed' version of this script. ############################################################################# ## MAINTENANCE HISTORY: ## Written by: Blaise Montandon O06 16Nov99 From test version in ## /usr/people/bmo01/Dscrtest/test_awk_combine_lines ## Updated by: Blaise Montandon O06 18Nov99 Changed ## egrep -v '^ *##|^ *# ' ## to egrep -v '^ *#[^!].*|^ *#$' ############################################################################# # set -x #### The following egrep was too ambitious. It removed valid code statements #### containing color specifications like #FF00FF. ## egrep -v '^ *#[^!].*|^ *#$' | \ sed 's| | |g;/^ *$/d' $1 | \ egrep -v '^ *##|^ *# ' | \ sed 's|^ *| |;s| *\\$| \\|' | \ awk '{ ############################################################# ## If the rec ends in back-slash, print the record ## w/o back-slash AND w/o line-feed (a space in their place). ############################################################# SAVECHAR = substr($0,length,length) if ( SAVECHAR == "\\" ) { printf ("%s ", substr($0,1,length - 1) ) } else { print $0 } }' ##################################################################### ## We can use 'cat -vt' to show if tabs (or non-printing characters) ## slipped through the above 'sed' and 'awk'. ##################################################################### # }' # }' | cat -vt # set -