The outage for Sunday 24th November has been cancelled.
Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
help.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file help.c
5 
6  \version V1.4
7  \date 07.07.14
8  \brief Provides a simple file-based command line help utility
9 
10  \copyright (c) UCL / Dr. Andrew C. R. Martin 1992-2014
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39  A pair of routines for handling a help facility. All screen/keyboard
40  I/O is via screen() and GetKybdString() routines.
41 
42 **************************************************************************
43 
44  Usage:
45  ======
46 
47 \code
48  blDoHelp(string,helpfilename)
49 \endcode
50 
51  This is the normal entry point and should
52  be supplied with the complete command
53  including the word `help'. If this is the
54  only word given, the routine will prompt
55  with Help> and give help on each word
56  typed until return is hit to exit help.
57  If help followed by a keyword is given,
58  help only on that topic will be supplied.
59 
60 \code
61  blHelp(string,helpfilename)
62 \endcode
63 
64  Generates help from helpfilename on the
65  topic named by string. If this is `help'
66  or `?', available topics will be listed.
67 
68 \code
69  blHelp(NULL,"CLOSE")
70 \endcode
71 
72  Used to close the help file
73 
74  Under Unix, the environment variable, HELPDIR should be set to
75  specify the directory in which help files are stored.
76  Under operating systems such as VAX/VMS and AmigaDOS which support
77  the assign command, the HELPDIR: assign should be set up for the help
78  directory.
79 
80 **************************************************************************
81 
82  Revision History:
83  =================
84 - V1.0 29.09.92 Original
85 - V1.1 12.08.93 Returns correctly if help file not found
86 - V1.2 04.01.94 Changed DoHelp() to fix problem with compilers which
87  don't let you write to strings defined in double
88  inverted commas and never assigned to a variable.
89  Added getenv call for Unix support
90 - V1.3 18.01.95 Help() changed to call OpenFile() rather than handling
91  alternative directory internally. Consequently assign
92  or envvar is called HELPDIR.
93 - V1.4 07.07.14 Use bl prefix for functions By: CTP
94 
95 *************************************************************************/
96 /* Doxygen
97  -------
98  #GROUP General Programming
99  #SUBGROUP User interaction
100  #FUNCTION blHelp()
101  Generates help from a help file on the topic named by string. If
102  this is `help' or `?', available topics will be listed.
103 
104  #FUNCTION blDoHelp()
105  Handles help facility. This is the normal entry point.
106 */
107 /************************************************************************/
108 /* Includes
109 */
110 #include <stdio.h>
111 #include <string.h>
112 #include <ctype.h>
113 #include <stdlib.h>
114 
115 #include "macros.h"
116 #include "WindIO.h"
117 #include "parse.h"
118 #include "help.h"
119 #include "general.h"
120 
121 /************************************************************************/
122 /* Defines and macros
123 */
124 #define BUFFLEN 240 /* Buffer for reading from file */
125 #define HELPENV "HELPDIR" /* The help directory (variable) for unix or
126  assign name for VMS/AmigaDOS */
127 
128 /************************************************************************/
129 /* Globals
130 */
131 
132 /************************************************************************/
133 /* Prototypes
134 */
135 
136 /************************************************************************/
137 /*>void blHelp(char *string, char *HelpFile)
138  -----------------------------------------
139 *//**
140 
141  \param[in] *string Topic on which to provide help. If "help" or
142  "?" then list all topics
143  \param[in] *HelpFile Name of help file, or "CLOSE" to close the
144  help file
145 
146  Generates help from a help file on the topic named by string. If
147  this is `help' or `?', available topics will be listed.
148 
149  Only one help file may be open at a time. Once a file is open, no check
150  is made on the HelpFile string to check that you still require the same
151  help file. You must first close the file before changing to a new file.
152 
153  Calling:
154  Help(NULL,"CLOSE")
155  will close the help file.
156 
157  The directory specified by the environment variable or assign
158  specified by the #define HELPENV (normally HELPDIR) will be searched
159  for the help file if not found in the local directory.
160 
161 - 25.09.92 Original
162 - 28.09.92 Added HELP
163 - 29.09.92 Changed to file-based version
164 - 07.10.92 Added paging support
165 - 12.08.93 Correctly returns if help file not found
166 - 05.01.94 Handles getenv() call for Unix
167 - 11.03.94 Resets FirstCall to TRUE when file is closed
168 - 18.01.95 Calls OpenFile() rather than handling alternative directory
169  internally. Consequently assign or envvar is called HELPDIR.
170 - 07.07.14 Use bl prefix for functions By: CTP
171 */
172 void blHelp(char *string,
173  char *HelpFile)
174 {
175  int nletters,
176  buffpos,
177  i,
178  Found;
179  static int FirstCall = TRUE;
180  char FileBuff[BUFFLEN],
181  buffer[80],
182  *ptr;
183  static FILE *fp = NULL;
184 
185  if(!strcmp(HelpFile,"CLOSE"))
186  {
187  if(fp)
188  fclose(fp);
189  FirstCall = TRUE;
190  return;
191  }
192 
193  /* If first call, open file */
194  if(FirstCall)
195  {
196  BOOL NoEnv;
197 
198  if((fp=blOpenFile(HelpFile, HELPENV, "r", &NoEnv))==NULL)
199  {
200  blScreen(" Error==> Unable to open help file.\n");
201  sprintf(FileBuff," The %s environment variable \
202 or assign has not been set.\n",HELPENV);
203  blScreen(FileBuff);
204  return;
205  }
206 
207  FirstCall = FALSE;
208  }
209 
210  /* Rewind the file */
211  rewind(fp);
212 
213  /* If asking from general help, display known commands */
214  if(blMatch(string,"HELP",&nletters) || string[0] == '?')
215  {
216  /* Search the file for keywords, echoing them to the screen */
217  buffpos = 0;
218 
219  while(fgets(FileBuff, BUFFLEN, fp))
220  {
221  TERMINATE(FileBuff);
222  if(FileBuff[0] == '#')
223  {
224  if(buffpos + strlen(FileBuff) > 58)
225  {
226  buffer[buffpos] = '\0';
227 
228  blScreen(" ");
229  blScreen(buffer);
230  blScreen("\n");
231  buffpos = 0;
232  }
233  for(i=1; i<strlen(FileBuff); i++)
234  {
235  buffer[buffpos++] = FileBuff[i];
236  }
237  buffer[buffpos++] = ' ';
238  }
239  }
240 
241  /* Print the last line */
242  if(buffpos != 0)
243  {
244  buffer[buffpos] = '\0';
245 
246  blScreen(" ");
247  blScreen(buffer);
248  blScreen("\n");
249  }
250  }
251  else /* Asking for help on a specific subject */
252  {
253  Found = FALSE;
254  blPagingOn();
255 
256  while(fgets(FileBuff, BUFFLEN, fp))
257  {
258  TERMINATE(FileBuff);
259  if(FileBuff[0] == '#')
260  {
261  ptr = FileBuff+1;
262  UPPER(ptr);
263  if(blMatch(string,ptr,&nletters))
264  {
265  Found = TRUE;
266  while(fgets(FileBuff, BUFFLEN, fp))
267  {
268  TERMINATE(FileBuff);
269  if(FileBuff[0] == '#') break;
270 
271  blScreen(FileBuff);
272  blScreen("\n");
273  }
274  }
275  }
276  }
277  if(!Found)
278  {
279  blScreen(" Sorry, no help on '");
280  blScreen(string);
281  blScreen("'\n");
282  }
283  blPagingOff();
284  }
285 }
286 
287 /************************************************************************/
288 /*>void blDoHelp(char *string, char *HelpFile)
289  -------------------------------------------
290 *//**
291 
292  \param[in] *string String on which to give help, must include
293  the word "help". If given on its own, sits
294  in a loop prompting for help.
295  \param[in] *HelpFile The help file to search
296 
297  Handles help facility.
298  This is the normal entry point and should be supplied with the
299  complete command including the word `help'.
300 
301  e.g. DoHelp("help","foo.hlp");
302  or DoHelp("help bar","foo.hlp");
303 
304  If help is the only word given, the routine will prompt with Help>
305  and give help on each word typed until return is hit to exit help. If
306  help followed by a keyword is given, help only on that topic will be
307  supplied.
308 
309 - 25.09.92 Original
310 - 28.09.92 Changed to call Help("Help")
311 - 02.10.92 Added GetKybdString() rather than gets()
312 - 04.01.94 Changed to fix problem with compilers which
313  don't let you write to strings defined in double
314  inverted commas and never assigned to a variable
315 - 07.07.14 Use bl prefix for functions By: CTP
316 */
317 void blDoHelp(char *string,
318  char *HelpFile)
319 {
320  int i;
321  char *str,
322  buffer[160];
323 
324  /* Trim any trailing spaces */
325  strcpy(buffer,string); /* 04.01.94: Put string in buffer */
326  for(i=strlen(buffer)-1;buffer[i]==' '||buffer[i]=='\t';i--);
327  buffer[++i]='\0';
328 
329  if((str=strchr(buffer,' '))!=NULL) /* See if a space is in the string*/
330  {
331  /* Yes, set pointer to the position after the space
332  (i.e. the keyword)
333  */
334  str++;
335  }
336  else
337  {
338  /* No keyword was specified, so give help on help */
339  blHelp("Help",HelpFile);
340  }
341 
342 
343  if(str) /* If specified, just give help on the keyword */
344  {
345  blHelp(str,HelpFile);
346  }
347  else /* Sit in a loop handling each keyword */
348  {
349  for(;;)
350  {
351  blPrompt("Help");
352  blGetKybdString(buffer, 160);
353 
354  TERMINATE(buffer);
355  if(buffer[0])
356  blHelp(buffer,HelpFile);
357  else
358  break;
359  }
360  }
361 }
void blScreen(char *string)
Definition: WindIO.c:155
short BOOL
Definition: SysDefs.h:64
#define NULL
Definition: array2.c:99
Header for window/normal interface routines.
Include file for the command parser.
void blGetKybdString(char *string, int maxlen)
Definition: WindIO.c:263
#define FALSE
Definition: macros.h:223
Useful macros.
void blDoHelp(char *string, char *HelpFile)
Definition: help.c:317
FILE * blOpenFile(char *filename, char *envvar, char *mode, BOOL *noenv)
Definition: OpenFile.c:146
#define TERMINATE(x)
Definition: macros.h:366
void blPagingOn(void)
Definition: WindIO.c:295
int blMatch(char *comstring, char *string2, int *nletters)
Definition: parse.c:304
void blHelp(char *string, char *HelpFile)
Definition: help.c:172
#define BUFFLEN
Definition: help.c:124
#define HELPENV
Definition: help.c:125
#define TRUE
Definition: macros.h:219
void blPrompt(char *string)
Definition: WindIO.c:217
Include file for help functions.
Header file for general purpose routines.
#define UPPER(x)
Definition: macros.h:390
void blPagingOff(void)
Definition: WindIO.c:311