South Africa fought their way back into the second Test with Pakistan on day three, with Robin Peterson an unlikely saviour with the bat.
The Proteas began Saturday's play at 5-139 in their first innings in reply to Pakistan's 338, but Peterson provided some stiff resistance in the South Africa tail to see the hosts in Cape Town to 326 all out.
Peterson's 84 was his highest Test score in 10 career matches and it set the game up well for the top-ranked Test side, with their all-star pace attack then tearing through the Pakistan top order before a brief fightback saw the tourists to 3-100 at stumps - an overall lead of 112.
Dale Steyn (2-24) and Vernon Philander (1-26) were at the fore of the attack, however Morne Morkel was a casualty after pulling up with a suspected hamstring injury just one ball into his second spell.
At the crease for Pakistan are Azhar Ali (45) and Misbah-ul-Haq (36), in the midst of a partnership worth 55 runs.
Opener Mohammad Hafeez was trapped lbw by Steyn for a second-ball duck, before Nasir Jamshed was back in the pavilion seven balls later, also for a duck, when Philander struck the opener's pads.
Ali and Younis Khan steadied the innings before the latter dragged onto his stumps off Steyn when on 14.
Misbah got his eye in before hitting the wind out of Peterson's sails with three sixes off the spinner.
Earlier, Peterson came to the crease at 6-164 when Dean Elgar became Saeed Ajmal's sixth dismissal of the inning.
And the tail-ender showed superb poise to bat for 106 deliveries, striking 15 boundaries and linking up for partnerships with AB de Villiers (61) and Vernon Philander (22) worth 46 and 67 runs respectively.
Mohammad Irfan (3-86) and Mohammad Hafeez (1-24) joined Ajmal as Pakistan's wicket-takers.
Pakistan will be aiming to set South Africa a target somewhere in the 300s when they resume their second innings on Sunday, with 334 the highest fourth-innings winning total made at Cape Town by Australia in 2002.