/*
 *      s t r c a d d _ c
 *
 *  Unix compatible routine to copy a string compressing
 *  the C language escape sequences to the equivalent
 *  character.  A NULL byte is appended to the end.  The
 *  target area must be large enough to hold the result,
 *  but an area the same size as the source is guarteed
 *  to be large enough.
 *
 *  Returns a pointer to the NULL byte terminating the target.
 *
 *  AMENDMENT HISTORY
 *  ~~~~~~~~~~~~~~~~~
 *  29 Aug 94   DJW   - First version
 */

#define __LIBRARY__

#include <libgen.h>
#include <string.h>
#include <qdos.h>

char *  strcadd (char * output, const char * input)
{
    int    i, j, k, c;

    do {
        c = (unsigned char) *input++ ;
        if (c != '\\') {
            *output++ = (char)c;
            continue;
        }
        c = (unsigned char) *input++;
        /*
         *  Allow for octal sequences
         */
        if (strpos((char *)_C_oct_a,c) >= 0) {
            for (i=0,j=0; (j < 3) && i<32 && (k=strpos((char *)_C_oct_a,c)) >= 0; j++) {
                i = 8*i + _C_oct[k];
                c = (unsigned char) *input++;
            }
            input--;
            *output++ = (char)i;
            continue;
        }
        /*
         *  allow for hexadecimal sequences
         */
        if (c == 'x') {
            c = (unsigned char) *input++;
            for (i=0,j=0; (j < 2) && i<16 && (k=strpos((char *)_C_hex_a,c)) >= 0; j++) {
                i = 16*i + _C_hex[k];
                c = (unsigned char) *input++;
            }
            input--;
            *output++ = (char)i;
            continue;
        }
        if ((i=strpos((char *)_C_esc_a,c)) >= 0) {
            *output++ = _C_esc[i];
            continue;
        }
        *output++ = (char)c;

    } while (*input);

    *output = '\0';
    return (output);
}


