Updated 2013-01-18 00:50:29 by pooryorick

ramsan This code tries to convert an old pre-ANSI C file to an ANSI C file. It will try to change things like:
int 
ParseGLFunc (interp, argc, argv, nArg)
    Tcl_Interp *interp;
    int argc;
    char *argv [];
    int *nArg;
{
   ....
}

to something like:
int ParseGLFunc( Tcl_Interp *interp, int argc,char *argv[], int *nArg)

Of course, without guarantees. Please, do correct it if you can improve.

Be aware that to convert from a pre-ANSI to an ANS C interface directly like this means that people with the older compilers will no longer be able to compile the code. That support issue may be intentional - but impact on customers or users should be considered.

This is related to the GNU program protoize [1]
set filein {C:\path\to\file.c.old}
set fileout {C:\path\to\file.c}

set fin [open $filein r]
set contents [read $fin]
close $fin

set funcname {\s*(\w+)\s*}
set varname {\s*(\w+)\s*}
set typename {\s*\w+[\s*&]*}

for { set i 1 } { $i < 10 } { incr i } {
    set rex "$funcname\\("
    for { set j 1 } { $j <= $i } { incr j } {
        if { $j > 1 } { append rex "," }
        append rex $varname
    }
    append rex "\\)"
    for { set j 1 } { $j <= $i } { incr j } {
        append rex "($typename\\[expr {$j+1}]\[]\[\\s]*)\\s*;"
    }
    while 1 {
        set idxs [regexp -inline -indices $rex $contents]
        if { $idxs == "" } { break }
        set contents_new [string range $contents 0 [expr [lindex [lindex $idxs 0] 0]-1]]
        append contents_new " [eval string range [list $contents] [lindex $idxs 1]]("
        for { set j 1 } { $j <= $i } { incr j } {
            set ipos [expr {$j+$i+1}]
            regsub -all {\s+} [eval string range [list $contents] [lindex $idxs $ipos]] { } arg
            if { $j > 1 } { append  contents_new "," }
            append contents_new $arg
        }
        append contents_new ")[string range $contents [expr [lindex [lindex $idxs 0] 1]+1] end]"
        set contents $contents_new
    }
}
set fout [open $fileout w]
puts $fout $contents
close $fout