1 /*************************************************************************************************
2  * Command line utility for system integration
3  *                                                      Copyright (C) 2003-2006 Mikio Hirabayashi
4  * This file is part of Estraier, a personal full-text search system.
5  * Estraier is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  * Estraier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along with Estraier;
12  * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
13  * MA 02111-1307 USA.
14  *************************************************************************************************/
15 
16 
17 #include "estcommon.h"
18 
19 
20 /* global variables */
21 const char *progname = NULL;             /* program name */
22 
23 
24 /* function prototypes */
25 int main(int argc, char **argv);
26 void usage(void);
27 int runcgiparam(int argc, char **argv);
28 int runhtmlesc(int argc, char **argv);
29 int runurlenc(int argc, char **argv);
30 
31 
32 /* main routine */
main(int argc,char ** argv)33 int main(int argc, char **argv){
34   int rv;
35   estputenv("LANG", ESTLOCALE);
36   estputenv("LC_ALL", ESTLOCALE);
37   cbstdiobin();
38   progname = argv[0];
39   if(argc < 2) usage();
40   rv = 0;
41   if(!strcmp(argv[1], "prefix")){
42     printf("%s\n", _EST_PREFIX);
43     rv = 0;
44   } else if(!strcmp(argv[1], "bindir")){
45     printf("%s\n", _EST_BINDIR);
46     rv = 0;
47   } else if(!strcmp(argv[1], "libexecdir")){
48     printf("%s\n", _EST_LEXEDIR);
49     rv = 0;
50   } else if(!strcmp(argv[1], "datadir")){
51     printf("%s\n", _EST_DATADIR);
52     rv = 0;
53   } else if(!strcmp(argv[1], "version")){
54     printf("%s\n", _EST_VERSION);
55     rv = 0;
56   } else if(!strcmp(argv[1], "cgiparam")){
57     rv = runcgiparam(argc, argv);
58   } else if(!strcmp(argv[1], "htmlesc")){
59     rv = runhtmlesc(argc, argv);
60   } else if(!strcmp(argv[1], "urlenc")){
61     rv = runurlenc(argc, argv);
62   } else {
63     usage();
64   }
65   return rv;
66 }
67 
68 
69 /* print the usage and exit */
usage(void)70 void usage(void){
71   fprintf(stderr, "%s: utility for system integration\n", progname);
72   fprintf(stderr, "\n");
73   fprintf(stderr, "usage:\n");
74   fprintf(stderr, "  %s prefix\n", progname);
75   fprintf(stderr, "  %s bindir\n", progname);
76   fprintf(stderr, "  %s libexecdir\n", progname);
77   fprintf(stderr, "  %s datadir\n", progname);
78   fprintf(stderr, "  %s version\n", progname);
79   fprintf(stderr, "  %s cgiparam name [qstr]\n", progname);
80   fprintf(stderr, "  %s htmlesc str\n", progname);
81   fprintf(stderr, "  %s urlenc str\n", progname);
82   fprintf(stderr, "\n");
83   exit(1);
84 }
85 
86 
87 /* extract parameters of CGI */
runcgiparam(int argc,char ** argv)88 int runcgiparam(int argc, char **argv){
89   CBDATUM *datum;
90   CBMAP *params;
91   CBLIST *pairs;
92   const char *name, *qstr, *rkey;
93   char *tmp, cc, *key, *val, *dkey, *dval;
94   int i, ic, err, rksiz;
95   name = NULL;
96   qstr = NULL;
97   for(i = 2; i < argc; i++){
98     if(!name){
99       name = argv[i];
100     } else if(!qstr){
101       qstr = argv[i];
102     } else {
103       usage();
104     }
105   }
106   if(!name) usage();
107   if(qstr){
108     tmp = cbmemdup(qstr, -1);
109   } else {
110     datum = cbdatumopen("", 0);
111     while((ic = getchar()) != EOF){
112       cc = (char)ic;
113       cbdatumcat(datum, &cc, 1);
114     }
115     tmp = cbdatumtomalloc(datum, NULL);
116   }
117   params = cbmapopen();
118   pairs = cbsplit(tmp, -1, "&");
119   err = FALSE;
120   for(i = 0; i < cblistnum(pairs); i++){
121     key = cbmemdup(cblistval(pairs, i, NULL), -1);
122     if((val = strchr(key, '=')) != NULL){
123       *(val++) = '\0';
124       dkey = cburldecode(key, NULL);
125       dval = cburldecode(val, NULL);
126       cbmapput(params, dkey, -1, dval, -1, FALSE);
127       free(dval);
128       free(dkey);
129     }
130     free(key);
131   }
132   cblistclose(pairs);
133   if(name[0] != '\0'){
134     if((rkey = cbmapget(params, name, -1, &rksiz)) != NULL){
135       for(i = 0; i < rksiz; i++){
136         putchar(rkey[i]);
137       }
138     } else {
139       fprintf(stderr, "%s: %s: no such parameter\n", progname, name);
140       err = TRUE;
141     }
142   } else {
143     cbmapiterinit(params);
144     while((rkey = cbmapiternext(params, NULL)) != NULL){
145       printf("%s\t%s\n", rkey, cbmapget(params, rkey, -1, NULL));
146     }
147   }
148   cbmapclose(params);
149   free(tmp);
150   return err ? 1 : 0;
151 }
152 
153 
154 /* escape a string for HTML */
runhtmlesc(int argc,char ** argv)155 int runhtmlesc(int argc, char **argv){
156   char *esc;
157   if(argc != 3) return 1;
158   esc = cbxmlescape(argv[2]);
159   printf("%s", esc);
160   free(esc);
161   return 0;
162 }
163 
164 
165 /* encode a string for URL */
runurlenc(int argc,char ** argv)166 int runurlenc(int argc, char **argv){
167   char *esc;
168   if(argc != 3) return 1;
169   esc = cburlencode(argv[2], -1);
170   printf("%s", esc);
171   free(esc);
172   return 0;
173 }
174 
175 
176 
177 /* END OF FILE */
178