-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwbh_support.c
453 lines (383 loc) · 10.8 KB
/
pwbh_support.c
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
#include "pwb_builtin.h"
#include "pwb_handle.h"
// Shadow curses `trace` to hide from bash `trace`
#define trace trace_curses
#include <curses.h>
#include <term.h>
// remove shadow to continue
#undef trace
#include <assert.h>
#include <stdio.h>
#include <contools.h>
#include <pager.h>
/**
* @brief Dispose of item referenced by SHELL_VAR::value.
* @param "var" variable whose contents are to be disposed of.
*
* Does the job of Bash's unavailable internal function,
* dispose_variable_value. Frees the memory pointed to by value
* member according to its type. The `value` member of the
* SHELL_VAR will be NULL when done.
*/
void pwb_dispose_variable_value(SHELL_VAR *var)
{
if (array_p(var))
array_dispose(array_cell(var));
else if (assoc_p(var))
assoc_dispose(assoc_cell(var));
else if (function_p(var))
dispose_command(function_cell(var));
else if (nameref_p(var))
FREE(nameref_cell(var));
else
FREE(value_cell(var));
var->value = NULL;
}
bool pwbh_p(SHELL_VAR *var)
{
if (specialvar_p(var))
{
PWBH *pwbh = pwbh_cell(var);
return strcmp(pwbh->idstring, PWB_ID_STRING)==0;
}
return false;
}
PWB_RESULT get_pwb_handle(PWBH **handle, const char *name)
{
PWB_RESULT result = PWB_FAILURE;
*handle = NULL;
SHELL_VAR *sv = find_variable(name);
if (sv)
{
if (pwbh_p(sv))
{
*handle = pwbh_cell(sv);
result = PWB_SUCCESS;
}
else
result = PWB_INVALID_HANDLE;
}
else
result = PWB_INVALID_VARIABLE;
return result;
}
/**
* @defgroup MARGIN_RELATIVE_POSITIONING
* @{
*/
bool pwbh_position_to_head(PWBH *pwbh)
{
int top_lines = pwbh->dparms.margin_top;
if (top_lines > 0)
{
ti_set_cursor_position(0, pwbh->dparms.margin_left);
return true;
}
return false;
}
bool pwbh_position_to_foot(PWBH *pwbh)
{
DPARMS *dparms = &pwbh->dparms;
int bottom_lines = dparms->margin_bottom;
if (bottom_lines > 0)
{
int pos_foot = dparms->margin_top + dparms->line_count;
ti_set_cursor_position(pos_foot, dparms->margin_left);
return true;
}
return false;
}
bool pwbh_position_to_left(PWBH *pwbh)
{
DPARMS *dparms = &pwbh->dparms;
int left_chars = dparms->margin_left;
if (left_chars > 0)
{
int top = dparms->margin_top;
ti_set_cursor_position(top, 1);
return true;
}
return false;
}
bool pwbh_position_to_right(PWBH *pwbh)
{
DPARMS *dparms = &pwbh->dparms;
int right_chars = dparms->margin_right;
if (right_chars > 0)
{
int right = dparms->margin_left + dparms->chars_count;
int top = dparms->margin_top;
ti_set_cursor_position(top, right);
return true;
}
return false;
}
/** @} */
/**
* Terminal setup stuff
*/
int pwb_terminal_init(void)
{
int error_return;
int result = setupterm((char*)NULL, STDIN_FILENO, &error_return);
if (result)
{
switch(error_return)
{
case 1: printf("curses unavailable in hardcopy mode.\n"); break;
case 0: printf("curses unavailable in generic terminal.\n"); break;
case -1: printf("curses unavailable, terminfo database missing.\n"); break;
}
}
return result;
}
int pwb_execute_command(WORD_LIST *wl)
{
int cmd_flags = CMD_INHIBIT_EXPANSION | CMD_STDPATH;
COMMAND *command = make_bare_simple_command();
command->value.Simple->words = wl;
command->value.Simple->redirects = (REDIRECT*)NULL;
command->flags = command->value.Simple->flags = cmd_flags;
return execute_command(command);
}
// Prototype to make function available in pwb_line_printer
// without moving it from near related functions.
void pwbh_print_set_shell_function(PWBH *pwbh, const char *func_name);
/**
* @brief Default auto-hilighting printer function for DPARMS
*/
int pwb_line_printer(int row_index,
int focus,
int length,
void *data_source,
void *data_extra)
{
PWBH *ph = (PWBH*)data_extra;
// update WORD_LIST values:
pwbh_print_set_row_index(ph, row_index);
pwbh_print_set_focus(ph, focus);
if (focus)
start_standout_mode();
int result = pwb_execute_command(ph->printer_wl);
if (focus)
{
stop_standout_mode();
if (ph->print_func_head && pwbh_position_to_head(ph))
{
pwbh_print_set_shell_function(ph, ph->print_func_head);
result = pwb_execute_command(ph->printer_wl);
}
if (!result && ph->print_func_foot && pwbh_position_to_foot(ph))
{
pwbh_print_set_shell_function(ph, ph->print_func_foot);
result = pwb_execute_command(ph->printer_wl);
}
if (!result && ph->print_func_left && pwbh_position_to_left(ph))
{
pwbh_print_set_length(ph, ph->dparms.margin_left);
pwbh_print_set_shell_function(ph, ph->print_func_left);
result = pwb_execute_command(ph->printer_wl);
}
if (!result && ph->print_func_right && pwbh_position_to_right(ph))
{
pwbh_print_set_length(ph, ph->dparms.margin_right);
pwbh_print_set_shell_function(ph, ph->print_func_right);
result = pwb_execute_command(ph->printer_wl);
}
// Restore default print function when finished printing margins
pwbh_print_set_length(ph, ph->dparms.chars_count);
pwbh_print_set_shell_function(ph, NULL);
}
return result;
}
/**
* @brief Alternate non-hilighting printer function for DPARMS
*/
int pwb_raw_line_printer(int row_index,
int focus,
int length,
void *data_source,
void *data_extra)
{
PWBH *ph = (PWBH*)data_extra;
// update WORD_LIST values:
pwbh_print_set_row_index(ph, row_index);
pwbh_print_set_focus(ph, focus);
return pwb_execute_command(ph->printer_wl);
}
/**
* @brief General function to print either header or footer
*/
int pwb_margin_printer(PWBH *handle, bool header)
{
int result = PWB_SUCCESS;
// Set function-wide DPARMS values
DPARMS *dparms = &handle->dparms;
pwbh_print_set_row_index(handle, dparms->index_row_focus);
int col_to_start = dparms->chars_left;
int line_to_start, lines_to_print;
if (header)
{
line_to_start = 0;
lines_to_print = dparms->margin_top;
}
else
{
line_to_start = dparms->line_bottom;
lines_to_print = dparms->line_count;
}
int margin_line = line_to_start;
for (int i=0; result==PWB_SUCCESS && i < lines_to_print; ++i)
{
ti_set_cursor_position(margin_line, col_to_start);
pwbh_print_set_focus(handle, i);
result = pwb_execute_command(handle->printer_wl);
++margin_line;
}
return result;
}
/**
* @brief For assertions to ensure a index is not out-of-range
*
* Forget about the WORD_LIST being an array, count the elements
* using the linked list pointers.
*/
int word_list_count(const WORD_LIST *wl)
{
int count = 0;
while (wl)
{
++count;
wl = wl->next;
}
return count;
}
/**
* @brief Calculate memory requirements for WORD_LIST of @p elements elements
* and @p int_els integer vaules.
*/
int pwb_calc_word_list_size(int elements, int int_els)
{
return elements * (sizeof(WORD_LIST) + sizeof(WORD_DESC))
+ int_els * WORD_LIST_INT_SIZE;
}
void pwb_truncate_word_list_at_index(WORD_LIST *wl, int position)
{
assert(position < word_list_count(wl));
wl[position].next = NULL;
}
/**
* @brief Set WORD_DESC::word to buffer at specified WORD_LIST position
*
* Important note: the WORD_LIST is both and array and a linked list.
* We exploit the array arrangement to directly find the correct WORD_DESC
*/
void pwb_initialize_word_list_int_arg(WORD_LIST *wl, int position, char *buffer)
{
assert(position < word_list_count(wl));
WORD_DESC *wd = wl[position].word;
wd->word = buffer;
// Set string value to '0' (remember it's now a string of \0):
*buffer = '0';
}
/**
* @brief Set the WORD_DESC::word pointer for the specified WORD_LIST argument
*
* Important note: the WORD_LIST is both and array and a linked list.
* We exploit the array arrangement to directly find the correct WORD_DESC
*/
void pwb_set_word_list_string_arg(WORD_LIST *wl, int position, const char *string)
{
assert(position < word_list_count(wl));
WORD_DESC *wd = wl[position].word;
wd->word = (char*)string;
}
/**
* @brief Write the string value of an integer to the WORD_DESC::word buffer
* for the specified WORD_LIST argument.
*
* Important note: the WORD_LIST is both and array and a linked list.
* We exploit the array arrangement to directly find the correct WORD_DESC
*/
void pwb_set_word_list_int_val(WORD_LIST *wl, int position, int value)
{
assert(position < word_list_count(wl));
char *buff = wl[position].word->word;
int written = snprintf(buff, WORD_LIST_INT_SIZE, "%d", value);
assert(written < WORD_LIST_INT_SIZE);
}
/**
* @brief Access to data_source name in DPARMS
*/
const char *pwbh_get_name_data_source(const PWBH * pwbh)
{
return (char*)pwbh->dparms.data_source;
}
int pwbh_get_length_data_source(const PWBH *pwbh)
{
return pwbh->dparms.row_count;
}
/**
* @brief Access to data_extra name in DPARMS
*/
const char *pwbh_get_name_data_extra(const PWBH * pwbh)
{
return (char*)pwbh->dparms.data_extra;
}
/**
* @brief Access to shell printer function name in printer word_list
*/
const char *pwbh_get_name_printer(const PWBH * pwbh)
{
return pwbh->printer_wl->word->word;
}
/**
* @brief Access to shell exec function name in exec word_list
*/
const char *pwbh_get_name_exec(const PWBH * pwbh)
{
if (pwbh->exec_wl)
return pwbh->exec_wl->word->word;
else
return NULL;
}
void pwbh_print_set_shell_function(PWBH *pwbh, const char *func_name)
{
// Make it easy to return to default (mandatory) print func
if (func_name == NULL)
func_name = pwbh->print_func_line;
pwb_set_word_list_string_arg(pwbh->printer_wl, 0, func_name);
}
void pwbh_print_set_row_index(PWBH *pwbh, int value)
{
pwb_set_word_list_int_val(pwbh->printer_wl, 1, value);
}
void pwbh_print_set_length(PWBH *pwbh, int value)
{
pwb_set_word_list_int_val(pwbh->printer_wl, 3, value);
}
void pwbh_print_set_focus(PWBH *pwbh, int value)
{
pwb_set_word_list_int_val(pwbh->printer_wl, 4, value);
}
void pwbh_exec_set_row_number(PWBH *pwbh, int value)
{
pwb_set_word_list_int_val(pwbh->exec_wl, 2, value);
}
/**
* @brief Set the row_number WORD_LIST param to the focus row
*/
void pwbh_exec_update_row_number(PWBH *pwbh)
{
pwb_set_word_list_int_val(pwbh->exec_wl, 2, pwbh->dparms.index_row_focus);
}
void pwbh_exec_set_keystroke(PWBH *pwbh, const char *keystroke)
{
pwb_set_word_list_string_arg(pwbh->exec_wl, 1, keystroke);
}
void pwbh_calc_borders(PWBH *pwbh)
{
DPARMS *dparms = &pwbh->dparms;
pager_calc_borders(dparms);
pwbh_print_set_length(pwbh, dparms->chars_count);
}