📄 startup.c
字号:
* Convert a version string into a number. */static intparse_version(const char *versionString){ int cnt; int vmaj, vmin, vrev; cnt = sscanf(versionString, "%d.%d.%d", &vmaj, &vmin, &vrev); if (cnt < 2) return -1; if (cnt == 2) vrev = 0; return (100 * vmaj + vmin) * 100 + vrev;}/* * Parse command line options */static voidparse_psql_options(int argc, char *argv[], struct adhoc_opts * options){ static struct option long_options[] = { {"echo-all", no_argument, NULL, 'a'}, {"no-align", no_argument, NULL, 'A'}, {"command", required_argument, NULL, 'c'}, {"dbname", required_argument, NULL, 'd'}, {"echo-queries", no_argument, NULL, 'e'}, {"echo-hidden", no_argument, NULL, 'E'}, {"file", required_argument, NULL, 'f'}, {"field-separator", required_argument, NULL, 'F'}, {"host", required_argument, NULL, 'h'}, {"html", no_argument, NULL, 'H'}, {"list", no_argument, NULL, 'l'}, {"log-file", required_argument, NULL, 'L'}, {"no-readline", no_argument, NULL, 'n'}, {"output", required_argument, NULL, 'o'}, {"port", required_argument, NULL, 'p'}, {"pset", required_argument, NULL, 'P'}, {"quiet", no_argument, NULL, 'q'}, {"record-separator", required_argument, NULL, 'R'}, {"single-step", no_argument, NULL, 's'}, {"single-line", no_argument, NULL, 'S'}, {"tuples-only", no_argument, NULL, 't'}, {"table-attr", required_argument, NULL, 'T'}, {"username", required_argument, NULL, 'U'}, {"set", required_argument, NULL, 'v'}, {"variable", required_argument, NULL, 'v'}, {"version", no_argument, NULL, 'V'}, {"password", no_argument, NULL, 'W'}, {"expanded", no_argument, NULL, 'x'}, {"no-psqlrc", no_argument, NULL, 'X'}, {"help", no_argument, NULL, '?'}, {NULL, 0, NULL, 0} }; int optindex; extern char *optarg; extern int optind; int c; bool used_old_u_option = false; memset(options, 0, sizeof *options); while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:uU:v:VWxX?", long_options, &optindex)) != -1) { switch (c) { case 'a': SetVariable(pset.vars, "ECHO", "all"); break; case 'A': pset.popt.topt.format = PRINT_UNALIGNED; break; case 'c': options->action_string = optarg; if (optarg[0] == '\\') { options->action = ACT_SINGLE_SLASH; options->action_string++; } else options->action = ACT_SINGLE_QUERY; break; case 'd': options->dbname = optarg; break; case 'e': SetVariable(pset.vars, "ECHO", "queries"); break; case 'E': SetVariableBool(pset.vars, "ECHO_HIDDEN"); break; case 'f': options->action = ACT_FILE; options->action_string = optarg; break; case 'F': pset.popt.topt.fieldSep = pg_strdup(optarg); break; case 'h': options->host = optarg; break; case 'H': pset.popt.topt.format = PRINT_HTML; break; case 'l': options->action = ACT_LIST_DB; break; case 'L': options->logfilename = optarg; break; case 'n': options->no_readline = true; break; case 'o': setQFout(optarg); break; case 'p': options->port = optarg; break; case 'P': { char *value; char *equal_loc; bool result; value = pg_strdup(optarg); equal_loc = strchr(value, '='); if (!equal_loc) result = do_pset(value, NULL, &pset.popt, true); else { *equal_loc = '\0'; result = do_pset(value, equal_loc + 1, &pset.popt, true); } if (!result) { fprintf(stderr, _("%s: could not set printing parameter \"%s\"\n"), pset.progname, value); exit(EXIT_FAILURE); } free(value); break; } case 'q': SetVariableBool(pset.vars, "QUIET"); break; case 'R': pset.popt.topt.recordSep = pg_strdup(optarg); break; case 's': SetVariableBool(pset.vars, "SINGLESTEP"); break; case 'S': SetVariableBool(pset.vars, "SINGLELINE"); break; case 't': pset.popt.topt.tuples_only = true; break; case 'T': pset.popt.topt.tableAttr = pg_strdup(optarg); break; case 'u': pset.getPassword = true; options->username = "\001"; /* hopefully nobody has that * username */ /* this option is out */ used_old_u_option = true; break; case 'U': options->username = optarg; break; case 'v': { char *value; char *equal_loc; value = pg_strdup(optarg); equal_loc = strchr(value, '='); if (!equal_loc) { if (!DeleteVariable(pset.vars, value)) { fprintf(stderr, _("%s: could not delete variable \"%s\"\n"), pset.progname, value); exit(EXIT_FAILURE); } } else { *equal_loc = '\0'; if (!SetVariable(pset.vars, value, equal_loc + 1)) { fprintf(stderr, _("%s: could not set variable \"%s\"\n"), pset.progname, value); exit(EXIT_FAILURE); } } free(value); break; } case 'V': showVersion(); exit(EXIT_SUCCESS); case 'W': pset.getPassword = true; break; case 'x': pset.popt.topt.expanded = true; break; case 'X': options->no_psqlrc = true; break; case '?': /* Actual help option given */ if (strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0) { usage(); exit(EXIT_SUCCESS); } /* unknown option reported by getopt */ else { fprintf(stderr, _("Try \"%s --help\" for more information.\n"), pset.progname); exit(EXIT_FAILURE); } break; default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), pset.progname); exit(EXIT_FAILURE); break; } } /* * if we still have arguments, use it as the database name and username */ while (argc - optind >= 1) { if (!options->dbname) options->dbname = argv[optind]; else if (!options->username) options->username = argv[optind]; else if (!QUIET()) fprintf(stderr, _("%s: warning: extra command-line argument \"%s\" ignored\n"), pset.progname, argv[optind]); optind++; } if (used_old_u_option && !QUIET()) fprintf(stderr, _("%s: Warning: The -u option is deprecated. Use -U.\n"), pset.progname);}/* * Load .psqlrc file, if found. */static voidprocess_psqlrc(char *argv0){ char home[MAXPGPATH]; char rc_file[MAXPGPATH]; char my_exec_path[MAXPGPATH]; char etc_path[MAXPGPATH]; find_my_exec(argv0, my_exec_path); get_etc_path(my_exec_path, etc_path); snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC); process_psqlrc_file(rc_file); if (get_home_path(home)) { snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC); process_psqlrc_file(rc_file); }}static voidprocess_psqlrc_file(char *filename){ char *psqlrc;#if defined(WIN32) && (!defined(__MINGW32__))#define R_OK 4#endif psqlrc = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1); sprintf(psqlrc, "%s-%s", filename, PG_VERSION); if (access(psqlrc, R_OK) == 0) (void) process_file(psqlrc); else if (access(filename, R_OK) == 0) (void) process_file(filename); free(psqlrc);}/* showVersion * * This output format is intended to match GNU standards. */static voidshowVersion(void){ puts("psql (PostgreSQL) " PG_VERSION);#if defined(USE_READLINE) puts(_("contains support for command-line editing"));#endif}/* * printSSLInfo * * Prints information about the current SSL connection, if SSL is in use */#ifdef USE_SSLstatic voidprintSSLInfo(void){ int sslbits = -1; SSL *ssl; ssl = PQgetssl(pset.db); if (!ssl) return; /* no SSL */ SSL_get_cipher_bits(ssl, &sslbits); printf(_("SSL connection (cipher: %s, bits: %i)\n\n"), SSL_get_cipher(ssl), sslbits);}#endif/* * checkWin32Codepage * * Prints a warning when win32 console codepage differs from Windows codepage */#ifdef WIN32static voidcheckWin32Codepage(void){ unsigned int wincp, concp; wincp = GetACP(); concp = GetConsoleCP(); if (wincp != concp) { printf(_("Warning: Console code page (%u) differs from Windows code page (%u)\n" " 8-bit characters may not work correctly. See psql reference\n" " page \"Notes for Windows users\" for details.\n\n"), concp, wincp); }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -