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