CTRACE(tfp, "HTLoadCSO: Looking for %s\n", arg);
CTRACE((tfp, "HTLoadCSO: Looking for %s\n", arg));
PRIVATE int HTLoadCSO ARGS4(	CONST char *,		arg,	HTParentAnchor *,	anAnchor,	HTFormat,		format_out,	HTStream*,		sink){    static CONST char end_form[] = "</BODY>\n</HTML>\n";    char *host, *cp;    int port = CSO_PORT;    int status;				/* tcp return */    char *command = NULL;    char *content = NULL;    int len, i, j, start, finish, flen, ndx;    int return_type, has_indexed;    CSOfield_info *fld;    char buf[2048];    HTFormat format_in = WWW_HTML;    HTStream *Target = NULL;    if (!acceptable_inited)	 init_acceptable();    if (!arg)	return -3;		/* Bad if no name sepcified	*/    if (!*arg)	return -2;		/* Bad if name had zero length	*/    CTRACE(tfp, "HTLoadCSO: Looking for %s\n", arg);    /*    **	Set up a socket to the server for the data.    */    status = HTDoConnect (arg, "cso", CSO_PORT, &s);    if (status == HT_INTERRUPTED) {	/*	**  Interrupt cleanly.	*/	CTRACE(tfp, "HTLoadCSO: Interrupted on connect; recovering cleanly.\n");	_HTProgress (CONNECTION_INTERRUPTED);	return HT_NOT_LOADED;    }    if (status < 0) {	CTRACE(tfp, "HTLoadCSO: Unable to connect to remote host for `%s'.\n",		    arg);	return HTInetStatus("connect");    }    HTInitInput(s);		/* Set up input buffering */    HTSprintf0(&command, "fields%c%c", CR, LF);    CTRACE(tfp, "HTLoadCSO: Connected, writing command `%s' to socket %d\n",		command, s);    _HTProgress (GOPHER_SENDING_CSO_REQUEST);    status = NETWRITE(s, command, (int)strlen(command));    FREE(command);    if (status < 0) {	CTRACE(tfp, "HTLoadCSO: Unable to send command.\n");	return HTInetStatus("send");    }    _HTProgress (GOPHER_SENT_CSO_REQUEST);    /*    **	Now read the data from the socket.    */    status = parse_cso_fields(buf, sizeof(buf));    if (status) {	NETCLOSE(s);	if (status == HT_INTERRUPTED) {	    _HTProgress (CONNECTION_INTERRUPTED);	} else if (buf[0] != '\0') {	    HTAlert(buf);	} else {	    HTAlert(FAILED_NO_RESPONSE);	}	return HT_NOT_LOADED;    }    Target = HTStreamStack(format_in,			   format_out,			   sink, anAnchor);    if (!Target || Target == NULL) {	char *temp = 0;	HTSprintf0(&temp, CANNOT_CONVERT_I_TO_O,		   HTAtom_name(format_in), HTAtom_name(format_out));	HTAlert(temp);	FREE(temp);	NETCLOSE(s);	return HT_NOT_LOADED;    }    host = HTParse(arg, "", PARSE_HOST);    if ((cp=strchr(host, ':')) != NULL) {	if (cp[1] >= '0' && cp[1] <= '9') {	    port = atoi((cp+1));	    if (port == CSO_PORT) {		*cp = '\0';	    }	}    }    anAnchor->safe = TRUE;    if (!(anAnchor->post_data && *anAnchor->post_data)) {	generate_cso_form(host, port, buf, Target);	(*Target->isa->_free)(Target);	FREE(host);	NETCLOSE(s);	free_CSOfields();	return HT_LOADED;    }    HTSprintf0(&command,     "<HTML>\n<HEAD>\n<TITLE>CSO/PH Results on %s</TITLE>\n</HEAD>\n<BODY>\n",	    host);    (*Target->isa->put_block)(Target, command, strlen(command));    FREE(command);    FREE(host);    StrAllocCopy(content, anAnchor->post_data);    if (content[strlen(content)-1] != '&')	StrAllocCat(content, "&");    len = strlen(content);    for (i = 0; i < len; i++) {	if (content[i] == '+') {	    content[i] = ' ';	}    }    HTUnEscape(content);    len = strlen(content);    return_type = 0;    has_indexed = 0;    start = finish = 0;    for (i = 0; i < len; i++) {	if (!content[i] || content[i] == '&') {	    /*	    **	Value parsed.  Unescape characters and look for first '='	    **	to delimit field name from value.	    */	    flen = i - start;	    finish = start + flen;	    content[finish] = '\0';	    for (j = start; j < finish; j++) {		if (content[j] == '=') {		    /*		    **	content[start..j-1] is field name,		    **	[j+1..finish-1] is value.		    */		    if ((content[start+1] == '_') &&			((content[start] == 'r') || (content[start] == 'q'))) {			/*			**  Decode fields number and lookup field info.			*/			sscanf (&content[start+2], "%d=", &ndx);			for (fld = CSOfields; fld; fld = fld->next) {			    if (ndx==fld->id) {				if ((j+1) >= finish)				    break;	/* ignore nulls */				if (content[start] == 'q') {				    /*				     * Append field to query line.				     */				    if (fld->lookup) {					if (fld->indexed)					    has_indexed = 1;					if (command == 0 || *command == 0) {					    StrAllocCopy(command, "query ");					} else {					    StrAllocCat(command, " ");					}					HTSprintf(&command, "%s=\"%s\"",						  fld->name, &content[j+1]);				    } else {					strcpy(buf,				"Warning: non-lookup field ignored<BR>\n");					(*Target->isa->put_block)(Target,								  buf,								  strlen(buf));				    }				} else if (content[start] == 'r') {				    fld->explicit_return = 1;				}				break;			    }			}		    } else if (!strncmp(&content[start],"return=",7)) {			if (!strcmp(&content[start+7],"all")) {			    return_type = 1;			} else if (!strcmp(&content[start+7],"selected")) {			    return_type = 2;			}		    }		}	    }	    start = i + 1;	}    }    FREE(content);    if ((command == 0 || *command == 0) || !has_indexed) {	NETCLOSE(s);	strcpy(buf,  "<EM>Error:</EM> At least one indexed field value must be specified!\n");	(*Target->isa->put_block)(Target, buf, strlen(buf));	strcpy(buf, "</BODY>\n</HTML>\n");	(*Target->isa->put_block)(Target, buf, strlen(buf));	(*Target->isa->_free)(Target);	free_CSOfields();	return HT_LOADED;    }    /*    **	Append return fields.    */    if (return_type == 1) {	StrAllocCat(command, " return all");    } else if (return_type == 2) {	StrAllocCat(command, " return");	for (fld = CSOfields; fld; fld = fld->next) {	    if (fld->explicit_return) {		HTSprintf(&command, " %s", fld->name);	    }	}    }    HTSprintf(&command, "%c%c", CR, LF);    strcpy(buf, "<H2>\n<EM>CSO/PH command:</EM> ");    (*Target->isa->put_block)(Target, buf, strlen(buf));    (*Target->isa->put_block)(Target, command, strlen(command));    strcpy(buf, "</H2>\n");    (*Target->isa->put_block)(Target, buf, strlen(buf));    CTRACE(tfp, "HTLoadCSO: Writing command `%s' to socket %d\n",		command, s);    status = NETWRITE(s, command, strlen(command));    FREE(command);    if (status < 0) {	CTRACE(tfp, "HTLoadCSO: Unable to send command.\n");	free_CSOfields();	return HTInetStatus("send");    }    generate_cso_report(Target);    NETCLOSE(s);    (*Target->isa->put_block)(Target, end_form, sizeof(end_form)-1);    (*Target->isa->_free)(Target);    FREE(host);    free_CSOfields();    return HT_LOADED;}