Skip to content

Commit

Permalink
More i18n work
Browse files Browse the repository at this point in the history
You can now specify the input/output character set if you aren't using UTF-8
natively.
  • Loading branch information
spc476 committed Jan 15, 2011
1 parent f91bb4e commit e810aac
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 21 deletions.
113 changes: 92 additions & 21 deletions src/clients/gnugol.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <errno.h>
#include <locale.h> #include <locale.h>
#include <assert.h> #include <assert.h>
#include <getopt.h> #include <getopt.h>
#include <iconv.h>


#include "nodelist.h" #include "nodelist.h"
#include "query.h" #include "query.h"
Expand Down Expand Up @@ -75,6 +77,7 @@ int usage (char *err) {
printf( printf(
"-e --engine [bing|google|dummy]\n" "-e --engine [bing|google|dummy]\n"
"-o --output [html|json|org|mdwn|wiki|text|term|ssml|textile|raw]\n" "-o --output [html|json|org|mdwn|wiki|text|term|ssml|textile|raw]\n"
"-C --charset cs character set used locally\n"
"-n --nresults number of results to fetch\n" "-n --nresults number of results to fetch\n"
"-p --position start of results to fetch\n" "-p --position start of results to fetch\n"
"-s --snippets 0|1 disable/enable snippets\n" "-s --snippets 0|1 disable/enable snippets\n"
Expand Down Expand Up @@ -126,6 +129,7 @@ int usage (char *err) {


static const struct option long_options[] = { static const struct option long_options[] = {
{ "about" , no_argument , NULL , 'a' } , { "about" , no_argument , NULL , 'a' } ,
{ "charset" , required_argument , NULL , 'C' } ,
{ "debug" , required_argument , NULL , 'D' } , { "debug" , required_argument , NULL , 'D' } ,
{ "engine" , required_argument , NULL , 'e' } , { "engine" , required_argument , NULL , 'e' } ,
{ "footer" , required_argument , NULL , 'F' } , { "footer" , required_argument , NULL , 'F' } ,
Expand Down Expand Up @@ -219,9 +223,9 @@ print_enabled_options(QueryOptions_t *o, FILE *fp) {


#define BOOLOPT(OPTION) OPTION = (strtoul(optarg,NULL,10) & 1) #define BOOLOPT(OPTION) OPTION = (strtoul(optarg,NULL,10) & 1)
#ifdef HAVE_GNUGOLD #ifdef HAVE_GNUGOLD
# define QSTRING "ad:D:e:F:H:hi:l:L:n:o:p:s:S:t:u:Uvb46mPRST" # define QSTRING "aC:d:D:e:F:H:hi:l:L:n:o:p:s:S:t:u:Uvb46mPRST"
#else #else
# define QSTRING "ad:D:e:F:H:hi:l:L:n:o:p:s:S:t:u:Uv" # define QSTRING "aC:d:D:e:F:H:hi:l:L:n:o:p:s:S:t:u:Uv"
#endif #endif


int process_options(int argc, char **argv, QueryOptions_t *o) int process_options(int argc, char **argv, QueryOptions_t *o)
Expand All @@ -244,6 +248,7 @@ int process_options(int argc, char **argv, QueryOptions_t *o)
switch (opt) switch (opt)
{ {
case 'a': o->about = 1; break; case 'a': o->about = 1; break;
case 'C': o->charset = optarg; break;
case 'd': o->desc = strtoul(optarg,NULL,10); break; case 'd': o->desc = strtoul(optarg,NULL,10); break;
case 'D': o->debug = strtoul(optarg,NULL,10); break; case 'D': o->debug = strtoul(optarg,NULL,10); break;
case 'e': case 'e':
Expand Down Expand Up @@ -311,13 +316,25 @@ int process_options(int argc, char **argv, QueryOptions_t *o)
return optind; return optind;
} }


void finish_setup(QueryOptions_t *o,int idx,int argc,char **argv) int finish_setup(QueryOptions_t *o,int idx,int argc,char **argv)
{ {
GnuGolEngine engine; GnuGolEngine engine;
char string[MAX_MTU]; char string[MAX_MTU];
size_t querylen = 0; size_t querylen = 0;
int i; int i;



if (o->charset != NULL)
{
o->icin = iconv_open("UTF-8",o->charset);
if (o->icin == (iconv_t)-1)
return errno;

o->icout = iconv_open(o->charset,"UTF-8");
if (o->icout == (iconv_t)-1)
return errno;
}

string[0] = '\0'; string[0] = '\0';


if (!o->about && ListEmpty(&c_engines)) if (!o->about && ListEmpty(&c_engines))
Expand All @@ -326,7 +343,7 @@ void finish_setup(QueryOptions_t *o,int idx,int argc,char **argv)
if (engine == NULL) if (engine == NULL)
{ {
fprintf(stderr,"default engine not found! Panic!\n"); fprintf(stderr,"default engine not found! Panic!\n");
exit(EXIT_FAILURE); return ENOENT;
} }


ListAddTail(&c_engines,&engine->node); ListAddTail(&c_engines,&engine->node);
Expand All @@ -338,25 +355,57 @@ void finish_setup(QueryOptions_t *o,int idx,int argc,char **argv)
if (engine == NULL) if (engine == NULL)
{ {
fprintf(stderr,"default engine not found! Panic!\n"); fprintf(stderr,"default engine not found! Panic!\n");
exit(EXIT_FAILURE); return ENOENT;
} }


ListAddTail(&c_engines,&engine->node); ListAddTail(&c_engines,&engine->node);
} }


for(i = idx; i < argc; i++) { for(i = idx; i < argc; i++)
if((querylen += (strlen(argv[i])+1) > MAX_MTU - 80)) { {
fprintf(stderr,"Too many words in query, try something smaller\n"); size_t arginlen;
exit(EXIT_FAILURE); size_t argoutlen;
}
/* FIXME: Although I did a length check above it could be cleaner here */ arginlen = strlen(argv[i]);
if(!o->url_escape) { argoutlen = arginlen * 4;
strcat(string,argv[i]);
if(i+1 < argc) strcat(string," "); char tmpbuf[argoutlen + 1]; /* guess at a good size */
} else { char *tin = argv[i];
strcat(string,argv[i]); char *tout = tmpbuf;
if(i+1 < argc) strcat(string,"+"); char *word;
} size_t convlen;

if (o->charset != NULL)
{
convlen = iconv(o->icin,&tin,&arginlen,&tout,&argoutlen);
if (convlen == (size_t)-1)
return errno;

assert(tout != tmpbuf);
*tout = '\0';
word = tmpbuf;
}
else
word = argv[i];

if((querylen += (strlen(word)+1) > MAX_MTU - 80))
{
fprintf(stderr,"Too many words in query, try something smaller\n");
return EINVAL;
}

/* FIXME: Although I did a length check above it could be cleaner here */

if(!o->url_escape)
{
strcat(string,word);
if(i+1 < argc) strcat(string," ");
}
else
{
strcat(string,word);
if(i+1 < argc) strcat(string,"+");
}
} }


if(!o->url_escape) { if(!o->url_escape) {
Expand All @@ -371,6 +420,7 @@ void finish_setup(QueryOptions_t *o,int idx,int argc,char **argv)
o->engine_name = "credits"; o->engine_name = "credits";
o->header_str = "About: "; o->header_str = "About: ";
} }
return 0;
} }


static void gnugol_default_language (QueryOptions_t *q) { static void gnugol_default_language (QueryOptions_t *q) {
Expand Down Expand Up @@ -453,7 +503,9 @@ int main(int argc, char **argv) {


process_environ(argv[0],&master); process_environ(argv[0],&master);
words = process_options(argc,argv,&master); words = process_options(argc,argv,&master);
finish_setup(&master,words,argc,argv);
if (finish_setup(&master,words,argc,argv) != 0)
return EXIT_FAILURE;


assert(!ListEmpty(&c_engines)); assert(!ListEmpty(&c_engines));


Expand All @@ -466,8 +518,27 @@ int main(int argc, char **argv) {
q = master; q = master;
result = gnugol_engine_query(engine,&q); result = gnugol_engine_query(engine,&q);


if(q.returned_results > 0) { if(q.returned_results > 0)
printf("%s",q.out.s); {
if (q.charset)
{
char outbuffer[(q.out.len * 4) + 1];
size_t convlen;
char *tin = q.out.s;
size_t sin = q.out.len;
char *tout = outbuffer;
size_t sout = sizeof(outbuffer);

convlen = iconv(q.icout,&tin,&sin,&tout,&sout);
if (convlen == (size_t)-1)
return EXIT_FAILURE;

assert(tout != outbuffer);
*tout = '\0';
printf("%s",outbuffer);
}
else
printf("%s",q.out.s);
} }


if(result < 0 || q.debug > 5) { if(result < 0 || q.debug > 5) {
Expand Down
1 change: 1 addition & 0 deletions src/common/engines.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <iconv.h>


#include "nodelist.h" #include "nodelist.h"
#include "query.h" #include "query.h"
Expand Down
1 change: 1 addition & 0 deletions src/common/format.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <iconv.h>
#include "query.h" #include "query.h"
#include "formats.h" #include "formats.h"
#include "utf8.h" #include "utf8.h"
Expand Down
1 change: 1 addition & 0 deletions src/engines/bing.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include <jansson.h> #include <jansson.h>
#include <ctype.h> #include <ctype.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <iconv.h>
#include "query.h" #include "query.h"
#include "utf8.h" #include "utf8.h"
#include "handy.h" #include "handy.h"
Expand Down
1 change: 1 addition & 0 deletions src/engines/credits.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include <dirent.h> #include <dirent.h>
#include <jansson.h> #include <jansson.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <iconv.h>
#include "query.h" #include "query.h"
#include "utf8.h" #include "utf8.h"
#include "handy.h" #include "handy.h"
Expand Down
1 change: 1 addition & 0 deletions src/engines/dns.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ along with this program. If not, see http://www.gnu.org/licenses/


#include <stddef.h> #include <stddef.h>
#include <errno.h> #include <errno.h>
#include <iconv.h>
#include "query.h" #include "query.h"
#include "gnugol_engines.h" #include "gnugol_engines.h"


Expand Down
1 change: 1 addition & 0 deletions src/engines/dummy.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include <string.h> #include <string.h>
#include <jansson.h> #include <jansson.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <iconv.h>
#include "query.h" #include "query.h"
#include "utf8.h" #include "utf8.h"
#include "handy.h" #include "handy.h"
Expand Down
1 change: 1 addition & 0 deletions src/engines/googlev1.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include <jansson.h> #include <jansson.h>
#include <ctype.h> #include <ctype.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <iconv.h>
#include "query.h" #include "query.h"
#include "utf8.h" #include "utf8.h"
#include "handy.h" #include "handy.h"
Expand Down
1 change: 1 addition & 0 deletions src/engines/googlev2.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include <string.h> #include <string.h>
#include <jansson.h> #include <jansson.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <iconv.h>
#include "query.h" #include "query.h"
#include "utf8.h" #include "utf8.h"
#include "handy.h" #include "handy.h"
Expand Down
1 change: 1 addition & 0 deletions src/engines/man.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ along with this program. If not, see http://www.gnu.org/licenses/


#include <stddef.h> #include <stddef.h>
#include <errno.h> #include <errno.h>
#include <iconv.h>
#include "query.h" #include "query.h"


int setup(QueryOptions_t *q) int setup(QueryOptions_t *q)
Expand Down
1 change: 1 addition & 0 deletions src/engines/omega.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ along with this program. If not, see http://www.gnu.org/licenses/


#include <stddef.h> #include <stddef.h>
#include <errno.h> #include <errno.h>
#include <iconv.h>
#include "query.h" #include "query.h"


int setup(QueryOptions_t *q) int setup(QueryOptions_t *q)
Expand Down
1 change: 1 addition & 0 deletions src/engines/opensearch.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


#include <stddef.h> #include <stddef.h>
#include <errno.h> #include <errno.h>
#include <iconv.h>
#include "query.h" #include "query.h"


int setup(QueryOptions_t *q) int setup(QueryOptions_t *q)
Expand Down
1 change: 1 addition & 0 deletions src/engines/stackapps.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ json API doc at:
#include <jansson.h> #include <jansson.h>
#include <ctype.h> #include <ctype.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <iconv.h>
#include "query.h" #include "query.h"
#include "utf8.h" #include "utf8.h"
#include "handy.h" #include "handy.h"
Expand Down
1 change: 1 addition & 0 deletions src/engines/wikipedia.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include <string.h> #include <string.h>
#include <jansson.h> #include <jansson.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <iconv.h>
#include "query.h" #include "query.h"
#include "utf8.h" #include "utf8.h"
#include "handy.h" #include "handy.h"
Expand Down
3 changes: 3 additions & 0 deletions src/include/query.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ struct query_options {
char *plugin_file; char *plugin_file;
char *server; char *server;
char *client; char *client;
char *charset;
iconv_t icin;
iconv_t icout;
buffer_obj_t out; buffer_obj_t out;
buffer_obj_t err; buffer_obj_t err;
buffer_obj_t wrn; buffer_obj_t wrn;
Expand Down

0 comments on commit e810aac

Please sign in to comment.