/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 * $Id: printascii.c,v 1.1 2008/09/04 18:34:46 cnguyen Exp $
 */

#include <stdlib.h>
#include <iostypes.h>
#include <ios.h>
#include <ioslibc.h>
#include <apputils.h>
#include <elf.h>
#include <iostypes.h>
#include <iop.h>


static char data[10*1024];
static char str[128];
static char line[80];


/*
 * This routine simply prints out the ascii content at the specified memory
 * location.
 */
int
printAscii (char *args, char *d)
{
    s32 rc = 0;
    u32 address;
    char *ptr;

    if (getNextToken(&ptr, &args) == 0) {
        printf ("Usage: print address\n");
        goto out;
    }
    address = asciiToInt(ptr);

    snprintf(str, sizeof(str), "%s", (char *)address);
    str[sizeof(str)-1] = '\0';
    printf ("0x%08X: %s\n", address, str);

out:

    return rc;
}


/*
 * This routine reads a list of memory addresses from the input text file
 * and prints out ascii contents at these addresses
 */
int
fprintAscii (char *args, char *d)
{
    s32  rc = 0;
    char *name;
    FILE *fp = 0;
    char *dst = line;
    char *src = data;
    s32  d1;
    
    u32  i, l, addr, phys_addr, len;

    if (getNextToken(&name, &args) == 0) {
        printf ("Usage: fprint file\n");
        goto out;
    }

    if ((name == NULL) || ((fp = fopen(name, "r")) == 0)) {
        printf ("fopen: unable to open file '%s'\n", name);
        goto out;
    }
    len = flen(fp);
    printf("file size = %d\n", len);
    if (fread(data, len, 1, fp) != 1) {
        printf ("fread: unable to read %d bytes\n", len);
        goto out;
    }

    i = l = 0;
    src = data;
    while (i < len) {
        /* Read one line */
        d1 = 0;
        memset(line, 0, sizeof(line));
        dst = line;
        // while (*src && (*src !='\n')) {
        while ((*src !='\n')) {
            *dst++ = *src++;
            i++;
        }
        *dst = 0;
        i++; src++;  /* Skip newline */
        l++;
        // printf("line %d: %s\n", l, line);
        
        addr = asciiToInt(line);
        phys_addr = addr & 0x7FFFFFFF;
       
        snprintf(str, sizeof(str), "%s", (char *)phys_addr);
        str[sizeof(str)-1] = '\0';
        printf ("0x%08X: %s\n", addr, str);
    }

out:
    if (fp) fclose(fp);
    return rc;
}

/* eof */
