xref: /freebsd/lib/libdpv/dpv.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * Copyright (c) 2013-2016 Devin Teske <dteske@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _DPV_H_
30 #define _DPV_H_
31 
32 #include <sys/types.h>
33 
34 #ifndef TRUE
35 #define TRUE 1
36 #endif
37 #ifndef FALSE
38 #define FALSE 0
39 #endif
40 
41 /* localeconv(3) */
42 #define LC_NUMERIC_DEFAULT	"en_US.ISO8859-1"
43 
44 /* Data to process */
45 extern long long dpv_overall_read;
46 
47 /* Interrupt flag */
48 extern int dpv_interrupt;	/* Set to TRUE in interrupt handler */
49 extern int dpv_abort;		/* Set to true in callback to abort */
50 
51 /*
52  * Display types for use with display_type member of dpv_config structure
53  */
54 enum dpv_display {
55 	DPV_DISPLAY_LIBDIALOG = 0,	/* Display using dialog(3) (default) */
56 	DPV_DISPLAY_STDOUT,		/* Display on stdout */
57 	DPV_DISPLAY_DIALOG,		/* Display using spawned dialog(1) */
58 	DPV_DISPLAY_XDIALOG,		/* Display using spawned Xdialog(1) */
59 };
60 
61 /*
62  * Output types for use with output_type member of dpv_config structure
63  */
64 enum dpv_output {
65 	DPV_OUTPUT_NONE = 0,	/* No output (default) */
66 	DPV_OUTPUT_FILE,	/* Read `output' member as file path */
67 	DPV_OUTPUT_SHELL,	/* Read `output' member as shell cmd */
68 };
69 
70 /*
71  * Activity types for use with status member of dpv_file_node structure.
72  * If you set a status other than DPV_STATUS_RUNNING on the current file in the
73  * action callback of dpv_config structure, you'll end callbacks for that
74  * dpv_file_node.
75  */
76 enum dpv_status {
77 	DPV_STATUS_RUNNING = 0,	/* Running (default) */
78 	DPV_STATUS_DONE,	/* Completed */
79 	DPV_STATUS_FAILED,	/* Oops, something went wrong */
80 };
81 
82 /*
83  * Anatomy of file option; pass an array of these as dpv() file_list argument
84  * terminated with a NULL pointer.
85  */
86 struct dpv_file_node {
87 	enum dpv_status		status; /* status of read operation */
88 	char			*msg;	/* display instead of "Done/Fail" */
89 	char			*name;	/* name of file to read */
90 	char			*path;	/* path to file */
91 	long long		length;	/* expected size */
92 	long long		read;	/* number units read (e.g., bytes) */
93 	struct dpv_file_node	*next;	/* pointer to next (end with NULL) */
94 };
95 
96 /*
97  * Anatomy of config option to pass as dpv() config argument
98  */
99 struct dpv_config {
100 	uint8_t	keep_tite;		/* Prevent visually distracting exit */
101 	enum dpv_display display_type;	/* Display (default TYPE_LIBDIALOG) */
102 	enum dpv_output  output_type;	/* Output (default TYPE_NONE) */
103 	int	debug;			/* Enable debugging output on stderr */
104 	int	display_limit;		/* Files per `page'. Default -1 */
105 	int	label_size;		/* Label size. Default 28 */
106 	int	pbar_size;		/* Mini-progress size. See dpv(3) */
107 	int	dialog_updates_per_second; /* Progress updates/s. Default 16 */
108 	int	status_updates_per_second; /* dialog(3) status updates/second.
109 	   	                            * Default 2 */
110 	uint16_t options;	/* Special options. Default 0 */
111 	char	*title;		/* widget title */
112 	char	*backtitle;	/* Widget backtitle */
113 	char	*aprompt;	/* Prompt append. Default NULL */
114 	char	*pprompt;	/* Prompt prefix. Default NULL */
115 	char	*msg_done;	/* Progress text. Default `Done' */
116 	char	*msg_fail;	/* Progress text. Default `Fail' */
117 	char	*msg_pending;	/* Progress text. Default `Pending' */
118 	char	*output;	/* Output format string; see dpv(3) */
119 	const char *status_solo; /* dialog(3) solo-status format.
120 	                          * Default DPV_STATUS_SOLO */
121 	const char *status_many; /* dialog(3) many-status format.
122 	                          * Default DPV_STATUS_MANY */
123 
124 	/*
125 	 * Function pointer; action to perform data transfer
126 	 */
127 	int (*action)(struct dpv_file_node *file, int out);
128 };
129 
130 /*
131  * Macros for dpv() options bitmask argument
132  */
133 #define DPV_TEST_MODE		0x0001	/* Test mode (fake reading data) */
134 #define DPV_WIDE_MODE		0x0002	/* prefix/append bump dialog width */
135 #define DPV_NO_LABELS		0x0004	/* Hide file_node.name labels */
136 #define DPV_USE_COLOR		0x0008	/* Override to force color output */
137 #define DPV_NO_OVERRUN		0x0010	/* Stop transfers when they hit 100% */
138 
139 /*
140  * Limits (modify with extreme care)
141  */
142 #define DPV_APROMPT_MAX		4096	/* Buffer size for `-a text' */
143 #define DPV_DISPLAY_LIMIT	10	/* Max file progress lines */
144 #define DPV_PPROMPT_MAX		4096	/* Buffer size for `-p text' */
145 #define DPV_STATUS_FORMAT_MAX	80	/* Buffer size for `-u format' */
146 
147 /*
148  * Extra display information
149  */
150 #define DPV_STATUS_SOLO		"%'10lli bytes read @ %'9.1f bytes/sec."
151 #define DPV_STATUS_MANY		(DPV_STATUS_SOLO " [%i/%i busy/wait]")
152 
153 /*
154  * Strings
155  */
156 #define DPV_DONE_DEFAULT	"Done"
157 #define DPV_FAIL_DEFAULT	"Fail"
158 #define DPV_PENDING_DEFAULT	"Pending"
159 
160 __BEGIN_DECLS
161 void	dpv_free(void);
162 int	dpv(struct dpv_config *_config, struct dpv_file_node *_file_list);
163 __END_DECLS
164 
165 #endif /* !_DPV_H_ */
166