[BACK]Return to gnum4.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / m4

Annotation of src/usr.bin/m4/gnum4.c, Revision 1.4

1.4     ! espie       1: /* $OpenBSD: gnum4.c,v 1.3 1999/11/17 15:34:13 espie Exp $ */
1.1       espie       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:
1.3       espie      55: static struct path_entry *new_path_entry __P((const char *));
                     56: static void ensure_m4path __P((void));
1.4     ! espie      57: static struct input_file *dopath __P((struct input_file *, const char *));
1.3       espie      58:
1.1       espie      59: static struct path_entry *
                     60: new_path_entry(dirname)
1.3       espie      61:        const char *dirname;
1.1       espie      62: {
                     63:        struct path_entry *n;
                     64:
                     65:        n = malloc(sizeof(struct path_entry));
                     66:        if (!n)
                     67:                errx(1, "out of memory");
                     68:        n->name = strdup(dirname);
                     69:        if (!n->name)
                     70:                errx(1, "out of memory");
                     71:        n->next = 0;
                     72:        return n;
                     73: }
                     74:
                     75: void
                     76: addtoincludepath(dirname)
                     77:        const char *dirname;
                     78: {
                     79:        struct path_entry *n;
                     80:
                     81:        n = new_path_entry(dirname);
                     82:
                     83:        if (last) {
                     84:                last->next = n;
                     85:                last = n;
                     86:        }
                     87:        else
                     88:                last = first = n;
                     89: }
                     90:
                     91: static void
                     92: ensure_m4path()
                     93: {
                     94:        static int envpathdone = 0;
                     95:        char *envpath;
                     96:        char *sweep;
                     97:        char *path;
                     98:
                     99:        if (envpathdone)
                    100:                return;
                    101:        envpathdone = TRUE;
                    102:        envpath = getenv("M4PATH");
                    103:        if (!envpath)
                    104:                return;
                    105:        /* for portability: getenv result is read-only */
                    106:        envpath = strdup(envpath);
                    107:        if (!envpath)
                    108:                errx(1, "out of memory");
                    109:        for (sweep = envpath;
                    110:            (path = strsep(&sweep, ":")) != NULL;)
                    111:            addtoincludepath(path);
                    112:        free(envpath);
                    113: }
                    114:
                    115: static
1.4     ! espie     116: struct input_file *
        !           117: dopath(i, filename)
        !           118:        struct input_file *i;
1.3       espie     119:        const char *filename;
1.1       espie     120: {
                    121:        char path[MAXPATHLEN];
                    122:        struct path_entry *pe;
1.4     ! espie     123:        FILE *f;
1.1       espie     124:
                    125:        for (pe = first; pe; pe = pe->next) {
                    126:                snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
1.4     ! espie     127:                if ((f = fopen(path, "r")) != 0) {
        !           128:                        set_input(i, f, path);
        !           129:                        return i;
        !           130:                }
1.1       espie     131:        }
                    132:        return NULL;
                    133: }
                    134:
1.4     ! espie     135: struct input_file *
        !           136: fopen_trypath(i, filename)
        !           137:        struct input_file *i;
1.1       espie     138:        const char *filename;
                    139: {
                    140:        FILE *f;
                    141:
                    142:        f = fopen(filename, "r");
1.4     ! espie     143:        if (f != NULL) {
        !           144:                set_input(i, f, filename);
        !           145:                return i;
        !           146:        }
1.1       espie     147:        if (filename[0] == '/')
                    148:                return NULL;
                    149:
                    150:        ensure_m4path();
                    151:
1.4     ! espie     152:        return dopath(i, filename);
1.1       espie     153: }
                    154: