1 /* Open and close files for Bison. 2 3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2012 Free Software 4 Foundation, Inc. 5 6 This file is part of Bison, the GNU Compiler Compiler. 7 8 This program is free software: you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation, either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 20 21 #include <config.h> 22 #include "system.h" 23 24 #include <error.h> 25 #include <dirname.h> 26 #include <get-errno.h> 27 #include <quote.h> 28 #include <quotearg.h> 29 #include <stdio-safer.h> 30 #include <xstrndup.h> 31 32 #include "complain.h" 33 #include "files.h" 34 #include "getargs.h" 35 #include "gram.h" 36 37 /* Initializing some values below (such SPEC_NAME_PREFIX to `yy') is 38 tempting, but don't do that: for the time being our handling of the 39 %directive vs --option leaves precedence to the options by deciding 40 that if a %directive sets a variable which is really set (i.e., not 41 NULL), then the %directive is ignored. As a result, %name-prefix, 42 for instance, will not be honored. */ 43 44 char const *spec_outfile = NULL; /* for -o. */ 45 char const *spec_file_prefix = NULL; /* for -b. */ 46 char const *spec_name_prefix = NULL; /* for -p. */ 47 char *spec_verbose_file = NULL; /* for --verbose. */ 48 char *spec_graph_file = NULL; /* for -g. */ 49 char *spec_xml_file = NULL; /* for -x. */ 50 char *spec_defines_file = NULL; /* for --defines. */ 51 char *parser_file_name; 52 53 /* All computed output file names. */ 54 static char **file_names = NULL; 55 static int file_names_count = 0; 56 57 uniqstr grammar_file = NULL; 58 uniqstr current_file = NULL; 59 60 /* If --output=dir/foo.c was specified, 61 DIR_PREFIX is `dir/' and ALL_BUT_EXT and ALL_BUT_TAB_EXT are `dir/foo'. 62 63 If --output=dir/foo.tab.c was specified, DIR_PREFIX is `dir/', 64 ALL_BUT_EXT is `dir/foo.tab', and ALL_BUT_TAB_EXT is `dir/foo'. 65 66 If --output was not specified but --file-prefix=dir/foo was specified, 67 ALL_BUT_EXT = `foo.tab' and ALL_BUT_TAB_EXT = `foo'. 68 69 If neither --output nor --file was specified but the input grammar 70 is name dir/foo.y, ALL_BUT_EXT and ALL_BUT_TAB_EXT are `foo'. 71 72 If neither --output nor --file was specified, DIR_PREFIX is the 73 empty string (meaning the current directory); otherwise it is 74 `dir/'. */ 75 76 char *all_but_ext; 77 static char *all_but_tab_ext; 78 char *dir_prefix; 79 80 /* C source file extension (the parser source). */ 81 static char *src_extension = NULL; 82 /* Header file extension (if option ``-d'' is specified). */ 83 static char *header_extension = NULL; 84 85 /*-----------------------------------------------------------------. 87 | Return a newly allocated string composed of the concatenation of | 88 | STR1, and STR2. | 89 `-----------------------------------------------------------------*/ 90 91 static char * 92 concat2 (char const *str1, char const *str2) 93 { 94 size_t len = strlen (str1) + strlen (str2); 95 char *res = xmalloc (len + 1); 96 char *cp; 97 cp = stpcpy (res, str1); 98 cp = stpcpy (cp, str2); 99 return res; 100 } 101 102 /*-----------------------------------------------------------------. 103 | Try to open file NAME with mode MODE, and print an error message | 104 | if fails. | 105 `-----------------------------------------------------------------*/ 106 107 FILE * 108 xfopen (const char *name, const char *mode) 109 { 110 FILE *ptr; 111 112 ptr = fopen_safer (name, mode); 113 if (!ptr) 114 error (EXIT_FAILURE, get_errno (), 115 _("%s: cannot open"), quotearg_colon (name)); 116 117 return ptr; 118 } 119 120 /*-------------------------------------------------------------. 121 | Try to close file PTR, and print an error message if fails. | 122 `-------------------------------------------------------------*/ 123 124 void 125 xfclose (FILE *ptr) 126 { 127 if (ptr == NULL) 128 return; 129 130 if (ferror (ptr)) 131 error (EXIT_FAILURE, 0, _("input/output error")); 132 133 if (fclose (ptr) != 0) 134 error (EXIT_FAILURE, get_errno (), _("cannot close file")); 135 } 136 137 139 /*------------------------------------------------------------------. 140 | Compute ALL_BUT_EXT, ALL_BUT_TAB_EXT and output files extensions. | 141 `------------------------------------------------------------------*/ 142 143 /* Compute extensions from the grammar file extension. */ 144 static void 145 compute_exts_from_gf (const char *ext) 146 { 147 if (strcmp (ext, ".y") == 0) 148 { 149 src_extension = xstrdup (language->src_extension); 150 header_extension = xstrdup (language->header_extension); 151 } 152 else 153 { 154 src_extension = xstrdup (ext); 155 header_extension = xstrdup (ext); 156 tr (src_extension, 'y', 'c'); 157 tr (src_extension, 'Y', 'C'); 158 tr (header_extension, 'y', 'h'); 159 tr (header_extension, 'Y', 'H'); 160 } 161 } 162 163 /* Compute extensions from the given c source file extension. */ 164 static void 165 compute_exts_from_src (const char *ext) 166 { 167 /* We use this function when the user specifies `-o' or `--output', 168 so the extenions must be computed unconditionally from the file name 169 given by this option. */ 170 src_extension = xstrdup (ext); 171 header_extension = xstrdup (ext); 172 tr (header_extension, 'c', 'h'); 173 tr (header_extension, 'C', 'H'); 174 } 175 176 177 /* Decompose FILE_NAME in four parts: *BASE, *TAB, and *EXT, the fourth 178 part, (the directory) is ranging from FILE_NAME to the char before 179 *BASE, so we don't need an additional parameter. 180 181 *EXT points to the last period in the basename, or NULL if none. 182 183 If there is no *EXT, *TAB is NULL. Otherwise, *TAB points to 184 `.tab' or `_tab' if present right before *EXT, or is NULL. *TAB 185 cannot be equal to *BASE. 186 187 None are allocated, they are simply pointers to parts of FILE_NAME. 188 Examples: 189 190 '/tmp/foo.tab.c' -> *BASE = 'foo.tab.c', *TAB = '.tab.c', *EXT = 191 '.c' 192 193 'foo.c' -> *BASE = 'foo.c', *TAB = NULL, *EXT = '.c' 194 195 'tab.c' -> *BASE = 'tab.c', *TAB = NULL, *EXT = '.c' 196 197 '.tab.c' -> *BASE = '.tab.c', *TAB = NULL, *EXT = '.c' 198 199 'foo.tab' -> *BASE = 'foo.tab', *TAB = NULL, *EXT = '.tab' 200 201 'foo_tab' -> *BASE = 'foo_tab', *TAB = NULL, *EXT = NULL 202 203 'foo' -> *BASE = 'foo', *TAB = NULL, *EXT = NULL. */ 204 205 static void 206 file_name_split (const char *file_name, 207 const char **base, const char **tab, const char **ext) 208 { 209 *base = last_component (file_name); 210 211 /* Look for the extension, i.e., look for the last dot. */ 212 *ext = mbsrchr (*base, '.'); 213 *tab = NULL; 214 215 /* If there is an extension, check if there is a `.tab' part right 216 before. */ 217 if (*ext) 218 { 219 size_t baselen = *ext - *base; 220 size_t dottablen = 4; 221 if (dottablen < baselen 222 && (strncmp (*ext - dottablen, ".tab", dottablen) == 0 223 || strncmp (*ext - dottablen, "_tab", dottablen) == 0)) 224 *tab = *ext - dottablen; 225 } 226 } 227 228 229 static void 230 compute_file_name_parts (void) 231 { 232 const char *base, *tab, *ext; 233 234 /* Compute ALL_BUT_EXT and ALL_BUT_TAB_EXT from SPEC_OUTFILE 235 or GRAMMAR_FILE. 236 237 The precise -o name will be used for FTABLE. For other output 238 files, remove the ".c" or ".tab.c" suffix. */ 239 if (spec_outfile) 240 { 241 file_name_split (spec_outfile, &base, &tab, &ext); 242 dir_prefix = xstrndup (spec_outfile, base - spec_outfile); 243 244 /* ALL_BUT_EXT goes up the EXT, excluding it. */ 245 all_but_ext = 246 xstrndup (spec_outfile, 247 (strlen (spec_outfile) - (ext ? strlen (ext) : 0))); 248 249 /* ALL_BUT_TAB_EXT goes up to TAB, excluding it. */ 250 all_but_tab_ext = 251 xstrndup (spec_outfile, 252 (strlen (spec_outfile) 253 - (tab ? strlen (tab) : (ext ? strlen (ext) : 0)))); 254 255 if (ext) 256 compute_exts_from_src (ext); 257 } 258 else 259 { 260 file_name_split (grammar_file, &base, &tab, &ext); 261 262 if (spec_file_prefix) 263 { 264 /* If --file-prefix=foo was specified, ALL_BUT_TAB_EXT = `foo'. */ 265 dir_prefix = 266 xstrndup (spec_file_prefix, 267 last_component (spec_file_prefix) - spec_file_prefix); 268 all_but_tab_ext = xstrdup (spec_file_prefix); 269 } 270 else if (yacc_flag) 271 { 272 /* If --yacc, then the output is `y.tab.c'. */ 273 dir_prefix = xstrdup (""); 274 all_but_tab_ext = xstrdup ("y"); 275 } 276 else 277 { 278 /* Otherwise, ALL_BUT_TAB_EXT is computed from the input 279 grammar: `foo/bar.yy' => `bar'. */ 280 dir_prefix = xstrdup (""); 281 all_but_tab_ext = 282 xstrndup (base, (strlen (base) - (ext ? strlen (ext) : 0))); 283 } 284 285 if (language->add_tab) 286 all_but_ext = concat2 (all_but_tab_ext, TAB_EXT); 287 else 288 all_but_ext = xstrdup (all_but_tab_ext); 289 290 /* Compute the extensions from the grammar file name. */ 291 if (ext && !yacc_flag) 292 compute_exts_from_gf (ext); 293 } 294 } 295 296 297 /* Compute the output file names. Warn if we detect conflicting 298 outputs to the same file. */ 299 300 void 301 compute_output_file_names (void) 302 { 303 compute_file_name_parts (); 304 305 /* If not yet done. */ 306 if (!src_extension) 307 src_extension = xstrdup (".c"); 308 if (!header_extension) 309 header_extension = xstrdup (".h"); 310 311 parser_file_name = 312 (spec_outfile 313 ? xstrdup (spec_outfile) 314 : concat2 (all_but_ext, src_extension)); 315 316 if (defines_flag) 317 { 318 if (! spec_defines_file) 319 spec_defines_file = concat2 (all_but_ext, header_extension); 320 } 321 322 if (graph_flag) 323 { 324 if (! spec_graph_file) 325 spec_graph_file = concat2 (all_but_tab_ext, ".dot"); 326 output_file_name_check (&spec_graph_file); 327 } 328 329 if (xml_flag) 330 { 331 if (! spec_xml_file) 332 spec_xml_file = concat2 (all_but_tab_ext, ".xml"); 333 output_file_name_check (&spec_xml_file); 334 } 335 336 if (report_flag) 337 { 338 if (!spec_verbose_file) 339 spec_verbose_file = concat2 (all_but_tab_ext, OUTPUT_EXT); 340 output_file_name_check (&spec_verbose_file); 341 } 342 343 free (all_but_tab_ext); 344 free (src_extension); 345 free (header_extension); 346 } 347 348 void 349 output_file_name_check (char **file_name) 350 { 351 bool conflict = false; 352 if (0 == strcmp (*file_name, grammar_file)) 353 { 354 complain (_("refusing to overwrite the input file %s"), 355 quote (*file_name)); 356 conflict = true; 357 } 358 else 359 { 360 int i; 361 for (i = 0; i < file_names_count; i++) 362 if (0 == strcmp (file_names[i], *file_name)) 363 { 364 warn (_("conflicting outputs to file %s"), 365 quote (*file_name)); 366 conflict = true; 367 } 368 } 369 if (conflict) 370 { 371 free (*file_name); 372 *file_name = strdup ("/dev/null"); 373 } 374 else 375 { 376 file_names = xnrealloc (file_names, ++file_names_count, 377 sizeof *file_names); 378 file_names[file_names_count-1] = xstrdup (*file_name); 379 } 380 } 381 382 void 383 output_file_names_free (void) 384 { 385 free (all_but_ext); 386 free (spec_verbose_file); 387 free (spec_graph_file); 388 free (spec_xml_file); 389 free (spec_defines_file); 390 free (parser_file_name); 391 free (dir_prefix); 392 { 393 int i; 394 for (i = 0; i < file_names_count; i++) 395 free (file_names[i]); 396 } 397 free (file_names); 398 } 399