1139747Simp/*- 24Srgrimes * Mach Operating System 34Srgrimes * Copyright (c) 1991,1990 Carnegie Mellon University 44Srgrimes * All Rights Reserved. 58876Srgrimes * 64Srgrimes * Permission to use, copy, modify and distribute this software and its 74Srgrimes * documentation is hereby granted, provided that both the copyright 84Srgrimes * notice and this permission notice appear in all copies of the 94Srgrimes * software, derivative works or modified versions, and any portions 104Srgrimes * thereof, and that both notices appear in supporting documentation. 118876Srgrimes * 128876Srgrimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 134Srgrimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 144Srgrimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 158876Srgrimes * 164Srgrimes * Carnegie Mellon requests users of this software to return to 178876Srgrimes * 184Srgrimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 194Srgrimes * School of Computer Science 204Srgrimes * Carnegie Mellon University 214Srgrimes * Pittsburgh PA 15213-3890 228876Srgrimes * 234Srgrimes * any improvements or extensions that they make and grant Carnegie the 244Srgrimes * rights to redistribute these changes. 254Srgrimes */ 264Srgrimes/* 274Srgrimes * Author: David B. Golub, Carnegie Mellon University 284Srgrimes * Date: 7/90 294Srgrimes */ 304Srgrimes 31116176Sobrien#include <sys/cdefs.h> 32116176Sobrien__FBSDID("$FreeBSD$"); 33116176Sobrien 342056Swollman#include <sys/param.h> 3547098Sbde#include <sys/systm.h> 3612734Sbde 372056Swollman#include <ddb/ddb.h> 384Srgrimes#include <ddb/db_lex.h> 394Srgrimes#include <ddb/db_variables.h> 404Srgrimes 4192756Salfredstatic int db_find_variable(struct db_variable **varp); 424Srgrimes 4312515Sphkstatic struct db_variable db_vars[] = { 444Srgrimes { "radix", &db_radix, FCN_NULL }, 4537504Sbde { "maxoff", &db_maxoff, FCN_NULL }, 464Srgrimes { "maxwidth", &db_max_width, FCN_NULL }, 474Srgrimes { "tabstops", &db_tab_stop_width, FCN_NULL }, 48137117Sjhb { "lines", &db_lines_per_page, FCN_NULL }, 49195699Srwatson { "curcpu", NULL, db_var_curcpu }, 50195699Srwatson { "db_cpu", NULL, db_var_db_cpu }, 51195699Srwatson#ifdef VIMAGE 52195699Srwatson { "curvnet", NULL, db_var_curvnet }, 53195699Srwatson { "db_vnet", NULL, db_var_db_vnet }, 54195699Srwatson#endif 554Srgrimes}; 56131952Smarcelstatic struct db_variable *db_evars = 57131952Smarcel db_vars + sizeof(db_vars)/sizeof(db_vars[0]); 584Srgrimes 5912515Sphkstatic int 60131952Smarceldb_find_variable(struct db_variable **varp) 614Srgrimes{ 624Srgrimes struct db_variable *vp; 63131952Smarcel int t; 644Srgrimes 654Srgrimes t = db_read_token(); 664Srgrimes if (t == tIDENT) { 67131952Smarcel for (vp = db_vars; vp < db_evars; vp++) { 68131952Smarcel if (!strcmp(db_tok_string, vp->name)) { 69131952Smarcel *varp = vp; 70131952Smarcel return (1); 71131952Smarcel } 724Srgrimes } 73131952Smarcel for (vp = db_regs; vp < db_eregs; vp++) { 74131952Smarcel if (!strcmp(db_tok_string, vp->name)) { 75131952Smarcel *varp = vp; 76131952Smarcel return (1); 77131952Smarcel } 784Srgrimes } 794Srgrimes } 804Srgrimes db_error("Unknown variable\n"); 814Srgrimes return (0); 824Srgrimes} 834Srgrimes 844Srgrimesint 85131952Smarceldb_get_variable(db_expr_t *valuep) 864Srgrimes{ 874Srgrimes struct db_variable *vp; 884Srgrimes 894Srgrimes if (!db_find_variable(&vp)) 90131952Smarcel return (0); 914Srgrimes 92131952Smarcel return (db_read_variable(vp, valuep)); 934Srgrimes} 944Srgrimes 95131952Smarcelint 96131952Smarceldb_set_variable(db_expr_t value) 974Srgrimes{ 984Srgrimes struct db_variable *vp; 994Srgrimes 1004Srgrimes if (!db_find_variable(&vp)) 101131952Smarcel return (0); 1024Srgrimes 103131952Smarcel return (db_write_variable(vp, value)); 1044Srgrimes} 1054Srgrimes 106131952Smarcelint 107131952Smarceldb_read_variable(struct db_variable *vp, db_expr_t *valuep) 1084Srgrimes{ 109131952Smarcel db_varfcn_t *func = vp->fcn; 1104Srgrimes 111131952Smarcel if (func == FCN_NULL) { 112131952Smarcel *valuep = *(vp->valuep); 113131952Smarcel return (1); 114131952Smarcel } 115131952Smarcel return ((*func)(vp, valuep, DB_VAR_GET)); 1164Srgrimes} 1174Srgrimes 118131952Smarcelint 119131952Smarceldb_write_variable(struct db_variable *vp, db_expr_t value) 1204Srgrimes{ 121131952Smarcel db_varfcn_t *func = vp->fcn; 1224Srgrimes 123131952Smarcel if (func == FCN_NULL) { 124131952Smarcel *(vp->valuep) = value; 125131952Smarcel return (1); 126131952Smarcel } 127131952Smarcel return ((*func)(vp, &value, DB_VAR_SET)); 1284Srgrimes} 1294Srgrimes 1304Srgrimesvoid 131131952Smarceldb_set_cmd(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 1324Srgrimes{ 1334Srgrimes struct db_variable *vp; 134131952Smarcel db_expr_t value; 135131952Smarcel int t; 1364Srgrimes 1374Srgrimes t = db_read_token(); 1384Srgrimes if (t != tDOLLAR) { 139131952Smarcel db_error("Unknown variable\n"); 140131952Smarcel return; 1414Srgrimes } 1424Srgrimes if (!db_find_variable(&vp)) { 143131952Smarcel db_error("Unknown variable\n"); 144131952Smarcel return; 1454Srgrimes } 1464Srgrimes 1474Srgrimes t = db_read_token(); 1484Srgrimes if (t != tEQ) 149131952Smarcel db_unread_token(t); 1504Srgrimes 1514Srgrimes if (!db_expression(&value)) { 152131952Smarcel db_error("No value\n"); 153131952Smarcel return; 1544Srgrimes } 155131952Smarcel if (db_read_token() != tEOL) 156131952Smarcel db_error("?\n"); 1574Srgrimes 158131952Smarcel db_write_variable(vp, value); 1594Srgrimes} 160