home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / x / xwais.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-07  |  12.3 KB  |  475 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.
  4.  
  5.    This is part of the X user-interface for the WAIS software.  Do with it
  6.    as you please.
  7.  
  8.    jonathan@Think.COM
  9.  
  10. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  11.  
  12. /*
  13.  * $Log: xwais.c,v $
  14.  * Revision 1.3  1994/08/08  07:33:13  pfeifer
  15.  * Moved wais_log_file_name and waislogfile to cutil.[ch]
  16.  *
  17.  * Revision 1.2  1994/08/05  07:28:40  pfeifer
  18.  * Release beta 04
  19.  *
  20.  * Revision 1.1  1993/02/16  15:10:18  freewais
  21.  * Initial revision
  22.  *
  23.  * Revision 1.17  92/06/03  17:09:21  jonathan
  24.  * Changed Scandirs to leave listwidgets where they are.  Less confusing.
  25.  * 
  26.  * Revision 1.16  92/04/28  15:33:35  jonathan
  27.  * Changed references to directory reading functions to use scandir.
  28.  * 
  29.  * Revision 1.15  92/03/23  16:11:11  jonathan
  30.  * ?
  31.  * 
  32.  * Revision 1.14  92/03/17  14:20:24  jonathan
  33.  * Generally cleaned up.
  34.  * 
  35.  * Revision 1.13  92/03/07  19:39:42  jonathan
  36.  * Fixed argument to ScanDirs.
  37.  * 
  38.  * Revision 1.12  92/03/06  14:48:19  jonathan
  39.  * New and Improved source loading!
  40.  * 
  41.  * Revision 1.11  92/03/05  11:47:09  jonathan
  42.  * Replace directory routines with scandir's.
  43.  * 
  44.  * Revision 1.10  92/03/01  14:03:40  jonathan
  45.  * Added command_name to main.
  46.  * 
  47.  * 
  48. */
  49.  
  50. #ifndef lint
  51. static char *RCSid = "$Header: /usr/local/ls6/src+data/src/freeWAIS-sf/x/RCS/xwais.c,v 1.3 1994/08/08 07:33:13 pfeifer Exp $";
  52. #endif
  53.  
  54. #define MAIN
  55. #define XWAIS_C
  56. #define XWAIS
  57. #include "xwais.h"
  58. #include "xwais.bit"
  59. #include <sys/stat.h>
  60.  
  61. #define offset(field) XtOffset(struct _app_resources*, field)
  62. static XtResource resources[] = {
  63.   {"questionDirectory", "QuestionDirectory", XtRString, sizeof(char *),
  64.          offset(questionDirectory), XtRString, "~/wais-questions/"},
  65.    {"userSourceDirectory", "UserSourceDirectory", XtRString, sizeof(char *),
  66.          offset(userSourceDirectory), XtRString, "~/wais-sources/"},
  67.    {"commonSourceDirectory", "CommonSourceDirectory", XtRString, sizeof(char *),
  68.          offset(commonSourceDirectory), XtRString, ""},
  69.    {"documentDirectory", "DocumentDirectory", XtRString, sizeof(char *),
  70.          offset(documentDirectory), XtRString, "~/wais-documents/"},
  71.    {"helpFile", "HelpFile", XtRString, sizeof(char *),
  72.          offset(helpFile), XtRString, "./XwaisHELP"},
  73.    {"removeSeekerCodes", "RemoveSeekerCodes", XtRString, sizeof(char *),
  74.       offset(removeSeekerCodes), XtRString, "On"},
  75.    {"rescanInterval", "RescanInterval", XtRString, sizeof(char *),
  76.       offset(rescanInterval), XtRString, "1"},
  77.    {"seedWords", "SeedWords", XtRString, sizeof(char *),
  78.       offset(seedWords), XtRString, ""},
  79.    {"initialSource", "initialSource", XtRString, sizeof(char *),
  80.       offset(initialSource), XtRString, ""},
  81.    {"questionName", "QuestionName", XtRString, sizeof(char *),
  82.       offset(questionName), XtRString, ""},
  83.    {"filters", "Filters", XtRString, sizeof(char *),
  84.       offset(filters), XtRString, ""},
  85.    {"doSearch", "DoSearch", XtRBoolean, sizeof(Boolean),
  86.       offset(doSearch), XtRBoolean, False},
  87.    {"defaultsInstalled", "DefaultsInstalled",  XtRBoolean, sizeof(Boolean),
  88.       offset(defaultsInstalled), XtRBoolean, False}
  89. };
  90. #undef offset
  91.  
  92. void SetIcon(parent)
  93. Widget parent;
  94. {
  95.   Arg args[1];
  96.   Pixmap icon_pixmap = None;
  97.  
  98.   XtSetArg (args[0], XtNiconPixmap, &icon_pixmap);
  99.   XtGetValues(parent, args, ONE);
  100.   if (icon_pixmap == None) {
  101.     XtSetArg(args[0], XtNiconPixmap, 
  102.          XCreateBitmapFromData(XtDisplay(parent),
  103.                    XtScreen(parent)->root,
  104.                    xwais_bits, xwais_width, xwais_height));
  105.     XtSetValues (parent, args, ONE);
  106.   }
  107. }
  108.  
  109. static quit;
  110.  
  111. static int
  112. my_alphasort(d1, d2)
  113.     struct dirent **d1;
  114.     struct dirent **d2;
  115. {
  116.     return strcasecmp(d1[0]->d_name, d2[0]->d_name);
  117. }
  118.  
  119. static int
  120. isqfile(dp)
  121. struct dirent *dp;
  122. {
  123.   char lastchar = dp->d_name[strlen(dp->d_name)-1];
  124.   return(lastchar != '~' && lastchar != '#' &&
  125.       strcmp(dp->d_name, ".") && strcmp(dp->d_name, ".."));
  126. }
  127.  
  128. void
  129. ReadQuestionDirectory(directory)
  130. char *directory;
  131. {
  132.   struct dirent **list;
  133.   char filename[MAX_FILENAME_LEN], lastchar;
  134.   FILE *fp;
  135.   char config[STRINGSIZE];
  136.   int i, j;
  137.   float shown;
  138.  
  139.   if ((j = scandir(directory, &list, isqfile, my_alphasort)) < 0)
  140.     {
  141.       char booboo[STRINGSIZE];
  142.       sprintf(booboo, "Error on open of questions directory: %s.\n", directory);
  143.       XwaisPrintf(booboo);
  144.       return;
  145.     }
  146.  
  147.   if(Question_items != NULL) {
  148.     for(i =0; Question_items[i] != NULL; i++) s_free(Question_items[i]);
  149.     s_free(Question_items);
  150.   }
  151.  
  152.   Question_items = (char**) s_malloc((j+1) * sizeof(char*));
  153.  
  154.   for (i = 0; i < j; i++) {
  155.     Question_items[i] = s_strdup(list[i]->d_name);
  156.     s_free(list[i]);
  157.   }
  158.  
  159.   NumQuestions = j;
  160.   s_free(list);
  161. }
  162.  
  163. static time_t usersourcetime, commonsourcetime, questiontime;
  164. static int rescanint;
  165.  
  166. void ScanDirs(closure, id)
  167.      Opaque closure;
  168.      XtIntervalId *id;
  169. {
  170.   char **list_data;
  171.   float top, shown;
  172.   int CurrentQuestion, CurrentSource, i;
  173.   struct stat buf;
  174.   boolean rescan;
  175.   char string[STRINGSIZE];
  176.  
  177.   rescan = FALSE;
  178.  
  179.   stat(app_resources.userSourceDirectory, &buf);
  180.  
  181.   if(buf.st_mtime != usersourcetime) {
  182.     usersourcetime = buf.st_mtime;
  183.     rescan = TRUE;
  184.   }
  185.  
  186.   if(app_resources.commonSourceDirectory[0] != 0) {
  187.     stat(app_resources.commonSourceDirectory, &buf);
  188.  
  189.     if(buf.st_mtime != commonsourcetime) {
  190.       commonsourcetime = buf.st_mtime;
  191.       rescan = TRUE;
  192.     }
  193.   }
  194.  
  195.   if (rescan) {
  196.     rescan = FALSE;
  197.  
  198.     CurrentSource = get_selected_source();
  199.     if (CurrentSource != NO_ITEM_SELECTED)
  200.       strcpy(string, Source_items[CurrentSource]);
  201.  
  202.     NumSources = 0;
  203.  
  204.     GetSourceNames(app_resources.userSourceDirectory);
  205.     if(app_resources.commonSourceDirectory[0] != 0)
  206.       GetSourceNames(app_resources.commonSourceDirectory);
  207.  
  208.     RebuildListWidget(sourcewindow, Source_items, LIST_NONE);
  209.  
  210.     if(CurrentSource != NO_ITEM_SELECTED) {
  211.       for(i = 0;
  212.       Source_items[i] != NULL && 
  213.       strcmp(Source_items[i], string) != 0;
  214.       i++);
  215. #ifndef MOTIF
  216.       if(i < NumSources) XawListHighlight(sourcewindow->ListWidget, i);
  217. #endif
  218.     }
  219.   }
  220.  
  221.   stat(app_resources.questionDirectory, &buf);
  222.  
  223.   if(buf.st_mtime != questiontime) {
  224.     questiontime = buf.st_mtime;
  225.     rescan = TRUE;
  226.   }
  227.  
  228.   if (rescan) {
  229.     NumQuestions = 0;
  230.  
  231.     CurrentQuestion = get_selected_question();
  232.  
  233.     if (CurrentQuestion != NO_ITEM_SELECTED)
  234.       strcpy(string, Question_items[CurrentQuestion]);
  235.  
  236.     ReadQuestionDirectory(app_resources.questionDirectory);
  237.     Question_items[NumQuestions] = NULL;
  238.  
  239.     RebuildListWidget(questionwindow, Question_items, LIST_NONE);
  240.  
  241.     if(CurrentQuestion != NO_ITEM_SELECTED) {
  242.       for(i = 0;
  243.       Question_items[i] != NULL &&
  244.       strcmp(Question_items[i], string) != 0;
  245.       i++);
  246.       if(i < NumQuestions) XawListHighlight(questionwindow->ListWidget, i);
  247.     }
  248.   }
  249.  
  250.   rescantimerid = XtAddTimeOut(rescanint, ScanDirs, (Opaque) ScanDirs);
  251. }
  252.  
  253. void ExitCommand()
  254. {
  255.   exit(-1);
  256. }
  257.  
  258. PopExit(parent, message)
  259. Widget parent;
  260. char *message;
  261. {
  262.   Widget shell, frame, labelwid, stringlabelwid;
  263.   WidgetClass wclass;
  264.   static String namestring;
  265.   Arg        args[5];
  266.   Position    x, y;
  267.   Dimension    width, height;
  268.   Cardinal    n;
  269.  
  270.   shell = XtCreatePopupShell("exitpopup", applicationShellWidgetClass,
  271.                  parent, NULL, ZERO);
  272.   frame = XtCreateManagedWidget("exitpopupform", formWidgetClass,
  273.                 shell, NULL, ZERO);
  274.  
  275.   labelwid = MakeLabel(frame, "exitlabel", message, NULL, NULL);
  276.  
  277.   MakeCommandButton(frame, "Ok", ExitCommand, labelwid, NULL, NULL);
  278.  
  279.   n = 0;
  280.   XtSetArg(args[n], XtNx, 100); n++;
  281.   XtSetArg(args[n], XtNy, 100); n++;
  282.   XtSetArg(args[n], XtNtitle, "XWAIS Error"); n++;
  283.   XtSetValues(shell, args, n);
  284.  
  285.   XtPopup(shell, XtGrabExclusive);
  286. }
  287.  
  288. void
  289. main(argc, argv)
  290.      int argc;
  291.      char *argv[];
  292. {
  293.   struct stat buf;
  294.   long first_filename_number = 1; /* for indexing into the arglist */
  295.   long count;
  296.  
  297.  
  298.  
  299.  
  300.   if (command_name = (char*)rindex(argv[0], '/'))
  301.     command_name++;
  302.   else
  303.     command_name = argv[0];
  304.  
  305.   
  306.  
  307.   NumSources = 0;
  308.  
  309.   double_click = FALSE;
  310.  
  311.   top = XtInitialize( "xwais", "Xwais", NULL, 0, &argc, argv);
  312.   
  313.   XtGetApplicationResources(top, &app_resources, resources,
  314.                 XtNumber(resources), NULL, 0);
  315.  
  316.   {
  317.     Arg        args[5];
  318.     Cardinal    n;
  319.  
  320.     n = 0;
  321.     XtSetArg(args[n], XtNtitle, "XWAIS"); n++;
  322.     XtSetArg(args[n], XtNiconName, "XWAIS"); n++;
  323.     XtSetValues(top, args, n);
  324.   }
  325.  
  326.   SetIcon(top);
  327.  
  328.   if (app_resources.defaultsInstalled == False) {
  329.     PopExit(top, "X resources not properly installed");
  330.     XtMainLoop();
  331.   }
  332.  
  333.   CurDpy = XtDisplay(top);
  334.  
  335.   form = SetupWaisDisplay(top);
  336.  
  337.   if(app_resources.documentDirectory[0] == '~') {
  338.     char *home, *dir, *getenv();
  339.     
  340.     if((home = getenv("HOME")) != NULL) {
  341.       if((dir = s_malloc(strlen(home) +
  342.                strlen(app_resources.documentDirectory) +
  343.                2)) == NULL) {
  344.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  345.     exit(-1);
  346.       }
  347.       strcpy(dir, home);
  348.       strcat(dir, &app_resources.documentDirectory[1]);
  349.       app_resources.documentDirectory=dir;
  350.     }
  351.   }
  352.  
  353.   if(app_resources.userSourceDirectory[0] == '~') {
  354.     char *home, *dir, *getenv();
  355.     
  356.     if((home = getenv("HOME")) != NULL) {
  357.       if((dir = s_malloc(strlen(home) +
  358.                strlen(app_resources.userSourceDirectory) +
  359.                2)) == NULL) {
  360.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  361.     exit(-1);
  362.       }
  363.       strcpy(dir, home);
  364.       strcat(dir, &app_resources.userSourceDirectory[1]);
  365.       app_resources.userSourceDirectory=dir;
  366.     }
  367.   }
  368.   sdir = app_resources.userSourceDirectory;
  369.   cdir = app_resources.commonSourceDirectory;
  370.  
  371.   if(app_resources.questionDirectory[0] == '~') {
  372.     char *home, *dir, *getenv();
  373.     
  374.     if((home = getenv("HOME")) != NULL) {
  375.       if((dir = s_malloc(strlen(home) +
  376.                strlen(app_resources.questionDirectory) +
  377.                2)) == NULL) {
  378.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  379.     exit(-1);
  380.       }
  381.       strcpy(dir, home);
  382.       strcat(dir, &app_resources.questionDirectory[1]);
  383.       app_resources.questionDirectory=dir;
  384.     }
  385.   }
  386.  
  387.   /* let's see if user directories exist, if not, try to create them */
  388.   {
  389.     DIR *dirp;
  390.     char *makedir;
  391.  
  392.     if((dirp = opendir(app_resources.questionDirectory)) == NULL) {
  393.       if((makedir = s_malloc(strlen(app_resources.questionDirectory) + 12))
  394.      == NULL) {
  395.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  396.     exit(-1);
  397.       }
  398.       strcpy(makedir, "/bin/mkdir ");
  399.       strcat(makedir, app_resources.questionDirectory);
  400.       if(makedir[strlen(makedir)-1] == '/') makedir[strlen(makedir)-1] = 0;
  401.       if(system(makedir) != 0)
  402.     fprintf(stderr, "Error creating directory: %s\n",
  403.         app_resources.questionDirectory);
  404.     }
  405.     else
  406.       closedir(dirp);
  407.  
  408.     if((dirp = opendir(app_resources.userSourceDirectory)) == NULL) {
  409.       if((makedir = s_malloc(strlen(app_resources.userSourceDirectory) + 12))
  410.      == NULL) {
  411.     fprintf(stderr, "Ran out of space trying to create directory name.\n");
  412.     exit(-1);
  413.       }
  414.       strcpy(makedir, "/bin/mkdir ");
  415.       strcat(makedir, app_resources.userSourceDirectory);
  416.       if(makedir[strlen(makedir)-1] == '/') makedir[strlen(makedir)-1] = 0;
  417.       if(system(makedir) != 0)
  418.     fprintf(stderr, "Error creating directory: %s\n",
  419.         app_resources.userSourceDirectory);
  420.     }
  421.     else
  422.       closedir(dirp);
  423.   }
  424.   
  425.   stat(app_resources.userSourceDirectory, &buf);
  426.   usersourcetime = buf.st_mtime;
  427.   GetSourceNames(app_resources.userSourceDirectory);
  428.  
  429.   if(app_resources.commonSourceDirectory[0] != 0) {
  430.     stat(app_resources.commonSourceDirectory, &buf);
  431.     commonsourcetime = buf.st_mtime;
  432.     GetSourceNames(app_resources.commonSourceDirectory);
  433.   }
  434.  
  435.   stat(app_resources.questionDirectory, &buf);
  436.   questiontime = buf.st_mtime;
  437.   ReadQuestionDirectory(app_resources.questionDirectory);
  438.  
  439.   Question_items[NumQuestions] = NULL;
  440.  
  441.   RebuildListWidget(questionwindow, Question_items, LIST_NONE);
  442.  
  443.   RebuildListWidget(sourcewindow, Source_items, LIST_NONE);
  444.  
  445.   /* and away we go! */
  446.  
  447.   rescanint = 1000 * atoi(app_resources.rescanInterval);
  448.  
  449.   rescantimerid = XtAddTimeOut(rescanint, ScanDirs, (Opaque) ScanDirs);
  450.  
  451.   XtRealizeWidget(top);
  452.  
  453.   XtMainLoop();
  454. }
  455.  
  456. /* ARGSUSED */
  457. void
  458. DoQuit(w, closure, call_data)
  459. Widget w;
  460. XtPointer closure, call_data;
  461. {
  462.   char msg[STRINGSIZE], quest[STRINGSIZE];
  463.  
  464.   if(double_click && LastClicked == w) {
  465.     exit(0); 
  466.   }
  467.  
  468.   msg[0] = 0;
  469.  
  470.   XwaisPrintf("If you really want to quit, press Quit again.\n");
  471.   Feep();
  472.   double_click = TRUE;
  473.   LastClicked = w;
  474. }
  475.