aboutsummaryrefslogtreecommitdiff
path: root/pkg/system/help/hbgetblk.x
blob: a39db0342497f4d5c1392ed32d0225fb51e7cfd2 (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<ctype.h>
include	"help.h"

.help hb_getnextblk
.nf ___________________________________________________________________________
HB_GETNEXTBLK -- Scan a file for the next help block, i.e., block of lines
delimited by ".help" and ".endhelp".  The syntax of a help block header is
as follows:

	.help[typestr] key1,key2,...,keyN section title

Whitespace is NOT permitted in the keyword list unless it is quoted.  Quotes
are optional for each of the three strings.  If the line ends with the
backslash character or a comma we assume that the header is continued on the
next line.  The typestr, denoting the type of help block, is optional.
If absent it defaults to "hlp".  System help blocks are of type "sys".

We keep track of the file line number when searching for a help block.  The
line number is updated in the HB structure.  For this number to be accurate,
the caller must see that it is initialized before we are called.
.endhelp ______________________________________________________________________

define	SZ_SBUF		(MAX_KEYS * SZ_KEY)
define	IS_KEYWCHAR	(IS_ALNUM($1) || $1 == '_' || $1 == '$')
define	exit_		90


int procedure hb_getnextblk (hb, ctrl)

pointer	hb
pointer	ctrl

bool	at_eof
char	key[SZ_KEY]
int	fd, n, ip, op, junk
pointer	sp, lbuf, sbuf, p
int	hb_getstr(), strmatch(), getline(), strlen()

begin
	call smark (sp)
	call salloc (lbuf, SZ_LINE, TY_CHAR)
	call salloc (sbuf, SZ_SBUF, TY_CHAR)

	# Search forward for the next help block.
	fd = H_IN(ctrl)
	at_eof = true

	while (getline (fd, Memc[lbuf]) != EOF) {
	    HB_LINENO(hb) = HB_LINENO(hb) + 1
	    if (strmatch (Memc[lbuf], "^.{help}") > 0) {
		at_eof = false
		break
	    }
	}

	if (at_eof) {
	    call sfree (sp)
	    return (EOF)
	}

	# Initialize everything in case of an early exit.
	HB_TYPE(hb)	= TY_UNKNOWN
	HB_NKEYS(hb)	= 0
	HB_TYPESTR(hb)	= EOS
	HB_SECTION(hb)	= EOS
	HB_TITLE(hb)	= EOS

	# Decode help block into the HB structure.  First step is to
	# get the type suffix string, if any.

	ip = strlen (".help") + 1
	p = lbuf + ip - 1
	if (IS_ALPHA (Memc[p])) {
	    for (n=0;  IS_ALNUM (Memc[p+n]);  n=n+1)
		;
	    call strcpy (Memc[p], HB_TYPESTR(hb), min(n,SZ_TYPESTR))
	    if (     strmatch (HB_TYPESTR(hb), "{sys}") > 0)
		HB_TYPE(hb) = TY_SYS
	    else if (strmatch (HB_TYPESTR(hb), "{hlp}") > 0)
		HB_TYPE(hb) = TY_HLP
	    else
		HB_TYPE(hb) = TY_UNKNOWN
	} else {
	    HB_TYPE(hb) = TY_HLP
	    n = 0
	}
	ip = ip + n

	# Now get the keyword string, and break the keywords out into
	# the keyword list.

	if (hb_getstr (fd, Memc[lbuf], ip, Memc[sbuf], SZ_SBUF,
	HB_LINENO(hb)) == 0)
	    goto exit_

	p = sbuf
	while (IS_WHITE (Memc[p]) || Memc[p] == ',')
	    p = p + 1
	for (n=1;  n <= MAX_KEYS && Memc[p] != EOS;  n=n+1) {
	    for (op=1;  IS_KEYWCHAR (Memc[p]);  op=op+1) {
		key[op] = Memc[p]
		p = p + 1
	    }
	    key[op] = EOS
	    call strcpy (key, HB_KEY(hb,n), SZ_KEY)
	    while (IS_WHITE (Memc[p]) || Memc[p] == ',')
		p = p + 1
	}
	HB_NKEYS(hb) = n - 1
		
	# Fetch section label string and title string.

	if (hb_getstr (fd, Memc[lbuf], ip, HB_SECTION(hb), SZ_SECTION,
	HB_LINENO(hb)) == 0)
	    goto exit_
	junk = hb_getstr (fd, Memc[lbuf], ip, HB_TITLE(hb), SZ_TITLE,
	    HB_LINENO(hb))

exit_	call sfree (sp)
	return (HB_LINENO(hb))
end


# HB_GETSTR -- Fetch a string (optionally quoted) from the line buffer.
# Handle everything having to do with continuation.

int procedure hb_getstr (fd, lbuf, ip, outstr, maxch, lineno)

int	fd
char	lbuf[ARB]
int	ip
char	outstr[ARB]
int	maxch
int	lineno

char	ch
int	op, dstart
int	stridx(), getline()
string	delim " \t'\""

begin
	while (IS_WHITE (lbuf[ip]))
	    ip = ip + 1
	op = 1

	# If quoted string, only a quote can end the string.
	dstart = 1
	if (lbuf[ip] == '\'' || lbuf[ip] == '"') {
	    ip = ip + 1
	    dstart = 3
	}

	# Fetch the string.
	for (ch=lbuf[ip];  stridx (ch, delim[dstart]) == 0;  ch=lbuf[ip]) {
	    if (op >= maxch)
		break

	    switch (ch) {
	    case '\\', ',':
		if (lbuf[ip+1] == '\n' || lbuf[ip+1] == EOS) {
		    # Continue on next line.
		    if (getline (fd, lbuf) == EOF)
			lbuf[1] = EOS
		    else
			lineno = lineno + 1
		    ip = 1
		    if (ch == '\\')
			next
		} else
		    ip = ip + 1
		outstr[op] = ch
		op = op + 1

	    case '\n', EOS:
		if (delim[dstart] == '\'') {
		    call eprintf ("Missing right quote in helpfile, line %d\n")
			call pargi (lineno)
		}
		break

	    default:
		outstr[op] = ch
		op = op + 1
		ip = ip + 1
	    }
	}

	if (ch != EOS)
	    ip = ip + 1
	outstr[op] = EOS

	return (op - 1)
end