summaryrefslogtreecommitdiff
path: root/app/xman/search.c
diff options
context:
space:
mode:
authorMatthieu Herrb <matthieu@cvs.openbsd.org>2013-09-28 16:23:03 +0000
committerMatthieu Herrb <matthieu@cvs.openbsd.org>2013-09-28 16:23:03 +0000
commit7b2099c1d55961dd9fd2cdb5b87d96d0024b25cb (patch)
tree695ff775a32ed99a10fe1ec0b40e4e91b831947f /app/xman/search.c
parent05a6265fa644e632cf39ab6cd8095a6d349f4de7 (diff)
Update to xman 1.1.3
Diffstat (limited to 'app/xman/search.c')
-rw-r--r--app/xman/search.c496
1 files changed, 246 insertions, 250 deletions
diff --git a/app/xman/search.c b/app/xman/search.c
index 362cfe47b..8ea654e52 100644
--- a/app/xman/search.c
+++ b/app/xman/search.c
@@ -1,4 +1,3 @@
-/* $XConsortium: search.c,v 1.21 94/04/17 20:43:58 rws Exp $ */
/*
Copyright (c) 1987, 1988 X Consortium
@@ -28,22 +27,21 @@ other dealings in this Software without prior written authorization
from the X Consortium.
*/
-/* $XFree86: xc/programs/xman/search.c,v 1.5 2001/01/27 17:24:27 herrb Exp $ */
#include "globals.h"
#include "vendor.h"
-/* Map <CR> and control-M to goto begining of file. */
+/* Map <CR> and control-M to goto beginning of file. */
#define SEARCHARGS 10
-static FILE * DoManualSearch(ManpageGlobals *man_globals, char * string);
-static int BEntrySearch(char * string, char ** first, int number);
+static FILE *DoManualSearch(ManpageGlobals *man_globals, char *string);
+static int BEntrySearch(char *string, char **first, int number);
/* Function Name: MakeSearchWidget
* Description: This Function Creates the Search Widget.
- * Arguments: man_globals - the pseudo globas for this manpage.
+ * Arguments: man_globals - the pseudo globals for this manpage.
* w - the widgets parent
* Returns: the search widget.
*/
@@ -51,56 +49,58 @@ static int BEntrySearch(char * string, char ** first, int number);
void
MakeSearchWidget(ManpageGlobals * man_globals, Widget parent)
{
- Widget dialog, command, text, cancel;
- Arg arglist[2];
- Cardinal num_args = 0;
-
- XtSetArg(arglist[0], XtNtransientFor, parent);
- man_globals->search_widget = XtCreatePopupShell(SEARCHNAME,
- transientShellWidgetClass,
- parent,
- arglist, 1);
-
- if (resources.clear_search_string) {
- XtSetArg(arglist[0], XtNvalue, ""); num_args++;
- }
+ Widget dialog, command, text, cancel;
+ Arg arglist[2];
+ Cardinal num_args = 0;
+
+ XtSetArg(arglist[0], XtNtransientFor, parent);
+ man_globals->search_widget = XtCreatePopupShell(SEARCHNAME,
+ transientShellWidgetClass,
+ parent, arglist, 1);
+
+ if (resources.clear_search_string) {
+ XtSetArg(arglist[0], XtNvalue, "");
+ num_args++;
+ }
- dialog = XtCreateManagedWidget(DIALOG, dialogWidgetClass,
- man_globals->search_widget,
- arglist, num_args);
+ dialog = XtCreateManagedWidget(DIALOG, dialogWidgetClass,
+ man_globals->search_widget,
+ arglist, num_args);
- if ( (text = XtNameToWidget(dialog, "value")) == (Widget) NULL)
- PopupWarning(NULL, "Could not find text widget in MakeSearchWidget.");
- else
- XtSetKeyboardFocus(dialog, text);
+ if ((text = XtNameToWidget(dialog, "value")) == (Widget) NULL)
+ PopupWarning(NULL, "Could not find text widget in MakeSearchWidget.");
+ else
+ XtSetKeyboardFocus(dialog, text);
- XawDialogAddButton(dialog, MANUALSEARCH, NULL, NULL);
- XawDialogAddButton(dialog, APROPOSSEARCH, NULL, NULL);
- XawDialogAddButton(dialog, CANCEL, NULL, NULL);
+ XawDialogAddButton(dialog, MANUALSEARCH, NULL, NULL);
+ XawDialogAddButton(dialog, APROPOSSEARCH, NULL, NULL);
+ XawDialogAddButton(dialog, CANCEL, NULL, NULL);
/*
- * This is a bit gross, but it get the cancel button underneath the
+ * This is a bit gross, but it get the cancel button underneath the
* others, and forms them up to the right size..
*/
- if ( ((command = XtNameToWidget(dialog, MANUALSEARCH)) == (Widget) NULL) ||
- ((cancel = XtNameToWidget(dialog, CANCEL)) == (Widget) NULL) )
- PopupWarning(NULL,
- "Could not find manual search widget in MakeSearchWidget.");
- else {
- static char * half_size[] = {
- MANUALSEARCH, APROPOSSEARCH, NULL
- };
- static char * full_size[] = {
- "label", "value", CANCEL, NULL
- };
-
- num_args = 0;
- XtSetArg(arglist[num_args], XtNfromVert, command); num_args++;
- XtSetArg(arglist[num_args], XtNfromHoriz, NULL); num_args++;
- XtSetValues(cancel, arglist, num_args);
- FormUpWidgets(dialog, full_size, half_size);
- }
+ if (((command = XtNameToWidget(dialog, MANUALSEARCH)) == (Widget) NULL) ||
+ ((cancel = XtNameToWidget(dialog, CANCEL)) == (Widget) NULL))
+ PopupWarning(NULL,
+ "Could not find manual search widget in MakeSearchWidget.");
+ else {
+ static const char *half_size[] = {
+ MANUALSEARCH, APROPOSSEARCH, NULL
+ };
+ static const char *full_size[] = {
+ "label", "value", CANCEL, NULL
+ };
+
+ num_args = 0;
+ XtSetArg(arglist[num_args], XtNfromVert, command);
+ num_args++;
+ XtSetArg(arglist[num_args], XtNfromHoriz, NULL);
+ num_args++;
+ XtSetValues(cancel, arglist, num_args);
+ FormUpWidgets(dialog, full_size, half_size);
+ }
}
@@ -111,32 +111,31 @@ MakeSearchWidget(ManpageGlobals * man_globals, Widget parent)
*/
static char *
-SearchString(
-ManpageGlobals * man_globals)
+SearchString(ManpageGlobals * man_globals)
{
- Widget dialog;
+ Widget dialog;
- dialog = XtNameToWidget(man_globals->search_widget, DIALOG);
- if (dialog != NULL)
- return(XawDialogGetValueString(dialog));
+ dialog = XtNameToWidget(man_globals->search_widget, DIALOG);
+ if (dialog != NULL)
+ return (XawDialogGetValueString(dialog));
- PopupWarning(man_globals,
- "Could not get the search string, no search will be preformed.");
- return(NULL);
+ PopupWarning(man_globals,
+ "Could not get the search string, no search will be preformed.");
+ return (NULL);
}
-
+
/* Function Name: DoSearch
* Description: This function performs a search for a man page or apropos
* search upon search string.
- * Arguments: man_globals - the pseudo globas for this manpage.
+ * Arguments: man_globals - the pseudo globals for this manpage.
* type - the type of search.
* Returns: none.
*/
#define LOOKLINES 6
-/*
+/*
* Manual searches look through the list of manual pages for the right one
* with a binary search.
*
@@ -149,154 +148,150 @@ ManpageGlobals * man_globals)
FILE *
DoSearch(ManpageGlobals * man_globals, int type)
{
- char cmdbuf[BUFSIZ],*mantmp, *manpath;
- char tmp[BUFSIZ],path[BUFSIZ];
- char string_buf[BUFSIZ], cmp_str[BUFSIZ], error_buf[BUFSIZ];
- char * search_string = SearchString(man_globals);
- FILE * file;
-#ifdef HAS_MKSTEMP
- int fd;
-#endif
- int count;
- Boolean flag;
-
- if (search_string == NULL) return(NULL);
-
- /* If the string is empty or starts with a space then do not search */
-
- if ( streq(search_string,"") ) {
- PopupWarning(man_globals, "Search string is empty.");
- return(NULL);
- }
-
- if (strlen(search_string) >= BUFSIZ) {
- PopupWarning(man_globals, "Search string too long.");
- return(NULL);
- }
- if (search_string[0] == ' ') {
- PopupWarning(man_globals, "First character cannot be a space.");
- return(NULL);
- }
-
- if (type == APROPOS) {
- char label[BUFSIZ];
-
- strlcpy(tmp, MANTEMP, sizeof(tmp)); /* get a temp file. */
-#ifdef HAS_MKSTEMP
- fd = mkstemp(tmp);
- if (fd < 0) {
- PopupWarning(man_globals, "Cant create temp file");
- return NULL;
+ char cmdbuf[BUFSIZ], *mantmp, *manpath;
+ char tmp[BUFSIZ], path[BUFSIZ];
+ char string_buf[BUFSIZ], cmp_str[BUFSIZ], error_buf[BUFSIZ];
+ char *search_string = SearchString(man_globals);
+ FILE *file;
+ int fd;
+ int count;
+ Boolean flag;
+
+ if (search_string == NULL)
+ return (NULL);
+
+ /* If the string is empty or starts with a space then do not search */
+
+ if (streq(search_string, "")) {
+ PopupWarning(man_globals, "Search string is empty.");
+ return (NULL);
+ }
+
+ if (strlen(search_string) >= BUFSIZ) {
+ PopupWarning(man_globals, "Search string too long.");
+ return (NULL);
+ }
+ if (search_string[0] == ' ') {
+ PopupWarning(man_globals, "First character cannot be a space.");
+ return (NULL);
}
-#else
- (void)mktemp(tmp);
-#endif
- mantmp = tmp;
- manpath=getenv("MANPATH");
- if (manpath == NULL || streq(manpath,"") ) {
+ if (type == APROPOS) {
+ char label[BUFSIZ];
+
+ strlcpy(tmp, MANTEMP, sizeof(tmp)); /* get a temp file. */
+ fd = mkstemp(tmp);
+ if (fd < 0) {
+ PopupWarning(man_globals, "Cant create temp file");
+ return NULL;
+ }
+ mantmp = tmp;
+
+ manpath = getenv("MANPATH");
+ if (manpath == NULL || streq(manpath, "")) {
#ifdef MANCONF
- if (!ReadManConfig(path))
+ if (!ReadManConfig(path))
#endif
- {
- strlcpy(path,SYSMANPATH,sizeof(path));
+ {
+ strlcpy(path, SYSMANPATH, sizeof(path));
#ifdef LOCALMANPATH
- strlcat(path,":",sizeof(path));
- strlcat(path,LOCALMANPATH,sizeof(path));
+ strlcat(path, ":", sizeof(path));
+ strlcat(path, LOCALMANPATH, sizeof(path));
#endif
- }
- } else {
- strlcpy(path,manpath,sizeof(path));
- }
+ }
+ }
+ else {
+ strlcpy(path, manpath, sizeof(path));
+ }
- snprintf(label, sizeof(label),
- "Results of apropos search on: %s", search_string);
+ snprintf(label, sizeof(label),
+ "Results of apropos search on: %s", search_string);
-#ifdef NO_MANPATH_SUPPORT /* not quite correct, but the best I can do. */
- snprintf(cmdbuf, sizeof(cmdbuf), APROPOS_FORMAT, search_string, mantmp);
+#ifdef NO_MANPATH_SUPPORT /* not quite correct, but the best I can do. */
+ snprintf(cmdbuf, sizeof(cmdbuf), APROPOS_FORMAT, search_string, mantmp);
#else
- snprintf(cmdbuf, sizeof(cmdbuf), APROPOS_FORMAT, path, search_string, mantmp);
+ snprintf(cmdbuf, sizeof(cmdbuf), APROPOS_FORMAT, path, search_string,
+ mantmp);
#endif
- if(system(cmdbuf) != 0) { /* execute search. */
- snprintf(error_buf, sizeof(error_buf), "Something went wrong trying to run %s\n",cmdbuf);
- PopupWarning(man_globals, error_buf);
- }
+ if (system(cmdbuf) != 0) { /* execute search. */
+ snprintf(error_buf, sizeof(error_buf),
+ "Something went wrong trying to run %s\n", cmdbuf);
+ PopupWarning(man_globals, error_buf);
+ }
-#ifdef HAS_MKSTEMP
- if ((file = fdopen(fd, "r")) == NULL)
-#else
- if((file = fopen(mantmp,"r")) == NULL)
-#endif
- PrintError("lost temp file? out of temp space?");
+ if ((file = fdopen(fd, "r")) == NULL)
+ PrintError("lost temp file? out of temp space?");
-/*
- * Since we keep the FD open we can unlink the file safely, this
- * will keep extra files out of /tmp.
+/*
+ * Since we keep the FD open we can remove the file safely, this
+ * will keep extra files out of /tmp.
*/
- unlink(mantmp);
-
- snprintf(string_buf, sizeof(string_buf), "%s: nothing appropriate", search_string);
-
- /*
- * Check first LOOKLINES lines for "nothing appropriate".
- */
-
- count = 0;
- flag = FALSE;
- while ( (fgets(cmp_str, BUFSIZ, file) != NULL) && (count < LOOKLINES) ) {
- if ( cmp_str[strlen(cmp_str) - 1] == '\n') /* strip off the '\n' */
- cmp_str[strlen(cmp_str) - 1] = '\0';
-
- if (streq(cmp_str, string_buf)) {
- flag = TRUE;
- break;
- }
- count++;
+ remove(mantmp);
+
+ snprintf(string_buf, sizeof(string_buf), "%s: nothing appropriate",
+ search_string);
+
+ /*
+ * Check first LOOKLINES lines for "nothing appropriate".
+ */
+
+ count = 0;
+ flag = FALSE;
+ while ((fgets(cmp_str, BUFSIZ, file) != NULL) && (count < LOOKLINES)) {
+ if (cmp_str[strlen(cmp_str) - 1] == '\n') /* strip off the '\n' */
+ cmp_str[strlen(cmp_str) - 1] = '\0';
+
+ if (streq(cmp_str, string_buf)) {
+ flag = TRUE;
+ break;
+ }
+ count++;
+ }
+
+ /*
+ * If the file is less than this number of lines then assume that there is
+ * nothing apropriate found. This does not confuse the apropos filter.
+ */
+
+ if (flag) {
+ fclose(file);
+ file = NULL;
+ ChangeLabel(man_globals->label, string_buf);
+ return (NULL);
+ }
+
+ snprintf(man_globals->manpage_title, sizeof(man_globals->manpage_title),
+ "%s", label);
+ ChangeLabel(man_globals->label, label);
+ fseek(file, 0L, SEEK_SET); /* reset file to point at top. */
}
-
- /*
- * If the file is less than this number of lines then assume that there is
- * nothing apropriate found. This does not confuse the apropos filter.
- */
-
- if (flag) {
- fclose(file);
- file = NULL;
- ChangeLabel(man_globals->label,string_buf);
- return(NULL);
+ else { /* MANUAL SEARCH */
+ file = DoManualSearch(man_globals, search_string);
+ if (file == NULL) {
+ snprintf(string_buf, sizeof(string_buf), "No manual entry for %s.",
+ search_string);
+ ChangeLabel(man_globals->label, string_buf);
+ if (man_globals->label == NULL)
+ PopupWarning(man_globals, string_buf);
+ return (NULL);
+ }
}
-
- snprintf(man_globals->manpage_title,sizeof(man_globals->manpage_title),
- "%s", label);
- ChangeLabel(man_globals->label,label);
- fseek(file, 0L, SEEK_SET); /* reset file to point at top. */
- }
- else { /* MANUAL SEACH */
- file = DoManualSearch(man_globals, search_string);
- if (file == NULL) {
- snprintf(string_buf,sizeof(string_buf),"No manual entry for %s.", search_string);
- ChangeLabel(man_globals->label, string_buf);
- if (man_globals->label == NULL)
- PopupWarning(man_globals, string_buf);
- return(NULL);
- }
- }
- if (resources.clear_search_string) {
- Arg arglist[1];
- Widget dialog;
+ if (resources.clear_search_string) {
+ Arg arglist[1];
+ Widget dialog;
- dialog = XtNameToWidget(man_globals->search_widget, DIALOG);
- if (dialog == NULL)
- PopupWarning(man_globals, "Could not clear the search string.");
+ dialog = XtNameToWidget(man_globals->search_widget, DIALOG);
+ if (dialog == NULL)
+ PopupWarning(man_globals, "Could not clear the search string.");
- XtSetArg(arglist[0], XtNvalue, "");
- XtSetValues(dialog, arglist, (Cardinal) 1);
- }
+ XtSetArg(arglist[0], XtNvalue, "");
+ XtSetValues(dialog, arglist, (Cardinal) 1);
+ }
- return(file);
+ return (file);
}
/* Function Name: DoManualSearch
@@ -307,46 +302,49 @@ DoSearch(ManpageGlobals * man_globals, int type)
#define NO_ENTRY -100
-static FILE *
-DoManualSearch(ManpageGlobals *man_globals, char * string)
+static FILE *
+DoManualSearch(ManpageGlobals * man_globals, char *string)
{
- int e_num = NO_ENTRY;
- int i;
+ int e_num = NO_ENTRY;
+ int i;
/* search current section first. */
-
- i = man_globals->current_directory;
- e_num = BEntrySearch(string, manual[i].entries, manual[i].nentries);
+
+ i = man_globals->current_directory;
+ e_num = BEntrySearch(string, manual[i].entries, manual[i].nentries);
/* search other sections. */
- if (e_num == NO_ENTRY) {
- i = 0; /* At the exit of the loop i needs to
- be the one we used. */
- while ( TRUE ) {
- if (i == man_globals->current_directory)
- if (++i >= sections) return(NULL);
- e_num = BEntrySearch(string, manual[i].entries, manual[i].nentries);
- if (e_num != NO_ENTRY) break;
- if (++i >= sections) return(NULL);
- }
+ if (e_num == NO_ENTRY) {
+ i = 0; /* At the exit of the loop i needs to
+ be the one we used. */
+ while (TRUE) {
+ if (i == man_globals->current_directory)
+ if (++i >= sections)
+ return (NULL);
+ e_num = BEntrySearch(string, manual[i].entries, manual[i].nentries);
+ if (e_num != NO_ENTRY)
+ break;
+ if (++i >= sections)
+ return (NULL);
+ }
/*
* Manual page found in some other section, unhighlight the current one.
*/
- if ( man_globals->manpagewidgets.box != NULL)
- XawListUnhighlight(
- man_globals->manpagewidgets.box[man_globals->current_directory]);
- }
- else {
- /*
- * Highlight the element we are searching for if it is in the directory
- * listing currently being shown.
- */
- if ( man_globals->manpagewidgets.box != NULL)
- XawListHighlight(man_globals->manpagewidgets.box[i], e_num);
- }
- return(FindManualFile(man_globals, i, e_num));
+ if (man_globals->manpagewidgets.box != NULL)
+ XawListUnhighlight(man_globals->manpagewidgets.
+ box[man_globals->current_directory]);
+ }
+ else {
+ /*
+ * Highlight the element we are searching for if it is in the directory
+ * listing currently being shown.
+ */
+ if (man_globals->manpagewidgets.box != NULL)
+ XawListHighlight(man_globals->manpagewidgets.box[i], e_num);
+ }
+ return (FindManualFile(man_globals, i, e_num));
}
/* Function Name: BEntrySearch
@@ -358,44 +356,42 @@ DoManualSearch(ManpageGlobals *man_globals, char * string)
*/
static int
-BEntrySearch(
-char * string,
-char ** first,
-int number)
+BEntrySearch(char *string, char **first, int number)
{
- int check, cmp, len_cmp, global_number;
- char *head, *tail;
-
- global_number = 0;
- while (TRUE) {
-
- if (number == 0) {
- return(NO_ENTRY); /* didn't find it. */
- }
+ int check, cmp, len_cmp, global_number;
+ char *head, *tail;
- check = number/2;
+ global_number = 0;
+ while (TRUE) {
- head = rindex(first[ global_number + check ], '/');
- if (head == NULL)
- PrintError("index failure in BEntrySearch");
- head++;
+ if (number == 0) {
+ return (NO_ENTRY); /* didn't find it. */
+ }
- tail = rindex(head, '.');
- if (tail == NULL)
- /* not an error, some systems (e.g. sgi) have only a .z suffix */
- tail = head + strlen(head);
+ check = number / 2;
- cmp = strncmp(string, head, (tail - head));
- len_cmp = strlen(string) - (int) (tail - head);
+ head = strrchr(first[global_number + check], '/');
+ if (head == NULL)
+ PrintError("index failure in BEntrySearch");
+ head++;
- if ( cmp == 0 && len_cmp == 0) {
- return(global_number + check);
- }
- else if ( cmp < 0 || ((cmp == 0) && (len_cmp < 0)) )
- number = check;
- else /* cmp > 0 || ((cmp == 0) && (len_cmp > 0)) */ {
- global_number += (check + 1);
- number -= ( check + 1 );
+ tail = strrchr(head, '.');
+ if (tail == NULL)
+ /* not an error, some systems (e.g. sgi) have only a .z suffix */
+ tail = head + strlen(head);
+
+ cmp = strncmp(string, head, (tail - head));
+ len_cmp = strlen(string) - (int) (tail - head);
+
+ if (cmp == 0 && len_cmp == 0) {
+ return (global_number + check);
+ }
+ else if (cmp < 0 || ((cmp == 0) && (len_cmp < 0)))
+ number = check;
+ else { /* cmp > 0 || ((cmp == 0) && (len_cmp > 0)) */
+
+ global_number += (check + 1);
+ number -= (check + 1);
+ }
}
- }
}