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 *************************************************************************************************/
21 /* global variables */
22 const char *g_progname; // program name
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);
54 int main(int argc, char **argv){
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);
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");
108 /* print formatted error string */
109 static void eprintf(const char *format, ...){
111 va_start(ap, format);
112 fprintf(stderr, "%s: ", g_progname);
113 vfprintf(stderr, format, ap);
114 fprintf(stderr, "\n");
119 /* parse arguments of url command */
120 static int runurl(int argc, char **argv){
125 for(int i = 2; i < argc; i++){
126 if(!path && argv[i][0] == '-'){
127 if(!strcmp(argv[i], "-d")){
129 } else if(!strcmp(argv[i], "-br")){
131 } else if(!strcmp(argv[i], "-rs")){
132 if(++i >= argc) usage();
145 if(path && path[0] == '@'){
146 isiz = strlen(path) - 1;
147 ibuf = tcmemdup(path + 1, isiz);
149 ibuf = tcreadfile(path, -1, &isiz);
152 eprintf("%s: cannot open", path ? path : "(stdin)");
155 int rv = procurl(ibuf, isiz, dec, br, base);
156 if(path && path[0] == '@' && !br) printf("\n");
162 /* parse arguments of base command */
163 static int runbase(int argc, char **argv){
166 for(int i = 2; i < argc; i++){
167 if(!path && argv[i][0] == '-'){
168 if(!strcmp(argv[i], "-d")){
181 if(path && path[0] == '@'){
182 isiz = strlen(path) - 1;
183 ibuf = tcmemdup(path + 1, isiz);
185 ibuf = tcreadfile(path, -1, &isiz);
188 eprintf("%s: cannot open", path ? path : "(stdin)");
191 int rv = procbase(ibuf, isiz, dec);
192 if(path && path[0] == '@') printf("\n");
198 /* parse arguments of quote command */
199 static int runquote(int argc, char **argv){
202 for(int i = 2; i < argc; i++){
203 if(!path && argv[i][0] == '-'){
204 if(!strcmp(argv[i], "-d")){
217 if(path && path[0] == '@'){
218 isiz = strlen(path) - 1;
219 ibuf = tcmemdup(path + 1, isiz);
221 ibuf = tcreadfile(path, -1, &isiz);
224 eprintf("%s: cannot open", path ? path : "(stdin)");
227 int rv = procquote(ibuf, isiz, dec);
228 if(path && path[0] == '@') printf("\n");
234 /* parse arguments of mime command */
235 static int runmime(int argc, char **argv){
241 for(int i = 2; i < argc; i++){
242 if(!path && argv[i][0] == '-'){
243 if(!strcmp(argv[i], "-d")){
245 } else if(!strcmp(argv[i], "-en")){
246 if(++i >= argc) usage();
248 } else if(!strcmp(argv[i], "-q")){
250 } else if(!strcmp(argv[i], "-on")){
263 if(path && path[0] == '@'){
264 isiz = strlen(path) - 1;
265 ibuf = tcmemdup(path + 1, isiz);
267 ibuf = tcreadfile(path, -1, &isiz);
270 eprintf("%s: cannot open", path ? path : "(stdin)");
273 if(!ename) ename = "UTF-8";
274 int rv = procmime(ibuf, isiz, dec, ename, qb, on);
275 if(path && path[0] == '@') printf("\n");
281 /* parse arguments of pack command */
282 static int runpack(int argc, char **argv){
286 for(int i = 2; i < argc; i++){
287 if(!path && argv[i][0] == '-'){
288 if(!strcmp(argv[i], "-d")){
290 } else if(!strcmp(argv[i], "-bwt")){
303 if(path && path[0] == '@'){
304 isiz = strlen(path) - 1;
305 ibuf = tcmemdup(path + 1, isiz);
307 ibuf = tcreadfile(path, -1, &isiz);
310 eprintf("%s: cannot open", path ? path : "(stdin)");
313 int rv = procpack(ibuf, isiz, dec, bwt);
314 if(path && path[0] == '@') printf("\n");
320 /* parse arguments of tcbs command */
321 static int runtcbs(int argc, char **argv){
324 for(int i = 2; i < argc; i++){
325 if(!path && argv[i][0] == '-'){
326 if(!strcmp(argv[i], "-d")){
339 if(path && path[0] == '@'){
340 isiz = strlen(path) - 1;
341 ibuf = tcmemdup(path + 1, isiz);
343 ibuf = tcreadfile(path, -1, &isiz);
346 eprintf("%s: cannot open", path ? path : "(stdin)");
349 int rv = proctcbs(ibuf, isiz, dec);
350 if(path && path[0] == '@') printf("\n");
356 /* parse arguments of zlib command */
357 static int runzlib(int argc, char **argv){
361 for(int i = 2; i < argc; i++){
362 if(!path && argv[i][0] == '-'){
363 if(!strcmp(argv[i], "-d")){
365 } else if(!strcmp(argv[i], "-gz")){
378 if(path && path[0] == '@'){
379 isiz = strlen(path) - 1;
380 ibuf = tcmemdup(path + 1, isiz);
382 ibuf = tcreadfile(path, -1, &isiz);
385 eprintf("%s: cannot open", path ? path : "(stdin)");
388 int rv = proczlib(ibuf, isiz, dec, gz);
389 if(path && path[0] == '@') printf("\n");
395 /* parse arguments of xml command */
396 static int runxml(int argc, char **argv){
400 for(int i = 2; i < argc; i++){
401 if(!path && argv[i][0] == '-'){
402 if(!strcmp(argv[i], "-d")){
404 } else if(!strcmp(argv[i], "-br")){
417 if(path && path[0] == '@'){
418 isiz = strlen(path) - 1;
419 ibuf = tcmemdup(path + 1, isiz);
421 ibuf = tcreadfile(path, -1, &isiz);
424 eprintf("%s: cannot open", path ? path : "(stdin)");
427 int rv = procxml(ibuf, isiz, dec, br);
428 if(path && path[0] == '@') printf("\n");
434 /* parse arguments of ucs command */
435 static int runucs(int argc, char **argv){
438 for(int i = 2; i < argc; i++){
439 if(!path && argv[i][0] == '-'){
440 if(!strcmp(argv[i], "-d")){
453 if(path && path[0] == '@'){
454 isiz = strlen(path) - 1;
455 ibuf = tcmemdup(path + 1, isiz);
457 ibuf = tcreadfile(path, -1, &isiz);
460 eprintf("%s: cannot open", path ? path : "(stdin)");
463 int rv = procucs(ibuf, isiz, dec);
464 if(path && path[0] == '@') printf("\n");
470 /* parse arguments of date command */
471 static int rundate(int argc, char **argv){
476 for(int i = 2; i < argc; i++){
477 if(argv[i][0] == '-'){
478 if(!strcmp(argv[i], "-ds")){
479 if(++i >= argc) usage();
481 } else if(!strcmp(argv[i], "-jl")){
482 if(++i >= argc) usage();
484 } else if(!strcmp(argv[i], "-wf")){
486 } else if(!strcmp(argv[i], "-rf")){
495 int rv = procdate(str, jl, wf, rf);
500 /* parse arguments of conf command */
501 static int runconf(int argc, char **argv){
503 for(int i = 2; i < argc; i++){
504 if(argv[i][0] == '-'){
505 if(!strcmp(argv[i], "-v")){
507 } else if(!strcmp(argv[i], "-i")){
509 } else if(!strcmp(argv[i], "-l")){
511 } else if(!strcmp(argv[i], "-p")){
520 int rv = procconf(mode);
525 /* perform url command */
526 static int procurl(const char *ibuf, int isiz, bool dec, bool br, const char *base){
528 char *obuf = tcurlresolve(base, ibuf);
532 TCMAP *elems = tcurlbreak(ibuf);
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);
546 char *obuf = tcurldecode(ibuf, &osiz);
547 fwrite(obuf, osiz, 1, stdout);
550 char *obuf = tcurlencode(ibuf, isiz);
551 fwrite(obuf, strlen(obuf), 1, stdout);
558 /* perform base command */
559 static int procbase(const char *ibuf, int isiz, bool dec){
562 char *obuf = tcbasedecode(ibuf, &osiz);
563 fwrite(obuf, osiz, 1, stdout);
566 char *obuf = tcbaseencode(ibuf, isiz);
567 fwrite(obuf, strlen(obuf), 1, stdout);
574 /* perform quote command */
575 static int procquote(const char *ibuf, int isiz, bool dec){
578 char *obuf = tcquotedecode(ibuf, &osiz);
579 fwrite(obuf, osiz, 1, stdout);
582 char *obuf = tcquoteencode(ibuf, isiz);
583 fwrite(obuf, strlen(obuf), 1, stdout);
590 /* perform mime command */
591 static int procmime(const char *ibuf, int isiz, bool dec, const char *ename, bool qb, bool on){
594 char *obuf = tcmimedecode(ibuf, ebuf);
596 fwrite(ebuf, strlen(ebuf), 1, stdout);
598 fwrite(obuf, strlen(obuf), 1, stdout);
602 char *obuf = tcmimeencode(ibuf, ename, !qb);
603 fwrite(obuf, strlen(obuf), 1, stdout);
610 /* perform pack command */
611 static int procpack(const char *ibuf, int isiz, bool dec, bool bwt){
614 char *obuf = tcpackdecode(ibuf, isiz, &osiz);
617 TCREADVNUMBUF(obuf, idx, step);
618 char *tbuf = tcbwtdecode(obuf + step, osiz - step, idx);
619 fwrite(tbuf, osiz - step, 1, stdout);
622 fwrite(obuf, osiz, 1, stdout);
629 tbuf = tcbwtencode(ibuf, isiz, &idx);
630 char vnumbuf[sizeof(int)+1];
632 TCSETVNUMBUF(step, vnumbuf, idx);
633 tbuf = tcrealloc(tbuf, isiz + step + 1);
634 memmove(tbuf + step, tbuf, isiz);
635 memcpy(tbuf, vnumbuf, step);
640 char *obuf = tcpackencode(ibuf, isiz, &osiz);
641 fwrite(obuf, osiz, 1, stdout);
649 /* perform tcbs command */
650 static int proctcbs(const char *ibuf, int isiz, bool dec){
653 char *obuf = tcbsdecode(ibuf, isiz, &osiz);
654 fwrite(obuf, osiz, 1, stdout);
658 char *obuf = tcbsencode(ibuf, isiz, &osiz);
659 fwrite(obuf, osiz, 1, stdout);
666 /* perform zlib command */
667 static int proczlib(const char *ibuf, int isiz, bool dec, bool gz){
670 char *obuf = gz ? tcgzipdecode(ibuf, isiz, &osiz) : tcinflate(ibuf, isiz, &osiz);
672 fwrite(obuf, osiz, 1, stdout);
675 eprintf("inflate failure");
679 char *obuf = gz ? tcgzipencode(ibuf, isiz, &osiz) : tcdeflate(ibuf, isiz, &osiz);
681 fwrite(obuf, osiz, 1, stdout);
684 eprintf("deflate failure");
691 /* perform xml command */
692 static int procxml(const char *ibuf, int isiz, bool dec, bool br){
694 TCLIST *elems = tcxmlbreak(ibuf);
695 for(int i = 0; i < tclistnum(elems); i++){
697 const char *elem = tclistval(elems, i, &esiz);
698 char *estr = tcmemdup(elem, esiz);
699 tcstrsubchr(estr, "\t\n\r", " ");
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);
712 TCMAP *attrs = tcxmlattrs(estr);
713 tcmapiterinit(attrs);
715 if((name = tcmapget2(attrs, "")) != NULL){
716 if(tcstrfwm(estr, "</")){
718 } else if(tcstrbwm(estr, "/>")){
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));
733 char *dstr = tcxmlunescape(estr);
734 printf("TEXT\t%s\n", dstr);
742 char *obuf = tcxmlunescape(ibuf);
743 fwrite(obuf, strlen(obuf), 1, stdout);
746 char *obuf = tcxmlescape(ibuf);
747 fwrite(obuf, strlen(obuf), 1, stdout);
754 /* perform ucs command */
755 static int procucs(const char *ibuf, int isiz, bool dec){
757 uint16_t *ary = tcmalloc(isiz + 1);
759 for(int i = 0; i < isiz; i += 2){
760 ary[anum++] = (((unsigned char *)ibuf)[i] << 8) + ((unsigned char *)ibuf)[i+1];
762 char *str = tcmalloc(isiz * 3 + 1);
763 tcstrucstoutf(ary, anum, str);
768 uint16_t *ary = tcmalloc(isiz * sizeof(uint16_t) + 1);
770 tcstrutftoucs(ibuf, ary, &anum);
771 for(int i = 0; i < anum; i++){
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);
787 tcdatestrwww(t, jl, buf);
791 tcdatestrhttp(t, jl, buf);
794 printf("%lld\n", (long long int)t);
800 /* perform conf command */
801 static int procconf(int mode){
804 printf("%s\n", tcversion);
807 printf("%s\n", _TC_APPINC);
810 printf("%s\n", _TC_APPLIBS);
813 printf("%s\n", _TC_BINDIR);
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));
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);