summaryrefslogtreecommitdiff
path: root/src/libpcp_pmda/src/help.c
blob: d9d17b8f47169a5c238f776c40b4581d961bada2 (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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (c) 1995-2003 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 */

/*
 * Get help text from files built using newhelp
 */
#include "pmapi.h"
#include "impl.h"
#include "pmda.h"
#include <sys/stat.h>

typedef struct {	/* beware: this data structure mirrored in chkhelp */
    pmID	pmid;
    __uint32_t	off_oneline;
    __uint32_t	off_text;
} help_idx_t;

typedef struct {	/* beware: this data structure mirrored in chkhelp */
    int		dir_fd;
    int		pag_fd;
    int		numidx;
    help_idx_t	*index;
    char	*text;
    int		textlen;
} help_t;

static help_t	*tab = NULL;
static int	numhelp = 0;

/*
 * open the help text files and return a handle on success
 */
int
pmdaOpenHelp(char *fname)
{
    char	pathname[MAXPATHLEN];
    int		sts, size;
    help_idx_t	hdr;
    help_t	*hp;
    struct stat	sbuf;

    for (sts = 0; sts < numhelp; sts++) {
	if (tab[sts].dir_fd == -1)
	    break;
    }
    if (sts == numhelp) {
	sts = numhelp++;
	tab = (help_t *)realloc(tab, numhelp * sizeof(tab[0]));
	if (tab == NULL) {
	    __pmNoMem("pmdaOpenHelp", numhelp * sizeof(tab[0]), PM_RECOV_ERR);
	    numhelp = 0;
	    return -oserror();
	}
    }
    hp = &tab[sts];
    memset(hp, 0, sizeof(*hp));
    hp->dir_fd = -1;
    hp->pag_fd = -1;

    snprintf(pathname, sizeof(pathname), "%s.dir", fname);
    hp->dir_fd = open(pathname, O_RDONLY);
    if (hp->dir_fd < 0) {
	sts = -oserror();
	goto failed;
    }

    if (read(hp->dir_fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
	sts = -EINVAL;
	goto failed;
    }

    if (hdr.pmid != 0x50635068 ||
	(hdr.off_oneline & 0xffff0000) != 0x31320000) {
	sts = -EINVAL;
	goto failed;
    }

    hp->numidx = hdr.off_text;
    size = (hp->numidx + 1) * sizeof(help_idx_t);

    hp->index = (help_idx_t *)__pmMemoryMap(hp->dir_fd, size, 0);
    if (hp->index == NULL) {
	sts = -oserror();
	goto failed;
    }

    snprintf(pathname, sizeof(pathname), "%s.pag", fname);
    hp->pag_fd = open(pathname, O_RDONLY);
    if (hp->pag_fd < 0) {
	sts = -oserror();
	goto failed;
    }
    if (fstat(hp->pag_fd, &sbuf) < 0) {
	sts = -oserror();
	goto failed;
    }
    hp->textlen = (int)sbuf.st_size;
    hp->text = (char *)__pmMemoryMap(hp->pag_fd, hp->textlen, 0);
    if (hp->text == NULL) {
	sts = -oserror();
	goto failed;
    }
    return numhelp - 1;

failed:
    pmdaCloseHelp(numhelp-1);
    return sts;
}

/*
 * retrieve pmID help text, ...
 *
 */
char *
pmdaGetHelp(int handle, pmID pmid, int type)
{
    int		i;
    help_t	*hp;

    if (handle < 0 || handle >= numhelp)
	return NULL;
    hp = &tab[handle];

    /* search forwards -- could use binary chop */
    for (i = 1; i <= hp->numidx; i++) {
	if (hp->index[i].pmid == pmid) {
	    if (type & PM_TEXT_ONELINE)
		return &hp->text[hp->index[i].off_oneline];
	    else
		return &hp->text[hp->index[i].off_text];
	}
    }

    return NULL;
}

/*
 * retrieve pmInDom help text, ...
 *
 */
char *
pmdaGetInDomHelp(int handle, pmInDom indom, int type)
{
    int			i;
    help_t		*hp;
    pmID		pmid;
    __pmID_int		*pip = (__pmID_int *)&pmid;

    if (handle < 0 || handle >= numhelp)
	return NULL;
    hp = &tab[handle];

    *pip = *((__pmID_int *)&indom);
    /*
     * set a bit here to disambiguate pmInDom from pmID
     * -- this "hack" is shared between here and newhelp/newhelp.c
     */
    pip->flag = 1;

    /* search backwards ... pmInDom entries are at the end */
    for (i = hp->numidx; i >= 1; i--) {
	if (hp->index[i].pmid == pmid) {
	    if (type & PM_TEXT_ONELINE)
		return &hp->text[hp->index[i].off_oneline];
	    else
		return &hp->text[hp->index[i].off_text];
	}
    }

    return NULL;
}

/*
 * this is only here for chkhelp(1) ... export the control data strcuture
 */
void *
__pmdaHelpTab(void)
{
    return (void *)tab;
}

void
pmdaCloseHelp(int handle)
{
    help_t		*hp;

    if (handle < 0 || handle >= numhelp)
	return;
    hp = &tab[handle];

    if (hp->dir_fd != -1)
	close(hp->dir_fd);
    if (hp->pag_fd != -1)
	close(hp->pag_fd);
    if (hp->index != NULL)
	__pmMemoryUnmap((void *)hp->index, (hp->numidx+1) * sizeof(help_idx_t));
    if (hp->text != NULL)
	__pmMemoryUnmap(hp->text, hp->textlen);

    hp->textlen = 0;
    hp->dir_fd = -1;
    hp->pag_fd = -1;
    hp->numidx = 0;
    hp->index = NULL;
    hp->text = NULL;
}