File: HTIcons.c

package info (click to toggle)
cern-httpd 3.0A-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 5,392 kB
  • ctags: 6,554
  • sloc: ansic: 37,902; makefile: 1,746; perl: 535; csh: 167; sh: 143
file content (336 lines) | stat: -rw-r--r-- 9,200 bytes parent folder | download | duplicates (3)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*			Icon Management		       		HTIcon.c
**			===============
**
**	This module contains the functions for initializing, adding
**	and selecting the icon for local directory listings, FTP and Gopher.
**
**	History:
**	   Mar 94	Written by Ari Luotonen, luotonen@ptsun00.cern.ch
**
*/

/* Library include files */
#include "HTMLPDTD.h"
#include "HTUtils.h"
#include "HTAnchor.h"
#include "HTParse.h"
#include "HTFile.h"
#include "HTFormat.h"
#include "HTChunk.h"
#include "HTIcons.h"					 /* Implemented here */

#ifdef VMS
typedef unsigned long mode_t;
#include "HTVMSUtils.h"
#endif /* VMS */

/* Globals */
PUBLIC BOOL HTDirShowBrackets = YES;
PUBLIC HTIconNode * icon_unknown = NULL;	/* Unknown file type */
PUBLIC HTIconNode * icon_blank = NULL;		/* Blank icon in heading */
PUBLIC HTIconNode * icon_parent = NULL;		/* Parent directory icon */
PUBLIC HTIconNode * icon_dir = NULL;		/* Directory icon */

/* Type definitions and global variables etc. local to this module */
PRIVATE HTList * icons = NULL;
PRIVATE int alt_len = 0;			/* Longest ALT text */

/* 
 * Global variable for the AddHref nodes
 * AddHref URL suff1 suff2 ...
 */
PRIVATE HTList * hrefs = NULL;


/* ------------------------------------------------------------------------- */

PRIVATE void alt_resize ARGS1(char *, alt)
{
    if (alt) {
	int len = strlen(alt);
	if (len > alt_len) alt_len = len;
    }
}


PUBLIC char * HTIcon_alt_string ARGS2(char *,	alt,
				      BOOL,	brackets)
{
    static char * ret = NULL;
    char * p = NULL;
    int len = alt ? strlen(alt) : 0;

    if (ret) free(ret);			/* From previous call */
    p = ret = (char*)malloc(alt_len + 3);
    if (!ret) outofmem(__FILE__, "HTIcon_alt_string");

    if (HTDirShowBrackets)
	*p++ = brackets ? '[' : ' ';
    if (alt) strcpy(p,alt);
    p += len;
    while (len++ < alt_len) *p++=' ';
    if (HTDirShowBrackets)
	*p++ = brackets ? ']' : ' ';
    *p = 0;

    return ret;
}


/*
**	HTAddIcon(url, alt, type_templ) adds icon:
**
**		<IMG SRC="url" ALT="[alt]">
**
**	for files for which content-type or content-encoding matches
**	type_templ.  If type_templ contains a slash, it is taken to be
**	a content-type template.  Otherwise, it is a content-encoding
**	template.
*/
PUBLIC void HTAddIcon ARGS3(char *,	url,
			    char *,	alt,
			    char *,	type_templ)
{
    HTIconNode * node;

    if (!url || !type_templ) return;

    node = (HTIconNode*)calloc(1,sizeof(HTIconNode));
    if (!node) outofmem(__FILE__, "HTAddIcon");

    if (url) StrAllocCopy(node->icon_url, url);
    if (alt) StrAllocCopy(node->icon_alt, alt);
    if (type_templ) StrAllocCopy(node->type_templ, type_templ);

    if (!icons) icons = HTList_new();
    HTList_addObject(icons, (void*)node);
    alt_resize(alt);
    CTRACE(stderr,
	   "AddIcon..... %s => SRC=\"%s\" ALT=\"%s\"\n",type_templ,url,
	   alt ? alt : "");
}


/*
 * Put the AddHrefs in a list. It can be used for indexing to
 * present special filetypes through a CGI.
 */
PUBLIC void HTAddHref ARGS2(char *,     url,
                            char *,     type_templ)
{
    HTHrefNode * node;

    if (!url || !type_templ) return;

    node = (HTHrefNode*)calloc(1,sizeof(HTHrefNode));
    if (!node) outofmem(__FILE__, "HTAddHref");

    if (url) StrAllocCopy(node->href_url, url);
    if (type_templ) StrAllocCopy(node->type_templ, type_templ);

    if (!hrefs) hrefs = HTList_new();
    HTList_addObject(hrefs, (void*)node);
    CTRACE(stderr,
           "AddHref..... %s => URL=\"%s\" \n",type_templ,url);
}



/*
**	HTAddUnknownIcon(url,alt) adds the icon used for files for which
**	no other icon seems appropriate (unknown type).
*/
PUBLIC void HTAddUnknownIcon ARGS2(char *, url,
				   char *, alt)
{
    icon_unknown = (HTIconNode*)calloc(1,sizeof(HTIconNode));
    if (!icon_unknown) outofmem(__FILE__, "HTAddUnknownIcon");

    if (url) StrAllocCopy(icon_unknown->icon_url, url);
    if (alt) StrAllocCopy(icon_unknown->icon_alt, alt);
    alt_resize(alt);
    CTRACE(stderr,"AddIcon..... UNKNOWN => SRC=\"%s\" ALT=\"%s\"\n",url,
	   alt ? alt : "");

}


/*
**	HTAddBlankIcon(url,alt) adds the blank icon used in the
**	heading of the listing.
*/
PUBLIC void HTAddBlankIcon ARGS2(char *, url,
				 char *, alt)
{
    icon_blank = (HTIconNode*)calloc(1,sizeof(HTIconNode));
    if (!icon_blank) outofmem(__FILE__, "HTAddBlankIcon");

    if (url) StrAllocCopy(icon_blank->icon_url, url);
    if (alt) StrAllocCopy(icon_blank->icon_alt, alt);
    alt_resize(alt);
    CTRACE(stderr,"AddIcon..... BLANK => SRC=\"%s\" ALT=\"%s\"\n",url,
	   alt ? alt : "");
}


/*
**	HTAddParentIcon(url,alt) adds the parent directory icon.
*/
PUBLIC void HTAddParentIcon ARGS2(char *, url,
				  char *, alt)
{
    icon_parent = (HTIconNode*)calloc(1,sizeof(HTIconNode));
    if (!icon_parent) outofmem(__FILE__, "HTAddBlankIcon");

    if (url) StrAllocCopy(icon_parent->icon_url, url);
    if (alt) StrAllocCopy(icon_parent->icon_alt, alt);
    alt_resize(alt);
    CTRACE(stderr,"AddIcon..... PARENT => SRC=\"%s\" ALT=\"%s\"\n",url,
	   alt ? alt : "");
}


/*
**	HTAddDirIcon(url,alt) adds the directory icon.
*/
PUBLIC void HTAddDirIcon ARGS2(char *, url,
			       char *, alt)
{
    icon_dir = (HTIconNode*)calloc(1,sizeof(HTIconNode));
    if (!icon_dir) outofmem(__FILE__, "HTAddBlankIcon");

    if (url) StrAllocCopy(icon_dir->icon_url, url);
    if (alt) StrAllocCopy(icon_dir->icon_alt, alt);
    alt_resize(alt);
    CTRACE(stderr,
	   "AddIcon..... DIRECTORY => SRC=\"%s\" ALT=\"%s\"\n",url,
	   alt ? alt : "");
}


PRIVATE BOOL match ARGS2(char *, templ,
			 char *, actual)
{
    static char * c1 = NULL;
    static char * c2 = NULL;
    char * slash1;
    char * slash2;

    StrAllocCopy(c1,templ);
    StrAllocCopy(c2,actual);

    slash1 = strchr(c1,'/');
    slash2 = strchr(c2,'/');

    if (slash1 && slash2) {
	*slash1++ = 0;
	*slash2++ = 0;
	return HTAA_templateMatch(c1,c2) && HTAA_templateMatch(slash1,slash2);
    }
    else if (!slash1 && !slash2)
	return HTAA_templateMatch(c1,c2);
    else
	return NO;
}


PRIVATE char * prefixed ARGS2(CONST char *,	prefix,
			      char *,		name)
{
    static char * ret = NULL;
    FREE(ret);	/* From previous call */

    ret = (char *)malloc(strlen(prefix) + strlen(name) + 2);
    if (!ret) outofmem(__FILE__, "prefixed");

    strcpy(ret,prefix);
    if (*prefix && prefix[strlen(prefix)-1] != '/')
	strcat(ret,"/");
    strcat(ret,name);
    return ret;
}


PUBLIC void HTStdIconInit ARGS1(CONST char *, url_prefix)
{
    CONST char * p = url_prefix ? url_prefix : "/internal-icon/";

    HTAddBlankIcon  (prefixed(p,"blank.xbm"),	NULL	);
    HTAddDirIcon    (prefixed(p,"directory.xbm"),"DIR"	);
    HTAddParentIcon (prefixed(p,"back.xbm"),	"UP"	);
    HTAddUnknownIcon(prefixed(p,"unknown.xbm"),	NULL	);
    HTAddIcon(prefixed(p,"unknown.xbm"),	NULL,	"*/*");
    HTAddIcon(prefixed(p,"binary.xbm"),		"BIN",	"binary");
    HTAddIcon(prefixed(p,"unknown.xbm"),	NULL,	"www/unknown");
    HTAddIcon(prefixed(p,"text.xbm"),		"TXT",	"text/*");
    HTAddIcon(prefixed(p,"image.xbm"),		"IMG",	"image/*");
    HTAddIcon(prefixed(p,"movie.xbm"),		"MOV",	"video/*");
    HTAddIcon(prefixed(p,"sound.xbm"),		"AU",	"audio/*");
    HTAddIcon(prefixed(p,"tar.xbm"),		"TAR",	"multipart/x-tar");
    HTAddIcon(prefixed(p,"tar.xbm"),		"TAR",	"multipart/x-gtar");
    HTAddIcon(prefixed(p,"compressed.xbm"),	"CMP",	"x-compress");
    HTAddIcon(prefixed(p,"compressed.xbm"),	"GZP",	"x-gzip");
    HTAddIcon(prefixed(p,"index.xbm"),		"IDX",	"application/x-gopher-index");
    HTAddIcon(prefixed(p,"index2.xbm"),		"CSO",	"application/x-gopher-cso");
    HTAddIcon(prefixed(p,"telnet.xbm"),		"TEL",	"application/x-gopher-telnet");
    HTAddIcon(prefixed(p,"unknown.xbm"),       	"DUP",	"application/x-gopher-duplicate");
    HTAddIcon(prefixed(p,"unknown.xbm"),	"TN",	"application/x-gopher-tn3270");
}


/*								 HTGetIcon()
** returns the icon corresponding to content_type or content_encoding.
*/
PUBLIC HTIconNode * HTGetIcon ARGS3(mode_t,	mode,
				    HTFormat,	content_type,
				    HTFormat,	content_encoding)
{
    if (!icon_unknown) icon_unknown = icon_blank;

    if ((mode & S_IFMT) == S_IFREG) {
	char * ct = content_type ? HTAtom_name(content_type) : NULL;
	char * ce = content_encoding ? HTAtom_name(content_encoding) : NULL;
	HTList * cur = icons;
	HTIconNode * node;

	while ((node = (HTIconNode*)HTList_nextObject(cur))) {
	    char * slash = strchr(node->type_templ,'/');
	    if ((ct && slash && match(node->type_templ,ct)) ||
		(ce && !slash && HTAA_templateMatch(node->type_templ,ce))) {
		return node;
	    }
	}
    } else if ((mode & S_IFMT) == S_IFDIR) {
	return icon_dir ? icon_dir : icon_unknown;
    } else if ((mode & S_IFMT) == S_IFLNK) {
	return icon_dir ? icon_dir : icon_unknown;	/* @@ */
    }

    return icon_unknown;
}


/*
 * Find the URL for a given type. Called from HTDirBrw.c
 */
PUBLIC HTHrefNode * HTGetHref ARGS1( char *,	filename)
{
    HTHrefNode * node;
    char *c;

    HTList * cur = hrefs;

    c = strrchr(filename, '.');
    if (c) {
	while ((node = (HTHrefNode*)HTList_nextObject(cur))) {
	    if ((!strcmp(node->type_templ,c)) ) {
		return node;
	    }
	}
    }
    return NULL;
}

/* END OF MODULE */