/*
 *               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.
 *
 *  Description:
 *      This file implements common utility functions.
 *
 */


#include <iostypes.h>
#include <ioslibc.h>

#include "apputils.h"


#define LINE_SIZE 256

int
parseCommand(char *line, Command *commands)
{
    char *cmd;
    int i;
    int cmdLen;
    int best = -1;

    getNextToken(&cmd, &line);
    cmdLen = strnlen(cmd, LINE_SIZE);
    if ( 0 == cmdLen)
        return 0;
    for (i = 0; commands[i].cmd; ++i) {
        if (!strncmp(cmd, commands[i].cmd, cmdLen)) {
            if (cmdLen == strnlen(commands[i].cmd, LINE_SIZE)) {
                best = i;
                break;
            }
            if (best != -1) {
                printf("Ambiguous command: '%s'\n", cmd);
                return -1;
            }
            best = i;
        }
    }
    if (best == -1) {
        printf("Unknown command: '%s'\n", cmd);
        return -1;
    }

    commands[best].proc(line, commands[best].arg);
    return 0;
}


int
getNextToken(char **next, char **rest)
{
    int valid = 0;
    char *p = *rest, *n;

    /* Strip leading white space */
    while (*p == ' ') {
        ++p;
    }
    *next = p;
    n = p;
    /* Find end of token */
    while (*n && *n != ' ') {
        valid = 1;
        ++n;
    }
    /* Strip trailing white space */
    while (*n == ' ') {
        *n++ = 0;
    }
    *rest = n;
    return valid;
}

int
help(char *args, char *p)
{
    int i;
    Command *commands = (Command *)p;
    char *cmd;

    if (getNextToken(&cmd, &args)) {
        int len = strnlen(cmd, LINE_SIZE);
        for (i = 0; commands[i].cmd; ++i) {
            if (!strncmp(cmd, commands[i].cmd, len)) {
                printf("    %s\n", commands[i].desc);
            }
        }
    } else {
        printf("Possible commands:\n");
        for (i = 0; commands[i].cmd; ++i) {
            printf("    %s\n", commands[i].desc);
        }
    }
    return 0;
}

u32
atou32(char *str)
{
    u32 v = 0;
    if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
        str += 2;
        while ((*str >= '0' && *str <= '9') ||
               (*str >= 'a' && *str <= 'f') ||
               (*str >= 'A' && *str <= 'F')) {
            char c = *str++;
            if (c <= '9') c -= '0';
            else if (c <= 'F') c -= 'A'-10;
            else c -= 'a'-10;
            v = (v << 4) + c;
        }
    } else {
        while (*str >= '0' && *str <= '9') {
            v = v*10 + *str++-'0';
        }
    }
    return v;
}

int
asciiToInt(char *str)
{
  return (int) atou32(str);
}

char *
stripPath(char *name, int size)
{
    int i = size;
    while (name[i] != '\\' && name[i] != '/' ) --i;
    return name + i + 1;
}

int
source(char *args, char *p)
{
    char *tok;
    FILE *fp = 0;
    char buf[256];
    Command *commands = (Command *)p;

    if (getNextToken(&tok, &args)) {
        fp = fopen(tok, "rb");
        if (fp == 0) {
            printf("fopen: unable to source file '%s'\n", tok);
            goto out;
        }
        while (1) {
            int i = 0;
            while ( i < sizeof(buf) - 1 ) {
                buf[i] = 0;
                fread(buf+i, 1, 1, fp);
                if (buf[i] == '\r') {
                    buf[i] = 0;
                    continue;
                }
                if (buf[i] == '\n' || buf[i] == '\0') {
                    buf[i] = 0;
                    break;
                }
                i++;
            }
            if (!*buf) break;
            printf("source: %s\n", buf);
            if (parseCommand(buf, commands) < 0) {
                printf("Error: %s\n", buf);
            }
        }
    }
out:
    if (fp) fclose(fp);
    return 0;
}
