Robin Peterson and AB de Villiers hauled South Africa back into the game on the third day of the second Test against Pakistan at Newlands on Saturday.
South Africa, struggling on 139 for five overnight, racked up 326 thanks to 84 from Peterson and 61 by De Villiers.
Pakistan then reached the close of play on 100 for three, an overall lead of 112 runs.
Pakistan first innings 338
South Africa first innings
(139 for five overnight)
G Smith lbw Ajmal 19
A Petersen c Azhar Ali b Ajmal 17
H Amla lbw b Ajmal 25
F du Plessis c Younus Khan b Ajmal 28
J Kallis lbw b Ajmal 2
AB de Villiers c Gul b Irfan 61
D Elgar c Younus Khan b 23
R Peterson c Gul b Hafeez 84
V Philander c Jamshed b Irfan 22
D Steyn c Sarfraz b Irfan 10
M Morkel not out 8
Total (all out, 102.1 overs) 326
Fall of wickets: 1-36 2-50 3-84 4-102 5-109, 6-164, 7-210,
8-277, 9-303, 10-326.
Pakistan second innings
M Hafeez lbw Steyn 0
N Jamshed lbw Philander 0
A Ali not out 45
Y Khan b Steyn 14
M-ul-Haq not out 36
Total (for three wickets, 44 overs) 100
Fall of wickets: 1-0 2-7 3-45.
The visitors slumped to seven for two after openers Mohammad Hafeez and Nasir Jamshed were both removed for a duck.
Younus Khan (14) then became fast bowler Dale Steyn's second victim of the innings when he chopped the ball on to his stumps.
But Azhar Ali (45 not out) and Misbah-ul-Haq (36 not out) settled Pakistani nerves with an undefeated fourth-wicket stand of 55.
The home team were hampered by the loss of paceman Morne Morkel who succumbed to a hamstring injury after having delivered 19 balls. He is unlikely to bowl again in the match.
Earlier, Peterson powered South Africa to within 12 of Pakistan's first-innings total after compiling his highest Test score.
His only previous Test half-century came against Bangladesh a decade ago.
Left-arm spinner Peterson has not had a great game with the ball but he showed his value as an all-rounder at number eight as he completely changed the momentum of the innings.
It was tough again early on for the batsmen, De Villiers and Dean Elgar adding 25 runs to South Africa's overnight score before the latter became off-spinner Saeed Ajmal's sixth wicket of the innings.
The left-handed Elgar flashed at a ball on 23 that gripped the surface and caught the edge, flying to Younus at slip.
Peterson joined De Villiers at the crease and the pair put on 46 for the seventh wicket in 11 overs.
De Villiers went to his 32nd Test half-century before debutant Mohammad Irfan claimed his first Test wicket, forcing the South African to spoon a catch to Umar Gul at mid-on when he had made 61.
Vernon Philander (22) then combined with Peterson for a partnership of 67 before the former was bounced out by Irfan with a well-directed short ball that was edged to Jamshed at point.
Steyn was dismissed for 10 before Peterson was the last to go. He tried to hit a six off part-time spinner Hafeez but succeeded only in picking out Gul on the long-on boundary.
Ajmal finished with six for 96 while Irfan picked up three for 86.
South Africa are 1-0 up in the three-match series.