-
Notifications
You must be signed in to change notification settings - Fork 0
/
eplkup.c
852 lines (721 loc) · 24.3 KB
/
eplkup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/*------------------------------------------------------------------------
-- Copyright (C) 2011-2014 Christopher Brochtrup
--
-- This file is part of eplkup.
--
-- eplkup is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- eplkup is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with eplkup. If not, see <http://www.gnu.org/licenses/>.
--
------------------------------------------------------------------------*/
//#include <wtypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <eb/eb.h>
#include <eb/error.h>
#include <eb/text.h>
#include <eb/font.h>
#include "eplkup_utils.h"
#include "eplkup_hook_handler.h"
#include "eplkup_data.h"
#include "eplkup_gaiji.h"
static void init(void);
static void print_title_to_out_file(void);
static void lookup_link(void);
static void lookup_word(void);
static void die(int exit_code);
static void usage(int exit_code, char *msg);
#ifdef _WIN32
static void parse_command_line(void);
#else
static void parse_command_line(int argc, char* argv[]);
#endif
/* Hooks - Used to process certain constructs as they come up (such as gaiji) */
static EB_Hookset hookset;
static EB_Hook hook_begin_subscript = { EB_HOOK_BEGIN_SUBSCRIPT , hook_set_begin_subscript };
static EB_Hook hook_begin_superscript = { EB_HOOK_BEGIN_SUPERSCRIPT , hook_set_begin_superscript };
static EB_Hook hook_end_subscript = { EB_HOOK_END_SUBSCRIPT , hook_set_end_subscript };
static EB_Hook hook_end_superscript = { EB_HOOK_END_SUPERSCRIPT , hook_set_end_superscript };
static EB_Hook hook_narrow_font = { EB_HOOK_NARROW_FONT , hook_set_narrow_font };
static EB_Hook hook_wide_font = { EB_HOOK_WIDE_FONT , hook_set_wide_font };
static EB_Hook hook_begin_keyword = { EB_HOOK_BEGIN_KEYWORD , hook_set_begin_keyword };
static EB_Hook hook_end_keyword = { EB_HOOK_END_KEYWORD , hook_set_end_keyword };
static EB_Hook hook_begin_emphasis = { EB_HOOK_BEGIN_EMPHASIS , hook_set_begin_emphasis };
static EB_Hook hook_end_emphasis = { EB_HOOK_END_EMPHASIS , hook_set_end_emphasis };
static EB_Hook hook_begin_reference = { EB_HOOK_BEGIN_REFERENCE , hook_set_begin_reference };
static EB_Hook hook_end_reference = { EB_HOOK_END_REFERENCE , hook_set_end_reference };
/* Inputs */
static char book_path[MAXLEN_PATH + 1] = ""; /* The path of the book */
static char in_path[MAXLEN_PATH + 1] = ""; /* The path of input file */
static char out_path[MAXLEN_PATH + 1] = ""; /* The path of the output file */
static int subbook_index = 0; /* The subbook to use */
static int show_hit_count = 0; /* 1 = Output the number of hits on the first line */
static int max_hits_to_output = MAX_HITS; /* The number of hits to output */
static int hit_to_output = -1; /* The index of a particular hit to output */
static int print_heading = 1; /* 1 = Print the hit heading */
static int print_text = 1; /* 1 = Print the hit text */
static int print_hit_number = 0; /* 1 = Output the hit number before each hit */
static int print_title = 0; /* 1 = Output the title of the subbook */
static int input_is_link = 0; /* 1 = Input file contains a link instead of a word */
static int set_emphasis_hook = 0; /* 1 = User wants to set the bold/emphasis hook */
static int set_keyword_hook = 0; /* 1 = User wants to set the keyword hook */
static int set_reference_hook = 0; /* 1 = User wants to set the link/reference hook */
static int set_subscript_hook = 0; /* 1 = User wants to set the subscript hook */
static int set_superscript_hook = 0; /* 1 = User wants to set the superscript hook */
static char conv_buf[MAXLEN_CONV + 1]; /* Storage for encoding conversion */
static char final_buf[MAXLEN_CONV + 1]; /* Text that will be output to file */
static char text[MAXLEN_TEXT + 1]; /* Entry text */
static char heading[MAXLEN_HEADING + 1]; /* Entry heading */
static char title[EB_MAX_TITLE_LENGTH + 1]; /* Subbook title */
static EB_Book book; /* The EPWING book object */
/*------------------------------------------------------------------------
-- Name: WinMain
--
------------------------------------------------------------------------*/
#ifdef _WIN32
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
#else
int main(int argc, char* argv[])
#endif
{
/* Parse the command line arguments */
#ifdef _WIN32
parse_command_line();
#else
parse_command_line(argc, argv);
#endif
/* Initialize */
init();
/* If the user wants to print the subbook title to the output file instead of performing a search */
if(print_title)
{
print_title_to_out_file();
}
/* Else if the input file contains a link instead of a word */
else if(input_is_link)
{
lookup_link();
}
/* Else, it is a normal word lookup */
else
{
lookup_word();
}
die(0);
return 0;
} /* WinMain */
/*------------------------------------------------------------------------
-- Name: init
--
-- Description:
-- Initialize EB Lib, set hooks, open the desired subbook.
--
-- Parameters:
-- None.
--
-- Returns:
-- None.
--
------------------------------------------------------------------------*/
static void init(void)
{
EB_Error_Code error_code;
EB_Subbook_Code subbook_list[EB_MAX_SUBBOOKS];
FILE *out_file = NULL;
int subbook_count;
/* Blank the output file */
out_file = fopen(out_path, "w");
if(out_file == NULL)
{
fprintf(stderr, "Error: Could not open output file, \"%s\"\n", out_path);
die(1);
}
fclose(out_file);
/* Init EB Lib */
eb_initialize_library();
eb_initialize_book(&book);
eb_initialize_hookset(&hookset);
/* Set hooks */
eb_set_hook(&hookset, &hook_narrow_font);
eb_set_hook(&hookset, &hook_wide_font);
if(set_emphasis_hook)
{
eb_set_hook(&hookset, &hook_begin_emphasis);
eb_set_hook(&hookset, &hook_end_emphasis);
}
if(set_keyword_hook)
{
eb_set_hook(&hookset, &hook_begin_keyword);
eb_set_hook(&hookset, &hook_end_keyword);
}
if(set_reference_hook)
{
eb_set_hook(&hookset, &hook_begin_reference);
eb_set_hook(&hookset, &hook_end_reference);
}
if(set_subscript_hook)
{
eb_set_hook(&hookset, &hook_begin_subscript);
eb_set_hook(&hookset, &hook_end_subscript);
}
if(set_superscript_hook)
{
eb_set_hook(&hookset, &hook_begin_superscript);
eb_set_hook(&hookset, &hook_end_superscript);
}
/* Open the EPWING book */
error_code = eb_bind(&book, book_path);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to bind the book, %s: %s\n", eb_error_message(error_code), book_path);
die(1);
}
/* Get the subbook list */
error_code = eb_subbook_list(&book, subbook_list, &subbook_count);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to get the subbbook list, %s\n", eb_error_message(error_code));
die(1);
}
/* Get the subbook */
error_code = eb_set_subbook(&book, subbook_list[subbook_index]);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to set the current subbook, %s\n", eb_error_message(error_code));
die(1);
}
/* Get the subbook directory (the name only, not the full path) */
error_code = eb_subbook_directory(&book, subbook_directory);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to get the subbook directory: %s\n", eb_error_message(error_code));
die(1);
}
/* Set the font */
if (eb_set_font(&book, EB_FONT_16) < 0)
{
fprintf(stderr, "Error: Failed to set the font size: %s\n", eb_error_message(error_code));
die(1);
}
} /* init */
/*------------------------------------------------------------------------
-- Name: print_title_to_out_file
--
-- Description:
-- Print the title of the book to the output file.
--
-- Parameters:
-- None.
--
-- Returns:
-- None.
--
------------------------------------------------------------------------*/
static void print_title_to_out_file(void)
{
EB_Error_Code error_code;
char *status_conv = NULL;
FILE *out_file = NULL;
/* Get the title of the subbook */
error_code = eb_subbook_title2(&book, subbook_index, title);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to get the title: %s\n", eb_error_message(error_code));
die(1);
}
/* Convert title from EUC-JP to UTF-8 */
status_conv = convert_encoding(conv_buf, MAXLEN_CONV, title, EB_MAX_TITLE_LENGTH, "UTF-8", "EUC-JP");
if(status_conv == NULL)
{
fprintf(stderr, "Error: Something went wrong when trying to encode the title\n");
die(1);
}
out_file = fopen(out_path, "w");
if(out_file == NULL)
{
fprintf(stderr, "Error: Could not open output file, \"%s\"\n", out_path);
die(1);
}
/* Output the text to file (in UTF-8) */
fwrite(conv_buf, 1, strlen(conv_buf), out_file);
fclose(out_file);
} /* print_title_to_out_file */
/*------------------------------------------------------------------------
-- Name: lookup_link
--
-- Description:
-- Lookup the input link and send the results to the output file.
--
-- Parameters:
-- None.
--
-- Returns:
-- None.
--
------------------------------------------------------------------------*/
static void lookup_link(void)
{
EB_Error_Code error_code;
EB_Position position;
FILE *in_file = NULL;
FILE *out_file = NULL;
char link_text[MAXLEN_LOOKUP_WORD] = "";
char *status_conv = NULL;
ssize_t text_length;
int parse_result;
in_file = fopen(in_path, "r");
if(in_file == NULL)
{
fprintf(stderr, "Error: Could not open input file: \"%s\"", in_path);
die(1);
}
if(fgets(link_text, MAXLEN_LOOKUP_WORD, in_file) == NULL)
{
fclose(in_file);
fprintf(stderr, "Error: Could not read word from input file: \"%s\"", in_path);
die(1);
}
fclose(in_file);
/* Parse the location of the link in the subbook */
parse_result = sscanf(link_text, "%X %X", &position.page, &position.offset);
/* If link was not parsed correctly (2 is the expected number of fields in the input file) */
if(parse_result != 2)
{
fprintf(stderr, "Error: Could not parse link from input file, %d.\n", parse_result);
die(1);
}
error_code = eb_seek_text(&book, &position);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to seek text, \"%s\"\n", eb_error_message(error_code));
die(1);
}
error_code = eb_read_text(&book, NULL, &hookset, NULL, MAXLEN_TEXT, text, &text_length);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to read text, \"%s\"\n", eb_error_message(error_code));
die(1);
}
/* Convert from EUC-JP to UTF-8 */
status_conv = convert_encoding(conv_buf, MAXLEN_CONV, text, text_length, "UTF-8", "EUC-JP");
if(status_conv == NULL)
{
fprintf(stderr, "Error: Something went wrong when trying to encode the the text\n");
die(1);
}
/* Replace gaiji that have UTF-8 equivalents */
replace_gaiji_with_utf8(final_buf, conv_buf);
out_file = fopen(out_path, "w");
/* Output the text to file (in UTF-8) */
fwrite(final_buf, 1, strlen(final_buf), out_file);
fclose(out_file);
} /* lookup_link */
/*------------------------------------------------------------------------
-- Name: lookup_word
--
-- Description:
-- Lookup the input word and send the results to the output file.
--
-- Parameters:
-- None.
--
-- Returns:
-- None.
--
------------------------------------------------------------------------*/
static void lookup_word(void)
{
char lookup_word_utf8[MAXLEN_LOOKUP_WORD + 1];
char lookup_word_eucjp[MAXLEN_LOOKUP_WORD + 1];
char *status_conv = NULL;
EB_Error_Code error_code;
EB_Hit hits[MAX_HITS];
FILE *in_file = NULL;
FILE *out_file = NULL;
int hit_count;
ssize_t heading_length;
ssize_t text_length;
int i;
/* Get the word to lookup */
in_file = fopen(in_path, "r");
if(in_file == NULL)
{
fprintf(stderr, "Error: Could not open input file: \"%s\"", in_path);
die(1);
}
if(fgets(lookup_word_utf8, MAXLEN_LOOKUP_WORD, in_file) == NULL)
{
fclose(in_file);
fprintf(stderr, "Error: Could not read word from input file: \"%s\"", in_path);
die(1);
}
fclose(in_file);
/* Remove the final '\n' */
if(lookup_word_utf8[strlen(lookup_word_utf8) - 1] == '\n')
{
lookup_word_utf8[strlen(lookup_word_utf8) - 1] = '\0';
}
/* Convert the lookup word from UTF-8 to EUC-JP */
status_conv = convert_encoding(lookup_word_eucjp, MAXLEN_LOOKUP_WORD, lookup_word_utf8, strlen(lookup_word_utf8), "EUC-JP", "UTF-8");
if(status_conv == NULL)
{
fprintf(stderr, "Error: Something went wront when trying to encode the lookup word\n");
die(1);
}
/* Perform an exact search of the lookup word */
error_code = eb_search_exactword(&book, lookup_word_eucjp);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to search for the word, %s: %s\n", eb_error_message(error_code), lookup_word_eucjp);
die(1);
}
while(1)
{
/* Get the list of hits */
error_code = eb_hit_list(&book, MAX_HITS, hits, &hit_count);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to get hit entries, %s\n", eb_error_message(error_code));
die(1);
}
/* Are we done? */
if(hit_count == 0)
{
break;
}
/* Create the output file */
out_file = fopen(out_path, "w");
if(out_file == NULL)
{
fprintf(stderr, "Error: Could not open output file, \"%s\"\n", out_path);
die(1);
}
/* Output only the number of hits? */
if(show_hit_count)
{
fprintf(out_file, "{HITS: %d}\n", hit_count);
}
/* Determine the max number of hits to output */
hit_count = MIN(hit_count, max_hits_to_output);
/* For each search hit, print the hit information to the output file */
for(i = 0; i < hit_count; i++)
{
/* Did the user specify a particular hit index to output? */
if(hit_to_output >= 0)
{
i = hit_to_output;
}
/* Output the hit number */
if(print_hit_number && (hit_count > 1) && (hit_to_output == -1))
{
fprintf(out_file, "{ENTRY: %d}\n", i);
}
/* Print the heading of the hit to file */
if(print_heading)
{
/* Seek to the heading */
error_code = eb_seek_text(&book, &(hits[i].heading));
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to seek the subbook, %s\n", eb_error_message(error_code));
fclose(out_file);
die(1);
}
/* Read the heading */
error_code = eb_read_heading(&book, NULL, &hookset, NULL, MAXLEN_HEADING, heading, &heading_length);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to read the subbook, %s\n", eb_error_message(error_code));
fclose(out_file);
die(1);
}
/* Convert from EUC-JP to UTF-8 */
status_conv = convert_encoding(conv_buf, MAXLEN_CONV, heading, heading_length, "UTF-8", "EUC-JP");
if(status_conv == NULL)
{
fprintf(stderr, "Error: Something went wrong when trying to encode the the heading\n");
fclose(out_file);
die(1);
}
/* Replace gaiji that have UTF-8 equivalents */
replace_gaiji_with_utf8(final_buf, conv_buf);
/* Output the header to file (in UTF-8) */
fprintf(out_file, "%s\n", conv_buf);
}
/* Print the text of the hit to file */
if(print_text)
{
/* Seek to the text */
error_code = eb_seek_text(&book, &(hits[i].text));
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to seek the subbook, %s\n", eb_error_message(error_code));
fclose(out_file);
die(1);
}
/* Read the text*/
error_code = eb_read_text(&book, NULL, &hookset, NULL, MAXLEN_TEXT, text, &text_length);
if(error_code != EB_SUCCESS)
{
fprintf(stderr, "Error: Failed to read the subbook, %s\n", eb_error_message(error_code));
fclose(out_file);
die(1);
}
}
/* Convert from EUC-JP to UTF-8 */
status_conv = convert_encoding(conv_buf, MAXLEN_CONV, text, text_length, "UTF-8", "EUC-JP");
if(status_conv == NULL)
{
fprintf(stderr, "Error: Something went wrong when trying to encode the the text\n");
fclose(out_file);
die(1);
}
/* Replace gaiji that have UTF-8 equivalents */
replace_gaiji_with_utf8(final_buf, conv_buf);
/* Output the text to file (in UTF-8) */
fwrite(final_buf, 1, strlen(final_buf), out_file);
/* Since the user specified a hit index, don't display the other hits */
if(hit_to_output >= 0)
{
break;
}
}
fclose(out_file);
}
} /* lookup_word */
/*------------------------------------------------------------------------
-- Name: die
--
-- Description:
-- Cleanup resources and exit.
--
-- Parameters:
-- (IN) exit_code - The exit code to use.
--
-- Returns:
-- None.
--
------------------------------------------------------------------------*/
static void die(int exit_code)
{
eb_finalize_book(&book);
eb_finalize_library();
eb_finalize_hookset(&hookset);
exit(exit_code);
} /* die */
/*------------------------------------------------------------------------
-- Name: parse_command_line
--
-- Description:
-- Parse the command line arguments.
--
-- Parameters:
-- None.
--
-- Returns:
-- None.
--
------------------------------------------------------------------------*/
#ifdef _WIN32
void parse_command_line()
#else
void parse_command_line(int argc, char* argv[])
#endif
{
int i;
#ifdef _WIN32
int argc = 0;
LPWSTR *argv;
char cur_arg[MAXLEN_ARG + 1] = "";
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
#else
char* cur_arg = NULL;
#define wcstombs(varname, src, dummy) varname = src;
#endif
if(argv == NULL)
{
fprintf(stderr, "Error: Could not determine arguments!\n");
exit(1);
}
/* For each argument */
for(i = 1; i < argc; i++)
{
/* Convert arg to char array */
wcstombs(cur_arg, argv[i], MAXLEN_ARG);
if(strcmp(cur_arg, "--emphasis") == 0)
{
set_emphasis_hook = 1;
}
else if(strcmp(cur_arg, "--gaiji") == 0)
{
i++;
wcstombs(cur_arg, argv[i], MAXLEN_ARG);
gaiji_option = atoi(cur_arg);
if((gaiji_option < GAIJI_OPTION_DEFAULT) || (gaiji_option > GAIJI_OPTION_HTML_IMG))
{
usage(1, "Bad --gaiji option!\n");
}
}
else if(strcmp(cur_arg, "--help") == 0)
{
usage(0, "");
}
else if(strcmp(cur_arg, "--hit") == 0)
{
i++;
wcstombs(cur_arg, argv[i], MAXLEN_ARG);
hit_to_output = atoi(cur_arg);
if(hit_to_output < 0)
{
usage(1, "Bad --hit option!\n");
}
}
else if(strcmp(cur_arg, "--hit-num") == 0)
{
print_hit_number = 1;
}
else if(strcmp(cur_arg, "--html-sub") == 0)
{
set_subscript_hook = 1;
}
else if(strcmp(cur_arg, "--html-sup") == 0)
{
set_superscript_hook = 1;
}
else if(strcmp(cur_arg, "--keyword") == 0)
{
set_keyword_hook = 1;
}
else if(strcmp(cur_arg, "--link") == 0)
{
set_reference_hook = 1;
}
else if(strcmp(cur_arg, "--link-in") == 0)
{
input_is_link = 1;
}
else if(strcmp(cur_arg, "--max-hits") == 0)
{
i++;
wcstombs(cur_arg, argv[i], MAXLEN_ARG);
max_hits_to_output = atoi(cur_arg);
if((max_hits_to_output < 1) || (max_hits_to_output > MAX_HITS))
{
usage(1, "Bad --max-hits option!\n");
}
}
else if(strcmp(cur_arg, "--no-header") == 0)
{
print_heading = 0;
}
else if(strcmp(cur_arg, "--no-text") == 0)
{
print_text = 0;
}
else if(strcmp(cur_arg, "--show-count") == 0)
{
show_hit_count = 1;
}
else if(strcmp(cur_arg, "--subbook") == 0)
{
i++;
wcstombs(cur_arg, argv[i], MAXLEN_ARG);
subbook_index = atoi(cur_arg);
if(subbook_index < 0)
{
usage(1, "Bad --subbook option!\n");
}
}
else if(strcmp(cur_arg, "--title") == 0)
{
print_title = 1;
}
else if(strcmp(cur_arg, "--ver") == 0)
{
usage(0, "eplkup version 1.4 by Christopher Brochtrup.\n");
}
else
{
if((argc - i) != 3)
{
usage(1, "Error: Incorrect number of arguments!\n");
}
/* Get the book path */
strncpy_len(book_path, cur_arg, MAXLEN_PATH);
/* Get the input file */
i++;
wcstombs(cur_arg, argv[i], MAXLEN_ARG);
strncpy_len(in_path, cur_arg, MAXLEN_PATH);
/* Get the output file */
i++;
wcstombs(cur_arg, argv[i], MAXLEN_ARG);
strncpy_len(out_path, cur_arg, MAXLEN_PATH);
}
}
} /* parse_command_line */
/*------------------------------------------------------------------------
-- Name: usage
--
-- Description:
-- Print usage information.
--
-- Parameters:
-- (IN) exist_code - The exit code to return to the shell.
-- (IN) msg - An additional message to print before the usage.
-- Leave blank if desired.
--
-- Returns:
-- None.
--
------------------------------------------------------------------------*/
void usage(int exit_code, char *msg)
{
if(strlen(msg) > 0)
{
fprintf(stderr, "%s\n\n", msg);
}
printf("Usage: eplkup [--emphasis] [--gaiji] [--help] [--hit #] [--hit-num] [--html-sub] \\\n");
printf(" [--html-sup] [--keyword] [--link] [--link-in] [--no-header] [--no-text] [--max-hits #] \\\n");
printf(" [--show-count] [--subbook #] [--title] [--ver] <book-path> <input-file> <output-file>\n");
printf("\n");
printf("Performs an exact search on the provided word in the provided EPWING book.\n");
printf("\n");
printf("Required:\n");
printf(" book-path - Directory that contains the EPWING \"CATALOG\" or \"CATALOGS\" file.\n");
printf(" input-file - File that contains the word to lookup (in UTF-8 without BOM).\n");
printf(" output-file - File that will contain the lookup text (in UTF-8 without BOM).\n");
printf("\n");
printf("Optional:\n");
printf(" --emphasis - Place HTML <em></em> tags around bold/emphasized text.\n");
printf(" --gaiji - 0 = Replace gaiji with no UTF-8 equivalents with a '?' (default).\n");
printf(" 1 = Replace gaiji with no UTF-8 equivalents with HTML image tags containing\n");
printf(" embedded base64 image data.\n");
printf(" --help - Show help.\n");
printf(" --hit - Specify which hit to output (starting at 0). If not specified, all hits will be output.\n");
printf(" --hit-num - Output the number of the hit above the hit output (if multiple hits). Ex: {ENTRY: 3}.\n");
printf(" --html-sub - Put HTML <sub></sub> tags around subscript text.\n");
printf(" --html-sup - Put HTML <sup></sup> tags around superscript text.\n");
printf(" --keyword - Put <KEYWORD></KEYWORD> tags around the keyword.\n");
printf(" --link - Put <LINK></LINK[page:offset]> tags around links/references.\n");
printf(" --link-in - The input file contains a link in the format: 'hex_page<whitespace>hex_offset'.\n");
printf(" --max-hits - Specify the number of hits to output when --hit is not specified. Default is %d.\n", MAX_HITS);
printf(" --no-header - Don't print the headers.\n");
printf(" --no-text - Don't print the text.\n");
printf(" --show-count - Output the number of lookup hits in the first line of the output file. Ex. {HITS: 6}\n");
printf(" --subbook - The subbook to use in the EPWING book. Default is 0.\n");
printf(" --title - Get the title of the subbook.\n");
printf(" --ver - Show version.\n");
printf("\n");
exit(exit_code);
} /* usage */