Robin Peterson and Dale Steyn led a South African fightback which left the second Test against Pakistan evenly poised at the end of the third day at Newlands on Saturday.
Pakistan were 100 for three in their second innings at the close, a lead of 112.
Peterson hit 84 to enable South Africa to get within 12 runs of Pakistan's first innings total of 338 and Steyn bowled at blistering pace to take two wickets as Pakistan lost their first three second innings wickets for 45 runs.
At that stage South Africa were slightly on top but Azhar Ali and captain Misbah-ul-Haq shared a largely cautious unbeaten partnership of 55 to see Pakistan through to the close without further loss.
South Africa were hampered by a recurrence of a hamstring injury to Morne Morkel, who left the field after bowling one ball in his second spell.
He had to stop bowling because of the same injury in the first innings as well and seems unlikely to take the ball again in the match.
Peterson led a fighting performance by South Africa's lower order batsmen as they recovered from an overnight 139 for five to reach 326 all out.
Pakistan's first innings lead of 12 runs was soon shown to be negligible as Steyn and Vernon Philander both took wickets in their first overs.
Mohammad Hafeez was comprehensively trapped leg before wicket by a ball which swung into him. After the briefest of consultations with opening partner Nasir Jamshed he walked off without seeking a review.
In the next over Jamshed fell in similar fashion to Philander.
After discussion with new partner Azhar he started to indicate he wanted a review but appeared to be told by umpire Steve Davis that he had taken longer than the 15 seconds allowed.
Replays showed the ball would have clipped his bails so the decision would have been upheld.
Azhar and first innings centurion Younis Khan battled through some testing bowling against Steyn, Philander and Morkel.
Bowling downwind in his second spell, Steyn was timed at more than 146kmh and troubled both batsmen before Younis, forced on to the back foot, chopped the ball into his stumps.
After being dismissed without scoring in the first innings, Misbah took 18 balls to score his first runs in the second innings.
Scoring slowed to a crawl, with left-arm spinner Peterson conceding only four runs in his first eight overs.
But Misbah gained in confidence, three times hitting Peterson for six as he advanced to 36 not out. Azhar was unbeaten on 45.
Overnight batsmen AB de Villiers and Dean Elgar started South Africa's recovery when they took their sixth wicket stand to 55 before Elgar was caught at slip off Saeed Ajmal for 22.
At that stage off-spinner Ajmal had taken all six wickets but he had no further success, finishing with six for 96.
The left-handed Peterson batted positively from the start, shrugging off a poor run of batting form which had yielded just 15 runs in his previous seven international innings, including five noughts.
He went to his second Test fifty off 73 balls and went on to pass his previous best score of 61 against Bangladesh in Dhaka ten years ago.
De Villiers fell to Mohammad Irfan for 61 in the first over with the second new ball after a seventh wicket stand of 46 with Peterson.
But Peterson and Philander (22) added an innings-best 67 for the eighth wicket. The last two wickets brought another 49 runs before Peterson was last out, caught on the long-on boundary off Mohammad Hafeez.
Peterson made his 84 runs off 106 balls with 15 fours.
The tall Irfan, playing in his first Test, took three for 86 and troubled the batsmen with steep bounce.