-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.c
executable file
·148 lines (132 loc) · 5.04 KB
/
main.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
/*========================================================================
epub2txt
Copyright (c)2012-2016 Kevin Boone
Distributed under the terms of the GPV, version 2.0
=========================================================================*/
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "klib_log.h"
#include "klib_path.h"
#include "klib_list.h"
#include "klib_getopt.h"
#include "klib_getoptspec.h"
#include "epub2txt.h"
/*========================================================================
show_short_usage
=========================================================================*/
void show_short_usage (FILE *f, const char *argv0)
{
fprintf (f, "Usage: %s [-anvw] {files...}\n", argv0);
fprintf (f, "'%s --longhelp' for more details.\n", argv0);
}
/*========================================================================
show_long_usage
=========================================================================*/
void show_long_usage (FILE *f, const char *argv0)
{
fprintf (f, "Usage: %s [options...] [expression]\n", argv0);
fprintf (f, " -a,--ascii ASCII output\n");
fprintf (f, " -d,--debug {level} Set debug level (0-4)\n");
fprintf (f, " --longhelp Detailed usage\n");
fprintf (f, " -n,--notrim Do not trim whitespace\n");
fprintf (f,
" -p,--paras {count} Write paragraph count every {count} paras\n");
fprintf (f,
" -s,--start {para} Start output from paragraph {para}\n");
fprintf (f, " -v,--version Show version and configuration\n");
fprintf (f, " -w,--width {cols} Format for cols columns\n");
fprintf (f, "If no width is specified, lines will not be broken except\n");
fprintf (f, "on paragraph boundaries\n");
}
/*========================================================================
show_version
=========================================================================*/
void show_version (FILE *f, const char *argv0)
{
fprintf (f, "epub2txt version " VERSION "\n");
fprintf (f, "Copyright (c)2013-2016 Kevin Boone\n");
}
/*=============================================================================
main()
=============================================================================*/
int main (int argc, char **argv)
{
const char *argv0 = argv[0];
klib_GetOpt *getopt = klib_getopt_new ();
klib_getopt_add_spec (getopt, "help", "help", 'h', KLIB_GETOPT_NOARG);
klib_getopt_add_spec (getopt, "longhelp", "longhelp", 0, KLIB_GETOPT_NOARG);
klib_getopt_add_spec (getopt, "version", "version", 'v', KLIB_GETOPT_NOARG);
klib_getopt_add_spec (getopt, "ascii", "ascii", 'a', KLIB_GETOPT_NOARG);
klib_getopt_add_spec (getopt, "paras", "paras", 'p', KLIB_GETOPT_COMPARG);
klib_getopt_add_spec (getopt, "start", "start", 's', KLIB_GETOPT_COMPARG);
klib_getopt_add_spec (getopt, "width", "width", 'w', KLIB_GETOPT_COMPARG);
klib_getopt_add_spec (getopt, "debug", "debug", 'd', KLIB_GETOPT_COMPARG);
klib_getopt_add_spec (getopt, "notrim", "notrim", 'n', KLIB_GETOPT_NOARG);
klib_Error *error = NULL;
klib_getopt_parse (getopt, argc, (const char **)argv, &error);
if (!error)
{
BOOL stopping_option = FALSE;
if (klib_getopt_arg_set (getopt, "help"))
{
show_short_usage (stdout, argv[0]);
stopping_option = TRUE;
}
else if (klib_getopt_arg_set (getopt, "longhelp"))
{
show_long_usage (stdout, argv[0]);
stopping_option = TRUE;
}
else if (klib_getopt_arg_set (getopt, "version"))
{
show_version (stdout, argv[0]);
stopping_option = TRUE;
}
if (!stopping_option)
{
BOOL ascii = klib_getopt_arg_set (getopt, "ascii");
BOOL notrim = klib_getopt_arg_set (getopt, "notrim");
const char *s_width = klib_getopt_get_arg (getopt, "width");
int width = 0;
if (s_width)
width = atoi (s_width);
const char *s_paras = klib_getopt_get_arg (getopt, "paras");
para_mark = 0;
if (s_paras)
para_mark = atoi (s_paras);
const char *s_start = klib_getopt_get_arg (getopt, "start");
start_para = 0;
if (s_start)
start_para = atoi (s_start);
const char *s_debug = klib_getopt_get_arg (getopt, "debug");
if (s_debug)
klib_log_set_level (atoi (s_debug));
else
klib_log_set_level (KLIB_LOG_WARNING);
int i, argc = klib_getopt_argc (getopt);
for (i = 0; i < argc; i++)
{
const char *argv = klib_getopt_argv (getopt, i);
klib_Error *error = NULL;
klib_log_info ("Processing EPUB file %s", argv);
epub2txt_do_file (argv, ascii, width, notrim, &error);
if (error)
{
klib_log_error ("%s: %s\n", argv0, klib_error_cstr (error));
klib_error_free (error);
}
}
}
}
else
{
// Error parsing command line
fprintf (stderr, "%s\n", klib_error_cstr (error));
klib_error_free (error);
}
klib_getopt_free (getopt);
return 0;
}