File: termdoc.c

package info (click to toggle)
gnuplot 4.4.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,168 kB
  • ctags: 8,165
  • sloc: ansic: 79,287; lisp: 5,017; cpp: 3,521; sh: 1,059; makefile: 905; objc: 647; asm: 539; csh: 297; perl: 253; awk: 235; pascal: 194; tcl: 88
file content (211 lines) | stat: -rw-r--r-- 6,106 bytes parent folder | download | duplicates (10)
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
#ifndef lint
static char *RCSid() { return RCSid("$Id: termdoc.c,v 1.15 2004/07/01 17:10:03 broeker Exp $"); }
#endif

/* GNUPLOT - termdoc.c */

/*[
 * Copyright 1986 - 1993, 1998, 2004   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 complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/

/*
 * AUTHORS
 *
 *  David Denholm - 1996
 */

/* this file 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_TERMDOC
 *
 * 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 TBOOLEAN single_top_level == TRUE,
 * 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
 */

#include "syscfg.h"

#define DOCS_TERMDOC_MAIN

#include "stdfn.h"
#include "gp_types.h"
#include "doc2x.h"

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

int termdoc_lineno;
char termdoc_filename[80];

TBOOLEAN single_top_level;

char *
get_line( 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 */
    static int seen_a_one = 0;

    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... */
		    safe_strncpy(termdoc_filename, buffer + 2, sizeof(termdoc_filename));
		    termdoc_filename[strlen(termdoc_filename) - 1] = NUL;
		    termdoc_lineno = 0;
		}
		goto read_another_line;		/* skip comments */
	    }
	}

	if (single_top_level == TRUE) {
	    if (buffer[0] == '1') {
		if (seen_a_one) {
		    buffer[0] = '2';
		}
		seen_a_one = 1;
	    }
	}
	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] == '#') {
	    safe_strncpy(termdoc_filename, termtext[line] + 2, sizeof(termdoc_filename));
	    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;
	    /* we've done the last line, so get next line from file */
	    line = -1;
	    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;
    safe_strncpy(buffer, termtext[line], max);
    /* dodgy; can overrun buffer; lh */
    /* strncat(buffer, "\n", max); */
    if (strlen(buffer) == (max - 1))
        buffer[max-2] = '\n';
    else
        strcat(buffer, "\n");

    if (isdigit((int)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;
	/* we've done the last line, so get next line from file */
	line = -1;
    }
    return buffer;
}


/* Safe, '\0'-terminated version of strncpy()
 * safe_strncpy(dest, src, n), where n = sizeof(dest)
 * This is basically the old fit.c(copy_max) function
 */

char *
safe_strncpy( char *d, const char *s, size_t n)
{
    char *ret;

    ret = strncpy(d, s, n);
    if (strlen(s) >= n)
        d[n-1] = NUL;

    return ret;
}


#ifdef TEST_TERMDOC
int
main()
{
    char line[256];

    while (get_line(line, sizeof(line), stdin))
	printf("%s:%d:%s", termdoc_filename, termdoc_lineno, line);
    return 0;
}
#endif