File: infopath.c

package info (click to toggle)
texinfo 7.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 113,148 kB
  • sloc: perl: 1,281,149; ansic: 135,801; sh: 12,218; xml: 9,069; makefile: 4,016; javascript: 1,923; awk: 1,889; sed: 78; pascal: 65
file content (264 lines) | stat: -rw-r--r-- 6,974 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* infopath.c -- INFOPATH handling.

   Copyright 1993-2024 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 "info.h"
#include "scan.h"
#include "util.h"
#include "session.h"
#include "filesys.h"

typedef struct {
  char *name;    /* Path to directory to be searched. */
  dev_t device;  /* Storage device this directory is on. */
  ino_t inode;   /* Inode number, used to detect duplicates. */
} INFO_DIR;

INFO_DIR **infodirs = 0;
size_t infodirs_index = 0;
size_t infodirs_slots = 0;

/* Exclude default file search directories. */
int infopath_no_defaults_p;

static void infopath_add_dir (char *path);
char *extract_colon_unit (char *string, int *idx);

void
infopath_init ()
{
  /* Initialize INFOPATH.
     Highest priority is the environment variable, if set
     Then comes the user's INFODIR from the Makefile.
     The hardwired default settings (filesys.h) are the lowest priority. */
  char *path_from_env = getenv ("INFOPATH");

  if (path_from_env)
    {
      infopath_add (path_from_env);
    }

  if (!infopath_no_defaults_p)
    {
#ifdef INFODIR /* $infodir, set by configure script in Makefile */
      infopath_add (INFODIR);
#ifdef INFODIR2 /* $datadir/info, which could be different. */
      if (!STREQ (INFODIR, INFODIR2))
        infopath_add (INFODIR2);
#endif /* INFODIR2 */
#endif /* INFODIR */
    }

  if (!path_from_env)
    {
      infopath_add (DEFAULT_INFOPATH);
    }
  else
    { 
      /* Only insert default path if there is a trailing : on INFOPATH. */

      unsigned len = strlen (path_from_env);
      if (len && path_from_env[len - 1] == PATH_SEP[0])
	{
	  path_from_env[len - 1] = 0;
	  infopath_add (DEFAULT_INFOPATH);
	}
    }
}

/* Return value to be freed by caller. */
char *
infopath_string (void)
{
  struct text_buffer path;
  int dir_idx;
  char *this_dir;

  this_dir = infopath_first (&dir_idx);
  if (!this_dir)
    return "";

  text_buffer_init (&path);

  while (1)
    {
      text_buffer_printf (&path, "%s", this_dir);
      this_dir = infopath_next (&dir_idx);
      if (!this_dir)
        break;
      text_buffer_add_char (&path, ':');
    }
  return text_buffer_base (&path); 
}

/* For each path element PREFIX/DIR in PATH substitute either
   PREFIX/share/info or PREFIX/info if that directory exists. */
static void
build_infopath_from_path (void)
{
  char *path_from_env, *temp_dirname;
  int dirname_index = 0;
  struct stat finfo;

  path_from_env = getenv ("PATH");

  while ((temp_dirname = extract_colon_unit (path_from_env, &dirname_index)))
    {
      unsigned int i, dir = 0;

      /* Find end of DIRNAME/ (but ignore "/") */
      for (i = 0; temp_dirname[i]; i++)
        if (i && IS_SLASH (temp_dirname[i]))
          dir = i + 1;

      /* Discard path elements ending with "/", "/.", or "/.." */
      if (!temp_dirname[dir] || STREQ (temp_dirname + dir, ".") || STREQ (temp_dirname + dir, "."))
        dir = 0;
      
      if (dir)
        {
          temp_dirname = xrealloc (temp_dirname, dir + strlen ("share/info") +1);

          /* first try DIRNAME/share/info */
          strcpy (temp_dirname + dir, "share/info");
          if (stat (temp_dirname, &finfo) != 0 || !S_ISDIR (finfo.st_mode))
            {
              /* then try DIRNAME/info */
              strcpy (temp_dirname + dir, "info");
              if (stat (temp_dirname, &finfo) != 0 || !S_ISDIR (finfo.st_mode))
                dir = 0;
            }
        }

      if (dir)
        infopath_add_dir (temp_dirname);
      else
        free (temp_dirname);
    }
}

/* Add directory at PATH to Info search path.  A reference to PATH is retained,
   or PATH is freed. */
static void
infopath_add_dir (char *path)
{
  struct stat dirinfo;
  INFO_DIR *entry;
  size_t i;

  if (stat (path, &dirinfo) == -1)
    {
      debug (2, ("inaccessible directory %s not added to INFOPATH", path));
      free (path);
      return; /* Doesn't exist, or not accessible. */
    }

  for (i = 0; i < infodirs_index; i++)
    {
      if (   dirinfo.st_ino == infodirs[i]->inode
          && dirinfo.st_dev == infodirs[i]->device
          /* On MS-Windows, `stat' returns zero as the inode, so we
             use file-name comparison instead for that OS.  */
          && (infodirs[i]->inode != 0 || fncmp (path, infodirs[i]->name) == 0))
        {
          debug (2, ("duplicate directory %s not added to INFOPATH", path));
          free (path);
          return; /* We have it already. */
        }
    }

  debug (2, ("adding %s to INFOPATH", path));
  entry = xmalloc (sizeof (INFO_DIR));
  entry->name = path;
  entry->inode = dirinfo.st_ino;
  entry->device = dirinfo.st_dev;
  add_pointer_to_array (entry, infodirs_index, infodirs, infodirs_slots, 8);
}

/* Add PATH to the list of paths found in INFOPATH.  PATH should be allocated
   on the heap and not referenced by the caller after calling this function.
   If PATH is "PATH", add a sequence of path elements derived from the
   environment variable PATH. */
void
infopath_add (char *path)
{
  int idx = 0;
  char *dirname;

  while ((dirname = extract_colon_unit (path, &idx)))
    {
      if (!strcmp ("PATH", dirname))
        {
          free (dirname);
          build_infopath_from_path ();
        }
      else
        infopath_add_dir (dirname);
    }
}

/* Used to iterate over INFOPATH.  Return value should not be freed
   by caller. */
char *
infopath_next (int *idx)
{
  INFO_DIR *entry;
 
  if (!infodirs)
   return 0;
  entry = infodirs[(*idx)++];
  if (!entry)
    return 0;
  return entry->name;
}

char *
infopath_first (int *idx)
{
  *idx = 0;
  return infopath_next (idx);
}

/* Given a string containing units of information separated by the
   PATH_SEP character, return the next one after IDX, or NULL if there
   are no more.  Advance IDX to the character after the colon. */
char *
extract_colon_unit (char *string, int *idx)
{
  unsigned int i = (unsigned int) *idx;
  unsigned int start = i;

  if (!string || i >= strlen (string))
    return NULL;

  if (!string[i]) /* end of string */
    return NULL;

  /* Advance to next PATH_SEP.  */
  while (string[i] && string[i] != PATH_SEP[0])
    i++;

  {
    char *value = xmalloc ((i - start) + 1);
    strncpy (value, &string[start], (i - start));
    value[i - start] = 0;

    i++; /* move past PATH_SEP */
    *idx = i;
    return value;
  }
}