/*****
/*	TITLE:			QUOTE -- String Symbol Quotation Utility
/*	VERSION:		1.1
/*	SYSTEM:			VAX
/*	SUBSYSTEM:		System Utilities
/*	SOURCE FILE:		QUOTE.C
/*	DCL INTERFACE:		QUO*TE == "$SYS$GSDEXE:QUOTE"
/*	AUTHOR:			Wayne E. Baisley
/*	DATE:			February 20, 1985
/*
/*	REVISIONS      DATE	PROGRAMMER		DESCRIPTION
/*	---------    --------	--------------	-------------------------------
/*
/*	   0)        02/20/85	W. E. Baisley	Initial Release
/*
/*	   1)        07/19/88	W. E. Baisley	Use new header files, very minor
/*						clean-up.
/*  */

/* Include system headers */

#include	ctype
#include	descrip
#include	rmsdef
#include	ssdef
#include	stdio
#include	string

static char version[] = "1.1";					/* This program's version number */

#define FOREVER		for (;;)
#define MAX_STR_LEN	(255)					/* Maximum string length of a symbol */
#define MAX_SYM_LEN	(255)					/* Maximum length of a symbol name */
#define PROMPT		"$_Symbol: "				/* Prompt string for obtaining a symbol name */
#define SS$_MAXPARM	(0X38098)				/* %CLI-F-MAXPARM, Too many parameters */

char *strupper (char *);

main (ac, av)

int ac;
char **av;

{

	register int status;					/* status value */
	long table;						/* symbol table index */
	register char *icp;					/* input  character pointer for string manipulation */
	register char *ocp;					/* output character pointer for string manipulation */
	int i;							/* for counting with */
	static char orignl_string[MAX_STR_LEN+1] = "";		/* string symbol's original contents */
	static char quoted_string[MAX_STR_LEN+1] = "";		/* symbol's contents after processing */
	static char getname_string[MAX_SYM_LEN+1] = "";		/* symbol name string for getting the string */
	static char setname_string[MAX_SYM_LEN+1] = "";		/* symbol name string for redefining the symbol */
	static char prompt_string[] = PROMPT;			/* prompt for getting a symbol name */
	static $DESCRIPTOR (orignl_desc, orignl_string);	/* descriptor for original symbol string */
	static $DESCRIPTOR (quoted_desc, quoted_string);	/* descriptor for processed string */
	static $DESCRIPTOR (getname_desc, getname_string);	/* get symbol name string descriptor */
	static $DESCRIPTOR (setname_desc, setname_string);	/* redefine symbol name string descriptor */
	static $DESCRIPTOR (prompt_desc, prompt_string);	/* descriptor for the prompt */

	FOREVER
	{

		if (--ac == 0)	{

			status = lib$get_command (&getname_desc, 		/* descriptor of buffer to receive input */
						  &prompt_desc, 		/* descriptor of the prompt string */
						  &getname_desc.dsc$w_length);	/* address of word to get input length */

			if (status != SS$_NORMAL)	{

				if ((status == RMS$_CONTROLC) ||
				    (status == RMS$_CONTROLY) ||
				    (status == RMS$_EOF))
					status = SS$_NORMAL;

				break;

			}

			if (getname_desc.dsc$w_length == 0)	{

				status = SS$_INSFARG;
				break;

			}

			else	{

				getname_string[getname_desc.dsc$w_length] = '\0';
				i = strspn (getname_string, "\t ");
				getname_desc.dsc$w_length -= i;
				strcpy (getname_string, &getname_string[i]);
				getname_desc.dsc$w_length = strcspn (getname_string, "\t ");
				getname_string[getname_desc.dsc$w_length] = '\0';

				if (strcspn (&getname_string[getname_desc.dsc$w_length+1], "\t ") != 0)	{

					status = SS$_MAXPARM;
					break;

				}

			}

		}

		else if (ac == 1)	{

			++av;
			strncpy (getname_string, *av, MAX_SYM_LEN+1);
			getname_desc.dsc$w_length = strlen (getname_string);

		}

		else	{

			status = SS$_MAXPARM;
			break;

		}

		strupper (getname_string);
		strcpy (setname_string, getname_string);
		setname_desc.dsc$w_length = getname_desc.dsc$w_length;

		if ((icp = strchr (getname_string, '*')) != NULL)	{

			strcpy (icp, icp+1);
			--getname_desc.dsc$w_length;

		}

		status = lib$get_symbol (&getname_desc, 			/* symbol name descriptor */
					 &orignl_desc, 				/* symbol string descriptor */
					 &orignl_desc.dsc$w_length,		/* address of word to get string length */
					 &table);				/* address of word to get table index */

		if (status != SS$_NORMAL)
			break;

		orignl_string[orignl_desc.dsc$w_length] = '\0';

		ocp = quoted_string;
		*ocp++ = '"';
		quoted_desc.dsc$w_length = 1;

		for (icp = orignl_string; *icp; ++icp)	{

			if ((*ocp++ = *icp) == '"')	{

				*ocp++ = '"';
				++quoted_desc.dsc$w_length;

			}

			if (++quoted_desc.dsc$w_length >= MAX_STR_LEN)
				break;

		}

		*ocp++ = '"';
		if (++quoted_desc.dsc$w_length >= MAX_STR_LEN)	{

			status = SS$_RESULTOVF;
			break;

		}

		status = lib$set_symbol (&setname_desc, 			/* symbol name descriptor */
					 &quoted_desc,				/* processed symbol string descriptor */
					 &table);				/* address of table index word */

		break;

	}
	
	exit (status);

}

/*****
/*      STRUPPER - Convert String To Upper Case
/*  */
	
char *strupper (s)
	
char *s;

{

	register char *cp = s;
	register char c;

	while (c = *cp)	{

		if (islower(c))
			*cp++ = c & 0137;
		else
			cp++;

	}

	return (s);
	
}
