annotate lib-src/mule/coco.c @ 160:1c55655d6702

Added tag r20-3b6 for changeset 3bb7ccffb0c0
author cvs
date Mon, 13 Aug 2007 09:41:47 +0200
parents 131b0175ea99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
1 #include <stdio.h>
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
2 #include "mulelib.h"
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
3
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
4 #define IN_BUF_SIZE 4096
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
5 #define OUT_BUF_SIZE (IN_BUF_SIZE * 5 + CONV_BUF_EXTRA)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
6
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
7 static char *coco_version = "2.2";
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
8
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
9 static int
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
10 strpcmp(p0, p1)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
11 char **p0, **p1;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
12 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
13 return strcmp(*p0, *p1);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
14 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
15
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
16 coco_list(stream)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
17 FILE *stream;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
18 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
19 int i, j, k, clm, len, maxlen;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
20 char **names;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
21
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
22 names = (char **)malloc((sizeof names[0]) * n_base_coding_system);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
23 maxlen = 0;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
24 for (i = 0; i < n_base_coding_system; i++) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
25 names[i] = coding_system_table[i].name;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
26 len = strlen(names[i]);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
27 if (len > maxlen) maxlen = len;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
28 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
29 qsort (names, n_base_coding_system, sizeof names[0], strpcmp);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
30
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
31 maxlen += 2;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
32 clm = 79 / maxlen;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
33 k = n_base_coding_system / clm;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
34 if (n_base_coding_system % clm) k++;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
35 fprintf (stream, "You can specify the following coding-systems:\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
36 for (i = 0; i < k; i++) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
37 len = 0;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
38 for (j = 0; j < clm; j++) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
39 if (i + j * k < n_base_coding_system) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
40 if (len < maxlen * j)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
41 while (len < maxlen * j) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
42 putc(' ', stream);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
43 len++;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
44 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
45 fprintf (stream, " %s", names[i + j * k]);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
46 len += strlen(names[i + j * k]) + 2;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
47 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
48 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
49 putc ('\n', stream);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
50 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
51 fprintf (stream, "You can add \"unix*\", \"dos*\", or \"mac*\"");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
52 fprintf (stream, " at the tail of these names\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
53 fprintf (stream, " to specify type of end-of-line");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
54 fprintf (stream, "(LF[default], CRLF, or CR).\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
55
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
56 free(names);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
57
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
58 return 0;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
59 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
60
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
61 main(argc, argv)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
62 int argc;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
63 char **argv;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
64 {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
65 int help = 0, list = 0, verbose = 0, query = 0;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
66 int in_total = 0, out_total = 0, n;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
67 char inbuf[IN_BUF_SIZE], outbuf[OUT_BUF_SIZE];
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
68 char *inname = NULL, *outname = NULL;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
69 coding_type incode, outcode;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
71 if (mulelib_initialize(argc, argv, NULL, NULL) < 0) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
72 fprintf(stderr, "Initialization of Mule libarary failed.\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
73 exit(1);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
74 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
75
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
76 while (--argc > 0) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
77 argv++;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
78 if (!strcmp(*argv, "-h")) { help = list = 1; break; }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
79 if (!strcmp(*argv, "-l")) { list = 1; break; }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
80 if (!strcmp(*argv, "-v")) { verbose = 1; continue; }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
81 if (!strcmp(*argv, "-q")) { query = 1; continue; }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
82 if (!query) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
83 if (outname != NULL) inname = outname;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
84 outname = *argv;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
85 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
86 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
87
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
88 if (help) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
89 printf("Version %s (mulelib version %s)\n",
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
90 coco_version, mule_library_version);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
91 printf("Usage: coco [-h|-l|-v|-q] [[INCODE] OUTCODE] <INFILE >OUTFILE\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
92 printf(" -h: print this message\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
93 printf(" -l: list valid INCODE and OUTCODE\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
94 printf(" -v: print information about conversion\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
95 printf(" -q: just investigate how INFILE is encoded\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
96 printf(" INCODE: coding-system of INFILE, defaults to `*autoconv*'\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
97 printf(" OUTCODE: coding-system of OUTFILE, defaults to `*internal*'\n");
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
98 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
99
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
100 if (list) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
101 coco_list(stdout);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
102 exit(0);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
103 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
104
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
105 if (set_coding_system(inname, &incode, outname, &outcode) < 0) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
106 fprintf(stderr, "%s\n", mule_error_msg);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
107 exit(1);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
108 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
109
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
110 while ((n = read(0, inbuf, sizeof inbuf)) > 0) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
111 in_total += n;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
112 if ((n = code_conversion(&incode, inbuf, n, &outcode, outbuf,
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
113 OUT_BUF_SIZE)) < 0) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
114 if (!query) write(1, outbuf, OUT_BUF_SIZE);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
115 exit(1);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
116 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
117 if (!query && n > 0) write(1, outbuf, n);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
118 out_total += n;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
119 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
120
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
121 if (query) {
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
122 fprintf(stderr, "In (%s): %6d bytes\n", incode.name, in_total);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
123 exit(0);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
124 }
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
125
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
126 /* Flush out an unfinished state (e.g. terminating with "ESC ( B"). */
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
127 n = code_conversion(&incode, inbuf, 0, &outcode, outbuf, OUT_BUF_SIZE);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
128 if (n > 0) write(1, outbuf, n);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
129 out_total += n;
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
130
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
131 if (verbose)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
132 fprintf(stderr, "In :%6d bytes of %s\nOut:%6d bytes of %s\n",
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
133 in_total, incode.name, out_total, outcode.name);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
134 exit(0);
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
135 }