xref: /freebsd/sys/ddb/db_variables.c (revision 22767031082dd3f46ff87d00ebcf9bafd1fa4ddb)
1dd3cb568SWarner Losh /*-
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
245b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
255b81b6b3SRodney W. Grimes  */
265b81b6b3SRodney W. Grimes /*
275b81b6b3SRodney W. Grimes  * 	Author: David B. Golub, Carnegie Mellon University
285b81b6b3SRodney W. Grimes  *	Date:	7/90
295b81b6b3SRodney W. Grimes  */
305b81b6b3SRodney W. Grimes 
31753960f7SDavid E. O'Brien #include <sys/cdefs.h>
32753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
33753960f7SDavid E. O'Brien 
34f540b106SGarrett Wollman #include <sys/param.h>
3507f6cad7SBruce Evans #include <sys/systm.h>
365b81b6b3SRodney W. Grimes 
375ccbc3ccSBruce Evans #include <ddb/ddb.h>
385b81b6b3SRodney W. Grimes #include <ddb/db_lex.h>
395b81b6b3SRodney W. Grimes #include <ddb/db_variables.h>
405b81b6b3SRodney W. Grimes 
4114e10f99SAlfred Perlstein static int	db_find_variable(struct db_variable **varp);
42f73a856dSPoul-Henning Kamp 
43f73a856dSPoul-Henning Kamp static struct db_variable db_vars[] = {
445b81b6b3SRodney W. Grimes 	{ "radix",	&db_radix, FCN_NULL },
453da6ef3cSBruce Evans 	{ "maxoff",	&db_maxoff, FCN_NULL },
465b81b6b3SRodney W. Grimes 	{ "maxwidth",	&db_max_width, FCN_NULL },
475b81b6b3SRodney W. Grimes 	{ "tabstops",	&db_tab_stop_width, FCN_NULL },
48d39d4a6eSJohn Baldwin 	{ "lines",	&db_lines_per_page, FCN_NULL },
49eddfbb76SRobert Watson 	{ "curcpu",	NULL, db_var_curcpu },
50eddfbb76SRobert Watson 	{ "db_cpu",	NULL, db_var_db_cpu },
51eddfbb76SRobert Watson #ifdef VIMAGE
52eddfbb76SRobert Watson 	{ "curvnet",	NULL, db_var_curvnet },
53eddfbb76SRobert Watson 	{ "db_vnet",	NULL, db_var_db_vnet },
54eddfbb76SRobert Watson #endif
555b81b6b3SRodney W. Grimes };
5663b6b7a7SPedro F. Giffuni static struct db_variable *db_evars = db_vars + nitems(db_vars);
575b81b6b3SRodney W. Grimes 
58f73a856dSPoul-Henning Kamp static int
5937224cd3SMarcel Moolenaar db_find_variable(struct db_variable **varp)
605b81b6b3SRodney W. Grimes {
615b81b6b3SRodney W. Grimes 	struct db_variable *vp;
6237224cd3SMarcel Moolenaar 	int t;
635b81b6b3SRodney W. Grimes 
645b81b6b3SRodney W. Grimes 	t = db_read_token();
655b81b6b3SRodney W. Grimes 	if (t == tIDENT) {
665b81b6b3SRodney W. Grimes 		for (vp = db_vars; vp < db_evars; vp++) {
675b81b6b3SRodney W. Grimes 			if (!strcmp(db_tok_string, vp->name)) {
685b81b6b3SRodney W. Grimes 				*varp = vp;
695b81b6b3SRodney W. Grimes 				return (1);
705b81b6b3SRodney W. Grimes 			}
715b81b6b3SRodney W. Grimes 		}
725b81b6b3SRodney W. Grimes 		for (vp = db_regs; vp < db_eregs; vp++) {
735b81b6b3SRodney W. Grimes 			if (!strcmp(db_tok_string, vp->name)) {
745b81b6b3SRodney W. Grimes 				*varp = vp;
755b81b6b3SRodney W. Grimes 				return (1);
765b81b6b3SRodney W. Grimes 			}
775b81b6b3SRodney W. Grimes 		}
785b81b6b3SRodney W. Grimes 	}
795b81b6b3SRodney W. Grimes 	db_error("Unknown variable\n");
805b81b6b3SRodney W. Grimes 	return (0);
815b81b6b3SRodney W. Grimes }
825b81b6b3SRodney W. Grimes 
835b81b6b3SRodney W. Grimes int
8437224cd3SMarcel Moolenaar db_get_variable(db_expr_t *valuep)
855b81b6b3SRodney W. Grimes {
865b81b6b3SRodney W. Grimes 	struct db_variable *vp;
875b81b6b3SRodney W. Grimes 
885b81b6b3SRodney W. Grimes 	if (!db_find_variable(&vp))
895b81b6b3SRodney W. Grimes 		return (0);
905b81b6b3SRodney W. Grimes 
9137224cd3SMarcel Moolenaar 	return (db_read_variable(vp, valuep));
925b81b6b3SRodney W. Grimes }
935b81b6b3SRodney W. Grimes 
9437224cd3SMarcel Moolenaar int
9537224cd3SMarcel Moolenaar db_set_variable(db_expr_t value)
965b81b6b3SRodney W. Grimes {
975b81b6b3SRodney W. Grimes 	struct db_variable *vp;
985b81b6b3SRodney W. Grimes 
995b81b6b3SRodney W. Grimes 	if (!db_find_variable(&vp))
1005b81b6b3SRodney W. Grimes 		return (0);
1015b81b6b3SRodney W. Grimes 
10237224cd3SMarcel Moolenaar 	return (db_write_variable(vp, value));
1035b81b6b3SRodney W. Grimes }
1045b81b6b3SRodney W. Grimes 
10537224cd3SMarcel Moolenaar int
10637224cd3SMarcel Moolenaar db_read_variable(struct db_variable *vp, db_expr_t *valuep)
1075b81b6b3SRodney W. Grimes {
108058284fcSBruce Evans 	db_varfcn_t *func = vp->fcn;
1095b81b6b3SRodney W. Grimes 
11037224cd3SMarcel Moolenaar 	if (func == FCN_NULL) {
1115b81b6b3SRodney W. Grimes 		*valuep = *(vp->valuep);
11237224cd3SMarcel Moolenaar 		return (1);
11337224cd3SMarcel Moolenaar 	}
11437224cd3SMarcel Moolenaar 	return ((*func)(vp, valuep, DB_VAR_GET));
1155b81b6b3SRodney W. Grimes }
1165b81b6b3SRodney W. Grimes 
11737224cd3SMarcel Moolenaar int
11837224cd3SMarcel Moolenaar db_write_variable(struct db_variable *vp, db_expr_t value)
1195b81b6b3SRodney W. Grimes {
120058284fcSBruce Evans 	db_varfcn_t *func = vp->fcn;
1215b81b6b3SRodney W. Grimes 
12237224cd3SMarcel Moolenaar 	if (func == FCN_NULL) {
12337224cd3SMarcel Moolenaar 		*(vp->valuep) = value;
12437224cd3SMarcel Moolenaar 		return (1);
12537224cd3SMarcel Moolenaar 	}
12637224cd3SMarcel Moolenaar 	return ((*func)(vp, &value, DB_VAR_SET));
1275b81b6b3SRodney W. Grimes }
1285b81b6b3SRodney W. Grimes 
1295b81b6b3SRodney W. Grimes void
130cd508278SPedro F. Giffuni db_set_cmd(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
1315b81b6b3SRodney W. Grimes {
1325b81b6b3SRodney W. Grimes 	struct db_variable *vp;
13337224cd3SMarcel Moolenaar 	db_expr_t value;
1345b81b6b3SRodney W. Grimes 	int t;
1355b81b6b3SRodney W. Grimes 
1365b81b6b3SRodney W. Grimes 	t = db_read_token();
137*22767031SEdward Tomasz Napierala 	if (t == tEOL) {
138*22767031SEdward Tomasz Napierala 		for (vp = db_vars; vp < db_evars; vp++) {
139*22767031SEdward Tomasz Napierala 			if (!db_read_variable(vp, &value)) {
140*22767031SEdward Tomasz Napierala 				db_printf("$%s\n", vp->name);
141*22767031SEdward Tomasz Napierala 				continue;
142*22767031SEdward Tomasz Napierala 			}
143*22767031SEdward Tomasz Napierala 			db_printf("$%-8s = %ld\n",
144*22767031SEdward Tomasz Napierala 			    vp->name, (unsigned long)value);
145*22767031SEdward Tomasz Napierala 		}
146*22767031SEdward Tomasz Napierala 		return;
147*22767031SEdward Tomasz Napierala 	}
1485b81b6b3SRodney W. Grimes 	if (t != tDOLLAR) {
1495b81b6b3SRodney W. Grimes 		db_error("Unknown variable\n");
1505b81b6b3SRodney W. Grimes 		return;
1515b81b6b3SRodney W. Grimes 	}
1525b81b6b3SRodney W. Grimes 	if (!db_find_variable(&vp)) {
1535b81b6b3SRodney W. Grimes 		db_error("Unknown variable\n");
1545b81b6b3SRodney W. Grimes 		return;
1555b81b6b3SRodney W. Grimes 	}
1565b81b6b3SRodney W. Grimes 
1575b81b6b3SRodney W. Grimes 	t = db_read_token();
1585b81b6b3SRodney W. Grimes 	if (t != tEQ)
1595b81b6b3SRodney W. Grimes 		db_unread_token(t);
1605b81b6b3SRodney W. Grimes 
1615b81b6b3SRodney W. Grimes 	if (!db_expression(&value)) {
1625b81b6b3SRodney W. Grimes 		db_error("No value\n");
1635b81b6b3SRodney W. Grimes 		return;
1645b81b6b3SRodney W. Grimes 	}
16537224cd3SMarcel Moolenaar 	if (db_read_token() != tEOL)
1665b81b6b3SRodney W. Grimes 		db_error("?\n");
1675b81b6b3SRodney W. Grimes 
16837224cd3SMarcel Moolenaar 	db_write_variable(vp, value);
1695b81b6b3SRodney W. Grimes }
170