1# fixtommath.tcl --
2#
3#	Changes to 'tommath.h' to make it conform with Tcl's linking
4#	conventions.
5#
6# Copyright (c) 2005 by Kevin B. Kenny.  All rights reserved.
7#
8# See the file "license.terms" for information on usage and redistribution
9# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10#----------------------------------------------------------------------
11
12set f [open [lindex $argv 0] r]
13set data [read $f]
14close $f
15
16set eat_endif 0
17set eat_semi 0
18set def_count 0
19foreach line [split $data \n] {
20    if { !$eat_semi && !$eat_endif } {
21	switch -regexp -- $line {
22	    {#define BN_H_} {
23		puts $line
24		puts {}
25		puts "\#include <tclTomMathDecls.h>"
26		puts "\#ifndef MODULE_SCOPE"
27		puts "\#define MODULE_SCOPE extern"
28		puts "\#endif"
29	    }
30	    {typedef\s+unsigned long\s+mp_digit;} {
31		# change the second 'typedef unsigned long mp
32		incr def_count
33		puts "\#ifndef MP_DIGIT_DECLARED"
34		if {$def_count == 2} {
35		    puts [string map {long int} $line]
36		} else {
37		    puts $line
38		}
39		puts "\#define MP_DIGIT_DECLARED"
40		puts "\#endif"
41	    }
42	    {typedef.*mp_digit;} {
43		puts "\#ifndef MP_DIGIT_DECLARED"
44		puts $line
45		puts "\#define MP_DIGIT_DECLARED"
46		puts "\#endif"
47	    }
48	    {typedef struct} {
49		puts "\#ifndef MP_INT_DECLARED"
50		puts "\#define MP_INT_DECLARED"
51		puts "typedef struct mp_int mp_int;"
52		puts "\#endif"
53		puts "struct mp_int \{"
54	    }
55	    \}\ mp_int\; {
56		puts "\};"
57	    }
58	    {^(char|int|void)} {
59		puts "/*"
60		puts $line
61		set eat_semi 1
62		set after_semi "*/"
63	    }
64	    {^extern (int|const)} {
65		puts "\#if defined(BUILD_tcl) || !defined(_WIN32)"
66		puts [regsub {^extern} $line "MODULE_SCOPE"]
67		set eat_semi 1
68		set after_semi "\#endif"
69	    }
70	    {define heap macros} {
71		puts $line
72		puts "\#if 0 /* these are macros in tclTomMathDecls.h */"
73		set eat_endif 1
74	    }
75	    {__x86_64__} {
76		puts "[string map {__x86_64__ NEVER} $line]\
77                      /* 128-bit ints fail in too many places */"
78	    }
79	    default {
80		puts $line
81	    }
82	}
83    } else {
84	puts $line
85    }
86    if {$eat_semi} {
87	if {[regexp {; *$} $line]} {
88	    puts $after_semi
89	    set eat_semi 0
90	}
91    }
92    if {$eat_endif} {
93	if {[regexp {^\#endif} $line]} {
94	    puts "\#endif"
95	    set eat_endif 0
96	}
97    }
98}
99