]> Pileus Git - ~andy/gtk/blob - gtk/maketypes.awk
715e6cd95d3930720a3dd63f871e1f935450764c
[~andy/gtk] / gtk / maketypes.awk
1
2 BEGIN {
3   type_name = "";       # GtkEnumType
4   type_macro = "";      # GTK_TYPE_ENUM_TYPE
5   type_ident = "";      # _gtk_enum_type
6   type_counter = 0;
7   gen_macros = 0;
8   gen_entries = 0;
9   gen_vars = 0;
10   boxed_copy = "";
11   boxed_free = "";
12   
13   for (i = 2; i < ARGC; i++)
14     {
15       if (ARGV[i] == "macros")
16         gen_macros = 1;
17       else if (ARGV[i] == "entries")
18         gen_entries = 1;
19       else if (ARGV[i] == "variables")
20         gen_vars = 1;
21       ARGV[i] = "";
22     }
23   
24   if (gen_macros)
25     {
26       printf ("/* type macros, generated by maketypes.awk */\n");
27       printf ("\n");
28       printf ("#ifdef G_OS_WIN32\n");
29       printf ("#  ifdef GTK_COMPILATION\n");
30       printf ("#    define GTKTYPEBUILTINS_VAR __declspec(dllexport)\n");
31       printf ("#  else\n");
32       printf ("#    define GTKTYPEBUILTINS_VAR extern __declspec(dllimport)\n");
33       printf ("#  endif\n");
34       printf ("#else\n");
35       printf ("#  ifdef GTK_COMPILATION\n");
36       printf ("#    define GTKTYPEBUILTINS_VAR\n");
37       printf ("#  else\n");
38       printf ("#    define GTKTYPEBUILTINS_VAR extern\n");
39       printf ("#  endif\n");
40       printf ("#endif\n");
41       printf ("\n");
42     }
43   else if (gen_entries)
44     printf ("/* type entries, generated by maketypes.awk */\n\n");
45   else if (gen_vars)
46     printf ("/* type variables, generated by maketypes.awk */\n\n");
47   else
48     {
49       printf ("hm? what do you want me to do?\n") > "/dev/stderr";
50       exit 1;
51     }
52 }
53
54 function set_type (set_type_1)
55 {
56   type_counter += 1;
57   type_name = set_type_1;
58   type_macro = "GTK_TYPE_";
59   
60   tmp = type_name;
61 # OK, the following is ridiculous, and sed s///g would be far easier
62   gsub ("[A-Z]", "@&", tmp);
63   gsub ("[^A-Z]@", "&_", tmp);
64   gsub ("@", "", tmp);
65   gsub ("[A-Z][A-Z][A-Z][0-9a-z]", "@&", tmp);
66   gsub ("@..", "&_", tmp);
67   gsub ("@", "", tmp);
68   type_macro = type_macro toupper (tmp);
69   type_ident = "_" tolower (tmp);
70
71   sub ("^GTK_TYPE_GTK_", "GTK_TYPE_", type_macro);
72 }
73
74 function generate (generate_what)
75 {
76   if (gen_macros)
77     {
78       printf ("GTKTYPEBUILTINS_VAR GType %s;\n", type_macro);
79     }
80   if (gen_entries)
81     {
82       printf ("  { \"%s\", &%s,\n", type_name, type_macro);
83       if (generate_what == "BOXED")
84         printf ("    GTK_TYPE_%s, %s, %s, },\n", generate_what, boxed_copy, boxed_free);
85       else
86         printf ("    GTK_TYPE_%s, %s_values },\n", generate_what, type_ident);
87     }
88   if (gen_vars)
89     {
90       printf ("GTKTYPEBUILTINS_VAR GType %s = 0;\n", type_macro);
91     }
92 }
93
94 # skip scheme comments
95 ";" {
96   sub (";.*", "");
97 }
98
99 # parse keywords
100
101 /\(define-enum/ {
102   if ($2 == "")
103     printf ("huh? define-enum keyword without arg?\n") > "/dev/stderr";
104   else
105     {
106       set_type($2);
107       generate("ENUM");
108     }
109 }
110
111 /\(define-flags/ {
112   if ($2 == "")
113     printf ("huh? define-flags keyword without arg?\n") > "/dev/stderr";
114   else
115     {
116       set_type($2);
117       generate("FLAGS");
118     }
119 }
120
121 /\(define-boxed/ {
122   if ($2 == "")
123     printf ("huh? define-boxed keyword without arg?\n") > "/dev/stderr";
124   else
125       {
126           boxed_copy = "NULL";
127           boxed_free = "NULL";
128           set_type($2);
129           do {
130               getline;
131               sub (";.*", "", $0);
132           } while ($0 ~ /^[ \t]*$/);
133           tmp_var1 = $1;
134           if ($0 ~ /\)/) { generate("BOXED"); next; }
135           do {
136               getline;
137               sub (";.*", "", $0);
138           } while ($0 ~ /^[ \t]*$/);
139           tmp_var2 = $1;
140           sub (/\).*/, "", tmp_var2);
141           if (tmp_var1 ~ /^[_A-Za-z][_A-Za-z0-9]*$/ &&
142               tmp_var2 ~ /^[_A-Za-z][_A-Za-z0-9]*$/)
143               {
144                   boxed_copy = tmp_var1;
145                   boxed_free = tmp_var2;
146                   # printf ("read boxed funcs: %s %s\n", boxed_copy, boxed_free) > "/dev/stderr";
147               }
148           generate("BOXED");
149       }
150 }
151
152 END {
153   if (gen_macros)
154     printf("\n#define\tGTK_TYPE_N_BUILTINS\t(%u)\n", type_counter);
155 }