Annotation of src/usr.bin/m4/gnum4.c, Revision 1.2
1.1 espie 1: /* $OpenBSD$ */
2:
3: /*
4: * Copyright (c) 1999 Marc Espie
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25: * SUCH DAMAGE.
26: */
27:
28: /*
29: * functions needed to support gnu-m4 extensions, including a fake freezing
30: */
31:
32: #include <sys/param.h>
33: #include <stddef.h>
34: #include <stdlib.h>
35: #include <stdio.h>
36: #include <string.h>
37: #include <err.h>
38: #include "mdef.h"
39: #include "stdd.h"
40: #include "extern.h"
41:
42: /*
43: * Support for include path search
44: * First search in the the current directory.
45: * If not found, and the path is not absolute, include path kicks in.
46: * First, -I options, in the order found on the command line.
47: * Then M4PATH env variable
48: */
49:
50: struct path_entry {
51: char *name;
52: struct path_entry *next;
53: } *first, *last;
54:
55: static struct path_entry *
56: new_path_entry(dirname)
57: char *dirname;
58: {
59: struct path_entry *n;
60:
61: n = malloc(sizeof(struct path_entry));
62: if (!n)
63: errx(1, "out of memory");
64: n->name = strdup(dirname);
65: if (!n->name)
66: errx(1, "out of memory");
67: n->next = 0;
68: return n;
69: }
70:
71: void
72: addtoincludepath(dirname)
73: const char *dirname;
74: {
75: struct path_entry *n;
76:
77: n = new_path_entry(dirname);
78:
79: if (last) {
80: last->next = n;
81: last = n;
82: }
83: else
84: last = first = n;
85: }
86:
87: static void
88: ensure_m4path()
89: {
90: static int envpathdone = 0;
91: char *envpath;
92: char *sweep;
93: char *path;
94:
95: if (envpathdone)
96: return;
97: envpathdone = TRUE;
98: envpath = getenv("M4PATH");
99: if (!envpath)
100: return;
101: /* for portability: getenv result is read-only */
102: envpath = strdup(envpath);
103: if (!envpath)
104: errx(1, "out of memory");
105: for (sweep = envpath;
106: (path = strsep(&sweep, ":")) != NULL;)
107: addtoincludepath(path);
108: free(envpath);
109: }
110:
111: static
112: FILE *
113: dopath(const char *filename)
114: {
115: char path[MAXPATHLEN];
116: struct path_entry *pe;
117: FILE *file;
118:
119: for (pe = first; pe; pe = pe->next) {
120: snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
121: if ((file = fopen(path, "r")) != 0)
122: return file;
123: }
124: return NULL;
125: }
126:
127: FILE *
128: fopen_trypath(filename)
129: const char *filename;
130: {
131: FILE *f;
132:
133: f = fopen(filename, "r");
134: if (f)
135: return f;
136: if (filename[0] == '/')
137: return NULL;
138:
139: ensure_m4path();
140:
141: return dopath(filename);
142: }
143:
144:
145: