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