[Midnightbsd-cvs] src [6824] trunk/bin/chio/chio.c: teach about utf8
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Fri Oct 3 00:12:00 EDT 2014
Revision: 6824
http://svnweb.midnightbsd.org/src/?rev=6824
Author: laffer1
Date: 2014-10-03 00:12:00 -0400 (Fri, 03 Oct 2014)
Log Message:
-----------
teach about utf8
Modified Paths:
--------------
trunk/bin/chio/chio.c
Modified: trunk/bin/chio/chio.c
===================================================================
--- trunk/bin/chio/chio.c 2014-10-03 04:10:32 UTC (rev 6823)
+++ trunk/bin/chio/chio.c 2014-10-03 04:12:00 UTC (rev 6824)
@@ -1,4 +1,4 @@
-/* $MidnightBSD: src/bin/chio/chio.c,v 1.3 2007/07/26 20:12:59 laffer1 Exp $ */
+/* $MidnightBSD$ */
/* $NetBSD: chio.c,v 1.6 1998/01/04 23:53:58 thorpej Exp $ */
/*-
* Copyright (c) 1996 Jason R. Thorpe <thorpej at and.com>
@@ -55,6 +55,8 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <langinfo.h>
+#include <locale.h>
#include "defs.h"
#include "pathnames.h"
@@ -82,6 +84,7 @@
static int do_ielem(const char *, int, char **);
static int do_return(const char *, int, char **);
static int do_voltag(const char *, int, char **);
+static void print_designator(const char *, u_int8_t, u_int8_t);
#ifndef CHET_VT
#define CHET_VT 10 /* Completely Arbitrary */
@@ -724,6 +727,10 @@
putchar('?');
putchar('>');
}
+ if (ces->ces_designator_length > 0)
+ print_designator(ces->ces_designator,
+ ces->ces_code_set,
+ ces->ces_designator_length);
putchar('\n');
}
@@ -1178,3 +1185,66 @@
"arg1 arg2 [arg3 [...]]\n", getprogname());
exit(1);
}
+
+#define UTF8CODESET "UTF-8"
+
+static void
+print_designator(const char *designator, u_int8_t code_set,
+ u_int8_t designator_length)
+{
+ printf(" serial number: <");
+ switch (code_set) {
+ case CES_CODE_SET_ASCII: {
+ /*
+ * The driver insures that the string is always NUL terminated.
+ */
+ printf("%s", designator);
+ break;
+ }
+ case CES_CODE_SET_UTF_8: {
+ char *cs_native;
+
+ setlocale(LC_ALL, "");
+ cs_native = nl_langinfo(CODESET);
+
+ /* See if we can natively print UTF-8 */
+ if (strcmp(cs_native, UTF8CODESET) == 0)
+ cs_native = NULL;
+
+ if (cs_native == NULL) {
+ /* We can natively print UTF-8, so use printf. */
+ printf("%s", designator);
+ } else {
+ int i;
+
+ /*
+ * We can't natively print UTF-8. We should
+ * convert it to the terminal's codeset, but that
+ * requires iconv(3) and FreeBSD doesn't have
+ * iconv(3) in the base system yet. So we use %XX
+ * notation for non US-ASCII characters instead.
+ */
+ for (i = 0; i < designator_length &&
+ designator[i] != '\0'; i++) {
+ if ((unsigned char)designator[i] < 0x80)
+ printf("%c", designator[i]);
+ else
+ printf("%%%02x",
+ (unsigned char)designator[i]);
+ }
+ }
+ break;
+ }
+ case CES_CODE_SET_BINARY: {
+ int i;
+
+ for (i = 0; i < designator_length; i++)
+ printf("%02X%s", designator[i],
+ (i == designator_length - 1) ? "" : " ");
+ break;
+ }
+ default:
+ break;
+ }
+ printf(">");
+}
More information about the Midnightbsd-cvs
mailing list