2
/***************************************************************************
5
* Fri May 20 18:14:32 2005
8
****************************************************************************/
11
* This program is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2 of the License, or
14
* (at your option) any later version.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29
void new_ext_html(char *ext, char *gisbase, char **html, int major, int minor,
40
pos1 = find_pos("<b>Drivers sections:</b>", html, 0); /* first go to section on "Drivers" */
42
/* we have a new version of the HTML docs that does not have a "Drivers" section anymore */
43
/* let's check for the special GEM comment */
46
("<!-- GEM Extensions StartHTML. Do not delete or change this comment! -->",
49
/* sorry, I can't handle these HTML docs */
51
("Unknown format of index.html. Unable to register HTML man pages.\n");
55
pos2 = find_pos("<hr>", html, pos1); /* the horizontal ruler marks the end of the HTML text */
56
if (find_pos("<h3>Installed extensions:</h3>", html, pos1) == -1) {
57
/* Extensions section does not yet exist: create it now */
58
insert_str("<h3>Installed extensions:</h3>\n", pos2, html);
59
insert_str("<ul>\n", pos2 + 1, html);
60
insert_str("</ul>\n", pos2 + 2, html);
61
insert_str("<p>\n", pos2 + 3, html);
64
start = find_pos("<h3>Installed extensions:</h3>", html, pos1);
65
end = find_pos("</ul>", html, start);
66
insert_here = start + 2;
67
/* check if this entry already exists and if so: bail out or overwrite, if force mode */
68
sprintf(item, "\">%s", ext);
69
pos3 = find_pos(item, html, insert_here);
72
print_warning("list item '%s' exists in index.html.\n", ext);
73
if ((FORCE) && (UPGRADE)) {
75
"<li><a href=\"../extensions/%s/index.html\">%s (%i.%i.%i)</a>\n",
76
ext, ext, major, minor, revision);
77
strcpy(html[pos3], item);
82
/* now go through all links in the Extensions section and insert this one in the right
83
alphabetical position */
84
pos3 = find_pos("<li><a href=", html, start);
85
while ((pos3 != -1) && (pos3 < end)) {
86
/* extract name of extension at this position */
87
first_char = strrchr(html[pos3], '"');
88
last_char = strrchr(html[pos3], '<');
89
len = (last_char - first_char) / sizeof(char);
90
strncpy(item, first_char + 2 * sizeof(char), len);
91
item[len - 1] = '\0'; /* get rid of '<' */
92
if (strcmp(ext, item) < 0) {
94
break; /* found our position: let's quit this! */
96
/* look for next item */
98
pos3 = find_pos("<li><a href=", html, start);
101
/* now insert new entry for this extension */
103
"<li><a href=\"../extensions/%s/index.html\">%s (%i.%i.%i)</a>\n",
104
ext, ext, major, minor, revision);
105
insert_str(item, insert_here, html);
110
void delete_ext_html(char *ext, char *gisbase, char **html)
112
int pos1, pos2, pos3;
118
pos1 = find_pos("<b>Drivers sections:</b>", html, 0); /* first go to section on "Drivers" */
120
/* we have a new version of the HTML docs that does not have a "Drivers" section anymore */
121
/* let's check for the special GEM comment */
124
("<!-- GEM Extensions StartHTML. Do not delete or change this comment! -->",
127
/* sorry, I can't handle these HTML docs */
129
("Unknown format of index.html. Unable to de-register HTML man pages.\n");
134
pos2 = find_pos("<hr>", html, pos1); /* the horizontal ruler marks the end of the HTML text */
135
if (find_pos("<h3>Installed extensions:</h3>", html, pos1) == -1) {
136
/* Extensions section does not exist: bail out! */
137
print_warning("no extensions section found in index.html.\n");
141
start = find_pos("<h3>Installed extensions:</h3>", html, pos1);
142
end = find_pos("</ul>", html, start);
143
/* check if the entry exists and if so delete */
145
sprintf(item, "\">%s", ext);
146
pos3 = find_pos(item, html, start);
148
/* does not exist: */
149
print_warning("extension '%s' not listed in index.html.\n", ext);
153
/* delete item, if it was found in the extensions section */
155
delete_str(pos3, html);
157
end--; /* end of extensions section is no one up! */
159
/* if no more entries left in the extensions list: delete the entire section */
160
pos3 = find_pos("<ul>", html, start);
161
if ((pos3 != -1) && (end > pos3) && (end - pos3 < 2)) {
162
for (i = 0; i < 4; i++) {
163
delete_str(pos3 - 1, html);
169
void register_html(char *pkg_short_name, char *gisbase, int major, int minor,
179
/* check if index.html exists and is readable */
180
sprintf(file, "%s/docs/html/index.html", gisbase);
181
f_in = fopen(file, "r");
183
if (errno == ENOENT) {
184
/* file does not exist */
188
/* sth. strange happened */
190
print_error(ERR_REGISTER_HTML, "checking for file '%s': %s\n",
191
file, strerror(errno));
195
/* create a temporary index.html copy for write access */
196
/* TODO: Do not hardcode temp paths */
197
strcpy(TMP_HTML, "/tmp/grass.extensions.db.XXXXXX"); /* TMP_HTML is a global variable */
200
f_out = fopen(TMP_HTML, "w+");
202
print_error(ERR_REGISTER_HTML,
203
"could not create temp file '%s': %s\n \
204
Make sure that directory /tmp exists on your system and you have write permission.\n", TMP_HTML, strerror(errno));
207
atexit(&exit_db); /* now need to register an at exit func to remove tmpdb automatically! */
209
/* everything fine: create a shell command to install HTML stuff */
212
"cp -vf %s %s/docs/html/index.html ; chmod -v a+r %s/docs/html/index.html ;",
213
TMP_HTML, gisbase, gisbase);
217
"cp -f %s %s/docs/html/index.html &>%s ; chmod a+r %s/docs/html/index.html &>%s ;",
218
TMP_HTML, gisbase, TMP_NULL, gisbase, TMP_NULL);
220
strcpy(HTML_CMD, str);
222
/* count number of lines in index.html */
224
while (fgets(str, MAXSTR, f_in) != NULL) {
232
/* create an array large enough to hold all lines in index.html */
233
/* plus the entries that are to be added for the extension */
234
/* plus one NULL terminator */
235
/* and copy all lines from index.html into this */
236
line = (char **)calloc(n_lines + 10, sizeof(char *));
237
for (i = 0; i < (n_lines + 10); i++) {
241
while (fgets(str, MAXSTR, f_in) != NULL) {
242
line[i] = (char *)malloc((1 + strlen(str)) * sizeof(char));
243
strcpy(line[i], str);
247
/* create "Extensions" entry in html document if necessary and add a link to */
248
/* this extension's HTML man index */
249
new_ext_html(pkg_short_name, gisbase, line, major, minor, revision);
251
/* write output to tmpfile */
253
while (line[i] != NULL) {
254
fprintf(f_out, line[i]);
264
for (i = 0; i < (n_lines + 10); i++) {
271
void deregister_html(char *pkg_short_name, char *gisbase)
280
/* check if index.html exists and is readable */
281
sprintf(file, "%s/docs/html/index.html", gisbase);
282
f_in = fopen(file, "r");
284
if (errno == ENOENT) {
285
/* file does not exist */
289
/* sth. strange happened */
291
print_error(ERR_REGISTER_HTML, "checking for file '%s': %s\n",
292
file, strerror(errno));
296
/* create a temporary index.html copy for write access */
297
/* TODO: Do not hardcode temp paths */
298
strcpy(TMP_HTML, "/tmp/grass.extensions.db.XXXXXX"); /* TMP_HTML is a global variable */
301
f_out = fopen(TMP_HTML, "w+");
303
print_error(ERR_REGISTER_HTML,
304
"could not create temp file '%s': %s\n \
305
Make sure that directory /tmp exists on your system and you have write permission.\n", TMP_HTML, strerror(errno));
308
atexit(&exit_db); /* now need to register an at exit func to remove tmpdb automatically! */
310
/* everything fine: create a shell command to copy modified HTML stuff on uninstall */
313
"cp -vf %s %s/docs/html/index.html ; chmod -v a+r %s/docs/html/index.html ;",
314
TMP_HTML, gisbase, gisbase);
318
"cp -f %s %s/docs/html/index.html &>%s ; chmod a+r %s/docs/html/index.html &>%s ;",
319
TMP_HTML, gisbase, TMP_NULL, gisbase, TMP_NULL);
321
strcpy(HTML_CMD, str);
324
/* count number of lines in index.html */
326
while (fgets(str, MAXSTR, f_in) != NULL) {
334
/* create an array large enough to hold all lines in index.html */
335
/* plus one NULL terminator */
336
/* and copy all lines from index.html into this */
337
line = (char **)calloc(n_lines + 1, sizeof(char *));
338
for (i = 0; i < (n_lines + 1); i++) {
342
while (fgets(str, MAXSTR, f_in) != NULL) {
343
line[i] = (char *)malloc((1 + strlen(str)) * sizeof(char));
344
strcpy(line[i], str);
348
/* delete link to this extension's HTML manual from index.html */
349
delete_ext_html(pkg_short_name, gisbase, line);
351
/* write output to tmpfile */
353
while (line[i] != NULL) {
354
fprintf(f_out, line[i]);
364
for (i = 0; i < (n_lines + 1); i++) {
372
Returns number of restored entries
374
int restore_html(char *gisbase)
378
char ext_idx[MAXSTR];
382
int n_entries, n_lines, i;
383
FILE *f_in, *f_out, *f_ext;
389
int major, minor, revision;
392
/* check if index.html exists and is readable */
393
sprintf(idx, "%s/docs/html/index.html", gisbase);
394
f_in = fopen(idx, "r");
396
if (errno == ENOENT) {
397
/* file does not exist */
401
/* sth. strange happened */
403
print_error(ERR_REGISTER_HTML, "checking for file '%s': %s\n",
404
idx, strerror(errno));
408
/* create a temporary index.html copy for write access */
409
/* TODO: Do not hardcode temp paths */
410
strcpy(TMP_HTML, "/tmp/grass.extensions.db.XXXXXX"); /* TMP_HTML is a global variable */
413
f_out = fopen(TMP_HTML, "w+");
415
print_error(ERR_REGISTER_HTML,
416
"could not create temp file '%s': %s\n \
417
Make sure that directory /tmp exists on your system and you have write permission.\n", TMP_HTML, strerror(errno));
420
/* everything fine: create a shell command to install HTML stuff */
423
"cp -vf %s %s/docs/html/index.html ; chmod -v a+r %s/docs/html/index.html ;",
424
TMP_HTML, gisbase, gisbase);
428
"cp -f %s %s/docs/html/index.html &>%s ; chmod a+r %s/docs/html/index.html &>%s ;",
429
TMP_HTML, gisbase, TMP_NULL, gisbase, TMP_NULL);
431
strcpy(HTML_CMD, str);
433
atexit(&exit_db); /* now need to register an at exit func to remove tmpdb automatically! */
435
/* allocate a pointer to the directory structure */
436
sprintf(dir, "%s/docs/extensions", gisbase);
439
/* directory does not exist or is not accessible */
444
/* count number of subdirs in docs/extensions/ each new link will require one entry in index.html */
447
while ((ep = readdir(dirp))) {
448
sprintf(subdir, "%s/%s", dir, ep->d_name);
449
if ((!strcmp(ep->d_name, ".")) || (!strcmp(ep->d_name, ".."))) {
452
subdirp = opendir(subdir);
453
if (subdirp == NULL) {
461
/* count number of lines in menu.tcl */
463
while (fgets(str, MAXSTR, f_in) != NULL) {
471
/* create an array large enough to hold all lines in index.html */
472
/* plus one new entry to make a link for each extension */
473
/* plus space for the new Extensions section */
474
/* plus one NULL terminator */
475
/* and copy all lines from menu.tcl into this */
476
line = (char **)calloc(n_lines + n_subdirs + 10, sizeof(char *));
477
for (i = 0; i < (n_lines + n_subdirs + 10); i++) {
481
while (fgets(str, MAXSTR, f_in) != NULL) {
482
line[i] = (char *)malloc((1 + strlen(str)) * sizeof(char));
483
strcpy(line[i], str);
486
line[i] = NULL; /* add NULL terminator */
488
/* PASS 2: re-create links if necessary */
491
while ((ep = readdir(dirp))) {
492
sprintf(subdir, "%s/%s", dir, ep->d_name);
493
if ((!strcmp(ep->d_name, ".")) || (!strcmp(ep->d_name, ".."))) {
496
subdirp = opendir(subdir);
497
if (subdirp == NULL) {
502
/* try to open extension's index.html file */
503
sprintf(ext_idx, "%s/index.html", subdir);
504
f_ext = fopen(ext_idx, "r");
506
continue; /* cannot access index.html: skip to next extension */
511
/* retrieve version information from extension's index.html */
513
while (fgets(str, MAXSTR, f_ext) != NULL) {
514
if (strstr(str, "<title>") != NULL) {
516
break; /* this is the title line: that's all we need */
520
continue; /* not a valid index.html: skip to next extension */
522
sscanf(strchr(str, '(') + sizeof(char), "%i.%i.%i", &major, &minor,
524
new_ext_html(ep->d_name, gisbase, line, major, minor, revision);
530
/* write output to tmpfile */
532
while (line[i] != NULL) {
533
fprintf(f_out, line[i]);
538
/* close remaining files */
543
for (i = 0; i < (n_lines + n_subdirs + 10); i++) {
548
return (num_restored);