version 1.7, 1997/01/24 23:43:10 |
version 1.8, 1997/01/25 00:24:31 |
|
|
/* $OpenBSD$ */ |
|
/**************************************************************** |
/**************************************************************** |
Copyright (C) AT&T and Lucent Technologies 1996 |
Copyright (C) AT&T and Lucent Technologies 1996 |
All Rights Reserved |
All Rights Reserved |
|
|
return(z); |
return(z); |
} |
} |
|
|
/* |
int format(char *buf, int bufsize, char *s, Node *a) /* printf-like conversions */ |
* printf-like conversions |
|
* returns len of buf or -1 on error |
|
*/ |
|
int format(char *buf, int bufsize, char *s, Node *a) |
|
{ |
{ |
char fmt[RECSIZE]; |
char fmt[RECSIZE]; |
char *p, *t, *os; |
char *p, *t, *os; |
Cell *x; |
Cell *x; |
int flag = 0, len = 0, n; |
int flag = 0, n; |
|
|
os = s; |
os = s; |
p = buf; |
p = buf; |
|
|
if (p - buf >= bufsize) |
if (p - buf >= bufsize) |
return -1; |
return -1; |
if (*s != '%') { |
if (*s != '%') { |
len++; |
|
*p++ = *s++; |
*p++ = *s++; |
continue; |
continue; |
} |
} |
if (*(s+1) == '%') { |
if (*(s+1) == '%') { |
len++; |
|
*p++ = '%'; |
*p++ = '%'; |
s += 2; |
s += 2; |
continue; |
continue; |
|
|
a = a->nnext; |
a = a->nnext; |
switch (flag) { |
switch (flag) { |
case 0: sprintf((char *)p, "%s", fmt); /* unknown, so dump it too */ |
case 0: sprintf((char *)p, "%s", fmt); /* unknown, so dump it too */ |
len += strlen(p); |
|
p += strlen(p); |
p += strlen(p); |
sprintf((char *)p, "%s", getsval(x)); |
sprintf((char *)p, "%s", getsval(x)); |
break; |
break; |
|
|
sprintf((char *)p, (char *)fmt, t); |
sprintf((char *)p, (char *)fmt, t); |
break; |
break; |
case 5: |
case 5: |
isnum(x) ? |
isnum(x) ? sprintf((char *)p, (char *)fmt, (int) getfval(x)) |
(getfval(x) ? |
|
sprintf((char *)p, (char *)fmt, (int) getfval(x)) |
|
: len++) |
|
: sprintf((char *)p, (char *)fmt, getsval(x)[0]); |
: sprintf((char *)p, (char *)fmt, getsval(x)[0]); |
break; |
break; |
} |
} |
tempfree(x); |
tempfree(x); |
len += strlen(p); |
|
p += strlen(p); |
p += strlen(p); |
s++; |
s++; |
} |
} |
*p = '\0'; |
*p = '\0'; |
for ( ; a; a = a->nnext) /* evaluate any remaining args */ |
for ( ; a; a = a->nnext) /* evaluate any remaining args */ |
execute(a); |
execute(a); |
return (len); |
return 0; |
} |
} |
|
|
Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */ |
Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */ |
|
|
Cell *x; |
Cell *x; |
Node *y; |
Node *y; |
char buf[3*RECSIZE]; |
char buf[3*RECSIZE]; |
int len; |
|
|
|
y = a[0]->nnext; |
y = a[0]->nnext; |
x = execute(a[0]); |
x = execute(a[0]); |
if ((len = format(buf, sizeof buf, getsval(x), y)) == -1) |
if (format(buf, sizeof buf, getsval(x), y) == -1) |
ERROR "printf string %.30s... too long", buf FATAL; |
ERROR "printf string %.30s... too long", buf FATAL; |
tempfree(x); |
tempfree(x); |
if (a[1] == NULL) { |
if (a[1] == NULL) { |
if (write(1, buf, len) != len) |
fputs((char *)buf, stdout); |
|
if (ferror(stdout)) |
ERROR "write error on stdout" FATAL; |
ERROR "write error on stdout" FATAL; |
} else { |
} else { |
fp = redirect((int)a[1], a[2]); |
fp = redirect((int)a[1], a[2]); |
ferror(fp); /* XXX paranoia */ |
fputs((char *)buf, fp); |
if (write(fileno(fp), buf, len) != len) |
fflush(fp); |
|
if (ferror(fp)) |
ERROR "write error on %s", filename(fp) FATAL; |
ERROR "write error on %s", filename(fp) FATAL; |
} |
} |
return(true); |
return(true); |