htbuffer stuff

This commit is contained in:
whottgen
2005-03-07 14:11:29 +00:00
parent 5660da9844
commit 360dc19fb0
13 changed files with 514 additions and 73 deletions

View File

@ -28,6 +28,7 @@
#include <containers_public.h>
#include "htmalloc.h"
#include "htbuffer.h"
#include "smmapd.h"
@ -35,7 +36,7 @@ int pg_worker_init(cfgl_t *cfg, void **handle);
int pg_worker_destroy(void *handle);
int pg_worker_work_setup(void *handle, void **work_handle);
int pg_worker_work_destroy(void *handle, void *work_handle);
int pg_worker_work(void *handle, void *work_handle, char *input, char *output);
int pg_worker_work(void *handle, void *work_handle, char *input, htbuffer_t *output);
class_descriptor_t pgworker = {
"pgworker",
@ -54,6 +55,10 @@ typedef struct pg_container_handle_s {
int param_cnt;
char *input_delimiter;
char *output_format_string;
char *dbhost;
char *dbname;
char *dbuser;
char *dbpass;
} pg_container_handle_t;
typedef struct pg_worker_handle_s {
@ -63,6 +68,11 @@ typedef struct pg_worker_handle_s {
PGconn *dbconn;
} pg_worker_handle_t;
static int process(pg_worker_handle_t *pwh, char *input, htbuffer_t *output);
static int count_params(const char *stmt);
static int break_input(pg_worker_handle_t *pwh, char *input);
int debug = 0;
@ -97,6 +107,10 @@ static int count_params(const char *stmt) {
syslog(LOG_ERR, "pgworker count_params: failure, x=0");
break;
}
if (x >= MAX_PARAM_COUNT) {
syslog(LOG_ERR, "pgworker count_params: failure, x > MAX_PARAM_COUNT");
break;
}
checkArray[x-1] = 1;
cnt++;
done = 1;
@ -148,6 +162,29 @@ int pg_worker_init(cfgl_t *cfg, void **handle) {
return -1;
}
pch->output_format_string = findcfgl(pch->cfg, "output_format_string");
pch->dbhost = findcfgl(pch->cfg, "dbhost");
if (pch->dbhost == NULL) {
syslog(LOG_ERR, "pgworker pg_worker_init: missing dbhost");
return -1;
}
pch->dbname = findcfgl(pch->cfg, "dbname");
if (pch->dbname == NULL) {
syslog(LOG_ERR, "pgworker pg_worker_init: missing dbname");
return -1;
}
pch->dbuser = findcfgl(pch->cfg, "dbuser");
if (pch->dbuser == NULL) {
syslog(LOG_ERR, "pgworker pg_worker_init: missing dbuser");
return -1;
}
pch->dbpass = findcfgl(pch->cfg, "dbpass");
if (pch->dbpass == NULL) {
syslog(LOG_ERR, "pgworker pg_worker_init: missing dbpass");
return -1;
}
*handle = pch;
return 0;
}
@ -167,13 +204,13 @@ int pg_worker_work_setup(void *handle, void **work_handle) {
pwh->param_array = (char **) htmalloc(sizeof(char*) * pwh->pch->param_cnt);
syslog(LOG_DEBUG, "pgworker c=%p w=%p", pwh->pch, pwh);
pwh->dbconn = PQsetdbLogin("confdb.hottis.de",
pwh->dbconn = PQsetdbLogin(pwh->pch->dbhost,
NULL,
NULL,
NULL,
"config",
"lurker",
"lurker123");
pwh->pch->dbname,
pwh->pch->dbuser,
pwh->pch->dbpass);
syslog(LOG_DEBUG, "pgworker (%p) pg_worker_work_setup: dbconn=%p",
pwh, pwh->dbconn);
@ -189,16 +226,12 @@ int pg_worker_work_destroy(void *handle, void *work_handle) {
free(pwh);
}
int pg_worker_work(void *handle, void *work_handle, char *input, char *output) {
int pg_worker_work(void *handle, void *work_handle, char *input, htbuffer_t *output) {
int res;
pg_container_handle_t *pch = (pg_container_handle_t*) handle;
pg_worker_handle_t *pwh = (pg_worker_handle_t*) work_handle;
syslog(LOG_DEBUG, "pgworker (%p) pg_worker_work entered", pwh);
sprintf(output, "pg-worker receives %s (pch %p, pwh %p) (called %d)\n",
input, pch, pwh, pwh->counter++);
res = process(pwh, input, output);
return SMM_OK;
@ -251,17 +284,17 @@ static int break_input(pg_worker_handle_t *pwh, char *input) {
#define MAX_RESULT_DIGIT_NUM 2
static int populate_output_format(pg_worker_handle_t *pwh, PGresult *pgr,
char *output, int tuple_num) {
htbuffer_t *output, int tuple_num) {
const char *formatPtr = pwh->pch->output_format_string;
const char *tmpPtr = formatPtr;
char numBuf[MAX_PARAM_DIGIT_NUM + 1];
char *s;
int i, x, cnt=0, done, res=0;
output[0] = '\0';
htbuffer_strcpy(output, "");
while ((formatPtr = strchr(formatPtr, '$')) && (res == 0)) {
strncat(output, tmpPtr, formatPtr - tmpPtr);
htbuffer_strncat(output, tmpPtr, formatPtr - tmpPtr);
syslog(LOG_DEBUG, "pgworker (%p) populate_output_format OUTPUT: %s",
pwh, output);
formatPtr++;
@ -281,14 +314,14 @@ static int populate_output_format(pg_worker_handle_t *pwh, PGresult *pgr,
s = PQgetvalue(pgr, tuple_num, x);
syslog(LOG_DEBUG, "pgworker (%p) populate_output_format OUTPUT PARAM: %s",
pwh, s);
strcat(output, s);
htbuffer_strcat(output, s);
done = 1;
}
}
if (!done) {
syslog(LOG_ERR, "pgworker (%p) populate_outputformat, not done, failure",
pwh);
output[0] = '\0';
htbuffer_strcpy(output, "");
res = -1;
break;
}
@ -296,31 +329,26 @@ static int populate_output_format(pg_worker_handle_t *pwh, PGresult *pgr,
if (done == 1) {
/* reminds */
strncat(output, tmpPtr, formatPtr - tmpPtr);
syslog(LOG_DEBUG, "OUTPUT: %s", output);
htbuffer_strncat(output, tmpPtr, formatPtr - tmpPtr);
syslog(LOG_DEBUG, "OUTPUT: %s", output->buf);
}
return res;
}
static int process(pg_worker_handle_t *pwh, char *input, char *output) {
static int process(pg_worker_handle_t *pwh, char *input, htbuffer_t *output) {
int i, j, t, f, res;
char *s;
PGresult *pgr;
if (0 == (res = break_input(pwh, input))) {
for (i = 0; i < pwh->pch->param_cnt; i++) {
syslog(LOG_DEBUG, "pgworker (%p) process: param %d --> %s",
pwh, i+1, pwh->param_array[i]);
}
pgr = PQexecParams(pwh->dbconn,
pwh->pch->sql_statement,
pwh->pch->param_cnt,
NULL,
pwh->param_array,
(const char * const *) pwh->param_array,
NULL,
NULL,
0);
@ -333,24 +361,16 @@ static int process(pg_worker_handle_t *pwh, char *input, char *output) {
syslog(LOG_DEBUG, "pgworker (%p) process PQnfields=%d", pwh, f);
if (t > 0) {
for (i = 0; i < t; i++) {
for (j = 0; j < f; j++) {
s = PQgetvalue(pgr, i, j);
syslog(LOG_DEBUG, "pgworker (%p) process result (%d, %d) = {%s}",
pwh, i, j, s);
}
}
if (NULL == pwh->pch->output_format_string) {
if (f > 1)
syslog(LOG_WARNING,
"pgworker (%p) process: more than one result fields (%d) but no format string",
pwh, f);
strcpy(output, PQgetvalue(pgr, 0, 0));
htbuffer_strcpy(output, PQgetvalue(pgr, 0, 0));
res = 0;
} else {
if (0 == (res = populate_output_format(pwh, pgr, output, 0))) {
syslog(LOG_DEBUG, "pgworker (%p) process: output %s", pwh, output);
syslog(LOG_DEBUG, "pgworker (%p) process: output %s", pwh, output->buf);
} else {
syslog(LOG_ERR, "pgworker (%p) process failure in population of output", pwh);
}
@ -358,9 +378,10 @@ static int process(pg_worker_handle_t *pwh, char *input, char *output) {
} else {
syslog(LOG_DEBUG, "pgworker (%p) process: no output", pwh);
}
PQclear(pgr);
}
PQclear(pgr);
return res;
}