]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/testbackend.c
Fix build errors on Fedora.
[thirdparty/cups.git] / backend / testbackend.c
index 3213450f74b82814aeb8aacfbf3d6fd582d88038..046fc3e30c1a2e08d395184607c18f934479abd1 100644 (file)
@@ -1,39 +1,29 @@
 /*
  * "$Id$"
  *
- *   Backend test program for the Common UNIX Printing System (CUPS).
+ * Backend test program for CUPS.
  *
- *   Copyright 2007-2009 by Apple Inc.
- *   Copyright 1997-2005 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2005 by Easy Software Products, all rights reserved.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   "LICENSE" which should have been included with this file.  If this
- *   file is missing or damaged, see the license at "http://www.cups.org/".
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * "LICENSE" which should have been included with this file.  If this
+ * file is missing or damaged, see the license at "http://www.cups.org/".
  *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   main()            - Run the named backend.
- *   sigterm_handler() - Flag when we get SIGTERM.
- *   usage()           - Show usage information.
- *   walk_cb()         - Show results of cupsSideChannelSNMPWalk...
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
  * Include necessary headers.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <cups/string.h>
+#include <cups/string-private.h>
 #include <cups/cups.h>
 #include <cups/sidechannel.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <errno.h>
 #include <sys/wait.h>
 #include <signal.h>
 
@@ -50,7 +40,7 @@ static int    job_canceled = 0;
  */
 
 static void    sigterm_handler(int sig);
-static void    usage(void);
+static void    usage(void) __attribute__((noreturn));
 static void    walk_cb(const char *oid, const char *data, int datalen,
                        void *context);
 
@@ -60,7 +50,7 @@ static void   walk_cb(const char *oid, const char *data, int datalen,
  *
  * Usage:
  *
- *    betest [-s] [-t] device-uri job-id user title copies options [file]
+ *    testbackend [-s] [-t] device-uri job-id user title copies options [file]
  */
 
 int                                    /* O - Exit status */
@@ -78,7 +68,9 @@ main(int  argc,                               /* I - Number of command-line args */
   const char   *oid = ".1.3.6.1.2.1.43.10.2.1.4.1.1";
                                        /* OID to lookup or walk */
   char         scheme[255],            /* Scheme in URI == backend */
-               backend[1024];          /* Backend path */
+               backend[1024],          /* Backend path */
+               libpath[1024],          /* Path for libcups */
+               *ptr;                   /* Pointer into path */
   const char   *serverbin;             /* CUPS_SERVERBIN environment variable */
   int          fd,                     /* Temporary file descriptor */
                back_fds[2],            /* Back-channel pipe */
@@ -90,6 +82,29 @@ main(int  argc,                              /* I - Number of command-line args */
                status;                 /* Exit status */
 
 
+ /*
+  * Get the current directory and point the run-time linker at the "cups"
+  * subdirectory...
+  */
+
+  if (getcwd(libpath, sizeof(libpath)) &&
+      (ptr = strrchr(libpath, '/')) != NULL && !strcmp(ptr, "/backend"))
+  {
+    strlcpy(ptr, "/cups", sizeof(libpath) - (size_t)(ptr - libpath));
+    if (!access(libpath, 0))
+    {
+#ifdef __APPLE__
+      fprintf(stderr, "Setting DYLD_LIBRARY_PATH to \"%s\".\n", libpath);
+      setenv("DYLD_LIBRARY_PATH", libpath, 1);
+#else
+      fprintf(stderr, "Setting LD_LIBRARY_PATH to \"%s\".\n", libpath);
+      setenv("LD_LIBRARY_PATH", libpath, 1);
+#endif /* __APPLE__ */
+    }
+    else
+      perror(libpath);
+  }
+
  /*
   * See if we have side-channel tests to do...
   */
@@ -289,7 +304,7 @@ main(int  argc,                             /* I - Number of command-line args */
          */
 
          if ((bytes = cupsBackChannelRead(buffer, sizeof(buffer), 0)) > 0)
-           write(2, buffer, bytes);
+           write(2, buffer, (size_t)bytes);
 
         /*
          * Throttle output to ~100hz...
@@ -317,7 +332,7 @@ main(int  argc,                             /* I - Number of command-line args */
        */
 
         while ((bytes = cupsBackChannelRead(buffer, sizeof(buffer), 5.0)) > 0)
-         write(2, buffer, bytes);
+         write(2, buffer, (size_t)bytes);
 
        exit(0);
       }
@@ -392,7 +407,7 @@ main(int  argc,                             /* I - Number of command-line args */
         while ((bytes = cupsBackChannelRead(buffer, sizeof(buffer),
                                            timeout)) > 0)
        {
-         write(2, buffer, bytes);
+         write(2, buffer, (size_t)bytes);
          timeout = 5.0;
        }
        write(2, "\nDEBUG: END\n", 12);
@@ -551,13 +566,13 @@ main(int  argc,                           /* I - Number of command-line args */
 
       length   = sizeof(buffer);
       scstatus = cupsSideChannelSNMPGet(oid, buffer, &length, 5.0);
-      printf("CUPS_SC_CMD_SNMP_GET %s returned %s, %s\n", oid,
-            statuses[scstatus], buffer);
+      printf("CUPS_SC_CMD_SNMP_GET %s returned %s, %d bytes (%s)\n", oid,
+            statuses[scstatus], (int)length, buffer);
 
       length   = sizeof(buffer);
       scstatus = cupsSideChannelSNMPGet(oid, buffer, &length, 5.0);
-      printf("CUPS_SC_CMD_SNMP_GET %s returned %s, %s\n", oid,
-            statuses[scstatus], buffer);
+      printf("CUPS_SC_CMD_SNMP_GET %s returned %s, %d bytes (%s)\n", oid,
+            statuses[scstatus], (int)length, buffer);
     }
 
     length   = 0;
@@ -572,7 +587,7 @@ main(int  argc,                             /* I - Number of command-line args */
     kill(data_pid, SIGTERM);
     kill(back_pid, SIGTERM);
   }
-  
+
   while ((pid = wait(&status)) > 0)
   {
     if (status)
@@ -616,13 +631,13 @@ sigterm_handler(int sig)          /* I - Signal */
 static void
 usage(void)
 {
-  puts("Usage: testbackend [-cancel] [-d] [-ps | -pcl] [-s [-oid OID] "
+  puts("Usage: testbackend [-cancel] [-d] [-ps | -pcl] [-s [-get OID] "
        "[-walk OID]] [-t] device-uri job-id user title copies options [file]");
   puts("");
   puts("Options:");
   puts("  -cancel     Simulate a canceled print job after 2 seconds.");
   puts("  -d          Show log messages from backend.");
-  puts("  -oid OID    Lookup the specified SNMP OID.");
+  puts("  -get OID    Lookup the specified SNMP OID.");
   puts("              (.1.3.6.1.2.1.43.10.2.1.4.1.1 is a good one for printers)");
   puts("  -pcl        Send PCL+PJL query and test page to backend.");
   puts("  -ps         Send PostScript query and test page to backend.");
@@ -645,7 +660,22 @@ walk_cb(const char *oid,           /* I - OID */
        int        datalen,             /* I - Length of data */
        void       *context)            /* I - Context (unused) */
 {
-  printf("CUPS_SC_CMD_SNMP_WALK %s=%s (%d bytes)\n", oid, data, datalen);
+  char temp[80];
+
+  (void)context;
+
+  if ((size_t)datalen > (sizeof(temp) - 1))
+  {
+    memcpy(temp, data, sizeof(temp) - 1);
+    temp[sizeof(temp) - 1] = '\0';
+  }
+  else
+  {
+    memcpy(temp, data, (size_t)datalen);
+    temp[datalen] = '\0';
+  }
+
+  printf("CUPS_SC_CMD_SNMP_WALK %s, %d bytes (%s)\n", oid, datalen, temp);
 }