| 1 |
/*** main.c ******************************************************************** |
| 2 |
** |
| 3 |
** This file is part of BibTool. |
| 4 |
** It is distributed under the GNU General Public License. |
| 5 |
** See the file COPYING for details. |
| 6 |
** |
| 7 |
** (c) 1996-2020 Gerd Neugebauer |
| 8 |
** |
| 9 |
** Net: [email protected] |
| 10 |
** |
| 11 |
** This program is free software; you can redistribute it and/or modify |
| 12 |
** it under the terms of the GNU General Public License as published by |
| 13 |
** the Free Software Foundation; either version 2, or (at your option) |
| 14 |
** any later version. |
| 15 |
** |
| 16 |
** This program is distributed in the hope that it will be useful, |
| 17 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
** GNU General Public License for more details. |
| 20 |
** |
| 21 |
** You should have received a copy of the GNU General Public License |
| 22 |
** along with this program; if not, write to the Free Software |
| 23 |
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 |
** |
| 25 |
**----------------------------------------------------------------------------- |
| 26 |
** >>> See the file doc/c_lib.tex for a printed documentation of the |
| 27 |
** >>> main parts of the C source. |
| 28 |
** |
| 29 |
**----------------------------------------------------------------------------- |
| 30 |
** Description: |
| 31 |
** This is the \BibTool{} main module. It contains the |main()| |
| 32 |
** function which evaluates the command line arguments and |
| 33 |
** proceeds accordingly. This means that usually resource files |
| 34 |
** and \BibTeX{} files are read and one or more \BibTeX{} files |
| 35 |
** are written. |
| 36 |
** |
| 37 |
** This file makes use of the \BibTool{} C library but is not |
| 38 |
** part of it. For this purpose it has to provide certain |
| 39 |
** functions which are expected by the library. These functions |
| 40 |
** are: |
| 41 |
** \begin{quote} |
| 42 |
** \texttt{save\_input\_file()}\\ |
| 43 |
** \texttt{save\_macro\_file()}\\ |
| 44 |
** \texttt{save\_output\_file()}\\ |
| 45 |
** \end{quote} |
| 46 |
** The arguments and the expected behavior of these functions is |
| 47 |
** described below. |
| 48 |
** |
| 49 |
** If you are trying to understand the implementation of |
| 50 |
** \BibTool{} the file |resource.h| plays a central |
| 51 |
** r\^ole. Consult the description of this file for further |
| 52 |
** details. |
| 53 |
** |
| 54 |
** If you are trying to write your own program to manipulate |
| 55 |
** \BibTeX{} files then this file can serve as a starting point. |
| 56 |
** But you should keep in mind that this file has grown over |
| 57 |
** several years and it contains the full complexity of the |
| 58 |
** \BibTool{} program logic. Thus you can reduce this file |
| 59 |
** drastically if you start playing around with the \BibTool{} C |
| 60 |
** library. |
| 61 |
** |
| 62 |
******************************************************************************/ |
| 63 |
|
| 64 |
#include <bibtool/bibtool.h> |
| 65 |
#include <bibtool/sbuffer.h> |
| 66 |
#include <bibtool/crossref.h> |
| 67 |
#include <bibtool/check.h> |
| 68 |
#include <bibtool/io.h> |
| 69 |
#ifdef HAVE_LIBKPATHSEA |
| 70 |
#ifdef __STDC__ |
| 71 |
#define HAVE_PROTOTYPES |
| 72 |
#endif |
| 73 |
#include <kpathsea/debug.h> |
| 74 |
#endif |
| 75 |
#include "config.h" |
| 76 |
|
| 77 |
/*****************************************************************************/ |
| 78 |
/* Internal Programs */ |
| 79 |
/*===========================================================================*/ |
| 80 |
|
| 81 |
#ifdef __STDC__ |
| 82 |
#define _ARG(A) A |
| 83 |
#else |
| 84 |
#define _ARG(A) () |
| 85 |
#endif |
| 86 |
int main _ARG((int argc,char *argv[])); /* main.c */ |
| 87 |
static bool do_keys _ARG((DB db,Record rec)); /* main.c */ |
| 88 |
static bool do_no_keys _ARG((DB db,Record rec)); /* main.c */ |
| 89 |
static bool update_crossref _ARG((DB db,Record rec));/* main.c */ |
| 90 |
static int rec_gt _ARG((Record r1,Record r2)); /* main.c */ |
| 91 |
static int rec_gt_cased _ARG((Record r1,Record r2));/* main.c */ |
| 92 |
static int rec_lt _ARG((Record r1,Record r2)); /* main.c */ |
| 93 |
static int rec_lt_cased _ARG((Record r1,Record r2));/* main.c */ |
| 94 |
static void usage _ARG((bool fullp)); /* main.c */ |
| 95 |
|
| 96 |
/*****************************************************************************/ |
| 97 |
/* External Programs and Variables */ |
| 98 |
/*===========================================================================*/ |
| 99 |
|
| 100 |
/*---------------------------------------------------------------------------*/ |
| 101 |
|
| 102 |
#ifndef __STDC__ |
| 103 |
#ifndef HAVE_GETENV |
| 104 |
|
| 105 |
char * getenv _ARG((char *name)); /* main.c */ |
| 106 |
|
| 107 |
/*----------------------------------------------------------------------------- |
| 108 |
** Dummy funtion returning NULL to indicate failure. |
| 109 |
**___________________________________________________ */ |
| 110 |
char * getenv(name) /* */ |
| 111 |
char *name; /* */ |
| 112 |
{ return (char*)NULL; /* */ |
| 113 |
} /*------------------------*/ |
| 114 |
#endif |
| 115 |
#endif |
| 116 |
|
| 117 |
|
| 118 |
#ifndef OptionLeadingCharacter |
| 119 |
#define OptionLeadingCharacter '-' |
| 120 |
#endif |
| 121 |
|
| 122 |
static char *use[] = |
| 123 |
{ "bibtool [options] [%co outfile] [[%ci] infile] ...\n", |
| 124 |
"\n\tOptions:\n", |
| 125 |
"\t%cA<c>\t\tKind of disambiguating key strings: <c>=0|a|A\n", |
| 126 |
"\t%cc\t\tInclude crossreferenced entries into the output (toggle)\n", |
| 127 |
"\t%cd\t\tCheck double entries (toggle)\n", |
| 128 |
"\t%cf <format>\tKey generation enabled (formatted key)\n", |
| 129 |
"\t%cF\t\tKey generation enabled with formatted key\n", |
| 130 |
"\t%ch\t\tPrint this help info and exit\n", |
| 131 |
"\t[%ci] infile\tSpecify input file. If %ci omitted it may not start\n", |
| 132 |
"\t\t\twith a %c. If absent stdin is taken to read from.\n", |
| 133 |
"\t\t\tMultiple input files may be given.\n", |
| 134 |
"\t%ck\t\tKey generation enabled.\n", |
| 135 |
"\t%cK\t\tKey generation enabled (long key).\n", |
| 136 |
"\t%cm macfile\tDump macros to macfile. - is stdout\n", |
| 137 |
"\t%cM macfile\tDump used macros to macfile. - is stdout\n", |
| 138 |
"\t%co outfile\tSpecify output file as next argument\n", |
| 139 |
"\t\t\tIf absent stdout is taken to write to.\n", |
| 140 |
"\t%cq\t\tQuiet mode. No warnings.\n", |
| 141 |
"\t%cr resource\tLoad resource file (several are possible).\n", |
| 142 |
"\t%cR\t\tLoad default resource file here.\n", |
| 143 |
"\t%cs\t\tSort.\n", |
| 144 |
"\t%cS\t\tSort reverse.\n", |
| 145 |
"\t%cv\t\tEnable verbose mode.\n", |
| 146 |
"\t%cV\t\tPrint version and exit.\n", |
| 147 |
"\t%cx file\t\tExtract from aux file.\n", |
| 148 |
"\t%cX <regex>\tExtract regular expression.\n", |
| 149 |
"\t%c- <rsc>\tEvaluate one resource command <rsc>.\n", |
| 150 |
"\t%c@\t\tPrint statistics (short).\n", |
| 151 |
"\t%c#\t\tPrint statistics.\n", |
| 152 |
#ifdef SYMBOL_DUMP |
| 153 |
"\t%c$\t\tSymbol table output (debugging only)\n", |
| 154 |
#endif |
| 155 |
0L, |
| 156 |
"Copyright (C) 2019 Gerd Neugebauer", |
| 157 |
"[email protected]" |
| 158 |
}; |
| 159 |
|
| 160 |
/*----------------------------------------------------------------------------- |
| 161 |
** Function: usage() |
| 162 |
** Purpose: Print the version number, a copyright notice, and |
| 163 |
** a short description of the command line options on the |
| 164 |
** |stderr| stream. |
| 165 |
** In addition certain configuration options are |
| 166 |
** printed. If the argument is |false| then only the |
| 167 |
** copyright and the version number are displayed. |
| 168 |
** Arguments: |
| 169 |
** fullp Boolean. If |false| only the version is displayed. |
| 170 |
** Returns: nothing |
| 171 |
**___________________________________________________ */ |
| 172 |
static void usage(fullp) /* */ |
| 173 |
bool fullp; /* */ |
| 174 |
{ static char *comma = ", "; /* */ |
| 175 |
char *sep = " "; /* */ |
| 176 |
POSSIBLY_UNUSED(comma); /* */ |
| 177 |
POSSIBLY_UNUSED(sep); /* */ |
| 178 |
/* */ |
| 179 |
show_version(); /* */ |
| 180 |
/* */ |
| 181 |
if (fullp) /* */ |
| 182 |
{ register char **cpp; /* */ |
| 183 |
for (cpp = use; *cpp; cpp++) /* */ |
| 184 |
{ ErrPrintF2(*cpp, /* */ |
| 185 |
OptionLeadingCharacter, /* */ |
| 186 |
OptionLeadingCharacter); /* */ |
| 187 |
} /* */ |
| 188 |
ErrPrint("\n"); /* */ |
| 189 |
} /* */ |
| 190 |
/* */ |
| 191 |
ErrPrintF("Library path: %s", /* */ |
| 192 |
(rsc_v_rsc == NULL /* */ |
| 193 |
? "none" /* */ |
| 194 |
: (char*)rsc_v_rsc)); /* */ |
| 195 |
/* */ |
| 196 |
ErrPrint("\nSpecial configuration options:"); /* */ |
| 197 |
#define SPECIAL_OPTIONS " none" |
| 198 |
#ifdef HAVE_LIBKPATHSEA |
| 199 |
ErrPrintF("%skpathsea",sep); /* */ |
| 200 |
sep = comma; /* */ |
| 201 |
#undef SPECIAL_OPTIONS |
| 202 |
#define SPECIAL_OPTIONS "" |
| 203 |
#endif |
| 204 |
#ifndef REGEX |
| 205 |
ErrPrintF("%sNO regex",sep); /* */ |
| 206 |
sep = comma; /* */ |
| 207 |
#undef SPECIAL_OPTIONS |
| 208 |
#define SPECIAL_OPTIONS "" |
| 209 |
#endif |
| 210 |
#ifdef EMTEX_LIKE_PATH |
| 211 |
ErrPrintF("%semTeX like path",sep); /* */ |
| 212 |
#undef SPECIAL_OPTIONS |
| 213 |
#define SPECIAL_OPTIONS "" |
| 214 |
#endif |
| 215 |
ErrPrintF("%s\n",SPECIAL_OPTIONS); /* */ |
| 216 |
} /*------------------------*/ |
| 217 |
|
| 218 |
#define UnknownWarning(X) WARNING2("Unknown flag ignored: ", X) |
| 219 |
#define NoSFileWarning WARNING("Missing select option. Flag ignored.") |
| 220 |
#define NoFileError(X) WARNING3("File ", SymbolValue(X), " not found.") |
| 221 |
#define MissingPattern WARNING("Missing pattern.") |
| 222 |
#define MissingResource WARNING("Missing resource.") |
| 223 |
#define NoRscError(X) WARNING3("Resource file ", X, " not found.") |
| 224 |
|
| 225 |
|
| 226 |
/*****************************************************************************/ |
| 227 |
/*** MAIN ***/ |
| 228 |
/*****************************************************************************/ |
| 229 |
|
| 230 |
/*----------------------------------------------------------------------------- |
| 231 |
** Function*: keep_selected() |
| 232 |
** Type: bool |
| 233 |
** Purpose: Mark the record as deleted if it is not selected. |
| 234 |
** |
| 235 |
** Arguments: |
| 236 |
** db the database |
| 237 |
** rec the record |
| 238 |
** Returns: |false| |
| 239 |
**___________________________________________________ */ |
| 240 |
static bool keep_selected(db, rec) /* */ |
| 241 |
DB db; /* */ |
| 242 |
Record rec; /* */ |
| 243 |
{ /* */ |
| 244 |
if (!is_selected(db, rec)) /* */ |
| 245 |
{ SetRecordDELETED(rec); } /* */ |
| 246 |
/* */ |
| 247 |
return false; /* */ |
| 248 |
} /*------------------------*/ |
| 249 |
|
| 250 |
#ifdef UNUSED |
| 251 |
/*----------------------------------------------------------------------------- |
| 252 |
** Function*: keep_xref() |
| 253 |
** Type: int |
| 254 |
** Purpose: Undelete crossreferenced entries |
| 255 |
** |
| 256 |
** Arguments: |
| 257 |
** db the database |
| 258 |
** rec the record |
| 259 |
** Returns: |false| |
| 260 |
**___________________________________________________ */ |
| 261 |
static bool keep_xref(db,rec) /* */ |
| 262 |
DB db; /* */ |
| 263 |
Record rec; /* */ |
| 264 |
{ /* */ |
| 265 |
if (!RecordIsDELETED(rec)) /* */ |
| 266 |
{ Symbol key; /* */ |
| 267 |
int count; /* */ |
| 268 |
/* */ |
| 269 |
for (count = rsc_xref_limit; /* Prevent infinite loop */ |
| 270 |
count >= 0; /* */ |
| 271 |
count--) /* */ |
| 272 |
{ /* */ |
| 273 |
if ((key = get_field(db,rec,sym_crossref)) == NULL)/* */ |
| 274 |
{ return false; } /* */ |
| 275 |
/* */ |
| 276 |
key = expand_rhs(key, /* */ |
| 277 |
sym_empty, /* */ |
| 278 |
sym_empty, /* */ |
| 279 |
db, /* */ |
| 280 |
true); /* */ |
| 281 |
if ((rec=db_search(db, key)) == RecordNULL) /* */ |
| 282 |
{ ErrPrintF("*** BibTool: Crossref `%s' not found.\n",/* */ |
| 283 |
SymbolValue(key)); /* */ |
| 284 |
return false; /* */ |
| 285 |
} /* */ |
| 286 |
/* */ |
| 287 |
if (!RecordIsDELETED(rec)) { return false; } /* */ |
| 288 |
ClearRecordDELETED(rec); /* */ |
| 289 |
} /* */ |
| 290 |
/* */ |
| 291 |
ErrPrintF("*** BibTool: Crossref limit exceeded; `%s' possibly looped.\n", |
| 292 |
SymbolValue(key)); /* */ |
| 293 |
} /* */ |
| 294 |
return false; /* */ |
| 295 |
} /*------------------------*/ |
| 296 |
#endif |
| 297 |
|
| 298 |
/*----------------------------------------------------------------------------- |
| 299 |
** Function*: write_macros() |
| 300 |
** Type: static void |
| 301 |
** Purpose: |
| 302 |
** |
| 303 |
** Arguments: |
| 304 |
** m_file the name of the macro file of NULL for none |
| 305 |
** the_db the database |
| 306 |
** Returns: nothing |
| 307 |
**___________________________________________________ */ |
| 308 |
static void write_macros(m_file, the_db) /* */ |
| 309 |
char* m_file; /* */ |
| 310 |
DB the_db; /* */ |
| 311 |
{ FILE * file; /* */ |
| 312 |
/* */ |
| 313 |
if (m_file == NULL || *m_file == 0) return; /* */ |
| 314 |
/* */ |
| 315 |
if (rsc_verbose) /* */ |
| 316 |
{ VerbosePrint1("Writing macros"); } /* */ |
| 317 |
/* */ |
| 318 |
if (strcmp(m_file,"-") == 0 || /* */ |
| 319 |
(file=fopen(m_file,"w")) == NULL) /* */ |
| 320 |
{ file = stdout; } /* */ |
| 321 |
print_db(file,the_db,rsc_all_macs?"s":"S"); /* */ |
| 322 |
if (file != stdout) { fclose(file); } /* */ |
| 323 |
} /*------------------------*/ |
| 324 |
|
| 325 |
/*----------------------------------------------------------------------------- |
| 326 |
** Function*: read_in_files() |
| 327 |
** Type: void |
| 328 |
** Purpose: Read all the files in the input file pipe. |
| 329 |
** Note that additional files can be pushed during this |
| 330 |
** function is active. |
| 331 |
** Arguments: |
| 332 |
** db the database |
| 333 |
** Returns: nothing |
| 334 |
**___________________________________________________ */ |
| 335 |
static void read_in_files(db) /* */ |
| 336 |
DB db; /* */ |
| 337 |
{ int i; /* */ |
| 338 |
Symbol in; /* */ |
| 339 |
/* */ |
| 340 |
for (i = 0; i < get_no_inputs(); i++) /* For all input files */ |
| 341 |
{ in = get_input_file(i); /* */ |
| 342 |
if (read_db(db, SymbolValue(in), rsc_verbose)) /* */ |
| 343 |
{ NoFileError(in); } /* */ |
| 344 |
} /* */ |
| 345 |
} /*------------------------*/ |
| 346 |
|
| 347 |
/*----------------------------------------------------------------------------- |
| 348 |
** Function*: write_output() |
| 349 |
** Type: void |
| 350 |
** Purpose: |
| 351 |
** |
| 352 |
** Arguments: |
| 353 |
** db the database |
| 354 |
** Returns: nothing |
| 355 |
**___________________________________________________ */ |
| 356 |
static void write_output(db) /* */ |
| 357 |
DB db; /* */ |
| 358 |
{ FILE *file; /* */ |
| 359 |
Symbol o_file = get_output_file(); /* */ |
| 360 |
/* */ |
| 361 |
if (o_file == NO_SYMBOL) /* */ |
| 362 |
{ file = stdout; } /* */ |
| 363 |
else if (*o_file == '\0') /* */ |
| 364 |
{ return; } /* */ |
| 365 |
else if ((file=fopen((char*)SymbolValue(o_file),"w")) == NULL)/* */ |
| 366 |
{ file = stdout; /* */ |
| 367 |
} /* */ |
| 368 |
/* */ |
| 369 |
if (rsc_select) { rsc_del_q = false; } /* */ |
| 370 |
/* */ |
| 371 |
{ char * print_spec = (char*)rsc_print_et; /* */ |
| 372 |
/* */ |
| 373 |
if (rsc_expand_macros) /* */ |
| 374 |
{ char * cp; /* */ |
| 375 |
print_spec = new_string(print_spec); /* */ |
| 376 |
for (cp=print_spec; *cp; cp++) /* */ |
| 377 |
{ if (*cp == 's' || /* */ |
| 378 |
*cp == 'S' || /* */ |
| 379 |
*cp == '$' ) *cp = ' '; /* */ |
| 380 |
} /* */ |
| 381 |
} /* */ |
| 382 |
print_db(file, db, print_spec); /* */ |
| 383 |
if (rsc_expand_macros) free(print_spec); /* */ |
| 384 |
} /* */ |
| 385 |
/* */ |
| 386 |
if (file != stdout) { fclose(file); } /* */ |
| 387 |
} /*------------------------*/ |
| 388 |
|
| 389 |
#define Toggle(X) X = !(X) |
| 390 |
|
| 391 |
/*----------------------------------------------------------------------------- |
| 392 |
** Function: main() |
| 393 |
** Purpose: This is the main function which is automatically |
| 394 |
** called when the program is started. This function |
| 395 |
** contains the overall program logic. It has to perform |
| 396 |
** the appropriate initializations, evaluate command line |
| 397 |
** arguments, and run the main loop. |
| 398 |
** Arguments: |
| 399 |
** argc Number of arguments |
| 400 |
** argv Array of arguments |
| 401 |
** Returns: 0 upon success. Usually a failure raises an exception |
| 402 |
** which leads to an |exit()|. Thus this function does |
| 403 |
** not need to signal an error to the calling environment. |
| 404 |
**___________________________________________________ */ |
| 405 |
int main(argc,argv) /* */ |
| 406 |
int argc; /* Argument count */ |
| 407 |
char *argv[]; /* Argument values */ |
| 408 |
{ DB the_db; /* The \BibTool{} program */ |
| 409 |
/* currently handles a */ |
| 410 |
/* single database at a */ |
| 411 |
/* time. The local */ |
| 412 |
/* variable |the_db| */ |
| 413 |
/* contains a reference to*/ |
| 414 |
/* this database. */ |
| 415 |
int i; /* */ |
| 416 |
bool need_rsc = true; /* */ |
| 417 |
int (*fct)(); /* Function pointer */ |
| 418 |
int c_len; /* */ |
| 419 |
int *c = NULL; /* */ |
| 420 |
/* */ |
| 421 |
init_error(stderr); /* */ |
| 422 |
init_bibtool(argv[0]); /* */ |
| 423 |
/* */ |
| 424 |
for (i = 1; i < argc; i++) /* */ |
| 425 |
{ char *ap; /* */ |
| 426 |
if (*(ap=argv[i]) != OptionLeadingCharacter) /* */ |
| 427 |
{ save_input_file(symbol((String)argv[i])); /* */ |
| 428 |
} /* */ |
| 429 |
else /* */ |
| 430 |
{ switch (*++ap) /* */ |
| 431 |
{ case 'A': set_base((String)(ap+1)); break;/* disambiguation */ |
| 432 |
case 'c': Toggle(rsc_xref_select); break;/* include crossreferences*/ |
| 433 |
case 'd': Toggle(rsc_double_check); break;/* double entries */ |
| 434 |
#ifdef HAVE_LIBKPATHSEA |
| 435 |
case 'D': kpathsea_debug=atoi(++ap); break;/* kpathsea debugging */ |
| 436 |
#endif |
| 437 |
case 'f': add_format(argv[++i]); /* !!! no break !!! */ |
| 438 |
case 'F': rsc_make_key = true; break; /* key generation */ |
| 439 |
case 'h': usage(true); return 1; /* print help */ |
| 440 |
case 'i': save_input_file(symbol((String)argv[++i])); break;/* */ |
| 441 |
case 'K': add_format("long"); break; /* key generation */ |
| 442 |
case 'k': add_format("short"); break; /* key generation */ |
| 443 |
case 'M': /* print macro table */ |
| 444 |
case 'm': rsc_all_macs = (*ap=='m'); /* print macro table */ |
| 445 |
save_macro_file(symbol((String)argv[++i]));/* */ |
| 446 |
break; /* */ |
| 447 |
case 'o': /* output file */ |
| 448 |
if (++i < argc) /* */ |
| 449 |
{ save_output_file(symbol((String)argv[i])); }/* */ |
| 450 |
else /* */ |
| 451 |
{ WARNING((String)"Missing output file name"); } /* */ |
| 452 |
break; /* */ |
| 453 |
case 'q': Toggle(rsc_quiet); break; /* quiet */ |
| 454 |
case 'r': /* resource file */ |
| 455 |
if (++i < argc && load_rsc((String)(argv[i])))/* */ |
| 456 |
{ need_rsc = false; } /* */ |
| 457 |
else /* */ |
| 458 |
{ NoRscError(argv[i]); } /* */ |
| 459 |
break; /* */ |
| 460 |
case 'R': need_rsc |= !search_rsc();break; /* default resource file */ |
| 461 |
case 's': Toggle(rsc_sort); break; /* sort */ |
| 462 |
case 'S': /* */ |
| 463 |
Toggle(rsc_sort); /* */ |
| 464 |
Toggle(rsc_sort_reverse); /* */ |
| 465 |
break; /* sort */ |
| 466 |
case 'v': Toggle(rsc_verbose); break; /* verbose */ |
| 467 |
case 'V': usage(false); return 1; /* version */ |
| 468 |
case 'x': /* extract */ |
| 469 |
rsc_all_macs = false; /* */ |
| 470 |
if (++i < argc) /* */ |
| 471 |
{ read_aux((String)(argv[i]), /* */ |
| 472 |
save_input_file, /* */ |
| 473 |
*++ap=='v'); /* */ |
| 474 |
} /* */ |
| 475 |
else /* */ |
| 476 |
{ NoSFileWarning; } /* */ |
| 477 |
break; /* */ |
| 478 |
case 'X': /* extract pattern */ |
| 479 |
rsc_all_macs = false; /* */ |
| 480 |
if (++i < argc) { save_regex((String)argv[i]); }/* */ |
| 481 |
else { MissingPattern; } /* */ |
| 482 |
break; /* */ |
| 483 |
case '#': Toggle(rsc_cnt_all); break; /* print full statistics */ |
| 484 |
case '@': Toggle(rsc_cnt_used); break; /* print short statistics */ |
| 485 |
case '-': /* extended command */ |
| 486 |
if (*++ap) { (void)use_rsc((String)ap); }/* */ |
| 487 |
else if (++i<argc){ (void)use_rsc((String)argv[i]); }/* */ |
| 488 |
else { MissingResource; }/* */ |
| 489 |
break; /* */ |
| 490 |
#ifdef SYMBOL_DUMP |
| 491 |
case '$': Toggle(rsc_dump_symbols); break; /* print symbol table */ |
| 492 |
#endif |
| 493 |
default: /* */ |
| 494 |
UnknownWarning(ap); /* */ |
| 495 |
usage(true); /* */ |
| 496 |
return 1; /* */ |
| 497 |
} /* */ |
| 498 |
} /* */ |
| 499 |
} /* */ |
| 500 |
/* */ |
| 501 |
if (need_rsc) { (void)search_rsc(); } /* */ |
| 502 |
/* */ |
| 503 |
if (get_no_inputs() == 0) /* If no input file given */ |
| 504 |
{ save_input_file(symbol((String)"-")); } /* then read from stdin */ |
| 505 |
/* */ |
| 506 |
init_read(); /* Just in case the path */ |
| 507 |
/* has been modified. */ |
| 508 |
the_db = new_db(); /* */ |
| 509 |
/* */ |
| 510 |
read_in_files(the_db); /* */ |
| 511 |
/* */ |
| 512 |
db_forall(the_db, keep_selected); /* */ |
| 513 |
/* */ |
| 514 |
apply_aux(the_db); /* */ |
| 515 |
/* */ |
| 516 |
if (rsc_xref_select) db_xref_undelete(the_db); /* */ |
| 517 |
/* */ |
| 518 |
if (rsc_cnt_all || rsc_cnt_used) /* */ |
| 519 |
{ int i; /* */ |
| 520 |
int * cnt = db_count(the_db,&c_len); /* */ |
| 521 |
/* */ |
| 522 |
if ((c=(int*)malloc(c_len*sizeof(int))) == NULL)/* */ |
| 523 |
{ rsc_cnt_all = rsc_cnt_used = 0; } /* */ |
| 524 |
else /* */ |
| 525 |
{ for (i = 0; i < c_len; i++) c[i] = cnt[i]; /* */ |
| 526 |
} /* */ |
| 527 |
} /* */ |
| 528 |
/* */ |
| 529 |
if (rsc_expand_crossref || rsc_expand_xdata) /* */ |
| 530 |
{ /* */ |
| 531 |
db_forall(the_db,expand_crossref); /* */ |
| 532 |
} /* */ |
| 533 |
/* */ |
| 534 |
if (rsc_sort || rsc_make_key || need_sort_key) { /* */ |
| 535 |
DebugPrint1("start keygen"); /* */ |
| 536 |
start_key_gen(); /* */ |
| 537 |
} /* */ |
| 538 |
/* */ |
| 539 |
if (rsc_make_key) /* */ |
| 540 |
{ /* */ |
| 541 |
if (rsc_key_preserve) /* */ |
| 542 |
{ db_forall(the_db, mark_key); } /* */ |
| 543 |
/* */ |
| 544 |
DebugPrint1("rewinding"); /* */ |
| 545 |
db_rewind(the_db); /* */ |
| 546 |
/* */ |
| 547 |
DebugPrint1("do_keys"); /* */ |
| 548 |
db_forall(the_db, do_keys); /* */ |
| 549 |
/* */ |
| 550 |
DebugPrint1("update crossref"); /* */ |
| 551 |
db_forall(the_db, update_crossref); /* */ |
| 552 |
/* */ |
| 553 |
DebugPrint1("end keygen"); /* */ |
| 554 |
end_key_gen(); /* */ |
| 555 |
} /* */ |
| 556 |
else /* */ |
| 557 |
{ /* */ |
| 558 |
db_forall(the_db,do_no_keys); /* */ |
| 559 |
} /* */ |
| 560 |
/* */ |
| 561 |
if (rsc_sort) /* */ |
| 562 |
{ /* */ |
| 563 |
if (rsc_sort_cased) /* */ |
| 564 |
{ if (rsc_sort_reverse) fct = rec_lt_cased; /* */ |
| 565 |
else fct = rec_gt_cased; /* */ |
| 566 |
} /* */ |
| 567 |
else /* */ |
| 568 |
{ if (rsc_sort_reverse) fct = rec_lt; /* */ |
| 569 |
else fct = rec_gt; /* */ |
| 570 |
} /* */ |
| 571 |
db_sort(the_db, fct); /* */ |
| 572 |
} /* */ |
| 573 |
/* */ |
| 574 |
if (rsc_srt_macs) /* Maybe sort macros */ |
| 575 |
{ db_mac_sort(the_db); } /* */ |
| 576 |
/* */ |
| 577 |
apply_checks(the_db); /* */ |
| 578 |
/* */ |
| 579 |
write_output(the_db); /* */ |
| 580 |
/* */ |
| 581 |
write_macros(get_macro_file(), the_db); /* */ |
| 582 |
/* */ |
| 583 |
if (rsc_cnt_all || rsc_cnt_used) /* */ |
| 584 |
{ int i; /* */ |
| 585 |
int *cnt = db_count(the_db, (int*)NULL); /* */ |
| 586 |
/* */ |
| 587 |
ErrC('\n'); /* */ |
| 588 |
for (i = 0; i < c_len; ++i) /* */ |
| 589 |
{ if (rsc_cnt_all || c[i] > 0) /* */ |
| 590 |
{ ErrPrintF3("--- %-15s %5d read %5d written\n",/* */ |
| 591 |
SymbolValue(get_entry_type(i)), /* */ |
| 592 |
c[i], /* */ |
| 593 |
cnt[i]); /* */ |
| 594 |
} /* */ |
| 595 |
} /* */ |
| 596 |
free(c); /* */ |
| 597 |
} /* */ |
| 598 |
/* */ |
| 599 |
#ifdef SYMBOL_DUMP |
| 600 |
if (rsc_dump_symbols) sym_dump(); /* Write symbols. */ |
| 601 |
#endif |
| 602 |
/* */ |
| 603 |
#ifdef FREE_MEMORY |
| 604 |
free_db(the_db); /* */ |
| 605 |
the_db = NULL; /* */ |
| 606 |
sym_gc(); /* */ |
| 607 |
#ifdef SYMBOL_DUMP |
| 608 |
sym_dump(); /* */ |
| 609 |
#endif |
| 610 |
#endif |
| 611 |
return 0; /* */ |
| 612 |
} /*------------------------*/ |
| 613 |
|
| 614 |
/*----------------------------------------------------------------------------- |
| 615 |
** Function*: rec_gt() |
| 616 |
** Purpose: |
| 617 |
** |
| 618 |
** |
| 619 |
** Arguments: |
| 620 |
** r1 the first record |
| 621 |
** r2 the second record |
| 622 |
** Returns: |
| 623 |
**___________________________________________________ */ |
| 624 |
static int rec_gt(r1, r2) /* */ |
| 625 |
Record r1; /* */ |
| 626 |
Record r2; /* */ |
| 627 |
{ return symcmp(RecordSortkey(r1), /* */ |
| 628 |
RecordSortkey(r2)) < 0; /* */ |
| 629 |
} /*------------------------*/ |
| 630 |
|
| 631 |
/*----------------------------------------------------------------------------- |
| 632 |
** Function*: rec_lt() |
| 633 |
** Purpose: |
| 634 |
** |
| 635 |
** |
| 636 |
** Arguments: |
| 637 |
** r1 the first record |
| 638 |
** r2 the second record |
| 639 |
** Returns: |
| 640 |
**___________________________________________________ */ |
| 641 |
static int rec_lt(r1, r2) /* */ |
| 642 |
Record r1; /* */ |
| 643 |
Record r2; /* */ |
| 644 |
{ return symcmp(RecordSortkey(r1), /* */ |
| 645 |
RecordSortkey(r2)) > 0; /* */ |
| 646 |
} /*------------------------*/ |
| 647 |
|
| 648 |
/*----------------------------------------------------------------------------- |
| 649 |
** Function*: rec_gt_cased() |
| 650 |
** Type: static int |
| 651 |
** Purpose: |
| 652 |
** |
| 653 |
** Arguments: |
| 654 |
** r1 the first record |
| 655 |
** r2 the second record |
| 656 |
** Returns: |
| 657 |
**___________________________________________________ */ |
| 658 |
static int rec_gt_cased(r1, r2) /* */ |
| 659 |
Record r1; /* */ |
| 660 |
Record r2; /* */ |
| 661 |
{ return symcmp(get_key(RecordSortkey(r1)), /* */ |
| 662 |
get_key(RecordSortkey(r2))) < 0; /* */ |
| 663 |
} /*------------------------*/ |
| 664 |
|
| 665 |
/*----------------------------------------------------------------------------- |
| 666 |
** Function*: rec_lt_cased() |
| 667 |
** Type: static int |
| 668 |
** Purpose: |
| 669 |
** |
| 670 |
** Arguments: |
| 671 |
** r1 the first record |
| 672 |
** r2 the second record |
| 673 |
** Returns: |
| 674 |
**___________________________________________________ */ |
| 675 |
static int rec_lt_cased(r1, r2) /* */ |
| 676 |
Record r1; /* */ |
| 677 |
Record r2; /* */ |
| 678 |
{ return symcmp(get_key(RecordSortkey(r1)), /* */ |
| 679 |
get_key(RecordSortkey(r2))) > 0; /* */ |
| 680 |
} /*------------------------*/ |
| 681 |
|
| 682 |
/*----------------------------------------------------------------------------- |
| 683 |
** Function: do_keys() |
| 684 |
** Purpose: Recreate the key for a given record. |
| 685 |
** Arguments: |
| 686 |
** rec the record |
| 687 |
** Returns: |false| |
| 688 |
**___________________________________________________ */ |
| 689 |
static bool do_keys(db, rec) /* */ |
| 690 |
DB db; /* */ |
| 691 |
Record rec; /* */ |
| 692 |
{ /* */ |
| 693 |
rewrite_record(db, rec); /* */ |
| 694 |
sort_record(rec); /* */ |
| 695 |
make_key(db,rec); /* */ |
| 696 |
if (rsc_sort || rsc_double_check || need_sort_key)/* */ |
| 697 |
{ make_sort_key(db,rec); } /* */ |
| 698 |
return false; /* */ |
| 699 |
} /*------------------------*/ |
| 700 |
|
| 701 |
/*----------------------------------------------------------------------------- |
| 702 |
** Function*: do_no_keys() |
| 703 |
** Purpose: Do what's need to be done if no keys need to be created. |
| 704 |
** In fact the existing key is just marked. |
| 705 |
** Arguments: |
| 706 |
** rec the record |
| 707 |
** Returns: |false| |
| 708 |
**___________________________________________________ */ |
| 709 |
static bool do_no_keys(db, rec) /* */ |
| 710 |
DB db; /* */ |
| 711 |
Record rec; /* */ |
| 712 |
{ /* */ |
| 713 |
rewrite_record(db, rec); /* */ |
| 714 |
sort_record(rec); /* */ |
| 715 |
mark_key(db,rec); /* */ |
| 716 |
if (rsc_sort || rsc_double_check || need_sort_key)/* */ |
| 717 |
{ make_sort_key(db,rec); } /* */ |
| 718 |
return false; /* */ |
| 719 |
} /*------------------------*/ |
| 720 |
|
| 721 |
/*----------------------------------------------------------------------------- |
| 722 |
** Function*: update_crossref() |
| 723 |
** Purpose: |
| 724 |
** |
| 725 |
** |
| 726 |
** Arguments: |
| 727 |
** rec the record |
| 728 |
** first_rec the first record |
| 729 |
** Returns: |false| |
| 730 |
**___________________________________________________ */ |
| 731 |
static bool update_crossref(db, rec) /* */ |
| 732 |
DB db; /* */ |
| 733 |
Record rec; /* */ |
| 734 |
{ register Symbol *hp; /* */ |
| 735 |
register int i; /* */ |
| 736 |
Symbol s; /* */ |
| 737 |
String t; /* */ |
| 738 |
/* */ |
| 739 |
if (!RecordIsXREF(rec)) return false; /* */ |
| 740 |
/* */ |
| 741 |
for (i = RecordFree(rec), hp = RecordHeap(rec); /* search crossref field */ |
| 742 |
i > 0 && *hp != sym_crossref; /* */ |
| 743 |
i -= 2, hp += 2) /* */ |
| 744 |
{ } /* */ |
| 745 |
if (i <= 0) /* */ |
| 746 |
{ DebugPrint1("*** No crossref found."); /* */ |
| 747 |
return false; /* */ |
| 748 |
} /* */ |
| 749 |
/* */ |
| 750 |
t = SymbolValue(*++hp); t++; /* */ |
| 751 |
sp_open(t); /* Try to extract */ |
| 752 |
if ((s = SParseSymbol(&t)) == NO_SYMBOL) /* the crossref as symbol*/ |
| 753 |
{ return false; } /* */ |
| 754 |
/* */ |
| 755 |
if ((s = db_new_key(db, s)) == NO_SYMBOL) /* */ |
| 756 |
{ ERROR2("Crossref not found: ",(char*)s); /* */ |
| 757 |
return false; /* */ |
| 758 |
} /* */ |
| 759 |
if (rsc_key_case) { s = get_key(s); } /* */ |
| 760 |
if ((t=(String)malloc((size_t)symlen(s) + 3)) /* */ |
| 761 |
== StringNULL) /* get temp mem */ |
| 762 |
{ OUT_OF_MEMORY("update_crossref()"); } /* */ |
| 763 |
/* */ |
| 764 |
(void)sprintf((char*)t, /* */ |
| 765 |
(*SymbolValue(*hp) == '"' /* */ |
| 766 |
? "\"%s\"" /* */ |
| 767 |
: "{%s}"), /* */ |
| 768 |
SymbolValue(s)); /* make new crossref */ |
| 769 |
*hp = symbol(t); /* store new crossref */ |
| 770 |
free(t); /* free temp memory */ |
| 771 |
return false; /* */ |
| 772 |
} /*------------------------*/ |
| 773 |
|
| 774 |
#ifdef DEBUG |
| 775 |
/*----------------------------------------------------------------------------- |
| 776 |
** Function*: printchar() |
| 777 |
** Purpose: For debugging of regex.c |
| 778 |
** Arguments: |
| 779 |
** c Character to print. |
| 780 |
** Returns: nothing |
| 781 |
**___________________________________________________ */ |
| 782 |
void printchar(c) /* */ |
| 783 |
register char c; /* */ |
| 784 |
{ ErrC(c); /* */ |
| 785 |
} /*------------------------*/ |
| 786 |
#endif |