main.c revision 8b6d6341
11abf7346Smrg/* $XConsortium: main.c,v 1.23 94/04/17 20:43:55 rws Exp $ */
21abf7346Smrg/* $XdotOrg: $ */
31abf7346Smrg/*
41abf7346Smrg
51abf7346SmrgCopyright (c) 1987, 1988  X Consortium
61abf7346Smrg
71abf7346SmrgPermission is hereby granted, free of charge, to any person obtaining
81abf7346Smrga copy of this software and associated documentation files (the
91abf7346Smrg"Software"), to deal in the Software without restriction, including
101abf7346Smrgwithout limitation the rights to use, copy, modify, merge, publish,
111abf7346Smrgdistribute, sublicense, and/or sell copies of the Software, and to
121abf7346Smrgpermit persons to whom the Software is furnished to do so, subject to
131abf7346Smrgthe following conditions:
141abf7346Smrg
151abf7346SmrgThe above copyright notice and this permission notice shall be included
161abf7346Smrgin all copies or substantial portions of the Software.
171abf7346Smrg
181abf7346SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
191abf7346SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
201abf7346SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
211abf7346SmrgIN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
221abf7346SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
231abf7346SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
241abf7346SmrgOTHER DEALINGS IN THE SOFTWARE.
251abf7346Smrg
261abf7346SmrgExcept as contained in this notice, the name of the X Consortium shall
271abf7346Smrgnot be used in advertising or otherwise to promote the sale, use or
281abf7346Smrgother dealings in this Software without prior written authorization
291abf7346Smrgfrom the X Consortium.
301abf7346Smrg
311abf7346Smrg*/
321abf7346Smrg/* $XFree86: xc/programs/xman/main.c,v 1.3 2000/03/03 23:16:27 dawes Exp $ */
331abf7346Smrg
341abf7346Smrg/*
351abf7346Smrg * xman - X window system manual page display program.
361abf7346Smrg * Author:    Chris D. Peterson, MIT Project Athena
371abf7346Smrg * Created:   October 22, 1987
381abf7346Smrg */
391abf7346Smrg
401abf7346Smrg#include "globals.h"
411abf7346Smrg#ifndef ZERO
421abf7346Smrg#include <X11/Xaw/Cardinals.h>
431abf7346Smrg#endif /* ZERO */
441abf7346Smrg
451abf7346Smrgstatic void ArgError(int argc, char ** argv);
461abf7346Smrgstatic void AdjustDefResources(void);
471abf7346Smrg
481abf7346Smrg#define Offset(field) (XtOffsetOf(Xman_Resources , field))
491abf7346Smrg
501abf7346Smrgstatic XtResource my_resources[] = {
511abf7346Smrg  {"directoryFontNormal", XtCFont, XtRFontStruct, sizeof(XFontStruct *),
521abf7346Smrg     Offset(fonts.directory), XtRString, DIRECTORY_NORMAL},
531abf7346Smrg  {"bothShown", XtCBoolean, XtRBoolean, sizeof(Boolean),
541abf7346Smrg     Offset(both_shown_initial), XtRString, "False"},
551abf7346Smrg  {"directoryHeight", "DirectoryHeight", XtRInt, sizeof(int),
561abf7346Smrg     Offset(directory_height), XtRString, "150"},
571abf7346Smrg  {"topCursor", XtCCursor, XtRCursor, sizeof(Cursor),
581abf7346Smrg     Offset(cursors.top), XtRString, XMAN_CURSOR},
591abf7346Smrg  {"helpCursor", XtCCursor, XtRCursor, sizeof(Cursor),
601abf7346Smrg     Offset(cursors.help), XtRString, HELP_CURSOR},
611abf7346Smrg  {"manpageCursor", XtCCursor, XtRCursor, sizeof(Cursor),
621abf7346Smrg     Offset(cursors.manpage), XtRString, MANPAGE_CURSOR},
631abf7346Smrg  {"searchEntryCursor", XtCCursor, XtRCursor, sizeof(Cursor),
641abf7346Smrg     Offset(cursors.search_entry), XtRString, SEARCH_ENTRY_CURSOR},
651abf7346Smrg  {"pointerColor", XtCForeground, XtRPixel, sizeof(Pixel),
661abf7346Smrg     Offset(cursors.fg_color), XtRString, "XtDefaultForeground"},
671abf7346Smrg  {"pointerColorBackground", XtCBackground, XtRPixel, sizeof(Pixel),
681abf7346Smrg     Offset(cursors.bg_color), XtRString, "XtDefaultBackground"},
691abf7346Smrg  {"help", XtCBoolean, XtRBoolean, sizeof(Boolean),
708b6d6341Smrg     Offset(show_help_syntax), XtRImmediate, NULL},
711abf7346Smrg  {"helpFile", XtCFile, XtRString, sizeof(char *),
721abf7346Smrg     Offset(help_file), XtRString, HELPFILE},
731abf7346Smrg  {"topBox", XtCBoolean, XtRBoolean, sizeof(Boolean),
741abf7346Smrg     Offset(top_box_active), XtRString, "True"},
751abf7346Smrg  {"clearSearchString", "ClearSearchString", XtRBoolean, sizeof(Boolean),
761abf7346Smrg     Offset(clear_search_string), XtRImmediate, (caddr_t) TRUE},
771abf7346Smrg  {"title", XtCString, XtRString, sizeof(char *),
781abf7346Smrg     Offset(title), XtRString, "xman"},
791abf7346Smrg  {"iconic", XtCBoolean, XtRBoolean, sizeof(Boolean),
801abf7346Smrg     Offset(iconic), XtRString, "False"},
811abf7346Smrg};
821abf7346Smrg
831abf7346Smrg#undef Offset
841abf7346Smrg
851abf7346Smrg/*
861abf7346Smrg * The resource that we absolutely need.
871abf7346Smrg */
881abf7346Smrg
891abf7346Smrgstatic char * fallback_resources[] = { "Xman*quitButton.translations:	#override \\n   <Btn1Up>: Quit() reset()",
901abf7346Smrg "Xman*helpButton.sensitive:                    FALSE",
911abf7346Smrg "Xman*manpageButton.sensitive:                 FALSE",
921abf7346Smrg "Xman*helpButton.Label:			Help",
931abf7346Smrg "Xman*quitButton.Label:			Quit",
941abf7346Smrg "Xman*manpageButton.Label:		        Manual Page",
951abf7346Smrg "Xman*topLabel.label:         		        No App-Defaults File",
961abf7346Smrg NULL,
971abf7346Smrg};
981abf7346Smrg
991abf7346Smrg/*
1001abf7346Smrg * This is necessary to keep all TopLevel shells from becoming
1011abf7346Smrg * the size that is specified on the command line.
1021abf7346Smrg */
1031abf7346Smrg
1041abf7346Smrgstatic XrmOptionDescRec xman_options[] = {
1051abf7346Smrg{"-geometry", "*topBox.geometry",        XrmoptionSepArg, (caddr_t) NULL},
1061abf7346Smrg{"-help",     "help",                    XrmoptionNoArg,  (caddr_t) "True"},
1071abf7346Smrg{"=",         "*topBox.geometry",        XrmoptionIsArg,  (caddr_t) NULL},
1081abf7346Smrg{"-pagesize", "*manualBrowser.geometry", XrmoptionSepArg, (caddr_t) NULL},
1091abf7346Smrg{"-notopbox", "topBox",                  XrmoptionNoArg,  (caddr_t) "False"},
1101abf7346Smrg{"-helpfile", "helpFile",                XrmoptionSepArg, (caddr_t) NULL},
1111abf7346Smrg{"-bothshown","bothShown",               XrmoptionNoArg,  (caddr_t) "True"},
1121abf7346Smrg{"-title",    "title",                   XrmoptionSepArg, (caddr_t) "xman"},
1131abf7346Smrg{"-iconic",   "iconic",                  XrmoptionNoArg,  (caddr_t) "True"},
1141abf7346Smrg};
1151abf7346Smrg
1168b6d6341Smrgstatic XtActionsRec xman_actions[] = {
1171abf7346Smrg  {"GotoPage",          GotoPage},
1181abf7346Smrg  {"Quit",              Quit},
1191abf7346Smrg  {"Search",            Search},
1201abf7346Smrg  {"PopupHelp",         PopupHelp},
1211abf7346Smrg  {"PopupSearch",       PopupSearch},
1221abf7346Smrg  {"CreateNewManpage",  CreateNewManpage},
1231abf7346Smrg  {"RemoveThisManpage", RemoveThisManpage},
1241abf7346Smrg  {"SaveFormattedPage", SaveFormattedPage},
1251abf7346Smrg#ifdef INCLUDE_XPRINT_SUPPORT
1261abf7346Smrg  {"PrintThisManpage",  PrintThisManpage},
1271abf7346Smrg#endif /* INCLUDE_XPRINT_SUPPORT */
1281abf7346Smrg  {"ShowVersion",       ShowVersion},
1291abf7346Smrg};
1301abf7346Smrg
1311abf7346Smrgchar **saved_argv;
1321abf7346Smrgint saved_argc;
1331abf7346Smrg
1341abf7346Smrg/*
1351abf7346Smrg * This atom is used to make the application ICCCM compliant.
1361abf7346Smrg */
1371abf7346SmrgAtom wm_delete_window;
1381abf7346Smrg
1391abf7346Smrg/*	Function Name: main
1401abf7346Smrg *	Description: This is the main driver for Xman.
1411abf7346Smrg *	Arguments: argc, argv - the command line arguments.
1421abf7346Smrg *	Returns: return, what return.
1431abf7346Smrg */
1441abf7346Smrg
1451abf7346Smrgint main(int argc, char ** argv)
1461abf7346Smrg{
1471abf7346Smrg  XtAppContext app_con;
1481abf7346Smrg
1491abf7346Smrg  saved_argc = argc;
1501abf7346Smrg  saved_argv = (char **)XtMalloc(argc * sizeof(char *));
1511abf7346Smrg  bcopy(argv, saved_argv, argc * sizeof(char *));
1521abf7346Smrg
1531abf7346Smrg  XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL);
1541abf7346Smrg
1551abf7346Smrg  initial_widget = XtAppInitialize(&app_con, "Xman", xman_options,
1561abf7346Smrg				   XtNumber(xman_options), &argc, argv,
1571abf7346Smrg				   fallback_resources, NULL, ZERO);
1581abf7346Smrg  wm_delete_window = XInternAtom(XtDisplay(initial_widget), "WM_DELETE_WINDOW",
1591abf7346Smrg				 False);
1601abf7346Smrg
1611abf7346Smrg  manglobals_context = XStringToContext(MANNAME);
1621abf7346Smrg
1631abf7346Smrg  AdjustDefResources();
1641abf7346Smrg
1651abf7346Smrg  XtGetApplicationResources( initial_widget, (caddr_t) &resources,
1661abf7346Smrg			    my_resources, XtNumber(my_resources),
1671abf7346Smrg			    NULL, (Cardinal) 0);
1681abf7346Smrg
1691abf7346Smrg  if ( (argc != 1) || (resources.show_help_syntax) ) {
1701abf7346Smrg    ArgError(argc, argv);
1711abf7346Smrg    return EXIT_FAILURE;
1721abf7346Smrg  }
1731abf7346Smrg
1741abf7346Smrg  XtAppAddActions(app_con, xman_actions, XtNumber(xman_actions));
1751abf7346Smrg
1761abf7346Smrg  if (!resources.fonts.directory)
1771abf7346Smrg	PrintError("Failed to get the directory font.");
1781abf7346Smrg
1791abf7346Smrg#ifdef DEBUG
1801abf7346Smrg  printf("debugging mode\n");
1811abf7346Smrg#endif
1821abf7346Smrg
1831abf7346Smrg/*
1841abf7346Smrg * Set the default width and height.
1851abf7346Smrg * I am not real happy with this method, but it will usually do something
1861abf7346Smrg * reasonable, if not the "right" thing.  It is not a real big issue since
1871abf7346Smrg * it is easy to change the values with resources or command line options.
1881abf7346Smrg * NOTE: if you are in a 100 dpi display you will lose.
1891abf7346Smrg */
1901abf7346Smrg
1911abf7346Smrg  default_width = DEFAULT_WIDTH;
1921abf7346Smrg  default_height=DisplayHeight(XtDisplay(initial_widget),
1931abf7346Smrg			       DefaultScreen(XtDisplay(initial_widget)));
1941abf7346Smrg  default_height *= 3;
1951abf7346Smrg  default_height /= 4;
1961abf7346Smrg
1971abf7346Smrg  if ( (sections = Man()) == 0 )
1981abf7346Smrg    PrintError("There are no manual sections to display, check your MANPATH.");
1991abf7346Smrg
2001abf7346Smrg  if (resources.top_box_active)
2011abf7346Smrg    MakeTopBox();
2021abf7346Smrg  else
2031abf7346Smrg    (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
2141abf7346Smrg  man_pages_shown = 1;
2151abf7346Smrg
2161abf7346Smrg  XtAppMainLoop(app_con);
2171abf7346Smrg
2181abf7346Smrg  return EXIT_SUCCESS;
2191abf7346Smrg}
2201abf7346Smrg
2211abf7346Smrg/*	Function Name: ArgError
2221abf7346Smrg *	Description:  Prints error message about unknow arguments.
2231abf7346Smrg *	Arguments: argc, argv - args not understood.
2241abf7346Smrg *	Returns: none.
2251abf7346Smrg */
2261abf7346Smrg
2271abf7346Smrgstatic void
2281abf7346SmrgArgError(int argc, char ** argv)
2291abf7346Smrg{
2301abf7346Smrg  int i;
2311abf7346Smrg
2321abf7346Smrg  static char **syntax, *syntax_def[] = {
2331abf7346Smrg  "-help",                   "Print this message",
2341abf7346Smrg  "-helpfile <filename>",    "Specifies the helpfile to use.",
2351abf7346Smrg  "-bothshown",              "Show both the directory and manpage at once.",
2361abf7346Smrg  "-notopbox",               "Starts with manpage rather than topbox.",
2371abf7346Smrg  "-geometry <geom>",        "Specifies the geometry of the top box.",
2381abf7346Smrg  "=<geom>",                 "Specifies the geometry of the top box.",
2391abf7346Smrg  "-pagesize <geom>",        "Specifies the geometry of the manual page.",
2401abf7346Smrg  "-bw <pixels>",            "Width of all window borders.",
2411abf7346Smrg  "-borderwidth <pixels>",   "Width of all window borders.",
2421abf7346Smrg  "-bd <color>",             "Color of all window borders.",
2431abf7346Smrg  "-bordercolor <color>",    "Color of all window borders.",
2441abf7346Smrg  "-fg <color>",             "Foreground color for the application.",
2451abf7346Smrg  "-foreground <color>",     "Foreground color for the application.",
2461abf7346Smrg  "-bg <color>",             "Background color for the application.",
2471abf7346Smrg  "-background <color>",     "Background color for the application.",
2481abf7346Smrg  "-display <display name>", "Specify a display that is not the default",
2491abf7346Smrg  "-fn <font>",              "Font to be used for button and label text.",
2501abf7346Smrg  "-font <font>",            "Font to be used for button and label text.",
2511abf7346Smrg  "-name <name>",            "Change the name used for retrieving resources.",
2521abf7346Smrg  "-title <name>",           "Change the name without affecting resources.",
2531abf7346Smrg  "-xrm <resource>",         "Specifies a resource on the command line.",
2541abf7346Smrg  NULL, NULL,
2551abf7346Smrg  };
2561abf7346Smrg
2571abf7346Smrg  syntax = syntax_def;
2581abf7346Smrg
2591abf7346Smrg  for (i = 1; i < argc ; i++)
2601abf7346Smrg    (void) printf("This argument is unknown to Xman: %s\n", argv[i]);
2611abf7346Smrg
2621abf7346Smrg  (void) printf("\nKnown arguments are:\n");
2631abf7346Smrg
2641abf7346Smrg  while ( *syntax != NULL ) {
2651abf7346Smrg    printf("%-30s - %s\n", syntax[0], syntax[1]);
2661abf7346Smrg    syntax += 2;
2671abf7346Smrg  }
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{
2801abf7346Smrg  char        *xwinhome = NULL;
2811abf7346Smrg  int i;
2821abf7346Smrg
2831abf7346Smrg  if (!(xwinhome = getenv("XWINHOME")))
2841abf7346Smrg    return;
2851abf7346Smrg
2861abf7346Smrg  for (i = 0; i < sizeof(my_resources)/sizeof(XtResource); i++) {
2871abf7346Smrg    if (!strcmp(my_resources[i].resource_name, "helpFile")) {
2881abf7346Smrg      if (!(my_resources[i].default_addr =
2891abf7346Smrg              malloc(strlen(xwinhome) + sizeof("/lib/X11/xman.help")))) {
2901abf7346Smrg        fprintf(stderr, "malloc failure\n");
2911abf7346Smrg        exit(EXIT_FAILURE);
2921abf7346Smrg      }
2931abf7346Smrg      sprintf(my_resources[i].default_addr, "%s/lib/X11/xman.help", xwinhome);
2941abf7346Smrg    }
2951abf7346Smrg  }
2961abf7346Smrg}
297