Index: tea4cups/trunk/tea4cups
===================================================================
--- tea4cups/trunk/tea4cups (revision 3514)
+++ tea4cups/trunk/tea4cups (revision 3523)
@@ -4,5 +4,5 @@
 # Tea4CUPS : Tee for CUPS
 #
-# (c) 2005-2009 Jerome Alet <alet@librelogiciel.com>
+# (c) 2005-2010 Jerome Alet <alet@librelogiciel.com>
 # (c) 2005 Peter Stuge <stuge-tea4cups@cdy.org>
 # This program is free software; you can redistribute it and/or modify
@@ -29,5 +29,5 @@
 Licensing terms :
 
-  (c) 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
+  (c) 2005-2010 Jerome Alet <alet@librelogiciel.com>
   (c) 2005 Peter Stuge <stuge-tea4cups@cdy.org>
   This program is free software; you can redistribute it and/or modify
@@ -343,4 +343,11 @@
 CUPS_PRINTER_OPTIONS = 0xe6ff
 
+CUPS_BACKEND_OK = 0
+CUPS_BACKEND_FAILED = 1
+CUPS_BACKEND_AUTH_REQUIRED = 2
+CUPS_BACKEND_HOLD = 3
+CUPS_BACKEND_STOP = 4
+CUPS_BACKEND_CANCEL = 5
+
 class FakeAttribute :
     """Fakes an IPPRequest attribute to simplify usage syntax."""
@@ -1290,5 +1297,5 @@
     def runBranches(self) :
         """Launches each hook defined for the current print queue."""
-        self.isCancelled = 0    # did a prehook cancel the print job ?
+        self.isCancelled = False    # did a prehook cancel the print job ?
         serialize = isTrue(self.getPrintQueueOption(self.PrinterName, "serialize", ignore=1))
         self.pipes = { 0: (0, 1) }
@@ -1299,8 +1306,10 @@
         for p in [ (k, v) for (k, v) in self.pipes.items() if k != 0 ] :
             os.close(p[1][1])
-        if not self.isCancelled :
+        if self.isCancelled :
+            retcode = CUPS_BACKEND_CANCEL # Job cancelled, for CUPS.
+        elif retcode == CUPS_BACKEND_OK :
             if self.RealBackend :
                 retcode = self.launchOriginalBackend()
-                if retcode :
+                if retcode != CUPS_BACKEND_OK :
                     onfail = self.getPrintQueueOption(self.PrinterName, \
                                                       "onfail", ignore=1)
@@ -1313,9 +1322,8 @@
             if self.runCommands("posthook", branches, serialize) :
                 self.logInfo("An error occured during the execution of posthooks.", "warn")
-        else :
-            retcode = 5 # Job cancelled, for CUPS.
+
         for p in [ (k, v) for (k, v) in self.pipes.items() if k != 0 ] :
             os.close(p[1][0])
-        if not retcode :
+        if retcode == CUPS_BACKEND_OK :
             self.logInfo("OK")
         else :
@@ -1358,5 +1366,5 @@
     def runCommands(self, btype, branches, serialize) :
         """Runs the commands for a particular branch type."""
-        exitcode = 0
+        exitcode = CUPS_BACKEND_OK
         btype = btype.lower()
         btypetitle = btype.title()
@@ -1371,8 +1379,12 @@
                     if (btype == "prehook") and (retcode == 255) : # -1
                         self.logInfo("Job %s cancelled by prehook %s" % (self.JobId, branch))
-                        self.isCancelled = 1
+                        self.isCancelled = True
+                    elif (btype == "prehook") and (retcode == 254) : # -2
+                        self.logInfo("Job %s hook processing stopped by prehook %s" % (self.JobId, branch))
+                        exitcode = CUPS_BACKEND_FAILED
+                        break
                     else :
                         self.logInfo("%s %s on printer %s didn't exit successfully." % (btypetitle, branch, self.PrinterName), "error")
-                        exitcode = 1
+                        exitcode = CUPS_BACKEND_FAILED
             self.logDebug("End serialized %ss" % btypetitle)
         else :
@@ -1395,8 +1407,8 @@
                     if (btype == "prehook") and (retcode == 255) : # -1
                         self.logInfo("Job %s cancelled by prehook %s" % (self.JobId, branch))
-                        self.isCancelled = 1
+                        self.isCancelled = True
                     else :
                         self.logInfo("%s %s (PID %s) on printer %s didn't exit successfully." % (btypetitle, branch, pid, self.PrinterName), "error")
-                        exitcode = 1
+                        exitcode = CUPS_BACKEND_FAILED
             self.logDebug("End forked %ss" % btypetitle)
         return exitcode
@@ -1418,5 +1430,5 @@
         while 1 :
             retcode = self.runOriginalBackend()
-            if not retcode :
+            if retcode == CUPS_BACKEND_OK :
                 break
             else :
@@ -1459,6 +1471,7 @@
             status = os.WEXITSTATUS(status)
             if status :
-                self.logInfo("CUPS backend %s returned %d." % (originalbackend, \
-                                                             status), "error")
+                self.logInfo("CUPS backend %s returned %d." % (originalbackend,
+                                                               status),
+                             "error")
             return status
         elif not killed :
@@ -1467,5 +1480,5 @@
             return -1
         else :
-            return 1
+            return CUPS_BACKEND_FAILED
 
 if __name__ == "__main__" :
Index: tea4cups/trunk/tea4cups.conf
===================================================================
--- tea4cups/trunk/tea4cups.conf (revision 3478)
+++ tea4cups/trunk/tea4cups.conf (revision 3523)
@@ -163,5 +163,5 @@
 # successfully. Any other value indicates that a problem occured
 # in the CUPS backend which handles the transmission of the job
-# to the printer.
+# to the printer. See the output of 'man backend' for details.
 #
 # prehook_0 : echo "Your print job has been accepted" | smbclient -M $TEAUSERNAME
