void send_file(void) { const char *file; FILE *f; char buf[BUFSIZ], *expanded; size_t len; file = get_input("Local file?"); if (file == NULL || *file == '\0') return; expanded = tilde_expand(file); f = fopen(expanded, "r"); if (f == NULL) { cu_warn("%s", file); return; } while (!feof(f) && !ferror(f)) { len = fread(buf, 1, sizeof(buf), f); if (len != 0) bufferevent_write(line_ev, buf, len); } fclose(f); free(expanded); }
void set_speed(void) { const char *s, *errstr; int speed; s = get_input("New speed?"); if (s == NULL || *s == '\0') return; speed = strtonum(s, 0, UINT_MAX, &errstr); if (errstr != NULL) { cu_warnx("speed is %s: %s", errstr, s); return; } if (set_line(speed) != 0) cu_warn("tcsetattr"); }
void xmodem_send(const char *file) { FILE *f; u_char buf[3 + XMODEM_BLOCK + 2], c; size_t len, pktlen; uint8_t num; uint16_t crc; int crc_mode; u_int i, total; struct termios tio; struct sigaction act, oact; f = fopen(file, "r"); if (f == NULL) { cu_warn("%s", file); return; } memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_flags = 0; act.sa_handler = xmodem_signal; if (sigaction(SIGINT, &act, &oact) != 0) cu_err(1, "sigaction"); xmodem_stop = 0; if (isatty(STDIN_FILENO)) { memcpy(&tio, &saved_tio, sizeof(tio)); tio.c_lflag &= ~ECHO; if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) != 0) cu_err(1, "tcsetattr"); } tcflush(line_fd, TCIFLUSH); if (xmodem_read(&c) != 0) goto fail; if (c == XMODEM_C) crc_mode = 1; else if (c == XMODEM_NAK) crc_mode = 0; else { cu_warnx("%s: unexpected response \%03hho", file, c); goto fail; } num = 1; total = 1; pktlen = 3 + XMODEM_BLOCK + (crc_mode ? 2 : 1); for (;;) { len = fread(buf + 3, 1, XMODEM_BLOCK, f); if (len == 0) break; memset(buf + 3 + len, XMODEM_SUB, XMODEM_BLOCK - len); buf[0] = XMODEM_SOH; buf[1] = num; buf[2] = 255 - num; if (crc_mode) { crc = xmodem_crc16(buf + 3, XMODEM_BLOCK); buf[3 + XMODEM_BLOCK] = crc >> 8; buf[3 + XMODEM_BLOCK + 1] = crc & 0xFF; } else { buf[3 + XMODEM_BLOCK] = 0; for (i = 0; i < XMODEM_BLOCK; i++) buf[3 + XMODEM_BLOCK] += buf[3 + i]; } for (i = 0; i < XMODEM_RETRIES; i++) { if (xmodem_stop) { errno = EINTR; goto fail; } cu_warnx("%s: sending block %u (attempt %u)", file, total, 1 + i); if (xmodem_write(buf, pktlen) != 0) goto fail; if (xmodem_read(&c) != 0) goto fail; if (c == XMODEM_ACK) break; if (c != XMODEM_NAK) { cu_warnx("%s: unexpected response \%03hho", file, c); } } if (i == XMODEM_RETRIES) { cu_warnx("%s: too many retries", file); goto out; } if (len < XMODEM_BLOCK) break; num++; total++; }