1 /* $OpenBSD: db_variables.c,v 1.21 2020/10/15 03:14:00 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
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 /*ARGSUSED*/
128 void
129 db_set_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
130 {
131 db_expr_t value;
132 struct db_variable *vp;
133 int t;
134
135 t = db_read_token();
136 if (t != tDOLLAR) {
137 db_error("Unknown variable\n");
138 /*NOTREACHED*/
139 }
140 if (!db_find_variable(&vp)) {
141 db_error("Unknown variable\n");
142 /*NOTREACHED*/
143 }
144
145 t = db_read_token();
146 if (t != tEQ)
147 db_unread_token(t);
148
149 if (!db_expression(&value)) {
150 db_error("No value\n");
151 /*NOTREACHED*/
152 }
153 if (db_read_token() != tEOL) {
154 db_error("?\n");
155 /*NOTREACHED*/
156 }
157
158 db_write_variable(vp, &value);
159 }
160
161 int
162 db_var_rw_int(struct db_variable *var, db_expr_t *expr, int mode)
163 {
164
165 if (mode == DB_VAR_SET)
166 *(int *)var->valuep = *expr;
167 else
168 *expr = *(int *)var->valuep;
169 return (0);
170 }
Cache object: 981e5e9bee7f908da2ebdb3839e98cfe
|