Initial commit based on v1.6.6

This commit is contained in:
Andrew Wood
2021-09-04 20:51:25 +01:00
commit 12f28d0689
80 changed files with 12819 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
/*
* Replacement getopt function's header file. Include this AFTER config.h.
*/
#ifndef _LIBRARY_GETOPT_H
#define _LIBRARY_GETOPT_H 1
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifndef HAVE_GETOPT
int minigetopt(int, char **, char *);
extern char *minioptarg;
extern int minioptind, miniopterr, minioptopt;
#define getopt minigetopt /* Flawfinder: ignore */
#define optarg minioptarg
#define optind minioptind
#define opterr miniopterr
#define optopt minioptopt
#endif /* !HAVE_GETOPT */
#ifdef __cplusplus
}
#endif
#endif /* _LIBRARY_GETOPT_H */
/* EOF */
+52
View File
@@ -0,0 +1,52 @@
/*
* Replacement gettext library header file. Include this within config.h,
* like this:
*
* #ifdef ENABLE_NLS
* # include "library/gettext.h"
* #else
* # define _(String) (String)
* # define N_(String) (String)
* #endif
*
*/
#ifndef _LIBRARY_GETTEXT_H
#define _LIBRARY_GETTEXT_H 1
#ifdef HAVE_GETTEXT
# ifdef HAVE_LIBINTL_H
# include <libintl.h>
# endif
# ifdef HAVE_LOCALE_H
# include <locale.h>
# endif
# define _(String) gettext (String)
# define N_(String) (String)
#else
# define _(String) minigettext (String)
# define N_(String) (String)
# define setlocale minisetlocale
# define bindtextdomain minibindtextdomain
# define textdomain minitextdomain
# ifndef LC_ALL
# define LC_ALL ""
# endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
char *minisetlocale(char *, char *);
char *minibindtextdomain(char *, char *);
char *minitextdomain(char *);
char *minigettext(char *);
#ifdef __cplusplus
}
#endif
#endif /* _LIBRARY_GETTEXT_H */
/* EOF */
+64
View File
@@ -0,0 +1,64 @@
/*
* Global program option structure and the parsing function prototype.
*/
#ifndef _OPTIONS_H
#define _OPTIONS_H 1
#ifdef __cplusplus
extern "C" {
#endif
struct opts_s;
typedef struct opts_s *opts_t;
struct opts_s { /* structure describing run-time options */
char *program_name; /* name the program is running as */
unsigned char do_nothing; /* exit-without-doing-anything flag */
unsigned char progress; /* progress bar flag */
unsigned char timer; /* timer flag */
unsigned char eta; /* ETA flag */
unsigned char fineta; /* absolute ETA flag */
unsigned char rate; /* rate counter flag */
unsigned char average_rate; /* average rate counter flag */
unsigned char bytes; /* bytes transferred flag */
unsigned char bufpercent; /* transfer buffer percentage flag */
unsigned int lastwritten; /* show N bytes last written */
unsigned char force; /* force-if-not-terminal flag */
unsigned char cursor; /* whether to use cursor positioning */
unsigned char numeric; /* numeric output only */
unsigned char wait; /* wait for transfer before display */
unsigned char linemode; /* count lines instead of bytes */
unsigned char null; /* lines are null-terminated */
unsigned char no_op; /* do nothing other than pipe data */
unsigned long long rate_limit; /* rate limit, in bytes per second */
unsigned long long buffer_size;/* buffer size, in bytes (0=default) */
unsigned int remote; /* PID of pv to update settings of */
unsigned long long size; /* total size of data */
unsigned char no_splice; /* flag set if never to use splice */
unsigned char skip_errors; /* skip read errors flag */
unsigned char stop_at_size; /* set if we stop at "size" bytes */
double interval; /* interval between updates */
double delay_start; /* delay before first display */
unsigned int watch_pid; /* process to watch fds of */
int watch_fd; /* fd to watch */
unsigned int width; /* screen width */
unsigned int height; /* screen height */
char *name; /* process name, if any */
char *format; /* output format, if any */
char *pidfile; /* PID file, if any */
int argc; /* number of non-option arguments */
char **argv; /* array of non-option arguments */
};
extern opts_t opts_parse(int, char **);
extern void opts_free(opts_t);
#ifdef __cplusplus
}
#endif
#endif /* _OPTIONS_H */
/* EOF */
+263
View File
@@ -0,0 +1,263 @@
/*
* Functions internal to the PV library.
*/
#ifndef _PV_INTERNAL_H
#define _PV_INTERNAL_H 1
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef _PV_H
#include "pv.h"
#endif /* _PV_H */
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#ifdef __cplusplus
extern "C" {
#endif
#define PV_DISPLAY_PROGRESS 1
#define PV_DISPLAY_TIMER 2
#define PV_DISPLAY_ETA 4
#define PV_DISPLAY_RATE 8
#define PV_DISPLAY_AVERAGERATE 16
#define PV_DISPLAY_BYTES 32
#define PV_DISPLAY_NAME 64
#define PV_DISPLAY_BUFPERCENT 128
#define PV_DISPLAY_OUTPUTBUF 256
#define PV_DISPLAY_FINETA 512
#define RATE_GRANULARITY 100000 /* usec between -L rate chunks */
#define REMOTE_INTERVAL 100000 /* usec between checks for -R */
#define BUFFER_SIZE 409600 /* default transfer buffer size */
#define BUFFER_SIZE_MAX 524288 /* max auto transfer buffer size */
#define MAX_READ_AT_ONCE 524288 /* max to read() in one go */
#define MAX_WRITE_AT_ONCE 524288 /* max to write() in one go */
#define TRANSFER_READ_TIMEOUT 90000 /* usec to time reads out at */
#define TRANSFER_WRITE_TIMEOUT 900000 /* usec to time writes out at */
#define MAXIMISE_BUFFER_FILL 1
/*
* Structure for holding PV internal state. Opaque outside the PV library.
*/
struct pvstate_s {
/***************
* Input files *
***************/
int input_file_count; /* number of input files */
const char **input_files; /* input files (0=first) */
/*******************
* Program control *
*******************/
unsigned char force; /* display even if not on terminal */
unsigned char cursor; /* use cursor positioning */
unsigned char numeric; /* numeric output only */
unsigned char wait; /* wait for data before display */
unsigned char linemode; /* count lines instead of bytes */
unsigned char null; /* lines are null-terminated */
unsigned char no_op; /* do nothing other than pipe data */
unsigned char skip_errors; /* skip read errors flag */
unsigned char stop_at_size; /* set if we stop at "size" bytes */
unsigned char no_splice; /* never use splice() */
unsigned long long rate_limit; /* rate limit, in bytes per second */
unsigned long long target_buffer_size; /* buffer size (0=default) */
unsigned long long size; /* total size of data */
double interval; /* interval between updates */
double delay_start; /* delay before first display */
unsigned int watch_pid; /* process to watch fds of */
int watch_fd; /* fd to watch */
unsigned int width; /* screen width */
unsigned int height; /* screen height */
const char *name; /* display name */
char default_format[512]; /* default format string */
const char *format_string; /* output format string */
/******************
* Program status *
******************/
const char *program_name; /* program name for error reporting */
const char *current_file; /* current file being read */
int exit_status; /* exit status to give (0=OK) */
/*******************
* Signal handling *
*******************/
int pv_sig_old_stderr; /* see pv_sig_ttou() */
struct timeval pv_sig_tstp_time; /* see pv_sig_tstp() / __cont() */
struct timeval pv_sig_toffset; /* total time spent stopped */
volatile sig_atomic_t pv_sig_newsize; /* whether we need to get term size again */
volatile sig_atomic_t pv_sig_abort; /* whether we need to abort right now */
volatile sig_atomic_t reparse_display; /* whether to re-check format string */
struct sigaction pv_sig_old_sigpipe;
struct sigaction pv_sig_old_sigttou;
struct sigaction pv_sig_old_sigtstp;
struct sigaction pv_sig_old_sigcont;
struct sigaction pv_sig_old_sigwinch;
struct sigaction pv_sig_old_sigint;
struct sigaction pv_sig_old_sighup;
struct sigaction pv_sig_old_sigterm;
/*****************
* Display state *
*****************/
long percentage;
long double prev_elapsed_sec;
long double prev_rate;
long double prev_trans;
unsigned long long initial_offset;
char *display_buffer;
long display_buffer_size;
int lastoutput_length; /* number of last-output bytes to show */
unsigned char lastoutput_buffer[256];
int prev_width; /* screen width last time we were called */
int prev_length; /* length of last string we output */
char str_name[512];
char str_transferred[128];
char str_bufpercent[128];
char str_timer[128];
char str_rate[128];
char str_average_rate[128];
char str_progress[1024];
char str_lastoutput[512];
char str_eta[128];
char str_fineta[128];
unsigned long components_used; /* bitmask of components used */
struct {
const char *string;
int length;
} format[100];
unsigned char display_visible; /* set once anything written to terminal */
/********************
* Cursor/IPC state *
********************/
#ifdef HAVE_IPC
int crs_shmid; /* ID of our shared memory segment */
int crs_pvcount; /* number of `pv' processes in total */
int crs_pvmax; /* highest number of `pv's seen */
int *crs_y_top; /* pointer to Y coord of topmost `pv' */
int crs_y_lastread; /* last value of _y_top seen */
int crs_y_offset; /* our Y offset from this top position */
int crs_needreinit; /* set if we need to reinit cursor pos */
int crs_noipc; /* set if we can't use IPC */
#endif /* HAVE_IPC */
int crs_lock_fd; /* fd of lockfile, -1 if none open */
char crs_lock_file[1024];
int crs_y_start; /* our initial Y coordinate */
/*******************
* Transfer state *
*******************/
/*
* The transfer buffer is used for moving data from the input files
* to the output when splice() is not available.
*
* If buffer_size is smaller than pv__target_bufsize, then
* pv_transfer will try to reallocate transfer_buffer to make
* buffer_size equal to pv__target_bufsize.
*
* Data from the input files is read into the buffer; read_position
* is the offset in the buffer that we've read data up to.
*
* Data is written to the output from the buffer, and write_position
* is the offset in the buffer that we've written data up to. It
* will always be less than or equal to read_position.
*/
unsigned char *transfer_buffer; /* data transfer buffer */
unsigned long long buffer_size; /* size of buffer */
unsigned long read_position; /* amount of data in buffer */
unsigned long write_position; /* buffered data written */
/*
* While reading from a file descriptor we keep track of how many
* times in a row we've seen errors (read_errors_in_a_row), and
* whether or not we have put a warning on stderr about read errors
* on this fd (read_error_warning_shown).
*
* Whenever the active file descriptor changes from
* last_read_skip_fd, we reset read_errors_in_a_row and
* read_error_warning_shown to 0 for the new file descriptor and set
* last_read_skip_fd to the new fd number.
*
* This way, we're treating each input file separately.
*/
int last_read_skip_fd;
unsigned long read_errors_in_a_row;
int read_error_warning_shown;
#ifdef HAVE_SPLICE
/*
* These variables are used to keep track of whether splice() was
* used; splice_failed_fd is the file descriptor that splice() last
* failed on, so that we don't keep trying to use it on an fd that
* doesn't support it, and splice_used is set to 1 if splice() was
* used this time within pv_transfer().
*/
int splice_failed_fd;
int splice_used;
#endif
long to_write; /* max to write this time around */
long written; /* bytes sent to stdout this time */
};
struct pvwatchfd_s {
unsigned int watch_pid; /* PID to watch */
int watch_fd; /* fd to watch, -1 = not displayed */
char file_fdinfo[4096]; /* path to /proc fdinfo file */
char file_fd[4096]; /* path to /proc fd symlink */
char file_fdpath[4096]; /* path to file that was opened */
char display_name[512]; /* name to show on progress bar */
struct stat64 sb_fd; /* stat of fd symlink */
struct stat64 sb_fd_link; /* lstat of fd symlink */
unsigned long long size; /* size of whole file, 0 if unknown */
long long position; /* position last seen at */
struct timeval start_time; /* time we started watching the fd */
};
typedef struct pvwatchfd_s *pvwatchfd_t;
void pv_error(pvstate_t, char *, ...);
int pv_main_loop(pvstate_t);
void pv_display(pvstate_t, long double, long long, long long);
long pv_transfer(pvstate_t, int, int *, int *, unsigned long long, long *);
void pv_set_buffer_size(unsigned long long, int);
int pv_next_file(pvstate_t, int, int);
void pv_crs_fini(pvstate_t);
void pv_crs_init(pvstate_t);
void pv_crs_update(pvstate_t, char *);
#ifdef HAVE_IPC
void pv_crs_needreinit(pvstate_t);
#endif
void pv_sig_allowpause(void);
void pv_sig_checkbg(void);
void pv_sig_nopause(void);
void pv_remote_init(pvstate_t);
void pv_remote_check(pvstate_t);
void pv_remote_fini(pvstate_t);
int pv_remote_set(pvstate_t);
int pv_watchfd_info(pvstate_t, pvwatchfd_t, int);
int pv_watchfd_changed(pvwatchfd_t);
long long pv_watchfd_position(pvwatchfd_t);
int pv_watchpid_scanfds(pvstate_t, pvstate_t, unsigned int, int *, pvwatchfd_t *, pvstate_t *, int *);
void pv_watchpid_setname(pvstate_t, pvwatchfd_t);
#ifdef __cplusplus
}
#endif
#endif /* _PV_INTERNAL_H */
/* EOF */
+165
View File
@@ -0,0 +1,165 @@
/*
* Functions used across the program.
*/
#ifndef _PV_H
#define _PV_H 1
#ifdef __cplusplus
extern "C" {
#endif
/*
* Opaque structure for PV internal state.
*/
struct pvstate_s;
typedef struct pvstate_s *pvstate_t;
/*
* Valid number types for pv_getnum_check().
*/
typedef enum {
PV_NUMTYPE_INTEGER,
PV_NUMTYPE_DOUBLE
} pv_numtype_t;
/*
* Simple string functions for processing numbers.
*/
/*
* Return the given string converted to a double.
*/
extern double pv_getnum_d(const char *);
/*
* Return the given string converted to an integer.
*/
extern int pv_getnum_i(const char *);
/*
* Return the given string converted to a long long.
*/
extern long long pv_getnum_ll(const char *);
/*
* Return zero if the given string is a number of the given type. NB an
* integer is both a valid integer and a valid double.
*/
extern int pv_getnum_check(const char *, pv_numtype_t);
/*
* Main PV functions.
*/
/*
* Create a new state structure, and return it, or 0 (NULL) on error.
*/
extern pvstate_t pv_state_alloc(const char *);
/*
* Set the formatting string, given a set of old-style formatting options.
*/
extern void pv_state_set_format(pvstate_t state, unsigned char progress,
unsigned char timer, unsigned char eta,
unsigned char fineta, unsigned char rate,
unsigned char average_rate, unsigned char bytes,
unsigned char bufpercent,
unsigned int lastwritten,
const char *name);
/*
* Set the various options.
*/
extern void pv_state_force_set(pvstate_t, unsigned char);
extern void pv_state_cursor_set(pvstate_t, unsigned char);
extern void pv_state_numeric_set(pvstate_t, unsigned char);
extern void pv_state_wait_set(pvstate_t, unsigned char);
extern void pv_state_delay_start_set(pvstate_t, double);
extern void pv_state_linemode_set(pvstate_t, unsigned char);
extern void pv_state_null_set(pvstate_t, unsigned char);
extern void pv_state_no_op_set(pvstate_t, unsigned char);
extern void pv_state_skip_errors_set(pvstate_t, unsigned char);
extern void pv_state_stop_at_size_set(pvstate_t, unsigned char);
extern void pv_state_rate_limit_set(pvstate_t, unsigned long long);
extern void pv_state_target_buffer_size_set(pvstate_t, unsigned long long);
extern void pv_state_no_splice_set(pvstate_t, unsigned char);
extern void pv_state_size_set(pvstate_t, unsigned long long);
extern void pv_state_interval_set(pvstate_t, double);
extern void pv_state_width_set(pvstate_t, unsigned int);
extern void pv_state_height_set(pvstate_t, unsigned int);
extern void pv_state_name_set(pvstate_t, const char *);
extern void pv_state_format_string_set(pvstate_t, const char *);
extern void pv_state_watch_pid_set(pvstate_t, unsigned int);
extern void pv_state_watch_fd_set(pvstate_t, int);
extern void pv_state_inputfiles(pvstate_t, int, const char **);
/*
* Work out the terminal size.
*/
extern void pv_screensize(unsigned int *width, unsigned int *height);
/*
* Calculate the total size of all input files.
*/
extern unsigned long long pv_calc_total_size(pvstate_t);
/*
* Set up signal handlers ready for running the main loop.
*/
extern void pv_sig_init(pvstate_t);
/*
* Enter the main transfer loop, transferring all input files to the output.
*/
extern int pv_main_loop(pvstate_t);
/*
* Watch the selected file descriptor of the selected process.
*/
extern int pv_watchfd_loop(pvstate_t);
/*
* Watch the selected process.
*/
extern int pv_watchpid_loop(pvstate_t);
/*
* Shut down signal handlers after running the main loop.
*/
extern void pv_sig_fini(pvstate_t);
/*
* Free a state structure, after which it can no longer be used.
*/
extern void pv_state_free(pvstate_t);
#ifdef ENABLE_DEBUGGING
# if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
# define __func__ __FUNCTION__
# else
# define __func__ "<unknown>"
# endif
# endif
# define debug(x,...) debugging_output(__func__, __FILE__, __LINE__, x, __VA_ARGS__)
#else
# define debug(x,...) do { } while (0)
#endif
/*
* Output debugging information, if debugging is enabled.
*/
void debugging_output(const char *, const char *, int, const char *, ...);
#ifdef __cplusplus
}
#endif
#endif /* _PV_H */
/* EOF */