#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAX_EXCEPTION_LEN   40
#define E_OK                0
#define E_PARAM             0x8004
#define MAXLINEIN           80       


void rqcformatexception(char *, int, int, unsigned char, unsigned int *);

/* Return a pointer to an RMX exception string ********************************
*/
char *exception(unsigned int exception_code)
    {
    static       char udi_msg[MAX_EXCEPTION_LEN + 1];
    unsigned int status,
                 len;
    char         temp_buf[40];
    
    rqcformatexception( udi_msg, 
                        MAX_EXCEPTION_LEN,
                        exception_code,
                        1,                      /* reserved, value must ==1 */
                        &status);
                    
    if (status == E_PARAM)      
        {
        sprintf(temp_buf, "[exception unknown (%xH)]", exception_code);
        strncpy(udi_msg, temp_buf, MAX_EXCEPTION_LEN);      
        }
    else
    if (status != E_OK)
        {
        sprintf(temp_buf, "[exception() error (%xH)]", exception_code);
        strncpy(udi_msg, temp_buf, MAX_EXCEPTION_LEN);      
        }
    else
        {
        len = *udi_msg;
        memcpy(udi_msg, udi_msg + 1, len);
        udi_msg[len] = '\0';
        }
    
    return udi_msg;     /* return addr of string */
    }

main(int count, char **argv)
    {
    unsigned int value;
    char const   *p;
    
    if (count == 1)
        {
        printf("exception { {value}H | {value} }\n");
        return 1;
        }
    
    if (toupper(argv[1][strlen(argv[1]) - 1]) == 'H')
        p = "%x";
    else
        p = "%d";
                
    sscanf(argv[1], p, &value);
    
    printf("Exception %s\n", exception(value));
    
    return 0;
    }

        
