[BACK]Return to db_lex.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / ddb

File: [cvs.NetBSD.org] / src / sys / ddb / db_lex.c (download)

Revision 1.20, Sun Nov 27 13:05:28 2005 UTC (14 years, 11 months ago) by yamt
Branch: MAIN
CVS Tags: yamt-x86pmap-base4, yamt-x86pmap-base3, yamt-x86pmap-base2, yamt-x86pmap-base, yamt-x86pmap, yamt-uio_vmspace-base5, yamt-uio_vmspace, yamt-splraiseipl-base5, yamt-splraiseipl-base4, yamt-splraiseipl-base3, yamt-splraiseipl-base2, yamt-splraiseipl-base, yamt-splraiseipl, yamt-readahead-base3, yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, yamt-pdpolicy-base9, yamt-pdpolicy-base8, yamt-pdpolicy-base7, yamt-pdpolicy-base6, yamt-pdpolicy-base5, yamt-pdpolicy-base4, yamt-pdpolicy-base3, yamt-pdpolicy-base2, yamt-pdpolicy-base, yamt-pdpolicy, yamt-nfs-mp-base2, yamt-nfs-mp-base, yamt-lazymbuf-base15, yamt-lazymbuf-base14, yamt-kmem-base3, yamt-kmem-base2, yamt-kmem-base, yamt-kmem, yamt-idlelwp-base8, yamt-idlelwp, wrstuden-revivesa-base-4, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, wrstuden-fixsa-newbase, wrstuden-fixsa-base-1, wrstuden-fixsa-base, wrstuden-fixsa, vmlocking2-base3, vmlocking2-base2, vmlocking2-base1, vmlocking2, vmlocking-nbase, vmlocking-base, vmlocking, thorpej-atomic-base, thorpej-atomic, simonb-wapbl-nbase, simonb-wapbl-base, simonb-wapbl, simonb-timecounters-base, simonb-timecounters, simonb-timcounters-final, rpaulo-netinet-merge-pcb-base, rpaulo-netinet-merge-pcb, reinoud-bufcleanup-nbase, reinoud-bufcleanup-base, reinoud-bufcleanup, post-newlock2-merge, peter-altq-base, peter-altq, nick-net80211-sync-base, nick-net80211-sync, nick-hppapmap-base2, nick-csl-alignment-base5, nick-csl-alignment-base, nick-csl-alignment, newlock2-nbase, newlock2-base, newlock2, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, netbsd-4-base, netbsd-4-0-RELEASE, netbsd-4-0-RC5, netbsd-4-0-RC4, netbsd-4-0-RC3, netbsd-4-0-RC2, netbsd-4-0-RC1, netbsd-4-0-1-RELEASE, netbsd-4-0, netbsd-4, mjf-ufs-trans-base, mjf-ufs-trans, mjf-devfs2-base, mjf-devfs2, mjf-devfs-base, mjf-devfs, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-nb4-arm-base, matt-nb4-arm, matt-mips64-base2, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, ktrace-lwp-base, keiichi-mipv6-nbase, keiichi-mipv6-base, keiichi-mipv6, jmcneill-pm-base, jmcneill-pm, jmcneill-base, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, haad-nbase2, haad-dm-base2, haad-dm-base1, haad-dm-base, haad-dm, gdamore-uart-base, gdamore-uart, elad-kernelauth-base, elad-kernelauth, cube-autoconf-base, cube-autoconf, chap-midi-nbase, chap-midi-base, chap-midi, bouyer-xeni386-nbase, bouyer-xeni386-merge1, bouyer-xeni386-base, bouyer-xeni386, bouyer-xenamd64-base2, bouyer-xenamd64-base, bouyer-xenamd64, ad-socklock-base1, ad-audiomp2-base, ad-audiomp2, ad-audiomp-base, ad-audiomp, abandoned-netbsd-4-base, abandoned-netbsd-4
Branch point for: yamt-nfs-mp, nick-hppapmap, jym-xensuspend
Changes since 1.19: +3 -3 lines

make DDB_COMMANDONENTER sysctl. (ddb.commandonenter)
suggested by Christos Zoulas.

/*	$NetBSD: db_lex.c,v 1.20 2005/11/27 13:05:28 yamt Exp $	*/

/*
 * Mach Operating System
 * Copyright (c) 1991,1990 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 *
 *	Author: David B. Golub, Carnegie Mellon University
 *	Date:	7/90
 */

/*
 * Lexical analyzer.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: db_lex.c,v 1.20 2005/11/27 13:05:28 yamt Exp $");

#include <sys/param.h>
#include <sys/systm.h>

#include <machine/db_machdep.h>

#include <ddb/db_lex.h>
#include <ddb/db_output.h>
#include <ddb/db_command.h>
#include <ddb/db_sym.h>
#include <ddb/db_extern.h>
#include <ddb/db_interface.h>

db_expr_t	db_tok_number;
char		db_tok_string[TOK_STRING_SIZE];

static char	db_line[DB_LINE_MAXLEN];
static const char *db_lp;
static const char *db_endlp;

static int	db_look_char = 0;
static int	db_look_token = 0;

static void	db_flush_line(void);
static int	db_read_char(void);
static void	db_unread_char(int);
static int	db_lex(void);

int
db_read_line(void)
{
	int	i;

	i = db_readline(db_line, sizeof(db_line));
	if (i == 0)
		return (0);	/* EOI */
	db_set_line(db_line, db_line + i);
	return (i);
}

void
db_set_line(const char *sp, const char *ep)
{

	db_lp = sp;
	db_endlp = ep;
}

static void
db_flush_line(void)
{

	db_lp = db_line;
	db_endlp = db_line;
}

static int
db_read_char(void)
{
	int	c;

	if (db_look_char != 0) {
		c = db_look_char;
		db_look_char = 0;
	}
	else if (db_lp >= db_endlp)
		c = -1;
	else
		c = *db_lp++;
	return (c);
}

static void
db_unread_char(int c)
{

	db_look_char = c;
}

void
db_unread_token(int t)
{

	db_look_token = t;
}

int
db_read_token(void)
{
	int	t;

	if (db_look_token) {
		t = db_look_token;
		db_look_token = 0;
	}
	else
		t = db_lex();
	return (t);
}

int	db_radix = 16;

/*
 * Convert the number to a string in the current radix.
 * This replaces the non-standard %n printf() format.
 */

char *
db_num_to_str(db_expr_t val)
{

	/*
	 * 2 chars for "0x", 1 for a sign ("-")
	 * up to 21 chars for a 64-bit number:
	 *   % echo 2^64 | bc | wc -c
	 *   21
	 * and 1 char for a terminal NUL
	 * 2+1+21+1 => 25
	 */
	static char buf[25];

	if (db_radix == 16)
		snprintf(buf, sizeof(buf), DB_EXPR_T_IS_QUAD ? "%#qx" : "%#lx",
		    val);
	else if (db_radix == 8)
		snprintf(buf, sizeof(buf), DB_EXPR_T_IS_QUAD ? "%#qo" : "%#lo",
		    val);
	else
		snprintf(buf, sizeof(buf), DB_EXPR_T_IS_QUAD ? "%qu" : "%lu",
		    val);

	return (buf);
}

void
db_flush_lex(void)
{

	db_flush_line();
	db_look_char = 0;
	db_look_token = 0;
}

static int
db_lex(void)
{
	int	c;

	c = db_read_char();
	while (c <= ' ' || c > '~') {
		if (c == '\n' || c == -1)
			return (tEOL);
		c = db_read_char();
	}

	if (c >= '0' && c <= '9') {
		/* number */
		db_expr_t	r, digit = 0;

		if (c > '0')
			r = db_radix;
		else {
			c = db_read_char();
			if (c == 'O' || c == 'o')
				r = 8;
			else if (c == 'T' || c == 't')
				r = 10;
			else if (c == 'X' || c == 'x')
				r = 16;
			else {
				r = db_radix;
				db_unread_char(c);
			}
			c = db_read_char();
		}
		db_tok_number = 0;
		for (;;) {
			if (c >= '0' && c <= ((r == 8) ? '7' : '9'))
				digit = c - '0';
			else if (r == 16 && ((c >= 'A' && c <= 'F') ||
				(c >= 'a' && c <= 'f'))) {
				if (c >= 'a')
					digit = c - 'a' + 10;
				else if (c >= 'A')
					digit = c - 'A' + 10;
			}
			else
				break;
			db_tok_number = db_tok_number * r + digit;
			c = db_read_char();
		}
		if ((c >= '0' && c <= '9') ||
		    (c >= 'A' && c <= 'Z') ||
		    (c >= 'a' && c <= 'z') ||
		    (c == '_')) {
			db_error("Bad character in number\n");
			/*NOTREACHED*/
		}
		db_unread_char(c);
		return (tNUMBER);
	}
	if ((c >= 'A' && c <= 'Z') ||
	    (c >= 'a' && c <= 'z') ||
	    c == '_' || c == '\\') {
		/* string */
		char *cp;

		cp = db_tok_string;
		if (c == '\\') {
			c = db_read_char();
			if (c == '\n' || c == -1) {
				db_error("Bad escape\n");
				/*NOTREACHED*/
			}
		}
		*cp++ = c;
		while (1) {
			c = db_read_char();
			if ((c >= 'A' && c <= 'Z') ||
			    (c >= 'a' && c <= 'z') ||
			    (c >= '0' && c <= '9') ||
			    c == '_' || c == '\\' || c == ':') {
				if (c == '\\') {
					c = db_read_char();
					if (c == '\n' || c == -1) {
						db_error("Bad escape\n");
						/*NOTREACHED*/
					}
				}
				*cp++ = c;
				if (cp == db_tok_string+sizeof(db_tok_string)) {
					db_error("String too long\n");
					/*NOTREACHED*/
				}
				continue;
			} else {
				*cp = '\0';
				break;
			}
		}
		db_unread_char(c);
		return (tIDENT);
	}

	switch (c) {
	case '+':
		return (tPLUS);
	case '-':
		return (tMINUS);
	case '.':
		c = db_read_char();
		if (c == '.')
			return (tDOTDOT);
		db_unread_char(c);
		return (tDOT);
	case '*':
		return (tSTAR);
	case '/':
		return (tSLASH);
	case '=':
		return (tEQ);
	case '%':
		return (tPCT);
	case '#':
		return (tHASH);
	case '(':
		return (tLPAREN);
	case ')':
		return (tRPAREN);
	case ',':
		return (tCOMMA);
	case '"':
		return (tDITTO);
	case '$':
		return (tDOLLAR);
	case '!':
		return (tEXCL);
	case '<':
		c = db_read_char();
		if (c == '<')
			return (tSHIFT_L);
		db_unread_char(c);
		break;
	case '>':
		c = db_read_char();
		if (c == '>')
			return (tSHIFT_R);
		db_unread_char(c);
		break;
	case -1:
		return (tEOF);
	}
	db_printf("Bad character\n");
	db_flush_lex();
	return (tEOF);
}