如何自动将K&R函数声明转换为ANSI函数声明?

// K&R syntax int foo(a, p) int a; char *p; { return 0; } // ANSI syntax int foo(int a, char *p) { return 0; } 

如您所见,在K&R样式中,变量的类型在新行中而不是在大括号中声明。 如何自动将K&R函数声明转换为ANSI函数声明? 在Linux中有人知道这么容易使用的工具吗?

您可以使用cproto或protoize(GCC的一部分)生成函数原型或将旧样式(K&R)函数转换为ANSI格式。

既然你想转换多行字符串,你可以考虑perl

你有

 void old_style( c , a ) char c; int a; { /* some multiline code */ } 

并且必须有

 void old_style( char c, int a) {} 

所以

 perl -i.bkp -nle 's/\((void|int|char|float|long) [a-zA-Z0-9_-]*\)([a-zA-Z0-9_-] ?,[a-zA-Z0-9_-] ?)\(.*{\)/\1(\2)/g' 

或类似的东西,会做的伎俩。

如果你试一试并在评论中输出,那么解决这个正确的正则表达式会更容易

 diff file.c file.c.bkp 

对于每个源文件。

1)如果要为.h文件创建标准C原型,请使用mkproto.c

mkproto thisoldfile.c> thisoldfile.h

然后,您可以根据需要粘贴C文件定义中的旧K&R代码。