home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / xgopher.1.3 / xgopher.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  5.1 KB  |  226 lines

  1. /* xgopher.c
  2.    Xgopher main program */
  3.  
  4.      /*---------------------------------------------------------------*/
  5.      /* Xgopher        version 1.3     08 April 1993                  */
  6.      /*                version 1.2     20 November 1992               */
  7.      /*                version 1.1     20 April 1992                  */
  8.      /*                version 1.0     04 March 1992                  */
  9.      /* X window system client for the University of Minnesota        */
  10.      /*                                Internet Gopher System.        */
  11.      /* Allan Tuchman, University of Illinois at Urbana-Champaign     */
  12.      /*                Computing and Communications Services Office   */
  13.      /* Copyright 1992, 1993 by                                       */
  14.      /*           the Board of Trustees of the University of Illinois */
  15.      /* Permission is granted to freely copy and redistribute this    */
  16.      /* software with the copyright notice intact.                    */
  17.      /*---------------------------------------------------------------*/
  18.  
  19. #include <stdio.h>
  20.  
  21. #include "conf.h"
  22. #include "gui.h"
  23. #include "appres.h"
  24. #include "util.h"
  25. #include "osdep.h"
  26. #include "version.h"
  27.  
  28. #define GLOBALS
  29. #include "globals.h"
  30. #undef GLOBALS
  31.  
  32. BOOLEAN    getOptions(
  33. #ifdef PROTO
  34.     int,
  35.     char **
  36. #endif
  37. );
  38.  
  39.  
  40.  
  41. /* noOptions
  42.    Cannot find the resources file... help the user a bit */
  43.  
  44. void
  45. noOptions()
  46. {
  47.     static char    *optErrText[] = {
  48.     "\n  The Xgopher Application Resources file is not available.",
  49.     "  The system administrator should install the resources file in a",
  50.     "  site-dependent location.",
  51.     "\n  This file can also be located in your own app-defaults directory,",
  52.     "  or the contents can be copied to one of your other X resource",
  53.     "  files.",
  54.     "\n  Xgopher cannot continue without this file.",
  55.         (char *) NULL };
  56.  
  57.     int    i = 0;
  58.  
  59.     while (optErrText[i] != (char *) NULL) {
  60.         fprintf (stderr, "%s\n", optErrText[i]);
  61.         i++;
  62.     }
  63. }
  64.  
  65.  
  66. /* usage
  67.    print usage message */
  68.  
  69. usage(name)
  70. char    *name;
  71. {
  72.     fprintf (stderr, "usage:\n");
  73.     fprintf (stderr,
  74.         "\t%s [gopher server [port number]] [X Toolkit options].\n",
  75.         name);
  76. }
  77.  
  78.  
  79. /* resourcesMismatch
  80.    inform user that the resource file is for the wrong version of Xgopher */
  81.  
  82. resourcesMismatch(theseResources, thisVersion)
  83. char    *theseResources, *thisVersion;
  84. {
  85.     fprintf (stderr, "  This is Xgopher version %s.\n", thisVersion);
  86.     fprintf (stderr,
  87.         "  The X resources file available is for version %s.\n",
  88.         theseResources);
  89.     fprintf (stderr,
  90.         "  Please provide the correct resources for this version.\n");
  91. }
  92.  
  93.  
  94. /* cmdLineOptions
  95.    Look for left over command line options after Xt has a chance. */
  96.  
  97. static BOOLEAN
  98. cmdLineOptions(argc, argv)
  99. int    argc;
  100. char    *argv[];
  101. {
  102.     /* see if Xt left us any options.  If there is exactly one,
  103.        and it has no leading '-', use it as the rootServer.
  104.        If there are exactly two, neither with a leading '-',
  105.        and the second is a number greater than zero, then use
  106.        them as the rootServer and rootPort.
  107.  
  108.        We may leak a few bytes in the following code, but there
  109.        is no guarantee that the previous values of the resources
  110.        were dynamically allocated. */
  111.  
  112.     if ((argc == 2  && argv[1][0] != '-')  ||
  113.         (argc == 3  && argv[1][0] != '-'  && argv[2][0] != '-')) {
  114.         int    hostLen = sizeof(char) * strlen(argv[1]) + 1;
  115.         char    *tmp;
  116.  
  117.         if (argc == 3) {
  118.             int    port=atoi(argv[2]);
  119.             if (port > 0) {
  120.                 appResources->rootPort = port;
  121.             } else {
  122.                 return FALSE;
  123.             }
  124.  
  125.         }
  126.  
  127.         tmp = malloc(hostLen);
  128.         strcpy(tmp, argv[1]);
  129.         appResources->rootServer = tmp;
  130.  
  131.         tmp = malloc(hostLen);
  132.         strcpy(tmp, argv[1]);
  133.         appResources->mainTitle = tmp;
  134.  
  135.  
  136.     } else if (argc > 1) {
  137.         return FALSE;
  138.     }
  139.  
  140.     return TRUE;
  141. }
  142.  
  143.  
  144. /* doMainItem
  145.    create and process the main, top level gopher item for this session. */
  146.  
  147. void
  148. doMainItem()
  149. {
  150.     gopherItemP        mainItem;
  151.     char            errorString[MESSAGE_STRING_LEN];
  152.  
  153.     mainItem = makeItem(A_DIRECTORY,
  154.                 appResources->mainTitle, appResources->rootPath,
  155.                 appResources->rootServer, appResources->rootPort,
  156.                 FALSE);
  157.             
  158.  
  159.     if (! processItem(mainItem)) {
  160.         sprintf(errorString,
  161. "Unrecoverable error: cannot access the main %s directory at\nhost: %s port %d", 
  162.             GOPHER, appResources->rootServer,
  163.             appResources->rootPort);
  164.         showFatalError(errorString);
  165.  
  166.     }
  167.  
  168.     if (appResources->markRoot) {
  169.         markCurrentDirectory();
  170.     }
  171.  
  172.     if (appResources->loadBookmarks) {
  173.         loadMarks();
  174.     }
  175. }
  176.  
  177.  
  178. /* xgopher
  179.    main program */
  180.  
  181. main(argc, argv)
  182. int    argc;
  183. char    **argv;
  184. {
  185.     char            *p;
  186.  
  187.     /* save last component of path name */
  188.  
  189.     if ((p = rindex(argv[0], '/')) == 0) p = argv[0];
  190.     strcpy(gopherName, p);
  191.  
  192.     /* initialize the Graphical User Interface, the X window system */
  193.  
  194.     if (! initGUI(&argc, argv)) {
  195.         noOptions();
  196.         exit(1);
  197.     }
  198.  
  199.     /* ensure resources version is correct */
  200.  
  201.     if (strcmp(appResources->resourcesVersion, XGOPHER_VERSION) != 0) {
  202.         resourcesMismatch(appResources->resourcesVersion,
  203.                 XGOPHER_VERSION);
  204.         closeGUIandQuit(1);
  205.     }
  206.  
  207.     /* command line options may override server host and port */
  208.  
  209.     if (! cmdLineOptions(argc, argv) ) {
  210.         usage(argv[0]);
  211.         closeGUIandQuit(1);
  212.     }
  213.  
  214.     /* options are Ok, create the main Xgopher panel */
  215.  
  216.     initMainPanel();
  217.  
  218.     /* initialize each item class */
  219.  
  220.     initItemClasses();
  221.  
  222.     doMainItem();
  223.  
  224.     doUserRequests();
  225. }
  226.