From d65bea26a867e3bbd053bf87b985b0e113256414 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 24 Mar 2016 18:27:28 -0400 Subject: [PATCH] Move psql's print.c and mbprint.c into src/fe_utils. Just turning the crank ... --- src/bin/psql/Makefile | 4 +-- src/bin/psql/command.c | 2 +- src/bin/psql/command.h | 2 +- src/bin/psql/common.c | 2 +- src/bin/psql/common.h | 6 +--- src/bin/psql/describe.c | 4 +-- src/bin/psql/mbprint.h | 18 ---------- src/bin/psql/nls.mk | 3 +- src/bin/psql/settings.h | 2 +- src/bin/psql/startup.c | 2 +- src/bin/scripts/.gitignore | 3 -- src/bin/scripts/Makefile | 12 +++---- src/bin/scripts/createlang.c | 2 +- src/bin/scripts/droplang.c | 2 +- src/bin/scripts/nls.mk | 1 + src/fe_utils/Makefile | 2 +- src/{bin/psql => fe_utils}/mbprint.c | 31 +++++++++++------- src/{bin/psql => fe_utils}/print.c | 38 ++++++++++++---------- src/include/fe_utils/mbprint.h | 29 +++++++++++++++++ src/{bin/psql => include/fe_utils}/print.h | 28 ++++++++++------ src/tools/msvc/Mkvcbuild.pm | 9 ++--- 21 files changed, 108 insertions(+), 94 deletions(-) delete mode 100644 src/bin/psql/mbprint.h rename src/{bin/psql => fe_utils}/mbprint.c (93%) rename src/{bin/psql => fe_utils}/print.c (98%) create mode 100644 src/include/fe_utils/mbprint.h rename src/{bin/psql => include/fe_utils}/print.h (93%) diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile index 251d6389900..4e68e3ebdca 100644 --- a/src/bin/psql/Makefile +++ b/src/bin/psql/Makefile @@ -22,8 +22,8 @@ override CPPFLAGS := -I. -I$(srcdir) -I$(libpq_srcdir) $(CPPFLAGS) LDFLAGS += -L$(top_builddir)/src/fe_utils -lpgfeutils OBJS= command.o common.o help.o input.o stringutils.o mainloop.o copy.o \ - startup.o prompt.o variables.o large_obj.o print.o describe.o \ - tab-complete.o mbprint.o \ + startup.o prompt.o variables.o large_obj.o describe.o \ + tab-complete.o \ sql_help.o psqlscan.o psqlscanslash.o \ $(WIN32RES) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index e5ec8af11c5..50dc43bf61a 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -44,7 +44,7 @@ #include "input.h" #include "large_obj.h" #include "mainloop.h" -#include "print.h" +#include "fe_utils/print.h" #include "psqlscanslash.h" #include "settings.h" #include "variables.h" diff --git a/src/bin/psql/command.h b/src/bin/psql/command.h index e2b6ea8ed03..4f0140fd563 100644 --- a/src/bin/psql/command.h +++ b/src/bin/psql/command.h @@ -8,7 +8,7 @@ #ifndef COMMAND_H #define COMMAND_H -#include "print.h" +#include "fe_utils/print.h" #include "psqlscan.h" diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 2b67a439da7..892058e9ac8 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -23,7 +23,7 @@ #include "settings.h" #include "command.h" #include "copy.h" -#include "mbprint.h" +#include "fe_utils/mbprint.h" static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec); diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index ba4c5699b3d..bdcb58f2f38 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -11,7 +11,7 @@ #include #include "libpq-fe.h" -#include "print.h" +#include "fe_utils/print.h" #define atooid(x) ((Oid) strtoul((x), NULL, 10)) @@ -28,10 +28,6 @@ extern volatile bool sigint_interrupt_enabled; extern sigjmp_buf sigint_interrupt_jmp; -extern volatile bool cancel_pressed; - -/* Note: cancel_pressed is defined in print.c, see that file for reasons */ - extern void setup_cancel_handler(void); extern void SetCancelConn(void); diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index b824d4e5492..7b2f4e64ef1 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -19,8 +19,8 @@ #include "common.h" #include "describe.h" -#include "mbprint.h" -#include "print.h" +#include "fe_utils/mbprint.h" +#include "fe_utils/print.h" #include "settings.h" #include "variables.h" diff --git a/src/bin/psql/mbprint.h b/src/bin/psql/mbprint.h deleted file mode 100644 index 01064d31007..00000000000 --- a/src/bin/psql/mbprint.h +++ /dev/null @@ -1,18 +0,0 @@ -/* src/bin/psql/mbprint.h */ -#ifndef MBPRINT_H -#define MBPRINT_H - - -struct lineptr -{ - unsigned char *ptr; - int width; -}; - -extern unsigned char *mbvalidate(unsigned char *pwcs, int encoding); -extern int pg_wcswidth(const char *pwcs, size_t len, int encoding); -extern void pg_wcsformat(const unsigned char *pwcs, size_t len, int encoding, struct lineptr * lines, int count); -extern void pg_wcssize(const unsigned char *pwcs, size_t len, int encoding, - int *width, int *height, int *format_size); - -#endif /* MBPRINT_H */ diff --git a/src/bin/psql/nls.mk b/src/bin/psql/nls.mk index 3746eeaeab2..b9a7992a4fa 100644 --- a/src/bin/psql/nls.mk +++ b/src/bin/psql/nls.mk @@ -2,9 +2,10 @@ CATALOG_NAME = psql AVAIL_LANGUAGES = cs de es fr it ja pl pt_BR ru zh_CN zh_TW GETTEXT_FILES = command.c common.c copy.c help.c input.c large_obj.c \ - mainloop.c print.c psqlscan.c psqlscanslash.c startup.c \ + mainloop.c psqlscan.c psqlscanslash.c startup.c \ describe.c sql_help.h sql_help.c \ tab-complete.c variables.c \ + ../../fe_utils/print.c \ ../../common/exec.c ../../common/fe_memutils.c ../../common/username.c \ ../../common/wait_error.c GETTEXT_TRIGGERS = N_ psql_error simple_prompt diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 20a6470e9d3..159a7a5579a 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -10,7 +10,7 @@ #include "variables.h" -#include "print.h" +#include "fe_utils/print.h" #define DEFAULT_FIELD_SEP "|" #define DEFAULT_RECORD_SEP "\n" diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 4bb3fdc595a..07e94d064a3 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -26,7 +26,7 @@ #include "help.h" #include "input.h" #include "mainloop.h" -#include "print.h" +#include "fe_utils/print.h" #include "settings.h" diff --git a/src/bin/scripts/.gitignore b/src/bin/scripts/.gitignore index 784f25b93e7..40998d9670d 100644 --- a/src/bin/scripts/.gitignore +++ b/src/bin/scripts/.gitignore @@ -9,7 +9,4 @@ /vacuumdb /pg_isready -/mbprint.c -/print.c - /tmp_check/ diff --git a/src/bin/scripts/Makefile b/src/bin/scripts/Makefile index 5e47e13a78a..4e342ef8887 100644 --- a/src/bin/scripts/Makefile +++ b/src/bin/scripts/Makefile @@ -18,7 +18,7 @@ include $(top_builddir)/src/Makefile.global PROGRAMS = createdb createlang createuser dropdb droplang dropuser clusterdb vacuumdb reindexdb pg_isready -override CPPFLAGS := -I$(top_srcdir)/src/bin/psql -I$(libpq_srcdir) $(CPPFLAGS) +override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) LDFLAGS += -L$(top_builddir)/src/fe_utils -lpgfeutils all: $(PROGRAMS) @@ -27,19 +27,16 @@ all: $(PROGRAMS) $(CC) $(CFLAGS) $^ $(libpq_pgport) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X) createdb: createdb.o common.o | submake-libpq submake-libpgport submake-libpgfeutils -createlang: createlang.o common.o print.o mbprint.o | submake-libpq submake-libpgport +createlang: createlang.o common.o | submake-libpq submake-libpgport submake-libpgfeutils createuser: createuser.o common.o | submake-libpq submake-libpgport submake-libpgfeutils dropdb: dropdb.o common.o | submake-libpq submake-libpgport submake-libpgfeutils -droplang: droplang.o common.o print.o mbprint.o | submake-libpq submake-libpgport +droplang: droplang.o common.o | submake-libpq submake-libpgport submake-libpgfeutils dropuser: dropuser.o common.o | submake-libpq submake-libpgport submake-libpgfeutils clusterdb: clusterdb.o common.o | submake-libpq submake-libpgport submake-libpgfeutils vacuumdb: vacuumdb.o common.o | submake-libpq submake-libpgport submake-libpgfeutils reindexdb: reindexdb.o common.o | submake-libpq submake-libpgport submake-libpgfeutils pg_isready: pg_isready.o common.o | submake-libpq submake-libpgport -print.c mbprint.c : % : $(top_srcdir)/src/bin/psql/% - rm -f $@ && $(LN_S) $< . - install: all installdirs $(INSTALL_PROGRAM) createdb$(X) '$(DESTDIR)$(bindir)'/createdb$(X) $(INSTALL_PROGRAM) dropdb$(X) '$(DESTDIR)$(bindir)'/dropdb$(X) @@ -60,8 +57,7 @@ uninstall: clean distclean maintainer-clean: rm -f $(addsuffix $(X), $(PROGRAMS)) $(addsuffix .o, $(PROGRAMS)) - rm -f common.o print.o mbprint.o $(WIN32RES) - rm -f print.c mbprint.c + rm -f common.o $(WIN32RES) rm -rf tmp_check check: diff --git a/src/bin/scripts/createlang.c b/src/bin/scripts/createlang.c index ad298c97f5a..f4eb0797f00 100644 --- a/src/bin/scripts/createlang.c +++ b/src/bin/scripts/createlang.c @@ -12,7 +12,7 @@ #include "postgres_fe.h" #include "common.h" -#include "print.h" +#include "fe_utils/print.h" static void help(const char *progname); diff --git a/src/bin/scripts/droplang.c b/src/bin/scripts/droplang.c index 9bbbd9a20a7..8b23fe13792 100644 --- a/src/bin/scripts/droplang.c +++ b/src/bin/scripts/droplang.c @@ -12,7 +12,7 @@ #include "postgres_fe.h" #include "common.h" -#include "print.h" +#include "fe_utils/print.h" #define atooid(x) ((Oid) strtoul((x), NULL, 10)) diff --git a/src/bin/scripts/nls.mk b/src/bin/scripts/nls.mk index 36299d8267a..45c1a7be1b9 100644 --- a/src/bin/scripts/nls.mk +++ b/src/bin/scripts/nls.mk @@ -6,5 +6,6 @@ GETTEXT_FILES = createdb.c createlang.c createuser.c \ clusterdb.c vacuumdb.c reindexdb.c \ pg_isready.c \ common.c \ + ../../fe_utils/print.c \ ../../common/fe_memutils.c ../../common/username.c GETTEXT_TRIGGERS = simple_prompt yesno_prompt diff --git a/src/fe_utils/Makefile b/src/fe_utils/Makefile index f6a52dfdb41..3223f1f26c7 100644 --- a/src/fe_utils/Makefile +++ b/src/fe_utils/Makefile @@ -17,7 +17,7 @@ include $(top_builddir)/src/Makefile.global override CPPFLAGS := -DFRONTEND -I$(libpq_srcdir) $(CPPFLAGS) -OBJS = simple_list.o string_utils.o +OBJS = mbprint.o print.o simple_list.o string_utils.o all: libpgfeutils.a diff --git a/src/bin/psql/mbprint.c b/src/fe_utils/mbprint.c similarity index 93% rename from src/bin/psql/mbprint.c rename to src/fe_utils/mbprint.c index 0776545ad04..97ba6927249 100644 --- a/src/bin/psql/mbprint.c +++ b/src/fe_utils/mbprint.c @@ -1,20 +1,21 @@ -/* - * psql - the PostgreSQL interactive terminal +/*------------------------------------------------------------------------- * - * Copyright (c) 2000-2016, PostgreSQL Global Development Group + * Multibyte character printing support for frontend code * - * src/bin/psql/mbprint.c * - * XXX this file does not really belong in psql/. Perhaps move to libpq? - * It also seems that the mbvalidate function is redundant with existing - * functionality. + * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/fe_utils/mbprint.c + * + *------------------------------------------------------------------------- */ - #include "postgres_fe.h" -#include "mbprint.h" -#ifndef PGSCRIPTS -#include "settings.h" -#endif + +#include "fe_utils/mbprint.h" + +#include "libpq-fe.h" + /* * To avoid version-skew problems, this file must not use declarations @@ -381,6 +382,12 @@ pg_wcsformat(const unsigned char *pwcs, size_t len, int encoding, (lines + 1)->ptr = NULL; /* terminate line array */ } + +/* + * Encoding validation: delete any unvalidatable characters from the string + * + * This seems redundant with existing functionality elsewhere? + */ unsigned char * mbvalidate(unsigned char *pwcs, int encoding) { diff --git a/src/bin/psql/print.c b/src/fe_utils/print.c similarity index 98% rename from src/bin/psql/print.c rename to src/fe_utils/print.c index f25a66eb36f..30efd3fdc62 100644 --- a/src/bin/psql/print.c +++ b/src/fe_utils/print.c @@ -1,13 +1,24 @@ -/* - * psql - the PostgreSQL interactive terminal +/*------------------------------------------------------------------------- * - * Copyright (c) 2000-2016, PostgreSQL Global Development Group + * Query-result printing support for frontend code * - * src/bin/psql/print.c + * This file used to be part of psql, but now it's separated out to allow + * other frontend programs to use it. Because the printing code needs + * access to the cancel_pressed flag as well as SIGPIPE trapping and + * pager open/close functions, all that stuff came with it. + * + * + * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/fe_utils/print.c + * + *------------------------------------------------------------------------- */ #include "postgres_fe.h" #include +#include #include #include #include @@ -20,32 +31,23 @@ #include #endif -#include +#include "fe_utils/print.h" #include "catalog/pg_type.h" +#include "fe_utils/mbprint.h" -#include "common.h" -#include "mbprint.h" -#include "print.h" /* - * We define the cancel_pressed flag in this file, rather than common.c where - * it naturally belongs, because this file is also used by non-psql programs - * (see the bin/scripts/ directory). In those programs cancel_pressed will - * never become set and will have no effect. + * If the calling program doesn't have any mechanism for setting + * cancel_pressed, it will have no effect. * * Note: print.c's general strategy for when to check cancel_pressed is to do * so at completion of each row of output. */ volatile bool cancel_pressed = false; -/* - * Likewise, the sigpipe_trap and pager open/close functions are here rather - * than in common.c so that this file can be used by non-psql programs. - */ static bool always_ignore_sigpipe = false; - /* info for locale-aware numeric formatting; set up by setDecimalLocale() */ static char *decimal_point; static int groupdigits; @@ -139,7 +141,7 @@ typedef struct unicodeStyleFormat bool wrap_right_border; } unicodeStyleFormat; -const unicodeStyleFormat unicode_style = { +static const unicodeStyleFormat unicode_style = { { { /* ─ */ diff --git a/src/include/fe_utils/mbprint.h b/src/include/fe_utils/mbprint.h new file mode 100644 index 00000000000..e37eb5cb336 --- /dev/null +++ b/src/include/fe_utils/mbprint.h @@ -0,0 +1,29 @@ +/*------------------------------------------------------------------------- + * + * Multibyte character printing support for frontend code + * + * + * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/fe_utils/mbprint.h + * + *------------------------------------------------------------------------- + */ +#ifndef MBPRINT_H +#define MBPRINT_H + +struct lineptr +{ + unsigned char *ptr; + int width; +}; + +extern unsigned char *mbvalidate(unsigned char *pwcs, int encoding); +extern int pg_wcswidth(const char *pwcs, size_t len, int encoding); +extern void pg_wcsformat(const unsigned char *pwcs, size_t len, int encoding, + struct lineptr * lines, int count); +extern void pg_wcssize(const unsigned char *pwcs, size_t len, int encoding, + int *width, int *height, int *format_size); + +#endif /* MBPRINT_H */ diff --git a/src/bin/psql/print.h b/src/include/fe_utils/print.h similarity index 93% rename from src/bin/psql/print.h rename to src/include/fe_utils/print.h index 9033c4bce38..ff902370f32 100644 --- a/src/bin/psql/print.h +++ b/src/include/fe_utils/print.h @@ -1,9 +1,14 @@ -/* - * psql - the PostgreSQL interactive terminal +/*------------------------------------------------------------------------- * - * Copyright (c) 2000-2016, PostgreSQL Global Development Group + * Query-result printing support for frontend code * - * src/bin/psql/print.h + * + * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/fe_utils/print.h + * + *------------------------------------------------------------------------- */ #ifndef PRINT_H #define PRINT_H @@ -11,6 +16,13 @@ #include "libpq-fe.h" +/* This is not a particularly great place for this ... */ +#ifndef __CYGWIN__ +#define DEFAULT_PAGER "more" +#else +#define DEFAULT_PAGER "less" +#endif + enum printFormat { PRINT_NOTHING = 0, /* to make sure someone initializes this */ @@ -161,6 +173,8 @@ typedef struct printQueryOpt } printQueryOpt; +extern volatile bool cancel_pressed; + extern const printTextFormat pg_asciiformat; extern const printTextFormat pg_asciiformat_old; extern printTextFormat pg_utf8format; /* ideally would be const, but... */ @@ -196,10 +210,4 @@ extern void setDecimalLocale(void); extern const printTextFormat *get_line_style(const printTableOpt *opt); extern void refresh_utf8format(const printTableOpt *opt); -#ifndef __CYGWIN__ -#define DEFAULT_PAGER "more" -#else -#define DEFAULT_PAGER "less" -#endif - #endif /* PRINT_H */ diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index d05b92a988a..1cd8452f49e 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -120,7 +120,7 @@ sub mkvcbuild our @pgcommonbkndfiles = @pgcommonallfiles; our @pgfeutilsfiles = qw( - simple_list.c string_utils.c); + mbprint.c print.c simple_list.c string_utils.c); $libpgport = $solution->AddProject('libpgport', 'lib', 'misc'); $libpgport->AddDefine('FRONTEND'); @@ -622,17 +622,12 @@ sub mkvcbuild foreach my $f (@files) { $f =~ s/\.o$/\.c/; - if ($f =~ /print\.c$/) - { # Also catches mbprint.c - $proj->AddFile('src/bin/psql/' . $f); - } - elsif ($f =~ /\.c$/) + if ($f =~ /\.c$/) { $proj->AddFile('src/bin/scripts/' . $f); } } $proj->AddIncludeDir('src/interfaces/libpq'); - $proj->AddIncludeDir('src/bin/psql'); $proj->AddReference($libpq, $libpgfeutils, $libpgcommon, $libpgport); $proj->AddDirResourceFile('src/bin/scripts');