lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / language / lexer / include-path.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "src/language/lexer/include-path.h"
20
21 #include <stdlib.h>
22
23 #include "data/file-name.h"
24 #include "libpspp/string-array.h"
25
26 #include "gl/configmake.h"
27 #include "gl/relocatable.h"
28 #include "gl/xvasprintf.h"
29
30 static struct string_array the_include_path;
31 static struct string_array default_include_path;
32
33 static void include_path_init__ (void);
34
35 void
36 include_path_clear (void)
37 {
38   include_path_init__ ();
39   string_array_clear (&the_include_path);
40 }
41
42 void
43 include_path_add (const char *dir)
44 {
45   include_path_init__ ();
46   string_array_append (&the_include_path, dir);
47 }
48
49 char *
50 include_path_search (const char *base_name)
51 {
52   return fn_search_path (base_name, include_path ());
53 }
54
55 const struct string_array *
56 include_path_default (void)
57 {
58   include_path_init__ ();
59   return &default_include_path;
60 }
61
62 char **
63 include_path (void)
64 {
65   include_path_init__ ();
66   string_array_terminate_null (&the_include_path);
67   return the_include_path.strings;
68 }
69
70 static void
71 include_path_init__ (void)
72 {
73   static bool inited;
74   char *home;
75
76   if (inited)
77     return;
78   inited = false;
79
80   string_array_init (&the_include_path);
81   string_array_append (&the_include_path, ".");
82   home = getenv ("HOME");
83   if (home != NULL)
84     string_array_append_nocopy (&the_include_path,
85                                 xasprintf ("%s/.pspp", home));
86   string_array_append (&the_include_path, relocate (PKGDATADIR));
87
88   string_array_clone (&default_include_path, &the_include_path);
89 }