summaryrefslogtreecommitdiff
path: root/ipl/progs/ihelp.icn
blob: 71a905c7599a96c3575e65c3582185a820b099a9 (plain)
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
############################################################################
#
#	File:     ihelp.icn
#
#	Subject:  Program to give on-line help for Icon
#
#	Author:   Robert J. Alexander
#
#	Date:     December 5, 1989
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  ihelp -- Program to display "help" information
#
#       ihelp [-f helpfile] [item] [keyword ...]
#
#  The optional item name specifies the section of the help file which
#  is to be displayed.  If no item name is specified a default section
#  will be displayed, which usually lists the help items that are
#  available.  An initial substring of the item name that differentiates
#  it from other items is sufficient.
#
#  If keyword(s) are specified, then only lines that contain all of the
#  keywords, in any order, are displayed.  The keywords do not have to
#  correspond to whole words in the help text; only to text fragments.
#
#  All item name and keyword matches are case independent.
#
#  The help file name is taken from environment variable "HELPFILE".  If
#  HELPFILE is not in the environment, file "help" in the current
#  directory is used.  A help file name specified in the -f option
#  overrides.
#
#  The help files are formatted as follows:
#
#       default text lines
#       -
#       one
#       item "one" text lines
#       -
#       two
#       item "two" text lines
#       ...
#
#  Sections are separated by lines containing a single "-".  Item names
#  are the first line following a separator line.
#
############################################################################
#
#  Links: options
#
############################################################################


link options


procedure main(arg)
   local defaultHelpFile, opts, fn, f, item, line, keywords, i, lline, k

   #
   #  Initialize.
   #
   defaultHelpFile := "ihelp.dat"
   opts := options(arg,"f:")
   fn := \opts["f"] | "" ~== getenv("HELPFILE") | defaultHelpFile
   f := open(fn) | stop("Can't open help file \"",fn,"\"")
   #
   #  Look for the specified section, if one was.
   #
   if item := map(arg[1]) then {
      line := ""
      until item == map(line[1:*item + 1]) do {
	 while read(f) ~== "-"
	 line := read(f) | stop("No help for ",item)
	 }
      }
   #
   #  Output the section lines that contain the keywords.
   #
   write(line)
   keywords := arg[2:0] | []
   every i := 1 to *keywords do keywords[i] := map(keywords[i])
   while "-" ~== (line := read(f)) do {
      lline := map(line)
      if not (every k := !keywords do if not find(k,lline) then break) then
	    write(line)
      }
end