xref: /titanic_41/usr/src/cmd/mdb/common/mdb/mdb_strio.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * String I/O Backend
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Simple backend to provide the ability to perform i/o reads from an in-memory
33*7c478bd9Sstevel@tonic-gate  * string.  This allows us to mdb_eval() a string -- by creating an i/o object
34*7c478bd9Sstevel@tonic-gate  * out of it, we can then pass it to the parser as stdin.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
38*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_io_impl.h>
39*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h>
40*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate typedef struct str_data {
43*7c478bd9Sstevel@tonic-gate 	char *str_base;		/* Pointer to private copy of string */
44*7c478bd9Sstevel@tonic-gate 	char *str_ptr;		/* Current seek pointer */
45*7c478bd9Sstevel@tonic-gate 	size_t str_len;		/* Length of string */
46*7c478bd9Sstevel@tonic-gate } str_data_t;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static ssize_t
strio_read(mdb_io_t * io,void * buf,size_t nbytes)49*7c478bd9Sstevel@tonic-gate strio_read(mdb_io_t *io, void *buf, size_t nbytes)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	str_data_t *sd = io->io_data;
52*7c478bd9Sstevel@tonic-gate 	size_t left = sd->str_base + sd->str_len - sd->str_ptr;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	if (left != 0) {
55*7c478bd9Sstevel@tonic-gate 		size_t obytes = nbytes < left ? nbytes : left;
56*7c478bd9Sstevel@tonic-gate 		(void) strncpy(buf, sd->str_ptr, obytes);
57*7c478bd9Sstevel@tonic-gate 		sd->str_ptr += obytes;
58*7c478bd9Sstevel@tonic-gate 		return (obytes);
59*7c478bd9Sstevel@tonic-gate 	}
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	return (0); /* At end of string: return EOF */
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static off64_t
strio_seek(mdb_io_t * io,off64_t offset,int whence)65*7c478bd9Sstevel@tonic-gate strio_seek(mdb_io_t *io, off64_t offset, int whence)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	str_data_t *sd = io->io_data;
68*7c478bd9Sstevel@tonic-gate 	char *nptr;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if (io->io_next != NULL)
71*7c478bd9Sstevel@tonic-gate 		return (IOP_SEEK(io->io_next, offset, whence));
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	switch (whence) {
74*7c478bd9Sstevel@tonic-gate 	case SEEK_SET:
75*7c478bd9Sstevel@tonic-gate 		nptr = sd->str_base + offset;
76*7c478bd9Sstevel@tonic-gate 		break;
77*7c478bd9Sstevel@tonic-gate 	case SEEK_CUR:
78*7c478bd9Sstevel@tonic-gate 		nptr = sd->str_ptr + offset;
79*7c478bd9Sstevel@tonic-gate 		break;
80*7c478bd9Sstevel@tonic-gate 	case SEEK_END:
81*7c478bd9Sstevel@tonic-gate 		nptr = sd->str_base + sd->str_len + offset;
82*7c478bd9Sstevel@tonic-gate 		break;
83*7c478bd9Sstevel@tonic-gate 	default:
84*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if (nptr < sd->str_base || nptr > sd->str_ptr + sd->str_len)
88*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	sd->str_ptr = nptr;
91*7c478bd9Sstevel@tonic-gate 	return ((off64_t)(nptr - sd->str_base));
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate static void
strio_close(mdb_io_t * io)95*7c478bd9Sstevel@tonic-gate strio_close(mdb_io_t *io)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	str_data_t *sd = io->io_data;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	strfree(sd->str_base);
100*7c478bd9Sstevel@tonic-gate 	mdb_free(sd, sizeof (str_data_t));
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate static const char *
strio_name(mdb_io_t * io)104*7c478bd9Sstevel@tonic-gate strio_name(mdb_io_t *io)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	if (io->io_next != NULL)
107*7c478bd9Sstevel@tonic-gate 		return (IOP_NAME(io->io_next));
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	return ("(string)");
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate static const mdb_io_ops_t strio_ops = {
113*7c478bd9Sstevel@tonic-gate 	strio_read,
114*7c478bd9Sstevel@tonic-gate 	no_io_write,
115*7c478bd9Sstevel@tonic-gate 	strio_seek,
116*7c478bd9Sstevel@tonic-gate 	no_io_ctl,
117*7c478bd9Sstevel@tonic-gate 	strio_close,
118*7c478bd9Sstevel@tonic-gate 	strio_name,
119*7c478bd9Sstevel@tonic-gate 	no_io_link,
120*7c478bd9Sstevel@tonic-gate 	no_io_unlink,
121*7c478bd9Sstevel@tonic-gate 	no_io_setattr,
122*7c478bd9Sstevel@tonic-gate 	no_io_suspend,
123*7c478bd9Sstevel@tonic-gate 	no_io_resume
124*7c478bd9Sstevel@tonic-gate };
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate mdb_io_t *
mdb_strio_create(const char * s)127*7c478bd9Sstevel@tonic-gate mdb_strio_create(const char *s)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	mdb_io_t *io = mdb_alloc(sizeof (mdb_io_t), UM_SLEEP);
130*7c478bd9Sstevel@tonic-gate 	str_data_t *sd = mdb_alloc(sizeof (str_data_t), UM_SLEEP);
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	/*
133*7c478bd9Sstevel@tonic-gate 	 * Our parser expects each command to end with '\n' or ';'.  To
134*7c478bd9Sstevel@tonic-gate 	 * simplify things for the caller, we append a trailing newline
135*7c478bd9Sstevel@tonic-gate 	 * so the argvec string can be passed directly sans modifications.
136*7c478bd9Sstevel@tonic-gate 	 */
137*7c478bd9Sstevel@tonic-gate 	sd->str_len = strlen(s) + 1;
138*7c478bd9Sstevel@tonic-gate 	sd->str_base = strndup(s, sd->str_len);
139*7c478bd9Sstevel@tonic-gate 	(void) strcat(sd->str_base, "\n");
140*7c478bd9Sstevel@tonic-gate 	sd->str_ptr = sd->str_base;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	io->io_ops = &strio_ops;
143*7c478bd9Sstevel@tonic-gate 	io->io_data = sd;
144*7c478bd9Sstevel@tonic-gate 	io->io_next = NULL;
145*7c478bd9Sstevel@tonic-gate 	io->io_refcnt = 0;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	return (io);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate int
mdb_iob_isastr(mdb_iob_t * iob)151*7c478bd9Sstevel@tonic-gate mdb_iob_isastr(mdb_iob_t *iob)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	mdb_io_t *io;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	for (io = iob->iob_iop; io != NULL; io = io->io_next) {
156*7c478bd9Sstevel@tonic-gate 		if (io->io_ops == &strio_ops)
157*7c478bd9Sstevel@tonic-gate 			return (1);
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	return (0);
161*7c478bd9Sstevel@tonic-gate }
162