source: tags/svn.1.5.4/WINDOW/AW_help.cxx

Last change on this file was 8309, checked in by westram, 14 years ago
  • moved much code into static scope

(partly reverted by [8310])

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.0 KB
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : AW_help.cxx                                        //
4//   Purpose   :                                                    //
5//                                                                  //
6//   Institute of Microbiology (Technical University Munich)        //
7//   http://www.arb-home.de/                                        //
8//                                                                  //
9// ================================================================ //
10
11#include "aw_awar.hxx"
12#include "aw_window.hxx"
13#include "aw_edit.hxx"
14#include "aw_root.hxx"
15#include "aw_global_awars.hxx"
16#include "aw_msg.hxx"
17
18#include <arbdb.h>
19#include <arb_file.h>
20
21#include <sys/stat.h>
22
23
24#define AWAR_HELP       "tmp/help/"
25#define AWAR_HELPFILE   AWAR_HELP "file"
26#define AWAR_HELPTEXT   AWAR_HELP "text"
27#define AWAR_HELPSEARCH AWAR_HELP "search"
28
29
30void AW_openURL(AW_root *aw_root, const char *url) {
31    GB_CSTR  ka;
32    char    *browser = aw_root->awar(AWAR_WWW_BROWSER)->read_string();
33
34    while ((ka = GBS_find_string(browser, "$(URL)", 0))) {
35        char *start       = GB_strpartdup(browser, ka-1);
36        char *new_browser = GBS_global_string_copy("%s%s%s", start, url, ka+6);
37
38        free(start);
39        free(browser);
40
41        browser = new_browser;
42    }
43
44    char *command = GBS_global_string_copy("(%s)&", browser);
45    printf("Action: '%s'\n", command);
46    if (system(command)) aw_message(GBS_global_string("'%s' failed", command));
47    free(command);
48
49    free(browser);
50}
51
52// --------------------
53//      help window
54
55struct aw_help_global_struct {
56    AW_window *aww;
57    AW_selection_list   *upid;
58    AW_selection_list   *downid;
59    char    *history;
60};
61static aw_help_global_struct aw_help_global;
62
63static char *get_full_qualified_help_file_name(const char *helpfile, bool path_for_edit = false) {
64    GB_CSTR   result             = 0;
65    char     *user_doc_path      = strdup(GB_getenvDOCPATH());
66    char     *devel_doc_path     = strdup(GB_path_in_ARBHOME("HELP_SOURCE/oldhelp"));
67    size_t    user_doc_path_len  = strlen(user_doc_path);
68    size_t    devel_doc_path_len = strlen(devel_doc_path);
69
70    const char *rel_path = 0;
71    if (strncmp(helpfile, user_doc_path, user_doc_path_len) == 0 && helpfile[user_doc_path_len] == '/') {
72        rel_path = helpfile+user_doc_path_len+1;
73    }
74    else if (strncmp(helpfile, devel_doc_path, devel_doc_path_len) == 0 && helpfile[devel_doc_path_len] == '/') {
75        aw_assert(0);            // when does this happen ? never ?
76        rel_path = helpfile+devel_doc_path_len+1;
77    }
78
79    if (helpfile[0]=='/' && !rel_path) {
80        result = GBS_global_string("%s", helpfile);
81    }
82    else {
83        if (!rel_path) rel_path = helpfile;
84
85        if (rel_path[0]) {
86            if (path_for_edit) {
87#if defined(DEBUG)
88                char *gen_doc_path = strdup(GB_path_in_ARBHOME("HELP_SOURCE/genhelp"));
89
90                char *devel_source = GBS_global_string_copy("%s/%s", devel_doc_path, rel_path);
91                char *gen_source   = GBS_global_string_copy("%s/%s", gen_doc_path, rel_path);
92
93                int devel_size = GB_size_of_file(devel_source);
94                int gen_size   = GB_size_of_file(gen_source);
95
96                aw_assert(devel_size <= 0 || gen_size <= 0); // only one of them shall exist
97
98                if (gen_size>0) {
99                    result = GBS_global_string("%s", gen_source); // edit generated doc
100                }
101                else {
102                    result = GBS_global_string("%s", devel_source); // use normal help source (may be non-existing)
103                }
104
105                free(gen_source);
106                free(devel_source);
107                free(gen_doc_path);
108#else
109                result = GBS_global_string("%s/%s", GB_getenvDOCPATH(), rel_path); // use real help file in RELEASE
110#endif // DEBUG
111            }
112            else {
113                result = GBS_global_string("%s/%s", GB_getenvDOCPATH(), rel_path);
114            }
115        }
116        else {
117            result = "";
118        }
119    }
120
121    free(devel_doc_path);
122    free(user_doc_path);
123
124    return strdup(result);
125}
126
127static char *get_full_qualified_help_file_name(AW_root *awr, bool path_for_edit = false) {
128    char *helpfile = awr->awar(AWAR_HELPFILE)->read_string();
129    char *qualified = get_full_qualified_help_file_name(helpfile, path_for_edit);
130    free(helpfile);
131    return qualified;
132}
133
134static char *get_local_help_url(AW_root *awr) {
135    char   *helpfile          = get_full_qualified_help_file_name(awr, false);
136    char   *user_doc_path     = strdup(GB_getenvDOCPATH());
137    char   *user_htmldoc_path = strdup(GB_getenvHTMLDOCPATH());
138    size_t  user_doc_path_len = strlen(user_doc_path);
139    char   *result            = 0;
140
141    if (strncmp(helpfile, user_doc_path, user_doc_path_len) == 0) { // "real" help file
142        result            = GBS_global_string_copy("%s%s_", user_htmldoc_path, helpfile+user_doc_path_len);
143        size_t result_len = strlen(result);
144
145        aw_assert(result_len > 5);
146
147        if (strcmp(result+result_len-5, ".hlp_") == 0) {
148            strcpy(result+result_len-5, ".html");
149        }
150        else {
151            freenull(result);
152            GB_export_error("Can't browse that file type.");
153        }
154    }
155    else { // on-the-fly-generated help file (e.g. search help)
156        GB_export_error("Can't browse temporary help node");
157    }
158
159    free(user_htmldoc_path);
160    free(user_doc_path);
161    free(helpfile);
162
163    return result;
164}
165
166static void aw_help_edit_help(AW_window *aww) {
167    char *helpfile = get_full_qualified_help_file_name(aww->get_root(), true);
168
169    if (GB_size_of_file(helpfile)<=0) {
170#if defined(DEBUG)
171        const char *base = GB_path_in_ARBHOME("HELP_SOURCE/oldhelp");
172#else
173        const char *base = GB_path_in_ARBLIB("help");
174#endif // DEBUG
175
176        const char *copy_cmd = GBS_global_string("cp %s/FORM.hlp %s", base, helpfile); // def_hlp_res("FORM.hlp"); (see check_ressources.pl)
177        printf("[Executing '%s']\n", copy_cmd);
178        system(copy_cmd);
179    }
180
181    AW_edit(helpfile);
182
183    free(helpfile);
184}
185
186static char *aw_ref_to_title(char *ref) {
187    if (!ref) return 0;
188
189    if (GBS_string_matches(ref, "*.ps", GB_IGNORE_CASE)) {   // Postscript file
190        return GBS_global_string_copy("Postscript: %s", ref);
191    }
192
193    char *result = 0;
194    char *file = 0;
195    {
196        char *helpfile = get_full_qualified_help_file_name(ref);
197        file = GB_read_file(helpfile);
198        free(helpfile);
199    }
200
201    if (file) {
202        result = GBS_string_eval(file, "*\nTITLE*\n*=*2:\t=", 0);
203        if (strcmp(file, result)==0) freenull(result);
204        free(file);
205    }
206    else {
207        GB_clear_error();
208    }
209
210    if (result==0) {
211        result = strdup(ref);
212    }
213
214    return result;
215}
216
217static void aw_help_select_newest_in_history(AW_root *aw_root) {
218    char *history = aw_help_global.history;
219    if (history) {
220        const char *sep      = strchr(history, '#');
221        char       *lastHelp = sep ? GB_strpartdup(history, sep-1) : strdup(history);
222
223        aw_root->awar(AWAR_HELPFILE)->write_string(lastHelp);
224        free(lastHelp);
225    }
226}
227
228static void aw_help_back(AW_root *aw_root) {
229    char *history = aw_help_global.history;
230    if (history) {
231        const char *sep = strchr(history, '#');
232        if (sep) {
233            char *first = GB_strpartdup(history, sep-1);
234
235            freeset(aw_help_global.history, GBS_global_string_copy("%s#%s", sep+1, first)); // wrap first to end
236            free(first);
237            aw_help_select_newest_in_history(aw_root);
238        }
239    }
240}
241static void aw_help_back(AW_window *aww) { aw_help_back(aww->get_root()); }
242
243static GB_ERROR aw_help_show_external_format(const char *help_file, const char *viewer) {
244    // Called to show *.ps or *.pdf in external viewer.
245    // Can as well show *.suffix.gz (decompresses to temporary *.suffix)
246
247    struct stat st;
248    GB_ERROR    error = NULL;
249    char        sys[1024];
250
251    sys[0] = 0;
252
253    if (stat(help_file, &st) == 0) { // *.ps exists
254        GBS_global_string_to_buffer(sys, sizeof(sys), "%s %s &", viewer, help_file);
255    }
256    else {
257        char *compressed = GBS_global_string_copy("%s.gz", help_file);
258
259        if (stat(compressed, &st) == 0) { // *.ps.gz exists
260            char *name_ext;
261            GB_split_full_path(compressed, NULL, NULL, &name_ext, NULL);
262            // 'name_ext' contains xxx.ps or xxx.pdf
263            char *name, *suffix;
264            GB_split_full_path(name_ext, NULL, NULL, &name, &suffix);
265
266            char *tempname     = GB_unique_filename(name, suffix);
267            char *uncompressed = GB_create_tempfile(tempname);
268
269            GBS_global_string_to_buffer(sys, sizeof(sys),
270                                        "(gunzip <%s >%s ; %s %s ; rm %s) &",
271                                        compressed, uncompressed,
272                                        viewer, uncompressed,
273                                        uncompressed);
274
275            free(uncompressed);
276            free(tempname);
277            free(name);
278            free(suffix);
279            free(name_ext);
280        }
281        else {
282            error = GBS_global_string("Neither %s nor %s exists", help_file, compressed);
283        }
284        free(compressed);
285    }
286
287    if (sys[0] && !error) error = GBK_system(sys);
288
289    return error;
290}
291
292static void aw_help_helpfile_changed_cb(AW_root *awr) {
293    char *help_file = get_full_qualified_help_file_name(awr);
294
295    if (!strlen(help_file)) {
296        awr->awar(AWAR_HELPTEXT)->write_string("no help");
297    }
298    else if (GBS_string_matches(help_file, "*.ps", GB_IGNORE_CASE)) { // Postscript file
299        GB_ERROR error = aw_help_show_external_format(help_file, GB_getenvARB_GS());
300        if (error) aw_message(error);
301        aw_help_select_newest_in_history(awr);
302    }
303    else if (GBS_string_matches(help_file, "*.pdf", GB_IGNORE_CASE)) { // PDF file
304        GB_ERROR error = aw_help_show_external_format(help_file, GB_getenvARB_PDFVIEW());
305        if (error) aw_message(error);
306        aw_help_select_newest_in_history(awr);
307    }
308    else {
309        if (aw_help_global.history) {
310            if (strncmp(help_file, aw_help_global.history, strlen(help_file)) != 0) {
311                // remove current help from history (if present) and prefix it to history
312                char *comm = GBS_global_string_copy("*#%s*=*1*2:*=%s#*1", help_file, help_file);
313                char *h    = GBS_string_eval(aw_help_global.history, comm, 0);
314
315                aw_assert(h);
316                freeset(aw_help_global.history, h);
317                free(comm);
318            }
319        }
320        else {
321            aw_help_global.history = strdup(help_file);
322        }
323
324        char *helptext = GB_read_file(help_file);
325        if (helptext) {
326            char *ptr;
327            char *h, *h2, *tok;
328
329            ptr = strdup(helptext);
330            aw_help_global.aww->clear_selection_list(aw_help_global.upid);
331            h2 = GBS_find_string(ptr, "\nUP", 0);
332            while ((h = h2)) {
333                h2 = GBS_find_string(h2+1, "\nUP", 0);
334                tok = strtok(h+3, " \n\t");  // now I got UP
335                char *title = aw_ref_to_title(tok);
336                if (tok) aw_help_global.aww->insert_selection(aw_help_global.upid, title, tok);
337                free(title);
338            }
339            free(ptr);
340            aw_help_global.aww->insert_default_selection(aw_help_global.upid, "   ", "");
341            aw_help_global.aww->update_selection_list(aw_help_global.upid);
342
343            ptr = strdup(helptext);
344            aw_help_global.aww->clear_selection_list(aw_help_global.downid);
345            h2 = GBS_find_string(ptr, "\nSUB", 0);
346            while ((h = h2)) {
347                h2 = GBS_find_string(h2+1, "\nSUB", 0);
348                tok = strtok(h+4, " \n\t");  // now I got SUB
349                char *title = aw_ref_to_title(tok);
350                if (tok) aw_help_global.aww->insert_selection(aw_help_global.downid, title, tok);
351                free(title);
352            }
353            free(ptr);
354            aw_help_global.aww->insert_default_selection(aw_help_global.downid, "   ", "");
355            aw_help_global.aww->update_selection_list(aw_help_global.downid);
356
357            ptr = GBS_find_string(helptext, "TITLE", 0);
358            if (!ptr) ptr = helptext;
359            ptr = GBS_string_eval(ptr, "{*\\:*}=*2", 0);
360
361            awr->awar(AWAR_HELPTEXT)->write_string(ptr);
362            free(ptr);
363            free(helptext);
364        }
365        else {
366            char *msg = GBS_global_string_copy("I cannot find the help file '%s'\n\n"
367                                               "Please help us to complete the ARB-Help by submitting\n"
368                                               "this missing helplink via ARB_NT/File/About/SubmitBug\n"
369                                               "Thank you.\n"
370                                               "\n"
371                                               "Details:\n"
372                                               "%s",
373                                               help_file, GB_await_error());
374            awr->awar(AWAR_HELPTEXT)->write_string(msg);
375            free(msg);
376        }
377    }
378    free(help_file);
379}
380
381static void aw_help_browse(AW_window *aww) {
382    char *help_url = get_local_help_url(aww->get_root());
383    if (help_url) {
384        AW_openURL(aww->get_root(), help_url);
385        free(help_url);
386    }
387    else {
388        aw_message(GBS_global_string("Can't detect URL of help file\n(Reason: %s)", GB_await_error()));
389    }
390}
391
392static void aw_help_search(AW_window *aww) {
393    GB_ERROR  error      = 0;
394    char     *searchtext = aww->get_root()->awar(AWAR_HELPSEARCH)->read_string();
395
396    if (searchtext[0]==0) error = "Empty searchstring";
397    else {
398        char        *helpfilename = 0;
399        static char *last_help; // tempfile containing last search result
400
401        // replace all spaces in 'searchtext' by '.*'
402        freeset(searchtext, GBS_string_eval(searchtext, " =.*", 0));
403
404        // grep .hlp for occurrences of 'searchtext'.
405        // write filenames of matching files into 'helpfilename'
406        {
407            {
408                char *helpname = GB_unique_filename("arb", "hlp");
409                helpfilename   = GB_create_tempfile(helpname);
410                free(helpname);
411            }
412
413            if (!helpfilename) error = GB_await_error();
414            else {
415                const char *gen_help_tmpl = "cd %s;grep -i '^[^#]*%s' `find . -name \"*.hlp\"` | sed -e 'sI:.*IIg' -e 'sI^\\./IIg' | sort | uniq > %s";
416                char       *gen_help_cmd  = GBS_global_string_copy(gen_help_tmpl, GB_getenvDOCPATH(), searchtext, helpfilename);
417
418                error = GBK_system(gen_help_cmd);
419
420                free(gen_help_cmd);
421                GB_remove_on_exit(helpfilename);
422            }
423        }
424
425        if (!error) {
426            char *result       = GB_read_file(helpfilename);
427            if (!result) error = GB_await_error();
428            else {
429                // write temporary helpfile containing links to matches as subtopics
430
431                FILE *helpfp       = fopen(helpfilename, "wt");
432                if (!helpfp) error = GB_export_IO_error("writing helpfile", helpfilename);
433                else {
434                    fprintf(helpfp, "\nUP arb.hlp\n");
435                    if (last_help) fprintf(helpfp, "UP %s\n", last_help);
436                    fputc('\n', helpfp);
437
438                    int   results = 0;
439                    char *rp      = result;
440                    while (1) {
441                        char *eol = strchr(rp, '\n');
442                        if (!eol) {
443                            eol = rp;
444                            while (*eol) ++eol;
445                        }
446                        if (eol>rp) {
447                            char old = eol[0];
448                            eol[0] = 0;
449                            fprintf(helpfp, "SUB %s\n", rp);
450                            results++;
451                            eol[0] = old;
452                        }
453                        if (eol[0]==0) break; // all results inserted
454                        rp = eol+1;
455                    }
456
457                    fprintf(helpfp, "\nTITLE\t\tResult of search for '%s'\n\n", searchtext);
458                    if (results>0)  fprintf(helpfp, "\t\t%i results are shown as subtopics\n",  results);
459                    else            fprintf(helpfp, "\t\tThere are no results.\n");
460
461                    if (results>0) freedup(last_help, helpfilename);
462
463                    fclose(helpfp);
464                    aww->get_root()->awar(AWAR_HELPFILE)->write_string(helpfilename); // display results in helpwindow
465                }
466                free(result);
467            }
468        }
469        free(helpfilename);
470    }
471
472    if (error) aw_message(error);
473
474    free(searchtext);
475}
476
477void AW_POPUP_HELP(AW_window *aw, AW_CL /* char */ helpcd) {
478    static AW_window_simple *helpwindow = 0;
479
480    AW_root *awr       = aw->get_root();
481    char    *help_file = (char*)helpcd;
482
483    if (!helpwindow) {
484        awr->awar_string(AWAR_HELPTEXT,   "", AW_ROOT_DEFAULT);
485        awr->awar_string(AWAR_HELPSEARCH, "", AW_ROOT_DEFAULT);
486        awr->awar_string(AWAR_HELPFILE,   "", AW_ROOT_DEFAULT);
487        awr->awar(AWAR_HELPFILE)->add_callback(aw_help_helpfile_changed_cb);
488
489        helpwindow = new AW_window_simple;
490        helpwindow->init(awr, "HELP", "HELP WINDOW");
491        helpwindow->load_xfig("help.fig");
492
493        helpwindow->button_length(10);
494
495        helpwindow->at("close");
496        helpwindow->callback((AW_CB0)AW_POPDOWN);
497        helpwindow->create_button("CLOSE", "CLOSE", "C");
498
499        helpwindow->at("back");
500        helpwindow->callback(aw_help_back);
501        helpwindow->create_button("BACK", "BACK", "B");
502
503
504        helpwindow->at("super");
505        aw_help_global.upid = helpwindow->create_selection_list(AWAR_HELPFILE, 0, 0, 3, 3);
506        helpwindow->insert_default_selection(aw_help_global.upid, "   ", "");
507        helpwindow->update_selection_list(aw_help_global.upid);
508
509        helpwindow->at("sub");
510        aw_help_global.downid = helpwindow->create_selection_list(AWAR_HELPFILE, 0, 0, 3, 3);
511        helpwindow->insert_default_selection(aw_help_global.downid, "   ", "");
512        helpwindow->update_selection_list(aw_help_global.downid);
513        aw_help_global.aww = helpwindow;
514        aw_help_global.history = 0;
515
516        helpwindow->at("text");
517        helpwindow->create_text_field(AWAR_HELPTEXT, 3, 3);
518
519        helpwindow->at("browse");
520        helpwindow->callback(aw_help_browse);
521        helpwindow->create_button("BROWSE", "BROWSE", "B");
522
523        helpwindow->at("expression");
524        helpwindow->create_input_field(AWAR_HELPSEARCH);
525
526        helpwindow->at("search");
527        helpwindow->callback(aw_help_search);
528        helpwindow->create_button("SEARCH", "SEARCH", "S");
529
530        helpwindow->at("edit");
531        helpwindow->callback(aw_help_edit_help);
532        helpwindow->create_button("EDIT", "EDIT", "E");
533
534    }
535
536    aw_assert(help_file);
537
538    awr->awar(AWAR_HELPFILE)->write_string(help_file);
539
540    if (!GBS_string_matches(help_file, "*.ps", GB_IGNORE_CASE) &&
541        !GBS_string_matches(help_file, "*.pdf", GB_IGNORE_CASE))
542    { // don't open help if postscript or pdf file
543        helpwindow->activate();
544    }
545}
546
547
548
Note: See TracBrowser for help on using the repository browser.