Mercurial > hg > octave-avbm
changeset 991:18be848f10a9
[project @ 1994-12-15 06:06:46 by jwe]
author | jwe |
---|---|
date | Thu, 15 Dec 1994 06:07:08 +0000 |
parents | d2dd114ba5dd |
children | 2015030c0621 |
files | src/help.cc src/lex.l src/parse.h src/parse.y src/pt-cmd.cc src/variables.cc src/variables.h |
diffstat | 7 files changed, 125 insertions(+), 46 deletions(-) [+] |
line wrap: on
line diff
--- a/src/help.cc +++ b/src/help.cc @@ -671,7 +671,7 @@ symbol_record *sym_rec = lookup_by_name (*argv, 0); - if (sym_rec) + if (sym_rec && sym_rec->is_defined ()) { char *h = sym_rec->help (); if (h && *h) @@ -682,6 +682,14 @@ } } + char *h = get_help_from_file (*argv); + if (h && *h) + { + output_buf << "\n" << h << "\n"; + delete [] h; + continue; + } + output_buf << "\nhelp: sorry, `" << *argv << "' is not documented\n"; }
--- a/src/lex.l +++ b/src/lex.l @@ -641,6 +641,10 @@ // input. if (interactive && ! (reading_fcn_file || get_input_from_eval_string)) yyrestart (stdin); + +// Delete the buffer for help text. + delete [] help_buf; + help_buf = 0; } // Replace backslash escapes in a string with the real values. @@ -985,7 +989,6 @@ defining_func = 1; promptflag--; beginning_of_function = 1; - help_buf[0] = '\0'; if (! (reading_fcn_file || reading_script_file)) input_line_number = 1; return FCN; @@ -1090,52 +1093,55 @@ static void grab_help_text (void) { - int max_len = HELP_BUF_LENGTH - 1; + if (! help_buf) + { + ostrstream buf; - int in_comment = 1; - int len = 0; - int c = 0; + int in_comment = 1; + int c = 0; - while ((c = yyinput ()) != EOF) - { - if (in_comment) + while ((c = yyinput ()) != EOF) { - help_buf[len++] = c; - if (c == '\n') - in_comment = 0; - } - else - { - switch (c) + if (in_comment) + { + buf << (char) c; + if (c == '\n') + in_comment = 0; + } + else { - case '%': - case '#': - in_comment = 1; - break; + switch (c) + { + case '%': + case '#': + in_comment = 1; + break; - case ' ': - case '\t': - break; + case ' ': + case '\t': + break; - default: - goto done; + default: + goto done; + } } } - if (len > max_len) + done: + + if (c) + yyunput (c, yytext); + + buf << ends; + + help_buf = buf.str (); + + if (! help_buf || ! *help_buf) { - warning ("grab_help_text: buffer overflow after caching %d chars", - max_len); - break; + delete [] help_buf; + help_buf = 0; } } - - done: - - if (c) - yyunput (c, yytext); - - help_buf[len] = '\0'; } // Return 1 if the given character matches any character in the given
--- a/src/parse.h +++ b/src/parse.h @@ -73,10 +73,8 @@ // The column of the current token. extern int current_input_column; -#define HELP_BUF_LENGTH 8192 - // Buffer for help text snagged from function files. -extern char help_buf [HELP_BUF_LENGTH]; +extern char *help_buf; // Nonzero means we're working on a plot command. extern int plotting;
--- a/src/parse.y +++ b/src/parse.y @@ -91,8 +91,7 @@ int current_input_column = 1; // Buffer for help text snagged from function files. -// Probably shouldn't be a fixed size... -char help_buf [HELP_BUF_LENGTH]; +char *help_buf = 0; // Nonzero means we're working on a plot command. int plotting = 0;
--- a/src/pt-cmd.cc +++ b/src/pt-cmd.cc @@ -281,7 +281,7 @@ for (int i = 0; i < steps; i++) { - tree_constant *rhs; + tree_constant *rhs = 0; if (nr == 1) {
--- a/src/variables.cc +++ b/src/variables.cc @@ -498,9 +498,34 @@ return 0; } -static void +static int +looks_like_octave_copyright (char *s) +{ + if (s && strncmp (s, " Copyright (C) ", 15) == 0) + { + s = strchr (s, '\n'); + if (s) + { + s++; + s = strchr (s, '\n'); + if (s) + { + s++; + if (strncmp (s, " This file is part of Octave.", 29) == 0) + return 1; + } + } + } + return 0; +} + +static char * gobble_leading_white_space (FILE *ffile) { + ostrstream buf; + + int first_comments_seen = 0; + int have_help_text = 0; int in_comment = 0; int c; while ((c = getc (ffile)) != EOF) @@ -508,6 +533,12 @@ current_input_column++; if (in_comment) { + if (! have_help_text) + { + first_comments_seen = 1; + buf << (char) c; + } + if (c == '\n') { input_line_number++; @@ -521,6 +552,8 @@ { case ' ': case '\t': + if (first_comments_seen) + have_help_text = 1; break; case '\n': @@ -536,10 +569,23 @@ default: current_input_column--; ungetc (c, ffile); - return; + goto done; } } } + + done: + + buf << ends; + char *help_txt = buf.str (); + + if (! help_txt || ! *help_txt || looks_like_octave_copyright (help_txt)) + { + delete help_txt; + help_txt = 0; + } + + return help_txt; } static int @@ -547,8 +593,6 @@ { int status = 0; - gobble_leading_white_space (ffile); - long pos = ftell (ffile); char buf [10]; @@ -596,6 +640,8 @@ // Check to see if this file defines a function or is just a list of // commands. + char *tmp_help_txt = gobble_leading_white_space (ffile); + if (is_function_file (ffile)) { unwind_protect_int (echo_input); @@ -618,6 +664,9 @@ reset_parser (); + delete [] help_buf; + help_buf = tmp_help_txt; + int status = yyparse (); if (status != 0) @@ -726,6 +775,23 @@ return sym_rec; } +char * +get_help_from_file (const char *f) +{ + char *path = fcn_file_in_path (f); + if (path && *path) + { + FILE *fptr = fopen (path, "r"); + if (fptr) + { + char *help_txt = gobble_leading_white_space (fptr); + fclose (fptr); + return help_txt; + } + } + return 0; +} + // Variable values. // Look for the given name in the global symbol table. If it refers
--- a/src/variables.h +++ b/src/variables.h @@ -68,6 +68,8 @@ extern symbol_record *lookup_by_name (const char *nm, int exec_script = 1); +extern char *get_help_from_file (const char *f); + extern char *builtin_string_variable (const char *); extern int builtin_real_scalar_variable (const char *, double&);