* \e or \edit -- edit the current query buffer, or edit a file and * make it the query buffer */
| 1001 | * make it the query buffer |
| 1002 | */ |
| 1003 | static backslashResult |
| 1004 | exec_command_edit(PsqlScanState scan_state, bool active_branch, |
| 1005 | PQExpBuffer query_buf, PQExpBuffer previous_buf) |
| 1006 | { |
| 1007 | backslashResult status = PSQL_CMD_SKIP_LINE; |
| 1008 | |
| 1009 | if (active_branch) |
| 1010 | { |
| 1011 | if (!query_buf) |
| 1012 | { |
| 1013 | pg_log_error("no query buffer"); |
| 1014 | status = PSQL_CMD_ERROR; |
| 1015 | } |
| 1016 | else |
| 1017 | { |
| 1018 | char *fname; |
| 1019 | char *ln = NULL; |
| 1020 | int lineno = -1; |
| 1021 | |
| 1022 | fname = psql_scan_slash_option(scan_state, |
| 1023 | OT_NORMAL, NULL, true); |
| 1024 | if (fname) |
| 1025 | { |
| 1026 | /* try to get separate lineno arg */ |
| 1027 | ln = psql_scan_slash_option(scan_state, |
| 1028 | OT_NORMAL, NULL, true); |
| 1029 | if (ln == NULL) |
| 1030 | { |
| 1031 | /* only one arg; maybe it is lineno not fname */ |
| 1032 | if (fname[0] && |
| 1033 | strspn(fname, "0123456789") == strlen(fname)) |
| 1034 | { |
| 1035 | /* all digits, so assume it is lineno */ |
| 1036 | ln = fname; |
| 1037 | fname = NULL; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | if (ln) |
| 1042 | { |
| 1043 | lineno = atoi(ln); |
| 1044 | if (lineno < 1) |
| 1045 | { |
| 1046 | pg_log_error("invalid line number: %s", ln); |
| 1047 | status = PSQL_CMD_ERROR; |
| 1048 | } |
| 1049 | } |
| 1050 | if (status != PSQL_CMD_ERROR) |
| 1051 | { |
| 1052 | bool discard_on_quit; |
| 1053 | |
| 1054 | expand_tilde(&fname); |
| 1055 | if (fname) |
| 1056 | { |
| 1057 | canonicalize_path(fname); |
| 1058 | /* Always clear buffer if the file isn't modified */ |
| 1059 | discard_on_quit = true; |
| 1060 | } |
no test coverage detected