2 static char *RCSid() { return RCSid("$Id: termdoc.c,v 1.15 2004/07/01 17:10:03 broeker Exp $"); }
5 /* GNUPLOT - termdoc.c */
8 * Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
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.
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,
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
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.
33 * This software is provided "as is" without express or implied warranty
34 * to the extent permitted by applicable law.
40 * David Denholm - 1996
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.
48 * Can be compiled as a standalone program to generate the raw
49 * .doc test, when compiled with -DTEST_TERMDOC
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.
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
68 #define DOCS_TERMDOC_MAIN
74 /* because we hide details of including terminal drivers,
75 * we provide line numbers and file names
79 char termdoc_filename[80];
81 TBOOLEAN single_top_level;
84 get_line( char *buffer, int max, FILE *fp)
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;
93 /* we are reading from file */
96 read_another_line: /* come here if a comment is read */
98 if (!fgets(buffer, max, fp))
99 return NULL; /* EOF */
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;
108 goto read_another_line; /* skip comments */
112 if (single_top_level == TRUE) {
113 if (buffer[0] == '1') {
120 if (buffer[0] != '<')
121 return buffer; /* the simple case */
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';
129 /* we're sending lines from terminal help */
131 /* process and skip comments. Note that the last line
132 * will invariably be a comment !
135 while (termtext[line][0] == 'C') {
136 if (termtext[line][1] == '#') {
137 safe_strncpy(termdoc_filename, termtext[line] + 2, sizeof(termdoc_filename));
142 if (!termtext[++line]) {
143 /* end of text : need to return a line from
144 * the file. Recursive call is best way out
146 termdoc_lineno = save_lineno;
147 /* we've done the last line, so get next line from file */
149 return get_line(buffer, max, fp);
154 /* termtext[line] is the next line of text.
155 * more efficient to return pointer, but we need to modify it
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';
165 strcat(buffer, "\n");
167 if (isdigit((int)buffer[0]))
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...
175 termdoc_lineno = save_lineno;
176 /* we've done the last line, so get next line from file */
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
189 safe_strncpy( char *d, const char *s, size_t n)
193 ret = strncpy(d, s, n);
207 while (get_line(line, sizeof(line), stdin))
208 printf("%s:%d:%s", termdoc_filename, termdoc_lineno, line);