#include <ctype.h>
#include <descrip.h>
#include <errno.h>

int skipbl(lin,i)
char lin[];
int i;
{
	int j=i;
	while (lin[j] == ' ' || lin[j] == '\t' || lin[j] == '\n')
	   j++;
	return(j);
}

#define TOLOWER(c) (isupper(c) ? tolower(c) : c)
MM_strcasecmp(s1, s2)
	register char *s1, *s2;
{
    	char c1, c2;

    	for (c1 = *s1, c2 = *s2; TOLOWER(c1) == TOLOWER(c2); 
    	    	    	    	    	    	c1 = *++s1, c2 = *++s2) {
		if (*s1 == '\0') return(0);
    	}
	return(TOLOWER(*s1) - TOLOWER(*s2));
}
MM_strncasecmp(s1, s2, n)
	register char *s1, *s2;
    	register int n;
{
    	char c1, c2;

    	for (c1 = *s1, c2 = *s2; (--n >= 0) && (TOLOWER(c1) == TOLOWER(c2)); 
    	    	    	    	    	    	c1 = *++s1, c2 = *++s2) {
		if (*s1 == '\0') return(0);
    	}
	return((n < 0) ? 0 : (TOLOWER(*s1) - TOLOWER(*s2)));
}

int cli_present(char *obj) {

    struct dsc$descriptor objdsc;

    objdsc.dsc$b_dtype = DSC$K_DTYPE_T;
    objdsc.dsc$b_class = DSC$K_CLASS_S;
    objdsc.dsc$w_length = strlen(obj);
    objdsc.dsc$a_pointer = obj;
    return (cli$present(&objdsc) & 1);

}

int cli_get_value(char *obj, char *val, int valsize) {

    struct dsc$descriptor objdsc, valdsc;
    unsigned short len;
    unsigned int status;

    objdsc.dsc$b_dtype = DSC$K_DTYPE_T;
    objdsc.dsc$b_class = DSC$K_CLASS_S;
    objdsc.dsc$w_length = strlen(obj);
    objdsc.dsc$a_pointer = obj;
    valdsc.dsc$b_dtype = DSC$K_DTYPE_T;
    valdsc.dsc$b_class = DSC$K_CLASS_S;
    valdsc.dsc$w_length = valsize-1;
    valdsc.dsc$a_pointer = val;
    status = cli$get_value(&objdsc, &valdsc, &len);
    if (status & 1) *(val+len) = '\0';
    return (status & 1);

}

void cli_parse_command(char *cmd, void *table) {

    struct dsc$descriptor dsc, sdsc;
    static $DESCRIPTOR(blank, " ");

    sdsc.dsc$b_dtype = DSC$K_DTYPE_T;
    sdsc.dsc$b_class = DSC$K_CLASS_S;
    sdsc.dsc$w_length = strlen(cmd);
    sdsc.dsc$a_pointer = cmd;
    dsc.dsc$b_dtype = DSC$K_DTYPE_T;
    dsc.dsc$b_class = DSC$K_CLASS_D;
    dsc.dsc$w_length = 0;
    dsc.dsc$a_pointer = 0;
    lib$get_foreign(&dsc);
    str$prefix(&dsc, &blank);
    str$prefix(&dsc, &sdsc);
    cli$dcl_parse(&dsc, table);
    str$free1_dx(&dsc);

}

int imatch(buf, n, str)
char *buf, *str;
int n;
{
	register char *p=(&buf[n]), *s=str;

	for (;;) {
	   if (*s == '\0') return(1);
	   else if (*p == '\0') break;
	   else if (tolower(*p) != tolower(*s))
	      break;
	   p++; s++;
	}
	return(0);
}

char *MM_geterror0(void) {

    unsigned short len;
    static char buf[512];
    struct dsc$descriptor dsc;

    dsc.dsc$b_dtype = DSC$K_DTYPE_T;
    dsc.dsc$b_class = DSC$K_CLASS_S;
    dsc.dsc$w_length = sizeof(buf)-1;
    dsc.dsc$a_pointer = buf;
    if (!sys$getmsg(vaxc$errno, &len, &dsc, 0x0f, 0) & 1) len = 0;
    buf[len] = '\0';
    return buf;

}

void just_address(char *out, char *in) {

    unsigned int ctx;
    int type;
    char *adr, *fa, *nam;

    ctx = 0;
    parse822(in, &ctx, &fa, &adr, &nam, &type);
    strcpy(out, (adr == 0) ? in : adr);

}
