1dd3cb568SWarner Losh /*-
2*796df753SPedro F. Giffuni * SPDX-License-Identifier: MIT-CMU
3*796df753SPedro F. Giffuni *
45b81b6b3SRodney W. Grimes * Mach Operating System
55b81b6b3SRodney W. Grimes * Copyright (c) 1991,1990 Carnegie Mellon University
65b81b6b3SRodney W. Grimes * All Rights Reserved.
75b81b6b3SRodney W. Grimes *
85b81b6b3SRodney W. Grimes * Permission to use, copy, modify and distribute this software and its
95b81b6b3SRodney W. Grimes * documentation is hereby granted, provided that both the copyright
105b81b6b3SRodney W. Grimes * notice and this permission notice appear in all copies of the
115b81b6b3SRodney W. Grimes * software, derivative works or modified versions, and any portions
125b81b6b3SRodney W. Grimes * thereof, and that both notices appear in supporting documentation.
135b81b6b3SRodney W. Grimes *
145b81b6b3SRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
155b81b6b3SRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
165b81b6b3SRodney W. Grimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
175b81b6b3SRodney W. Grimes *
185b81b6b3SRodney W. Grimes * Carnegie Mellon requests users of this software to return to
195b81b6b3SRodney W. Grimes *
205b81b6b3SRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
215b81b6b3SRodney W. Grimes * School of Computer Science
225b81b6b3SRodney W. Grimes * Carnegie Mellon University
235b81b6b3SRodney W. Grimes * Pittsburgh PA 15213-3890
245b81b6b3SRodney W. Grimes *
255b81b6b3SRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the
265b81b6b3SRodney W. Grimes * rights to redistribute these changes.
275b81b6b3SRodney W. Grimes */
285b81b6b3SRodney W. Grimes /*
295b81b6b3SRodney W. Grimes * Author: David B. Golub, Carnegie Mellon University
305b81b6b3SRodney W. Grimes * Date: 7/90
315b81b6b3SRodney W. Grimes */
325b81b6b3SRodney W. Grimes
33f540b106SGarrett Wollman #include <sys/param.h>
3407f6cad7SBruce Evans #include <sys/systm.h>
355b81b6b3SRodney W. Grimes
365ccbc3ccSBruce Evans #include <ddb/ddb.h>
375b81b6b3SRodney W. Grimes #include <ddb/db_lex.h>
385b81b6b3SRodney W. Grimes #include <ddb/db_variables.h>
395b81b6b3SRodney W. Grimes
4014e10f99SAlfred Perlstein static int db_find_variable(struct db_variable **varp);
41f73a856dSPoul-Henning Kamp
42f73a856dSPoul-Henning Kamp static struct db_variable db_vars[] = {
435b81b6b3SRodney W. Grimes { "radix", &db_radix, FCN_NULL },
443da6ef3cSBruce Evans { "maxoff", &db_maxoff, FCN_NULL },
455b81b6b3SRodney W. Grimes { "maxwidth", &db_max_width, FCN_NULL },
465b81b6b3SRodney W. Grimes { "tabstops", &db_tab_stop_width, FCN_NULL },
47d39d4a6eSJohn Baldwin { "lines", &db_lines_per_page, FCN_NULL },
48eddfbb76SRobert Watson { "curcpu", NULL, db_var_curcpu },
49eddfbb76SRobert Watson { "db_cpu", NULL, db_var_db_cpu },
50eddfbb76SRobert Watson #ifdef VIMAGE
51eddfbb76SRobert Watson { "curvnet", NULL, db_var_curvnet },
52eddfbb76SRobert Watson { "db_vnet", NULL, db_var_db_vnet },
53eddfbb76SRobert Watson #endif
545b81b6b3SRodney W. Grimes };
5563b6b7a7SPedro F. Giffuni static struct db_variable *db_evars = db_vars + nitems(db_vars);
565b81b6b3SRodney W. Grimes
57f73a856dSPoul-Henning Kamp static int
db_find_variable(struct db_variable ** varp)5837224cd3SMarcel Moolenaar db_find_variable(struct db_variable **varp)
595b81b6b3SRodney W. Grimes {
605b81b6b3SRodney W. Grimes struct db_variable *vp;
6137224cd3SMarcel Moolenaar int t;
625b81b6b3SRodney W. Grimes
635b81b6b3SRodney W. Grimes t = db_read_token();
645b81b6b3SRodney W. Grimes if (t == tIDENT) {
655b81b6b3SRodney W. Grimes for (vp = db_vars; vp < db_evars; vp++) {
665b81b6b3SRodney W. Grimes if (!strcmp(db_tok_string, vp->name)) {
675b81b6b3SRodney W. Grimes *varp = vp;
685b81b6b3SRodney W. Grimes return (1);
695b81b6b3SRodney W. Grimes }
705b81b6b3SRodney W. Grimes }
715b81b6b3SRodney W. Grimes for (vp = db_regs; vp < db_eregs; vp++) {
725b81b6b3SRodney W. Grimes if (!strcmp(db_tok_string, vp->name)) {
735b81b6b3SRodney W. Grimes *varp = vp;
745b81b6b3SRodney W. Grimes return (1);
755b81b6b3SRodney W. Grimes }
765b81b6b3SRodney W. Grimes }
775b81b6b3SRodney W. Grimes }
785b81b6b3SRodney W. Grimes db_error("Unknown variable\n");
795b81b6b3SRodney W. Grimes return (0);
805b81b6b3SRodney W. Grimes }
815b81b6b3SRodney W. Grimes
825b81b6b3SRodney W. Grimes int
db_get_variable(db_expr_t * valuep)8337224cd3SMarcel Moolenaar db_get_variable(db_expr_t *valuep)
845b81b6b3SRodney W. Grimes {
855b81b6b3SRodney W. Grimes struct db_variable *vp;
865b81b6b3SRodney W. Grimes
875b81b6b3SRodney W. Grimes if (!db_find_variable(&vp))
885b81b6b3SRodney W. Grimes return (0);
895b81b6b3SRodney W. Grimes
9037224cd3SMarcel Moolenaar return (db_read_variable(vp, valuep));
915b81b6b3SRodney W. Grimes }
925b81b6b3SRodney W. Grimes
9337224cd3SMarcel Moolenaar int
db_set_variable(db_expr_t value)9437224cd3SMarcel Moolenaar db_set_variable(db_expr_t value)
955b81b6b3SRodney W. Grimes {
965b81b6b3SRodney W. Grimes struct db_variable *vp;
975b81b6b3SRodney W. Grimes
985b81b6b3SRodney W. Grimes if (!db_find_variable(&vp))
995b81b6b3SRodney W. Grimes return (0);
1005b81b6b3SRodney W. Grimes
10137224cd3SMarcel Moolenaar return (db_write_variable(vp, value));
1025b81b6b3SRodney W. Grimes }
1035b81b6b3SRodney W. Grimes
10437224cd3SMarcel Moolenaar int
db_read_variable(struct db_variable * vp,db_expr_t * valuep)10537224cd3SMarcel Moolenaar db_read_variable(struct db_variable *vp, db_expr_t *valuep)
1065b81b6b3SRodney W. Grimes {
107058284fcSBruce Evans db_varfcn_t *func = vp->fcn;
1085b81b6b3SRodney W. Grimes
10937224cd3SMarcel Moolenaar if (func == FCN_NULL) {
1105b81b6b3SRodney W. Grimes *valuep = *(vp->valuep);
11137224cd3SMarcel Moolenaar return (1);
11237224cd3SMarcel Moolenaar }
11337224cd3SMarcel Moolenaar return ((*func)(vp, valuep, DB_VAR_GET));
1145b81b6b3SRodney W. Grimes }
1155b81b6b3SRodney W. Grimes
11637224cd3SMarcel Moolenaar int
db_write_variable(struct db_variable * vp,db_expr_t value)11737224cd3SMarcel Moolenaar db_write_variable(struct db_variable *vp, db_expr_t value)
1185b81b6b3SRodney W. Grimes {
119058284fcSBruce Evans db_varfcn_t *func = vp->fcn;
1205b81b6b3SRodney W. Grimes
12137224cd3SMarcel Moolenaar if (func == FCN_NULL) {
12237224cd3SMarcel Moolenaar *(vp->valuep) = value;
12337224cd3SMarcel Moolenaar return (1);
12437224cd3SMarcel Moolenaar }
12537224cd3SMarcel Moolenaar return ((*func)(vp, &value, DB_VAR_SET));
1265b81b6b3SRodney W. Grimes }
1275b81b6b3SRodney W. Grimes
1285b81b6b3SRodney W. Grimes void
db_set_cmd(db_expr_t dummy1,bool dummy2,db_expr_t dummy3,char * dummy4)129cd508278SPedro F. Giffuni db_set_cmd(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4)
1305b81b6b3SRodney W. Grimes {
1315b81b6b3SRodney W. Grimes struct db_variable *vp;
13237224cd3SMarcel Moolenaar db_expr_t value;
1335b81b6b3SRodney W. Grimes int t;
1345b81b6b3SRodney W. Grimes
1355b81b6b3SRodney W. Grimes t = db_read_token();
13622767031SEdward Tomasz Napierala if (t == tEOL) {
13722767031SEdward Tomasz Napierala for (vp = db_vars; vp < db_evars; vp++) {
13822767031SEdward Tomasz Napierala if (!db_read_variable(vp, &value)) {
13922767031SEdward Tomasz Napierala db_printf("$%s\n", vp->name);
14022767031SEdward Tomasz Napierala continue;
14122767031SEdward Tomasz Napierala }
14222767031SEdward Tomasz Napierala db_printf("$%-8s = %ld\n",
14322767031SEdward Tomasz Napierala vp->name, (unsigned long)value);
14422767031SEdward Tomasz Napierala }
14522767031SEdward Tomasz Napierala return;
14622767031SEdward Tomasz Napierala }
1475b81b6b3SRodney W. Grimes if (t != tDOLLAR) {
1485b81b6b3SRodney W. Grimes db_error("Unknown variable\n");
1495b81b6b3SRodney W. Grimes return;
1505b81b6b3SRodney W. Grimes }
1515b81b6b3SRodney W. Grimes if (!db_find_variable(&vp)) {
1525b81b6b3SRodney W. Grimes db_error("Unknown variable\n");
1535b81b6b3SRodney W. Grimes return;
1545b81b6b3SRodney W. Grimes }
1555b81b6b3SRodney W. Grimes
1565b81b6b3SRodney W. Grimes t = db_read_token();
1575b81b6b3SRodney W. Grimes if (t != tEQ)
1585b81b6b3SRodney W. Grimes db_unread_token(t);
1595b81b6b3SRodney W. Grimes
1605b81b6b3SRodney W. Grimes if (!db_expression(&value)) {
1615b81b6b3SRodney W. Grimes db_error("No value\n");
1625b81b6b3SRodney W. Grimes return;
1635b81b6b3SRodney W. Grimes }
16437224cd3SMarcel Moolenaar if (db_read_token() != tEOL)
1655b81b6b3SRodney W. Grimes db_error("?\n");
1665b81b6b3SRodney W. Grimes
16737224cd3SMarcel Moolenaar db_write_variable(vp, value);
1685b81b6b3SRodney W. Grimes }
169