db_variables.c revision 139747
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: head/sys/ddb/db_variables.c 139747 2005-01-06 01:34:41Z imp $"); 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 }, 494Srgrimes}; 50131952Smarcelstatic struct db_variable *db_evars = 51131952Smarcel db_vars + sizeof(db_vars)/sizeof(db_vars[0]); 524Srgrimes 5312515Sphkstatic int 54131952Smarceldb_find_variable(struct db_variable **varp) 554Srgrimes{ 564Srgrimes struct db_variable *vp; 57131952Smarcel int t; 584Srgrimes 594Srgrimes t = db_read_token(); 604Srgrimes if (t == tIDENT) { 61131952Smarcel for (vp = db_vars; vp < db_evars; vp++) { 62131952Smarcel if (!strcmp(db_tok_string, vp->name)) { 63131952Smarcel *varp = vp; 64131952Smarcel return (1); 65131952Smarcel } 664Srgrimes } 67131952Smarcel for (vp = db_regs; vp < db_eregs; vp++) { 68131952Smarcel if (!strcmp(db_tok_string, vp->name)) { 69131952Smarcel *varp = vp; 70131952Smarcel return (1); 71131952Smarcel } 724Srgrimes } 734Srgrimes } 744Srgrimes db_error("Unknown variable\n"); 754Srgrimes return (0); 764Srgrimes} 774Srgrimes 784Srgrimesint 79131952Smarceldb_get_variable(db_expr_t *valuep) 804Srgrimes{ 814Srgrimes struct db_variable *vp; 824Srgrimes 834Srgrimes if (!db_find_variable(&vp)) 84131952Smarcel return (0); 854Srgrimes 86131952Smarcel return (db_read_variable(vp, valuep)); 874Srgrimes} 884Srgrimes 89131952Smarcelint 90131952Smarceldb_set_variable(db_expr_t value) 914Srgrimes{ 924Srgrimes struct db_variable *vp; 934Srgrimes 944Srgrimes if (!db_find_variable(&vp)) 95131952Smarcel return (0); 964Srgrimes 97131952Smarcel return (db_write_variable(vp, value)); 984Srgrimes} 994Srgrimes 100131952Smarcelint 101131952Smarceldb_read_variable(struct db_variable *vp, db_expr_t *valuep) 1024Srgrimes{ 103131952Smarcel db_varfcn_t *func = vp->fcn; 1044Srgrimes 105131952Smarcel if (func == FCN_NULL) { 106131952Smarcel *valuep = *(vp->valuep); 107131952Smarcel return (1); 108131952Smarcel } 109131952Smarcel return ((*func)(vp, valuep, DB_VAR_GET)); 1104Srgrimes} 1114Srgrimes 112131952Smarcelint 113131952Smarceldb_write_variable(struct db_variable *vp, db_expr_t value) 1144Srgrimes{ 115131952Smarcel db_varfcn_t *func = vp->fcn; 1164Srgrimes 117131952Smarcel if (func == FCN_NULL) { 118131952Smarcel *(vp->valuep) = value; 119131952Smarcel return (1); 120131952Smarcel } 121131952Smarcel return ((*func)(vp, &value, DB_VAR_SET)); 1224Srgrimes} 1234Srgrimes 1244Srgrimesvoid 125131952Smarceldb_set_cmd(db_expr_t dummy1, boolean_t dummy2, db_expr_t dummy3, char *dummy4) 1264Srgrimes{ 1274Srgrimes struct db_variable *vp; 128131952Smarcel db_expr_t value; 129131952Smarcel int t; 1304Srgrimes 1314Srgrimes t = db_read_token(); 1324Srgrimes if (t != tDOLLAR) { 133131952Smarcel db_error("Unknown variable\n"); 134131952Smarcel return; 1354Srgrimes } 1364Srgrimes if (!db_find_variable(&vp)) { 137131952Smarcel db_error("Unknown variable\n"); 138131952Smarcel return; 1394Srgrimes } 1404Srgrimes 1414Srgrimes t = db_read_token(); 1424Srgrimes if (t != tEQ) 143131952Smarcel db_unread_token(t); 1444Srgrimes 1454Srgrimes if (!db_expression(&value)) { 146131952Smarcel db_error("No value\n"); 147131952Smarcel return; 1484Srgrimes } 149131952Smarcel if (db_read_token() != tEOL) 150131952Smarcel db_error("?\n"); 1514Srgrimes 152131952Smarcel db_write_variable(vp, value); 1534Srgrimes} 154