dt_pragma.c revision 239536
1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License (the "License").
6178479Sjb * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21178566Sjb
22178479Sjb/*
23178566Sjb * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24239536Spfg * Copyright (c) 2011, Joyent Inc. All rights reserved.
25178479Sjb */
26178479Sjb
27178479Sjb#pragma ident	"%Z%%M%	%I%	%E% SMI"
28178479Sjb
29178479Sjb#include <assert.h>
30178479Sjb#include <strings.h>
31178566Sjb#if defined(sun)
32178479Sjb#include <alloca.h>
33178566Sjb#endif
34239536Spfg#include <fcntl.h>
35178479Sjb#include <stdlib.h>
36178479Sjb#include <stdio.h>
37178479Sjb
38239536Spfg#include <sys/types.h>
39239536Spfg#include <sys/stat.h>
40239536Spfg
41178479Sjb#include <dt_parser.h>
42178479Sjb#include <dt_impl.h>
43178479Sjb#include <dt_provider.h>
44178479Sjb#include <dt_module.h>
45178479Sjb
46178479Sjb/*
47178479Sjb * This callback function is installed in a given identifier hash to search for
48178479Sjb * and apply deferred pragmas that are pending for a given new identifier name.
49178479Sjb * Multiple pragmas may be pending for a given name; we processs all of them.
50178479Sjb */
51178479Sjb/*ARGSUSED*/
52178479Sjbstatic void
53178479Sjbdt_pragma_apply(dt_idhash_t *dhp, dt_ident_t *idp)
54178479Sjb{
55178479Sjb	dt_idhash_t *php;
56178479Sjb	dt_ident_t *pdp;
57178479Sjb
58178479Sjb	if ((php = yypcb->pcb_pragmas) == NULL)
59178479Sjb		return; /* no pragmas pending for current compilation pass */
60178479Sjb
61178479Sjb	while ((pdp = dt_idhash_lookup(php, idp->di_name)) != NULL) {
62178479Sjb		switch (pdp->di_kind) {
63178479Sjb		case DT_IDENT_PRAGAT:
64178479Sjb			idp->di_attr = pdp->di_attr;
65178479Sjb			break;
66178479Sjb		case DT_IDENT_PRAGBN:
67178479Sjb			idp->di_vers = pdp->di_vers;
68178479Sjb			break;
69178479Sjb		}
70178479Sjb		dt_idhash_delete(php, pdp);
71178479Sjb	}
72178479Sjb}
73178479Sjb
74178479Sjb/*
75178479Sjb * The #pragma attributes directive can be used to reset stability attributes
76178479Sjb * on a global identifier or inline definition.  If the identifier is already
77178479Sjb * defined, we can just change di_attr.  If not, we insert the pragma into a
78178479Sjb * hash table of the current pcb's deferred pragmas for later processing.
79178479Sjb */
80178479Sjbstatic void
81178479Sjbdt_pragma_attributes(const char *prname, dt_node_t *dnp)
82178479Sjb{
83178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
84178479Sjb	dtrace_attribute_t attr, *a;
85178479Sjb	dt_provider_t *pvp;
86178479Sjb	const char *name, *part;
87178479Sjb	dt_ident_t *idp;
88178479Sjb
89178479Sjb	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT ||
90178479Sjb	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
91178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
92178479Sjb		    "<attributes> <ident>\n", prname);
93178479Sjb	}
94178479Sjb
95178479Sjb	if (dtrace_str2attr(dnp->dn_string, &attr) == -1) {
96178479Sjb		xyerror(D_PRAGMA_INVAL, "invalid attributes "
97178479Sjb		    "specified by #pragma %s\n", prname);
98178479Sjb	}
99178479Sjb
100178479Sjb	dnp = dnp->dn_list;
101178479Sjb	name = dnp->dn_string;
102178479Sjb
103178479Sjb	if (strcmp(name, "provider") == 0) {
104178479Sjb		dnp = dnp->dn_list;
105178479Sjb		name = dnp->dn_string;
106178479Sjb
107178479Sjb		dnp = dnp->dn_list;
108178479Sjb		part = dnp->dn_string;
109178479Sjb
110178479Sjb		if ((pvp = dt_provider_lookup(dtp, name)) != NULL) {
111178479Sjb			if (strcmp(part, "provider") == 0) {
112178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_provider;
113178479Sjb			} else if (strcmp(part, "module") == 0) {
114178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_mod;
115178479Sjb			} else if (strcmp(part, "function") == 0) {
116178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_func;
117178479Sjb			} else if (strcmp(part, "name") == 0) {
118178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_name;
119178479Sjb			} else if (strcmp(part, "args") == 0) {
120178479Sjb				a = &pvp->pv_desc.dtvd_attr.dtpa_args;
121178479Sjb			} else {
122178479Sjb				xyerror(D_PRAGMA_INVAL, "invalid component "
123178479Sjb				    "\"%s\" in attribute #pragma "
124178479Sjb				    "for provider %s\n", name, part);
125178479Sjb			}
126178479Sjb
127178479Sjb			*a = attr;
128178479Sjb			return;
129178479Sjb		}
130178479Sjb
131178479Sjb	} else if ((idp = dt_idstack_lookup(
132178479Sjb	    &yypcb->pcb_globals, name)) != NULL) {
133178479Sjb
134178479Sjb		if (idp->di_gen != dtp->dt_gen) {
135178479Sjb			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
136178479Sjb			    "entity defined outside program scope\n", prname);
137178479Sjb		}
138178479Sjb
139178479Sjb		idp->di_attr = attr;
140178479Sjb		return;
141178479Sjb	}
142178479Sjb
143178479Sjb	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
144178479Sjb	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
145178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
146178479Sjb
147178479Sjb	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGAT, 0, 0,
148178479Sjb	    attr, 0, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
149178479Sjb
150178479Sjb	if (idp == NULL)
151178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
152178479Sjb
153178479Sjb	if (dtp->dt_globals->dh_defer == NULL)
154178479Sjb		dtp->dt_globals->dh_defer = &dt_pragma_apply;
155178479Sjb}
156178479Sjb
157178479Sjb/*
158178479Sjb * The #pragma binding directive can be used to reset the version binding
159178479Sjb * on a global identifier or inline definition.  If the identifier is already
160178479Sjb * defined, we can just change di_vers.  If not, we insert the pragma into a
161178479Sjb * hash table of the current pcb's deferred pragmas for later processing.
162178479Sjb */
163178479Sjbstatic void
164178479Sjbdt_pragma_binding(const char *prname, dt_node_t *dnp)
165178479Sjb{
166178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
167178479Sjb	dt_version_t vers;
168178479Sjb	const char *name;
169178479Sjb	dt_ident_t *idp;
170178479Sjb
171178479Sjb	if (dnp == NULL || dnp->dn_kind != DT_NODE_STRING ||
172178479Sjb	    dnp->dn_list == NULL || dnp->dn_list->dn_kind != DT_NODE_IDENT) {
173178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
174178479Sjb		    "\"version\" <ident>\n", prname);
175178479Sjb	}
176178479Sjb
177178479Sjb	if (dt_version_str2num(dnp->dn_string, &vers) == -1) {
178178479Sjb		xyerror(D_PRAGMA_INVAL, "invalid version string "
179178479Sjb		    "specified by #pragma %s\n", prname);
180178479Sjb	}
181178479Sjb
182178479Sjb	name = dnp->dn_list->dn_string;
183178479Sjb	idp = dt_idstack_lookup(&yypcb->pcb_globals, name);
184178479Sjb
185178479Sjb	if (idp != NULL) {
186178479Sjb		if (idp->di_gen != dtp->dt_gen) {
187178479Sjb			xyerror(D_PRAGMA_SCOPE, "#pragma %s cannot modify "
188178479Sjb			    "entity defined outside program scope\n", prname);
189178479Sjb		}
190178479Sjb		idp->di_vers = vers;
191178479Sjb		return;
192178479Sjb	}
193178479Sjb
194178479Sjb	if (yypcb->pcb_pragmas == NULL && (yypcb->pcb_pragmas =
195178479Sjb	    dt_idhash_create("pragma", NULL, 0, 0)) == NULL)
196178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
197178479Sjb
198178479Sjb	idp = dt_idhash_insert(yypcb->pcb_pragmas, name, DT_IDENT_PRAGBN, 0, 0,
199178479Sjb	    _dtrace_defattr, vers, &dt_idops_thaw, (void *)prname, dtp->dt_gen);
200178479Sjb
201178479Sjb	if (idp == NULL)
202178479Sjb		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
203178479Sjb
204178479Sjb	if (dtp->dt_globals->dh_defer == NULL)
205178479Sjb		dtp->dt_globals->dh_defer = &dt_pragma_apply;
206178479Sjb}
207178479Sjb
208239536Spfgstatic void
209239536Spfgdt_pragma_depends_finddep(dtrace_hdl_t *dtp, const char *lname, char *lib,
210239536Spfg    size_t len)
211239536Spfg{
212239536Spfg	dt_dirpath_t *dirp;
213239536Spfg	struct stat sbuf;
214239536Spfg	int found = 0;
215239536Spfg
216239536Spfg	for (dirp = dt_list_next(&dtp->dt_lib_path); dirp != NULL;
217239536Spfg	    dirp = dt_list_next(dirp)) {
218239536Spfg		(void) snprintf(lib, len, "%s/%s", dirp->dir_path, lname);
219239536Spfg
220239536Spfg		if (stat(lib, &sbuf) == 0) {
221239536Spfg			found = 1;
222239536Spfg			break;
223239536Spfg		}
224239536Spfg	}
225239536Spfg
226239536Spfg	if (!found)
227239536Spfg		xyerror(D_PRAGMA_DEPEND,
228239536Spfg		    "failed to find dependency in libpath: %s", lname);
229239536Spfg}
230239536Spfg
231178479Sjb/*
232178479Sjb * The #pragma depends_on directive can be used to express a dependency on a
233178479Sjb * module, provider or library which if not present will cause processing to
234178479Sjb * abort.
235178479Sjb */
236178479Sjbstatic void
237178479Sjbdt_pragma_depends(const char *prname, dt_node_t *cnp)
238178479Sjb{
239178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
240178479Sjb	dt_node_t *nnp = cnp ? cnp->dn_list : NULL;
241178479Sjb	int found;
242178479Sjb	dt_lib_depend_t *dld;
243178566Sjb	char lib[MAXPATHLEN];
244178479Sjb
245178479Sjb	if (cnp == NULL || nnp == NULL ||
246178479Sjb	    cnp->dn_kind != DT_NODE_IDENT || nnp->dn_kind != DT_NODE_IDENT) {
247178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #pragma %s "
248178479Sjb		    "<class> <name>\n", prname);
249178479Sjb	}
250178479Sjb
251178479Sjb	if (strcmp(cnp->dn_string, "provider") == 0)
252178479Sjb		found = dt_provider_lookup(dtp, nnp->dn_string) != NULL;
253178479Sjb	else if (strcmp(cnp->dn_string, "module") == 0) {
254178479Sjb		dt_module_t *mp = dt_module_lookup_by_name(dtp, nnp->dn_string);
255178479Sjb		found = mp != NULL && dt_module_getctf(dtp, mp) != NULL;
256178479Sjb	} else if (strcmp(cnp->dn_string, "library") == 0) {
257178479Sjb		if (yypcb->pcb_cflags & DTRACE_C_CTL) {
258178566Sjb			assert(dtp->dt_filetag != NULL);
259178479Sjb
260239536Spfg			dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
261239536Spfg			    sizeof (lib));
262239536Spfg
263178479Sjb			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
264178479Sjb			    dtp->dt_filetag);
265178479Sjb			assert(dld != NULL);
266178479Sjb
267178479Sjb			if ((dt_lib_depend_add(dtp, &dld->dtld_dependencies,
268178479Sjb			    lib)) != 0) {
269178479Sjb				xyerror(D_PRAGMA_DEPEND,
270178566Sjb				    "failed to add dependency %s:%s\n", lib,
271178479Sjb				    dtrace_errmsg(dtp, dtrace_errno(dtp)));
272178479Sjb			}
273178566Sjb		} else {
274178566Sjb			/*
275178566Sjb			 * By this point we have already performed a topological
276178566Sjb			 * sort of the dependencies; we process this directive
277178566Sjb			 * as satisfied as long as the dependency was properly
278178566Sjb			 * loaded.
279178566Sjb			 */
280178566Sjb			if (dtp->dt_filetag == NULL)
281178566Sjb				xyerror(D_PRAGMA_DEPEND, "main program may "
282178566Sjb				    "not explicitly depend on a library");
283178566Sjb
284178566Sjb			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep,
285178566Sjb			    dtp->dt_filetag);
286178566Sjb			assert(dld != NULL);
287178566Sjb
288239536Spfg			dt_pragma_depends_finddep(dtp, nnp->dn_string, lib,
289239536Spfg			    sizeof (lib));
290178566Sjb			dld = dt_lib_depend_lookup(&dtp->dt_lib_dep_sorted,
291178566Sjb			    lib);
292178566Sjb			assert(dld != NULL);
293178566Sjb
294178566Sjb			if (!dld->dtld_loaded)
295178566Sjb				xyerror(D_PRAGMA_DEPEND, "program requires "
296178566Sjb				    "library \"%s\" which failed to load",
297178566Sjb				    lib);
298178479Sjb		}
299178566Sjb
300178566Sjb		found = B_TRUE;
301178479Sjb	} else {
302178479Sjb		xyerror(D_PRAGMA_INVAL, "invalid class %s "
303178479Sjb		    "specified by #pragma %s\n", cnp->dn_string, prname);
304178479Sjb	}
305178479Sjb
306178479Sjb	if (!found) {
307178479Sjb		xyerror(D_PRAGMA_DEPEND, "program requires %s %s\n",
308178479Sjb		    cnp->dn_string, nnp->dn_string);
309178479Sjb	}
310178479Sjb}
311178479Sjb
312178479Sjb/*
313178479Sjb * The #pragma error directive can be followed by any list of tokens, which we
314178479Sjb * just concatenate and print as part of our error message.
315178479Sjb */
316178479Sjbstatic void
317178479Sjbdt_pragma_error(const char *prname, dt_node_t *dnp)
318178479Sjb{
319178479Sjb	dt_node_t *enp;
320178479Sjb	size_t n = 0;
321178479Sjb	char *s;
322178479Sjb
323178479Sjb	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
324178479Sjb		if (enp->dn_kind == DT_NODE_IDENT ||
325178479Sjb		    enp->dn_kind == DT_NODE_STRING)
326178479Sjb			n += strlen(enp->dn_string) + 1;
327178479Sjb	}
328178479Sjb
329178479Sjb	s = alloca(n + 1);
330178479Sjb	s[0] = '\0';
331178479Sjb
332178479Sjb	for (enp = dnp; enp != NULL; enp = enp->dn_list) {
333178479Sjb		if (enp->dn_kind == DT_NODE_IDENT ||
334178479Sjb		    enp->dn_kind == DT_NODE_STRING) {
335178479Sjb			(void) strcat(s, enp->dn_string);
336178479Sjb			(void) strcat(s, " ");
337178479Sjb		}
338178479Sjb	}
339178479Sjb
340178479Sjb	xyerror(D_PRAGERR, "#%s: %s\n", prname, s);
341178479Sjb}
342178479Sjb
343178479Sjb/*ARGSUSED*/
344178479Sjbstatic void
345178479Sjbdt_pragma_ident(const char *prname, dt_node_t *dnp)
346178479Sjb{
347178479Sjb	/* ignore any #ident or #pragma ident lines */
348178479Sjb}
349178479Sjb
350178479Sjbstatic void
351178479Sjbdt_pragma_option(const char *prname, dt_node_t *dnp)
352178479Sjb{
353178479Sjb	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
354178479Sjb	char *opt, *val;
355178479Sjb
356178479Sjb	if (dnp == NULL || dnp->dn_kind != DT_NODE_IDENT) {
357178479Sjb		xyerror(D_PRAGMA_MALFORM,
358178479Sjb		    "malformed #pragma %s <option>=<val>\n", prname);
359178479Sjb	}
360178479Sjb
361178479Sjb	if (dnp->dn_list != NULL) {
362178479Sjb		xyerror(D_PRAGMA_MALFORM,
363178479Sjb		    "superfluous arguments specified for #pragma %s\n", prname);
364178479Sjb	}
365178479Sjb
366178479Sjb	opt = alloca(strlen(dnp->dn_string) + 1);
367178479Sjb	(void) strcpy(opt, dnp->dn_string);
368178479Sjb
369178479Sjb	if ((val = strchr(opt, '=')) != NULL)
370178479Sjb		*val++ = '\0';
371178479Sjb
372178479Sjb	if (dtrace_setopt(dtp, opt, val) == -1) {
373178479Sjb		if (val == NULL) {
374178479Sjb			xyerror(D_PRAGMA_OPTSET,
375178479Sjb			    "failed to set option '%s': %s\n", opt,
376178479Sjb			    dtrace_errmsg(dtp, dtrace_errno(dtp)));
377178479Sjb		} else {
378178479Sjb			xyerror(D_PRAGMA_OPTSET,
379178479Sjb			    "failed to set option '%s' to '%s': %s\n",
380178479Sjb			    opt, val, dtrace_errmsg(dtp, dtrace_errno(dtp)));
381178479Sjb		}
382178479Sjb	}
383178479Sjb}
384178479Sjb
385178479Sjb/*
386178479Sjb * The #line directive is used to reset the input line number and to optionally
387178479Sjb * note the file name for use in error messages.  Sun cpp(1) also produces a
388178479Sjb * third integer token after the filename which is one of the following:
389178479Sjb *
390178479Sjb * 0 - line change has nothing to do with an #include file
391178479Sjb * 1 - line change because we just entered a #include file
392178479Sjb * 2 - line change because we just exited a #include file
393178479Sjb *
394178479Sjb * We use these state tokens to adjust pcb_idepth, which in turn controls
395178479Sjb * whether type lookups access the global type space or not.
396178479Sjb */
397178479Sjbstatic void
398178479Sjbdt_pragma_line(const char *prname, dt_node_t *dnp)
399178479Sjb{
400178479Sjb	dt_node_t *fnp = dnp ? dnp->dn_list : NULL;
401178479Sjb	dt_node_t *inp = fnp ? fnp->dn_list : NULL;
402178479Sjb
403178479Sjb	if ((dnp == NULL || dnp->dn_kind != DT_NODE_INT) ||
404178479Sjb	    (fnp != NULL && fnp->dn_kind != DT_NODE_STRING) ||
405178479Sjb	    (inp != NULL && inp->dn_kind != DT_NODE_INT)) {
406178479Sjb		xyerror(D_PRAGMA_MALFORM, "malformed #%s "
407178479Sjb		    "<line> [ [\"file\"] state ]\n", prname);
408178479Sjb	}
409178479Sjb
410178479Sjb	/*
411178479Sjb	 * If a file is specified, free any old pcb_filetag and swap fnp's
412178479Sjb	 * dn_string into pcb_filetag as the new filename for error messages.
413178479Sjb	 */
414178479Sjb	if (fnp != NULL) {
415178479Sjb		if (yypcb->pcb_filetag != NULL)
416178479Sjb			free(yypcb->pcb_filetag);
417178479Sjb
418178479Sjb		/*
419178479Sjb		 * This is not pretty, but is a necessary evil until we either
420178479Sjb		 * write "dpp" or get a useful standalone cpp from DevPro.  If
421178479Sjb		 * the filename begins with /dev/fd, we know it's the master
422178479Sjb		 * input file (see dt_preproc() in dt_cc.c), so just clear the
423178479Sjb		 * dt_filetag pointer so error messages refer to the main file.
424178479Sjb		 */
425178479Sjb		if (strncmp(fnp->dn_string, "/dev/fd/", 8) != 0) {
426178479Sjb			yypcb->pcb_filetag = fnp->dn_string;
427178479Sjb			fnp->dn_string = NULL;
428178479Sjb		} else
429178479Sjb			yypcb->pcb_filetag = NULL;
430178479Sjb	}
431178479Sjb
432178479Sjb	if (inp != NULL) {
433178479Sjb		if (inp->dn_value == 1)
434178479Sjb			yypcb->pcb_idepth++;
435178479Sjb		else if (inp->dn_value == 2 && yypcb->pcb_idepth != 0)
436178479Sjb			yypcb->pcb_idepth--;
437178479Sjb	}
438178479Sjb
439178479Sjb	yylineno = dnp->dn_value;
440178479Sjb}
441178479Sjb
442178479Sjb/*
443178479Sjb * D compiler pragma types range from control directives to common pragmas to
444178479Sjb * D custom pragmas, in order of specificity.  Similar to gcc, we use #pragma D
445178479Sjb * as a special prefix for our pragmas so they can be used in mixed headers.
446178479Sjb */
447178479Sjb#define	DT_PRAGMA_DIR	0	/* pragma directive may be used after naked # */
448178479Sjb#define	DT_PRAGMA_SUB	1	/* pragma directive may be used after #pragma */
449178479Sjb#define	DT_PRAGMA_DCP	2	/* pragma may only be used after #pragma D */
450178479Sjb
451178479Sjbstatic const struct dt_pragmadesc {
452178479Sjb	const char *dpd_name;
453178479Sjb	void (*dpd_func)(const char *, dt_node_t *);
454178479Sjb	int dpd_kind;
455178479Sjb} dt_pragmas[] = {
456178479Sjb	{ "attributes", dt_pragma_attributes, DT_PRAGMA_DCP },
457178479Sjb	{ "binding", dt_pragma_binding, DT_PRAGMA_DCP },
458178479Sjb	{ "depends_on", dt_pragma_depends, DT_PRAGMA_DCP },
459178479Sjb	{ "error", dt_pragma_error, DT_PRAGMA_DIR },
460178479Sjb	{ "ident", dt_pragma_ident, DT_PRAGMA_DIR },
461178479Sjb	{ "line", dt_pragma_line, DT_PRAGMA_DIR },
462178479Sjb	{ "option", dt_pragma_option, DT_PRAGMA_DCP },
463178479Sjb	{ NULL, NULL }
464178479Sjb};
465178479Sjb
466178479Sjb/*
467178479Sjb * Process a control line #directive by looking up the directive name in our
468178479Sjb * lookup table and invoking the corresponding function with the token list.
469178479Sjb * According to K&R[A12.9], we silently ignore null directive lines.
470178479Sjb */
471178479Sjbvoid
472178479Sjbdt_pragma(dt_node_t *pnp)
473178479Sjb{
474178479Sjb	const struct dt_pragmadesc *dpd;
475178479Sjb	dt_node_t *dnp;
476178479Sjb	int kind = DT_PRAGMA_DIR;
477178479Sjb
478178479Sjb	for (dnp = pnp; dnp != NULL; dnp = dnp->dn_list) {
479178479Sjb		if (dnp->dn_kind == DT_NODE_INT) {
480178479Sjb			dt_pragma_line("line", dnp);
481178479Sjb			break;
482178479Sjb		}
483178479Sjb
484178479Sjb		if (dnp->dn_kind != DT_NODE_IDENT)
485178479Sjb			xyerror(D_PRAGCTL_INVAL, "invalid control directive\n");
486178479Sjb
487178479Sjb		if (kind == DT_PRAGMA_DIR &&
488178479Sjb		    strcmp(dnp->dn_string, "pragma") == 0) {
489178479Sjb			kind = DT_PRAGMA_SUB;
490178479Sjb			continue;
491178479Sjb		}
492178479Sjb
493178479Sjb		if (kind == DT_PRAGMA_SUB &&
494178479Sjb		    strcmp(dnp->dn_string, "D") == 0) {
495178479Sjb			kind = DT_PRAGMA_DCP;
496178479Sjb			continue;
497178479Sjb		}
498178479Sjb
499178479Sjb		for (dpd = dt_pragmas; dpd->dpd_name != NULL; dpd++) {
500178479Sjb			if (dpd->dpd_kind <= kind &&
501178479Sjb			    strcmp(dpd->dpd_name, dnp->dn_string) == 0)
502178479Sjb				break;
503178479Sjb		}
504178479Sjb
505178479Sjb		yylineno--; /* since we've already seen \n */
506178479Sjb
507178479Sjb		if (dpd->dpd_name != NULL) {
508178479Sjb			dpd->dpd_func(dpd->dpd_name, dnp->dn_list);
509178479Sjb			yylineno++;
510178479Sjb			break;
511178479Sjb		}
512178479Sjb
513178479Sjb		switch (kind) {
514178479Sjb		case DT_PRAGMA_DIR:
515178479Sjb			xyerror(D_PRAGCTL_INVAL, "invalid control directive: "
516178479Sjb			    "#%s\n", dnp->dn_string);
517178479Sjb			/*NOTREACHED*/
518178479Sjb		case DT_PRAGMA_SUB:
519178479Sjb			break; /* K&R[A12.8] says to ignore unknown pragmas */
520178479Sjb		case DT_PRAGMA_DCP:
521178479Sjb		default:
522178479Sjb			xyerror(D_PRAGMA_INVAL, "invalid D pragma: %s\n",
523178479Sjb			    dnp->dn_string);
524178479Sjb		}
525178479Sjb
526178479Sjb		yylineno++;
527178479Sjb		break;
528178479Sjb	}
529178479Sjb
530178479Sjb	dt_node_list_free(&pnp);
531178479Sjb}
532