4 * (C) 1989 Saeko Hirabauashi & Kouichi Hirabayashi
6 * Absolutely no warranty. Use this software with your own risk.
8 * Permission to use, copy, modify and distribute this software for any
9 * purpose and without fee is hereby granted, provided that the above
10 * copyright and disclaimer notice.
12 * This program was written to fit into 64K+64K memory of the Minix 1.2.
18 #include <sys/types.h>
29 extern char **FS
, **RS
, **OFS
, **ORS
, **FILENAME
;
30 extern double *NF
, *NR
;
33 extern SYMBOL
*argtab
[];
34 extern CELL
*getvar();
36 char *strsave(), *strcpy(), *getsval(), *jStrchr(), *strchr();
37 double getfval(), atof();
38 CELL
*mkcell(), *mktmp(), *execute(), *patexec();
41 extern CELL truecell
, falsecell
;
57 char *fnam
, *s
, str
[BUFSIZ
];
61 if ((int) p
->n_arg
[0]) /* read into var */
65 if ((int) p
->n_arg
[1]) { /* file name */
66 u
= execute(p
->n_arg
[1]);
68 fp
= getfp(fnam
, (int) p
->n_arg
[2]);
75 u
= execute(p
->n_arg
[0]);
78 return mktmp(NUM
, NULL
, (double) i
);
82 get1rec(buf
, fp
) char *buf
; FILE *fp
;
90 if ((rs
= **RS
) == '\0') { /* multi line record */
97 if (feof(fp
) || (c
= getc(fp
)) == EOF
)
100 for ( ; c
!= rs
&& c
!= EOF
; c
= getc(fp
)) {
102 *s
++ = c
; c
= getc(fp
);
107 if ((c
= getc(fp
)) == '\n' || c
== EOF
)
118 if (buf
== record
&& c
!= EOF
) {
120 mkfld(record
, *FS
, field
);
124 return s
> buf
|| c
!= EOF
? 1 : 0;
132 while (ifp
== stdin
|| infileno
< (int)*ARGC
) {
135 if (infileno
== (int)*ARGC
)
137 sprintf(str
, "%d", infileno
);
138 u
= getvar(str
, argtab
);
140 if (strchr(file
, '=') != NULL
) {
145 else if (strcmp(file
, "") == 0) {
147 if (infileno == (int)*ARGC - 1)
154 if (strcmp(file
, "-") == 0)
157 ifp
= efopen(file
, "r");
170 ifp
= stdin
; /* for further "getline" */
175 mkfld(rec
, sep
, fld
) char *rec
, *sep
; CELL
*fld
[];
183 return r_mkfld(rec
, sep
, fld
);
185 if (*sep
== ' ' || *sep
== '\0') {
186 sep
= " \t\n"; skip
++;
188 for (i
= 1, n
= (int) *NF
; i
<= n
; i
++) {
189 sfree(fld
[i
]->c_sval
);
193 for (i
= 0, s
= rec
; ; ) {
196 while (*s
&& strchr(" \t\n", *s
))
201 while (*s
&& !jStrchr(sep
, *s
)) {
208 fld
[++i
] = mkcell(FLD
|STR
|NUM
, str
, atof(str
));
210 fld
[++i
] = mkcell(FLD
|STR
, str
, 0.0);
221 r_mkfld(rec
, sep
, fld
) char *rec
, *sep
; CELL
*fld
[];
227 extern int r_start
, r_length
;
229 if (strcmp(*FS
, fs_str
) != 0) {
230 sfree(fs_str
); sfree(fs_pat
);
231 fs_str
= strsave(*FS
);
232 fs_pat
= mkpat(fs_str
);
234 for (i
= 1, n
= (int) *NF
; i
<= n
; i
++) {
235 sfree(fld
[i
]->c_sval
);
239 for (i
= 0, s
= rec
, t
= str
; *s
; ) {
240 if (match(fs_pat
, s
)) {
241 for (n
= r_start
; --n
> 0; )
250 fld
[++i
] = mkcell(FLD
|STR
, str
, 0.0);
260 register char *s
, *t
;
263 for (j
= (int)*NF
, i
= 1; i
<= j
; i
++)
267 for ( ; i
< MAXFLD
; i
++)
271 error("too many field (%d)", i
);
274 for (t
= record
, i
= 1, j
= (int) *NF
; i
<= j
; i
++) {
277 for (s
= getsval(field
[i
]); *s
; )
289 u
= execute(p
->n_arg
[0]);
290 i
= (int) getfval(u
);
294 for (++j
; j
<= i
; j
++) {
295 if (field
[j
] == NULL
)
296 field
[j
] = mkcell(FLD
|STR
, "", 0.0);
308 u
= execute(p
->n_arg
[0]);
313 u
= execute(p
->n_arg
[1]);
329 u
= execute(p
->n_arg
[0]);
334 u
= &truecell
; break;
340 u
= execute(p
->n_arg
[2]);
343 u
= execute(p
->n_arg
[1]);
361 char *s, str[BUFSIZ];
363 for (*str = '\0', i = 1, j = (int) *NF; i <= j; i++) {
366 s = getsval(field[i]);
372 fprintf(stdout
, "%s%s", record
, *ORS
);
377 format(t
, p
) char *t
; NODE
*p
;
380 char *r
, *s
, *s0
, fmt
[BUFSIZ
];
384 u
= execute(p
->n_arg
[2]);
387 printf("fmt(%s)\n", s);
389 for (i
= 3; *s
; s
++) {
391 *t
++ = *s
++; *t
++ = *s
; continue;
396 else if (*(s
+ 1) == '%') {
397 *t
++ = *s
++; continue;
399 for (r
= fmt
, *r
++ = *s
++; *r
++ = *s
; s
++) {
400 if (strchr("%cdefgosux", *s
))
404 if (p
->n_arg
[i
] == NULL
)
405 error("not enough args in printf(%s)", s0
);
406 v
= execute(p
->n_arg
[i
++]);
412 printf("val(%d)(%s)\n", v->c_type, v->c_sval);
416 sprintf(t
, fmt
, (int) x
);
419 if (*(s
- 1) != 'l') {
420 *--r
= 'l'; *++r
= 'd'; *++r
= '\0';
422 sprintf(t
, fmt
, (long) x
);
424 case 'e': case 'f': case 'g':
427 case 'o': case 'u': case 'x':
429 sprintf(t
, fmt
, (long) x
);
431 sprintf(t
, fmt
, (int) x
);
450 char *f_name
; /* file name */
456 getfp(file
, type
) char *file
;
459 register char *name
, *mode
;
461 FILE *fp
, *efopen(), *epopen();
463 for (i
= 0; i
< MAXFILE
; i
++)
464 if (filetab
[i
].f_name
&& strcmp(filetab
[i
].f_name
, file
) == 0)
465 return filetab
[i
].f_fp
;
466 for (i
= 0; i
< MAXFILE
; i
++)
467 if (!filetab
[i
].f_fp
)
470 error("too many files to open");
473 case R_OUT
: mode
= "w"; break;
474 case R_APD
: mode
= "a"; break;
477 name
= awktmp(i
); mode
= "w"; /* MS-DOS */
479 fp
= epopen(file
, "w");
483 case R_IN
: mode
= "r"; break;
487 int savefd
, fd
, result
;
491 O_WRONLY
|O_TEXT
|O_CREAT
|O_TRUNC
,S_IREAD
|S_IWRITE
)) == -1)
492 error("can't open %s", name
);
493 savefd
= dup(1); dup2(fd
, 1); close(fd
);
496 error("can't exec %s", file
);
497 dup2(savefd
, 1); close(savefd
); close(fd
);
501 fp
= epopen(file
,"r");
506 fp
= efopen(name
, mode
);
508 filetab
[i
].f_name
= strsave(file
);
509 filetab
[i
].f_type
= type
;
510 filetab
[i
].f_fp
= fp
;
518 for (i
= 0; i
< MAXFILE
; i
++)
527 for (i
= 0; i
< MAXFILE
; i
++)
528 if (strcmp(s
, filetab
[i
].f_name
) == 0) {
532 i
= (i
== MAXFILE
) ? 0 : 1;
533 return mktmp(NUM
, NULL
, (double) i
);
539 int fd
, result
, savefd
;
542 if (filetab
[i
].f_fp
== NULL
)
544 switch (filetab
[i
].f_type
) {
547 fclose(filetab
[i
].f_fp
);
550 pclose(filetab
[i
].f_fp
);
553 case R_IN
: case R_OUT
: case R_APD
:
554 fclose(filetab
[i
].f_fp
);
558 fclose(filetab
[i
].f_fp
);
559 if ((fd
= open(awktmp(i
), O_RDONLY
)) == NULL
)
560 error("can't open %s", awktmp(i
));
565 system(filetab
[i
].f_name
)) == -1)
567 spawnl(P_WAIT, "/usr/bin/sh", "sh", "-c", filetab[i].f_name, (char *) 0)) == -1)
568 fprintf(stderr, "can't spawn /bin/sh\n");
570 error("can't exec %s", filetab
[i
].f_name
);
575 pclose(filetab
[i
].f_fp
);
579 sfree(filetab
[i
].f_name
);
580 filetab
[i
].f_type
= 0;
581 filetab
[i
].f_name
= NULL
;
582 filetab
[i
].f_fp
= NULL
;
587 epopen(file
, mod
) char *file
, *mod
;
591 if ((fp
= popen(file
, mod
)) == NULL
)
592 error("can't poen %s", file
);
602 sprintf(str
, "awk000%02d.tmp", i
);
606 Index(s
, t
) char *s
, *t
;
608 register char *u
, *v
;
611 for (i
= 1; *s
; s
++, i
++) {
612 for (u
= s
, v
= t
; *v
; u
++, v
++) {