Ab initio nonlinear optics in solids: linear electro-optic effect and electric-field induced second-harmonic generation