rem: line.s = buf;
add: line.s = tbl_buf;
context: int db_fifo( FILE *fifo, char *response_file ){	static db_key_t keys1[MAX_ARRAY];	static db_op_t  ops1[MAX_ARRAY];	static db_val_t vals1[MAX_ARRAY];	static db_key_t keys2[MAX_ARRAY];	static db_op_t  ops2[MAX_ARRAY];	static db_val_t vals2[MAX_ARRAY];	static db_res_t *select_res;	str   line;	int   db_cmd;	int   nr1, nr2;	int   ret;	int   n;	ret = -1; /* default is error */	rpl =  0;	if (fifo_db_con==0) /* disabled due to database init/binding errors */		goto error;	/* first check the response file */	rpl = open_reply_pipe( response_file );	if (rpl==0)		goto error;	/* first name must be the real name of the DB operation */	line.s = buf;	if (!read_line( line.s, MAX_SIZE_LINE, fifo, &line.len) || line.len==0) {		double_log("DB command name expected");		goto error;	}	trim_spaces(line);	/* check the name of the command */	if (line.len==SELECT_STR_LEN	&& !strncasecmp( line.s, SELECT_STR, line.len)) {		db_cmd = SELECT_CMD;	} else if (line.len==DELETE_STR_LEN	&& !strncasecmp( line.s, DELETE_STR, line.len)) {		db_cmd = DELETE_CMD;	} else if (line.len==INSERT_STR_LEN	&& !strncasecmp( line.s, INSERT_STR, line.len)) {		db_cmd = INSERT_CMD;	} else if (line.len==UPDATE_STR_LEN	&& !strncasecmp( line.s, UPDATE_STR, line.len)) {		db_cmd = UPDATE_CMD;	} else if (line.len==RAWQUERY_STR_LEN	&& !strncasecmp( line.s, RAWQUERY_STR, line.len)) {		db_cmd = RAWQUERY_CMD;	} else if (line.len==RAWQUERYRES_STR_LEN	&& !strncasecmp( line.s, RAWQUERYRES_STR, line.len)) {		db_cmd = RAWQUERYRES_CMD;	} else {		double_log("Unknown DB command name");		goto error;	}	DBG("DEBUG:db_fifo: cmd \"%.*s\" received\n",line.len,line.s);	nr1 = 0;	nr2 = 0;	if (db_cmd==SELECT_CMD) {		/* read the columns to be fetched */		if ( get_keys( fifo, keys1, &nr1, MAX_ARRAY)!=0 )			goto error;	} else if (db_cmd==UPDATE_CMD) {		/* read the col=val pairs to be updated */		if (get_avps( fifo , keys1, ops1, vals1, &nr1, MAX_ARRAY)!=0 )			goto error;		/* must be at least one AVP in an update command */		if (nr1==0) {			double_log("UPDATE command must have at least one"				" field to update");			goto error;		}		/* all the operators must be '=' */		for(n=0;n<nr1;n++) {			if (ops1[n][0]!='=' || ops1[n][1]!='\0') {				double_log("Invalid operator in updated fields (expected = )");				goto error1;			}		}/*end for*/	} else if (db_cmd==RAWQUERY_CMD || db_cmd==RAWQUERYRES_CMD) {		/* read the raw db command  */		line.s = buf;		if (!read_line( line.s, MAX_SIZE_LINE-1,fifo,&line.len) || !line.len) {			double_log("Raw db command expected");			goto error;		}		trim_spaces(line);		/* run the command */		if (db_cmd==RAWQUERY_CMD)			n = fifo_dbf.raw_query( fifo_db_con, line.s, 0);		else			n = fifo_dbf.raw_query( fifo_db_con, line.s, &select_res);		if (n!=0) {			double_log("Internal Server error - DB query failed");			goto error;		}		/* any results? */		if (db_cmd==RAWQUERYRES_CMD) {			/* get all response and write them into reply fifo */			print_res( select_res, rpl);			/* free the query response */			fifo_dbf.free_result( fifo_db_con, select_res);		}		/* done with success */		goto done;	}	/* read the table name */	line.s = buf;	if (!read_line( line.s, MAX_SIZE_LINE-1, fifo, &line.len) || !line.len) {		double_log("Table name expected");		goto error1;	}	trim_spaces(line);	/* select the correct table */	line.s[line.len] = 0; /* make it null terminated */	if (fifo_dbf.use_table( fifo_db_con, line.s) < 0) {		double_log("use_table function failed");		goto error1;	}	/*read 'where' avps */	if (get_avps( fifo , keys2, ops2, vals2, &nr2, MAX_ARRAY)!=0 )		goto error1;	switch (db_cmd) {		case SELECT_CMD:			/* push the query */			n = fifo_dbf.query( fifo_db_con, nr2?keys2:0, nr2?ops2:0,						nr2?vals2:0, nr1?keys1:0, nr2, nr1, 0, &select_res );			if (n!=0) {				double_log("Internal Server error - DB query failed");				goto error2;			}			/* get all response and write them into reply fifo */			print_res( select_res, rpl);			/* free the query response */			fifo_dbf.free_result( fifo_db_con, select_res);			break;		case UPDATE_CMD:			if (nr1==0) {				double_log("No values for update (empty first AVP list)");				goto error;			}			/* all the operators must be '=' in the first avp list */			for(n=0;n<nr1;n++) {				if (ops1[n][0]!='=' || ops1[n][1]!='\0') {					double_log("Invalid operator in updated fields "						"(expected = )");					goto error;				}			}/*end for*/			/* push the query */			n = fifo_dbf.update( fifo_db_con, nr2?keys2:0, nr2?ops2:0,					nr2?vals2:0, keys1, vals1, nr2, nr1 );			if (n!=0) {				double_log("Internal Server error - DB query failed");				goto error2;			}			break;		case DELETE_CMD:			/* push the query */			n = fifo_dbf.delete( fifo_db_con, nr2?keys2:0, nr2?ops2:0,					nr2?vals2:0, nr2);			if (n!=0) {				double_log("Internal Server error - DB query failed");				goto error2;			}			break;		case INSERT_CMD:			if (nr2==0) {				double_log("Nothing to insert (empty AVP list)");				goto error;			}			/* all the operators must be '=' */			for(n=0;n<nr2;n++) {				if (ops2[n][0]!='=' || ops2[n][1]!='\0') {					double_log("Invalid operator in inserted fields "						"(expected = )");					goto error;				}			}/*end for*/			/* push the query */			n = fifo_dbf.insert( fifo_db_con, nr2?keys2:0, nr2?vals2:0, nr2);			if (n!=0) {				double_log("Internal Server error - DB query failed");				goto error2;			}			break;	}	/* success */done:	ret = 0;error2:	for(;nr2;nr2--)		pkg_free( (void*)keys2[nr2-1] );error1:	for(;nr1;nr1--)		pkg_free( (void*)keys1[nr1-1] );error:	if (rpl) fclose(rpl);	return ret;}