Skip to contentSkip to conversation

UTF-8 fix

PATCH New Marcel Sebek 2 participants 4 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 UTF-8 fix

sensors doesn't print degree sign correctly on utf-8 console. This patch fixes it.

CHANGES +1 -0
diff -urN lm_sensors2/CHANGES lm_sensors2-new/CHANGES
@@ -31,6 +31,7 @@
                    Drop linux 2.2 support
                    Add w83627hf
   Program sensors: Do not show algorithm by default
+                   Fix output to UTF-8 console
   Program sensors-detect: Fix PATH issues
   
 
prog/sensors/chips.c +17 -4
diff -urN lm_sensors2/prog/sensors/chips.c lm_sensors2-new/prog/sensors/chips.c
@@ -34,6 +34,7 @@
 static inline float deg_ctof( float );
 
 extern int fahrenheit;
+extern int utf8;
 
 char *spacestr(int n)
 {
@@ -65,12 +66,18 @@
    char degv[5];
 
    if (fahrenheit) {
-      sprintf(degv, "%cF", 176);
+      if (utf8)
+        sprintf(degv, "%c%cF", 0xc2, 0xb0);
+      else
+        sprintf(degv, "%cF", 176);
       n_cur  = deg_ctof(n_cur);
       n_over = deg_ctof(n_over);
       n_hyst = deg_ctof(n_hyst);
    } else {
-      sprintf(degv, "%cC", 176);
+      if (utf8)
+        sprintf(degv, "%c%cC", 0xc2, 0xb0);
+      else
+        sprintf(degv, "%cC", 176);
    }
 
 /* use %* to pass precision as an argument */
@@ -4065,13 +4072,19 @@
 	char suffix[5];
 
 	if (fahrenheit) {
-		sprintf (suffix,"%cF",176);
+		if (utf8)
+			sprintf (suffix,"%c%cF",0xc2,0xb0);
+		else
+			sprintf (suffix,"%cF",176);
 		n_cur = deg_ctof (n_cur);
 		n_high = deg_ctof (n_high);
 		n_low = deg_ctof (n_low);
 		n_crit = deg_ctof (n_crit);
 		n_hyst = deg_ctof (n_hyst);
-	} else sprintf (suffix,"%cC",176);
+	} else if (utf8)
+		sprintf (suffix,"%c%cC",0xc2,0xb0);
+	else
+		sprintf (suffix,"%cC",176);
 
 	printf ("%+6.4f%s (high = %+6.4f%s, low = %+6.4f%s, crit = %+6.4f%s, hyst = %+6.4f%s)",
 			n_cur,suffix,
prog/sensors/main.c +10 -0
diff -urN lm_sensors2/prog/sensors/main.c lm_sensors2-new/prog/sensors/main.c
@@ -22,6 +22,8 @@
 #include <getopt.h>
 #include <string.h>
 #include <errno.h>
+#include <locale.h>
+#include <langinfo.h>
 
 #include "lib/sensors.h" 
 #include "lib/error.h"
@@ -53,6 +55,7 @@
 sensors_chip_name chips[CHIPS_MAX];
 int chips_count=0;
 int do_sets, do_unknown, fahrenheit, show_algorithm, hide_adapter, hide_unknown;
+int utf8;
 
 void print_short_help(void)
 {
@@ -167,6 +170,13 @@
     { 0,0,0,0 }
   };
 
+  setlocale(LC_CTYPE, "");
+
+  if (strcmp(nl_langinfo(CODESET), "UTF-8") = 0)
+    utf8 = 1;
+  else
+    utf8 = 0;
+
   do_unknown = 0;
   do_sets = 0;
   show_algorithm = 0;
Signature · 4 lines
Marcel Sebek
jabber: [email protected]                     ICQ: 279852819
linux user number: 307850                 GPG ID: 5F88735E
GPG FP: 0F01 BAB8 3148 94DB B95D  1FCA 8B63 CA06 5F88 735E
#1 lore raw

PATCH 1/1 UTF-8 fix

Quoted 24 lines · Jean Delvare
Quoted 2 lines

sensors doesn't print degree sign correctly on utf-8 console. This patch fixes it.

Hi Sebek,

Interesting patch. I'm not using UTF-8 but believe many people do and will appreciate the change you propose.

I took a look at the patch itself and I won't accept it as is, because of the code duplication it adds. First because there are three places where you have to "intercept" temperature prints (common, lm92 and lm80, which you forgot BTW). Second because you have to handle both Celcius and Farhenheit each time. I of course do not blame you, but ourselves, for that, since this is the base code that is bad.

I changed that by centralizing the construction of the "degrees" string in a single place (in main.c, because it is in no way chip dependant). I just commited the change to CVS.

I would appreciate it much if you could checkout our CVS code (module lm_sensors2) and submit a new patch based on it. You should only need to intervene in one place (where degstr is built, in main.c), which will make the patch much cleaner IMHO.

Quoted 6 lines

I don't do it myself because 1* I'd like you to test the change I just made and 2* I couldn't test the UTF-8 thing, so your help is needed anyway.

Thanks.

OK, here's new patch against latest CVS tree.

CHANGES +1 -0
diff -urN lm_sensors2/CHANGES lm_sensors2-new/CHANGES
@@ -31,6 +31,7 @@
                    Drop linux 2.2 support
                    Add w83627hf
   Program sensors: Do not show algorithm by default
+                   Fix output to UTF-8 console
   Program sensors-detect: Fix PATH issues
   
 
prog/sensors/main.c +8 -1
diff -urN lm_sensors2/prog/sensors/main.c lm_sensors2-new/prog/sensors/main.c
@@ -22,6 +22,8 @@
 #include <getopt.h>
 #include <string.h>
 #include <errno.h>
+#include <locale.h>
+#include <langinfo.h>
 
 #include "lib/sensors.h" 
 #include "lib/error.h"
@@ -169,6 +171,8 @@
     { 0,0,0,0 }
   };
 
+  setlocale(LC_CTYPE, "");
+
   do_unknown = 0;
   do_sets = 0;
   show_algorithm = 0;
@@ -248,7 +252,10 @@
   close_config_file();
 
   /* build the degrees string */
-  sprintf(degstr, "%c", 176);
+  if (strcmp(nl_langinfo(CODESET), "UTF-8") = 0)
+    sprintf(degstr, "%c%c", 0xc2, 0xb0);
+  else
+    sprintf(degstr, "%c", 176);
   if (fahrenheit) {
     strcat(degstr, "F");
   } else {
Signature · 4 lines
Marcel Sebek
jabber: [email protected]                     ICQ: 279852819
linux user number: 307850                 GPG ID: 5F88735E
GPG FP: 0F01 BAB8 3148 94DB B95D  1FCA 8B63 CA06 5F88 735E
#2 lore raw

PATCH 1/1 UTF-8 fix

In reply to Marcel Sebek
Quoted 2 lines

sensors doesn't print degree sign correctly on utf-8 console. This patch fixes it.

Hi Sebek,

Interesting patch. I'm not using UTF-8 but believe many people do and will appreciate the change you propose.

I took a look at the patch itself and I won't accept it as is, because of the code duplication it adds. First because there are three places where you have to "intercept" temperature prints (common, lm92 and lm80, which you forgot BTW). Second because you have to handle both Celcius and Farhenheit each time. I of course do not blame you, but ourselves, for that, since this is the base code that is bad.

I changed that by centralizing the construction of the "degrees" string in a single place (in main.c, because it is in no way chip dependant). I just commited the change to CVS.

I would appreciate it much if you could checkout our CVS code (module lm_sensors2) and submit a new patch based on it. You should only need to intervene in one place (where degstr is built, in main.c), which will make the patch much cleaner IMHO.

I don't do it myself because 1* I'd like you to test the change I just made and 2* I couldn't test the UTF-8 thing, so your help is needed anyway.

Thanks.

Quoted 110 lines

diff -urN lm_sensors2/CHANGES lm_sensors2-new/CHANGES

CHANGES +1 -0
@@ -31,6 +31,7 @@
                    Drop linux 2.2 support
                    Add w83627hf
   Program sensors: Do not show algorithm by default
+                   Fix output to UTF-8 console
   Program sensors-detect: Fix PATH issues
 
 

diff -urN lm_sensors2/prog/sensors/chips.c
lm_sensors2-new/prog/sensors/chips.c---
lm_sensors2/prog/sensors/chips.c 2004-01-27 18:19:03.000000000 +0100+++
lm_sensors2-new/prog/sensors/chips.c 2004-01-30 12:59:05.000000000 +0100@@ -34,6 +34,7 @@
static inline float deg_ctof( float );

extern int fahrenheit;

CHANGES +17 -4
+extern int utf8;
 
 char *spacestr(int n)
 {
@@ -65,12 +66,18 @@
    char degv[5];
 
    if (fahrenheit) {
-      sprintf(degv, "%cF", 176);
+      if (utf8)
+        sprintf(degv, "%c%cF", 0xc2, 0xb0);
+      else
+        sprintf(degv, "%cF", 176);
       n_cur  = deg_ctof(n_cur);
       n_over = deg_ctof(n_over);
       n_hyst = deg_ctof(n_hyst);
    } else {
-      sprintf(degv, "%cC", 176);
+      if (utf8)
+        sprintf(degv, "%c%cC", 0xc2, 0xb0);
+      else
+        sprintf(degv, "%cC", 176);
    }
 
 /* use %* to pass precision as an argument */
@@ -4065,13 +4072,19 @@
 	char suffix[5];
 
 	if (fahrenheit) {
-		sprintf (suffix,"%cF",176);
+		if (utf8)
+			sprintf (suffix,"%c%cF",0xc2,0xb0);
+		else
+			sprintf (suffix,"%cF",176);
 		n_cur = deg_ctof (n_cur);
 		n_high = deg_ctof (n_high);
 		n_low = deg_ctof (n_low);
 		n_crit = deg_ctof (n_crit);
 		n_hyst = deg_ctof (n_hyst);
-	} else sprintf (suffix,"%cC",176);
+	} else if (utf8)
+		sprintf (suffix,"%c%cC",0xc2,0xb0);
+	else
+		sprintf (suffix,"%cC",176);
 
 	printf ("%+6.4f%s (high = %+6.4f%s, low = %+6.4f%s, crit >  	%+6.4f%s, hyst = %+6.4f%s)",
 			n_cur,suffix,

diff -urN lm_sensors2/prog/sensors/main.c
lm_sensors2-new/prog/sensors/main.c--- lm_sensors2/prog/sensors/main.c 2004-01-25
19:56:52.000000000 +0100+++ lm_sensors2-new/prog/sensors/main.c 2004-01-30 12:59:05.000000000 +0100

CHANGES +3 -0
@@ -22,6 +22,8 @@
 #include <getopt.h>
 #include <string.h>
 #include <errno.h>
+#include <locale.h>
+#include <langinfo.h>
 
 #include "lib/sensors.h" 
 #include "lib/error.h"
@@ -53,6 +55,7 @@
 sensors_chip_name chips[CHIPS_MAX];
 int chips_count=0;
 int do_sets, do_unknown, fahrenheit, show_algorithm, hide_adapter,
 hide_unknown;
+int utf8;
 
 void print_short_help(void)

{

CHANGES +7 -0
@@ -167,6 +170,13 @@
     { 0,0,0,0 }
   };
 
+  setlocale(LC_CTYPE, "");
+
+  if (strcmp(nl_langinfo(CODESET), "UTF-8") = 0)
+    utf8 = 1;
+  else
+    utf8 = 0;
+
   do_unknown = 0;
   do_sets = 0;
   show_algorithm = 0;
Signature · 5 lines
-- 
Marcel Sebek
jabber: [email protected]                     ICQ: 279852819
linux user number: 307850                 GPG ID: 5F88735E
GPG FP: 0F01 BAB8 3148 94DB B95D  1FCA 8B63 CA06 5F88 735E
Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/
Applied
#3 lore raw

PATCH 1/1 UTF-8 fix

In reply to Marcel Sebek
Quoted 1 line

OK, here's new patch against latest CVS tree.

Thanks, applied.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/