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