*** a/doc/src/sgml/ref/psql-ref.sgml --- b/doc/src/sgml/ref/psql-ref.sgml *************** *** 1731,1736 **** hello 10 --- 1731,1749 ---- + \ib filename [ quote_string ] + + + The \ib command appends content of file filename + to current query buffer. If parameter quote_string + is not set, no quotation is used. If it is set, content of file will be + quoted by quote_string enclosed in $. + + + + + + \ir filename *** a/src/bin/psql/command.c --- b/src/bin/psql/command.c *************** *** 59,64 **** static backslashResult exec_command(const char *cmd, --- 59,65 ---- PQExpBuffer query_buf); static bool do_edit(const char *filename_arg, PQExpBuffer query_buf, int lineno, bool *edited); + static bool do_append(const char *filename_arg, const char *quote_string, PQExpBuffer query_buf); static bool do_connect(char *dbname, char *user, char *host, char *port); static bool do_shell(const char *command); static bool do_watch(PQExpBuffer query_buf, long sleep); *************** *** 798,806 **** exec_command(const char *cmd, } ! /* \i and \ir include files */ else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ! || strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0) { char *fname = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true); --- 799,808 ---- } ! /* \i, \ib and \ir include files */ else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ! || strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0 ! || strcmp(cmd, "ib") == 0 || strcmp(cmd, "include_buffer") == 0) { char *fname = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true); *************** *** 812,823 **** exec_command(const char *cmd, } else { ! bool include_relative; - include_relative = (strcmp(cmd, "ir") == 0 - || strcmp(cmd, "include_relative") == 0); - expand_tilde(&fname); - success = (process_file(fname, false, include_relative) == EXIT_SUCCESS); free(fname); } } --- 814,845 ---- } else { ! bool include_buffer; ! ! include_buffer = (strcmp(cmd, "ib") == 0 ! || strcmp(cmd, "include_buffer") == 0); ! ! ! if (include_buffer) ! { ! char *quote_string = psql_scan_slash_option(scan_state, ! OT_NORMAL, NULL, true); ! expand_tilde(&fname); ! success = !do_append(fname, quote_string, query_buf); ! if (success) { ! status = PSQL_CMD_NEWEDIT; ! } else { ! status = PSQL_CMD_ERROR; ! } ! } else { ! bool include_relative; ! ! include_relative = (strcmp(cmd, "ir") == 0 ! || strcmp(cmd, "include_relative") == 0); ! expand_tilde(&fname); ! success = (process_file(fname, false, include_relative) == EXIT_SUCCESS); ! } free(fname); } } *************** *** 2099,2104 **** do_edit(const char *filename_arg, PQExpBuffer query_buf, --- 2121,2175 ---- } + /* + * do_append + * + * Read content from file and append it to query_buffer. + */ + static bool + do_append(const char *fname, const char *quote_string, PQExpBuffer query_buf) + { + FILE *stream = NULL; + bool error = false; + stream = fopen(fname, PG_BINARY_R); + if (!stream) + { + psql_error("%s: %s\n", fname, strerror(errno)); + error = true; + } + else + { + /* read file back into query_buf */ + char line[1024]; + + /* insert newline to separate buffer from file input */ + appendPQExpBufferStr(query_buf, "\n"); + /* if quote_string exists add it before file content */ + if (quote_string != NULL) { + appendPQExpBufferStr(query_buf, "$"); + appendPQExpBufferStr(query_buf, quote_string); + appendPQExpBufferStr(query_buf, "$\n"); + } + while (fgets(line, sizeof(line), stream) != NULL) + appendPQExpBufferStr(query_buf, line); + + if (ferror(stream)) + { + psql_error("%s: %s\n", fname, strerror(errno)); + error = true; + } + + fclose(stream); + /* if quote_string exists add it after file content */ + if (quote_string != NULL) { + appendPQExpBufferStr(query_buf, "$"); + appendPQExpBufferStr(query_buf, quote_string); + appendPQExpBufferStr(query_buf, "$"); + } + } + return error; + } + /* * process_file *** a/src/bin/psql/help.c --- b/src/bin/psql/help.c *************** *** 187,192 **** slashUsage(unsigned short int pager) --- 187,193 ---- fprintf(output, _(" \\s [FILE] display history or save it to file\n")); #endif fprintf(output, _(" \\w FILE write query buffer to file\n")); + fprintf(output, _(" \\ib FILE [QUOTE_STR] append file to query buffer\n")); fprintf(output, "\n"); fprintf(output, _("Input/Output\n"));