/* "Copyleft" Chris Baird */ #include /* write(2) */ #include /* write(2) */ #include #include /* open(2) */ #include /* atof(3) */ #include /* usleep(3), getopt(3), write(2) */ #include /* err(3) */ #include /* strncpy(3) */ #include /* line speed setting */ extern char *optarg; extern int optind; /* default line speed */ int output_baud = 300; /* default device to use */ #define DEFAULT_LINE "/dev/tty00" /* the delay after a newline (in seconds); slow software will need more time */ float newline_delay = 0.75; /* the delay for each character, relative to line speed; if talking to a BASIC * program that reads characters individually make it at least 2 (at 300 bps) */ float character_fudge = 2.0; int character_echo = 1; /* ------------------------------------------------------------------------- */ char output_device[32]; int usage (void) { fprintf (stderr, "Usage: u2a\n\t[-l output device (%s)]\n" "\t[-e disable character echo\n" "\t[-b line speed (%d)]\n" "\t[-n newline delay in seconds (%.2f)]\n" "\t[-c relative per character delay (%.2f)]\n", output_device, output_baud, newline_delay, character_fudge); exit (1); } int main(int argc, char *argv[]) { FILE *fp; struct termios origterm; int output_fd, c; strncpy (output_device, DEFAULT_LINE, 11); while ((c = getopt(argc, argv, "hl:b:n:c:e")) != -1) switch(c) { case 'e': character_echo = 0; break; case 'h': usage (); break; case 'l': strncpy (output_device, optarg, 31); /* sanity check for existing device/file later */ break; case 'b': output_baud = atoi(optarg); if ((output_baud < 50) || (output_baud >500000)) err (1, "Unacceptable line speed \"%d\"\n", output_baud); break; case 'n': newline_delay = atof(optarg); if ((newline_delay < 0.0) || (newline_delay > 100.0)) err (1, "Unacceptable newline delay \"%.2f\"\n", newline_delay); break; case 'c': character_fudge = atof(optarg); if ((character_fudge < 1.0) || (character_fudge > 100.0)) err (1, "Unacceptable character delay \"%.2f\"\n", character_fudge); break; } argc -= optind; argv += optind; if (argv[0] == NULL) usage(); fp = fopen (argv[0], "r"); if (fp == NULL) err (1, "could not open \"%s\"\n", argv[argc]); /* open outputline */ output_fd = open (output_device, O_WRONLY|O_EXLOCK, NULL); if (output_fd == -1) err (1, "Unable to open \"%s\"", output_device); if (tcgetattr (output_fd, &origterm) < 0) warn ("unable retrieve line settings"); origterm.c_cflag &= ~(CSIZE|CRTSCTS|MDMBUF); origterm.c_cflag |= CS8|PARENB|PARODD|HUPCL|CLOCAL; cfsetospeed (&origterm, output_baud); if (tcsetattr (output_fd, TCSADRAIN, &origterm) < 0) warn ("unable to configure line settings"); while ((c = fgetc(fp)) != EOF) { if (character_echo) write (2, &c, 1); if (c == 10) c = 13; write (output_fd, &c, 1); if (c == 13) { sleep ((int)newline_delay); usleep (1000000 * (float)(newline_delay - (int)newline_delay)); } else usleep (((long)10000000 * (long)character_fudge) / (float)output_baud); } fclose (fp); close (output_fd); fprintf (stderr, "%c*** TRANSFER COMPLETE ***%c\n", 7, 7); return 0; }