summaryrefslogtreecommitdiff
path: root/lib/libfile.c
blob: 7e123b7e650d6fe848710688c64582acf5e48caf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# libfile.c: open and read a single auxiliary data file.
#
# Copyright (C) 1992, 2011 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


#include "config.h"
#include "paths.h"

#include "c-ctype.h"
#include "line.h"
#include "libfile.h"
#include "pathsrch.h"


/* Private variables to hold the auxiliary file we're reading from, and
   its name.  */ 
static FILE *lib_file = NULL;
static string lib_filename;
static unsigned lib_file_line_number;


/* Prepare to read the ``library file'' FILENAME.DEFAULT_SUFFIX (if
   FILENAME has no suffix), or just FILENAME (if it has).  Give a fatal
   error if it cannot be opened.  Return the resulting FILE * (although
   usually this will just be ignored by the caller).  */

FILE *
libfile_start (string filename, string default_suffix)
{
  string *lib_dirs = initialize_path_list (LIB_ENVVAR, DEFAULT_LIB_PATH);
  string name = extend_filename (filename, default_suffix);

  lib_filename = find_path_filename (name, lib_dirs);

  if (lib_filename == NULL)
    FATAL1 ("%s: Cannot find library file in path", name);

  lib_file = xfopen (lib_filename, "r");
  lib_file_line_number = 1;
  
  return lib_file;
}


/* Close our current open file.  If we don't have any file open, give a
   fatal error.  */

void
libfile_close ()
{
  assert (lib_file != NULL);
  
  fclose (lib_file);
  lib_file = NULL;
}

/* Return the name of the currently open file, or NULL if none.  */

string
libfilename ()
{
  return lib_file == NULL ? NULL : lib_filename;
}


/* Analogously, for the current line number.  */

unsigned
libfile_linenumber ()
{
  return lib_file == NULL ? 0 : lib_file_line_number;
}

/* Return the next nonblank non-comment line from `lib_file', or NULL if
   we are at EOF.  Also remove any trailing comment on the line.  */

string
libfile_line ()
{
  string s;
  string line;
  boolean skip = true;
  
  assert (lib_file != NULL);

  do
    {
      line = read_line (lib_file);
      lib_file_line_number++;
      
      /* If at EOF, quit the loop.  */
      if (line == NULL)
        break;

      s = line;
      
      /* Move ahead to the first nonblank character.  */
      while (*s != 0 && ISSPACE (*s))
        s++;

     /* Keep going if the line was blank or a comment.  */
      if (*s == 0 || *s == '%')
        free (line);
      else
        skip = false;
    }
  while (skip);
  
  if (line != NULL)
    {
      s = strrchr (line, '%');
      if (s != NULL)
        *s = 0;
    }

  return line;
}