1#  Copyright (C) 2003,2004 Free Software Foundation, Inc.
2#  Contributed by Kelley Cook, June 2004.
3#  Original code from Neil Booth, May 2003.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the
7# Free Software Foundation; either version 2, or (at your option) any
8# later version.
9# 
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14# 
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19# This Awk script reads in the option records generated from 
20# opt-gather.awk, combines the flags of duplicat options and generates a
21# C file.
22#
23# This program uses functions from opt-functions.awk
24#
25# Usage: awk -f opt-functions.awk -f optc-gen.awk \
26#            [-v header_name=header.h] < inputfile > options.c
27
28BEGIN {
29	n_opts = 0
30	n_langs = 0
31        quote = "\042"
32	comma = ","
33	FS=SUBSEP
34	# Default the name of header created from opth-gen.awk to options.h
35	if (header_name == "") header_name="options.h"
36}
37
38# Collect the text and flags of each option into an array
39	{
40		if ($1 == "Language") {
41			langs[n_langs] = $2
42			n_langs++;
43		}
44		else {
45			name = opt_args("Mask", $1)
46			if (name == "") {
47				opts[n_opts]  = $1
48				flags[n_opts] = $2
49				help[n_opts]  = $3
50				n_opts++;
51			}
52		}
53	}
54
55# Dump that array of options into a C file.
56END {
57print "/* This file is auto-generated by opts.sh.  */"
58print ""
59n_headers = split(header_name, headers, " ")
60for (i = 1; i <= n_headers; i++)
61	print "#include " quote headers[i] quote
62print "#include " quote "opts.h" quote
63print "#include " quote "intl.h" quote
64print ""
65
66for (i = 0; i < n_opts; i++) {
67	name = var_name(flags[i]);
68	if (name == "")
69		continue;
70
71	if (flag_set_p("VarExists", flags[i]))
72		continue;
73
74	init = opt_args("Init", flags[i])
75	if (init != "")
76		init = " = " init;
77	else if (name in var_seen)
78		continue;
79
80	print "/* Set by -" opts[i] "."
81	print "   " help[i] "  */"
82	print var_type(flags[i]) name init ";"
83	print ""
84
85	var_seen[name] = 1;
86}
87
88print ""
89print "/* Local state variables.  */"
90for (i = 0; i < n_opts; i++) {
91	name = static_var(opts[i], flags[i]);
92	if (name != "")
93		print "static " var_type(flags[i]) name ";"
94}
95print ""
96
97print "const char * const lang_names[] =\n{"
98for (i = 0; i < n_langs; i++) {
99	macros[i] = "CL_" langs[i]
100	gsub( "[^A-Za-z0-9_]", "X", macros[i] )
101	s = substr("         ", length (macros[i]))
102	print "  " quote langs[i] quote ","
103    }
104
105print "  0\n};\n"
106print "const unsigned int cl_options_count = N_OPTS;\n"
107
108print "const struct cl_option cl_options[] =\n{"
109
110for (i = 0; i < n_opts; i++)
111	back_chain[i] = "N_OPTS";
112
113for (i = 0; i < n_opts; i++) {
114	# Combine the flags of identical switches.  Switches
115	# appear many times if they are handled by many front
116	# ends, for example.
117	while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
118		flags[i + 1] = flags[i] " " flags[i + 1];
119		i++;
120	}
121
122	len = length (opts[i]);
123	enum = "OPT_" opts[i]
124	if (opts[i] == "finline-limit=")
125		enum = enum "eq"
126	gsub ("[^A-Za-z0-9]", "_", enum)
127
128	# If this switch takes joined arguments, back-chain all
129	# subsequent switches to it for which it is a prefix.  If
130	# a later switch S is a longer prefix of a switch T, T
131	# will be back-chained to S in a later iteration of this
132	# for() loop, which is what we want.
133	if (flag_set_p("Joined.*", flags[i])) {
134		for (j = i + 1; j < n_opts; j++) {
135			if (substr (opts[j], 1, len) != opts[i])
136				break;
137			back_chain[j] = enum;
138		}
139	}
140
141	s = substr("                                  ", length (opts[i]))
142	if (i + 1 == n_opts)
143		comma = ""
144
145	if (help[i] == "")
146		hlp = "0"
147	else
148		hlp = quote help[i] quote;
149
150	printf("  { %c-%s%c,\n    %s,\n    %s, %u,\n",
151	       quote, opts[i], quote, hlp, back_chain[i], len)
152	condition = opt_args("Condition", flags[i])
153	cl_flags = switch_flags(flags[i])
154	if (condition != "")
155		printf("#if %s\n" \
156		       "    %s,\n" \
157		       "#else\n" \
158		       "    CL_DISABLED,\n" \
159		       "#endif\n",
160		       condition, cl_flags, cl_flags)
161	else
162		printf("    %s,\n", cl_flags)
163	printf("    %s, %s }%s\n", var_ref(opts[i], flags[i]),
164	       var_set(flags[i]), comma)
165}
166
167print "};"
168}
169