source: git/Singular/fehelp.cc @ 5c5638

spielwiese
Last change on this file since 5c5638 was 6ed924, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: fixed help *x* git-svn-id: file:///usr/local/Singular/svn/trunk@8062 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 30.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: help system
6* versin $Id: fehelp.cc,v 1.43 2005-05-06 10:08:43 Singular Exp $
7*/
8
9#include <string.h>
10#include <unistd.h>
11#include <stdio.h>
12#include <stddef.h>
13#include <stdlib.h>
14#include <time.h>
15
16#include "mod2.h"
17#include <mylimits.h>
18#include "tok.h"
19#include "omalloc.h"
20#include "febase.h"
21#include "ipid.h"
22#include "ipshell.h"
23#include "libparse.h"
24#include "feOpt.h"
25#include "dError.h"
26
27/*****************************************************************
28 *
29 * Declarations: Data  structures
30 *
31 *****************************************************************/
32#define MAX_HE_ENTRY_LENGTH 160
33typedef struct
34{
35  char key[MAX_HE_ENTRY_LENGTH];
36  char node[MAX_HE_ENTRY_LENGTH];
37  char url[MAX_HE_ENTRY_LENGTH];
38  long  chksum;
39} heEntry_s;
40typedef  heEntry_s * heEntry;
41
42typedef void (*heBrowserHelpProc)(heEntry hentry, int br);
43typedef BOOLEAN (*heBrowserInitProc)(int warn, int br);
44
45typedef struct
46{
47  char* browser;
48  heBrowserInitProc init_proc;
49  heBrowserHelpProc help_proc;
50  char* required;
51  char* action;
52} heBrowser_s;
53typedef heBrowser_s * heBrowser;
54
55/*****************************************************************
56 *
57 * Declarations: Local functions
58 *
59 *****************************************************************/
60static char* strclean(char* str);
61static BOOLEAN heKey2Entry(char* filename, char* key, heEntry hentry);
62static int heReKey2Entry (char* filename, char* key, heEntry hentry);
63static BOOLEAN strmatch(char* s, char* re);
64static BOOLEAN heOnlineHelp(char* s);
65static void heBrowserHelp(heEntry hentry);
66static long heKeyChksum(char* key);
67
68// browser functions
69static BOOLEAN heGenInit(int,int);    static void heGenHelp(heEntry hentry,int);
70                                      static void heBuiltinHelp(heEntry hentry,int);
71static BOOLEAN heDummyInit(int,int);   static void heDummyHelp(heEntry hentry,int);
72static BOOLEAN heEmacsInit(int,int);   static void heEmacsHelp(heEntry hentry,int);
73
74#ifdef ix86_Win
75static void heHtmlHelp(heEntry hentry,int);
76static void heWinHelp(heEntry hentry,int);
77#include "sing_win.h"
78#endif
79
80static heBrowser heCurrentHelpBrowser = NULL;
81static int heCurrentHelpBrowserIndex= -1;
82
83
84/*****************************************************************
85 *
86 * Definition: available help browsers
87 *
88 *****************************************************************/
89// order is improtant -- first possible help is choosen
90#if 1
91static heBrowser_s *heHelpBrowsers=NULL;
92#else
93static heBrowser_s heHelpBrowsers[] =
94{
95#ifdef ix86_Win
96//  { "html",     heDummyInit,    heHtmlHelp, NULL, NULL},
97//  { "winhlp",   heDummyInit,    heWinHelp, NULL, NULL},
98#endif
99  { "mozilla",  heGenInit,      heGenHelp, "xDhE:mozilla:", "(mozilla -remote \"openURL(%h)\")||(mozilla %h)"},
100  { "netscape", heGenInit,      heGenHelp, "xDhE:netscape:", "(netscape -remote \"openURL(%h)\")||(netscape %h)"},
101  { "tkinfo",   heGenInit,      heGenHelp, "xDiE:tkinfo:", "tkinfo '(%i)%n' &"},
102  { "xinfo",    heGenInit,      heGenHelp, "xDiE:xterm:E:info:", "xterm -e info -f %i --node='%n' &"},
103  { "mac",      heGenInit,      heGenHelp,  "xhE:open:","open %h" },
104  { "machtml",  heGenInit,      heGenHelp,  "xE:open:","open %H" },
105  { "info",     heGenInit,      heGenHelp, "xiE:info:", "info -f %i --node='%n' &"},
106  { "builtin",  heGenInit,      heBuiltinHelp, "i", NULL},
107  { "dummy",    heDummyInit,    heDummyHelp, NULL, NULL},
108  { "emacs",    heEmacsInit,    heEmacsHelp, NULL, NULL},
109  { NULL, NULL, NULL, NULL, NULL} // must be the last record
110};
111#endif
112
113/*****************************************************************
114 *
115 * Implementation: public function
116 *
117 *****************************************************************/
118void feHelp(char *str)
119{
120  str = strclean(str);
121  if (str == NULL) {heBrowserHelp(NULL); return;}
122
123  if (strlen(str) > MAX_HE_ENTRY_LENGTH - 2)  // need room for extra **
124    str[MAX_HE_ENTRY_LENGTH - 3] = '\0';
125
126  BOOLEAN key_is_regexp = (strchr(str, '*') != NULL);
127  heEntry_s hentry;
128  char* idxfile = feResource('x' /*"IdxFile"*/);
129
130  // Try exact match of help string with key in index
131  if (!key_is_regexp && idxfile != NULL && heKey2Entry(idxfile, str, &hentry))
132  {
133    heBrowserHelp(&hentry);
134    return;
135  }
136
137  // try proc help and library help
138  if (! key_is_regexp && heOnlineHelp(str)) return;
139
140  // Try to match approximately with key in index file
141  if (idxfile != NULL)
142  {
143    char* matches = StringSetS("");
144    int found = heReKey2Entry(idxfile, str, &hentry);
145
146    // Try to match with str*
147    if (found == 0)
148    {
149      char mkey[MAX_HE_ENTRY_LENGTH];
150      strcpy(mkey, str);
151      strcat(mkey, "*");
152      found = heReKey2Entry(idxfile, mkey, &hentry);
153      // Try to match with *str*
154      if (found == 0)
155      {
156        mkey[0] = '*';
157        strcpy(mkey + 1, str);
158        strcat(mkey, "*");
159        found = heReKey2Entry(idxfile, mkey, &hentry);
160      }
161
162      // Print warning and return if nothing found
163      if (found == 0)
164      {
165        Warn("No help for topic '%s' (not even for '*%s*')", str, str);
166        WarnS("Try '?;'       for general help");
167        WarnS("or  '?Index;'  for all available help topics.");
168        return;
169      }
170    }
171
172    // do help if unique match was found
173    if (found == 1)
174    {
175      heBrowserHelp(&hentry);
176      return;
177    }
178    // Print warning about multiple matches and return
179    if (key_is_regexp)
180      Warn("No unique help for '%s'", str);
181    else
182      Warn("No help for topic '%s'", str);
183    Warn("Try one of");
184    PrintS(matches);
185    PrintS("\n");
186    return;
187  }
188
189  // no idx file, let Browsers deal with it, if they can
190  strcpy(hentry.key, str);
191  *hentry.node = '\0';
192  *hentry.url = '\0';
193  hentry.chksum = 0;
194  heBrowserHelp(&hentry);
195}
196static void feBrowserFile()
197{
198  FILE *f=feFopen("help.cnf","r",NULL,TRUE);
199  int br=0;
200  if (f!=NULL)
201  {
202    char buf[512];
203    while (fgets( buf, sizeof(buf), f))
204    {
205      if ((buf[0]!='#') && (buf[0]>' ')) br++;
206    }
207    fseek(f,0,SEEK_SET);
208    // for the 4(!) default browsers
209    heHelpBrowsers=(heBrowser_s*)omAlloc0((br+4)*sizeof(heBrowser_s));
210    br=0;
211    while (fgets( buf, sizeof(buf), f))
212    {
213      if ((buf[0]!='#') && (buf[0]>' '))
214      {
215        char *name=strtok(buf,"!");
216        char *req=strtok(NULL,"!");
217        char *cmd=strtok(NULL,"!");
218        while ((cmd[0]!='\0') && (cmd[strlen(cmd)-1]<=' '))
219          cmd[strlen(cmd)-1]='\0';
220        //Print("name %d >>%s<<\n\treq:>>%s<<\n\tcmd:>>%s<<\n",br,name,req,cmd);
221        heHelpBrowsers[br].browser=(char *)omStrDup(name);
222        heHelpBrowsers[br].init_proc=heGenInit;
223        heHelpBrowsers[br].help_proc=heGenHelp;
224        heHelpBrowsers[br].required=omStrDup(req);
225        heHelpBrowsers[br].action=omStrDup(cmd);
226        br++;
227      }
228    }
229    fclose(f);
230  }
231  else
232  {
233#ifdef ix86_Win
234    // for the 6(!) default browsers
235    heHelpBrowsers=(heBrowser_s*)omAlloc0(6*sizeof(heBrowser_s));
236#else
237    // for the 4(!) default browsers
238    heHelpBrowsers=(heBrowser_s*)omAlloc0(4*sizeof(heBrowser_s));
239#endif
240  }
241#ifdef ix86_Win
242  heHelpBrowsers[br].browser="winhlp";
243  heHelpBrowsers[br].init_proc=heGenInit;
244  heHelpBrowsers[br].help_proc=heWinHelp;
245  heHelpBrowsers[br].required="h";
246  //heHelpBrowsers[br].action=NULL;
247  br++;
248  heHelpBrowsers[br].browser="html";
249  heHelpBrowsers[br].init_proc=heGenInit;
250  heHelpBrowsers[br].help_proc=heHtmlHelp;
251  heHelpBrowsers[br].required="h";
252  //heHelpBrowsers[br].action=NULL;
253  br++;
254#endif
255  heHelpBrowsers[br].browser="builtin";
256  heHelpBrowsers[br].init_proc=heGenInit;
257  heHelpBrowsers[br].help_proc=heBuiltinHelp;
258  heHelpBrowsers[br].required="i";
259  //heHelpBrowsers[br].action=NULL;
260  br++;
261  heHelpBrowsers[br].browser="dummy";
262  heHelpBrowsers[br].init_proc=heDummyInit;
263  heHelpBrowsers[br].help_proc=heDummyHelp;
264  //heHelpBrowsers[br].required=NULL;
265  //heHelpBrowsers[br].action=NULL;
266  br++;
267  heHelpBrowsers[br].browser="emacs";
268  heHelpBrowsers[br].init_proc=heEmacsInit;
269  heHelpBrowsers[br].help_proc=heEmacsHelp;
270  //heHelpBrowsers[br].required=NULL;
271  //heHelpBrowsers[br].action=NULL;
272  //br++;
273  //heHelpBrowsers[br].browser=NULL;
274  //heHelpBrowsers[br].init_proc=NULL;
275  //heHelpBrowsers[br].help_proc=NULL;
276  //heHelpBrowsers[br].required=NULL;
277  //heHelpBrowsers[br].action=NULL;
278}
279
280char* feHelpBrowser(char* which, int warn)
281{
282  int i = 0;
283
284  // if no argument, choose first available help browser
285  if (heHelpBrowsers==NULL) feBrowserFile();
286  if (which == NULL || *which == '\0')
287  {
288    // return, if already set
289    if (heCurrentHelpBrowser != NULL)
290      return heCurrentHelpBrowser->browser;
291
292    // First, try emacs, if emacs-option is set
293    // Under Win, always use html
294#ifndef ix86_Win
295    if (feOptValue(FE_OPT_EMACS) != NULL)
296    {
297      while (heHelpBrowsers[i].browser != NULL)
298      {
299        if (strcmp(heHelpBrowsers[i].browser, "emacs") == 0 &&
300            (heHelpBrowsers[i].init_proc(0,i)))
301        {
302          heCurrentHelpBrowser = &(heHelpBrowsers[i]);
303          heCurrentHelpBrowserIndex=i;
304          goto Finish;
305        }
306        i++;
307      }
308      i=0;
309    }
310#endif
311    while (heHelpBrowsers[i].browser != NULL)
312    {
313      if (heHelpBrowsers[i].init_proc(0,i))
314      {
315        heCurrentHelpBrowser = &(heHelpBrowsers[i]);
316        heCurrentHelpBrowserIndex=i;
317        goto Finish;
318      }
319      i++;
320    }
321    // should never get here
322    dReportBug("should never get here");
323  }
324
325  // with argument, find matching help browser
326  while (heHelpBrowsers[i].browser != NULL &&
327         strcmp(heHelpBrowsers[i].browser, which) != 0)
328  {i++;}
329
330  if (heHelpBrowsers[i].browser == NULL)
331  {
332    if (warn) Warn("No help browser '%s' available.", which);
333  }
334  else
335  {
336    // see whether we can init it
337    if (heHelpBrowsers[i].init_proc(warn,i))
338    {
339      heCurrentHelpBrowser = &(heHelpBrowsers[i]);
340      heCurrentHelpBrowserIndex=i;
341      goto Finish;
342    }
343  }
344
345  // something went wrong
346  if (heCurrentHelpBrowser == NULL)
347  {
348    feHelpBrowser();
349    assume(heCurrentHelpBrowser != NULL);
350    if (warn)
351      Warn("Setting help browser to '%s'.", heCurrentHelpBrowser->browser);
352    return heCurrentHelpBrowser->browser;
353  }
354  else
355  {
356    // or, leave as is
357    if (warn)
358      Warn("Help browser stays at '%s'.",  heCurrentHelpBrowser->browser);
359    return heCurrentHelpBrowser->browser;
360  }
361
362  Finish:
363  // update value of Browser Option
364  if (feOptSpec[FE_OPT_BROWSER].value == NULL ||
365      strcmp((char*) feOptSpec[FE_OPT_BROWSER].value,
366             heCurrentHelpBrowser->browser) != 0)
367  {
368    omfree(feOptSpec[FE_OPT_BROWSER].value);
369    feOptSpec[FE_OPT_BROWSER].value
370     = (void*) omStrDup(heCurrentHelpBrowser->browser);
371  }
372  return heCurrentHelpBrowser->browser;
373}
374
375void  feStringAppendBrowsers(int warn)
376{
377  int i;
378  StringAppendS("Available HelpBrowsers: ");
379
380  i = 0;
381  if (heHelpBrowsers==NULL) feBrowserFile();
382  while (heHelpBrowsers[i].browser != NULL)
383  {
384    if (heHelpBrowsers[i].init_proc(warn,i))
385      StringAppend("%s, ", heHelpBrowsers[i].browser);
386    i++;
387  }
388  StringAppend("\nCurrent HelpBrowser: %s ", feHelpBrowser());
389}
390
391
392/*****************************************************************
393 *
394 * Implementation: local function
395 *
396 *****************************************************************/
397// Remove whitspaces from beginning and end, return NULL if only whitespaces
398static char* strclean(char* str)
399{
400  if (str == NULL) return NULL;
401  char *s=str;
402  while (*s <= ' ' && *s != '\0') s++;
403  if (*s == '\0') return NULL;
404  char *ss=s;
405  while (*ss!='\0') ss++;
406  ss--;
407  while (*ss <= ' ' && *ss != '\0')
408  {
409    *ss='\0';
410    ss--;
411  }
412  if (*ss == '\0') return NULL;
413  return s;
414}
415
416// Finds help entry for key:
417// returns filled-in hentry and TRUE, on success
418// FALSE, on failure
419// Assumes that lines of idx file have the following form
420// key\tnode\turl\tchksum\n (chksum ma be empty, then it is set to -1)
421// and that lines are sorted alpahbetically w.r.t. index entries
422static BOOLEAN heKey2Entry(char* filename, char* key, heEntry hentry)
423{
424  FILE* fd;
425  char c, k;
426  int kl, i;
427  *(hentry->key) = '\0';
428  *(hentry->url) = '\0';
429  *(hentry->node) = '\0';
430  hentry->chksum = 0;
431  if (filename == NULL || key == NULL)  return FALSE;
432  fd = fopen(filename, "r");
433  if (fd == NULL) return FALSE;
434  kl = strlen(key);
435
436  k = key[0];
437  i = 0;
438  while ((c = getc(fd)) != EOF)
439  {
440    if (c < k)
441    {
442      /* Skip line */
443      while (getc(fd) != '\n') {};
444      if (i)
445      {
446        i=0;
447        k=key[0];
448      }
449    }
450    else if (c == k)
451    {
452      i++;
453      if (i == kl)
454      {
455        // \t must follow, otherwise only substring match
456        if (getc(fd) != '\t') goto Failure;
457
458        // Now we found an exact match
459        if (hentry->key != key) strcpy(hentry->key, key);
460        // get node
461        i = 0;
462        while ((c = getc(fd)) != '\t' && c != EOF)
463        {
464          hentry->node[i] = c;
465          i++;
466        }
467        if (c == EOF) goto Failure;
468
469        // get url
470        hentry->node[i] = '\0';
471        i = 0;
472        while ((c = getc(fd)) != '\t' && c != EOF)
473        {
474          hentry->url[i] = c;
475          i++;
476        }
477        if (c == EOF) goto Failure;
478
479        // get chksum
480        hentry->url[i] = '\0';
481
482        if (fscanf(fd, "%ld\n", &(hentry->chksum)) != 1)
483        {
484          hentry->chksum = -1;
485        }
486        fclose(fd);
487        return TRUE;
488      }
489      else if (i > kl)
490      {
491        goto Failure;
492      }
493      else
494      {
495        k = key[i];
496      }
497    }
498    else
499    {
500      goto Failure;
501    }
502  }
503  Failure:
504  fclose(fd);
505  return FALSE;
506}
507
508// return TRUE if s matches re
509// FALSE, otherwise
510// does not distinguish lower and upper cases
511// inteprets * as wildcard
512static BOOLEAN strmatch(char* s, char* re)
513{
514  if (s == NULL || *s == '\0')
515    return (re == NULL || *re == '\0' || strcmp(re, "*") == 0);
516  if (re == NULL || *re == '\0') return FALSE;
517
518  int i;
519  char ls[MAX_HE_ENTRY_LENGTH + 1];
520  char rs[MAX_HE_ENTRY_LENGTH + 1];
521  char *l, *r, *ll, *rr;
522
523  // make everything to lower case
524  i=1;
525  ls[0] = '\0';
526  do
527  {
528    if (*s >= 'A' && *s <= 'Z') ls[i] = *s + ('a' - 'A');
529    else ls[i] = *s;
530    i++;
531    s++;
532  } while (*s != '\0');
533  ls[i] = '\0';
534  l = &(ls[1]);
535
536  i=1;
537  rs[0] = '\0';
538  do
539  {
540    if (*re >= 'A' && *re <= 'Z') rs[i]= *re + ('a' - 'A');
541    else rs[i] = *re;
542    i++;
543    re++;
544  } while (*re != '\0');
545  rs[i] = '\0';
546  r = &(rs[1]);
547
548  // chopp of exact matches from beginning and end
549  while (*r != '*' && *r != '\0' && *l != '\0')
550  {
551    if (*r != *l) return FALSE;
552    *r = '\0';
553    *s = '\0';
554    r++;
555    l++;
556  }
557  if (*r == '\0') return (*l == '\0');
558  if (*r == '*' && r[1] == '\0') return TRUE;
559  if (*l == '\0') return FALSE;
560
561  rr = &r[strlen(r) - 1];
562  ll = &l[strlen(l) - 1];
563  while (*rr != '*' && *rr != '\0' && *ll != '\0')
564  {
565    if (*rr != *ll) return FALSE;
566    *rr = '\0';
567    *ll = '\0';
568    rr--;
569    ll--;
570  }
571  if (*rr == '\0') return (*ll == '\0');
572  if (*rr == '*' && rr[-1] == '\0') return TRUE;
573  if (*ll == '\0') return FALSE;
574
575  // now *r starts with a * and ends with a *
576  r++;
577  *rr = '\0'; rr--;
578  while (*r != '\0')
579  {
580    rr = r + 1;
581    while (*rr != '*' && *rr != '\0') rr++;
582    if (*rr == '*')
583    {
584      *rr = '\0';
585      rr++;
586    }
587    l = strstr(l, r);
588    if (l == NULL) return FALSE;
589    r = rr;
590  }
591  return TRUE;
592}
593
594// similar to heKey2Entry, except that
595// key is taken as regexp (see above)
596// and number of matches is returned
597// if number of matches > 0, then hentry contains entry for first match
598// if number of matches > 1, matches are printed as komma-separated
599// into global string
600static int heReKey2Entry (char* filename, char* key, heEntry hentry)
601{
602  int i = 0;
603  FILE* fd;
604  char index_key[MAX_HE_ENTRY_LENGTH];
605
606  if (filename == NULL || key == NULL)  return 0;
607  fd = fopen(filename, "r");
608  if (fd == NULL) return 0;
609  memset(index_key,0,MAX_HE_ENTRY_LENGTH);
610  while (fscanf(fd, "%[^\t]\t%*[^\n]\n", index_key) == 1)
611  {
612    if ((index_key[MAX_HE_ENTRY_LENGTH-1]!='\0'))
613    {
614      index_key[MAX_HE_ENTRY_LENGTH-1]='\0';
615      Werror("index file corrupt at line >>%s<<",index_key);
616      break;
617    }
618    else if (strmatch(index_key, key))
619    {
620      i++;
621      if (i == 1)
622      {
623        heKey2Entry(filename, index_key, hentry);
624      }
625      else if (i == 2)
626      {
627        StringAppend("?%s; ?%s;", hentry->key, index_key);
628      }
629      else
630      {
631        StringAppend(" ?%s;", index_key);
632      }
633    }
634  }
635  fclose(fd);
636  return i;
637}
638
639// test for h being a string and print it
640static void hePrintHelpStr(const idhdl hh,const char *id,const char *pa)
641{
642  if ((hh!=NULL) && (IDTYP(hh)==STRING_CMD))
643  {
644    PrintS(IDSTRING(hh));
645    PrintLn();
646  }
647  else
648    Print("`%s` not found in package %s\n",id,pa);
649}
650// try to find the help string as a loaded procedure or library
651// if found, display the help and return TRUE
652// otherwise, return FALSE
653static BOOLEAN heOnlineHelp(char* s)
654{
655  idhdl h=IDROOT->get(s,myynest);
656  char *ss;
657
658  // try help for a procedure
659  if (h!=NULL)
660  {
661    if  (IDTYP(h)==PROC_CMD)
662    {
663      char *lib=iiGetLibName(IDPROC(h));
664      if((lib!=NULL)&&(*lib!='\0'))
665      {
666        Print("// proc %s from lib %s\n",s,lib);
667        s=iiGetLibProcBuffer(IDPROC(h), 0);
668        if (s!=NULL)
669        {
670          PrintS(s);
671          omFree((ADDRESS)s);
672        }
673        return TRUE;
674      }
675      else
676      {
677        char s_help[200];
678        strcpy(s_help,s);
679        strcat(s_help,"_help");
680        idhdl hh=IDROOT->get(s_help,0);
681        hePrintHelpStr(hh,s_help,"Top");
682      }
683    }
684    else if (IDTYP(h)==PACKAGE_CMD)
685    {
686      idhdl hh=IDPACKAGE(h)->idroot->get("info",0);
687      hePrintHelpStr(hh,"info",s);
688    }
689    else if ((ss=strstr(s,"::"))!=NULL)
690    {
691      *ss='\0';
692      ss+=2;
693      h=ggetid(s);
694      if (h!=NULL)
695      {     
696        Print("help for %s from package %\n",ss,s);
697        char s_help[200];
698        strcpy(s_help,ss);
699        strcat(s_help,"_help");
700        idhdl hh=IDPACKAGE(h)->idroot->get(s_help,0);
701        hePrintHelpStr(hh,s_help,s);
702      }
703      else Print("package %s not found\n",s); 
704    }
705    return FALSE;
706  }
707
708  // try help for a library
709  int ls = strlen(s);
710  char* str = NULL;
711  // check that it ends with "[.,_]lib"
712  if (strlen(s) >=4 &&  strcmp(&s[ls-3], "lib") == 0)
713  {
714    if (s[ls - 4] == '.') str = s;
715    else
716    {
717      str = omStrDup(s);
718      str[ls - 4] = '.';
719    }
720  }
721  else
722  {
723    return FALSE;
724  }
725
726  char libnamebuf[128];
727  FILE *fp=NULL;
728  // first, search for library of that name in LIB string
729  if ((str[1]!='\0') &&
730      ((iiLocateLib(str, libnamebuf) && (fp=feFopen(libnamebuf, "rb")) !=NULL)
731       ||
732       ((fp=feFopen(str,"rb", libnamebuf))!=NULL)))
733  {
734    extern FILE *yylpin;
735    lib_style_types lib_style; // = OLD_LIBSTYLE;
736
737    yylpin = fp;
738#ifdef HAVE_NS
739    yylplex(str, libnamebuf, &lib_style, IDROOT, FALSE, GET_INFO);
740#else
741    yylplex(str, libnamebuf, &lib_style, GET_INFO);
742#endif /* HAVE_NS */
743    reinit_yylp();
744    if(lib_style == OLD_LIBSTYLE)
745    {
746      char buf[256];
747      fseek(fp, 0, SEEK_SET);
748      Warn( "library %s has an old format. Please fix it for the next time",
749            str);
750      if (str != s) omFree(str);
751      BOOLEAN found=FALSE;
752      while (fgets( buf, sizeof(buf), fp))
753      {
754        if (strncmp(buf,"//",2)==0)
755        {
756          if (found) return TRUE;
757        }
758        else if ((strncmp(buf,"proc ",5)==0)||(strncmp(buf,"LIB ",4)==0))
759        {
760          if (!found) WarnS("no help part in library found");
761          return TRUE;
762        }
763        else
764        {
765          found=TRUE;
766          PrintS(buf);
767        }
768      }
769    }
770    else
771    {
772      if (str != s) omFree(str);
773      fclose( yylpin );
774      PrintS(text_buffer);
775      omFree(text_buffer);
776      text_buffer=NULL;
777    }
778    return TRUE;
779  }
780
781  if (str != s) omFree(str);
782  return FALSE;
783}
784
785static long heKeyChksum(char* key)
786{
787  if (key == NULL || *key == '\0') return 0;
788#ifdef HAVE_NS
789  idhdl h=IDROOT->get(key,myynest);
790#else
791  idhdl h=idroot->get(key,myynest);
792#endif /* HAVE_NS */
793  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
794  {
795    procinfo *pi = IDPROC(h);
796    if (pi != NULL) return pi->data.s.help_chksum;
797  }
798  return 0;
799}
800
801/*****************************************************************
802 *
803 * Implementation : Help Browsers
804 *
805 *****************************************************************/
806
807static BOOLEAN feHelpCalled = FALSE;
808
809static void heBrowserHelp(heEntry hentry)
810{
811  // check checksums of procs
812  int kchksum = (hentry != NULL && hentry->chksum > 0 ?
813                 heKeyChksum(hentry->key) : 0);
814  if (kchksum  && kchksum != hentry->chksum && heOnlineHelp(hentry->key))
815    return;
816
817  if (heCurrentHelpBrowser == NULL) feHelpBrowser(NULL, 0);
818  assume(heCurrentHelpBrowser != NULL);
819  if (! feHelpCalled)
820  {
821    Warn("Displaying help in browser '%s'.", heCurrentHelpBrowser->browser);
822    if (strcmp(heCurrentHelpBrowser->browser, "netscape") == 0 &&
823        feResource('h', 0) == NULL)
824    {
825      Warn("Using URL '%s'.", feResource('u', 0));
826    }
827    Warn("Use 'system(\"--browser\", <browser>);' to change browser,");
828    char* browsers = StringSetS("where <browser> can be: ");
829    int i = 0;
830    i = 0;
831    while (heHelpBrowsers[i].browser != NULL)
832    {
833      if (heHelpBrowsers[i].init_proc(0,i))
834        StringAppend("\"%s\", ", heHelpBrowsers[i].browser);
835      i++;
836    }
837    if (browsers[strlen(browsers)-2] == ',')
838    {
839      browsers[strlen(browsers)-2] = '.';
840      browsers[strlen(browsers)-1] = '\0';
841    }
842    WarnS(browsers);
843  }
844
845  heCurrentHelpBrowser->help_proc(hentry, heCurrentHelpBrowserIndex);
846  feHelpCalled = TRUE;
847}
848
849#define MAX_SYSCMD_LEN MAXPATHLEN*2
850static BOOLEAN heGenInit(int warn, int br)
851{
852  if (heHelpBrowsers[br].required==NULL) return TRUE;
853  char *p=heHelpBrowsers[br].required;
854  while (*p>'\0')
855  {
856    switch (*p)
857    {
858      case '#': break;
859      case ' ': break;
860      case 'i': /* singular.hlp */
861      case 'x': /* singular.idx */
862      case 'h': /* html dir */
863               if (feResource(*p, warn) == NULL)
864               {
865                 if (warn) Warn("ressource `%c` not found",*p);
866                 return FALSE;
867               }
868               break;
869      case 'D': /* DISPLAY */
870               if (getenv("DISPLAY") == NULL)
871               {
872                 if (warn) WarnS("ressource `D` not found");
873                 return FALSE;
874               }
875               break;
876      case 'E': /* executable: E:xterm: */
877               {
878                 char name[128];
879                 char exec[128];
880                 memset(name,0,128);
881                 int i=0;
882                 p++;
883                 while (((*p==':')||(*p<=' ')) && (*p!='\0')) p++;
884                 while((i<127) && (*p>' ') && (*p!=':'))
885                 {
886                   name[i]=*p; p++; i++;
887                 }
888                 if (i==0) return FALSE;
889                 if (omFindExec(name,exec)==NULL)
890                 {
891                   if (warn) Warn("executable `%s` not found",name);
892                   return FALSE;
893                 }
894               }
895               break;
896      default: Warn("unknown char %c",*p);
897               break;
898    }
899    p++;
900  }
901  return TRUE;
902}
903#ifdef ix86_Win
904static void heHtmlHelp(heEntry hentry, int br)
905{
906  char url[MAXPATHLEN];
907  char* html_dir = feResource('h' /*"HtmlDir"*/);
908  sprintf(url, "%s/%s",
909          (html_dir != NULL ? html_dir : feResource('u' /*"ManualUrl"*/)),
910          (hentry!=NULL && *(hentry->url)!='\0' ? hentry->url : "index.htm"));
911
912  heOpenWinntUrl(url, (html_dir != NULL ? 1 : 0));
913}
914
915static void heWinHelp(heEntry hentry, int br)
916{
917  char keyw[MAX_HE_ENTRY_LENGTH];
918  if ((hentry!=NULL)&&(hentry->key!=NULL))
919    strcpy(keyw,hentry->key);
920  else
921    strcpy(keyw," ");
922  char* helppath = feResource('h' /*"HtmlDir"*/);
923  const char *filename="/Manual.hlp";
924  int helppath_len=0;
925  if (helppath!=NULL) helppath_len=strlen(helppath);
926  char *callpath=(char *)omAlloc0(helppath_len+strlen(filename)+1);
927  if ((helppath!=NULL) && (*helppath>' '))
928    strcpy(callpath,helppath);
929  strcat(callpath,filename);
930  heOpenWinntHlp(keyw,callpath);
931  omfree(callpath);
932}
933#endif
934static void heGenHelp(heEntry hentry, int br)
935{
936  char sys[MAX_SYSCMD_LEN];
937  char url[MAXPATHLEN];
938  char *p=heHelpBrowsers[br].action;
939  if (p==NULL) {PrintS("no action ?\n"); return;}
940  memset(sys,0,MAX_SYSCMD_LEN);
941  int i=0;
942  while ((*p>'\0')&& (i<MAX_SYSCMD_LEN))
943  {
944    if ((*p)=='%')
945    {
946      p++;
947      switch (*p)
948      {
949        case 'f': /* local html:file */
950        case 'h': /* local html:URL */
951        case 'H': /* www html */
952                 {
953                   char temp[256];
954                   char *htmldir = feResource('h' /*"HtmlDir"*/);
955                   if ((*p=='h')&&(htmldir!=NULL))
956                     strcat(sys,"file://localhost");
957                   else if ((*p=='H')||(htmldir==NULL))
958                     htmldir = feResource('u' /* %H -> "ManualUrl"*/);
959                     /* always defined */
960                   if (hentry != NULL && *(hentry->url) != '\0')
961                   #ifdef HAVE_VSNPRINTF
962                     snprintf(temp,256,"%s/%s", htmldir, hentry->url);
963                   else
964                     snprintf(temp,256,"%s/index.htm", htmldir);
965                   #else
966                     sprintf(temp,"%s/%s", htmldir, hentry->url);
967                   else
968                     sprintf(temp,"%s/index.htm", htmldir);
969                   #endif
970                   strcat(sys,temp);
971                   if ((*p)=='f')
972                   { // remove #SEC
973                     char *pp=strchr(sys,'#');
974                     if (pp!=NULL)
975                     {
976                       *pp='\0';
977                       i=strlen(sys);
978                       memset(pp,0,MAX_SYSCMD_LEN-i);
979                     }
980                   }
981                   i=strlen(sys);
982                   break;
983                 }
984        case 'i': /* singular.hlp */
985                 {
986                   char *i_res=feResource('i');
987                   if (i_res!=NULL) strcat(sys,i_res);
988                   else
989                   {
990                     WarnS("singular.hlp not found");
991                     return;
992                   }
993                   i=strlen(sys);
994                   break;
995                 }
996        case 'n': /* info node */
997                 {
998                   char temp[256];
999                   if ((hentry!=NULL) && (*(hentry->node) != '\0'))
1000                     sprintf(temp,"%s",hentry->node);
1001                   //else if ((hentry!=NULL) && (hentry->key!=NULL))
1002                   //  sprintf(temp,"Index '%s'",hentry->key);
1003                   else
1004                     sprintf(temp,"Top");
1005                   strcat(sys,temp);
1006                   i=strlen(sys);
1007                   break;
1008                 }
1009        default: break;
1010      }
1011      p++;
1012    }
1013    else
1014    {
1015      sys[i]=*p;
1016      p++;i++;
1017    }
1018  }
1019  Print("running `%s`\n",sys);
1020  system(sys);
1021}
1022
1023#ifdef ix86_Win
1024static void heHtmlHelp(heEntry hentry)
1025{
1026  char url[MAXPATHLEN];
1027  char* html_dir = feResource('h' /*"HtmlDir"*/);
1028  sprintf(url, "%s/%s",
1029          (html_dir != NULL ? html_dir : feResource('u' /*"ManualUrl"*/)),
1030          (hentry!=NULL && *(hentry->url)!='\0' ? hentry->url : "index.htm"));
1031
1032  heOpenWinntUrl(url, (html_dir != NULL ? 1 : 0));
1033}
1034
1035static void heWinHelp(heEntry hentry)
1036{
1037  char keyw[MAX_HE_ENTRY_LENGTH];
1038  if ((hentry!=NULL)&&(hentry->key!=NULL))
1039    strcpy(keyw,hentry->key);
1040  else
1041    strcpy(keyw," ");
1042  char* helppath = feResource('h' /*"HtmlDir"*/);
1043  const char *filename="/Manual.hlp";
1044  int helppath_len=0;
1045  if (helppath!=NULL) helppath_len=strlen(helppath);
1046  char *callpath=(char *)omAlloc0(helppath_len+strlen(filename)+1);
1047  if ((helppath!=NULL) && (*helppath>' '))
1048    strcpy(callpath,helppath);
1049  strcat(callpath,filename);
1050  heOpenWinntHlp(keyw,callpath);
1051  omfree(callpath);
1052}
1053#endif
1054
1055static BOOLEAN heDummyInit(int warn, int br)
1056{
1057  return TRUE;
1058}
1059static void heDummyHelp(heEntry hentry, int br)
1060{
1061  Werror("No functioning help browser available.");
1062}
1063
1064static BOOLEAN heEmacsInit(int warn, int br)
1065{
1066  return TRUE;
1067}
1068static void heEmacsHelp(heEntry hentry, int br)
1069{
1070  WarnS("Your help command could not be executed. Use");
1071  Warn("C-h C-s %s",
1072       (hentry != NULL && *(hentry->node) != '\0' ? hentry->node : "Top"));
1073  Warn("to enter the Singular online help. For general");
1074  Warn("information on Singular running under Emacs, type C-h m.");
1075}
1076static int singular_manual(char *str);
1077static void heBuiltinHelp(heEntry hentry, int br)
1078{
1079  char* node = omStrDup(hentry != NULL && *(hentry->node) != '\0' ?
1080                       hentry->node : "Top");
1081  singular_manual(node);
1082  omFree(node);
1083}
1084
1085
1086/* ========================================================================== */
1087// old, stupid builtin_help
1088// This could be implemented much more clever, but I'm too lazy to do this now
1089//
1090#define HELP_OK        0
1091#define FIN_INDEX    '\037'
1092#define HELP_NOT_OPEN  1
1093#define HELP_NOT_FOUND 2
1094#define BUF_LEN        256
1095#define IDX_LEN        64
1096#define MAX_LINES      21
1097
1098static inline char tolow(char p)
1099{
1100  if (('A'<=p)&&(p<='Z')) return p | 040;
1101  return p;
1102}
1103
1104/*************************************************/
1105static int show(unsigned long offset,FILE *help, char *close)
1106{ char buffer[BUF_LEN+1];
1107  int  lines = 0;
1108
1109  if( help== NULL)
1110    if( (help = fopen(feResource('i'), "rb")) == NULL)
1111      return HELP_NOT_OPEN;
1112
1113  fseek(help,  (long)(offset+1), (int)0);
1114  while( !feof(help)
1115        && *fgets(buffer, BUF_LEN, help) != EOF
1116        && buffer[0] != FIN_INDEX)
1117  {
1118    printf("%s", buffer);
1119    if(lines++> MAX_LINES)
1120    {
1121      printf("\n Press <RETURN> to continue or x to exit help.\n");
1122      fflush(stdout);
1123      *close = (char)getchar();
1124      if(*close=='x')
1125      {
1126        getchar();
1127        break;
1128      }
1129      lines=0;
1130    }
1131  }
1132  if(*close!='x')
1133  {
1134    printf("\nEnd of part. Press <RETURN> to continue or x to exit help.\n");
1135    fflush(stdout);
1136    *close = (char)getchar();
1137    if(*close=='x')
1138      getchar();
1139  }
1140  return HELP_OK;
1141}
1142
1143/*************************************************/
1144static int singular_manual(char *str)
1145{ FILE *index=NULL,*help=NULL;
1146  unsigned long offset;
1147  char *p,close;
1148  int done = 0;
1149  char buffer[BUF_LEN+1],
1150       Index[IDX_LEN+1],
1151       String[IDX_LEN+1];
1152
1153  if( (index = fopen(feResource('i'), "rb")) == NULL)
1154  {
1155    return HELP_NOT_OPEN;
1156  }
1157
1158  for(p=str; *p; p++) *p = tolow(*p);/* */
1159  do
1160  {
1161    p--;
1162  }
1163  while ((p != str) && (*p<=' '));
1164  p++;
1165  *p='\0';
1166  (void)sprintf(String, " %s ", str);
1167
1168  while(!feof(index)
1169        && fgets(buffer, BUF_LEN, index) != (char *)0
1170        && buffer[0] != FIN_INDEX);
1171
1172  while(!feof(index))
1173  {
1174    (void)fgets(buffer, BUF_LEN, index); /* */
1175    (void)sscanf(buffer, "Node:%[^\177]\177%ld\n", Index, &offset);
1176    for(p=Index; *p; p++) *p = tolow(*p);/* */
1177    (void)strcat(Index, " ");
1178    if( strstr(Index, String)!=NULL)
1179    {
1180      done++; (void)show(offset, help, &close);
1181    }
1182    Index[0]='\0';
1183    if(close=='x')
1184    break;
1185  }
1186  if (index != NULL) (void)fclose(index);
1187  if (help != NULL) (void)fclose(help);
1188  if(! done)
1189  {
1190    Warn("`%s` not found",String);
1191    return HELP_NOT_FOUND;
1192  }
1193  return HELP_OK;
1194}
1195/*************************************************/
Note: See TracBrowser for help on using the repository browser.