
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "defines.h"
#include "cus.h"


/*	Unqoute a string buffer if quoted by skipping the first char
	and replacing the last char by the null terminater.			*/

char *strcvt(char *str)
{
	char *ptr = str;
	int found = NO;

	if (*ptr == QUOTE) {
		while (*ptr != NUL) {
			if (*ptr != QUOTE)
				found = YES;
			ptr++;
		}
		if (found) {
			*(--ptr) = NUL;
			return (++str);
		}
		else return str;
	}
	else
		return str;
}


/*  Truncate a domain extension if exists in a given ARPA address, so
    that the name of the host will be remain only.					*/

void truncdomain(char *str)
{
	while( (*str != '.') && (*str != NUL))
		str++;
	if (*str == '.')
		*str = NUL;
	return;
}


/*	Split an argument string into an argument array with pointers to
	null-terminated argument strings like argv[].		*/

void splitarg(char **cppArgv, char *cpCmnd)
{
	char *cp;

	cp = cpCmnd;

	for ( ; ; ) {
		*cppArgv++ = cp;
		while ( (*cp != SP) && (*cp != HT) && (*cp != NL) && (*cp != NUL) )
			cp++;
		if (*cp == NUL)
			break;
		*cp++ = NUL;
		while ( (*cp == SP) || (*cp == HT) )
			cp++;
	}
	*cppArgv = NULL;

	return;
}


int cnvbuf(char *cpBufFirst, char *cpBufLast)
{
	char *cpRead;
	char *cpWrite;
	int rc;

	cpRead = cpWrite = cpBufFirst;

	for ( ; ; ) {
		if (cpRead > cpBufLast)
			CU(cpWrite - cpBufFirst);
		if (cpRead < cpBufLast) {
			if (*cpRead == '\\' && *(cpRead+1) == LF) {
				cpRead += 2;
				continue;
			}
		}
		*cpWrite++ = *cpRead++;
	}

	CUS:
	return rc;
}

