Annotation of libwww/Library/src/HTDir.c, revision 2.22
2.1 frystyk 1: /* HTDir.c
2: ** DIRECTORY BROWSING
3: **
4: ** (c) COPYRIGHT MIT 1995.
5: ** Please first read the full copyright statement in the file COPYRIGH.
2.22 ! frystyk 6: ** @(#) $Id: HTDir.c,v 2.21 1999/02/07 18:24:19 frystyk Exp $
2.1 frystyk 7: **
8: ** This is unix-specific code in general
9: ** The module is intended for use in HTFile.c and HTFTP.c where
10: ** it replaces the old directory browsing routine.
11: ** The module is only compiled if GOT_READ_DIR is defined
12: **
13: ** Authors:
2.5 frystyk 14: ** HF Henrik Frystyk, MIT, <frystyk@w3.org>
2.1 frystyk 15: ** History:
16: ** Sep 95 HFN written
17: **
18: ** Note:
19: ** It could be a HTML table instead
20: */
21:
22: /* Library include files */
2.20 frystyk 23: #include "wwwsys.h"
2.18 frystyk 24: #include "WWWUtil.h"
25: #include "WWWCore.h"
2.21 frystyk 26: #include "WWWFile.h"
2.18 frystyk 27: #include "WWWHTML.h"
2.1 frystyk 28: #include "HTIcons.h"
29: #include "HTDescpt.h"
30: #include "HTDir.h" /* Implemented here */
31:
32: /* Macros and other defines */
33: #define PUTC(c) (*target->isa->put_character)(target, c)
34: #define PUTS(s) (*target->isa->put_string)(target, s)
35: #define START(e) (*target->isa->start_element)(target, e, 0, 0)
36: #define END(e) (*target->isa->end_element)(target, e)
37: #define FREE_TARGET (*target->isa->_free)(target)
38:
39: #define DEFAULT_MINFW 15
2.4 frystyk 40: #define DEFAULT_MAXFW 25
2.1 frystyk 41:
42: /* Type definitions and global variables etc. local to this module */
43:
44: struct _HTStructured {
2.13 frystyk 45: const HTStructuredClass * isa;
2.1 frystyk 46: /* ... */
47: };
48:
49: struct _HTDir {
50: HTStructured * target;
51: HTRequest * request;
52: HTArray * array; /* Array for sorted listings */
53: char * fnbuf; /* File name buffer */
54: char * lnbuf; /* Rest of line */
55: char * base; /* base url is any */
56: HTDirShow show; /* What do we want to show */
57: HTDirKey key; /* Key for sorting */
58: int size; /* Number of files */
59: int curfw; /* Max file name length in list */
60: };
61:
62: typedef struct _HTDirNode {
63: char * fname;
64: char * date;
65: char * size;
66: char * note;
67: HTFileMode mode;
68: } HTDirNode;
69:
70: typedef enum _HTShowLength { /* Width of each collumn */
71: HT_DLEN_SIZE = 6,
72: HT_DLEN_DATE = 15,
73: HT_DLEN_SPACE = 1,
74: HT_DLEN_DES = 25
75: } HTShowLength;
76:
77: PRIVATE int MinFileW = DEFAULT_MINFW;
78: PRIVATE int MaxFileW = DEFAULT_MAXFW;
79:
80: /* ------------------------------------------------------------------------- */
81: /* LINE JUSTIFICATION */
82: /* ------------------------------------------------------------------------- */
83:
84: /*
85: ** Left-justifies str_in into str_out expecting str_out having size length
86: ** l is number of chars. No 0 termination, rest of line filled with ' '
87: */
88: PRIVATE void LeftStr (char **outstr, char * instr, int l)
89: {
90: char *out = *outstr;
91: while (l-- > 0 && *instr && (*out++ = *instr++));
92: while (l-- > 0) *out++ = ' ';
93: *outstr = out;
94: }
95:
96: /*
97: ** Like LeftStr(), but result is right-justified.
98: ** l is number of chars. No 0 termination
99: */
100: PRIVATE void RightStr (char **outstr, char * instr, int l)
101: {
102: char *start = *outstr+l-strlen(instr);
103: char *out = *outstr;
104: while (out<start) *out++ = ' ';
105: while (*instr && (*out++ = *instr++));
106: *outstr = out;
107: }
108:
109: /* ------------------------------------------------------------------------- */
110: /* NODE MANAGEMENT */
111: /* ------------------------------------------------------------------------- */
112:
113: /*
114: ** Create a sort key node
115: */
116: PRIVATE HTDirNode * HTDirNode_new (void)
117: {
118: HTDirNode *node;
2.10 frystyk 119: if ((node = (HTDirNode *) HT_CALLOC(1, sizeof(HTDirNode))) == NULL)
120: HT_OUTOFMEM("HTDirNode_new");
2.1 frystyk 121: return node;
122: }
123:
124: /*
125: ** Free a sort key node
126: */
127: PRIVATE BOOL HTDirNode_free (HTDirNode *node)
128: {
129: if (node) {
2.10 frystyk 130: HT_FREE(node->fname);
131: HT_FREE(node->date);
132: HT_FREE(node->size);
133: HT_FREE(node->note);
134: HT_FREE(node);
2.1 frystyk 135: return YES;
136: }
137: return NO;
138: }
139:
140: /*
2.17 frystyk 141: ** Escape a filename and add a '/' if it's a directory
142: */
143: PRIVATE char * expand_name (char * name, HTFileMode mode)
144: {
145: char * escaped = HTEscape(name, URL_XPALPHAS);
146: if (mode == HT_IS_DIR)
147: if (*(name+strlen(name)-1) != '/') StrAllocCat(escaped, "/");
148: return escaped;
149: }
150:
151: /*
2.1 frystyk 152: ** Output an element in HTML
153: ** Returns YES if OK, else NO
154: */
155: PRIVATE BOOL HTDirNode_print (HTDir *dir, HTDirNode *node)
156: {
157: char *tp = NULL;
158: HTStructured *target = dir->target;
159: if (dir->show & HT_DS_ICON) {
160: HTFormat format = NULL;
161: HTEncoding encoding = NULL;
162: double q=1.0;
2.15 frystyk 163: HTIconNode * icon;
2.1 frystyk 164: if (node->mode == HT_IS_FILE)
2.14 frystyk 165: HTBind_getFormat(node->fname, &format, &encoding, NULL, NULL, &q);
2.15 frystyk 166: icon = HTIcon_find(node->mode, format, encoding);
2.1 frystyk 167:
168: /* Are we having a hot or a cold icon? */
169: if (!(dir->show & HT_DS_HOTI)) {
2.9 frystyk 170: if (icon) {
2.15 frystyk 171: char * alt = HTIcon_alternative(icon, YES);
172: HTMLPutImg(target, HTIcon_url(icon), alt, NULL);
173: HT_FREE(alt);
2.9 frystyk 174: PUTC(' ');
175: }
2.1 frystyk 176: }
177:
178: /* Start the anchor element */
179: if (dir->base) {
2.17 frystyk 180: char *escaped = expand_name(node->fname, node->mode);
2.10 frystyk 181: char *full;
2.12 frystyk 182: if ((full = (char *) HT_MALLOC(strlen(escaped)+strlen(dir->base)+1)) == NULL)
2.10 frystyk 183: HT_OUTOFMEM("HTDirNode_print");
2.1 frystyk 184: strcpy(full, dir->base);
185: strcat(full, escaped);
186: HTStartAnchor(target, NULL, full);
2.10 frystyk 187: HT_FREE(escaped);
188: HT_FREE(full);
2.1 frystyk 189: } else {
2.17 frystyk 190: char *escaped = expand_name(node->fname, node->mode);
2.1 frystyk 191: HTStartAnchor(target, NULL, escaped);
2.10 frystyk 192: HT_FREE(escaped);
2.1 frystyk 193: }
194:
195: if (dir->show & HT_DS_HOTI) {
2.15 frystyk 196: char * alt = HTIcon_alternative(icon, YES);
197: HTMLPutImg(target, HTIcon_url(icon), alt, NULL);
2.1 frystyk 198: PUTC(' ');
199: }
200: } else {
201: if (dir->base) {
2.17 frystyk 202: char *escaped = expand_name(node->fname, node->mode);
2.10 frystyk 203: char *full;
2.12 frystyk 204: if ((full = (char *) HT_MALLOC(strlen(escaped)+strlen(dir->base)+1)) == NULL)
2.10 frystyk 205: HT_OUTOFMEM("HTDirNode_print");
2.1 frystyk 206: strcpy(full, dir->base);
207: strcat(full, escaped);
208: HTStartAnchor(target, NULL, escaped);
2.10 frystyk 209: HT_FREE(escaped);
210: HT_FREE(full);
2.1 frystyk 211: } else {
212: char *escaped = HTEscape(node->fname, URL_XPALPHAS);
213: HTStartAnchor(target, NULL, escaped);
2.10 frystyk 214: HT_FREE(escaped);
2.1 frystyk 215: }
216: }
217:
218: /* Insert the anchor text and end anchor */
219: {
220: char *in = node->fname;
221: char *out = dir->fnbuf;
222: int l = dir->curfw;
223: while (l-- > 0 && *in && (*out++ = *in++));
224: if (*in)
225: *(out-1) = '>';
226: else if (node->mode == HT_IS_DIR) {
227: *out++ = '/';
228: l--;
229: }
230: *out = '\0';
231: PUTS(dir->fnbuf);
232: END(HTML_A);
233: out = dir->fnbuf;
234: while (l-- >= 0) *out++ = ' ';
235: LeftStr(&out, " ", HT_DLEN_SPACE);
236: *out = '\0';
237: PUTS(dir->fnbuf);
238: }
239:
240: /* Print the rest of it */
241: tp = dir->lnbuf;
242: if (node->date) {
243: RightStr(&tp, node->date, HT_DLEN_DATE);
244: LeftStr(&tp, " ", HT_DLEN_SPACE);
245: }
246: if (node->size) {
247: RightStr(&tp, node->size, HT_DLEN_SIZE);
248: LeftStr(&tp, " ", HT_DLEN_SPACE);
249: }
250: if (node->note) {
251: LeftStr(&tp, node->note, HT_DLEN_DES);
252: LeftStr(&tp, " ", HT_DLEN_SPACE);
253: }
254: *tp = '\0';
255: PUTS(dir->lnbuf);
256: PUTC('\n');
257: return YES;
258: }
259:
260: /* ------------------------------------------------------------------------- */
261: /* DIRECTORY MANAGEMENT */
262: /* ------------------------------------------------------------------------- */
263:
264: /* HTDir_headLine
265: ** --------------
266: ** Puts out the header line of the list itself
267: ** Returns YES if OK, else NO
268: */
269: PRIVATE BOOL HTDir_headLine (HTDir *dir)
270: {
271: if (dir) {
272: char *tp;
273: HTStructured *target = dir->target;
274: START(HTML_PRE);
275: if (dir->show & HT_DS_ICON) {
2.15 frystyk 276: HTIconNode * icon = HTIcon_find(HT_IS_BLANK, NULL, NULL);
2.9 frystyk 277: if (icon) {
2.15 frystyk 278: char * alt = HTIcon_alternative(icon, NO);
279: HTMLPutImg(target, HTIcon_url(icon), alt, NULL);
280: HT_FREE(alt);
2.9 frystyk 281: PUTC(' ');
282: }
2.1 frystyk 283: }
284:
285: tp = dir->fnbuf;
286: LeftStr(&tp, "Name", dir->curfw);
287: LeftStr(&tp, " ", HT_DLEN_SPACE);
288: *tp = '\0';
289: PUTS(dir->fnbuf);
290:
291: tp = dir->lnbuf;
292: if (dir->show & HT_DS_DATE) {
2.2 frystyk 293: LeftStr(&tp, "Last Modified", HT_DLEN_DATE);
2.1 frystyk 294: LeftStr(&tp, " ", HT_DLEN_SPACE);
295: }
296: if (dir->show & HT_DS_SIZE) {
297: RightStr(&tp, "Size", HT_DLEN_SIZE);
298: LeftStr(&tp, " ", HT_DLEN_SPACE);
299: }
300: if (dir->show & HT_DS_DES) {
301: LeftStr(&tp, "Description", HT_DLEN_DATE);
302: LeftStr(&tp, " ", HT_DLEN_SPACE);
303: }
304: *tp = '\0';
305: PUTS(dir->lnbuf);
306: START(HTML_HR);
307: PUTC('\n');
308: return YES;
309: }
310: return NO;
311: }
312:
313: /* HTDir_setWidth
314: ** --------------
315: ** The module automatically ajusts the width of the directory listing as
316: ** a function of the file name. The width can flows dynamically between
317: ** an upper and a lower limit.
318: */
319: PUBLIC BOOL HTDir_setWidth (int minfile, int maxfile)
320: {
321: MinFileW = (minfile>=0) ? minfile : 0;
322: MaxFileW = (maxfile>minfile) ? maxfile : minfile+1;
323: return YES;
324: }
325:
326: /* HTDir_new
327: ** ---------
328: ** Creates a structured stream object and sets up the initial HTML stuff
329: ** Returns the dir object if OK, else NULL
330: */
331: PUBLIC HTDir * HTDir_new (HTRequest * request, HTDirShow show, HTDirKey key)
332: {
333: HTDir *dir;
334: char *title = NULL;
335: if (!request) return NULL;
336:
337: /* Create object */
2.10 frystyk 338: if ((dir = (HTDir *) HT_CALLOC(1, sizeof (HTDir))) == NULL ||
339: (dir->fnbuf = (char *) HT_MALLOC(MaxFileW+HT_DLEN_SPACE)) == NULL)
340: HT_OUTOFMEM("HTDir_new");
2.1 frystyk 341: dir->target = HTMLGenerator(request, NULL, WWW_HTML,
342: HTRequest_outputFormat(request),
343: HTRequest_outputStream(request));
2.16 frystyk 344: HTRequest_setOutputConnected(request, YES);
2.7 frystyk 345: HTAnchor_setFormat(HTRequest_anchor(request), WWW_HTML);
2.1 frystyk 346: dir->request = request;
347: dir->show = show;
348: dir->key = key;
349: if (key==HT_DK_NONE)
350: dir->curfw = MaxFileW;
351: else {
352: dir->curfw = MinFileW;
353: dir->array = HTArray_new(256);
354: }
2.16 frystyk 355:
356: /* We're all OK */
357: HTRequest_addError(request, ERR_INFO, NO, HTERR_OK, NULL, 0, "HTDir_new");
2.1 frystyk 358:
359: /* Find the length of the fields */
360: {
361: int len = HT_DLEN_SPACE+1;
362: if (show & HT_DS_SIZE) len += (HT_DLEN_SIZE+HT_DLEN_SPACE);
363: if (show & HT_DS_DATE) len += (HT_DLEN_DATE+HT_DLEN_SPACE);
364: if (show & HT_DS_DES) len += HT_DLEN_DES;
2.10 frystyk 365: if ((dir->lnbuf = (char *) HT_MALLOC(len)) == NULL)
366: HT_OUTOFMEM("HTDir_new");
2.1 frystyk 367: }
368:
369: /* Find the title and the base URL */
370: {
371: char *addr = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
372: char *path = HTParse(addr, "", PARSE_PATH+PARSE_PUNCTUATION);
373: char *ptr;
374: if ((ptr = strchr(path, ';')) || (ptr = strchr(path, '?')))
375: *ptr = '\0';
376: StrAllocCopy(title, path);
377: HTUnEscape(title); /* Title */
378: if((ptr=strrchr(path, '/')) && (ptr<path+strlen(path)-1 || ptr==path)){
379: StrAllocCopy(dir->base, ++ptr);
380: StrAllocCat(dir->base, "/");
381: }
2.22 ! frystyk 382: HTTRACE(PROT_TRACE, "HTDir_new... base is `%s\'\n" _ dir->base ? dir->base : "");
2.10 frystyk 383: HT_FREE(addr);
384: HT_FREE(path);
2.1 frystyk 385: }
386:
387: /* Start the HTML stuff */
388: {
389: HTStructured *target = dir->target;
390: START(HTML_HTML);
391: START(HTML_HEAD);
392: START(HTML_TITLE);
393: PUTS("Current index is ");
394: PUTS(title);
395: END(HTML_TITLE);
396: END(HTML_HEAD);
397: START(HTML_BODY);
398: START(HTML_H1);
399: PUTS("Index of ");
400: PUTS(title);
401: END(HTML_H1);
402: }
2.10 frystyk 403: HT_FREE(title);
2.1 frystyk 404: return dir;
405: }
406:
407: /* HTDir_addElement
408: ** ---------------
409: ** This function accepts a directory line. "data" and "size", and
410: ** "description" can all be NULL
411: ** Returns YES if OK, else NO
412: */
413: PUBLIC BOOL HTDir_addElement (HTDir *dir, char *name, char *date, char *size,
414: HTFileMode mode)
415: {
416: HTDirNode *node = HTDirNode_new();
417: if (!dir || !name) return NO;
2.17 frystyk 418: if ((node->fname = (char *) HT_MALLOC(strlen(name) + 2)) == NULL)
419: HT_OUTOFMEM("HTDir_addElement");
420: strcpy(node->fname, name); /* Mandatory */
2.1 frystyk 421: if (dir->show & HT_DS_DATE && date) StrAllocCopy(node->date, date);
422: if (dir->show & HT_DS_SIZE && size) StrAllocCopy(node->size, size);
423: if (dir->show & HT_DS_DES) {
424: #if 0
425:
426: /* FIND DESCRIPTION */
427:
428: #endif
429: }
2.17 frystyk 430:
431:
432: /* Set the mode of the file */
2.1 frystyk 433: node->mode = mode;
2.17 frystyk 434:
435: /* Should we display now or later? */
2.1 frystyk 436: if (dir->key == HT_DK_NONE) {
437: if (!dir->size++) HTDir_headLine(dir);
438: HTDirNode_print(dir, node);
439: HTDirNode_free(node);
440: } else {
441: int slen = strlen(name);
442: if (slen > dir->curfw)
443: dir->curfw = slen < MaxFileW ? slen : MaxFileW;
444: HTArray_addObject(dir->array, (void *) node);
445: }
446: return YES;
447: }
448:
2.13 frystyk 449: PRIVATE int DirSort (const void *a, const void *b)
2.1 frystyk 450: {
2.8 frystyk 451: #if 0
2.1 frystyk 452: HTDirNode *aa = *(HTDirNode **) a;
453: HTDirNode *bb = *(HTDirNode **) b;
454: return strcmp(aa->fname, bb->fname);
2.8 frystyk 455: #else
2.15 frystyk 456: return strcmp((*((HTDirNode **) a))->fname,
457: (*((HTDirNode **) b))->fname);
2.1 frystyk 458: #endif
459: }
460:
2.13 frystyk 461: PRIVATE int DirCaseSort (const void *a, const void *b)
2.1 frystyk 462: {
2.8 frystyk 463: #if 0
2.1 frystyk 464: HTDirNode *aa = *(HTDirNode **) a;
465: HTDirNode *bb = *(HTDirNode **) b;
466: return strcasecomp(aa->fname, bb->fname);
2.8 frystyk 467: #else
2.15 frystyk 468: return strcasecomp((*((HTDirNode **) a))->fname,
469: (*((HTDirNode **) b))->fname);
2.1 frystyk 470: #endif
471: }
472:
473: /* HTDir_free
474: ** ----------
475: ** If we are sorting then do the sorting and put out the list,
476: ** else just append the end of the list.
477: */
478: PUBLIC BOOL HTDir_free (HTDir * dir)
479: {
480: if (!dir) return NO;
481: if (dir->key != HT_DK_NONE) {
482: HTArray *array = dir->array;
483: void **data;
484: HTDirNode *node;
485: HTDir_headLine(dir);
486: HTArray_sort(array, (dir->key==HT_DK_CINS ? DirCaseSort : DirSort));
487: node = (HTDirNode *) HTArray_firstObject(array, data);
488: while (node) {
489: HTDirNode_print(dir, node);
490: HTDirNode_free(node);
491: node = (HTDirNode *) HTArray_nextObject(array, data);
492: }
493: dir->size = HTArray_size(array);
494: HTArray_delete(array);
495: }
496:
497: /* Put out the end of the HTML stuff */
498: {
499: HTStructured *target = dir->target;
500: START(HTML_HR);
501: if (!dir->size)
502: PUTS("Empty directory");
503: else if (dir->size == 1)
504: PUTS("1 File");
505: else {
506: char buffer[20];
507: sprintf(buffer, "%u files", dir->size);
508: PUTS(buffer);
509: }
510: END(HTML_PRE);
511: END(HTML_BODY);
512: END(HTML_HTML);
513: FREE_TARGET;
514: }
515:
2.10 frystyk 516: HT_FREE(dir->fnbuf);
517: HT_FREE(dir->lnbuf);
518: HT_FREE(dir->base);
519: HT_FREE(dir);
2.1 frystyk 520: return YES;
521: }
Webmaster