Fossil SCM

make output to the Windows console binary-safe

jan.nijtmans 2012-09-05 07:31 eclipse-project
Commit a830168d3fe49e60fd40e059836551a65f7698e0
3 files changed +2 -8 +31 -13 +6 -15
+2 -8
--- src/blob.c
+++ src/blob.c
@@ -767,22 +767,16 @@
767767
int blob_write_to_file(Blob *pBlob, const char *zFilename){
768768
FILE *out;
769769
int wrote;
770770
771771
if( zFilename[0]==0 || (zFilename[0]=='-' && zFilename[1]==0) ){
772
- int n;
772
+ int n = blob_size(pBlob);
773773
#if defined(_WIN32)
774
- if( _isatty(fileno(stdout)) ){
775
- char *z;
776
- z = fossil_utf8_to_console(blob_str(pBlob));
777
- n = strlen(z);
778
- fwrite(z, 1, n, stdout);
779
- free(z);
774
+ if( fossil_utf8_to_console(blob_buffer(pBlob), n, 0) >= 0 ){
780775
return n;
781776
}
782777
#endif
783
- n = blob_size(pBlob);
784778
fwrite(blob_buffer(pBlob), 1, n, stdout);
785779
return n;
786780
}else{
787781
int i, nName;
788782
char *zName, zBuf[1000];
789783
--- src/blob.c
+++ src/blob.c
@@ -767,22 +767,16 @@
767 int blob_write_to_file(Blob *pBlob, const char *zFilename){
768 FILE *out;
769 int wrote;
770
771 if( zFilename[0]==0 || (zFilename[0]=='-' && zFilename[1]==0) ){
772 int n;
773 #if defined(_WIN32)
774 if( _isatty(fileno(stdout)) ){
775 char *z;
776 z = fossil_utf8_to_console(blob_str(pBlob));
777 n = strlen(z);
778 fwrite(z, 1, n, stdout);
779 free(z);
780 return n;
781 }
782 #endif
783 n = blob_size(pBlob);
784 fwrite(blob_buffer(pBlob), 1, n, stdout);
785 return n;
786 }else{
787 int i, nName;
788 char *zName, zBuf[1000];
789
--- src/blob.c
+++ src/blob.c
@@ -767,22 +767,16 @@
767 int blob_write_to_file(Blob *pBlob, const char *zFilename){
768 FILE *out;
769 int wrote;
770
771 if( zFilename[0]==0 || (zFilename[0]=='-' && zFilename[1]==0) ){
772 int n = blob_size(pBlob);
773 #if defined(_WIN32)
774 if( fossil_utf8_to_console(blob_buffer(pBlob), n, 0) >= 0 ){
 
 
 
 
 
775 return n;
776 }
777 #endif
 
778 fwrite(blob_buffer(pBlob), 1, n, stdout);
779 return n;
780 }else{
781 int i, nName;
782 char *zName, zBuf[1000];
783
+31 -13
--- src/file.c
+++ src/file.c
@@ -1116,47 +1116,65 @@
11161116
#endif
11171117
return zValue;
11181118
}
11191119
11201120
/*
1121
-** Translate UTF8 to MBCS for display on the console. Return a pointer to the
1122
-** translated text.. Call fossil_mbcs_free() to deallocate any memory
1123
-** used to store the returned pointer when done.
1121
+** Display UTF8 on the console. Return the number of
1122
+** Characters written. If stdout or stderr is redirected
1123
+** to a file, so it is not a console, -1 is returned and
1124
+** nothing is written.
11241125
*/
1125
-char *fossil_utf8_to_console(const char *zUtf8){
1126
+int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){
11261127
#ifdef _WIN32
1127
- int nChar, nByte;
1128
+ int nChar;
11281129
WCHAR *zUnicode; /* Unicode version of zUtf8 */
11291130
char *zConsole; /* Console version of zUtf8 */
11301131
int codepage; /* Console code page */
11311132
1132
- nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, NULL, 0);
1133
- zUnicode = malloc( nChar*sizeof(zUnicode[0]) );
1133
+ static int once = 1;
1134
+ static int istty[2];
1135
+ if( once ){
1136
+ istty[0] = _isatty(fileno(stdout));
1137
+ istty[1] = _isatty(fileno(stderr));
1138
+ once = 0;
1139
+ }
1140
+ if( !istty[toStdErr] ){
1141
+ /* stdout/stderr is not a console. */
1142
+ return -1;
1143
+ }
1144
+
1145
+ nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, NULL, 0);
1146
+ zUnicode = malloc( (nChar + 1) *sizeof(zUnicode[0]) );
11341147
if( zUnicode==0 ){
11351148
return 0;
11361149
}
1137
- nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zUnicode, nChar);
1150
+ nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zUnicode, nChar);
11381151
if( nChar==0 ){
11391152
free(zUnicode);
11401153
return 0;
11411154
}
1155
+ zUnicode[nChar] = '\0';
11421156
codepage = GetConsoleCP();
1143
- nByte = WideCharToMultiByte(codepage, 0, zUnicode, -1, 0, 0, 0, 0);
1144
- zConsole = malloc( nByte );
1157
+ nByte = WideCharToMultiByte(codepage, 0, zUnicode, nChar, 0, 0, 0, 0);
1158
+ zConsole = malloc( nByte + 1);
11451159
if( zConsole==0 ){
11461160
free(zUnicode);
11471161
return 0;
11481162
}
1149
- nByte = WideCharToMultiByte(codepage, 0, zUnicode, -1, zConsole, nByte, 0, 0);
1163
+ nByte = WideCharToMultiByte(codepage, 0, zUnicode, nChar, zConsole, nByte, 0, 0);
1164
+ zConsole[nByte] = '\0';
11501165
free(zUnicode);
11511166
if( nByte == 0 ){
11521167
free(zConsole);
11531168
zConsole = 0;
1169
+ return 0;
11541170
}
1155
- return zConsole;
1171
+ fwrite(zConsole, 1, nByte, toStdErr ? stderr : stdout);
1172
+ fflush(toStdErr ? stderr : stdout);
1173
+ return nChar;
11561174
#else
1157
- return (char*)zUtf8; /* No-op on unix */
1175
+ return -1; /* No-op on unix */
11581176
#endif
11591177
}
11601178
11611179
/*
11621180
** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
11631181
--- src/file.c
+++ src/file.c
@@ -1116,47 +1116,65 @@
1116 #endif
1117 return zValue;
1118 }
1119
1120 /*
1121 ** Translate UTF8 to MBCS for display on the console. Return a pointer to the
1122 ** translated text.. Call fossil_mbcs_free() to deallocate any memory
1123 ** used to store the returned pointer when done.
 
1124 */
1125 char *fossil_utf8_to_console(const char *zUtf8){
1126 #ifdef _WIN32
1127 int nChar, nByte;
1128 WCHAR *zUnicode; /* Unicode version of zUtf8 */
1129 char *zConsole; /* Console version of zUtf8 */
1130 int codepage; /* Console code page */
1131
1132 nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, NULL, 0);
1133 zUnicode = malloc( nChar*sizeof(zUnicode[0]) );
 
 
 
 
 
 
 
 
 
 
 
 
1134 if( zUnicode==0 ){
1135 return 0;
1136 }
1137 nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zUnicode, nChar);
1138 if( nChar==0 ){
1139 free(zUnicode);
1140 return 0;
1141 }
 
1142 codepage = GetConsoleCP();
1143 nByte = WideCharToMultiByte(codepage, 0, zUnicode, -1, 0, 0, 0, 0);
1144 zConsole = malloc( nByte );
1145 if( zConsole==0 ){
1146 free(zUnicode);
1147 return 0;
1148 }
1149 nByte = WideCharToMultiByte(codepage, 0, zUnicode, -1, zConsole, nByte, 0, 0);
 
1150 free(zUnicode);
1151 if( nByte == 0 ){
1152 free(zConsole);
1153 zConsole = 0;
 
1154 }
1155 return zConsole;
 
 
1156 #else
1157 return (char*)zUtf8; /* No-op on unix */
1158 #endif
1159 }
1160
1161 /*
1162 ** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
1163
--- src/file.c
+++ src/file.c
@@ -1116,47 +1116,65 @@
1116 #endif
1117 return zValue;
1118 }
1119
1120 /*
1121 ** Display UTF8 on the console. Return the number of
1122 ** Characters written. If stdout or stderr is redirected
1123 ** to a file, so it is not a console, -1 is returned and
1124 ** nothing is written.
1125 */
1126 int fossil_utf8_to_console(const char *zUtf8, int nByte, int toStdErr){
1127 #ifdef _WIN32
1128 int nChar;
1129 WCHAR *zUnicode; /* Unicode version of zUtf8 */
1130 char *zConsole; /* Console version of zUtf8 */
1131 int codepage; /* Console code page */
1132
1133 static int once = 1;
1134 static int istty[2];
1135 if( once ){
1136 istty[0] = _isatty(fileno(stdout));
1137 istty[1] = _isatty(fileno(stderr));
1138 once = 0;
1139 }
1140 if( !istty[toStdErr] ){
1141 /* stdout/stderr is not a console. */
1142 return -1;
1143 }
1144
1145 nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, NULL, 0);
1146 zUnicode = malloc( (nChar + 1) *sizeof(zUnicode[0]) );
1147 if( zUnicode==0 ){
1148 return 0;
1149 }
1150 nChar = MultiByteToWideChar(CP_UTF8, 0, zUtf8, nByte, zUnicode, nChar);
1151 if( nChar==0 ){
1152 free(zUnicode);
1153 return 0;
1154 }
1155 zUnicode[nChar] = '\0';
1156 codepage = GetConsoleCP();
1157 nByte = WideCharToMultiByte(codepage, 0, zUnicode, nChar, 0, 0, 0, 0);
1158 zConsole = malloc( nByte + 1);
1159 if( zConsole==0 ){
1160 free(zUnicode);
1161 return 0;
1162 }
1163 nByte = WideCharToMultiByte(codepage, 0, zUnicode, nChar, zConsole, nByte, 0, 0);
1164 zConsole[nByte] = '\0';
1165 free(zUnicode);
1166 if( nByte == 0 ){
1167 free(zConsole);
1168 zConsole = 0;
1169 return 0;
1170 }
1171 fwrite(zConsole, 1, nByte, toStdErr ? stderr : stdout);
1172 fflush(toStdErr ? stderr : stdout);
1173 return nChar;
1174 #else
1175 return -1; /* No-op on unix */
1176 #endif
1177 }
1178
1179 /*
1180 ** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
1181
+6 -15
--- src/printf.c
+++ src/printf.c
@@ -814,25 +814,16 @@
814814
** if the output is going to the screen. If output is redirected into
815815
** a file, no translation occurs. No translation ever occurs on unix.
816816
*/
817817
void fossil_puts(const char *z, int toStdErr){
818818
#if defined(_WIN32)
819
- static int once = 1;
820
- static int istty[2];
821
- char *zToFree = 0;
822
- if( once ){
823
- istty[0] = _isatty(fileno(stdout));
824
- istty[1] = _isatty(fileno(stderr));
825
- once = 0;
826
- }
827
- assert( toStdErr==0 || toStdErr==1 );
828
- if( istty[toStdErr] ) z = zToFree = fossil_utf8_to_console(z);
829
- fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
830
- free(zToFree);
831
-#else
832
- fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
833
-#endif
819
+ if( fossil_utf8_to_console(z, strlen(z), toStdErr) >= 0 ){
820
+ return;
821
+ }
822
+#endif
823
+ assert( toStdErr==0 || toStdErr==1 );
824
+ fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
834825
fflush(toStdErr ? stderr : stdout);
835826
}
836827
837828
/*
838829
** Write output for user consumption. If g.cgiOutput is enabled, then
839830
--- src/printf.c
+++ src/printf.c
@@ -814,25 +814,16 @@
814 ** if the output is going to the screen. If output is redirected into
815 ** a file, no translation occurs. No translation ever occurs on unix.
816 */
817 void fossil_puts(const char *z, int toStdErr){
818 #if defined(_WIN32)
819 static int once = 1;
820 static int istty[2];
821 char *zToFree = 0;
822 if( once ){
823 istty[0] = _isatty(fileno(stdout));
824 istty[1] = _isatty(fileno(stderr));
825 once = 0;
826 }
827 assert( toStdErr==0 || toStdErr==1 );
828 if( istty[toStdErr] ) z = zToFree = fossil_utf8_to_console(z);
829 fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
830 free(zToFree);
831 #else
832 fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
833 #endif
834 fflush(toStdErr ? stderr : stdout);
835 }
836
837 /*
838 ** Write output for user consumption. If g.cgiOutput is enabled, then
839
--- src/printf.c
+++ src/printf.c
@@ -814,25 +814,16 @@
814 ** if the output is going to the screen. If output is redirected into
815 ** a file, no translation occurs. No translation ever occurs on unix.
816 */
817 void fossil_puts(const char *z, int toStdErr){
818 #if defined(_WIN32)
819 if( fossil_utf8_to_console(z, strlen(z), toStdErr) >= 0 ){
820 return;
821 }
822 #endif
823 assert( toStdErr==0 || toStdErr==1 );
824 fwrite(z, 1, strlen(z), toStdErr ? stderr : stdout);
 
 
 
 
 
 
 
 
 
825 fflush(toStdErr ? stderr : stdout);
826 }
827
828 /*
829 ** Write output for user consumption. If g.cgiOutput is enabled, then
830

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button