f88690181263c6f7c0ab669f9d27ef642addc648
[dragonfly.git] / sys / ddb / db_variables.c
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  * $FreeBSD: src/sys/ddb/db_variables.c,v 1.18 1999/08/28 00:41:11 peter Exp $
27  * $DragonFly: src/sys/ddb/db_variables.c,v 1.4 2005/12/23 21:35:44 swildner Exp $
28  */
29
30 /*
31  *      Author: David B. Golub, Carnegie Mellon University
32  *      Date:   7/90
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37
38 #include <ddb/ddb.h>
39 #include <ddb/db_lex.h>
40 #include <ddb/db_variables.h>
41
42 static int      db_find_variable (struct db_variable **varp);
43 static void     db_write_variable (struct db_variable *, db_expr_t *);
44
45 #ifdef notused
46 static int      db_set_variable (db_expr_t value);
47 #endif
48
49 static struct db_variable db_vars[] = {
50         { "radix",      &db_radix, FCN_NULL },
51         { "maxoff",     &db_maxoff, FCN_NULL },
52         { "maxwidth",   &db_max_width, FCN_NULL },
53         { "tabstops",   &db_tab_stop_width, FCN_NULL },
54 };
55 static struct db_variable *db_evars = 
56                 db_vars + sizeof(db_vars)/sizeof(db_vars[0]);
57
58 static int
59 db_find_variable(struct db_variable **varp)
60 {
61         int     t;
62         struct db_variable *vp;
63
64         t = db_read_token();
65         if (t == tIDENT) {
66             for (vp = db_vars; vp < db_evars; vp++) {
67                 if (!strcmp(db_tok_string, vp->name)) {
68                     *varp = vp;
69                     return (1);
70                 }
71             }
72             for (vp = db_regs; vp < db_eregs; vp++) {
73                 if (!strcmp(db_tok_string, vp->name)) {
74                     *varp = vp;
75                     return (1);
76                 }
77             }
78         }
79         db_error("Unknown variable\n");
80         return (0);
81 }
82
83 int
84 db_get_variable(db_expr_t *valuep)
85 {
86         struct db_variable *vp;
87
88         if (!db_find_variable(&vp))
89             return (0);
90
91         db_read_variable(vp, valuep);
92
93         return (1);
94 }
95
96 #ifdef notused
97 static int
98 db_set_variable(db_expr_t value)
99 {
100         struct db_variable *vp;
101
102         if (!db_find_variable(&vp))
103             return (0);
104
105         db_write_variable(vp, &value);
106
107         return (1);
108 }
109 #endif
110
111 void
112 db_read_variable(struct db_variable *vp, db_expr_t *valuep)
113 {
114         db_varfcn_t     *func = vp->fcn;
115
116         if (func == FCN_NULL)
117             *valuep = *(vp->valuep);
118         else
119             (*func)(vp, valuep, DB_VAR_GET);
120 }
121
122 static void
123 db_write_variable(struct db_variable *vp, db_expr_t *valuep)
124 {
125         db_varfcn_t     *func = vp->fcn;
126
127         if (func == FCN_NULL)
128             *(vp->valuep) = *valuep;
129         else
130             (*func)(vp, valuep, DB_VAR_SET);
131 }
132
133 void
134 db_set_cmd(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4)
135 {
136         db_expr_t       value;
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             return;
144         }
145         if (!db_find_variable(&vp)) {
146             db_error("Unknown variable\n");
147             return;
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             return;
157         }
158         if (db_read_token() != tEOL) {
159             db_error("?\n");
160         }
161
162         db_write_variable(vp, &value);
163 }