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