In my engine I load Cg shaders from pairs of vertex/pixel shader files. I would like to be able to stack shaders to combine them (lighting + material, etc.). Short of breaking up the shaders into separate functions and then creating a single shader script string from those, do you know of any good ways of stacking different shaders in Cg?
It sounds a little bit like what you want is similar to the dynamic shader linkage feature in DirectX 11? The interfaces feature of Cg lets you accomplish simlar things. It lets you reconfigure shaders so you can easily and cleanly do things like change the way lighting is calculated or change the material type.
For example, say you want to write a shader but need to be able to change the way lighting is calculated. You could define a Light interface which has a function that will calculate light somehow. Elsewhere you implement the Light interface as, for example, CoolLight and UberLight. At runtime you can specify which implementation of the Light interface should be used, in pretty much the same way that you specify other parameters.
To see if this is what you want I recommend looking the Cg user manual (http://developer.download.nvidia.com/cg/Cg_2.2/CgUsersManual.pdf). If you search for "Shared Parameters and Interfaces" there's a small example.