home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / LPR.C < prev    next >
C/C++ Source or Header  |  1992-07-29  |  11KB  |  389 lines

  1. /*
  2.  * LPR - dump job to printer
  3.  *
  4.  *
  5.  *   Copyright (C) 1991, University of Waterloo
  6.  *
  7.  *   Portions Copyright (C) 1990, National Center for Supercomputer Applications
  8.  *   and portions copyright (c) 1990, Clarkson University
  9.  *
  10.  *   This program is free software; you can redistribute it and/or modify
  11.  *   it, but you may not sell it.
  12.  *
  13.  *   This program is distributed in the hope that it will be useful,
  14.  *   but without any warranty; without even the implied warranty of
  15.  *   merchantability or fitness for a particular purpose.
  16.  *
  17.  *       Erick Engelke                   or via E-Mail
  18.  *       Faculty of Engineering
  19.  *       University of Waterloo          Erick@development.watstar.uwaterloo.ca
  20.  *       200 University Ave.,
  21.  *       Waterloo, Ont., Canada
  22.  *       N2L 3G1
  23.  *
  24.  *
  25.  * The following notes on control and data files were obtained from Clarkson's
  26.  * CUTE project.
  27.  *
  28.  *
  29.  * Control File: format is the first character in the line is a command,
  30.  * the rest of the line is the argument.  Also note lowercase letters
  31.  * denote the data file names (of various types).
  32.  *
  33.  * currently valid commands are:
  34.  *
  35.  *     J -- "job name" on banner page
  36.  *     C -- "class name" on banner page
  37.  *     L -- "literal" user's name to print on banner
  38.  *     T -- "title" for pr
  39.  *     H -- "host name" of machine where lpr was done
  40.  *     P -- "person" user's login name
  41.  *     I -- "indent" amount to indent output
  42.  *     f -- "file name" name of text file to print
  43.  *     l -- "file name" text file with control chars
  44.  *     p -- "file name" text file to print with pr(1)
  45.  *     t -- "file name" troff(1) file to print
  46.  *     n -- "file name" ditroff(1) file to print
  47.  *     d -- "file name" dvi file to print
  48.  *     g -- "file name" plot(1G) file to print
  49.  *     v -- "file name" plain raster file to print (impress)
  50.  *     c -- "file name" cifplot file to print
  51.  *     1 -- "R font file" for troff
  52.  *     2 -- "I font file" for troff
  53.  *     3 -- "B font file" for troff
  54.  *     4 -- "S font file" for troff
  55.  *     N -- "name" of file (used by lpq)
  56.  *     U -- "unlink" name of file to remove
  57.  *           (after we print it. (Pass 2 only)).
  58.  *     M -- "mail" to user when done printing
  59.  *
  60.  * Currently it looks like only a lowercase filename and U command are
  61.  * necessary.  However one should also include J, L, H, and P.
  62.  *
  63.  * In general the lpd program doesn't care what the data file looks like.
  64.  * It should however be of the type specified in the control file
  65.  * otherwise it will probably print incorrectly.
  66.  *
  67.  * The format is ?fA<number><hostname>.  ? is either c for control or d
  68.  * for data files.  Number is a 3 digit number (0 padded) used for job
  69.  * number information.  Hostname is the name of the originating host and
  70.  * had best be equal to whatever shows up in the from field when a
  71.  * connection is opened (ie probably should be the "real" hostname).
  72.  * Currently all of these must be used as the program has them compiled in
  73.  * (by stupid use of pointers).  I may change this in time but currently
  74.  * it is the law if you want everything to work (some things will work
  75.  * just fine without it, queueing a file just wants names, showing the
  76.  * queue expects a number to start in the fourth position, deleting a file
  77.  * expects the hostname to start in the 7th position and go to the end of
  78.  * the filename.
  79.  *
  80.  * default userid = server = "UNKNOWN";
  81.  *
  82.  * get's sequence file from getenv( "SEQUENCE" )
  83.  * else "SEQUENCE.LPR"
  84.  *
  85.  * if ( ( title = getenv( "TITLE" )) ==  NULL )
  86.  *      title = "stdprn";
  87.  * if ( ( jobname = getenv( "JOBNAME" )) == NULL )
  88.  *       jobname = "job_name";
  89.  * if ( (  class = getenv("CLASS")) == NULL )
  90.  *       class = server
  91.  */
  92.  
  93.  
  94. #include <stdio.h>
  95. #include <io.h>
  96. #include <tcp.h>
  97.  
  98. #define SHORT_LIST 3
  99. #define LONG_LIST  4
  100.  
  101. #define LPQ_PORT 515
  102. #define LOCAL_PORT 722
  103.  
  104. #define SEQFILE "SEQUENCE.LPR"
  105. char *seqwhere;
  106.  
  107. word getsequence()
  108. {
  109.     static word oldseq = 0;
  110.     FILE *f;
  111.  
  112.     if ( ! oldseq ) {
  113.         /* must look to disk for an old sequence number */
  114.         if ( f = fopen( seqwhere, "rt" )) {
  115.             fscanf( f, "%u", &oldseq );
  116.             fclose( f );
  117.         }
  118.     }
  119.  
  120.     oldseq = ( oldseq + 1 ) % 1000;
  121.  
  122.     if ( f = fopen( seqwhere, "wt" )) {
  123.         fprintf( f, "%u", oldseq );
  124.         fclose( f );
  125.     }
  126.     return( oldseq );
  127. }
  128.  
  129. lpr( localhostname, printer, rhostname, filename, title, jobname,
  130.     class, username, servername )
  131. char *localhostname;
  132. char *printer;
  133. char *rhostname;
  134. char *filename;
  135. char *title;
  136. char *jobname;
  137. char *class;
  138. char *username;
  139. char *servername;
  140. {
  141.     tcp_Socket socketdata, *s;
  142.     longword filesize;
  143.     longword host;
  144.     int status = 0;
  145.     int connected = 0;
  146.     int completed = 0;
  147.     int localport;
  148.     word seqnum;
  149.  
  150.     char buffer[ 1024 ];
  151.     char remotename[ 80 ];
  152.     char cmdfile[ 1024 ];
  153.     longword remaining, found, bufsize;
  154.     FILE *f;
  155.  
  156.  
  157.     if (!(f = fopen( filename, "rb"))) {
  158.     printf("Unable to open file '%s'\n", filename );
  159.     return( 3 );
  160.     }
  161.  
  162.     sock_init();
  163.     s = &socketdata;
  164.     if (!(host = resolve( rhostname ))) {
  165.         printf( "lpq: unknown host %s\n",rhostname);
  166.         return(1);
  167.     }
  168.  
  169.     localport = 255 + (MsecClock() & 255);
  170.     if ( !tcp_open( s, localport, host, LPQ_PORT, NULL)) {
  171.       printf("Unable to open socket.");
  172.       tcp_shutdown();
  173.       return(1);
  174.    }
  175.  
  176.    printf( "connecting...\r");
  177.    sock_wait_established( s, sock_delay, NULL, &status );
  178.    connected = 1;
  179.    printf("connection established\n");
  180.  
  181.    /* is there an opening message - non-standard but possible */
  182.  
  183.    if (sock_dataready( s )) {
  184.        sock_fastread( s, buffer, sizeof( buffer ));
  185.        sock_tick( s, &status );    /* in case above message closed port */
  186.    }
  187.  
  188.    /* use ipnumber/time  */
  189.    seqnum = getsequence();
  190.    sprintf(remotename,"fA%03u%s", seqnum, localhostname );
  191.    
  192.    /* we are connected */
  193.    sprintf( buffer, "\2%s\n", printer );    /* select a printer */
  194.    sock_puts( s, buffer );
  195.  
  196.    /* state #2 */
  197.  
  198.    sock_wait_input( s, sock_delay, NULL, &status );
  199.  
  200.    sock_fastread( s, buffer, sizeof( buffer ));
  201.  
  202.    switch (*buffer) {
  203.        case 0 : break;
  204.        case 1 : printf("Printer '%s' is not available\n", printer);
  205.         goto close_it;
  206.        default: rip( buffer );
  207.                 printf("ERROR: %s\n", buffer);
  208.         goto close_it;
  209.    }
  210.  
  211.    /* printer is accepted, printing file */
  212.    filesize = filelength( fileno( f ));
  213.  
  214.  
  215.    sprintf( buffer, "\3%ld d%s\n", filesize, remotename );
  216.    sock_puts( s , buffer );
  217.    sock_wait_input( s, sock_delay, NULL, &status );
  218.  
  219.    /* state 3, reply from filename */
  220.    sock_fastread( s, buffer, sizeof( buffer ));
  221.  
  222.    switch (*buffer) {
  223.     case 0: break;
  224.         case 1: printf("remote host complains of bad connection");
  225.         goto close_it;
  226.     case 2: puts("remote host out of storage space");
  227.         goto close_it;
  228.     }
  229.  
  230.     /* dump file */
  231.  
  232.     remaining = filesize;
  233.     bufsize = (longword) sizeof( buffer );
  234.     do {
  235.     if (remaining < bufsize ) bufsize = remaining;
  236.     if (!(found = fread( buffer, 1, sizeof(buffer), f)))
  237.         break;
  238.     sock_write( s, buffer, found );
  239.     sock_tick( s , &status );
  240.     if (sock_dataready( s )) {
  241.         puts("LPR: interrupted on transfer...");
  242.         goto close_it;
  243.     }
  244.     } while ( (remaining -= found) > 0 );
  245.  
  246.     sock_putc( s, 0 );
  247.  
  248.     /* check on status of this file */
  249.  
  250.     sock_wait_input( s, sock_delay, NULL, &status );
  251.     sock_fastread( s, buffer, sizeof(buffer));
  252.  
  253.     switch (*buffer) {
  254.     case 0: break;
  255.     default:puts("file was rejected");
  256.         goto close_it;        /* could retry */
  257.     }
  258.  
  259.     sprintf( cmdfile,   "H%s\n"  "P%s\n" "C%s\n" "L%s\n"
  260.             "T%s\n" "fd%s\n" "N%s\n" "Ud%s\n" "J%s\n",
  261.         servername,     /* eg "development" */
  262.     username,    /* eg "erick", */
  263.         class ? class : servername, /* eg "NDSW or development", */
  264.  
  265.     username,        /* eg  "erick",    for on banner */
  266.  
  267.         title,          /* title */
  268.     remotename,    /* file processor */
  269.         title,          /* name */
  270.     remotename,
  271.         jobname
  272.     );
  273.  
  274.     sprintf( buffer, "\2%d c%s\n", strlen( cmdfile ), remotename );
  275.  
  276.     sock_puts( s , buffer );
  277.     sock_flush( s );
  278.  
  279.     sock_wait_input( s, sock_delay, NULL, &status );
  280.     sock_fastread( s, buffer, sizeof( buffer ));
  281.  
  282.     switch (*buffer) {
  283.     case 0: break;
  284.         case 1: puts("Bad connection");
  285.         goto close_it;
  286.         case 2: puts("Out of space on host");
  287.                 goto close_it;
  288.         default:puts("Unknown error");
  289.                 goto close_it;
  290.     }
  291.  
  292.     sock_puts( s, cmdfile );
  293.     sock_putc( s, 0 );
  294.     sock_flush( s );
  295.  
  296.     sock_wait_input( s, sock_delay, NULL, &status );
  297.     sock_fastread( s, buffer, sizeof( buffer ));
  298.  
  299.     switch (*buffer) {
  300.         case 0: puts("Completed - job accepted");
  301.                 completed = 1;
  302.         sock_putc( s, 0 );
  303.         sock_flush( s );
  304.         break;
  305.         default:puts("Control file rejected");
  306.                 status = 3;
  307.                 break;
  308.     }
  309.  
  310.     /* all roads come here */
  311. close_it:
  312.     sock_tick( s, &status);    /* in case they sent reset */
  313.     sock_close( s );
  314.     sock_wait_closed( s, sock_delay, NULL, &status );
  315.  
  316. sock_err:
  317.     switch( status ) {
  318.     case 1: break;
  319.         case-1: printf("Remote host reset connection\n\r");
  320.         status = 3;
  321.         break;
  322.     }
  323.     if (!connected)
  324.        printf( "\n\rCould not get connected.  Perhaps you were not in the /etc/hosts.lpd file!\n\r");
  325.  
  326.     return( !completed );
  327. }
  328.  
  329.  
  330. main(argc, argv)
  331. int argc;
  332. char **argv;
  333. {
  334.     char remotename[128];
  335.     char *hostname;
  336.     char *filename;
  337.     char *printer;
  338.     char *userid, *server;
  339.     char *title, *jobname;
  340.     char *rhostname;
  341.     char *class;
  342.     int status;
  343.  
  344.     userid = server = "UNKNOWN";
  345.     if ((seqwhere = getenv( "SEQUENCE" )) == NULL )
  346.         seqwhere = SEQFILE;
  347.     if ( ( title = getenv( "TITLE" )) ==  NULL )
  348.         title = "stdprn";
  349.     if ( ( jobname = getenv( "JOBNAME" )) == NULL )
  350.         jobname = "job_name";
  351.     class = getenv("CLASS");
  352.  
  353.     puts("LPR using Waterloo TCP");
  354.     switch (argc) {
  355.     case 3:
  356.         /* no printername */
  357.         rhostname = argv[1];
  358.         filename = argv[2];
  359.         printer = "lp";
  360.         break;
  361.     case 6:
  362.         /* whole thing */
  363.         userid = argv[4];
  364.         server = argv[5];        /* and continue on below */
  365.     case 4:
  366.         /* Hostname and printer */
  367.         printer = argv[1];
  368.         rhostname = argv[2];
  369.         filename = argv[3];
  370.         break;
  371.     default:
  372.               printf( "Usage: LPR [printer] host filename [userid server]");
  373.           exit(1);
  374.    }
  375.     hostname = gethostname( NULL, 0 );
  376.     if ( !hostname || !*hostname ) {
  377.         puts("You must define your hostname in the WATTCP.CFG file!");
  378.         exit( 3 );
  379.     }
  380.  
  381.     strlwr( hostname );
  382.     strlwr( userid );
  383.     strlwr( server );
  384.     status = lpr( hostname, printer, rhostname, filename, title, jobname,
  385.         class, userid, server );
  386.  
  387.     return( status ? 3 : 0 );
  388. }
  389.