xref: /freebsd/lib/libdpv/dpv.h (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*-
2  * Copyright (c) 2013-2014 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 /* Data to process */
42 extern long long dpv_overall_read;
43 
44 /* Interrupt flag */
45 extern int dpv_interrupt;	/* Set to TRUE in interrupt handler */
46 extern int dpv_abort;		/* Set to true in callback to abort */
47 
48 /*
49  * Display types for use with display_type member of dpv_config structure
50  */
51 enum dpv_display {
52 	DPV_DISPLAY_LIBDIALOG = 0,	/* Display using dialog(3) (default) */
53 	DPV_DISPLAY_STDOUT,		/* Display on stdout */
54 	DPV_DISPLAY_DIALOG,		/* Display using spawned dialog(1) */
55 	DPV_DISPLAY_XDIALOG,		/* Display using spawned Xdialog(1) */
56 };
57 
58 /*
59  * Output types for use with output_type member of dpv_config structure
60  */
61 enum dpv_output {
62 	DPV_OUTPUT_NONE = 0,	/* No output (default) */
63 	DPV_OUTPUT_FILE,	/* Read `output' member as file path */
64 	DPV_OUTPUT_SHELL,	/* Read `output' member as shell cmd */
65 };
66 
67 /*
68  * Activity types for use with status member of dpv_file_node structure.
69  * If you set a status other than DPV_STATUS_RUNNING on the current file in the
70  * action callback of dpv_config structure, you'll end callbacks for that
71  * dpv_file_node.
72  */
73 enum dpv_status {
74 	DPV_STATUS_RUNNING = 0,	/* Running (default) */
75 	DPV_STATUS_DONE,	/* Completed */
76 	DPV_STATUS_FAILED,	/* Oops, something went wrong */
77 };
78 
79 /*
80  * Anatomy of file option; pass an array of these as dpv() file_list argument
81  * terminated with a NULL pointer.
82  */
83 struct dpv_file_node {
84 	enum dpv_status		status; /* status of read operation */
85 	char			*msg;	/* display instead of "Done/Fail" */
86 	char			*name;	/* name of file to read */
87 	char			*path;	/* path to file */
88 	long long		length;	/* expected size */
89 	long long		read;	/* number units read (e.g., bytes) */
90 	struct dpv_file_node	*next;	/* pointer to next (end with NULL) */
91 };
92 
93 /*
94  * Anatomy of config option to pass as dpv() config argument
95  */
96 struct dpv_config {
97 	enum dpv_display display_type;	/* Display (default TYPE_LIBDIALOG) */
98 	enum dpv_output  output_type;	/* Output (default TYPE_NONE) */
99 	int	debug;			/* Enable debugging output on stderr */
100 	int	display_limit;		/* Files per `page'. Default -1 */
101 	int	label_size;		/* Label size. Default 28 */
102 	int	pbar_size;		/* Mini-progress size. See dpv(3) */
103 	int	dialog_updates_per_second; /* Progress updates/s. Default 16 */
104 	int	status_updates_per_second; /* dialog(3) status updates/second.
105 	   	                            * Default 2 */
106 	uint16_t options;	/* Special options. Default 0 */
107 	char	*title;		/* widget title */
108 	char	*backtitle;	/* Widget backtitle */
109 	char	*aprompt;	/* Prompt append. Default NULL */
110 	char	*pprompt;	/* Prompt prefix. Default NULL */
111 	char	*msg_done;	/* Progress text. Default `Done' */
112 	char	*msg_fail;	/* Progress text. Default `Fail' */
113 	char	*msg_pending;	/* Progress text. Default `Pending' */
114 	char	*output;	/* Output format string; see dpv(3) */
115 	const char *status_solo; /* dialog(3) solo-status format.
116 	                          * Default DPV_STATUS_SOLO */
117 	const char *status_many; /* dialog(3) many-status format.
118 	                          * Default DPV_STATUS_MANY */
119 
120 	/*
121 	 * Function pointer; action to perform data transfer
122 	 */
123 	int (*action)(struct dpv_file_node *file, int out);
124 };
125 
126 /*
127  * Macros for dpv() options bitmask argument
128  */
129 #define DPV_TEST_MODE		0x0001	/* Test mode (fake reading data) */
130 #define DPV_WIDE_MODE		0x0002	/* prefix/append bump dialog width */
131 #define DPV_NO_LABELS		0x0004	/* Hide file_node.name labels */
132 #define DPV_USE_COLOR		0x0008	/* Override to force color output */
133 #define DPV_NO_OVERRUN		0x0010	/* Stop transfers when they hit 100% */
134 
135 /*
136  * Limits (modify with extreme care)
137  */
138 #define DPV_APROMPT_MAX		4096	/* Buffer size for `-a text' */
139 #define DPV_DISPLAY_LIMIT	10	/* Max file progress lines */
140 #define DPV_PPROMPT_MAX		4096	/* Buffer size for `-p text' */
141 #define DPV_STATUS_FORMAT_MAX	80	/* Buffer size for `-u format' */
142 
143 /*
144  * Extra display information
145  */
146 #define DPV_STATUS_SOLO		"%'10lli bytes read @ %'9.1f bytes/sec."
147 #define DPV_STATUS_MANY		(DPV_STATUS_SOLO " [%i/%i busy/wait]")
148 
149 /*
150  * Strings
151  */
152 #define DPV_DONE_DEFAULT	"Done"
153 #define DPV_FAIL_DEFAULT	"Fail"
154 #define DPV_PENDING_DEFAULT	"Pending"
155 
156 __BEGIN_DECLS
157 void	dpv_free(void);
158 int	dpv(struct dpv_config *_config, struct dpv_file_node *_file_list);
159 __END_DECLS
160 
161 #endif /* !_DPV_H_ */
162