* helper for do_edit(): actually invoke the editor * * Returns true on success, false if we failed to invoke the editor or * it returned nonzero status. (An error message is printed for failed- * to-invoke cases, but not if the editor returns nonzero status.) */
| 3730 | * to-invoke cases, but not if the editor returns nonzero status.) |
| 3731 | */ |
| 3732 | static bool |
| 3733 | editFile(const char *fname, int lineno) |
| 3734 | { |
| 3735 | const char *editorName; |
| 3736 | const char *editor_lineno_arg = NULL; |
| 3737 | char *sys; |
| 3738 | int result; |
| 3739 | |
| 3740 | Assert(fname != NULL); |
| 3741 | |
| 3742 | /* Find an editor to use */ |
| 3743 | editorName = getenv("PSQL_EDITOR"); |
| 3744 | if (!editorName) |
| 3745 | editorName = getenv("EDITOR"); |
| 3746 | if (!editorName) |
| 3747 | editorName = getenv("VISUAL"); |
| 3748 | if (!editorName) |
| 3749 | editorName = DEFAULT_EDITOR; |
| 3750 | |
| 3751 | /* Get line number argument, if we need it. */ |
| 3752 | if (lineno > 0) |
| 3753 | { |
| 3754 | editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG"); |
| 3755 | #ifdef DEFAULT_EDITOR_LINENUMBER_ARG |
| 3756 | if (!editor_lineno_arg) |
| 3757 | editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG; |
| 3758 | #endif |
| 3759 | if (!editor_lineno_arg) |
| 3760 | { |
| 3761 | pg_log_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number"); |
| 3762 | return false; |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | /* |
| 3767 | * On Unix the EDITOR value should *not* be quoted, since it might include |
| 3768 | * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it |
| 3769 | * if necessary. But this policy is not very workable on Windows, due to |
| 3770 | * severe brain damage in their command shell plus the fact that standard |
| 3771 | * program paths include spaces. |
| 3772 | */ |
| 3773 | #ifndef WIN32 |
| 3774 | if (lineno > 0) |
| 3775 | sys = psprintf("exec %s %s%d '%s'", |
| 3776 | editorName, editor_lineno_arg, lineno, fname); |
| 3777 | else |
| 3778 | sys = psprintf("exec %s '%s'", |
| 3779 | editorName, fname); |
| 3780 | #else |
| 3781 | if (lineno > 0) |
| 3782 | sys = psprintf("\"%s\" %s%d \"%s\"", |
| 3783 | editorName, editor_lineno_arg, lineno, fname); |
| 3784 | else |
| 3785 | sys = psprintf("\"%s\" \"%s\"", |
| 3786 | editorName, fname); |
| 3787 | #endif |
| 3788 | result = system(sys); |
| 3789 | if (result == -1) |