xref: /freebsd/sys/ddb/db_variables.c (revision ce834215a70ff69e7e222827437116eee2f9ac6f)
1 /*
2  * Mach Operating System
3  * Copyright (c) 1991,1990 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie the
24  * rights to redistribute these changes.
25  *
26  *	$Id: db_variables.c,v 1.13 1997/02/22 09:28:31 peter Exp $
27  */
28 
29 /*
30  * 	Author: David B. Golub, Carnegie Mellon University
31  *	Date:	7/90
32  */
33 
34 #include <sys/param.h>
35 
36 #include <ddb/ddb.h>
37 #include <ddb/db_lex.h>
38 #include <ddb/db_variables.h>
39 
40 static int	db_find_variable __P((struct db_variable **varp));
41 static void	db_write_variable __P((struct db_variable *, db_expr_t *));
42 
43 #ifdef notused
44 static int	db_set_variable __P((db_expr_t value));
45 #endif
46 
47 static struct db_variable db_vars[] = {
48 	{ "radix",	&db_radix, FCN_NULL },
49 	{ "maxoff",	(int *)&db_maxoff, FCN_NULL },
50 	{ "maxwidth",	&db_max_width, FCN_NULL },
51 	{ "tabstops",	&db_tab_stop_width, FCN_NULL },
52 };
53 static struct db_variable *db_evars =
54 		db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
55 
56 static int
57 db_find_variable(varp)
58 	struct db_variable	**varp;
59 {
60 	int	t;
61 	struct db_variable *vp;
62 
63 	t = db_read_token();
64 	if (t == tIDENT) {
65 	    for (vp = db_vars; vp < db_evars; vp++) {
66 		if (!strcmp(db_tok_string, vp->name)) {
67 		    *varp = vp;
68 		    return (1);
69 		}
70 	    }
71 	    for (vp = db_regs; vp < db_eregs; vp++) {
72 		if (!strcmp(db_tok_string, vp->name)) {
73 		    *varp = vp;
74 		    return (1);
75 		}
76 	    }
77 	}
78 	db_error("Unknown variable\n");
79 	return (0);
80 }
81 
82 int
83 db_get_variable(valuep)
84 	db_expr_t	*valuep;
85 {
86 	struct db_variable *vp;
87 
88 	if (!db_find_variable(&vp))
89 	    return (0);
90 
91 	db_read_variable(vp, valuep);
92 
93 	return (1);
94 }
95 
96 #ifdef notused
97 static int
98 db_set_variable(value)
99 	db_expr_t	value;
100 {
101 	struct db_variable *vp;
102 
103 	if (!db_find_variable(&vp))
104 	    return (0);
105 
106 	db_write_variable(vp, &value);
107 
108 	return (1);
109 }
110 #endif
111 
112 void
113 db_read_variable(vp, valuep)
114 	struct db_variable *vp;
115 	db_expr_t	*valuep;
116 {
117 	db_varfcn_t	*func = vp->fcn;
118 
119 	if (func == FCN_NULL)
120 	    *valuep = *(vp->valuep);
121 	else
122 	    (*func)(vp, valuep, DB_VAR_GET);
123 }
124 
125 static void
126 db_write_variable(vp, valuep)
127 	struct db_variable *vp;
128 	db_expr_t	*valuep;
129 {
130 	db_varfcn_t	*func = vp->fcn;
131 
132 	if (func == FCN_NULL)
133 	    *(vp->valuep) = *valuep;
134 	else
135 	    (*func)(vp, valuep, DB_VAR_SET);
136 }
137 
138 void
139 db_set_cmd(dummy1, dummy2, dummy3, dummy4)
140 	db_expr_t	dummy1;
141 	boolean_t	dummy2;
142 	db_expr_t	dummy3;
143 	char *		dummy4;
144 {
145 	db_expr_t	value;
146 	struct db_variable *vp;
147 	int	t;
148 
149 	t = db_read_token();
150 	if (t != tDOLLAR) {
151 	    db_error("Unknown variable\n");
152 	    return;
153 	}
154 	if (!db_find_variable(&vp)) {
155 	    db_error("Unknown variable\n");
156 	    return;
157 	}
158 
159 	t = db_read_token();
160 	if (t != tEQ)
161 	    db_unread_token(t);
162 
163 	if (!db_expression(&value)) {
164 	    db_error("No value\n");
165 	    return;
166 	}
167 	if (db_read_token() != tEOL) {
168 	    db_error("?\n");
169 	}
170 
171 	db_write_variable(vp, &value);
172 }
173