South Africa pulled themselves back into contention on day two of their Test against Sri Lanka on Sunday.