Imported Upstream version 15.8a
[platform/upstream/cscope.git] / src / vpinit.c
1 /*===========================================================================
2  Copyright (c) 1998-2000, The Santa Cruz Operation 
3  All rights reserved.
4  
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7
8  *Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10
11  *Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14
15  *Neither name of The Santa Cruz Operation nor the names of its contributors
16  may be used to endorse or promote products derived from this software
17  without specific prior written permission. 
18
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  INTERRUPTION)
27  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30  DAMAGE. 
31  =========================================================================*/
32
33 /* vpinit - initialize vpdirs or update vpdirs based on currentdir */
34
35 #include <stdio.h>      /* stderr */
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include "vp.h"
40 #include "alloc.h"
41 #include "library.h"
42 #include "global.h"
43 #include "constants.h"
44
45 static char const rcsid[] = "$Id: vpinit.c,v 1.8 2006/07/23 20:59:20 broeker Exp $";
46
47 #if !NOMALLOC
48 char    **vpdirs;       /* directories (including current) in view path */
49 #else
50 char    vpdirs[MAXDIR][DIRLEN + 1];
51 #define MAXVPATH (MAXDIR * (DIRLEN + 1))
52 #endif
53 int     vpndirs;        /* number of directories in view path */
54
55 void
56 vpinit(char *current_dir)
57 {
58         char    *suffix;        /* path from view path node */
59         char    *vpath;         /* VPATH environment variable value */
60         char    buf[MAXPATH + 1];
61         int     i;
62         char    *s;
63 #if NOMALLOC
64         char    *node;          /* view path node */
65         char    vpathbuf[MAXVPATH + 1];
66 #endif
67         
68         /* if an existing directory list is to be updated, free it */
69         if (current_dir != NULL && vpndirs > 0) {
70 #if !NOMALLOC
71                 for (i = 0; i < vpndirs; ++i) {
72                         free(vpdirs[i]);
73                 }
74                 free(vpdirs);
75 #endif
76                 vpndirs = 0;
77         }
78         /* return if the directory list has been computed */
79         /* or there isn't a view path environment variable */
80         if (vpndirs > 0 || (vpath = getenv("VPATH")) == NULL ||
81             *vpath == '\0') {
82                 return;
83         }
84         /* if not given, get the current directory name */
85         if (current_dir == NULL && (current_dir = getcwd(buf, MAXPATH)) == NULL) {
86                 (void) fprintf(stderr, "%s: cannot get current directory name\n", argv0);
87                 return;
88         }
89         /* see if this directory is in the first view path node */
90         for (i = 0; vpath[i] == current_dir[i] && vpath[i] != '\0'; ++i) {
91                 ;
92         }
93         if ((vpath[i] != ':' && vpath[i] != '\0') ||
94             (current_dir[i] != '/' && current_dir[i] != '\0')) {
95                 return;
96         }
97         suffix = &current_dir[i];
98 #if !NOMALLOC
99
100         /* count the nodes in the view path */
101         vpndirs = 1;
102         for (i = 0; vpath[i] != '\0'; ++i) {
103                 if (vpath[i] == ':' && vpath[i + 1]) {
104                         ++vpndirs;
105                 }
106         }
107         /* create the source directory list */
108         vpdirs = mymalloc(vpndirs * sizeof(char *));
109
110         /* don't change VPATH in the environment */
111         vpath = my_strdup(vpath);
112         
113         /* split the view path into nodes */
114         for (i = 0, s = vpath; *s != '\0'; ++i) {
115                 vpdirs[i] = s;
116                 while (*s != '\0' && *++s != ':') {
117                         if (*s == '\n') {
118                                 *s = '\0';
119                         }
120                 }
121                 if (*s != '\0') {
122                         *s++ = '\0';
123                 }
124         }
125         /* convert the view path nodes to directories */
126         for (i = 0; i < vpndirs; ++i) {
127                 s = mymalloc((strlen(vpdirs[i]) + strlen(suffix) + 1));
128                 (void) strcpy(s, vpdirs[i]);
129                 (void) strcat(s, suffix);
130                 vpdirs[i] = s;
131         }
132         free(vpath);
133 #else
134         /* don't change VPATH in the environment */
135         if (strlen(vpath) > MAXVPATH) {
136                 (void) fprintf(stderr, "%s: VPATH is longer than %d characters: %s\n", argv0, MAXVPATH, vpath);
137                 return;
138         }
139         (void) strcpy(vpathbuf, vpath);
140         s = vpathbuf;
141         
142         /* convert the view path nodes to directories */
143         while (*s != '\0') {
144                 
145                 /* get the next node */
146                 node = s;
147                 while (*s != '\0' && *++s != ':') {
148                         if (*s == '\n') {
149                                 *s = '\0';
150                         }
151                 }
152                 if (*s != '\0') {
153                         *s++ = '\0';
154                 }
155                 /* ignore a directory that is too long */
156                 if (strlen(node) + strlen(suffix) > DIRLEN) {
157                         (void) fprintf(stderr, "%s: VPATH directory is longer than %d characters: %s%s\n", argv0, DIRLEN, node, suffix);
158                 }
159                 else if (vpndirs >= MAXDIR) {
160                         (void) fprintf(stderr, "%s: VPATH has more than %d nodes\n", argv0, vpndirs);
161                         return;
162                 }
163                 else {
164                         /* create the view path directory */
165                         (void) strcpy(vpdirs[vpndirs], node);
166                         (void) strcat(vpdirs[vpndirs], suffix);
167                         ++vpndirs;
168                 }
169         }
170 #endif
171 }