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