-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_handle.c
349 lines (294 loc) · 12 KB
/
pwb_handle.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
#include "pwb_builtin.h"
#include "pwb_handle.h"
#include "pwb_utilities.h"
#include <string.h>
#include <assert.h>
/**
* @brief Prepare block of memory to be linked WORD_LIST and WORD_DESC elements
* @param "buffer" buffer of sufficient size for number of @p els.
* @param "els" number of WORD_LIST->WORD_DESC elements to initialize
*
* Calculate @p buffer size by multiplying `(sizeof(WORD_LIST) + sizeof(WORD_DESC))`
* by @p els.
*
* The memory will be organized as an array of @p els WORD_LIST,
* followed by an array of @p els WORD_DESC, with all the elements
* also connected as linked elements.
*
* Elements in the returned WORD_LIST can thus be accessed directly
* through indexed access or by walking the links.
*/
WORD_LIST * pwb_initialize_word_list(char* buffer, int els)
{
WORD_LIST *retval = (WORD_LIST*)buffer;
WORD_LIST *wlptr = retval;
WORD_LIST *wlend = wlptr + els;
WORD_DESC *wdptr = (WORD_DESC*)wlend;
while (wlptr < wlend)
{
WORD_LIST *wlnext = wlptr+1;
if (wlnext >= wlend)
wlnext = NULL;
wlptr->next = wlnext;
wlptr->word = wdptr;
// Leave WORD_DESC set to NULL/0
++wlptr;
++wdptr;
}
return retval;
}
/**
* @brief Calculate memory size required to be apportioned to a PWBH struct
* @param "data_source_name" name of Bash variable to pass to script
* for data
* @param "printer_func_name" name of Bash script function that will
* print a text line
* @param "exec_func_name" optional name of Bash script function to
* call to perform some action
* @param "data_extra_name" optional name of Bash variable that will
* be passed to print and exec functions
* for context
*
* Returns amount of memory needed to contain the entirety of a
* PWBH and its contents. Call this function to determine the
* buffer size needed to pass to @ref pwb_initialize_handle.
*
* The function names are not important in this step because they
* will be saved as pointers and not copied. They are here to be a
* mirror of the arguments of @ref pwb_initialize_handle.
*
* The significant argument is @p exec_func_name. If it is provided,
* a WORD_LIST of arguments will be set aside for calling the exec
* function.
*/
int pwb_calc_handle_size(const char *data_source_name,
const char *printer_func_name,
const char *handle_name,
const char *exec_func_name,
const char *data_extra_name,
const char *head_printer_func_name,
const char *foot_printer_func_name,
const char *left_printer_func_name,
const char *right_printer_func_name)
{
int total_bytes = sizeof(PWBH);
int int_count = 0;
total_bytes += 1 + strlen(data_source_name);
// Reserve memory for strings
total_bytes += get_string_saved_len(data_source_name);
total_bytes += get_string_saved_len(data_extra_name);
total_bytes += get_string_saved_len(handle_name);
total_bytes += get_string_saved_len(printer_func_name);
total_bytes += get_string_saved_len(exec_func_name);
total_bytes += get_string_saved_len(head_printer_func_name);
total_bytes += get_string_saved_len(foot_printer_func_name);
total_bytes += get_string_saved_len(left_printer_func_name);
total_bytes += get_string_saved_len(right_printer_func_name);
// Reserve memory for WORD_LISTs for callback functions
if (printer_func_name && *printer_func_name)
{
int_count += 3;
total_bytes += pwb_calc_word_list_size(7,3);
}
if (exec_func_name && *exec_func_name)
{
total_bytes += pwb_calc_word_list_size(6,1);
int_count += 1;
}
// Reserve memory for strings
// int slen;
// if (data_source_name && (slen=strlen(data_source_name))>0)
// total_bytes += 1 + slen;
// if (data_extra_name && (slen=strlen(data_extra_name))>0)
// total_bytes += 1 + slen;
// if (handle_name && (slen=strlen(handle_name))>0)
// total_bytes += 1 + slen;
// if (printer_func_name && (slen=strlen(printer_func_name))>0)
// {
// total_bytes += 1 + slen;
// int_count += 3;
// total_bytes += pwb_calc_word_list_size(7,3);
// }
// if (exec_func_name && (slen=strlen(exec_func_name))>0)
// {
// total_bytes += 1 + slen;
// int_count += 1;
// total_bytes += pwb_calc_word_list_size(6,1);
// }
return total_bytes + (int_count * WORD_LIST_INT_SIZE);
}
/**
* @brief Allocate memory in @p buffer
*
* @param "data_source_name" name of Bash variable to pass to script
* for data
* @param "printer_func_name" name of Bash script function that will
* print a text line
* @param "exec_func_name" optional name of Bash script function to
* call to perform some action
* @param "data_extra_name" optional name of Bash variable that will
* be passed to print and exec functions
* for context
*
* In the context of the application, these values will be taken from
* command line arguments whose scope exceeds the scope of the handle,
* so I'm just assigning the argument pointers to the appropriate
* places in the PWBH.
*/
PWBH * pwb_initialize_handle(char *buffer,
int buffer_len,
const char *data_source_name,
int data_count,
const char *printer_name,
const char *handle_name,
const char *exec_name,
const char *data_extra_name,
const char *head_printer_name,
const char *foot_printer_name,
const char *left_printer_name,
const char *right_printer_name)
{
#define MEMTEST free < buffer + buffer_len + 1
memset(buffer, 0, buffer_len);
PWBH *pwbh = (PWBH*)buffer;
memcpy(pwbh, PWB_ID_STRING, sizeof(PWB_ID_STRING));
// DPARMS basics:
pwbh->dparms.row_count = data_count;
pwbh->dparms.printer = pwb_line_printer;
pwbh->dparms.data_extra = (void*)pwbh;
// Allocate memory starting from just after the PWBH
char *free = buffer + sizeof(PWBH);
assert(MEMTEST);
char *buff_end = buffer + buffer_len;
// For PWB handle members
// pwb_save_string_to_handle(&pwbh->head_printer_name,
// &free, buff_end, head_printer_name);
// pwb_save_string_to_handle(&pwbh->foot_printer_name,
// &free, buff_end, foot_printer_name);
// For DPARMS in header
const char *copy_data_source_name = NULL;
pack_string_in_block(©_data_source_name, &free, buff_end, data_source_name);
pwbh->dparms.data_source = (char*)copy_data_source_name;
// For WORD_LIST parameters
const char *copy_handle_name = NULL;
const char *copy_exec_name = NULL;
const char *copy_data_extra_name = NULL;
pack_string_in_block(©_handle_name, &free, buff_end, handle_name);
pack_string_in_block(©_exec_name, &free, buff_end, exec_name);
pack_string_in_block(©_data_extra_name, &free, buff_end, data_extra_name);
pack_string_in_block(&pwbh->print_func_line, &free, buff_end, printer_name);
pack_string_in_block(&pwbh->print_func_head, &free, buff_end, head_printer_name);
pack_string_in_block(&pwbh->print_func_foot, &free, buff_end, foot_printer_name);
pack_string_in_block(&pwbh->print_func_left, &free, buff_end, left_printer_name);
pack_string_in_block(&pwbh->print_func_right, &free, buff_end, right_printer_name);
// Prepare WORD_LIST for printer function callback
if (pwbh->print_func_line || pwbh->print_func_head || pwbh->print_func_foot)
{
pwbh->printer_wl = pwb_initialize_word_list(free, 7);
free += pwb_calc_word_list_size(7,3);
// Set integer WORD_LIST elements first
// row_index
pwb_initialize_word_list_int_arg(pwbh->printer_wl, 1, free);
free += WORD_LIST_INT_SIZE;
// char_limit
pwb_initialize_word_list_int_arg(pwbh->printer_wl, 3, free);
free += WORD_LIST_INT_SIZE;
// focus_flag
pwb_initialize_word_list_int_arg(pwbh->printer_wl, 4, free);
free += WORD_LIST_INT_SIZE;
// Confirm we're on track, memory-wise
assert(MEMTEST);
// Set string WORD_LIST elements
pwb_set_word_list_string_arg(pwbh->printer_wl, 0, pwbh->print_func_line);
pwb_set_word_list_string_arg(pwbh->printer_wl, 2, copy_data_source_name);
pwb_set_word_list_string_arg(pwbh->printer_wl, 5, copy_handle_name);
// Set last WORD_LIST item according to supplied arguments
if (copy_data_extra_name)
pwb_set_word_list_string_arg(pwbh->printer_wl, 6, copy_data_extra_name);
else
pwb_truncate_word_list_at_index(pwbh->printer_wl, 5);
}
if (copy_exec_name)
{
pwbh->exec_wl = pwb_initialize_word_list(free, 6);
free += pwb_calc_word_list_size(6, 1);
// Set integer WORD_LIST elements first
// data row number
pwb_initialize_word_list_int_arg(pwbh->exec_wl, 2, free);
free += WORD_LIST_INT_SIZE;
pwb_set_word_list_string_arg(pwbh->exec_wl, 0, copy_exec_name);
// Skip WORD_LIST index 1 because it will be allocated for each function call
pwb_set_word_list_string_arg(pwbh->exec_wl, 3, copy_data_source_name);
pwb_set_word_list_string_arg(pwbh->exec_wl, 4, copy_handle_name);
// Set last WORD_LIST item according to supplied arguments
if (copy_data_extra_name)
pwb_set_word_list_string_arg(pwbh->exec_wl, 5, copy_data_extra_name);
else
pwb_truncate_word_list_at_index(pwbh->exec_wl, 4);
}
return pwbh;
}
PWBH *pwb_get_handle_from_name(const char *handle_name)
{
assert(handle_name && strlen(handle_name)>0);
SHELL_VAR *sv = find_variable(handle_name);
if (sv && pwbh_p(sv))
return pwbh_cell(sv);
return NULL;
}
#ifdef PWB_HANDLE_MAIN
#include "pwb_builtin.h"
#include <alloca.h>
#include "pwbh_support.c"
#include "pwb_utilities.c"
void dump_word_list(WORD_LIST *wl)
{
int count = 0;
while (wl)
{
printf("%2d: '%s'\n", ++count, wl->word->word);
wl = wl->next;
}
}
int main(int argc, const char **argv)
{
const char *handle_name = "bogus_handle";
const char *ds_name = "dsource";
int ds_count = 10;
const char *pf_name = "printer_func";
const char *ef_name = "exec_func";
const char *de_name = "data_extra";
const char *top_print = "header_printer";
const char *bottom_print = "footer_printer";
int hsize = pwb_calc_handle_size(ds_name, pf_name, ef_name, handle_name,
de_name, top_print, bottom_print);
char *buff = alloca(hsize);
PWBH *pwbh = pwb_initialize_handle(buff, hsize, ds_name,
ds_count, pf_name,
handle_name,
ef_name, de_name,
top_print, bottom_print);
printf("The handle shows string '%s'\n", (char*)pwbh);
printf("\nLet's set some strings:\n");
pwbh_print_set_row_index(pwbh, 10);
pwbh_print_set_focus(pwbh, 10);
pwbh_print_set_length(pwbh, 50);
pwbh_exec_set_row_number(pwbh, 15);
pwbh_exec_set_keystroke(pwbh, "weird_keystroke, baby");
printf("Dumping the printer function arguments:\n");
dump_word_list(pwbh->printer_wl);
printf("\nDumping the exec function arguments:\n");
dump_word_list(pwbh->exec_wl);
return 0;
}
#endif
/* Local Variables: */
/* compile-command: "gcc \*/
/* -DPWB_HANDLE_MAIN \*/
/* -std=c99 -Wall -Werror -ggdb \*/
/* -I/usr/include/bash \*/
/* -I/usr/include/bash/include \*/
/* -fsanitize=address \*/
/* -o test_pwb_handle pwb_handle.c \*/
/* -lpager -ltinfo " */
/* End: */