main.c revision 6d36ef34
11abf7346Smrg/*
21abf7346Smrg
31abf7346SmrgCopyright (c) 1987, 1988  X Consortium
41abf7346Smrg
51abf7346SmrgPermission is hereby granted, free of charge, to any person obtaining
61abf7346Smrga copy of this software and associated documentation files (the
71abf7346Smrg"Software"), to deal in the Software without restriction, including
81abf7346Smrgwithout limitation the rights to use, copy, modify, merge, publish,
91abf7346Smrgdistribute, sublicense, and/or sell copies of the Software, and to
101abf7346Smrgpermit persons to whom the Software is furnished to do so, subject to
111abf7346Smrgthe following conditions:
121abf7346Smrg
131abf7346SmrgThe above copyright notice and this permission notice shall be included
141abf7346Smrgin all copies or substantial portions of the Software.
151abf7346Smrg
161abf7346SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
171abf7346SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
181abf7346SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
191abf7346SmrgIN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
201abf7346SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
211abf7346SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
221abf7346SmrgOTHER DEALINGS IN THE SOFTWARE.
231abf7346Smrg
241abf7346SmrgExcept as contained in this notice, the name of the X Consortium shall
251abf7346Smrgnot be used in advertising or otherwise to promote the sale, use or
261abf7346Smrgother dealings in this Software without prior written authorization
271abf7346Smrgfrom the X Consortium.
281abf7346Smrg
291abf7346Smrg*/
301abf7346Smrg
311abf7346Smrg/*
321abf7346Smrg * xman - X window system manual page display program.
331abf7346Smrg * Author:    Chris D. Peterson, MIT Project Athena
341abf7346Smrg * Created:   October 22, 1987
351abf7346Smrg */
361abf7346Smrg
371abf7346Smrg#include "globals.h"
381abf7346Smrg#ifndef ZERO
391abf7346Smrg#include <X11/Xaw/Cardinals.h>
401abf7346Smrg#endif /* ZERO */
411abf7346Smrg
426d36ef34Smrgstatic void ArgError(int argc, char **argv);
431abf7346Smrgstatic void AdjustDefResources(void);
441abf7346Smrg
451abf7346Smrg#define Offset(field) (XtOffsetOf(Xman_Resources , field))
461abf7346Smrg
471abf7346Smrgstatic XtResource my_resources[] = {
486d36ef34Smrg    {"directoryFontNormal", XtCFont, XtRFontStruct, sizeof(XFontStruct *),
491abf7346Smrg     Offset(fonts.directory), XtRString, DIRECTORY_NORMAL},
506d36ef34Smrg    {"bothShown", XtCBoolean, XtRBoolean, sizeof(Boolean),
511abf7346Smrg     Offset(both_shown_initial), XtRString, "False"},
526d36ef34Smrg    {"directoryHeight", "DirectoryHeight", XtRInt, sizeof(int),
536d36ef34Smrg     Offset(directory_height), XtRString, "150"},
546d36ef34Smrg    {"topCursor", XtCCursor, XtRCursor, sizeof(Cursor),
551abf7346Smrg     Offset(cursors.top), XtRString, XMAN_CURSOR},
566d36ef34Smrg    {"helpCursor", XtCCursor, XtRCursor, sizeof(Cursor),
571abf7346Smrg     Offset(cursors.help), XtRString, HELP_CURSOR},
586d36ef34Smrg    {"manpageCursor", XtCCursor, XtRCursor, sizeof(Cursor),
591abf7346Smrg     Offset(cursors.manpage), XtRString, MANPAGE_CURSOR},
606d36ef34Smrg    {"searchEntryCursor", XtCCursor, XtRCursor, sizeof(Cursor),
611abf7346Smrg     Offset(cursors.search_entry), XtRString, SEARCH_ENTRY_CURSOR},
626d36ef34Smrg    {"pointerColor", XtCForeground, XtRPixel, sizeof(Pixel),
631abf7346Smrg     Offset(cursors.fg_color), XtRString, "XtDefaultForeground"},
646d36ef34Smrg    {"pointerColorBackground", XtCBackground, XtRPixel, sizeof(Pixel),
651abf7346Smrg     Offset(cursors.bg_color), XtRString, "XtDefaultBackground"},
666d36ef34Smrg    {"help", XtCBoolean, XtRBoolean, sizeof(Boolean),
678b6d6341Smrg     Offset(show_help_syntax), XtRImmediate, NULL},
686d36ef34Smrg    {"helpFile", XtCFile, XtRString, sizeof(char *),
691abf7346Smrg     Offset(help_file), XtRString, HELPFILE},
706d36ef34Smrg    {"topBox", XtCBoolean, XtRBoolean, sizeof(Boolean),
711abf7346Smrg     Offset(top_box_active), XtRString, "True"},
726d36ef34Smrg    {"clearSearchString", "ClearSearchString", XtRBoolean, sizeof(Boolean),
731abf7346Smrg     Offset(clear_search_string), XtRImmediate, (caddr_t) TRUE},
746d36ef34Smrg    {"title", XtCString, XtRString, sizeof(char *),
751abf7346Smrg     Offset(title), XtRString, "xman"},
766d36ef34Smrg    {"iconic", XtCBoolean, XtRBoolean, sizeof(Boolean),
771abf7346Smrg     Offset(iconic), XtRString, "False"},
781abf7346Smrg};
791abf7346Smrg
801abf7346Smrg#undef Offset
811abf7346Smrg
821abf7346Smrg/*
831abf7346Smrg * The resource that we absolutely need.
841abf7346Smrg */
851abf7346Smrg
866d36ef34Smrgstatic char *fallback_resources[] = {
876d36ef34Smrg    "Xman*quitButton.translations:	#override \\n   <Btn1Up>: Quit() reset()",
886d36ef34Smrg    "Xman*helpButton.sensitive:                    FALSE",
896d36ef34Smrg    "Xman*manpageButton.sensitive:                 FALSE",
906d36ef34Smrg    "Xman*helpButton.Label:			Help",
916d36ef34Smrg    "Xman*quitButton.Label:			Quit",
926d36ef34Smrg    "Xman*manpageButton.Label:		        Manual Page",
936d36ef34Smrg    "Xman*topLabel.label:         		        No App-Defaults File",
946d36ef34Smrg    NULL,
951abf7346Smrg};
961abf7346Smrg
971abf7346Smrg/*
981abf7346Smrg * This is necessary to keep all TopLevel shells from becoming
991abf7346Smrg * the size that is specified on the command line.
1001abf7346Smrg */
1011abf7346Smrg
1021abf7346Smrgstatic XrmOptionDescRec xman_options[] = {
1036d36ef34Smrg    {"-geometry", "*topBox.geometry", XrmoptionSepArg, (caddr_t) NULL},
1046d36ef34Smrg    {"-help", "help", XrmoptionNoArg, (caddr_t) "True"},
1056d36ef34Smrg    {"=", "*topBox.geometry", XrmoptionIsArg, (caddr_t) NULL},
1066d36ef34Smrg    {"-pagesize", "*manualBrowser.geometry", XrmoptionSepArg, (caddr_t) NULL},
1076d36ef34Smrg    {"-notopbox", "topBox", XrmoptionNoArg, (caddr_t) "False"},
1086d36ef34Smrg    {"-helpfile", "helpFile", XrmoptionSepArg, (caddr_t) NULL},
1096d36ef34Smrg    {"-bothshown", "bothShown", XrmoptionNoArg, (caddr_t) "True"},
1106d36ef34Smrg    {"-title", "title", XrmoptionSepArg, (caddr_t) "xman"},
1116d36ef34Smrg    {"-iconic", "iconic", XrmoptionNoArg, (caddr_t) "True"},
1121abf7346Smrg};
1131abf7346Smrg
1148b6d6341Smrgstatic XtActionsRec xman_actions[] = {
1156d36ef34Smrg    {"GotoPage", GotoPage},
1166d36ef34Smrg    {"Quit", Quit},
1176d36ef34Smrg    {"Search", Search},
1186d36ef34Smrg    {"PopupHelp", PopupHelp},
1196d36ef34Smrg    {"PopupSearch", PopupSearch},
1206d36ef34Smrg    {"CreateNewManpage", CreateNewManpage},
1216d36ef34Smrg    {"RemoveThisManpage", RemoveThisManpage},
1226d36ef34Smrg    {"SaveFormattedPage", SaveFormattedPage},
1231abf7346Smrg#ifdef INCLUDE_XPRINT_SUPPORT
1246d36ef34Smrg    {"PrintThisManpage", PrintThisManpage},
1256d36ef34Smrg#endif                          /* INCLUDE_XPRINT_SUPPORT */
1266d36ef34Smrg    {"ShowVersion", ShowVersion},
1271abf7346Smrg};
1281abf7346Smrg
1291abf7346Smrgchar **saved_argv;
1301abf7346Smrgint saved_argc;
1311abf7346Smrg
1321abf7346Smrg/*
1331abf7346Smrg * This atom is used to make the application ICCCM compliant.
1341abf7346Smrg */
1351abf7346SmrgAtom wm_delete_window;
1361abf7346Smrg
1371abf7346Smrg/*	Function Name: main
1381abf7346Smrg *	Description: This is the main driver for Xman.
1391abf7346Smrg *	Arguments: argc, argv - the command line arguments.
1401abf7346Smrg *	Returns: return, what return.
1411abf7346Smrg */
1421abf7346Smrg
1436d36ef34Smrgint
1446d36ef34Smrgmain(int argc, char **argv)
1451abf7346Smrg{
1466d36ef34Smrg    XtAppContext app_con;
1471abf7346Smrg
1486d36ef34Smrg    saved_argc = argc;
1496d36ef34Smrg    saved_argv = (char **) XtMalloc(argc * sizeof(char *));
1506d36ef34Smrg    bcopy(argv, saved_argv, argc * sizeof(char *));
1511abf7346Smrg
1526d36ef34Smrg    XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL);
1531abf7346Smrg
1546d36ef34Smrg    initial_widget = XtAppInitialize(&app_con, "Xman", xman_options,
1556d36ef34Smrg                                     XtNumber(xman_options), &argc, argv,
1566d36ef34Smrg                                     fallback_resources, NULL, ZERO);
1576d36ef34Smrg    wm_delete_window =
1586d36ef34Smrg        XInternAtom(XtDisplay(initial_widget), "WM_DELETE_WINDOW", False);
1591abf7346Smrg
1606d36ef34Smrg    manglobals_context = XStringToContext(MANNAME);
1611abf7346Smrg
1626d36ef34Smrg    AdjustDefResources();
1631abf7346Smrg
1646d36ef34Smrg    XtGetApplicationResources(initial_widget, (caddr_t) & resources,
1656d36ef34Smrg                              my_resources, XtNumber(my_resources),
1666d36ef34Smrg                              NULL, (Cardinal) 0);
1671abf7346Smrg
1686d36ef34Smrg    if ((argc != 1) || (resources.show_help_syntax)) {
1696d36ef34Smrg        ArgError(argc, argv);
1706d36ef34Smrg        return EXIT_FAILURE;
1716d36ef34Smrg    }
1721abf7346Smrg
1736d36ef34Smrg    XtAppAddActions(app_con, xman_actions, XtNumber(xman_actions));
1741abf7346Smrg
1756d36ef34Smrg    if (!resources.fonts.directory)
1766d36ef34Smrg        PrintError("Failed to get the directory font.");
1771abf7346Smrg
1781abf7346Smrg#ifdef DEBUG
1796d36ef34Smrg    printf("debugging mode\n");
1801abf7346Smrg#endif
1811abf7346Smrg
1821abf7346Smrg/*
1831abf7346Smrg * Set the default width and height.
1841abf7346Smrg * I am not real happy with this method, but it will usually do something
1851abf7346Smrg * reasonable, if not the "right" thing.  It is not a real big issue since
1861abf7346Smrg * it is easy to change the values with resources or command line options.
1871abf7346Smrg * NOTE: if you are in a 100 dpi display you will lose.
1881abf7346Smrg */
1891abf7346Smrg
1906d36ef34Smrg    default_width = DEFAULT_WIDTH;
1916d36ef34Smrg    default_height = DisplayHeight(XtDisplay(initial_widget),
1926d36ef34Smrg                                   DefaultScreen(XtDisplay(initial_widget)));
1936d36ef34Smrg    default_height *= 3;
1946d36ef34Smrg    default_height /= 4;
1951abf7346Smrg
1966d36ef34Smrg    if ((sections = Man()) == 0)
1976d36ef34Smrg        PrintError
1986d36ef34Smrg            ("There are no manual sections to display, check your MANPATH.");
1991abf7346Smrg
2006d36ef34Smrg    if (resources.top_box_active)
2016d36ef34Smrg        MakeTopBox();
2026d36ef34Smrg    else
2036d36ef34Smrg        (void) CreateManpage(NULL);
2041abf7346Smrg
2051abf7346Smrg/*
2061abf7346Smrg * We need to keep track of the number of manual pages that are shown on
2071abf7346Smrg * the screen so that if this user does not have a top box then when he
2081abf7346Smrg * removes all his manual pages we can kill off the xman process.
2091abf7346Smrg * To make things easier we will consider the top box a shown manual page
2101abf7346Smrg * here, but since you cannot remove it, man_page_shown only goes to zero when
2111abf7346Smrg * no top box is present.
2121abf7346Smrg */
2131abf7346Smrg
2146d36ef34Smrg    man_pages_shown = 1;
2151abf7346Smrg
2166d36ef34Smrg    XtAppMainLoop(app_con);
2171abf7346Smrg
2186d36ef34Smrg    return EXIT_SUCCESS;
2191abf7346Smrg}
2201abf7346Smrg
2211abf7346Smrg/*	Function Name: ArgError
2226d36ef34Smrg *	Description:  Prints error message about unknown arguments.
2231abf7346Smrg *	Arguments: argc, argv - args not understood.
2241abf7346Smrg *	Returns: none.
2251abf7346Smrg */
2261abf7346Smrg
2276d36ef34Smrgstatic void
2286d36ef34SmrgArgError(int argc, char **argv)
2291abf7346Smrg{
2306d36ef34Smrg    int i;
2316d36ef34Smrg
2326d36ef34Smrg    static const char **syntax, *syntax_def[] = {
2336d36ef34Smrg        "-help", "Print this message",
2346d36ef34Smrg        "-helpfile <filename>", "Specifies the helpfile to use.",
2356d36ef34Smrg        "-bothshown", "Show both the directory and manpage at once.",
2366d36ef34Smrg        "-notopbox", "Starts with manpage rather than topbox.",
2376d36ef34Smrg        "-geometry <geom>", "Specifies the geometry of the top box.",
2386d36ef34Smrg        "=<geom>", "Specifies the geometry of the top box.",
2396d36ef34Smrg        "-pagesize <geom>", "Specifies the geometry of the manual page.",
2406d36ef34Smrg        "-bw <pixels>", "Width of all window borders.",
2416d36ef34Smrg        "-borderwidth <pixels>", "Width of all window borders.",
2426d36ef34Smrg        "-bd <color>", "Color of all window borders.",
2436d36ef34Smrg        "-bordercolor <color>", "Color of all window borders.",
2446d36ef34Smrg        "-fg <color>", "Foreground color for the application.",
2456d36ef34Smrg        "-foreground <color>", "Foreground color for the application.",
2466d36ef34Smrg        "-bg <color>", "Background color for the application.",
2476d36ef34Smrg        "-background <color>", "Background color for the application.",
2486d36ef34Smrg        "-display <display name>", "Specify a display that is not the default",
2496d36ef34Smrg        "-fn <font>", "Font to be used for button and label text.",
2506d36ef34Smrg        "-font <font>", "Font to be used for button and label text.",
2516d36ef34Smrg        "-name <name>", "Change the name used for retrieving resources.",
2526d36ef34Smrg        "-title <name>", "Change the name without affecting resources.",
2536d36ef34Smrg        "-xrm <resource>", "Specifies a resource on the command line.",
2546d36ef34Smrg        NULL, NULL,
2556d36ef34Smrg    };
2566d36ef34Smrg
2576d36ef34Smrg    syntax = syntax_def;
2586d36ef34Smrg
2596d36ef34Smrg    for (i = 1; i < argc; i++)
2606d36ef34Smrg        (void) printf("This argument is unknown to Xman: %s\n", argv[i]);
2616d36ef34Smrg
2626d36ef34Smrg    (void) printf("\nKnown arguments are:\n");
2636d36ef34Smrg
2646d36ef34Smrg    while (*syntax != NULL) {
2656d36ef34Smrg        printf("%-30s - %s\n", syntax[0], syntax[1]);
2666d36ef34Smrg        syntax += 2;
2676d36ef34Smrg    }
2681abf7346Smrg}
2691abf7346Smrg
2701abf7346Smrg/*    Function Name: AdjustDefResources
2711abf7346Smrg *    Description: Changes default resources which contain paths when
2721abf7346Smrg *            XWINHOME is set
2731abf7346Smrg *    Arguments: none
2741abf7346Smrg *    Returns: nothing
2751abf7346Smrg */
2761abf7346Smrg
2771abf7346Smrgstatic void
2781abf7346SmrgAdjustDefResources(void)
2791abf7346Smrg{
2806d36ef34Smrg    char *xwinhome = NULL;
2816d36ef34Smrg    int i;
2826d36ef34Smrg
2836d36ef34Smrg    if (!(xwinhome = getenv("XWINHOME")))
2846d36ef34Smrg        return;
2856d36ef34Smrg
2866d36ef34Smrg    for (i = 0; i < sizeof(my_resources) / sizeof(XtResource); i++) {
2876d36ef34Smrg        if (!strcmp(my_resources[i].resource_name, "helpFile")) {
2886d36ef34Smrg            char filename[PATH_MAX];
2896d36ef34Smrg
2906d36ef34Smrg            snprintf(filename, sizeof(filename), "%s/lib/X11/xman.help",
2916d36ef34Smrg                     xwinhome);
2926d36ef34Smrg            if (!(my_resources[i].default_addr = strdup(filename))) {
2936d36ef34Smrg                fprintf(stderr, "malloc failure\n");
2946d36ef34Smrg                exit(EXIT_FAILURE);
2956d36ef34Smrg            }
2966d36ef34Smrg        }
2971abf7346Smrg    }
2981abf7346Smrg}
299