File: path.c

package info (click to toggle)
mpsql 2.1-2
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 3,528 kB
  • ctags: 4,886
  • sloc: ansic: 35,184; makefile: 3,761; sh: 44
file content (311 lines) | stat: -rw-r--r-- 9,427 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*  -*- c -*-  */
/* -------------------------------------------------------------------- *
**  copyright (c) 1995 ipvr stuttgart and thomas harrer
** -------------------------------------------------------------------- *
**
**  libhelp
**
**  a comprehensive hypertext help system for OSF/Motif(tm) applications. 
**  based on libhtmlw from NCSA Mosaic(tm) version 2.4
**
**  written by thomas harrer
**  e-mail: Thomas.Harrer@rus.uni-stuttgart.de
**  
** -------------------------------------------------------------------- *
*h  $Id: path.c,v 1.8 1995/06/28 12:59:30 thomas Exp $
** -------------------------------------------------------------------- *
**
*h  module:		path.c
**
**  contents:		the help path implementation
**
**  interface:		procedures 
**			* get_help_path
**
** -------------------------------------------------------------------- *
**  license and copying issues:
**
**  this software is free; you can redistribute it and/or modify it 
**  under terms similar to the gnu general public license (version 1 
**  or any later version published by the free software foundation). 
**  see the file Licence for more details.
**
**  this program is distributed in the hope that it will be useful,
**  but without any warranty; without even the implied warranty of
**  merchantability or fitness for a particular purpose.  
** -------------------------------------------------------------------- */
/*----------------------------------------------------------------------*
*g  include section
**----------------------------------------------------------------------*/
/* application definitions  */
#include "helpp.h"
#include "path.h"
#include "util.h"
#include "buffer.h"

/* -------------------------------------------------------------------- *
*g  module identification
** -------------------------------------------------------------------- */
#ifdef RCSID
static char rcsid [] =
    "$Id: path.c,v 1.8 1995/06/28 12:59:30 thomas Exp $";
#endif /* RCSID */

/* -------------------------------------------------------------------- *
*p  private prototypes
** -------------------------------------------------------------------- */
static void initialize_help_path (void);

/* -------------------------------------------------------------------- *
*g  module data
** -------------------------------------------------------------------- */
/* the help path  */
static char**	help_path = NULL;      /* the help path (string array).	*/
static int	help_max = 0;	       /* max (strlen (help_path [i])).	*/

/* -------------------------------------------------------------------- *
*p  procedure-name:	path_get_helppath
**
**  purpose:		try to add path prefices in front of the help
**			file string. take the first that results in an
**			accessible help file.
**  initialization:	the help path (array of directory strings) is 
**			initialized of not already.
** -------------------------------------------------------------------- *
**  args:		filename and opt. anchor (file#anchor)
**  return type:	static char*
** -------------------------------------------------------------------- */
char*
path_get_helppath (/* i  */ char* str)
{
    if (str) {

	/* local data.  */
	char* 	pathname;	/* return pointer		*/
	char* 	filename;	/* filename part of str.		*/
	int 	i = 0;		/* loop counter (see below).	*/

	execute ("get_help_path");

	/*
	 *  the help path is stored in an array of strings for ease of 
	 *  access. it must be initialized from the MPSQL_HELP_PATH 
	 *  environment variable and from the HELP_PATH praeprocessor 
	 *  macro. it it is not already initialized, we do it first.
	 */
	if (!help_path)  {    
	    initialize_help_path ();
	}	

	/* we strip of the anchor;  */
	filename = get_file (str); /* scope -> returned  or local. */
	if (!filename) return NULL;

	/*
	 *  if the file name is absolute: we just return it 
	 *  (without anchor). there is compressed file transparency 
	 *  for absolute filenames too.
	 */
	if ((filename [0] == '/') || (filename [0] == '.')) {

	    if (0 == access (filename, R_OK)) {
		return filename;
	    } else {
		int len = c_strlen (filename) + 4;
		checked_realloc (filename, len, char);
		c_strcat (filename, ".gz");
		if (0 == access (filename, R_OK)) {
		    return filename;
		} else {
		    checked_free (filename);
		    return NULL;
		}
	    }
	}

	/* we need one buffer for new filenames. ret points to this buffer.  */
	/* begin  */  {
	    int len = c_strlen (filename) + 4;
	    if (help_path[0]) {
		int	hp0_len = c_strlen (help_path[0]);
		if (help_max < hp0_len)  {
		    help_max = hp0_len;
		}
	    }
	    len += help_max;
	    checked_malloc (pathname, len, char); /* scope returned.  */
	}
    
	if (help_path[0]) i = 0; else i = 1;

	/*
	 *  this is the actual help path search using access().
	 *  at this level gzipped files transparency comes into play.
	 */
	while (help_path[i]) {

	    c_strcpy (pathname, help_path[i]);
	    c_strcat (pathname, filename);

	    if (access (pathname, R_OK) == 0)
		break;

	    /* only support for gzip-compressed files.  */
	    c_strcat (pathname, ".gz");
	    if (access (pathname, R_OK) == 0)
		break;

	    i++;
	}
	
	/* if filename is not returned, we free allocation.  */
	checked_free (filename);

	if (help_path[i]) {
	    return pathname;
	} else {
	    /* not found in the help path.  */
	    checked_free (pathname);
	    return NULL;
	}
    } else  {
	return NULL;
    }
    
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	path_add_current
**
**  purpose:		adds the current document path to the 
**			help path.
** -------------------------------------------------------------------- */
void
path_add_current (/* i  */ char* path)
{
    static buffer_t* current = NULL;   /* the current pathname.         */
    execute ("path_add_current");
    
    /* initialize current path buffer */
    if (!current) {
	current = bf_new ();	/* allocated only once. never freeed. */
    }

    if (path) {
	bf_strcpy (current, path);
	help_path[0] = bf_the_buffer (current);
    }
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	path_the_helppath
**
**  purpose:		exports the help path
** -------------------------------------------------------------------- */
char**
path_the_helppath (void)
{
    execute ("the_help_path");
    return help_path;
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	initialize_help_path
**
**  purpose:		initializes the help path array. this array
**			contains the entries of the help path as strings.
** -------------------------------------------------------------------- *
**  side effects:	help_path is initialized and help_max is set
**			to the maximum string length of entries.
** -------------------------------------------------------------------- */
static void
initialize_help_path (void)
{
    int 	i = 0;		       /* loop counter			*/
    int 	count = 0;	       /* number of path entries.	*/
    char* 	path;		       /* path from environment		*/
    char* 	prefix = NULL;	       /* copy of the whole path.	*/

    char* 	token;		       /* for token scanning (strtok).  */
    char 	delim[] = ":";	       /* delimiters dto.		*/

    execute ("initialize_help_path");

    /* if it seems that the help_path is already set up we do nothing.  */
    if (help_path) return;

    path = getenv ("MPSQL_HELP_PATH");

    /* first we make a copy of the whole path list by concatenating.  */
    if (!path) {
	checked_strdup (prefix, HELP_PATH); /* scope local */
    } else {
	int size = c_strlen (HELP_PATH) + c_strlen (path) + 2;
	checked_malloc (prefix, size, char); /* scope local */
	c_strcpy (prefix, path);
	c_strcat (prefix, ":");
	c_strcat (prefix, HELP_PATH);
    }
	
    /* we compute the number of path entries.  */
    for (i = 0; *(prefix + i) != '\0'; i++) {
	if (*(prefix + i) == ':')
	    count++;
    }

    /* and allocate as much slots for pathname strings.  */
    checked_malloc (help_path, count + 3, char*); /* must not be freed */

    /* we begin with the first path component.  */
    token = strtok (prefix, delim);

    help_path[0] = NULL;
    i = 1;

    /* and seperate the whole path list.  */
    while (token) {
	int len = c_strlen (token) + 2;
	int end;
	char* entry;
	    
	checked_malloc (entry, len, char); /* scope: global or local */

	/* we save the maximum string lenght for later use.  */
	if (help_max < len) help_max = len;
	    
	/* after some modifications we store the pathname in help_path.  */
	c_strcpy (entry, token);
	end = c_strlen (entry);
	if ((end > 0) && (entry [end - 1] != '/')) {
	    entry [end] = '/';
	    entry [end + 1] = '\0';
	}

	/* entry is either stored in the help path or freed.  */
	if (end != 0) {
	    help_path[i] = entry;
	    i++;
	} else  {
	    checked_free (entry);
	}

	/* now we continue with the next entry in the path.  */
	token = strtok (NULL, delim);
    }
    /* we mark the end of the help path with the NULL string.  */
    help_path[i] = NULL;
    if (prefix)  {
	checked_free (prefix);
    }
}

/* -------------------------------------------------------------------- *
*l  emacs:
**  local variables:
**  mode:		c
**  outline-regexp:	"\*[HGPLT]"
**  gh-language:	"English"
**  comment-column:	32
**  eval:		(outline-minor-mode t)
**  end:
** -------------------------------------------------------------------- */