view rvinterf/lowlevel/format.c @ 924:d452188587b4

rvinterf: begin change to backslash escape output format Right now throughout the rvinterf suite, any time we emit output that is expected to be ASCII, but may contain non-printable garbage, we use 'cat -v' form of garbage character representation. Unfortunately, this transformation is lossy (can't be reversed 100% reliably in the user's wetware), hence we would like to migrate to C-style backslash escapes, including doubling of any already-present backslashes - this escape mechanism is lossless. Begin this change by converting the output of RV and L1 traces in rvinterf and rvtdump.
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 23 May 2023 03:10:50 +0000
parents e7502631a0f9
children 85d144f9fe56
line wrap: on
line source

/*
 * This module implements the decoding of Rx packets
 * into human-readable form.
 */

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "../include/pktmux.h"
#include "../include/limits.h"

extern u_char rxpkt[];
extern size_t rxpkt_len;

static char fmtbuf[MAX_PKT_FROM_TARGET*8];	/* size it generously */

void
print_rv_trace()
{
	int i, c;
	char *dp;

	dp = fmtbuf;
	strcpy(dp, "RV ");
	dp += 3;
	/* the SWE static ID is sent MSB first */
	for (i = 1; i <= 4; i++) {
		sprintf(dp, "%02X", rxpkt[i]);
		dp += 2;
	}
	/* severity level */
	sprintf(dp, " %d ", rxpkt[5]);
	dp = index(dp, '\0');
	for (i = 6; i < rxpkt_len; i++) {
		c = rxpkt[i];
		switch (c) {
		case '\\':
			*dp++ = '\\';
			*dp++ = '\\';
			continue;
		case '\r':
			*dp++ = '\\';
			*dp++ = 'r';
			continue;
		case '\n':
			*dp++ = '\\';
			*dp++ = 'n';
			continue;
		}
		if (c >= ' ' && c <= '~')
			*dp++ = c;
		else if (c <= 7 && (i+1 == rxpkt_len || !isdigit(rxpkt[i+1]))) {
			sprintf(dp, "\\%d", c);
			dp += 2;
		} else {
			sprintf(dp, "\\x%02X", c);
			dp += 4;
		}
	}
	*dp = '\0';
	output_line(fmtbuf);
}

void
print_l1_trace()
{
	int i, c;
	char *dp;

	dp = fmtbuf;
	strcpy(dp, "L1: ");
	dp += 4;
	for (i = 1; i < rxpkt_len; i++) {
		if ((i+1 < rxpkt_len) &&
		    (rxpkt[i] == '\r' && rxpkt[i+1] == '\n' ||
		     rxpkt[i] == '\n' && rxpkt[i+1] == '\r')) {
			*dp = '\0';
			output_line(fmtbuf);
			if (i+2 == rxpkt_len)
				return;
			dp = fmtbuf;
			*dp++ = '+';
			*dp++ = ' ';
			i++;
			continue;
		}
		c = rxpkt[i];
		switch (c) {
		case '\\':
			*dp++ = '\\';
			*dp++ = '\\';
			continue;
		case '\r':
			*dp++ = '\\';
			*dp++ = 'r';
			continue;
		case '\n':
			*dp++ = '\\';
			*dp++ = 'n';
			continue;
		}
		if (c >= ' ' && c <= '~')
			*dp++ = c;
		else if (c <= 7 && (i+1 == rxpkt_len || !isdigit(rxpkt[i+1]))) {
			sprintf(dp, "\\%d", c);
			dp += 2;
		} else {
			sprintf(dp, "\\x%02X", c);
			dp += 4;
		}
	}
	/* will get here only if no newline sequence at the end */
	*dp = '\0';
	output_line(fmtbuf);
}

void
print_g23_trace()
{
	/* messy logic factored out into libg23 */
	format_g23_packet(rxpkt, (int)rxpkt_len, fmtbuf);
	output_line(fmtbuf);
}

void
print_tm_output_raw()
{
	int i;
	char *dp;

	dp = fmtbuf;
	strcpy(dp, "TM:");
	dp += 3;
	for (i = 1; i < rxpkt_len; i++) {
		sprintf(dp, " %02X", rxpkt[i]);
		dp += 3;
	}
	*dp = '\0';
	output_line(fmtbuf);
}

void
print_unknown_packet()
{
	int i;
	char *dp;

	dp = fmtbuf;
	strcpy(dp, "UNK:");
	dp += 4;
	for (i = 0; i < rxpkt_len; i++) {
		sprintf(dp, " %02X", rxpkt[i]);
		dp += 3;
	}
	*dp = '\0';
	output_line(fmtbuf);
}