1 /*
2  * State management functions.
3  */
4 
5 #include "pv-internal.h"
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 
12 /*
13  * Create a new state structure, and return it, or 0 (NULL) on error.
14  */
pv_state_alloc(const char * program_name)15 pvstate_t pv_state_alloc(const char *program_name)
16 {
17 	pvstate_t state;
18 
19 	state = calloc(1, sizeof(*state));
20 	if (NULL == state)
21 		return NULL;
22 
23 	state->program_name = program_name;
24 	state->watch_pid = 0;
25 	state->watch_fd = -1;
26 #ifdef HAVE_IPC
27 	state->crs_shmid = -1;
28 	state->crs_pvcount = 1;
29 #endif				/* HAVE_IPC */
30 	state->crs_lock_fd = -1;
31 
32 	state->reparse_display = 1;
33 	state->current_file = _("none");
34 #ifdef HAVE_SPLICE
35 	state->splice_failed_fd = -1;
36 #endif				/* HAVE_SPLICE */
37 	state->display_visible = false;
38 
39 	return state;
40 }
41 
42 
43 /*
44  * Free a state structure, after which it can no longer be used.
45  */
pv_state_free(pvstate_t state)46 void pv_state_free(pvstate_t state)
47 {
48 	if (0 == state)
49 		return;
50 
51 	if (state->display_buffer)
52 		free(state->display_buffer);
53 	state->display_buffer = NULL;
54 
55 	if (state->transfer_buffer)
56 		free(state->transfer_buffer);
57 	state->transfer_buffer = NULL;
58 
59 	free(state);
60 
61 	return;
62 }
63 
64 
65 /*
66  * Set the formatting string, given a set of old-style formatting options.
67  */
pv_state_set_format(pvstate_t state,bool progress,bool timer,bool eta,bool fineta,bool rate,bool average_rate,bool bytes,bool bufpercent,unsigned int lastwritten,const char * name)68 void pv_state_set_format(pvstate_t state, bool progress,
69 			 bool timer, bool eta,
70 			 bool fineta, bool rate,
71 			 bool average_rate, bool bytes,
72 			 bool bufpercent,
73 			 unsigned int lastwritten, const char *name)
74 {
75 #define PV_ADDFORMAT(x,y) if (x) { \
76 		if (state->default_format[0] != '\0') \
77 			strcat(state->default_format, " "); \
78 		strcat(state->default_format, y); \
79 	}
80 
81 	state->default_format[0] = '\0';
82 	PV_ADDFORMAT(name, "%N");
83 	PV_ADDFORMAT(bytes, "%b");
84 	PV_ADDFORMAT(bufpercent, "%T");
85 	PV_ADDFORMAT(timer, "%t");
86 	PV_ADDFORMAT(rate, "%r");
87 	PV_ADDFORMAT(average_rate, "%a");
88 	PV_ADDFORMAT(progress, "%p");
89 	PV_ADDFORMAT(eta, "%e");
90 	PV_ADDFORMAT(fineta, "%I");
91 	if (lastwritten > 0) {
92 		char buf[16];
93 		memset(buf, 0, sizeof(buf));
94 #ifdef HAVE_SNPRINTF
95 		(void) snprintf(buf, sizeof(buf) - 1, "%%%uA",
96 				lastwritten);
97 #else
98 		(void) sprintf(buf, "%%%uA", lastwritten);
99 #endif
100 		PV_ADDFORMAT(lastwritten > 0, buf);
101 	}
102 
103 	state->name = name;
104 	state->reparse_display = 1;
105 }
106 
107 
pv_state_force_set(pvstate_t state,bool val)108 void pv_state_force_set(pvstate_t state, bool val)
109 {
110 	state->force = val;
111 }
112 
pv_state_cursor_set(pvstate_t state,bool val)113 void pv_state_cursor_set(pvstate_t state, bool val)
114 {
115 	state->cursor = val;
116 };
117 
pv_state_numeric_set(pvstate_t state,bool val)118 void pv_state_numeric_set(pvstate_t state, bool val)
119 {
120 	state->numeric = val;
121 };
122 
pv_state_wait_set(pvstate_t state,bool val)123 void pv_state_wait_set(pvstate_t state, bool val)
124 {
125 	state->wait = val;
126 };
127 
pv_state_delay_start_set(pvstate_t state,double val)128 void pv_state_delay_start_set(pvstate_t state, double val)
129 {
130 	state->delay_start = val;
131 };
132 
pv_state_linemode_set(pvstate_t state,bool val)133 void pv_state_linemode_set(pvstate_t state, bool val)
134 {
135 	state->linemode = val;
136 };
137 
pv_state_null_set(pvstate_t state,bool val)138 void pv_state_null_set(pvstate_t state, bool val)
139 {
140 	state->null = val;
141 };
142 
pv_state_no_op_set(pvstate_t state,bool val)143 void pv_state_no_op_set(pvstate_t state, bool val)
144 {
145 	state->no_op = val;
146 };
147 
pv_state_skip_errors_set(pvstate_t state,unsigned int val)148 void pv_state_skip_errors_set(pvstate_t state, unsigned int val)
149 {
150 	state->skip_errors = val;
151 };
152 
pv_state_stop_at_size_set(pvstate_t state,bool val)153 void pv_state_stop_at_size_set(pvstate_t state, bool val)
154 {
155 	state->stop_at_size = val;
156 };
157 
pv_state_rate_limit_set(pvstate_t state,unsigned long long val)158 void pv_state_rate_limit_set(pvstate_t state, unsigned long long val)
159 {
160 	state->rate_limit = val;
161 };
162 
pv_state_target_buffer_size_set(pvstate_t state,unsigned long long val)163 void pv_state_target_buffer_size_set(pvstate_t state,
164 				     unsigned long long val)
165 {
166 	state->target_buffer_size = val;
167 };
168 
pv_state_no_splice_set(pvstate_t state,bool val)169 void pv_state_no_splice_set(pvstate_t state, bool val)
170 {
171 	state->no_splice = val;
172 };
173 
pv_state_size_set(pvstate_t state,unsigned long long val)174 void pv_state_size_set(pvstate_t state, unsigned long long val)
175 {
176 	state->size = val;
177 };
178 
pv_state_interval_set(pvstate_t state,double val)179 void pv_state_interval_set(pvstate_t state, double val)
180 {
181 	state->interval = val;
182 };
183 
pv_state_width_set(pvstate_t state,unsigned int val)184 void pv_state_width_set(pvstate_t state, unsigned int val)
185 {
186 	state->width = val;
187 };
188 
pv_state_height_set(pvstate_t state,unsigned int val)189 void pv_state_height_set(pvstate_t state, unsigned int val)
190 {
191 	state->height = val;
192 };
193 
pv_state_name_set(pvstate_t state,const char * val)194 void pv_state_name_set(pvstate_t state, const char *val)
195 {
196 	state->name = val;
197 };
198 
pv_state_format_string_set(pvstate_t state,const char * val)199 void pv_state_format_string_set(pvstate_t state, const char *val)
200 {
201 	state->format_string = val;
202 };
203 
pv_state_watch_pid_set(pvstate_t state,unsigned int val)204 void pv_state_watch_pid_set(pvstate_t state, unsigned int val)
205 {
206 	state->watch_pid = val;
207 };
208 
pv_state_watch_fd_set(pvstate_t state,int val)209 void pv_state_watch_fd_set(pvstate_t state, int val)
210 {
211 	state->watch_fd = val;
212 };
213 
214 
215 /*
216  * Set the array of input files.
217  */
pv_state_inputfiles(pvstate_t state,int input_file_count,const char ** input_files)218 void pv_state_inputfiles(pvstate_t state, int input_file_count,
219 			 const char **input_files)
220 {
221 	state->input_file_count = input_file_count;
222 	state->input_files = input_files;
223 }
224 
225 /* EOF */
226