[LUGA] Mit freundlicher Unterstützung von:
WSR

Mail Thread Index


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[luga] Network-Printing mit JetDirect



Hi,
hoffe das erreicht diejenigen, die's interessiert.
Wir (IHS) verwenden etliche HP Laserjets (4.., 5Si..) mit
eingebauten JetDirect-Karten. Der daf├╝r eingesetzte Treiber
(hpdriver.c) ist Shareware, ich sende ihn als Attachment mit.

/etc/printcap:

ljmmc:\
        :lp=/dev/null:\
        :sd=/var/spool/lp/request/ljmmc:\
        :if=/opt/printing/bin/hpdriver:\
        :lf=/var/log/lpd-errs:\
        :af=/var/spool/lp/request/ljmmc/acct:\
        :mx#0:



/var/spool/lp/request/ljmmc/.config:

host <IP-Adresse des Druckers>
port 9100
orientation portrait
bpage 1
btitle <Text auf der Bannerpage>
bprintername <Printername, in diesem Fall ljmmc>



Mit "bpage 0" wird keine Bannerpage gedruckt. Ob PostScript
oder nicht, es funktioniert. Wer Lust hat, kann auch eigene
Meldungen (Datum?) auf das Druckerdisplay schreiben, ist
im Sourcecode nicht schwer zu finden.


/*
// Copyright (C) 1994, 1995, 1996, 1997 Lawrence LeMay. lemay@mail.cs.umn.edu
//
// hpdriver.c, shareware version 2.1  All rights reserved
//
// Copyright (C) 1994, 1995, 1996, 1997 Lawrence LeMay. lemay@mail.cs.umn.edu
//
// cc hpdriver.c -o hpdriver -O
//
// cc hpdriver.c -o hpdriver -O -lsocket -L/usr/ucblib -lucb -lnsl
*/

/*
   alle Logs in ein gemeinsames Logfile: /var/log/lpd-errs (statt fuer jeden
   Drucker eigenes unter /var/spool/lp/request/.....
   Logging um Druckernamen, Datum und Username ergaenzt
   Griessl, 7/98
*/


#include    <stdio.h>
#include    <ctype.h>
#include    <sys/types.h>
#include    <sys/time.h>
#include    <fcntl.h>
#include    <sys/socket.h>
#include    <netinet/in.h>
#include    <arpa/inet.h>
#include    <stdlib.h>
#include    <string.h>
#include    <signal.h>
#include    <memory.h>
#include    <sys/errno.h>
extern int	errno;

#define    MAXLINE    4096

#define UEL "\033%-12345X\n"
#define UELPJL "\033%-12345X@PJL\n"
#define THEEND "\033%-12345X@PJL\n@PJL USTATUSOFF\n\033%-12345X\n"

char    host[80], user[80], capuser[80];
char    sendline[MAXLINE], recvline[MAXLINE + 1];
char    statusfile[80], bannerfile[80];
char    buffer[16384];
int     buffersize;
int     type, port, jobcount, bannercount, sentbanner, tempcount, start, stop;
int	protocol, ustatuspagecount;
int     sockfd, aborting;
FILE    *log, *acct;
long	t;
char	*ct;

sigset_t	newmask, pendmask;

char    duplex[20], binding[20], manualfeed[20], orientation[20], outbin[20];
char    symset[20], ptsize[20], hostaddr[20], resolution[20];
char    bpage[10], btitle[80], bprintername[40];

main(argc, argv)
int    argc;
char    *argv[];
{
    int            i, j, job, sendbuff;
    struct sockaddr_in    serv_addr;

    protocol = 0;

    load_options(argc, argv);

    jobcount = -2;
    bannercount = -1;
    sentbanner = 0;
    ustatuspagecount = 0;
    buffersize = 0;
    aborting = 0;

    sigemptyset(&newmask);
    sigaddset(&newmask, SIGINT);
    if (sigprocmask(SIG_BLOCK, &newmask, 0) < 0) {
        t = time(0);
        ct = ctime(&t); 
        fprintf(log,"hpdriver for %s: %s CANNOT BLOCK SIGINT\n",bprintername,ct);
        fflush(log);
    }

    i = 30; /* # of attempts to connect to printer */
    do {
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family      = AF_INET;
        serv_addr.sin_addr.s_addr = inet_addr(hostaddr);
        serv_addr.sin_port        = htons(port);

        /*
         * Open a TCP socket (an Internet stream socket).
         */

        if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
	    t = time(0);
  	    ct = ctime(&t); 
            fprintf(log,"hpdriver for %s: %s CANNOT OPEN STREAM SOCKET\n",bprintername,ct);
            fflush(log);
            exit(1);
        }

        sendbuff = MAXLINE;
        if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char *) &sendbuff,
                       sizeof(sendbuff)) < 0) {
	    t = time(0);
	    ct = ctime(&t); 
            fprintf(log,"hpdriver for %s: %s CANNOT SET SO_SNDBUF\n",bprintername,ct);
            fflush(log);
            exit(1);
        }

        /*
         * Connect to the server.
         */

        if (connect(sockfd, (struct sockaddr *) &serv_addr,
                sizeof(serv_addr)) < 0) {
            close(sockfd);
            t = time(0);
    	    ct = ctime(&t);
            fprintf(log,"hpdriver for %s: %s %s@%s CANNOT CONNECT TO PRINTER\n",bprintername,ct,user,host);
            fflush(log);
            if (--i <= 0) exit(1);
            switch (i) {
              case 30:
              case 29: sleep(1); break;
              default: sleep(6);
            }
        } else break;
    } while (1);

    errno = 0;

    sendjob(stdin, sockfd);        /* do it all */

    accounting();

    close(sockfd);
    exit(0);
}

load_options(argc, argv)
int    argc;
char    *argv[];
{
    int     i,h,n;
    char    logfile[80], configfile[80];
    char    str1[80], str2[80], str3[80];
    char    *pjloptions, *p, *q;
    FILE    *config;

    h = 0;
    n = 0;
    for(i=0; i<(argc-1); i++) {
        if (!strncmp("-h",argv[i],2)) {
            strcpy(host,argv[i+1]);
            h = 1;
        }
        if (!strncmp("-n",argv[i],2)) {
            strcpy(user,argv[i+1]);
            n = 1;
        }
        if (!strncmp("-c",argv[i],2)) {
	    protocol = 2; 
        }
    }
    if (!n && h) strcpy(user,host);
    strcpy(capuser,user);
    for(i=0; i<strlen(capuser); i++) capuser[i]=toupper(capuser[i]);

    acct = fopen(argv[argc-1], "a"); /* account file */

/*    strcpy(logfile,argv[argc-1]);	*/
/*    strcpy(strrchr(logfile,'/'),"/log");	*/

    strcpy(logfile,"/var/log/lpd-errs");	/* NEU */
    log = fopen(logfile,"a");

    strcpy(statusfile,argv[argc-1]);
    strcpy(strrchr(statusfile,'/'),"/status");

    strcpy(bannerfile,argv[argc-1]);
    strcpy(strrchr(bannerfile,'/'),"/.banner");

    strcpy(configfile,argv[argc-1]);
    strcpy(strrchr(configfile,'/'),"/.config");
    config = fopen(configfile,"r");
    if (config != NULL) {
        while(fgets(recvline,MAXLINE,config) != NULL)
        if (sscanf(recvline,"%s %s",str1, str2)==2) {
            if (!strcmp(str1,"duplex"))      strcpy(duplex,str2);
            if (!strcmp(str1,"binding"))     strcpy(binding,str2);
            if (!strcmp(str1,"manualfeed"))  strcpy(manualfeed,str2);
            if (!strcmp(str1,"orientation")) strcpy(orientation,str2);
            if (!strcmp(str1,"outbin"))      strcpy(outbin,str2);
            if (!strcmp(str1,"host"))        strcpy(hostaddr,str2);
            if (!strcmp(str1,"port"))        sscanf(str2,"%i",&port);
            if (!strcmp(str1,"symset"))      strcpy(symset,str2);
            if (!strcmp(str1,"resolution"))  strcpy(resolution,str2);
            if (!strcmp(str1,"ptsize"))      strcpy(ptsize,str2);
            if (!strcmp(str1,"bpage"))       strcpy(bpage,str2);
            if (!strcmp(str1,"btitle"))      strcpy(btitle,recvline+7);
            if (!strcmp(str1,"bprintername"))strcpy(bprintername,str2);
            if (!strcmp(str1,"protocol"))    sscanf(str2,"%i",&protocol);
        }
    }

    if (pjloptions = getenv("HPOPTIONS"))
        do {
        if (p = strchr(pjloptions,':')) {
            strncpy(str3,pjloptions,(p-pjloptions));
            str3[p-pjloptions]='\0';
        } else
            strcpy(str3,pjloptions);
        if (q=strchr(str3,'=')) *q = ' ';
        if(sscanf(str3,"%s %s",str1, str2)==2) {
            if (!strcmp(str1,"duplex"))      strcpy(duplex,str2);
            if (!strcmp(str1,"binding"))     strcpy(binding,str2);
            if (!strcmp(str1,"manualfeed"))  strcpy(manualfeed,str2);
            if (!strcmp(str1,"orientation")) strcpy(orientation,str2);
            if (!strcmp(str1,"outbin"))      strcpy(outbin,str2);
            if (!strcmp(str1,"host"))        strcpy(hostaddr,str2);
            if (!strcmp(str1,"port"))        sscanf(str2,"%i",&port);
            if (!strcmp(str1,"symset"))      strcpy(symset,str2);
            if (!strcmp(str1,"resolution"))  strcpy(resolution,str2);
            if (!strcmp(str1,"ptsize"))      strcpy(ptsize,str2);
            if (!strcmp(str1,"bpage"))       strcpy(bpage,str2);
            if (!strcmp(str1,"btitle"))      strcpy(btitle,str2);
            if (!strcmp(str1,"bprintername"))strcpy(bprintername,str2);
            if (!strcmp(str1,"protocol"))    sscanf(str2,"%i",&protocol);
        }
        pjloptions = p+1;
        } while (p);
}

sendjob(fp, sockfd)
	FILE    *fp;
	int    sockfd;
{
    int    i, n;
    int    length;
    fd_set                   readfds, writefds;
    FILE	*banner;

#define STARTJOB "\033%-12345X@PJL\n\
@PJL USTATUSOFF\n\
@PJL USTATUS DEVICE = ON\n\
@PJL USTATUS PAGE = ON\n\
@PJL USTATUS JOB = ON\n\
@PJL SET TIMEOUT = 300\n\
@PJL JOB NAME = \"PS or PCL5 Job\"\n\
@PJL DEFAULT PERSONALITY = AUTO\n\
@PJL DEFAULT LPARM : POSTSCRIPT PRTPSERRS = ON\n"

#define ENDJOB "\033%-12345X@PJL\n\
@PJL EOJ NAME = \"End of PJL Job\"\n\
@PJL RESET\n\
\033%-12345X\n"

#define STARTBANNER "\033%-12345X@PJL\n\
@PJL JOB NAME = \"Banner\"\n\
@PJL SET DUPLEX = OFF\n\
@PJL SET ORIENTATION = PORTRAIT\n"

#define ENDBANNER "\033%-12345X@PJL\n\
@PJL EOJ NAME = \"Banner\"\n\
\033%-12345X\n"

#define STARTINNER "\033%-12345X@PJL\n\
@PJL JOB NAME = \"Inner Job\"\n"

#define ENDINNER "\033%-12345X@PJL\n\
@PJL RESET\n\
@PJL EOJ NAME = \"Inner Job\"\n\
\033%-12345X\n"

    if (sigpending(&pendmask) < 0) {
	t = time(0);
        ct = ctime(&t); 
        fprintf(log,"hpdriver for %s: %s SIGPENDING ERROR\n",bprintername,ct);
        fflush(log);
    }

    if (sigismember(&pendmask, SIGINT)) {
        t = time(0);
        ct = ctime(&t); 
        fprintf(log,"hpdriver for %s: %s SIGINT CAUGHT: JOB ABORTED\n",bprintername,ct);
        fflush(log);
        exit(0);
    }

    outbuff(STARTJOB,strlen(STARTJOB));

    start = getpagecount(sockfd);
/***
    fprintf(log,"hpdriver: Initial page count is %i\n",start);
    fflush(log);
***/

/* hack for printing banner page */
  if (!strcmp("1",bpage)) { /* create a banner page */
    sentbanner = 1;
    outbuff(STARTBANNER,strlen(STARTBANNER));
    fmtbuff("@PJL SET OUTBIN = %s\n",outbin);
    fmtbuff("@PJL SET MANUALFEED = %s\n",manualfeed);
    outbuff("%!\n",3);
    outbuff("/Courier-Bold findfont 20 scalefont setfont\n",44);
    fmtbuff("72 576 moveto (Name:     %s) show\n",user);
    fmtbuff("72 546 moveto (Host:     %s) show\n",host);
    fmtbuff("72 516 moveto (Printer:  %s) show\n",bprintername);
    t = time(0);
    ct = ctime(&t);
    fmtbuff("72 486 moveto (Date:     %s) show\n",ct);
    outbuff("/Courier-Bold findfont 30 scalefont setfont\n",44);
    outbuff("/printtitle\n",12);
    fmtbuff("{0 0 moveto (%s) show} def\n",btitle);
    outbuff("72 648 translate\n",17);
    outbuff(".95 -.05 0 {setgray printtitle -1 .5 translate} for\n",52);
    outbuff("1 setgray printtitle\n",22);
    outbuff("showpage\n",9);
    outbuff(ENDBANNER,strlen(ENDBANNER));
  } 
  else if (strcmp("2",bpage)) { /* use a .banner file if available */
    banner = fopen(bannerfile,"r");
    if (banner != NULL) {
        sentbanner = 1;
        outbuff(STARTBANNER,strlen(STARTBANNER));
        fmtbuff("@PJL SET OUTBIN = %s\n",outbin);
        fmtbuff("@PJL SET MANUALFEED = %s\n",manualfeed);
        while (!feof(banner)) {
            length = fread(sendline, sizeof(char), MAXLINE, banner);
            outbuff(sendline,length);
        }
        outbuff(ENDBANNER,strlen(ENDBANNER));
    }
  }

/* send the main job */
    outbuff(STARTINNER,strlen(STARTINNER));

    fmtbuff("@PJL SET DUPLEX = %s\n",duplex);
    fmtbuff("@PJL SET BINDING = %s\n",binding);
    fmtbuff("@PJL SET MANUALFEED = %s\n",manualfeed);
    fmtbuff("@PJL SET ORIENTATION = %s\n",orientation);
    fmtbuff("@PJL SET OUTBIN = %s\n",outbin);
    fmtbuff("@PJL SET SYMSET = %s\n",symset);
    fmtbuff("@PJL SET RESOLUTION = %s\n",resolution);
    fmtbuff("@PJL SET PTSIZE = %s\n",ptsize);
    type = 0;
    do {
        FD_ZERO(&readfds);
        FD_SET(sockfd, &readfds);
        FD_ZERO(&writefds);
        FD_SET(sockfd, &writefds);
        select(sockfd+1, &readfds, &writefds, (fd_set *) 0, 0);

        if (FD_ISSET(sockfd, &readfds)) read_response(sockfd);
        if (FD_ISSET(sockfd, &writefds)) {
            if (length = fread(sendline, sizeof(char), MAXLINE, fp)) {
                if (!type) {
                    i = strlen(sendline);
                    if (i < 2) type=2;
                    else if (!strncmp("%!",sendline,2)) type=1;
                    else if ((i>2) && (!strncmp("\004%!",sendline,3))) type=1;
                    else if ((i>8) && (!strncmp("\033%-12345X",sendline,9)))
                         type=3;
                    else type = 2;

                    if ((type == 1) && (protocol == 2))
                        outbuff("\033%-12345X\001M",11);
                    if (type == 2)
                        outbuff("\033&k2G",5);
                }
                if ((type == 1) && (protocol == 2))
                    outbuffquoted(sendline,length);
                else outbuff(sendline,length);
            }
        }
        if (sigpending(&pendmask) < 0) {
            t = time(0);
            ct = ctime(&t); 
            fprintf(log,"hpdriver for %s: %s SIGPENDING ERROR\n",bprintername,ct);
            fflush(log);
        }
        if (sigismember(&pendmask, SIGINT)) {
            t = time(0);
            ct = ctime(&t);
            fprintf(log,"hpdriver for %s: %s SIGINT CAUGHT: ATTEMPTING TO ABORT REMAINING JOB\n",bprintername,ct);
            fflush(log);
            outbuff("\003\004",2);  /* ctrl-C, EOF */
            flushbuff();
            length = 0;
        }
    } while ( length>0 );
    outbuff(ENDINNER,strlen(ENDINNER));
    flushbuff();

    if (jobcount == -2) getjobcount(sockfd);
/***
    fprintf(log,"hpdriver: Job page count (including banner page) is %i\n",jobcount);
    fflush(log);
***/

    stop = getpagecount(sockfd);
/***
    fprintf(log,"hpdriver: Ending page count is %i\n",stop);
    fflush(log);
***/

    outbuff(ENDJOB,strlen(ENDJOB));
}

/*
 * Read a line from a descriptor.  Read the line one byte at a time,
 * looking for a newline or '\014'.  We store the newline in the buffer,
 * then follow it with a null (the same as fgets(3)).
 * We return the number of characters up to, but not including,
 * the null (the same as strlen(3)).
 */

int
readline(fd, ptr, maxlen)
	int    fd, maxlen;
	char   *ptr;
{
    int                      n, rc;
    char                     c;
    static struct timeval    timeout;
    fd_set                   readfds;
    char                     *head;

    head = ptr;
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;

    for (n = 1; n < maxlen; n++) {
        FD_ZERO(&readfds);
        FD_SET(fd, &readfds);
        if (select(fd+1, &readfds, (fd_set *) 0, (fd_set *) 0, &timeout) < 0) {
            t = time(0);
            ct = ctime(&t); 
            fprintf(log,"hpdriver for %s: %s SELECT ERROR IN READLINE\n",bprintername,ct);
            fflush(log);
            aborting = 1;
            accounting();
            exit(0);
        }

        if (!FD_ISSET(fd, &readfds)) break;

        if ( (rc = read(fd, &c, 1)) == 1) {
            *ptr++ = c;
            if (c == '\n') break;
            if (c == '\014') break;
        } else if (rc == 0) {
            if (n == 1)
                return(0);    /* EOF, no data read */
            else
                break;        /* EOF, some data was read */
        } else {
    	    t = time(0);
    	    ct = ctime(&t); 
	    fprintf(log,"hpdriver for %s: %s IN READLINE, ERRNO = %d\n",bprintername,ct,errno);
	    fflush(log);
            aborting = 1;
            accounting();
            exit(0);  /* error */
        }
    }

    *ptr = 0;
    return(n);
}

int getpagecount(sockfd)
	int    sockfd;
{
    int i, n, x, loop, count;
    char *query[5];
    struct timeval    tp;

    query[0] =    "\033%-12345X@PJL\n";
    query[1] =    "@PJL ECHO ABCDE\n";
    query[2] =    "\033%-12345X@PJL\n";
    query[3] =    "@PJL INFO PAGECOUNT\n";
    query[4] =    "\033%-12345X@PJL\n";

    count = -1;
    for (x=0; x<10; x++) {

        for(i=0; i<5; i++) outbuff(query[i],strlen(query[i]));
        flushbuff();

        loop = 10;
        while (loop) {
            n = readline(sockfd, recvline, MAXLINE);
            if (n > 0) {
                if (!strlen(recvline)) {
                    loop--;
                    continue;
                }
                if (!strncmp(recvline,query[1],15)){
/* recieved the appropriate echo line, now we can read the pagecount data */
                    n = readline(sockfd, recvline, MAXLINE);
                    n = readline(sockfd, recvline, MAXLINE);
                    n = readline(sockfd, recvline, MAXLINE);
                    sscanf(recvline,"%i",&count);
                    n = readline(sockfd, recvline, MAXLINE);
                    return count;
                }
            }
        }
        if (count > -1) break;
    }
    return -1; /* error, couldnt get the pagecount */
}

read_response(sockfd)
	int    sockfd;
{
    int i, n, count, end, name, code;
    char *p, *q;
    FILE *status;

    n = readline(sockfd, recvline, MAXLINE);
    if (n < 0) {
        t = time(0);
        ct = ctime(&t); 
        fprintf(log,"hpdriver for %s: %s READLINE ERROR\n",bprintername,ct);
        fflush(log);
        exit(0);
    } 
    if (n > 0) {
        if (!strlen(recvline)) return;

	if (strstr(recvline,"@PJL USTATUS PAGE")) {
	    ustatuspagecount++;
            for(i=0; i<5; i++) {
                n = readline(sockfd, recvline, MAXLINE);
                if (recvline[0] == '\014') break;
	    }
	}

        if (strstr(recvline,"@PJL USTATUS DEVICE")) {
            for(i=0; i<10; i++) {
              if (readline(sockfd, recvline, MAXLINE) > 0) {
                if (recvline[0] == '\014') break;
                if (p=strstr(recvline,"DISPLAY")) {
                    if ((p=strchr(p,'\"')) != NULL) {
                        p++;
                        if ((q=strchr(p,'\"')) != NULL) {
                            *q = '\0';
                            status = fopen(statusfile,"w");
                            fprintf(status,"%s\n",p);
		            t = time(0);
  			    ct = ctime(&t); 
                            fprintf(log,"hpdriver for %s: %s NEW DEVICE STATUS: %s\n",bprintername,ct,p);
                            fflush(log);
                            fclose(status);
                        }
                    }
                }
              }
            }
        }

        if (strstr(recvline,"@PJL USTATUS JOB")) {
            end = 0; name = 0;
            for(i=0; i<10; i++) {
                n = readline(sockfd, recvline, MAXLINE);
                if (strstr(recvline,"END")) {
                    end = 1;
                    continue;
                }
                if (strstr(recvline,"NAME")) {
                    if (strstr(recvline,"Inner Job")) name = 1;
                    else if (strstr(recvline,"Banner")) name = 2;
                    continue;
                }
                if (strstr(recvline,"PAGES")) {
                    sscanf(recvline,"PAGES=%i",&count);
                    tempcount += count;
                    if (name == 2) bannercount = count;
/****
                    fprintf(log,"hpdriver: Count is : %d\n",count);
                    fflush(log);
****/
                    if (end && (name == 1)) {
                        jobcount = tempcount;
                        break;
                    }
                }
                if (recvline[0] == '\014') break;
            }
        }

        if (strstr(recvline,"PCL")) {
            for(i=0; i<10; i++) {
                n = readline(sockfd, recvline, MAXLINE);
                if (recvline[0] == '\014') break;
            }
        }
        if (strstr(recvline,"%%[")) {
        }
    }
}

getjobcount(sockfd)
	int    sockfd;
{
	long    seconds;

#define NULLJOB "\033%-12345X@PJL COMMENT ethernet test\n\033%-12345X\n"

    seconds = time(0);
    while (jobcount == -2) {
        read_response(sockfd);

        if (sigpending(&pendmask) < 0) {
   	    t = time(0);
  	    ct = ctime(&t);
            fprintf(log,"hpdriver for %s: %s SIGPENDING ERROR\n",bprintername,ct);
            fflush(log);
        }

        if (sigismember(&pendmask, SIGINT)) {
            t = time(0);
            ct = ctime(&t);
            fprintf(log,"hpdriver for %s: %s SIGINT IN GETJOBCOUNT\n",bprintername,ct);
            fflush(log);
            aborting = 1;
            accounting();
            exit(0);
        }

        if ((time(0) - seconds) > 10) {
            seconds = time(0);
            outbuff(NULLJOB,strlen(NULLJOB));
            flushbuff();
        }
    }
}

fmtbuff(fmt, arg) 
	char *fmt, *arg;
{
	char line[256];

	if (strlen(arg) > 0) {
		sprintf(line,fmt,arg);
		outbuff(line,strlen(line));
	}
}

int outbuff(ptr, len)
	char   *ptr;
	int len;
{
    int    nleft, nwritten;
    char   *index;
    static struct timeval    timeout;
    fd_set readfds, writefds;

    timeout.tv_sec = 2;
    timeout.tv_usec = 0;

    if (buffersize+len >= 16384) flushbuff(); /* shouldnt happen */

    memcpy(&buffer[buffersize],ptr,len);
    buffersize += len;

    if (buffersize >= 8192) {

        nleft = 8192;
        index = buffer;

        while (nleft > 0) {

            FD_ZERO(&writefds);
            FD_ZERO(&readfds);
            FD_SET(sockfd, &writefds);
            FD_SET(sockfd, &readfds);
            select(sockfd+1, &readfds, &writefds, (fd_set *) 0, &timeout);

            if (FD_ISSET(sockfd, &writefds)) {
                nwritten = write(sockfd, index, nleft);

                if ((nwritten < 0) || ((errno > 94) && (errno < 149))) {
	                t = time(0);
           	        ct = ctime(&t);
                	fprintf(log,"HPDRIVER for %s: %s IN OUTBUFF, ERRNO = %d\n",bprintername,ct,errno);
                    fflush(log);
                    aborting = 1;
                    accounting();
                    exit(0);
                }

                nleft -= nwritten;
                index += nwritten;
            }

            if (FD_ISSET(sockfd, &readfds)) read_response(sockfd);
        }
        memcpy(buffer,buffer+8192,buffersize-8192);
        buffersize -= 8192;
    }
}

int outbuffquoted(ptr, len)
	char   *ptr;
	int len;
{
    int    i, nleft, nwritten;
    char   *index;
    static struct timeval    timeout;
    fd_set readfds, writefds;

    timeout.tv_sec = 2;
    timeout.tv_usec = 0;

    if (buffersize+len >= 16384) flushbuff(); /* shouldnt happen */

    for (i=0; i<len; i++)
      switch (ptr[i]) {
        case '\001' :
        case '\003' :
        case '\004' :
        case '\005' :
        case '\021' :
        case '\023' :
        case '\024' :
        case '\033' :
        case '\034' : buffer[buffersize++] = '\001';
                      buffer[buffersize++] = (ptr[i] ^ 0x40);
                      break;
        default : buffer[buffersize++] = ptr[i];
      }

    if (buffersize >= 8192) {

        nleft = 8192;
        index = buffer;

        while (nleft > 0) {

            FD_ZERO(&writefds);
            FD_ZERO(&readfds);
            FD_SET(sockfd, &writefds);
            FD_SET(sockfd, &readfds);
            select(sockfd+1, &readfds, &writefds, (fd_set *) 0, &timeout);

            if (FD_ISSET(sockfd, &writefds)) {
                nwritten = write(sockfd, index, nleft);

                if ((nwritten < 0) || ((errno > 94) && (errno < 149))) {
		    t = time(0);
		    ct = ctime(&t); 
                    fprintf(log,"HPDRIVER for %s: %s IN OUTBUFF, ERRNO = %d\n",bprintername,ct,errno);
                    fflush(log);
                    aborting = 1;
                    accounting();
                    exit(0);
                }

                nleft -= nwritten;
                index += nwritten;
            }

            if (FD_ISSET(sockfd, &readfds)) read_response(sockfd);
        }
        memcpy(buffer,buffer+8192,buffersize-8192);
        buffersize -= 8192;
    }
}

flushbuff() 
{
    int    nleft, nwritten;
    char   *index;
    static struct timeval    timeout;
    fd_set readfds, writefds;

    timeout.tv_sec = 2;
    timeout.tv_usec = 0;

    nleft = buffersize;
    index = buffer;

    while (nleft > 0) {

        FD_ZERO(&writefds);
        FD_ZERO(&readfds);
        FD_SET(sockfd, &writefds);
        FD_SET(sockfd, &readfds);
        select(sockfd+1, &readfds, &writefds, (fd_set *) 0, &timeout);

        if (FD_ISSET(sockfd, &writefds)) {
            nwritten = write(sockfd, index, nleft);

            if ((nwritten < 0) || ((errno > 94) && (errno < 149))) {
		t = time(0);
   	        ct = ctime(&t); 
    	        fprintf(log,"HPDRIVER for %s: %s IN FLUSHBUFF, ERRNO = %d\n",bprintername,ct,errno);
                fflush(log);
                aborting = 1;
                accounting();
                exit(0);
            }

            nleft -= nwritten;
            index += nwritten;
        }

        if (FD_ISSET(sockfd, &readfds)) read_response(sockfd);
    }
    buffersize = 0;
}

accounting()
{
    if ((bannercount == -1) && (sentbanner)) bannercount = 1;
    if (jobcount < 1) {
        if ((start == -1) || (stop == -1) || (start > stop)) {
            jobcount = 0;
        } else {
            jobcount = stop - start;
        }
    }
    if ((jobcount < 1) && (ustatuspagecount > 0)) jobcount = ustatuspagecount;
    if ((bannercount == 1) || (bannercount == 2)) jobcount -= bannercount;
    if (jobcount < 0) jobcount = 0; 
    fprintf(acct,"%4d.00 %s:%s\n",jobcount,host,user);
    fflush(acct);

    if (aborting) {
	t = time(0);
        ct = ctime(&t); 
        fprintf(log,"hpdriver for %s: %s %s@%s JOB ABORTED, PAGE COUNT = %d\n",bprintername,ct,user,host,jobcount);
        fflush(log);
        return; /* socket is down, so cannot call outbuff */
    }
    t = time(0);
    ct = ctime(&t);
    fprintf(log,"hpdriver for %s: %s %s@%s job completed, page count = %d\n",bprintername,ct,user,host,jobcount);
    fflush(log);

/* the next 4 lines display information on the LCD display */
    outbuff(UELPJL,strlen(UELPJL));
  /*  sprintf(sendline,"@PJL RDYMSG DISPLAY = \"00 %-8s %i\"\n",capuser,jobcount);   */
    sprintf(sendline,"@PJL RDYMSG DISPLAY = \"00 %s ready\"\n",bprintername);
    outbuff(sendline,strlen(sendline));
    outbuff(UEL,strlen(UEL));

    outbuff(THEEND,strlen(THEEND));
    flushbuff();
}

------------------------------------------------------------------------
Peter Griessl                                email:   griessl@ihs.ac.at
Institut fuer Hoehere Studien (IHS)          Tel:     +43-1- 599 91 222
A-1060 Wien, Stumpergasse 56                 Fax:     +43-1- 597 06 35
------------------------------------------------------------------------


powered by LINUX the choice of a gnu generation
linux user group austria;
Suche
Suche
Letzte Änderung:
webmaster@luga.at
September 2010