📄 print.c
字号:
/* * psql - the PostgreSQL interactive terminal * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.43 2003/08/14 18:49:42 tgl Exp $ */#include "postgres_fe.h"#include "common.h"#include "print.h"#include <math.h>#include <signal.h>#if !defined(_MSC_VER) && !defined(__BORLANDC__)#include <unistd.h>#endif#ifndef WIN32#include <sys/ioctl.h> /* for ioctl() */#endif#ifdef HAVE_TERMIOS_H#include <termios.h>#endif#include "pqsignal.h"#include "libpq-fe.h"#include "mbprint.h"/*************************//* Unaligned text *//*************************/static voidprint_unaligned_text(const char *title, const char *const * headers, const char *const * cells, const char *const * footers, const char *opt_fieldsep, const char *opt_recordsep, bool opt_barebones, FILE *fout){ unsigned int col_count = 0; unsigned int i; const char *const * ptr; bool need_recordsep = false; if (!opt_fieldsep) opt_fieldsep = ""; if (!opt_recordsep) opt_recordsep = ""; /* print title */ if (!opt_barebones && title) fprintf(fout, "%s%s", title, opt_recordsep); /* print headers and count columns */ for (ptr = headers; *ptr; ptr++) { col_count++; if (!opt_barebones) { if (col_count > 1) fputs(opt_fieldsep, fout); fputs(*ptr, fout); } } if (!opt_barebones) need_recordsep = true; /* print cells */ i = 0; for (ptr = cells; *ptr; ptr++) { if (need_recordsep) { fputs(opt_recordsep, fout); need_recordsep = false; } fputs(*ptr, fout); if ((i + 1) % col_count) fputs(opt_fieldsep, fout); else need_recordsep = true; i++; } /* print footers */ if (!opt_barebones && footers) for (ptr = footers; *ptr; ptr++) { if (need_recordsep) { fputs(opt_recordsep, fout); need_recordsep = false; } fputs(*ptr, fout); need_recordsep = true; } /* the last record needs to be concluded with a newline */ if (need_recordsep) fputc('\n', fout);}static voidprint_unaligned_vertical(const char *title, const char *const * headers, const char *const * cells, const char *const * footers, const char *opt_fieldsep, const char *opt_recordsep, bool opt_barebones, FILE *fout){ unsigned int col_count = 0; unsigned int i; const char *const * ptr; if (!opt_fieldsep) opt_fieldsep = ""; if (!opt_recordsep) opt_recordsep = ""; /* print title */ if (!opt_barebones && title) fputs(title, fout); /* count columns */ for (ptr = headers; *ptr; ptr++) col_count++; /* print records */ for (i = 0, ptr = cells; *ptr; i++, ptr++) { if (i != 0 || (!opt_barebones && title)) { fputs(opt_recordsep, fout); if (i % col_count == 0) fputs(opt_recordsep, fout); /* another one */ } fputs(headers[i % col_count], fout); fputs(opt_fieldsep, fout); fputs(*ptr, fout); } /* print footers */ if (!opt_barebones && footers && *footers) { fputs(opt_recordsep, fout); for (ptr = footers; *ptr; ptr++) { fputs(opt_recordsep, fout); fputs(*ptr, fout); } } fputc('\n', fout);}/********************//* Aligned text *//********************//* draw "line" */static void_print_horizontal_line(const unsigned int col_count, const unsigned int *widths, unsigned short border, FILE *fout){ unsigned int i, j; if (border == 1) fputc('-', fout); else if (border == 2) fputs("+-", fout); for (i = 0; i < col_count; i++) { for (j = 0; j < widths[i]; j++) fputc('-', fout); if (i < col_count - 1) { if (border == 0) fputc(' ', fout); else fputs("-+-", fout); } } if (border == 2) fputs("-+", fout); else if (border == 1) fputc('-', fout); fputc('\n', fout);}static voidprint_aligned_text(const char *title, const char *const * headers, const char *const * cells, const char *const * footers, const char *opt_align, bool opt_barebones, unsigned short int opt_border, int encoding, FILE *fout){ unsigned int col_count = 0; unsigned int cell_count = 0; unsigned int *head_w, *cell_w; unsigned int i, tmp; unsigned int *widths, total_w; const char *const * ptr; /* count columns */ for (ptr = headers; *ptr; ptr++) col_count++; if (col_count > 0) { widths = calloc(col_count, sizeof(*widths)); if (!widths) { perror("calloc"); exit(EXIT_FAILURE); } head_w = calloc(col_count, sizeof(*head_w)); if (!head_w) { perror("calloc"); exit(EXIT_FAILURE); } } else { widths = NULL; head_w = NULL; } /* count cells (rows * cols) */ for (ptr = cells; *ptr; ptr++) cell_count++; if (cell_count > 0) { cell_w = calloc(cell_count, sizeof(*cell_w)); if (!cell_w) { perror("calloc"); exit(EXIT_FAILURE); } } else cell_w = NULL; /* calc column widths */ for (i = 0; i < col_count; i++) { tmp = pg_wcswidth((unsigned char *) headers[i], strlen(headers[i]), encoding); if (tmp > widths[i]) widths[i] = tmp; head_w[i] = tmp; } for (i = 0, ptr = cells; *ptr; ptr++, i++) { tmp = pg_wcswidth((unsigned char *) *ptr, strlen(*ptr), encoding); if (tmp > widths[i % col_count]) widths[i % col_count] = tmp; cell_w[i] = tmp; } if (opt_border == 0) total_w = col_count - 1; else if (opt_border == 1) total_w = col_count * 3 - 1; else total_w = col_count * 3 + 1; for (i = 0; i < col_count; i++) total_w += widths[i]; /* print title */ if (title && !opt_barebones) { tmp = pg_wcswidth((unsigned char *) title, strlen(title), encoding); if (tmp >= total_w) fprintf(fout, "%s\n", title); else fprintf(fout, "%-*s%s\n", (total_w - tmp) / 2, "", title); } /* print headers */ if (!opt_barebones) { if (opt_border == 2) _print_horizontal_line(col_count, widths, opt_border, fout); if (opt_border == 2) fputs("| ", fout); else if (opt_border == 1) fputc(' ', fout); for (i = 0; i < col_count; i++) { unsigned int nbspace; nbspace = widths[i] - head_w[i]; /* centered */ fprintf(fout, "%-*s%s%-*s", nbspace / 2, "", headers[i], (nbspace + 1) / 2, ""); if (i < col_count - 1) { if (opt_border == 0) fputc(' ', fout); else fputs(" | ", fout); } } if (opt_border == 2) fputs(" |", fout); else if (opt_border == 1) fputc(' ', fout);; fputc('\n', fout); _print_horizontal_line(col_count, widths, opt_border, fout); } /* print cells */ for (i = 0, ptr = cells; *ptr; i++, ptr++) { /* beginning of line */ if (i % col_count == 0) { if (opt_border == 2) fputs("| ", fout); else if (opt_border == 1) fputc(' ', fout); } /* content */ if (opt_align[i % col_count] == 'r') { fprintf(fout, "%*s%s", widths[i % col_count] - cell_w[i], "", cells[i]); } else { if ((i + 1) % col_count == 0 && opt_border != 2) fputs(cells[i], fout); else fprintf(fout, "%-s%*s", cells[i], widths[i % col_count] - cell_w[i], ""); } /* divider */ if ((i + 1) % col_count) { if (opt_border == 0) fputc(' ', fout); else fputs(" | ", fout); } /* end of line */ else { if (opt_border == 2) fputs(" |", fout); fputc('\n', fout); } } if (opt_border == 2) _print_horizontal_line(col_count, widths, opt_border, fout); /* print footers */ if (footers && !opt_barebones) for (ptr = footers; *ptr; ptr++) fprintf(fout, "%s\n", *ptr); fputc('\n', fout); /* clean up */ free(cell_w); free(head_w); free(widths);}static voidprint_aligned_vertical(const char *title, const char *const * headers, const char *const * cells, const char *const * footers, bool opt_barebones, unsigned short int opt_border, int encoding, FILE *fout){ unsigned int col_count = 0; unsigned int record = 1; const char *const * ptr; unsigned int i, tmp = 0, hwidth = 0, dwidth = 0; char *divider; unsigned int cell_count = 0; unsigned int *cell_w, *head_w; if (cells[0] == NULL) { puts(gettext("(No rows)\n")); return; } /* count headers and find longest one */ for (ptr = headers; *ptr; ptr++) col_count++; if (col_count > 0) { head_w = calloc(col_count, sizeof(*head_w)); if (!head_w) { perror("calloc"); exit(EXIT_FAILURE); } } else head_w = NULL; for (i = 0; i < col_count; i++) { tmp = pg_wcswidth((unsigned char *) headers[i], strlen(headers[i]), encoding); if (tmp > hwidth) hwidth = tmp; head_w[i] = tmp; } /* Count cells, find their lengths */ for (ptr = cells; *ptr; ptr++) cell_count++; if (cell_count > 0) { cell_w = calloc(cell_count, sizeof(*cell_w)); if (!cell_w) { perror("calloc"); exit(EXIT_FAILURE); } } else cell_w = NULL; /* find longest data cell */ for (i = 0, ptr = cells; *ptr; ptr++, i++) { tmp = pg_wcswidth((unsigned char *) *ptr, strlen(*ptr), encoding); if (tmp > dwidth) dwidth = tmp; cell_w[i] = tmp; } /* print title */ if (!opt_barebones && title) fprintf(fout, "%s\n", title); /* make horizontal border */ divider = malloc(hwidth + dwidth + 10); if (!divider) { perror("malloc"); exit(EXIT_FAILURE); } divider[0] = '\0'; if (opt_border == 2) strcat(divider, "+-"); for (i = 0; i < hwidth; i++) strcat(divider, opt_border > 0 ? "-" : " "); if (opt_border > 0) strcat(divider, "-+-"); else strcat(divider, " "); for (i = 0; i < dwidth; i++) strcat(divider, opt_border > 0 ? "-" : " "); if (opt_border == 2) strcat(divider, "-+"); /* print records */ for (i = 0, ptr = cells; *ptr; i++, ptr++) { if (i % col_count == 0) { if (!opt_barebones) { char *record_str = malloc(32); size_t record_str_len; if (!record_str) { perror("malloc"); exit(EXIT_FAILURE); } if (opt_border == 0) snprintf(record_str, 32, "* Record %d", record++); else snprintf(record_str, 32, "[ RECORD %d ]", record++); record_str_len = strlen(record_str); if (record_str_len + opt_border > strlen(divider)) fprintf(fout, "%.*s%s\n", opt_border, divider, record_str); else { char *div_copy = strdup(divider); if (!div_copy) { perror("malloc"); exit(EXIT_FAILURE); } strncpy(div_copy + opt_border, record_str, record_str_len); fprintf(fout, "%s\n", div_copy); free(div_copy); } free(record_str); } else if (i != 0 || opt_border == 2) fprintf(fout, "%s\n", divider); } if (opt_border == 2) fputs("| ", fout); fprintf(fout, "%-s%*s", headers[i % col_count], hwidth - head_w[i % col_count], ""); if (opt_border > 0) fputs(" | ", fout); else fputs(" ", fout); if (opt_border < 2) fprintf(fout, "%s\n", *ptr); else fprintf(fout, "%-s%*s |\n", *ptr, dwidth - cell_w[i], ""); } if (opt_border == 2) fprintf(fout, "%s\n", divider); /* print footers */ if (!opt_barebones && footers && *footers) { if (opt_border < 2) fputc('\n', fout); for (ptr = footers; *ptr; ptr++) fprintf(fout, "%s\n", *ptr); } fputc('\n', fout); free(divider); free(cell_w); free(head_w);}/**********************//* HTML printing ******//**********************/voidhtml_escaped_print(const char *in, FILE *fout){ const char *p; for (p = in; *p; p++) switch (*p) { case '&': fputs("&", fout); break; case '<': fputs("<", fout); break; case '>': fputs(">", fout); break; case '\n': fputs("<br />\n", fout); break; case '"': fputs(""", fout); break; case '\'': fputs("'", fout); break; default: fputc(*p, fout); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -