/*
 *	Copyright (c) 1995, 1996, Sun Microsystems, Inc.
 *
 * This software is provided by Sun ``AS IS'' and any express or implied
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose are disclaimed.
 * In no event shall Sun Microsystems be liable for any direct, indirect,
 * incidental, special, exemplary, or consequential damages.
 *
 * This software is not a product, and is provided for evaluation purposes
 * only.
 *
 * This software may not be resold without the express permission of
 * Sun Microsystems.
 *
 * Feedback on this tool may be sent to packet-shell-owner@sunroof.eng.sun.com.
 *
 * Packet Shell
 *
 */

#pragma ident	"@(#)data.c	1.26	99/05/28 SMI"

#include <tcl.h>
#include "psh.h"
#include "proto.h"
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

static char *proto_name;

int
init(proto_tbl_t *prototbl, Tcl_Interp *interp)
{
	if ((proto_name = strdup(prototbl->proto_name)) == NULL) {
		Tcl_AppendResult(interp, "Internal Error: malloc: no space",
		    NULL);
		return (0);
	}
	return (1);
}

/*
 * Create a "default" packet.
 */
int
pinit(ClientData clientData, Tcl_Interp *interp,
    struct packet *pckt, int argc, char *argv[])
{
	Tcl_ResetResult(interp);
	if (pckt->p_len == 0)
		pckt->p_len = pckt->p_maxlen;
	if (ptbl_update(pckt, 1, NULL, pckt->p_len) == 0) {
		return (TCL_ERROR);
	}
	return (TCL_OK);
}


int
decode(struct packet *pkt, int element, char **name, int *next_offset)
{
	*name = "end";
	*next_offset = pkt->p_len;
	return (0);
}

int
list(int element, ClientData clientData, Tcl_Interp *interp,
    struct packet *pckt, int argc, char *argv[])
{
	char tmp[128];
	char *p;

	/*
	 * Print it all
	 */
	for (p = pckt->p_buf + pckt->p_tbl[element].pt_offset;
	    p < pckt->p_buf + pckt->p_len; p++) {
		(void) sprintf(tmp, "%.2x ", *p & 0xff);
		Tcl_AppendResult(interp, tmp, NULL);
		if ((((int)p) & 0xf) == 0xf) {
			Tcl_AppendResult(interp, "\n", NULL);
		}
	}
	return (TCL_OK);
}
