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: releng/11.0/sys/ddb/db_variables.c 298354 2016-04-20 16:19:44Z pfg $"); 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}; 56298354Spfgstatic struct db_variable *db_evars = db_vars + nitems(db_vars); 574Srgrimes 5812515Sphkstatic int 59131952Smarceldb_find_variable(struct db_variable **varp) 604Srgrimes{ 614Srgrimes struct db_variable *vp; 62131952Smarcel int t; 634Srgrimes 644Srgrimes t = db_read_token(); 654Srgrimes if (t == tIDENT) { 66131952Smarcel for (vp = db_vars; vp < db_evars; vp++) { 67131952Smarcel if (!strcmp(db_tok_string, vp->name)) { 68131952Smarcel *varp = vp; 69131952Smarcel return (1); 70131952Smarcel } 714Srgrimes } 72131952Smarcel for (vp = db_regs; vp < db_eregs; vp++) { 73131952Smarcel if (!strcmp(db_tok_string, vp->name)) { 74131952Smarcel *varp = vp; 75131952Smarcel return (1); 76131952Smarcel } 774Srgrimes } 784Srgrimes } 794Srgrimes db_error("Unknown variable\n"); 804Srgrimes return (0); 814Srgrimes} 824Srgrimes 834Srgrimesint 84131952Smarceldb_get_variable(db_expr_t *valuep) 854Srgrimes{ 864Srgrimes struct db_variable *vp; 874Srgrimes 884Srgrimes if (!db_find_variable(&vp)) 89131952Smarcel return (0); 904Srgrimes 91131952Smarcel return (db_read_variable(vp, valuep)); 924Srgrimes} 934Srgrimes 94131952Smarcelint 95131952Smarceldb_set_variable(db_expr_t value) 964Srgrimes{ 974Srgrimes struct db_variable *vp; 984Srgrimes 994Srgrimes if (!db_find_variable(&vp)) 100131952Smarcel return (0); 1014Srgrimes 102131952Smarcel return (db_write_variable(vp, value)); 1034Srgrimes} 1044Srgrimes 105131952Smarcelint 106131952Smarceldb_read_variable(struct db_variable *vp, db_expr_t *valuep) 1074Srgrimes{ 108131952Smarcel db_varfcn_t *func = vp->fcn; 1094Srgrimes 110131952Smarcel if (func == FCN_NULL) { 111131952Smarcel *valuep = *(vp->valuep); 112131952Smarcel return (1); 113131952Smarcel } 114131952Smarcel return ((*func)(vp, valuep, DB_VAR_GET)); 1154Srgrimes} 1164Srgrimes 117131952Smarcelint 118131952Smarceldb_write_variable(struct db_variable *vp, db_expr_t value) 1194Srgrimes{ 120131952Smarcel db_varfcn_t *func = vp->fcn; 1214Srgrimes 122131952Smarcel if (func == FCN_NULL) { 123131952Smarcel *(vp->valuep) = value; 124131952Smarcel return (1); 125131952Smarcel } 126131952Smarcel return ((*func)(vp, &value, DB_VAR_SET)); 1274Srgrimes} 1284Srgrimes 1294Srgrimesvoid 130283248Spfgdb_set_cmd(db_expr_t dummy1, bool dummy2, db_expr_t dummy3, char *dummy4) 1314Srgrimes{ 1324Srgrimes struct db_variable *vp; 133131952Smarcel db_expr_t value; 134131952Smarcel int t; 1354Srgrimes 1364Srgrimes t = db_read_token(); 1374Srgrimes if (t != tDOLLAR) { 138131952Smarcel db_error("Unknown variable\n"); 139131952Smarcel return; 1404Srgrimes } 1414Srgrimes if (!db_find_variable(&vp)) { 142131952Smarcel db_error("Unknown variable\n"); 143131952Smarcel return; 1444Srgrimes } 1454Srgrimes 1464Srgrimes t = db_read_token(); 1474Srgrimes if (t != tEQ) 148131952Smarcel db_unread_token(t); 1494Srgrimes 1504Srgrimes if (!db_expression(&value)) { 151131952Smarcel db_error("No value\n"); 152131952Smarcel return; 1534Srgrimes } 154131952Smarcel if (db_read_token() != tEOL) 155131952Smarcel db_error("?\n"); 1564Srgrimes 157131952Smarcel db_write_variable(vp, value); 1584Srgrimes} 159