File: HelpNode.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (376 lines) | stat: -rw-r--r-- 9,693 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* 
 * Motif Tools Library, Version 3.1
 * $Id$
 * 
 * Written by David Flanagan.
 * Copyright (c) 1992-2001 by David Flanagan.
 * All Rights Reserved.  See the file COPYRIGHT for details.
 * This is open source software.  See the file LICENSE for details.
 * There is no warranty for this software.  See NO_WARRANTY for details.
 *
 * $Log$
 * Revision 1.1.1.1  2001/07/18 11:06:02  root
 * Initial checkin.
 *
 * Revision 1.2  2001/06/12 16:25:28  andre
 * *** empty log message ***
 *
 *
 */

#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#ifdef VMS
#include <perror.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>    
#include <X11/Xos.h>
#include <Xmt/Xmt.h>
#include <Xmt/Help.h>
#include <Xmt/Hash.h>
#include <Xmt/AppResP.h>

#if NeedFunctionPrototypes
void XmtHelpDefineNode(Widget w, StringConst name, XmtHelpNode *node)
#else
void XmtHelpDefineNode(w, name, node)
Widget w;
StringConst name;
XmtHelpNode *node;
#endif
{
    XmtAppResources *app = XmtGetApplicationResources(w);

    if (app->help_node_table == NULL)
	app->help_node_table = XmtHashTableCreate(5);
	
    XmtHashTableStore(app->help_node_table,
		      (XtPointer)XrmStringToQuark(name),
		      (XtPointer)node);
}
    

/*
 * Parse a help file, defining any nodes we find
 * The node syntax, sort of.  Keywords begin with a
 * '.' as the first character on a line.
 * 
 * help-file:: {{include} node}
 * include:: <.Include line>
 * node:: <.Node line> {attributes} body
 * attributes: .Title | .Keywords | .Crossrefs | .Subsections | .Format
 * body:: .Filename | .Body lines .End
 *
 *   .Include <filename>  [parse some other file in the same way]
 *   .Node <nodename>
 *   .Title <node-title>
 *   .Keywords <list-of-keywords>
 *   .Crossrefs <list-of-crossrefs>
 *   .Subsections <list-of-subnodes>
 *   .Format <format> [Text and XmString are currently supported formats]
 *   .Filename <filename> [optional straight text file, instead of .Body/.End]
 *   .Body
 *   <any number of lines of text>
 *   .End
 *
 * A help node begins with .Node, has any attributes in any order, and
 * ends with either a .File line, or a .Body line followed by the body
 * and a .End line.
 */

#define LINELEN 512

#if NeedFunctionPrototypes
static void ParseStringList(String line, String** list_return,
			    short *len_return)
#else
static void ParseStringList(line, list_return, len_return)
String line;
String **list_return;
short int *len_return;
#endif
{
    String *list = NULL;
    short num = 0;
    short max = 0;
    register char *p, *q;
    int len;
    String word;

    p = line;
    while(*p) {
	/* skip leading whitespace */
	while(isspace(*p)) p++;
	if (!*p) break;

	/* we're at the begining of a word.  Go find comma or EOL or EOS */
	for(q = p; *q && (*q != '\n') && (*q != ','); q++);

	/* make more space in the list, if needed */
	if (num == max) {
	    max += 5;
	    list = (String *) XtRealloc((char *)list, max * sizeof(String));
	}

	/* copy the word from the line */
	len = q - p;
	word = strncpy(XtMalloc(len+1), p, len);
	word[len] = '\0';
	list[num++] = word;

	/* if we're at a comma or newline, skip over it */
	if ((*q == ',') || (*q == '\n')) q++;

	p = q;
    }

    /* free up any excess allocated memory */
    if (num < max) list = (String *) XtRealloc((char *)list,
					       num*sizeof(String));
    
    /* and return the list and list length */
    *list_return = list;
    *len_return = num;
}

#if NeedFunctionPrototypes
static void FreeStringList(String *list, short num)
#else
static void FreeStringList(list, num)
String *list;
short int num;
#endif
{
    int i;

    for(i=0; i < num; i++) XtFree(list[i]);
    XtFree((char *)list);
}

#if NeedFunctionPrototypes
static XmtHelpNode *ParseNode(FILE *f)
#else
static XmtHelpNode *ParseNode(f)
FILE *f;
#endif
{
    XmtHelpNode *node;
    char line[LINELEN];
    char buf[LINELEN];
    char *s;
    int len;
    
    /*
     * We've already read the .Node line, so read any number of
     * attributes, then look for a filename specified by .Filename,
     * or for the node body specified by a .Body/.End pair.  In this
     * latter case, just save the offset and the length of the text
     * to be read later when needed
     */

    /*
     * first, allocate and initialize the node
     */
    node = (XmtHelpNode *) XtCalloc(1, sizeof(XmtHelpNode));
    node->format = XmtHelpFormatString;

    /* parse any number of attribute lines */
    while(fgets(line, LINELEN-1, f)) {
	if (strncmp(line, ".Title", 6) == 0) {
	    s = &line[6];
	    while(isspace(*s)) s++;
	    len = strlen(s) - 1;  /* minus 1 to remove newline */
	    s[len] = '\0';
	    node->title = strcpy(XtMalloc(len+1), s);
	}
	else if (strncmp(line, ".Keywords", 9) == 0)
	    ParseStringList(&line[9], &node->keywords, &node->num_keywords);
	else if (strncmp(line, ".Crossrefs", 10) == 0)
	    ParseStringList(&line[10], &node->crossrefs, &node->num_crossrefs);
	else if (strncmp(line, ".Subsections", 12) == 0)
	    ParseStringList(&line[12], &node->subnodes, &node->num_subnodes);
	else if (strncmp(line, ".Format", 7) == 0) {
	    sscanf(&line[7], "%s", buf);
	    if (strncmp(buf, "XmString", 8) == 0)
		node->format = XmtHelpFormatXmString;
	}
	else
	   break;
    }

    /* Now look for a .Filename line or a .Body line */
    if (strncmp(line, ".Filename", 9) == 0) {
	sscanf(&line[9], "%s", buf);
	node->body = XtNewString(buf);
	node->is_filename = True;
	return node;
    }
    else if (strncmp(line, ".Body", 5) == 0) {
	node->file = f;
	node->offset = ftell(f);
	/* scan 'till .End */
	while(fgets(line, LINELEN-1, f)) {
	    if ((line[0] == '.') && (strncmp(line, ".End", 4) == 0)) break;
	}
	node->length = ftell(f) - strlen(line) - node->offset;
	return node;
    }
    else {  /* nothing found; an error */
	if (node->title) XtFree(node->title);
	if (node->keywords) FreeStringList(node->keywords, node->num_keywords);
	if (node->crossrefs) FreeStringList(node->crossrefs,
					    node->num_crossrefs);
	if (node->subnodes) FreeStringList(node->subnodes, node->num_subnodes);
	XtFree((char *)node);
	return NULL;
    }
}


#if NeedFunctionPrototypes
void XmtHelpParseFile(Widget w, StringConst filename)
#else
void XmtHelpParseFile(w, filename)
Widget w;
StringConst filename;
#endif
{
    XmtAppResources *app = XmtGetApplicationResources(w);
    String fullname;
    FILE *f;
    char line[LINELEN];
    char name[LINELEN];
    XmtHelpNode *node;


    fullname = XmtFindFile(w, "help", filename, ".txt",
			   NULL, app->help_file_path, XmtSearchEverywhere);

    if (!fullname || (f = fopen(fullname, "r")) == NULL) {
	XmtWarningMsg("XmtHelpParseFile", "notFound",
		      "can't find a readable help file named '%s'",
		      filename);
	XtFree(fullname);
	return;
    }

    XtFree(fullname);

    while(fgets(line, LINELEN-1, f)) {
	/*
	 * a help file is a sequence of include statements and node
	 * definitions.  Look for one or the other and discard anything else
	 */
	if (line[0] != '.') continue;
	if (strncmp(line, ".Include", 8) == 0) {
	    sscanf(&line[8], "%s", name);
	    XmtHelpParseFile(w, name);
	}
	else if (strncmp(line, ".Node", 5) == 0) {
	    /* a node definition */
	    sscanf(&line[5], "%s", name);
	    node = ParseNode(f);
	    if (node != NULL)
		XmtHelpDefineNode(w, name, node);
	    else { /* error */
		XmtWarningMsg("XmtHelpParseFile", "badNode",
			      "Node '%s':\n\tExpected .Filename or .Body following .Node",
			      name);
	    }
	}
    }
}

#if NeedFunctionPrototypes
XmtHelpNode *XmtHelpLookupNode(Widget w, StringConst name)
#else
XmtHelpNode *XmtHelpLookupNode(w, name)
Widget w;
StringConst name;
#endif
{
    XmtAppResources *app = XmtGetApplicationResources(w);
    XrmQuark q = XrmStringToQuark(name);
    XmtHelpNode *node;

    if (!app->help_file_loaded) {
	app->help_file_loaded = True;
	if (app->help_file) XmtHelpParseFile(w, app->help_file);
    }

    if (app->help_node_table == NULL) return NULL;
    
    if (XmtHashTableLookup(app->help_node_table,
			   (XtPointer)q, (XtPointer *)&node))
	return node;
    else
	return NULL;
}

#if NeedFunctionPrototypes
String XmtHelpNodeGetBody(XmtHelpNode *node)
#else
String XmtHelpNodeGetBody(node)
XmtHelpNode *node;
#endif
{
    if (node->body && !node->is_filename)
	return XtNewString(node->body);
    else if (node->body) { /* return the entire contents of the named file */
	FILE *f;
	struct stat stats;
	int size;
	String text;

	/* open the file */
	if ((f = fopen(node->body, "r")) == NULL) {
	    XmtWarningMsg("XmtHelpNodeGetBody", "fopen",
			  "Can't open file '%s': %s",
			  node->body, strerror(errno));
	    return NULL;
	}

	/* find out how long it is */
	if (fstat(fileno(f), &stats) == -1) {
	    XmtWarningMsg("XmtHelpNodeGetBody", "fstat",
			  "Can't stat file '%s': %s",
			  node->body, strerror(errno));
	    fclose(f);
	    return NULL;
	}

	size = (int) stats.st_size;
	text = (String) XtMalloc(size+1);
	if ((size = read(fileno(f), text, size)) == -1) {
	    /* I don't really think this will ever fail, so no warning msg */
	    XtFree(text);
	    text = NULL;
	}
	fclose(f);
	text[size] = '\0';
	return text;
    }
    else if (node->file) { /* return a portion of the already opened file */
	String text;
	int len;
	
	text = XtMalloc(node->length + 1);
	if (fseek(node->file, node->offset, 0) != 0) {
	    XmtWarningMsg("XmtHelpNodeGetBody", "fseek",
			  "fseek: %s", strerror(errno));
	    return NULL;
	}
	if ((len = fread(text, sizeof(char), node->length, node->file))==EOF) {
	    XmtWarningMsg("XmtHelpNodeGetBody", "fread",
			  "fread: %s", strerror(errno));
	    XtFree(text);
	    return NULL;
	}
	text[len] = '\0';
	return text;
    }
    else
	return NULL;
}