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