xref: /openbsd/sys/ddb/db_variables.c (revision d415bd75)
1 /*	$OpenBSD: db_variables.c,v 1.22 2023/03/08 04:43:07 guenther 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 
33 #include <machine/db_machdep.h>
34 
35 #include <ddb/db_lex.h>
36 #include <ddb/db_variables.h>
37 #include <ddb/db_command.h>
38 #include <ddb/db_sym.h>
39 #include <ddb/db_extern.h>
40 #include <ddb/db_var.h>
41 
42 struct db_variable db_vars[] = {
43 	{ "radix",	(long *)&db_radix, db_var_rw_int },
44 	{ "maxoff",	(long *)&db_maxoff, db_var_rw_int },
45 	{ "maxwidth",	(long *)&db_max_width, db_var_rw_int },
46 	{ "tabstops",	(long *)&db_tab_stop_width, db_var_rw_int },
47 	{ "lines",	(long *)&db_max_line, db_var_rw_int },
48 	{ "log",	(long *)&db_log, db_var_rw_int }
49 };
50 struct db_variable *db_evars = db_vars + nitems(db_vars);
51 
52 int
53 db_find_variable(struct db_variable **varp)
54 {
55 	int	t;
56 	struct db_variable *vp;
57 
58 	t = db_read_token();
59 	if (t == tIDENT) {
60 		for (vp = db_vars; vp < db_evars; vp++) {
61 			if (!strcmp(db_tok_string, vp->name)) {
62 				*varp = vp;
63 				return (1);
64 			}
65 		}
66 		for (vp = db_regs; vp < db_eregs; vp++) {
67 			if (!strcmp(db_tok_string, vp->name)) {
68 				*varp = vp;
69 				return (1);
70 			}
71 		}
72 	}
73 	db_error("Unknown variable\n");
74 	/*NOTREACHED*/
75 	return 0;
76 }
77 
78 int
79 db_get_variable(db_expr_t *valuep)
80 {
81 	struct db_variable *vp;
82 
83 	if (!db_find_variable(&vp))
84 		return (0);
85 
86 	db_read_variable(vp, valuep);
87 
88 	return (1);
89 }
90 
91 int
92 db_set_variable(db_expr_t value)
93 {
94 	struct db_variable *vp;
95 
96 	if (!db_find_variable(&vp))
97 		return (0);
98 
99 	db_write_variable(vp, &value);
100 
101 	return (1);
102 }
103 
104 
105 void
106 db_read_variable(struct db_variable *vp, db_expr_t *valuep)
107 {
108 	int	(*func)(struct db_variable *, db_expr_t *, int) = vp->fcn;
109 
110 	if (func == FCN_NULL)
111 		*valuep = *(vp->valuep);
112 	else
113 		(*func)(vp, valuep, DB_VAR_GET);
114 }
115 
116 void
117 db_write_variable(struct db_variable *vp, db_expr_t *valuep)
118 {
119 	int	(*func)(struct db_variable *, db_expr_t *, int) = vp->fcn;
120 
121 	if (func == FCN_NULL)
122 		*(vp->valuep) = *valuep;
123 	else
124 		(*func)(vp, valuep, DB_VAR_SET);
125 }
126 
127 void
128 db_set_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
129 {
130 	db_expr_t	value;
131 	struct db_variable *vp;
132 	int	t;
133 
134 	t = db_read_token();
135 	if (t != tDOLLAR) {
136 		db_error("Unknown variable\n");
137 		/*NOTREACHED*/
138 	}
139 	if (!db_find_variable(&vp)) {
140 		db_error("Unknown variable\n");
141 		/*NOTREACHED*/
142 	}
143 
144 	t = db_read_token();
145 	if (t != tEQ)
146 		db_unread_token(t);
147 
148 	if (!db_expression(&value)) {
149 		db_error("No value\n");
150 		/*NOTREACHED*/
151 	}
152 	if (db_read_token() != tEOL) {
153 		db_error("?\n");
154 		/*NOTREACHED*/
155 	}
156 
157 	db_write_variable(vp, &value);
158 }
159 
160 int
161 db_var_rw_int(struct db_variable *var, db_expr_t *expr, int mode)
162 {
163 
164 	if (mode == DB_VAR_SET)
165 		*(int *)var->valuep = *expr;
166 	else
167 		*expr = *(int *)var->valuep;
168 	return (0);
169 }
170