*BSD News Article 84130


Return to BSD News archive

Newsgroups: comp.unix.bsd.freebsd.misc
Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!news.cs.su.oz.au!metro!metro!munnari.OZ.AU!news.ecn.uoknor.edu!news.wildstar.net!newsfeed.direct.ca!op.net!news.mathworks.com!howland.erols.net!vixen.cso.uiuc.edu!uwm.edu!reuter.cse.ogi.edu!qiclab.scn.rain.com!psgrain!quack!quack.kfu.com!nsayer
From: nsayer@quack.kfu.com (Nick Sayer)
Subject: Jetdirect via *BSD
Message-ID: <nGogtM2@quack.kfu.com>
Sender: news@quack.kfu.com (0000-News(0000))
Organization: The Duck Pond public unix, +1 408 249 9630, log in as guest.
Date: Mon, 2 Dec 1996 21:45:31 UTC
Lines: 124

I suppose this has been done, but I'll post it anyway.

HP Jetdirects are as varied as the printers they serve. One thing
they all have in common, though, is that when they are given an IP
address, they will listen for print jobs on port 9100, accepting
one connection at a time.

So to get this to work with a FreeBSD machine being the spooler,
I wrote this little C program. It sends stdin to a given host at
port 9100. If you want, you can change the port number with -p and
a -c will cause it to change \n to \r\n. You use it with printcap
entries like this:

lp:\
	:lp=/dev/null.lp:sd=/var/spool/lp:if=/etc/lpf:\
        :lf=/var/log/lpd-errs:

here's /etc/lpf:

#! /bin/sh
exec /etc/sock -c hpcorporate
exit 1

You need a separate copy of /dev/null for each printer, since lpd tries
to use file locking to prevent multiple lpds from trying to print to the
same printer. Since each job wants to have its own socket, you should
use if= instead of of=. If you're hooking a jetdirect up to a non-postscript
printer, you can use a filter that runs ghostscript to do whatever
postscript-to-raster conversion is appropriate.

----- cut here -----

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>

extern struct hostent *gethostbyname();
extern int getopt();

extern char *optarg;
extern int optind, opterr;

void usage(c)
char *c;
{
  printf("Usage: %s [-c] [-p port] host\n",c);
  exit(1);
}

main(argc,argv)
int argc;
char **argv;
{
  int cr=0,sock,c;
  unsigned short port=9100;
  FILE *f;
  struct hostent *hostad;
  struct sockaddr_in addr;

  while ((c = getopt(argc, argv, "cp:")) != -1)
  {
    switch (c)
    {
      case 'c': cr++;
      break;
      case 'p': port=atoi(optarg);
      break;
    }
  }

  if (optind!=argc-1)
    usage(*argv);

  hostad=gethostbyname(argv[optind]);
  if (hostad==NULL)
  {
    herror("gethostbyname()");
    exit(1);
  }

  bcopy(hostad->h_addr_list[0],&addr.sin_addr.s_addr,4);
  addr.sin_port=htons(port);
  addr.sin_family=AF_INET;

  sock=socket(PF_INET,SOCK_STREAM,0);
  if (sock<0)
  {
    perror("socket()");
    exit(1);
  }

  if (connect(sock,(struct sockaddr *)&addr,sizeof(addr))<0)
  {
    perror("connect()");
    exit(1);
  }

  f=fdopen(sock,"w");
  if (f==NULL)
  {
    perror("fdopen()");
    exit(1);
  }

  while(!feof(stdin))
  {
    c=getchar();
    if (c=='\n' && cr)
      putc('\r',f);
    putc(c,f);
  }

  exit(0);
}

----- ereh tuc -----

-- 
Nick Sayer <nsayer@quack.kfu.com>  | 
N6QQQ @ N0ARY.#NORCAL.CA.USA.NOAM  | WASPs are people too.
+1 408 249 9630, log in as 'guest' | 
URL: http://www.kfu.com/~nsayer/   |