home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
games
/
volume13
/
okbridge
/
part06
/
help.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-01-12
|
6KB
|
266 lines
/* help.c -- help functions for the bridge program.
!
! Copyright (C) 1990,1991 by Matthew Clegg
!
! This program may be copied and distributed freely. Please do not
! charge money for this program or for any program derived from it.
! If you modify this program, then include a notice stating plainly
! that your program is derived from the okbridge program and is not
! the same as the official okbridge program.
!
! I welcome any suggestions for improvement to okbridge, and
! I would be especially happy to receive improved source code.
! If you have comments or suggestions, or if you would like to
! join the okbridge mailing list, then write to
!
! mclegg@cs.ucsd.edu
!
*
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#include "globals.h"
#include "ps.h"
#include "input.h"
#include "terminal.h"
/* char *help_file_name = "okbridge.help"; */
#include "helpfile.h"
extern int errno;
extern char *sys_errlist[];
extern char *strdup();
extern char *getenv ();
typedef struct help_entry_struct {
char *topic;
char *description;
long int file_offset;
struct help_entry_struct *next;
} *help_entry;
static help_entry main_topic = NULL;
FILE *help_file = NULL;
static display_topics (message)
char *message;
/* Displays the list of topics, with the given message as the top line.
. Does not display the main topic.
*/
{
int i;
help_entry e;
char msg_buf[80];
clear_screen ();
print (1,1,message);
if (main_topic == NULL) {
print (3,1,"The help system is empty.");
} else {
i = 3;
for (e = main_topic->next; e != NULL; e = e->next) {
if (strcasecmp(e->topic, "slam")) {
sprintf (msg_buf, "%-10s -- %s",e->topic,
e->description);
print (i++, 1, msg_buf);
};
};
};
input_acknowledgment (24);
};
static int read_help_line (buf, buflen)
char *buf;
/* Reads a line from the help file. Returns the number of characters
read of -1 if EOF is encountered. Skips lines beginning with a '#'.
*/
{
int i, ch;
do {
ch = getc(help_file);
i = 0;
while ((ch != '\n') && (ch != EOF)) {
if (i < buflen-1) buf[i++] = ch;
ch = getc(help_file);
};
buf[i] = '\0';
if (ch == EOF) return (-1);
while ((i > 0) && isspace(buf[i-1])) buf[--i] = '\0';
} while (buf[0] == '#');
return (i);
};
static display_help_entry (e)
help_entry e;
/* Displays the help_entry e. */
{
char line[81];
int lines_on_page, log;
lines_on_page = 0;
/*
sprintf (line, "%s -- %s", e->topic, e->description);
print(++lines_on_page, 1, line);
lines_on_page += 1;
*/
fseek (help_file, e->file_offset, SEEK_SET);
log = read_help_line (line, 81);
while ((log >= 0) && strcmp(line, "--")) {
if ((lines_on_page > 22) || (line[0] == '^')) {
input_acknowledgment (24);
clear_screen ();
lines_on_page = 0;
if (line[0] == '^') line[0] = ' ';
};
print (++lines_on_page, 1, line);
log = read_help_line (line, 81);
};
input_acknowledgment (24);
};
static FILE *open_helpfile ()
/* Tries to open the helpfile with given name from the help_directory.
* If an error, prints an error message and returns NULL. Otherwise,
* returns a pointer to the opened file.
*/
{
char filename_buf [80], msg_buf[80], *envhelpdir;
FILE *fp;
if ((envhelpdir = getenv("OKBRIDGE_HELPFILE")) != NULL)
sprintf (filename_buf, "%s", envhelpdir);
else
sprintf (filename_buf, "%s", help_file_name);
fp = fopen (filename_buf, "r");
if (fp == NULL)
fp = fopen ("okbridge.help", "r");
if (fp == NULL) {
sprintf (msg_buf, "Error opening helpfile %s", filename_buf);
print (3, 1, msg_buf);
sprintf (msg_buf, "System reports error: %s",
sys_errlist[errno]);
print (4, 1, msg_buf);
input_acknowledgment (24);
};
return (fp);
};
static help_entry find_help_topic (topic)
char *topic;
/* Looks for the help entry with the associated topic. If it is found,
* returns a pointer to the corresponding record. Otherwise, returns NULL.
*/
{
help_entry e;
e = main_topic;
while (e != NULL) {
if (!strcasecmp(e->topic, topic))
return (e);
e = e->next;
};
return (e);
};
static help_entry read_new_help_entry ()
/* Reads a help entry from the help_file. Allocates a help_entry record
. and records the pertinent information in that record. Returns the
. allocated record or NULL if the end of file is reached.
*/
{
char line_buf[81], keyword_buf[80];
help_entry e;
char *curpos, *keypos, *descpos, ch;
int log;
log = read_help_line (line_buf, 81);
if (log < 0)
return (NULL);
for (keypos = line_buf; isspace(*keypos); keypos++);
for (curpos = keypos;(*curpos != '\0') && !isspace(*curpos);curpos++);
for (descpos = curpos; isspace(*descpos); descpos++);
e = (help_entry) malloc(sizeof(struct help_entry_struct));
*curpos = '\0';
e->topic = strdup (keypos);
e->description = strdup (descpos);
e->file_offset = ftell (help_file);
e->next = NULL;
do
log = read_help_line (line_buf, 81);
while
((log >= 0) && strcmp(line_buf, "--"));
return (e);
};
initialize_help_system ()
/* Called once at the beginning of the program to read the file of help
* topics.
*/
{
help_entry e;
help_file = open_helpfile ();
if (help_file == NULL) return;
e = main_topic = read_new_help_entry ();
while (e != NULL) {
e->next = read_new_help_entry ();
e = e->next;
};
};
display_help (topic)
char *topic;
/* Displays help on the given topic. This consists of looking up the
* help file associated to this topic and displaying the contents of this
* file on the screen. If the topic string is empty, then displays first
* the contents of the main topic file, and then displays a list of the
* topics. If there is no help on the given topic, then displays a list
* of topics.
*/
{
help_entry he;
char line_buf[81];
if (main_topic == NULL)
return;
Suspend_Comment_Display ();
clear_screen ();
if (strlen(topic) == 0) {
display_help_entry (main_topic);
display_topics ("Here is a list of available topics: ");
} else if ((he = find_help_topic(topic)) != NULL)
display_help_entry (he);
else {
sprintf (line_buf, "%s %s",
"There is no help for this topic.",
"The available topics are");
display_topics (line_buf);
};
Continue_Comment_Display ();
};