[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.3

1.3     ! espie       1: /* $OpenBSD: gnum4.c,v 1.2 1999/09/14 08:35:16 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));
        !            57: static FILE *dopath __P((const char *));
        !            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
                    116: FILE *
1.3     ! espie     117: dopath(filename)
        !           118:        const char *filename;
1.1       espie     119: {
                    120:        char path[MAXPATHLEN];
                    121:        struct path_entry *pe;
                    122:        FILE *file;
                    123:
                    124:        for (pe = first; pe; pe = pe->next) {
                    125:                snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
                    126:                if ((file = fopen(path, "r")) != 0)
                    127:                        return file;
                    128:        }
                    129:        return NULL;
                    130: }
                    131:
                    132: FILE *
                    133: fopen_trypath(filename)
                    134:        const char *filename;
                    135: {
                    136:        FILE *f;
                    137:
                    138:        f = fopen(filename, "r");
                    139:        if (f)
                    140:                return f;
                    141:        if (filename[0] == '/')
                    142:                return NULL;
                    143:
                    144:        ensure_m4path();
                    145:
                    146:        return dopath(filename);
                    147: }
                    148:
                    149:
                    150: