home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / CEREBRUM / WAT9609.ZIP / APPS / LPR.C < prev    next >
Text File  |  1994-11-28  |  12KB  |  412 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 <stdlib.h>
  96. #include <string.h>
  97. #include <conio.h>
  98. #include <io.h>
  99. #include <tcp.h>
  100.  
  101. #define SHORT_LIST 3
  102. #define LONG_LIST  4
  103. #define MFCF_COUNT 5
  104. char *mfcf[MFCF_COUNT];
  105.  
  106.  
  107. #define LPQ_PORT 515
  108. #define LOCAL_PORT 722
  109.  
  110. #define SEQFILE "SEQUENCE.LPR"
  111. char *seqwhere;
  112.  
  113. word getsequence( void )
  114. {
  115.     static word oldseq = 0;
  116.     FILE *f;
  117.  
  118.     if ( ! oldseq ) {
  119.         /* must look to disk for an old sequence number */
  120.         if ( (f = fopen( seqwhere, "rt" )) != NULL ) {
  121.             fscanf( f, "%u", &oldseq );
  122.             fclose( f );
  123.         }
  124.     }
  125.  
  126.     oldseq = ( oldseq + 1 ) % 1000;
  127.  
  128.     if ( (f = fopen( seqwhere, "wt" )) != NULL ) {
  129.         fprintf( f, "%u", oldseq );
  130.         fclose( f );
  131.     }
  132.     return( oldseq );
  133. }
  134.  
  135. int lpr( char *localhostname, char *printer, char *rhostname, char *filename,
  136.     char *title, char *jobname, char *class, char *username, char *servername )
  137. {
  138.     static tcp_Socket socketdata;
  139.     tcp_Socket *s;
  140.     longword filesize;
  141.     longword host;
  142.     int status = 0;
  143.     int connected = 0;
  144.     int completed = 0;
  145.     int localport;
  146.     word seqnum, i;
  147.  
  148.     static char buffer[ 1024 ];
  149.     char *b2;
  150.     char remotename[ 80 ];
  151.     static char cmdfile[ 1024 ];
  152.     longword remaining;
  153.     word found;
  154.     FILE *f;
  155.  
  156.     if (( *class == 0 ) || (*class == ' ')) class = NULL;
  157.  
  158.     for ( i = 0; i < MFCF_COUNT ; ++i ) {
  159.         if ( mfcf[ i ] )
  160.             printf("Extra option: %s\n", mfcf[i] );
  161.     }
  162.  
  163.     if ( (f = fopen( filename, "rb")) == NULL ) {
  164.     printf("Unable to open file '%s'\n", filename );
  165.     return( 3 );
  166.     }
  167.  
  168.     sock_init();
  169.     s = &socketdata;
  170.     if (!(host = resolve( rhostname ))) {
  171.         printf( "lpq: unknown host %s\n",rhostname);
  172.         return(1);
  173.     }
  174.  
  175.     localport = 255 + (MsecClock() & 255);
  176.     if ( !tcp_open( s, localport, host, LPQ_PORT, NULL)) {
  177.       printf("Unable to open socket.");
  178.       tcp_shutdown();
  179.       return(1);
  180.    }
  181.  
  182.    printf( "connecting...\r");
  183.    sock_wait_established( s, sock_delay, NULL, &status );
  184.    connected = 1;
  185.    printf("connection established\n");
  186.  
  187.    /* is there an opening message - non-standard but possible */
  188.  
  189.    if (sock_dataready( s )) {
  190.        sock_fastread( s, buffer, sizeof( buffer ));
  191.        sock_tick( s, &status );    /* in case above message closed port */
  192.    }
  193.  
  194.    /* use ipnumber/time  */
  195.    seqnum = getsequence();
  196.    sprintf(remotename,"fA%03u%s", seqnum, localhostname );
  197.    
  198.    /* we are connected */
  199.    sprintf( buffer, "\2%s\n", printer );    /* select a printer */
  200.    sock_puts( s, buffer );
  201.  
  202.    /* state #2 */
  203.  
  204.    sock_wait_input( s, sock_delay, NULL, &status );
  205.  
  206.    sock_fastread( s, buffer, sizeof( buffer ));
  207.  
  208.    switch (*buffer) {
  209.        case 0 : break;
  210.        case 1 : printf("Printer '%s' is not available\n", printer);
  211.         goto close_it;
  212.        default: rip( buffer );
  213.                 printf("ERROR: %s\n", buffer);
  214.         goto close_it;
  215.    }
  216.  
  217.    /* printer is accepted, printing file */
  218.    filesize = filelength( fileno( f ));
  219.  
  220.  
  221.    sprintf( buffer, "\3%ld d%s\n", filesize, remotename );
  222.    sock_puts( s , buffer );
  223.    sock_wait_input( s, sock_delay, NULL, &status );
  224.  
  225.    /* state 3, reply from filename */
  226.    sock_fastread( s, buffer, sizeof( buffer ));
  227.  
  228.    switch (*buffer) {
  229.     case 0: break;
  230.         case 1: printf("remote host complains of bad connection");
  231.         goto close_it;
  232.     case 2: puts("remote host out of storage space");
  233.         goto close_it;
  234.     }
  235.  
  236.     /* dump file */
  237.  
  238.     remaining = filesize;
  239.     do {
  240.         cprintf("Transferred: %lu %c  \r",
  241.             ((filesize - remaining + 1L)*100L)/(filesize+1), 37 );
  242.         backgroundon();
  243.         if ( (found = fread( buffer, 1, sizeof(buffer), f)) == 0 )
  244.         break;
  245.         backgroundoff();
  246.  
  247.         sock_write( s, buffer, found );
  248.     sock_tick( s , &status );
  249.     if (sock_dataready( s )) {
  250.         puts("LPR: interrupted on transfer...");
  251.         goto close_it;
  252.     }
  253.     } while ( (remaining -= (longword)found) > 0 );
  254.  
  255.     sock_putc( s, 0 );
  256.  
  257.     /* check on status of this file */
  258.  
  259.     sock_wait_input( s, sock_delay, NULL, &status );
  260.     sock_fastread( s, buffer, sizeof(buffer));
  261.  
  262.     switch (*buffer) {
  263.     case 0: break;
  264.     default:puts("file was rejected");
  265.         goto close_it;        /* could retry */
  266.     }
  267.  
  268.     sprintf( cmdfile,   "H%s\n"  "P%s\n" "C%s\n" "L%s\n",
  269.         servername,     /* eg "development" */
  270.     username,    /* eg "erick", */
  271.         class ? class : servername, /* eg "NDSW or development", */
  272.  
  273.         username );     /* eg  "erick"       for on banner */
  274.  
  275.     for ( i = 0 ; i < MFCF_COUNT ; ++i ) {
  276.         if ( mfcf[i] ) {
  277.             strcat( cmdfile, mfcf[i] );
  278.             strcat( cmdfile, "\n" );
  279.         }
  280.     }
  281.     b2 = strchr( cmdfile, 0 );
  282.     sprintf( b2, "T%s\n" "fd%s\n" "N%s\n" "Ud%s\n" "J%s\n",
  283.         title,          /* title */
  284.     remotename,    /* file processor */
  285.         title,          /* name */
  286.     remotename,
  287.         jobname
  288.     );
  289.     printf("\nOptions:\n%s\n", cmdfile );
  290.  
  291.     sprintf( buffer, "\2%d c%s\n", strlen( cmdfile ), remotename );
  292.  
  293.     sock_puts( s , buffer );
  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: break;
  301.         case 1: puts("Bad connection");
  302.         goto close_it;
  303.         case 2: puts("Out of space on host");
  304.                 goto close_it;
  305.         default:puts("Unknown error");
  306.                 goto close_it;
  307.     }
  308.  
  309.     sock_puts( s, cmdfile );
  310.     sock_putc( s, 0 );
  311.     sock_flush( s );
  312.  
  313.     sock_wait_input( s, sock_delay, NULL, &status );
  314.     sock_fastread( s, buffer, sizeof( buffer ));
  315.  
  316.     switch (*buffer) {
  317.         case 0: puts("Completed - job accepted");
  318.                 completed = 1;
  319.         sock_putc( s, 0 );
  320.         sock_flush( s );
  321.         break;
  322.         default:puts("Control file rejected");
  323.                 status = 3;
  324.                 break;
  325.     }
  326.  
  327.     /* all roads come here */
  328. close_it:
  329.     sock_tick( s, &status);    /* in case they sent reset */
  330.     sock_close( s );
  331.     sock_wait_closed( s, sock_delay, NULL, &status );
  332.  
  333. sock_err:
  334.     switch( status ) {
  335.     case 1: break;
  336.         case-1: printf("Remote host reset connection\n\r");
  337.         status = 3;
  338.         break;
  339.     }
  340.     if (!connected)
  341.        printf( "\n\rCould not get connected.  Perhaps you were not in the /etc/hosts.lpd file!\n\r");
  342.  
  343.     return( !completed );
  344. }
  345.  
  346.  
  347. int main( int argc, char **argv )
  348. {
  349.     char remotename[128];
  350.     char *hostname;
  351.     char *filename;
  352.     char *printer;
  353.     char *userid, *server;
  354.     char *title, *jobname;
  355.     char *rhostname;
  356.     char *class;
  357.     int status, i;
  358.  
  359.     for ( i = 0 ; i < MFCF_COUNT ; ++i ) {
  360.         sprintf( remotename, "MFCF%u", i );
  361.         mfcf[i] = getenv( remotename );
  362.         if ( *mfcf[i] == 0 ) mfcf[i] = NULL;
  363.     }
  364.  
  365.  
  366.     userid = server = "UNKNOWN";
  367.     if ((seqwhere = getenv( "SEQUENCE" )) == NULL )
  368.         seqwhere = SEQFILE;
  369.     if ( ( title = getenv( "TITLE" )) ==  NULL )
  370.         title = "stdprn";
  371.     if ( ( jobname = getenv( "JOBNAME" )) == NULL )
  372.         jobname = "job_name";
  373.     class = getenv("CLASS");
  374.  
  375.     puts("LPR using Waterloo TCP");
  376.     switch (argc) {
  377.     case 3:
  378.         /* no printername */
  379.         rhostname = argv[1];
  380.         filename = argv[2];
  381.         printer = "lp";
  382.         break;
  383.     case 6:
  384.         /* whole thing */
  385.         userid = argv[4];
  386.         server = argv[5];        /* and continue on below */
  387.     case 4:
  388.         /* Hostname and printer */
  389.         printer = argv[1];
  390.         rhostname = argv[2];
  391.         filename = argv[3];
  392.         break;
  393.     default:
  394.               printf( "Usage: LPR [printer] host filename [userid server]");
  395.           exit(1);
  396.    }
  397.     hostname = gethostname( NULL, 0 );
  398.     if ( !hostname || !*hostname ) {
  399.         puts("You must define your hostname in the WATTCP.CFG file!");
  400.         exit( 3 );
  401.     }
  402.  
  403.     strlwr( hostname );
  404.     strlwr( userid );
  405.     strlwr( server );
  406. /* 94.11.19 -- removed extra parms */
  407.     status = lpr( hostname, printer, rhostname, filename, title, jobname,
  408.         class, userid, server /*, MFCF_COUNT, mfcf*/ );
  409.  
  410.     return( status ? 3 : 0 );
  411. }
  412.