[BACK]Return to termdoc.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gnuplot / docs

Annotation of OpenXM_contrib/gnuplot/docs/termdoc.c, Revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>