Module Name:    othersrc
Committed By:   wiz
Date:           Mon Oct  3 13:41:42 UTC 2011

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

Log Message:
David Laight's script to convert K&R functions to ANSI

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.

Added here so it won't get lost.


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

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

Added files:

Index: othersrc/usr.bin/protoz/protoz
diff -u /dev/null othersrc/usr.bin/protoz/protoz:1.1
--- /dev/null	Mon Oct  3 13:41:42 2011
+++ othersrc/usr.bin/protoz/protoz	Mon Oct  3 13:41:41 2011
@@ -0,0 +1,142 @@
+#! /bin/sh
+#	$NetBSD: protoz,v 1.1 2011/10/03 13:41:41 wiz Exp $
+#
+# Copyright (c) 2009 The NetBSD Foundation, Inc.
+# All rights reserved.
+#
+# This code is derived from software contributed to The NetBSD Foundation
+# by David Laight.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+# Script to convert K&R functions to ANSI
+#
+# 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"' ,]*)$/{
+	h
+    : a
+	# Remove an argument
+	s/('"$as"', */(/
+	t a
+	# Check we removed all args
+	s/^'"$as *($as"')$//
+	t b
+	# Nope - recover original text
+	g
+	b
+    : b
+	# Get next argument type
+	# On EOF data buffer is written to stdout
+	# so it better hold the partial data
+	g
+	N
+	# Save unmodified input in hold space
+	h
+	s/.*\n\(.*\)$/\1/
+	t b1
+    : b1
+	# Check has expected format, we do not support "int (*a)();"
+	s/^[][ 	'"$a"'*,]*;$//
+	t b
+	# Same with a comment
+	s/^[][ 	'"$a"'*,]*;[ 	]*\/\*.*\*\/$//
+	t b
+	# Function pointer
+	s/^['"$a"' 	*]*(\*'"$as"')([][  '"$a"'*,]*);$//
+	t b
+	# Line after args must be just "{"
+	s/^{$//
+	g
+	# Recover original text
+	t c
+	# Not "{" so leave unchanged
+	b
+    : c
+	# At this point we have the whole function defn in the buffer
+	# Remove any comments
+	s/\/\*[^\n]*\*\///g
+	# Replace blocks of whitespace with a single space
+	s/[ 	][ 	]*/ /g
+	s/[ 	]*\(\n\)[ 	]*/\1/g
+	s/[ 	];/;/g
+	# Replace the (,) in function pointer defs with other sequences
+    : c1
+	s/\(\n['"$a"' 	*]*\)(\(\*'"$as"'\))(\([][  '"$a"'*,]*\))\(;\n\)/\1{\2}{\3}\4/
+	t c1
+    : c2
+	# Insert type of arg before its name from: "type arg;\n"
+	s/\([(,] *\)\('"$as"'\)\([,)].*\n\)\([ 	'"$a"'*]*[ *]\)*\2\([][]*\);\n/\1\4\2\5\3/
+	t c2
+	# And from: "type arg,"
+	s/\([(,] *\)\('"$as"'\)\([,)].*\n\)\([ 	'"$a"'*]* \)\([*]*\)\2\([][]*\),[ 	]*/\1\4\5\2\6\3\4/
+	t c2
+	# And for function pointers
+	s/\([(,] *\)\('"$as"'\)\([,)].*\n\)\([^{]*{\*\2}{[^}]*}\);\n/\1\4\3/
+	# s/\([(,] *\)\('"$as"'\)\([,)].*\n\)\([ 	'"$a"'*]*[ *]*{\**\2}{[][ 	'"$a"',]*}\);/\1\4\3/g
+	t c2
+	# Convert function pointer args back to C
+	s/{\(\*'"$as"'\)}{\([^}]*\)}/(\1)(\2)/g
+	# Verify we have nothing left over.
+	s/^'"$as"' *([]['"$a"' ,*]*)\n{$/&/
+	t d
+	g
+	t
+    : d
+	# Recover any comments deleted above by processing the original text
+	s/\n{$//
+	x
+	# Remove args without comments, then the body of the first line
+    : d1
+	s/\n[^\n]*;\(\n\)/\1/
+	t d1
+	s/^[^\n]*)\(\n\)/\1/
+	# Replace the ; with a : to separate the name from comment
+	s/;/:/g
+	# Move the comment marker to before the type
+	s/\(\n\)[ 	]*\([^\n]*\)\/\*/\1	\/\*\2/g
+	# Remove the type
+	s/\/\*[^ ][^,:\n]*[ 	*]\([]['"$a"']*[,:]\)/\/\* \1/g
+	# Remove the initial blank line
+	s/^\n//
+	# Append to the prototype generated earlier
+	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

Reply via email to