| 1 | /*** io.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 | ** |
| 28 | ** |
| 29 | ******************************************************************************/ |
| 30 | |
| 31 | #include <bibtool/general.h> |
| 32 | #include <bibtool/error.h> |
| 33 | #include <bibtool/io.h> |
| 34 | #include <bibtool/symbols.h> |
| 35 | #include "config.h" |
| 36 | |
| 37 | /*****************************************************************************/ |
| 38 | /* Internal Programs */ |
| 39 | /*===========================================================================*/ |
| 40 | |
| 41 | /*****************************************************************************/ |
| 42 | /* External Programs and Variables */ |
| 43 | /*===========================================================================*/ |
| 44 | |
| 45 | /*---------------------------------------------------------------------------*/ |
| 46 | |
| 47 | |
| 48 | /*****************************************************************************/ |
| 49 | /*** Input File Pipe Section ***/ |
| 50 | /*****************************************************************************/ |
| 51 | |
| 52 | #define InputFilePipeIncrement 8 |
| 53 | |
| 54 | static Symbol* input_files; |
| 55 | static int input_file_size = 0; |
| 56 | static int input_file_ptr = 0; |
| 57 | |
| 58 | #define InputPipeIsFull (input_file_ptr >= input_file_size) |
| 59 | #define InputPipeIsEmpty (input_file_ptr == 0) |
| 60 | #define PushToInputPipe(FILE) input_files[input_file_ptr++] = FILE |
| 61 | #define ForAllInputFiles(FILE) for (FILE=input_files; \ |
| 62 | FILE<input_files[input_file_ptr]; \ |
| 63 | FILE++) |
| 64 | |
| 65 | /*----------------------------------------------------------------------------- |
| 66 | ** Function: save_input_file() |
| 67 | ** Purpose: The input file pipe is a dynamic array of strings. |
| 68 | ** This fifo stack is used to store the input \BibTeX{} |
| 69 | ** files to be processed by \BibTool. |
| 70 | ** |
| 71 | ** This function is called to push an string into the pipe. |
| 72 | ** If necessary the array has to be allocated or enlarged. |
| 73 | ** This is done in larger junks to avoid lots of calls to |
| 74 | ** |realloc()|. |
| 75 | ** Arguments: |
| 76 | ** file File name to save. |
| 77 | ** Returns: nothing |
| 78 | **___________________________________________________ */ |
| 79 | void save_input_file(file) /* */ |
| 80 | Symbol file; /* */ |
| 81 | { /* */ |
| 82 | if (file == NO_SYMBOL) /* */ |
| 83 | { WARNING("Missing input file name. Flag ignored.");/* */ |
| 84 | return; /* */ |
| 85 | } /* */ |
| 86 | if (*SymbolValue(file) == '-' && /* */ |
| 87 | SymbolValue(file)[1] == '\0') /* */ |
| 88 | { file = NO_SYMBOL; } /* */ |
| 89 | /* */ |
| 90 | if (InputPipeIsFull) /* No space left? */ |
| 91 | { input_file_size += InputFilePipeIncrement; /* */ |
| 92 | /* */ |
| 93 | if (InputPipeIsEmpty /* Try to enlarge array */ |
| 94 | ? NULL==(input_files= /* */ |
| 95 | (Symbol*)malloc(sizeof(Symbol) /* */ |
| 96 | *(size_t)input_file_size))/* */ |
| 97 | : NULL==(input_files= /* */ |
| 98 | (Symbol*)realloc((char*)input_files,/* */ |
| 99 | sizeof(Symbol) /* */ |
| 100 | *(size_t)input_file_size))/* */ |
| 101 | ) /* */ |
| 102 | { OUT_OF_MEMORY("input file pipe."); } /* */ |
| 103 | } /* */ |
| 104 | PushToInputPipe(file); /* */ |
| 105 | } /*------------------------*/ |
| 106 | |
| 107 | /*----------------------------------------------------------------------------- |
| 108 | ** Function: get_no_inputs() |
| 109 | ** Type: int |
| 110 | ** Purpose: Get the number of input file names stored. |
| 111 | ** |
| 112 | ** Arguments: none |
| 113 | ** Returns: the number of currently stored input files |
| 114 | **___________________________________________________ */ |
| 115 | int get_no_inputs() /* */ |
| 116 | { /* */ |
| 117 | return input_file_ptr; /* */ |
| 118 | } /*------------------------*/ |
| 119 | |
| 120 | /*----------------------------------------------------------------------------- |
| 121 | ** Function: get_input_file() |
| 122 | ** Type: char * |
| 123 | ** Purpose: Getter for a single input file name. |
| 124 | ** |
| 125 | ** Arguments: |
| 126 | ** i the index of the input file to access |
| 127 | ** Returns: the references input file name or NULL otherwise |
| 128 | **___________________________________________________ */ |
| 129 | Symbol get_input_file(i) /* */ |
| 130 | int i; /* */ |
| 131 | { return (i >= 0 && i < input_file_ptr /* */ |
| 132 | ? input_files[i] /* */ |
| 133 | : NO_SYMBOL); /* */ |
| 134 | } /*------------------------*/ |
| 135 | |
| 136 | |
| 137 | /*****************************************************************************/ |
| 138 | /*** Output File Section ***/ |
| 139 | /*****************************************************************************/ |
| 140 | |
| 141 | static Symbol output_file = NO_SYMBOL; /* */ |
| 142 | |
| 143 | /*----------------------------------------------------------------------------- |
| 144 | ** Function: save_output_file() |
| 145 | ** Purpose: Simply feed the output file name into the static variable. |
| 146 | ** This function is useful since it can be called from rsc.c |
| 147 | ** Arguments: |
| 148 | ** file File name to save |
| 149 | ** Returns: nothing |
| 150 | **___________________________________________________ */ |
| 151 | void save_output_file(file) /* */ |
| 152 | Symbol file; /* */ |
| 153 | { if (output_file != NO_SYMBOL) /* */ |
| 154 | { WARNING2("Output file redefined: ", /* */ |
| 155 | SymbolValue(file)); /* */ |
| 156 | } /* */ |
| 157 | output_file = file; /* */ |
| 158 | } /*------------------------*/ |
| 159 | |
| 160 | /*----------------------------------------------------------------------------- |
| 161 | ** Function: get_output_file() |
| 162 | ** Type: Symbol |
| 163 | ** Purpose: This is the getter for the output file name. |
| 164 | ** |
| 165 | ** Arguments: none |
| 166 | ** Returns: the output file name or NULL for STDOUT |
| 167 | **___________________________________________________ */ |
| 168 | Symbol get_output_file() /* */ |
| 169 | { return output_file; /* */ |
| 170 | } /*------------------------*/ |
| 171 | |
| 172 | |
| 173 | /*****************************************************************************/ |
| 174 | /*** Macro File Section ***/ |
| 175 | /*****************************************************************************/ |
| 176 | |
| 177 | static Symbol macro_file = NO_SYMBOL; /* */ |
| 178 | |
| 179 | /*----------------------------------------------------------------------------- |
| 180 | ** Function: save_macro_file() |
| 181 | ** Purpose: Simply feed the macro file name into the static variable. |
| 182 | ** This function is useful since it can be called from rsc.c |
| 183 | ** Arguments: |
| 184 | ** file File name to save |
| 185 | ** Returns: nothing |
| 186 | **___________________________________________________ */ |
| 187 | void save_macro_file(file) /* */ |
| 188 | Symbol file; /* */ |
| 189 | { if ( macro_file != NULL ) /* */ |
| 190 | { WARNING2("Macro file redefined: ", /* */ |
| 191 | SymbolValue(file)); /* */ |
| 192 | } /* */ |
| 193 | macro_file = file; /* */ |
| 194 | } /*------------------------*/ |
| 195 | |
| 196 | /*----------------------------------------------------------------------------- |
| 197 | ** Function: get_macro_file() |
| 198 | ** Purpose: This is the getter for the macro file name. |
| 199 | ** |
| 200 | ** Arguments: none |
| 201 | ** Returns: the macro file name or NULL for none |
| 202 | **___________________________________________________ */ |
| 203 | Symbol get_macro_file() /* */ |
| 204 | { return macro_file; /* */ |
| 205 | } /*------------------------*/ |