Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4290269

Browse files
committed
Implemented direct consile write via port/snprintf.c
1 parent0cfd6d5 commit4290269

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

‎src/bin/psql/startup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ main(int argc, char *argv[])
149149
pset.popt.topt.env_columns=getenv("COLUMNS") ?atoi(getenv("COLUMNS")) :0;
150150

151151
pset.notty= (!isatty(fileno(stdin))|| !isatty(fileno(stdout)));
152-
#ifdefHAVE_WIN_32_LIBEDIT
152+
#ifdefHAVE_WIN32_LIBEDIT
153153
if (!pset.notty&&pset.encoding==PG_SQL_ASCII) {
154-
_setmode(_fileno(stdout),_O_U8TEXT);
155154
pset.encoding=PG_UTF8;
156155
}
157156
#endif
@@ -904,3 +903,4 @@ EstablishVariableSpace(void)
904903
SetVariableAssignHook(pset.vars,"PROMPT3",prompt3_hook);
905904
SetVariableAssignHook(pset.vars,"VERBOSITY",verbosity_hook);
906905
}
906+

‎src/include/port.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ extern unsigned char pg_ascii_tolower(unsigned char ch);
151151
#ifdefprintf
152152
#undef printf
153153
#endif
154+
#ifdeffputs
155+
#undef fputs
156+
#endif
154157

155158
externintpg_vsnprintf(char*str,size_tcount,constchar*fmt,va_listargs);
156159
externintpg_snprintf(char*str,size_tcount,constchar*fmt,...)pg_attribute_printf(3,4);
@@ -159,6 +162,9 @@ extern intpg_vfprintf(FILE *stream, const char *fmt, va_list args);
159162
externintpg_fprintf(FILE*stream,constchar*fmt,...)pg_attribute_printf(2,3);
160163
externintpg_printf(constchar*fmt,...)pg_attribute_printf(1,2);
161164

165+
#ifdefHAVE_WIN32_LIBEDIT
166+
externintfputs(constchar*s,FILE*stream);
167+
#endif
162168
/*
163169
*The GCC-specific code below prevents the pg_attribute_printf above from
164170
*being replaced, and this is required because gcc doesn't know anything
@@ -179,6 +185,10 @@ extern intpg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
179185
#definefprintfpg_fprintf
180186
#defineprintfpg_printf
181187
#endif
188+
#ifdefHAVE_WIN32_LIBEDIT
189+
/* Catch fputs as well so we can use WriteConsole for table output */
190+
#definefputs(s,f)pg_fputs(s,f)
191+
#endif
182192
#endif/* USE_REPL_SNPRINTF */
183193

184194
#if defined(WIN32)

‎src/port/snprintf.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@
103103
#undefvfprintf
104104
#undeffprintf
105105
#undefprintf
106-
106+
#ifdefHAVE_WIN32_LIBEDIT
107+
#undef fputs
108+
#endif
107109
/* Info about where the formatted output is going */
108110
typedefstruct
109111
{
@@ -262,11 +264,31 @@ flushbuffer(PrintfTarget *target)
262264
if (!target->failed&&nc>0)
263265
{
264266
size_twritten;
265-
267+
#ifdefHAVE_WIN32_LIBEDIT
268+
/*With Win32 libedit we use WriteW API to write to
269+
* console instead of fwrite*/
270+
if (isatty(fileno(target->stream)))
271+
{
272+
/* Convert message from buffer (expected as utf8)
273+
to widechar */
274+
HANDLEconsoleHandle=_get_osf_handle(_fileno(target->stream));
275+
DWORDactuallyWritten;
276+
wchar_t*widebuf= (wchar_t*)malloc(nc*sizeof(wchar_t));
277+
written=MultiByteToWideChar(CP_UTF8,0,target->bufstart,nc,widebuf,nc);
278+
WriteConsoleW(consoleHandle,widebuf,written,&actuallyWritten,NULL);
279+
if (actuallyWritten==written)
280+
target->nchars+=nc;
281+
else
282+
target->failed= true;
283+
}else {
284+
#endif
266285
written = fwrite(target->bufstart, 1, nc, target->stream);
267286
target->nchars+=written;
268287
if (written!=nc)
269288
target->failed= true;
289+
#ifdefHAVE_WIN32_LIBEDIT
290+
}
291+
#endif
270292
}
271293
target->bufptr=target->bufstart;
272294
}
@@ -1139,3 +1161,22 @@ trailing_pad(int *padlen, PrintfTarget *target)
11391161
++(*padlen);
11401162
}
11411163
}
1164+
1165+
#ifdefHAVE_WIN32_LIBEDIT
1166+
/* replacement to fputs function which uses flushBuffer */
1167+
intpg_fputs(constchar*s,FILE*stream)
1168+
{
1169+
PrintfTargettarget;
1170+
if (stream==NULL)
1171+
{
1172+
errno=EINVAL;
1173+
return-1;
1174+
}
1175+
target.bufstart=target.bufptr=s;
1176+
target.nchars=strlen(s);
1177+
target.bufend=NULL;
1178+
target.failed=false;
1179+
flushbuffer(&target);
1180+
returntarget.failed ?-1 :target.nchars;
1181+
}
1182+
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp