Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_process.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Dridi Boukelmoune <dridi@varnish-software.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * XXX:
30
 *      -ignore-stderr (otherwise output to stderr is fail)
31
 */
32
33
#include "config.h"
34
35
#include <sys/ioctl.h>          // Linux: struct winsize
36
37
#include <ctype.h>
38
#include <fcntl.h>
39
#include <inttypes.h>
40
#include <poll.h>
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <string.h>
44
#ifdef __sun
45
#  include <stropts.h>
46
#endif
47
#include <termios.h>
48
#include <unistd.h>
49
50
#include "vtc.h"
51
52
#include "vre.h"
53
#include "vev.h"
54
#include "vlu.h"
55
#include "vsub.h"
56
#include "vtim.h"
57
58
#include "teken.h"
59
60
struct process {
61
        unsigned                magic;
62
#define PROCESS_MAGIC           0x1617b43e
63
        char                    *name;
64
        struct vtclog           *vl;
65
        VTAILQ_ENTRY(process)   list;
66
67
        char                    *spec;
68
        char                    *dir;
69
        char                    *out;
70
        char                    *err;
71
        int                     fd_term;
72
        int                     fd_stderr;
73
        int                     f_stdout;
74
        int                     f_stderr;
75
        struct vlu              *vlu_stdout;
76
        struct vlu              *vlu_stderr;
77
        int                     log;
78
        pid_t                   pid;
79
        int                     expect_exit;
80
        int                     expect_signal;
81
        int                     allow_core;
82
83
        uintmax_t               stdout_bytes;
84
        uintmax_t               stderr_bytes;
85
86
        pthread_mutex_t         mtx;
87
        pthread_t               tp;
88
        unsigned                hasthread;
89
90
        int                     nlin;
91
        int                     ncol;
92
        int                     ansi_response;
93
        char                    **vram;
94
        teken_t                 tek[1];
95
};
96
97
static VTAILQ_HEAD(, process)   processes =
98
    VTAILQ_HEAD_INITIALIZER(processes);
99
100
static void term_resize(struct process *pp, int lin, int col);
101
102
/**********************************************************************
103
 * Terminal emulation
104
 */
105
106
static void
107
term_cursor(void *priv, const teken_pos_t *pos)
108 144693
{
109 144693
        (void)priv;
110 144693
        (void)pos;
111 144693
}
112
113
static void
114
term_putchar(void *priv, const teken_pos_t *pos, teken_char_t ch,
115
    const teken_attr_t *at)
116 1013530
{
117 1013530
        struct process *pp;
118
119 1013530
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
120 1013530
        (void)at;
121 1013530
        if (ch > 126 || ch < 32)
122 562
                ch = '?';
123 1013530
        assert(pos->tp_row < pp->nlin);
124 1013530
        assert(pos->tp_col < pp->ncol);
125 1013530
        pp->vram[pos->tp_row][pos->tp_col] = ch;
126 1013530
}
127
128
static void
129
term_fill(void *priv, const teken_rect_t *r, teken_char_t c,
130
    const teken_attr_t *a)
131 1875
{
132 1875
        teken_pos_t p;
133
134
        /* Braindead implementation of fill() - just call putchar(). */
135 1875
        for (p.tp_row = r->tr_begin.tp_row;
136 11575
            p.tp_row < r->tr_end.tp_row; p.tp_row++)
137 9700
                for (p.tp_col = r->tr_begin.tp_col;
138 892327
                    p.tp_col < r->tr_end.tp_col; p.tp_col++)
139 882627
                        term_putchar(priv, &p, c, a);
140 1875
}
141
142
static void
143
term_copy(void *priv, const teken_rect_t *r, const teken_pos_t *p)
144 1697
{
145 1697
        struct process *pp;
146 1697
        int nrow, ncol, y; /* Has to be signed - >= 0 comparison */
147
148
        /*
149
         * Copying is a little tricky. We must make sure we do it in
150
         * correct order, to make sure we don't overwrite our own data.
151
         */
152 1697
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
153
154 1697
        nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
155 1697
        ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
156
157 1697
        if (p->tp_row < r->tr_begin.tp_row) {
158
                /* Copy from top to bottom. */
159 23297
                for (y = 0; y < nrow; y++)
160 22663
                        vmemmove(&pp->vram[p->tp_row + y][p->tp_col],
161 634
                                 &pp->vram[r->tr_begin.tp_row + y][r->tr_begin.tp_col],
162 634
                                 ncol);
163 1063
        } else {
164
                /* Copy from bottom to top. */
165 5973
                for (y = nrow - 1; y >= 0; y--)
166 4910
                        vmemmove(&pp->vram[p->tp_row + y][p->tp_col],
167 1063
                                 &pp->vram[r->tr_begin.tp_row + y][r->tr_begin.tp_col],
168 1063
                                 ncol);
169 1063
        }
170 1697
}
171
172
static void
173
term_respond(void *priv, const void *p, size_t l)
174 16
{
175 16
        struct process *pp;
176 16
        int r;
177
178 16
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
179
180 16
        vtc_dump(pp->vl, 4, "term_response", p, l);
181 16
        if (pp->ansi_response) {
182 2
                r = write(pp->fd_term, p, l);
183 2
                if (r != l)
184 0
                        vtc_fatal(pp->vl, "Could not write to process: %s",
185 0
                            strerror(errno));
186 2
        }
187 16
}
188
189
static void
190
term_param(void *priv, int p, unsigned int v)
191 139
{
192 139
        struct process *pp;
193
194 139
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
195 139
        if (p == TP_132COLS && v)
196 12
                term_resize(pp, pp->nlin, 132);
197 139
        if (p == TP_132COLS && !v)
198 24
                term_resize(pp, pp->nlin, 80);
199 139
}
200
201
static const teken_funcs_t process_teken_func = {
202
        .tf_cursor      =       term_cursor,
203
        .tf_putchar     =       term_putchar,
204
        .tf_fill        =       term_fill,
205
        .tf_copy        =       term_copy,
206
        .tf_respond     =       term_respond,
207
        .tf_param       =       term_param,
208
};
209
210
static void
211
term_screen_dump(const struct process *pp)
212 171
{
213 171
        int i;
214 171
        const teken_pos_t *pos;
215
216 4659
        for (i = 0; i < pp->nlin; i++)
217 4488
                vtc_dump(pp->vl, 3, "screen", pp->vram[i], pp->ncol);
218 171
        pos = teken_get_cursor(pp->tek);
219 171
        vtc_log(pp->vl, 3, "Cursor at line %d column %d",
220 171
            pos->tp_row + 1, pos->tp_col + 1);
221 171
}
222
223
static void
224
term_resize(struct process *pp, int lin, int col)
225 104
{
226 104
        teken_pos_t pos;
227 104
        char **vram;
228 104
        int i, j;
229
230 104
        vram = calloc(lin, sizeof *pp->vram);
231 104
        AN(vram);
232 2752
        for (i = 0; i < lin; i++) {
233 2648
                vram[i] = calloc(col + 1L, 1);
234 2648
                AN(vram[i]);
235 2648
                memset(vram[i], ' ', col);
236 2648
                vram[i][col] = '\0';
237 2648
        }
238 104
        if (pp->vram != NULL) {
239 1124
                for (i = 0; i < lin; i++) {
240 1087
                        if (i >= pp->nlin)
241 8
                                break;
242 1079
                        j = col;
243 1079
                        if (j > pp->ncol)
244 384
                                j = pp->ncol;
245 1079
                        vmemcpy(vram[i], pp->vram[i], j);
246 1079
                }
247 1125
                for (i = 0; i < pp->nlin; i++)
248 1080
                        free(pp->vram[i]);
249 45
                free(pp->vram);
250 45
        }
251 104
        pp->vram = vram;
252 104
        pp->nlin = lin;
253 104
        pp->ncol = col;
254
255 104
        pos.tp_row = lin;
256 104
        pos.tp_col = col;
257 104
        teken_set_winsize(pp->tek, &pos);
258 104
}
259
260
static int
261
term_find_textline(const struct process *pp, int *x, int y, const char *pat)
262 5424
{
263 5424
        const char *t;
264 5424
        int l;
265
266 5424
        if (*x == 0) {
267 4201
                t = strstr(pp->vram[y], pat);
268 4201
                if (t != NULL) {
269 79
                        *x = 1 + (t - pp->vram[y]);
270 79
                        return (1);
271 79
                }
272 4201
        } else if (*x <= pp->ncol) {
273 1223
                t = pp->vram[y] + *x - 1;
274 1223
                l = vstrlen(pat);
275 1223
                assert((*x - 1) + (l - 1) < pp->ncol);
276 1223
                if (!vmemcmp(t, pat, l))
277 194
                        return (1);
278 1223
        }
279 5151
        return (0);
280 5424
}
281
282
static int
283
term_find_text(const struct process *pp, int *x, int *y, const char *pat)
284 551
{
285 551
        int yy;
286
287 551
        if (*y == 0) {
288 5227
                for (yy = 0; yy < pp->nlin; yy++) {
289 5094
                        if (term_find_textline(pp, x, yy, pat)) {
290 88
                                *y = yy + 1;
291 88
                                return (1);
292 88
                        }
293 5094
                }
294 330
        } else if (*y <= pp->nlin) {
295 330
                if (term_find_textline(pp, x, *y - 1, pat))
296 185
                        return (1);
297 330
        }
298 278
        return (0);
299 551
}
300
301
static void
302
term_expect_text(struct process *pp,
303
    const char *lin, const char *col, const char *pat)
304 273
{
305 273
        int x, y, l, d = 10000;
306 273
        char *t;
307
308 273
        y = strtoul(lin, NULL, 0);
309 273
        if (y < 0 || y > pp->nlin)
310 0
                vtc_fatal(pp->vl, "YYY %d nlin %d", y, pp->nlin);
311 273
        x = strtoul(col, NULL, 0);
312 277
        for(l = 0; l <= 10 && x > pp->ncol; l++)        // wait for screen change
313 4
                VTIM_sleep(0.1);
314 273
        if (x < 0 || x > pp->ncol)
315 0
                vtc_fatal(pp->vl, "XXX %d ncol %d", x, pp->ncol);
316 273
        l = vstrlen(pat);
317 273
        if (x + l - 1 > pp->ncol)
318 0
                vtc_fatal(pp->vl, "XXX %d ncol %d", x + l - 1, pp->ncol);
319 273
        PTOK(pthread_mutex_lock(&pp->mtx));
320 551
        while (!term_find_text(pp, &x, &y, pat)) {
321 278
                if (d > 1000000) {
322 4
                        vtc_log(pp->vl, 3,
323 4
                            "Have no yet found pattern '%s'", pat);
324 4
                        term_screen_dump(pp);
325 4
                }
326 278
                if (x != 0 && y != 0) {
327 132
                        t = pp->vram[y - 1] + x - 1;
328 132
                        vtc_log(pp->vl, 4,
329 132
                            "text at %d,%d: '%.*s'", y, x, l, t);
330 132
                }
331 278
                PTOK(pthread_mutex_unlock(&pp->mtx));
332 278
                usleep(d);
333 278
                PTOK(pthread_mutex_lock(&pp->mtx));
334 278
                if (d < 3000000)
335 278
                        d += d;
336 278
        }
337 273
        PTOK(pthread_mutex_unlock(&pp->mtx));
338 273
        vtc_log(pp->vl, 4, "found expected text at %d,%d: '%s'", y, x, pat);
339 273
}
340
341
static void
342
term_expect_cursor(const struct process *pp, const char *lin, const char *col)
343 8
{
344 8
        int x, y, l;
345 8
        const teken_pos_t *pos;
346
347 8
        pos = teken_get_cursor(pp->tek);
348 8
        y = strtoul(lin, NULL, 0);
349 8
        if (y < 0 || y > pp->nlin)
350 0
                vtc_fatal(pp->vl, "YYY %d nlin %d", y, pp->nlin);
351 8
        x = strtoul(col, NULL, 0);
352 8
        for(l = 0; l < 10 && x > pp->ncol; l++) // wait for screen change
353 0
                VTIM_sleep(0.1);
354 8
        if (x < 0 || x > pp->ncol)
355 0
                vtc_fatal(pp->vl, "XXX %d ncol %d", x, pp->ncol);
356 8
        if (y != 0 && (y-1) != pos->tp_row)
357 0
                vtc_fatal(pp->vl, "Cursor on line %d (expected %d)",
358 0
                    pos->tp_row + 1, y);
359 8
        if (x != 0 && (x-1) != pos->tp_col)
360 0
                vtc_fatal(pp->vl, "Cursor in column %d (expected %d)",
361 0
                    pos->tp_col + 1, y);
362 8
}
363
364
static void
365
term_match_text(struct process *pp,
366
    const char *lin, const char *col, const char *re)
367 34
{
368 34
        int i, l, err, erroff;
369 34
        struct vsb *vsb, re_vsb[1];
370 34
        size_t len;
371 34
        ssize_t x, y;
372 34
        vre_t *vre;
373 34
        char errbuf[VRE_ERROR_LEN];
374
375 34
        vsb = VSB_new_auto();
376 34
        AN(vsb);
377
378 34
        y = strtoul(lin, NULL, 0);
379 34
        if (y < 0 || y > pp->nlin)
380 0
                vtc_fatal(pp->vl, "YYY %zd nlin %d", y, pp->nlin);
381 34
        x = strtoul(col, NULL, 0);
382 34
        for(l = 0; l < 10 && x > pp->ncol; l++) // wait for screen change
383 0
                VTIM_sleep(0.1);
384 34
        if (x < 0 || x > pp->ncol)
385 0
                vtc_fatal(pp->vl, "XXX %zd ncol %d", x, pp->ncol);
386
387 34
        if (x)
388 16
                x--;
389
390 34
        if (y)
391 18
                y--;
392
393 34
        vre = VRE_compile(re, 0, &err, &erroff, 1);
394 34
        if (vre == NULL) {
395 0
                AN(VSB_init(re_vsb, errbuf, sizeof errbuf));
396 0
                AZ(VRE_error(re_vsb, err));
397 0
                AZ(VSB_finish(re_vsb));
398 0
                VSB_fini(re_vsb);
399 0
                vtc_fatal(pp->vl, "invalid regexp \"%s\" at %d (%s)",
400 0
                    re, erroff, errbuf);
401 0
        }
402
403 34
        PTOK(pthread_mutex_lock(&pp->mtx));
404
405 34
        len = (pp->nlin - y) * (pp->ncol - x);
406 824
        for (i = y; i < pp->nlin; i++) {
407 790
                VSB_bcat(vsb, &pp->vram[i][x], pp->ncol - x);
408 790
                VSB_putc(vsb, '\n');
409 790
        }
410
411 34
        AZ(VSB_finish(vsb));
412
413 34
        if (VRE_match(vre, VSB_data(vsb), len, 0, NULL) < 1)
414 0
                vtc_fatal(pp->vl, "match failed: (\"%s\")", re);
415 34
        else
416 34
                vtc_log(pp->vl, 4, "match succeeded");
417
418 34
        PTOK(pthread_mutex_unlock(&pp->mtx));
419 34
        VSB_destroy(&vsb);
420 34
        VRE_free(&vre);
421 34
}
422
423
/**********************************************************************
424
 * Allocate and initialize a process
425
 */
426
427
#define PROCESS_EXPAND(field, format, ...)                              \
428 177
        do {                                                            \
429 177
                vsb = macro_expandf(p->vl, format, __VA_ARGS__);        \
430 177
                AN(vsb);                                                \
431 177
                p->field = strdup(VSB_data(vsb));                       \
432 177
                AN(p->field);                                           \
433 177
                VSB_destroy(&vsb);                                      \
434 177
        } while (0)
435
436
static void
437
process_coverage(struct process *p)
438 59
{
439 59
        const teken_attr_t *a;
440 59
        teken_pos_t pos;
441 59
        int fg, bg;
442
443
        // Code-Coverage of Teken
444
445 59
        (void)teken_get_sequence(p->tek, TKEY_UP);
446 59
        (void)teken_get_sequence(p->tek, TKEY_F1);
447 59
        (void)teken_256to8(0);
448 59
        (void)teken_256to16(0);
449 59
        a = teken_get_defattr(p->tek);
450 59
        teken_set_defattr(p->tek, a);
451 59
        a = teken_get_curattr(p->tek);
452 59
        teken_set_curattr(p->tek, a);
453 59
        (void)teken_get_winsize(p->tek);
454 59
        pos.tp_row = 0;
455 59
        pos.tp_col = 8;
456 59
        teken_set_cursor(p->tek, &pos);
457 59
        teken_get_defattr_cons25(p->tek, &fg, &bg);
458 59
}
459
460
static struct process *
461
process_new(const char *name)
462 59
{
463 59
        struct process *p;
464 59
        struct vsb *vsb;
465 59
        char buf[1024];
466
467 59
        ALLOC_OBJ(p, PROCESS_MAGIC);
468 59
        AN(p);
469 59
        REPLACE(p->name, name);
470 59
        PTOK(pthread_mutex_init(&p->mtx, NULL));
471
472 59
        p->vl = vtc_logopen("%s", name);
473 59
        AN(p->vl);
474
475 59
        PROCESS_EXPAND(dir, "${tmpdir}/%s", name);
476 59
        PROCESS_EXPAND(out, "${tmpdir}/%s/term", name);
477 59
        PROCESS_EXPAND(err, "${tmpdir}/%s/stderr", name);
478
479 59
        bprintf(buf, "rm -rf %s ; mkdir -p %s ; touch %s %s",
480 59
            p->dir, p->dir, p->out, p->err);
481 59
        AZ(system(buf));
482
483 59
        p->fd_term = -1;
484
485 59
        VTAILQ_INSERT_TAIL(&processes, p, list);
486 59
        teken_init(p->tek, &process_teken_func, p);
487 59
        term_resize(p, 24, 80);
488 59
        process_coverage(p);
489 59
        return (p);
490 59
}
491
492
#undef PROCESS_EXPAND
493
494
/**********************************************************************
495
 * Clean up process
496
 */
497
498
static void
499
process_delete(struct process *p)
500 59
{
501 59
        int i;
502
503 59
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
504 59
        PTOK(pthread_mutex_destroy(&p->mtx));
505 59
        vtc_logclose(p->vl);
506 59
        free(p->name);
507 59
        free(p->dir);
508 59
        free(p->out);
509 59
        free(p->err);
510
511 1627
        for (i = 0; i < p->nlin; i++)
512 1568
                free(p->vram[i]);
513 59
        free(p->vram);
514
515
        /*
516
         * We do not delete the directory, it may contain useful stdout
517
         * and stderr files. They will be deleted on account of belonging
518
         * to the test's tmpdir.
519
         */
520
521
        /* XXX: MEMLEAK (?) */
522 59
        FREE_OBJ(p);
523 59
}
524
525
static void
526
process_undef(const struct process *p)
527 59
{
528 59
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
529
530 59
        macro_undef(p->vl, p->name, "dir");
531 59
        macro_undef(p->vl, p->name, "out");
532 59
        macro_undef(p->vl, p->name, "err");
533 59
}
534
535
/**********************************************************************
536
 * Data stream handling
537
 */
538
539
static int
540
process_vlu_func(void *priv, const char *l)
541 265
{
542 265
        struct process *p;
543
544 265
        CAST_OBJ_NOTNULL(p, priv, PROCESS_MAGIC);
545 265
        vtc_dump(p->vl, 4, "output", l, -1);
546 265
        return (0);
547 265
}
548
549
static int v_matchproto_(vev_cb_f)
550
process_stdout(const struct vev *ev, int what)
551 3377
{
552 3377
        struct process *p;
553 3377
        char buf[BUFSIZ];
554 3377
        int i;
555
556 3377
        CAST_OBJ_NOTNULL(p, ev->priv, PROCESS_MAGIC);
557 3377
        (void)what;
558 3377
        i = read(p->fd_term, buf, sizeof buf);
559 3377
        if (i <= 0) {
560 79
                vtc_log(p->vl, 4, "stdout read %d", i);
561 79
                return (1);
562 79
        }
563 3298
        PTOK(pthread_mutex_lock(&p->mtx));
564 3297
        p->stdout_bytes += i;
565 3297
        PTOK(pthread_mutex_unlock(&p->mtx));
566 3298
        if (p->log == 1)
567 76
                (void)VLU_Feed(p->vlu_stdout, buf, i);
568 3222
        else if (p->log == 2)
569 277
                vtc_dump(p->vl, 4, "stdout", buf, i);
570 2945
        else if (p->log == 3)
571 53
                vtc_hexdump(p->vl, 4, "stdout", buf, i);
572 3298
        assert(write(p->f_stdout, buf, i) == i);
573 3298
        PTOK(pthread_mutex_lock(&p->mtx));
574 3298
        teken_input(p->tek, buf, i);
575 3298
        PTOK(pthread_mutex_unlock(&p->mtx));
576 3298
        return (0);
577 3298
}
578
579
static int v_matchproto_(vev_cb_f)
580
process_stderr(const struct vev *ev, int what)
581 94
{
582 94
        struct process *p;
583 94
        char buf[BUFSIZ];
584 94
        int i;
585
586 94
        CAST_OBJ_NOTNULL(p, ev->priv, PROCESS_MAGIC);
587 94
        (void)what;
588 94
        i = read(p->fd_stderr, buf, sizeof buf);
589 94
        if (i <= 0) {
590 79
                vtc_log(p->vl, 4, "stderr read %d", i);
591 79
                return (1);
592 79
        }
593 15
        PTOK(pthread_mutex_lock(&p->mtx));
594 15
        p->stderr_bytes += i;
595 15
        PTOK(pthread_mutex_unlock(&p->mtx));
596 15
        vtc_dump(p->vl, 4, "stderr", buf, i);
597 15
        assert(write(p->f_stderr, buf, i) == i);
598 15
        return (0);
599 15
}
600
601
static void
602
process_cleanup(void *priv)
603 0
{
604 0
        struct vev_root *evb = priv;
605 0
        VEV_Destroy(&evb);
606 0
}
607
608
static void *
609
process_thread(void *priv)
610 79
{
611 79
        struct process *p;
612 79
        struct vev_root *evb;
613 79
        struct vev *ev;
614 79
        int r;
615
616 79
        CAST_OBJ_NOTNULL(p, priv, PROCESS_MAGIC);
617
618 79
        p->f_stdout = open(p->out, O_WRONLY|O_APPEND);
619 79
        assert(p->f_stdout >= 0);
620 79
        p->f_stderr = open(p->err, O_WRONLY|O_APPEND);
621 79
        assert(p->f_stderr >= 0);
622
623 79
        evb = VEV_New();
624 79
        AN(evb);
625 79
        pthread_cleanup_push(process_cleanup, evb);
626
627 79
        ev = VEV_Alloc();
628 79
        AN(ev);
629 79
        ev->fd = p->fd_term;
630 79
        ev->fd_flags = VEV__RD | VEV__HUP | VEV__ERR;
631 79
        ev->callback = process_stdout;
632 79
        ev->priv = p;
633 79
        AZ(VEV_Start(evb, ev));
634
635 79
        ev = VEV_Alloc();
636 79
        AN(ev);
637 79
        ev->fd = p->fd_stderr;
638 79
        ev->fd_flags = VEV__RD | VEV__HUP | VEV__ERR;
639 79
        ev->callback = process_stderr;
640 79
        ev->priv = p;
641 79
        AZ(VEV_Start(evb, ev));
642
643 79
        if (p->log == 1) {
644 19
                p->vlu_stdout = VLU_New(process_vlu_func, p, 1024);
645 19
                AN(p->vlu_stdout);
646 19
                p->vlu_stderr = VLU_New(process_vlu_func, p, 1024);
647 19
                AN(p->vlu_stderr);
648 19
        }
649
650 3513
        do {
651 3513
                r = VEV_Once(evb);
652 3513
        } while (r == 1);
653
654 79
        if (r < 0)
655 0
                vtc_fatal(p->vl, "VEV_Once() = %d, error %s", r,
656 0
                    strerror(errno));
657
658 79
        vtc_wait4(p->vl, p->pid,
659 79
            p->expect_exit, p->expect_signal, p->allow_core);
660 79
        closefd(&p->f_stdout);
661 79
        closefd(&p->f_stderr);
662
663 79
        PTOK(pthread_mutex_lock(&p->mtx));
664
665
        /* NB: We keep the other macros around */
666 79
        macro_undef(p->vl, p->name, "pid");
667 79
        p->pid = -1;
668
669 79
        PTOK(pthread_mutex_unlock(&p->mtx));
670
671 79
        pthread_cleanup_pop(0);
672 79
        VEV_Destroy(&evb);
673 79
        if (p->log == 1) {
674 19
                VLU_Destroy(&p->vlu_stdout);
675 19
                VLU_Destroy(&p->vlu_stderr);
676 19
        }
677 79
        return (NULL);
678 79
}
679
680
static void
681
process_winsz(struct process *p, int fd)
682 88
{
683 88
        struct winsize ws;
684 88
        int i;
685
686 88
        memset(&ws, 0, sizeof ws);
687 88
        ws.ws_row = (short)p->nlin;
688 88
        ws.ws_col = (short)p->ncol;
689 88
        i = ioctl(fd, TIOCSWINSZ, &ws);
690 88
        if (i)
691 4
                vtc_log(p->vl, 4, "TIOCWINSZ %d %s", i, strerror(errno));
692 88
}
693
694
static void
695
process_init_term(struct process *p, int fd)
696 79
{
697 79
        struct termios tt;
698 79
        int i;
699
700 79
        process_winsz(p, fd);
701
702 79
        memset(&tt, 0, sizeof tt);
703 79
        tt.c_cflag = CREAD | CS8 | HUPCL;
704 79
        tt.c_iflag = BRKINT | ICRNL | IMAXBEL | IXON | IXANY;
705 79
        tt.c_lflag = ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOKE | ECHOCTL;
706 79
        tt.c_oflag = OPOST | ONLCR;
707 79
        i = cfsetispeed(&tt, B9600);
708 79
        if (i)
709 0
                vtc_log(p->vl, 4, "cfsetispeed %d %s", i, strerror(errno));
710 79
        i = cfsetospeed(&tt, B9600);
711 79
        if (i)
712 0
                vtc_log(p->vl, 4, "cfsetospeed %d %s", i, strerror(errno));
713 79
        tt.c_cc[VEOF] = '\x04';                 // CTRL-D
714 79
        tt.c_cc[VERASE] = '\x08';               // CTRL-H (Backspace)
715 79
        tt.c_cc[VKILL] = '\x15';                // CTRL-U
716 79
        tt.c_cc[VINTR] = '\x03';                // CTRL-C
717 79
        tt.c_cc[VQUIT] = '\x1c';                // CTRL-backslash
718
719 79
        i = tcsetattr(fd, TCSAFLUSH, &tt);
720 79
        if (i)
721 0
                vtc_log(p->vl, 4, "TCSAFLUSH %d %s", i, strerror(errno));
722 79
}
723
724
/**********************************************************************
725
 * Start the process thread
726
 */
727
728
static void
729
process_start(struct process *p)
730 79
{
731 79
        struct vsb *cl;
732 79
        int fd2[2];
733 79
        int master, slave;
734 79
        const char *slavename;
735 79
        char c;
736
737 79
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
738 79
        if (p->hasthread)
739 0
                vtc_fatal(p->vl, "Already running, -wait first");
740
741 79
        vtc_log(p->vl, 4, "CMD: %s", p->spec);
742
743 79
        cl = macro_expand(p->vl, p->spec);
744 79
        AN(cl);
745
746 79
        master = posix_openpt(O_RDWR|O_NOCTTY);
747 79
        assert(master >= 0);
748 79
        AZ(grantpt(master));
749 79
        AZ(unlockpt(master));
750 79
        slavename = ptsname(master);
751 79
        AN(slavename);
752
753 79
        AZ(pipe(fd2));
754
755 79
        p->pid = fork();
756 79
        assert(p->pid >= 0);
757 79
        if (p->pid == 0) {
758 79
                assert(setsid() == getpid());
759 79
                assert(dup2(fd2[1], STDERR_FILENO) == STDERR_FILENO);
760 79
                AZ(close(STDIN_FILENO));
761 79
                slave = open(slavename, O_RDWR);
762 79
                assert(slave == STDIN_FILENO);
763
#ifdef __sun
764
                if (ioctl(slave, I_PUSH, "ptem"))
765
                        vtc_log(p->vl, 4, "PUSH ptem: %s", strerror(errno));
766
                if (ioctl(slave, I_PUSH, "ldterm"))
767
                        vtc_log(p->vl, 4, "PUSH ldterm: %s", strerror(errno));
768
                (void)ioctl(STDIN_FILENO, TIOCSCTTY, NULL);
769
#else
770 79
                AZ(ioctl(STDIN_FILENO, TIOCSCTTY, NULL));
771 79
#endif
772 79
                AZ(close(STDOUT_FILENO));
773 79
                assert(dup2(slave, STDOUT_FILENO) == STDOUT_FILENO);
774 79
                VSUB_closefrom(STDERR_FILENO + 1);
775 79
                process_init_term(p, slave);
776
777 79
                AZ(setenv("TERM", "xterm", 1));
778 79
                AZ(unsetenv("TERMCAP"));
779
                // Not using NULL because GCC is now even more demented...
780 79
                assert(write(STDERR_FILENO, "+", 1) == 1);
781
782
                // Make sure the child will not ignore the -close signal.
783 79
                (void)signal(SIGHUP, SIG_DFL);
784
785 79
                AZ(execl("/bin/sh", "/bin/sh", "-c", VSB_data(cl), (char*)0));
786 79
                exit(1);
787 79
        }
788 0
        vtc_log(p->vl, 3, "PID: %ld", (long)p->pid);
789 0
        VSB_destroy(&cl);
790
791 0
        assert(read(fd2[0], &c, 1) == 1);
792 0
        p->fd_term = master;
793 0
        closefd(&fd2[1]);
794 0
        p->fd_stderr = fd2[0];
795 0
        macro_def(p->vl, p->name, "pid", "%ld", (long)p->pid);
796 0
        macro_def(p->vl, p->name, "dir", "%s", p->dir);
797 0
        macro_def(p->vl, p->name, "out", "%s", p->out);
798 0
        macro_def(p->vl, p->name, "err", "%s", p->err);
799 0
        p->hasthread = 1;
800 0
        PTOK(pthread_create(&p->tp, NULL, process_thread, p));
801 0
}
802
803
/**********************************************************************
804
 * Wait for process thread to stop
805
 */
806
807
static void
808
process_wait(struct process *p)
809 79
{
810 79
        void *v;
811
812 79
        if (p->hasthread) {
813 79
                PTOK(pthread_join(p->tp, &v));
814 79
                p->hasthread = 0;
815 79
        }
816 79
        vtc_log(p->vl, 4, "stdout %ju bytes, stderr %ju bytes",
817 79
            p->stdout_bytes, p->stderr_bytes);
818 79
}
819
820
/**********************************************************************
821
 * Send a signal to a process
822
 */
823
824
static void
825
process_kill(struct process *p, const char *sig)
826 30
{
827 30
        int j = 0;
828 30
        pid_t pid;
829
830 30
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
831 30
        AN(sig);
832
833 30
        PTOK(pthread_mutex_lock(&p->mtx));
834 30
        pid = p->pid;
835 30
        PTOK(pthread_mutex_unlock(&p->mtx));
836
837 30
        if (pid <= 0)
838 0
                vtc_fatal(p->vl, "Cannot signal a non-running process");
839
840 30
        if (!vstrcmp(sig, "TERM"))
841 22
                j = SIGTERM;
842 8
        else if (!vstrcmp(sig, "INT"))
843 1
                j = SIGINT;
844 7
        else if (!vstrcmp(sig, "KILL"))
845 2
                j = SIGKILL;
846 5
        else if (!vstrcmp(sig, "HUP"))
847 5
                j = SIGHUP;
848 0
        else if (*sig == '-')
849 0
                j = strtoul(sig + 1, NULL, 10);
850 0
        else
851 0
                vtc_fatal(p->vl, "Could not grok signal (%s)", sig);
852
853 30
        if (p->expect_signal == 0)
854 29
                p->expect_signal = -j;
855 30
        if (kill(-pid, j) < 0)
856 0
                vtc_fatal(p->vl, "Failed to send signal %d (%s)",
857 0
                    j, strerror(errno));
858 30
        else
859 30
                vtc_log(p->vl, 4, "Sent signal %d", j);
860 30
}
861
862
/**********************************************************************
863
 * Write to a process' stdin
864
 */
865
866
static void
867
process_write(const struct process *p, const char *text)
868 146
{
869 146
        int r, len;
870
871 146
        if (!p->hasthread)
872 0
                vtc_fatal(p->vl, "Cannot write to a non-running process");
873
874 146
        len = vstrlen(text);
875 146
        vtc_log(p->vl, 4, "Writing %d bytes", len);
876 146
        r = write(p->fd_term, text, len);
877 146
        if (r != len)
878 0
                vtc_fatal(p->vl, "Failed to write: len=%d %s (%d)",
879 0
                    len, strerror(errno), errno);
880 146
}
881
882
static void
883
process_write_hex(const struct process *p, const char *text)
884 125
{
885 125
        struct vsb *vsb;
886
887 125
        if (!p->hasthread)
888 0
                vtc_fatal(p->vl, "Cannot write to a non-running process");
889
890 125
        vsb = vtc_hex_to_bin(p->vl, text);
891 125
        assert(VSB_len(vsb) >= 0);
892 125
        vtc_hexdump(p->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
893 125
        AZ(VSB_tofile(vsb, p->fd_term));
894 125
        VSB_destroy(&vsb);
895 125
}
896
897
static void
898
process_close(struct process *p)
899 2
{
900
901 2
        if (!p->hasthread)
902 0
                vtc_fatal(p->vl, "Cannot close a non-running process");
903
904 2
        process_kill(p, "HUP");
905 2
}
906
907
/* SECTION: process process
908
 *
909
 * Run a process with stdin+stdout on a pseudo-terminal and stderr on a pipe.
910
 *
911
 * Output from the pseudo-terminal is copied verbatim to ${pNAME_out},
912
 * and the -log/-dump/-hexdump flags will also put it in the vtc-log.
913
 *
914
 * The pseudo-terminal is not in ECHO mode, but if the programs run set
915
 * it to ECHO mode ("stty sane") any input sent to the process will also
916
 * appear in this stream because of the ECHO.
917
 *
918
 * Output from the stderr-pipe is copied verbatim to ${pNAME_err}, and
919
 * is always included in the vtc_log.
920
 *
921
 *      process pNAME SPEC [-allow-core] [-expect-exit N] [-expect-signal N]
922
 *              [-dump] [-hexdump] [-log]
923
 *              [-run] [-close] [-kill SIGNAL] [-start] [-stop] [-wait]
924
 *              [-write STRING] [-writeln STRING] [-writehex HEXSTRING]
925
 *              [-need-bytes [+]NUMBER]
926
 *              [-screen-dump] [-winsz LINES COLUMNS] [-ansi-response]
927
 *              [-expect-cursor LINE COLUMN] [-expect-text LINE COLUMN TEXT]
928
 *              [-match-text LINE COLUMN REGEXP]
929
 *
930
 * pNAME
931
 *      Name of the process. It must start with 'p'.
932
 *
933
 * SPEC
934
 *      The command(s) to run in this process.
935
 *
936
 * \-hexdump
937
 *      Log output with vtc_hexdump(). Must be before -start/-run.
938
 *
939
 * \-dump
940
 *      Log output with vtc_dump(). Must be before -start/-run.
941
 *
942
 * \-log
943
 *      Log output with VLU/vtc_log(). Must be before -start/-run.
944
 *
945
 * \-start
946
 *      Start the process.
947
 *
948
 * \-expect-exit N
949
 *      Expect exit status N
950
 *
951
 * \-expect-signal N
952
 *      Expect signal in exit status N
953
 *
954
 * \-allow-core
955
 *      Core dump in exit status is OK
956
 *
957
 * \-wait
958
 *      Wait for the process to finish.
959
 *
960
 * \-run
961
 *      Shorthand for -start -wait.
962
 *
963
 *      In most cases, if you just want to start a process and wait for it
964
 *      to finish, you can use the ``shell`` command instead.
965
 *      The following commands are equivalent::
966
 *
967
 *          shell "do --something"
968
 *
969
 *          process p1 "do --something" -run
970
 *
971
 *      However, you may use the ``process`` variant to conveniently
972
 *      collect the standard input and output without dealing with shell
973
 *      redirections yourself. The ``shell`` command can also expect an
974
 *      expression from either output, consider using it if you only need
975
 *      to match one.
976
 *
977
 * \-key KEYSYM
978
 *      Send emulated key-press.
979
 *      KEYSYM can be one of (NPAGE, PPAGE, HOME, END)
980
 *
981
 *
982
 * \-kill SIGNAL
983
 *      Send a signal to the process. The argument can be either
984
 *      the string "TERM", "INT", or "KILL" for SIGTERM, SIGINT or SIGKILL
985
 *      signals, respectively, or a hyphen (-) followed by the signal
986
 *      number.
987
 *
988
 *      If you need to use other signal names, you can use the ``kill``\(1)
989
 *      command directly::
990
 *
991
 *          shell "kill -USR1 ${pNAME_pid}"
992
 *
993
 *      Note that SIGHUP usage is discouraged in test cases.
994
 *
995
 * \-stop
996
 *      Shorthand for -kill TERM.
997
 *
998
 * \-close
999
 *      Alias for "-kill HUP"
1000
 *
1001
 * \-winsz LINES COLUMNS
1002
 *      Change the terminal window size to LIN lines and COL columns.
1003
 *
1004
 * \-write STRING
1005
 *      Write a string to the process' stdin.
1006
 *
1007
 * \-writeln STRING
1008
 *      Same as -write followed by a newline (\\n).
1009
 *
1010
 * \-writehex HEXSTRING
1011
 *      Same as -write but interpreted as hexadecimal bytes.
1012
 *
1013
 * \-need-bytes [+]NUMBER
1014
 *      Wait until at least NUMBER bytes have been received in total.
1015
 *      If '+' is prefixed, NUMBER new bytes must be received.
1016
 *
1017
 * \-ansi-response
1018
 *      Respond to terminal respond-back sequences
1019
 *
1020
 * \-expect-cursor LINE COLUMN
1021
 *      Expect cursors location
1022
 *
1023
 * \-expect-text LINE COLUMNS TEXT
1024
 *      Wait for TEXT to appear at LIN,COL on the virtual screen.
1025
 *      Lines and columns are numbered 1...N
1026
 *      LIN==0 means "on any line"
1027
 *      COL==0 means "anywhere on the line"
1028
 *
1029
 * \-match-text LINE COLUMN REGEXP
1030
 *      Wait for the PAT regular expression to match the text at LIN,COL on the virtual screen.
1031
 *      Lines and columns are numbered 1...N
1032
 *      LIN==0 means "on any line"
1033
 *      COL==0 means "anywhere on the line"
1034
 *
1035
 *
1036
 * \-screen-dump
1037
 *      Dump the virtual screen into vtc_log
1038
 *
1039
 */
1040
1041
void
1042
cmd_process(CMD_ARGS)
1043 1965
{
1044 1965
        struct process *p, *p2;
1045 1965
        uintmax_t u, v, bsnap;
1046 1965
        unsigned lin,col;
1047 1965
        int spec_set = 0;
1048
1049 1965
        (void)priv;
1050
1051 1965
        if (av == NULL) {
1052
                /* Reset and free */
1053 1122
                VTAILQ_FOREACH_SAFE(p, &processes, list, p2) {
1054 59
                        if (p->pid > 0) {
1055 6
                                process_kill(p, "TERM");
1056 6
                                sleep(2);
1057 6
                                if (p->pid > 0)
1058 0
                                        process_kill(p, "KILL");
1059 6
                        }
1060 59
                        if (p->hasthread)
1061 10
                                process_wait(p);
1062 59
                        VTAILQ_REMOVE(&processes, p, list);
1063 59
                        process_undef(p);
1064 59
                        process_delete(p);
1065 59
                }
1066 1122
                return;
1067 1122
        }
1068
1069 843
        AZ(vstrcmp(av[0], "process"));
1070 843
        av++;
1071
1072 843
        VTC_CHECK_NAME(vl, av[0], "Process", 'p');
1073 843
        VTAILQ_FOREACH(p, &processes, list)
1074 1827
                if (!vstrcmp(p->name, av[0]))
1075 784
                        break;
1076 843
        if (p == NULL)
1077 59
                p = process_new(av[0]);
1078 843
        av++;
1079
1080 843
        PTOK(pthread_mutex_lock(&p->mtx));
1081 843
        bsnap = p->stdout_bytes;
1082 843
        PTOK(pthread_mutex_unlock(&p->mtx));
1083
1084 1917
        for (; *av != NULL; av++) {
1085 1074
                if (vtc_error)
1086 0
                        break;
1087
1088 1074
                if (!vstrcmp(*av, "-allow-core")) {
1089 0
                        p->allow_core = 1;
1090 0
                        continue;
1091 0
                }
1092 1074
                if (!vstrcmp(*av, "-close")) {
1093 2
                        process_close(p);
1094 2
                        continue;
1095 2
                }
1096 1072
                if (!vstrcmp(*av, "-dump")) {
1097 20
                        if (p->hasthread)
1098 0
                                vtc_fatal(p->vl,
1099 0
                                    "Cannot dump a running process");
1100 20
                        p->log = 2;
1101 20
                        continue;
1102 20
                }
1103 1052
                if (!vstrcmp(*av, "-expect-exit")) {
1104 20
                        p->expect_exit = strtoul(av[1], NULL, 0);
1105 20
                        av++;
1106 20
                        continue;
1107 20
                }
1108 1032
                if (!vstrcmp(*av, "-expect-signal")) {
1109 0
                        p->expect_signal = strtoul(av[1], NULL, 0);
1110 0
                        av++;
1111 0
                        continue;
1112 0
                }
1113 1032
                if (!vstrcmp(*av, "-hexdump")) {
1114 4
                        if (p->hasthread)
1115 0
                                vtc_fatal(p->vl,
1116 0
                                    "Cannot dump a running process");
1117 4
                        p->log = 3;
1118 4
                        continue;
1119 4
                }
1120 1028
                if (!vstrcmp(*av, "-key")) {
1121 6
                        if (!vstrcmp(av[1], "NPAGE"))
1122 2
                                process_write(p, "\x1b\x5b\x36\x7e");
1123 4
                        else if (!vstrcmp(av[1], "PPAGE"))
1124 2
                                process_write(p, "\x1b\x5b\x35\x7e");
1125 2
                        else if (!vstrcmp(av[1], "HOME"))
1126 1
                                process_write(p, "\x1b\x4f\x48");
1127 1
                        else if (!vstrcmp(av[1], "END"))
1128 1
                                process_write(p, "\x1b\x4f\x46");
1129 0
                        else
1130 0
                                vtc_fatal(p->vl, "Unknown key %s", av[1]);
1131 6
                        continue;
1132 6
                }
1133 1022
                if (!vstrcmp(*av, "-kill")) {
1134 12
                        process_kill(p, av[1]);
1135 12
                        av++;
1136 12
                        continue;
1137 12
                }
1138 1010
                if (!vstrcmp(*av, "-log")) {
1139 19
                        if (p->hasthread)
1140 0
                                vtc_fatal(p->vl,
1141 0
                                    "Cannot log a running process");
1142 19
                        p->log = 1;
1143 19
                        continue;
1144 19
                }
1145 991
                if (!vstrcmp(*av, "-need-bytes")) {
1146 9
                        u = strtoumax(av[1], NULL, 0);
1147 9
                        if (av[1][0] == '+')
1148 7
                                u += bsnap;
1149 9
                        av++;
1150 17
                        do {
1151 17
                                PTOK(pthread_mutex_lock(&p->mtx));
1152 17
                                v = p->stdout_bytes;
1153 17
                                PTOK(pthread_mutex_unlock(&p->mtx));
1154 17
                                vtc_log(p->vl, 4, "Have %ju bytes", v);
1155 17
                                VTIM_sleep(0.5);
1156 17
                        } while(v < u);
1157 9
                        continue;
1158 9
                }
1159 982
                if (!vstrcmp(*av, "-run")) {
1160 23
                        process_start(p);
1161 23
                        process_wait(p);
1162 23
                        continue;
1163 23
                }
1164 959
                if (!vstrcmp(*av, "-ansi-response")) {
1165 2
                        p->ansi_response = 1;
1166 2
                        continue;
1167 2
                }
1168 957
                if (!vstrcmp(*av, "-expect-text")) {
1169 273
                        ARGN(vl, av, 1);
1170 273
                        ARGN(vl, av, 2);
1171 273
                        ARGN(vl, av, 3);
1172 273
                        term_expect_text(p, av[1], av[2], av[3]);
1173 273
                        av += 3;
1174 273
                        continue;
1175 273
                }
1176 684
                if (!vstrcmp(*av, "-expect-cursor")) {
1177 8
                        ARGN(vl, av, 1);
1178 8
                        ARGN(vl, av, 2);
1179 8
                        term_expect_cursor(p, av[1], av[2]);
1180 8
                        av += 2;
1181 8
                        continue;
1182 8
                }
1183 676
                if (!vstrcmp(*av, "-match-text")) {
1184 34
                        ARGN(vl, av, 1);
1185 34
                        ARGN(vl, av, 2);
1186 34
                        ARGN(vl, av, 3);
1187 34
                        term_match_text(p, av[1], av[2], av[3]);
1188 34
                        av += 3;
1189 34
                        continue;
1190 34
                }
1191 642
                if (!vstrcmp(*av, "-screen_dump") ||
1192 642
                    !vstrcmp(*av, "-screen-dump")) {
1193 167
                        term_screen_dump(p);
1194 167
                        continue;
1195 167
                }
1196 475
                if (!vstrcmp(*av, "-start")) {
1197
                        // Clear the "screen"
1198 56
                        teken_input(p->tek, "\r\n\x1b[H\x1b[2J", 9);
1199 56
                        process_start(p);
1200 56
                        continue;
1201 56
                }
1202 419
                if (!vstrcmp(*av, "-stop")) {
1203 10
                        process_kill(p, "TERM");
1204 10
                        sleep(1);
1205 10
                        continue;
1206 10
                }
1207 409
                if (!vstrcmp(*av, "-wait")) {
1208 46
                        process_wait(p);
1209 46
                        continue;
1210 46
                }
1211 363
                if (!vstrcmp(*av, "-winsz")) {
1212 9
                        lin = atoi(av[1]);
1213 9
                        assert(lin > 1);
1214 9
                        col = atoi(av[2]);
1215 9
                        assert(col > 1);
1216 9
                        av += 2;
1217 9
                        PTOK(pthread_mutex_lock(&p->mtx));
1218 9
                        term_resize(p, lin, col);
1219 9
                        PTOK(pthread_mutex_unlock(&p->mtx));
1220 9
                        process_winsz(p, p->fd_term);
1221 9
                        continue;
1222 9
                }
1223 354
                if (!vstrcmp(*av, "-write")) {
1224 124
                        process_write(p, av[1]);
1225 124
                        av++;
1226 124
                        continue;
1227 124
                }
1228 230
                if (!vstrcmp(*av, "-writehex")) {
1229 125
                        process_write_hex(p, av[1]);
1230 125
                        av++;
1231 125
                        continue;
1232 125
                }
1233 105
                if (!vstrcmp(*av, "-writeln")) {
1234 8
                        process_write(p, av[1]);
1235 8
                        process_write(p, "\n");
1236 8
                        av++;
1237 8
                        continue;
1238 8
                }
1239 97
                if (**av == '-' || spec_set)
1240 0
                        vtc_fatal(p->vl, "Unknown process argument: %s", *av);
1241 97
                REPLACE(p->spec, *av);
1242 97
                spec_set = 1;
1243 97
        }
1244 843
}