source: git/Singular/fehelp.cc @ 23f2f57

fieker-DuValspielwiese
Last change on this file since 23f2f57 was 4cbc5c, checked in by Hans Schönemann <hannes@…>, 19 years ago
hannes: list wrong index git-svn-id: file:///usr/local/Singular/svn/trunk@7870 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 30.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: help system
6* versin $Id: fehelp.cc,v 1.40 2005-04-22 15:14:02 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(5*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]!='\0')
613    {
614      index_key[MAX_HE_ENTRY_LENGTH]='\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// try to find the help string as a loaded procedure or library
640// if found, display the help and return TRUE
641// otherwise, return FALSE
642static BOOLEAN heOnlineHelp(char* s)
643{
644#ifdef HAVE_NAMESPACES
645  idhdl h, ns;
646  iiname2hdl(s, &ns, &h);
647#else /* HAVE_NAMESPACES */
648#ifdef HAVE_NS
649  idhdl h=IDROOT->get(s,myynest);
650#else
651  idhdl h=idroot->get(s,myynest);
652#endif /* HAVE_NS */
653#endif /* HAVE_NAMESPACES */
654
655  // try help for a procedure
656  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
657  {
658    char *lib=iiGetLibName(IDPROC(h));
659    if((lib!=NULL)&&(*lib!='\0'))
660    {
661      Print("// proc %s from lib %s\n",s,lib);
662      s=iiGetLibProcBuffer(IDPROC(h), 0);
663      if (s!=NULL)
664      {
665        PrintS(s);
666        omFree((ADDRESS)s);
667      }
668      return TRUE;
669    }
670    return FALSE;
671  }
672
673  // try help for a library
674  int ls = strlen(s);
675  char* str = NULL;
676  // check that it ends with "[.,_]lib"
677  if (strlen(s) >=4 &&  strcmp(&s[ls-3], "lib") == 0)
678  {
679    if (s[ls - 4] == '.') str = s;
680    else
681    {
682      str = omStrDup(s);
683      str[ls - 4] = '.';
684    }
685  }
686  else
687  {
688    return FALSE;
689  }
690
691  char libnamebuf[128];
692  FILE *fp=NULL;
693  // first, search for library of that name in LIB string
694  if ((str[1]!='\0') &&
695      ((iiLocateLib(str, libnamebuf) && (fp=feFopen(libnamebuf, "rb")) !=NULL)
696       ||
697       ((fp=feFopen(str,"rb", libnamebuf))!=NULL)))
698  {
699    extern FILE *yylpin;
700    lib_style_types lib_style; // = OLD_LIBSTYLE;
701
702    yylpin = fp;
703#ifdef HAVE_NAMESPACES
704    yylplex(str, libnamebuf, &lib_style, IDROOT, FALSE, GET_INFO);
705#else /* HAVE_NAMESPACES */
706#ifdef HAVE_NS
707    yylplex(str, libnamebuf, &lib_style, IDROOT, FALSE, GET_INFO);
708#else
709    yylplex(str, libnamebuf, &lib_style, GET_INFO);
710#endif /* HAVE_NS */
711#endif /* HAVE_NAMESPACES */
712    reinit_yylp();
713    if(lib_style == OLD_LIBSTYLE)
714    {
715      char buf[256];
716      fseek(fp, 0, SEEK_SET);
717      Warn( "library %s has an old format. Please fix it for the next time",
718            str);
719      if (str != s) omFree(str);
720      BOOLEAN found=FALSE;
721      while (fgets( buf, sizeof(buf), fp))
722      {
723        if (strncmp(buf,"//",2)==0)
724        {
725          if (found) return TRUE;
726        }
727        else if ((strncmp(buf,"proc ",5)==0)||(strncmp(buf,"LIB ",4)==0))
728        {
729          if (!found) WarnS("no help part in library found");
730          return TRUE;
731        }
732        else
733        {
734          found=TRUE;
735          PrintS(buf);
736        }
737      }
738    }
739    else
740    {
741      if (str != s) omFree(str);
742      fclose( yylpin );
743      PrintS(text_buffer);
744      omFree(text_buffer);
745      text_buffer=NULL;
746    }
747    return TRUE;
748  }
749
750  if (str != s) omFree(str);
751  return FALSE;
752}
753
754static long heKeyChksum(char* key)
755{
756  if (key == NULL || *key == '\0') return 0;
757#ifdef HAVE_NAMESPACES
758  idhdl h, ns;
759  iiname2hdl(key, &ns, &h);
760#else /* HAVE_NAMESPACES */
761#ifdef HAVE_NS
762  idhdl h=IDROOT->get(key,myynest);
763#else
764  idhdl h=idroot->get(key,myynest);
765#endif /* HAVE_NS */
766#endif /* HAVE_NAMESPACES */
767  if ((h!=NULL) && (IDTYP(h)==PROC_CMD))
768  {
769    procinfo *pi = IDPROC(h);
770    if (pi != NULL) return pi->data.s.help_chksum;
771  }
772  return 0;
773}
774
775/*****************************************************************
776 *
777 * Implementation : Help Browsers
778 *
779 *****************************************************************/
780
781static BOOLEAN feHelpCalled = FALSE;
782
783static void heBrowserHelp(heEntry hentry)
784{
785  // check checksums of procs
786  int kchksum = (hentry != NULL && hentry->chksum > 0 ?
787                 heKeyChksum(hentry->key) : 0);
788  if (kchksum  && kchksum != hentry->chksum && heOnlineHelp(hentry->key))
789    return;
790
791  if (heCurrentHelpBrowser == NULL) feHelpBrowser(NULL, 0);
792  assume(heCurrentHelpBrowser != NULL);
793  if (! feHelpCalled)
794  {
795    Warn("Displaying help in browser '%s'.", heCurrentHelpBrowser->browser);
796    if (strcmp(heCurrentHelpBrowser->browser, "netscape") == 0 &&
797        feResource('h', 0) == NULL)
798    {
799      Warn("Using URL '%s'.", feResource('u', 0));
800    }
801    Warn("Use 'system(\"--browser\", <browser>);' to change browser,");
802    char* browsers = StringSetS("where <browser> can be: ");
803    int i = 0;
804    i = 0;
805    while (heHelpBrowsers[i].browser != NULL)
806    {
807      if (heHelpBrowsers[i].init_proc(0,i))
808        StringAppend("\"%s\", ", heHelpBrowsers[i].browser);
809      i++;
810    }
811    if (browsers[strlen(browsers)-2] == ',')
812    {
813      browsers[strlen(browsers)-2] = '.';
814      browsers[strlen(browsers)-1] = '\0';
815    }
816    WarnS(browsers);
817  }
818
819  heCurrentHelpBrowser->help_proc(hentry, heCurrentHelpBrowserIndex);
820  feHelpCalled = TRUE;
821}
822
823#define MAX_SYSCMD_LEN MAXPATHLEN*2
824static BOOLEAN heGenInit(int warn, int br)
825{
826  if (heHelpBrowsers[br].required==NULL) return TRUE;
827  char *p=heHelpBrowsers[br].required;
828  while (*p>'\0')
829  {
830    switch (*p)
831    {
832      case '#': break;
833      case ' ': break;
834      case 'i': /* singular.hlp */
835      case 'x': /* singular.idx */
836      case 'h': /* html dir */
837               if (feResource(*p, warn) == NULL)
838               {
839                 if (warn) Warn("ressource `%c` not found",*p);
840                 return FALSE;
841               }
842               break;
843      case 'D': /* DISPLAY */
844               if (getenv("DISPLAY") == NULL)
845               {
846                 if (warn) WarnS("ressource `D` not found");
847                 return FALSE;
848               }
849               break;
850      case 'E': /* executable: E:xterm: */
851               {
852                 char name[128];
853                 char exec[128];
854                 memset(name,0,128);
855                 int i=0;
856                 p++;
857                 while (((*p==':')||(*p<=' ')) && (*p!='\0')) p++;
858                 while((i<127) && (*p>' ') && (*p!=':'))
859                 {
860                   name[i]=*p; p++; i++;
861                 }
862                 if (i==0) return FALSE;
863                 if (omFindExec(name,exec)==NULL)
864                 {
865                   if (warn) Warn("executable `%s` not found",name);
866                   return FALSE;
867                 }
868               }
869               break;
870      default: Warn("unknown char %c",*p);
871               break;
872    }
873    p++;
874  }
875  return TRUE;
876}
877#ifdef ix86_Win
878static void heHtmlHelp(heEntry hentry, int br)
879{
880  char url[MAXPATHLEN];
881  char* html_dir = feResource('h' /*"HtmlDir"*/);
882  sprintf(url, "%s/%s",
883          (html_dir != NULL ? html_dir : feResource('u' /*"ManualUrl"*/)),
884          (hentry!=NULL && *(hentry->url)!='\0' ? hentry->url : "index.htm"));
885
886  heOpenWinntUrl(url, (html_dir != NULL ? 1 : 0));
887}
888
889static void heWinHelp(heEntry hentry, int br)
890{
891  char keyw[MAX_HE_ENTRY_LENGTH];
892  if ((hentry!=NULL)&&(hentry->key!=NULL))
893    strcpy(keyw,hentry->key);
894  else
895    strcpy(keyw," ");
896  char* helppath = feResource('h' /*"HtmlDir"*/);
897  const char *filename="/Manual.hlp";
898  int helppath_len=0;
899  if (helppath!=NULL) helppath_len=strlen(helppath);
900  char *callpath=(char *)omAlloc0(helppath_len+strlen(filename)+1);
901  if ((helppath!=NULL) && (*helppath>' '))
902    strcpy(callpath,helppath);
903  strcat(callpath,filename);
904  heOpenWinntHlp(keyw,callpath);
905  omfree(callpath);
906}
907#endif
908static void heGenHelp(heEntry hentry, int br)
909{
910  char sys[MAX_SYSCMD_LEN];
911  char url[MAXPATHLEN];
912  char *p=heHelpBrowsers[br].action;
913  if (p==NULL) {PrintS("no action ?\n"); return;}
914  memset(sys,0,MAX_SYSCMD_LEN);
915  int i=0;
916  while ((*p>'\0')&& (i<MAX_SYSCMD_LEN))
917  {
918    if ((*p)=='%')
919    {
920      p++;
921      switch (*p)
922      {
923        case 'f': /* local html:file */
924        case 'h': /* local html:URL */
925        case 'H': /* www html */
926                 {
927                   char temp[256];
928                   char *htmldir = feResource('h' /*"HtmlDir"*/);
929                   if ((*p=='h')&&(htmldir!=NULL))
930                     strcat(sys,"file://localhost");
931                   else if ((*p=='H')||(htmldir==NULL))
932                     htmldir = feResource('u' /* %H -> "ManualUrl"*/);
933                     /* always defined */
934                   if (hentry != NULL && *(hentry->url) != '\0')
935                   #ifdef HAVE_VSNPRINTF
936                     snprintf(temp,256,"%s/%s", htmldir, hentry->url);
937                   else
938                     snprintf(temp,256,"%s/index.htm", htmldir);
939                   #else
940                     sprintf(temp,"%s/%s", htmldir, hentry->url);
941                   else
942                     sprintf(temp,"%s/index.htm", htmldir);
943                   #endif
944                   strcat(sys,temp);
945                   if ((*p)=='f')
946                   { // remove #SEC
947                     char *pp=strchr(sys,'#');
948                     if (pp!=NULL)
949                     { 
950                       *pp='\0';
951                       i=strlen(sys);
952                       memset(pp,0,MAX_SYSCMD_LEN-i);
953                     } 
954                   }
955                   i=strlen(sys);
956                   break;
957                 }
958        case 'i': /* singular.hlp */
959                 {
960                   char *i_res=feResource('i');
961                   if (i_res!=NULL) strcat(sys,i_res);
962                   else
963                   {
964                     WarnS("singular.hlp not found");
965                     return;
966                   }
967                   i=strlen(sys);
968                   break;
969                 }
970        case 'n': /* info node */
971                 {
972                   char temp[256];
973                   if ((hentry!=NULL) && (*(hentry->node) != '\0'))
974                     sprintf(temp,"%s",hentry->node);
975                   //else if ((hentry!=NULL) && (hentry->key!=NULL))
976                   //  sprintf(temp,"Index '%s'",hentry->key);
977                   else
978                     sprintf(temp,"Top");
979                   strcat(sys,temp);
980                   i=strlen(sys);
981                   break;
982                 }
983        default: break;
984      }
985      p++;
986    }
987    else
988    {
989      sys[i]=*p;
990      p++;i++;
991    }
992  }
993  Print("running `%s`\n",sys);
994  system(sys);
995}
996
997#ifdef ix86_Win
998static void heHtmlHelp(heEntry hentry)
999{
1000  char url[MAXPATHLEN];
1001  char* html_dir = feResource('h' /*"HtmlDir"*/);
1002  sprintf(url, "%s/%s",
1003          (html_dir != NULL ? html_dir : feResource('u' /*"ManualUrl"*/)),
1004          (hentry!=NULL && *(hentry->url)!='\0' ? hentry->url : "index.htm"));
1005
1006  heOpenWinntUrl(url, (html_dir != NULL ? 1 : 0));
1007}
1008
1009static void heWinHelp(heEntry hentry)
1010{
1011  char keyw[MAX_HE_ENTRY_LENGTH];
1012  if ((hentry!=NULL)&&(hentry->key!=NULL))
1013    strcpy(keyw,hentry->key);
1014  else
1015    strcpy(keyw," ");
1016  char* helppath = feResource('h' /*"HtmlDir"*/);
1017  const char *filename="/Manual.hlp";
1018  int helppath_len=0;
1019  if (helppath!=NULL) helppath_len=strlen(helppath);
1020  char *callpath=(char *)omAlloc0(helppath_len+strlen(filename)+1);
1021  if ((helppath!=NULL) && (*helppath>' '))
1022    strcpy(callpath,helppath);
1023  strcat(callpath,filename);
1024  heOpenWinntHlp(keyw,callpath);
1025  omfree(callpath);
1026}
1027#endif
1028
1029static BOOLEAN heDummyInit(int warn, int br)
1030{
1031  return TRUE;
1032}
1033static void heDummyHelp(heEntry hentry, int br)
1034{
1035  Werror("No functioning help browser available.");
1036}
1037
1038static BOOLEAN heEmacsInit(int warn, int br)
1039{
1040  return TRUE;
1041}
1042static void heEmacsHelp(heEntry hentry, int br)
1043{
1044  WarnS("Your help command could not be executed. Use");
1045  Warn("C-h C-s %s",
1046       (hentry != NULL && *(hentry->node) != '\0' ? hentry->node : "Top"));
1047  Warn("to enter the Singular online help. For general");
1048  Warn("information on Singular running under Emacs, type C-h m.");
1049}
1050static int singular_manual(char *str);
1051static void heBuiltinHelp(heEntry hentry, int br)
1052{
1053  char* node = omStrDup(hentry != NULL && *(hentry->node) != '\0' ?
1054                       hentry->node : "Top");
1055  singular_manual(node);
1056  omFree(node);
1057}
1058
1059
1060/* ========================================================================== */
1061// old, stupid builtin_help
1062// This could be implemented much more clever, but I'm too lazy to do this now
1063//
1064#define HELP_OK        0
1065#define FIN_INDEX    '\037'
1066#define HELP_NOT_OPEN  1
1067#define HELP_NOT_FOUND 2
1068#define BUF_LEN        256
1069#define IDX_LEN        64
1070#define MAX_LINES      21
1071
1072static inline char tolow(char p)
1073{
1074  if (('A'<=p)&&(p<='Z')) return p | 040;
1075  return p;
1076}
1077
1078/*************************************************/
1079static int show(unsigned long offset,FILE *help, char *close)
1080{ char buffer[BUF_LEN+1];
1081  int  lines = 0;
1082
1083  if( help== NULL)
1084    if( (help = fopen(feResource('i'), "rb")) == NULL)
1085      return HELP_NOT_OPEN;
1086
1087  fseek(help,  (long)(offset+1), (int)0);
1088  while( !feof(help)
1089        && *fgets(buffer, BUF_LEN, help) != EOF
1090        && buffer[0] != FIN_INDEX)
1091  {
1092    printf("%s", buffer);
1093    if(lines++> MAX_LINES)
1094    {
1095      printf("\n Press <RETURN> to continue or x to exit help.\n");
1096      fflush(stdout);
1097      *close = (char)getchar();
1098      if(*close=='x')
1099      {
1100        getchar();
1101        break;
1102      }
1103      lines=0;
1104    }
1105  }
1106  if(*close!='x')
1107  {
1108    printf("\nEnd of part. Press <RETURN> to continue or x to exit help.\n");
1109    fflush(stdout);
1110    *close = (char)getchar();
1111    if(*close=='x')
1112      getchar();
1113  }
1114  return HELP_OK;
1115}
1116
1117/*************************************************/
1118static int singular_manual(char *str)
1119{ FILE *index=NULL,*help=NULL;
1120  unsigned long offset;
1121  char *p,close;
1122  int done = 0;
1123  char buffer[BUF_LEN+1],
1124       Index[IDX_LEN+1],
1125       String[IDX_LEN+1];
1126
1127  if( (index = fopen(feResource('i'), "rb")) == NULL)
1128  {
1129    return HELP_NOT_OPEN;
1130  }
1131
1132  for(p=str; *p; p++) *p = tolow(*p);/* */
1133  do
1134  {
1135    p--;
1136  }
1137  while ((p != str) && (*p<=' '));
1138  p++;
1139  *p='\0';
1140  (void)sprintf(String, " %s ", str);
1141
1142  while(!feof(index)
1143        && fgets(buffer, BUF_LEN, index) != (char *)0
1144        && buffer[0] != FIN_INDEX);
1145
1146  while(!feof(index))
1147  {
1148    (void)fgets(buffer, BUF_LEN, index); /* */
1149    (void)sscanf(buffer, "Node:%[^\177]\177%ld\n", Index, &offset);
1150    for(p=Index; *p; p++) *p = tolow(*p);/* */
1151    (void)strcat(Index, " ");
1152    if( strstr(Index, String)!=NULL)
1153    {
1154      done++; (void)show(offset, help, &close);
1155    }
1156    Index[0]='\0';
1157    if(close=='x')
1158    break;
1159  }
1160  if (index != NULL) (void)fclose(index);
1161  if (help != NULL) (void)fclose(help);
1162  if(! done)
1163  {
1164    Warn("`%s` not found",String);
1165    return HELP_NOT_FOUND;
1166  }
1167  return HELP_OK;
1168}
1169/*************************************************/
Note: See TracBrowser for help on using the repository browser.