-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_actions.c
715 lines (609 loc) · 20.3 KB
/
pwb_actions.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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#include "pwb_actions.h"
#include <argeater.h>
#include <pager.h>
#include <contools.h>
#include <stdio.h>
#include <stdlib.h> // for malloc/free
#include <alloca.h>
#include <math.h> // for log10
#include "pwb_handle.h"
#include "pwb_errors.h"
#include "pwb_argeater.h"
#include "pwb_keymap.h"
void print_the_word_list(WORD_LIST *wl)
{
WORD_LIST *ptr = wl;
int count=0;
while (ptr)
{
printf("%d: %s.\n", ++count, ptr->word->word);
ptr = ptr->next;
}
}
PWB_RESULT pwb_action_dummy(PWBH *handle, ACLONE *args)
{
printf("In the non-operational function, placeholder_runner.\n");
return PWB_SUCCESS;
}
PWB_RESULT pwb_action_version(PWBH *handle, ACLONE *args)
{
printf("pwb version 0.0.9\n");
return PWB_SUCCESS;
}
/**
* @brief Prints visible characters of string to limit in first argument
*
* Note that this function will get a NULL handle value because it's
* a 'waives_handle' function.
*
* This function will print characters found in the string, but only
* count those NOT IN a CSI sequence. Trailing CSI sequences will be
* printed, even after the maximum visible characters are printed, to
* ensure cleanup CSI will be sent to the terminal.
*/
PWB_RESULT pwb_action_limit_print(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_FAILURE;
unsigned int len = -1;
const char *string = NULL;
AE_ITEM items[] = {
{ (const char **)&len, "length", '\0', AET_ARGUMENT,
"maximum screen characters to print", NULL, pwb_argeater_unsigned_int_setter },
{ &string, "string", '\0', AET_ARGUMENT,
"string to print" }
};
AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
if (string==NULL)
(*error_sink)("missing string to print");
else if (len < 0)
(*error_sink)("Invalid string length (%d)", len);
result = PWB_SUCCESS;
if (len > 0)
{
bool in_csi = false;
int count = 0;
const char *ptr = string;
while (*ptr)
{
if (in_csi)
{
// Letter terminates a CSI, we're not discriminating beyond that
if ((*ptr>='a' && *ptr<='z') || (*ptr>='A' && *ptr<='Z'))
in_csi = false;
// Only numerals and ';' in CSI expression, otherwise it's an error
else if (!((*ptr>='0' && *ptr <='9') || *ptr==';'))
{
(*error_sink)("at char position %d, unexpected character '%c'"
" (%d) in CSI expression",
ptr - string, *ptr, *ptr);
result = PWB_FAILURE;
break;
}
// We're not counting 'em, just print and move along
putc(*ptr, stdout);
++ptr;
continue;
}
// Check if we're starting a CSI
else if (*ptr=='\x1b')
{
// Abort if escape does not start CSI sequence
if (*(ptr+1)!='[')
{
(*error_sink)("ate char position %d, unexpected character '%c' "
" (%d) following an escape character",
ptr - string, *ptr, *ptr);
result = PWB_FAILURE;
break;
}
putc(*ptr++, stdout);
putc(*ptr++, stdout);
in_csi = true;
continue;
}
if (count < len)
putc(*ptr, stdout);
++count;
++ptr;
}
}
}
return result;
}
PWB_RESULT pwb_action_measure_string(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_FAILURE;
const char *var_name="PWB_VALUE";
const char *string=NULL;
AE_ITEM items[] = {
{ &string, "string", '\0', AET_ARGUMENT,
"string to measure" },
{ &var_name, "var", 'v', AET_VALUE_OPTION,
"Alternate to 'PWB_VALUE' for reporting result" }
};
AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
if (string==NULL)
(*error_sink)("missing string to measure");
else
{
bool in_csi = false;
int count = 0;
const char *ptr = string;
while (*ptr)
{
if (in_csi)
{
// Letter terminates a CSI, we're not discriminating beyond that
if ((*ptr>='a' && *ptr<='z') || (*ptr>='A' && *ptr<='Z'))
in_csi = false;
// Only numerals and ';' in CSI expression, otherwise it's an error
else if (!((*ptr>='0' && *ptr <='9') || *ptr==';'))
{
(*error_sink)("at char position %d, unexpected character '%c'"
" (%d) in CSI expression",
ptr - string, *ptr, *ptr);
break;
}
++ptr;
continue;
}
// Check if we're starting a CSI
else if (*ptr=='\x1b')
{
// Abort if escape does not start CSI sequence
if (*(ptr+1)!='[')
{
(*error_sink)("ate char position %d, unexpected character '%c' "
" (%d) following an escape character",
ptr - string, *ptr, *ptr);
break;
}
ptr+=2;
in_csi = true;
continue;
}
// Don't let the increment languish under a conditional,
// or we'll never get out of here:
++ptr;
++count;
}
// Don't attempt log10 on a zero value, but
// make sure there's room for a '0'
int num_length = count==0?1:(floor(log10(count))+1);
// Room for \0 terminator
++num_length;
char *buff = xmalloc(num_length);
if (buff)
{
SHELL_VAR *sv = find_variable(var_name);
if (!sv)
sv = bind_variable(var_name, "", 0);
if (sv)
{
snprintf(buff, num_length, "%d", count);
pwb_dispose_variable_value(sv);
sv->value = buff;
if (invisible_p(sv))
VUNSETATTR(sv, att_invisible);
result = PWB_SUCCESS;
}
}
}
}
return result;
}
PWB_RESULT pwb_action_get_keystroke(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_SUCCESS;
const char *var_name="PWB_VALUE";
AE_ITEM items[] = {
{ &var_name, "var", 'v', AET_VALUE_OPTION,
"Alternate to 'PWB_VALUE' for reporting result" }
};
AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
const char *keys = get_keystroke(NULL, 0);
if (keys)
{
int keylen = strlen(keys);
if (keylen > 0)
{
SHELL_VAR *sv = find_variable(var_name);
if (!sv)
sv = bind_variable(var_name, "", 0);
if (sv)
{
pwb_dispose_variable_value(sv);
sv->value = savestring(keys);
if (invisible_p(sv))
VUNSETATTR(sv, att_invisible);
result = PWB_SUCCESS;
}
}
}
}
return result;
}
PWB_RESULT pwb_action_init(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_SUCCESS;
pwb_terminal_init();
pager_init();
ti_hide_cursor();
return result;
}
PWB_RESULT pwb_action_restore(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_SUCCESS;
pager_cleanup();
ti_show_cursor();
return result;
}
PWB_RESULT pwb_action_declare(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_FAILURE;
static const char *handle_name = NULL;
static const char *data_source = NULL;
static int data_count = -1;
static const char *print_func = NULL;
static const char *exec_func = NULL;
static const char *extra_data = NULL;
static const char *use_non_hilite_print = NULL;
static const char *head_print_func = NULL;
static const char *foot_print_func = NULL;
static const char *left_print_func = NULL;
static const char *right_print_func = NULL;
static AE_ITEM items[] = {
{ &handle_name, "handle_name", '\0', AET_ARGUMENT,
"name of new handle" },
{ &data_source, "data_source", '\0', AET_ARGUMENT,
"name of data source" },
{ (const char **)&data_count, "data_count", '\0', AET_ARGUMENT,
"lines in data source", NULL, pwb_argeater_int_setter },
{ &print_func, "printer", '\0', AET_ARGUMENT,
"name of line print function", NULL, pwb_argeater_function_setter },
{ &exec_func, "exec", 'e', AET_VALUE_OPTION,
"name of line execute function", NULL, pwb_argeater_function_setter},
{ &extra_data, "extra_data", 'd', AET_VALUE_OPTION,
"name of extra data variable" },
{ &head_print_func, "top_printer", 't', AET_VALUE_OPTION,
"name of function to print top margin",
NULL, pwb_argeater_function_setter },
{ &foot_print_func, "bottom_printer", 'b', AET_VALUE_OPTION,
"name of function to print bottom margin",
NULL, pwb_argeater_function_setter },
{ &left_print_func, "left_printer", 'l', AET_VALUE_OPTION,
"name of function to print left margin",
NULL, pwb_argeater_function_setter },
{ &right_print_func, "right_printer", 'r', AET_VALUE_OPTION,
"name of function to print right margin",
NULL, pwb_argeater_function_setter },
{ &use_non_hilite_print, "no_hilites", 'h', AET_FLAG_OPTION,
"do not auto-hilite focus lines" }
};
AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
// Validate a few parameters
if ( data_count < 0 )
{
(*error_sink)("PWB action 'declare' requires a non-negative data count value.");
goto early_exit;
}
if ( print_func == NULL || handle_name == NULL || data_source == NULL )
{
(*error_sink)("PWB action 'declare' requires a handle, a data_source, and a print function.");
goto early_exit;
}
int handle_size = pwb_calc_handle_size(data_source,
print_func,
handle_name,
exec_func,
extra_data,
head_print_func,
foot_print_func,
left_print_func,
right_print_func);
char *buff = (char*)xmalloc(handle_size);
if (buff)
{
__attribute__((unused))
PWBH *pwbh = pwb_initialize_handle(buff, handle_size,
data_source,
data_count,
print_func,
handle_name,
exec_func,
extra_data,
head_print_func,
foot_print_func,
left_print_func,
right_print_func);
WORD_LIST *wl_ptr = pwbh->printer_wl;
__attribute__((unused))
WORD_LIST *wl_length = &wl_ptr[3];
// Kinda kludgy, but easier to just switch here if requesting
// no-hilite version than adding parameters to pwb_initialize_handle:
if (use_non_hilite_print)
pwbh->dparms.printer = pwb_raw_line_printer;
SHELL_VAR *sv = NULL;
if (variable_context == 0)
sv = bind_variable(handle_name, "", 0);
else
sv = make_local_variable(handle_name, 0);
if (sv)
{
pwb_dispose_variable_value(sv);
sv->value = buff;
VSETATTR(sv, att_special);
if (invisible_p(sv))
VUNSETATTR(sv, att_invisible);
// Initialize the terminal and pager as soon
// as we know everything is gonna be OK:
pwb_terminal_init();
pager_init();
result = PWB_SUCCESS;
}
}
else
(*error_sink)("Out of memory for creating a new PWB handle.");
}
early_exit:
return result;
}
PWB_RESULT pwb_action_report(PWBH *handle, ACLONE *args)
{
printf("In pwb_action_report!\n");
PWB_RESULT result = PWB_FAILURE;
if (handle)
{
printf(" data source: %s\n", pwbh_get_name_data_source(handle));
printf(" row count: %d\n", pwbh_get_length_data_source(handle));
printf(" printer name: %s\n", pwbh_get_name_printer(handle));
printf("\nline printer arguments\n");
print_the_word_list(handle->printer_wl);
if (handle->exec_wl)
{
printf("\nexec_function arguments\n");
print_the_word_list(handle->exec_wl);
}
result = PWB_SUCCESS;
}
return result;
}
PWB_RESULT pwb_action_trigger(PWBH * handle, ACLONE *args)
{
PWB_RESULT result = PWB_FAILURE;
WORD_LIST *word_list = handle->exec_wl;
static unsigned int keymap_index = -1;
static const char *keystroke = NULL;
static AE_ITEM items[] = {
{ (const char **)&keymap_index, "keymap_index", '\0', AET_ARGUMENT,
"keymap action index", NULL, pwb_argeater_unsigned_int_setter },
{ &keystroke, "keystroke", '\0', AET_ARGUMENT,
"optional keystroke string" }
};
static AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
// Last action in array calls the exec function. Don't proceed
// unless the exec function has been declared (test by checking
// for the WORD_LIST handle member).
if (keymap_index == action_table_count-1)
{
if (word_list)
pwbh_exec_set_keystroke(handle, keystroke);
else
{
(*error_sink)("Attempted to call undeclared exec function.");
result = PWB_EXEC_FUNCTION_UNDECLARED;
goto early_exit;
}
}
PWB_KEYACT action = get_keyact(keymap_index);
if (action)
{
// Make word_list from arguments
result = (*action)(handle, word_list);
}
else
{
(*error_sink)("Index %d is out-of-range (0...%d).",
keymap_index,
action_table_count);
result = PWB_INVALID_ARGUMENT;
}
}
early_exit:
return result;
}
/* pwb_action_start() in its own source file */
PWB_RESULT pwb_action_set_margins(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_SUCCESS;
static int top = -1;
static int right = -1;
static int bottom = -1;
static int left = -1;
static AE_ITEM items[] = {
{ (const char **)&top, "top_margin", '\0', AET_ARGUMENT,
"value of left margin", NULL, pwb_argeater_int_setter },
{ (const char **)&right, "right_margin", '\0', AET_ARGUMENT,
"value of left margin", NULL, pwb_argeater_int_setter },
{ (const char **)&bottom, "bottom_margin", '\0', AET_ARGUMENT,
"value of left margin", NULL, pwb_argeater_int_setter },
{ (const char **)&left, "left_margin", '\0', AET_ARGUMENT,
"value of left margin", NULL, pwb_argeater_int_setter }
};
static AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
if (!pager_set_margins(&handle->dparms, top, right, bottom, left))
{
(*error_sink)("Impossible margins request.");
result = PWB_FAILURE;
}
}
return result;
}
PWB_RESULT pwb_action_plot_header(PWBH *handle, ACLONE *pargs)
{
PWB_RESULT result = PWB_FAILURE;
if (!pwb_margin_printer(handle, true))
result = 0;
return result;
}
PWB_RESULT pwb_action_plot_line(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_FAILURE;
static int row = -1;
static AE_ITEM items[] = {
{ (const char **)&row, "data_row", '\0', AET_ARGUMENT,
"index of row to replot", NULL, pwb_argeater_int_setter }
};
static AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
if (row >= 0)
{
// pager_plot_row() calls function
pager_plot_row(&handle->dparms, row);
result = ARV_CONTINUE;
}
}
return result;
}
PWB_RESULT pwb_action_plot_screen(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_SUCCESS;
pwbh_calc_borders(handle);
pager_plot(&handle->dparms);
return result;
}
PWB_RESULT pwb_action_get_data_source(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_SUCCESS;
const char *var_name="PWB_VALUE";
AE_ITEM items[] = {
{ &var_name, "var", 'v', AET_VALUE_OPTION,
"Alternate to 'PWB_VALUE' for reporting result" }
};
AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
SHELL_VAR *sv = find_variable(var_name);
if (!sv)
sv = bind_variable(var_name, "", 0);
if (sv)
{
pwb_dispose_variable_value(sv);
sv->value = savestring(pwbh_get_name_data_source(handle));
if (invisible_p(sv))
VUNSETATTR(sv, att_invisible);
result = PWB_SUCCESS;
}
}
return result;
}
PWB_RESULT pwb_action_get_data_count(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_SUCCESS;
int count = handle->dparms.row_count;
// +1 for count of digits, +1 for terminating /0
int num_length = count==0?1:(floor(log10(count))+1) + 1;
char *buff = alloca(num_length);
snprintf(buff, num_length, "%d", count);
const char *var_name="PWB_VALUE";
AE_ITEM items[] = {
{ &var_name, "var", 'v', AET_VALUE_OPTION,
"Alternate to 'PWB_VALUE' for reporting result" }
};
AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
SHELL_VAR *sv = find_variable(var_name);
if (!sv)
sv = bind_variable(var_name, "", 0);
if (sv)
{
pwb_dispose_variable_value(sv);
sv->value = savestring(buff);
if (invisible_p(sv))
VUNSETATTR(sv, att_invisible);
result = PWB_SUCCESS;
}
}
return result;
}
PWB_RESULT pwb_action_update_data_count(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_SUCCESS;
int new_count = 0;
AE_ITEM items[] = {
{ (const char **)&new_count, "count", '\0', AET_ARGUMENT,
"New value to use for the data count",
NULL, argeater_int_setter }
};
AE_MAP map = INIT_MAP(items);
if (argeater_process(args, &map))
{
handle->dparms.row_count = new_count;
}
return result;
}
PWB_RESULT pwb_action_erase_head(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_FAILURE;
DPARMS *dparms = &handle->dparms;
if (dparms->margin_top > 0)
{
int pos_left = dparms->margin_left + 1;
// put cursor at top/left of top margin
ti_printf("\x1b[1;%dH", pos_left);
// print empty lines for each row
for (int i=0; i<dparms->margin_top; ++i)
{
// Erase chars-count characters on this line
ti_printf("\x1b[%dX", dparms->chars_count);
// Down one to next line
ti_printf("\x1b[1B");
ti_printf("\x1b[%G", pos_left);
}
// Leave with cursor at top/left
ti_printf("\x1b[1;%dH", pos_left);
result = PWB_SUCCESS;
}
return result;
}
PWB_RESULT pwb_action_erase_foot(PWBH *handle, ACLONE *args)
{
PWB_RESULT result = PWB_FAILURE;
DPARMS *dparms = &handle->dparms;
if (dparms->margin_bottom > 0)
{
// Make sure cursor is below scroll region to prevent
// downward cursor movement from scrolling the content:
int pos_top = dparms->line_bottom + 2;
int pos_left = dparms->margin_left + 1;
// put cursor at top/left of top margin
ti_printf("\x1b[%d;%dH", pos_top, pos_left);
// print empty lines for each row
for (int i=0; i<dparms->margin_bottom; ++i)
{
// Erase chars-count characters on this line
ti_printf("\x1b[%dX", dparms->chars_count);
// Down one to next line
ti_printf("\x1b[1B");
ti_printf("\x1b[%G", pos_left);
}
// Leave with cursor at top/left
ti_printf("\x1b[%d;%dH", pos_top, pos_left);
result = PWB_SUCCESS;
}
return result;
}