opt-functions.awk revision 169689
1130812Smarcel#  Copyright (C) 2003,2004 Free Software Foundation, Inc.
2130812Smarcel#  Contributed by Kelley Cook, June 2004.
3130812Smarcel#  Original code from Neil Booth, May 2003.
4130812Smarcel#
5130812Smarcel# This program is free software; you can redistribute it and/or modify it
6130812Smarcel# under the terms of the GNU General Public License as published by the
7130812Smarcel# Free Software Foundation; either version 2, or (at your option) any
8130812Smarcel# later version.
9130812Smarcel# 
10130812Smarcel# This program is distributed in the hope that it will be useful,
11130812Smarcel# but WITHOUT ANY WARRANTY; without even the implied warranty of
12130812Smarcel# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13130812Smarcel# GNU General Public License for more details.
14130812Smarcel# 
15130812Smarcel# You should have received a copy of the GNU General Public License
16130812Smarcel# along with this program; if not, write to the Free Software
17130812Smarcel# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18130812Smarcel
19130812Smarcel# Some common subroutines for use by opt[ch]-gen.awk.
20130812Smarcel
21130812Smarcel# Return nonzero if FLAGS contains a flag matching REGEX.
22130812Smarcelfunction flag_set_p(regex, flags)
23130812Smarcel{
24130812Smarcel	return (" " flags " ") ~ (" " regex " ")
25130812Smarcel}
26130812Smarcel
27130812Smarcel# Return STRING if FLAGS contains a flag matching regexp REGEX,
28130812Smarcel# otherwise return the empty string.
29130812Smarcelfunction test_flag(regex, flags, string)
30130812Smarcel{
31130812Smarcel	if (flag_set_p(regex, flags))
32130812Smarcel		return string
33130812Smarcel	return ""
34130812Smarcel}
35130812Smarcel
36130812Smarcel# If FLAGS contains a "NAME(...argument...)" flag, return the value
37130812Smarcel# of the argument.  Return the empty string otherwise.
38130812Smarcelfunction opt_args(name, flags)
39130812Smarcel{
40130812Smarcel	flags = " " flags
41130812Smarcel	if (flags !~ " " name "\\(")
42130812Smarcel		return ""
43130812Smarcel	sub(".* " name "\\(", "", flags)
44130812Smarcel	sub("\\).*", "", flags)
45130812Smarcel
46130812Smarcel	return flags
47130812Smarcel}
48130812Smarcel
49130812Smarcel# Return the Nth comma-separated element of S.  Return the empty string
50130812Smarcel# if S does not contain N elements.
51130812Smarcelfunction nth_arg(n, s)
52130812Smarcel{
53130812Smarcel	while (n-- > 0) {
54130812Smarcel		if (s !~ ",")
55130812Smarcel			return ""
56130812Smarcel		sub("[^,]*, *", "", s)
57130812Smarcel	}
58130812Smarcel	sub(",.*", "", s)
59130812Smarcel	return s
60130812Smarcel}
61130812Smarcel
62130812Smarcel# Return a bitmask of CL_* values for option flags FLAGS.
63130812Smarcelfunction switch_flags (flags)
64130812Smarcel{
65130812Smarcel	result = "0"
66130812Smarcel	for (j = 0; j < n_langs; j++) {
67130812Smarcel		regex = langs[j]
68130812Smarcel		gsub ( "\\+", "\\+", regex )
69130812Smarcel		result = result test_flag(regex, flags, " | " macros[j])
70130812Smarcel	}
71130812Smarcel	result = result \
72130812Smarcel	  test_flag("Common", flags, " | CL_COMMON") \
73130812Smarcel	  test_flag("Target", flags, " | CL_TARGET") \
74130812Smarcel	  test_flag("Joined", flags, " | CL_JOINED") \
75130812Smarcel	  test_flag("JoinedOrMissing", flags, " | CL_JOINED | CL_MISSING_OK") \
76130812Smarcel	  test_flag("Separate", flags, " | CL_SEPARATE") \
77130812Smarcel	  test_flag("RejectNegative", flags, " | CL_REJECT_NEGATIVE") \
78130812Smarcel	  test_flag("UInteger", flags, " | CL_UINTEGER") \
79130812Smarcel	  test_flag("Undocumented", flags,  " | CL_UNDOCUMENTED") \
80130812Smarcel	  test_flag("Report", flags, " | CL_REPORT")
81130812Smarcel	sub( "^0 \\| ", "", result )
82130812Smarcel	return result
83130812Smarcel}
84130812Smarcel
85130812Smarcel# If FLAGS includes a Var flag, return the name of the variable it specifies.
86130812Smarcel# Return the empty string otherwise.
87130812Smarcelfunction var_name(flags)
88130812Smarcel{
89130812Smarcel	return nth_arg(0, opt_args("Var", flags))
90130812Smarcel}
91130812Smarcel
92130812Smarcel# Return true if the option described by FLAGS has a globally-visible state.
93130812Smarcelfunction global_state_p(flags)
94130812Smarcel{
95130812Smarcel	return (var_name(flags) != "" \
96130812Smarcel		|| opt_args("Mask", flags) != "" \
97130812Smarcel		|| opt_args("InverseMask", flags) != "")
98130812Smarcel}
99130812Smarcel
100130812Smarcel# Return true if the option described by FLAGS must have some state
101130812Smarcel# associated with it.
102130812Smarcelfunction needs_state_p(flags)
103130812Smarcel{
104130812Smarcel	return flag_set_p("Target", flags)
105130812Smarcel}
106130812Smarcel
107130812Smarcel# If FLAGS describes an option that needs a static state variable,
108130812Smarcel# return the name of that variable, otherwise return "".  NAME is
109130812Smarcel# the name of the option.
110function static_var(name, flags)
111{
112	if (global_state_p(flags) || !needs_state_p(flags))
113		return ""
114	gsub ("[^A-Za-z0-9]", "_", name)
115	return "VAR_" name
116}
117
118# Return the type of variable that should be associated with the given flags.
119function var_type(flags)
120{
121	if (!flag_set_p("Joined.*", flags))
122		return "int "
123	else if (flag_set_p("UInteger", flags))
124		return "int "
125	else
126		return "const char *"
127}
128
129# Given that an option has flags FLAGS, return an initializer for the
130# "var_cond" and "var_value" fields of its cl_options[] entry.
131function var_set(flags)
132{
133	s = nth_arg(1, opt_args("Var", flags))
134	if (s != "")
135		return "CLVC_EQUAL, " s
136	s = opt_args("Mask", flags);
137	if (s != "") {
138		vn = var_name(flags);
139		if (vn)
140			return "CLVC_BIT_SET, OPTION_MASK_" s
141		else
142			return "CLVC_BIT_SET, MASK_" s
143	}
144	s = nth_arg(0, opt_args("InverseMask", flags));
145	if (s != "") {
146		vn = var_name(flags);
147		if (vn)
148			return "CLVC_BIT_CLEAR, OPTION_MASK_" s
149		else
150			return "CLVC_BIT_CLEAR, MASK_" s
151	}
152	if (var_type(flags) == "const char *")
153		return "CLVC_STRING, 0"
154	return "CLVC_BOOLEAN, 0"
155}
156
157# Given that an option called NAME has flags FLAGS, return an initializer
158# for the "flag_var" field of its cl_options[] entry.
159function var_ref(name, flags)
160{
161	name = var_name(flags) static_var(name, flags)
162	if (name != "")
163		return "&" name
164	if (opt_args("Mask", flags) != "")
165		return "&target_flags"
166	if (opt_args("InverseMask", flags) != "")
167		return "&target_flags"
168	return "0"
169}
170