+static char **opt_cmd = NULL;
+static char *opt_title = NULL;
+static char *opt_embed = NULL;
+static char *opt_class = NULL;
+
+int
+utf8decode(char *s, long *u) {
+ uchar c;
+ int i, n, rtn;
+
+ rtn = 1;
+ c = *s;
+ if(~c & B7) { /* 0xxxxxxx */
+ *u = c;
+ return rtn;
+ } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
+ *u = c&(B4|B3|B2|B1|B0);
+ n = 1;
+ } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
+ *u = c&(B3|B2|B1|B0);
+ n = 2;
+ } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
+ *u = c & (B2|B1|B0);
+ n = 3;
+ } else
+ goto invalid;
+ for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
+ c = *s;
+ if((c & (B7|B6)) != B7) /* 10xxxxxx */
+ goto invalid;
+ *u <<= 6;
+ *u |= c & (B5|B4|B3|B2|B1|B0);
+ }
+ if((n == 1 && *u < 0x80) ||
+ (n == 2 && *u < 0x800) ||
+ (n == 3 && *u < 0x10000) ||
+ (*u >= 0xD800 && *u <= 0xDFFF))
+ goto invalid;
+ return rtn;
+invalid:
+ *u = 0xFFFD;
+ return rtn;
+}
+
+int
+utf8encode(long *u, char *s) {
+ uchar *sp;
+ ulong uc;
+ int i, n;
+
+ sp = (uchar*) s;
+ uc = *u;
+ if(uc < 0x80) {
+ *sp = uc; /* 0xxxxxxx */
+ return 1;
+ } else if(*u < 0x800) {
+ *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
+ n = 1;
+ } else if(uc < 0x10000) {
+ *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
+ n = 2;
+ } else if(uc <= 0x10FFFF) {
+ *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
+ n = 3;
+ } else {
+ goto invalid;
+ }
+ for(i=n,++sp; i>0; --i,++sp)
+ *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
+ return n+1;
+invalid:
+ /* U+FFFD */
+ *s++ = '\xEF';
+ *s++ = '\xBF';
+ *s = '\xBD';
+ return 3;
+}
+
+/* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
+ UTF-8 otherwise return 0 */
+int
+isfullutf8(char *s, int b) {
+ uchar *c1, *c2, *c3;
+
+ c1 = (uchar *) s;
+ c2 = (uchar *) ++s;
+ c3 = (uchar *) ++s;
+ if(b < 1)
+ return 0;
+ else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1)
+ return 0;
+ else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
+ ((b == 1) ||
+ ((b == 2) && (*c2&(B7|B6)) == B7)))
+ return 0;
+ else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
+ ((b == 1) ||
+ ((b == 2) && (*c2&(B7|B6)) == B7) ||
+ ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7)))
+ return 0;
+ else
+ return 1;
+}
+
+int
+utf8size(char *s) {
+ uchar c = *s;
+
+ if(~c&B7)
+ return 1;
+ else if((c&(B7|B6|B5)) == (B7|B6))
+ return 2;
+ else if((c&(B7|B6|B5|B4)) == (B7|B6|B5))
+ return 3;
+ else
+ return 4;
+}