1 /* GNUPLOT - termdoc.c */
2
3 /*[
4 * Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
5 *
6 * Permission to use, copy, and distribute this software and its
7 * documentation for any purpose with or without fee is hereby granted,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation.
11 *
12 * Permission to modify the software is granted, but not the right to
13 * distribute the complete modified source code. Modifications are to
14 * be distributed as patches to the released version. Permission to
15 * distribute binaries produced by compiling modified sources is granted,
16 * provided you
17 * 1. distribute the corresponding source modifications from the
18 * released version in the form of a patch file along with the binaries,
19 * 2. add special version identification to distinguish your version
20 * in addition to the base release version number,
21 * 3. provide your name and address as the primary contact for the
22 * support of your modified version, and
23 * 4. retain our contact information in regard to use of the base
24 * software.
25 * Permission to distribute the released version of the source code along
26 * with corresponding source modifications in the form of a patch file is
27 * granted with same provisions 2 through 4 for binary distributions.
28 *
29 * This software is provided "as is" without express or implied warranty
30 * to the extent permitted by applicable law.
31 ]*/
32
33 /*
34 * AUTHORS
35 *
36 * David Denholm - 1996
37 */
38
39 /* this file provides a replacement for fgets() which inserts all the
40 * help from the terminal drivers line by line at the < in the
41 * gnuplot.doc file. This way, doc2* dont need to know what's going
42 * on, and think it's all coming from one place.
43 *
44 * Can be compiled as a standalone program to generate the raw
45 * .doc test, when compiled with -DTEST_TERMDOC
46 *
47 * Strips comment lines {so none of doc2* need to bother}
48 * but magic comments beginning C# are used as markers
49 * for line number recording (as c compilers)
50 * We set BEGIN_HELP macro to "C#<driver>" as a special marker.
51 *
52 * Hmm - this is turning more and more into a preprocessor !
53 * gnuplot.doc now has multiple top-level entries, but
54 * some help systems (eg VMS) cannot tolerate this.
55 * As a complete bodge, conditional on TBOOLEAN single_top_level == TRUE,
56 * we accept only the first 1, and map any subsequent 1's to 2's
57 * At present, this leaves a bogus, empty section called
58 * commands, but that's a small price to pay to get it
59 * working properly
60 */
61
62 #include "syscfg.h"
63
64 #define DOCS_TERMDOC_MAIN
65
66 #include "stdfn.h"
67 #include "gp_types.h"
68 #include "doc2x.h"
69
70 /* because we hide details of including terminal drivers,
71 * we provide line numbers and file names
72 */
73
74 int termdoc_lineno;
75 char termdoc_filename[80];
76
77 TBOOLEAN single_top_level;
78
79 char *
get_line(char * buffer,int max,FILE * fp)80 get_line( char *buffer, int max, FILE *fp)
81 {
82 static int line = -1; /* not going yet */
83 static int level = 0; /* terminals are at level 1 - we add this */
84 static int save_lineno; /* for saving lineno */
85 static int seen_a_one = 0;
86
87 if (line == -1) {
88
89 /* we are reading from file */
90
91 {
92 read_another_line: /* come here if a comment is read */
93
94 if (!fgets(buffer, max, fp))
95 return NULL; /* EOF */
96 ++termdoc_lineno;
97 if (buffer[0] == 'C') {
98 if (buffer[1] == '#') {
99 /* should not happen in gnuplot.doc, but... */
100 safe_strncpy(termdoc_filename, buffer + 2, sizeof(termdoc_filename));
101 termdoc_filename[strlen(termdoc_filename) - 1] = NUL;
102 termdoc_lineno = 0;
103 }
104 goto read_another_line; /* skip comments */
105 }
106 }
107
108 if (single_top_level == TRUE) {
109 if (buffer[0] == '1') {
110 if (seen_a_one) {
111 buffer[0] = '2';
112 }
113 seen_a_one = 1;
114 }
115 }
116 if (buffer[0] != '<')
117 return buffer; /* the simple case */
118
119 /* prepare to return text from the terminal drivers */
120 save_lineno = termdoc_lineno;
121 termdoc_lineno = -1; /* dont count the C# */
122 level = buffer[1] - '1';
123 line = 0;
124 }
125 /* we're sending lines from terminal help */
126
127 /* process and skip comments. Note that the last line
128 * will invariably be a comment !
129 */
130
131 while (termtext[line][0] == 'C') {
132 if (termtext[line][1] == '#') {
133 safe_strncpy(termdoc_filename, termtext[line] + 2, sizeof(termdoc_filename));
134 termdoc_lineno = 0;
135 }
136 ++termdoc_lineno;
137
138 if (!termtext[++line]) {
139 /* end of text : need to return a line from
140 * the file. Recursive call is best way out
141 */
142 termdoc_lineno = save_lineno;
143 /* we've done the last line, so get next line from file */
144 line = -1;
145 return get_line(buffer, max, fp);
146 }
147 }
148
149
150 /* termtext[line] is the next line of text.
151 * more efficient to return pointer, but we need to modify it
152 */
153
154 ++termdoc_lineno;
155 safe_strncpy(buffer, termtext[line], max);
156 /* dodgy; can overrun buffer; lh */
157 /* strncat(buffer, "\n", max); */
158 if (strlen(buffer) == (max - 1))
159 buffer[max-2] = '\n';
160 else
161 strcat(buffer, "\n");
162
163 if (isdigit((int)buffer[0]))
164 buffer[0] += level;
165
166 if (!termtext[++line]) {
167 /* end of terminal help : return to input file next time
168 * last (pseudo-)line in each terminal should be a comment,
169 * so we shouldn't get here, but...
170 */
171 termdoc_lineno = save_lineno;
172 /* we've done the last line, so get next line from file */
173 line = -1;
174 }
175 return buffer;
176 }
177
178
179 /* Safe, '\0'-terminated version of strncpy()
180 * safe_strncpy(dest, src, n), where n = sizeof(dest)
181 * This is basically the old fit.c(copy_max) function
182 */
183
184 char *
safe_strncpy(char * d,const char * s,size_t n)185 safe_strncpy( char *d, const char *s, size_t n)
186 {
187 char *ret;
188
189 ret = strncpy(d, s, n);
190 if (strlen(s) >= n)
191 d[n-1] = NUL;
192
193 return ret;
194 }
195
196
197 #ifdef TEST_TERMDOC
198 int
main()199 main()
200 {
201 char line[256];
202
203 while (get_line(line, sizeof(line), stdin))
204 printf("%s:%d:%s", termdoc_filename, termdoc_lineno, line);
205 return 0;
206 }
207 #endif
208