2 * SPDX-License-Identifier: MIT-CMU
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
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.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 * Author: David B. Golub, Carnegie Mellon University
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/libkern.h>
42 #include <ddb/db_lex.h>
44 static char db_line[DB_MAXLINE];
45 static char * db_lp, *db_endlp;
47 static int db_lex(int);
48 static void db_flush_line(void);
49 static int db_read_char(void);
50 static void db_unread_char(int);
57 i = db_readline(db_line, sizeof(db_line));
66 * Simulate a line of input into DDB.
69 db_inject_line(const char *command)
72 strlcpy(db_line, command, sizeof(db_line));
74 db_endlp = db_lp + strlen(command);
78 * In rare cases, we may want to pull the remainder of the line input
79 * verbatim, rather than lexing it. For example, when assigning literal
80 * values associated with scripts. In that case, return a static pointer to
81 * the current location in the input buffer. The caller must be aware that
82 * the contents are not stable if other lex/input calls are made.
103 if (db_lp >= db_endlp)
111 db_unread_char(int c)
115 /* Unread EOL at EOL is okay. */
116 if (db_lp < db_endlp)
117 db_error("db_unread_char(-1) before end of line\n");
119 if (db_lp > db_line) {
122 db_error("db_unread_char() wrong char\n");
124 db_error("db_unread_char() at beginning of line\n");
129 static int db_look_token = 0;
132 db_unread_token(int t)
138 db_read_token_flags(int flags)
142 MPASS((flags & ~(DRT_VALID_FLAGS_MASK)) == 0);
153 db_expr_t db_tok_number;
154 char db_tok_string[TOK_STRING_SIZE];
156 db_expr_t db_radix = 16;
168 int c, n, radix_mode;
169 bool lex_wspace, lex_hex_numbers;
171 switch (flags & DRT_RADIX_MASK) {
172 case DRT_DEFAULT_RADIX:
181 case DRT_HEXADECIMAL:
186 lex_wspace = ((flags & DRT_WSPACE) != 0);
187 lex_hex_numbers = ((flags & DRT_HEX) != 0);
190 for (n = 0; c <= ' ' || c > '~'; n++) {
191 if (c == '\n' || c == -1)
195 if (lex_wspace && n != 0) {
200 if ((c >= '0' && c <= '9') ||
202 ((c >= 'a' && c <= 'f') ||
203 (c >= 'A' && c <= 'F')))) {
207 if (radix_mode != -1)
213 if (c == 'O' || c == 'o')
215 else if (c == 'T' || c == 't')
217 else if (c == 'X' || c == 'x')
227 if (c >= '0' && c <= ((r == 8) ? '7' : '9'))
229 else if (r == 16 && ((c >= 'A' && c <= 'F') ||
230 (c >= 'a' && c <= 'f'))) {
232 digit = c - 'a' + 10;
234 digit = c - 'A' + 10;
238 db_tok_number = db_tok_number * r + digit;
241 if ((c >= '0' && c <= '9') ||
242 (c >= 'A' && c <= 'Z') ||
243 (c >= 'a' && c <= 'z') ||
246 db_error("Bad character in number\n");
253 if ((c >= 'A' && c <= 'Z') ||
254 (c >= 'a' && c <= 'z') ||
255 c == '_' || c == '\\')
263 if (c == '\n' || c == -1)
264 db_error("Bad escape\n");
269 if ((c >= 'A' && c <= 'Z') ||
270 (c >= 'a' && c <= 'z') ||
271 (c >= '0' && c <= '9') ||
272 c == '_' || c == '\\' || c == ':' || c == '.')
276 if (c == '\n' || c == -1)
277 db_error("Bad escape\n");
280 if (cp == db_tok_string+sizeof(db_tok_string)) {
281 db_error("String too long\n");
334 return (tLOG_NOT_EQ);
341 return (tCOLONCOLON);
371 return (tGREATER_EQ);
381 db_printf("Bad character\n");