home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XGAMES / SPIDER.TAR / spider / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-28  |  4.4 KB  |  249 lines

  1. /*
  2.  *    Spider
  3.  *
  4.  *    (c) Copyright 1989, Donald R. Woods and Sun Microsystems, Inc.
  5.  *    (c) Copyright 1990, David Lemke and Network Computing Devices Inc.
  6.  *
  7.  *    See copyright.h for the terms of the copyright.
  8.  *
  9.  *    @(#)util.c    2.3    90/04/30
  10.  *
  11.  */
  12.  
  13. /*
  14.  * misc utility funcs
  15.  */
  16.  
  17. #include    "defs.h"
  18. #include    "globals.h"
  19. #ifndef KITLESS
  20. #include    <sys/file.h>
  21. #endif /* KITLESS */
  22. #ifdef XAW
  23. #include    "xaw_ui.h"
  24. #endif /* XAW */
  25. #include    <ctype.h>
  26. #include    <string.h>
  27. #include    <pwd.h>
  28.  
  29. #define    NUM_RETRIES    5
  30.  
  31. int    replayTime = 200;
  32.  
  33. #ifndef XVIEW
  34. /*
  35.  * gets current PRIMARY selection
  36.  *
  37.  * this is a pretty gross hack, but it works...
  38.  */
  39. char    *
  40. get_selection()
  41. {
  42. static Atom    selection = (Atom) 0;
  43. static Atom    target = (Atom) 0;
  44. Window    win;
  45. unsigned char    *prop;
  46. XEvent    ev;
  47. XSelectionEvent    *sev;
  48. Atom    type;
  49. int    format;
  50. unsigned long    elmts, left;
  51. int    retry = 0;
  52.  
  53.     if (!selection)    {
  54.         selection = XInternAtom(dpy, "PRIMARY", False);
  55.         target = XInternAtom(dpy, "STRING", False);
  56.     }
  57.  
  58.     win = XGetSelectionOwner(dpy, selection);
  59.  
  60.     if (win == None)    /* nobody owns it */
  61.         return (NULL);
  62.  
  63. #ifdef XAW
  64.     {
  65.     String    str;
  66.     XawTextPosition    start, end;
  67.     Arg    args[1];
  68.     XawTextBlock    text;
  69.  
  70.     XtSetArg(args[0], XtNstring, &str);
  71.     if (helptext && win == XtWindow(helptext))    {
  72.         XawTextGetSelectionPos(helptext, &start, &end);
  73.         XawTextSourceRead(XawTextGetSource(helptext),
  74.             start, &text, end - start);
  75.     } else if (win == XtWindow(file))    {
  76.         XawTextGetSelectionPos(file, &start, &end);
  77.         XawTextSourceRead(XawTextGetSource(file),
  78.             start, &text, end - start);
  79.     } else    {
  80.         goto skip;
  81.     }
  82.     prop = (unsigned char *)malloc(end - start + 1);
  83.     (void)strncpy(prop, text.ptr, end - start);
  84.     prop[end - start] = '\0';
  85.     return ((char *)prop);
  86.     }
  87.  
  88.     skip:
  89. #endif /* XAW */
  90.  
  91.     XConvertSelection(dpy, selection, target, None, table, CurrentTime);
  92.  
  93.     XSync(dpy, 0);
  94.  
  95.     /* wait for notification */
  96.     while(XCheckTypedEvent(dpy, SelectionNotify, &ev) == False) {
  97.         XSync(dpy, 0);
  98.         if (retry++ == NUM_RETRIES)
  99.             return (NULL);
  100.         sleep(1);
  101.     }
  102.  
  103.     sev = (XSelectionEvent *)&ev;
  104.  
  105.     if (sev->property == None)    /* nothing to get */
  106.         return (NULL);
  107.  
  108.     (void)XGetWindowProperty(dpy, table, sev->property,
  109.         0L, 1024L,
  110.         False, AnyPropertyType, &type, &format,
  111.         &elmts, &left, &prop);
  112.     
  113.     assert(type == target);
  114.  
  115.     if (format != 8)    /* only want chars */
  116.         return (NULL);
  117.  
  118.     return ((char *)prop);
  119. }
  120. #endif /* XVIEW */
  121.  
  122. #ifdef XAW
  123. char    *helpDir;
  124.  
  125. /*
  126.  * see if all the help files are there
  127.  */
  128. Bool
  129. can_get_help_files(helpfiles)
  130. char    helpfiles[6][256];
  131. {
  132. int    i;
  133.  
  134.     (void)sprintf(helpfiles[0], "%s/doc.intro", helpDir);
  135.     (void)sprintf(helpfiles[1], "%s/doc.rules", helpDir);
  136.     (void)sprintf(helpfiles[2], "%s/doc.controls", helpDir);
  137.     (void)sprintf(helpfiles[3], "%s/doc.examples", helpDir);
  138.     (void)sprintf(helpfiles[4], "%s/doc.misc", helpDir);
  139.     (void)sprintf(helpfiles[5], "%s/doc.summary", helpDir);
  140.  
  141.     for (i = 0; i < 6; i++)    {
  142.         if (access(helpfiles[i], R_OK) == -1)    {
  143.             return False;
  144.         }
  145.     }
  146.     return True;
  147. }
  148. #endif /* XAW */
  149.  
  150.  
  151. char    *
  152. remove_newlines(str)
  153. char    *str;
  154. {
  155. char    *newstr;
  156. char    *n;
  157. extern char    *getenv();
  158.  
  159.     /* pad it generously to provide for tilde expansion */
  160.     n = newstr = (char *)calloc((unsigned)(strlen(str) + 256), 1);
  161.  
  162.     /* remove leading whitespace */
  163.     while (isspace(*str))    {
  164.         str++;
  165.     }
  166.  
  167.     /* tilde expansion */
  168.     if (*str == '~')    {
  169.         /* user */
  170.         if (*(str + 1) == '/')    {
  171.             (void)strcpy(newstr, getenv("HOME"));
  172.         } else    {
  173.             char    uname[20], *t;
  174.             struct passwd    *pwd;
  175.             int    len;
  176.  
  177.             t = strchr(str + 1, '/');
  178.             if (t)    {
  179.                 len = t - str - 1;
  180.             } else    {
  181.                 len = strlen(str);
  182.             }
  183.             (void)strncpy(uname, str + 1, len);
  184.             uname[len] = '\0';
  185.             if (pwd = getpwnam(uname))    {
  186.                 (void)strcpy(newstr, pwd->pw_dir);
  187.                 str += len;
  188.             }
  189.         }
  190.         n += strlen(newstr);
  191.         str++;
  192.     }
  193.  
  194.     /* strip newlines in selection */
  195.     while (*str)    {
  196.         if (*str != '\n')
  197.             *n++ = *str;
  198.         str++;
  199.     }
  200.     *n = '\0';
  201.     return (newstr);
  202. }
  203.  
  204. #ifndef XVIEW
  205. void
  206. delay()
  207. {
  208.     if (replayTime)
  209.         usleep((unsigned)replayTime);
  210. }
  211.  
  212. #ifdef    LOCAL_USLEEP
  213.  
  214. #include <signal.h>
  215. #include <X11/Xos.h>        /* for (sys/)time.h */
  216.  
  217. usleep(value)
  218. long value;
  219. {
  220.     void stopme();
  221.     struct itimerval ntval, otval;
  222.  
  223.     ntval.it_interval.tv_sec = 0;
  224.     ntval.it_interval.tv_usec = 0;
  225.     ntval.it_value.tv_sec = 0;
  226.     ntval.it_value.tv_usec = value;
  227.     signal(SIGALRM, stopme);
  228.     setitimer(ITIMER_REAL, &ntval, &otval);
  229.     pause();
  230. }
  231.  
  232. void
  233. stopme()
  234. {
  235.     signal(SIGALRM, SIG_DFL);
  236. }
  237. #endif    /* LOCAL_USLEEP */
  238.  
  239. #endif /* XVIEW */
  240.  
  241. #ifdef    LOCAL_STRDUP
  242. char    *
  243. strdup(s)
  244. char    *s;
  245. {
  246.     return strcpy(malloc((unsigned) strlen(s) + 1), s);
  247. }
  248. #endif    /* LOCAL_STRDUP */
  249.