* process_file * * Reads commands from filename and passes them to the main processing loop. * Handler for \i and \ir, but can be used for other things as well. Returns * MainLoop() error code. * * If use_relative_path is true and filename is not an absolute path, then open * the file from where the currently processed file (if any) is located. */
| 4006 | * the file from where the currently processed file (if any) is located. |
| 4007 | */ |
| 4008 | int |
| 4009 | process_file(char *filename, bool use_relative_path) |
| 4010 | { |
| 4011 | FILE *fd; |
| 4012 | int result; |
| 4013 | char *oldfilename; |
| 4014 | char relpath[MAXPGPATH]; |
| 4015 | |
| 4016 | if (!filename) |
| 4017 | { |
| 4018 | fd = stdin; |
| 4019 | filename = NULL; |
| 4020 | } |
| 4021 | else if (strcmp(filename, "-") != 0) |
| 4022 | { |
| 4023 | canonicalize_path(filename); |
| 4024 | |
| 4025 | /* |
| 4026 | * If we were asked to resolve the pathname relative to the location |
| 4027 | * of the currently executing script, and there is one, and this is a |
| 4028 | * relative pathname, then prepend all but the last pathname component |
| 4029 | * of the current script to this pathname. |
| 4030 | */ |
| 4031 | if (use_relative_path && pset.inputfile && |
| 4032 | !is_absolute_path(filename) && !has_drive_prefix(filename)) |
| 4033 | { |
| 4034 | strlcpy(relpath, pset.inputfile, sizeof(relpath)); |
| 4035 | get_parent_directory(relpath); |
| 4036 | join_path_components(relpath, relpath, filename); |
| 4037 | canonicalize_path(relpath); |
| 4038 | |
| 4039 | filename = relpath; |
| 4040 | } |
| 4041 | |
| 4042 | fd = fopen(filename, PG_BINARY_R); |
| 4043 | |
| 4044 | if (!fd) |
| 4045 | { |
| 4046 | pg_log_error("%s: %m", filename); |
| 4047 | return EXIT_FAILURE; |
| 4048 | } |
| 4049 | } |
| 4050 | else |
| 4051 | { |
| 4052 | fd = stdin; |
| 4053 | filename = "<stdin>"; /* for future error messages */ |
| 4054 | } |
| 4055 | |
| 4056 | oldfilename = pset.inputfile; |
| 4057 | pset.inputfile = ignore_log_file ? NULL : filename; |
| 4058 | |
| 4059 | pg_logging_config(pset.inputfile ? 0 : PG_LOG_FLAG_TERSE); |
| 4060 | |
| 4061 | result = MainLoop(fd); |
| 4062 | |
| 4063 | if (fd != stdin) |
| 4064 | fclose(fd); |
| 4065 |
no test coverage detected