xref: /netbsd-src/sys/ddb/db_variables.c (revision 0b9f50897e9a9c6709320fafb4c3787fddcc0a45)
1 /*
2  * Mach Operating System
3  * Copyright (c) 1991,1990 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie the
24  * rights to redistribute these changes.
25  *
26  *	$Id: db_variables.c,v 1.4 1993/08/02 17:53:15 mycroft Exp $
27  */
28 
29 #include "param.h"
30 #include "proc.h"
31 #include <machine/db_machdep.h>
32 
33 #include <ddb/db_lex.h>
34 #include <ddb/db_variables.h>
35 
36 extern unsigned int	db_maxoff;
37 
38 extern int	db_radix;
39 extern int	db_max_width;
40 extern int	db_tab_stop_width;
41 extern int	db_max_line;
42 
43 struct db_variable db_vars[] = {
44 	{ "radix",	&db_radix, FCN_NULL },
45 	{ "maxoff",	(int *)&db_maxoff, FCN_NULL },
46 	{ "maxwidth",	&db_max_width, FCN_NULL },
47 	{ "tabstops",	&db_tab_stop_width, FCN_NULL },
48 	{ "lines",	&db_max_line, FCN_NULL },
49 };
50 struct db_variable *db_evars = db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
51 
52 int
53 db_find_variable(varp)
54 	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 	return (0);
76 }
77 
78 int
79 db_get_variable(valuep)
80 	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(value)
94 	db_expr_t	value;
95 {
96 	struct db_variable *vp;
97 
98 	if (!db_find_variable(&vp))
99 	    return (0);
100 
101 	db_write_variable(vp, &value);
102 
103 	return (1);
104 }
105 
106 
107 db_read_variable(vp, valuep)
108 	struct db_variable *vp;
109 	db_expr_t	*valuep;
110 {
111 	int	(*func)() = vp->fcn;
112 
113 	if (func == FCN_NULL)
114 	    *valuep = *(vp->valuep);
115 	else
116 	    (*func)(vp, valuep, DB_VAR_GET);
117 }
118 
119 db_write_variable(vp, valuep)
120 	struct db_variable *vp;
121 	db_expr_t	*valuep;
122 {
123 	int	(*func)() = vp->fcn;
124 
125 	if (func == FCN_NULL)
126 	    *(vp->valuep) = *valuep;
127 	else
128 	    (*func)(vp, valuep, DB_VAR_SET);
129 }
130 
131 void
132 db_set_cmd()
133 {
134 	db_expr_t	value;
135 	int	(*func)();
136 	struct db_variable *vp;
137 	int	t;
138 
139 	t = db_read_token();
140 	if (t != tDOLLAR) {
141 	    db_error("Unknown variable\n");
142 	    return;
143 	}
144 	if (!db_find_variable(&vp)) {
145 	    db_error("Unknown variable\n");
146 	    return;
147 	}
148 
149 	t = db_read_token();
150 	if (t != tEQ)
151 	    db_unread_token(t);
152 
153 	if (!db_expression(&value)) {
154 	    db_error("No value\n");
155 	    return;
156 	}
157 	if (db_read_token() != tEOL) {
158 	    db_error("?\n");
159 	}
160 
161 	db_write_variable(vp, &value);
162 }
163