xref: /netbsd-src/sys/ddb/db_variables.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: db_variables.c,v 1.8 1996/02/05 01:57:19 christos Exp $	*/
2 
3 /*
4  * Mach Operating System
5  * Copyright (c) 1991,1990 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie the
26  * rights to redistribute these changes.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/proc.h>
31 
32 #include <machine/db_machdep.h>
33 
34 #include <ddb/db_lex.h>
35 #include <ddb/db_variables.h>
36 #include <ddb/db_command.h>
37 #include <ddb/db_sym.h>
38 #include <ddb/db_extern.h>
39 
40 extern unsigned int	db_maxoff;
41 
42 extern int	db_radix;
43 extern int	db_max_width;
44 extern int	db_tab_stop_width;
45 extern int	db_max_line;
46 
47 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 	{ "lines",	&db_max_line, FCN_NULL },
53 };
54 struct db_variable *db_evars = db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
55 
56 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 	/*NOTREACHED*/
80 	return 0;
81 }
82 
83 int
84 db_get_variable(valuep)
85 	db_expr_t	*valuep;
86 {
87 	struct db_variable *vp;
88 
89 	if (!db_find_variable(&vp))
90 	    return (0);
91 
92 	db_read_variable(vp, valuep);
93 
94 	return (1);
95 }
96 
97 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 
111 
112 void
113 db_read_variable(vp, valuep)
114 	struct db_variable *vp;
115 	db_expr_t	*valuep;
116 {
117 	int	(*func) __P((struct db_variable *, db_expr_t *, int)) = vp->fcn;
118 
119 	if (func == FCN_NULL)
120 	    *valuep = *(vp->valuep);
121 	else
122 	    (*func)(vp, valuep, DB_VAR_GET);
123 }
124 
125 void
126 db_write_variable(vp, valuep)
127 	struct db_variable *vp;
128 	db_expr_t	*valuep;
129 {
130 	int	(*func) __P((struct db_variable *, db_expr_t *, int)) = vp->fcn;
131 
132 	if (func == FCN_NULL)
133 	    *(vp->valuep) = *valuep;
134 	else
135 	    (*func)(vp, valuep, DB_VAR_SET);
136 }
137 
138 /*ARGSUSED*/
139 void
140 db_set_cmd(addr, have_addr, count, modif)
141 	db_expr_t	addr;
142 	int		have_addr;
143 	db_expr_t	count;
144 	char *		modif;
145 {
146 	db_expr_t	value;
147 	struct db_variable *vp;
148 	int	t;
149 
150 	t = db_read_token();
151 	if (t != tDOLLAR) {
152 	    db_error("Unknown variable\n");
153 	    /*NOTREACHED*/
154 	}
155 	if (!db_find_variable(&vp)) {
156 	    db_error("Unknown variable\n");
157 	    /*NOTREACHED*/
158 	}
159 
160 	t = db_read_token();
161 	if (t != tEQ)
162 	    db_unread_token(t);
163 
164 	if (!db_expression(&value)) {
165 	    db_error("No value\n");
166 	    /*NOTREACHED*/
167 	}
168 	if (db_read_token() != tEOL) {
169 	    db_error("?\n");
170 	    /*NOTREACHED*/
171 	}
172 
173 	db_write_variable(vp, &value);
174 }
175