diff options
| author | Bruce Momjian <bruce@momjian.us> | 1998-09-01 04:40:42 +0000 |
|---|---|---|
| committer | Bruce Momjian <bruce@momjian.us> | 1998-09-01 04:40:42 +0000 |
| commit | fa1a8d6a97068295fe30ac646aec7493a6305bc2 (patch) | |
| tree | 645f7cef3c78fbab4d6d7bbc7c9a61ad2893d273 /src/interfaces | |
| parent | af74855a608da4cd7ef88ceb2241ec1c75537f39 (diff) | |
| download | postgresql-fa1a8d6a97068295fe30ac646aec7493a6305bc2.tar.gz | |
OK, folks, here is the pgindent output.
Diffstat (limited to 'src/interfaces')
31 files changed, 2792 insertions, 2562 deletions
diff --git a/src/interfaces/ecpg/include/ecpgerrno.h b/src/interfaces/ecpg/include/ecpgerrno.h index 2303f2a215..56cf8d5f57 100644 --- a/src/interfaces/ecpg/include/ecpgerrno.h +++ b/src/interfaces/ecpg/include/ecpgerrno.h @@ -4,7 +4,7 @@ #include <errno.h> /* This is a list of all error codes the embedded SQL program can return */ -#define ECPG_NO_ERROR 0 +#define ECPG_NO_ERROR 0 #define ECPG_NOT_FOUND 100 /* system error codes returned by ecpglib get the correct number, @@ -14,7 +14,7 @@ /* first we have a set of ecpg messages, they start at 200 */ #define ECPG_UNSUPPORTED -200 -#define ECPG_TOO_MANY_ARGUMENTS -201 +#define ECPG_TOO_MANY_ARGUMENTS -201 #define ECPG_TOO_FEW_ARGUMENTS -202 #define ECPG_TOO_MANY_MATCHES -203 #define ECPG_INT_FORMAT -204 @@ -33,4 +33,4 @@ #define ECPG_TRANS -401 #define ECPG_CONNECT -402 -#endif /* !_ECPG_ERROR_H */ +#endif /* !_ECPG_ERROR_H */ diff --git a/src/interfaces/ecpg/include/ecpglib.h b/src/interfaces/ecpg/include/ecpglib.h index 0f0bd2b38c..1c907a673c 100644 --- a/src/interfaces/ecpg/include/ecpglib.h +++ b/src/interfaces/ecpg/include/ecpglib.h @@ -1,43 +1,46 @@ #include <c.h> #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -void ECPGdebug(int, FILE *); -bool ECPGsetconn(int, const char *); -bool ECPGconnect(int, const char *, const char *, const char *, const char *); -bool ECPGdo(int, char *,...); -bool ECPGtrans(int, const char *); -bool ECPGdisconnect(int, const char *); + void ECPGdebug(int, FILE *); + bool ECPGsetconn(int, const char *); + bool ECPGconnect(int, const char *, const char *, const char *, const char *); + bool ECPGdo(int, char *,...); + bool ECPGtrans(int, const char *); + bool ECPGdisconnect(int, const char *); -void ECPGlog(const char *format,...); + void ECPGlog(const char *format,...); #ifdef LIBPQ_FE_H -bool ECPGsetdb(PGconn *); + bool ECPGsetdb(PGconn *); #endif /* Here are some methods used by the lib. */ /* Returns a pointer to a string containing a simple type name. */ -const char *ECPGtype_name(enum ECPGttype); + const char *ECPGtype_name(enum ECPGttype); /* A generic varchar type. */ -struct ECPGgeneric_varchar -{ - int len; - char arr[1]; -}; + struct ECPGgeneric_varchar + { + int len; + char arr[1]; + }; /* print an error message */ -void sqlprint(void); + void sqlprint(void); -struct cursor { const char *name; - char *command; + struct cursor + { + const char *name; + char *command; struct cursor *next; - }; + }; -extern int no_auto_trans; + extern int no_auto_trans; /* define this for simplicity as well as compatibility */ @@ -45,6 +48,7 @@ extern int no_auto_trans; #ifdef __cplusplus } + #endif #include <ecpgerrno.h> diff --git a/src/interfaces/ecpg/include/ecpgtype.h b/src/interfaces/ecpg/include/ecpgtype.h index 27641df39a..945ce0b98d 100644 --- a/src/interfaces/ecpg/include/ecpgtype.h +++ b/src/interfaces/ecpg/include/ecpgtype.h @@ -30,27 +30,29 @@ #include <stdio.h> #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -enum ECPGttype -{ - ECPGt_char = 1, ECPGt_unsigned_char, ECPGt_short, ECPGt_unsigned_short, - ECPGt_int, ECPGt_unsigned_int, ECPGt_long, ECPGt_unsigned_long, - ECPGt_bool, - ECPGt_float, ECPGt_double, - ECPGt_varchar, ECPGt_varchar2, - ECPGt_array, - ECPGt_struct, - ECPGt_EOIT, /* End of insert types. */ - ECPGt_EORT, /* End of result types. */ - ECPGt_NO_INDICATOR /* no indicator */ -}; + enum ECPGttype + { + ECPGt_char = 1, ECPGt_unsigned_char, ECPGt_short, ECPGt_unsigned_short, + ECPGt_int, ECPGt_unsigned_int, ECPGt_long, ECPGt_unsigned_long, + ECPGt_bool, + ECPGt_float, ECPGt_double, + ECPGt_varchar, ECPGt_varchar2, + ECPGt_array, + ECPGt_struct, + ECPGt_EOIT, /* End of insert types. */ + ECPGt_EORT, /* End of result types. */ + ECPGt_NO_INDICATOR /* no indicator */ + }; #define IS_SIMPLE_TYPE(type) ((type) >= ECPGt_char && (type) <= ECPGt_varchar2) -const char * ECPGtype_name(enum ECPGttype); + const char *ECPGtype_name(enum ECPGttype); #ifdef __cplusplus } + #endif diff --git a/src/interfaces/ecpg/include/sqlca.h b/src/interfaces/ecpg/include/sqlca.h index f96067d974..d1fedd5b85 100644 --- a/src/interfaces/ecpg/include/sqlca.h +++ b/src/interfaces/ecpg/include/sqlca.h @@ -2,49 +2,50 @@ #define POSTGRES_SQLCA_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -struct sqlca -{ - char sqlcaid[8]; - long sqlabc; - long sqlcode; - struct + struct sqlca { - int sqlerrml; - char sqlerrmc[70]; - } sqlerrm; - char sqlerrp[8]; - long sqlerrd[6]; - /* Element 0: empty */ - /* 1: empty */ - /* 2: number of rows processed */ - /* after an INSERT, UPDATE or*/ - /* DELETE statement */ - /* 3: empty */ - /* 4: empty */ - /* 5: empty */ - char sqlwarn[8]; + char sqlcaid[8]; + long sqlabc; + long sqlcode; + struct + { + int sqlerrml; + char sqlerrmc[70]; + } sqlerrm; + char sqlerrp[8]; + long sqlerrd[6]; + /* Element 0: empty */ + /* 1: empty */ + /* 2: number of rows processed */ + /* after an INSERT, UPDATE or */ + /* DELETE statement */ + /* 3: empty */ + /* 4: empty */ + /* 5: empty */ + char sqlwarn[8]; /* Element 0: set to 'W' if at least one other is 'W' */ - /* 1: if 'W' at least one character string */ - /* value was truncated when it was */ - /* stored into a host variable. */ - /* 2: empty */ - /* 3: empty */ - /* 4: empty */ - /* 5: empty */ - /* 6: empty */ - /* 7: empty */ - - char sqlext[8]; -}; + /* 1: if 'W' at least one character string */ + /* value was truncated when it was */ + /* stored into a host variable. */ + /* 2: empty */ + /* 3: empty */ + /* 4: empty */ + /* 5: empty */ + /* 6: empty */ + /* 7: empty */ + + char sqlext[8]; + }; + + extern struct sqlca sqlca; -extern struct sqlca sqlca; - #endif #ifdef __cplusplus } + #endif - diff --git a/src/interfaces/ecpg/lib/ecpglib.c b/src/interfaces/ecpg/lib/ecpglib.c index b2bb9f1415..3804544514 100644 --- a/src/interfaces/ecpg/lib/ecpglib.c +++ b/src/interfaces/ecpg/lib/ecpglib.c @@ -25,58 +25,58 @@ #include <sqlca.h> /* variables visible to the programs */ -int no_auto_trans; +int no_auto_trans; -static struct sqlca sqlca_init = +static struct sqlca sqlca_init = { - {'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '}, - sizeof(struct sqlca), - 0, - { 0, {0}}, - {'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '}, - {0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0} + {'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '}, + sizeof(struct sqlca), + 0, + {0, {0}}, + {'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0} }; struct sqlca sqlca = { - {'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '}, - sizeof(struct sqlca), - 0, - { 0, {0}}, - {'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '}, - {0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0} + {'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '}, + sizeof(struct sqlca), + 0, + {0, {0}}, + {'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0} }; static struct connection { - char *name; - PGconn *connection; + char *name; + PGconn *connection; struct connection *next; -} *all_connections = NULL, *actual_connection = NULL; +} *all_connections = NULL, *actual_connection = NULL; struct variable { enum ECPGttype type; - void *value; - long varcharsize; - long arrsize; - long offset; + void *value; + long varcharsize; + long arrsize; + long offset; enum ECPGttype ind_type; - void *ind_value; - long ind_varcharsize; - long ind_arrsize; - long ind_offset; + void *ind_value; + long ind_varcharsize; + long ind_arrsize; + long ind_offset; struct variable *next; }; struct statement { - int lineno; - char *command; + int lineno; + char *command; struct variable *inlist; struct variable *outlist; }; @@ -98,7 +98,7 @@ register_error(long code, char *fmt,...) } static void -ECPGfinish(struct connection *act) +ECPGfinish(struct connection * act) { if (act != NULL) { @@ -114,8 +114,8 @@ ECPGfinish(struct connection *act) else { struct connection *con; - - for (con = all_connections; con->next && con->next != act; con = con->next); + + for (con = all_connections; con->next && con->next != act; con = con->next); if (con->next) { con->next = act->next; @@ -123,7 +123,7 @@ ECPGfinish(struct connection *act) free(act); } } - + if (actual_connection == act) actual_connection = all_connections; } @@ -131,20 +131,21 @@ ECPGfinish(struct connection *act) ECPGlog("ECPGfinish: called an extra time.\n"); } -static char *ecpg_alloc(long size, int lineno) +static char * +ecpg_alloc(long size, int lineno) { - char *new = (char *) malloc(size); + char *new = (char *) malloc(size); if (!new) { ECPGfinish(actual_connection); ECPGlog("out of memory\n"); - register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno); + register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno); return NULL; } - + memset(new, '\0', size); - return(new); + return (new); } /* This function returns a newly malloced string that has the ' and \ @@ -159,8 +160,8 @@ quote_postgres(char *arg, int lineno) ri; if (!res) - return(res); - + return (res); + for (i = 0, ri = 0; arg[i]; i++, ri++) { switch (arg[i]) @@ -181,62 +182,61 @@ quote_postgres(char *arg, int lineno) /* create a list of variables */ static bool -create_statement(int lineno, struct statement **stmt, char *query, va_list ap) +create_statement(int lineno, struct statement ** stmt, char *query, va_list ap) { struct variable **list = &((*stmt)->inlist); - enum ECPGttype type; - + enum ECPGttype type; + if (!(*stmt = (struct statement *) ecpg_alloc(sizeof(struct statement), lineno))) return false; - + (*stmt)->command = query; (*stmt)->lineno = lineno; - + list = &((*stmt)->inlist); type = va_arg(ap, enum ECPGttype); - + while (type != ECPGt_EORT) - { - if (type == ECPGt_EOIT) - { - list = &((*stmt)->outlist); - } - else - { - struct variable *var, *ptr; - - if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno))) - return false; - - var->type = type; - var->value = va_arg(ap, void *); - var->varcharsize = va_arg(ap, long); - var->arrsize = va_arg(ap, long); - var->offset = va_arg(ap, long); - var->ind_type = va_arg(ap, enum ECPGttype); - var->ind_value = va_arg(ap, void *); - var->ind_varcharsize = va_arg(ap, long); - var->ind_arrsize = va_arg(ap, long); - var->ind_offset = va_arg(ap, long); - var->next = NULL; - - for (ptr = *list; ptr && ptr->next; ptr=ptr->next); - - if (ptr == NULL) - *list = var; + { + if (type == ECPGt_EOIT) + list = &((*stmt)->outlist); else - ptr->next = var; - } - - type = va_arg(ap, enum ECPGttype); + { + struct variable *var, + *ptr; + + if (!(var = (struct variable *) ecpg_alloc(sizeof(struct variable), lineno))) + return false; + + var->type = type; + var->value = va_arg(ap, void *); + var->varcharsize = va_arg(ap, long); + var->arrsize = va_arg(ap, long); + var->offset = va_arg(ap, long); + var->ind_type = va_arg(ap, enum ECPGttype); + var->ind_value = va_arg(ap, void *); + var->ind_varcharsize = va_arg(ap, long); + var->ind_arrsize = va_arg(ap, long); + var->ind_offset = va_arg(ap, long); + var->next = NULL; + + for (ptr = *list; ptr && ptr->next; ptr = ptr->next); + + if (ptr == NULL) + *list = var; + else + ptr->next = var; + } + + type = va_arg(ap, enum ECPGttype); } - - return(true); + + return (true); } static bool -ECPGexecute(struct statement *stmt) +ECPGexecute(struct statement * stmt) { bool status = false; char *copiedquery; @@ -244,8 +244,8 @@ ECPGexecute(struct statement *stmt) PGnotify *notify; struct variable *var; - memcpy((char *)&sqlca, (char *)&sqlca_init, sizeof(sqlca)); - + memcpy((char *) &sqlca, (char *) &sqlca_init, sizeof(sqlca)); + copiedquery = strdup(stmt->command); /* @@ -270,7 +270,7 @@ ECPGexecute(struct statement *stmt) */ buff[0] = '\0'; - + /* check for null value and set input buffer accordingly */ switch (var->ind_type) { @@ -292,117 +292,117 @@ ECPGexecute(struct statement *stmt) default: break; } - + if (*buff == '\0') { - switch (var->type) - { - case ECPGt_short: - case ECPGt_int: - sprintf(buff, "%d", *(int *) var->value); - tobeinserted = buff; - break; + switch (var->type) + { + case ECPGt_short: + case ECPGt_int: + sprintf(buff, "%d", *(int *) var->value); + tobeinserted = buff; + break; - case ECPGt_unsigned_short: - case ECPGt_unsigned_int: - sprintf(buff, "%d", *(unsigned int *) var->value); - tobeinserted = buff; - break; + case ECPGt_unsigned_short: + case ECPGt_unsigned_int: + sprintf(buff, "%d", *(unsigned int *) var->value); + tobeinserted = buff; + break; - case ECPGt_long: - sprintf(buff, "%ld", *(long *) var->value); - tobeinserted = buff; - break; + case ECPGt_long: + sprintf(buff, "%ld", *(long *) var->value); + tobeinserted = buff; + break; - case ECPGt_unsigned_long: - sprintf(buff, "%ld", *(unsigned long *) var->value); - tobeinserted = buff; - break; + case ECPGt_unsigned_long: + sprintf(buff, "%ld", *(unsigned long *) var->value); + tobeinserted = buff; + break; - case ECPGt_float: - sprintf(buff, "%.14g", *(float *) var->value); - tobeinserted = buff; - break; + case ECPGt_float: + sprintf(buff, "%.14g", *(float *) var->value); + tobeinserted = buff; + break; - case ECPGt_double: - sprintf(buff, "%.14g", *(double *) var->value); - tobeinserted = buff; - break; + case ECPGt_double: + sprintf(buff, "%.14g", *(double *) var->value); + tobeinserted = buff; + break; - case ECPGt_bool: - sprintf(buff, "'%c'", (*(char *) var->value ? 't' : 'f')); - tobeinserted = buff; - break; + case ECPGt_bool: + sprintf(buff, "'%c'", (*(char *) var->value ? 't' : 'f')); + tobeinserted = buff; + break; - case ECPGt_char: - case ECPGt_unsigned_char: - { - /* set slen to string length if type is char * */ - int slen = (var->varcharsize == 0) ? strlen((char *) var->value) : var->varcharsize; - char * tmp; - - if (!(newcopy = ecpg_alloc(slen + 1, stmt->lineno))) - return false; - - strncpy(newcopy, (char *) var->value, slen); - newcopy[slen] = '\0'; - - if (!(mallocedval = (char *) ecpg_alloc(2 * strlen(newcopy) + 3, stmt->lineno))) - return false; - - strcpy(mallocedval, "'"); - tmp = quote_postgres(newcopy, stmt->lineno); - if (!tmp) - return false; - - strcat(mallocedval, tmp); - strcat(mallocedval, "'"); - - free(newcopy); - - tobeinserted = mallocedval; - } - break; + case ECPGt_char: + case ECPGt_unsigned_char: + { + /* set slen to string length if type is char * */ + int slen = (var->varcharsize == 0) ? strlen((char *) var->value) : var->varcharsize; + char *tmp; - case ECPGt_varchar: - { - struct ECPGgeneric_varchar *variable = - (struct ECPGgeneric_varchar *) (var->value); - char *tmp; - - if (!(newcopy = (char *) ecpg_alloc(variable->len + 1, stmt->lineno))) - return false; - - strncpy(newcopy, variable->arr, variable->len); - newcopy[variable->len] = '\0'; - - if (!(mallocedval = (char *) ecpg_alloc(2 * strlen(newcopy) + 3, stmt->lineno))) - return false; - - strcpy(mallocedval, "'"); - tmp = quote_postgres(newcopy, stmt->lineno); - if (!tmp) - return false; - - strcat(mallocedval, tmp); - strcat(mallocedval, "'"); - - free(newcopy); - - tobeinserted = mallocedval; - } - break; + if (!(newcopy = ecpg_alloc(slen + 1, stmt->lineno))) + return false; - default: - /* Not implemented yet */ - register_error(ECPG_UNSUPPORTED, "Unsupported type %s on line %d.", - ECPGtype_name(var->type), stmt->lineno); - return false; - break; - } + strncpy(newcopy, (char *) var->value, slen); + newcopy[slen] = '\0'; + + if (!(mallocedval = (char *) ecpg_alloc(2 * strlen(newcopy) + 3, stmt->lineno))) + return false; + + strcpy(mallocedval, "'"); + tmp = quote_postgres(newcopy, stmt->lineno); + if (!tmp) + return false; + + strcat(mallocedval, tmp); + strcat(mallocedval, "'"); + + free(newcopy); + + tobeinserted = mallocedval; + } + break; + + case ECPGt_varchar: + { + struct ECPGgeneric_varchar *variable = + (struct ECPGgeneric_varchar *) (var->value); + char *tmp; + + if (!(newcopy = (char *) ecpg_alloc(variable->len + 1, stmt->lineno))) + return false; + + strncpy(newcopy, variable->arr, variable->len); + newcopy[variable->len] = '\0'; + + if (!(mallocedval = (char *) ecpg_alloc(2 * strlen(newcopy) + 3, stmt->lineno))) + return false; + + strcpy(mallocedval, "'"); + tmp = quote_postgres(newcopy, stmt->lineno); + if (!tmp) + return false; + + strcat(mallocedval, tmp); + strcat(mallocedval, "'"); + + free(newcopy); + + tobeinserted = mallocedval; + } + break; + + default: + /* Not implemented yet */ + register_error(ECPG_UNSUPPORTED, "Unsupported type %s on line %d.", + ECPGtype_name(var->type), stmt->lineno); + return false; + break; + } } else - tobeinserted = buff; + tobeinserted = buff; /* * Now tobeinserted points to an area that is to be inserted at @@ -410,7 +410,7 @@ ECPGexecute(struct statement *stmt) */ if (!(newcopy = (char *) ecpg_alloc(strlen(copiedquery) + strlen(tobeinserted) + 1, stmt->lineno))) return false; - + strcpy(newcopy, copiedquery); if ((p = strstr(newcopy, ";;")) == NULL) { @@ -438,7 +438,8 @@ ECPGexecute(struct statement *stmt) /* * Now everything is safely copied to the newcopy. Lets free the - * oldcopy and let the copiedquery get the var->value from the newcopy. + * oldcopy and let the copiedquery get the var->value from the + * newcopy. */ if (mallocedval != NULL) { @@ -481,7 +482,7 @@ ECPGexecute(struct statement *stmt) ECPGlog("ECPGexecute line %d: error: %s", stmt->lineno, PQerrorMessage(actual_connection->connection)); register_error(ECPG_PGSQL, "Postgres error: %s line %d.", - PQerrorMessage(actual_connection->connection), stmt->lineno); + PQerrorMessage(actual_connection->connection), stmt->lineno); } else { @@ -489,7 +490,10 @@ ECPGexecute(struct statement *stmt) var = stmt->outlist; switch (PQresultStatus(results)) { - int nfields, ntuples, act_tuple, act_field; + int nfields, + ntuples, + act_tuple, + act_field; case PGRES_TUPLES_OK: @@ -502,7 +506,7 @@ ECPGexecute(struct statement *stmt) nfields = PQnfields(results); sqlca.sqlerrd[2] = ntuples = PQntuples(results); status = true; - + if (ntuples < 1) { ECPGlog("ECPGexecute line %d: Incorrect number of matches: %d\n", @@ -512,268 +516,274 @@ ECPGexecute(struct statement *stmt) break; } - for (act_field = 0; act_field < nfields && status; act_field++) - { - char *pval; - char *scan_length; - - if (var == NULL) - { - ECPGlog("ECPGexecute line %d: Too few arguments.\n", stmt->lineno); - register_error(ECPG_TOO_FEW_ARGUMENTS, "Too few arguments line %d.", stmt->lineno); - return(false); - } - - /* if we don't have enough space, we cannot read all tuples */ - if ((var->arrsize > 0 && ntuples > var->arrsize) || (var->ind_arrsize > 0 && ntuples > var->ind_arrsize)) - { - ECPGlog("ECPGexecute line %d: Incorrect number of matches: %d don't fit into array of %d\n", - stmt->lineno, ntuples, var->arrsize); - register_error(ECPG_TOO_MANY_MATCHES, "Too many matches line %d.", stmt->lineno); - status = false; - break; - } - for (act_tuple = 0; act_tuple < ntuples; act_tuple++) - { - pval = PQgetvalue(results, act_tuple, act_field); - - ECPGlog("ECPGexecute line %d: RESULT: %s\n", stmt->lineno, pval ? pval : ""); - - /* Now the pval is a pointer to the var->value. */ - /* We will have to decode the var->value */ - - /* check for null var->value and set indicator accordingly */ - switch (var->ind_type) - { - case ECPGt_short: - case ECPGt_unsigned_short: - ((short *) var->ind_value)[act_tuple] = -PQgetisnull(results, act_tuple, act_field); - break; - case ECPGt_int: - case ECPGt_unsigned_int: - ((int *) var->ind_value)[act_tuple] = -PQgetisnull(results, act_tuple, act_field); - break; - case ECPGt_long: - case ECPGt_unsigned_long: - ((long *) var->ind_value)[act_tuple] = -PQgetisnull(results, act_tuple, act_field); - break; - default: - break; - } - - switch (var->type) - { - long res; - unsigned long ures; - double dres; - - case ECPGt_short: - case ECPGt_int: - case ECPGt_long: - if (pval) - { - res = strtol(pval, &scan_length, 10); - if (*scan_length != '\0') /* Garbage left */ - { - register_error(ECPG_INT_FORMAT, "Not correctly formatted int type: %s line %d.", - pval, stmt->lineno); - status = false; - res = 0L; - } - } - else - res = 0L; - - /* Again?! Yes */ - switch (var->type) - { - case ECPGt_short: - ((short *) var->value)[act_tuple] = (short) res; - break; - case ECPGt_int: - ((int *) var->value)[act_tuple] = (int) res; - break; - case ECPGt_long: - ((long *) var->value)[act_tuple] = res; - break; - default: - /* Cannot happen */ - break; - } - break; - - case ECPGt_unsigned_short: - case ECPGt_unsigned_int: - case ECPGt_unsigned_long: - if (pval) - { - ures = strtoul(pval, &scan_length, 10); - if (*scan_length != '\0') /* Garbage left */ - { - register_error(ECPG_UINT_FORMAT, "Not correctly formatted unsigned type: %s line %d.", - pval, stmt->lineno); - status = false; - ures = 0L; - } - } - else - ures = 0L; - - /* Again?! Yes */ - switch (var->type) - { - case ECPGt_unsigned_short: - ((unsigned short *) var->value)[act_tuple] = (unsigned short) ures; - break; - case ECPGt_unsigned_int: - ((unsigned int *) var->value)[act_tuple] = (unsigned int) ures; - break; - case ECPGt_unsigned_long: - ((unsigned long *) var->value)[act_tuple] = ures; - break; - default: - /* Cannot happen */ - break; - } - break; - - - case ECPGt_float: - case ECPGt_double: - if (pval) - { - dres = strtod(pval, &scan_length); - if (*scan_length != '\0') /* Garbage left */ - { - register_error(ECPG_FLOAT_FORMAT, "Not correctly formatted floating point type: %s line %d.", - pval, stmt->lineno); - status = false; - dres = 0.0; - } - } - else - dres = 0.0; - - /* Again?! Yes */ - switch (var->type) - { - case ECPGt_float: - ((float *) var->value)[act_tuple] = dres; - break; - case ECPGt_double: - ((double *) var->value)[act_tuple] = dres; - break; - default: - /* Cannot happen */ - break; - } - break; - - case ECPGt_bool: - if (pval) - { - if (pval[0] == 'f' && pval[1] == '\0') - { - ((char *) var->value)[act_tuple] = false; - break; - } - else if (pval[0] == 't' && pval[1] == '\0') - { - ((char *) var->value)[act_tuple] = true; - break; - } - } - - register_error(ECPG_CONVERT_BOOL, "Unable to convert %s to bool on line %d.", - (pval ? pval : "NULL"), - stmt->lineno); - status = false; - break; - - case ECPGt_char: - case ECPGt_unsigned_char: - { - if (var->varcharsize == 0) - { - /* char* */ - strncpy(((char **) var->value)[act_tuple], pval, strlen(pval)); - (((char **) var->value)[act_tuple])[strlen(pval)] = '\0'; - } - else - { - strncpy((char *) ((long)var->value + var->offset * act_tuple), pval, var->varcharsize); - if (var->varcharsize < strlen(pval)) - { - /* truncation */ - switch (var->ind_type) - { - case ECPGt_short: - case ECPGt_unsigned_short: - ((short *) var->ind_value)[act_tuple] = var->varcharsize; - break; - case ECPGt_int: - case ECPGt_unsigned_int: - ((int *) var->ind_value)[act_tuple] = var->varcharsize; - break; - case ECPGt_long: - case ECPGt_unsigned_long: - ((long *) var->ind_value)[act_tuple] = var->varcharsize; - break; - default: - break; - } - sqlca.sqlwarn[0] = sqlca.sqlwarn[1] = 'W'; - } - } - } - break; - - case ECPGt_varchar: - { - struct ECPGgeneric_varchar *variable = - (struct ECPGgeneric_varchar *) ((long)var->value + var->offset * act_tuple); - - if (var->varcharsize == 0) - strncpy(variable->arr, pval, strlen(pval)); - else - strncpy(variable->arr, pval, var->varcharsize); - - variable->len = strlen(pval); - if (var->varcharsize > 0 && variable->len > var->varcharsize) - { - /* truncation */ - switch (var->ind_type) - { - case ECPGt_short: - case ECPGt_unsigned_short: - ((short *) var->ind_value)[act_tuple] = var->varcharsize; - break; - case ECPGt_int: - case ECPGt_unsigned_int: - ((int *) var->ind_value)[act_tuple] = var->varcharsize; - break; - case ECPGt_long: - case ECPGt_unsigned_long: - ((long *) var->ind_value)[act_tuple] = var->varcharsize; - break; - default: - break; - } - sqlca.sqlwarn[0] = sqlca.sqlwarn[1] = 'W'; - - variable->len = var->varcharsize; - } - } - break; - - default: - register_error(ECPG_UNSUPPORTED, "Unsupported type %s on line %d.", - ECPGtype_name(var->type), stmt->lineno); - status = false; - break; - } - } - var = var->next; + for (act_field = 0; act_field < nfields && status; act_field++) + { + char *pval; + char *scan_length; + + if (var == NULL) + { + ECPGlog("ECPGexecute line %d: Too few arguments.\n", stmt->lineno); + register_error(ECPG_TOO_FEW_ARGUMENTS, "Too few arguments line %d.", stmt->lineno); + return (false); + } + + /* + * if we don't have enough space, we cannot read all + * tuples + */ + if ((var->arrsize > 0 && ntuples > var->arrsize) || (var->ind_arrsize > 0 && ntuples > var->ind_arrsize)) + { + ECPGlog("ECPGexecute line %d: Incorrect number of matches: %d don't fit into array of %d\n", + stmt->lineno, ntuples, var->arrsize); + register_error(ECPG_TOO_MANY_MATCHES, "Too many matches line %d.", stmt->lineno); + status = false; + break; + } + for (act_tuple = 0; act_tuple < ntuples; act_tuple++) + { + pval = PQgetvalue(results, act_tuple, act_field); + + ECPGlog("ECPGexecute line %d: RESULT: %s\n", stmt->lineno, pval ? pval : ""); + + /* Now the pval is a pointer to the var->value. */ + /* We will have to decode the var->value */ + + /* + * check for null var->value and set indicator + * accordingly + */ + switch (var->ind_type) + { + case ECPGt_short: + case ECPGt_unsigned_short: + ((short *) var->ind_value)[act_tuple] = -PQgetisnull(results, act_tuple, act_field); + break; + case ECPGt_int: + case ECPGt_unsigned_int: + ((int *) var->ind_value)[act_tuple] = -PQgetisnull(results, act_tuple, act_field); + break; + case ECPGt_long: + case ECPGt_unsigned_long: + ((long *) var->ind_value)[act_tuple] = -PQgetisnull(results, act_tuple, act_field); + break; + default: + break; + } + + switch (var->type) + { + long res; + unsigned long ures; + double dres; + + case ECPGt_short: + case ECPGt_int: + case ECPGt_long: + if (pval) + { + res = strtol(pval, &scan_length, 10); + if (*scan_length != '\0') /* Garbage left */ + { + register_error(ECPG_INT_FORMAT, "Not correctly formatted int type: %s line %d.", + pval, stmt->lineno); + status = false; + res = 0L; + } + } + else + res = 0L; + + /* Again?! Yes */ + switch (var->type) + { + case ECPGt_short: + ((short *) var->value)[act_tuple] = (short) res; + break; + case ECPGt_int: + ((int *) var->value)[act_tuple] = (int) res; + break; + case ECPGt_long: + ((long *) var->value)[act_tuple] = res; + break; + default: + /* Cannot happen */ + break; + } + break; + + case ECPGt_unsigned_short: + case ECPGt_unsigned_int: + case ECPGt_unsigned_long: + if (pval) + { + ures = strtoul(pval, &scan_length, 10); + if (*scan_length != '\0') /* Garbage left */ + { + register_error(ECPG_UINT_FORMAT, "Not correctly formatted unsigned type: %s line %d.", + pval, stmt->lineno); + status = false; + ures = 0L; + } + } + else + ures = 0L; + + /* Again?! Yes */ + switch (var->type) + { + case ECPGt_unsigned_short: + ((unsigned short *) var->value)[act_tuple] = (unsigned short) ures; + break; + case ECPGt_unsigned_int: + ((unsigned int *) var->value)[act_tuple] = (unsigned int) ures; + break; + case ECPGt_unsigned_long: + ((unsigned long *) var->value)[act_tuple] = ures; + break; + default: + /* Cannot happen */ + break; + } + break; + + + case ECPGt_float: + case ECPGt_double: + if (pval) + { + dres = strtod(pval, &scan_length); + if (*scan_length != '\0') /* Garbage left */ + { + register_error(ECPG_FLOAT_FORMAT, "Not correctly formatted floating point type: %s line %d.", + pval, stmt->lineno); + status = false; + dres = 0.0; + } + } + else + dres = 0.0; + + /* Again?! Yes */ + switch (var->type) + { + case ECPGt_float: + ((float *) var->value)[act_tuple] = dres; + break; + case ECPGt_double: + ((double *) var->value)[act_tuple] = dres; + break; + default: + /* Cannot happen */ + break; + } + break; + + case ECPGt_bool: + if (pval) + { + if (pval[0] == 'f' && pval[1] == '\0') + { + ((char *) var->value)[act_tuple] = false; + break; + } + else if (pval[0] == 't' && pval[1] == '\0') + { + ((char *) var->value)[act_tuple] = true; + break; + } + } + + register_error(ECPG_CONVERT_BOOL, "Unable to convert %s to bool on line %d.", + (pval ? pval : "NULL"), + stmt->lineno); + status = false; + break; + + case ECPGt_char: + case ECPGt_unsigned_char: + { + if (var->varcharsize == 0) + { + /* char* */ + strncpy(((char **) var->value)[act_tuple], pval, strlen(pval)); + (((char **) var->value)[act_tuple])[strlen(pval)] = '\0'; + } + else + { + strncpy((char *) ((long) var->value + var->offset * act_tuple), pval, var->varcharsize); + if (var->varcharsize < strlen(pval)) + { + /* truncation */ + switch (var->ind_type) + { + case ECPGt_short: + case ECPGt_unsigned_short: + ((short *) var->ind_value)[act_tuple] = var->varcharsize; + break; + case ECPGt_int: + case ECPGt_unsigned_int: + ((int *) var->ind_value)[act_tuple] = var->varcharsize; + break; + case ECPGt_long: + case ECPGt_unsigned_long: + ((long *) var->ind_value)[act_tuple] = var->varcharsize; + break; + default: + break; + } + sqlca.sqlwarn[0] = sqlca.sqlwarn[1] = 'W'; + } + } + } + break; + + case ECPGt_varchar: + { + struct ECPGgeneric_varchar *variable = + (struct ECPGgeneric_varchar *) ((long) var->value + var->offset * act_tuple); + + if (var->varcharsize == 0) + strncpy(variable->arr, pval, strlen(pval)); + else + strncpy(variable->arr, pval, var->varcharsize); + + variable->len = strlen(pval); + if (var->varcharsize > 0 && variable->len > var->varcharsize) + { + /* truncation */ + switch (var->ind_type) + { + case ECPGt_short: + case ECPGt_unsigned_short: + ((short *) var->ind_value)[act_tuple] = var->varcharsize; + break; + case ECPGt_int: + case ECPGt_unsigned_int: + ((int *) var->ind_value)[act_tuple] = var->varcharsize; + break; + case ECPGt_long: + case ECPGt_unsigned_long: + ((long *) var->ind_value)[act_tuple] = var->varcharsize; + break; + default: + break; + } + sqlca.sqlwarn[0] = sqlca.sqlwarn[1] = 'W'; + + variable->len = var->varcharsize; + } + } + break; + + default: + register_error(ECPG_UNSUPPORTED, "Unsupported type %s on line %d.", + ECPGtype_name(var->type), stmt->lineno); + status = false; + break; + } + } + var = var->next; } if (status && var != NULL) @@ -832,30 +842,30 @@ ECPGexecute(struct statement *stmt) } bool -ECPGdo(int lineno, char *query, ...) +ECPGdo(int lineno, char *query,...) { - va_list args; - struct statement *stmt; - + va_list args; + struct statement *stmt; + va_start(args, query); if (create_statement(lineno, &stmt, query, args) == false) - return(false); + return (false); va_end(args); /* are we connected? */ if (actual_connection == NULL || actual_connection->connection == NULL) - { + { ECPGlog("ECPGdo: not connected\n"); register_error(ECPG_NOT_CONN, "Not connected in line %d", lineno); return false; - } + } - return(ECPGexecute(stmt)); + return (ECPGexecute(stmt)); } bool -ECPGtrans(int lineno, const char * transaction) +ECPGtrans(int lineno, const char *transaction) { PGresult *res; @@ -876,9 +886,9 @@ ECPGsetconn(int lineno, const char *connection_name) { struct connection *con = all_connections; - ECPGlog("ECPGsetconn: setting actual connection to %s\n", connection_name); - - for (; con && strcmp(connection_name, con->name) != 0; con=con->next); + ECPGlog("ECPGsetconn: setting actual connection to %s\n", connection_name); + + for (; con && strcmp(connection_name, con->name) != 0; con = con->next); if (con) { actual_connection = con; @@ -892,43 +902,43 @@ ECPGsetconn(int lineno, const char *connection_name) } bool -ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd, const char * connection_name) +ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd, const char *connection_name) { struct connection *this = (struct connection *) ecpg_alloc(sizeof(struct connection), lineno); if (!this) return false; - + if (dbname == NULL && connection_name == NULL) connection_name = "DEFAULT"; - + /* add connection to our list */ if (connection_name != NULL) this->name = strdup(connection_name); else this->name = strdup(dbname); - + if (all_connections == NULL) this->next = NULL; else this->next = all_connections; actual_connection = all_connections = this; - - ECPGlog("ECPGconnect: opening database %s %s%s\n", dbname ? dbname : "NULL", user ? "for user ": "", user ? user : ""); + + ECPGlog("ECPGconnect: opening database %s %s%s\n", dbname ? dbname : "NULL", user ? "for user " : "", user ? user : ""); sqlca.sqlcode = 0; this->connection = PQsetdbLogin(NULL, NULL, NULL, NULL, dbname, user, passwd); - + if (PQstatus(this->connection) == CONNECTION_BAD) { ECPGfinish(this); - ECPGlog("connect: could not open database %s %s%s in line %d\n", dbname ? dbname : "NULL", user ? "for user ": "", user ? user : "", lineno); + ECPGlog("connect: could not open database %s %s%s in line %d\n", dbname ? dbname : "NULL", user ? "for user " : "", user ? user : "", lineno); register_error(ECPG_CONNECT, "connect: could not open database %s.", dbname ? dbname : "NULL"); return false; } - + return true; } @@ -936,7 +946,7 @@ bool ECPGdisconnect(int lineno, const char *connection_name) { struct connection *con; - + if (strcmp(connection_name, "CURRENT") == 0) ECPGfinish(actual_connection); else if (strcmp(connection_name, "ALL") == 0) @@ -944,26 +954,24 @@ ECPGdisconnect(int lineno, const char *connection_name) for (con = all_connections; con;) { struct connection *f = con; - + con = con->next; ECPGfinish(f); } } - else + else { - for (con = all_connections; con && strcmp(con->name, connection_name) != 0;con = con->next); + for (con = all_connections; con && strcmp(con->name, connection_name) != 0; con = con->next); if (con == NULL) - { + { ECPGlog("disconnect: not connected to connection %s\n", connection_name); register_error(ECPG_NO_CONN, "No such connection %s in line %d", connection_name, lineno); return false; } else - { ECPGfinish(con); - } } - + return true; } @@ -986,7 +994,7 @@ ECPGlog(const char *format,...) if (!f) return; - + sprintf(f, "[%d]: %s", getpid(), format); va_start(ap, format); diff --git a/src/interfaces/ecpg/lib/typename.c b/src/interfaces/ecpg/lib/typename.c index 9ef1d28252..0ee2a39cc9 100644 --- a/src/interfaces/ecpg/lib/typename.c +++ b/src/interfaces/ecpg/lib/typename.c @@ -8,7 +8,7 @@ ECPGtype_name(enum ECPGttype typ) { switch (typ) { - case ECPGt_char: + case ECPGt_char: return "char"; case ECPGt_unsigned_char: return "unsigned char"; diff --git a/src/interfaces/ecpg/preproc/ecpg.c b/src/interfaces/ecpg/preproc/ecpg.c index 69b1e650a2..6c07e303ec 100644 --- a/src/interfaces/ecpg/preproc/ecpg.c +++ b/src/interfaces/ecpg/preproc/ecpg.c @@ -9,8 +9,9 @@ #include <getopt.h> #else #include <unistd.h> -extern int optind; +extern int optind; extern char *optarg; + #endif #include <stdlib.h> #if defined(HAVE_STRING_H) @@ -22,7 +23,7 @@ extern char *optarg; #include "extern.h" struct _include_path *include_paths; -int no_auto_trans = 0; +int no_auto_trans = 0; struct cursor *cur = NULL; static void @@ -33,22 +34,24 @@ usage(char *progname) } static void -add_include_path(char * path) +add_include_path(char *path) { struct _include_path *ip = include_paths; - - include_paths = mm_alloc(sizeof(struct _include_path)); - include_paths->path = path; - include_paths->next = ip; + + include_paths = mm_alloc(sizeof(struct _include_path)); + include_paths->path = path; + include_paths->next = ip; } int main(int argc, char *const argv[]) { - int fnr, c, out_option = 0; - struct _include_path *ip; - - add_include_path("/usr/include"); + int fnr, + c, + out_option = 0; + struct _include_path *ip; + + add_include_path("/usr/include"); add_include_path(INCLUDE_PATH); add_include_path("/usr/local/include"); add_include_path("."); @@ -66,10 +69,10 @@ main(int argc, char *const argv[]) break; case 'I': add_include_path(optarg); - break; - case 't': - no_auto_trans = 1; - break; + break; + case 't': + no_auto_trans = 1; + break; case 'v': fprintf(stderr, "ecpg - the postgresql preprocessor, version: %d.%d.%d\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL); fprintf(stderr, "exec sql include ... search starts here:\n"); @@ -86,14 +89,15 @@ main(int argc, char *const argv[]) if (optind >= argc) /* no files specified */ { usage(argv[0]); - return(ILLEGAL_OPTION); + return (ILLEGAL_OPTION); } else { /* after the options there must not be anything but filenames */ for (fnr = optind; fnr < argc; fnr++) { - char *output_filename = NULL, *ptr2ext; + char *output_filename = NULL, + *ptr2ext; input_filename = mm_alloc(strlen(argv[fnr]) + 5); @@ -104,7 +108,7 @@ main(int argc, char *const argv[]) if (ptr2ext == NULL) { ptr2ext = input_filename + strlen(input_filename); - + /* no extension => add .pgc */ ptr2ext[0] = '.'; ptr2ext[1] = 'p'; @@ -116,12 +120,12 @@ main(int argc, char *const argv[]) if (out_option == 0)/* calculate the output name */ { output_filename = strdup(input_filename); - + ptr2ext = strrchr(output_filename, '.'); /* make extension = .c */ ptr2ext[1] = 'c'; ptr2ext[2] = '\0'; - + yyout = fopen(output_filename, "w"); if (yyout == NULL) { @@ -139,13 +143,14 @@ main(int argc, char *const argv[]) { struct cursor *ptr; struct _defines *defptr; - + /* remove old cursor definitions if any are still there */ for (ptr = cur; ptr != NULL;) { struct cursor *this = ptr; - struct arguments *l1, *l2; - + struct arguments *l1, + *l2; + free(ptr->command); free(ptr->name); for (l1 = argsinsert; l1; l1 = l2) @@ -161,20 +166,21 @@ main(int argc, char *const argv[]) ptr = ptr->next; free(this); } - + /* remove old defines as well */ for (defptr = defines; defptr != NULL;) { struct _defines *this = defptr; + free(defptr->new); free(defptr->old); defptr = defptr->next; free(this); } - + /* initialize lex */ lex_init(); - + /* we need two includes and a constant */ fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/* These two include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL); @@ -189,7 +195,7 @@ main(int argc, char *const argv[]) if (output_filename) free(output_filename); - + free(input_filename); } } diff --git a/src/interfaces/ecpg/preproc/ecpg_keywords.c b/src/interfaces/ecpg/preproc/ecpg_keywords.c index f8faba2670..77d7eaacc0 100644 --- a/src/interfaces/ecpg/preproc/ecpg_keywords.c +++ b/src/interfaces/ecpg/preproc/ecpg_keywords.c @@ -30,8 +30,8 @@ static ScanKeyword ScanKeywords[] = { {"found", SQL_FOUND}, {"go", SQL_GO}, {"goto", SQL_GOTO}, - {"identified", SQL_IDENTIFIED}, - {"immediate", SQL_IMMEDIATE}, + {"identified", SQL_IDENTIFIED}, + {"immediate", SQL_IMMEDIATE}, {"indicator", SQL_INDICATOR}, {"open", SQL_OPEN}, {"release", SQL_RELEASE}, diff --git a/src/interfaces/ecpg/preproc/extern.h b/src/interfaces/ecpg/preproc/extern.h index 1f25d0824d..86ee536121 100644 --- a/src/interfaces/ecpg/preproc/extern.h +++ b/src/interfaces/ecpg/preproc/extern.h @@ -3,55 +3,63 @@ /* variables */ -extern int braces_open, no_auto_trans; +extern int braces_open, + no_auto_trans; extern char *yytext; extern int yylineno, yyleng; extern FILE *yyin, *yyout; -struct _include_path { char * path; - struct _include_path * next; - }; +struct _include_path +{ + char *path; + struct _include_path *next; +}; extern struct _include_path *include_paths; -struct cursor { char *name; - char *command; - struct arguments * argsinsert; - struct arguments * argsresult; - struct cursor *next; - }; - +struct cursor +{ + char *name; + char *command; + struct arguments *argsinsert; + struct arguments *argsresult; + struct cursor *next; +}; + extern struct cursor *cur; -struct _defines { char *old; - char *new; - struct _defines *next; - }; +struct _defines +{ + char *old; + char *new; + struct _defines *next; +}; extern struct _defines *defines; - + /* This is a linked list of the variable names and types. */ struct variable { - char * name; - struct ECPGtype * type; - int brace_level; - struct variable * next; + char *name; + struct ECPGtype *type; + int brace_level; + struct variable *next; }; - + extern struct ECPGtype ecpg_no_indicator; extern struct variable no_indicator; -struct arguments { - struct variable * variable; - struct variable * indicator; - struct arguments * next; +struct arguments +{ + struct variable *variable; + struct variable *indicator; + struct arguments *next; }; -extern struct arguments * argsinsert; -extern struct arguments * argsresult; +extern struct arguments *argsinsert; +extern struct arguments *argsresult; /* functions */ @@ -59,15 +67,15 @@ extern void lex_init(void); extern char *input_filename; extern int yyparse(void); extern void *mm_alloc(size_t), *mm_realloc(void *, size_t); -ScanKeyword * ScanECPGKeywordLookup(char *); -ScanKeyword * ScanCKeywordLookup(char *); +ScanKeyword *ScanECPGKeywordLookup(char *); +ScanKeyword *ScanCKeywordLookup(char *); extern void yyerror(char *); /* return codes */ #define OK 0 -#define PARSE_ERROR -1 +#define PARSE_ERROR -1 #define ILLEGAL_OPTION -2 -#define NO_INCLUDE_FILE ENOENT +#define NO_INCLUDE_FILE ENOENT #define OUT_OF_MEMORY ENOMEM diff --git a/src/interfaces/ecpg/preproc/type.c b/src/interfaces/ecpg/preproc/type.c index cf115ea02d..73ec51e21e 100644 --- a/src/interfaces/ecpg/preproc/type.c +++ b/src/interfaces/ecpg/preproc/type.c @@ -24,31 +24,31 @@ mm_alloc(size_t size) static struct ECPGstruct_member * struct_member_dup(struct ECPGstruct_member * rm) { - struct ECPGstruct_member *new = NULL; - - while (rm) - { - struct ECPGtype *type; - - switch(rm->typ->typ) - { - case ECPGt_struct: - type = ECPGmake_struct_type(rm->typ->u.members); - break; - case ECPGt_array: - type = ECPGmake_array_type(ECPGmake_simple_type(rm->typ->u.element->typ, rm->typ->u.element->size), rm->typ->size); - break; - default: - type = ECPGmake_simple_type(rm->typ->typ, rm->typ->size); - break; - } - - ECPGmake_struct_member(rm->name, type, &new); - - rm = rm->next; - } - - return(new); + struct ECPGstruct_member *new = NULL; + + while (rm) + { + struct ECPGtype *type; + + switch (rm->typ->typ) + { + case ECPGt_struct: + type = ECPGmake_struct_type(rm->typ->u.members); + break; + case ECPGt_array: + type = ECPGmake_array_type(ECPGmake_simple_type(rm->typ->u.element->typ, rm->typ->u.element->size), rm->typ->size); + break; + default: + type = ECPGmake_simple_type(rm->typ->typ, rm->typ->size); + break; + } + + ECPGmake_struct_member(rm->name, type, &new); + + rm = rm->next; + } + + return (new); } /* The NAME argument is copied. The type argument is preserved as a pointer. */ @@ -103,51 +103,52 @@ ECPGmake_struct_type(struct ECPGstruct_member * rm) return ne; } -static const char *get_type(enum ECPGttype typ) +static const char * +get_type(enum ECPGttype typ) { - switch (typ) - { - case ECPGt_char: - return("ECPGt_char"); - break; - case ECPGt_unsigned_char: - return("ECPGt_unsigned_char"); - break; - case ECPGt_short: - return("ECPGt_short"); - break; - case ECPGt_unsigned_short: - return("ECPGt_unsigned_short"); - break; - case ECPGt_int: - return("ECPGt_int"); - break; - case ECPGt_unsigned_int: - return("ECPGt_unsigned_int"); - break; - case ECPGt_long: - return("ECPGt_long"); - break; - case ECPGt_unsigned_long: - return("ECPGt_unsigned_int"); - break; - case ECPGt_float: - return("ECPGt_float"); - break; - case ECPGt_double: - return("ECPGt_double"); - break; - case ECPGt_bool: - return("ECPGt_bool"); - break; - case ECPGt_varchar: - return("ECPGt_varchar"); - case ECPGt_NO_INDICATOR: /* no indicator */ - return("ECPGt_NO_INDICATOR"); - break; - default: - abort(); - } + switch (typ) + { + case ECPGt_char: + return ("ECPGt_char"); + break; + case ECPGt_unsigned_char: + return ("ECPGt_unsigned_char"); + break; + case ECPGt_short: + return ("ECPGt_short"); + break; + case ECPGt_unsigned_short: + return ("ECPGt_unsigned_short"); + break; + case ECPGt_int: + return ("ECPGt_int"); + break; + case ECPGt_unsigned_int: + return ("ECPGt_unsigned_int"); + break; + case ECPGt_long: + return ("ECPGt_long"); + break; + case ECPGt_unsigned_long: + return ("ECPGt_unsigned_int"); + break; + case ECPGt_float: + return ("ECPGt_float"); + break; + case ECPGt_double: + return ("ECPGt_double"); + break; + case ECPGt_bool: + return ("ECPGt_bool"); + break; + case ECPGt_varchar: + return ("ECPGt_varchar"); + case ECPGt_NO_INDICATOR: /* no indicator */ + return ("ECPGt_NO_INDICATOR"); + break; + default: + abort(); + } } /* Dump a type. @@ -164,13 +165,11 @@ static const char *get_type(enum ECPGttype typ) size is the maxsize in case it is a varchar. Otherwise it is the size of the variable (required to do array fetches of structs). */ -static void -ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype typ, +static void ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype typ, long varcharsize, long arrsiz, const char *siz, const char *prefix); -static void -ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, long arrsiz, - struct ECPGtype * typ, struct ECPGtype * ind_typ, const char *offset, const char *prefix, const char * ind_prefix); +static void ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, long arrsiz, + struct ECPGtype * typ, struct ECPGtype * ind_typ, const char *offset, const char *prefix, const char *ind_prefix); void @@ -181,41 +180,39 @@ ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype * typ, const char *in ind_typ = &ecpg_no_indicator; ind_name = "no_indicator"; } - - switch(typ->typ) - { - case ECPGt_array: - if (IS_SIMPLE_TYPE(typ->u.element->typ)) - { - ECPGdump_a_simple(o, name, typ->u.element->typ, - typ->u.element->size, typ->size, NULL, prefix); - if (ind_typ == &ecpg_no_indicator) - ECPGdump_a_simple(o, ind_name, ind_typ->typ, ind_typ->size, -1, NULL, ind_prefix); - else - ECPGdump_a_simple(o, ind_name, ind_typ->u.element->typ, - ind_typ->u.element->size, ind_typ->size, NULL, prefix); - } - else if (typ->u.element->typ == ECPGt_array) - { - yyerror("No nested arrays allowed (except strings)"); /* Array of array, */ - } - else if (typ->u.element->typ == ECPGt_struct) - { - /* Array of structs. */ - ECPGdump_a_struct(o, name, ind_name, typ->size, typ->u.element, ind_typ->u.element, NULL, prefix, ind_prefix); - } - else - { - yyerror("Internal error: unknown datatype, pleqase inform pgsql-bugs@postgresql.org"); - } - break; - case ECPGt_struct: - ECPGdump_a_struct(o, name, ind_name, 1, typ, ind_typ, NULL, prefix, ind_prefix); - break; - default: - ECPGdump_a_simple(o, name, typ->typ, typ->size, -1, NULL, prefix); - ECPGdump_a_simple(o, ind_name, ind_typ->typ, ind_typ->size, -1, NULL, ind_prefix); - break; + + switch (typ->typ) + { + case ECPGt_array: + if (IS_SIMPLE_TYPE(typ->u.element->typ)) + { + ECPGdump_a_simple(o, name, typ->u.element->typ, + typ->u.element->size, typ->size, NULL, prefix); + if (ind_typ == &ecpg_no_indicator) + ECPGdump_a_simple(o, ind_name, ind_typ->typ, ind_typ->size, -1, NULL, ind_prefix); + else + ECPGdump_a_simple(o, ind_name, ind_typ->u.element->typ, + ind_typ->u.element->size, ind_typ->size, NULL, prefix); + } + else if (typ->u.element->typ == ECPGt_array) + { + yyerror("No nested arrays allowed (except strings)"); /* Array of array, */ + } + else if (typ->u.element->typ == ECPGt_struct) + { + /* Array of structs. */ + ECPGdump_a_struct(o, name, ind_name, typ->size, typ->u.element, ind_typ->u.element, NULL, prefix, ind_prefix); + } + else + yyerror("Internal error: unknown datatype, pleqase inform pgsql-bugs@postgresql.org"); + break; + case ECPGt_struct: + ECPGdump_a_struct(o, name, ind_name, 1, typ, ind_typ, NULL, prefix, ind_prefix); + break; + default: + ECPGdump_a_simple(o, name, typ->typ, typ->size, -1, NULL, prefix); + ECPGdump_a_simple(o, ind_name, ind_typ->typ, ind_typ->size, -1, NULL, ind_prefix); + break; } } @@ -228,51 +225,51 @@ ECPGdump_a_simple(FILE *o, const char *name, enum ECPGttype typ, long arrsize, const char *siz, const char *prefix - ) +) { - if (typ == ECPGt_NO_INDICATOR) - fprintf(o, "\n\tECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, "); - else - { - char *variable = (char *)mm_alloc(strlen(name) + ((prefix == NULL) ? 0 : strlen(prefix)) + 4); - char *offset = (char *)mm_alloc(strlen(name) + strlen("sizeof(struct varchar_)") + 1); - - if (varcharsize == 0 || arrsize >= 0) - sprintf(variable, "(%s%s)", prefix ? prefix : "", name); - else - sprintf(variable, "&(%s%s)", prefix ? prefix : "", name); - - switch (typ) - { - case ECPGt_varchar: - sprintf(offset, "sizeof(struct varchar_%s)", name); - break; - case ECPGt_char: - case ECPGt_unsigned_char: - sprintf(offset, "%ld*sizeof(char)", varcharsize); - break; - default: - sprintf(offset, "sizeof(%s)", ECPGtype_name(typ)); - break; + if (typ == ECPGt_NO_INDICATOR) + fprintf(o, "\n\tECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, "); + else + { + char *variable = (char *) mm_alloc(strlen(name) + ((prefix == NULL) ? 0 : strlen(prefix)) + 4); + char *offset = (char *) mm_alloc(strlen(name) + strlen("sizeof(struct varchar_)") + 1); + + if (varcharsize == 0 || arrsize >= 0) + sprintf(variable, "(%s%s)", prefix ? prefix : "", name); + else + sprintf(variable, "&(%s%s)", prefix ? prefix : "", name); + + switch (typ) + { + case ECPGt_varchar: + sprintf(offset, "sizeof(struct varchar_%s)", name); + break; + case ECPGt_char: + case ECPGt_unsigned_char: + sprintf(offset, "%ld*sizeof(char)", varcharsize); + break; + default: + sprintf(offset, "sizeof(%s)", ECPGtype_name(typ)); + break; + } + + if (arrsize < 0) + arrsize = 1; + + if (siz == NULL) + fprintf(o, "\n\t%s,%s,%ldL,%ldL,%s, ", get_type(typ), variable, varcharsize, arrsize, offset); + else + fprintf(o, "\n\t%s,%s,%ldL,%ldL,%s, ", get_type(typ), variable, varcharsize, arrsize, siz); + + free(variable); + free(offset); } - - if (arrsize < 0) - arrsize = 1; - - if (siz == NULL) - fprintf(o, "\n\t%s,%s,%ldL,%ldL,%s, ", get_type(typ), variable, varcharsize, arrsize, offset); - else - fprintf(o, "\n\t%s,%s,%ldL,%ldL,%s, ", get_type(typ), variable, varcharsize, arrsize, siz); - - free(variable); - free(offset); - } } /* Penetrate a struct and dump the contents. */ static void -ECPGdump_a_struct(FILE *o, const char *name, const char * ind_name, long arrsiz, struct ECPGtype * typ, struct ECPGtype * ind_typ, const char *offsetarg, const char *prefix, const char *ind_prefix) +ECPGdump_a_struct(FILE *o, const char *name, const char *ind_name, long arrsiz, struct ECPGtype * typ, struct ECPGtype * ind_typ, const char *offsetarg, const char *prefix, const char *ind_prefix) { /* @@ -280,9 +277,11 @@ ECPGdump_a_struct(FILE *o, const char *name, const char * ind_name, long arrsiz, * then we are in a struct in a struct and the offset is used as * offset. */ - struct ECPGstruct_member *p, *ind_p = NULL; + struct ECPGstruct_member *p, + *ind_p = NULL; char obuf[BUFSIZ]; - char pbuf[BUFSIZ], ind_pbuf[BUFSIZ]; + char pbuf[BUFSIZ], + ind_pbuf[BUFSIZ]; const char *offset; if (offsetarg == NULL) @@ -291,36 +290,36 @@ ECPGdump_a_struct(FILE *o, const char *name, const char * ind_name, long arrsiz, offset = obuf; } else - { offset = offsetarg; - } sprintf(pbuf, "%s%s.", prefix ? prefix : "", name); prefix = pbuf; - + sprintf(ind_pbuf, "%s%s.", ind_prefix ? ind_prefix : "", ind_name); ind_prefix = ind_pbuf; - if (ind_typ != NULL) ind_p = ind_typ->u.members; + if (ind_typ != NULL) + ind_p = ind_typ->u.members; for (p = typ->u.members; p; p = p->next) { ECPGdump_a_type(o, p->name, p->typ, (ind_p != NULL) ? ind_p->name : NULL, (ind_p != NULL) ? ind_p->typ : NULL, prefix, ind_prefix); - if (ind_p != NULL) ind_p = ind_p->next; + if (ind_p != NULL) + ind_p = ind_p->next; } } void ECPGfree_struct_member(struct ECPGstruct_member * rm) { - while (rm) - { - struct ECPGstruct_member *p = rm; - - rm = rm->next; - free(p->name); - free(p->typ); - free(p); - } + while (rm) + { + struct ECPGstruct_member *p = rm; + + rm = rm->next; + free(p->name); + free(p->typ); + free(p); + } } void @@ -337,7 +336,7 @@ ECPGfree_type(struct ECPGtype * typ) else if (typ->u.element->typ == ECPGt_struct) { /* Array of structs. */ - ECPGfree_struct_member(typ->u.element->u.members); + ECPGfree_struct_member(typ->u.element->u.members); free(typ->u.members); } else @@ -345,13 +344,11 @@ ECPGfree_type(struct ECPGtype * typ) } else if (typ->typ == ECPGt_struct) { - ECPGfree_struct_member(typ->u.members); + ECPGfree_struct_member(typ->u.members); free(typ->u.members); } else - { abort(); - } } free(typ); } diff --git a/src/interfaces/ecpg/preproc/type.h b/src/interfaces/ecpg/preproc/type.h index 835f50b359..dd7e8e3758 100644 --- a/src/interfaces/ecpg/preproc/type.h +++ b/src/interfaces/ecpg/preproc/type.h @@ -25,7 +25,7 @@ struct ECPGtype }; /* Everything is malloced. */ -void ECPGmake_struct_member(char *, struct ECPGtype *, struct ECPGstruct_member **); +void ECPGmake_struct_member(char *, struct ECPGtype *, struct ECPGstruct_member **); struct ECPGtype *ECPGmake_simple_type(enum ECPGttype, long); struct ECPGtype *ECPGmake_varchar_type(enum ECPGttype, long); struct ECPGtype *ECPGmake_array_type(struct ECPGtype *, long); @@ -45,7 +45,7 @@ void ECPGfree_type(struct ECPGtype *); size is the maxsize in case it is a varchar. Otherwise it is the size of the variable (required to do array fetches of structs). */ -void ECPGdump_a_type(FILE *, const char *, struct ECPGtype *, const char *, struct ECPGtype *, const char *, const char *); +void ECPGdump_a_type(FILE *, const char *, struct ECPGtype *, const char *, struct ECPGtype *, const char *, const char *); /* A simple struct to keep a variable and its type. */ struct ECPGtemp_type @@ -71,19 +71,19 @@ enum WHEN struct when { enum WHEN code; - char *command; - char *str; + char *command; + char *str; }; struct index { - int index1; - int index2; - char *str; + int index1; + int index2; + char *str; }; struct this_type { - enum ECPGttype type_enum; - char *type_str; + enum ECPGttype type_enum; + char *type_str; }; diff --git a/src/interfaces/libpgtcl/libpgtcl.h b/src/interfaces/libpgtcl/libpgtcl.h index 7dfec8f581..fc021d56dc 100644 --- a/src/interfaces/libpgtcl/libpgtcl.h +++ b/src/interfaces/libpgtcl/libpgtcl.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: libpgtcl.h,v 1.4 1997/09/08 02:40:03 momjian Exp $ + * $Id: libpgtcl.h,v 1.5 1998/09/01 04:39:53 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -19,4 +19,4 @@ extern int Pgtcl_Init(Tcl_Interp * interp); extern int Pgtcl_SafeInit(Tcl_Interp * interp); -#endif /* LIBPGTCL_H */ +#endif /* LIBPGTCL_H */ diff --git a/src/interfaces/libpgtcl/pgtcl.c b/src/interfaces/libpgtcl/pgtcl.c index af6f4d74af..e500f12ccd 100644 --- a/src/interfaces/libpgtcl/pgtcl.c +++ b/src/interfaces/libpgtcl/pgtcl.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtcl.c,v 1.11 1998/06/16 04:10:15 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtcl.c,v 1.12 1998/09/01 04:39:55 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -23,117 +23,118 @@ #include "pgtclId.h" /* - * Pgtcl_Init - * initialization package for the PGLITE Tcl package + * Pgtcl_Init + * initialization package for the PGLITE Tcl package * */ int -Pgtcl_Init (Tcl_Interp *interp) +Pgtcl_Init(Tcl_Interp * interp) { - /* finish off the ChannelType struct. Much easier to do it here then - * to guess where it might be by position in the struct. This is needed - * for Tcl7.6 and beyond, which have the getfileproc. - */ + /* + * finish off the ChannelType struct. Much easier to do it here then + * to guess where it might be by position in the struct. This is + * needed for Tcl7.6 and beyond, which have the getfileproc. + */ #if HAVE_TCL_GETFILEPROC - Pg_ConnType.getFileProc = PgGetFileProc; + Pg_ConnType.getFileProc = PgGetFileProc; #endif - /* register all pgtcl commands */ - Tcl_CreateCommand(interp, - "pg_conndefaults", - Pg_conndefaults, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_connect", - Pg_connect, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_disconnect", - Pg_disconnect, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_exec", - Pg_exec, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_select", - Pg_select, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_result", - Pg_result, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_open", - Pg_lo_open, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_close", - Pg_lo_close, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_read", - Pg_lo_read, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_write", - Pg_lo_write, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_lseek", - Pg_lo_lseek, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_creat", - Pg_lo_creat, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_tell", - Pg_lo_tell, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_unlink", - Pg_lo_unlink, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_import", - Pg_lo_import, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_lo_export", - Pg_lo_export, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_CreateCommand(interp, - "pg_listen", - Pg_listen, - (ClientData)NULL, (Tcl_CmdDeleteProc*)NULL); - - Tcl_PkgProvide(interp, "Pgtcl", "1.2"); - - return TCL_OK; + /* register all pgtcl commands */ + Tcl_CreateCommand(interp, + "pg_conndefaults", + Pg_conndefaults, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_connect", + Pg_connect, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_disconnect", + Pg_disconnect, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_exec", + Pg_exec, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_select", + Pg_select, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_result", + Pg_result, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_open", + Pg_lo_open, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_close", + Pg_lo_close, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_read", + Pg_lo_read, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_write", + Pg_lo_write, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_lseek", + Pg_lo_lseek, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_creat", + Pg_lo_creat, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_tell", + Pg_lo_tell, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_unlink", + Pg_lo_unlink, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_import", + Pg_lo_import, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_lo_export", + Pg_lo_export, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_CreateCommand(interp, + "pg_listen", + Pg_listen, + (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); + + Tcl_PkgProvide(interp, "Pgtcl", "1.2"); + + return TCL_OK; } int -Pgtcl_SafeInit (Tcl_Interp *interp) +Pgtcl_SafeInit(Tcl_Interp * interp) { - return Pgtcl_Init(interp); + return Pgtcl_Init(interp); } diff --git a/src/interfaces/libpgtcl/pgtclCmds.c b/src/interfaces/libpgtcl/pgtclCmds.c index b442dfe242..bd6641554d 100644 --- a/src/interfaces/libpgtcl/pgtclCmds.c +++ b/src/interfaces/libpgtcl/pgtclCmds.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.30 1998/09/01 03:28:47 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclCmds.c,v 1.31 1998/09/01 04:39:56 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -225,236 +225,258 @@ tcl_value(char *value) /********************************** * pg_conndefaults - + syntax: pg_conndefaults - + the return result is a list describing the possible options and their current default values for a call to pg_connect with the new -conninfo syntax. Each entry in the list is a sublist of the format: - {optname label dispchar dispsize value} - + {optname label dispchar dispsize value} + **********************************/ int -Pg_conndefaults(ClientData cData, Tcl_Interp *interp, int argc, char **argv) +Pg_conndefaults(ClientData cData, Tcl_Interp * interp, int argc, char **argv) { - PQconninfoOption *option; - char buf[8192]; + PQconninfoOption *option; + char buf[8192]; - Tcl_ResetResult(interp); - for(option = PQconndefaults(); option->keyword != NULL; option++) { - if(option->val == NULL) { - option->val = ""; + Tcl_ResetResult(interp); + for (option = PQconndefaults(); option->keyword != NULL; option++) + { + if (option->val == NULL) + option->val = ""; + sprintf(buf, "{%s} {%s} {%s} %d {%s}", + option->keyword, + option->label, + option->dispchar, + option->dispsize, + option->val); + Tcl_AppendElement(interp, buf); } - sprintf(buf, "{%s} {%s} {%s} %d {%s}", - option->keyword, - option->label, - option->dispchar, - option->dispsize, - option->val); - Tcl_AppendElement(interp, buf); - } - - return TCL_OK; + + return TCL_OK; } /********************************** * pg_connect - make a connection to a backend. - + make a connection to a backend. + syntax: pg_connect dbName [-host hostName] [-port portNumber] [-tty pqtty]] - + the return result is either an error message or a handle for a database connection. Handles start with the prefix "pgp" - + **********************************/ int -Pg_connect(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_connect(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - char *pghost = NULL; - char *pgtty = NULL; - char *pgport = NULL; - char *pgoptions = NULL; - char *dbName; - int i; - PGconn *conn; - - if (argc == 1) { - Tcl_AppendResult(interp, "pg_connect: database name missing\n", 0); - Tcl_AppendResult(interp, "pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]\n", 0); - Tcl_AppendResult(interp, "pg_connect -conninfo <conninfo-string>", 0); - return TCL_ERROR; - - } + char *pghost = NULL; + char *pgtty = NULL; + char *pgport = NULL; + char *pgoptions = NULL; + char *dbName; + int i; + PGconn *conn; + + if (argc == 1) + { + Tcl_AppendResult(interp, "pg_connect: database name missing\n", 0); + Tcl_AppendResult(interp, "pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]\n", 0); + Tcl_AppendResult(interp, "pg_connect -conninfo <conninfo-string>", 0); + return TCL_ERROR; - if (!strcmp("-conninfo", argv[1])) { - /* - * Establish a connection using the new PQconnectdb() interface - */ - if (argc != 3) { - Tcl_AppendResult(interp, "pg_connect: syntax error\n", 0); - Tcl_AppendResult(interp, "pg_connect -conninfo <conninfo-string>", 0); - return TCL_ERROR; } - conn = PQconnectdb(argv[2]); - } else { - /* - * Establish a connection using the old PQsetdb() interface - */ - if (argc > 2) { - /* parse for pg environment settings */ - i = 2; - while (i+1 < argc) { - if (strcmp(argv[i], "-host") == 0) { - pghost = argv[i+1]; - i += 2; + + if (!strcmp("-conninfo", argv[1])) + { + + /* + * Establish a connection using the new PQconnectdb() interface + */ + if (argc != 3) + { + Tcl_AppendResult(interp, "pg_connect: syntax error\n", 0); + Tcl_AppendResult(interp, "pg_connect -conninfo <conninfo-string>", 0); + return TCL_ERROR; } - else - if (strcmp(argv[i], "-port") == 0) { - pgport = argv[i+1]; - i += 2; - } - else - if (strcmp(argv[i], "-tty") == 0) { - pgtty = argv[i+1]; - i += 2; - } - else if (strcmp(argv[i], "-options") == 0) { - pgoptions = argv[i+1]; - i += 2; - } - else { - Tcl_AppendResult(interp, "Bad option to pg_connect : \n", - argv[i], 0); - Tcl_AppendResult(interp, "pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]",0); - return TCL_ERROR; + conn = PQconnectdb(argv[2]); + } + else + { + + /* + * Establish a connection using the old PQsetdb() interface + */ + if (argc > 2) + { + /* parse for pg environment settings */ + i = 2; + while (i + 1 < argc) + { + if (strcmp(argv[i], "-host") == 0) + { + pghost = argv[i + 1]; + i += 2; + } + else if (strcmp(argv[i], "-port") == 0) + { + pgport = argv[i + 1]; + i += 2; + } + else if (strcmp(argv[i], "-tty") == 0) + { + pgtty = argv[i + 1]; + i += 2; + } + else if (strcmp(argv[i], "-options") == 0) + { + pgoptions = argv[i + 1]; + i += 2; + } + else + { + Tcl_AppendResult(interp, "Bad option to pg_connect : \n", + argv[i], 0); + Tcl_AppendResult(interp, "pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]", 0); + return TCL_ERROR; + } + } /* while */ + if ((i % 2 != 0) || i != argc) + { + Tcl_AppendResult(interp, "wrong # of arguments to pg_connect\n", argv[i], 0); + Tcl_AppendResult(interp, "pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]", 0); + return TCL_ERROR; } - } /* while */ - if ((i % 2 != 0) || i != argc) { - Tcl_AppendResult(interp, "wrong # of arguments to pg_connect\n", argv[i],0); - Tcl_AppendResult(interp, "pg_connect databaseName [-host hostName] [-port portNumber] [-tty pgtty]]",0); - return TCL_ERROR; - } + } + dbName = argv[1]; + conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); } - dbName = argv[1]; - conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); - } - if (conn->status == CONNECTION_OK) { - PgSetConnectionId(interp, conn); - return TCL_OK; - } - else { - Tcl_AppendResult(interp, "Connection to database failed\n", 0); - Tcl_AppendResult(interp, conn->errorMessage, 0); - PQfinish(conn); - return TCL_ERROR; - } + if (conn->status == CONNECTION_OK) + { + PgSetConnectionId(interp, conn); + return TCL_OK; + } + else + { + Tcl_AppendResult(interp, "Connection to database failed\n", 0); + Tcl_AppendResult(interp, conn->errorMessage, 0); + PQfinish(conn); + return TCL_ERROR; + } } /********************************** * pg_disconnect close a backend connection - + syntax: pg_disconnect connection - + The argument passed in must be a connection pointer. - + **********************************/ int -Pg_disconnect(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_disconnect(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - Tcl_Channel conn_chan; + Tcl_Channel conn_chan; - if (argc != 2) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", "pg_disconnect connection", 0); - return TCL_ERROR; - } + if (argc != 2) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", "pg_disconnect connection", 0); + return TCL_ERROR; + } - conn_chan = Tcl_GetChannel(interp, argv[1], 0); - if (conn_chan == NULL) { - Tcl_ResetResult(interp); - Tcl_AppendResult(interp, argv[1], " is not a valid connection\n", 0); - return TCL_ERROR; - } + conn_chan = Tcl_GetChannel(interp, argv[1], 0); + if (conn_chan == NULL) + { + Tcl_ResetResult(interp); + Tcl_AppendResult(interp, argv[1], " is not a valid connection\n", 0); + return TCL_ERROR; + } - return Tcl_UnregisterChannel(interp, conn_chan); + return Tcl_UnregisterChannel(interp, conn_chan); } /********************************** * pg_exec send a query string to the backend connection - + syntax: pg_exec connection query - + the return result is either an error message or a handle for a query result. Handles start with the prefix "pgp" **********************************/ int -Pg_exec(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_exec(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - Pg_ConnectionId *connid; - PGconn *conn; - PGresult *result; - int connStatus; - - if (argc != 3) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_exec connection queryString", 0); - return TCL_ERROR; - } + Pg_ConnectionId *connid; + PGconn *conn; + PGresult *result; + int connStatus; - conn = PgGetConnectionId(interp, argv[1], &connid); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } + if (argc != 3) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_exec connection queryString", 0); + return TCL_ERROR; + } - if (connid->res_copyStatus != RES_COPY_NONE) { - Tcl_SetResult(interp, "Attempt to query while COPY in progress", TCL_STATIC); - return TCL_ERROR; - } + conn = PgGetConnectionId(interp, argv[1], &connid); + if (conn == (PGconn *) NULL) + return TCL_ERROR; + + if (connid->res_copyStatus != RES_COPY_NONE) + { + Tcl_SetResult(interp, "Attempt to query while COPY in progress", TCL_STATIC); + return TCL_ERROR; + } - connStatus = conn->status; - result = PQexec(conn, argv[2]); + connStatus = conn->status; + result = PQexec(conn, argv[2]); - /* Transfer any notify events from libpq to Tcl event queue. */ - PgNotifyTransferEvents(connid); + /* Transfer any notify events from libpq to Tcl event queue. */ + PgNotifyTransferEvents(connid); - if (result) { - int rId = PgSetResultId(interp, argv[1], result); - if (result->resultStatus == PGRES_COPY_IN || - result->resultStatus == PGRES_COPY_OUT) { - connid->res_copyStatus = RES_COPY_INPROGRESS; - connid->res_copy = rId; + if (result) + { + int rId = PgSetResultId(interp, argv[1], result); + + if (result->resultStatus == PGRES_COPY_IN || + result->resultStatus == PGRES_COPY_OUT) + { + connid->res_copyStatus = RES_COPY_INPROGRESS; + connid->res_copy = rId; + } + return TCL_OK; + } + else + { + /* error occurred during the query */ + Tcl_SetResult(interp, conn->errorMessage, TCL_VOLATILE); + return TCL_ERROR; } - return TCL_OK; - } - else { - /* error occurred during the query */ - Tcl_SetResult(interp, conn->errorMessage, TCL_VOLATILE); - return TCL_ERROR; - } } /********************************** * pg_result get information about the results of a query - + syntax: - pg_result result ?option? - + pg_result result ?option? + the options are: - -status + -status the status of the result -conn the connection that produced the result @@ -475,302 +497,332 @@ Pg_exec(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) -tupleArray tupleNumber arrayName stores the values of the tuple in array arrayName, indexed by the attributes returned - -clear + -clear clear the result buffer. Do not reuse after this **********************************/ int -Pg_result(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_result(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGresult *result; - char *opt; - int i; - int tupno; - char *arrVar; - char nameBuffer[256]; - - if (argc < 3 || argc > 5) { - Tcl_AppendResult(interp, "Wrong # of arguments\n",0); - goto Pg_result_errReturn; - } - - result = PgGetResultId(interp, argv[1]); - if (result == (PGresult *)NULL) { - Tcl_AppendResult(interp, "First argument is not a valid query result", 0); - return TCL_ERROR; - } + PGresult *result; + char *opt; + int i; + int tupno; + char *arrVar; + char nameBuffer[256]; + + if (argc < 3 || argc > 5) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", 0); + goto Pg_result_errReturn; + } - opt = argv[2]; + result = PgGetResultId(interp, argv[1]); + if (result == (PGresult *) NULL) + { + Tcl_AppendResult(interp, "First argument is not a valid query result", 0); + return TCL_ERROR; + } - if (strcmp(opt, "-status") == 0) { - Tcl_AppendResult(interp, pgresStatus[PQresultStatus(result)], 0); - return TCL_OK; - } - else if (strcmp(opt, "-oid") == 0) { - Tcl_AppendResult(interp, PQoidStatus(result), 0); - return TCL_OK; - } - else if (strcmp(opt, "-conn") == 0) { - return PgGetConnByResultId(interp, argv[1]); - } - else if (strcmp(opt, "-clear") == 0) { - PgDelResultId(interp, argv[1]); - PQclear(result); - return TCL_OK; - } - else if (strcmp(opt, "-numTuples") == 0) { - sprintf(interp->result, "%d", PQntuples(result)); - return TCL_OK; - } - else if (strcmp(opt, "-numAttrs") == 0) { - sprintf(interp->result, "%d", PQnfields(result)); - return TCL_OK; - } - else if (strcmp(opt, "-assign") == 0) { - if (argc != 4) { - Tcl_AppendResult(interp, "-assign option must be followed by a variable name",0); - return TCL_ERROR; + opt = argv[2]; + + if (strcmp(opt, "-status") == 0) + { + Tcl_AppendResult(interp, pgresStatus[PQresultStatus(result)], 0); + return TCL_OK; } - arrVar = argv[3]; - /* this assignment assigns the table of result tuples into a giant - array with the name given in the argument, - the indices of the array or (tupno,attrName). - Note we expect field names not to exceed a few dozen characters, - so truncating to prevent buffer overflow shouldn't be a problem. - */ - for (tupno = 0; tupno<PQntuples(result); tupno++) { - for (i=0;i<PQnfields(result);i++) { - sprintf(nameBuffer, "%d,%.200s", tupno, PQfname(result,i)); - if (Tcl_SetVar2(interp, arrVar, nameBuffer, + else if (strcmp(opt, "-oid") == 0) + { + Tcl_AppendResult(interp, PQoidStatus(result), 0); + return TCL_OK; + } + else if (strcmp(opt, "-conn") == 0) + return PgGetConnByResultId(interp, argv[1]); + else if (strcmp(opt, "-clear") == 0) + { + PgDelResultId(interp, argv[1]); + PQclear(result); + return TCL_OK; + } + else if (strcmp(opt, "-numTuples") == 0) + { + sprintf(interp->result, "%d", PQntuples(result)); + return TCL_OK; + } + else if (strcmp(opt, "-numAttrs") == 0) + { + sprintf(interp->result, "%d", PQnfields(result)); + return TCL_OK; + } + else if (strcmp(opt, "-assign") == 0) + { + if (argc != 4) + { + Tcl_AppendResult(interp, "-assign option must be followed by a variable name", 0); + return TCL_ERROR; + } + arrVar = argv[3]; + + /* + * this assignment assigns the table of result tuples into a giant + * array with the name given in the argument, the indices of the + * array or (tupno,attrName). Note we expect field names not to + * exceed a few dozen characters, so truncating to prevent buffer + * overflow shouldn't be a problem. + */ + for (tupno = 0; tupno < PQntuples(result); tupno++) + { + for (i = 0; i < PQnfields(result); i++) + { + sprintf(nameBuffer, "%d,%.200s", tupno, PQfname(result, i)); + if (Tcl_SetVar2(interp, arrVar, nameBuffer, #ifdef TCL_ARRAYS - tcl_value(PQgetvalue(result,tupno,i)), + tcl_value(PQgetvalue(result, tupno, i)), #else - PQgetvalue(result,tupno,i), + PQgetvalue(result, tupno, i), #endif - TCL_LEAVE_ERR_MSG) == NULL) - return TCL_ERROR; - } - } - Tcl_AppendResult(interp, arrVar, 0); - return TCL_OK; - } - else if (strcmp(opt, "-assignbyidx") == 0) { - if (argc != 4) { - Tcl_AppendResult(interp, "-assignbyidx option must be followed by a variable name",0); - return TCL_ERROR; - } - arrVar = argv[3]; - /* this assignment assigns the table of result tuples into a giant - array with the name given in the argument, - the indices of the array or (tupno,attrName). - Here, we still assume PQfname won't exceed 200 characters, - but we dare not make the same assumption about the data in field 0. - */ - for (tupno = 0; tupno<PQntuples(result); tupno++) { - const char *field0 = PQgetvalue(result,tupno,0); - char * workspace = malloc(strlen(field0) + 210); - for (i=1;i<PQnfields(result);i++) { - sprintf(workspace, "%s,%.200s", field0, PQfname(result,i)); - if (Tcl_SetVar2(interp, arrVar, workspace, - PQgetvalue(result,tupno,i), - TCL_LEAVE_ERR_MSG) == NULL) { - free(workspace); - return TCL_ERROR; - } - } - free(workspace); - } - Tcl_AppendResult(interp, arrVar, 0); - return TCL_OK; - } - else if (strcmp(opt, "-getTuple") == 0) { - if (argc != 4) { - Tcl_AppendResult(interp, "-getTuple option must be followed by a tuple number",0); - return TCL_ERROR; + TCL_LEAVE_ERR_MSG) == NULL) + return TCL_ERROR; + } + } + Tcl_AppendResult(interp, arrVar, 0); + return TCL_OK; } - tupno = atoi(argv[3]); - if (tupno < 0 || tupno >= PQntuples(result)) { - Tcl_AppendResult(interp, "argument to getTuple cannot exceed number of tuples - 1",0); - return TCL_ERROR; + else if (strcmp(opt, "-assignbyidx") == 0) + { + if (argc != 4) + { + Tcl_AppendResult(interp, "-assignbyidx option must be followed by a variable name", 0); + return TCL_ERROR; + } + arrVar = argv[3]; + + /* + * this assignment assigns the table of result tuples into a giant + * array with the name given in the argument, the indices of the + * array or (tupno,attrName). Here, we still assume PQfname won't + * exceed 200 characters, but we dare not make the same assumption + * about the data in field 0. + */ + for (tupno = 0; tupno < PQntuples(result); tupno++) + { + const char *field0 = PQgetvalue(result, tupno, 0); + char *workspace = malloc(strlen(field0) + 210); + + for (i = 1; i < PQnfields(result); i++) + { + sprintf(workspace, "%s,%.200s", field0, PQfname(result, i)); + if (Tcl_SetVar2(interp, arrVar, workspace, + PQgetvalue(result, tupno, i), + TCL_LEAVE_ERR_MSG) == NULL) + { + free(workspace); + return TCL_ERROR; + } + } + free(workspace); + } + Tcl_AppendResult(interp, arrVar, 0); + return TCL_OK; } - for (i=0; i<PQnfields(result); i++) { - Tcl_AppendElement(interp, PQgetvalue(result,tupno,i)); + else if (strcmp(opt, "-getTuple") == 0) + { + if (argc != 4) + { + Tcl_AppendResult(interp, "-getTuple option must be followed by a tuple number", 0); + return TCL_ERROR; + } + tupno = atoi(argv[3]); + if (tupno < 0 || tupno >= PQntuples(result)) + { + Tcl_AppendResult(interp, "argument to getTuple cannot exceed number of tuples - 1", 0); + return TCL_ERROR; + } + for (i = 0; i < PQnfields(result); i++) + Tcl_AppendElement(interp, PQgetvalue(result, tupno, i)); + return TCL_OK; } - return TCL_OK; - } - else if (strcmp(opt, "-tupleArray") == 0) { - if (argc != 5) { - Tcl_AppendResult(interp, "-tupleArray option must be followed by a tuple number and array name",0); - return TCL_ERROR; + else if (strcmp(opt, "-tupleArray") == 0) + { + if (argc != 5) + { + Tcl_AppendResult(interp, "-tupleArray option must be followed by a tuple number and array name", 0); + return TCL_ERROR; + } + tupno = atoi(argv[3]); + if (tupno < 0 || tupno >= PQntuples(result)) + { + Tcl_AppendResult(interp, "argument to tupleArray cannot exceed number of tuples - 1", 0); + return TCL_ERROR; + } + for (i = 0; i < PQnfields(result); i++) + { + if (Tcl_SetVar2(interp, argv[4], PQfname(result, i), + PQgetvalue(result, tupno, i), + TCL_LEAVE_ERR_MSG) == NULL) + return TCL_ERROR; + } + return TCL_OK; } - tupno = atoi(argv[3]); - if (tupno < 0 || tupno >= PQntuples(result)) { - Tcl_AppendResult(interp, "argument to tupleArray cannot exceed number of tuples - 1",0); - return TCL_ERROR; + else if (strcmp(opt, "-attributes") == 0) + { + for (i = 0; i < PQnfields(result); i++) + Tcl_AppendElement(interp, PQfname(result, i)); + return TCL_OK; } - for ( i = 0; i < PQnfields(result); i++) { - if (Tcl_SetVar2(interp, argv[4], PQfname(result, i), - PQgetvalue(result, tupno, i), - TCL_LEAVE_ERR_MSG) == NULL) { - return TCL_ERROR; - } + else if (strcmp(opt, "-lAttributes") == 0) + { + for (i = 0; i < PQnfields(result); i++) + { + /* start a sublist */ + if (i > 0) + Tcl_AppendResult(interp, " {", 0); + else + Tcl_AppendResult(interp, "{", 0); + Tcl_AppendElement(interp, PQfname(result, i)); + sprintf(nameBuffer, "%ld", (long) PQftype(result, i)); + Tcl_AppendElement(interp, nameBuffer); + sprintf(nameBuffer, "%ld", (long) PQfsize(result, i)); + Tcl_AppendElement(interp, nameBuffer); + /* end the sublist */ + Tcl_AppendResult(interp, "}", 0); + } + return TCL_OK; } - return TCL_OK; - } - else if (strcmp(opt, "-attributes") == 0) { - for (i=0;i<PQnfields(result);i++) { - Tcl_AppendElement(interp, PQfname(result,i)); - } - return TCL_OK; - } - else if (strcmp(opt, "-lAttributes") == 0) { - for (i = 0; i < PQnfields(result); i++) { - /* start a sublist */ - if (i > 0) - Tcl_AppendResult(interp, " {", 0); else - Tcl_AppendResult(interp, "{", 0); - Tcl_AppendElement(interp, PQfname(result, i)); - sprintf(nameBuffer, "%ld", (long) PQftype(result, i)); - Tcl_AppendElement(interp, nameBuffer); - sprintf(nameBuffer, "%ld", (long) PQfsize(result, i)); - Tcl_AppendElement(interp, nameBuffer); - /* end the sublist */ - Tcl_AppendResult(interp, "}", 0); - } - return TCL_OK; - } - else { - Tcl_AppendResult(interp, "Invalid option",0); - goto Pg_result_errReturn; - } - - - Pg_result_errReturn: - Tcl_AppendResult(interp, - "pg_result result ?option? where ?option is\n", - "\t-status\n", - "\t-conn\n", - "\t-assign arrayVarName\n", - "\t-assignbyidx arrayVarName\n", - "\t-numTuples\n", - "\t-numAttrs\n" - "\t-attributes\n" - "\t-lAttributes\n" - "\t-getTuple tupleNumber\n", - "\t-tupleArray tupleNumber arrayVarName\n", - "\t-clear\n", - "\t-oid\n", - (char*)0); - return TCL_ERROR; - + { + Tcl_AppendResult(interp, "Invalid option", 0); + goto Pg_result_errReturn; + } + + +Pg_result_errReturn: + Tcl_AppendResult(interp, + "pg_result result ?option? where ?option is\n", + "\t-status\n", + "\t-conn\n", + "\t-assign arrayVarName\n", + "\t-assignbyidx arrayVarName\n", + "\t-numTuples\n", + "\t-numAttrs\n" + "\t-attributes\n" + "\t-lAttributes\n" + "\t-getTuple tupleNumber\n", + "\t-tupleArray tupleNumber arrayVarName\n", + "\t-clear\n", + "\t-oid\n", + (char *) 0); + return TCL_ERROR; + } /********************************** * pg_lo_open - open a large object - + open a large object + syntax: - pg_lo_open conn objOid mode + pg_lo_open conn objOid mode where mode can be either 'r', 'w', or 'rw' **********************/ int -Pg_lo_open(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_open(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - int lobjId; - int mode; - int fd; - - if (argc != 4) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_lo_open connection lobjOid mode", 0); - return TCL_ERROR; - } - - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - lobjId = atoi(argv[2]); - if (strlen(argv[3]) < 1 || - strlen(argv[3]) > 2) - { - Tcl_AppendResult(interp,"mode argument must be 'r', 'w', or 'rw'",0); - return TCL_ERROR; - } - switch (argv[3][0]) { - case 'r': - case 'R': - mode = INV_READ; - break; - case 'w': - case 'W': - mode = INV_WRITE; - break; - default: - Tcl_AppendResult(interp,"mode argument must be 'r', 'w', or 'rw'",0); - return TCL_ERROR; - } - switch (argv[3][1]) { - case '\0': - break; - case 'r': - case 'R': - mode = mode & INV_READ; - break; - case 'w': - case 'W': - mode = mode & INV_WRITE; - break; - default: - Tcl_AppendResult(interp,"mode argument must be 'r', 'w', or 'rw'",0); - return TCL_ERROR; - } - - fd = lo_open(conn,lobjId,mode); - sprintf(interp->result,"%d",fd); - return TCL_OK; + PGconn *conn; + int lobjId; + int mode; + int fd; + + if (argc != 4) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_lo_open connection lobjOid mode", 0); + return TCL_ERROR; + } + + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; + + lobjId = atoi(argv[2]); + if (strlen(argv[3]) < 1 || + strlen(argv[3]) > 2) + { + Tcl_AppendResult(interp, "mode argument must be 'r', 'w', or 'rw'", 0); + return TCL_ERROR; + } + switch (argv[3][0]) + { + case 'r': + case 'R': + mode = INV_READ; + break; + case 'w': + case 'W': + mode = INV_WRITE; + break; + default: + Tcl_AppendResult(interp, "mode argument must be 'r', 'w', or 'rw'", 0); + return TCL_ERROR; + } + switch (argv[3][1]) + { + case '\0': + break; + case 'r': + case 'R': + mode = mode & INV_READ; + break; + case 'w': + case 'W': + mode = mode & INV_WRITE; + break; + default: + Tcl_AppendResult(interp, "mode argument must be 'r', 'w', or 'rw'", 0); + return TCL_ERROR; + } + + fd = lo_open(conn, lobjId, mode); + sprintf(interp->result, "%d", fd); + return TCL_OK; } /********************************** * pg_lo_close - close a large object - + close a large object + syntax: - pg_lo_close conn fd + pg_lo_close conn fd **********************/ int -Pg_lo_close(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_close(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - int fd; + PGconn *conn; + int fd; - if (argc != 3) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_lo_close connection fd", 0); - return TCL_ERROR; - } + if (argc != 3) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_lo_close connection fd", 0); + return TCL_ERROR; + } - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - fd = atoi(argv[2]); - sprintf(interp->result,"%d",lo_close(conn,fd)); - return TCL_OK; + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; + + fd = atoi(argv[2]); + sprintf(interp->result, "%d", lo_close(conn, fd)); + return TCL_OK; } /********************************** * pg_lo_read - reads at most len bytes from a large object into a variable named + reads at most len bytes from a large object into a variable named bufVar - + syntax: pg_lo_read conn fd bufVar len @@ -778,94 +830,96 @@ Pg_lo_close(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) **********************/ int -Pg_lo_read(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_read(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - int fd; - int nbytes = 0; - char *buf; - char *bufVar; - int len; - - if (argc != 5) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - " pg_lo_read conn fd bufVar len", 0); - return TCL_ERROR; - } + PGconn *conn; + int fd; + int nbytes = 0; + char *buf; + char *bufVar; + int len; - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - fd = atoi(argv[2]); + if (argc != 5) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + " pg_lo_read conn fd bufVar len", 0); + return TCL_ERROR; + } - bufVar = argv[3]; + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; - len = atoi(argv[4]); + fd = atoi(argv[2]); - if (len <= 0) { - sprintf(interp->result,"%d",nbytes); - return TCL_OK; - } - buf = ckalloc(len+1); + bufVar = argv[3]; - nbytes = lo_read(conn,fd,buf,len); + len = atoi(argv[4]); + + if (len <= 0) + { + sprintf(interp->result, "%d", nbytes); + return TCL_OK; + } + buf = ckalloc(len + 1); + + nbytes = lo_read(conn, fd, buf, len); + + Tcl_SetVar(interp, bufVar, buf, TCL_LEAVE_ERR_MSG); + sprintf(interp->result, "%d", nbytes); + ckfree(buf); + return TCL_OK; - Tcl_SetVar(interp,bufVar,buf,TCL_LEAVE_ERR_MSG); - sprintf(interp->result,"%d",nbytes); - ckfree(buf); - return TCL_OK; - } /*********************************** Pg_lo_write - write at most len bytes to a large object + write at most len bytes to a large object syntax: pg_lo_write conn fd buf len ***********************************/ int -Pg_lo_write(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_write(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - char *buf; - int fd; - int nbytes = 0; - int len; - - if (argc != 5) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_lo_write conn fd buf len", 0); - return TCL_ERROR; - } + PGconn *conn; + char *buf; + int fd; + int nbytes = 0; + int len; - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - fd = atoi(argv[2]); + if (argc != 5) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_lo_write conn fd buf len", 0); + return TCL_ERROR; + } - buf = argv[3]; + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; - len = atoi(argv[4]); + fd = atoi(argv[2]); - if (len <= 0) { - sprintf(interp->result,"%d",nbytes); - return TCL_OK; - } + buf = argv[3]; + + len = atoi(argv[4]); + + if (len <= 0) + { + sprintf(interp->result, "%d", nbytes); + return TCL_OK; + } - nbytes = lo_write(conn,fd,buf,len); - sprintf(interp->result,"%d",nbytes); - return TCL_OK; + nbytes = lo_write(conn, fd, buf, len); + sprintf(interp->result, "%d", nbytes); + return TCL_OK; } /*********************************** Pg_lo_lseek - seek to a certain position in a large object + seek to a certain position in a large object syntax pg_lo_lseek conn fd offset whence @@ -874,42 +928,44 @@ whence can be either "SEEK_CUR", "SEEK_END", or "SEEK_SET" ***********************************/ int -Pg_lo_lseek(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_lseek(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - int fd; - char *whenceStr; - int offset, whence; - - if (argc != 5) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_lo_lseek conn fd offset whence", 0); - return TCL_ERROR; - } + PGconn *conn; + int fd; + char *whenceStr; + int offset, + whence; - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - fd = atoi(argv[2]); - - offset = atoi(argv[3]); - - whenceStr = argv[4]; - if (strcmp(whenceStr,"SEEK_SET") == 0) { - whence = SEEK_SET; - } else if (strcmp(whenceStr,"SEEK_CUR") == 0) { - whence = SEEK_CUR; - } else if (strcmp(whenceStr,"SEEK_END") == 0) { - whence = SEEK_END; - } else { - Tcl_AppendResult(interp, "the whence argument to Pg_lo_lseek must be SEEK_SET, SEEK_CUR or SEEK_END",0); - return TCL_ERROR; - } - - sprintf(interp->result,"%d",lo_lseek(conn,fd,offset,whence)); - return TCL_OK; + if (argc != 5) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_lo_lseek conn fd offset whence", 0); + return TCL_ERROR; + } + + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; + + fd = atoi(argv[2]); + + offset = atoi(argv[3]); + + whenceStr = argv[4]; + if (strcmp(whenceStr, "SEEK_SET") == 0) + whence = SEEK_SET; + else if (strcmp(whenceStr, "SEEK_CUR") == 0) + whence = SEEK_CUR; + else if (strcmp(whenceStr, "SEEK_END") == 0) + whence = SEEK_END; + else + { + Tcl_AppendResult(interp, "the whence argument to Pg_lo_lseek must be SEEK_SET, SEEK_CUR or SEEK_END", 0); + return TCL_ERROR; + } + + sprintf(interp->result, "%d", lo_lseek(conn, fd, offset, whence)); + return TCL_OK; } @@ -925,89 +981,92 @@ for now, we don't support any additional storage managers. ***********************************/ int -Pg_lo_creat(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_creat(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - char *modeStr; - char *modeWord; - int mode; - - if (argc != 3) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_lo_creat conn mode", 0); - return TCL_ERROR; - } + PGconn *conn; + char *modeStr; + char *modeWord; + int mode; - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - modeStr = argv[2]; - - modeWord = strtok(modeStr,"|"); - if (strcmp(modeWord,"INV_READ") == 0) { - mode = INV_READ; - } else if (strcmp(modeWord,"INV_WRITE") == 0) { - mode = INV_WRITE; - } else { - Tcl_AppendResult(interp, - "invalid mode argument to Pg_lo_creat\nmode argument must be some OR'd combination of INV_READ, and INV_WRITE", - 0); - return TCL_ERROR; - } - - while ( (modeWord = strtok((char*)NULL, "|")) != NULL) { - if (strcmp(modeWord,"INV_READ") == 0) { - mode |= INV_READ; - } else if (strcmp(modeWord,"INV_WRITE") == 0) { - mode |= INV_WRITE; - } else { - Tcl_AppendResult(interp, - "invalid mode argument to Pg_lo_creat\nmode argument must be some OR'd combination of INV_READ, INV_WRITE", - 0); - return TCL_ERROR; + if (argc != 3) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_lo_creat conn mode", 0); + return TCL_ERROR; + } + + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; + + modeStr = argv[2]; + + modeWord = strtok(modeStr, "|"); + if (strcmp(modeWord, "INV_READ") == 0) + mode = INV_READ; + else if (strcmp(modeWord, "INV_WRITE") == 0) + mode = INV_WRITE; + else + { + Tcl_AppendResult(interp, + "invalid mode argument to Pg_lo_creat\nmode argument must be some OR'd combination of INV_READ, and INV_WRITE", + 0); + return TCL_ERROR; + } + + while ((modeWord = strtok((char *) NULL, "|")) != NULL) + { + if (strcmp(modeWord, "INV_READ") == 0) + mode |= INV_READ; + else if (strcmp(modeWord, "INV_WRITE") == 0) + mode |= INV_WRITE; + else + { + Tcl_AppendResult(interp, + "invalid mode argument to Pg_lo_creat\nmode argument must be some OR'd combination of INV_READ, INV_WRITE", + 0); + return TCL_ERROR; + } } - } - sprintf(interp->result,"%d",lo_creat(conn,mode)); - return TCL_OK; + sprintf(interp->result, "%d", lo_creat(conn, mode)); + return TCL_OK; } /*********************************** Pg_lo_tell - returns the current seek location of the large object + returns the current seek location of the large object syntax: pg_lo_tell conn fd ***********************************/ int -Pg_lo_tell(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_tell(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - int fd; + PGconn *conn; + int fd; - if (argc != 3) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_lo_tell conn fd", 0); - return TCL_ERROR; - } + if (argc != 3) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_lo_tell conn fd", 0); + return TCL_ERROR; + } - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - fd = atoi(argv[2]); + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; - sprintf(interp->result,"%d",lo_tell(conn,fd)); - return TCL_OK; + fd = atoi(argv[2]); + + sprintf(interp->result, "%d", lo_tell(conn, fd)); + return TCL_OK; } /*********************************** Pg_lo_unlink - unlink a file based on lobject id + unlink a file based on lobject id syntax: pg_lo_unlink conn lobjId @@ -1015,38 +1074,39 @@ Pg_lo_unlink ***********************************/ int -Pg_lo_unlink(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_unlink(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - int lobjId; - int retval; + PGconn *conn; + int lobjId; + int retval; - if (argc != 3) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_lo_tell conn fd", 0); - return TCL_ERROR; - } + if (argc != 3) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_lo_tell conn fd", 0); + return TCL_ERROR; + } - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - lobjId = atoi(argv[2]); + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; - retval = lo_unlink(conn,lobjId); - if (retval == -1) { - sprintf(interp->result,"Pg_lo_unlink of '%d' failed",lobjId); - return TCL_ERROR; - } - - sprintf(interp->result,"%d",retval); - return TCL_OK; + lobjId = atoi(argv[2]); + + retval = lo_unlink(conn, lobjId); + if (retval == -1) + { + sprintf(interp->result, "Pg_lo_unlink of '%d' failed", lobjId); + return TCL_ERROR; + } + + sprintf(interp->result, "%d", retval); + return TCL_OK; } /*********************************** Pg_lo_import - import a Unix file into an (inversion) large objct + import a Unix file into an (inversion) large objct returns the oid of that object upon success returns InvalidOid upon failure @@ -1056,77 +1116,79 @@ Pg_lo_import ***********************************/ int -Pg_lo_import(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_import(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - char* filename; - Oid lobjId; + PGconn *conn; + char *filename; + Oid lobjId; - if (argc != 3) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_lo_import conn filename", 0); - return TCL_ERROR; - } + if (argc != 3) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_lo_import conn filename", 0); + return TCL_ERROR; + } - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - filename = argv[2]; + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; - lobjId = lo_import(conn,filename); - if (lobjId == InvalidOid) { - sprintf(interp->result, "Pg_lo_import of '%s' failed",filename); - return TCL_ERROR; - } - sprintf(interp->result,"%d",lobjId); - return TCL_OK; + filename = argv[2]; + + lobjId = lo_import(conn, filename); + if (lobjId == InvalidOid) + { + sprintf(interp->result, "Pg_lo_import of '%s' failed", filename); + return TCL_ERROR; + } + sprintf(interp->result, "%d", lobjId); + return TCL_OK; } /*********************************** Pg_lo_export - export an Inversion large object to a Unix file - + export an Inversion large object to a Unix file + syntax: pg_lo_export conn lobjId filename ***********************************/ int -Pg_lo_export(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_lo_export(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - PGconn *conn; - char* filename; - Oid lobjId; - int retval; - - if (argc != 4) { - Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_lo_export conn lobjId filename", 0); - return TCL_ERROR; - } + PGconn *conn; + char *filename; + Oid lobjId; + int retval; - conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId**)NULL); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - - lobjId = atoi(argv[2]); - filename = argv[3]; - - retval = lo_export(conn,lobjId,filename); - if (retval == -1) { - sprintf(interp->result, "Pg_lo_export %d %s failed",lobjId, filename); - return TCL_ERROR; - } - return TCL_OK; + if (argc != 4) + { + Tcl_AppendResult(interp, "Wrong # of arguments\n", + "pg_lo_export conn lobjId filename", 0); + return TCL_ERROR; + } + + conn = PgGetConnectionId(interp, argv[1], (Pg_ConnectionId **) NULL); + if (conn == (PGconn *) NULL) + return TCL_ERROR; + + lobjId = atoi(argv[2]); + filename = argv[3]; + + retval = lo_export(conn, lobjId, filename); + if (retval == -1) + { + sprintf(interp->result, "Pg_lo_export %d %s failed", lobjId, filename); + return TCL_ERROR; + } + return TCL_OK; } /********************************** * pg_select send a select query string to the backend connection - + syntax: pg_select connection query var proc @@ -1136,7 +1198,7 @@ Pg_lo_export(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) Originally I was also going to update changes but that has turned out to be not so simple. Instead, the caller should get the OID of any - table they want to update and update it themself in the loop. I may + table they want to update and update it themself in the loop. I may try to write a simplified table lookup and update function to make that task a little easier. @@ -1145,43 +1207,45 @@ Pg_lo_export(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) **********************************/ int -Pg_select(ClientData cData, Tcl_Interp *interp, int argc, char **argv) +Pg_select(ClientData cData, Tcl_Interp * interp, int argc, char **argv) { - Pg_ConnectionId *connid; - PGconn *conn; - PGresult *result; - int r; - size_t tupno, column, ncols; + Pg_ConnectionId *connid; + PGconn *conn; + PGresult *result; + int r; + size_t tupno, + column, + ncols; Tcl_DString headers; - char buffer[2048]; - struct info_s { - char *cname; - int change; - } *info; + char buffer[2048]; + struct info_s + { + char *cname; + int change; + } *info; if (argc != 5) { Tcl_AppendResult(interp, "Wrong # of arguments\n", - "pg_select connection queryString var proc", 0); + "pg_select connection queryString var proc", 0); return TCL_ERROR; } - conn = PgGetConnectionId(interp, argv[1], &connid); - if (conn == (PGconn *)NULL) { - return TCL_ERROR; - } - + conn = PgGetConnectionId(interp, argv[1], &connid); + if (conn == (PGconn *) NULL) + return TCL_ERROR; + if ((result = PQexec(conn, argv[2])) == 0) - { + { /* error occurred during the query */ Tcl_SetResult(interp, conn->errorMessage, TCL_STATIC); return TCL_ERROR; - } + } - /* Transfer any notify events from libpq to Tcl event queue. */ - PgNotifyTransferEvents(connid); + /* Transfer any notify events from libpq to Tcl event queue. */ + PgNotifyTransferEvents(connid); - if ((info = (struct info_s *)ckalloc(sizeof(*info) * (ncols = PQnfields(result)))) == NULL) + if ((info = (struct info_s *) ckalloc(sizeof(*info) * (ncols = PQnfields(result)))) == NULL) { Tcl_AppendResult(interp, "Not enough memory", 0); return TCL_ERROR; @@ -1218,10 +1282,10 @@ Pg_select(ClientData cData, Tcl_Interp *interp, int argc, char **argv) if (r == TCL_ERROR) { - char msg[60]; + char msg[60]; sprintf(msg, "\n (\"pg_select\" body line %d)", - interp->errorLine); + interp->errorLine); Tcl_AddErrorInfo(interp, msg); } @@ -1229,7 +1293,7 @@ Pg_select(ClientData cData, Tcl_Interp *interp, int argc, char **argv) } } - ckfree((void*)info); + ckfree((void *) info); Tcl_UnsetVar(interp, argv[3], 0); Tcl_AppendResult(interp, "", 0); return TCL_OK; @@ -1237,7 +1301,7 @@ Pg_select(ClientData cData, Tcl_Interp *interp, int argc, char **argv) /*********************************** Pg_listen - create or remove a callback request for notifies on a given name + create or remove a callback request for notifies on a given name syntax: pg_listen conn notifyname ?callbackcommand? @@ -1250,66 +1314,73 @@ Pg_listen vwait or update can be used to enter the Tcl event loop. ***********************************/ int -Pg_listen(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) +Pg_listen(ClientData cData, Tcl_Interp * interp, int argc, char *argv[]) { - char *origrelname; - char *caserelname; - char *callback = NULL; + char *origrelname; + char *caserelname; + char *callback = NULL; Pg_TclNotifies *notifies; - Tcl_HashEntry *entry; - Pg_ConnectionId *connid; - PGconn *conn; - PGresult *result; - int new; + Tcl_HashEntry *entry; + Pg_ConnectionId *connid; + PGconn *conn; + PGresult *result; + int new; - if (argc < 3 || argc > 4) { + if (argc < 3 || argc > 4) + { Tcl_AppendResult(interp, "wrong # args, should be \"", argv[0], " connection relname ?callback?\"", 0); return TCL_ERROR; - } - - /* - * Get the command arguments. Note that the relation name will be copied - * by Tcl_CreateHashEntry while the callback string must be allocated. - */ - conn = PgGetConnectionId(interp, argv[1], &connid); - if (conn == (PGconn *)NULL) { + } + + /* + * Get the command arguments. Note that the relation name will be + * copied by Tcl_CreateHashEntry while the callback string must be + * allocated. + */ + conn = PgGetConnectionId(interp, argv[1], &connid); + if (conn == (PGconn *) NULL) return TCL_ERROR; - } /* * LISTEN/NOTIFY do not preserve case unless the relation name is - * quoted. We have to do the same thing to ensure that we will find + * quoted. We have to do the same thing to ensure that we will find * the desired pg_listen item. */ origrelname = argv[2]; caserelname = (char *) ckalloc((unsigned) (strlen(origrelname) + 1)); - if (*origrelname == '"') { + if (*origrelname == '"') + { /* Copy a quoted string without downcasing */ strcpy(caserelname, origrelname + 1); caserelname[strlen(caserelname) - 1] = '\0'; - } else { + } + else + { /* Downcase it */ - char *rels = origrelname; - char *reld = caserelname; - while (*rels) { + char *rels = origrelname; + char *reld = caserelname; + + while (*rels) *reld++ = tolower(*rels++); - } *reld = '\0'; } - if ((argc > 3) && *argv[3]) { + if ((argc > 3) && *argv[3]) + { callback = (char *) ckalloc((unsigned) (strlen(argv[3]) + 1)); strcpy(callback, argv[3]); - } + } /* Find or make a Pg_TclNotifies struct for this interp and connection */ - for (notifies = connid->notify_list; notifies; notifies = notifies->next) { + for (notifies = connid->notify_list; notifies; notifies = notifies->next) + { if (notifies->interp == interp) break; } - if (notifies == NULL) { + if (notifies == NULL) + { notifies = (Pg_TclNotifies *) ckalloc(sizeof(Pg_TclNotifies)); notifies->interp = interp; Tcl_InitHashTable(¬ifies->notify_hash, TCL_STRING_KEYS); @@ -1319,22 +1390,27 @@ Pg_listen(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) (ClientData) notifies); } - /* - * Set or update a callback for a relation - */ - if (callback) { + /* + * Set or update a callback for a relation + */ + if (callback) + { entry = Tcl_CreateHashEntry(¬ifies->notify_hash, caserelname, &new); - if (new) { + if (new) + { /* New callback, execute a listen command on the relation */ - char *cmd = (char *) ckalloc((unsigned) (strlen(origrelname)+8)); + char *cmd = (char *) ckalloc((unsigned) (strlen(origrelname) + 8)); + sprintf(cmd, "LISTEN %s", origrelname); result = PQexec(conn, cmd); ckfree(cmd); /* Transfer any notify events from libpq to Tcl event queue. */ PgNotifyTransferEvents(connid); - if (!result || (result->resultStatus != PGRES_COMMAND_OK)) { + if (!result || (result->resultStatus != PGRES_COMMAND_OK)) + { /* Error occurred during the execution of command */ - if (result) PQclear(result); + if (result) + PQclear(result); ckfree(callback); ckfree(caserelname); Tcl_DeleteHashEntry(entry); @@ -1342,7 +1418,9 @@ Pg_listen(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) return TCL_ERROR; } PQclear(result); - } else { + } + else + { /* Update, free the old callback string */ ckfree((char *) Tcl_GetHashValue(entry)); } @@ -1350,24 +1428,26 @@ Pg_listen(ClientData cData, Tcl_Interp *interp, int argc, char* argv[]) Tcl_SetHashValue(entry, callback); /* Start the notify event source if it isn't already running */ PgStartNotifyEventSource(connid); - } - - /* - * Remove a callback for a relation. There is no way to - * un-listen a relation, so we simply remove the callback from - * the notify hash table. - */ - if (callback == NULL) { + } + + /* + * Remove a callback for a relation. There is no way to un-listen a + * relation, so we simply remove the callback from the notify hash + * table. + */ + if (callback == NULL) + { entry = Tcl_FindHashEntry(¬ifies->notify_hash, caserelname); - if (entry == NULL) { + if (entry == NULL) + { Tcl_AppendResult(interp, "not listening on ", origrelname, 0); ckfree(caserelname); return TCL_ERROR; } ckfree((char *) Tcl_GetHashValue(entry)); Tcl_DeleteHashEntry(entry); - } + } ckfree(caserelname); - return TCL_OK; + return TCL_OK; } diff --git a/src/interfaces/libpgtcl/pgtclCmds.h b/src/interfaces/libpgtcl/pgtclCmds.h index 052a1a0a6f..0f8b26f689 100644 --- a/src/interfaces/libpgtcl/pgtclCmds.h +++ b/src/interfaces/libpgtcl/pgtclCmds.h @@ -5,7 +5,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pgtclCmds.h,v 1.10 1998/06/16 04:10:17 momjian Exp $ + * $Id: pgtclCmds.h,v 1.11 1998/09/01 04:39:57 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -28,33 +28,37 @@ * name. (All their callbacks will be called, but in an unspecified order.) */ -typedef struct Pg_TclNotifies_s { - struct Pg_TclNotifies_s *next; /* list link */ - Tcl_Interp *interp; /* This Tcl interpreter */ - /* NB: if interp == NULL, the interpreter is gone but we haven't - * yet got round to deleting the Pg_TclNotifies structure. - */ - Tcl_HashTable notify_hash; /* Active pg_listen requests */ -} Pg_TclNotifies; +typedef struct Pg_TclNotifies_s +{ + struct Pg_TclNotifies_s *next; /* list link */ + Tcl_Interp *interp; /* This Tcl interpreter */ -typedef struct Pg_ConnectionId_s { - char id[32]; - PGconn *conn; - int res_max; /* Max number of results allocated */ - int res_hardmax; /* Absolute max to allow */ - int res_count; /* Current count of active results */ - int res_last; /* Optimize where to start looking */ - int res_copy; /* Query result with active copy */ - int res_copyStatus; /* Copying status */ - PGresult **results; /* The results */ + /* + * NB: if interp == NULL, the interpreter is gone but we haven't yet + * got round to deleting the Pg_TclNotifies structure. + */ + Tcl_HashTable notify_hash; /* Active pg_listen requests */ +} Pg_TclNotifies; - Pg_TclNotifies *notify_list; /* head of list of notify info */ - int notifier_running; /* notify event source is live */ +typedef struct Pg_ConnectionId_s +{ + char id[32]; + PGconn *conn; + int res_max; /* Max number of results allocated */ + int res_hardmax; /* Absolute max to allow */ + int res_count; /* Current count of active results */ + int res_last; /* Optimize where to start looking */ + int res_copy; /* Query result with active copy */ + int res_copyStatus; /* Copying status */ + PGresult **results; /* The results */ -} Pg_ConnectionId; + Pg_TclNotifies *notify_list;/* head of list of notify info */ + int notifier_running; /* notify event source is live */ + +} Pg_ConnectionId; #define RES_COPY_NONE 0 -#define RES_COPY_INPROGRESS 1 +#define RES_COPY_INPROGRESS 1 #define RES_COPY_FIN 2 @@ -62,38 +66,38 @@ typedef struct Pg_ConnectionId_s { /* registered Tcl functions */ /* **************************/ extern int Pg_conndefaults( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_connect( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_disconnect( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_exec( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_select( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_result( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_open( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_close( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_read( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_write( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_lseek( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_creat( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_tell( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_unlink( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_import( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_lo_export( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); extern int Pg_listen( - ClientData cData, Tcl_Interp *interp, int argc, char* argv[]); + ClientData cData, Tcl_Interp * interp, int argc, char *argv[]); -#endif /*PGTCLCMDS_H*/ +#endif /* PGTCLCMDS_H */ diff --git a/src/interfaces/libpgtcl/pgtclId.c b/src/interfaces/libpgtcl/pgtclId.c index 1d3cd29709..9377e322e7 100644 --- a/src/interfaces/libpgtcl/pgtclId.c +++ b/src/interfaces/libpgtcl/pgtclId.c @@ -1,18 +1,18 @@ /*------------------------------------------------------------------------- * * pgtclId.c-- - * useful routines to convert between strings and pointers - * Needed because everything in tcl is a string, but we want pointers - * to data structures + * useful routines to convert between strings and pointers + * Needed because everything in tcl is a string, but we want pointers + * to data structures * - * ASSUMPTION: sizeof(long) >= sizeof(void*) + * ASSUMPTION: sizeof(long) >= sizeof(void*) * * * Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclId.c,v 1.13 1998/08/22 04:34:22 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclId.c,v 1.14 1998/09/01 04:39:58 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -27,129 +27,145 @@ #include "pgtclId.h" -static int PgEndCopy(Pg_ConnectionId *connid, int *errorCodePtr) +static int +PgEndCopy(Pg_ConnectionId * connid, int *errorCodePtr) { - connid->res_copyStatus = RES_COPY_NONE; - if (PQendcopy(connid->conn)) { - connid->results[connid->res_copy]->resultStatus = PGRES_BAD_RESPONSE; - connid->res_copy = -1; - *errorCodePtr = EIO; - return -1; - } else { - connid->results[connid->res_copy]->resultStatus = PGRES_COMMAND_OK; - connid->res_copy = -1; - return 0; - } + connid->res_copyStatus = RES_COPY_NONE; + if (PQendcopy(connid->conn)) + { + connid->results[connid->res_copy]->resultStatus = PGRES_BAD_RESPONSE; + connid->res_copy = -1; + *errorCodePtr = EIO; + return -1; + } + else + { + connid->results[connid->res_copy]->resultStatus = PGRES_COMMAND_OK; + connid->res_copy = -1; + return 0; + } } /* - * Called when reading data (via gets) for a copy <rel> to stdout. + * Called when reading data (via gets) for a copy <rel> to stdout. * - * NOTE: this routine knows way more than it ought to about libpq's - * internal buffering mechanisms. + * NOTE: this routine knows way more than it ought to about libpq's + * internal buffering mechanisms. */ -int PgInputProc(DRIVER_INPUT_PROTO) +int +PgInputProc(DRIVER_INPUT_PROTO) { - Pg_ConnectionId *connid; - PGconn *conn; - char c; - int avail; - - connid = (Pg_ConnectionId *)cData; - conn = connid->conn; - - if (connid->res_copy < 0 || - connid->results[connid->res_copy]->resultStatus != PGRES_COPY_OUT) { - *errorCodePtr = EBUSY; - return -1; - } - - /* Try to load any newly arrived data */ - conn->errorMessage[0] = '\0'; - PQconsumeInput(conn); - if (conn->errorMessage[0]) { - *errorCodePtr = EIO; - return -1; - } - - /* Move data from libpq's buffer to Tcl's. - * We want to accept data only in units of whole lines, - * not partial lines. This ensures that we can recognize - * the terminator line "\\.\n". (Otherwise, if it happened - * to cross a packet/buffer boundary, we might hand the first - * one or two characters off to Tcl, which we shouldn't.) - */ - - conn->inCursor = conn->inStart; - - avail = bufSize; - while (avail > 0 && conn->inCursor < conn->inEnd) { - c = conn->inBuffer[conn->inCursor++]; - *buf++ = c; - --avail; - if (c == '\n') { - /* Got a complete line; mark the data removed from libpq */ - conn->inStart = conn->inCursor; - /* Is it the endmarker line? */ - if (bufSize-avail == 3 && buf[-3] == '\\' && buf[-2] == '.') { - /* Yes, change state and return 0 */ - return PgEndCopy(connid, errorCodePtr); - } - /* No, return the data to Tcl */ - /* fprintf(stderr, "returning %d chars\n", bufSize - avail); */ - return bufSize - avail; + Pg_ConnectionId *connid; + PGconn *conn; + char c; + int avail; + + connid = (Pg_ConnectionId *) cData; + conn = connid->conn; + + if (connid->res_copy < 0 || + connid->results[connid->res_copy]->resultStatus != PGRES_COPY_OUT) + { + *errorCodePtr = EBUSY; + return -1; + } + + /* Try to load any newly arrived data */ + conn->errorMessage[0] = '\0'; + PQconsumeInput(conn); + if (conn->errorMessage[0]) + { + *errorCodePtr = EIO; + return -1; + } + + /* + * Move data from libpq's buffer to Tcl's. We want to accept data only + * in units of whole lines, not partial lines. This ensures that we + * can recognize the terminator line "\\.\n". (Otherwise, if it + * happened to cross a packet/buffer boundary, we might hand the first + * one or two characters off to Tcl, which we shouldn't.) + */ + + conn->inCursor = conn->inStart; + + avail = bufSize; + while (avail > 0 && conn->inCursor < conn->inEnd) + { + c = conn->inBuffer[conn->inCursor++]; + *buf++ = c; + --avail; + if (c == '\n') + { + /* Got a complete line; mark the data removed from libpq */ + conn->inStart = conn->inCursor; + /* Is it the endmarker line? */ + if (bufSize - avail == 3 && buf[-3] == '\\' && buf[-2] == '.') + { + /* Yes, change state and return 0 */ + return PgEndCopy(connid, errorCodePtr); + } + /* No, return the data to Tcl */ + /* fprintf(stderr, "returning %d chars\n", bufSize - avail); */ + return bufSize - avail; + } } - } - - /* We don't have a complete line. - * We'd prefer to leave it in libpq's buffer until the rest arrives, - * but there is a special case: what if the line is longer than the - * buffer Tcl is offering us? In that case we'd better hand over - * a partial line, else we'd get into an infinite loop. - * Do this in a way that ensures we can't misrecognize a terminator - * line later: leave last 3 characters in libpq buffer. - */ - if (avail == 0 && bufSize > 3) { - conn->inStart = conn->inCursor - 3; - return bufSize - 3; - } - return 0; + + /* + * We don't have a complete line. We'd prefer to leave it in libpq's + * buffer until the rest arrives, but there is a special case: what if + * the line is longer than the buffer Tcl is offering us? In that + * case we'd better hand over a partial line, else we'd get into an + * infinite loop. Do this in a way that ensures we can't misrecognize + * a terminator line later: leave last 3 characters in libpq buffer. + */ + if (avail == 0 && bufSize > 3) + { + conn->inStart = conn->inCursor - 3; + return bufSize - 3; + } + return 0; } /* - * Called when writing data (via puts) for a copy <rel> from stdin + * Called when writing data (via puts) for a copy <rel> from stdin */ -int PgOutputProc(DRIVER_OUTPUT_PROTO) +int +PgOutputProc(DRIVER_OUTPUT_PROTO) { - Pg_ConnectionId *connid; - PGconn *conn; - - connid = (Pg_ConnectionId *)cData; - conn = connid->conn; - - if (connid->res_copy < 0 || - connid->results[connid->res_copy]->resultStatus != PGRES_COPY_IN) { - *errorCodePtr = EBUSY; - return -1; - } - - conn->errorMessage[0] = '\0'; - - PQputnbytes(conn, buf, bufSize); - - if (conn->errorMessage[0]) { - *errorCodePtr = EIO; - return -1; - } - - /* This assumes Tcl script will write the terminator line - * in a single operation; maybe not such a good assumption? - */ - if (bufSize >= 3 && strncmp(&buf[bufSize-3], "\\.\n", 3) == 0) { - if (PgEndCopy(connid, errorCodePtr) == -1) - return -1; - } - return bufSize; + Pg_ConnectionId *connid; + PGconn *conn; + + connid = (Pg_ConnectionId *) cData; + conn = connid->conn; + + if (connid->res_copy < 0 || + connid->results[connid->res_copy]->resultStatus != PGRES_COPY_IN) + { + *errorCodePtr = EBUSY; + return -1; + } + + conn->errorMessage[0] = '\0'; + + PQputnbytes(conn, buf, bufSize); + + if (conn->errorMessage[0]) + { + *errorCodePtr = EIO; + return -1; + } + + /* + * This assumes Tcl script will write the terminator line in a single + * operation; maybe not such a good assumption? + */ + if (bufSize >= 3 && strncmp(&buf[bufSize - 3], "\\.\n", 3) == 0) + { + if (PgEndCopy(connid, errorCodePtr) == -1) + return -1; + } + return bufSize; } #if HAVE_TCL_GETFILEPROC @@ -157,59 +173,62 @@ int PgOutputProc(DRIVER_OUTPUT_PROTO) Tcl_File PgGetFileProc(ClientData cData, int direction) { - return (Tcl_File)NULL; + return (Tcl_File) NULL; } #endif Tcl_ChannelType Pg_ConnType = { - "pgsql", /* channel type */ - NULL, /* blockmodeproc */ - PgDelConnectionId, /* closeproc */ - PgInputProc, /* inputproc */ - PgOutputProc, /* outputproc */ - /* Note the additional stuff can be left NULL, - or is initialized during a PgSetConnectionId */ + "pgsql", /* channel type */ + NULL, /* blockmodeproc */ + PgDelConnectionId, /* closeproc */ + PgInputProc, /* inputproc */ + PgOutputProc, /* outputproc */ + + /* + * Note the additional stuff can be left NULL, or is initialized + * during a PgSetConnectionId + */ }; /* * Create and register a new channel for the connection */ void -PgSetConnectionId(Tcl_Interp *interp, PGconn *conn) +PgSetConnectionId(Tcl_Interp * interp, PGconn *conn) { - Tcl_Channel conn_chan; - Pg_ConnectionId *connid; - int i; - - connid = (Pg_ConnectionId *)ckalloc(sizeof(Pg_ConnectionId)); - connid->conn = conn; - connid->res_count = 0; - connid->res_last = -1; - connid->res_max = RES_START; - connid->res_hardmax = RES_HARD_MAX; - connid->res_copy = -1; - connid->res_copyStatus = RES_COPY_NONE; - connid->results = (PGresult**)ckalloc(sizeof(PGresult*) * RES_START); - for (i = 0; i < RES_START; i++) - connid->results[i] = NULL; - connid->notify_list = NULL; - connid->notifier_running = 0; - - sprintf(connid->id, "pgsql%d", PQsocket(conn)); + Tcl_Channel conn_chan; + Pg_ConnectionId *connid; + int i; + + connid = (Pg_ConnectionId *) ckalloc(sizeof(Pg_ConnectionId)); + connid->conn = conn; + connid->res_count = 0; + connid->res_last = -1; + connid->res_max = RES_START; + connid->res_hardmax = RES_HARD_MAX; + connid->res_copy = -1; + connid->res_copyStatus = RES_COPY_NONE; + connid->results = (PGresult **) ckalloc(sizeof(PGresult *) * RES_START); + for (i = 0; i < RES_START; i++) + connid->results[i] = NULL; + connid->notify_list = NULL; + connid->notifier_running = 0; + + sprintf(connid->id, "pgsql%d", PQsocket(conn)); #if TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION == 5 - /* Original signature (only seen in Tcl 7.5) */ - conn_chan = Tcl_CreateChannel(&Pg_ConnType, connid->id, NULL, NULL, (ClientData)connid); + /* Original signature (only seen in Tcl 7.5) */ + conn_chan = Tcl_CreateChannel(&Pg_ConnType, connid->id, NULL, NULL, (ClientData) connid); #else - /* Tcl 7.6 and later use this */ - conn_chan = Tcl_CreateChannel(&Pg_ConnType, connid->id, (ClientData)connid, - TCL_READABLE | TCL_WRITABLE); + /* Tcl 7.6 and later use this */ + conn_chan = Tcl_CreateChannel(&Pg_ConnType, connid->id, (ClientData) connid, + TCL_READABLE | TCL_WRITABLE); #endif - Tcl_SetChannelOption(interp, conn_chan, "-buffering", "line"); - Tcl_SetResult(interp, connid->id, TCL_VOLATILE); - Tcl_RegisterChannel(interp, conn_chan); + Tcl_SetChannelOption(interp, conn_chan, "-buffering", "line"); + Tcl_SetResult(interp, connid->id, TCL_VOLATILE); + Tcl_RegisterChannel(interp, conn_chan); } @@ -217,22 +236,23 @@ PgSetConnectionId(Tcl_Interp *interp, PGconn *conn) * Get back the connection from the Id */ PGconn * -PgGetConnectionId(Tcl_Interp *interp, char *id, Pg_ConnectionId **connid_p) +PgGetConnectionId(Tcl_Interp * interp, char *id, Pg_ConnectionId ** connid_p) { - Tcl_Channel conn_chan; - Pg_ConnectionId *connid; + Tcl_Channel conn_chan; + Pg_ConnectionId *connid; - conn_chan = Tcl_GetChannel(interp, id, 0); - if(conn_chan == NULL || Tcl_GetChannelType(conn_chan) != &Pg_ConnType) { - Tcl_ResetResult(interp); - Tcl_AppendResult(interp, id, " is not a valid postgresql connection", 0); - return (PGconn *)NULL; - } + conn_chan = Tcl_GetChannel(interp, id, 0); + if (conn_chan == NULL || Tcl_GetChannelType(conn_chan) != &Pg_ConnType) + { + Tcl_ResetResult(interp); + Tcl_AppendResult(interp, id, " is not a valid postgresql connection", 0); + return (PGconn *) NULL; + } - connid = (Pg_ConnectionId *)Tcl_GetChannelInstanceData(conn_chan); - if (connid_p) - *connid_p = connid; - return connid->conn; + connid = (Pg_ConnectionId *) Tcl_GetChannelInstanceData(conn_chan); + if (connid_p) + *connid_p = connid; + return connid->conn; } @@ -240,55 +260,58 @@ PgGetConnectionId(Tcl_Interp *interp, char *id, Pg_ConnectionId **connid_p) * Remove a connection Id from the hash table and * close all portals the user forgot. */ -int PgDelConnectionId(DRIVER_DEL_PROTO) +int +PgDelConnectionId(DRIVER_DEL_PROTO) { - Tcl_HashEntry *entry; - Tcl_HashSearch hsearch; - Pg_ConnectionId *connid; - Pg_TclNotifies *notifies; - int i; - - connid = (Pg_ConnectionId *)cData; - - for (i = 0; i < connid->res_max; i++) { - if (connid->results[i]) - PQclear(connid->results[i]); - } - ckfree((void*)connid->results); - - /* Release associated notify info */ - while ((notifies = connid->notify_list) != NULL) { - connid->notify_list = notifies->next; - for (entry = Tcl_FirstHashEntry(¬ifies->notify_hash, &hsearch); - entry != NULL; - entry = Tcl_NextHashEntry(&hsearch)) { - ckfree((char*) Tcl_GetHashValue(entry)); + Tcl_HashEntry *entry; + Tcl_HashSearch hsearch; + Pg_ConnectionId *connid; + Pg_TclNotifies *notifies; + int i; + + connid = (Pg_ConnectionId *) cData; + + for (i = 0; i < connid->res_max; i++) + { + if (connid->results[i]) + PQclear(connid->results[i]); } - Tcl_DeleteHashTable(¬ifies->notify_hash); - Tcl_DontCallWhenDeleted(notifies->interp, PgNotifyInterpDelete, - (ClientData) notifies); - ckfree((void*) notifies); - } - - /* Turn off the Tcl event source for this connection, - * and delete any pending notify events. - */ - PgStopNotifyEventSource(connid); - - /* Close the libpq connection too */ - PQfinish(connid->conn); - connid->conn = NULL; - - /* - * We must use Tcl_EventuallyFree because we don't want the connid struct - * to vanish instantly if Pg_Notify_EventProc is active for it. - * (Otherwise, closing the connection from inside a pg_listen callback - * could lead to coredump.) Pg_Notify_EventProc can detect that the - * connection has been deleted from under it by checking connid->conn. - */ - Tcl_EventuallyFree((ClientData) connid, TCL_DYNAMIC); - - return 0; + ckfree((void *) connid->results); + + /* Release associated notify info */ + while ((notifies = connid->notify_list) != NULL) + { + connid->notify_list = notifies->next; + for (entry = Tcl_FirstHashEntry(¬ifies->notify_hash, &hsearch); + entry != NULL; + entry = Tcl_NextHashEntry(&hsearch)) + ckfree((char *) Tcl_GetHashValue(entry)); + Tcl_DeleteHashTable(¬ifies->notify_hash); + Tcl_DontCallWhenDeleted(notifies->interp, PgNotifyInterpDelete, + (ClientData) notifies); + ckfree((void *) notifies); + } + + /* + * Turn off the Tcl event source for this connection, and delete any + * pending notify events. + */ + PgStopNotifyEventSource(connid); + + /* Close the libpq connection too */ + PQfinish(connid->conn); + connid->conn = NULL; + + /* + * We must use Tcl_EventuallyFree because we don't want the connid + * struct to vanish instantly if Pg_Notify_EventProc is active for it. + * (Otherwise, closing the connection from inside a pg_listen callback + * could lead to coredump.) Pg_Notify_EventProc can detect that the + * connection has been deleted from under it by checking connid->conn. + */ + Tcl_EventuallyFree((ClientData) connid, TCL_DYNAMIC); + + return 0; } @@ -298,102 +321,110 @@ int PgDelConnectionId(DRIVER_DEL_PROTO) * is probably just not clearing result handles like they should. */ int -PgSetResultId(Tcl_Interp *interp, char *connid_c, PGresult *res) +PgSetResultId(Tcl_Interp * interp, char *connid_c, PGresult *res) { - Tcl_Channel conn_chan; - Pg_ConnectionId *connid; - int resid, i; - char buf[32]; + Tcl_Channel conn_chan; + Pg_ConnectionId *connid; + int resid, + i; + char buf[32]; - conn_chan = Tcl_GetChannel(interp, connid_c, 0); - if(conn_chan == NULL) - return TCL_ERROR; - connid = (Pg_ConnectionId *)Tcl_GetChannelInstanceData(conn_chan); + conn_chan = Tcl_GetChannel(interp, connid_c, 0); + if (conn_chan == NULL) + return TCL_ERROR; + connid = (Pg_ConnectionId *) Tcl_GetChannelInstanceData(conn_chan); - for (resid = connid->res_last+1; resid != connid->res_last; resid++) { - if (resid == connid->res_max) - resid = 0; - if (!connid->results[resid]) + for (resid = connid->res_last + 1; resid != connid->res_last; resid++) { - connid->res_last = resid; - break; + if (resid == connid->res_max) + resid = 0; + if (!connid->results[resid]) + { + connid->res_last = resid; + break; + } } - } - if (connid->results[resid]) { - if (connid->res_max == connid->res_hardmax) { - Tcl_SetResult(interp, "hard limit on result handles reached", - TCL_STATIC); - return TCL_ERROR; + if (connid->results[resid]) + { + if (connid->res_max == connid->res_hardmax) + { + Tcl_SetResult(interp, "hard limit on result handles reached", + TCL_STATIC); + return TCL_ERROR; + } + connid->res_last = connid->res_max; + resid = connid->res_max; + connid->res_max *= 2; + if (connid->res_max > connid->res_hardmax) + connid->res_max = connid->res_hardmax; + connid->results = (PGresult **) ckrealloc((void *) connid->results, + sizeof(PGresult *) * connid->res_max); + for (i = connid->res_last; i < connid->res_max; i++) + connid->results[i] = NULL; } - connid->res_last = connid->res_max; - resid = connid->res_max; - connid->res_max *= 2; - if (connid->res_max > connid->res_hardmax) - connid->res_max = connid->res_hardmax; - connid->results = (PGresult**)ckrealloc((void*)connid->results, - sizeof(PGresult*) * connid->res_max); - for (i = connid->res_last; i < connid->res_max; i++) - connid->results[i] = NULL; - } - - connid->results[resid] = res; - sprintf(buf, "%s.%d", connid_c, resid); - Tcl_SetResult(interp, buf, TCL_VOLATILE); - return resid; + + connid->results[resid] = res; + sprintf(buf, "%s.%d", connid_c, resid); + Tcl_SetResult(interp, buf, TCL_VOLATILE); + return resid; } -static int getresid(Tcl_Interp *interp, char *id, Pg_ConnectionId **connid_p) +static int +getresid(Tcl_Interp * interp, char *id, Pg_ConnectionId ** connid_p) { - Tcl_Channel conn_chan; - char *mark; - int resid; - Pg_ConnectionId *connid; - - if (!(mark = strchr(id, '.'))) - return -1; - *mark = '\0'; - conn_chan = Tcl_GetChannel(interp, id, 0); - *mark = '.'; - if(conn_chan == NULL || Tcl_GetChannelType(conn_chan) != &Pg_ConnType) { - Tcl_SetResult(interp, "Invalid connection handle", TCL_STATIC); - return -1; - } - - if (Tcl_GetInt(interp, mark + 1, &resid) == TCL_ERROR) { - Tcl_SetResult(interp, "Poorly formated result handle", TCL_STATIC); - return -1; - } - - connid = (Pg_ConnectionId *)Tcl_GetChannelInstanceData(conn_chan); - - if (resid < 0 || resid > connid->res_max || connid->results[resid] == NULL) { - Tcl_SetResult(interp, "Invalid result handle", TCL_STATIC); - return -1; - } - - *connid_p = connid; - - return resid; + Tcl_Channel conn_chan; + char *mark; + int resid; + Pg_ConnectionId *connid; + + if (!(mark = strchr(id, '.'))) + return -1; + *mark = '\0'; + conn_chan = Tcl_GetChannel(interp, id, 0); + *mark = '.'; + if (conn_chan == NULL || Tcl_GetChannelType(conn_chan) != &Pg_ConnType) + { + Tcl_SetResult(interp, "Invalid connection handle", TCL_STATIC); + return -1; + } + + if (Tcl_GetInt(interp, mark + 1, &resid) == TCL_ERROR) + { + Tcl_SetResult(interp, "Poorly formated result handle", TCL_STATIC); + return -1; + } + + connid = (Pg_ConnectionId *) Tcl_GetChannelInstanceData(conn_chan); + + if (resid < 0 || resid > connid->res_max || connid->results[resid] == NULL) + { + Tcl_SetResult(interp, "Invalid result handle", TCL_STATIC); + return -1; + } + + *connid_p = connid; + + return resid; } /* * Get back the result pointer from the Id */ -PGresult * -PgGetResultId(Tcl_Interp *interp, char *id) +PGresult * +PgGetResultId(Tcl_Interp * interp, char *id) { - Pg_ConnectionId *connid; - int resid; - - if (!id) - return NULL; - resid = getresid(interp, id, &connid); - if (resid == -1) - return NULL; - return connid->results[resid]; + Pg_ConnectionId *connid; + int resid; + + if (!id) + return NULL; + resid = getresid(interp, id, &connid); + if (resid == -1) + return NULL; + return connid->results[resid]; } @@ -401,15 +432,15 @@ PgGetResultId(Tcl_Interp *interp, char *id) * Remove a result Id from the hash tables */ void -PgDelResultId(Tcl_Interp *interp, char *id) +PgDelResultId(Tcl_Interp * interp, char *id) { - Pg_ConnectionId *connid; - int resid; + Pg_ConnectionId *connid; + int resid; - resid = getresid(interp, id, &connid); - if (resid == -1) - return; - connid->results[resid] = 0; + resid = getresid(interp, id, &connid); + if (resid == -1) + return; + connid->results[resid] = 0; } @@ -417,25 +448,26 @@ PgDelResultId(Tcl_Interp *interp, char *id) * Get the connection Id from the result Id */ int -PgGetConnByResultId(Tcl_Interp *interp, char *resid_c) +PgGetConnByResultId(Tcl_Interp * interp, char *resid_c) { - char *mark; - Tcl_Channel conn_chan; - - if (!(mark = strchr(resid_c, '.'))) - goto error_out; - *mark = '\0'; - conn_chan = Tcl_GetChannel(interp, resid_c, 0); - *mark = '.'; - if(conn_chan && Tcl_GetChannelType(conn_chan) == &Pg_ConnType) { - Tcl_SetResult(interp, Tcl_GetChannelName(conn_chan), TCL_VOLATILE); - return TCL_OK; - } - - error_out: - Tcl_ResetResult(interp); - Tcl_AppendResult(interp, resid_c, " is not a valid connection\n", 0); - return TCL_ERROR; + char *mark; + Tcl_Channel conn_chan; + + if (!(mark = strchr(resid_c, '.'))) + goto error_out; + *mark = '\0'; + conn_chan = Tcl_GetChannel(interp, resid_c, 0); + *mark = '.'; + if (conn_chan && Tcl_GetChannelType(conn_chan) == &Pg_ConnType) + { + Tcl_SetResult(interp, Tcl_GetChannelName(conn_chan), TCL_VOLATILE); + return TCL_OK; + } + +error_out: + Tcl_ResetResult(interp); + Tcl_AppendResult(interp, resid_c, " is not a valid connection\n", 0); + return TCL_ERROR; } @@ -458,137 +490,146 @@ PgGetConnByResultId(Tcl_Interp *interp, char *resid_c) the channel can outlive the interpreter it was created by!) Upon closure of the channel, we immediately delete any pending events that reference it. But for interpreter deletion, we just set any - matching interp pointers in the Pg_TclNotifies list to NULL. The + matching interp pointers in the Pg_TclNotifies list to NULL. The list item stays around until the connection is deleted. (This avoids trouble with walking through a list whose members may get deleted under us.) *******************************************/ -typedef struct { - Tcl_Event header; /* Standard Tcl event info */ - PGnotify info; /* Notify name from SQL server */ - Pg_ConnectionId *connid; /* Connection for server */ -} NotifyEvent; +typedef struct +{ + Tcl_Event header; /* Standard Tcl event info */ + PGnotify info; /* Notify name from SQL server */ + Pg_ConnectionId *connid; /* Connection for server */ +} NotifyEvent; /* Setup before waiting in event loop */ -static void Pg_Notify_SetupProc (ClientData clientData, int flags) +static void +Pg_Notify_SetupProc(ClientData clientData, int flags) { - Pg_ConnectionId *connid = (Pg_ConnectionId *) clientData; - Tcl_File handle; - int pqsock; - - /* We classify SQL notifies as Tcl file events. */ - if (!(flags & TCL_FILE_EVENTS)) { - return; - } - - /* Set up to watch for asynchronous data arrival on backend channel */ - pqsock = PQsocket(connid->conn); - if (pqsock < 0) - return; - - handle = Tcl_GetFile((ClientData) pqsock, TCL_UNIX_FD); - Tcl_WatchFile(handle, TCL_READABLE); + Pg_ConnectionId *connid = (Pg_ConnectionId *) clientData; + Tcl_File handle; + int pqsock; + + /* We classify SQL notifies as Tcl file events. */ + if (!(flags & TCL_FILE_EVENTS)) + return; + + /* Set up to watch for asynchronous data arrival on backend channel */ + pqsock = PQsocket(connid->conn); + if (pqsock < 0) + return; + + handle = Tcl_GetFile((ClientData) pqsock, TCL_UNIX_FD); + Tcl_WatchFile(handle, TCL_READABLE); } /* Check to see if events have arrived in event loop */ -static void Pg_Notify_CheckProc (ClientData clientData, int flags) +static void +Pg_Notify_CheckProc(ClientData clientData, int flags) { - Pg_ConnectionId *connid = (Pg_ConnectionId *) clientData; - Tcl_File handle; - int pqsock; - - /* We classify SQL notifies as Tcl file events. */ - if (!(flags & TCL_FILE_EVENTS)) { - return; - } - - /* Consume any data available from the SQL server - * (this just buffers it internally to libpq). - * We use Tcl_FileReady to avoid a useless kernel call - * when no data is available. - */ - pqsock = PQsocket(connid->conn); - if (pqsock < 0) - return; - - handle = Tcl_GetFile((ClientData) pqsock, TCL_UNIX_FD); - if (Tcl_FileReady(handle, TCL_READABLE) != 0) { - PQconsumeInput(connid->conn); - } - - /* Transfer notify events from libpq to Tcl event queue. */ - PgNotifyTransferEvents(connid); + Pg_ConnectionId *connid = (Pg_ConnectionId *) clientData; + Tcl_File handle; + int pqsock; + + /* We classify SQL notifies as Tcl file events. */ + if (!(flags & TCL_FILE_EVENTS)) + return; + + /* + * Consume any data available from the SQL server (this just buffers + * it internally to libpq). We use Tcl_FileReady to avoid a useless + * kernel call when no data is available. + */ + pqsock = PQsocket(connid->conn); + if (pqsock < 0) + return; + + handle = Tcl_GetFile((ClientData) pqsock, TCL_UNIX_FD); + if (Tcl_FileReady(handle, TCL_READABLE) != 0) + PQconsumeInput(connid->conn); + + /* Transfer notify events from libpq to Tcl event queue. */ + PgNotifyTransferEvents(connid); } /* Dispatch an event that has reached the front of the event queue */ -static int Pg_Notify_EventProc (Tcl_Event *evPtr, int flags) +static int +Pg_Notify_EventProc(Tcl_Event * evPtr, int flags) { - NotifyEvent *event = (NotifyEvent *) evPtr; - Pg_TclNotifies *notifies; - Tcl_HashEntry *entry; - char *callback; - char *svcallback; - - /* We classify SQL notifies as Tcl file events. */ - if (!(flags & TCL_FILE_EVENTS)) { - return 0; - } - - /* Preserve/Release to ensure the connection struct doesn't disappear - * underneath us. - */ - Tcl_Preserve((ClientData) event->connid); - - /* - * Loop for each interpreter that has ever registered on the connection. - * Each one can get a callback. - */ - - for (notifies = event->connid->notify_list; - notifies != NULL; - notifies = notifies->next) { - Tcl_Interp *interp = notifies->interp; - if (interp == NULL) - continue; /* ignore deleted interpreter */ - /* - * Find the callback to be executed for this interpreter, if any. - */ - entry = Tcl_FindHashEntry(¬ifies->notify_hash, - event->info.relname); - if (entry == NULL) - continue; /* no pg_listen in this interpreter */ - callback = (char *) Tcl_GetHashValue(entry); - if (callback == NULL) - continue; /* safety check -- shouldn't happen */ + NotifyEvent *event = (NotifyEvent *) evPtr; + Pg_TclNotifies *notifies; + Tcl_HashEntry *entry; + char *callback; + char *svcallback; + + /* We classify SQL notifies as Tcl file events. */ + if (!(flags & TCL_FILE_EVENTS)) + return 0; + /* - * We have to copy the callback string in case the user executes - * a new pg_listen during the callback. + * Preserve/Release to ensure the connection struct doesn't disappear + * underneath us. */ - svcallback = (char *) ckalloc((unsigned) (strlen(callback) + 1)); - strcpy(svcallback, callback); + Tcl_Preserve((ClientData) event->connid); + /* - * Execute the callback. + * Loop for each interpreter that has ever registered on the + * connection. Each one can get a callback. */ - Tcl_Preserve((ClientData) interp); - if (Tcl_GlobalEval(interp, svcallback) != TCL_OK) { - Tcl_AddErrorInfo(interp, "\n (\"pg_listen\" script)"); - Tcl_BackgroundError(interp); + + for (notifies = event->connid->notify_list; + notifies != NULL; + notifies = notifies->next) + { + Tcl_Interp *interp = notifies->interp; + + if (interp == NULL) + continue; /* ignore deleted interpreter */ + + /* + * Find the callback to be executed for this interpreter, if any. + */ + entry = Tcl_FindHashEntry(¬ifies->notify_hash, + event->info.relname); + if (entry == NULL) + continue; /* no pg_listen in this interpreter */ + callback = (char *) Tcl_GetHashValue(entry); + if (callback == NULL) + continue; /* safety check -- shouldn't happen */ + + /* + * We have to copy the callback string in case the user executes a + * new pg_listen during the callback. + */ + svcallback = (char *) ckalloc((unsigned) (strlen(callback) + 1)); + strcpy(svcallback, callback); + + /* + * Execute the callback. + */ + Tcl_Preserve((ClientData) interp); + if (Tcl_GlobalEval(interp, svcallback) != TCL_OK) + { + Tcl_AddErrorInfo(interp, "\n (\"pg_listen\" script)"); + Tcl_BackgroundError(interp); + } + Tcl_Release((ClientData) interp); + ckfree(svcallback); + + /* + * Check for the possibility that the callback closed the + * connection. + */ + if (event->connid->conn == NULL) + break; } - Tcl_Release((ClientData) interp); - ckfree(svcallback); - /* - * Check for the possibility that the callback closed the connection. - */ - if (event->connid->conn == NULL) - break; - } - Tcl_Release((ClientData) event->connid); + Tcl_Release((ClientData) event->connid); - return 1; + return 1; } /* @@ -598,18 +639,21 @@ static int Pg_Notify_EventProc (Tcl_Event *evPtr, int flags) * (to capture notifies that arrive when we're idle). */ -void PgNotifyTransferEvents (Pg_ConnectionId *connid) +void +PgNotifyTransferEvents(Pg_ConnectionId * connid) { - PGnotify *notify; - - while ((notify = PQnotifies(connid->conn)) != NULL) { - NotifyEvent *event = (NotifyEvent *) ckalloc(sizeof(NotifyEvent)); - event->header.proc = Pg_Notify_EventProc; - event->info = *notify; - event->connid = connid; - Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL); - free(notify); - } + PGnotify *notify; + + while ((notify = PQnotifies(connid->conn)) != NULL) + { + NotifyEvent *event = (NotifyEvent *) ckalloc(sizeof(NotifyEvent)); + + event->header.proc = Pg_Notify_EventProc; + event->info = *notify; + event->connid = connid; + Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL); + free(notify); + } } /* @@ -621,27 +665,28 @@ void PgNotifyTransferEvents (Pg_ConnectionId *connid) * rid of pending Tcl events that reference a dying connection. */ -void PgNotifyInterpDelete(ClientData clientData, Tcl_Interp *interp) +void +PgNotifyInterpDelete(ClientData clientData, Tcl_Interp * interp) { - /* Mark the interpreter dead, but don't do anything else yet */ - Pg_TclNotifies *notifies = (Pg_TclNotifies *) clientData; - notifies->interp = NULL; + /* Mark the interpreter dead, but don't do anything else yet */ + Pg_TclNotifies *notifies = (Pg_TclNotifies *) clientData; + + notifies->interp = NULL; } /* Comparison routine for detecting events to be removed by DeleteEvent */ -static int NotifyEventDeleteProc(Tcl_Event *evPtr, ClientData clientData) +static int +NotifyEventDeleteProc(Tcl_Event * evPtr, ClientData clientData) { - NotifyEvent *event; - Pg_ConnectionId *connid = (Pg_ConnectionId *) clientData; - - if (evPtr->proc != Pg_Notify_EventProc) { - return 0; - } - event = (NotifyEvent *) evPtr; - if (event->connid != connid) { - return 0; - } - return 1; + NotifyEvent *event; + Pg_ConnectionId *connid = (Pg_ConnectionId *) clientData; + + if (evPtr->proc != Pg_Notify_EventProc) + return 0; + event = (NotifyEvent *) evPtr; + if (event->connid != connid) + return 0; + return 1; } /* Start and stop the notify event source for a connection. @@ -651,24 +696,28 @@ static int NotifyEventDeleteProc(Tcl_Event *evPtr, ClientData clientData) * closed. */ -void PgStartNotifyEventSource(Pg_ConnectionId *connid) +void +PgStartNotifyEventSource(Pg_ConnectionId * connid) { - /* Start the notify event source if it isn't already running */ - if (! connid->notifier_running) { - Tcl_CreateEventSource(Pg_Notify_SetupProc, Pg_Notify_CheckProc, - (ClientData) connid); - connid->notifier_running = 1; - } + /* Start the notify event source if it isn't already running */ + if (!connid->notifier_running) + { + Tcl_CreateEventSource(Pg_Notify_SetupProc, Pg_Notify_CheckProc, + (ClientData) connid); + connid->notifier_running = 1; + } } -void PgStopNotifyEventSource(Pg_ConnectionId *connid) +void +PgStopNotifyEventSource(Pg_ConnectionId * connid) { - /* Remove the event source */ - if (connid->notifier_running) { - Tcl_DeleteEventSource(Pg_Notify_SetupProc, Pg_Notify_CheckProc, - (ClientData) connid); - connid->notifier_running = 0; - } - /* Kill any queued Tcl events that reference this channel */ - Tcl_DeleteEvents(NotifyEventDeleteProc, (ClientData) connid); + /* Remove the event source */ + if (connid->notifier_running) + { + Tcl_DeleteEventSource(Pg_Notify_SetupProc, Pg_Notify_CheckProc, + (ClientData) connid); + connid->notifier_running = 0; + } + /* Kill any queued Tcl events that reference this channel */ + Tcl_DeleteEvents(NotifyEventDeleteProc, (ClientData) connid); } diff --git a/src/interfaces/libpgtcl/pgtclId.h b/src/interfaces/libpgtcl/pgtclId.h index 815b11db34..addb4e260c 100644 --- a/src/interfaces/libpgtcl/pgtclId.h +++ b/src/interfaces/libpgtcl/pgtclId.h @@ -1,50 +1,50 @@ /*------------------------------------------------------------------------- * * pgtclId.h-- -* useful routines to convert between strings and pointers +* useful routines to convert between strings and pointers * Needed because everything in tcl is a string, but often, pointers * to data structures are needed. -* +* * * Copyright (c) 1994, Regents of the University of California * -* $Id: pgtclId.h,v 1.7 1998/06/16 04:10:17 momjian Exp $ +* $Id: pgtclId.h,v 1.8 1998/09/01 04:39:59 momjian Exp $ * *------------------------------------------------------------------------- */ - -extern void PgSetConnectionId(Tcl_Interp *interp, PGconn *conn); + +extern void PgSetConnectionId(Tcl_Interp * interp, PGconn *conn); #if TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION == 5 /* Only Tcl 7.5 had drivers with this signature */ -# define DRIVER_DEL_PROTO ClientData cData, Tcl_Interp *interp, \ - Tcl_File inFile, Tcl_File outFile -# define DRIVER_OUTPUT_PROTO ClientData cData, Tcl_File outFile, char *buf, \ +#define DRIVER_DEL_PROTO ClientData cData, Tcl_Interp *interp, \ + Tcl_File inFile, Tcl_File outFile +#define DRIVER_OUTPUT_PROTO ClientData cData, Tcl_File outFile, char *buf, \ int bufSize, int *errorCodePtr -# define DRIVER_INPUT_PROTO ClientData cData, Tcl_File inFile, char *buf, \ +#define DRIVER_INPUT_PROTO ClientData cData, Tcl_File inFile, char *buf, \ int bufSize, int *errorCodePtr #else /* Tcl 7.6 and beyond use this signature */ -# define DRIVER_OUTPUT_PROTO ClientData cData, char *buf, int bufSize, \ +#define DRIVER_OUTPUT_PROTO ClientData cData, char *buf, int bufSize, \ int *errorCodePtr -# define DRIVER_INPUT_PROTO ClientData cData, char *buf, int bufSize, \ +#define DRIVER_INPUT_PROTO ClientData cData, char *buf, int bufSize, \ int *errorCodePtr -# define DRIVER_DEL_PROTO ClientData cData, Tcl_Interp *interp +#define DRIVER_DEL_PROTO ClientData cData, Tcl_Interp *interp #endif -extern PGconn *PgGetConnectionId(Tcl_Interp *interp, char *id, \ - Pg_ConnectionId **); -extern PgDelConnectionId(DRIVER_DEL_PROTO); -extern int PgOutputProc(DRIVER_OUTPUT_PROTO); -extern PgInputProc(DRIVER_INPUT_PROTO); -extern int PgSetResultId(Tcl_Interp *interp, char *connid, PGresult *res); -extern PGresult *PgGetResultId(Tcl_Interp *interp, char *id); -extern void PgDelResultId(Tcl_Interp *interp, char *id); -extern int PgGetConnByResultId(Tcl_Interp *interp, char *resid); -extern void PgStartNotifyEventSource(Pg_ConnectionId *connid); -extern void PgStopNotifyEventSource(Pg_ConnectionId *connid); -extern void PgNotifyTransferEvents(Pg_ConnectionId *connid); -extern void PgNotifyInterpDelete(ClientData clientData, Tcl_Interp *interp); +extern PGconn *PgGetConnectionId(Tcl_Interp * interp, char *id, \ + Pg_ConnectionId **); +extern PgDelConnectionId(DRIVER_DEL_PROTO); +extern int PgOutputProc(DRIVER_OUTPUT_PROTO); +extern PgInputProc(DRIVER_INPUT_PROTO); +extern int PgSetResultId(Tcl_Interp * interp, char *connid, PGresult *res); +extern PGresult *PgGetResultId(Tcl_Interp * interp, char *id); +extern void PgDelResultId(Tcl_Interp * interp, char *id); +extern int PgGetConnByResultId(Tcl_Interp * interp, char *resid); +extern void PgStartNotifyEventSource(Pg_ConnectionId * connid); +extern void PgStopNotifyEventSource(Pg_ConnectionId * connid); +extern void PgNotifyTransferEvents(Pg_ConnectionId * connid); +extern void PgNotifyInterpDelete(ClientData clientData, Tcl_Interp * interp); /* GetFileProc is needed in Tcl 7.6 and later */ #if (TCL_MAJOR_VERSION * 100 + TCL_MINOR_VERSION) >= 706 @@ -55,6 +55,7 @@ extern void PgNotifyInterpDelete(ClientData clientData, Tcl_Interp *interp); #if HAVE_TCL_GETFILEPROC extern Tcl_File PgGetFileProc(ClientData cData, int direction); + #endif extern Tcl_ChannelType Pg_ConnType; diff --git a/src/interfaces/libpq/fe-auth.c b/src/interfaces/libpq/fe-auth.c index bd90b2314f..d3b2d2d274 100644 --- a/src/interfaces/libpq/fe-auth.c +++ b/src/interfaces/libpq/fe-auth.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.23 1998/09/01 03:28:50 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.24 1998/09/01 04:40:01 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -42,7 +42,7 @@ #include <unistd.h> #endif #include <pwd.h> -#endif /* WIN32 */ +#endif /* WIN32 */ #ifdef HAVE_CRYPT_H #include <crypt.h> @@ -56,7 +56,8 @@ struct authsvc { - char name[NAMEDATALEN]; /* service nickname (for command line) */ + char name[NAMEDATALEN]; /* service nickname (for command + * line) */ MsgType msgtype; /* startup packet header type */ int allowed; /* initially allowed (before command line * option parsing)? */ @@ -76,17 +77,17 @@ static struct authsvc authsvcs[] = { #ifdef KRB4 {"krb4", STARTUP_KRB4_MSG, 1}, {"kerberos", STARTUP_KRB4_MSG, 1}, -#endif /* KRB4 */ +#endif /* KRB4 */ #ifdef KRB5 {"krb5", STARTUP_KRB5_MSG, 1}, {"kerberos", STARTUP_KRB5_MSG, 1}, -#endif /* KRB5 */ +#endif /* KRB5 */ {UNAUTHNAME, STARTUP_MSG, #if defined(KRB4) || defined(KRB5) 0 #else /* !(KRB4 || KRB5) */ 1 -#endif /* !(KRB4 || KRB5) */ +#endif /* !(KRB4 || KRB5) */ }, {"password", STARTUP_PASSWORD_MSG, 0} }; @@ -223,7 +224,7 @@ pg_krb4_sendauth(const char *PQerrormsg, int sock, return STATUS_OK; } -#endif /* KRB4 */ +#endif /* KRB4 */ #ifdef KRB5 /*---------------------------------------------------------------- @@ -457,7 +458,7 @@ pg_krb5_sendauth(const char *PQerrormsg, int sock, return code ? STATUS_ERROR : STATUS_OK; } -#endif /* KRB5 */ +#endif /* KRB5 */ static int pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq) @@ -521,7 +522,7 @@ fe_sendauth(AuthRequest areq, PGconn *conn, const char *hostname, if (password == NULL || *password == '\0') { (void) sprintf(PQerrormsg, - "fe_sendauth: no password supplied\n"); + "fe_sendauth: no password supplied\n"); return STATUS_ERROR; } if (pg_password_sendauth(conn, password, areq) != STATUS_OK) @@ -607,10 +608,10 @@ fe_getauthname(char *PQerrormsg) case STARTUP_MSG: { #ifdef WIN32 - char username[128]; - DWORD namesize = sizeof(username) - 1; + char username[128]; + DWORD namesize = sizeof(username) - 1; - if (GetUserName(username,&namesize)) + if (GetUserName(username, &namesize)) name = username; #else struct passwd *pw = getpwuid(geteuid()); diff --git a/src/interfaces/libpq/fe-auth.h b/src/interfaces/libpq/fe-auth.h index c014204722..817aa88de7 100644 --- a/src/interfaces/libpq/fe-auth.h +++ b/src/interfaces/libpq/fe-auth.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: fe-auth.h,v 1.9 1998/08/17 03:50:33 scrappy Exp $ + * $Id: fe-auth.h,v 1.10 1998/09/01 04:40:03 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -30,10 +30,9 @@ #define DEFAULT_CLIENT_AUTHSVC UNAUTHNAME #else /* KRB4 || KRB5 */ #define DEFAULT_CLIENT_AUTHSVC "kerberos" -#endif /* KRB4 || KRB5 */ +#endif /* KRB4 || KRB5 */ -extern int -fe_sendauth(AuthRequest areq, PGconn *conn, const char *hostname, +extern int fe_sendauth(AuthRequest areq, PGconn *conn, const char *hostname, const char *password, char *PQerrormsg); extern MsgType fe_getauthsvc(char *PQerrormsg); extern void fe_setauthsvc(const char *name, char *PQerrormsg); @@ -42,4 +41,4 @@ extern char *fe_getauthname(char *PQerrormsg); #define PG_KRB4_VERSION "PGVER4.1" /* at most KRB_SENDAUTH_VLEN chars */ #define PG_KRB5_VERSION "PGVER5.1" -#endif /* FE_AUTH_H */ +#endif /* FE_AUTH_H */ diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 30f9933950..85a95c9727 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.79 1998/08/17 03:50:34 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.80 1998/09/01 04:40:04 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -50,7 +50,8 @@ static void closePGconn(PGconn *conn); static int conninfo_parse(const char *conninfo, char *errorMessage); static char *conninfo_getval(char *keyword); static void conninfo_free(void); -static void defaultNoticeProcessor(void * arg, const char * message); +static void defaultNoticeProcessor(void *arg, const char *message); + /* XXX Why is this not static? */ void PQsetenv(PGconn *conn); @@ -282,8 +283,10 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, cons { PGconn *conn; char *tmp; + /* An error message from some service we call. */ bool error = FALSE; + /* We encountered an error that prevents successful completion */ int i; @@ -359,9 +362,10 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, cons if (conn->dbName) { + /* - * if the database name is surrounded by double-quotes, then - * don't convert case + * if the database name is surrounded by double-quotes, then don't + * convert case */ if (*conn->dbName == '"') { @@ -370,8 +374,8 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, cons } else for (i = 0; conn->dbName[i]; i++) - if (isascii((unsigned char)conn->dbName[i]) && - isupper(conn->dbName[i])) + if (isascii((unsigned char) conn->dbName[i]) && + isupper(conn->dbName[i])) conn->dbName[i] = tolower(conn->dbName[i]); } @@ -392,57 +396,63 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, cons static int update_db_info(PGconn *conn) { - char *tmp, *old = conn->dbName; - + char *tmp, + *old = conn->dbName; + if (strchr(conn->dbName, '@') != NULL) { /* old style: dbname[@server][:port] */ tmp = strrchr(conn->dbName, ':'); - if (tmp != NULL) /* port number given */ + if (tmp != NULL) /* port number given */ { - conn->pgport = strdup(tmp + 1); + conn->pgport = strdup(tmp + 1); *tmp = '\0'; } - + tmp = strrchr(conn->dbName, '@'); - if (tmp != NULL) /* host name given */ + if (tmp != NULL) /* host name given */ { - conn->pghost = strdup(tmp + 1); + conn->pghost = strdup(tmp + 1); *tmp = '\0'; } - + conn->dbName = strdup(old); free(old); } else { - int offset; - + int offset; + /* - * only allow protocols tcp and unix - */ + * only allow protocols tcp and unix + */ if (strncmp(conn->dbName, "tcp:", 4) == 0) offset = 4; else if (strncmp(conn->dbName, "unix:", 5) == 0) offset = 5; - else return 0; - + else + return 0; + if (strncmp(conn->dbName + offset, "postgresql://", strlen("postgresql://")) == 0) { - /* new style: <tcp|unix>:postgresql://server[:port][/dbname][?options] */ + + /* + * new style: + * <tcp|unix>:postgresql://server[:port][/dbname][?options] + */ offset += strlen("postgresql://"); - + tmp = strrchr(conn->dbName + offset, '?'); - if (tmp != NULL) /* options given */ + if (tmp != NULL) /* options given */ { - conn->pgoptions = strdup(tmp + 1); + conn->pgoptions = strdup(tmp + 1); *tmp = '\0'; } - + tmp = strrchr(conn->dbName + offset, '/'); - if (tmp != NULL) /* database name given */ + if (tmp != NULL) /* database name given */ { - conn->dbName = strdup(tmp + 1); + conn->dbName = strdup(tmp + 1); *tmp = '\0'; } else @@ -452,30 +462,31 @@ update_db_info(PGconn *conn) else if (conn->pguser) conn->dbName = strdup(conn->pguser); } - + tmp = strrchr(old + offset, ':'); - if (tmp != NULL) /* port number given */ + if (tmp != NULL) /* port number given */ { - conn->pgport = strdup(tmp + 1); + conn->pgport = strdup(tmp + 1); *tmp = '\0'; } - + if (strncmp(old, "unix:", 5) == 0) { conn->pghost = NULL; if (strcmp(old + offset, "localhost") != 0) { (void) sprintf(conn->errorMessage, - "connectDB() -- non-tcp access only possible on localhost\n"); - return 1; + "connectDB() -- non-tcp access only possible on localhost\n"); + return 1; } } - else conn->pghost = strdup(old + offset); - + else + conn->pghost = strdup(old + offset); + free(old); } } - + return 0; } @@ -498,12 +509,12 @@ connectDB(PGconn *conn) char beresp; int on = 1; - /* - * parse dbName to get all additional info in it, if any - */ + /* + * parse dbName to get all additional info in it, if any + */ if (update_db_info(conn) != 0) goto connect_errReturn; - + /* * Initialize the startup packet. */ @@ -535,7 +546,8 @@ connectDB(PGconn *conn) } family = AF_INET; } - else { + else + { hp = NULL; family = AF_UNIX; } @@ -556,7 +568,7 @@ connectDB(PGconn *conn) else conn->raddr_len = UNIXSOCK_PATH(conn->raddr.un, portno); #endif - + /* Connect to the server */ if ((conn->sock = socket(family, SOCK_STREAM, 0)) < 0) @@ -577,14 +589,14 @@ connectDB(PGconn *conn) } /* - * Set the right options. - * We need nonblocking I/O, and we don't want delay of outgoing data. + * Set the right options. We need nonblocking I/O, and we don't want + * delay of outgoing data. */ #ifndef WIN32 if (fcntl(conn->sock, F_SETFL, O_NONBLOCK) < 0) #else - if (ioctlsocket(conn->sock,FIONBIO, &on) != 0) + if (ioctlsocket(conn->sock, FIONBIO, &on) != 0) #endif { (void) sprintf(conn->errorMessage, @@ -606,16 +618,16 @@ connectDB(PGconn *conn) } if (setsockopt(conn->sock, pe->p_proto, TCP_NODELAY, #ifdef WIN32 - (char *) + (char *) #endif - &on, + &on, sizeof(on)) < 0) { (void) sprintf(conn->errorMessage, - "connectDB() -- setsockopt failed: errno=%d\n%s\n", + "connectDB() -- setsockopt failed: errno=%d\n%s\n", errno, strerror(errno)); #ifdef WIN32 - printf("Winsock error: %i\n",WSAGetLastError()); + printf("Winsock error: %i\n", WSAGetLastError()); #endif goto connect_errReturn; } @@ -626,7 +638,7 @@ connectDB(PGconn *conn) if (getsockname(conn->sock, &conn->laddr.sa, &laddrlen) < 0) { (void) sprintf(conn->errorMessage, - "connectDB() -- getsockname() failed: errno=%d\n%s\n", + "connectDB() -- getsockname() failed: errno=%d\n%s\n", errno, strerror(errno)); goto connect_errReturn; } @@ -640,15 +652,15 @@ connectDB(PGconn *conn) if (pqPacketSend(conn, (char *) &sp, sizeof(StartupPacket)) != STATUS_OK) { sprintf(conn->errorMessage, - "connectDB() -- couldn't send startup packet: errno=%d\n%s\n", + "connectDB() -- couldn't send startup packet: errno=%d\n%s\n", errno, strerror(errno)); goto connect_errReturn; } /* - * Perform the authentication exchange: - * wait for backend messages and respond as necessary. - * We fall out of this loop when done talking to the postmaster. + * Perform the authentication exchange: wait for backend messages and + * respond as necessary. We fall out of this loop when done talking to + * the postmaster. */ for (;;) @@ -659,8 +671,10 @@ connectDB(PGconn *conn) /* Load data, or detect EOF */ if (pqReadData(conn) < 0) goto connect_errReturn; - /* Scan the message. - * If we run out of data, loop around to try again. + + /* + * Scan the message. If we run out of data, loop around to try + * again. */ conn->inCursor = conn->inStart; @@ -679,7 +693,7 @@ connectDB(PGconn *conn) if (beresp != 'R') { (void) sprintf(conn->errorMessage, - "connectDB() -- expected authentication request\n"); + "connectDB() -- expected authentication request\n"); goto connect_errReturn; } @@ -710,20 +724,21 @@ connectDB(PGconn *conn) } /* - * Now we expect to hear from the backend. - * A ReadyForQuery message indicates that startup is successful, - * but we might also get an Error message indicating failure. - * (Notice messages indicating nonfatal warnings are also allowed - * by the protocol, as is a BackendKeyData message.) - * Easiest way to handle this is to let PQgetResult() read the messages. - * We just have to fake it out about the state of the connection. + * Now we expect to hear from the backend. A ReadyForQuery message + * indicates that startup is successful, but we might also get an + * Error message indicating failure. (Notice messages indicating + * nonfatal warnings are also allowed by the protocol, as is a + * BackendKeyData message.) Easiest way to handle this is to let + * PQgetResult() read the messages. We just have to fake it out about + * the state of the connection. */ conn->status = CONNECTION_OK; conn->asyncStatus = PGASYNC_BUSY; res = PQgetResult(conn); /* NULL return indicating we have gone to IDLE state is expected */ - if (res) { + if (res) + { if (res->resultStatus != PGRES_FATAL_ERROR) sprintf(conn->errorMessage, "connectDB() -- unexpected message during startup\n"); @@ -731,16 +746,17 @@ connectDB(PGconn *conn) goto connect_errReturn; } - /* Given the new protocol that sends a ReadyForQuery message - * after successful backend startup, it should no longer be - * necessary to send an empty query to test for startup. + /* + * Given the new protocol that sends a ReadyForQuery message after + * successful backend startup, it should no longer be necessary to + * send an empty query to test for startup. */ #if 0 /* - * Send a blank query to make sure everything works; in - * particular, that the database exists. + * Send a blank query to make sure everything works; in particular, + * that the database exists. */ res = PQexec(conn, " "); if (res == NULL || res->resultStatus != PGRES_EMPTY_QUERY) @@ -754,8 +770,8 @@ connectDB(PGconn *conn) #endif - /* Post-connection housekeeping. - * Send environment variables to server + /* + * Post-connection housekeeping. Send environment variables to server */ PQsetenv(conn); @@ -781,32 +797,38 @@ PQsetenv(PGconn *conn) { struct EnvironmentOptions *eo; char setQuery[80]; /* mjl: size okay? XXX */ + #ifdef MULTIBYTE - char *envname = "PGCLIENTENCODING"; - static char envbuf[64]; /* big enough? */ - char *env; - char *encoding = 0; + char *envname = "PGCLIENTENCODING"; + static char envbuf[64]; /* big enough? */ + char *env; + char *encoding = 0; PGresult *rtn; + #endif #ifdef MULTIBYTE /* query server encoding */ env = getenv(envname); - if (!env) { - rtn = PQexec(conn, "select getdatabaseencoding()"); - if (rtn && PQresultStatus(rtn) == PGRES_TUPLES_OK) { - encoding = PQgetvalue(rtn,0,0); - if (encoding) { - /* set client encoding */ - sprintf(envbuf,"%s=%s",envname,encoding); - putenv(envbuf); - } - PQclear(rtn); - } - if (!encoding) { /* this should not happen */ - sprintf(envbuf,"%s=%s",envname,pg_encoding_to_char(MULTIBYTE)); - putenv(envbuf); - } + if (!env) + { + rtn = PQexec(conn, "select getdatabaseencoding()"); + if (rtn && PQresultStatus(rtn) == PGRES_TUPLES_OK) + { + encoding = PQgetvalue(rtn, 0, 0); + if (encoding) + { + /* set client encoding */ + sprintf(envbuf, "%s=%s", envname, encoding); + putenv(envbuf); + } + PQclear(rtn); + } + if (!encoding) + { /* this should not happen */ + sprintf(envbuf, "%s=%s", envname, pg_encoding_to_char(MULTIBYTE)); + putenv(envbuf); + } } #endif @@ -833,12 +855,13 @@ PQsetenv(PGconn *conn) /* * makeEmptyPGconn - * - create a PGconn data structure with (as yet) no interesting data + * - create a PGconn data structure with (as yet) no interesting data */ static PGconn * makeEmptyPGconn(void) { - PGconn *conn = (PGconn *) malloc(sizeof(PGconn)); + PGconn *conn = (PGconn *) malloc(sizeof(PGconn)); + if (conn == NULL) return conn; @@ -914,11 +937,11 @@ closePGconn(PGconn *conn) { if (conn->sock >= 0) { + /* - * Try to send "close connection" message to backend. - * Ignore any error. - * Note: this routine used to go to substantial lengths to avoid - * getting SIGPIPE'd if the connection were already closed. + * Try to send "close connection" message to backend. Ignore any + * error. Note: this routine used to go to substantial lengths to + * avoid getting SIGPIPE'd if the connection were already closed. * Now we rely on pqFlush to avoid the signal. */ (void) pqPuts("X", conn); @@ -998,9 +1021,10 @@ int PQrequestCancel(PGconn *conn) { int tmpsock = -1; - struct { - uint32 packetlen; - CancelRequestPacket cp; + struct + { + uint32 packetlen; + CancelRequestPacket cp; } crp; /* Check we have an open connection */ @@ -1015,9 +1039,8 @@ PQrequestCancel(PGconn *conn) } /* - * We need to open a temporary connection to the postmaster. - * Use the information saved by connectDB to do this with - * only kernel calls. + * We need to open a temporary connection to the postmaster. Use the + * information saved by connectDB to do this with only kernel calls. */ if ((tmpsock = socket(conn->raddr.sa.sa_family, SOCK_STREAM, 0)) < 0) { @@ -1029,6 +1052,7 @@ PQrequestCancel(PGconn *conn) strcpy(conn->errorMessage, "PQrequestCancel() -- connect() failed: "); goto cancel_errReturn; } + /* * We needn't set nonblocking I/O or NODELAY options here. */ @@ -1040,7 +1064,7 @@ PQrequestCancel(PGconn *conn) crp.cp.backendPID = htonl(conn->be_pid); crp.cp.cancelAuthCode = htonl(conn->be_key); - if (send(tmpsock, (char*) &crp, sizeof(crp), 0) != (int) sizeof(crp)) + if (send(tmpsock, (char *) &crp, sizeof(crp), 0) != (int) sizeof(crp)) { strcpy(conn->errorMessage, "PQrequestCancel() -- send() failed: "); goto cancel_errReturn; @@ -1404,6 +1428,7 @@ char * PQerrorMessage(PGconn *conn) { static char noConn[] = "PQerrorMessage: conn pointer is NULL\n"; + if (!conn) return noConn; return conn->errorMessage; @@ -1441,7 +1466,7 @@ PQuntrace(PGconn *conn) } void -PQsetNoticeProcessor (PGconn *conn, PQnoticeProcessor proc, void *arg) +PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg) { if (conn == NULL) return; @@ -1457,7 +1482,7 @@ PQsetNoticeProcessor (PGconn *conn, PQnoticeProcessor proc, void *arg) */ static void -defaultNoticeProcessor(void * arg, const char * message) +defaultNoticeProcessor(void *arg, const char *message) { /* Note: we expect the supplied string to end with a newline already. */ fprintf(stderr, "%s", message); diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index e8a38f6465..9f2651365b 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.64 1998/09/01 03:28:51 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.65 1998/09/01 04:40:05 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -33,11 +33,11 @@ #define TUPARR_GROW_BY 100 /* keep this in same order as ExecStatusType in libpq-fe.h */ -const char * const pgresStatus[] = { +const char *const pgresStatus[] = { "PGRES_EMPTY_QUERY", "PGRES_COMMAND_OK", "PGRES_TUPLES_OK", - "PGRES_COPY_OUT", + "PGRES_COPY_OUT", "PGRES_COPY_IN", "PGRES_BAD_RESPONSE", "PGRES_NONFATAL_ERROR", @@ -53,10 +53,10 @@ static PGresult *makeEmptyPGresult(PGconn *conn, ExecStatusType status); static void freeTuple(PGresAttValue *tuple, int numAttributes); static void addTuple(PGresult *res, PGresAttValue *tup); static void parseInput(PGconn *conn); -static int getRowDescriptions(PGconn *conn); -static int getAnotherTuple(PGconn *conn, int binary); -static int getNotify(PGconn *conn); -static int getNotice(PGconn *conn); +static int getRowDescriptions(PGconn *conn); +static int getAnotherTuple(PGconn *conn, int binary); +static int getNotify(PGconn *conn); +static int getNotice(PGconn *conn); /* @@ -127,7 +127,7 @@ PQclear(PGresult *res) static void freeTuple(PGresAttValue *tuple, int numAttributes) { - int i; + int i; if (tuple) { @@ -170,12 +170,13 @@ addTuple(PGresult *res, PGresAttValue *tup) { /* grow the array */ res->tupArrSize += TUPARR_GROW_BY; + /* - * we can use realloc because shallow copying of the structure - * is okay. Note that the first time through, res->tuples is NULL. - * realloc is supposed to do the right thing in that case. - * Also note that the positions beyond res->ntups are garbage, - * not necessarily NULL. + * we can use realloc because shallow copying of the structure is + * okay. Note that the first time through, res->tuples is NULL. + * realloc is supposed to do the right thing in that case. Also + * note that the positions beyond res->ntups are garbage, not + * necessarily NULL. */ res->tuples = (PGresAttValue **) realloc(res->tuples, res->tupArrSize * sizeof(PGresAttValue *)); @@ -187,10 +188,10 @@ addTuple(PGresult *res, PGresAttValue *tup) /* * PQsendQuery - * Submit a query, but don't wait for it to finish + * Submit a query, but don't wait for it to finish * * Returns: 1 if successfully submitted - * 0 if error (conn->errorMessage is set) + * 0 if error (conn->errorMessage is set) */ int @@ -204,7 +205,7 @@ PQsendQuery(PGconn *conn, const char *query) return 0; } /* check to see if the query string is too long */ - if (strlen(query) > MAX_MESSAGE_LEN-2) + if (strlen(query) > MAX_MESSAGE_LEN - 2) { sprintf(conn->errorMessage, "PQsendQuery() -- query is too long. " "Maximum length is %d\n", MAX_MESSAGE_LEN - 2); @@ -259,11 +260,11 @@ PQconsumeInput(PGconn *conn) if (!conn) return; - /* Load more data, if available. - * We do this no matter what state we are in, since we are probably - * getting called because the application wants to get rid - * of a read-select condition. - * Note that we will NOT block waiting for more input. + /* + * Load more data, if available. We do this no matter what state we + * are in, since we are probably getting called because the + * application wants to get rid of a read-select condition. Note that + * we will NOT block waiting for more input. */ if (pqReadData(conn) < 0) strcpy(conn->asyncErrorMessage, conn->errorMessage); @@ -280,30 +281,34 @@ PQconsumeInput(PGconn *conn) static void parseInput(PGconn *conn) { - char id; + char id; - /* + /* * Loop to parse successive complete messages available in the buffer. */ for (;;) { - /* - * Quit if in COPY_OUT state: we expect raw data from the server until - * PQendcopy is called. Don't try to parse it according to the normal - * protocol. (This is bogus. The data lines ought to be part of the - * protocol and have identifying leading characters.) + + /* + * Quit if in COPY_OUT state: we expect raw data from the server + * until PQendcopy is called. Don't try to parse it according to + * the normal protocol. (This is bogus. The data lines ought to + * be part of the protocol and have identifying leading + * characters.) */ if (conn->asyncStatus == PGASYNC_COPY_OUT) return; + /* * OK to try to read a message type code. */ conn->inCursor = conn->inStart; if (pqGetc(&id, conn)) return; + /* - * NOTIFY and NOTICE messages can happen in any state besides COPY OUT; - * always process them right away. + * NOTIFY and NOTICE messages can happen in any state besides COPY + * OUT; always process them right away. */ if (id == 'A') { @@ -317,18 +322,19 @@ parseInput(PGconn *conn) } else { + /* - * Other messages should only be processed while in BUSY state. - * (In particular, in READY state we hold off further parsing - * until the application collects the current PGresult.) - * If the state is IDLE then we got trouble. + * Other messages should only be processed while in BUSY + * state. (In particular, in READY state we hold off further + * parsing until the application collects the current + * PGresult.) If the state is IDLE then we got trouble. */ if (conn->asyncStatus != PGASYNC_BUSY) { if (conn->asyncStatus == PGASYNC_IDLE) { sprintf(conn->errorMessage, - "Backend message type 0x%02x arrived while idle\n", + "Backend message type 0x%02x arrived while idle\n", id); DONOTICE(conn, conn->errorMessage); /* Discard the unexpected message; good idea?? */ @@ -341,18 +347,21 @@ parseInput(PGconn *conn) case 'C': /* command complete */ if (conn->result == NULL) conn->result = makeEmptyPGresult(conn, - PGRES_COMMAND_OK); + PGRES_COMMAND_OK); if (pqGets(conn->result->cmdStatus, CMDSTATUS_LEN, conn)) return; conn->asyncStatus = PGASYNC_READY; break; case 'E': /* error return */ - if (pqGets(conn->asyncErrorMessage,ERROR_MSG_LENGTH,conn)) + if (pqGets(conn->asyncErrorMessage, ERROR_MSG_LENGTH, conn)) return; /* delete any partially constructed result */ pqClearAsyncResult(conn); - /* we leave result NULL while setting asyncStatus=READY; - * this signals an error condition to PQgetResult. + + /* + * we leave result NULL while setting + * asyncStatus=READY; this signals an error condition + * to PQgetResult. */ conn->asyncStatus = PGASYNC_READY; break; @@ -366,18 +375,20 @@ parseInput(PGconn *conn) if (id != '\0') { sprintf(conn->errorMessage, - "unexpected character %c following 'I'\n", id); + "unexpected character %c following 'I'\n", id); DONOTICE(conn, conn->errorMessage); } if (conn->result == NULL) conn->result = makeEmptyPGresult(conn, - PGRES_EMPTY_QUERY); + PGRES_EMPTY_QUERY); conn->asyncStatus = PGASYNC_READY; break; case 'K': /* secret key data from the backend */ - /* This is expected only during backend startup, - * but it's just as easy to handle it as part of the - * main loop. Save the data and continue processing. + + /* + * This is expected only during backend startup, but + * it's just as easy to handle it as part of the main + * loop. Save the data and continue processing. */ if (pqGetInt(&(conn->be_pid), 4, conn)) return; @@ -389,7 +400,8 @@ parseInput(PGconn *conn) return; /* We pretty much ignore this message type... */ break; - case 'T': /* row descriptions (start of query results) */ + case 'T': /* row descriptions (start of query + * results) */ if (conn->result == NULL) { /* First 'T' in a query sequence */ @@ -398,7 +410,9 @@ parseInput(PGconn *conn) } else { - /* A new 'T' message is treated as the start of + + /* + * A new 'T' message is treated as the start of * another PGresult. (It is not clear that this * is really possible with the current backend.) * We stop parsing until the application accepts @@ -418,7 +432,7 @@ parseInput(PGconn *conn) else { sprintf(conn->errorMessage, - "Backend sent D message without prior T\n"); + "Backend sent D message without prior T\n"); DONOTICE(conn, conn->errorMessage); /* Discard the unexpected message; good idea?? */ conn->inStart = conn->inEnd; @@ -435,7 +449,7 @@ parseInput(PGconn *conn) else { sprintf(conn->errorMessage, - "Backend sent B message without prior T\n"); + "Backend sent B message without prior T\n"); DONOTICE(conn, conn->errorMessage); /* Discard the unexpected message; good idea?? */ conn->inStart = conn->inEnd; @@ -450,8 +464,8 @@ parseInput(PGconn *conn) break; default: sprintf(conn->asyncErrorMessage, - "unknown protocol character '%c' read from backend. " - "(The protocol character is the first character the " + "unknown protocol character '%c' read from backend. " + "(The protocol character is the first character the " "backend sends in response to a query it receives).\n", id); /* Discard the unexpected message; good idea?? */ @@ -567,7 +581,7 @@ getAnotherTuple(PGconn *conn, int binary) tup = conn->curTuple; /* Get the null-value bitmap */ - nbytes = (nfields + BYTELEN-1) / BYTELEN; + nbytes = (nfields + BYTELEN - 1) / BYTELEN; if (nbytes >= MAX_FIELDS) { sprintf(conn->asyncErrorMessage, @@ -637,7 +651,7 @@ getAnotherTuple(PGconn *conn, int binary) /* * PQisBusy - * Return TRUE if PQgetResult would block waiting for input. + * Return TRUE if PQgetResult would block waiting for input. */ int @@ -656,14 +670,14 @@ PQisBusy(PGconn *conn) /* * PQgetResult - * Get the next PGresult produced by a query. - * Returns NULL if and only if no query work remains. + * Get the next PGresult produced by a query. + * Returns NULL if and only if no query work remains. */ -PGresult * +PGresult * PQgetResult(PGconn *conn) { - PGresult *res; + PGresult *res; if (!conn) return NULL; @@ -694,16 +708,16 @@ PQgetResult(PGconn *conn) res = NULL; /* query is complete */ break; case PGASYNC_READY: + /* * conn->result is the PGresult to return, or possibly NULL - * indicating an error. - * conn->asyncErrorMessage holds the errorMessage to return. - * (We keep it stashed there so that other user calls can't - * overwrite it prematurely.) + * indicating an error. conn->asyncErrorMessage holds the + * errorMessage to return. (We keep it stashed there so that + * other user calls can't overwrite it prematurely.) */ res = conn->result; - conn->result = NULL; /* handing over ownership to caller */ - conn->curTuple = NULL; /* just in case */ + conn->result = NULL;/* handing over ownership to caller */ + conn->curTuple = NULL; /* just in case */ if (!res) res = makeEmptyPGresult(conn, PGRES_FATAL_ERROR); strcpy(conn->errorMessage, conn->asyncErrorMessage); @@ -742,11 +756,13 @@ PQgetResult(PGconn *conn) PGresult * PQexec(PGconn *conn, const char *query) { - PGresult *result; - PGresult *lastResult; + PGresult *result; + PGresult *lastResult; - /* Silently discard any prior query result that application didn't eat. - * This is probably poor design, but it's here for backward compatibility. + /* + * Silently discard any prior query result that application didn't + * eat. This is probably poor design, but it's here for backward + * compatibility. */ while ((result = PQgetResult(conn)) != NULL) { @@ -755,19 +771,20 @@ PQexec(PGconn *conn, const char *query) { PQclear(result); sprintf(conn->errorMessage, - "PQexec: you gotta get out of a COPY state yourself.\n"); + "PQexec: you gotta get out of a COPY state yourself.\n"); return NULL; } PQclear(result); } /* OK to send the message */ - if (! PQsendQuery(conn, query)) + if (!PQsendQuery(conn, query)) return NULL; - /* For backwards compatibility, return the last result if there are - * more than one. We have to stop if we see copy in/out, however. - * We will resume parsing when application calls PQendcopy. + /* + * For backwards compatibility, return the last result if there are + * more than one. We have to stop if we see copy in/out, however. We + * will resume parsing when application calls PQendcopy. */ lastResult = NULL; while ((result = PQgetResult(conn)) != NULL) @@ -788,7 +805,7 @@ PQexec(PGconn *conn, const char *query) * This is possible in several places, so we break it out as a subroutine. * Entry: 'N' flag character has already been consumed. * Exit: returns 0 if successfully consumed Notice message. - * returns EOF if not enough data. + * returns EOF if not enough data. */ static int getNotice(PGconn *conn) @@ -804,12 +821,12 @@ getNotice(PGconn *conn) * This is possible in several places, so we break it out as a subroutine. * Entry: 'A' flag character has already been consumed. * Exit: returns 0 if successfully consumed Notify message. - * returns EOF if not enough data. + * returns EOF if not enough data. */ static int getNotify(PGconn *conn) { - PGnotify tempNotify; + PGnotify tempNotify; PGnotify *newNotify; if (pqGetInt(&(tempNotify.be_pid), 4, conn)) @@ -885,14 +902,16 @@ PQgetline(PGconn *conn, char *s, int maxlen) return EOF; } - /* Since this is a purely synchronous routine, we don't bother to + /* + * Since this is a purely synchronous routine, we don't bother to * maintain conn->inCursor; there is no need to back up. */ while (maxlen > 1) { if (conn->inStart < conn->inEnd) { - char c = conn->inBuffer[conn->inStart++]; + char c = conn->inBuffer[conn->inStart++]; + if (c == '\n') { result = 0; /* success exit */ @@ -951,7 +970,7 @@ PQputnbytes(PGconn *conn, const char *buffer, int nbytes) int PQendcopy(PGconn *conn) { - PGresult *result; + PGresult *result; if (!conn) return 0; @@ -960,7 +979,7 @@ PQendcopy(PGconn *conn) conn->asyncStatus != PGASYNC_COPY_OUT) { sprintf(conn->errorMessage, - "PQendcopy() -- I don't think there's a copy in progress."); + "PQendcopy() -- I don't think there's a copy in progress."); return 1; } @@ -980,10 +999,10 @@ PQendcopy(PGconn *conn) return 0; } - /* Trouble. - * The worst case is that we've lost sync with the backend entirely - * due to application screwup of the copy in/out protocol. - * To recover, reset the connection (talk about using a sledgehammer...) + /* + * Trouble. The worst case is that we've lost sync with the backend + * entirely due to application screwup of the copy in/out protocol. To + * recover, reset the connection (talk about using a sledgehammer...) */ PQclear(result); @@ -1014,9 +1033,9 @@ PQendcopy(PGconn *conn) * nargs : # of arguments in args array. * * RETURNS - * PGresult with status = PGRES_COMMAND_OK if successful. + * PGresult with status = PGRES_COMMAND_OK if successful. * *actual_result_len is > 0 if there is a return value, 0 if not. - * PGresult with status = PGRES_FATAL_ERROR if backend returns an error. + * PGresult with status = PGRES_FATAL_ERROR if backend returns an error. * NULL on communications failure. conn->errorMessage will be set. * ---------------- */ @@ -1031,7 +1050,7 @@ PQfn(PGconn *conn, int nargs) { bool needInput = false; - ExecStatusType status = PGRES_FATAL_ERROR; + ExecStatusType status = PGRES_FATAL_ERROR; char id; int i; @@ -1051,9 +1070,9 @@ PQfn(PGconn *conn, if (pqPuts("F ", conn)) /* function */ return NULL; - if (pqPutInt(fnid, 4, conn)) /* function id */ + if (pqPutInt(fnid, 4, conn))/* function id */ return NULL; - if (pqPutInt(nargs, 4, conn)) /* # of args */ + if (pqPutInt(nargs, 4, conn)) /* # of args */ return NULL; for (i = 0; i < nargs; ++i) @@ -1084,8 +1103,10 @@ PQfn(PGconn *conn, pqReadData(conn) < 0) break; } - /* Scan the message. - * If we run out of data, loop around to try again. + + /* + * Scan the message. If we run out of data, loop around to try + * again. */ conn->inCursor = conn->inStart; needInput = true; @@ -1093,9 +1114,10 @@ PQfn(PGconn *conn, if (pqGetc(&id, conn)) continue; - /* We should see V or E response to the command, - * but might get N and/or A notices first. - * We also need to swallow the final Z before returning. + /* + * We should see V or E response to the command, but might get N + * and/or A notices first. We also need to swallow the final Z + * before returning. */ switch (id) { @@ -1119,7 +1141,7 @@ PQfn(PGconn *conn, conn)) continue; } - if (pqGetc(&id, conn)) /* get the last '0' */ + if (pqGetc(&id, conn)) /* get the last '0' */ continue; } if (id == '0') @@ -1127,7 +1149,8 @@ PQfn(PGconn *conn, /* correctly finished function result message */ status = PGRES_COMMAND_OK; } - else { + else + { /* The backend violates the protocol. */ sprintf(conn->errorMessage, "FATAL: PQfn: protocol error: id=%x\n", id); @@ -1249,7 +1272,7 @@ check_tuple_field_number(const char *routineName, PGresult *res, char * PQfname(PGresult *res, int field_num) { - if (! check_field_number("PQfname", res, field_num)) + if (!check_field_number("PQfname", res, field_num)) return NULL; if (res->attDescs) return res->attDescs[field_num].name; @@ -1282,8 +1305,8 @@ PQfnumber(PGresult *res, const char *field_name) } else for (i = 0; field_case[i]; i++) - if (isascii((unsigned char)field_case[i]) && - isupper(field_case[i])) + if (isascii((unsigned char) field_case[i]) && + isupper(field_case[i])) field_case[i] = tolower(field_case[i]); for (i = 0; i < res->numAttributes; i++) @@ -1301,7 +1324,7 @@ PQfnumber(PGresult *res, const char *field_name) Oid PQftype(PGresult *res, int field_num) { - if (! check_field_number("PQftype", res, field_num)) + if (!check_field_number("PQftype", res, field_num)) return InvalidOid; if (res->attDescs) return res->attDescs[field_num].typid; @@ -1312,7 +1335,7 @@ PQftype(PGresult *res, int field_num) short PQfsize(PGresult *res, int field_num) { - if (! check_field_number("PQfsize", res, field_num)) + if (!check_field_number("PQfsize", res, field_num)) return 0; if (res->attDescs) return res->attDescs[field_num].typlen; @@ -1323,7 +1346,7 @@ PQfsize(PGresult *res, int field_num) int PQfmod(PGresult *res, int field_num) { - if (! check_field_number("PQfmod", res, field_num)) + if (!check_field_number("PQfmod", res, field_num)) return 0; if (res->attDescs) return res->attDescs[field_num].atttypmod; @@ -1347,8 +1370,11 @@ PQcmdStatus(PGresult *res) const char * PQoidStatus(PGresult *res) { - char *p, *e, *scan; - int slen, olen; + char *p, + *e, + *scan; + int slen, + olen; if (!res) return ""; @@ -1356,22 +1382,20 @@ PQoidStatus(PGresult *res) if (strncmp(res->cmdStatus, "INSERT ", 7) != 0) return ""; - /* The cmdStatus string looks like - * INSERT oid count\0 - * In order to be able to return an ordinary C string without - * damaging the result for PQcmdStatus or PQcmdTuples, we copy - * the oid part of the string to just after the null, so that - * cmdStatus looks like - * INSERT oid count\0oid\0 - * ^ our return value points here - * Pretty klugy eh? This routine should've just returned an Oid value. + /* + * The cmdStatus string looks like INSERT oid count\0 In order to be + * able to return an ordinary C string without damaging the result for + * PQcmdStatus or PQcmdTuples, we copy the oid part of the string to + * just after the null, so that cmdStatus looks like INSERT oid + * count\0oid\0 ^ our return value points here Pretty klugy eh? This + * routine should've just returned an Oid value. */ slen = strlen(res->cmdStatus); - p = res->cmdStatus + 7; /* where oid is now */ - e = res->cmdStatus + slen + 1; /* where to put the oid string */ + p = res->cmdStatus + 7; /* where oid is now */ + e = res->cmdStatus + slen + 1; /* where to put the oid string */ - for (scan = p; *scan && *scan != ' '; ) + for (scan = p; *scan && *scan != ' ';) scan++; olen = scan - p; if (slen + olen + 2 > sizeof(res->cmdStatus)) @@ -1439,7 +1463,7 @@ PQcmdTuples(PGresult *res) char * PQgetvalue(PGresult *res, int tup_num, int field_num) { - if (! check_tuple_field_number("PQgetvalue", res, tup_num, field_num)) + if (!check_tuple_field_number("PQgetvalue", res, tup_num, field_num)) return NULL; return res->tuples[tup_num][field_num].value; } @@ -1452,7 +1476,7 @@ PQgetvalue(PGresult *res, int tup_num, int field_num) int PQgetlength(PGresult *res, int tup_num, int field_num) { - if (! check_tuple_field_number("PQgetlength", res, tup_num, field_num)) + if (!check_tuple_field_number("PQgetlength", res, tup_num, field_num)) return 0; if (res->tuples[tup_num][field_num].len != NULL_LEN) return res->tuples[tup_num][field_num].len; @@ -1466,7 +1490,7 @@ PQgetlength(PGresult *res, int tup_num, int field_num) int PQgetisnull(PGresult *res, int tup_num, int field_num) { - if (! check_tuple_field_number("PQgetisnull", res, tup_num, field_num)) + if (!check_tuple_field_number("PQgetisnull", res, tup_num, field_num)) return 1; /* pretend it is null */ if (res->tuples[tup_num][field_num].len == NULL_LEN) return 1; diff --git a/src/interfaces/libpq/fe-lobj.c b/src/interfaces/libpq/fe-lobj.c index 8b89e940f2..190cab1810 100644 --- a/src/interfaces/libpq/fe-lobj.c +++ b/src/interfaces/libpq/fe-lobj.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.15 1998/08/17 03:50:37 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.16 1998/09/01 04:40:07 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -28,7 +28,7 @@ #include <fcntl.h> #include <sys/stat.h> -#include "libpq/libpq-fs.h" /* must come after sys/stat.h */ +#include "libpq/libpq-fs.h" /* must come after sys/stat.h */ #define LO_BUFSIZE 1024 diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c index baa0d7feed..d091f6e6db 100644 --- a/src/interfaces/libpq/fe-misc.c +++ b/src/interfaces/libpq/fe-misc.c @@ -24,7 +24,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.19 1998/08/29 02:09:25 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.20 1998/09/01 04:40:08 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -46,7 +46,7 @@ #if !defined(NO_UNISTD_H) #include <unistd.h> #endif -#endif /* WIN32 */ +#endif /* WIN32 */ #ifdef HAVE_SYS_SELECT_H #include <sys/select.h> @@ -87,7 +87,7 @@ pqGetc(char *result, PGconn *conn) static int pqPutBytes(const char *s, int nbytes, PGconn *conn) { - int avail = conn->outBufSize - conn->outCount; + int avail = conn->outBufSize - conn->outCount; while (nbytes > avail) { @@ -117,10 +117,10 @@ int pqGets(char *s, int maxlen, PGconn *conn) { /* Copy conn data to locals for faster search loop */ - char *inBuffer = conn->inBuffer; - int inCursor = conn->inCursor; - int inEnd = conn->inEnd; - int slen; + char *inBuffer = conn->inBuffer; + int inCursor = conn->inCursor; + int inEnd = conn->inEnd; + int slen; while (inCursor < inEnd && inBuffer[inCursor]) inCursor++; @@ -133,8 +133,8 @@ pqGets(char *s, int maxlen, PGconn *conn) strcpy(s, inBuffer + conn->inCursor); else { - strncpy(s, inBuffer + conn->inCursor, maxlen-1); - s[maxlen-1] = '\0'; + strncpy(s, inBuffer + conn->inCursor, maxlen - 1); + s[maxlen - 1] = '\0'; } conn->inCursor = ++inCursor; @@ -149,7 +149,7 @@ pqGets(char *s, int maxlen, PGconn *conn) int pqPuts(const char *s, PGconn *conn) { - if (pqPutBytes(s, strlen(s)+1, conn)) + if (pqPutBytes(s, strlen(s) + 1, conn)) return EOF; if (conn->Pfdebug) @@ -170,7 +170,7 @@ pqGetnchar(char *s, int len, PGconn *conn) memcpy(s, conn->inBuffer + conn->inCursor, len); /* no terminating null */ - + conn->inCursor += len; if (conn->Pfdebug) @@ -204,8 +204,8 @@ pqPutnchar(const char *s, int len, PGconn *conn) int pqGetInt(int *result, int bytes, PGconn *conn) { - uint16 tmp2; - uint32 tmp4; + uint16 tmp2; + uint32 tmp4; switch (bytes) { @@ -244,19 +244,19 @@ pqGetInt(int *result, int bytes, PGconn *conn) int pqPutInt(int value, int bytes, PGconn *conn) { - uint16 tmp2; - uint32 tmp4; + uint16 tmp2; + uint32 tmp4; switch (bytes) { case 2: tmp2 = htons((uint16) value); - if (pqPutBytes((const char*) &tmp2, 2, conn)) + if (pqPutBytes((const char *) &tmp2, 2, conn)) return EOF; break; case 4: tmp4 = htonl((uint32) value); - if (pqPutBytes((const char*) &tmp4, 4, conn)) + if (pqPutBytes((const char *) &tmp4, 4, conn)) return EOF; break; default: @@ -278,8 +278,8 @@ pqPutInt(int value, int bytes, PGconn *conn) static int pqReadReady(PGconn *conn) { - fd_set input_mask; - struct timeval timeout; + fd_set input_mask; + struct timeval timeout; if (conn->sock < 0) return 0; @@ -288,7 +288,7 @@ pqReadReady(PGconn *conn) FD_SET(conn->sock, &input_mask); timeout.tv_sec = 0; timeout.tv_usec = 0; - if (select(conn->sock+1, &input_mask, (fd_set *) NULL, (fd_set *) NULL, + if (select(conn->sock + 1, &input_mask, (fd_set *) NULL, (fd_set *) NULL, &timeout) < 0) { sprintf(conn->errorMessage, @@ -302,17 +302,17 @@ pqReadReady(PGconn *conn) /* --------------------------------------------------------------------- */ /* pqReadData: read more data, if any is available * Possible return values: - * 1: successfully loaded at least one more byte - * 0: no data is presently available, but no error detected - * -1: error detected (including EOF = connection closure); - * conn->errorMessage set + * 1: successfully loaded at least one more byte + * 0: no data is presently available, but no error detected + * -1: error detected (including EOF = connection closure); + * conn->errorMessage set * NOTE: callers must not assume that pointers or indexes into conn->inBuffer * remain valid across this call! */ int pqReadData(PGconn *conn) { - int nread; + int nread; if (conn->sock < 0) { @@ -331,16 +331,19 @@ pqReadData(PGconn *conn) } else conn->inStart = conn->inCursor = conn->inEnd = 0; - /* If the buffer is fairly full, enlarge it. - * We need to be able to enlarge the buffer in case a single message - * exceeds the initial buffer size. We enlarge before filling the - * buffer entirely so as to avoid asking the kernel for a partial packet. - * The magic constant here should be at least one TCP packet. + + /* + * If the buffer is fairly full, enlarge it. We need to be able to + * enlarge the buffer in case a single message exceeds the initial + * buffer size. We enlarge before filling the buffer entirely so as + * to avoid asking the kernel for a partial packet. The magic constant + * here should be at least one TCP packet. */ if (conn->inBufSize - conn->inEnd < 2000) { - int newSize = conn->inBufSize * 2; - char * newBuf = (char *) realloc(conn->inBuffer, newSize); + int newSize = conn->inBufSize * 2; + char *newBuf = (char *) realloc(conn->inBuffer, newSize); + if (newBuf) { conn->inBuffer = newBuf; @@ -350,7 +353,7 @@ pqReadData(PGconn *conn) /* OK, try to read some data */ tryAgain: - nread = recv(conn->sock, conn->inBuffer + conn->inEnd, + nread = recv(conn->sock, conn->inBuffer + conn->inEnd, conn->inBufSize - conn->inEnd, 0); if (nread < 0) { @@ -376,22 +379,24 @@ tryAgain: return 1; } - /* A return value of 0 could mean just that no data is now available, - * or it could mean EOF --- that is, the server has closed the connection. - * Since we have the socket in nonblock mode, the only way to tell the - * difference is to see if select() is saying that the file is ready. - * Grumble. Fortunately, we don't expect this path to be taken much, - * since in normal practice we should not be trying to read data unless - * the file selected for reading already. + /* + * A return value of 0 could mean just that no data is now available, + * or it could mean EOF --- that is, the server has closed the + * connection. Since we have the socket in nonblock mode, the only way + * to tell the difference is to see if select() is saying that the + * file is ready. Grumble. Fortunately, we don't expect this path to + * be taken much, since in normal practice we should not be trying to + * read data unless the file selected for reading already. */ - if (! pqReadReady(conn)) + if (!pqReadReady(conn)) return 0; /* definitely no data available */ - /* Still not sure that it's EOF, - * because some data could have just arrived. + /* + * Still not sure that it's EOF, because some data could have just + * arrived. */ tryAgain2: - nread = recv(conn->sock, conn->inBuffer + conn->inEnd, + nread = recv(conn->sock, conn->inBuffer + conn->inEnd, conn->inBufSize - conn->inEnd, 0); if (nread < 0) { @@ -417,22 +422,22 @@ tryAgain2: return 1; } - /* OK, we are getting a zero read even though select() says ready. + /* + * OK, we are getting a zero read even though select() says ready. * This means the connection has been closed. Cope. */ sprintf(conn->errorMessage, "pqReadData() -- backend closed the channel unexpectedly.\n" "\tThis probably means the backend terminated abnormally" " before or while processing the request.\n"); - conn->status = CONNECTION_BAD; /* No more connection to - * backend */ + conn->status = CONNECTION_BAD; /* No more connection to backend */ #ifdef WIN32 closesocket(conn->sock); #else close(conn->sock); #endif conn->sock = -1; - + return -1; } @@ -442,8 +447,8 @@ tryAgain2: int pqFlush(PGconn *conn) { - char * ptr = conn->outBuffer; - int len = conn->outCount; + char *ptr = conn->outBuffer; + int len = conn->outCount; if (conn->sock < 0) { @@ -455,10 +460,11 @@ pqFlush(PGconn *conn) { /* Prevent being SIGPIPEd if backend has closed the connection. */ #ifndef WIN32 - pqsigfunc oldsighandler = pqsignal(SIGPIPE, SIG_IGN); + pqsigfunc oldsighandler = pqsignal(SIGPIPE, SIG_IGN); + #endif - int sent = send(conn->sock, ptr, len, 0); + int sent = send(conn->sock, ptr, len, 0); #ifndef WIN32 pqsignal(SIGPIPE, oldsighandler); @@ -479,7 +485,7 @@ pqFlush(PGconn *conn) #endif default: sprintf(conn->errorMessage, - "pqFlush() -- couldn't send data: errno=%d\n%s\n", + "pqFlush() -- couldn't send data: errno=%d\n%s\n", errno, strerror(errno)); return EOF; } @@ -511,8 +517,8 @@ pqFlush(PGconn *conn) int pqWait(int forRead, int forWrite, PGconn *conn) { - fd_set input_mask; - fd_set output_mask; + fd_set input_mask; + fd_set output_mask; if (conn->sock < 0) { @@ -521,14 +527,15 @@ pqWait(int forRead, int forWrite, PGconn *conn) } /* loop in case select returns EINTR */ - for (;;) { + for (;;) + { FD_ZERO(&input_mask); FD_ZERO(&output_mask); if (forRead) FD_SET(conn->sock, &input_mask); if (forWrite) FD_SET(conn->sock, &output_mask); - if (select(conn->sock+1, &input_mask, &output_mask, (fd_set *) NULL, + if (select(conn->sock + 1, &input_mask, &output_mask, (fd_set *) NULL, (struct timeval *) NULL) < 0) { if (errno == EINTR) diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c index 6e0a6e3ff4..2a189f0545 100644 --- a/src/interfaces/libpq/fe-print.c +++ b/src/interfaces/libpq/fe-print.c @@ -9,7 +9,7 @@ * didn't really belong there. * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.11 1998/09/01 03:28:54 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.12 1998/09/01 04:40:09 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -30,7 +30,7 @@ #else #include <termios.h> #endif -#endif /* WIN32 */ +#endif /* WIN32 */ #include <stdlib.h> #include <signal.h> #include <string.h> @@ -52,19 +52,16 @@ static struct winsize #endif -static void -do_field(PQprintOpt *po, PGresult *res, +static void do_field(PQprintOpt *po, PGresult *res, const int i, const int j, char *buf, const int fs_len, char **fields, const int nFields, char **fieldNames, unsigned char *fieldNotNum, int *fieldMax, const int fieldMaxLen, FILE *fout); -static char * -do_header(FILE *fout, PQprintOpt *po, const int nFields, +static char *do_header(FILE *fout, PQprintOpt *po, const int nFields, int *fieldMax, char **fieldNames, unsigned char *fieldNotNum, const int fs_len, PGresult *res); -static void -output_row(FILE *fout, PQprintOpt *po, const int nFields, char **fields, +static void output_row(FILE *fout, PQprintOpt *po, const int nFields, char **fields, unsigned char *fieldNotNum, int *fieldMax, char *border, const int row_index); static void fill(int length, int max, char filler, FILE *fp); @@ -150,7 +147,7 @@ PQprint(FILE *fout, if (fout == NULL) fout = stdout; - if (po->pager && fout == stdout + if (po->pager && fout == stdout #ifndef WIN32 && isatty(fileno(stdin)) && @@ -502,20 +499,20 @@ PQprintTuples(PGresult *res, * if this variable is not defined, the same encoding as * the backend is assumed. */ -int PQmblen(unsigned char *s) +int +PQmblen(unsigned char *s) { - char *str; - int encoding = -1; - - str = getenv("PGCLIENTENCODING"); - if (str) { - encoding = pg_char_to_encoding(str); - } - if (encoding < 0) { - encoding = MULTIBYTE; - } - return(pg_encoding_mblen(encoding, s)); + char *str; + int encoding = -1; + + str = getenv("PGCLIENTENCODING"); + if (str) + encoding = pg_char_to_encoding(str); + if (encoding < 0) + encoding = MULTIBYTE; + return (pg_encoding_mblen(encoding, s)); } + #endif static void @@ -552,11 +549,11 @@ do_field(PQprintOpt *po, PGresult *res, if (!skipit) { #ifdef MULTIBYTE - int len; + int len; - for (p = pval, o = buf; *p; - len = PQmblen(p),memcpy(o,p,len), - o+=len, p+=len) + for (p = pval, o = buf; *p; + len = PQmblen(p), memcpy(o, p, len), + o += len, p += len) #else for (p = pval, o = buf; *p; *(o++) = *(p++)) #endif diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 0e2112c159..993536023b 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: libpq-fe.h,v 1.39 1998/08/29 04:05:45 momjian Exp $ + * $Id: libpq-fe.h,v 1.40 1998/09/01 04:40:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -60,7 +60,7 @@ extern "C" } ExecStatusType; /* string descriptions of the ExecStatusTypes */ - extern const char * const pgresStatus[]; + extern const char *const pgresStatus[]; /* * POSTGRES backend dependent Constants. @@ -127,7 +127,7 @@ extern "C" } PGnotify; /* PQnoticeProcessor is a typedef for a callback function type */ - typedef void (*PQnoticeProcessor) (void * arg, const char * message); + typedef void (*PQnoticeProcessor) (void *arg, const char *message); /* PGAsyncStatusType is private to libpq, really shouldn't be seen by users */ typedef enum @@ -137,7 +137,7 @@ extern "C" PGASYNC_READY, /* result ready for PQgetResult */ PGASYNC_COPY_IN, /* Copy In data transfer in progress */ PGASYNC_COPY_OUT /* Copy Out data transfer in progress */ - } PGAsyncStatusType; + } PGAsyncStatusType; /* generic socket address type for PGconn connection information. * Really shouldn't be visible to users */ @@ -148,7 +148,7 @@ extern "C" #ifndef WIN32 struct sockaddr_un un; #endif - } PGSockAddr; + } PGSockAddr; /* large-object-access data ... allocated only if large-object code is used. * Really shouldn't be visible to users */ @@ -187,13 +187,14 @@ extern "C" FILE *Pfdebug; /* Callback procedure for notice/error message processing */ - PQnoticeProcessor noticeHook; + PQnoticeProcessor noticeHook; void *noticeArg; /* Status indicators */ - ConnStatusType status; - PGAsyncStatusType asyncStatus; - Dllist *notifyList; /* Notify msgs not yet handed to application */ + ConnStatusType status; + PGAsyncStatusType asyncStatus; + Dllist *notifyList; /* Notify msgs not yet handed to + * application */ /* Connection data */ int sock; /* Unix FD for socket, -1 if not connected */ @@ -205,28 +206,33 @@ extern "C" int be_pid; /* PID of backend --- needed for cancels */ int be_key; /* key of backend --- needed for cancels */ char salt[2]; /* password salt received from backend */ - PGlobjfuncs *lobjfuncs; /* private state for large-object access fns */ + PGlobjfuncs *lobjfuncs; /* private state for large-object access + * fns */ /* Buffer for data received from backend and not yet processed */ - char *inBuffer; /* currently allocated buffer */ + char *inBuffer; /* currently allocated buffer */ int inBufSize; /* allocated size of buffer */ - int inStart; /* offset to first unconsumed data in buffer */ + int inStart; /* offset to first unconsumed data in + * buffer */ int inCursor; /* next byte to tentatively consume */ - int inEnd; /* offset to first position after avail data */ + int inEnd; /* offset to first position after avail + * data */ /* Buffer for data not yet sent to backend */ - char *outBuffer; /* currently allocated buffer */ - int outBufSize; /* allocated size of buffer */ + char *outBuffer; /* currently allocated buffer */ + int outBufSize; /* allocated size of buffer */ int outCount; /* number of chars waiting in buffer */ /* Status for asynchronous result construction */ - PGresult *result; /* result being constructed */ - PGresAttValue *curTuple; /* tuple currently being read */ - - /* Message space. Placed last for code-size reasons. - * errorMessage is the message last returned to the application. - * When asyncStatus=READY, asyncErrorMessage is the pending message - * that will be put in errorMessage by PQgetResult. */ + PGresult *result; /* result being constructed */ + PGresAttValue *curTuple;/* tuple currently being read */ + + /* + * Message space. Placed last for code-size reasons. errorMessage + * is the message last returned to the application. When + * asyncStatus=READY, asyncErrorMessage is the pending message + * that will be put in errorMessage by PQgetResult. + */ char errorMessage[ERROR_MSG_LENGTH]; char asyncErrorMessage[ERROR_MSG_LENGTH]; } PGconn; @@ -303,8 +309,8 @@ extern "C" extern PGconn *PQconnectdb(const char *conninfo); extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, const char *pgtty, - const char *dbName, - const char *login, const char *pwd); + const char *dbName, + const char *login, const char *pwd); #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \ PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL) @@ -313,6 +319,7 @@ extern "C" /* close the current connection and free the PGconn data structure */ extern void PQfinish(PGconn *conn); + /* * close the current connection and restablish a new one with the same * parameters @@ -331,16 +338,16 @@ extern "C" extern char *PQtty(PGconn *conn); extern ConnStatusType PQstatus(PGconn *conn); extern char *PQerrorMessage(PGconn *conn); - extern int PQsocket(PGconn *conn); + extern int PQsocket(PGconn *conn); /* Enable/disable tracing */ extern void PQtrace(PGconn *conn, FILE *debug_port); extern void PQuntrace(PGconn *conn); /* Override default notice processor */ - extern void PQsetNoticeProcessor (PGconn *conn, - PQnoticeProcessor proc, - void *arg); + extern void PQsetNoticeProcessor(PGconn *conn, + PQnoticeProcessor proc, + void *arg); /* === in fe-exec.c === */ @@ -349,7 +356,7 @@ extern "C" extern PGnotify *PQnotifies(PGconn *conn); /* Interface for multiple-result or asynchronous queries */ - extern int PQsendQuery(PGconn *conn, const char *query); + extern int PQsendQuery(PGconn *conn, const char *query); extern PGresult *PQgetResult(PGconn *conn); /* Routines for managing an asychronous query */ @@ -362,14 +369,17 @@ extern "C" extern void PQputnbytes(PGconn *conn, const char *buffer, int nbytes); extern int PQendcopy(PGconn *conn); - /* "Fast path" interface --- not really recommended for application use */ + /* + * "Fast path" interface --- not really recommended for application + * use + */ extern PGresult *PQfn(PGconn *conn, - int fnid, - int *result_buf, - int *result_len, - int result_is_int, - PQArgBlock *args, - int nargs); + int fnid, + int *result_buf, + int *result_len, + int result_is_int, + PQArgBlock *args, + int nargs); /* Accessor functions for PGresult objects */ extern ExecStatusType PQresultStatus(PGresult *res); @@ -393,31 +403,32 @@ extern "C" /* === in fe-print.c === */ extern void PQprint(FILE *fout, /* output stream */ - PGresult *res, - PQprintOpt *ps); /* option structure */ + PGresult *res, + PQprintOpt *ps); /* option structure */ - /* PQdisplayTuples() is a better version of PQprintTuples(), - * but both are obsoleted by PQprint(). + /* + * PQdisplayTuples() is a better version of PQprintTuples(), but both + * are obsoleted by PQprint(). */ extern void PQdisplayTuples(PGresult *res, - FILE *fp, /* where to send the - * output */ - int fillAlign, /* pad the fields with - * spaces */ - const char *fieldSep, /* field separator */ - int printHeader, /* display headers? */ - int quiet); + FILE *fp, /* where to send the + * output */ + int fillAlign, /* pad the fields with + * spaces */ + const char *fieldSep, /* field separator */ + int printHeader, /* display headers? */ + int quiet); extern void PQprintTuples(PGresult *res, - FILE *fout, /* output stream */ - int printAttName, /* print attribute names - * or not */ - int terseOutput, /* delimiter bars or - * not? */ - int width); /* width of column, if - * 0, use variable width */ + FILE *fout, /* output stream */ + int printAttName, /* print attribute names + * or not */ + int terseOutput, /* delimiter bars or + * not? */ + int width); /* width of column, if + * 0, use variable width */ #ifdef MULTIBYTE - extern int PQmblen(unsigned char *s); + extern int PQmblen(unsigned char *s); #endif /* === in fe-lobj.c === */ @@ -436,6 +447,7 @@ extern "C" #ifdef __cplusplus }; + #endif -#endif /* LIBPQ_FE_H */ +#endif /* LIBPQ_FE_H */ diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index d330c036de..c338fa9233 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: libpq-int.h,v 1.1 1998/08/17 03:52:36 scrappy Exp $ + * $Id: libpq-int.h,v 1.2 1998/09/01 04:40:12 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -27,7 +27,7 @@ * NB: we used to use PG_PROTOCOL_LATEST from the backend pqcomm.h file, * but that's not really the right thing: just recompiling libpq * against a more recent backend isn't going to magically update it - * for most sorts of protocol changes. So, when you change libpq + * for most sorts of protocol changes. So, when you change libpq * to support a different protocol revision, you have to change this * constant too. PG_PROTOCOL_EARLIEST and PG_PROTOCOL_LATEST in * pqcomm.h describe what the backend knows, not what libpq knows. @@ -46,28 +46,29 @@ /* === in fe-connect.c === */ - extern int pqPacketSend(PGconn *conn, const char *buf, size_t len); +extern int pqPacketSend(PGconn *conn, const char *buf, size_t len); /* === in fe-exec.c === */ - extern void pqClearAsyncResult(PGconn *conn); +extern void pqClearAsyncResult(PGconn *conn); /* === in fe-misc.c === */ - /* "Get" and "Put" routines return 0 if successful, EOF if not. - * Note that for Get, EOF merely means the buffer is exhausted, - * not that there is necessarily any error. - */ - extern int pqGetc(char *result, PGconn *conn); - extern int pqGets(char *s, int maxlen, PGconn *conn); - extern int pqPuts(const char *s, PGconn *conn); - extern int pqGetnchar(char *s, int len, PGconn *conn); - extern int pqPutnchar(const char *s, int len, PGconn *conn); - extern int pqGetInt(int *result, int bytes, PGconn *conn); - extern int pqPutInt(int value, int bytes, PGconn *conn); - extern int pqReadData(PGconn *conn); - extern int pqFlush(PGconn *conn); - extern int pqWait(int forRead, int forWrite, PGconn *conn); + /* + * "Get" and "Put" routines return 0 if successful, EOF if not. Note that + * for Get, EOF merely means the buffer is exhausted, not that there is + * necessarily any error. + */ +extern int pqGetc(char *result, PGconn *conn); +extern int pqGets(char *s, int maxlen, PGconn *conn); +extern int pqPuts(const char *s, PGconn *conn); +extern int pqGetnchar(char *s, int len, PGconn *conn); +extern int pqPutnchar(const char *s, int len, PGconn *conn); +extern int pqGetInt(int *result, int bytes, PGconn *conn); +extern int pqPutInt(int value, int bytes, PGconn *conn); +extern int pqReadData(PGconn *conn); +extern int pqFlush(PGconn *conn); +extern int pqWait(int forRead, int forWrite, PGconn *conn); /* max length of message to send */ #define MAX_MESSAGE_LEN 8193 @@ -90,8 +91,9 @@ #ifndef strerror #if defined(sun) && defined(sparc) && !defined(__SVR4) extern char *sys_errlist[]; + #define strerror(A) (sys_errlist[(A)]) -#endif /* sunos4 */ -#endif /* !strerror */ +#endif /* sunos4 */ +#endif /* !strerror */ -#endif /* LIBPQ_INT_H */ +#endif /* LIBPQ_INT_H */ diff --git a/src/interfaces/libpq/libpqdll.c b/src/interfaces/libpq/libpqdll.c index a72e2289a0..d8cb6b6a74 100644 --- a/src/interfaces/libpq/libpqdll.c +++ b/src/interfaces/libpq/libpqdll.c @@ -1,6 +1,8 @@ #define WIN32_LEAN_AND_MEAN #include <windows.h> -BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, - LPVOID lpReserved ){ +BOOL WINAPI +DllMain(HINSTANCE hinstDLL, DWORD fdwReason, + LPVOID lpReserved) +{ return TRUE; } diff --git a/src/interfaces/libpq/pqsignal.c b/src/interfaces/libpq/pqsignal.c index 8e48a1b94d..d1a59004e4 100644 --- a/src/interfaces/libpq/pqsignal.c +++ b/src/interfaces/libpq/pqsignal.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/pqsignal.c,v 1.8 1998/09/01 03:28:56 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/pqsignal.c,v 1.9 1998/09/01 04:40:14 momjian Exp $ * * NOTES * This shouldn't be in libpq, but the monitor and some other @@ -38,5 +38,5 @@ pqsignal(int signo, pqsigfunc func) if (sigaction(signo, &act, &oact) < 0) return SIG_ERR; return oact.sa_handler; -#endif /* !USE_POSIX_SIGNALS */ +#endif /* !USE_POSIX_SIGNALS */ } diff --git a/src/interfaces/libpq/pqsignal.h b/src/interfaces/libpq/pqsignal.h index 4c34a37ebe..d86ad8c346 100644 --- a/src/interfaces/libpq/pqsignal.h +++ b/src/interfaces/libpq/pqsignal.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pqsignal.h,v 1.4 1997/09/08 02:40:38 momjian Exp $ + * $Id: pqsignal.h,v 1.5 1998/09/01 04:40:15 momjian Exp $ * * NOTES * This shouldn't be in libpq, but the monitor and some other @@ -23,4 +23,4 @@ typedef void (*pqsigfunc) (int); extern pqsigfunc pqsignal(int signo, pqsigfunc func); -#endif /* PQSIGNAL_H */ +#endif /* PQSIGNAL_H */ diff --git a/src/interfaces/libpq/win32.h b/src/interfaces/libpq/win32.h index 878c7afbd4..b2a5e3db74 100644 --- a/src/interfaces/libpq/win32.h +++ b/src/interfaces/libpq/win32.h @@ -11,7 +11,7 @@ /* - * Some compat functions + * Some compat functions */ #define open(a,b,c) _open(a,b,c) #define read(a,b,c) _read(a,b,c) diff --git a/src/interfaces/python/pgmodule.c b/src/interfaces/python/pgmodule.c index 44f4856e26..47960ae894 100644 --- a/src/interfaces/python/pgmodule.c +++ b/src/interfaces/python/pgmodule.c @@ -43,15 +43,15 @@ static PyObject *PGError; #ifndef NO_DIRECT #define DIRECT_ACCESS 1 /* enables direct access functions */ -#endif /* NO_DIRECT */ +#endif /* NO_DIRECT */ #ifndef NO_LARGE #define LARGE_OBJECTS 1 /* enables large objects support */ -#endif /* NO_LARGE */ +#endif /* NO_LARGE */ #ifndef NO_DEF_VAR #define DEFAULT_VARS 1 /* enables default variables use */ -#endif /* NO_DEF_VAR */ +#endif /* NO_DEF_VAR */ /* --------------------------------------------------------------------- */ @@ -65,7 +65,7 @@ PyObject *pg_default_opt; /* default connection options */ PyObject *pg_default_tty; /* default debug tty */ PyObject *pg_default_port; /* default connection port */ -#endif /* DEFAULT_VARS */ +#endif /* DEFAULT_VARS */ /* --------------------------------------------------------------------- */ @@ -110,7 +110,7 @@ typedef struct staticforward PyTypeObject PglargeType; #define is_pglargeobject(v) ((v)->ob_type == &PglargeType) -#endif /* LARGE_OBJECTS */ +#endif /* LARGE_OBJECTS */ /* --------------------------------------------------------------------- */ @@ -148,7 +148,7 @@ check_lo(pglargeobject * self, int level) return 1; } -#endif /* LARGE_OBJECTS */ +#endif /* LARGE_OBJECTS */ /* --------------------------------------------------------------------- */ @@ -649,7 +649,7 @@ staticforward PyTypeObject PglargeType = { 0, /* tp_hash */ }; -#endif /* LARGE_OBJECTS */ +#endif /* LARGE_OBJECTS */ /* --------------------------------------------------------------------- */ @@ -823,7 +823,7 @@ pgconnect(pgobject * self, PyObject * args, PyObject * dict) if ((!pgdbname) && (pg_default_base != Py_None)) pgdbname = PyString_AsString(pg_default_base); -#endif /* DEFAULT_VARS */ +#endif /* DEFAULT_VARS */ if ((npgobj = PyObject_NEW(pgobject, &PgType)) == NULL) return NULL; @@ -1213,7 +1213,7 @@ pg_endcopy(pgobject * self, PyObject * args) return Py_None; } -#endif /* DIRECT_ACCESS */ +#endif /* DIRECT_ACCESS */ static PyObject * @@ -1381,13 +1381,13 @@ static struct PyMethodDef pgobj_methods[] = { {"putline", (PyCFunction) pg_putline, 1}, /* direct access: putline */ {"getline", (PyCFunction) pg_getline, 1}, /* direct access: getline */ {"endcopy", (PyCFunction) pg_endcopy, 1}, /* direct access: endcopy */ -#endif /* DIRECT_ACCESS */ +#endif /* DIRECT_ACCESS */ #ifdef LARGE_OBJECTS {"locreate", (PyCFunction) pg_locreate, 1}, /* creates large object */ {"getlo", (PyCFunction) pg_getlo, 1}, /* get lo from oid */ {"loimport", (PyCFunction) pg_loimport, 1}, /* imports lo from file */ -#endif /* LARGE_OBJECTS */ +#endif /* LARGE_OBJECTS */ {NULL, NULL} /* sentinel */ }; @@ -1747,7 +1747,7 @@ pgsetdefport(PyObject * self, PyObject * args) return old; } -#endif /* DEFAULT_VARS */ +#endif /* DEFAULT_VARS */ /* List of functions defined in the module */ @@ -1765,7 +1765,7 @@ static struct PyMethodDef pg_methods[] = { {"set_deftty", pgsetdeftty, 1}, /* sets default debug tty */ {"get_defport", pggetdefport, 1}, /* gets default port */ {"set_defport", pgsetdefport, 1}, /* sets default port */ -#endif /* DEFAULT_VARS */ +#endif /* DEFAULT_VARS */ {NULL, NULL} /* sentinel */ }; @@ -1796,7 +1796,7 @@ initpg(void) PyDict_SetItemString(dict, "SEEK_SET", PyInt_FromLong(SEEK_SET)); PyDict_SetItemString(dict, "SEEK_CUR", PyInt_FromLong(SEEK_CUR)); PyDict_SetItemString(dict, "SEEK_END", PyInt_FromLong(SEEK_END)); -#endif /* LARGE_OBJECTS */ +#endif /* LARGE_OBJECTS */ #ifdef DEFAULT_VARS /* prepares default values */ @@ -1810,7 +1810,7 @@ initpg(void) pg_default_port = Py_None; Py_INCREF(Py_None); pg_default_tty = Py_None; -#endif /* DEFAULT_VARS */ +#endif /* DEFAULT_VARS */ /* Check for errors */ if (PyErr_Occurred()) |
