]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/serial.c
Remove old files.
[thirdparty/cups.git] / backend / serial.c
index a8777e70df620884af0bc6bfbe6227ef1cc755a5..7a775259dbb2f4d6e5a3c0641940d4ed97e29992 100644 (file)
@@ -1,25 +1,16 @@
 /*
- * "$Id: serial.c 181 2006-06-22 20:01:18Z jlovell $"
+ * "$Id: serial.c 6910 2007-09-04 20:34:29Z mike $"
  *
  *   Serial port backend for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ *   Copyright 2007 by Apple Inc.
+ *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
+ *   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 please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  *   This file is subject to the Apple OS-Developed Software exception.
  *
@@ -27,6 +18,7 @@
  *
  *   main()         - Send a file to the printer or server.
  *   list_devices() - List all serial devices.
+ *   side_cb()      - Handle side-channel requests...
  */
 
 /*
@@ -91,7 +83,8 @@
  * Local functions...
  */
 
-void   list_devices(void);
+static void    list_devices(void);
+static void    side_cb(int print_fd, int device_fd, int use_bc);
 
 
 /*
@@ -111,9 +104,9 @@ main(int  argc,                             /* I - Number of command-line arguments (6 or 7) */
                username[255],          /* Username info (not used) */
                resource[1024],         /* Resource info (device and options) */
                *options,               /* Pointer to options */
-               name[255],              /* Name of option */
-               value[255],             /* Value of option */
-               *ptr;                   /* Pointer into name or value */
+               *name,                  /* Name of option */
+               *value,                 /* Value of option */
+               sep;                    /* Option separator */
   int          port;                   /* Port number (not used) */
   int          copies;                 /* Number of copies to print */
   int          print_fd,               /* Print file */
@@ -168,7 +161,9 @@ main(int  argc,                             /* I - Number of command-line arguments (6 or 7) */
   }
   else if (argc < 6 || argc > 7)
   {
-    fputs("Usage: serial job-id user title copies options [file]\n", stderr);
+    _cupsLangPrintf(stderr,
+                    _("Usage: %s job-id user title copies options [file]\n"),
+                   argv[0]);
     return (CUPS_BACKEND_FAILED);
   }
 
@@ -190,7 +185,9 @@ main(int  argc,                             /* I - Number of command-line arguments (6 or 7) */
 
     if ((print_fd = open(argv[6], O_RDONLY)) < 0)
     {
-      perror("ERROR: unable to open print file");
+      _cupsLangPrintf(stderr,
+                      _("ERROR: Unable to open print file \"%s\": %s\n"),
+                      argv[6], strerror(errno));
       return (CUPS_BACKEND_FAILED);
     }
 
@@ -240,8 +237,9 @@ main(int  argc,                             /* I - Number of command-line arguments (6 or 7) */
        * available printer in the class.
        */
 
-        fputs("INFO: Unable to open serial port, queuing on next printer in class...\n",
-             stderr);
+        _cupsLangPuts(stderr,
+                     _("INFO: Unable to contact printer, queuing on next "
+                       "printer in class...\n"));
 
        /*
         * Sleep 5 seconds to keep the job from requeuing too rapidly...
@@ -254,13 +252,15 @@ main(int  argc,                           /* I - Number of command-line arguments (6 or 7) */
 
       if (errno == EBUSY)
       {
-        fputs("INFO: Serial port busy; will retry in 30 seconds...\n", stderr);
+        _cupsLangPuts(stderr,
+                     _("INFO: Printer busy; will retry in 30 seconds...\n"));
        sleep(30);
       }
       else
       {
-       fprintf(stderr, "ERROR: Unable to open serial port device file \"%s\": %s\n",
-               resource, strerror(errno));
+       _cupsLangPrintf(stderr,
+                       _("ERROR: Unable to open device file \"%s\": %s\n"),
+                       resource, strerror(errno));
        return (CUPS_BACKEND_FAILED);
       }
     }
@@ -291,29 +291,30 @@ main(int  argc,                           /* I - Number of command-line arguments (6 or 7) */
       * Get the name...
       */
 
-      for (ptr = name; *options && *options != '=';)
-        if (ptr < (name + sizeof(name) - 1))
-          *ptr++ = *options++;
-      *ptr = '\0';
+      name = options;
+
+      while (*options && *options != '=' && *options != '+' && *options != '&')
+        options ++;
+
+      if ((sep = *options) != '\0')
+        *options++ = '\0';
 
-      if (*options == '=')
+      if (sep == '=')
       {
        /*
         * Get the value...
        */
 
-        options ++;
-
-       for (ptr = value; *options && *options != '+' && *options != '&';)
-          if (ptr < (value + sizeof(value) - 1))
-            *ptr++ = *options++;
-       *ptr = '\0';
+        value = options;
 
-       if (*options == '+' || *options == '&')
+       while (*options && *options != '+' && *options != '&')
          options ++;
+
+        if (*options)
+         *options++ = '\0';
       }
       else
-        value[0] = '\0';
+        value = (char *)"";
 
      /*
       * Process the option...
@@ -376,7 +377,8 @@ main(int  argc,                             /* I - Number of command-line arguments (6 or 7) */
              break;
 #  endif /* B230400 */
           default :
-             fprintf(stderr, "WARNING: Unsupported baud rate %s!\n", value);
+             _cupsLangPrintf(stderr, _("WARNING: Unsupported baud rate %s!\n"),
+                             value);
              break;
        }
 #endif /* B19200 == 19200 */
@@ -556,6 +558,7 @@ main(int  argc,                             /* I - Number of command-line arguments (6 or 7) */
       if (!print_bytes)
        FD_SET(print_fd, &input);
       FD_SET(device_fd, &input);
+      FD_SET(CUPS_SC_FD, &input);
 
       FD_ZERO(&output);
       if (print_bytes)
@@ -564,6 +567,13 @@ main(int  argc,                            /* I - Number of command-line arguments (6 or 7) */
       if (select(nfds, &input, &output, NULL, NULL) < 0)
        continue;                       /* Ignore errors here */
 
+     /*
+      * Check if we have a side-channel request ready...
+      */
+
+      if (FD_ISSET(CUPS_SC_FD, &input))
+        side_cb(print_fd, device_fd, 1);
+
      /*
       * Check if we have back-channel data ready...
       */
@@ -593,7 +603,7 @@ main(int  argc,                             /* I - Number of command-line arguments (6 or 7) */
 
          if (errno != EAGAIN || errno != EINTR)
          {
-           perror("ERROR: Unable to read print data");
+           _cupsLangPrintError(_("ERROR: Unable to read print data"));
 
             tcsetattr(device_fd, TCSADRAIN, &origopts);
 
@@ -669,7 +679,7 @@ main(int  argc,                             /* I - Number of command-line arguments (6 or 7) */
 
          if (errno != EAGAIN && errno != EINTR && errno != ENOTTY)
          {
-           perror("ERROR: Unable to write print data");
+           _cupsLangPrintError(_("ERROR: Unable to write print data"));
 
             tcsetattr(device_fd, TCSADRAIN, &origopts);
 
@@ -712,14 +722,14 @@ main(int  argc,                           /* I - Number of command-line arguments (6 or 7) */
  * 'list_devices()' - List all serial devices.
  */
 
-void
+static void
 list_devices(void)
 {
-#if defined(__hpux) || defined(__sgi) || defined(__sun) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+#if defined(__hpux) || defined(__sgi) || defined(__sun) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
   static char  *funky_hex = "0123456789abcdefghijklmnopqrstuvwxyz";
                                        /* Funky hex numbering used for some *
                                         * devices                           */
-#endif /* __hpux || __sgi || __sun || __FreeBSD__ || __OpenBSD__ */
+#endif /* __hpux || __sgi || __sun || __FreeBSD__ || __OpenBSD__ || __FreeBSD_kernel__ */
 
 #ifdef __linux
   int                  i, j;           /* Looping vars */
@@ -778,6 +788,14 @@ list_devices(void)
       printf("serial serial:%s?baud=230400 \"Unknown\" \"USB Serial Port #%d\"\n",
              device, i + 1);
     }
+
+    sprintf(device, "/dev/ttyUSB%d", i);
+    if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
+    {
+      close(fd);
+      printf("serial serial:%s?baud=230400 \"Unknown\" \"USB Serial Port #%d\"\n",
+             device, i + 1);
+    }
   }
 
   for (i = 0; i < 64; i ++)
@@ -1007,7 +1025,7 @@ list_devices(void)
       printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
              device, i + 1);
   }
-#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
   int  i, j;           /* Looping vars */
   int  fd;             /* File descriptor */
   char device[255];    /* Device filename */
@@ -1218,5 +1236,57 @@ list_devices(void)
 
 
 /*
- * End of "$Id: serial.c 181 2006-06-22 20:01:18Z jlovell $".
+ * 'side_cb()' - Handle side-channel requests...
+ */
+
+static void
+side_cb(int print_fd,                  /* I - Print file */
+        int device_fd,                 /* I - Device file */
+       int use_bc)                     /* I - Using back-channel? */
+{
+  cups_sc_command_t    command;        /* Request command */
+  cups_sc_status_t     status;         /* Request/response status */
+  char                 data[2048];     /* Request/response data */
+  int                  datalen;        /* Request/response data size */
+
+
+  datalen = sizeof(data);
+
+  if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
+  {
+    _cupsLangPuts(stderr,
+                  _("WARNING: Failed to read side-channel request!\n"));
+    return;
+  }
+
+  switch (command)
+  {
+    case CUPS_SC_CMD_DRAIN_OUTPUT :
+        if (backendDrainOutput(print_fd, device_fd))
+         status = CUPS_SC_STATUS_IO_ERROR;
+       else if (tcdrain(device_fd))
+         status = CUPS_SC_STATUS_IO_ERROR;
+       else
+         status = CUPS_SC_STATUS_OK;
+
+       datalen = 0;
+        break;
+
+    case CUPS_SC_CMD_GET_BIDI :
+        data[0] = use_bc;
+        datalen = 1;
+        break;
+
+    default :
+        status  = CUPS_SC_STATUS_NOT_IMPLEMENTED;
+       datalen = 0;
+       break;
+  }
+
+  cupsSideChannelWrite(command, status, data, datalen, 1.0);
+}
+
+
+/*
+ * End of "$Id: serial.c 6910 2007-09-04 20:34:29Z mike $".
  */