xref: /freebsd/contrib/one-true-awk/testdir/T.getline (revision 23f24377b1a9ab6677f00f2302484d6658d94cab)
1*23f24377SWarner Loshecho T.getline: test getline function
2*23f24377SWarner Losh
3*23f24377SWarner Loshawk=${awk-../a.out}
4*23f24377SWarner Losh
5*23f24377SWarner Loshwho >foo1
6*23f24377SWarner Loshcat foo1 | $awk '
7*23f24377SWarner LoshBEGIN {
8*23f24377SWarner Losh	while (getline)
9*23f24377SWarner Losh		print
10*23f24377SWarner Losh	exit
11*23f24377SWarner Losh}
12*23f24377SWarner Losh' >foo
13*23f24377SWarner Loshcmp -s foo1 foo || echo 'BAD: T.getline (bare getline)'
14*23f24377SWarner Losh
15*23f24377SWarner Loshwho >foo1
16*23f24377SWarner Loshcat foo1 | $awk '
17*23f24377SWarner LoshBEGIN {
18*23f24377SWarner Losh	while (getline xxx)
19*23f24377SWarner Losh		print xxx
20*23f24377SWarner Losh	exit
21*23f24377SWarner Losh}
22*23f24377SWarner Losh' >foo
23*23f24377SWarner Loshcmp -s foo1 foo || echo 'BAD: T.getline (getline xxx)'
24*23f24377SWarner Losh
25*23f24377SWarner Losh$awk '
26*23f24377SWarner LoshBEGIN {
27*23f24377SWarner Losh	while (getline <"/etc/passwd")
28*23f24377SWarner Losh		print
29*23f24377SWarner Losh	exit
30*23f24377SWarner Losh}
31*23f24377SWarner Losh' >foo
32*23f24377SWarner Loshcmp -s /etc/passwd foo || echo 'BAD: T.getline (getline <file)'
33*23f24377SWarner Losh
34*23f24377SWarner Loshcat /etc/passwd | $awk '
35*23f24377SWarner LoshBEGIN {
36*23f24377SWarner Losh	while (getline <"-")	# stdin
37*23f24377SWarner Losh		print
38*23f24377SWarner Losh	exit
39*23f24377SWarner Losh}
40*23f24377SWarner Losh' >foo
41*23f24377SWarner Loshcmp -s /etc/passwd foo || echo 'BAD: T.getline (getline <"-")'
42*23f24377SWarner Losh
43*23f24377SWarner Losh$awk '
44*23f24377SWarner LoshBEGIN {
45*23f24377SWarner Losh	while (getline <ARGV[1])
46*23f24377SWarner Losh		print
47*23f24377SWarner Losh	exit
48*23f24377SWarner Losh}
49*23f24377SWarner Losh' /etc/passwd >foo
50*23f24377SWarner Loshcmp -s /etc/passwd foo || echo 'BAD: T.getline (getline <arg)'
51*23f24377SWarner Losh
52*23f24377SWarner Losh$awk '
53*23f24377SWarner LoshBEGIN {
54*23f24377SWarner Losh	while (getline x <ARGV[1])
55*23f24377SWarner Losh		print x
56*23f24377SWarner Losh	exit
57*23f24377SWarner Losh}
58*23f24377SWarner Losh' /etc/passwd >foo
59*23f24377SWarner Loshcmp -s /etc/passwd foo || echo 'BAD: T.getline (getline x <arg)'
60*23f24377SWarner Losh
61*23f24377SWarner Losh$awk '
62*23f24377SWarner LoshBEGIN {
63*23f24377SWarner Losh	while (("cat " ARGV[1]) | getline)
64*23f24377SWarner Losh		print
65*23f24377SWarner Losh	exit
66*23f24377SWarner Losh}
67*23f24377SWarner Losh' /etc/passwd >foo
68*23f24377SWarner Loshcmp -s /etc/passwd foo || echo 'BAD: T.getline (cat arg | getline)'
69*23f24377SWarner Losh
70*23f24377SWarner Losh$awk '
71*23f24377SWarner LoshBEGIN {
72*23f24377SWarner Losh	while (("cat " ARGV[1]) | getline x)
73*23f24377SWarner Losh		print x
74*23f24377SWarner Losh	exit
75*23f24377SWarner Losh}
76*23f24377SWarner Losh' /etc/passwd >foo
77*23f24377SWarner Loshcmp -s /etc/passwd foo || echo 'BAD: T.getline (cat arg | getline x)'
78*23f24377SWarner Losh
79*23f24377SWarner Losh$awk ' BEGIN { print getline <"/glop/glop/glop" } ' >foo
80*23f24377SWarner Loshecho '-1' >foo1
81*23f24377SWarner Loshcmp -s foo foo1 || echo 'BAD: T.getline (non-existent file)'
82*23f24377SWarner Losh
83*23f24377SWarner Loshecho 'false false equal' >foo1
84*23f24377SWarner Losh$awk 'BEGIN {
85*23f24377SWarner Losh	"echo 0" | getline
86*23f24377SWarner Losh	if ($0) printf "true "
87*23f24377SWarner Losh	else printf "false "
88*23f24377SWarner Losh	if ($1) printf "true "
89*23f24377SWarner Losh	else printf "false "
90*23f24377SWarner Losh	if ($0==$1) printf "equal\n"
91*23f24377SWarner Losh	else printf "not equal\n"
92*23f24377SWarner Losh}' >foo2
93*23f24377SWarner Loshcmp -s foo1 foo2 || echo 1>&2 'BAD: T.getline bad $0 type in cmd|getline'
94*23f24377SWarner Losh
95*23f24377SWarner Loshecho 'L1
96*23f24377SWarner LoshL2' | $awk 'BEGIN { $0="old stuff"; $1="new"; getline x; print}' >foo1
97*23f24377SWarner Loshecho 'new stuff' >foo2
98*23f24377SWarner Loshcmp -s foo1 foo2 || echo 1>&2 'BAD: T.getline bad update $0'
99