#include "info.h"
#include "info-utils.h"
#if defined (HANDLE_MAN_PAGES)
# include "man.h"
#endif
int ISO_Latin_p = 0;
char *info_parsed_filename = (char *)NULL;
char *info_parsed_nodename = (char *)NULL;
static void save_filename (), saven_filename ();
static void save_nodename (), saven_nodename ();
static REFERENCE **info_references_internal ();
void
info_parse_node (string, newlines_okay)
char *string;
int newlines_okay;
{
register int i = 0;
save_filename ((char *)NULL);
save_nodename ((char *)NULL);
if (!string || !*string)
return;
string += skip_whitespace (string);
if (*string == '(')
{
i = 0;
string++;
while (string[i] && string[i] != ')')
i++;
saven_filename (string, i);
string += i;
if (*string)
string++;
}
i = skip_node_characters (string, newlines_okay);
saven_nodename (string, i);
canonicalize_whitespace (info_parsed_nodename);
if (info_parsed_nodename && !*info_parsed_nodename)
{
free (info_parsed_nodename);
info_parsed_nodename = (char *)NULL;
}
}
void
info_parse_label (label, node)
char *label;
NODE *node;
{
register int i;
char *nodeline;
save_nodename ((char *)NULL);
save_filename ((char *)NULL);
nodeline = node->contents;
i = string_in_line (label, nodeline);
if (i == -1)
return;
nodeline += i;
nodeline += skip_whitespace (nodeline);
info_parse_node (nodeline, DONT_SKIP_NEWLINES);
}
REFERENCE **
info_menu_of_node (node)
NODE *node;
{
long position;
SEARCH_BINDING search;
REFERENCE **menu = (REFERENCE **)NULL;
search.buffer = node->contents;
search.start = 0;
search.end = node->nodelen;
search.flags = S_FoldCase;
position = search_forward (INFO_MENU_LABEL, &search);
if (position == -1)
return ((REFERENCE **) NULL);
search.start = position + strlen (INFO_MENU_LABEL);
search.start += skip_line (search.buffer + search.start);
search.start--;
menu = info_menu_items (&search);
return (menu);
}
REFERENCE **
info_xrefs_of_node (node)
NODE *node;
{
SEARCH_BINDING search;
#if defined (HANDLE_MAN_PAGES)
if (node->flags & N_IsManPage)
return (xrefs_of_manpage (node));
#endif
search.buffer = node->contents;
search.start = 0;
search.end = node->nodelen;
search.flags = S_FoldCase;
return (info_xrefs (&search));
}
REFERENCE **
info_menu_items (binding)
SEARCH_BINDING *binding;
{
return (info_references_internal (INFO_MENU_ENTRY_LABEL, binding));
}
REFERENCE **
info_xrefs (binding)
SEARCH_BINDING *binding;
{
return (info_references_internal (INFO_XREF_LABEL, binding));
}
static REFERENCE **
info_references_internal (label, binding)
char *label;
SEARCH_BINDING *binding;
{
SEARCH_BINDING search;
REFERENCE **refs = (REFERENCE **)NULL;
int refs_index = 0, refs_slots = 0;
int searching_for_menu_items = 0;
long position;
search.buffer = binding->buffer;
search.start = binding->start;
search.end = binding->end;
search.flags = S_FoldCase | S_SkipDest;
searching_for_menu_items = (strcasecmp (label, INFO_MENU_ENTRY_LABEL) == 0);
while ((position = search_forward (label, &search)) != -1)
{
int offset, start;
char *refdef;
REFERENCE *entry;
search.start = position;
search.start += skip_whitespace (search.buffer + search.start);
start = search.start - binding->start;
refdef = search.buffer + search.start;
offset = string_in_line (":", refdef);
if (offset == -1)
{
if (searching_for_menu_items)
continue;
else
{
int temp;
temp = skip_line (refdef);
offset = string_in_line (":", refdef + temp);
if (offset == -1)
continue;
else
offset += temp;
}
}
entry = (REFERENCE *)xmalloc (sizeof (REFERENCE));
entry->filename = (char *)NULL;
entry->nodename = (char *)NULL;
entry->label = (char *)xmalloc (offset);
strncpy (entry->label, refdef, offset - 1);
entry->label[offset - 1] = '\0';
canonicalize_whitespace (entry->label);
refdef += offset;
entry->start = start;
entry->end = refdef - binding->buffer;
if (*refdef == ':')
{
entry->nodename = xstrdup (entry->label);
}
else
{
refdef += skip_whitespace_and_newlines (refdef);
if (searching_for_menu_items)
info_parse_node (refdef, DONT_SKIP_NEWLINES);
else
info_parse_node (refdef, SKIP_NEWLINES);
if (info_parsed_filename)
entry->filename = xstrdup (info_parsed_filename);
if (info_parsed_nodename)
entry->nodename = xstrdup (info_parsed_nodename);
}
add_pointer_to_array
(entry, refs_index, refs, refs_slots, 50, REFERENCE *);
}
return (refs);
}
REFERENCE *
info_get_labeled_reference (label, references)
char *label;
REFERENCE **references;
{
register int i;
REFERENCE *entry;
for (i = 0; references && (entry = references[i]); i++)
{
if (strcmp (label, entry->label) == 0)
return (entry);
}
return ((REFERENCE *)NULL);
}
REFERENCE **
info_concatenate_references (ref1, ref2)
REFERENCE **ref1, **ref2;
{
register int i, j;
REFERENCE **result;
int size;
if (!ref1)
return (ref2);
else if (!ref2)
return (ref1);
for (i = 0; ref1[i]; i++);
size = i;
for (i = 0; ref2[i]; i++);
size += i;
result = (REFERENCE **)xmalloc ((1 + size) * sizeof (REFERENCE *));
for (i = 0; ref1[i]; i++)
result[i] = ref1[i];
j = i;
for (i = 0; ref2[i]; i++)
result[j++] = ref2[i];
result[j] = (REFERENCE *)NULL;
free (ref1);
free (ref2);
return (result);
}
void
info_free_references (references)
REFERENCE **references;
{
register int i;
REFERENCE *entry;
if (references)
{
for (i = 0; references && (entry = references[i]); i++)
{
maybe_free (entry->label);
maybe_free (entry->filename);
maybe_free (entry->nodename);
free (entry);
}
free (references);
}
}
void
canonicalize_whitespace (string)
char *string;
{
register int i, j;
int len, whitespace_found, whitespace_loc;
char *temp;
if (!string)
return;
len = strlen (string);
temp = (char *)xmalloc (1 + len);
whitespace_found = 0;
for (i = 0, j = 0; string[i]; i++)
{
if (whitespace_or_newline (string[i]))
{
whitespace_found++;
whitespace_loc = i;
continue;
}
else
{
if (whitespace_found && whitespace_loc)
{
whitespace_found = 0;
if (j)
temp[j++] = ' ';
}
temp[j++] = string[i];
}
}
if (j && whitespace (temp[j - 1]))
j--;
temp[j] = '\0';
strcpy (string, temp);
free (temp);
}
static char the_rep[10];
char *
printed_representation (character, hpos)
unsigned char character;
int hpos;
{
register int i = 0;
int printable_limit;
if (ISO_Latin_p)
printable_limit = 160;
else
printable_limit = 127;
if (character == '\177')
{
the_rep[i++] = '^';
the_rep[i++] = '?';
}
else if (iscntrl (character))
{
switch (character)
{
case '\r':
case '\n':
the_rep[i++] = character;
break;
case '\t':
{
int tw;
tw = ((hpos + 8) & 0xf8) - hpos;
while (i < tw)
the_rep[i++] = ' ';
}
break;
default:
the_rep[i++] = '^';
the_rep[i++] = (character | 0x40);
}
}
else if (character > printable_limit)
{
sprintf (the_rep + i, "\\%0o", character);
i = strlen (the_rep);
}
else
the_rep[i++] = character;
the_rep[i] = '\0';
return (the_rep);
}
static int parsed_filename_size = 0;
static int parsed_nodename_size = 0;
static void save_string (), saven_string ();
static void
save_filename (filename)
char *filename;
{
save_string (filename, &info_parsed_filename, &parsed_filename_size);
}
static void
saven_filename (filename, len)
char *filename;
int len;
{
saven_string (filename, len,
&info_parsed_filename, &parsed_filename_size);
}
static void
save_nodename (nodename)
char *nodename;
{
save_string (nodename, &info_parsed_nodename, &parsed_nodename_size);
}
static void
saven_nodename (nodename, len)
char *nodename;
int len;
{
saven_string (nodename, len,
&info_parsed_nodename, &parsed_nodename_size);
}
static void
save_string (string, string_p, string_size_p)
char *string;
char **string_p;
int *string_size_p;
{
if (!string || !*string)
{
if (*string_p)
free (*string_p);
*string_p = (char *)NULL;
*string_size_p = 0;
}
else
{
if (strlen (string) >= *string_size_p)
*string_p = (char *)xrealloc
(*string_p, (*string_size_p = 1 + strlen (string)));
strcpy (*string_p, string);
}
}
static void
saven_string (string, len, string_p, string_size_p)
char *string;
int len;
char **string_p;
int *string_size_p;
{
if (!string)
{
if (*string_p)
free (*string_p);
*string_p = (char *)NULL;
*string_size_p = 0;
}
else
{
if (len >= *string_size_p)
*string_p = (char *)xrealloc (*string_p, (*string_size_p = 1 + len));
strncpy (*string_p, string, len);
(*string_p)[len] = '\0';
}
}
char *
filename_non_directory (pathname)
char *pathname;
{
char *filename;
filename = (char *) strrchr (pathname, '/');
if (filename)
filename++;
else
filename = pathname;
return (filename);
}
int
internal_info_node_p (node)
NODE *node;
{
#if defined (NEVER)
if (node &&
(node->filename && !*node->filename) &&
!node->parent && node->nodename)
return (1);
else
return (0);
#else
return ((node != (NODE *)NULL) && ((node->flags & N_IsInternal) != 0));
#endif
}
void
name_internal_node (node, name)
NODE *node;
char *name;
{
if (!node)
return;
node->filename = "";
node->parent = (char *)NULL;
node->nodename = name;
node->flags |= N_IsInternal;
}
WINDOW *
get_internal_info_window (name)
char *name;
{
WINDOW *win;
for (win = windows; win; win = win->next)
if (internal_info_node_p (win->node) &&
(strcmp (win->node->nodename, name) == 0))
break;
return (win);
}