http://packetstormsecurity.org/UNIX/admin/xscreensaver-3.34.tar.gz
[xscreensaver] / hacks / xsublim.c
index 231bddc4c6f5ae4a10864e0aef4d8a5d97881ec3..1dbc754d56a2f70ce931b050f71b0c67add92001 100644 (file)
@@ -1,8 +1,8 @@
-/*****************************************************************************
+/****************************************************************************
  *                                                                           *
  * xsublim -- Submit.  Conform.  Obey.                                       *
  *                                                                           *
- * Copyright (c) 1999 Greg Knauss (greg@eod.com)                             *
+ * Copyright (c) 1999 - 2000 Greg Knauss (greg@eod.com)                      *
  *                                                                           *
  * Thanks to Jamie Zawinski, whose suggestions and advice made what was a    *
  * boring little program into a less boring (and a _lot_ less little)        *
@@ -31,6 +31,8 @@
 /* Arguments ******************************************************************
 
        -font font           Font to use
+       -file filename       New-line delimited phrase file
+       -program executable  New-line delimited phrase-producing executable
        -delayShow ms        Microsecs for display of each word
        -delayWord ms        Microsecs for blank between words
        -delayPhraseMin ms   Microsecs for min blank between phrases
        -no-screensaver      Draw over active windows       
        -outline             Draw a contrasting outline around words (Default)
        -no-outline          Draw words without an outline
+       -center              Draw words in the center of the screen (Default)
+       -no-center           Draw words randomly around the screen
+*/
+
+
+/* Changelog ******************************************************************
+
+       1.1.1  20000407  Added -program
+       1.1.0  19991221  Added -file
+       1.0.1  19990716  Assume that XGetImage()/XDestroyImage() don't leak,
+                         which they apparently don't.  I have no idea how I
+                         convinced myself that they did.  Huh.  (greg@eod.com)
+       1.0.0  19990716  Initial release
 */
 
 
 #define XSUBLIM_TEXT_COUNT         1000
 #define XSUBLIM_TEXT_LENGTH        128
 #define XSUBLIM_TEXT_OUTLINE       1
+#define XSUBLIM_PROGRAM_SIZE       1024*10
 #define XSUBLIM_ARG_DELAYSHOW      "delayShow"
 #define XSUBLIM_ARG_DELAYWORD      "delayWord"
 #define XSUBLIM_ARG_DELAYPHRASEMIN "delayPhraseMin"
 #define XSUBLIM_ARG_DELAYPHRASEMAX "delayPhraseMax"
 #define XSUBLIM_ARG_RANDOM         "random"
 #define XSUBLIM_ARG_FILE           "file"
+#define XSUBLIM_ARG_PROGRAM        "program"
 #define XSUBLIM_ARG_SCREENSAVER    "screensaver"
 #define XSUBLIM_ARG_OUTLINE        "outline"
 #define XSUBLIM_ARG_CENTER         "center"
@@ -71,6 +88,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
+#include <sys/stat.h>
 #include <X11/Intrinsic.h>
 #include <X11/IntrinsicP.h>
 #include <X11/CoreP.h>
 #endif
 
 #include "usleep.h"
+#include "yarandom.h"
 #include "resources.h"
 
 
@@ -103,10 +122,12 @@ char*        defaults[] =
         "Submit.\\n"
         "Conform.\\n"
         "Obey.\\n"
+        "OBEY. OBEY. OBEY.\\n"
         "Consume.\\n"
         "Be silent.\\n"
         "Fear.\\n"
         "Waste.\\n"
+        "Money.\\n"
         "Watch TV.\\n"
         "Hate yourself.\\n"
         "Buy needlessly.\\n"
@@ -117,6 +138,9 @@ char*        defaults[] =
         "You serve no purpose.\\n"
         "Your contributions are ignored.\\n"
         "They are laughing at you.\\n"
+        "They lied to you.\\n"
+        "They read your mail.\\n"
+        "They know.\\n"
         "Surrender.\\n"
         "You will fail.\\n"
         "Never question.\\n"
@@ -166,6 +190,10 @@ XrmOptionDescRec options[] =
         XrmoptionNoArg,"false"},
        {"-" XSUBLIM_ARG_FILE,          "." XSUBLIM_ARG_FILE,
         XrmoptionSepArg,0 },
+#if !defined(VMS)
+       {"-" XSUBLIM_ARG_PROGRAM,       "." XSUBLIM_ARG_PROGRAM,
+        XrmoptionSepArg,0 },
+#endif
        {"-" XSUBLIM_ARG_SCREENSAVER,   "." XSUBLIM_ARG_SCREENSAVER,
         XrmoptionNoArg,"true"},
        {"-no-" XSUBLIM_ARG_SCREENSAVER,"." XSUBLIM_ARG_SCREENSAVER,
@@ -340,7 +368,9 @@ int main(int argc,char* argv[])
                SIGQUIT,
                SIGILL,
                SIGTRAP,
+#if defined(SIGIOT)
                SIGIOT,
+#endif
                SIGABRT,
 #if defined(SIGEMT)
                SIGEMT,
@@ -380,7 +410,6 @@ int main(int argc,char* argv[])
                "-*-times-*-r-*-*-*-600-*-*-p-*-*-*",
                "-*-*-*-r-*-sans-*-600-*-*-p-*-*-*",
                "-*-*-*-r-*-*-*-600-*-*-m-*-*-*",
-
                "-*-helvetica-*-r-*-*-*-240-*-*-p-*-*-*",
                "-*-lucida-*-r-*-*-*-240-*-*-p-*-*-*",
                "-*-times-*-r-*-*-*-240-*-*-p-*-*-*",
@@ -431,6 +460,7 @@ int main(int argc,char* argv[])
        int               arg_DelayPhraseMin;
        int               arg_DelayPhraseMax;
        char*             arg_Text;
+       char*             arg_Source;
 
        /* Set-up ---------------------------------------------------------- */
 
@@ -441,8 +471,10 @@ int main(int argc,char* argv[])
                signal(sig_Number,xsublim_Sig_Catch);
        }
 
-       /* Randomize */
-       srandom((int)time((time_t*)0));
+       /* Randomize -- only need to do this here because this program
+           doesn't use the `screenhack.h' or `lockmore.h' APIs. */
+# undef ya_rand_init
+        ya_rand_init (0);
 
        /* Handle all the X nonsense */
 #if defined(__sgi)
@@ -511,10 +543,99 @@ int main(int argc,char* argv[])
        text_Item = 0;
        text_Count = 0;
        memset(text_Used,0,sizeof(text_Used));
-       arg_Text = get_string_resource(XSUBLIM_ARG_PHRASES,"Phrases");
+       arg_Source = get_string_resource(XSUBLIM_ARG_FILE,"Filename");
+       if (arg_Source != NULL)
+       {
+               FILE*       file_Fs;
+               struct stat file_Stat;
+
+               file_Fs = fopen(arg_Source,"rb");
+               if (file_Fs == NULL)
+               {
+                       fprintf(stderr,"%s: Could not open '%s'\n",progname,
+                        arg_Source);
+                       exit(-1);
+               }
+               if (fstat(fileno(file_Fs),&file_Stat) != 0)
+               {
+                       fprintf(stderr,"%s: Could not stat '%s'\n",progname,
+                        arg_Source);
+                       exit(-1);
+               }
+               arg_Text = calloc(1,file_Stat.st_size+1);
+               if (arg_Text != NULL)
+               {
+                       if (fread(arg_Text,file_Stat.st_size,1,file_Fs) != 1)
+                       {
+                               fprintf(stderr,"%s: Could not read '%s'\n",
+                                progname,arg_Source);
+                               exit(-1);
+                       }
+               }
+               fclose(file_Fs);
+       }
+       else
+       {
+               arg_Source = get_string_resource(XSUBLIM_ARG_PROGRAM,
+                "Executable");
+               if (arg_Source != NULL)
+               {
+                       char* exe_Command = calloc(1,strlen(arg_Source)+10);
+                       FILE* exe_Fs;
+
+                       if (exe_Command == NULL)
+                       {
+                               fprintf(stderr,
+                                "%s: Could not allocate space for '%s'\n",
+                                progname,arg_Source);
+                               exit(-1);
+                       }
+                       sprintf(exe_Command,"( %s ) 2>&1",arg_Source);
+
+                       exe_Fs = popen(exe_Command,"r");
+                       if (exe_Fs == NULL)
+                       {
+                               fprintf(stderr,"%s: Could not run '%s'\n",
+                                progname,arg_Source);
+                               exit(-1);
+                       }
+                       arg_Text = calloc(1,XSUBLIM_PROGRAM_SIZE);
+                       if (arg_Text != NULL)
+                       {
+                               if (fread(arg_Text,1,XSUBLIM_PROGRAM_SIZE,
+                                exe_Fs) <= 0)
+                               {
+                                       fprintf(stderr,
+                                        "%s: Could not read output of '%s'\n",
+                                        progname,arg_Source);
+                                       exit(-1);
+                               }
+                               if (
+                                strstr(arg_Text,": not found") ||
+                                strstr(arg_Text,": Not found") ||
+                                strstr(arg_Text,": command not found") ||
+                                strstr(arg_Text,": Command not found"))
+                               {
+                                       fprintf(stderr,
+                                        "%s: Could not find '%s'\n",
+                                        progname,arg_Source);
+                                       exit(-1);
+                               }
+                       }
+                       fclose(exe_Fs);
+               }
+               else
+               {
+                       arg_Text =
+                        get_string_resource(XSUBLIM_ARG_PHRASES,"Phrases");
+                       if (arg_Text != NULL)
+                       {
+                               arg_Text = strdup(arg_Text);
+                       }
+               }
+       }
        if (arg_Text != NULL)
        {
-               arg_Text = strdup(arg_Text);
                while (((text_Phrase = strtok(arg_Text,"\n")) != NULL) &&
                 (text_Count < XSUBLIM_TEXT_COUNT))
                {
@@ -522,8 +643,8 @@ int main(int argc,char* argv[])
                        text_List[text_Count] = text_Phrase;
                        text_Count++;
                }
+               text_List[text_Count] = NULL;
        }
-       text_List[text_Count] = NULL;
        if (text_Count == 0)
        {
                fprintf(stderr,"%s: No text to display\n",progname);
@@ -734,18 +855,15 @@ int main(int argc,char* argv[])
                                         image_Y,image_Width,image_Height);
                                }
 
-                               /* Free the image (and it's goddamned structure
-                                  -- the man page for XCreateImage() lies,
-                                  lies, lies!) */
+                               /* Free the image */
                                XDestroyImage(image_Image);
-                               XFree(image_Image);
                        }
 
                        /* Restore the error handler, ungrab the server */
-                        XSync(disp_Display, FALSE);
+                        XSync(disp_Display,FALSE);
                        XSetErrorHandler(Xsublim_Sh_Handler);
                        XUngrabServer(disp_Display);
-                        XSync(disp_Display, FALSE);
+                        XSync(disp_Display,FALSE);
 
                        /* Pause between words */
                        if (Xsublim_Sig_Last == -1)