db_variables.c revision 798
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.2 1993/10/16 16:47:29 rgrimes Exp $
27 */
28
29/*
30 * 	Author: David B. Golub, Carnegie Mellon University
31 *	Date:	7/90
32 */
33
34#include "param.h"
35#include "systm.h"
36#include "proc.h"
37#include "ddb/ddb.h"
38
39#include <ddb/db_lex.h>
40#include <ddb/db_variables.h>
41
42void db_read_variable(struct db_variable *, db_expr_t *);
43static void db_write_variable(struct db_variable *, db_expr_t *);
44
45struct db_variable db_vars[] = {
46	{ "radix",	&db_radix, FCN_NULL },
47	{ "maxoff",	(int *)&db_maxoff, FCN_NULL },
48	{ "maxwidth",	&db_max_width, FCN_NULL },
49	{ "tabstops",	&db_tab_stop_width, FCN_NULL },
50};
51struct db_variable *db_evars = db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
52
53int
54db_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	return (0);
77}
78
79int
80db_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
93int
94db_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
108void
109db_read_variable(vp, valuep)
110	struct db_variable *vp;
111	db_expr_t	*valuep;
112{
113	int	(*func)() = vp->fcn;
114
115	if (func == FCN_NULL)
116	    *valuep = *(vp->valuep);
117	else
118	    (*func)(vp, valuep, DB_VAR_GET);
119}
120
121static void
122db_write_variable(vp, valuep)
123	struct db_variable *vp;
124	db_expr_t	*valuep;
125{
126	int	(*func)() = vp->fcn;
127
128	if (func == FCN_NULL)
129	    *(vp->valuep) = *valuep;
130	else
131	    (*func)(vp, valuep, DB_VAR_SET);
132}
133
134void
135db_set_cmd(db_expr_t dummy1, int dummy2, db_expr_t dummy3, char *dummy4)
136{
137	db_expr_t	value;
138	int	(*func)();
139	struct db_variable *vp;
140	int	t;
141
142	t = db_read_token();
143	if (t != tDOLLAR) {
144	    db_error("Unknown variable\n");
145	    return;
146	}
147	if (!db_find_variable(&vp)) {
148	    db_error("Unknown variable\n");
149	    return;
150	}
151
152	t = db_read_token();
153	if (t != tEQ)
154	    db_unread_token(t);
155
156	if (!db_expression(&value)) {
157	    db_error("No value\n");
158	    return;
159	}
160	if (db_read_token() != tEOL) {
161	    db_error("?\n");
162	}
163
164	db_write_variable(vp, &value);
165}
166