summaryrefslogtreecommitdiffstats
path: root/lib/libdpv/dpv.3
blob: 4779540bcdcc13a045e158738730b06c875e8762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
.\" Copyright (c) 2013-2015 Devin Teske
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd Oct 22, 2015
.Dt DPV 3
.Os
.Sh NAME
.Nm dpv
.Nd dialog progress view library
.Sh LIBRARY
.Lb libdpv
.Sh SYNOPSIS
.In dpv.h
.Ft int
.Fo dpv
.Fa "struct dpv_config *config, struct dpv_file_node *file_list"
.Fc
.Ft void
.Fo dpv_free
.Fa "void"
.Fc
.Sh DESCRIPTION
The
.Nm
library provides an interface for creating complex
.Dq gauge
widgets for displaying progress on various actions.
The
.Nm
library can display progress with one of
.Xr dialog 3 ,
.Xr dialog 1 ,
or
.Xr Xdialog 1
.Pq x11/xdialog from the ports tree .
.Pp
The
.Fn dpv
.Fa config
argument contains the following properties for configuring global display
features:
.Bd -literal -offset indent
struct dpv_config {
    enum dpv_display display_type;  /* Def. DPV_DISPLAY_LIBDIALOG */
    enum dpv_output  output_type;   /* Default DPV_OUTPUT_NONE */
    int              debug;         /* Enable debug on stderr */
    int              display_limit; /* Files/page. Default -1 */
    int              label_size;    /* Label size. Default 28 */
    int              pbar_size;     /* Mini-progress size */
    int              dialog_updates_per_second; /* Default 16 */
    int              status_updates_per_second; /* Default 2 */
    uint16_t         options;       /* Default 0 (none) */
    char             *title;        /* Widget title */
    char             *backtitle;    /* Widget backtitle */
    char             *aprompt;      /* Append. Default NULL */
    char             *pprompt;      /* Prefix. Default NULL */
    char             *msg_done;     /* Default `Done' */
    char             *msg_fail;     /* Default `Fail' */
    char             *msg_pending;  /* Default `Pending' */
    char             *output;       /* Output format string */
    const char       *status_solo;  /* dialog(3) solo-status format.
                                     * Default DPV_STATUS_SOLO */
    const char       *status_many;  /* dialog(3) many-status format.
                                     * Default DPV_STATUS_MANY */

    /*
     * Function pointer; action to perform data transfer
     */
    int (*action)(struct dpv_file_node *file, int out);
};

enum dpv_display {
    DPV_DISPLAY_LIBDIALOG = 0, /* Use dialog(3) (default) */
    DPV_DISPLAY_STDOUT,        /* Use stdout */
    DPV_DISPLAY_DIALOG,        /* Use spawned dialog(1) */
    DPV_DISPLAY_XDIALOG,       /* Use spawned Xdialog(1) */
};

enum dpv_output {
    DPV_OUTPUT_NONE = 0, /* No output (default) */
    DPV_OUTPUT_FILE,     /* Read `output' member as file path */
    DPV_OUTPUT_SHELL,    /* Read `output' member as shell cmd */
};
.Ed
.Pp
The
.Va options
member of the
.Fn dpv
.Fa config
argument is a mask of bit fields indicating various processing options.
Possible flags are as follows:
.Bl -tag -width DPV_NO_OVERRUN
.It Dv DPV_TEST_MODE
Enable test mode.
In test mode, the
.Fn action
callback of the
.Fa config
argument is not called but instead simulated-data is used to drive progress.
Appends
.Dq [TEST MODE]
to the status line
.Po
to override, set the
.Va status_format
member of the
.Fn dpv
.Fa config
argument;
e.g., to
.Dv DPV_STATUS_DEFAULT
.Pc .
.It Dv DPV_WIDE_MODE
Enable wide mode.
In wide mode, the length of the
.Va aprompt
and
.Va pprompt
members of the
.Fn dpv
.Fa config
argument will bump the width of the gauge widget.
Prompts wider than the maximum width will wrap
.Po
unless using
.Xr Xdialog 1 ;
see BUGS section below
.Pc .
.It Dv DPV_NO_LABELS
Disables the display of labels associated with each transfer
.Po
.Va label_size
member of
.Fn dpv
.Fa config
argument is ignored
.Pc .
.It Dv DPV_USE_COLOR
Force the use of color even if the
.Va display_type
does not support color
.Po
.Ev USE_COLOR
environment variable is ignored
.Pc .
.It Dv DPV_NO_OVERRUN
When enabled, callbacks for the current
.Vt dpv_file_node
are terminated when
.Fn action
returns 100 or greater
.Po
alleviates the need to change the
.Va status
of the current
.Vt dpv_file_node
but may also cause file truncation if the stream exceeds expected length
.Pc .
.El
.Pp
The
.Fa file_list
argument to
.Fn dpv
is a pointer to a
.Dq linked-list ,
described as follows in
.In dpv.h :
.Bd -literal -offset indent
struct dpv_file_node {
    enum dpv_status    status; /* status of read operation */
    char               *msg;   /* display instead of "Done/Fail" */
    char               *name;  /* name of file to read */
    char               *path;  /* path to file */
    long long          length; /* expected size */
    long long          read;   /* number units read (e.g., bytes) */
    struct dpv_file_node *next;/* pointer to next (end with NULL) */
};
.Ed
.Pp
For each of the items in the
.Fa file_list
.Dq linked-list
argument, the
.Fn action
callback member of the
.Fn dpv
.Fa config
argument is called.
The
.Fn action
function should perform a
.Dq nominal
action on the file and return.
The return value of
.Vt int
represents the current progress percentage
.Pq 0-100
for the current file.
.Pp
The
.Fn action
callback provides two variables for each call.
.Fa file
provides a reference to the current
.Vt dpv_file_node
being processed.
.Fa out
provides a file descriptor where the data should go.
.Pp
If the
.Va output
member of the
.Fn dpv
.Fa config
argument was set to DPV_OUTPUT_NONE
.Pq default ; when invoking Fn dpv ,
the
.Fa out
file descriptor of
.Fn action
will be zero and should be ignored.
If
.Fa output
was set to DPV_OUTPUT_FILE,
.Fa out
will be an open file descriptor to a file.
If
.Fa output
was set to DPV_OUTPUT_SHELL,
.Fa out
will be an open file descriptor to a pipe for a spawned shell program.
When
.Fa out
is greater than zero, you should write any data you have read back to
.Fa out .
.Pp
To abort
.Fn dpv ,
either from the
.Fn action
callback or asynchronously from a signal handler, two globals are provided via
.In dpv.h :
.Bd -literal -offset indent
extern int dpv_interrupt; /* Set to TRUE in interrupt handler */
extern int dpv_abort;     /* Set to true in callback to abort */
.Ed
.Pp
These globals are not automatically reset and must be manually maintained.
Don't forget to reset these globals before subsequent invocations of
.Fn dpv
when making multiple calls from the same program.
.Pp
In addition, the
.Va status
member of the
.Fn action
.Fa file
argument can be used to control callbacks for the current file.
The
.Va status
member can be set to any of the following from
.In dpv.h :
.Bd -literal -offset indent
enum dpv_status {
	DPV_STATUS_RUNNING = 0, /* Running (default) */
	DPV_STATUS_DONE,        /* Completed */
	DPV_STATUS_FAILED,      /* Oops, something went wrong */
};
.Ed
.Pp
The default
.Fa status
is zero, DPV_STATUS_RUNING, which keeps the callbacks coming for the current
.Fn file .
Setting
.Ql file->status
to anything other than DPV_STATUS_RUNNING will cause
.Fn dpv
to loop to the next file, effecting the next callback, if any.
.Pp
The
.Fn action
callback is responsible for calculating percentages and
.Pq recommended
maintaining a
.Nm
global counter so
.Fn dpv
can display throughput statistics.
Percentages are reported through the
.Vt int
return value of the
.Fn action
callback.
Throughput statistics are calculated from the following global
.Vt int
in
.In dpv.h :
.Bd -literal -offset indent
extern int dpv_overall_read;
.Ed
.Pp
This should be set to the number of bytes that have been read for all files.
Throughput information is displayed in the status line
.Pq only available when using Xr dialog 3
at the bottom of the screen.
See DPV_DISPLAY_LIBDIALOG above.
.Pp
Note that
.Va dpv_overall_read
does not have to represent bytes.
For example, you can change the
.Va status_format
to display something other than
.Dq Li bytes
and increment
.Va dpv_overall_read
accordingly
.Pq e.g., counting lines .
.Pp
When
.Fn dpv
is processing the current file, the
.Va length
and
.Va read
members of the
.Fn action
.Fa file
argument are used for calculating the display of mini progress bars
.Po
if enabled; see
.Va pbar_size
above
.Pc .
If the
.Va length
member of the current
.Fa file
is less than zero
.Pq indicating an unknown file length ,
a
.Xr humanize_number 3
version of the
.Va read
member is used instead of a traditional progress bar.
Otherwise a progress bar is calculated as percentage read to file length.
.Fn action
callback must maintain these member values for mini-progress bars.
.Pp
The
.Fn dpv_free
function performs
.Xr free 3
on private global variables initialized by
.Fn dpv .
.Sh ENVIRONMENT
The following environment variables are referenced by
.Nm :
.Bl -tag -width ".Ev USE_COLOR"
.It Ev DIALOG
Override command string used to launch
.Xr dialog 1
.Pq requires Dv DPV_DISPLAY_DIALOG
or
.Xr Xdialog 1
.Pq requires Dv DPV_DISPLAY_XDIALOG ;
default is either
.Ql dialog
.Pq for Dv DPV_DISPLAY_DIALOG
or
.Ql Xdialog
.Pq for Dv DPV_DISPLAY_XDIALOG .
.It Ev DIALOGRC
If set and non-NULL, path to
.Ql .dialogrc
file.
.It Ev HOME
If
.Ql Ev $DIALOGRC
is either not set or NULL, used as a prefix to
.Ql .dialogrc
.Pq i.e., Ql $HOME/.dialogrc .
.It Ev USE_COLOR
If set and NULL, disables the use of color when using
.Xr dialog 1
.Pq does not apply to Xr Xdialog 1 .
.It Ev msg_done Ev msg_fail Ev msg_pending
Internationalization strings for overriding the default English strings
.Ql Done ,
.Ql Fail ,
and
.Ql Pending
respectively.
To prevent their usage, explicitly set the
.Va msg_done ,
.Va msg_fail ,
and
.Va msg_pending
members of
.Fn dpv
.Fa config
argument to default macros
.Pq DPV_DONE_DEFAULT, DPV_FAIL_DEFAULT, and DPV_PENDING_DEFAULT
or desired values.
.El
.Sh FILES
.Bl -tag -width ".Pa $HOME/.dialogrc" -compact
.It Pa $HOME/.dialogrc
.El
.Sh SEE ALSO
.Xr dialog 1 ,
.Xr dialog 3 ,
.Xr Xdialog 1
.Sh HISTORY
The
.Nm
library first appeared in
.Fx 10.2 .
.Sh AUTHORS
.An Devin Teske Aq dteske@FreeBSD.org
.Sh BUGS
.Xr Xdialog 1 ,
when given both
.Ql Fl -title Ar title
.Po
see above
.Ql Va title
member of
.Va struct dpv_config
.Pc
and
.Ql Fl -backtitle Ar backtitle
.Po
see above
.Ql Va backtitle
member of
.Va struct dpv_config
.Pc ,
displays the backtitle in place of the title and vice-versa.
.Pp
.Xr Xdialog 1
does not wrap long prompt texts received after initial launch.
This is a known issue with the
.Ql --gauge
widget in
.Xr Xdialog 1 .
Embed escaped newlines within prompt text(s) to force line breaks.
.Pp
.Xr dialog 1
does not display the first character after a series of escaped escape-sequences
(e.g., ``\\\\n'' produces ``\\'' instead of ``\\n'').
This is a known issue with
.Xr dialog 1
and does not affect
.Xr dialog 3
or
.Xr Xdialog 1 .
.Pp
If your application ignores
.Ev USE_COLOR
when set and NULL before calling
.Xr dpv 3
with color escape sequences anyway,
.Xr dialog 3
and
.Xr dialog 1
may not render properly.
Workaround is to detect when
.Ev USE_COLOR
is set and NULL and either not use color escape sequences at that time or use
.Xr unsetenv 3
to unset
.Ev USE_COLOR ,
forcing interpretation of color sequences.
This does not effect
.Xr Xdialog 1 ,
which renders the color escape sequences as plain text.
See
.Do Li
embedded "\\Z" sequences
.Dc
in
.Xr dialog 1
for additional information.
OpenPOWER on IntegriCloud