17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5843e1988Sjohnlev * Common Development and Distribution License (the "License"). 6843e1988Sjohnlev * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22a576ab5bSrab * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*f11c6b60SJohn Levon * 25*f11c6b60SJohn Levon * Copyright 2020 Joyent, Inc. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #ifndef _MDB_IO_H 297c478bd9Sstevel@tonic-gate #define _MDB_IO_H 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #ifdef __cplusplus 327c478bd9Sstevel@tonic-gate extern "C" { 337c478bd9Sstevel@tonic-gate #endif 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #ifdef _MDB 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #include <sys/types.h> 387c478bd9Sstevel@tonic-gate #include <setjmp.h> 397c478bd9Sstevel@tonic-gate #include <stdarg.h> 407c478bd9Sstevel@tonic-gate #include <stdio.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate typedef struct mdb_iob mdb_iob_t; /* I/O buffer */ 437c478bd9Sstevel@tonic-gate typedef struct mdb_io mdb_io_t; /* I/O implementation */ 447c478bd9Sstevel@tonic-gate struct mdb_arg; /* Argument structure */ 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #define MDB_IOB_DEFTAB 8 /* Default tabstop */ 477c478bd9Sstevel@tonic-gate #define MDB_IOB_DEFMARGIN 16 /* Default margin width */ 487c478bd9Sstevel@tonic-gate #define MDB_IOB_DEFROWS 24 /* Default rows */ 497c478bd9Sstevel@tonic-gate #define MDB_IOB_DEFCOLS 80 /* Default columns */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #define MDB_IOB_RDONLY 0x0001 /* Buffer is for reading */ 527c478bd9Sstevel@tonic-gate #define MDB_IOB_WRONLY 0x0002 /* Buffer is for writing */ 537c478bd9Sstevel@tonic-gate #define MDB_IOB_EOF 0x0004 /* Read buffer has reached EOF */ 547c478bd9Sstevel@tonic-gate #define MDB_IOB_ERR 0x0008 /* Underlying i/o error occurred */ 557c478bd9Sstevel@tonic-gate #define MDB_IOB_INDENT 0x0010 /* Lines are auto-indented */ 567c478bd9Sstevel@tonic-gate #define MDB_IOB_PGENABLE 0x0020 /* Pager enabled */ 577c478bd9Sstevel@tonic-gate #define MDB_IOB_PGSINGLE 0x0040 /* Line-at-a-time pager active */ 587c478bd9Sstevel@tonic-gate #define MDB_IOB_PGCONT 0x0080 /* Continue paging until next reset */ 597c478bd9Sstevel@tonic-gate #define MDB_IOB_AUTOWRAP 0x0100 /* Auto-wrap if next chunk won't fit */ 607c478bd9Sstevel@tonic-gate #define MDB_IOB_TTYLIKE 0x0200 /* Input is interactive like a tty */ 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate typedef struct mdb_iob_stack { 637c478bd9Sstevel@tonic-gate mdb_iob_t *stk_top; /* Topmost stack element */ 647c478bd9Sstevel@tonic-gate size_t stk_size; /* Number of stack elements */ 657c478bd9Sstevel@tonic-gate } mdb_iob_stack_t; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate typedef struct mdb_iob_ctx { 687c478bd9Sstevel@tonic-gate jmp_buf ctx_rpcb; /* Read-side context label */ 697c478bd9Sstevel@tonic-gate jmp_buf ctx_wpcb; /* Write-side context label */ 707c478bd9Sstevel@tonic-gate void *ctx_rptr; /* Read-side client data */ 717c478bd9Sstevel@tonic-gate void *ctx_wptr; /* Write-side client data */ 727c478bd9Sstevel@tonic-gate void *ctx_data; /* Pointer to client data */ 737c478bd9Sstevel@tonic-gate mdb_iob_t *ctx_iob; /* Storage for iob save/restore */ 747c478bd9Sstevel@tonic-gate } mdb_iob_ctx_t; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate #define MDB_IOB_RDIOB 0 /* Index for pipe's read-side iob */ 777c478bd9Sstevel@tonic-gate #define MDB_IOB_WRIOB 1 /* Index for pipe's write-side iob */ 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate typedef void mdb_iobsvc_f(mdb_iob_t *, mdb_iob_t *, mdb_iob_ctx_t *); 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate #define MDBIOC (('m' << 24) | ('d' << 16) | ('b' << 8)) 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate #define MDB_IOC_CTTY (MDBIOC | 0x01) /* Clear child tty settings */ 847c478bd9Sstevel@tonic-gate #define MDB_IOC_TSET (MDBIOC | 0x02) /* Set terminal type */ 857c478bd9Sstevel@tonic-gate #define MDB_IOC_GETFD (MDBIOC | 0x04) /* Get file descriptor (if any) */ 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate typedef void mdb_table_print_f(void *); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate #define MDB_TBL_DONE 0 917c478bd9Sstevel@tonic-gate #define MDB_TBL_PRNT 1 927c478bd9Sstevel@tonic-gate #define MDB_TBL_FUNC 2 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_io_hold(mdb_io_t *); 957c478bd9Sstevel@tonic-gate extern void mdb_io_rele(mdb_io_t *); 967c478bd9Sstevel@tonic-gate extern void mdb_io_destroy(mdb_io_t *); 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate extern mdb_iob_t *mdb_iob_create(mdb_io_t *, uint_t); 997c478bd9Sstevel@tonic-gate extern void mdb_iob_pipe(mdb_iob_t **, mdb_iobsvc_f *, mdb_iobsvc_f *); 1007c478bd9Sstevel@tonic-gate extern void mdb_iob_destroy(mdb_iob_t *); 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate extern void mdb_iob_flush(mdb_iob_t *); 1037c478bd9Sstevel@tonic-gate extern void mdb_iob_nlflush(mdb_iob_t *); 1047c478bd9Sstevel@tonic-gate extern void mdb_iob_discard(mdb_iob_t *); 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate extern void mdb_iob_push_io(mdb_iob_t *, mdb_io_t *); 1077c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_iob_pop_io(mdb_iob_t *); 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate extern void mdb_iob_resize(mdb_iob_t *, size_t, size_t); 1107c478bd9Sstevel@tonic-gate extern void mdb_iob_setpager(mdb_iob_t *, mdb_io_t *); 1117c478bd9Sstevel@tonic-gate extern void mdb_iob_clearlines(mdb_iob_t *); 1127c478bd9Sstevel@tonic-gate extern void mdb_iob_tabstop(mdb_iob_t *, size_t); 1137c478bd9Sstevel@tonic-gate extern void mdb_iob_margin(mdb_iob_t *, size_t); 1147c478bd9Sstevel@tonic-gate extern void mdb_iob_setbuf(mdb_iob_t *, void *, size_t); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate extern void mdb_iob_setflags(mdb_iob_t *, uint_t); 1177c478bd9Sstevel@tonic-gate extern void mdb_iob_clrflags(mdb_iob_t *, uint_t); 1187c478bd9Sstevel@tonic-gate extern uint_t mdb_iob_getflags(mdb_iob_t *); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate extern void mdb_iob_vprintf(mdb_iob_t *, const char *, va_list); 1217c478bd9Sstevel@tonic-gate extern void mdb_iob_aprintf(mdb_iob_t *, const char *, const struct mdb_arg *); 1227c478bd9Sstevel@tonic-gate extern void mdb_iob_printf(mdb_iob_t *, const char *, ...); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate extern size_t mdb_iob_vsnprintf(char *, size_t, const char *, va_list); 1257c478bd9Sstevel@tonic-gate extern size_t mdb_iob_asnprintf(char *, size_t, const char *, 1267c478bd9Sstevel@tonic-gate const struct mdb_arg *); 1277c478bd9Sstevel@tonic-gate extern size_t mdb_iob_snprintf(char *, size_t, const char *, ...); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate extern void mdb_iob_nputs(mdb_iob_t *, const char *, size_t); 1307c478bd9Sstevel@tonic-gate extern void mdb_iob_puts(mdb_iob_t *, const char *); 1317c478bd9Sstevel@tonic-gate extern void mdb_iob_putc(mdb_iob_t *, int); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate extern void mdb_iob_fill(mdb_iob_t *, int, size_t); 1347c478bd9Sstevel@tonic-gate extern void mdb_iob_ws(mdb_iob_t *, size_t); 1357c478bd9Sstevel@tonic-gate extern void mdb_iob_tab(mdb_iob_t *); 1367c478bd9Sstevel@tonic-gate extern void mdb_iob_nl(mdb_iob_t *); 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate extern ssize_t mdb_iob_ngets(mdb_iob_t *, char *, size_t); 1397c478bd9Sstevel@tonic-gate extern int mdb_iob_getc(mdb_iob_t *); 1407c478bd9Sstevel@tonic-gate extern int mdb_iob_ungetc(mdb_iob_t *, int); 1417c478bd9Sstevel@tonic-gate extern int mdb_iob_eof(mdb_iob_t *); 1427c478bd9Sstevel@tonic-gate extern int mdb_iob_err(mdb_iob_t *); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate extern ssize_t mdb_iob_read(mdb_iob_t *, void *, size_t); 1457c478bd9Sstevel@tonic-gate extern ssize_t mdb_iob_write(mdb_iob_t *, const void *, size_t); 1467c478bd9Sstevel@tonic-gate extern int mdb_iob_ctl(mdb_iob_t *, int, void *); 1477c478bd9Sstevel@tonic-gate extern const char *mdb_iob_name(mdb_iob_t *); 1487c478bd9Sstevel@tonic-gate extern size_t mdb_iob_lineno(mdb_iob_t *); 1497c478bd9Sstevel@tonic-gate extern size_t mdb_iob_gettabstop(mdb_iob_t *); 1507c478bd9Sstevel@tonic-gate extern size_t mdb_iob_getmargin(mdb_iob_t *); 1517c478bd9Sstevel@tonic-gate 152*f11c6b60SJohn Levon extern void mdb_iob_set_autowrap(mdb_iob_t *); 153*f11c6b60SJohn Levon 1547c478bd9Sstevel@tonic-gate extern void mdb_iob_stack_create(mdb_iob_stack_t *); 1557c478bd9Sstevel@tonic-gate extern void mdb_iob_stack_destroy(mdb_iob_stack_t *); 1567c478bd9Sstevel@tonic-gate extern void mdb_iob_stack_push(mdb_iob_stack_t *, mdb_iob_t *, size_t); 1577c478bd9Sstevel@tonic-gate extern mdb_iob_t *mdb_iob_stack_pop(mdb_iob_stack_t *); 1587c478bd9Sstevel@tonic-gate extern size_t mdb_iob_stack_size(mdb_iob_stack_t *); 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate extern const char *mdb_iob_format2str(const char *); 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * Available i/o backend constructors for common MDB code. These are 1647c478bd9Sstevel@tonic-gate * implemented in the corresponding .c files. 1657c478bd9Sstevel@tonic-gate */ 1667c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_logio_create(mdb_io_t *); 1677c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_fdio_create_path(const char **, const char *, int, mode_t); 1687c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_fdio_create_named(int fd, const char *); 1697c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_fdio_create(int); 1707c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_strio_create(const char *); 1717c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_termio_create(const char *, mdb_io_t *, mdb_io_t *); 1727c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_pipeio_create(mdb_iobsvc_f *, mdb_iobsvc_f *); 1737c478bd9Sstevel@tonic-gate extern mdb_io_t *mdb_nullio_create(void); 174843e1988Sjohnlev extern mdb_io_t *mdb_memio_create(char *, size_t); 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * Functions for testing whether the given iob is of a given backend type: 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate extern int mdb_iob_isastr(mdb_iob_t *); 1807c478bd9Sstevel@tonic-gate extern int mdb_iob_isatty(mdb_iob_t *); 1817c478bd9Sstevel@tonic-gate extern int mdb_iob_isapipe(mdb_iob_t *); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate extern void mdb_table_print(uint_t, const char *, ...); 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate extern int mdb_setupterm(const char *, mdb_io_t *, int *); 1867c478bd9Sstevel@tonic-gate 187a576ab5bSrab extern int mdb_fdio_fileno(mdb_io_t *); 188a576ab5bSrab 1897c478bd9Sstevel@tonic-gate #endif /* _MDB */ 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate #endif 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate #endif /* _MDB_IO_H */ 196