]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libmytinfo/buildpath.c
This is the addition of a syslog(3) security.* top-level category. This
[FreeBSD/FreeBSD.git] / lib / libmytinfo / buildpath.c
1 /*
2  * buildpath.c
3  *
4  * By Ross Ridge
5  * Public Domain
6  * 92/02/01 07:29:42
7  *
8  * _buildpath builds a list of file names and terminal descriprions extracted
9  * from its arguments. It returns a pointer to a structure that is used by
10  * other routines as the list of file names to search for terminal
11  * descriptions.  It is passed a variable number of arguments consisting
12  * of file name and type pairs. The file name can actually be a list of
13  * file names seperated by spaces and any environment variables specified
14  * by a dollar sign ($) followed by its name are substituted in. A type
15  * of 1 indicates that the file name may actually be termcap description
16  * and a type of 2 indicates it may be a terminfo description. A type of 0
17  * indicates that the file name can only be a file name (or list of them).
18  *
19  */
20
21 #include "defs.h"
22
23 #include <ctype.h>
24
25 #ifdef USE_SCCS_IDS
26 static const char SCCSid[] = "@(#) mytinfo buildpath.c 3.2 92/02/01 public domain, By Ross Ridge";
27 #endif
28
29 /* more memory is allocated for file names every HUNK file names */
30 #define HUNK 32
31
32 /* characters that seperate file names in a list */
33 #define SEPERATORS " :"
34
35 static struct term_path *path = NULL;   /* the list of files */
36 static int files = 0;                   /* # of files in the list */
37 static int size = 0;                    /* # of files there is space for */
38
39 /* add a file name, type pair to the list */
40 static int
41 addfile(file, type)
42 char *file;
43 int type; {
44         int l;
45         char *s;
46
47         if (file == NULL) {
48                 if (type != -1)
49                         return -1;
50         } else if (file[0] == '\0')
51                 return -1;
52
53 #ifdef DEBUG
54         if (file != NULL)
55                 printf("addfile: %s\n", file);
56 #endif
57
58         if (files >= size) {
59                 size += HUNK;
60                 path = (struct term_path *) reallocf((anyptr) path,
61                         size * sizeof(struct term_path));
62                 if (path == NULL)
63                         return 0;
64         }
65         if (file == NULL) {
66                 path[files].file = file;
67         } else {
68                 l = strlen(file) + 1;
69                 s = (char *) malloc(l * sizeof(char));
70                 if (s == NULL)
71                         return 0;
72                 path[files].file = strcpy(s, file);
73         }
74         path[files].type = type;
75
76         return ++files;
77 }
78
79 /* deallocate space used by the path list */
80 void
81 _delpath(ppath)
82 struct term_path *ppath; {
83         struct term_path *p;
84
85         p = ppath;
86         while(p->file != NULL) {
87                 free((anyptr)p->file);
88                 p++;
89         }
90
91         free((anyptr)ppath);
92 }
93
94 /* build a list of paths. see above */
95 #ifdef lint
96 /*VARARGS2*/
97 struct term_path *
98 _buildpath(file, type)
99 char *file;
100 int type;
101 #else
102 #ifdef USE_STDARG
103 #ifdef USE_PROTOTYPES
104 struct term_path *_buildpath(char *file, int type, ...)
105 #else
106 struct term_path *_buildpath(file, type)
107 char *file;
108 int type;
109 #endif /* USE_PROTOTYPES */
110 #else /* USE_STDARG */
111 struct term_path *_buildpath(va_alist)
112 va_dcl
113 #endif /* USE_STDARG */
114 #endif /* lint */
115 {
116 #ifndef lint
117 #ifndef USE_STDARG
118         char *file;
119         int type;
120 #endif
121 #endif
122         va_list ap;
123         register char *s, *d, *e;
124         char *p;
125         char line[MAX_BUF+1];
126         char name[MAX_NAME+1];
127         int i,j;
128
129         size = 0;
130         files = 0;
131         path = NULL;
132
133 #ifdef lint
134         ap = NULL;
135 #else
136 #ifdef USE_STDARG
137         va_start(ap, type);
138 #else
139         va_start(ap);
140         file = va_arg(ap, char *);
141         type = va_arg(ap, int);
142 #endif
143 #endif
144
145         while (type >= 0 && type <= 2) {
146                 s = file;
147                 d = line;
148                 i = 0;
149                 while(*s != '\0') {
150                         if (*s == '$') {
151                                 s++;
152                                 j = 0;
153                                 while(*s != '\0' && (*s == '_' || isalnum(*s)))
154                                         if (j < MAX_NAME) {
155                                                 name[j] = *s++;
156                                                 j++;
157                                         } else
158                                                 break;
159                                 name[j] = '\0';
160                                 e = getenv(name);
161                                 if (e != NULL) {
162                                         while(*e != '\0') {
163                                                 if (i < MAX_BUF) {
164                                                         *d++ = *e++;
165                                                         i++;
166                                                 } else
167                                                         break;
168                                         }
169                                 } else if (*s == '/')
170                                         s++;
171                         } else {
172                                 if (i < MAX_BUF) {
173                                         *d++ = *s++;
174                                         i++;
175                                 } else
176                                         break;
177                         }
178                 }
179                 *d = '\0';
180                 if (type == 0 || line[0] == '/') {
181                         p = line;
182                         while ((s = strsep(&p, SEPERATORS)) != NULL && *s == '\0')
183                                 ;
184                         while(s != NULL) {
185                                 if (addfile(s, 0) == 0)
186                                         return NULL;
187                                 while ((s = strsep(&p, SEPERATORS)) != NULL && *s == '\0')
188                                         ;
189                         }
190                 } else
191                         if (addfile(line, type) == 0)
192                                 return NULL;
193                 file = va_arg(ap, char *);
194                 type = va_arg(ap, int);
195         }
196         addfile(NULL, -1);
197         return path;
198 }