1 /* $NetBSD: db_lex.c,v 1.20 2005/11/27 13:05:28 yamt Exp $ */
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 "AS IS"
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.
28 * Author: David B. Golub, Carnegie Mellon University
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: db_lex.c,v 1.20 2005/11/27 13:05:28 yamt Exp $");
39 #include <sys/param.h>
40 #include <sys/systm.h>
44 db_expr_t db_tok_number
;
45 char db_tok_string
[TOK_STRING_SIZE
];
47 static char db_line
[DB_LINE_MAXLEN
];
48 static const char *db_lp
;
49 static const char *db_endlp
;
51 static int db_look_char
= 0;
52 static int db_look_token
= 0;
54 static void db_flush_line(void);
55 static int db_read_char(void);
56 static void db_unread_char(int);
57 static int db_lex(void);
64 i
= db_readline(db_line
, sizeof(db_line
));
67 db_set_line(db_line
, db_line
+ i
);
72 db_set_line(const char *sp
, const char *ep
)
92 if (db_look_char
!= 0) {
96 else if (db_lp
>= db_endlp
)
104 db_unread_char(int c
)
111 db_unread_token(int t
)
134 * Convert the number to a string in the current radix.
135 * This replaces the non-standard %n printf() format.
139 db_num_to_str(db_expr_t val
)
143 * 2 chars for "0x", 1 for a sign ("-")
144 * up to 21 chars for a 64-bit number:
145 * % echo 2^64 | bc | wc -c
147 * and 1 char for a terminal NUL
153 snprintf(buf
, sizeof(buf
), DB_EXPR_T_IS_QUAD
? "%#qx" : "%#lx",
155 else if (db_radix
== 8)
156 snprintf(buf
, sizeof(buf
), DB_EXPR_T_IS_QUAD
? "%#qo" : "%#lo",
159 snprintf(buf
, sizeof(buf
), DB_EXPR_T_IS_QUAD
? "%qu" : "%lu",
180 while (c
<= ' ' || c
> '~') {
181 if (c
== '\n' || c
== -1)
186 if (c
>= '0' && c
<= '9') {
188 db_expr_t r
, digit
= 0;
194 if (c
== 'O' || c
== 'o')
196 else if (c
== 'T' || c
== 't')
198 else if (c
== 'X' || c
== 'x')
208 if (c
>= '0' && c
<= ((r
== 8) ? '7' : '9'))
210 else if (r
== 16 && ((c
>= 'A' && c
<= 'F') ||
211 (c
>= 'a' && c
<= 'f'))) {
213 digit
= c
- 'a' + 10;
215 digit
= c
- 'A' + 10;
219 db_tok_number
= db_tok_number
* r
+ digit
;
222 if ((c
>= '0' && c
<= '9') ||
223 (c
>= 'A' && c
<= 'Z') ||
224 (c
>= 'a' && c
<= 'z') ||
226 db_error("Bad character in number\n");
232 if ((c
>= 'A' && c
<= 'Z') ||
233 (c
>= 'a' && c
<= 'z') ||
234 c
== '_' || c
== '\\') {
241 if (c
== '\n' || c
== -1) {
242 db_error("Bad escape\n");
249 if ((c
>= 'A' && c
<= 'Z') ||
250 (c
>= 'a' && c
<= 'z') ||
251 (c
>= '0' && c
<= '9') ||
252 c
== '_' || c
== '\\' || c
== ':') {
255 if (c
== '\n' || c
== -1) {
256 db_error("Bad escape\n");
261 if (cp
== db_tok_string
+sizeof(db_tok_string
)) {
262 db_error("String too long\n");
323 db_printf("Bad character\n");
329 * Utility routine - discard tokens through end-of-line.
342 db_error(const char *s
)