# HG changeset patch # User Francisco J Ballesteros # Date 1317223237 0 # Node ID f9712ae5de2f9a9f7e6656fcd547a416aa617553 # Parent c1fa71ee7a7c5015076017967ad71d693e4e8d30 tinytools: some C helper scripts. they rely on a silly serial-sam, so I had to include that as well. R=nix-dev, rminnich CC=nix-dev http://codereview.appspot.com/5139051 diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/argsused --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/argsused Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,17 @@ +#!/bin/rc + +cat $* | ssam ', x/\n[a-zA-Z0-9_]+\(.*\)\n{\n}\n/ x/\(.*\)/ { + t +1 + +1 a/\n/ + } + , x/\n\(.*\)\n/ { + x/[a-zA-Z0-9_]+[,)]/ { + i/USED(/ + x/,/c/)/ + } + } + , x/\n{\n\(.*\)\n/ x/\(.*/ s/\)/);/g + , x/\n{\n\(.*/ x/\(.*/ y/USED\([a-zA-Z0-9_]*\);/c/\n / + , x/\n{\n\n/c/\n{\n/ + , x/\n \n}\n/c/\n}\n/ + ,p' diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/badexterns --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/badexterns Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,21 @@ +#!/bin/rc +# +# check out externs used just in one place; and warn. +# +rfork e + +if(~ $#* 0 1){ + echo usage: $0 files >[1=2] + exit usage +} + +nms=`{ {for(o in $*){ nm $o | grep ' [TDB] ' }} | awk '{print $NF}' | sort -u} + +nm $* >/tmp/nmout +for(n in $nms){ + if(~ `{grep ' '^$n^'$' /tmp/nmout | wc -l} 1) + grep ' '^$n^'$' /tmp/nmout +} +rm /tmp/nmout +exit '' + diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/badglobals --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/badglobals Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,22 @@ +#!/bin/rc +# +# check out that we seem to use our globals +# +rfork e + +NOT READY + +if(~ $#* 0 ){ + echo usage: $0 files >[1=2] + exit usage +} + +for(o in $*){ + nms=`{nm $o | grep ' [TDBtdb] ' | awk '{print $NF}'} + for(n in $nms){ + c=`{echo $o | sed 's/\.[0-9]$/.c/'} + grep '(^'^$n^[^a-z0-9A-Z_]')|(^[^a-z0-9A-Z_]'^$n^'[^a-z0-9A-Z_])' $c + } +} +exit '' + diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/enum2sw --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/enum2sw Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,6 @@ +#!/bin/rc + +sed -e 's/^ / case /' \ + -e 's/[ ]=.*,/,/' \ + -e 's/,/:/' \ + -e 's/$/\n break;/' diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/f2p --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/f2p Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,6 @@ +#!/bin/rc + +cat $* | ssam ', y/.+\n[π∏∆∂A-Za-z0-9_]+\(.*\n/c// ' \ + ', s/\n([π∏∆∂A-Za-z0-9_]+\()/ \1/g' ', p' | \ +grep -v '^static' | grep -v threadmain | sed -e 's/$/;/' -e 's/^/extern /' | sort +2 +exit '' diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/ifs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/ifs Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,4 @@ +#!/bin/rc +cat $* | sed -e 's/(if|for|while) \(/\1(/' -e 's/\) {/){/' -e 's/} else/}else/' -e 's/else {/else{/' \ + -e 's/enum ?{/enum\n{/' \ + -e 's/^struct ?([^{]+){/struct\n\1 {/' diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/mkdeps --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/mkdeps Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,34 @@ +#!/bin/rc +# +# generate list of HFILES and dependencies for each .$O +# +rfork e + +if(~ $#* 0 1){ + echo usage: $0 files >[1=2] + exit usage +} + +cs=$* +hdrs=`{grep -h '^#include[ ]*"' $cs | sed 's/#include[ ]*"([^"]+)".*/\1/' | sort -u} + +echo 'HFILES=\' + +all=() +for(h in $hdrs){ + nh=`{grep '^#include[ ]*"'^$h^'"' $cs | wc -l} + if(~ $nh $#cs){ + echo ' '^$h^'\' + all=($all $h) + } +} + +for(c in $cs){ + hs=`{grep -h '^#include[ ]*"' $c | sed 's/#include[ ]*"([^"]+)".*/\1/' } + echo -n `{echo $c | sed 's/\.c$/.$O:/'} + for(h in $hs) + if(! ~ $h $all) + echo -n ' '^$h + echo +} +exit '' diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/mktypedefs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/mktypedefs Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,7 @@ +#!/bin/rc + +grep '^struct[ ]+' $* | + sed -e 's/struct[ ]+([∏πa-zA-Z0-9_]+).*/typedef struct \1 \1;/' | + sort -u + + diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/p2f --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/p2f Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,2 @@ +#!/bin/rc +cat $* | sed 's/[a-z_0-9∏π∂∆]+\(/\n&/' | sed 's/;$/\n{\n}\n/' diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/psort --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/psort Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,3 @@ +#!/bin/rc + +sort -f +1 -2 diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/c/rep --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/c/rep Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,12 @@ +#!/bin/rc +rfork e +tmp=/tmp/rep.$pid +if(~ $#* 0){ + echo usage $0 from to.... + exit usage +} +orig=$1 +shift +cat >$tmp ; for(f in $*) { cat $tmp | sed s/$orig/$"f/g} +rm -f $tmp +exit '' diff -r c1fa71ee7a7c -r f9712ae5de2f rc/bin/ssam --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc/bin/ssam Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,12 @@ +#!/bin/rc +t=/tmp/ssam.$pid + +fn exit () { + rm -f $t +} +cat > $t +{ + for (c in $*) { echo $c } + echo q +} | sam -d $t >[2] /dev/null +rm -f $t diff -r c1fa71ee7a7c -r f9712ae5de2f sys/man/1/f2p --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sys/man/1/f2p Wed Sep 28 15:20:37 2011 +0000 @@ -0,0 +1,114 @@ +.TH F2P 1 +.SH NAME +argsused, badexterns, badglobals, enum2sw, f2p, ifs, mkdeps, mktypedefs, p2f, psort, rep \- tiny C tools +.SH SYNOPSIS +.B c/argsused +[ +.I file ... +] +.PP +.B c/badexterns +.I files ... +.PP +.B c/badglobals +.I files ... +.PP +.B c/enum2sw +.PP +.B c/f2p +[ +.I file ... +] +.PP +.B c/ifs +[ +.I file ... +] +.PP +.B c/mkdeps +[ +.I file ... +] +.PP +.B c/mktypedefs +[ +.I file ... +] +.PP +.B c/p2f +[ +.I file ... +] +.PP +.B c/psort +.PP +.B c/rep +.I from +.I to... +.SH DESCRIPTION +These are some tools to help write C programs in Plan 9. They are all installed +under +.BR /bin/c . +Many of them use standard input if no file is given as an argument, and are suitable +for use within +.IR acme (1). +.TF c/mktypedefs +.TP +.B c/argsused +generates a +.I USED(x) +sentence for each argument in the functions found in the input files or standard input. +.TP +.B c/badexterns +receives a series of object files as arguments and locates external symbols +used in just one file. Those should perhaps be declared as static symbols. +.TP +.B c/badglobals +receives a series of object tiles as arguments and locates global symbols that +appear to be unused. They could probably go. +.TP +.B c/enum2sw +receives as input a declaration of values (e.g., part of declarations within a +.IR enum ) +and generates a +.I switch +statement for them. +.TP +.B c/f2p +generates a prototype declaration for each non-static function found in the input. +.TP +.B c/p2f +does the opposite, and writes an empty function definition for each prototype found in the input. +.TP +.B c/ifs +gets rid of spaces that should not be there according to the Plan 9 C style. +.TP +.B c/mkdeps +generates dependencies for +.I mk (1) +according to +.I include +directives found in the input. +.TP +.B c/mktypedefs +generates +.I typedef +declarations for each +.I struct +definition found in the input. +.TP +.B c/psort +sorts prototypes by name. +.TP +.B c/rep +repeats the text found in the input so that +.I from +is replaced each time by one of the values given as +.I to . +.SH SOURCE +.B /rc/bin/c +.SH BUGS +Many. All these tools are tiny shell scripts and do not perform much error +checking. Also, they assume code is written using the style of +Plan 9 from Bell Labs source code. +