| 1 | /*** print.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 | ** Description: |
| 27 | ** This module provides also access to the functions and |
| 28 | ** variables defined in |entry.c|. Consult also the documentation |
| 29 | ** of this file for details. |
| 30 | ** |
| 31 | ** |
| 32 | ******************************************************************************/ |
| 33 | |
| 34 | #include <bibtool/general.h> |
| 35 | #include <bibtool/symbols.h> |
| 36 | #include <bibtool/print.h> |
| 37 | #include <bibtool/entry.h> |
| 38 | #include <bibtool/rsc.h> |
| 39 | #include <bibtool/macros.h> |
| 40 | #include <bibtool/sbuffer.h> |
| 41 | #include <bibtool/expand.h> |
| 42 | #include <bibtool/error.h> |
| 43 | |
| 44 | /*****************************************************************************/ |
| 45 | /* Internal Programs */ |
| 46 | /*===========================================================================*/ |
| 47 | |
| 48 | #ifdef __STDC__ |
| 49 | #define _ARG(A) A |
| 50 | #else |
| 51 | #define _ARG(A) () |
| 52 | #endif |
| 53 | char * sput_record _ARG((Record rec,DB db,String start));/* print.c */ |
| 54 | static int fput_char _ARG((int c)); /* print.c */ |
| 55 | static int sput_char _ARG((int c)); /* print.c */ |
| 56 | static void indent _ARG((int col,int (*fct)_ARG((int))));/* print.c */ |
| 57 | static void line_breaking _ARG((String t,int align,int (*fct)_ARG((int))));/* print.c*/ |
| 58 | static void print_equation _ARG((String pre,Symbol s,Symbol t,int align,int (*fct)_ARG((int))));/* print.c*/ |
| 59 | static void puts_in _ARG((String s,int in,int (*fct)_ARG((int))));/* print.c */ |
| 60 | void fput_record _ARG((FILE *file,Record rec,DB db,String start));/* print.c*/ |
| 61 | void put_record _ARG((int (*fct)_ARG((int)),Record rec,DB db,String start));/* print.c*/ |
| 62 | void set_key_type _ARG((String s)); /* print.c */ |
| 63 | void set_symbol_type _ARG((String s)); /* print.c */ |
| 64 | |
| 65 | /*****************************************************************************/ |
| 66 | /* External Programs */ |
| 67 | /*===========================================================================*/ |
| 68 | |
| 69 | /*---------------------------------------------------------------------------*/ |
| 70 | |
| 71 | #define TAB_WIDTH 8 |
| 72 | |
| 73 | static int symbol_type = SYMBOL_TYPE_LOWER; /* */ |
| 74 | |
| 75 | static String s_upper = (String)"upper"; /* */ |
| 76 | static String s_lower = (String)"lower"; /* */ |
| 77 | static String s_cased = (String)"cased"; /* */ |
| 78 | |
| 79 | /*----------------------------------------------------------------------------- |
| 80 | ** Function: set_symbol_type() |
| 81 | ** Purpose: Function to set the symbol type which is used by the |
| 82 | ** printing routine. The argument is a string describing |
| 83 | ** the value to use. Possible values are |"upper"|, |
| 84 | ** |"lower"|, and |"cased"|. The comparison of the values |
| 85 | ** is performed case insensitive. |
| 86 | ** |
| 87 | ** If no appropriate value is found then an error message |
| 88 | ** is issued as the only action. |
| 89 | ** |
| 90 | ** This function is called from \File{rsc.c}. |
| 91 | ** Arguments: |
| 92 | ** s String description of the value. |
| 93 | ** Returns: nothing |
| 94 | **___________________________________________________ */ |
| 95 | void set_symbol_type(s) /* */ |
| 96 | register String s; /* */ |
| 97 | { if ( case_eq(s, s_upper) ) /* */ |
| 98 | { symbol_type = SYMBOL_TYPE_UPPER; } /* */ |
| 99 | else if ( case_eq(s, s_cased) ) /* */ |
| 100 | { symbol_type = SYMBOL_TYPE_CASED; } /* */ |
| 101 | else if ( case_eq(s, s_lower) ) /* */ |
| 102 | { symbol_type = SYMBOL_TYPE_LOWER; } /* */ |
| 103 | else /* */ |
| 104 | { Err("Unknown symbol type ignored.\n"); } /* */ |
| 105 | } /*------------------------*/ |
| 106 | |
| 107 | /*----------------------------------------------------------------------------- |
| 108 | ** Function: get_symbol_type() |
| 109 | ** Type: String |
| 110 | ** Purpose: Getter for the symbol type. |
| 111 | ** Arguments: none |
| 112 | ** Returns: one of the values SYMBOL\_TYPE\_UPPER, SYMBOL\_TYPE\_LOWER, |
| 113 | ** or SYMBOL\_TYPE\_CASED as defined in header \textsf{type.h} |
| 114 | **___________________________________________________ */ |
| 115 | String get_symbol_type() /* */ |
| 116 | { switch (symbol_type) /* */ |
| 117 | { case SYMBOL_TYPE_UPPER: return s_upper; /* */ |
| 118 | case SYMBOL_TYPE_LOWER: return s_lower; /* */ |
| 119 | case SYMBOL_TYPE_CASED: return s_cased; /* */ |
| 120 | default: return NULL; /* */ |
| 121 | } /* */ |
| 122 | } /*------------------------*/ |
| 123 | |
| 124 | #ifdef MAYBE_IN_THE_NEXT_RELEASE |
| 125 | |
| 126 | static int key_type = SYMBOL_TYPE_LOWER; /* */ |
| 127 | |
| 128 | /*----------------------------------------------------------------------------- |
| 129 | ** Function*: set_key_type() |
| 130 | ** Purpose: Wrapper function to set the static variable key_type. |
| 131 | ** This function is called from rsc.c |
| 132 | ** Arguments: |
| 133 | ** s String description of the value. |
| 134 | ** Returns: nothing |
| 135 | **___________________________________________________ */ |
| 136 | void set_key_type(s) /* */ |
| 137 | register String s; /* */ |
| 138 | { if ( case_cmp(s,s_upper) ) /* */ |
| 139 | { key_type = SYMBOL_TYPE_UPPER; } /* */ |
| 140 | else if ( case_cmp(s,s_cased) ) /* */ |
| 141 | { key_type = SYMBOL_TYPE_CASED; } /* */ |
| 142 | else if ( case_cmp(s,s_lower) ) /* */ |
| 143 | { key_type = SYMBOL_TYPE_LOWER; } /* */ |
| 144 | else /* */ |
| 145 | { Err("Unknown key type ignored.\n"); } /* */ |
| 146 | } /*------------------------*/ |
| 147 | #endif |
| 148 | |
| 149 | /*------------------------*/ |
| 150 | static int column = 0; /* The current column of */ |
| 151 | /* the output stream is */ |
| 152 | /* kept in this variable.*/ |
| 153 | |
| 154 | #define NL (void)(*fct)('\n'),column=0 |
| 155 | #define PUTC(C) (void)((*fct)(C),++column) |
| 156 | #define PUTS(S) puts_in((String)(S), 0, fct) |
| 157 | |
| 158 | /*----------------------------------------------------------------------------- |
| 159 | ** Function: puts_in() |
| 160 | ** Purpose: Print a string and update current column. |
| 161 | ** Arguments: |
| 162 | ** s string to be printed. |
| 163 | ** in indentation. Alignment column. |
| 164 | ** fct function to use for writing a character. |
| 165 | ** Returns: nothing |
| 166 | **___________________________________________________ */ |
| 167 | static void puts_in(s,in,fct) /* */ |
| 168 | register String s; /* */ |
| 169 | register int in; /* */ |
| 170 | int (*fct)_ARG((int)); /* */ |
| 171 | { /* */ |
| 172 | while ( *s ) /* */ |
| 173 | { (void)(*fct)(*s); /* */ |
| 174 | switch ( *(s++) ) /* */ |
| 175 | { case '\t': /* */ |
| 176 | column += TAB_WIDTH - (column%TAB_WIDTH); /* */ |
| 177 | break; /* */ |
| 178 | case '\n': /* */ |
| 179 | column = 0; /* */ |
| 180 | if ( in > 0 ) indent(in, fct); /* */ |
| 181 | break; /* */ |
| 182 | default: ++column; /* */ |
| 183 | } /* */ |
| 184 | } /* */ |
| 185 | } /*------------------------*/ |
| 186 | |
| 187 | /*----------------------------------------------------------------------------- |
| 188 | ** Function: indent() |
| 189 | ** Purpose: Add spaces or tabs to indent to the given column. |
| 190 | ** If the current column is beyond col then nothing happens. |
| 191 | ** The resource use.tabs can be used to disable the use of TAB. |
| 192 | ** Arguments: |
| 193 | ** col Target column |
| 194 | ** fct function to use for writing a character. |
| 195 | ** Returns: nothing |
| 196 | **___________________________________________________ */ |
| 197 | static void indent(col,fct) /* */ |
| 198 | register int col; /* */ |
| 199 | int (*fct)_ARG((int)); /* */ |
| 200 | { /* */ |
| 201 | if ( col > rsc_linelen ) col = rsc_linelen; /* */ |
| 202 | while ( column < col ) /* */ |
| 203 | { if ( rsc_use_tabs /* TAB is allowed and */ |
| 204 | && column+TAB_WIDTH-(column%TAB_WIDTH) <= col )/* enough space left */ |
| 205 | { (void)(*fct)('\t'); /* then put a TAB and */ |
| 206 | column += TAB_WIDTH - (column%TAB_WIDTH); /* update column. */ |
| 207 | } /* */ |
| 208 | else /* otherwise */ |
| 209 | { (void)(*fct)(' '); /* write a single space */ |
| 210 | ++column; /* and advance column. */ |
| 211 | } /* */ |
| 212 | } /* */ |
| 213 | } /*------------------------*/ |
| 214 | |
| 215 | /*----------------------------------------------------------------------------- |
| 216 | ** Function: line_breaking() |
| 217 | ** Purpose: Write out a right hand side of an equation. |
| 218 | ** If it does not fit break the line into several parts and |
| 219 | ** print them on successive lines. |
| 220 | ** Temporarily end marks are placed inside the string, |
| 221 | ** but the old contents has been restored at the end. |
| 222 | ** Arguments: |
| 223 | ** t string to print. |
| 224 | ** align starting column for continuation lines. |
| 225 | ** fct function to use for writing a character. |
| 226 | ** Returns: nothing |
| 227 | **___________________________________________________ */ |
| 228 | static void line_breaking(t, align, fct) /* */ |
| 229 | register String t; /* string to print. */ |
| 230 | int align; /* alignment column */ |
| 231 | int (*fct)_ARG((int)); /* */ |
| 232 | { register String s; /* intermediate pointer */ |
| 233 | char end_c; /* temp. character. */ |
| 234 | int brace, /* brace counter */ |
| 235 | len; /* length of rem. output */ |
| 236 | bool first = true; /* indicator for # */ |
| 237 | /* */ |
| 238 | while (is_space(*t)) ++t; /* skip leading spaces */ |
| 239 | /* */ |
| 240 | indent(align, fct); /* goto alignment column */ |
| 241 | /* */ |
| 242 | while (*t) /* as long as sth to print*/ |
| 243 | { s = t; /* */ |
| 244 | /* */ |
| 245 | switch (*t) /* */ |
| 246 | { case '"': /* QUOTED PART */ |
| 247 | for ( len = 2, ++t; /* */ |
| 248 | *t && *t != '\"'; /* Search terminating " */ |
| 249 | ++t, ++len ) /* */ |
| 250 | { if ( *t == '\\' && *(t+1) != '\0' ) /* skip over quoted and */ |
| 251 | { ++t; ++len; } /* similar constructs. */ |
| 252 | } /* */ |
| 253 | if ( *t ) ++t; /* skip after end, if poss*/ |
| 254 | if ( *t ) { end_c = *t; *t = '\0'; } /* save char and mark end.*/ |
| 255 | else { end_c = *t; } /* */ |
| 256 | break; /* */ |
| 257 | case '{': /* BRACED PART */ |
| 258 | brace = 1; /* */ |
| 259 | for ( len = 2, ++t; /* find matching brace. */ |
| 260 | *t && brace > 0; /* */ |
| 261 | ++t, ++len ) /* */ |
| 262 | { switch ( *t ) /* */ |
| 263 | { case '\\': if ( *(t+1) ) ++t; break; /* ignore \{ \} etc */ |
| 264 | case '{': ++brace; break; /* */ |
| 265 | case '}': brace--; break; /* */ |
| 266 | } /* */ |
| 267 | } /* */ |
| 268 | if ( *t ) { end_c = *t; *t = '\0'; } /* save char and mark end.*/ |
| 269 | else { end_c = *t; } /* */ |
| 270 | break; /* */ |
| 271 | default: /* Now we should have a */ |
| 272 | while ( is_allowed(*t) ) ++t; /* SYMBOL */ |
| 273 | end_c = *t; *t = '\0'; /* */ |
| 274 | s = SymbolValue(get_item(symbol(s), /* */ |
| 275 | symbol_type)); /* */ |
| 276 | len = strlen((char*)s); /* */ |
| 277 | } /* */ |
| 278 | /* Now s is a single */ |
| 279 | /* string to print. */ |
| 280 | /* t points to the closing*/ |
| 281 | /* '\0' of s */ |
| 282 | /* end_c is the old *t */ |
| 283 | while ( *s ) /* */ |
| 284 | { if ( len + (first?0:3) <= rsc_linelen - column)/* Is there enough space*/ |
| 285 | { if ( !first ) PUTS(" # "); /* Maybe add separator */ |
| 286 | puts_in(s, align, fct); /* write it out */ |
| 287 | s = t; /* and we are done */ |
| 288 | } /* */ |
| 289 | else if ( !first ) /* If sth has been before */ |
| 290 | { puts_in((String)"\n# ", align - 2, fct); /* start a new line */ |
| 291 | first = true; /* */ |
| 292 | } /* Now we have to break */ |
| 293 | else /* a single entry */ |
| 294 | { Uchar save_c; /* */ |
| 295 | String save_ptr, /* */ |
| 296 | ptr; /* */ |
| 297 | /* */ |
| 298 | if ( 0 <= rsc_linelen - column ) /* */ |
| 299 | save_ptr = s + rsc_linelen - column; /* Potential end */ |
| 300 | else /* */ |
| 301 | save_ptr = s; /* */ |
| 302 | /* */ |
| 303 | for ( ptr = s; /* Search next newline */ |
| 304 | ptr < save_ptr && *ptr != '\n'; /* or end of region */ |
| 305 | ptr++ ) {} /* */ |
| 306 | /* */ |
| 307 | if ( *ptr == '\n' ) /* */ |
| 308 | { save_ptr = ptr; /* */ |
| 309 | *save_ptr = '\0'; /* Save and mark end. */ |
| 310 | puts_in(s, align, fct); /* */ |
| 311 | NL; /* */ |
| 312 | indent(align,fct); /* */ |
| 313 | *save_ptr = '\n'; /* Restore end */ |
| 314 | len += s - save_ptr - 1; /* Update the length */ |
| 315 | s = save_ptr + 1; /* */ |
| 316 | } /* */ |
| 317 | else /* */ |
| 318 | { /* */ |
| 319 | while ( save_ptr != s && *save_ptr != ' ' )/* */ |
| 320 | { save_ptr--; } /* Find a SPC backward */ |
| 321 | /* */ |
| 322 | if ( save_ptr == s ) /* If no SPC found then */ |
| 323 | { while ( *save_ptr && *save_ptr != ' ' )/* search one forward. */ |
| 324 | { save_ptr++; } /* */ |
| 325 | } /* */ |
| 326 | len += s - save_ptr; /* Update the length */ |
| 327 | save_c = *save_ptr; *save_ptr = '\0'; /* Save and mark end. */ |
| 328 | puts_in(s, align, fct); /* */ |
| 329 | if (save_c != 0) /* */ |
| 330 | { NL; /* */ |
| 331 | indent(align, fct); /* */ |
| 332 | } /* */ |
| 333 | *save_ptr = save_c; /* Restore end */ |
| 334 | s = save_ptr; /* */ |
| 335 | while ( is_space(*s) ) { s++; len--; } /* Skip spaces */ |
| 336 | } /* */ |
| 337 | } /* */ |
| 338 | } /* */ |
| 339 | *t = end_c; /* Restore the end */ |
| 340 | /* */ |
| 341 | while ( *t && *t != '#' ) ++t; /* Search next # */ |
| 342 | if ( *t ) ++t; /* Skip beyond the # */ |
| 343 | while ( is_space(*t) ) ++t; /* Ignore following spaces*/ |
| 344 | first = false; /* */ |
| 345 | } /* */ |
| 346 | } /*------------------------*/ |
| 347 | |
| 348 | /*----------------------------------------------------------------------------- |
| 349 | ** Function: print_equation() |
| 350 | ** Purpose: Print something of the form s = t |
| 351 | ** If desired it can be indented. t is broken if it doesn't fit |
| 352 | ** in one line. |
| 353 | ** Arguments: |
| 354 | ** pre string to be printed before s. |
| 355 | ** s left hand side |
| 356 | ** t right hand side |
| 357 | ** align target column. If negative no indentation is performed. |
| 358 | ** fct function to use for writing a character. |
| 359 | ** Returns: nothing |
| 360 | **___________________________________________________' */ |
| 361 | static void print_equation(pre, lhs, rhs, align, fct)/* */ |
| 362 | String pre; /* */ |
| 363 | Symbol lhs; /* */ |
| 364 | Symbol rhs; /* */ |
| 365 | int align; /* */ |
| 366 | int (*fct)_ARG((int)); /* */ |
| 367 | { /* */ |
| 368 | if ( align < 0 ) /* */ |
| 369 | { PUTS(pre); /* */ |
| 370 | PUTS(SymbolValue(get_item(lhs, symbol_type))); /* */ |
| 371 | PUTS(rsc_print_we ? " = " : "="); /* */ |
| 372 | } /* */ |
| 373 | else /* */ |
| 374 | { indent(rsc_indent, fct); /* */ |
| 375 | PUTS(pre); /* */ |
| 376 | PUTS(SymbolValue(get_item(lhs, symbol_type))); /* */ |
| 377 | if ( column >= align - 2 && rsc_print_we ) /* */ |
| 378 | { PUTC(' '); } /* */ |
| 379 | else if ( rsc_eq_right ) /* */ |
| 380 | { indent(align - 2, fct); } /* */ |
| 381 | else if ( column < align || rsc_print_we ) /* */ |
| 382 | { PUTC(' '); } /* */ |
| 383 | PUTC('='); /* */ |
| 384 | if ( rsc_print_we ) { PUTC(' '); } /* */ |
| 385 | line_breaking(SymbolValue(rhs), align, fct); /* */ |
| 386 | } /* */ |
| 387 | } /*------------------------*/ |
| 388 | |
| 389 | static FILE * ofile=NULL; |
| 390 | |
| 391 | /*----------------------------------------------------------------------------- |
| 392 | ** Function: fput_char() |
| 393 | ** Purpose: Output function which places the character on the |ofile| |
| 394 | ** stream. |
| 395 | ** Arguments: |
| 396 | ** c Character to print. |
| 397 | ** Returns: The return status of |fputc()|. |
| 398 | **___________________________________________________ */ |
| 399 | static int fput_char(c) /* */ |
| 400 | int c; /* */ |
| 401 | { return fputc(c, ofile); /* */ |
| 402 | } /*------------------------*/ |
| 403 | |
| 404 | /*----------------------------------------------------------------------------- |
| 405 | ** Function: fput_record() |
| 406 | ** Purpose: Format and print a complete record onto a given stream. |
| 407 | ** for further details see |put_record()|. |
| 408 | ** Arguments: |
| 409 | ** file Stream to print onto. |
| 410 | ** db Database containing the record. |
| 411 | ** rec Record to print. |
| 412 | ** start Initial string used before the type. Should be "@" normally. |
| 413 | ** Returns: nothing |
| 414 | **___________________________________________________ */ |
| 415 | void fput_record(file, rec, db, start) /* */ |
| 416 | FILE *file; /* */ |
| 417 | DB db; /* */ |
| 418 | Record rec; /* record to print */ |
| 419 | String start; /* initial string = "@" */ |
| 420 | { /* */ |
| 421 | ofile = file; /* */ |
| 422 | put_record(fput_char, rec, db, start); /* */ |
| 423 | } /*------------------------*/ |
| 424 | |
| 425 | static StringBuffer *osb = NULL; |
| 426 | |
| 427 | /*----------------------------------------------------------------------------- |
| 428 | ** Function: sput_char() |
| 429 | ** Purpose: Output function which places the character on the |osb| |
| 430 | ** string buffer. |
| 431 | ** Arguments: |
| 432 | ** c Character to print. |
| 433 | ** Returns: The return status of |fputc()|. |
| 434 | **___________________________________________________ */ |
| 435 | static int sput_char(c) /* */ |
| 436 | int c; /* */ |
| 437 | { return sbputchar(c, osb); /* */ |
| 438 | } /*------------------------*/ |
| 439 | |
| 440 | /*----------------------------------------------------------------------------- |
| 441 | ** Function: sput_record() |
| 442 | ** Purpose: Format and print a complete record into a string and return it. |
| 443 | ** The string returned points to static memory which is |
| 444 | ** reused upon the next invocation of this function. |
| 445 | ** Arguments: |
| 446 | ** file Stream to print onto. |
| 447 | ** db Database containing the record. |
| 448 | ** rec Record to print. |
| 449 | ** start Initial string used before the type. Should be "@" normally. |
| 450 | ** Returns: The string containing the printed representation. |
| 451 | **___________________________________________________ */ |
| 452 | char * sput_record(rec, db, start) /* */ |
| 453 | DB db; /* */ |
| 454 | Record rec; /* record to print */ |
| 455 | String start; /* initial string = "@" */ |
| 456 | { /* */ |
| 457 | if ( osb == NULL ) osb = sbopen(); /* */ |
| 458 | sbrewind(osb); /* */ |
| 459 | put_record(sput_char, rec, db, start); /* */ |
| 460 | return sbflush(osb); /* */ |
| 461 | } /*------------------------*/ |
| 462 | |
| 463 | /*----------------------------------------------------------------------------- |
| 464 | ** Function: put_record() |
| 465 | ** Purpose: Format and print a complete record. |
| 466 | ** The record type and several resources are taken into |
| 467 | ** account. The following external variables (from |
| 468 | ** |rsc.c|) are taken into account: |
| 469 | ** \begin{description} |
| 470 | ** \item[rsc\_parentheses] If this boolean variable is |
| 471 | ** |true| then |(| and |)| are used to delimit the |
| 472 | ** record. Otherwise |{| and |}| are used. |
| 473 | ** \item[rsc\_col\_p] This integer variable controls the |
| 474 | ** indentation of preamble records. |
| 475 | ** \item[rsc\_col\_s] This integer variable controls the |
| 476 | ** indentation of string records. |
| 477 | ** \item[rsc\_expand\_macros] If this boolean variable is |
| 478 | ** set then macros are expanded before the record is |
| 479 | ** printed. This does not effect the internal |
| 480 | ** representation. |
| 481 | ** \item[rsc\_col] This integer variable controls the |
| 482 | ** indentation of normal records. |
| 483 | ** \item[rsc\_col\_key] This integer variable controls the |
| 484 | ** indentation of the key in a normal record. |
| 485 | ** \item[rsc\_newlines] This integer variable controls |
| 486 | ** the number of newlines printed after a normal record. |
| 487 | ** \item[rsc\_linelen] This integer variable controls |
| 488 | ** the length of the line. The line breaking algorithm |
| 489 | ** is applied if this column is about to be violated. |
| 490 | ** \item[rsc\_indent] This integer variable controls the |
| 491 | ** indentation of equations. |
| 492 | ** \item[rsc\_eq\_right] This boolean variable controls |
| 493 | ** the alignment of the |=| in equations. It it is set |
| 494 | ** then the equality sign is flushed right. Otherwise it |
| 495 | ** is flushed left. |
| 496 | ** \end{description} |
| 497 | ** |
| 498 | ** The field in the record are sorted with |
| 499 | ** |sort_record()| before they are printed. |
| 500 | ** |
| 501 | ** In normal records all fields not starting with an |
| 502 | ** allowed character are ignored. Thus it is possible to |
| 503 | ** store private and invisible information in a |
| 504 | ** field. Simply start the field name with an not allowed |
| 505 | ** character like |%|. |
| 506 | ** Arguments: |
| 507 | ** fct function to use for writing a character. |
| 508 | ** db Database containing the record. |
| 509 | ** rec Record to print. |
| 510 | ** start Initial string used before the type. Should be "@" normally. |
| 511 | ** Returns: nothing |
| 512 | **___________________________________________________ */ |
| 513 | void put_record(fct, rec, db, start) /* */ |
| 514 | int (*fct)_ARG((int)); /* */ |
| 515 | Record rec; /* */ |
| 516 | DB db; /* */ |
| 517 | String start; /* initial string = "@" */ |
| 518 | { Symbol *hp; /* heap pointer */ |
| 519 | unsigned int i; /* */ |
| 520 | char open_brace, close_brace; /* */ |
| 521 | static int first = 1; /* */ |
| 522 | /* */ |
| 523 | sort_record(rec); /* */ |
| 524 | /* */ |
| 525 | hp = RecordHeap(rec); /* */ |
| 526 | if ( rsc_no_nl && first ) { first = 0; } /* */ |
| 527 | else if ( IsNormalRecord(RecordType(rec)) ) { NL; }/* */ |
| 528 | /* */ |
| 529 | if ( *SymbolValue(RecordComment(rec)) ) /* */ |
| 530 | { PUTS(SymbolValue(RecordComment(rec))); /* */ |
| 531 | NL; /* */ |
| 532 | } /* */ |
| 533 | /* */ |
| 534 | if (rsc_parentheses) /* */ |
| 535 | { open_brace = '('; /* */ |
| 536 | close_brace = ')'; /* */ |
| 537 | } /* */ |
| 538 | else /* */ |
| 539 | { open_brace = '{'; /* */ |
| 540 | close_brace = '}'; /* */ |
| 541 | } /* */ |
| 542 | /* */ |
| 543 | switch (RecordType(rec)) /* */ |
| 544 | { case BIB_COMMENT: /* */ |
| 545 | #ifdef OLD |
| 546 | indent(rsc_col_c, fct); /* */ |
| 547 | PUTS(*hp); /* */ |
| 548 | PUTC(' '); /* */ |
| 549 | NL; /* */ |
| 550 | #endif |
| 551 | break; /* */ |
| 552 | case BIB_PREAMBLE: /* */ |
| 553 | PUTS(start); /* */ |
| 554 | PUTS(SymbolValue(EntryName(RecordType(rec))));/* */ |
| 555 | PUTC(open_brace); /* */ |
| 556 | indent(rsc_col_p,fct); /* */ |
| 557 | line_breaking(SymbolValue(*hp), /* */ |
| 558 | rsc_col_p, /* */ |
| 559 | fct); /* */ |
| 560 | PUTC(' '); /* */ |
| 561 | PUTC(close_brace); /* */ |
| 562 | NL; /* */ |
| 563 | break; /* */ |
| 564 | case BIB_STRING: /* */ |
| 565 | PUTS(start); /* */ |
| 566 | PUTS(SymbolValue(EntryName(RecordType(rec))));/* */ |
| 567 | PUTC(open_brace); /* */ |
| 568 | print_equation(s_empty, /* */ |
| 569 | *hp, /* */ |
| 570 | *(hp+1), /* */ |
| 571 | rsc_col_s, /* */ |
| 572 | fct); /* */ |
| 573 | PUTC(' '); /* */ |
| 574 | PUTC(close_brace); /* */ |
| 575 | NL; /* */ |
| 576 | break; /* */ |
| 577 | case BIB_ALIAS: /* */ |
| 578 | PUTS(start); /* */ |
| 579 | PUTS(SymbolValue(EntryName(RecordType(rec))));/* */ |
| 580 | PUTC(open_brace); /* */ |
| 581 | print_equation(s_empty, /* */ |
| 582 | *hp, /* */ |
| 583 | *(hp+1), /* */ |
| 584 | rsc_col_s, /* */ |
| 585 | fct); /* */ |
| 586 | PUTC(' '); /* */ |
| 587 | PUTC(close_brace); /* */ |
| 588 | NL; /* */ |
| 589 | break; /* */ |
| 590 | case BIB_INCLUDE: /* */ |
| 591 | PUTS(start); /* */ |
| 592 | PUTS(SymbolValue(EntryName(RecordType(rec))));/* */ |
| 593 | PUTC(open_brace); /* */ |
| 594 | PUTS(SymbolValue(*RecordHeap(rec))); /* */ |
| 595 | PUTC(close_brace); /* */ |
| 596 | NL; /* */ |
| 597 | break; /* */ |
| 598 | case BIB_MODIFY: /* */ |
| 599 | default: /* */ |
| 600 | /* */ |
| 601 | if (RecordType(rec) == type_xdata /* */ |
| 602 | && rsc_expand_xdata) return; /* */ |
| 603 | /* */ |
| 604 | PUTS(start); /* */ |
| 605 | PUTS(SymbolValue(EntryName(RecordType(rec))));/* */ |
| 606 | PUTC(open_brace); /* */ |
| 607 | { String comma1 = s_empty, /* */ |
| 608 | comma2 = (String)","; /* */ |
| 609 | /* */ |
| 610 | if (rsc_print_ce) /* */ |
| 611 | { comma1 = comma2; /* */ |
| 612 | comma2 = s_empty; /* */ |
| 613 | } /* */ |
| 614 | /* */ |
| 615 | for ( i = RecordFree(rec); i > 0; i -= 2 ) /* */ |
| 616 | { /* No deleted or */ |
| 617 | if (*hp && is_allowed(*SymbolValue(*hp)))/* private entry */ |
| 618 | { /* */ |
| 619 | if ( *(hp+1) ) /* If equation */ |
| 620 | { PUTS(comma1); /* */ |
| 621 | NL; /* */ |
| 622 | print_equation(comma2, /* */ |
| 623 | *hp, /* */ |
| 624 | (rsc_expand_macros /* */ |
| 625 | ? expand_rhs(*(hp+1),/* */ |
| 626 | (rsc_braces/* */ |
| 627 | ? sym_open_brace/* */ |
| 628 | : sym_double_quote),/* */ |
| 629 | (rsc_braces/* */ |
| 630 | ? sym_close_brace/* */ |
| 631 | : sym_double_quote),/* */ |
| 632 | db, /* */ |
| 633 | false) /* */ |
| 634 | : *(hp+1) ), /* */ |
| 635 | rsc_col, /* */ |
| 636 | fct); /* */ |
| 637 | } /* */ |
| 638 | else /* Otherwise print a key */ |
| 639 | { indent(rsc_col_key, fct); /* */ |
| 640 | PUTS(SymbolValue(get_key(*hp))); /* */ |
| 641 | } /* */ |
| 642 | } /* */ |
| 643 | hp += 2; /* Goto next pair. */ |
| 644 | } /* */ |
| 645 | } /* */ |
| 646 | if (rsc_print_tc) { PUTC(','); } /* */ |
| 647 | NL; /* */ |
| 648 | PUTC(close_brace); /* */ |
| 649 | for ( i = rsc_newlines; i > 0; --i ) { NL; } /* */ |
| 650 | } /* */ |
| 651 | } /*------------------------*/ |