File: run_action.c

package info (click to toggle)
ibm-3270 4.0ga12-3
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 17,740 kB
  • sloc: ansic: 120,111; sh: 4,284; makefile: 822; pascal: 798; perl: 344; exp: 184; tcl: 94
file content (344 lines) | stat: -rw-r--r-- 8,070 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 1993-2016, 2018 Paul Mattes.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the names of Paul Mattes nor the names of his contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *      run_action.c
 *              Floating action invocation.
 */

#include "globals.h"

#include "wincmn.h"
#include <errno.h>
#include <fcntl.h>

#include "actions.h"
#include "idle.h"
#include "kybd.h"
#include "lazya.h"
#include "popups.h"
#include "source.h"
#include "task.h"
#include "trace.h"
#include "utils.h"
#include "varbuf.h"
#include "w3misc.h"
#include "xio.h"

static void action_data(task_cbh handle, const char *buf, size_t len,
	bool success);
static bool action_done(task_cbh handle, bool success, bool abort);

/* Action context. */
typedef struct {
    llist_t llist;	/* list linkage */
    char *result;	/* accumulated result */
    size_t result_len;
    enum iaction ia;	/* cause */
} action_context_t;
static llist_t action_contexts = LLIST_INIT(action_contexts);

/* Action callback collection. */
typedef struct {
    llist_t llist;	/* linkage */
    tcb_t *cb;	/* callback block */
} action_cb_t;
static llist_t action_cbs = LLIST_INIT(action_cbs);

/**
 * Callback for data returned to keymap.
 *
 * @param[in] handle	Callback handle
 * @param[in] buf	Buffer
 * @param[in] len	Buffer length
 * @param[in] success	True if data, false if error message
 */
static void
action_data(task_cbh handle, const char *buf, size_t len, bool success)
{
    action_context_t *k;
    bool found = false;

    FOREACH_LLIST(&action_contexts, k, action_context_t *) {
	if (k == (action_context_t *)handle) {
	    found = true;
	    break;
	}
    } FOREACH_LLIST_END(&action_contexts, k, action_context_t *);

    if (!found) {
	vtrace("action_data: no match\n");
	return;
    }

    k->result = Realloc(k->result, k->result_len + 1 + len + 1);
    if (k->result_len) {
	k->result[k->result_len++] = '\n';
    }
    strncpy(k->result + k->result_len, buf, len);
    k->result[k->result_len + len] = '\0';
    k->result_len += len;
}

/**
 * Callback for completion of one command executed from a keymap.
 *
 * @param[in] handle		Callback handle
 * @param[in] success		True if child succeeded
 * @param[in] abort		True if aborting
 *
 * @return True if context is complete
 */
static bool
action_done(task_cbh handle, bool success, bool abort)
{
    action_context_t *k;
    bool found = false;

    FOREACH_LLIST(&action_contexts, k, action_context_t *) {
	if (k == (action_context_t *)handle) {
	    found = true;
	    break;
	}
    } FOREACH_LLIST_END(&action_contexts, k, action_context_t *);

    if (!found) {
	vtrace("action_data: no match\n");
	return true;
    }

    if (success) {
	if (k->result) {
	    popup_an_info("%s", k->result);
	}
    } else {
	if (k->result) {
	    popup_an_error("%s", k->result);
	} else {
	    popup_an_error("%s failed", ia_name[k->ia]);
	}
    }

    if (k->result) {
	Free(k->result);
    }
    llist_unlink(&k->llist);
    Free(k);

    /* Yes, done. */
    return true;
}

/**
 * Push an action.
 *
 * @param[in] ia	Cause.
 * @param[in] s		Text of action.
 */
static void
push_action(enum iaction ia, char *s)
{
    action_cb_t *acb;
    tcb_t *cb;
    bool found = false;
    action_context_t *k;

    /* Find a callback block. */
    FOREACH_LLIST(&action_cbs, acb, action_cb_t *) {
	if (acb->cb->ia == ia) {
	    cb = acb->cb;
	    found = true;
	    break;
	}
    } FOREACH_LLIST_END(&action_cbs, acb, action_cb_t *);

    if (!found) {
	acb = (action_cb_t *)Calloc(sizeof(action_cb_t) + sizeof(tcb_t), 1);
	llist_init(&acb->llist);
	acb->cb = cb = (tcb_t *)(acb + 1);
	LLIST_APPEND(&acb->llist, action_cbs);
	cb->shortname = ia_name[ia];
	cb->ia = ia;
	cb->flags = CB_NEW_TASKQ | CB_UI;
	cb->data = action_data;
	cb->done = action_done;
	cb->run = NULL;
    }

    /* Set up a context. */
    k = (action_context_t *)Malloc(sizeof(action_context_t));
    llist_init(&k->llist);
    k->result = NULL;
    k->result_len = 0;
    k->ia = ia;
    LLIST_APPEND(&k->llist, action_contexts);

    /* Push a callback with a macro. */
    push_cb(s, strlen(s), cb, (task_cbh)k);
}

/**
 * Push a floating keymap action.
 *
 * @param[in] s		Text of action.
 */
void
push_keymap_action(char *s)
{
    push_action(IA_KEYMAP, s);
}

/**
 * Push a floating macro.
 *
 * @param[in] s		Text of action.
 */
void
push_macro(char *s)
{
    push_action(IA_MACRO, s);
}

/**
 * Push a floating keypad action.
 *
 * @param[in] s		Text of action.
 */
void
push_keypad_action(char *s)
{
    push_action(IA_KEYPAD, s);
}

/**
 * Run an action.
 *
 * @param[in] name	Action name
 * @param[in] cause	Cause
 * @param[in] parm1	First parameter (optional)
 * @param[in] parm2	Second parameter (optional)
 *
 * @return true
 */
bool
run_action(const char *name, enum iaction cause, const char *parm1,
	const char *parm2)
{
    if (!parm1) {
	push_action(cause, lazyaf("%s()", name));
    } else if (!parm2) {
	push_action(cause, lazyaf("%s(%s)", name, parm1));
    } else {
	push_action(cause, lazyaf("%s(%s,%s)", name, parm1, parm2));
    }
    return true;
}

/**
 * Format a parameter for safe consumption as a parameter.
 *
 * @param[in] s		Parameter
 *
 * @return Possibly-quoted copy
 */
char *
safe_param(const char *s)
{
    varbuf_t r;
    char c;
    char *ret;
    bool quoted = false;

    if (strcspn(s, " ,()\\\b\f\r\n\t\v\"") == strlen(s)) {
	/* Safe already. */
	return (char *)s;
    }

    /* Quote it. */
    vb_init(&r);
    vb_appends(&r, "\"");
    while ((c = *s++)) {
	if (quoted) {
	    /* Pass the backslash and whatever follows. */
	    vb_appends(&r, "\\");
	    vb_append(&r, &c, 1);
	    quoted = false;
	} else {
	    if (c == '\\') {
		/* Remember a backslash. */
		quoted = true;
	    } else if (c == '"') {
		/* Double quotes need to be escaped. */
		vb_appends(&r, "\\\"");
	    } else {
		/* Pass through anything else. */
		vb_append(&r, &c, 1);
	    }
	}
    }

    if (quoted) {
	/* Trailing backslash must be quoted. */
	vb_appends(&r, "\\\\");
    }

    vb_appends(&r, "\"");
    ret = vb_consume(&r);
    lazya(ret);
    return ret;
}

/**
 * Run an action, given an array of parameters.
 *
 * @param[in] name	Action name
 * @param[in] cause	Cause
 * @param[in] count	Parameter count
 * @param[in] parms	Parameters
 *
 * @return true
 */
bool
run_action_a(const char *name, enum iaction cause, unsigned count,
	const char **parms)
{
    varbuf_t r;
    unsigned i;
    char *s;

    vb_init(&r);
    vb_appendf(&r, "%s(", name);
    for (i = 0; i < count; i++) {
	vb_appendf(&r, "%s%s", (i > 0)? ",": "", safe_param(parms[i]));
    }
    vb_appends(&r, ")");
    s = vb_consume(&r);
    push_action(cause, s);
    Free(s);

    return true;
}