]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/tokyocabinet/tcucodec.c
Purged was missed, Adding here.
[bacula/bacula] / bacula / src / lib / tokyocabinet / tcucodec.c
1 /*************************************************************************************************
2  * Popular encoders and decoders of the utility API
3  *                                                      Copyright (C) 2006-2008 Mikio Hirabayashi
4  * This file is part of Tokyo Cabinet.
5  * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
6  * the GNU Lesser General Public License as published by the Free Software Foundation; either
7  * version 2.1 of the License or any later version.  Tokyo Cabinet is distributed in the hope
8  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
10  * License for more details.
11  * You should have received a copy of the GNU Lesser General Public License along with Tokyo
12  * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
13  * Boston, MA 02111-1307 USA.
14  *************************************************************************************************/
15
16
17 #include <tcutil.h>
18 #include "myconf.h"
19
20
21 /* global variables */
22 const char *g_progname;                  // program name
23
24
25 /* function prototypes */
26 int main(int argc, char **argv);
27 static void usage(void);
28 static void eprintf(const char *format, ...);
29 static int runurl(int argc, char **argv);
30 static int runbase(int argc, char **argv);
31 static int runquote(int argc, char **argv);
32 static int runmime(int argc, char **argv);
33 static int runpack(int argc, char **argv);
34 static int runtcbs(int argc, char **argv);
35 static int runzlib(int argc, char **argv);
36 static int runxml(int argc, char **argv);
37 static int runucs(int argc, char **argv);
38 static int rundate(int argc, char **argv);
39 static int runconf(int argc, char **argv);
40 static int procurl(const char *ibuf, int isiz, bool dec, bool br, const char *base);
41 static int procbase(const char *ibuf, int isiz, bool dec);
42 static int procquote(const char *ibuf, int isiz, bool dec);
43 static int procmime(const char *ibuf, int isiz, bool dec, const char *ename, bool qb, bool on);
44 static int procpack(const char *ibuf, int isiz, bool dec, bool bwt);
45 static int proctcbs(const char *ibuf, int isiz, bool dec);
46 static int proczlib(const char *ibuf, int isiz, bool dec, bool gz);
47 static int procxml(const char *ibuf, int isiz, bool dec, bool br);
48 static int procucs(const char *ibuf, int isiz, bool dec);
49 static int procdate(const char *str, int jl, bool wf, bool rf);
50 static int procconf(int mode);
51
52
53 /* main routine */
54 int main(int argc, char **argv){
55   g_progname = argv[0];
56   if(argc < 2) usage();
57   int rv = 0;
58   if(!strcmp(argv[1], "url")){
59     rv = runurl(argc, argv);
60   } else if(!strcmp(argv[1], "base")){
61     rv = runbase(argc, argv);
62   } else if(!strcmp(argv[1], "quote")){
63     rv = runquote(argc, argv);
64   } else if(!strcmp(argv[1], "mime")){
65     rv = runmime(argc, argv);
66   } else if(!strcmp(argv[1], "pack")){
67     rv = runpack(argc, argv);
68   } else if(!strcmp(argv[1], "tcbs")){
69     rv = runtcbs(argc, argv);
70   } else if(!strcmp(argv[1], "zlib")){
71     rv = runzlib(argc, argv);
72   } else if(!strcmp(argv[1], "xml")){
73     rv = runxml(argc, argv);
74   } else if(!strcmp(argv[1], "ucs")){
75     rv = runucs(argc, argv);
76   } else if(!strcmp(argv[1], "date")){
77     rv = rundate(argc, argv);
78   } else if(!strcmp(argv[1], "conf")){
79     rv = runconf(argc, argv);
80   } else {
81     usage();
82   }
83   return rv;
84 }
85
86
87 /* print the usage and exit */
88 static void usage(void){
89   fprintf(stderr, "%s: popular encoders and decoders of Tokyo Cabinet\n", g_progname);
90   fprintf(stderr, "\n");
91   fprintf(stderr, "usage:\n");
92   fprintf(stderr, "  %s url [-d] [-br] [-rs base] [file]\n", g_progname);
93   fprintf(stderr, "  %s base [-d] [file]\n", g_progname);
94   fprintf(stderr, "  %s quote [-d] [file]\n", g_progname);
95   fprintf(stderr, "  %s mime [-d] [-en name] [-q] [file]\n", g_progname);
96   fprintf(stderr, "  %s pack [-d] [-bwt] [file]\n", g_progname);
97   fprintf(stderr, "  %s tcbs [-d] [file]\n", g_progname);
98   fprintf(stderr, "  %s zlib [-d] [-gz] [file]\n", g_progname);
99   fprintf(stderr, "  %s xml [-d] [-br] [file]\n", g_progname);
100   fprintf(stderr, "  %s ucs [-d] [file]\n", g_progname);
101   fprintf(stderr, "  %s date [-ds str] [-jl num] [-wf] [-rf]\n", g_progname);
102   fprintf(stderr, "  %s conf [-v|-i|-l|-p]\n", g_progname);
103   fprintf(stderr, "\n");
104   exit(1);
105 }
106
107
108 /* print formatted error string */
109 static void eprintf(const char *format, ...){
110   va_list ap;
111   va_start(ap, format);
112   fprintf(stderr, "%s: ", g_progname);
113   vfprintf(stderr, format, ap);
114   fprintf(stderr, "\n");
115   va_end(ap);
116 }
117
118
119 /* parse arguments of url command */
120 static int runurl(int argc, char **argv){
121   char *path = NULL;
122   bool dec = false;
123   bool br = false;
124   char *base = NULL;
125   for(int i = 2; i < argc; i++){
126     if(!path && argv[i][0] == '-'){
127       if(!strcmp(argv[i], "-d")){
128         dec = true;
129       } else if(!strcmp(argv[i], "-br")){
130         br = true;
131       } else if(!strcmp(argv[i], "-rs")){
132         if(++i >= argc) usage();
133         base = argv[i];
134       } else {
135         usage();
136       }
137     } else if(!path){
138       path = argv[i];
139     } else {
140       usage();
141     }
142   }
143   char *ibuf;
144   int isiz;
145   if(path && path[0] == '@'){
146     isiz = strlen(path) - 1;
147     ibuf = tcmemdup(path + 1, isiz);
148   } else {
149     ibuf = tcreadfile(path, -1, &isiz);
150   }
151   if(!ibuf){
152     eprintf("%s: cannot open", path ? path : "(stdin)");
153     return 1;
154   }
155   int rv = procurl(ibuf, isiz, dec, br, base);
156   if(path && path[0] == '@' && !br) printf("\n");
157   free(ibuf);
158   return rv;
159 }
160
161
162 /* parse arguments of base command */
163 static int runbase(int argc, char **argv){
164   char *path = NULL;
165   bool dec = false;
166   for(int i = 2; i < argc; i++){
167     if(!path && argv[i][0] == '-'){
168       if(!strcmp(argv[i], "-d")){
169         dec = true;
170       } else {
171         usage();
172       }
173     } else if(!path){
174       path = argv[i];
175     } else {
176       usage();
177     }
178   }
179   char *ibuf;
180   int isiz;
181   if(path && path[0] == '@'){
182     isiz = strlen(path) - 1;
183     ibuf = tcmemdup(path + 1, isiz);
184   } else {
185     ibuf = tcreadfile(path, -1, &isiz);
186   }
187   if(!ibuf){
188     eprintf("%s: cannot open", path ? path : "(stdin)");
189     return 1;
190   }
191   int rv = procbase(ibuf, isiz, dec);
192   if(path && path[0] == '@') printf("\n");
193   free(ibuf);
194   return rv;
195 }
196
197
198 /* parse arguments of quote command */
199 static int runquote(int argc, char **argv){
200   char *path = NULL;
201   bool dec = false;
202   for(int i = 2; i < argc; i++){
203     if(!path && argv[i][0] == '-'){
204       if(!strcmp(argv[i], "-d")){
205         dec = true;
206       } else {
207         usage();
208       }
209     } else if(!path){
210       path = argv[i];
211     } else {
212       usage();
213     }
214   }
215   char *ibuf;
216   int isiz;
217   if(path && path[0] == '@'){
218     isiz = strlen(path) - 1;
219     ibuf = tcmemdup(path + 1, isiz);
220   } else {
221     ibuf = tcreadfile(path, -1, &isiz);
222   }
223   if(!ibuf){
224     eprintf("%s: cannot open", path ? path : "(stdin)");
225     return 1;
226   }
227   int rv = procquote(ibuf, isiz, dec);
228   if(path && path[0] == '@') printf("\n");
229   free(ibuf);
230   return rv;
231 }
232
233
234 /* parse arguments of mime command */
235 static int runmime(int argc, char **argv){
236   char *path = NULL;
237   bool dec = false;
238   char *ename = NULL;
239   bool qb = false;
240   bool on = false;
241   for(int i = 2; i < argc; i++){
242     if(!path && argv[i][0] == '-'){
243       if(!strcmp(argv[i], "-d")){
244         dec = true;
245       } else if(!strcmp(argv[i], "-en")){
246         if(++i >= argc) usage();
247         ename = argv[i];
248       } else if(!strcmp(argv[i], "-q")){
249         qb = true;
250       } else if(!strcmp(argv[i], "-on")){
251         on = true;
252       } else {
253         usage();
254       }
255     } else if(!path){
256       path = argv[i];
257     } else {
258       usage();
259     }
260   }
261   char *ibuf;
262   int isiz;
263   if(path && path[0] == '@'){
264     isiz = strlen(path) - 1;
265     ibuf = tcmemdup(path + 1, isiz);
266   } else {
267     ibuf = tcreadfile(path, -1, &isiz);
268   }
269   if(!ibuf){
270     eprintf("%s: cannot open", path ? path : "(stdin)");
271     return 1;
272   }
273   if(!ename) ename = "UTF-8";
274   int rv = procmime(ibuf, isiz, dec, ename, qb, on);
275   if(path && path[0] == '@') printf("\n");
276   free(ibuf);
277   return rv;
278 }
279
280
281 /* parse arguments of pack command */
282 static int runpack(int argc, char **argv){
283   char *path = NULL;
284   bool dec = false;
285   bool bwt = false;
286   for(int i = 2; i < argc; i++){
287     if(!path && argv[i][0] == '-'){
288       if(!strcmp(argv[i], "-d")){
289         dec = true;
290       } else if(!strcmp(argv[i], "-bwt")){
291         bwt = true;
292       } else {
293         usage();
294       }
295     } else if(!path){
296       path = argv[i];
297     } else {
298       usage();
299     }
300   }
301   char *ibuf;
302   int isiz;
303   if(path && path[0] == '@'){
304     isiz = strlen(path) - 1;
305     ibuf = tcmemdup(path + 1, isiz);
306   } else {
307     ibuf = tcreadfile(path, -1, &isiz);
308   }
309   if(!ibuf){
310     eprintf("%s: cannot open", path ? path : "(stdin)");
311     return 1;
312   }
313   int rv = procpack(ibuf, isiz, dec, bwt);
314   if(path && path[0] == '@') printf("\n");
315   free(ibuf);
316   return rv;
317 }
318
319
320 /* parse arguments of tcbs command */
321 static int runtcbs(int argc, char **argv){
322   char *path = NULL;
323   bool dec = false;
324   for(int i = 2; i < argc; i++){
325     if(!path && argv[i][0] == '-'){
326       if(!strcmp(argv[i], "-d")){
327         dec = true;
328       } else {
329         usage();
330       }
331     } else if(!path){
332       path = argv[i];
333     } else {
334       usage();
335     }
336   }
337   char *ibuf;
338   int isiz;
339   if(path && path[0] == '@'){
340     isiz = strlen(path) - 1;
341     ibuf = tcmemdup(path + 1, isiz);
342   } else {
343     ibuf = tcreadfile(path, -1, &isiz);
344   }
345   if(!ibuf){
346     eprintf("%s: cannot open", path ? path : "(stdin)");
347     return 1;
348   }
349   int rv = proctcbs(ibuf, isiz, dec);
350   if(path && path[0] == '@') printf("\n");
351   free(ibuf);
352   return rv;
353 }
354
355
356 /* parse arguments of zlib command */
357 static int runzlib(int argc, char **argv){
358   char *path = NULL;
359   bool dec = false;
360   bool gz = false;
361   for(int i = 2; i < argc; i++){
362     if(!path && argv[i][0] == '-'){
363       if(!strcmp(argv[i], "-d")){
364         dec = true;
365       } else if(!strcmp(argv[i], "-gz")){
366         gz = true;
367       } else {
368         usage();
369       }
370     } else if(!path){
371       path = argv[i];
372     } else {
373       usage();
374     }
375   }
376   char *ibuf;
377   int isiz;
378   if(path && path[0] == '@'){
379     isiz = strlen(path) - 1;
380     ibuf = tcmemdup(path + 1, isiz);
381   } else {
382     ibuf = tcreadfile(path, -1, &isiz);
383   }
384   if(!ibuf){
385     eprintf("%s: cannot open", path ? path : "(stdin)");
386     return 1;
387   }
388   int rv = proczlib(ibuf, isiz, dec, gz);
389   if(path && path[0] == '@') printf("\n");
390   free(ibuf);
391   return rv;
392 }
393
394
395 /* parse arguments of xml command */
396 static int runxml(int argc, char **argv){
397   char *path = NULL;
398   bool dec = false;
399   bool br = false;
400   for(int i = 2; i < argc; i++){
401     if(!path && argv[i][0] == '-'){
402       if(!strcmp(argv[i], "-d")){
403         dec = true;
404       } else if(!strcmp(argv[i], "-br")){
405         br = true;
406       } else {
407         usage();
408       }
409     } else if(!path){
410       path = argv[i];
411     } else {
412       usage();
413     }
414   }
415   char *ibuf;
416   int isiz;
417   if(path && path[0] == '@'){
418     isiz = strlen(path) - 1;
419     ibuf = tcmemdup(path + 1, isiz);
420   } else {
421     ibuf = tcreadfile(path, -1, &isiz);
422   }
423   if(!ibuf){
424     eprintf("%s: cannot open", path ? path : "(stdin)");
425     return 1;
426   }
427   int rv = procxml(ibuf, isiz, dec, br);
428   if(path && path[0] == '@') printf("\n");
429   free(ibuf);
430   return rv;
431 }
432
433
434 /* parse arguments of ucs command */
435 static int runucs(int argc, char **argv){
436   char *path = NULL;
437   bool dec = false;
438   for(int i = 2; i < argc; i++){
439     if(!path && argv[i][0] == '-'){
440       if(!strcmp(argv[i], "-d")){
441         dec = true;
442       } else {
443         usage();
444       }
445     } else if(!path){
446       path = argv[i];
447     } else {
448       usage();
449     }
450   }
451   char *ibuf;
452   int isiz;
453   if(path && path[0] == '@'){
454     isiz = strlen(path) - 1;
455     ibuf = tcmemdup(path + 1, isiz);
456   } else {
457     ibuf = tcreadfile(path, -1, &isiz);
458   }
459   if(!ibuf){
460     eprintf("%s: cannot open", path ? path : "(stdin)");
461     return 1;
462   }
463   int rv = procucs(ibuf, isiz, dec);
464   if(path && path[0] == '@') printf("\n");
465   free(ibuf);
466   return rv;
467 }
468
469
470 /* parse arguments of date command */
471 static int rundate(int argc, char **argv){
472   char *str = NULL;
473   int jl = INT_MAX;
474   bool wf = false;
475   bool rf = false;
476   for(int i = 2; i < argc; i++){
477     if(argv[i][0] == '-'){
478       if(!strcmp(argv[i], "-ds")){
479         if(++i >= argc) usage();
480         str = argv[i];
481       } else if(!strcmp(argv[i], "-jl")){
482         if(++i >= argc) usage();
483         jl = atoi(argv[i]);
484       } else if(!strcmp(argv[i], "-wf")){
485         wf = true;
486       } else if(!strcmp(argv[i], "-rf")){
487         rf = true;
488       } else {
489         usage();
490       }
491     } else {
492       usage();
493     }
494   }
495   int rv = procdate(str, jl, wf, rf);
496   return rv;
497 }
498
499
500 /* parse arguments of conf command */
501 static int runconf(int argc, char **argv){
502   int mode = 0;
503   for(int i = 2; i < argc; i++){
504     if(argv[i][0] == '-'){
505       if(!strcmp(argv[i], "-v")){
506         mode = 'v';
507       } else if(!strcmp(argv[i], "-i")){
508         mode = 'i';
509       } else if(!strcmp(argv[i], "-l")){
510         mode = 'l';
511       } else if(!strcmp(argv[i], "-p")){
512         mode = 'p';
513       } else {
514         usage();
515       }
516     } else {
517       usage();
518     }
519   }
520   int rv = procconf(mode);
521   return rv;
522 }
523
524
525 /* perform url command */
526 static int procurl(const char *ibuf, int isiz, bool dec, bool br, const char *base){
527   if(base){
528     char *obuf = tcurlresolve(base, ibuf);
529     printf("%s", obuf);
530     free(obuf);
531   } else if(br){
532     TCMAP *elems = tcurlbreak(ibuf);
533     const char *elem;
534     if((elem = tcmapget2(elems, "self")) != NULL) printf("self: %s\n", elem);
535     if((elem = tcmapget2(elems, "scheme")) != NULL) printf("scheme: %s\n", elem);
536     if((elem = tcmapget2(elems, "host")) != NULL) printf("host: %s\n", elem);
537     if((elem = tcmapget2(elems, "port")) != NULL) printf("port: %s\n", elem);
538     if((elem = tcmapget2(elems, "authority")) != NULL) printf("authority: %s\n", elem);
539     if((elem = tcmapget2(elems, "path")) != NULL) printf("path: %s\n", elem);
540     if((elem = tcmapget2(elems, "file")) != NULL) printf("file: %s\n", elem);
541     if((elem = tcmapget2(elems, "query")) != NULL) printf("query: %s\n", elem);
542     if((elem = tcmapget2(elems, "fragment")) != NULL) printf("fragment: %s\n", elem);
543     tcmapdel(elems);
544   } else if(dec){
545     int osiz;
546     char *obuf = tcurldecode(ibuf, &osiz);
547     fwrite(obuf, osiz, 1, stdout);
548     free(obuf);
549   } else {
550     char *obuf = tcurlencode(ibuf, isiz);
551     fwrite(obuf, strlen(obuf), 1, stdout);
552     free(obuf);
553   }
554   return 0;
555 }
556
557
558 /* perform base command */
559 static int procbase(const char *ibuf, int isiz, bool dec){
560   if(dec){
561     int osiz;
562     char *obuf = tcbasedecode(ibuf, &osiz);
563     fwrite(obuf, osiz, 1, stdout);
564     free(obuf);
565   } else {
566     char *obuf = tcbaseencode(ibuf, isiz);
567     fwrite(obuf, strlen(obuf), 1, stdout);
568     free(obuf);
569   }
570   return 0;
571 }
572
573
574 /* perform quote command */
575 static int procquote(const char *ibuf, int isiz, bool dec){
576   if(dec){
577     int osiz;
578     char *obuf = tcquotedecode(ibuf, &osiz);
579     fwrite(obuf, osiz, 1, stdout);
580     free(obuf);
581   } else {
582     char *obuf = tcquoteencode(ibuf, isiz);
583     fwrite(obuf, strlen(obuf), 1, stdout);
584     free(obuf);
585   }
586   return 0;
587 }
588
589
590 /* perform mime command */
591 static int procmime(const char *ibuf, int isiz, bool dec, const char *ename, bool qb, bool on){
592   if(dec){
593     char ebuf[32];
594     char *obuf = tcmimedecode(ibuf, ebuf);
595     if(on){
596       fwrite(ebuf, strlen(ebuf), 1, stdout);
597     } else {
598       fwrite(obuf, strlen(obuf), 1, stdout);
599     }
600     free(obuf);
601   } else {
602     char *obuf = tcmimeencode(ibuf, ename, !qb);
603     fwrite(obuf, strlen(obuf), 1, stdout);
604     free(obuf);
605   }
606   return 0;
607 }
608
609
610 /* perform pack command */
611 static int procpack(const char *ibuf, int isiz, bool dec, bool bwt){
612   if(dec){
613     int osiz;
614     char *obuf = tcpackdecode(ibuf, isiz, &osiz);
615     if(bwt && osiz > 0){
616       int idx, step;
617       TCREADVNUMBUF(obuf, idx, step);
618       char *tbuf = tcbwtdecode(obuf + step, osiz - step, idx);
619       fwrite(tbuf, osiz - step, 1, stdout);
620       free(tbuf);
621     } else {
622       fwrite(obuf, osiz, 1, stdout);
623     }
624     free(obuf);
625   } else {
626     char *tbuf = NULL;
627     if(bwt){
628       int idx;
629       tbuf = tcbwtencode(ibuf, isiz, &idx);
630       char vnumbuf[sizeof(int)+1];
631       int step;
632       TCSETVNUMBUF(step, vnumbuf, idx);
633       tbuf = tcrealloc(tbuf, isiz + step + 1);
634       memmove(tbuf + step, tbuf, isiz);
635       memcpy(tbuf, vnumbuf, step);
636       isiz += step;
637       ibuf = tbuf;
638     }
639     int osiz;
640     char *obuf = tcpackencode(ibuf, isiz, &osiz);
641     fwrite(obuf, osiz, 1, stdout);
642     free(obuf);
643     free(tbuf);
644   }
645   return 0;
646 }
647
648
649 /* perform tcbs command */
650 static int proctcbs(const char *ibuf, int isiz, bool dec){
651   if(dec){
652     int osiz;
653     char *obuf = tcbsdecode(ibuf, isiz, &osiz);
654     fwrite(obuf, osiz, 1, stdout);
655     free(obuf);
656   } else {
657     int osiz;
658     char *obuf = tcbsencode(ibuf, isiz, &osiz);
659     fwrite(obuf, osiz, 1, stdout);
660     free(obuf);
661   }
662   return 0;
663 }
664
665
666 /* perform zlib command */
667 static int proczlib(const char *ibuf, int isiz, bool dec, bool gz){
668   if(dec){
669     int osiz;
670     char *obuf = gz ? tcgzipdecode(ibuf, isiz, &osiz) : tcinflate(ibuf, isiz, &osiz);
671     if(obuf){
672       fwrite(obuf, osiz, 1, stdout);
673       free(obuf);
674     } else {
675       eprintf("inflate failure");
676     }
677   } else {
678     int osiz;
679     char *obuf = gz ? tcgzipencode(ibuf, isiz, &osiz) : tcdeflate(ibuf, isiz, &osiz);
680     if(obuf){
681       fwrite(obuf, osiz, 1, stdout);
682       free(obuf);
683     } else {
684       eprintf("deflate failure");
685     }
686   }
687   return 0;
688 }
689
690
691 /* perform xml command */
692 static int procxml(const char *ibuf, int isiz, bool dec, bool br){
693   if(br){
694     TCLIST *elems = tcxmlbreak(ibuf);
695     for(int i = 0; i < tclistnum(elems); i++){
696       int esiz;
697       const char *elem = tclistval(elems, i, &esiz);
698       char *estr = tcmemdup(elem, esiz);
699       tcstrsubchr(estr, "\t\n\r", "  ");
700       tcstrtrim(estr);
701       if(*estr != '\0'){
702         if(*elem == '<'){
703           if(tcstrfwm(estr, "<!--")){
704             printf("COMMENT\t%s\n", estr);
705           } else if(tcstrfwm(estr, "<![CDATA[")){
706             printf("CDATA\t%s\n", estr);
707           } else if(tcstrfwm(estr, "<!")){
708             printf("DECL\t%s\n", estr);
709           } else if(tcstrfwm(estr, "<?")){
710             printf("XMLDECL\t%s\n", estr);
711           } else {
712             TCMAP *attrs = tcxmlattrs(estr);
713             tcmapiterinit(attrs);
714             const char *name;
715             if((name = tcmapget2(attrs, "")) != NULL){
716               if(tcstrfwm(estr, "</")){
717                 printf("END");
718               } else if(tcstrbwm(estr, "/>")){
719                 printf("EMPTY");
720               } else {
721                 printf("BEGIN");
722               }
723               printf("\t%s", name);
724               while((name = tcmapiternext2(attrs)) != NULL){
725                 if(*name == '\0') continue;
726                 printf("\t%s\t%s", name, tcmapiterval2(name));
727               }
728               printf("\n");
729             }
730             tcmapdel(attrs);
731           }
732         } else {
733           char *dstr = tcxmlunescape(estr);
734           printf("TEXT\t%s\n", dstr);
735           free(dstr);
736         }
737       }
738       free(estr);
739     }
740     tclistdel(elems);
741   } else if(dec){
742     char *obuf = tcxmlunescape(ibuf);
743     fwrite(obuf, strlen(obuf), 1, stdout);
744     free(obuf);
745   } else {
746     char *obuf = tcxmlescape(ibuf);
747     fwrite(obuf, strlen(obuf), 1, stdout);
748     free(obuf);
749   }
750   return 0;
751 }
752
753
754 /* perform ucs command */
755 static int procucs(const char *ibuf, int isiz, bool dec){
756   if(dec){
757     uint16_t *ary = tcmalloc(isiz + 1);
758     int anum = 0;
759     for(int i = 0; i < isiz; i += 2){
760       ary[anum++] = (((unsigned char *)ibuf)[i] << 8) + ((unsigned char *)ibuf)[i+1];
761     }
762     char *str = tcmalloc(isiz * 3 + 1);
763     tcstrucstoutf(ary, anum, str);
764     printf("%s", str);
765     free(str);
766     free(ary);
767   } else {
768     uint16_t *ary = tcmalloc(isiz * sizeof(uint16_t) + 1);
769     int anum;
770     tcstrutftoucs(ibuf, ary, &anum);
771     for(int i = 0; i < anum; i++){
772       int c = ary[i];
773       putchar(c >> 8);
774       putchar(c & 0xff);
775     }
776     free(ary);
777   }
778   return 0;
779 }
780
781
782 /* perform date command */
783 static int procdate(const char *str, int jl, bool wf, bool rf){
784   int64_t t = str ? tcstrmktime(str) : time(NULL);
785   if(wf){
786     char buf[48];
787     tcdatestrwww(t, jl, buf);
788     printf("%s\n", buf);
789   } else if(rf){
790     char buf[48];
791     tcdatestrhttp(t, jl, buf);
792     printf("%s\n", buf);
793   } else {
794     printf("%lld\n", (long long int)t);
795   }
796   return 0;
797 }
798
799
800 /* perform conf command */
801 static int procconf(int mode){
802   switch(mode){
803   case 'v':
804     printf("%s\n", tcversion);
805     break;
806   case 'i':
807     printf("%s\n", _TC_APPINC);
808     break;
809   case 'l':
810     printf("%s\n", _TC_APPLIBS);
811     break;
812   case 'p':
813     printf("%s\n", _TC_BINDIR);
814     break;
815   default:
816     printf("myconf(version): %s\n", tcversion);
817     printf("myconf(libver): %d\n", _TC_LIBVER);
818     printf("myconf(formatver): %s\n", _TC_FORMATVER);
819     printf("myconf(prefix): %s\n", _TC_PREFIX);
820     printf("myconf(includedir): %s\n", _TC_INCLUDEDIR);
821     printf("myconf(libdir): %s\n", _TC_LIBDIR);
822     printf("myconf(bindir): %s\n", _TC_BINDIR);
823     printf("myconf(libexecdir): %s\n", _TC_LIBEXECDIR);
824     printf("myconf(appinc): %s\n", _TC_APPINC);
825     printf("myconf(applibs): %s\n", _TC_APPLIBS);
826     printf("myconf(bigend): %d\n", TCBIGEND);
827     printf("myconf(usezlib): %d\n", TCUSEZLIB);
828     printf("sizeof(bool): %d\n", sizeof(bool));
829     printf("sizeof(char): %d\n", sizeof(char));
830     printf("sizeof(short): %d\n", sizeof(short));
831     printf("sizeof(int): %d\n", sizeof(int));
832     printf("sizeof(long): %d\n", sizeof(long));
833     printf("sizeof(long long): %d\n", sizeof(long long));
834     printf("sizeof(float): %d\n", sizeof(float));
835     printf("sizeof(double): %d\n", sizeof(double));
836     printf("sizeof(long double): %d\n", sizeof(long double));
837     printf("sizeof(size_t): %d\n", sizeof(size_t));
838     printf("sizeof(time_t): %d\n", sizeof(time_t));
839     printf("sizeof(off_t): %d\n", sizeof(off_t));
840     printf("sizeof(ino_t): %d\n", sizeof(ino_t));
841     printf("sizeof(intptr_t): %d\n", sizeof(intptr_t));
842     printf("sizeof(void *): %d\n", sizeof(void *));
843     printf("macro(CHAR_MAX): %llu\n", (unsigned long long)CHAR_MAX);
844     printf("macro(SHRT_MAX): %llu\n", (unsigned long long)SHRT_MAX);
845     printf("macro(INT_MAX): %llu\n", (unsigned long long)INT_MAX);
846     printf("macro(LONG_MAX): %llu\n", (unsigned long long)LONG_MAX);
847     printf("macro(LLONG_MAX): %llu\n", (unsigned long long)LLONG_MAX);
848     printf("macro(PATH_MAX): %llu\n", (unsigned long long)PATH_MAX);
849     printf("macro(RAND_MAX): %llu\n", (unsigned long long)RAND_MAX);
850     printf("sysconf(_SC_CLK_TCK): %ld\n", sysconf(_SC_CLK_TCK));
851     printf("sysconf(_SC_OPEN_MAX): %ld\n", sysconf(_SC_OPEN_MAX));
852     printf("sysconf(_SC_PAGESIZE): %ld\n", sysconf(_SC_PAGESIZE));
853     struct stat sbuf;
854     if(stat(MYCDIRSTR, &sbuf) == 0){
855       printf("stat(st_uid): %d\n", (int)sbuf.st_uid);
856       printf("stat(st_gid): %d\n", (int)sbuf.st_gid);
857       printf("stat(st_blksize): %d\n", (int)sbuf.st_blksize);
858     }
859   }
860   return 0;
861 }
862
863
864
865 // END OF FILE