diff --git a/src/decode.c b/src/decode.c index dda3722..c759888 100644 --- a/src/decode.c +++ b/src/decode.c @@ -103,6 +103,37 @@ static int address_fprint(FILE *stream, const patty_ax25_frame *frame) { return 0; } +static int frame_fprint(FILE *stream, + const patty_ax25_frame *frame, + void *data, + size_t len) { + if (address_fprint(stream, frame) < 0) { + goto error_io; + } + + if (frame->type == PATTY_AX25_FRAME_INFO) { + if (fprintf(stream, " poll %d ns %d nr %d", + PATTY_AX25_CONTROL_POLL(frame->control), + PATTY_AX25_CONTROL_SEQ_SEND(frame->control), + PATTY_AX25_CONTROL_SEQ_RECV(frame->control)) < 0) { + goto error_io; + } + } + + if (fprintf(stream, " proto %02x %ld bytes\n", frame->proto, len) < 0) { + goto error_io; + } + + if (hexdump_fprint(stream, data, len) < 0) { + goto error_io; + } + + return 0; + +error_io: + return -1; +} + int main(int argc, char **argv) { patty_kiss_tnc *tnc; ssize_t len; @@ -116,7 +147,7 @@ int main(int argc, char **argv) { if ((tnc = patty_kiss_tnc_open(argv[1], 330)) == NULL) { perror("Unable to open TNC"); - exit(127); + goto error_kiss_tnc_open; } while ((len = patty_kiss_tnc_recv(tnc, &data, &port)) > 0) { @@ -125,24 +156,24 @@ int main(int argc, char **argv) { if (patty_ax25_frame_decode(&frame, data, len) < 0) { perror("Unable to decode frame"); - exit(127); + goto error_ax25_frame_decode; } - address_fprint(stderr, &frame); + if (frame_fprint(stderr, &frame, data, len) < 0) { + perror("Unable to print frame"); - if (frame.type == PATTY_AX25_FRAME_INFO) { - fprintf(stderr, " poll %d", PATTY_AX25_CONTROL_POLL(frame.control)); - fprintf(stderr, " ns %d", PATTY_AX25_CONTROL_SEQ_SEND(frame.control)); - fprintf(stderr, " nr %d", PATTY_AX25_CONTROL_SEQ_RECV(frame.control)); + goto error_frame_fprint; } - - fprintf(stderr, " proto %02x", frame.proto); - fprintf(stderr, " %ld bytes\n", len); - - hexdump_fprint(stderr, data, len); } patty_kiss_tnc_close(tnc); return 0; + +error_frame_fprint: +error_ax25_frame_decode: + patty_kiss_tnc_close(tnc); + +error_kiss_tnc_open: + return 127; }