aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/io.c
blob: e06bfcc742244aedb1a6287bf7f2a798edbd9be1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include "rc.h"

#define	c0	    t->child[0]
#define	c1	    t->child[1]
#define	c2	    t->child[2]

#undef  bufsize
#define bufsize 512
#define strsize 100

//------------------------------------------------------------------------
// buffer operations

/* open file */
Io*
openfd(int fd)
{
    Io *f;

    f     = emalloc(sizeof *f + bufsize);
    f->fd = fd;
    f->b  = f->e = f->buf;
    return f;
}

/* open string */
Io*
openstr(void)
{
    Io *f;

    f     = emalloc(sizeof *f + strsize + 1);
    f->fd = -1;
    f->b  = f->buf;
    f->e  = f->buf+strsize;
    memset(f->b, 0, strsize+1);

    return f;
}

/* open core (not nil terminated) */
Io*
opencore(int len, char *s)
{
    Io *f;

    f = emalloc(sizeof *f + len);
    f->fd = -1;
    f->b  = f->buf;
    f->e  = f->buf+len;
    memcpy(f->b, s, len);

    return f;
}

void
rewindio(Io *f)
{
    if (f->fd < 0)
        f->b = f->buf;
    else {
        f->b = f->e = f->buf;
        lseek(f->fd, 0, 0);
    }
}

void
closeio(Io *f)
{
    if (f->fd >= 0)
        close(f->fd);

    efree(f);
}

/* has the chance to realloc */
void
flush(Io **fp)
{
    int   n;
    char *s;
    Io   *f;

    f = *fp;
    if (f->fd < 0) {
        n = f->e - f->b;
        f = erealloc(f, sizeof *f + n + strsize + 1);
        if (!f)
            panic("can't realloc %d bytes in flush", n+strsize+1);
        f->b = f->buf+n;
        f->e = f->buf+n+strsize;
        memset(f->b, 0, strsize+1);
    } else {
        n = f->b - f->buf;
        if (n && write(f->fd, f->buf, n) < 0) {
            write(3, "write error\n", 12);
            // if (ntrap)
            //     dotrap();
        }
        f->b = f->buf;
        f->e = f->buf + bufsize;
    }

    *fp  = f;
}

//------------------------------------------------------------------------
// read from io

int
rchr(Io *f)
{
    int n;
    if (f->b == f->e) {
        if (f->fd < 0 || (n = read(f->fd, f->buf, bufsize)) <= 0)
            return EOF;

        f->b = f->buf;
        f->e = f->b + n;
    }

    return *f->b++&0xFF;
}

//------------------------------------------------------------------------
// printf functionality

/* character literal */
int
pchr(Io *f, int c)
{
    if (f->b == f->e)
        flush(&f);

    return *f->b++=c;
}

/* quote */
void
pquo(Io *f, char *s)
{
    pchr(f, '\'');
    for (; *s; s++)
        if (*s == '\'')
            pfmt(f, "''");
        else
            pchr(f, *s);
    pchr(f, '\'');
}

/* word */
void
pwrd(Io *f, char *s)
{
    char *t;
    for(t = s; *t; t++)
        if(!wordchr(*t))
            break;
    if(t == s || *t)
        pquo(f, s);
    else
        pstr(f, s);
}

/* pointer */
void
pptr(Io *f, void *v)
{
    int n;
    uintptr p;

    if (!v) {
        pstr(f, "<nil>");
        return;
    }

    p = (uintptr)v;
    if ((sizeof(uintptr) == sizeof(uvlong)) && p >>32)
        for (n = 60; n >= 32; n-=4)
            pchr(f, "0123456789ABCDEF"[(p>>n)&0xF]);
    for (n = 28; n >= 0; n-=4)
        pchr(f, "0123456789ABCDEF"[(p>>n)&0xF]);
}

/* string */
void
pstr(Io *f, char *s)
{
    if(!s || !s[0])
        s = "<null>";

    while(*s)
        pchr(f, *s++);
}

/* decimal */
void
pdec(Io *f, int n)
{
    if (n < 0) {
        n = -n;
        pchr(f, '-');
        if (n >= 0) {
            pdec(f, n);
            return;
        }
        n = 1 - n;
        pdec(f, n/10);
        pchr(f, n%10+'1');
        return;
    }

    if (n > 9)
        pdec(f, n/10);
    pchr(f, n%10+'0');
}

/* octal */
void
poct(Io *f, uint n)
{
    if (n > 7)
        poct(f, n>>3);
    pchr(f, (n&7)+'0');
}

/* value */
void
pval(Io *f, Word *a)
{
    if(a) {
        while(a->link && a->link->word) {
            pwrd(f, a->word);
            pchr(f, ' ');
            a = a->link;
        }
        pwrd(f, a->word);
    }
}

/* tree */
static
void
pdeglob(Io *f, char *s)
{
	while(*s){
		if(*s==GLOB)
			s++;
		pchr(f, *s++);
	}
}

void
pcmd(Io *f, Tree *t)
{
    if(!t)
        return;

    switch(t->type){
    default:	    pfmt(f, "bad<%d> %p %p %p", t->type, c0, c1, c2);
	break;
	case Tdol:	    pfmt(f, "$%t", c0);
	break;
	case Tquote:	pfmt(f, "$\"%t", c0);
	break;
    case Tand:      pfmt(f, "%t&", c0);
	break;
	case Tcarot:	pfmt(f, "%t^%t", c0, c1);
	break;
	case Ttick:	    pfmt(f, "`%t", c0);
	break;
	case Tandand:	pfmt(f, "%t && %t", c0, c1);
	break;
	case Tbang:	    pfmt(f, "! %t", c0);
	break;
	case Tbrace:	pfmt(f, "{%t}", c0);
	break;
	case Tcount:	pfmt(f, "$#%t", c0);
	break;
	case Tfunc:	    pfmt(f, "func %t %t", c0, c1);
	break;
	case Tif:	    (c2) ? pfmt(f, "if%t%t else %t", c0, c1, c2): pfmt(f, "if%t%t", c0, c1);
	break;
	case Toror:	    pfmt(f, "%t || %t", c0, c1);
	break;
	case Tpcmd:     /* fallthrough */
	case Tparen:    pfmt(f, "(%t)", c0);
	break;
	case Tsub:	    pfmt(f, "$%t(%t)", c0, c1);
	break;
	case Tsimple:	pfmt(f, "%t", c0);
	break;
	case Tsubshell:	pfmt(f, "@ %t", c0);
	break;
	case Tswitch:	pfmt(f, "switch %t %t", c0, c1);
	break;
    case Tcase:	    pfmt(f, "case %t:\n%t", c0, c1);
	break;
	case Ttwiddle:	pfmt(f, "~ %t %t", c0, c1);
	break;
	case Twhile:	pfmt(f, "while %t%t", c0, c1);
	break;
	case Targs:
		if(c0==0)
			pfmt(f, "%t", c1);
		else if(c1==0)
			pfmt(f, "%t", c0);
		else
			pfmt(f, "%t %t", c0, c1);
		break;
	case Tsemi:
		if(c0) {
			if(c1)
				pfmt(f, "%t%c%t", c0, '\n', c1);
			else 
                pfmt(f, "%t", c0);
		} else 
            pfmt(f, "%t", c1);
		break;
	case Twords:
		if(c0)
			pfmt(f, "%t ", c0);
		pfmt(f, "%t", c1);
		break;
	case Tfor:
		pfmt(f, "for(%t", c0);
		if(c1)
			pfmt(f, " in %t", c1);
		pfmt(f, ")%t", c2);
		break;
	case Tword:
		if(t->quoted)
			pfmt(f, "%Q", t->str);
		else 
            pdeglob(f, t->str);
		break;
	case Tdup:
		if(t->redir.type==Rdupfd)
			pfmt(f, ">[%d=%d]", t->redir.fd[1], t->redir.fd[0]); /* yes, fd1, then fd0; read lex.c */
		else
			pfmt(f, ">[%d=]", t->redir.fd[0]);
		pfmt(f, "%t", c1);
		break;
	case Tpipefd:
	case Tredir:
		switch(t->redir.type){
		case Rhere:
			pchr(f, '<');
		case Rread:
		case Rrdwr:
			pchr(f, '<');
			if(t->redir.type==Rrdwr)
				pchr(f, '>');
			if(t->redir.fd[0]!=0)
				pfmt(f, "[%d]", t->redir.fd[0]);
			break;
		case Rappend:
			pchr(f, '>');
		case Rwrite:
			pchr(f, '>');
			if(t->redir.fd[0]!=1)
				pfmt(f, "[%d]", t->redir.fd[0]);
			break;
		}
		pfmt(f, "%t", c0);
		if(c1)
			pfmt(f, " %t", c1);
		break;
	case Teq:
		pfmt(f, "%t=%t", c0, c1);
		if(c2)
			pfmt(f, " %t", c2);
		break;
	case Tpipe:
		pfmt(f, "%t|", c0);
		if(t->redir.fd[1]==0){
			if(t->redir.fd[0]!=1)
				pfmt(f, "[%d]", t->redir.fd[0]);
		}
		else pfmt(f, "[%d=%d]", t->redir.fd[0], t->redir.fd[1]);
		pfmt(f, "%t", c1);
		break;
	}
}

/* rc specific printf */
static int pfmtlev;

void
vpfmt(Io *f, char *fmt, va_list args) 
{
    char err[124];

    pfmtlev++;
    for (; *fmt; fmt++)
        if (*fmt!='%')
            pchr(f, *fmt);
        else switch(*++fmt) {
        case '\0':
            break;
        case 'c':
            pchr(f, va_arg(args, int));
            break;
        case 'd':
            pdec(f, va_arg(args, int));
            break;
        case 'o':
            poct(f, va_arg(args, uint));
            break;
        case 'p':
            pptr(f, va_arg(args, void*));
            break;
        case 'Q':
            pquo(f, va_arg(args, char*));
            break;
        case 'q':
            pwrd(f, va_arg(args, char*));
            break;
        case 'r':
            pstr(f, strerror(errno));
            break;
        case 's':
            pstr(f, va_arg(args, char*));
            break;
        case 't':
            pcmd(f, va_arg(args, Tree*));
            break;
        case 'v':
            pval(f, va_arg(args, Word*));
            break;
        }

    if (--pfmtlev==0)
        flush(&f);
}

void
pfmt(Io *f, char *fmt, ...)
{
    va_list args;
    char    err[124];

    va_start(args, fmt);
    vpfmt(f, fmt, args);
    va_end(args);
}