/*
  cgicvt.c
  reads QUERY_STRING environment variable and breaks up
  string with special character translations
*/


#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
	char *qs;
	int quote = 0;

	if (!(qs = getenv("QUERY_STRING")))
		return(0);

	while (*qs)
	{
		switch (*qs)
		{
			default:
				putchar(*qs++);
				break;

			case '%':
				if (*++qs == '%')
					putchar(*qs++);
				else
				{
					int c;
					char buf[4];

					buf[0] = qs[0];
					buf[1] = qs[1];
					buf[2] = 0;
					qs += 2;
					c = strtol(buf, NULL, 16);

					if (c != '\r')
					{
						if (c == '\'')
							printf("'\"'\"");

						putchar(c);
					}
				}

				break;

			case '+':
				qs++;
				putchar(' ');
				break;

			case '&':
				qs++;
				putchar('\'');
				putchar('\n');
				quote = 0;
				break;

			case '=':
				qs++;
				putchar('=');
				putchar('\'');
				quote = 1;
				break;
		}
	}

	if (quote)
		putchar('\'');

	putchar('\n');
	return(0);
}
