PostgreSQL Source Code: src/interfaces/libpq/fe-print.c File Reference (original) (raw)

69{

70 int nFields;

71

73

74 if (nFields > 0)

75 {

76 int i,

77 j;

78 int nTups;

79 int *fieldMax = NULL;

80 unsigned char *fieldNotNum = NULL;

81 char *border = NULL;

82 char **fields = NULL;

83 const char **fieldNames = NULL;

84 int fieldMaxLen = 0;

85 int numFieldName;

86 int fs_len = strlen(po->fieldSep);

87 int total_line_length = 0;

88 bool usePipe = false;

89 char *pagerenv;

90

91#if !defined(WIN32)

92 sigset_t osigset;

93 bool sigpipe_masked = false;

94 bool sigpipe_pending;

95#endif

96

97#ifdef TIOCGWINSZ

98 struct winsize screen_size;

99#else

100 struct winsize

101 {

102 int ws_row;

103 int ws_col;

104 } screen_size;

105#endif

106

108 fieldNames = (const char **) calloc(nFields, sizeof(char *));

109 fieldNotNum = (unsigned char *) calloc(nFields, 1);

110 fieldMax = (int *) calloc(nFields, sizeof(int));

111 if (!fieldNames || !fieldNotNum || !fieldMax)

112 {

114 goto exit;

115 }

116 for (numFieldName = 0;

118 numFieldName++)

119 ;

120 for (j = 0; j < nFields; j++)

121 {

123 const char *s = (j < numFieldName && po->fieldName[j][0]) ?

125

126 fieldNames[j] = s;

127 len = s ? strlen(s) : 0;

128 fieldMax[j] = len;

129 len += fs_len;

130 if (len > fieldMaxLen)

131 fieldMaxLen = len;

132 total_line_length += len;

133 }

134

135 total_line_length += nFields * strlen(po->fieldSep) + 1;

136

137 if (fout == NULL)

139 if (po->pager && fout == stdout && isatty(fileno(stdin)) &&

140 isatty(fileno(stdout)))

141 {

142

143

144

145

146#ifdef TIOCGWINSZ

147 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1 ||

148 screen_size.ws_col == 0 ||

149 screen_size.ws_row == 0)

150 {

151 screen_size.ws_row = 24;

152 screen_size.ws_col = 80;

153 }

154#else

155 screen_size.ws_row = 24;

156 screen_size.ws_col = 80;

157#endif

158

159

160

161

162

163

164

165 pagerenv = getenv("PAGER");

166

167 if (pagerenv != NULL &&

168 strspn(pagerenv, " \t\r\n") != strlen(pagerenv) &&

171 nTups * (nFields + 1) >= screen_size.ws_row) ||

173 nTups * (total_line_length / screen_size.ws_col + 1) *

174 (1 + (po->standard != 0)) >= screen_size.ws_row -

176 (total_line_length / screen_size.ws_col + 1) * 2

177 - (po->header != 0) * 2

178 )))

179 {

180 fflush(NULL);

181 fout = popen(pagerenv, "w");

182 if (fout)

183 {

184 usePipe = true;

185#ifndef WIN32

187 sigpipe_masked = true;

188#endif

189 }

190 else

192 }

193 }

194

196 {

197 fields = (char **) calloc((size_t) nTups + 1,

198 nFields * sizeof(char *));

199 if (!fields)

200 {

202 goto exit;

203 }

204 }

206 {

208 {

212 else

214 }

215 else

216 {

217 int len = 0;

218

219 for (j = 0; j < nFields; j++)

220 {

221 const char *s = fieldNames[j];

222

223 fputs(s, fout);

224 len += strlen(s) + fs_len;

225 if ((j + 1) < nFields)

227 }

228 fputc('\n', fout);

229 for (len -= fs_len; len--; fputc('-', fout));

230 fputc('\n', fout);

231 }

232 }

234 {

236 fprintf(fout, "

%s

\n", po->caption);

237 else

239 "

"

240 "Query retrieved %d rows * %d fields"

241 "\n",

242 nTups, nFields);

243 }

244 for (i = 0; i < nTups; i++)

245 {

247 {

250 "<table %s><caption align=\"top\">%d\n",

252 else

254 }

255 for (j = 0; j < nFields; j++)

256 {

257 if (do\_field(po, res, i, j, fs_len, fields, nFields,

258 fieldNames, fieldNotNum,

259 fieldMax, fieldMaxLen, fout))

260 goto exit;

261 }

263 fputs("\n", fout);

264 }

266 {

268 {

270 {

273 "<table %s><caption align=\"top\">%s\n",

276 else

278 "<table %s><caption align=\"top\">"

279 "Retrieved %d rows * %d fields"

280 "\n",

282 }

283 else

285 }

287 border = do_header(fout, po, nFields, fieldMax, fieldNames,

288 fieldNotNum, fs_len, res);

289 for (i = 0; i < nTups; i++)

290 output_row(fout, po, nFields, fields,

291 fieldNotNum, fieldMax, border, i);

292 }

295 (PQntuples(res) == 1) ? "" : "s");

297 fputs("\n", fout);

298

299exit:

300 free(fieldMax);

301 free(fieldNotNum);

302 free(border);

303 if (fields)

304 {

305

306 size_t numfields = ((size_t) nTups + 1) * (size_t) nFields;

307

308 while (numfields-- > 0)

309 free(fields[numfields]);

310 free(fields);

311 }

312 free(fieldNames);

313 if (usePipe)

314 {

315#ifdef WIN32

316 _pclose(fout);

317#else

318 pclose(fout);

319

320

321 if (sigpipe_masked)

323#endif

324 }

325 }

326}

static char * do_header(FILE *fout, const PQprintOpt *po, const int nFields, int *fieldMax, const char **fieldNames, unsigned char *fieldNotNum, const int fs_len, const PGresult *res)

static bool do_field(const PQprintOpt *po, const PGresult *res, const int i, const int j, const int fs_len, char **fields, const int nFields, const char **fieldNames, unsigned char *fieldNotNum, int *fieldMax, const int fieldMaxLen, FILE *fout)

static void output_row(FILE *fout, const PQprintOpt *po, const int nFields, char **fields, unsigned char *fieldNotNum, int *fieldMax, char *border, const int row_index)

void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)

int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)