diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/utf2any/extra |
Initial commit
Diffstat (limited to 'support/utf2any/extra')
-rw-r--r-- | support/utf2any/extra/README | 5 | ||||
-rw-r--r-- | support/utf2any/extra/utf7.l | 261 | ||||
-rw-r--r-- | support/utf2any/extra/utf8.l | 297 |
3 files changed, 563 insertions, 0 deletions
diff --git a/support/utf2any/extra/README b/support/utf2any/extra/README new file mode 100644 index 0000000000..15ef8045ec --- /dev/null +++ b/support/utf2any/extra/README @@ -0,0 +1,5 @@ + +Here are just two predecessors to utf2any. These programs are very +simple and can be used by hackers who wish to write their own UTF +application. + diff --git a/support/utf2any/extra/utf7.l b/support/utf2any/extra/utf7.l new file mode 100644 index 0000000000..204d8afc70 --- /dev/null +++ b/support/utf2any/extra/utf7.l @@ -0,0 +1,261 @@ +/* file: utf7.l */ + +%{ + +#ifdef __MSDOS__ +# include <dir.h> +# include <fcntl.h> +# include <io.h> +#else +# include <unistd.h> +#endif +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> + +int + printcode = 0; + +unsigned int + outcode [2], + instep, + outstep; + +char + *programname; + +void + get_programname (char const *argv0), + syntax (void), + errit (char const *format, ...), + utf (void), + nextout (void), + outchar (long unsigned); + +#ifdef __MSDOS__ +#define strcasecmp(s1, s2) (stricmp(s1, s2)) +#endif + +#define YY_NO_UNPUT +#define YY_SKIP_YYWRAP +#ifdef yywrap +# undef yywrap +#endif +int yywrap() +{ + return 1; +} + +%} + +%Start _utf + +%% + +<INITIAL>"+-" { fputc ('+', stdout); } +<INITIAL>"+" { instep = outstep = 0; + BEGIN _utf; } + +<_utf>[A-Za-z0-9+/] { utf (); } +<_utf>"-" { BEGIN INITIAL; } +<_utf>.|\n { fputc (yytext [0], stdout); + BEGIN INITIAL; } + +%% + +void utf () +{ + unsigned + i, + c; + + i = yytext [0]; + if (i >= 'A' && i <= 'Z') + c = i - 'A'; + else if (i >= 'a' && i <= 'z') + c = i + 26 - 'a'; + else if (i >= '0' && i <= '9') + c = i + 52 - '0'; + else if (i == '+') + c = 62; + else if (i == '/') + c = 63; + + switch (instep) { + case 0: + outcode [outstep] = (c << 2); + break; + case 1: + outcode [outstep] |= (c >> 4); + nextout (); + outcode [outstep] = (c << 4); + break; + case 2: + outcode [outstep] |= (c >> 2); + nextout (); + outcode [outstep] = (c << 6); + break; + case 3: + outcode [outstep] |= c; + nextout (); + break; + } + if (++instep == 4) + instep = 0; +} + +void nextout () +{ + unsigned + c; + + if (outstep == 0) { + outstep = 1; + } else { + outstep = 0; + c = ((outcode [0] & 0xFF) << 8) | (outcode [1] & 0xFF); + outchar (c); + } +} + +void outchar (long unsigned c) +{ + int + i; + char + *s; + + /* + * iso-8859-1 + */ + if (c < 256) { + fputc (c, stdout); + return; + } + + /* + * iso-8859-15 + */ + i = 0; + switch (c) { + case 0x20Ac: i = 0xA4; break; /* euro */ + case 0x0160: i = 0xA6; break; /* S caron */ + case 0x0161: i = 0xA8; break; /* s caron */ + case 0x017D: i = 0xB4; break; /* Z caron */ + case 0x017E: i = 0xB8; break; /* z caron */ + case 0x0152: i = 0xBC; break; /* OE ligature */ + case 0x0153: i = 0xBD; break; /* oe ligature */ + case 0x0178: i = 0xBE; break; /* Y diaeresis */ + } + if (i) { + fputc (i, stdout); + return; + } + + /* + * substitutions + */ + s = NULL; + switch (c) { + case 0x0132: s = "IJ"; break; + case 0x0133: s = "ij"; break; + } + if (s) { + fputs (s, stdout); + return; + } + + if (printcode) { + if (c < 0x10000) + printf ("U+%04X", (unsigned) c); + else + printf ("U+%08lX", c); + } else + fputc (191, stdout); +} + +int main (int argc, char *argv []) +{ + get_programname (argv [0]); + + while (argc > 1) + if (! strcmp (argv [1], "-c")) { + printcode = 1; + argv++; + argc--; + } else + break; + + switch (argc) { + case 1: + if (isatty (fileno (stdin))) + syntax (); + yyin = stdin; + break; + case 2: + yyin = fopen (argv [1], "r"); + if (! yyin) + errit ("Opening file \"%s\": %s", argv [1], strerror (errno)); + break; + default: + syntax (); + } + + yylex (); + + if (yyin != stdin) + fclose (yyin); + + return 0; +} + +void get_programname (char const *argv0) +{ +#ifdef __MSDOS__ + char + name [MAXFILE]; + fnsplit (argv0, NULL, NULL, name, NULL); + programname = strdup (name); +#else /* unix */ + char + *p; + p = strrchr (argv0, '/'); + if (p) + programname = strdup (p + 1); + else + programname = strdup (argv0); +#endif +} + +void errit (char const *format, ...) +{ + va_list + list; + + fprintf (stderr, "\nError %s: ", programname); + + va_start (list, format); + vfprintf (stderr, format, list); + + fprintf (stderr, "\n\n"); + + exit (1); +} + +void syntax () +{ + fprintf ( + stderr, + "\n" + "Syntax: %s [-c] [utf-7 encoded file]\n" + "\n" + "The file will be translated to iso-8859-1 *and* iso-8859-15\n" + "\n" + " -c : print U+code for characters not in iso-8859-1/15\n" + "\n", + programname + ); + + exit (1); +} diff --git a/support/utf2any/extra/utf8.l b/support/utf2any/extra/utf8.l new file mode 100644 index 0000000000..49134feab4 --- /dev/null +++ b/support/utf2any/extra/utf8.l @@ -0,0 +1,297 @@ +/* + * file: utf8.l + * + * required flex option: + * + * -8 (generate 8bit scanner) + */ + +%{ + +#ifdef __MSDOS__ +# include <dir.h> +# include <fcntl.h> +# include <io.h> +#else +# include <unistd.h> +#endif +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> + +int + printcode = 0; + +char + *programname; + +void + get_programname (char const *argv0), + syntax (void), + errit (char const *format, ...), + bytes2 (void), + bytes3 (void), + bytes4 (void), + bytes5 (void), + bytes6 (void), + outchar (long unsigned); + +#define YY_NO_UNPUT +#define YY_SKIP_YYWRAP +#ifdef yywrap +# undef yywrap +#endif +int yywrap() +{ + return 1; +} + +%} + +%% + +[\300-\337]. { bytes2 (); } +[\340-\357].. { bytes3 (); } +[\360-\367]... { bytes4 (); } +[\370-\373].... { bytes5 (); } +[\374-\375]..... { bytes6 (); } + +%% + +void bytes2 () +{ + unsigned + u [2], + c; + int + i; + + for (i = 0; i < 2; i++) + u [i] = (unsigned char) yytext [i]; + + c = ( u [1] & 0x3F) + | ((u [0] & 0x1F) << 6); + + outchar (c); +} + + +void bytes3 () +{ + unsigned + u [3], + c; + int + i; + + for (i = 0; i < 3; i++) + u [i] = (unsigned char) yytext [i]; + + c = ( u [2] & 0x3F) + | ((u [1] & 0x3F) << 6) + | ((u [0] & 0x0F) << 12); + + outchar (c); +} + +void bytes4 () +{ + long unsigned + u [4], + c; + int + i; + + for (i = 0; i < 4; i++) + u [i] = (unsigned char) yytext [i]; + + c = ( u [3] & 0x3F) + | ((u [2] & 0x3F) << 6) + | ((u [1] & 0x3F) << 12) + | ((u [0] & 0x07) << 18); + + outchar (c); +} + +void bytes5 () +{ + long unsigned + u [5], + c; + int + i; + + for (i = 0; i < 5; i++) + u [i] = (unsigned char) yytext [i]; + + c = ( u [4] & 0x3F) + | ((u [3] & 0x3F) << 6) + | ((u [2] & 0x3F) << 12) + | ((u [1] & 0x3F) << 18) + | ((u [0] & 0x03) << 24); + + outchar (c); +} + +void bytes6 () +{ + long unsigned + u [6], + c; + int + i; + + for (i = 0; i < 6; i++) + u [i] = (unsigned char) yytext [i]; + + c = ( u [5] & 0x3F) + | ((u [4] & 0x3F) << 6) + | ((u [3] & 0x3F) << 12) + | ((u [2] & 0x3F) << 18) + | ((u [1] & 0x3F) << 24) + | ((u [0] & 0x01) << 30); + + outchar (c); +} + +void outchar (long unsigned c) +{ + int + i; + char + *s; + + /* + * iso-8859-1 + */ + if (c < 256) { + fputc (c, stdout); + return; + } + + /* + * iso-8859-15 + */ + i = 0; + switch (c) { + case 0x20Ac: i = 0xA4; break; /* euro */ + case 0x0160: i = 0xA6; break; /* S caron */ + case 0x0161: i = 0xA8; break; /* s caron */ + case 0x017D: i = 0xB4; break; /* Z caron */ + case 0x017E: i = 0xB8; break; /* z caron */ + case 0x0152: i = 0xBC; break; /* OE ligature */ + case 0x0153: i = 0xBD; break; /* oe ligature */ + case 0x0178: i = 0xBE; break; /* Y diaeresis */ + } + if (i) { + fputc (i, stdout); + return; + } + + /* + * substitutions + */ + s = NULL; + switch (c) { + case 0x0132: s = "IJ"; break; + case 0x0133: s = "ij"; break; + } + if (s) { + fputs (s, stdout); + return; + } + + if (printcode) { + if (c < 0x10000) + printf ("U+%04X", (unsigned) c); + else + printf ("U+%08lX", c); + } else + fputc (191, stdout); +} + +int main (int argc, char *argv []) +{ + get_programname (argv [0]); + + while (argc > 1) + if (! strcmp (argv [1], "-c")) { + printcode = 1; + argv++; + argc--; + } else + break; + + switch (argc) { + case 1: + if (isatty (fileno (stdin))) + syntax (); + yyin = stdin; + break; + case 2: + yyin = fopen (argv [1], "r"); + if (! yyin) + errit ("Opening file \"%s\": %s", argv [1], strerror (errno)); + break; + default: + syntax (); + } + + yylex (); + + if (yyin != stdin) + fclose (yyin); + + return 0; +} + +void get_programname (char const *argv0) +{ +#ifdef __MSDOS__ + char + name [MAXFILE]; + fnsplit (argv0, NULL, NULL, name, NULL); + programname = strdup (name); +#else /* unix */ + char + *p; + p = strrchr (argv0, '/'); + if (p) + programname = strdup (p + 1); + else + programname = strdup (argv0); +#endif +} + +void errit (char const *format, ...) +{ + va_list + list; + + fprintf (stderr, "\nError %s: ", programname); + + va_start (list, format); + vfprintf (stderr, format, list); + + fprintf (stderr, "\n\n"); + + exit (1); +} + +void syntax () +{ + fprintf ( + stderr, + "\n" + "Syntax: %s [-c] [utf-8 encoded file]\n" + "\n" + "The file will be translated to iso-8859-1 *and* iso-8859-15\n" + "\n" + " -c : print U+code for characters not in iso-8859-1/15\n" + "\n", + programname + ); + + exit (1); +} |