#include #include #include #include #include #include #include void sprog_process(const struct sprog_family *fam, void *arg, int nstdin, int nstdout); void sprog_error(const char *text, ...) { va_list l; va_start(l, text); vfprintf(stderr, text, l); va_end(l); } void sprog_sleep(int msec) { usleep(msec * 1000); } void sprog_communicate(const struct sprog_family *fam, const char *port, int baud) { int pstdin[2]; int pstdout[2]; int pid; void *arg; struct serial_device dev; serial_open(&dev, port, baud); arg = fam->setup(&dev); pipe(pstdin); pipe(pstdout); pid = fork(); if(pid<0) sprog_error("Unable to fork: %s\n", strerror(errno)); else { if(pid==0) { serial_communicate(&dev, pstdout[0], pstdin[1]); fam->close(arg); } else sprog_process(fam, arg, pstdin[0], pstdout[1]); } } void sprog_process(const struct sprog_family *fam, void *arg, int nstdin, int nstdout) { dup2(nstdin, 0); dup2(nstdout, 1); fam->init(arg); exit(0); }