db_variables.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* $OpenBSD: db_variables.c,v 1.17 2015/03/14 05:48:17 tedu Exp $ */
  2. /* $NetBSD: db_variables.c,v 1.8 1996/02/05 01:57:19 christos Exp $ */
  3. /*
  4. * Mach Operating System
  5. * Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University
  6. * All Rights Reserved.
  7. *
  8. * Permission to use, copy, modify and distribute this software and its
  9. * documentation is hereby granted, provided that both the copyright
  10. * notice and this permission notice appear in all copies of the
  11. * software, derivative works or modified versions, and any portions
  12. * thereof, and that both notices appear in supporting documentation.
  13. *
  14. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  15. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  16. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  17. *
  18. * Carnegie Mellon requests users of this software to return to
  19. *
  20. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  21. * School of Computer Science
  22. * Carnegie Mellon University
  23. * Pittsburgh PA 15213-3890
  24. *
  25. * any improvements or extensions that they make and grant Carnegie Mellon
  26. * the rights to redistribute these changes.
  27. */
  28. #include <sys/param.h>
  29. #include <sys/systm.h>
  30. #include <machine/db_machdep.h>
  31. #include <ddb/db_lex.h>
  32. #include <ddb/db_variables.h>
  33. #include <ddb/db_command.h>
  34. #include <ddb/db_sym.h>
  35. #include <ddb/db_extern.h>
  36. #include <ddb/db_var.h>
  37. struct db_variable db_vars[] = {
  38. { "radix", (long *)&db_radix, db_var_rw_int },
  39. { "maxoff", (long *)&db_maxoff, db_var_rw_int },
  40. { "maxwidth", (long *)&db_max_width, db_var_rw_int },
  41. { "tabstops", (long *)&db_tab_stop_width, db_var_rw_int },
  42. { "lines", (long *)&db_max_line, db_var_rw_int },
  43. { "log", (long *)&db_log, db_var_rw_int }
  44. };
  45. struct db_variable *db_evars = db_vars + nitems(db_vars);
  46. int
  47. db_find_variable(struct db_variable **varp)
  48. {
  49. int t;
  50. struct db_variable *vp;
  51. t = db_read_token();
  52. if (t == tIDENT) {
  53. for (vp = db_vars; vp < db_evars; vp++) {
  54. if (!strcmp(db_tok_string, vp->name)) {
  55. *varp = vp;
  56. return (1);
  57. }
  58. }
  59. for (vp = db_regs; vp < db_eregs; vp++) {
  60. if (!strcmp(db_tok_string, vp->name)) {
  61. *varp = vp;
  62. return (1);
  63. }
  64. }
  65. }
  66. db_error("Unknown variable\n");
  67. /*NOTREACHED*/
  68. return 0;
  69. }
  70. int
  71. db_get_variable(db_expr_t *valuep)
  72. {
  73. struct db_variable *vp;
  74. if (!db_find_variable(&vp))
  75. return (0);
  76. db_read_variable(vp, valuep);
  77. return (1);
  78. }
  79. int
  80. db_set_variable(db_expr_t value)
  81. {
  82. struct db_variable *vp;
  83. if (!db_find_variable(&vp))
  84. return (0);
  85. db_write_variable(vp, &value);
  86. return (1);
  87. }
  88. void
  89. db_read_variable(struct db_variable *vp, db_expr_t *valuep)
  90. {
  91. int (*func)(struct db_variable *, db_expr_t *, int) = vp->fcn;
  92. if (func == FCN_NULL)
  93. *valuep = *(vp->valuep);
  94. else
  95. (*func)(vp, valuep, DB_VAR_GET);
  96. }
  97. void
  98. db_write_variable(struct db_variable *vp, db_expr_t *valuep)
  99. {
  100. int (*func)(struct db_variable *, db_expr_t *, int) = vp->fcn;
  101. if (func == FCN_NULL)
  102. *(vp->valuep) = *valuep;
  103. else
  104. (*func)(vp, valuep, DB_VAR_SET);
  105. }
  106. /*ARGSUSED*/
  107. void
  108. db_set_cmd(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
  109. {
  110. db_expr_t value;
  111. struct db_variable *vp;
  112. int t;
  113. t = db_read_token();
  114. if (t != tDOLLAR) {
  115. db_error("Unknown variable\n");
  116. /*NOTREACHED*/
  117. }
  118. if (!db_find_variable(&vp)) {
  119. db_error("Unknown variable\n");
  120. /*NOTREACHED*/
  121. }
  122. t = db_read_token();
  123. if (t != tEQ)
  124. db_unread_token(t);
  125. if (!db_expression(&value)) {
  126. db_error("No value\n");
  127. /*NOTREACHED*/
  128. }
  129. if (db_read_token() != tEOL) {
  130. db_error("?\n");
  131. /*NOTREACHED*/
  132. }
  133. db_write_variable(vp, &value);
  134. }
  135. int
  136. db_var_rw_int(struct db_variable *var, db_expr_t *expr, int mode)
  137. {
  138. if (mode == DB_VAR_SET)
  139. *var->valuep = *(int *)expr;
  140. else
  141. *expr = *(int *)var->valuep;
  142. return (0);
  143. }