In all cases where the imprisonment is in a criminal, or supposed criminal matter, if it shall appear to the court or judge that there is sufficient legal cause for the commitment of the applicant, although such commitment may have been informally made or without due authority, or the process may have been executed by a person not authorized, the court or judge shall make a new commitment in proper form, directed to the proper officer, or shall admit the applicant to bail, if the case be bailable, as upon a preliminary examination.
SDCL 21-27-17