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 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.
Team manager and doctor Mohammad Moosajee said he was a doubtful starter for the third and final Test starting at Centurion on Friday.
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.
"I had a good chat to a few people about how we were going to play Saeed Ajmal," he said.
Off-spinner Ajmal took all five wickets on Friday and finished with six for 96.
"Saeed is a genius of spin bowling," added Peterson.
"He can bowl about four different deliveries an over. Even if you can pick all of them it is tough to have that absolute concentration all the time. We had our hands full but the more overs he bowled the easier it became."
Both Peterson and Pakistan bowling coach Mohammad Akram felt that a target of around 250 was a benchmark.
"We don't want to chase more than 300," said Peterson. "Hopefully it will be under 250."
Akram said: "Anything over 250 will be a really good total."
He said although Pakistan were disappointed that South Africa's lower order had made so many runs the team was in a good position.
"South Africa are a good side. You expect you will get serious resistance. We are still in the game. After (defeat in the first Test in) Johannesburg, the boys are really upbeat about where we are today," added Akram.
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 while, in the next over, Nasir Jamshed fell in similar fashion to Philander.
Azhar Ali 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 reached 36 not out while Ali 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 Ajmal for 22.
At that stage off-spinner Ajmal had taken all six wickets.
The left-handed Peterson shrugged 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 10 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 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.