| 1 |
/* |
| 2 |
* main.c: command line parsing and top level |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
#include "halibut.h" |
| 9 |
|
| 10 |
static void dbg_prtsource(paragraph * sourceform); |
| 11 |
static void dbg_prtwordlist(int level, word * w); |
| 12 |
static void dbg_prtkws(keywordlist * kws); |
| 13 |
|
| 14 |
int main(int argc, char **argv) |
| 15 |
{ |
| 16 |
char **infiles; |
| 17 |
char *outfile; |
| 18 |
int nfiles; |
| 19 |
int nogo; |
| 20 |
int errs; |
| 21 |
int reportcols; |
| 22 |
int debug; |
| 23 |
#ifdef C_ASSERT |
| 24 |
{ C_ASSERT(!word_Normal); /* For getcmdstyle */ } |
| 25 |
#endif |
| 26 |
|
| 27 |
initversionstring(); |
| 28 |
|
| 29 |
/* |
| 30 |
* Set up initial (default) parameters. |
| 31 |
*/ |
| 32 |
infiles = mknewa(char *, argc); |
| 33 |
outfile = NULL; |
| 34 |
nfiles = 0; |
| 35 |
nogo = errs = FALSE; |
| 36 |
reportcols = 0; |
| 37 |
debug = 0; |
| 38 |
|
| 39 |
if (argc == 1) |
| 40 |
{ |
| 41 |
usage(); |
| 42 |
exit(EXIT_SUCCESS); |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* Parse command line arguments. |
| 47 |
*/ |
| 48 |
while (--argc) |
| 49 |
{ |
| 50 |
char *p = *++argv; |
| 51 |
if (*p == '-') |
| 52 |
{ |
| 53 |
/* |
| 54 |
* An option. |
| 55 |
*/ |
| 56 |
while (p && *++p) |
| 57 |
{ |
| 58 |
char c = *p; |
| 59 |
switch (c) |
| 60 |
{ |
| 61 |
case '-': |
| 62 |
/* |
| 63 |
* Long option. |
| 64 |
*/ |
| 65 |
{ |
| 66 |
char *opt, *val; |
| 67 |
opt = p++; /* opt will have _one_ leading - */ |
| 68 |
while (*p && *p != '=') |
| 69 |
p++; /* find end of option */ |
| 70 |
if (*p == '=') |
| 71 |
{ |
| 72 |
*p++ = '\0'; |
| 73 |
val = p; |
| 74 |
} else |
| 75 |
val = NULL; |
| 76 |
if (!strcmp(opt, "-version")) |
| 77 |
{ |
| 78 |
showversion(); |
| 79 |
nogo = TRUE; |
| 80 |
} else if (!strcmp(opt, "-licence") || |
| 81 |
!strcmp(opt, "-license")) |
| 82 |
{ |
| 83 |
licence(); |
| 84 |
nogo = TRUE; |
| 85 |
} else if (!strcmp(opt, "-output")) |
| 86 |
{ |
| 87 |
if (!val) |
| 88 |
errs = TRUE, error(err_optnoarg, opt); |
| 89 |
else |
| 90 |
outfile = val; |
| 91 |
} else if (!strcmp(opt, "-precise")) |
| 92 |
{ |
| 93 |
reportcols = 1; |
| 94 |
} else |
| 95 |
{ |
| 96 |
errs = TRUE, error(err_nosuchopt, opt); |
| 97 |
} |
| 98 |
} |
| 99 |
p = NULL; |
| 100 |
break; |
| 101 |
case 'V': |
| 102 |
case 'L': |
| 103 |
case 'P': |
| 104 |
case 'd': |
| 105 |
/* |
| 106 |
* Option requiring no parameter. |
| 107 |
*/ |
| 108 |
switch (c) |
| 109 |
{ |
| 110 |
case 'V': |
| 111 |
showversion(); |
| 112 |
nogo = TRUE; |
| 113 |
break; |
| 114 |
case 'L': |
| 115 |
licence(); |
| 116 |
nogo = TRUE; |
| 117 |
break; |
| 118 |
case 'P': |
| 119 |
reportcols = 1; |
| 120 |
break; |
| 121 |
case 'd': |
| 122 |
debug = TRUE; |
| 123 |
break; |
| 124 |
} |
| 125 |
break; |
| 126 |
case 'o': |
| 127 |
/* |
| 128 |
* Option requiring parameter. |
| 129 |
*/ |
| 130 |
p++; |
| 131 |
if (!*p && argc > 1) |
| 132 |
--argc, p = *++argv; |
| 133 |
else if (!*p) |
| 134 |
{ |
| 135 |
char opt[2]; |
| 136 |
opt[0] = c; |
| 137 |
opt[1] = '\0'; |
| 138 |
errs = TRUE, error(err_optnoarg, opt); |
| 139 |
} |
| 140 |
/* |
| 141 |
* Now c is the option and p is the parameter. |
| 142 |
*/ |
| 143 |
switch (c) |
| 144 |
{ |
| 145 |
case 'o': |
| 146 |
outfile = p; |
| 147 |
break; |
| 148 |
} |
| 149 |
p = NULL; /* prevent continued processing */ |
| 150 |
break; |
| 151 |
default: |
| 152 |
/* |
| 153 |
* Unrecognised option. |
| 154 |
*/ |
| 155 |
{ |
| 156 |
char opt[2]; |
| 157 |
opt[0] = c; |
| 158 |
opt[1] = '\0'; |
| 159 |
errs = TRUE, error(err_nosuchopt, opt); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} else |
| 164 |
{ |
| 165 |
/* |
| 166 |
* A non-option argument. |
| 167 |
*/ |
| 168 |
infiles[nfiles++] = p; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if (errs) |
| 173 |
exit(EXIT_FAILURE); |
| 174 |
if (nogo) |
| 175 |
exit(EXIT_SUCCESS); |
| 176 |
|
| 177 |
/* |
| 178 |
* Do the work. |
| 179 |
*/ |
| 180 |
if (nfiles == 0) |
| 181 |
{ |
| 182 |
error(err_noinput); |
| 183 |
usage(); |
| 184 |
exit(EXIT_FAILURE); |
| 185 |
} |
| 186 |
|
| 187 |
{ |
| 188 |
input in; |
| 189 |
paragraph *sourceform, *p; |
| 190 |
indexdata *idx; |
| 191 |
keywordlist *keywords; |
| 192 |
|
| 193 |
in.filenames = infiles; |
| 194 |
in.nfiles = nfiles; |
| 195 |
in.currfp = NULL; |
| 196 |
in.currindex = 0; |
| 197 |
in.npushback = in.pushbacksize = 0; |
| 198 |
in.pushback = NULL; |
| 199 |
in.reportcols = reportcols; |
| 200 |
in.stack = NULL; |
| 201 |
|
| 202 |
idx = make_index(); |
| 203 |
|
| 204 |
sourceform = read_input(&in, idx); |
| 205 |
if (!sourceform) |
| 206 |
exit(EXIT_FAILURE); |
| 207 |
|
| 208 |
sfree(in.pushback); |
| 209 |
|
| 210 |
mark_attr_ends(sourceform); |
| 211 |
|
| 212 |
sfree(infiles); |
| 213 |
|
| 214 |
keywords = get_keywords(sourceform); |
| 215 |
if (!keywords) |
| 216 |
exit(EXIT_FAILURE); |
| 217 |
gen_citations(sourceform, keywords); |
| 218 |
subst_keywords(sourceform, keywords); |
| 219 |
|
| 220 |
for (p = sourceform; p; p = p->next) |
| 221 |
if (p->type == para_IM) |
| 222 |
index_merge(idx, TRUE, p->keyword, p->words); |
| 223 |
|
| 224 |
build_index(idx); |
| 225 |
|
| 226 |
|
| 227 |
if (debug) |
| 228 |
{ |
| 229 |
index_debug(idx); |
| 230 |
dbg_prtkws(keywords); |
| 231 |
dbg_prtsource(sourceform); |
| 232 |
} |
| 233 |
|
| 234 |
xhtml_backend(sourceform, keywords, idx); |
| 235 |
|
| 236 |
free_para_list(sourceform); |
| 237 |
free_keywords(keywords); |
| 238 |
cleanup_index(idx); |
| 239 |
} |
| 240 |
|
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
static void dbg_prtsource(paragraph * sourceform) |
| 245 |
{ |
| 246 |
/* |
| 247 |
* Output source form in debugging format. |
| 248 |
*/ |
| 249 |
|
| 250 |
paragraph *p; |
| 251 |
for (p = sourceform; p; p = p->next) |
| 252 |
{ |
| 253 |
wchar_t *wp; |
| 254 |
printf("para %d ", p->type); |
| 255 |
if (p->keyword) |
| 256 |
{ |
| 257 |
wp = p->keyword; |
| 258 |
while (*wp) |
| 259 |
{ |
| 260 |
putchar('\"'); |
| 261 |
for (; *wp; wp++) |
| 262 |
putchar(*wp); |
| 263 |
putchar('\"'); |
| 264 |
if (*++wp) |
| 265 |
printf(", "); |
| 266 |
} |
| 267 |
} else |
| 268 |
printf("(no keyword)"); |
| 269 |
printf(" {\n"); |
| 270 |
dbg_prtwordlist(1, p->words); |
| 271 |
printf("}\n"); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
static void dbg_prtkws(keywordlist * kws) |
| 276 |
{ |
| 277 |
/* |
| 278 |
* Output keywords in debugging format. |
| 279 |
*/ |
| 280 |
|
| 281 |
int i; |
| 282 |
keyword *kw; |
| 283 |
|
| 284 |
for (i = 0; (kw = index234(kws->keys, i)) != NULL; i++) |
| 285 |
{ |
| 286 |
wchar_t *wp; |
| 287 |
printf("keyword "); |
| 288 |
wp = kw->key; |
| 289 |
while (*wp) |
| 290 |
{ |
| 291 |
putchar('\"'); |
| 292 |
for (; *wp; wp++) |
| 293 |
putchar(*wp); |
| 294 |
putchar('\"'); |
| 295 |
if (*++wp) |
| 296 |
printf(", "); |
| 297 |
} |
| 298 |
printf(" {\n"); |
| 299 |
dbg_prtwordlist(1, kw->text); |
| 300 |
printf("}\n"); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
static void dbg_prtwordlist(int level, word * w) |
| 305 |
{ |
| 306 |
for (; w; w = w->next) |
| 307 |
{ |
| 308 |
wchar_t *wp; |
| 309 |
printf("%*sword %d ", level * 4, "", w->type); |
| 310 |
if (w->text) |
| 311 |
{ |
| 312 |
printf("\""); |
| 313 |
for (wp = w->text; *wp; wp++) |
| 314 |
putchar(*wp); |
| 315 |
printf("\""); |
| 316 |
} else |
| 317 |
printf("(no text)"); |
| 318 |
if (w->breaks) |
| 319 |
printf(" [breaks]"); |
| 320 |
if (w->alt) |
| 321 |
{ |
| 322 |
printf(" alt = {\n"); |
| 323 |
dbg_prtwordlist(level + 1, w->alt); |
| 324 |
printf("%*s}", level * 4, ""); |
| 325 |
} |
| 326 |
printf("\n"); |
| 327 |
} |
| 328 |
} |