diff options
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/pg_dump/common.c | 20 | ||||
-rw-r--r-- | src/bin/pg_dump/pg_dump.c | 404 | ||||
-rw-r--r-- | src/bin/pg_dump/pg_dump.h | 11 | ||||
-rw-r--r-- | src/bin/pg_passwd/pg_passwd.c | 4 | ||||
-rw-r--r-- | src/bin/psql/psql.c | 296 | ||||
-rw-r--r-- | src/bin/psql/stringutils.c | 4 |
6 files changed, 396 insertions, 343 deletions
diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c index e4a17d3654..dddc211ea8 100644 --- a/src/bin/pg_dump/common.c +++ b/src/bin/pg_dump/common.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.19 1997/11/21 19:02:50 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.20 1998/02/26 04:38:41 momjian Exp $ * * Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2 * @@ -56,7 +56,7 @@ static int strInArray(const char *pattern, char **arr, int arr_size); * NOTE: should hash this, but just do linear search for now */ -char * +char * findTypeByOid(TypeInfo *tinfo, int numTypes, const char *oid) { int i; @@ -84,7 +84,7 @@ findTypeByOid(TypeInfo *tinfo, int numTypes, const char *oid) * NOTE: should hash this, but just do linear search for now * */ -char * +char * findOprByOid(OprInfo *oprinfo, int numOprs, const char *oid) { int i; @@ -482,15 +482,19 @@ fmtId(const char *rawid) static char id[MAXQUERYLEN]; for (cp = rawid; *cp != '\0'; cp++) - if (! (islower(*cp) || isdigit(*cp) || (*cp == '_'))) break; + if (!(islower(*cp) || isdigit(*cp) || (*cp == '_'))) + break; - if (*cp != '\0') { + if (*cp != '\0') + { strcpy(id, "\""); strcat(id, rawid); strcat(id, "\""); cp = id; - } else { + } + else + { cp = rawid; } - return(cp); -} /* fmtId() */ + return (cp); +} /* fmtId() */ diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 16cc679738..9e1b78a60f 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -21,7 +21,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.63 1998/02/18 15:33:37 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.64 1998/02/26 04:38:54 momjian Exp $ * * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * @@ -47,7 +47,7 @@ * * * Modifications - 1/26/98 - pjlobo@euitt.upm.es - * - Added support for password authentication + * - Added support for password authentication *------------------------------------------------------------------------- */ @@ -79,8 +79,9 @@ #include "pg_dump.h" static void dumpSequence(FILE *fout, TableInfo tbinfo); -static void dumpTriggers(FILE *fout, const char *tablename, - TableInfo *tblinfo, int numTables); +static void +dumpTriggers(FILE *fout, const char *tablename, + TableInfo *tblinfo, int numTables); static char *checkForQuote(const char *s); static void clearTableInfo(TableInfo *, int); static void @@ -92,7 +93,7 @@ static void setMaxOid(FILE *fout); static char *AddAcl(char *s, const char *add); static char *GetPrivledges(char *s); -static ACL *ParseACL(const char *acls,int *count); +static ACL *ParseACL(const char *acls, int *count); extern char *optarg; extern int optind, @@ -126,7 +127,7 @@ usage(const char *progname) fprintf(stderr, "\t -d \t\t dump data as proper insert strings\n"); fprintf(stderr, - "\t -D \t\t dump data as inserts with attribute names\n"); + "\t -D \t\t dump data as inserts with attribute names\n"); fprintf(stderr, "\t -f filename \t\t script output filename\n"); fprintf(stderr, @@ -421,7 +422,7 @@ dumpClasses(const TableInfo tblinfo[], const int numTables, FILE *fout, if (g_verbose) fprintf(stderr, "%s dumping out the contents of %s of %d tables %s\n", g_comment_start, all_only, numTables, g_comment_end); - + /* Dump SEQUENCEs first (if dataOnly) */ if (dataOnly) { @@ -433,7 +434,7 @@ dumpClasses(const TableInfo tblinfo[], const int numTables, FILE *fout, { if (g_verbose) fprintf(stderr, "%s dumping out schema of sequence %s %s\n", - g_comment_start, tblinfo[i].relname, g_comment_end); + g_comment_start, tblinfo[i].relname, g_comment_end); fprintf(fout, "\\connect - %s\n", tblinfo[i].usename); dumpSequence(fout, tblinfo[i]); } @@ -447,8 +448,8 @@ dumpClasses(const TableInfo tblinfo[], const int numTables, FILE *fout, /* Skip VIEW relations */ if (isViewRule(tblinfo[i].relname)) continue; - - if (tblinfo[i].sequence) /* already dumped */ + + if (tblinfo[i].sequence)/* already dumped */ continue; if (!onlytable || (!strcmp(classname, onlytable))) @@ -532,16 +533,16 @@ main(int argc, char **argv) const char *dbname = NULL; const char *pghost = NULL; const char *pgport = NULL; - char *tablename = NULL; + char *tablename = NULL; int oids = 0, acls = 0; TableInfo *tblinfo; int numTables; - char connect_string[512] = ""; - char tmp_string[128]; - char username[64]; - char password[64]; - int use_password = 0; + char connect_string[512] = ""; + char tmp_string[128]; + char username[64]; + char password[64]; + int use_password = 0; g_verbose = false; @@ -585,7 +586,7 @@ main(int argc, char **argv) break; case 't': /* Dump data for this table only */ { - int i; + int i; tablename = strdup(optarg); for (i = 0; tablename[i]; i++) @@ -633,28 +634,32 @@ main(int argc, char **argv) exit(2); } - /*g_conn = PQsetdb(pghost, pgport, NULL, NULL, dbname);*/ - if (pghost != NULL) { - sprintf(tmp_string, "host=%s ", pghost); - strcat(connect_string, tmp_string); + /* g_conn = PQsetdb(pghost, pgport, NULL, NULL, dbname); */ + if (pghost != NULL) + { + sprintf(tmp_string, "host=%s ", pghost); + strcat(connect_string, tmp_string); } - if (pgport != NULL) { - sprintf(tmp_string, "port=%s ", pgport); - strcat(connect_string, tmp_string); + if (pgport != NULL) + { + sprintf(tmp_string, "port=%s ", pgport); + strcat(connect_string, tmp_string); } - if (dbname != NULL) { - sprintf(tmp_string, "dbname=%s ", dbname); - strcat(connect_string, tmp_string); + if (dbname != NULL) + { + sprintf(tmp_string, "dbname=%s ", dbname); + strcat(connect_string, tmp_string); } - if (use_password) { - prompt_for_password(username, password); - strcat(connect_string, "authtype=password "); - sprintf(tmp_string, "user=%s ", username); - strcat(connect_string, tmp_string); - sprintf(tmp_string, "password=%s ", password); - strcat(connect_string, tmp_string); - bzero(tmp_string, sizeof(tmp_string)); - bzero(password, sizeof(password)); + if (use_password) + { + prompt_for_password(username, password); + strcat(connect_string, "authtype=password "); + sprintf(tmp_string, "user=%s ", username); + strcat(connect_string, tmp_string); + sprintf(tmp_string, "password=%s ", password); + strcat(connect_string, tmp_string); + bzero(tmp_string, sizeof(tmp_string)); + bzero(password, sizeof(password)); } g_conn = PQconnectdb(connect_string); bzero(connect_string, sizeof(connect_string)); @@ -685,7 +690,8 @@ main(int argc, char **argv) dumpClasses(tblinfo, numTables, g_fout, tablename, oids); } - if (!dataOnly) /* dump indexes and triggers at the end for performance */ + if (!dataOnly) /* dump indexes and triggers at the end + * for performance */ { dumpSchemaIdx(g_fout, tablename, tblinfo, numTables); dumpTriggers(g_fout, tablename, tblinfo, numTables); @@ -1407,8 +1413,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs) PQclear(res); sprintf(query, - "SELECT pg_class.oid, relname, relkind, relacl, usename, " - "relchecks, reltriggers " + "SELECT pg_class.oid, relname, relkind, relacl, usename, " + "relchecks, reltriggers " "from pg_class, pg_user " "where relowner = usesysid and " "(relkind = 'r' or relkind = 'S') and relname !~ '^pg_' " @@ -1445,27 +1451,28 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs) tblinfo[i].usename = strdup(PQgetvalue(res, i, i_usename)); tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks)); tblinfo[i].ntrig = atoi(PQgetvalue(res, i, i_reltriggers)); - + /* Get CHECK constraints */ if (tblinfo[i].ncheck > 0) { PGresult *res2; - int i_rcname, i_rcsrc; + int i_rcname, + i_rcsrc; int ntups2; int i2; - + if (g_verbose) fprintf(stderr, "%s finding CHECK constraints for relation: %s %s\n", g_comment_start, tblinfo[i].relname, g_comment_end); - + sprintf(query, "SELECT rcname, rcsrc from pg_relcheck " "where rcrelid = '%s'::oid ", tblinfo[i].oid); res2 = PQexec(g_conn, query); if (!res2 || - PQresultStatus(res2) != PGRES_TUPLES_OK) + PQresultStatus(res2) != PGRES_TUPLES_OK) { fprintf(stderr, "getTables(): SELECT (for CHECK) failed\n"); exit_nicely(g_conn); @@ -1474,49 +1481,53 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs) if (ntups2 != tblinfo[i].ncheck) { fprintf(stderr, "getTables(): relation %s: %d CHECKs were expected, but got %d\n", - tblinfo[i].relname, tblinfo[i].ncheck, ntups2); + tblinfo[i].relname, tblinfo[i].ncheck, ntups2); exit_nicely(g_conn); } i_rcname = PQfnumber(res2, "rcname"); i_rcsrc = PQfnumber(res2, "rcsrc"); - tblinfo[i].check_expr = (char **) malloc (ntups2 * sizeof (char *)); + tblinfo[i].check_expr = (char **) malloc(ntups2 * sizeof(char *)); for (i2 = 0; i2 < ntups2; i2++) { - char *name = PQgetvalue(res2, i2, i_rcname); - char *expr = PQgetvalue(res2, i2, i_rcsrc); - + char *name = PQgetvalue(res2, i2, i_rcname); + char *expr = PQgetvalue(res2, i2, i_rcsrc); + query[0] = 0; - if ( name[0] != '$' ) - sprintf (query, "CONSTRAINT %s ", name); - sprintf (query, "%sCHECK %s", query, expr); - tblinfo[i].check_expr[i2] = strdup (query); + if (name[0] != '$') + sprintf(query, "CONSTRAINT %s ", name); + sprintf(query, "%sCHECK %s", query, expr); + tblinfo[i].check_expr[i2] = strdup(query); } PQclear(res2); } else tblinfo[i].check_expr = NULL; - + /* Get Triggers */ if (tblinfo[i].ntrig > 0) { PGresult *res2; - int i_tgname, i_tgfoid, i_tgtype, i_tgnargs, i_tgargs; + int i_tgname, + i_tgfoid, + i_tgtype, + i_tgnargs, + i_tgargs; int ntups2; int i2; - + if (g_verbose) fprintf(stderr, "%s finding Triggers for relation: %s %s\n", g_comment_start, tblinfo[i].relname, g_comment_end); - + sprintf(query, "SELECT tgname, tgfoid, tgtype, tgnargs, tgargs " "from pg_trigger " "where tgrelid = '%s'::oid ", tblinfo[i].oid); res2 = PQexec(g_conn, query); if (!res2 || - PQresultStatus(res2) != PGRES_TUPLES_OK) + PQresultStatus(res2) != PGRES_TUPLES_OK) { fprintf(stderr, "getTables(): SELECT (for TRIGGER) failed\n"); exit_nicely(g_conn); @@ -1525,7 +1536,7 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs) if (ntups2 != tblinfo[i].ntrig) { fprintf(stderr, "getTables(): relation %s: %d Triggers were expected, but got %d\n", - tblinfo[i].relname, tblinfo[i].ntrig, ntups2); + tblinfo[i].relname, tblinfo[i].ntrig, ntups2); exit_nicely(g_conn); } i_tgname = PQfnumber(res2, "tgname"); @@ -1533,72 +1544,73 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs) i_tgtype = PQfnumber(res2, "tgtype"); i_tgnargs = PQfnumber(res2, "tgnargs"); i_tgargs = PQfnumber(res2, "tgargs"); - tblinfo[i].triggers = (char **) malloc (ntups2 * sizeof (char *)); + tblinfo[i].triggers = (char **) malloc(ntups2 * sizeof(char *)); for (i2 = 0, query[0] = 0; i2 < ntups2; i2++) { - char *tgfunc = PQgetvalue(res2, i2, i_tgfoid); - int2 tgtype = atoi(PQgetvalue(res2, i2, i_tgtype)); - int tgnargs = atoi(PQgetvalue(res2, i2, i_tgnargs)); - char *tgargs = PQgetvalue(res2, i2, i_tgargs); - char *p; - char farg[MAXQUERYLEN]; - int findx; - + char *tgfunc = PQgetvalue(res2, i2, i_tgfoid); + int2 tgtype = atoi(PQgetvalue(res2, i2, i_tgtype)); + int tgnargs = atoi(PQgetvalue(res2, i2, i_tgnargs)); + char *tgargs = PQgetvalue(res2, i2, i_tgargs); + char *p; + char farg[MAXQUERYLEN]; + int findx; + for (findx = 0; findx < numFuncs; findx++) { if (strcmp(finfo[findx].oid, tgfunc) == 0 && - finfo[findx].lang == ClanguageId && - finfo[findx].nargs == 0 && + finfo[findx].lang == ClanguageId && + finfo[findx].nargs == 0 && strcmp(finfo[findx].prorettype, "0") == 0) break; } if (findx == numFuncs) { fprintf(stderr, "getTables(): relation %s: cannot find function with oid %s for trigger %s\n", - tblinfo[i].relname, tgfunc, PQgetvalue(res2, i2, i_tgname)); + tblinfo[i].relname, tgfunc, PQgetvalue(res2, i2, i_tgname)); exit_nicely(g_conn); } tgfunc = finfo[findx].proname; - sprintf (query, "CREATE TRIGGER %s ", PQgetvalue(res2, i2, i_tgname)); + sprintf(query, "CREATE TRIGGER %s ", PQgetvalue(res2, i2, i_tgname)); /* Trigger type */ findx = 0; if (TRIGGER_FOR_BEFORE(tgtype)) - strcat (query, "BEFORE"); + strcat(query, "BEFORE"); else - strcat (query, "AFTER"); + strcat(query, "AFTER"); if (TRIGGER_FOR_INSERT(tgtype)) { - strcat (query, " INSERT"); + strcat(query, " INSERT"); findx++; } if (TRIGGER_FOR_DELETE(tgtype)) { if (findx > 0) - strcat (query, " OR DELETE"); + strcat(query, " OR DELETE"); else - strcat (query, " DELETE"); + strcat(query, " DELETE"); findx++; } if (TRIGGER_FOR_UPDATE(tgtype)) if (findx > 0) - strcat (query, " OR UPDATE"); + strcat(query, " OR UPDATE"); else - strcat (query, " UPDATE"); - sprintf (query, "%s ON %s FOR EACH ROW EXECUTE PROCEDURE %s (", - query, tblinfo[i].relname, tgfunc); + strcat(query, " UPDATE"); + sprintf(query, "%s ON %s FOR EACH ROW EXECUTE PROCEDURE %s (", + query, tblinfo[i].relname, tgfunc); for (findx = 0; findx < tgnargs; findx++) { - char *s, *d; - - for (p = tgargs; ; ) + char *s, + *d; + + for (p = tgargs;;) { - p = strchr (p, '\\'); + p = strchr(p, '\\'); if (p == NULL) { fprintf(stderr, "getTables(): relation %s: bad argument string (%s) for trigger %s\n", - tblinfo[i].relname, - PQgetvalue(res2, i2, i_tgargs), - PQgetvalue(res2, i2, i_tgname)); + tblinfo[i].relname, + PQgetvalue(res2, i2, i_tgargs), + PQgetvalue(res2, i2, i_tgname)); exit_nicely(g_conn); } p++; @@ -1607,23 +1619,23 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs) p++; continue; } - if ( p[0] == '0' && p[1] == '0' && p[2] == '0') + if (p[0] == '0' && p[1] == '0' && p[2] == '0') break; } p--; - for (s = tgargs, d = &(farg[0]); s < p; ) + for (s = tgargs, d = &(farg[0]); s < p;) { if (*s == '\'') *d++ = '\\'; *d++ = *s++; } *d = 0; - sprintf (query, "%s'%s'%s", query, farg, - (findx < tgnargs - 1) ? ", " : ""); + sprintf(query, "%s'%s'%s", query, farg, + (findx < tgnargs - 1) ? ", " : ""); tgargs = p + 4; } - strcat (query, ");\n"); - tblinfo[i].triggers[i2] = strdup (query); + strcat(query, ");\n"); + tblinfo[i].triggers[i2] = strdup(query); } PQclear(res2); } @@ -1786,19 +1798,19 @@ getTableAttrs(TableInfo *tblinfo, int numTables) if (PQgetvalue(res, j, i_atthasdef)[0] == 't') { PGresult *res2; - + if (g_verbose) fprintf(stderr, "%s finding DEFAULT expression for attr: %s %s\n", g_comment_start, tblinfo[i].attnames[j], g_comment_end); - + sprintf(q, "SELECT adsrc from pg_attrdef " "where adrelid = '%s'::oid and adnum = %d ", tblinfo[i].oid, j + 1); res2 = PQexec(g_conn, q); if (!res2 || - PQresultStatus(res2) != PGRES_TUPLES_OK) + PQresultStatus(res2) != PGRES_TUPLES_OK) { fprintf(stderr, "getTableAttrs(): SELECT (for DEFAULT) failed\n"); exit_nicely(g_conn); @@ -2037,11 +2049,11 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i, (finfo[i].retset) ? " SETOF " : "", fmtId(findTypeByOid(tinfo, numTypes, finfo[i].prorettype)), (finfo[i].lang == INTERNALlanguageId) ? finfo[i].prosrc : - (finfo[i].lang == ClanguageId) ? finfo[i].probin : - (finfo[i].lang == SQLlanguageId) ? finfo[i].prosrc : "unknown", + (finfo[i].lang == ClanguageId) ? finfo[i].probin : + (finfo[i].lang == SQLlanguageId) ? finfo[i].prosrc : "unknown", (finfo[i].lang == INTERNALlanguageId) ? "INTERNAL" : - (finfo[i].lang == ClanguageId) ? "C" : - (finfo[i].lang == SQLlanguageId) ? "SQL" : "unknown"); + (finfo[i].lang == ClanguageId) ? "C" : + (finfo[i].lang == SQLlanguageId) ? "SQL" : "unknown"); fputs(q, fout); @@ -2091,13 +2103,13 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators, strcmp(oprinfo[i].oprkind, "b") == 0) { sprintf(leftarg, ", LEFTARG = %s ", - fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft))); + fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprleft))); } if (strcmp(oprinfo[i].oprkind, "l") == 0 || strcmp(oprinfo[i].oprkind, "b") == 0) { sprintf(rightarg, ", RIGHTARG = %s ", - fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprright))); + fmtId(findTypeByOid(tinfo, numTypes, oprinfo[i].oprright))); } if (strcmp(oprinfo[i].oprcom, "0") == 0) commutator[0] = '\0'; @@ -2177,10 +2189,10 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs, /* skip all the builtin oids */ if (atoi(agginfo[i].oid) < g_last_builtin_oid) continue; - + sprintf(basetype, "BASETYPE = %s, ", - fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggbasetype))); + fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggbasetype))); if (strcmp(agginfo[i].aggtransfn1, "-") == 0) sfunc1[0] = '\0'; @@ -2203,7 +2215,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs, sprintf(sfunc2, "SFUNC2 = %s, STYPE2 = %s", agginfo[i].aggtransfn2, - fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype2))); + fmtId(findTypeByOid(tinfo, numTypes, agginfo[i].aggtranstype2))); if (agginfo[i].agginitval2) sprintf(sfunc2, "%s, INITCOND2 = '%s'", sfunc2, agginfo[i].agginitval2); @@ -2257,16 +2269,17 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs, static char * AddAcl(char *s, const char *add) { - char *t; + char *t; - if (s == (char *)NULL) + if (s == (char *) NULL) return (strdup(add)); - t=(char *)calloc((strlen(s) + strlen(add)+1),sizeof(char)); - sprintf(t,"%s,%s",s,add); + t = (char *) calloc((strlen(s) + strlen(add) + 1), sizeof(char)); + sprintf(t, "%s,%s", s, add); - return(t); + return (t); } + /* * This will take a string of 'arwR' and return a * comma delimited string of SELECT,INSERT,UPDATE,DELETE,RULE @@ -2274,92 +2287,96 @@ AddAcl(char *s, const char *add) static char * GetPrivledges(char *s) { - char *acls=NULL; + char *acls = NULL; - /*Grant All == arwR */ - /* INSERT == ar */ - /* UPDATE/DELETE == rw */ - /* SELECT == r */ - /* RULE == R */ + /* Grant All == arwR */ + /* INSERT == ar */ + /* UPDATE/DELETE == rw */ + /* SELECT == r */ + /* RULE == R */ - if (strstr(s,"arwR")) - return(strdup("ALL")); + if (strstr(s, "arwR")) + return (strdup("ALL")); - if (strstr(s,"ar")) - acls=AddAcl(acls,"INSERT"); + if (strstr(s, "ar")) + acls = AddAcl(acls, "INSERT"); - if (strstr(s,"rw")) - acls=AddAcl(acls,"UPDATE,DELETE"); - else - if (strchr(s,'r')) - acls=AddAcl(acls,"SELECT"); + if (strstr(s, "rw")) + acls = AddAcl(acls, "UPDATE,DELETE"); + else if (strchr(s, 'r')) + acls = AddAcl(acls, "SELECT"); - if (strchr(s,'R')) - acls=AddAcl(acls,"RULES"); + if (strchr(s, 'R')) + acls = AddAcl(acls, "RULES"); - return(acls); + return (acls); } + /* This will parse the acl string of TableInfo * into a two deminsional aray: - * user | Privledges + * user | Privledges * So to reset the acls I need to grant these priviledges * to user */ static ACL * -ParseACL(const char *acls,int *count) +ParseACL(const char *acls, int *count) { - ACL *ParsedAcl=NULL; - int i, - len, - NumAcls=1, /*There is always public*/ - AclLen=0; - char *s=NULL, - *user=NULL, - *priv=NULL, - *tok; - - AclLen=strlen(acls); - - if (AclLen == 0) { - *count=0; + ACL *ParsedAcl = NULL; + int i, + len, + NumAcls = 1, /* There is always public */ + AclLen = 0; + char *s = NULL, + *user = NULL, + *priv = NULL, + *tok; + + AclLen = strlen(acls); + + if (AclLen == 0) + { + *count = 0; return (ACL *) NULL; } - for (i=0;i<AclLen;i++) + for (i = 0; i < AclLen; i++) if (acls[i] == ',') NumAcls++; - ParsedAcl=(ACL *)calloc(AclLen,sizeof(ACL)); - if (!ParsedAcl) { - fprintf(stderr,"Could not allocate space for ACLS!\n"); + ParsedAcl = (ACL *) calloc(AclLen, sizeof(ACL)); + if (!ParsedAcl) + { + fprintf(stderr, "Could not allocate space for ACLS!\n"); exit_nicely(g_conn); } - s=strdup(acls); - - /* Setup up public*/ - ParsedAcl[0].user=strdup("Public"); - tok=strtok(s,","); - ParsedAcl[0].privledges=GetPrivledges(strchr(tok,'=')); - - /*Do the rest of the users*/ - i=1; - while ((i < NumAcls) && ((tok=strtok(NULL,",")) != (char *)NULL)) { - /*User name is string up to = in tok*/ - len=strchr(tok,'=') - tok -1 ; - user=(char*)calloc(len+1,sizeof(char)); - strncpy(user,tok+1,len); - if (user[len-1] == '\"') - user[len-1]=(char)NULL; - priv=GetPrivledges(tok+len+2); - ParsedAcl[i].user=user; - ParsedAcl[i].privledges=priv; + s = strdup(acls); + + /* Setup up public */ + ParsedAcl[0].user = strdup("Public"); + tok = strtok(s, ","); + ParsedAcl[0].privledges = GetPrivledges(strchr(tok, '=')); + + /* Do the rest of the users */ + i = 1; + while ((i < NumAcls) && ((tok = strtok(NULL, ",")) != (char *) NULL)) + { + /* User name is string up to = in tok */ + len = strchr(tok, '=') - tok - 1; + user = (char *) calloc(len + 1, sizeof(char)); + strncpy(user, tok + 1, len); + if (user[len - 1] == '\"') + user[len - 1] = (char) NULL; + priv = GetPrivledges(tok + len + 2); + ParsedAcl[i].user = user; + ParsedAcl[i].privledges = priv; i++; } - *count=NumAcls; + *count = NumAcls; return (ParsedAcl); } + /* * dumpTables: * write out to fout all the user-define tables @@ -2379,8 +2396,8 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables, char **parentRels; /* list of names of parent relations */ int numParents; int actual_atts; /* number of attrs in this CREATE statment */ - ACL *ACLlist; - + ACL *ACLlist; + /* First - dump SEQUENCEs */ for (i = 0; i < numTables; i++) { @@ -2392,10 +2409,10 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables, dumpSequence(fout, tblinfo[i]); } } - + for (i = 0; i < numTables; i++) { - if (tblinfo[i].sequence) /* already dumped */ + if (tblinfo[i].sequence)/* already dumped */ continue; if (!tablename || (!strcmp(tblinfo[i].relname, tablename))) @@ -2427,7 +2444,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables, sprintf(q, "%s(%d)", q, - tblinfo[i].atttypmod[j]-VARHDRSZ); + tblinfo[i].atttypmod[j] - VARHDRSZ); actual_atts++; } else if (!strcmp(tblinfo[i].typnames[j], "varchar")) @@ -2440,7 +2457,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables, sprintf(q, "%s(%d)", q, - tblinfo[i].atttypmod[j]-VARHDRSZ); + tblinfo[i].atttypmod[j] - VARHDRSZ); actual_atts++; } else @@ -2486,25 +2503,28 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables, } strcat(q, ";\n"); fputs(q, fout); - - if (acls) { + + if (acls) + { ACLlist = ParseACL(tblinfo[i].relacl, &l); - if (ACLlist == (ACL *)NULL) + if (ACLlist == (ACL *) NULL) if (l == 0) continue; - else { - fprintf(stderr,"Could not parse ACL list for %s...Exiting!\n", - tblinfo[i].relname); + else + { + fprintf(stderr, "Could not parse ACL list for %s...Exiting!\n", + tblinfo[i].relname); exit_nicely(g_conn); - } + } /* Revoke Default permissions for PUBLIC */ fprintf(fout, - "REVOKE ALL on %s from PUBLIC;\n", + "REVOKE ALL on %s from PUBLIC;\n", tblinfo[i].relname); - for(k = 0; k < l; k++) { - if (ACLlist[k].privledges != (char *)NULL) + for (k = 0; k < l; k++) + { + if (ACLlist[k].privledges != (char *) NULL) fprintf(fout, "GRANT %s on %s to %s;\n", ACLlist[k].privledges, tblinfo[i].relname, @@ -2913,16 +2933,17 @@ dumpSequence(FILE *fout, TableInfo tbinfo) } -static void -dumpTriggers(FILE *fout, const char *tablename, - TableInfo *tblinfo, int numTables) +static void +dumpTriggers(FILE *fout, const char *tablename, + TableInfo *tblinfo, int numTables) { - int i, j; - + int i, + j; + if (g_verbose) fprintf(stderr, "%s dumping out triggers %s\n", g_comment_start, g_comment_end); - + for (i = 0; i < numTables; i++) { if (tablename && strcmp(tblinfo[i].relname, tablename)) @@ -2934,4 +2955,3 @@ dumpTriggers(FILE *fout, const char *tablename, } } } - diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 9aed251ce9..38da913d98 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -5,7 +5,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: pg_dump.h,v 1.29 1998/01/16 23:20:43 momjian Exp $ + * $Id: pg_dump.h,v 1.30 1998/02/26 04:39:01 momjian Exp $ * * Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2 * @@ -156,9 +156,10 @@ typedef struct _oprInfo * * Matthew C. Aycock 12/02/97 */ -typedef struct _AclType { - char *user; - char *privledges; +typedef struct _AclType +{ + char *user; + char *privledges; } ACL; @@ -242,7 +243,7 @@ dumpIndices(FILE *fout, IndInfo *indinfo, int numIndices, TableInfo *tbinfo, int numTables, const char *tablename); extern const char * -fmtId(const char *identifier); + fmtId(const char *identifier); /* largest query string size */ #define MAXQUERYLEN 5000 diff --git a/src/bin/pg_passwd/pg_passwd.c b/src/bin/pg_passwd/pg_passwd.c index 0973698ad6..de9ffd65a7 100644 --- a/src/bin/pg_passwd/pg_passwd.c +++ b/src/bin/pg_passwd/pg_passwd.c @@ -6,9 +6,9 @@ #include <stdio.h> #include <stdlib.h> #if defined(HAVE_STRING_H) -# include <string.h> +#include <string.h> #else -# include <strings.h> +#include <strings.h> #endif #include <unistd.h> #include <errno.h> diff --git a/src/bin/psql/psql.c b/src/bin/psql/psql.c index 0e3fb520af..2ec07c89d9 100644 --- a/src/bin/psql/psql.c +++ b/src/bin/psql/psql.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.135 1998/02/25 14:50:36 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.136 1998/02/26 04:39:09 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -99,12 +99,14 @@ typedef struct _psqlSettings #ifdef TIOCGWINSZ struct winsize screen_size; + #else struct winsize { int ws_row; int ws_col; } screen_size; + #endif /* declarations for functions in this file */ @@ -114,8 +116,9 @@ static void handleCopyOut(PGresult *res, FILE *copystream); static void handleCopyIn(PGresult *res, const bool mustprompt, FILE *copystream); -static int tableList(PsqlSettings *pset, bool deep_tablelist, - char info_type, bool system_tables); +static int +tableList(PsqlSettings *pset, bool deep_tablelist, + char info_type, bool system_tables); static int tableDesc(PsqlSettings *pset, char *table, FILE *fout); static int objectDescription(PsqlSettings *pset, char *object, FILE *fout); static int rightsList(PsqlSettings *pset); @@ -211,7 +214,7 @@ slashUsage(PsqlSettings *pset) else fout = stdout; - /* if you add/remove a line here, change the row test above */ + /* if you add/remove a line here, change the row test above */ fprintf(fout, " \\? -- help\n"); fprintf(fout, " \\a -- toggle field-alignment (currenty %s)\n", on(pset->opt.align)); fprintf(fout, " \\C [<captn>] -- set html3 caption (currently '%s')\n", pset->opt.caption ? pset->opt.caption : ""); @@ -306,7 +309,7 @@ listAllDbs(PsqlSettings *pset) */ int tableList(PsqlSettings *pset, bool deep_tablelist, char info_type, - bool system_tables) + bool system_tables) { char listbuf[512]; int nColumns; @@ -330,7 +333,7 @@ tableList(PsqlSettings *pset, bool deep_tablelist, char info_type, #ifdef TIOCGWINSZ } #endif - + listbuf[0] = '\0'; strcat(listbuf, "SELECT usename, relname, relkind, relhasrules "); strcat(listbuf, "FROM pg_class, pg_user "); @@ -408,27 +411,27 @@ tableList(PsqlSettings *pset, bool deep_tablelist, char info_type, { /* Display the information */ - fprintf(fout,"\nDatabase = %s\n", PQdb(pset->db)); - fprintf(fout," +------------------+----------------------------------+----------+\n"); - fprintf(fout," | Owner | Relation | Type |\n"); - fprintf(fout," +------------------+----------------------------------+----------+\n"); + fprintf(fout, "\nDatabase = %s\n", PQdb(pset->db)); + fprintf(fout, " +------------------+----------------------------------+----------+\n"); + fprintf(fout, " | Owner | Relation | Type |\n"); + fprintf(fout, " +------------------+----------------------------------+----------+\n"); /* next, print out the instances */ for (i = 0; i < PQntuples(res); i++) { - fprintf(fout," | %-16.16s", PQgetvalue(res, i, 0)); - fprintf(fout," | %-32.32s | ", PQgetvalue(res, i, 1)); + fprintf(fout, " | %-16.16s", PQgetvalue(res, i, 0)); + fprintf(fout, " | %-32.32s | ", PQgetvalue(res, i, 1)); rk = PQgetvalue(res, i, 2); rr = PQgetvalue(res, i, 3); if (strcmp(rk, "r") == 0) - fprintf(fout,"%-8.8s |", (rr[0] == 't') ? "view?" : "table"); + fprintf(fout, "%-8.8s |", (rr[0] == 't') ? "view?" : "table"); else if (strcmp(rk, "i") == 0) - fprintf(fout,"%-8.8s |", "index"); + fprintf(fout, "%-8.8s |", "index"); else - fprintf(fout,"%-8.8s |", "sequence"); - fprintf(fout,"\n"); + fprintf(fout, "%-8.8s |", "sequence"); + fprintf(fout, "\n"); } - fprintf(fout," +------------------+----------------------------------+----------+\n"); + fprintf(fout, " +------------------+----------------------------------+----------+\n"); PQclear(res); } if (usePipe) @@ -518,19 +521,19 @@ rightsList(PsqlSettings *pset) /* Display the information */ - fprintf(fout,"\nDatabase = %s\n", PQdb(pset->db)); - fprintf(fout," +------------------+----------------------------------------------------+\n"); - fprintf(fout," | Relation | Grant/Revoke Permissions |\n"); - fprintf(fout," +------------------+----------------------------------------------------+\n"); + fprintf(fout, "\nDatabase = %s\n", PQdb(pset->db)); + fprintf(fout, " +------------------+----------------------------------------------------+\n"); + fprintf(fout, " | Relation | Grant/Revoke Permissions |\n"); + fprintf(fout, " +------------------+----------------------------------------------------+\n"); /* next, print out the instances */ for (i = 0; i < PQntuples(res); i++) { - fprintf(fout," | %-16.16s", PQgetvalue(res, i, 0)); - fprintf(fout," | %-50.50s | ", PQgetvalue(res, i, 1)); - fprintf(fout,"\n"); + fprintf(fout, " | %-16.16s", PQgetvalue(res, i, 0)); + fprintf(fout, " | %-50.50s | ", PQgetvalue(res, i, 1)); + fprintf(fout, "\n"); } - fprintf(fout," +------------------+----------------------------------------------------+\n"); + fprintf(fout, " +------------------+----------------------------------------------------+\n"); PQclear(res); if (usePipe) { @@ -562,8 +565,10 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout) char *rnotnull; char *rhasdef; int i; - int attlen, atttypmod; - PGresult *res, *res2; + int attlen, + atttypmod; + PGresult *res, + *res2; int usePipe = 0; char *pagerenv; @@ -580,15 +585,18 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout) #ifdef TIOCGWINSZ } #endif - + /* Build the query */ - /* if the table name is surrounded by double-quotes, then don't convert case */ + /* + * if the table name is surrounded by double-quotes, then don't + * convert case + */ if (*table == '"') { table++; - if (*(table+strlen(table)-1) == '"') - *(table+strlen(table)-1) = '\0'; + if (*(table + strlen(table) - 1) == '"') + *(table + strlen(table) - 1) = '\0'; } else { @@ -628,21 +636,22 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout) else fout = stdout; } + /* - * Display the information + * Display the information */ - fprintf(fout,"\nTable = %s\n", table); - fprintf(fout,"+----------------------------------+----------------------------------+-------+\n"); - fprintf(fout,"| Field | Type | Length|\n"); - fprintf(fout,"+----------------------------------+----------------------------------+-------+\n"); + fprintf(fout, "\nTable = %s\n", table); + fprintf(fout, "+----------------------------------+----------------------------------+-------+\n"); + fprintf(fout, "| Field | Type | Length|\n"); + fprintf(fout, "+----------------------------------+----------------------------------+-------+\n"); /* next, print out the instances */ for (i = 0; i < PQntuples(res); i++) { - char type_str[33]; - - fprintf(fout,"| %-32.32s | ", PQgetvalue(res, i, 1)); + char type_str[33]; + + fprintf(fout, "| %-32.32s | ", PQgetvalue(res, i, 1)); rtype = PQgetvalue(res, i, 2); attlen = atoi(PQgetvalue(res, i, 3)); atttypmod = atoi(PQgetvalue(res, i, 4)); @@ -660,10 +669,10 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout) strncat(type_str, "[]", 32 - strlen(type_str)); type_str[32] = '\0'; } - + if (rnotnull[0] == 't') { - strncat(type_str," not null", 32 - strlen(type_str)); + strncat(type_str, " not null", 32 - strlen(type_str)); type_str[32] = '\0'; } if (rhasdef[0] == 't') @@ -679,27 +688,27 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout) strcat(descbuf, PQgetvalue(res, i, 0)); if (!(res2 = PSQLexec(pset, descbuf))) return -1; - strcat(type_str," default "); - strncat(type_str, PQgetvalue(res2, 0, 0), 32-strlen(type_str)); + strcat(type_str, " default "); + strncat(type_str, PQgetvalue(res2, 0, 0), 32 - strlen(type_str)); type_str[32] = '\0'; } - fprintf(fout,"%-32.32s |", type_str); + fprintf(fout, "%-32.32s |", type_str); if (strcmp(rtype, "text") == 0) - fprintf(fout,"%6s |", "var"); + fprintf(fout, "%6s |", "var"); else if (strcmp(rtype, "bpchar") == 0 || strcmp(rtype, "varchar") == 0) - fprintf(fout,"%6i |", atttypmod != -1 ? atttypmod - VARHDRSZ : 0); + fprintf(fout, "%6i |", atttypmod != -1 ? atttypmod - VARHDRSZ : 0); else { if (attlen > 0) - fprintf(fout,"%6i |", attlen); + fprintf(fout, "%6i |", attlen); else - fprintf(fout,"%6s |", "var"); + fprintf(fout, "%6s |", "var"); } - fprintf(fout,"\n"); + fprintf(fout, "\n"); } - fprintf(fout,"+----------------------------------+----------------------------------+-------+\n"); + fprintf(fout, "+----------------------------------+----------------------------------+-------+\n"); PQclear(res); if (usePipe) { @@ -745,18 +754,21 @@ objectDescription(PsqlSettings *pset, char *object, FILE *fout) #ifdef TIOCGWINSZ } #endif - + /* Build the query */ while (isspace(*object)) object++; - - /* if the object name is surrounded by double-quotes, then don't convert case */ + + /* + * if the object name is surrounded by double-quotes, then don't + * convert case + */ if (*object == '"') { object++; - if (*(object+strlen(object)-1) == '"') - *(object+strlen(object)-1) = '\0'; + if (*(object + strlen(object) - 1) == '"') + *(object + strlen(object) - 1) = '\0'; } else { @@ -766,14 +778,15 @@ objectDescription(PsqlSettings *pset, char *object, FILE *fout) } descbuf[0] = '\0'; - if (strchr(object,'.') != NULL) + if (strchr(object, '.') != NULL) { - char table[NAMEDATALEN],column[NAMEDATALEN]; + char table[NAMEDATALEN], + column[NAMEDATALEN]; - StrNCpy(table,object, - ((strchr(object,'.')-object+1) < NAMEDATALEN) ? - (strchr(object,'.')-object+1) : NAMEDATALEN); - StrNCpy(column,strchr(object,'.')+1,NAMEDATALEN); + StrNCpy(table, object, + ((strchr(object, '.') - object + 1) < NAMEDATALEN) ? + (strchr(object, '.') - object + 1) : NAMEDATALEN); + StrNCpy(column, strchr(object, '.') + 1, NAMEDATALEN); strcat(descbuf, "SELECT DISTINCT description "); strcat(descbuf, "FROM pg_class, pg_attribute, pg_description "); strcat(descbuf, "WHERE pg_class.relname = '"); @@ -783,18 +796,18 @@ objectDescription(PsqlSettings *pset, char *object, FILE *fout) strcat(descbuf, "pg_attribute.attname = '"); strcat(descbuf, column); strcat(descbuf, "' and "); - strcat(descbuf, " pg_attribute.oid = pg_description.objoid " ); + strcat(descbuf, " pg_attribute.oid = pg_description.objoid "); if (!(res = PSQLexec(pset, descbuf))) return -1; } else - { + { strcat(descbuf, "SELECT DISTINCT description "); strcat(descbuf, "FROM pg_class, pg_description "); strcat(descbuf, "WHERE pg_class.relname = '"); strcat(descbuf, object); - strcat(descbuf, "'" ); - strcat(descbuf, " and pg_class.oid = pg_description.objoid " ); + strcat(descbuf, "'"); + strcat(descbuf, " and pg_class.oid = pg_description.objoid "); if (!(res = PSQLexec(pset, descbuf))) return -1; else if (PQntuples(res) <= 0) @@ -806,7 +819,7 @@ objectDescription(PsqlSettings *pset, char *object, FILE *fout) strcat(descbuf, "WHERE pg_type.typname = '"); strcat(descbuf, object); strcat(descbuf, "' and "); - strcat(descbuf, " pg_type.oid = pg_description.objoid " ); + strcat(descbuf, " pg_type.oid = pg_description.objoid "); if (!(res = PSQLexec(pset, descbuf))) return -1; else if (PQntuples(res) <= 0) @@ -817,8 +830,8 @@ objectDescription(PsqlSettings *pset, char *object, FILE *fout) strcat(descbuf, "FROM pg_proc, pg_description "); strcat(descbuf, "WHERE pg_proc.proname = '"); strcat(descbuf, object); - strcat(descbuf, "'" ); - strcat(descbuf, " and pg_proc.oid = pg_description.objoid " ); + strcat(descbuf, "'"); + strcat(descbuf, " and pg_proc.oid = pg_description.objoid "); if (!(res = PSQLexec(pset, descbuf))) return -1; else if (PQntuples(res) <= 0) @@ -829,9 +842,9 @@ objectDescription(PsqlSettings *pset, char *object, FILE *fout) strcat(descbuf, "FROM pg_operator, pg_description "); strcat(descbuf, "WHERE pg_operator.oprname = '"); strcat(descbuf, object); - strcat(descbuf, "'" ); + strcat(descbuf, "'"); /* operator descriptions are attached to the proc */ - strcat(descbuf, " and RegprocToOid(pg_operator.oprcode) = pg_description.objoid " ); + strcat(descbuf, " and RegprocToOid(pg_operator.oprcode) = pg_description.objoid "); if (!(res = PSQLexec(pset, descbuf))) return -1; else if (PQntuples(res) <= 0) @@ -842,8 +855,8 @@ objectDescription(PsqlSettings *pset, char *object, FILE *fout) strcat(descbuf, "FROM pg_aggregate, pg_description "); strcat(descbuf, "WHERE pg_aggregate.aggname = '"); strcat(descbuf, object); - strcat(descbuf, "'" ); - strcat(descbuf, " and pg_aggregate.oid = pg_description.objoid " ); + strcat(descbuf, "'"); + strcat(descbuf, " and pg_aggregate.oid = pg_description.objoid "); if (!(res = PSQLexec(pset, descbuf))) return -1; } @@ -868,15 +881,16 @@ objectDescription(PsqlSettings *pset, char *object, FILE *fout) else fout = stdout; } + /* * * Display the information */ - fprintf(fout,"\nObject = %s\n", object); + fprintf(fout, "\nObject = %s\n", object); /* next, print out the instances */ for (i = 0; i < PQntuples(res); i++) - fprintf(fout,"%s\n",PQgetvalue(res, i, 0)); + fprintf(fout, "%s\n", PQgetvalue(res, i, 0)); PQclear(res); if (usePipe) @@ -1376,7 +1390,7 @@ do_connect(const char *new_dbname, dbparam = PQdb(olddb); pset->db = PQsetdb(PQhost(olddb), PQport(olddb), - NULL, NULL, dbparam); + NULL, NULL, dbparam); if (!pset->quiet) { if (!new_user) @@ -1625,6 +1639,7 @@ HandleSlashCmds(PsqlSettings *pset, int status = CMD_SKIP_LINE; char *optarg; bool success; + /* * Pointer inside the <cmd> string to the argument of the slash * command, assuming it is a one-character slash command. If it's not @@ -1736,36 +1751,40 @@ HandleSlashCmds(PsqlSettings *pset, } } break; - case 'd': /* \d describe database information */ + case 'd': /* \d describe database information */ if (strncmp(cmd, "da", 2) == 0) { - /* aggregates */ - SendQuery(&success, pset,"\ + /* aggregates */ + SendQuery(&success, pset, "\ SELECT a.aggname AS aggname, \ t.typname AS typname, \ obj_description(a.oid) as description \ FROM pg_aggregate a, pg_type t \ WHERE a.aggbasetype = t.oid \ ORDER BY aggname, typname;", - false, false, 0); - SendQuery(&success, pset,"\ + false, false, 0); + SendQuery(&success, pset, "\ SELECT a.aggname AS aggname, \ 'all types' as all_types, \ obj_description(a.oid) as description \ FROM pg_aggregate a \ WHERE a.aggbasetype = 0 \ ORDER BY aggname;", - false, false, 0); + false, false, 0); } else if (strncmp(cmd, "dd", 2) == 0) - /* descriptions */ - objectDescription(pset, optarg+1, NULL); + /* descriptions */ + objectDescription(pset, optarg + 1, NULL); else if (strncmp(cmd, "df", 2) == 0) { - /* functions/procedures */ - /* we skip in/out funcs by excluding functions that take - some arguments, but have no types defined for those arguments */ - SendQuery(&success, pset,"\ + /* functions/procedures */ + + /* + * we skip in/out funcs by excluding functions that take + * some arguments, but have no types defined for those + * arguments + */ + SendQuery(&success, pset, "\ SELECT t.typname as return_type, \ p.proname as function, \ substr(oid8types(p.proargtypes),1,20) as arguments, \ @@ -1775,8 +1794,8 @@ HandleSlashCmds(PsqlSettings *pset, (pronargs = 0 or oid8types(p.proargtypes) != '') and \ t.typname != 'bool' \ ORDER BY return_type, function;", - false, false, 0); - SendQuery(&success, pset,"\ + false, false, 0); + SendQuery(&success, pset, "\ SELECT t.typname as rtns, \ p.proname as function, \ oid8types(p.proargtypes) as arguments, \ @@ -1786,15 +1805,15 @@ HandleSlashCmds(PsqlSettings *pset, (pronargs = 0 or oid8types(p.proargtypes) != '') and \ t.typname = 'bool' \ ORDER BY rtns, function;", - false, false, 0); + false, false, 0); } else if (strncmp(cmd, "di", 2) == 0) - /* only indices */ + /* only indices */ tableList(pset, false, 'i', false); else if (strncmp(cmd, "do", 2) == 0) { - /* operators */ - SendQuery(&success, pset,"\ + /* operators */ + SendQuery(&success, pset, "\ SELECT o.oprname AS op, \ t0.typname AS result, \ t1.typname AS left_type, \ @@ -1809,8 +1828,8 @@ HandleSlashCmds(PsqlSettings *pset, o.oprleft = t1.oid AND \ o.oprright = t2.oid \ ORDER BY op, result, left_type, right_type;", - false, false, 0); - SendQuery(&success, pset,"\ + false, false, 0); + SendQuery(&success, pset, "\ SELECT o.oprname AS left_unary, \ t0.typname AS return_type, \ t1.typname AS operand, \ @@ -1821,8 +1840,8 @@ HandleSlashCmds(PsqlSettings *pset, o.oprkind = 'l' AND \ o.oprright = t1.oid \ ORDER BY left_unary, return_type, operand;", - false, false, 0); - SendQuery(&success, pset,"\ + false, false, 0); + SendQuery(&success, pset, "\ SELECT o.oprname AS right_unary, \ t0.typname AS return_type, \ t1.typname AS operand, \ @@ -1833,29 +1852,29 @@ HandleSlashCmds(PsqlSettings *pset, o.oprkind = 'r' AND \ o.oprleft = t1.oid \ ORDER BY right_unary, return_type, operand;", - false, false, 0); + false, false, 0); } else if (strncmp(cmd, "ds", 2) == 0) - /* only sequences */ + /* only sequences */ tableList(pset, false, 'S', false); else if (strncmp(cmd, "dS", 2) == 0) - /* system tables */ + /* system tables */ tableList(pset, false, 'b', true); else if (strncmp(cmd, "dt", 2) == 0) - /* only tables */ + /* only tables */ tableList(pset, false, 't', false); else if (strncmp(cmd, "dT", 2) == 0) - /* types */ - SendQuery(&success, pset,"\ + /* types */ + SendQuery(&success, pset, "\ SELECT typname AS type, \ obj_description(oid) as description \ FROM pg_type \ WHERE typrelid = 0 AND \ typname !~ '^_.*' \ ORDER BY type;", - false, false, 0); + false, false, 0); else if (!optarg) - /* show tables, sequences and indices */ + /* show tables, sequences and indices */ tableList(pset, false, 'b', false); else if (strcmp(optarg, "*") == 0) { /* show everything */ @@ -1863,11 +1882,11 @@ HandleSlashCmds(PsqlSettings *pset, tableList(pset, true, 'b', false); } else if (strncmp(cmd, "d ", 2) == 0) - /* describe the specified table */ + /* describe the specified table */ tableDesc(pset, optarg, NULL); else slashUsage(pset); - + break; case 'e': /* edit */ { @@ -2049,7 +2068,7 @@ HandleSlashCmds(PsqlSettings *pset, } free(cmd); return status; -} /* HandleSlashCmds() */ +} /* HandleSlashCmds() */ /* MainLoop() * Main processing loop for reading lines of input @@ -2087,17 +2106,20 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) /* We've reached the end of our command input. */ bool success; bool in_quote; - bool was_bslash; /* backslash */ + bool was_bslash; /* backslash */ int paren_level; char *query_start; if (query_alloced == false) { - if((query = malloc(MAX_QUERY_BUFFER)) == NULL) { - + if ((query = malloc(MAX_QUERY_BUFFER)) == NULL) + { + perror("Memory Allocation Failed"); - } else { + } + else + { query_alloced = true; } } @@ -2177,7 +2199,8 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) */ if (line == NULL || (!interactive && *line == '\0')) - { /* No more input. Time to quit, or \i done */ + { /* No more input. Time to quit, or \i + * done */ if (!pset->quiet) printf("EOF\n");/* Goes on prompt line */ eof = true; @@ -2247,7 +2270,7 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) } line[i] = hold_char; query_start = line + i; - break; /* handle command */ + break; /* handle command */ /* start an extended comment? */ } @@ -2260,15 +2283,16 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) if (was_bslash) was_bslash = false; - else if (i > 0 && line[i-1] == '\\') + else if (i > 0 && line[i - 1] == '\\') was_bslash = true; /* inside a quote? */ if (in_quote && (line[i] != '\'' || was_bslash)) { - /* do nothing */; + /* do nothing */ ; } - else if (xcomment != NULL) /*inside an extended comment?*/ + else if (xcomment != NULL) /* inside an extended + * comment? */ { if (line[i] == '*' && line[i + 1] == '/') { @@ -2276,28 +2300,28 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) i++; } } - /* possible backslash command? */ + /* possible backslash command? */ else if (line[i] == '/' && line[i + 1] == '*') { xcomment = line + i; i++; } - /* single-line comment? truncate line */ + /* single-line comment? truncate line */ else if ((line[i] == '-' && line[i + 1] == '-') || (line[i] == '/' && line[i + 1] == '/')) { /* print comment at top of query */ if (pset->singleStep) fprintf(stdout, "%s\n", line + i); - line[i] = '\0'; /* remove comment */ + line[i] = '\0'; /* remove comment */ break; } else if (line[i] == '\'') { in_quote ^= 1; } - /* semi-colon? then send query now */ + /* semi-colon? then send query now */ else if (!paren_level && line[i] == ';') { char hold_char = line[i + 1]; @@ -2317,10 +2341,10 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) successResult &= success; line[i + 1] = hold_char; query_start = line + i + 1; - /* sometimes, people do ';\g', don't execute twice */ + /* sometimes, people do ';\g', don't execute twice */ if (*query_start && /* keeps us from going off the end */ *query_start == '\\' && - *(query_start+1) == 'g') + *(query_start + 1) == 'g') query_start += 2; querySent = true; } @@ -2397,7 +2421,7 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) free(query); return successResult; -} /* MainLoop() */ +} /* MainLoop() */ int main(int argc, char **argv) @@ -2416,11 +2440,11 @@ main(int argc, char **argv) char *singleQuery = NULL; bool listDatabases = 0; - int successResult = 1; + int successResult = 1; bool singleSlashCmd = 0; - int c; + int c; - char *home = NULL; /* Used to store $HOME */ + char *home = NULL; /* Used to store $HOME */ MemSet(&settings, 0, sizeof settings); settings.opt.align = 1; @@ -2565,16 +2589,20 @@ main(int argc, char **argv) /* * 20.06.97 ACRM See if we've got a /etc/psqlrc or .psqlrc file */ - if(!access("/etc/psqlrc",R_OK)) + if (!access("/etc/psqlrc", R_OK)) HandleSlashCmds(&settings, "\\i /etc/psqlrc", ""); - if((home = getenv("HOME"))!=NULL) { - char *psqlrc = NULL, - *line = NULL; + if ((home = getenv("HOME")) != NULL) + { + char *psqlrc = NULL, + *line = NULL; - if((psqlrc = (char *)malloc(strlen(home) + 10))!=NULL) { + if ((psqlrc = (char *) malloc(strlen(home) + 10)) != NULL) + { sprintf(psqlrc, "%s/.psqlrc", home); - if(!access(psqlrc, R_OK)) { - if((line = (char *)malloc(strlen(psqlrc) + 5))!=NULL) { + if (!access(psqlrc, R_OK)) + { + if ((line = (char *) malloc(strlen(psqlrc) + 5)) != NULL) + { sprintf(line, "\\i %s", psqlrc); HandleSlashCmds(&settings, line, ""); free(line); diff --git a/src/bin/psql/stringutils.c b/src/bin/psql/stringutils.c index a868be9ddd..62d8fef574 100644 --- a/src/bin/psql/stringutils.c +++ b/src/bin/psql/stringutils.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/psql/stringutils.c,v 1.12 1997/09/08 02:33:45 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/stringutils.c,v 1.13 1998/02/26 04:39:13 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -53,7 +53,7 @@ leftTrim(char *s) #endif -char * +char * rightTrim(char *s) { char *sEnd; |