#include #include #include #include #include #include #include #include #include "gnome-helpwin.h" #include "toc2.h" #include "toc2-man.h" #include "toc2-ghelp.h" #include "toc2-info.h" static struct _toc_config *toc_config; struct _toc { GList *manTable; GList *infoTable; GList *ghelpTable; }; static struct _toc_config *addToConfig(struct _toc_config *index, gchar *paths, gint type); static gint countChars(gchar *s, gchar ch); static void buildTocConfig(gchar *manPath, gchar *infoPath, gchar *ghelpPath); static GList *findFirstEntryByName(GList *table, gchar *name); static gint countChars(gchar *s, gchar ch) { gint count = 0; while (s && *s) { if (*s == ch) count++; s++; } return count; } static struct _toc_config *addToConfig(struct _toc_config *index, gchar *paths, gint type) { gchar buf[BUFSIZ]; gchar *rest, *s; if (!paths) { return index; } strncpy(buf, paths, sizeof(buf)); rest = buf; while ((s = strtok(rest, ":"))) { rest = NULL; index->path = g_strdup(s); index->type = type; index++; } return index; } static void buildTocConfig(gchar *manPath, gchar *infoPath, gchar *ghelpPath) { gint count; struct _toc_config *index; count = 4; count += countChars(manPath, ':'); count += countChars(infoPath, ':'); count += countChars(ghelpPath, ':'); toc_config = g_malloc(count * sizeof(*toc_config)); index = toc_config; index = addToConfig(index, manPath, TOC_MAN_TYPE); index = addToConfig(index, infoPath, TOC_INFO_TYPE); index = addToConfig(index, ghelpPath, TOC_GHELP_TYPE); index->path = NULL; index->type = 0; } Toc newToc(gchar *manPath, gchar *infoPath, gchar *ghelpPath) { Toc res; buildTocConfig(manPath, infoPath, ghelpPath); res = g_malloc(sizeof(*res)); res->manTable = newManTable(toc_config); res->ghelpTable = newGhelpTable(toc_config); res->infoTable = newInfoTable(toc_config); return res; } GString *genManTocHTML(Toc toc) { GString *res, *s, *tablestart=NULL; GList *l; gchar *name; gchar *link; gchar ext, last_ext, last_initial; gint numcol, numrow; res = g_string_new(_("

Table of Contents

\n")); /* Man Pages */ g_string_append(res, _("

Man Pages

\n")); last_ext = ' '; last_initial = ' '; l = toc->manTable; g_string_append(res, "\n"); last_ext = ' '; last_initial = ' '; l = toc->manTable; numrow = 0; numcol = 3; #define ONE_HONKING_TABLE #ifdef ONE_HONKING_TABLE tablestart = g_string_new(NULL); g_string_sprintf(tablestart, "\n",numcol); #endif while (l) { name = ((struct _big_table_entry *)l->data)->name; link = ((struct _big_table_entry *)l->data)->filename; ext = ((struct _big_table_entry *)l->data)->ext; if (ext != last_ext) { if (numrow) { for (;numrow < numcol; numrow++) g_string_append(res, "\t\n"); g_string_append(res, "\n\n"); /*#ifndef ONE_HONKING_TABLE */ g_string_append(res, "
 
\n"); /*#endif */ } #if 1 s = g_string_new(NULL); g_string_sprintf(s, "",ext); g_string_append(res, s->str ); g_string_free(s, TRUE); g_string_append(res, "


"); g_string_append(res, getManSection(ext)); g_string_append(res, "

\n\n"); #endif /*#ifndef ONE_HONKING_TABLE */ g_string_append(res, tablestart->str); /*#endif */ numrow = 0; } else if (last_initial != *name) { if (numrow) { for (;numrow < numcol; numrow++) g_string_append(res, "\t \n"); g_string_append(res, "\n"); s = g_string_new(NULL); g_string_sprintf(s, "


\n",numcol); g_string_append(res, s->str); g_string_free(s,TRUE); #ifndef ONE_HONKING_TABLE g_string_append(res,"\n"); g_string_append(res, tablestart->str); #endif } #if 0 g_string_append(res, "

\n"); #endif numrow = 0; } if (numrow == numcol) { g_string_append(res, "\n"); #ifndef ONE_HONKING_TABLE g_string_append(res, "\n"); #endif numrow = 0; } if (numrow == 0) { #ifndef ONE_HONKING_TABLE g_string_append(res, tablestart->str); #endif g_string_append(res, "\n"); } s = g_string_new(NULL); /* XXX should also have mime type info */ g_string_sprintf(s, "\t%s\n", name, ext, name); g_string_append(res, s->str); g_string_free(s, TRUE); numrow++; last_initial = *name; last_ext = ext; l = g_list_next(l); } /*#ifdef ONE_HONKING_TABLE*/ if (numrow) { for (;numrow < numcol; numrow++) g_string_append(res, "\t \n"); g_string_append(res, "\n"); } g_string_append(res, "\n"); if (tablestart) g_string_free(tablestart, TRUE); /*#endif */ #if 0 if (1) { FILE *f; printf("writing /tmp/tocman.html\n"); f = fopen("/tmp/tocman.html", "w"); fwrite(res->str, strlen(res->str), 1, f); fclose(f); } #endif return res; } GString *genInfoTocHTML(Toc toc) { GString *res, *s; GList *l; gchar *name; gchar *link; gint numrow, numcol; res = g_string_new(_("

Table of Contents

\n")); g_string_append(res, _("

Info Pages

\n")); l = toc->infoTable; numrow = 0; numcol = 3; s = g_string_new(NULL); g_string_sprintf(s, "\n",numcol); g_string_append(res, s->str); g_string_free(s,TRUE); while (l) { name = ((struct _big_table_entry *)l->data)->name; link = ((struct _big_table_entry *)l->data)->filename; /* only output 1 entry for each info file */ if (!((struct _big_table_entry *)l->data)->section) { s = g_string_new(NULL); /* XXX should also have mime type info */ if (numrow == numcol) { g_string_append(res, "\n"); numrow = 0; } if (numrow == 0) g_string_append(res, "\n"); g_string_sprintf(s, "\t\n", name, name); g_string_append(res, s->str); g_string_free(s, TRUE); numrow++; } l = g_list_next(l); } if (numrow) { for (;numrow < numcol; numrow++) g_string_append(res, "\t\n"); g_string_append(res, "\n"); } g_string_append(res, "
" "%s 
\n"); #if 0 if (1) { FILE *f; printf("writing /tmp/tocinfo.html\n"); f = fopen("/tmp/tocinfo.html", "w"); fwrite(res->str, strlen(res->str), 1, f); fclose(f); } #endif return res; } GString *genGhelpTocHTML(Toc toc) { GString *res, *s; GList *l; gchar *name; gchar *link; gint numcol, numrow; res = g_string_new(_("

Table of Contents

\n")); g_string_append(res, _("

GNOME Help

\n")); l = toc->ghelpTable; numrow = 0; numcol = 3; s = g_string_new(NULL); g_string_sprintf(s, "\n",numcol); g_string_append(res, s->str); g_string_free(s,TRUE); while (l) { name = ((struct _big_table_entry *)l->data)->name; link = ((struct _big_table_entry *)l->data)->filename; if (numrow == numcol) { g_string_append(res, "\n"); numrow = 0; } if (numrow == 0) g_string_append(res, "\n"); s = g_string_new(NULL); /* XXX should also have mime type info */ g_string_sprintf(s, "\t ", name, name); g_string_append(res, s->str); g_string_free(s, TRUE); l = g_list_next(l); numrow++; } if (numrow) { for (;numrow < numcol; numrow++) g_string_append(res, "\t\n"); g_string_append(res, "\n"); } g_string_append(res, "
" "%s 
\n"); return res; } gchar *tocLookupMan(Toc toc, gchar *name, gchar ext) { GList *p; p = toc->manTable; if (ext != ' ') { while (p && ((struct _big_table_entry *)p->data)->ext != ext) { p = p->next; } } if (!p) { return NULL; } while (p) { if (!strcmp(((struct _big_table_entry *)p->data)->name, name)) { if ((ext == ' ') || (((struct _big_table_entry *)p->data)->ext == ext)) { return ((struct _big_table_entry *)p->data)->filename; } return NULL; } p = p->next; } return NULL; } gchar *tocLookupGhelp(Toc toc, gchar *name) { GList *p; p = findFirstEntryByName(toc->ghelpTable, name); if (!p) { return NULL; } return ((struct _big_table_entry *)p->data)->filename; } gchar *tocLookupInfo(Toc toc, gchar *name, gchar *anchor) { GList *p; p = findFirstEntryByName(toc->infoTable, name); if (!p) { return NULL; } if (! ((struct _big_table_entry *)p->data)->expanded) { g_message("expanding info table: %s", name); if (expandInfoTable(p, name)) { return NULL; } } if (! ((struct _big_table_entry *)p->data)->indirect) { return ((struct _big_table_entry *)p->data)->filename; } /* Yes this is all very inefficient */ while (p) { if (((struct _big_table_entry *)p->data)->section && !strcmp(((struct _big_table_entry *)p->data)->section, anchor)) { /* Make sure we are still looking at name */ if (!strcmp(((struct _big_table_entry *)p->data)->name, name)) { return ((struct _big_table_entry *)p->data)->filename; } } p = p->next; } return NULL; } static GList *findFirstEntryByName(GList *table, gchar *name) { while (table) { if (!strcmp(((struct _big_table_entry *)table->data)->name, name)) { return table; } table = table->next; } return NULL; } /* returns HTML of matches to substr search */ GString *findMatchesBySubstr(Toc toc, gchar *substr) { GString *out; GString *tmp; GList *p; gboolean foundman, foundinfo, foundghelp; out = g_string_new(_("\n\n

Results of the substring search " "for the string ")); g_string_sprintfa(out, ""%s"

\n", substr); /* first do Manual Pages */ foundman = FALSE; tmp = g_string_new(_("

\n
\n

Manual Pages

\n

\n

    \n")); p = toc->manTable; /* if substr = "" then dont search */ while (p && *substr) { gchar *name=((struct _big_table_entry *)p->data)->name; gchar ext=((struct _big_table_entry *)p->data)->ext; if (!strncasecmp(name, substr, strlen(substr))) { g_string_sprintfa(tmp,"
  • %s(%c)\n", name, ext, name, ext); foundman = TRUE; } p = p->next; } g_string_append(tmp, "
\n"); if (foundman) g_string_append(out, tmp->str); g_string_free(tmp, TRUE); /* info pages */ foundinfo = FALSE; tmp = g_string_new(_("\n

\n
\n

GNU Info Pages

\n

\n

    \n")); p = toc->infoTable; while (p && *substr) { gchar *name=((struct _big_table_entry *)p->data)->name; /* only one entry per info file (avoids problem with expanded info) */ if (!((struct _big_table_entry *)p->data)->section && !strncasecmp(name, substr, strlen(substr))) { g_string_sprintfa(tmp,"
  • %s\n", name, name); foundinfo = TRUE; } p = p->next; } g_string_append(tmp, "
\n"); if (foundinfo) g_string_append(out, tmp->str); g_string_free(tmp, TRUE); /* ghelp pages */ foundghelp = FALSE; tmp = g_string_new(_("\n

\n
\n

GNOME Help Pages

\n

\n

    \n")); p = toc->ghelpTable; while (p && *substr) { gchar *name=((struct _big_table_entry *)p->data)->name; /* only one entry per info file (avoids problem with expanded info) */ if (!strncasecmp(name, substr, strlen(substr))) { g_string_sprintfa(tmp,"
  • %s\n", name, name); foundghelp = TRUE; } p = p->next; } g_string_append(tmp, "
\n"); if (foundghelp) g_string_append(out, tmp->str); g_string_free(tmp, TRUE); if (!foundman && !foundinfo && !foundghelp) g_string_append(out, _("
No matches found\n")); g_string_append(out, "\n\n"); #if 0 if (1) { FILE *f=fopen("/tmp/test.html", "w"); fprintf(f, "%s", out->str); fclose(f); } #endif return out; }