Module Name:    othersrc
Committed By:   apb
Date:           Mon Feb 13 15:04:49 UTC 2012

Modified Files:
        othersrc/usr.bin/protoz: protoz

Log Message:
Move the sed script into a shell function, and add command line
argument processing code to filter stdin to stdout when invoked
without arguments, to edit all files specified on the command line,
and to recursively look for *.c files to process when a directory
is specified on the command line.  Previously, only one command line
argument was accepted, and it was expected to be a directory.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 othersrc/usr.bin/protoz/protoz

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: othersrc/usr.bin/protoz/protoz
diff -u othersrc/usr.bin/protoz/protoz:1.1 othersrc/usr.bin/protoz/protoz:1.2
--- othersrc/usr.bin/protoz/protoz:1.1	Mon Oct  3 13:41:41 2011
+++ othersrc/usr.bin/protoz/protoz	Mon Feb 13 15:04:49 2012
@@ -1,7 +1,7 @@
 #! /bin/sh
-#	$NetBSD: protoz,v 1.1 2011/10/03 13:41:41 wiz Exp $
+#	$NetBSD: protoz,v 1.2 2012/02/13 15:04:49 apb Exp $
 #
-# Copyright (c) 2009 The NetBSD Foundation, Inc.
+# Copyright (c) 2009,2012 The NetBSD Foundation, Inc.
 # All rights reserved.
 #
 # This code is derived from software contributed to The NetBSD Foundation
@@ -31,19 +31,27 @@
 
 # Script to convert K&R functions to ANSI
 #
+# When run without args, it filters stdin to stdout.
+#
+# When run with args, it expects them to be file or directory names.
+# Each file is edited regardless of its name.  Each directory
+# is searched for *.c files that could benefit from edited.
+#
 # BUGS:
 #	Dpesn't handle functions with no arguments
 #	Doesn't handle va_list args
 #	Doesn't like blank lines or extra line breaks
 #	Generated ANSI definitions are often longer than 80 cols.
 
-dir=${1:-.}
-
 a='a-zA-Z0-9_'
 as='['"a-zA-Z_"']['"$a"']*'
 as_v='['"a-uw-zA-Z_"']['"$a"']*'
-grep -rl --include '*.c' "^$as *($as\( *, *$as\)*)$" $dir | while read f; do
-sed -e '/^'"$as"' *(['"$a"' ,]*)$/{
+
+# process_stream -- copy stdin to stdout while editing it as appropriate
+#
+process_stream()
+{
+    sed -e '/^'"$as"' *(['"$a"' ,]*)$/{
 	h
     : a
 	# Remove an argument
@@ -133,10 +141,32 @@ sed -e '/^'"$as"' *(['"$a"' ,]*)$/{
 	H
 	# get prototype
 	g
-}' $f >$f.new
-res=$?
-[ $res -ne 0 ] && { echo "sed failed $res"; rm $f.new; continue; }
-cmp -s $f $f.new && { rm $f.new; continue; }
-echo "updated $f"
-mv $f.new $f
-done
+    }'
+}
+
+if [ $# = 0 ]; then
+    # filter stdin to stdout
+    process_stream
+else
+    # args may be file or directory names.
+    # For plain files, just process them.
+    # For directories, recursively look for *.c files that
+    # may benefit from editing.
+    for arg in "$@" ; do
+	if [ -f "$arg" ]; then
+	    echo "$arg"
+	elif [ -d "$arg" ]; then
+	    grep -rl --include '*.c' "^$as *($as\( *, *$as\)*)$" "$arg"
+	else
+	    echo >&2 "$0: $arg is not a file or directory"
+	fi
+    done \
+    | while read f; do
+	process_stream <$f >$f.new
+	res=$?
+	[ $res -ne 0 ] && { echo "sed failed $res"; rm $f.new; continue; }
+	cmp -s $f $f.new && { rm $f.new; continue; }
+	echo >&2 "updated $f"
+	mv $f.new $f
+    done
+fi

Reply via email to