An unbeaten half-century from Kumar Sangakkara on day four helped give Sri Lanka a chance of saving the first Test against South Africa at Galle.
The Proteas declared for the second time in the match at tea on day four with a lead of 369 and four sessions remaining to dismiss their hosts.
When Upul Tharanga fell with just 14 on the board at a ground that has never seen 300 made in the final innings, that looked like it would be a straightforward task.
But Sangakkara made 58 and Kaushal Silva contributed 37 in an unbeaten partnership of 96 to leave Sri Lanka needing 260 on the final day with nine wickets remaining.
It took the tourists less than five overs to take the one wicket they needed overnight with AB de Villiers claiming a stunning catch at second slip off the bowling of Morne Morkel to remove Rangana Herath for 19.
That gave the Proteas first-innings a lead of 163 on a ground where the highest winning total ever chased down is just 96.
South Africa lost Dean Elgar for 12 as the opener got an inside edge onto a Herath delivery and Dinesh Chandimal claimed the catch behind the stumps.
Alviro Petersen (32) dispatched Dilruwan Perera to the boundary twice in three balls but the bowler had his revenge in his next over thanks to an absolute ripper which feathered the edge.
Skipper Hashim Amla (22) was Perera's second wicket with Tharanga taking a low catch at short mid-on to bring De Villiers to the crease.
He and Faf du Plessis put on 43 for the fourth wicket before Herath got one to bite and turn and take out the latter's off stump for 37.
De Villiers and Quinton de Kock then decided it was time to up the pace as the pair of them hit six boundaries in five overs to edge South Africa closer to what they thought would be a safe declaration.
De Villiers reached 50 off just 55 balls but was gone for the addition of only one more, clean bowled to become Perera's third scalp.
De Kock looped one back to him to take the bowler's figures to four for 79 and with that Amla brought his men in at 206 for six.
The hosts had to survive four sessions to save the Test but got off to a poor start as Tharanga nicked Dale Steyn behind in the third over.
That brought Sangakkara to the crease with Sri Lanka in desperate need for their talisman to perform and he did not disappoint, guiding them to the close at 110 for one alongside Kaushal and keeping the Test just about alive.