/* 
   The JitterBug bug tracking system
   HTL helper functions

   Copyright (C) Andrew Tridgell 1999

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "jitterbug.h"

/* show a selection list, including a blank element */
void html_select_list(char **list, char *vname, char *def)
{
	int i;

	printf("<select name=%s>\n", vname);
        printf("<option %s value=\"\">\n", (def && *def)?"":"SELECTED");
	for (i=0;list && list[i];i++) {
		char *s = quotequotes(list[i]);
		printf("<option %s value=\"%s\">%s\n", 
		       (def && strcmp(def, list[i])==0) ? "SELECTED":"",
		       s, s);
		free(s);
	}
	printf("</select>\n");
}

void html_input_submit(char *name, char *label)
{
	printf("<input type=submit name=\"%s\" value=\"%s\">", name, label);
}


void html_input_text(char *name, int width)
{
	printf("<input type=text size=%d name=\"%s\">", width, name);
}

void html_form_start(char *action)
{
	printf("<form method=POST action=\"%s\">", action);
}


void html_form_end(void)
{
	printf("</form>\n");
}

void html_input_hidden(char *name, char *value)
{
	printf("<input type=hidden name=\"%s\" value=\"%s\">", name, value);
}


void html_doctype(void)
{
	printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n");
}

/* not strictly just for html, but a very useful hack for constructing
   strings without allocating them */
char *xsprintf(char *format, ...)
{
	static char buf[1024];
	va_list ap;  
	int ret;

	va_start(ap, format);
	ret = vslprintf(buf,sizeof(buf),format,ap);
	va_end(ap);
	return buf;
}

/* return an integer as a string */
char *xistr(int i)
{
	return xsprintf("%d", i);
}

