2 * $Id: lp.c,v 1.21 2009-01-21 02:43:46 didg Exp $
4 * Copyright (c) 1990,1994 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
8 * Copyright (c) 1983 Regents of the University of California.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * Interface to lpr system.
46 #endif /* HAVE_CONFIG_H */
48 #include <sys/param.h>
50 #include <sys/socket.h>
55 #endif /* HAVE_UNISTD_H */
59 #include <netinet/in.h>
64 #endif /* ABS_PRINT */
71 #endif /* HAVE_FCNTL_H */
74 #include <atalk/logger.h>
75 #include <netatalk/at.h>
76 #include <atalk/atp.h>
77 #include <atalk/paths.h>
78 #include <atalk/unicode.h>
85 #include "print_cups.h"
89 /* These functions aren't used outside of lp.c */
91 int lp_disconn_inet( int );
93 int lp_disconn_unix( int );
95 char hostname[ MAXHOSTNAMELEN ];
97 extern struct sockaddr_at *sat;
106 char *lp_created_for; /* Holds the content of the Postscript %%For Comment if available */
111 #define LP_INIT (1<<0)
112 #define LP_OPEN (1<<1)
113 #define LP_PIPE (1<<2)
114 #define LP_CONNECT (1<<3)
115 #define LP_QUEUE (1<<4)
116 #define LP_JOBPENDING (1<<5)
118 void lp_origin (int origin)
120 lp.lp_origin = origin;
123 /* the converted string should always be shorter, but ... FIXME! */
124 static void convert_octal (char *string, charset_t dest)
126 unsigned char *p, *q;
131 while ( *p != '\0' ) {
135 if (dest && isdigit(*p) && isdigit(*(p+1)) && isdigit(*(p+2)) ) {
140 ch = strtol( temp, NULL, 8);
141 if ( ch && ch < 0xff)
160 static void translate(charset_t from, charset_t dest, char **option)
164 if (*option != NULL) {
165 convert_octal(*option, from);
167 if ((size_t) -1 != (convert_string_allocate(from, dest, *option, strlen(*option), &translated)) ) {
169 *option = translated;
176 static void lp_setup_comments (charset_t dest)
180 switch (lp.lp_origin) {
191 LOG(log_debug, logtype_papd, "job: %s", lp.lp_job );
193 translate(from, dest, &lp.lp_job);
195 if (lp.lp_created_for) {
197 LOG(log_debug, logtype_papd, "for: %s", lp.lp_created_for );
199 translate(from, dest, &lp.lp_created_for);
203 LOG(log_debug, logtype_papd, "person: %s", lp.lp_person );
205 translate(from, dest, &lp.lp_person);
209 #define is_var(a, b) (strncmp((a), (b), 2) == 0)
211 static size_t quote(char *dest, char *src, const size_t bsize, size_t len)
215 while (len && used < bsize ) {
221 if (used + 2 > bsize )
238 static char* pipexlate(char *src)
241 static char destbuf[MAXPATHLEN +1];
242 size_t destlen = MAXPATHLEN;
250 memset(dest, 0, MAXPATHLEN +1);
251 if ((p = strchr(src, '%')) == NULL) { /* nothing to do */
252 strncpy(dest, src, MAXPATHLEN);
255 /* first part of the path. copy and forward to the next variable. */
256 len = MIN((size_t)(p - src), destlen);
258 strncpy(dest, src, len);
263 while (p && destlen > 0) {
264 /* now figure out what the variable is */
266 if (is_var(p, "%U")) {
268 } else if (is_var(p, "%C") || is_var(p, "%J") ) {
270 } else if (is_var(p, "%F")) {
271 q = lp.lp_created_for;
272 } else if (is_var(p, "%%")) {
276 /* copy the stuff over. if we don't understand something that we
277 * should, just skip it over. */
279 len = MIN(strlen(q), destlen);
280 len = quote(dest, q, destlen, len);
283 len = MIN(2, destlen);
284 strncpy(dest, q, len);
289 /* stuff up to next % */
291 p = strchr(src, '%');
292 len = p ? MIN((size_t)(p - src), destlen) : destlen;
294 strncpy(dest, src, len);
303 void lp_person( person )
306 if ( lp.lp_person != NULL ) {
307 free( lp.lp_person );
309 if (( lp.lp_person = (char *)malloc( strlen( person ) + 1 )) == NULL ) {
310 LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
313 strcpy( lp.lp_person, person );
323 if ( lp.lp_person == NULL ) {
326 err = ABS_canprint( lp.lp_person, printer->p_role, printer->p_srvid,
328 printer->p_pagecost = floor( atof( cost ) * 10000.0 );
329 printer->p_balance = atof( balance ) + atof( cost );
330 return( err < 0 ? -1 : 0 );
332 #endif /* ABS_PRINT */
337 if ( lp.lp_host != NULL ) {
340 if (( lp.lp_host = (char *)malloc( strlen( host ) + 1 )) == NULL ) {
341 LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
344 strcpy( lp.lp_host, host );
345 LOG(log_debug, logtype_papd, "host: %s", lp.lp_host );
348 /* Currently lp_job and lp_for will not handle the
349 * conversion of macroman chars > 0x7f correctly
350 * This should be added.
356 if ( lp.lp_job != NULL ) {
360 lp.lp_job = strdup(job);
362 LOG(log_debug, logtype_papd, "job: %s", lp.lp_job );
367 void lp_for ( lpfor )
370 if ( lp.lp_created_for != NULL ) {
371 free( lp.lp_created_for );
374 lp.lp_created_for = strdup(lpfor);
378 int lp_init( out, sat )
380 struct sockaddr_at *sat;
382 int authenticated = 0;
385 char *cp, buf[ BUFSIZ ];
387 #endif /* HAVE_CUPS */
391 #endif /* ABS_PRINT */
393 if ( printer->p_flags & P_AUTH ) {
396 /* cap style "log on to afp server before printing" authentication */
398 if ( printer->p_authprintdir && (printer->p_flags & P_AUTH_CAP) ) {
399 int addr_net = ntohs( sat->sat_addr.s_net );
400 int addr_node = sat->sat_addr.s_node;
401 char addr_filename[256];
402 char auth_string[256];
403 char *username, *afpdpid;
407 memset( auth_string, 0, 256 );
408 sprintf(addr_filename, "%s/net%d.%dnode%d",
409 printer->p_authprintdir, addr_net/256, addr_net%256,
411 if (stat(addr_filename, &cap_st) == 0) {
412 if ((cap_file = fopen(addr_filename, "r")) != NULL) {
413 if (fgets(auth_string, 256, cap_file) != NULL) {
414 username = auth_string;
415 if ((afpdpid = strrchr( auth_string, ':' )) != NULL) {
419 if (getpwnam(username) != NULL ) {
420 LOG(log_info, logtype_papd, "CAP authenticated %s", username);
424 LOG(log_info, logtype_papd, "CAP error: invalid username: '%s'", username);
427 LOG(log_info, logtype_papd, "CAP error: could not read username");
430 LOG(log_info, logtype_papd, "CAP error: %s", strerror(errno));
433 LOG(log_info, logtype_papd, "CAP error: %s", strerror(errno));
437 if ( printer->p_flags & P_AUTH_PSSP ) {
438 if ( lp.lp_person != NULL ) {
443 if ( authenticated == 0 ) {
444 LOG(log_error, logtype_papd, "lp_init: must authenticate" );
445 spoolerror( out, "Authentication required." );
450 if (( printer->p_flags & P_ACCOUNT ) && printer->p_pagecost > 0 &&
451 ! ABS_canprint( lp.lp_person, printer->p_role,
452 printer->p_srvid, cost, balance )) {
453 LOG(log_error, logtype_papd, "lp_init: no ABS funds" );
454 spoolerror( out, "No ABS funds available." );
457 #endif /* ABS_PRINT */
460 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
461 LOG(log_error, logtype_papd, "gethostname: %s", strerror(errno) );
465 if ( lp.lp_flags & LP_INIT ) {
466 LOG(log_error, logtype_papd, "lp_init: already inited, die!" );
474 if ( printer->p_flags & P_SPOOLED ) {
477 /* check if queuing is enabled: mode & 010 on lock file */
478 if ( stat( printer->p_lock, &st ) < 0 ) {
479 LOG(log_error, logtype_papd, "lp_init: %s: %s", printer->p_lock, strerror(errno) );
480 spoolerror( out, NULL );
483 if ( st.st_mode & 010 ) {
484 LOG(log_info, logtype_papd, "lp_init: queuing is disabled" );
485 spoolerror( out, "Queuing is disabled." );
489 if (( fd = open( ".seq", O_RDWR|O_CREAT, 0661 )) < 0 ) {
490 LOG(log_error, logtype_papd, "lp_init: can't create .seq" );
491 spoolerror( out, NULL );
495 #ifndef SOLARIS /* flock is unsupported, I doubt this stuff works anyway with newer solaris so ignore for now */
496 if ( flock( fd, LOCK_EX ) < 0 ) {
497 LOG(log_error, logtype_papd, "lp_init: can't lock .seq" );
498 spoolerror( out, NULL );
504 if (( len = read( fd, buf, sizeof( buf ))) < 0 ) {
505 LOG(log_error, logtype_papd, "lp_init read: %s", strerror(errno) );
506 spoolerror( out, NULL );
510 for ( cp = buf; len; len--, cp++ ) {
511 if ( *cp < '0' || *cp > '9' ) {
514 n = n * 10 + ( *cp - '0' );
519 n = ( n + 1 ) % 1000;
520 sprintf( buf, "%03d\n", n );
522 write( fd, buf, strlen( buf ));
526 if (cups_get_printer_status ( printer ) == 0)
528 spoolerror( out, "Queuing is disabled." );
532 lp.lp_seq = getpid();
533 #endif /* HAVE CUPS */
535 lp.lp_flags |= LP_PIPE;
536 lp.lp_seq = getpid();
539 lp.lp_flags |= LP_INIT;
543 int lp_open( out, sat )
545 struct sockaddr_at *sat;
547 char name[ MAXPATHLEN ];
549 struct passwd *pwent;
552 LOG (log_debug, logtype_papd, "lp_open");
555 if ( lp.lp_flags & LP_JOBPENDING ) {
559 if (( lp.lp_flags & LP_INIT ) == 0 && lp_init( out, sat ) != 0 ) {
562 if ( lp.lp_flags & LP_OPEN ) {
563 /* LOG(log_error, logtype_papd, "lp_open already open" ); */
568 if ( lp.lp_flags & LP_PIPE ) {
570 /* go right to program */
571 if (lp.lp_person != NULL) {
572 if((pwent = getpwnam(lp.lp_person)) != NULL) {
573 if(setreuid(pwent->pw_uid, pwent->pw_uid) != 0) {
574 LOG(log_info, logtype_papd, "setreuid error: %s", strerror(errno));
577 LOG(log_info, logtype_papd, "Error getting username (%s)", lp.lp_person);
581 lp_setup_comments(CH_UNIX);
582 if (( lp.lp_stream = popen( pipexlate(printer->p_printer), "w" )) == NULL ) {
583 LOG(log_error, logtype_papd, "lp_open popen %s: %s", printer->p_printer, strerror(errno) );
584 spoolerror( out, NULL );
587 LOG(log_debug, logtype_papd, "lp_open: opened %s", pipexlate(printer->p_printer) );
589 sprintf( name, "df%c%03d%s", lp.lp_letter++, lp.lp_seq, hostname );
591 if (( fd = open( name, O_WRONLY|O_CREAT|O_EXCL, 0660 )) < 0 ) {
592 LOG(log_error, logtype_papd, "lp_open %s: %s", name, strerror(errno) );
593 spoolerror( out, NULL );
597 if ( NULL == (lp.lp_spoolfile = (char *) malloc (strlen (name) +1)) ) {
598 LOG(log_error, logtype_papd, "malloc: %s", strerror(errno));
601 strcpy ( lp.lp_spoolfile, name);
603 if (lp.lp_person != NULL) {
604 if ((pwent = getpwnam(lp.lp_person)) == NULL) {
605 LOG(log_error, logtype_papd, "getpwnam %s: no such user", lp.lp_person);
606 spoolerror( out, NULL );
610 if ((pwent = getpwnam(printer->p_operator)) == NULL) {
611 LOG(log_error, logtype_papd, "getpwnam %s: no such user", printer->p_operator);
612 spoolerror( out, NULL );
617 if (fchown(fd, pwent->pw_uid, -1) < 0) {
618 LOG(log_error, logtype_papd, "chown %s %s: %s", pwent->pw_name, name, strerror(errno));
619 spoolerror( out, NULL );
623 if (( lp.lp_stream = fdopen( fd, "w" )) == NULL ) {
624 LOG(log_error, logtype_papd, "lp_open fdopen: %s", strerror(errno) );
625 spoolerror( out, NULL );
629 LOG(log_debug, logtype_papd, "lp_open: opened %s", name );
632 lp.lp_flags |= LP_OPEN;
638 if (( lp.lp_flags & LP_INIT ) == 0 || ( lp.lp_flags & LP_OPEN ) == 0 ) {
641 fclose( lp.lp_stream );
643 lp.lp_flags &= ~LP_OPEN;
644 lp.lp_flags |= LP_JOBPENDING;
650 int lp_write(in, buf, len )
655 #define BUFSIZE 32768
656 static char tempbuf[BUFSIZE];
657 static char tempbuf2[BUFSIZE];
658 static size_t bufpos = 0;
659 static int last_line_translated = 1; /* if 0, append a \n a the start */
662 /* Before we write out anything check for a pending job, e.g. cover page */
663 if (lp.lp_flags & LP_JOBPENDING)
666 /* foomatic doesn't handle mac line endings, so we convert them for
667 * the Postscript headers
668 * REALLY ugly hack, remove ASAP again */
669 if ((printer->p_flags & P_FOOMATIC_HACK) && (in->pf_state & PF_TRANSLATE) &&
670 (buf[len-1] != '\n') ) {
671 if (len <= BUFSIZE) {
672 if (!last_line_translated) {
674 memcpy(tempbuf2+1, buf, len++);
677 memcpy(tempbuf2, buf, len);
679 if (tempbuf2[len-1] == '\r')
680 tempbuf2[len-1] = '\n';
683 last_line_translated = 1;
685 LOG(log_debug, logtype_papd, "lp_write: %s", tbuf );
689 LOG(log_error, logtype_papd, "lp_write: conversion buffer too small" );
694 if (printer->p_flags & P_FOOMATIC_HACK && buf[len-1] == '\n') {
695 last_line_translated = 1;
698 last_line_translated = 0;
701 /* To be able to do commandline substitutions on piped printers
702 * we store the start of the print job in a buffer.
703 * %%EndComment triggers writing to file */
704 if (( lp.lp_flags & LP_OPEN ) == 0 ) {
706 LOG(log_debug, logtype_papd, "lp_write: writing to temporary buffer" );
708 if ((bufpos+len) > BUFSIZE) {
709 LOG(log_error, logtype_papd, "lp_write: temporary buffer too small" );
710 /* FIXME: call lp_open here? abort isn't nice... */
714 memcpy(tempbuf + bufpos, tbuf, len);
716 if (bufpos > BUFSIZE/2)
717 in->pf_state |= PF_STW; /* we used half of the buffer, start writing */
722 if ( fwrite( tempbuf, 1, bufpos, lp.lp_stream ) != bufpos ) {
723 LOG(log_error, logtype_papd, "lp_write: %s", strerror(errno) );
729 if ( fwrite( tbuf, 1, len, lp.lp_stream ) != len ) {
730 LOG(log_error, logtype_papd, "lp_write: %s", strerror(errno) );
738 char name[ MAXPATHLEN ];
741 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
745 if ( lp.lp_flags & LP_OPEN ) {
749 for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
750 sprintf( name, "df%c%03d%s", letter, lp.lp_seq, hostname );
751 if ( unlink( name ) < 0 ) {
752 LOG(log_error, logtype_papd, "lp_cancel unlink %s: %s", name, strerror(errno) );
760 * Create printcap control file, signal printer. Errors here should
761 * remove queue files.
768 char buf[ MAXPATHLEN ];
769 char tfname[ MAXPATHLEN ];
770 char cfname[ MAXPATHLEN ];
774 #endif /* HAVE_CUPS */
776 if (( lp.lp_flags & LP_INIT ) == 0 || lp.lp_letter == 'A' ) {
780 lp.lp_flags &= ~LP_JOBPENDING;
782 if ( printer->p_flags & P_SPOOLED ) {
784 sprintf( tfname, "tfA%03d%s", lp.lp_seq, hostname );
785 if (( fd = open( tfname, O_WRONLY|O_EXCL|O_CREAT, 0660 )) < 0 ) {
786 LOG(log_error, logtype_papd, "lp_print %s: %s", tfname, strerror(errno) );
789 if (( cfile = fdopen( fd, "w" )) == NULL ) {
790 LOG(log_error, logtype_papd, "lp_print %s: %s", tfname, strerror(errno) );
793 fprintf( cfile, "H%s\n", hostname ); /* XXX lp_host? */
795 if ( lp.lp_person ) {
796 fprintf( cfile, "P%s\n", lp.lp_person );
798 fprintf( cfile, "P%s\n", printer->p_operator );
801 if ( lp.lp_job && *lp.lp_job ) {
802 fprintf( cfile, "J%s\n", lp.lp_job );
803 fprintf( cfile, "T%s\n", lp.lp_job );
805 fprintf( cfile, "JMac Job\n" );
806 fprintf( cfile, "TMac Job\n" );
809 fprintf( cfile, "C%s\n", hostname ); /* XXX lp_host? */
811 if ( lp.lp_person ) {
812 fprintf( cfile, "L%s\n", lp.lp_person );
814 fprintf( cfile, "L%s\n", printer->p_operator );
817 for ( letter = 'A'; letter < lp.lp_letter; letter++ ) {
818 fprintf( cfile, "fdf%c%03d%s\n", letter, lp.lp_seq, hostname );
819 fprintf( cfile, "Udf%c%03d%s\n", letter, lp.lp_seq, hostname );
822 if ( lp.lp_job && *lp.lp_job ) {
823 fprintf( cfile, "N%s\n", lp.lp_job );
825 fprintf( cfile, "NMac Job\n" );
829 sprintf( cfname, "cfA%03d%s", lp.lp_seq, hostname );
830 if ( link( tfname, cfname ) < 0 ) {
831 LOG(log_error, logtype_papd, "lp_print can't link %s to %s: %s", cfname,
832 tfname, strerror(errno) );
837 if (( s = lp_conn_unix()) < 0 ) {
838 LOG(log_error, logtype_papd, "lp_print: lp_conn_unix: %s", strerror(errno) );
842 sprintf( buf, "\1%s\n", printer->p_printer );
844 if ( write( s, buf, n ) != n ) {
845 LOG(log_error, logtype_papd, "lp_print write: %s" , strerror(errno));
848 if ( read( s, buf, 1 ) != 1 ) {
849 LOG(log_error, logtype_papd, "lp_print read: %s" , strerror(errno));
853 lp_disconn_unix( s );
855 if ( buf[ 0 ] != '\0' ) {
856 LOG(log_error, logtype_papd, "lp_print lpd said %c: %s", buf[ 0 ], strerror(errno) );
860 if ( ! (lp.lp_job && *lp.lp_job) ) {
861 lp.lp_job = strdup("Mac Job");
864 lp_setup_comments(add_charset(cups_get_language ()));
866 if (lp.lp_person != NULL) {
867 cups_print_job ( printer->p_printer, lp.lp_spoolfile, lp.lp_job, lp.lp_person, printer->p_cupsoptions);
868 } else if (lp.lp_created_for != NULL) {
869 cups_print_job ( printer->p_printer, lp.lp_spoolfile, lp.lp_job, lp.lp_created_for, printer->p_cupsoptions);
871 cups_print_job ( printer->p_printer, lp.lp_spoolfile, lp.lp_job, printer->p_operator, printer->p_cupsoptions);
874 /*LOG(log_info, logtype_papd, "lp_print unlink %s", lp.lp_spoolfile );*/
875 unlink ( lp.lp_spoolfile );
877 #endif /* HAVE_CUPS*/
879 LOG(log_info, logtype_papd, "lp_print queued" );
884 int lp_disconn_unix( int fd )
886 return( close( fd ));
892 struct sockaddr_un saun;
894 if (( s = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
895 LOG(log_error, logtype_papd, "lp_conn_unix socket: %s", strerror(errno) );
898 memset( &saun, 0, sizeof( struct sockaddr_un ));
899 saun.sun_family = AF_UNIX;
900 strcpy( saun.sun_path, _PATH_DEVPRINTER );
901 if ( connect( s, (struct sockaddr *)&saun,
902 strlen( saun.sun_path ) + 2 ) < 0 ) {
903 LOG(log_error, logtype_papd, "lp_conn_unix connect %s: %s", saun.sun_path, strerror(errno) );
911 int lp_disconn_inet( int fd )
913 return( close( fd ));
918 int privfd, port = IPPORT_RESERVED - 1;
919 struct sockaddr_in sin;
923 if (( sp = getservbyname( "printer", "tcp" )) == NULL ) {
924 LOG(log_error, logtype_papd, "printer/tcp: unknown service" );
928 if ( gethostname( hostname, sizeof( hostname )) < 0 ) {
929 LOG(log_error, logtype_papd, "gethostname: %s", strerror(errno) );
933 if (( hp = gethostbyname( hostname )) == NULL ) {
934 LOG(log_error, logtype_papd, "%s: unknown host", hostname );
938 if (( privfd = rresvport( &port )) < 0 ) {
939 LOG(log_error, logtype_papd, "lp_connect: socket: %s", strerror(errno) );
944 memset( &sin, 0, sizeof( struct sockaddr_in ));
945 sin.sin_family = AF_INET;
946 /* sin.sin_addr.s_addr = htonl( INADDR_LOOPBACK ); */
947 memcpy( &sin.sin_addr, hp->h_addr, hp->h_length );
948 sin.sin_port = sp->s_port;
950 if ( connect( privfd, (struct sockaddr *)&sin,
951 sizeof( struct sockaddr_in )) < 0 ) {
952 LOG(log_error, logtype_papd, "lp_connect: %s", strerror(errno) );
966 if (( s = lp_conn_inet()) < 0 ) {
967 LOG(log_error, logtype_papd, "lp_rmjob: %s", strerror(errno) );
971 if ( lp.lp_person == NULL ) {
975 sprintf( buf, "\5%s %s %d\n", printer->p_printer, lp.lp_person, job );
977 if ( write( s, buf, n ) != n ) {
978 LOG(log_error, logtype_papd, "lp_rmjob write: %s", strerror(errno) );
979 lp_disconn_inet( s );
982 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
983 LOG(log_debug, logtype_papd, "read %.*s", n, buf );
986 lp_disconn_inet( s );
990 char *kw_rank = "Rank";
991 char *kw_active = "active";
993 char *tag_rank = "rank: ";
994 char *tag_owner = "owner: ";
995 char *tag_job = "job: ";
996 char *tag_files = "files: ";
997 char *tag_size = "size: ";
998 char *tag_status = "status: ";
1001 struct papfile *out;
1003 char buf[ 1024 ], *start, *stop, *p, *q;
1004 int linelength, crlflength;
1005 static struct papfile pf;
1010 if (( s = lp_conn_unix()) < 0 ) {
1011 LOG(log_error, logtype_papd, "lp_queue: %s", strerror(errno) );
1015 sprintf( buf, "\3%s\n", printer->p_printer );
1017 if ( write( s, buf, n ) != n ) {
1018 LOG(log_error, logtype_papd, "lp_queue write: %s", strerror(errno) );
1019 lp_disconn_unix( s );
1022 pf.pf_state = PF_BOT;
1024 while (( n = read( s, buf, sizeof( buf ))) > 0 ) {
1025 append( &pf, buf, n );
1029 if ( markline( &pf, &start, &linelength, &crlflength ) > 0 ) {
1031 stop = start + linelength;
1032 for ( p = start; p < stop; p++ ) {
1033 if ( *p == ' ' || *p == '\t' ) {
1038 CONSUME( &pf , linelength + crlflength);
1043 * Keys: "Rank", a number, "active"
1044 * Anything else is status.
1047 if ( len == strlen( kw_rank ) &&
1048 strncmp( kw_rank, start, len ) == 0 ) {
1049 CONSUME( &pf, linelength + crlflength );
1052 if (( len == strlen( kw_active ) &&
1053 strncmp( kw_active, start, len ) == 0 ) ||
1054 isdigit( *start )) { /* a job line */
1055 append( out, tag_rank, strlen( tag_rank ));
1056 append( out, start, p - start );
1057 append( out, "\n", 1 );
1059 for ( ; p < stop; p++ ) {
1060 if ( *p != ' ' && *p != '\t' ) {
1064 for ( q = p; p < stop; p++ ) {
1065 if ( *p == ' ' || *p == '\t' ) {
1070 append( out, ".\n", 2 );
1071 CONSUME( &pf, linelength + crlflength );
1074 append( out, tag_owner, strlen( tag_owner ));
1075 append( out, q, p - q );
1076 append( out, "\n", 1 );
1078 for ( ; p < stop; p++ ) {
1079 if ( *p != ' ' && *p != '\t' ) {
1083 for ( q = p; p < stop; p++ ) {
1084 if ( *p == ' ' || *p == '\t' ) {
1089 append( out, ".\n", 2 );
1090 CONSUME( &pf , linelength + crlflength );
1093 append( out, tag_job, strlen( tag_job ));
1094 append( out, q, p - q );
1095 append( out, "\n", 1 );
1097 for ( ; p < stop; p++ ) {
1098 if ( *p != ' ' && *p != '\t' ) {
1102 for ( q = p, p = stop; p > q; p-- ) {
1103 if ( *p == ' ' || *p == '\t' ) {
1107 for ( ; p > q; p-- ) {
1108 if ( *p != ' ' && *p != '\t' ) {
1112 for ( ; p > q; p-- ) {
1113 if ( *p == ' ' || *p == '\t' ) {
1118 append( out, ".\n", 2 );
1119 CONSUME( &pf, linelength + crlflength );
1122 append( out, tag_files, strlen( tag_files ));
1123 append( out, q, p - q );
1124 append( out, "\n", 1 );
1126 for ( ; p < stop; p++ ) {
1127 if ( *p != ' ' && *p != '\t' ) {
1131 append( out, tag_size, strlen( tag_size ));
1132 append( out, p, stop - p );
1133 append( out, "\n.\n", 3 );
1135 CONSUME( &pf, linelength + crlflength );
1140 append( out, tag_status, strlen( tag_status ));
1141 append( out, start, linelength );
1142 append( out, "\n.\n", 3 );
1144 CONSUME( &pf, linelength + crlflength );
1146 append( out, "*\n", 2 );
1147 lp_disconn_unix( s );
1152 #endif /* HAVE_CUPS */