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# RCS: @(#) $Id: fix_tommath_h.tcl,v 1.6 2007/02/14 17:59:22 kennykb Exp $
12#
13#----------------------------------------------------------------------
14
15set f [open [lindex $argv 0] r]
16set data [read $f]
17close $f
18
19set eat_endif 0
20set eat_semi 0
21set def_count 0
22foreach line [split $data \n] {
23    if { !$eat_semi && !$eat_endif } {
24	switch -regexp -- $line {
25	    {#define BN_H_} {
26		puts $line
27		puts {}
28		puts "\#include <tclTomMathDecls.h>"
29		puts "\#ifndef MODULE_SCOPE"
30		puts "\#define MODULE_SCOPE extern"
31		puts "\#endif"
32	    }
33	    {typedef\s+unsigned long\s+mp_digit;} {
34		# change the second 'typedef unsigned long mp
35		incr def_count
36		puts "\#ifndef MP_DIGIT_DECLARED"
37		if {$def_count == 2} {
38		    puts [string map {long int} $line]
39		} else {
40		    puts $line
41		}
42		puts "\#define MP_DIGIT_DECLARED"
43		puts "\#endif"
44	    }
45	    {typedef.*mp_digit;} {
46		puts "\#ifndef MP_DIGIT_DECLARED"
47		puts $line
48		puts "\#define MP_DIGIT_DECLARED"
49		puts "\#endif"
50	    }
51	    {typedef struct} {
52		puts "\#ifndef MP_INT_DECLARED"
53		puts "\#define MP_INT_DECLARED"
54		puts "typedef struct mp_int mp_int;"
55		puts "\#endif"
56		puts "struct mp_int \{"
57	    }
58	    \}\ mp_int\; {
59		puts "\};"
60	    }
61	    {^(char|int|void)} {
62		puts "/*"
63		puts $line
64		set eat_semi 1
65		set after_semi "*/"
66	    }
67	    {^extern (int|const)} {
68		puts "\#if defined(BUILD_tcl) || !defined(_WIN32)"
69		puts [regsub {^extern} $line "MODULE_SCOPE"]
70		set eat_semi 1
71		set after_semi "\#endif"
72	    }
73	    {define heap macros} {
74		puts $line
75		puts "\#if 0 /* these are macros in tclTomMathDecls.h */"
76		set eat_endif 1
77	    }
78	    {__x86_64__} {
79		puts "[string map {__x86_64__ NEVER} $line]\
80                      /* 128-bit ints fail in too many places */"
81	    }
82	    default {
83		puts $line
84	    }
85	}
86    } else {
87	puts $line
88    }
89    if {$eat_semi} {
90	if {[regexp {; *$} $line]} {
91	    puts $after_semi
92	    set eat_semi 0
93	}
94    }
95    if {$eat_endif} {
96	if {[regexp {^\#endif} $line]} {
97	    puts "\#endif"
98	    set eat_endif 0
99	}
100    }
101}
102