File: termdoc.c

package info (click to toggle)
gnuplot 3.5beta6.340-5
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 4,792 kB
  • ctags: 4,205
  • sloc: ansic: 41,878; asm: 539; makefile: 498; objc: 379; csh: 297; sh: 277; pascal: 194; perl: 138; lisp: 88
file content (223 lines) | stat: -rw-r--r-- 5,429 bytes parent folder | download
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
/*
 * $Id: termdoc.c,v 1.18 1997/07/22 23:24:28 drd Exp $
 *
 */

/* GNUPLOT - termdoc.c */
/*
 * Copyright (C) 1986 - 1993, 1997   Thomas Williams, Colin Kelley
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted, 
 * provided that the above copyright notice appear in all copies and 
 * that both that copyright notice and this permission notice appear 
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the modified code.  Modifications are to be distributed 
 * as patches to released version.
 *  
 * This software is provided "as is" without express or implied warranty.
 * 
 *
 * AUTHORS
 *
 *  David Denholm - 1996
 */
 
/* this file is included into all the doc2* convertors
 * it provides a replacement for fgets() which inserts all the
 * help from the terminal drivers line by line at the < in the
 * gnuplot.doc file. This way, doc2* dont need to know what's going
 * on, and think it's all coming from one place.
 *
 * Can be compiled as a standalone program to generate the raw
 * .doc test, when compiled with -DTEST
 *
 * Strips comment lines {so none of doc2* need to bother} 
 * but magic comments beginning  C#  are used as markers
 * for line number recording (as c compilers)
 * We set BEGIN_HELP macro to "C#<driver>" as a special marker.
 *
 * Hmm - this is turning more and more into a preprocessor !
 * gnuplot.doc now has multiple top-level entries, but
 * some help systems (eg VMS) cannot tolerate this.
 * As a complete bodge, conditional on SINGLE_TOP_LEVEL,
 * we accept only the first 1, and map any subsequent 1's to 2's
 * At present, this leaves a bogus, empty section called
 * commands, but that's a small price to pay to get it
 * working properly
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef TEST
#include <stdio.h>
#include <ctype.h>
#endif

/*#if (DJGPP==2)*/ /* HBB: this should be included for *all* compilers, IMHO */
#include <string.h>
/*#endif*/

/* may need to something more clever for 64k machines ? */

#define GOT_DRIVER_H   /* a complete lie, but they dont need it ! */
#define TERM_HELP

/* for error reporting, we arrange for each terminals help to start
 *  "C#driver"
 * and end
 *  "C#"
 * requires ansi preprocesser. It's not a big loss if it
 * doesn't work, since it is really for use by doc maintainer.
 */

#ifdef __STDC__
#define START_HELP(driver) "C#" #driver , 
#define END_HELP(driver)   ,"C#",
#else
#define START_HELP(driver) /*nowt*/
#define END_HELP(driver)   ,
#endif


char *termtext[] = {
#ifdef ALL_TERM
#include "allterm.h"
#else
#include "term.h"
#endif
NULL
};



/* because we hide details of including terminal drivers,
 * we provide line numbers and file names
 */

int termdoc_lineno;
char termdoc_filename[80];

char *get_line __PROTO((char *buffer, int max, FILE *fp));

char *get_line(buffer,max,fp)
char *buffer;
int max;
FILE *fp;
{
	static int line=-1;  /* not going yet */
	static int level=0; /* terminals are at level 1 - we add this */

	static int save_lineno; /* for saving lineno */

#ifdef SINGLE_TOP_LEVEL
	static int seen_a_one = 0;
#endif

	if (line == -1) {

		/* we are reading from file */

		{
			read_another_line: /* come here if a comment is read */

			if (!fgets(buffer, max, fp))
				return NULL; /* EOF */
			++termdoc_lineno;
			if (buffer[0] == 'C') {
				if (buffer[1] == '#') {
					/* should not happen in gnuplot.doc, but... */ 
					strcpy(termdoc_filename, buffer+2);
					termdoc_filename[strlen(termdoc_filename)-1] = 0;
					termdoc_lineno = 0;
				}
				goto read_another_line; /* skip comments */
			}
		}

#ifdef SINGLE_TOP_LEVEL
		if (buffer[0] == '1')
		{
			if (seen_a_one)
			{
				buffer[0] = '2';
			}
			seen_a_one = 1;
		}
#endif


		if (buffer[0] != '<')
			return buffer; /* the simple case */

		/* prepare to return text from the terminal drivers */
		save_lineno = termdoc_lineno;
		termdoc_lineno = -1; /* dont count the C# */
		level=buffer[1]-'1';
		line=0;
	}

	/* we're sending lines from terminal help */

	/* process and skip comments. Note that the last line
	 * will invariably be a comment !
	 */

	while(termtext[line][0]=='C')
	{
		if (termtext[line][1] == '#') {
			strcpy(termdoc_filename, termtext[line]+2);
			termdoc_lineno = 0;
		}
		++termdoc_lineno;

		if (!termtext[++line])
		{
			/* end of text : need to return a line from
			 * the file. Recursive call is best way out
			 */
			termdoc_lineno = save_lineno;
			line=(-1); /* we've done the last line, so get next line from file */
			return get_line(buffer, max, fp);
		}
	}


	/* termtext[line] is the next line of text.
	 * more efficient to return pointer, but we need to modify it
	 */

	++termdoc_lineno;
	strncpy(buffer, termtext[line], max);
	strncat(buffer, "\n", max);
	if (isdigit(buffer[0]))
		buffer[0] += level;

	if (!termtext[++line]) {
		/* end of terminal help : return to input file next time
		 * last (pseudo-)line in each terminal should be a comment,
		 * so we shouldn't get here, but...
		 */
		termdoc_lineno = save_lineno;
		line=(-1); /* we've done the last line, so get next line from file */
	}

	return buffer;
}


#ifdef TEST
int main()
{
	char line[256];
	while(get_line(line, 256, stdin))
		printf("%s:%d:%s", termdoc_filename, termdoc_lineno, line);
	return 0;
}
#endif

#define fgets(a,b,c) get_line(a,b,c)