xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_io.h (revision f11c6b60)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2020 Joyent, Inc.
26  */
27 
28 #ifndef	_MDB_IO_H
29 #define	_MDB_IO_H
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 #ifdef _MDB
36 
37 #include <sys/types.h>
38 #include <setjmp.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 
42 typedef struct mdb_iob mdb_iob_t;	/* I/O buffer */
43 typedef struct mdb_io mdb_io_t;		/* I/O implementation */
44 struct mdb_arg;				/* Argument structure */
45 
46 #define	MDB_IOB_DEFTAB		8	/* Default tabstop */
47 #define	MDB_IOB_DEFMARGIN	16	/* Default margin width */
48 #define	MDB_IOB_DEFROWS		24	/* Default rows */
49 #define	MDB_IOB_DEFCOLS		80	/* Default columns */
50 
51 #define	MDB_IOB_RDONLY		0x0001	/* Buffer is for reading */
52 #define	MDB_IOB_WRONLY		0x0002	/* Buffer is for writing */
53 #define	MDB_IOB_EOF		0x0004	/* Read buffer has reached EOF */
54 #define	MDB_IOB_ERR		0x0008	/* Underlying i/o error occurred */
55 #define	MDB_IOB_INDENT		0x0010	/* Lines are auto-indented */
56 #define	MDB_IOB_PGENABLE	0x0020	/* Pager enabled */
57 #define	MDB_IOB_PGSINGLE	0x0040	/* Line-at-a-time pager active */
58 #define	MDB_IOB_PGCONT		0x0080	/* Continue paging until next reset */
59 #define	MDB_IOB_AUTOWRAP	0x0100	/* Auto-wrap if next chunk won't fit */
60 #define	MDB_IOB_TTYLIKE		0x0200	/* Input is interactive like a tty */
61 
62 typedef struct mdb_iob_stack {
63 	mdb_iob_t *stk_top;		/* Topmost stack element */
64 	size_t stk_size;		/* Number of stack elements */
65 } mdb_iob_stack_t;
66 
67 typedef struct mdb_iob_ctx {
68 	jmp_buf ctx_rpcb;		/* Read-side context label */
69 	jmp_buf ctx_wpcb;		/* Write-side context label */
70 	void *ctx_rptr;			/* Read-side client data */
71 	void *ctx_wptr;			/* Write-side client data */
72 	void *ctx_data;			/* Pointer to client data */
73 	mdb_iob_t *ctx_iob;		/* Storage for iob save/restore */
74 } mdb_iob_ctx_t;
75 
76 #define	MDB_IOB_RDIOB	0		/* Index for pipe's read-side iob */
77 #define	MDB_IOB_WRIOB	1		/* Index for pipe's write-side iob */
78 
79 typedef void mdb_iobsvc_f(mdb_iob_t *, mdb_iob_t *, mdb_iob_ctx_t *);
80 
81 #define	MDBIOC		(('m' << 24) | ('d' << 16) | ('b' << 8))
82 
83 #define	MDB_IOC_CTTY	(MDBIOC | 0x01)	/* Clear child tty settings */
84 #define	MDB_IOC_TSET	(MDBIOC | 0x02)	/* Set terminal type */
85 #define	MDB_IOC_GETFD	(MDBIOC | 0x04) /* Get file descriptor (if any) */
86 
87 
88 typedef void mdb_table_print_f(void *);
89 
90 #define	MDB_TBL_DONE	0
91 #define	MDB_TBL_PRNT	1
92 #define	MDB_TBL_FUNC	2
93 
94 extern mdb_io_t *mdb_io_hold(mdb_io_t *);
95 extern void mdb_io_rele(mdb_io_t *);
96 extern void mdb_io_destroy(mdb_io_t *);
97 
98 extern mdb_iob_t *mdb_iob_create(mdb_io_t *, uint_t);
99 extern void mdb_iob_pipe(mdb_iob_t **, mdb_iobsvc_f *, mdb_iobsvc_f *);
100 extern void mdb_iob_destroy(mdb_iob_t *);
101 
102 extern void mdb_iob_flush(mdb_iob_t *);
103 extern void mdb_iob_nlflush(mdb_iob_t *);
104 extern void mdb_iob_discard(mdb_iob_t *);
105 
106 extern void mdb_iob_push_io(mdb_iob_t *, mdb_io_t *);
107 extern mdb_io_t *mdb_iob_pop_io(mdb_iob_t *);
108 
109 extern void mdb_iob_resize(mdb_iob_t *, size_t, size_t);
110 extern void mdb_iob_setpager(mdb_iob_t *, mdb_io_t *);
111 extern void mdb_iob_clearlines(mdb_iob_t *);
112 extern void mdb_iob_tabstop(mdb_iob_t *, size_t);
113 extern void mdb_iob_margin(mdb_iob_t *, size_t);
114 extern void mdb_iob_setbuf(mdb_iob_t *, void *, size_t);
115 
116 extern void mdb_iob_setflags(mdb_iob_t *, uint_t);
117 extern void mdb_iob_clrflags(mdb_iob_t *, uint_t);
118 extern uint_t mdb_iob_getflags(mdb_iob_t *);
119 
120 extern void mdb_iob_vprintf(mdb_iob_t *, const char *, va_list);
121 extern void mdb_iob_aprintf(mdb_iob_t *, const char *, const struct mdb_arg *);
122 extern void mdb_iob_printf(mdb_iob_t *, const char *, ...);
123 
124 extern size_t mdb_iob_vsnprintf(char *, size_t, const char *, va_list);
125 extern size_t mdb_iob_asnprintf(char *, size_t, const char *,
126     const struct mdb_arg *);
127 extern size_t mdb_iob_snprintf(char *, size_t, const char *, ...);
128 
129 extern void mdb_iob_nputs(mdb_iob_t *, const char *, size_t);
130 extern void mdb_iob_puts(mdb_iob_t *, const char *);
131 extern void mdb_iob_putc(mdb_iob_t *, int);
132 
133 extern void mdb_iob_fill(mdb_iob_t *, int, size_t);
134 extern void mdb_iob_ws(mdb_iob_t *, size_t);
135 extern void mdb_iob_tab(mdb_iob_t *);
136 extern void mdb_iob_nl(mdb_iob_t *);
137 
138 extern ssize_t mdb_iob_ngets(mdb_iob_t *, char *, size_t);
139 extern int mdb_iob_getc(mdb_iob_t *);
140 extern int mdb_iob_ungetc(mdb_iob_t *, int);
141 extern int mdb_iob_eof(mdb_iob_t *);
142 extern int mdb_iob_err(mdb_iob_t *);
143 
144 extern ssize_t mdb_iob_read(mdb_iob_t *, void *, size_t);
145 extern ssize_t mdb_iob_write(mdb_iob_t *, const void *, size_t);
146 extern int mdb_iob_ctl(mdb_iob_t *, int, void *);
147 extern const char *mdb_iob_name(mdb_iob_t *);
148 extern size_t mdb_iob_lineno(mdb_iob_t *);
149 extern size_t mdb_iob_gettabstop(mdb_iob_t *);
150 extern size_t mdb_iob_getmargin(mdb_iob_t *);
151 
152 extern void mdb_iob_set_autowrap(mdb_iob_t *);
153 
154 extern void mdb_iob_stack_create(mdb_iob_stack_t *);
155 extern void mdb_iob_stack_destroy(mdb_iob_stack_t *);
156 extern void mdb_iob_stack_push(mdb_iob_stack_t *, mdb_iob_t *, size_t);
157 extern mdb_iob_t *mdb_iob_stack_pop(mdb_iob_stack_t *);
158 extern size_t mdb_iob_stack_size(mdb_iob_stack_t *);
159 
160 extern const char *mdb_iob_format2str(const char *);
161 
162 /*
163  * Available i/o backend constructors for common MDB code.  These are
164  * implemented in the corresponding .c files.
165  */
166 extern mdb_io_t *mdb_logio_create(mdb_io_t *);
167 extern mdb_io_t *mdb_fdio_create_path(const char **, const char *, int, mode_t);
168 extern mdb_io_t *mdb_fdio_create_named(int fd, const char *);
169 extern mdb_io_t *mdb_fdio_create(int);
170 extern mdb_io_t *mdb_strio_create(const char *);
171 extern mdb_io_t *mdb_termio_create(const char *, mdb_io_t *, mdb_io_t *);
172 extern mdb_io_t *mdb_pipeio_create(mdb_iobsvc_f *, mdb_iobsvc_f *);
173 extern mdb_io_t *mdb_nullio_create(void);
174 extern mdb_io_t *mdb_memio_create(char *, size_t);
175 
176 /*
177  * Functions for testing whether the given iob is of a given backend type:
178  */
179 extern int mdb_iob_isastr(mdb_iob_t *);
180 extern int mdb_iob_isatty(mdb_iob_t *);
181 extern int mdb_iob_isapipe(mdb_iob_t *);
182 
183 extern void mdb_table_print(uint_t, const char *, ...);
184 
185 extern int mdb_setupterm(const char *, mdb_io_t *, int *);
186 
187 extern int mdb_fdio_fileno(mdb_io_t *);
188 
189 #endif /* _MDB */
190 
191 #ifdef	__cplusplus
192 }
193 #endif
194 
195 #endif	/* _MDB_IO_H */
196