| 1 | /*** rsc.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 contains functions which deal with resources. |
| 28 | ** Resources are commands to configure the behaviour of |
| 29 | ** \BibTool. They can be read either from a file or from a |
| 30 | ** string. |
| 31 | ** |
| 32 | ** The syntax of resources are modelled after the syntax rules |
| 33 | ** for \BibTeX{} files. See the user's guide for details of the syntax. |
| 34 | ** |
| 35 | ******************************************************************************/ |
| 36 | |
| 37 | #include <bibtool/general.h> |
| 38 | #include <bibtool/error.h> |
| 39 | #include <bibtool/symbols.h> |
| 40 | #include <bibtool/macros.h> |
| 41 | #include <bibtool/parse.h> |
| 42 | #include <bibtool/print.h> |
| 43 | #include <bibtool/tex_aux.h> |
| 44 | #include <bibtool/tex_read.h> |
| 45 | #include <bibtool/check.h> |
| 46 | #include <bibtool/crossref.h> |
| 47 | #include <bibtool/key.h> |
| 48 | #include <bibtool/rewrite.h> |
| 49 | #include <bibtool/s_parse.h> |
| 50 | #include <bibtool/entry.h> |
| 51 | #include <bibtool/type.h> |
| 52 | #include <bibtool/version.h> |
| 53 | #include <bibtool/io.h> |
| 54 | #include "config.h" |
| 55 | #define RSC_INIT |
| 56 | #include <bibtool/rsc.h> |
| 57 | |
| 58 | /*****************************************************************************/ |
| 59 | /* Internal Programs */ |
| 60 | /*===========================================================================*/ |
| 61 | |
| 62 | #ifdef __STDC__ |
| 63 | #define _ARG(A) A |
| 64 | #else |
| 65 | #define _ARG(A) () |
| 66 | #endif |
| 67 | static bool test_true _ARG((Symbol s)); /* */ |
| 68 | static void init_rsc _ARG((void)); /* */ |
| 69 | |
| 70 | #define NoRscError(X) WARNING3("Resource file ",X," not found.") |
| 71 | |
| 72 | /*****************************************************************************/ |
| 73 | /* External Programs */ |
| 74 | /*===========================================================================*/ |
| 75 | |
| 76 | #ifndef __STDC__ |
| 77 | #ifndef HAVE_GETENV |
| 78 | extern char * getenv _ARG((char* name)); /* */ |
| 79 | #endif |
| 80 | #endif |
| 81 | |
| 82 | /*---------------------------------------------------------------------------*/ |
| 83 | |
| 84 | #define RscNumeric(SYM,S,V,I) static Symbol S = NO_SYMBOL; |
| 85 | #define RscString(SYM,S,V,I) static Symbol S = NO_SYMBOL; |
| 86 | #define RscBoolean(SYM,S,V,I) static Symbol S = NO_SYMBOL; |
| 87 | #define RscByFct(SYM,S,FCT) static Symbol S = NO_SYMBOL; |
| 88 | #include <bibtool/resource.h> |
| 89 | |
| 90 | /*----------------------------------------------------------------------------- |
| 91 | ** Function: init_rsc() |
| 92 | ** Purpose: This function performs resource initializations. It |
| 93 | ** is necessary to call this function before the other |
| 94 | ** functions in this module can be expected to work |
| 95 | ** properly. Thus it is called at the beginning of the |
| 96 | ** functions is question. |
| 97 | ** Details: |
| 98 | ** Initialize the key words. |
| 99 | ** Tricky use of CPP. The header file resource.h is included |
| 100 | ** for the second time. To avoid confusion in makedepend the |
| 101 | ** hader file is only included if MAKEDPEND is not defined. |
| 102 | ** The same trick can be found again later. |
| 103 | ** Arguments: none |
| 104 | ** Returns: nothing |
| 105 | **___________________________________________________ */ |
| 106 | static void init_rsc() /* */ |
| 107 | { /* */ |
| 108 | #define RscNumeric(SYM,S,V,I) S = symbol((String)SYM); |
| 109 | #define RscString(SYM,S,V,I) S = symbol((String)SYM); |
| 110 | #define RscBoolean(SYM,S,V,I) S = symbol((String)SYM); |
| 111 | #define RscByFct(SYM,S,FCT) S = symbol((String)SYM); |
| 112 | #ifndef MAKEDEPEND |
| 113 | #include <bibtool/resource.h> |
| 114 | #endif |
| 115 | } /*------------------------*/ |
| 116 | |
| 117 | /*----------------------------------------------------------------------------- |
| 118 | ** Function: search_rsc() |
| 119 | ** Purpose: Try to open the resource file at different places: |
| 120 | ** \begin{itemize} |
| 121 | ** \item In the place indicated by the environment variable |
| 122 | ** |RSC_ENV_VAR|. This step is skipped if the macro |
| 123 | ** |RSC_ENV_VAR| is not defined (at compile time of the |
| 124 | ** module). |
| 125 | ** \item In the home directory. The home directory is |
| 126 | ** determined by an environment variable. The macro |
| 127 | ** |HOME_ENV_VAR| contains the name of this environment |
| 128 | ** variable. If this macro is not defined (at |
| 129 | ** compile time of the module) then this step is skipped. |
| 130 | ** \item In the usual place for resource files. |
| 131 | ** \end{itemize} |
| 132 | ** For each step |load_rsc()| is called until it |
| 133 | ** succeeds. |
| 134 | ** |
| 135 | ** The files sought is determined by the macro |
| 136 | ** |DefaultResourceFile| at compile time of the |
| 137 | ** module. (see |bibtool.h|) |
| 138 | ** Arguments: none |
| 139 | ** Returns: |true| iff the resource loading succeeds somewhere. |
| 140 | **___________________________________________________ */ |
| 141 | bool search_rsc() /* */ |
| 142 | { static String def = (String)DefaultResourceFile; /* */ |
| 143 | register char *ap; /* */ |
| 144 | register char *fn; /* */ |
| 145 | size_t len; /* */ |
| 146 | bool found; |
| 147 | /* */ |
| 148 | #ifdef RSC_ENV_VAR |
| 149 | if ( (ap=getenv(RSC_ENV_VAR)) != NULL /* Try to get the name */ |
| 150 | && load_rsc((String)ap) ) return true; /* from the environment. */ |
| 151 | #endif |
| 152 | /* */ |
| 153 | #ifdef HOME_ENV_VAR |
| 154 | if ( (ap=getenv(HOME_ENV_VAR)) != NULL ) /* Try to get the resource*/ |
| 155 | { len = strlen(ap) + /* */ |
| 156 | strlen(DIR_SEP) + /* */ |
| 157 | strlen((char*)def) + 1; /* file from the home */ |
| 158 | if ((fn=malloc(len)) != NULL) /* directory */ |
| 159 | { (void)strcpy(fn, ap); /* */ |
| 160 | (void)strcat(fn, DIR_SEP); /* */ |
| 161 | (void)strcat(fn, (char*)def); /* */ |
| 162 | found = load_rsc((String)fn); /* */ |
| 163 | free(fn); /* */ |
| 164 | if (found) return true; /* */ |
| 165 | } /* */ |
| 166 | } /* */ |
| 167 | #endif |
| 168 | /* if all fails then try */ |
| 169 | return load_rsc(def); /* to use a default. */ |
| 170 | } /*------------------------*/ |
| 171 | |
| 172 | /*----------------------------------------------------------------------------- |
| 173 | ** Function: load_rsc() |
| 174 | ** Purpose: This function tries to load a resource file. |
| 175 | ** Details: |
| 176 | ** Perform initialization if required. |
| 177 | ** The main job is done by |read_rsc()|. This function is located |
| 178 | ** in |parse.c| since it shares subroutines with the parser. |
| 179 | ** Arguments: |
| 180 | ** name The name of the resource file to read. |
| 181 | ** Returns: |false| iff the reading failed. |
| 182 | **___________________________________________________ */ |
| 183 | bool load_rsc(name) /* */ |
| 184 | register String name; /* */ |
| 185 | { /* */ |
| 186 | if (r_v == NULL) { init_rsc(); } /* */ |
| 187 | return (name != NULL ? read_rsc(name) : false); /* */ |
| 188 | } /*------------------------*/ |
| 189 | |
| 190 | /*----------------------------------------------------------------------------- |
| 191 | ** Function: resource() |
| 192 | ** Type: int |
| 193 | ** Purpose: |
| 194 | ** |
| 195 | ** Arguments: |
| 196 | ** name the name of the resource file |
| 197 | ** Returns: |
| 198 | **___________________________________________________ */ |
| 199 | bool resource(name) /* */ |
| 200 | register String name; /* */ |
| 201 | { /* */ |
| 202 | bool ret = load_rsc(name); /* */ |
| 203 | if (!ret) { NoRscError(name); } /* */ |
| 204 | return ret; /* */ |
| 205 | } /*------------------------*/ |
| 206 | |
| 207 | /*----------------------------------------------------------------------------- |
| 208 | ** Function*: test_true() |
| 209 | ** Purpose: A boolean resource can be set to true in different ways: |
| 210 | ** |true|, |t|, |yes|, |on|, |1| |
| 211 | ** which represent ``true''. Each other value is |
| 212 | ** interpreted as ``false''. |
| 213 | ** The comparison is done case insensitive. |
| 214 | ** Arguments: |
| 215 | ** sym String to check for the boolean value. |
| 216 | ** Returns: |true| iff the string represents true. |
| 217 | **___________________________________________________ */ |
| 218 | static bool test_true(sym) /* */ |
| 219 | Symbol sym; /* */ |
| 220 | { register String s = SymbolValue(sym); /* */ |
| 221 | /* */ |
| 222 | switch ( *s ) /* */ |
| 223 | { case '1': return (s[1] == '\0'); /* */ |
| 224 | case 'o': case 'O': /* */ |
| 225 | return ((s[1] == 'n' || s[1] == 'N') && /* */ |
| 226 | s[2] == '\0' ); /* */ |
| 227 | case 'y': case 'Y': /* */ |
| 228 | return ((s[1] == 'e' || s[1] == 'E') && /* */ |
| 229 | (s[2] == 's' || s[2] == 'S') && /* */ |
| 230 | s[3] == '\0' ); /* */ |
| 231 | case 't': case 'T': /* */ |
| 232 | return (s[1] == '\0' || /* */ |
| 233 | ((s[1] == 'r' || s[1] == 'R') && /* */ |
| 234 | (s[2] == 'u' || s[2] == 'U') && /* */ |
| 235 | (s[3] == 'e' || s[3] == 'E') && /* */ |
| 236 | s[4] == '\0')); /* */ |
| 237 | } /* */ |
| 238 | return false; /* */ |
| 239 | } /*------------------------*/ |
| 240 | |
| 241 | /*----------------------------------------------------------------------------- |
| 242 | ** Function: use_rsc() |
| 243 | ** Purpose: This function can be used to evaluate a single |
| 244 | ** resource instruction. The argument is a string which |
| 245 | ** is parsed to extract the resource command. |
| 246 | ** |
| 247 | ** This is an entry point for command line options which |
| 248 | ** set resources. |
| 249 | ** Arguments: |
| 250 | ** s String containing a resource command. |
| 251 | ** Returns: |true| iff an error has occurred. |
| 252 | **___________________________________________________ */ |
| 253 | bool use_rsc(s) /* */ |
| 254 | String s; /* */ |
| 255 | { register Symbol name, /* */ |
| 256 | value; /* */ |
| 257 | /* */ |
| 258 | sp_open(s); /* */ |
| 259 | if ((name = SParseSymbol(&s)) == NULL) return true;/* */ |
| 260 | /* */ |
| 261 | sp_skip(&s); /* */ |
| 262 | /* */ |
| 263 | if ((value = SParseValue(&s)) == NULL) /* */ |
| 264 | { UnlinkSymbol(name); /* */ |
| 265 | return true; /* */ |
| 266 | } /* */ |
| 267 | /* */ |
| 268 | if (r_v == NULL) { init_rsc(); } /* */ |
| 269 | /* */ |
| 270 | return set_rsc(name, value); /* */ |
| 271 | } /*------------------------*/ |
| 272 | |
| 273 | /*----------------------------------------------------------------------------- |
| 274 | ** Function: set_rsc() |
| 275 | ** Purpose: Set the resource to a given value. Here the assignment |
| 276 | ** is divided into two parts: the name and the value. |
| 277 | ** Both arguments are assumed to be symbols. |
| 278 | ** Arguments: |
| 279 | ** name Name of the resource to set. |
| 280 | ** val The new value of the resource. |
| 281 | ** Returns: |false| iff everything went right. |
| 282 | **___________________________________________________ */ |
| 283 | bool set_rsc(name,val) /* */ |
| 284 | Symbol name; /* */ |
| 285 | Symbol val; /* */ |
| 286 | { /* */ |
| 287 | if ( rsc_verbose ) /* */ |
| 288 | { VerbosePrint4("Set resource ", /* */ |
| 289 | (char*)SymbolValue(name), /* */ |
| 290 | " = ", /* */ |
| 291 | ( val == NO_SYMBOL /* */ |
| 292 | ? "*NULL*" /* */ |
| 293 | : (char*)SymbolValue(val))); /* */ |
| 294 | } /* */ |
| 295 | /* */ |
| 296 | #define SETQ(V,T) V=T; UnlinkSymbol(name); |
| 297 | #define RscNumeric(SYM,S,V,I) \ |
| 298 | if ( name == S ) { SETQ(V,atoi((char*)SymbolValue(val))); \ |
| 299 | UnlinkSymbol(val); return false; } |
| 300 | #define RscString(SYM,S,V,I) \ |
| 301 | if ( name == S ) { V = (String)new_string((char*)SymbolValue(val)); \ |
| 302 | UnlinkSymbol(name); return false; } |
| 303 | #define RscBoolean(SYM,S,V,I) \ |
| 304 | if ( name == S ) { SETQ(V,test_true(val)); \ |
| 305 | UnlinkSymbol(val); return false; } |
| 306 | #define RscByFct(SYM,S,FCT) \ |
| 307 | if ( name == S ) { (void)FCT; \ |
| 308 | UnlinkSymbol(name); return false; } |
| 309 | #define RSC_FIRST(C) case C: |
| 310 | #define RSC_NEXT(C) break; case C: |
| 311 | /* */ |
| 312 | switch (*SymbolValue(name)) /* */ |
| 313 | { /* */ |
| 314 | #ifndef MAKEDEPEND |
| 315 | #include <bibtool/resource.h> |
| 316 | #endif |
| 317 | } /* */ |
| 318 | /* */ |
| 319 | ERROR3("Resource ", SymbolValue(name), /* */ |
| 320 | " unknown."); /* */ |
| 321 | UnlinkSymbol(name); /* */ |
| 322 | UnlinkSymbol(val); /* */ |
| 323 | return 1; /* */ |
| 324 | } /*------------------------*/ |
| 325 | |
| 326 | /*----------------------------------------------------------------------------- |
| 327 | ** Function: rsc_print() |
| 328 | ** Purpose: Print a string to the error stream as defined in |
| 329 | ** |error.h|. The string is automatically augmented by a |
| 330 | ** trailing newline. |
| 331 | ** This wrapper function is used for the resource |print|. |
| 332 | ** Arguments: |
| 333 | ** s String to print. |
| 334 | ** Returns: nothing |
| 335 | **___________________________________________________ */ |
| 336 | void rsc_print(s) /* */ |
| 337 | String s; /* */ |
| 338 | { ErrPrintF("%s\n", (char*)s); /* print the string itself*/ |
| 339 | /* followed by a newline */ |
| 340 | } /*------------------------*/ |