home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / wattcp / apps / lpr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  11.6 KB  |  413 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. #define MFCF_COUNT 5
  101. char *mfcf[MFCF_COUNT];
  102.  
  103.  
  104. #define LPQ_PORT 515
  105. #define LOCAL_PORT 722
  106.  
  107. #define SEQFILE "SEQUENCE.LPR"
  108. char *seqwhere;
  109.  
  110. word getsequence()
  111. {
  112.     static word oldseq = 0;
  113.     FILE *f;
  114.  
  115.     if ( ! oldseq ) {
  116.         /* must look to disk for an old sequence number */
  117.         if ( f = fopen( seqwhere, "rt" )) {
  118.             fscanf( f, "%u", &oldseq );
  119.             fclose( f );
  120.         }
  121.     }
  122.  
  123.     oldseq = ( oldseq + 1 ) % 1000;
  124.  
  125.     if ( f = fopen( seqwhere, "wt" )) {
  126.         fprintf( f, "%u", oldseq );
  127.         fclose( f );
  128.     }
  129.     return( oldseq );
  130. }
  131.  
  132. lpr( localhostname, printer, rhostname, filename, title, jobname,
  133.     class, username, servername )
  134. char *localhostname;
  135. char *printer;
  136. char *rhostname;
  137. char *filename;
  138. char *title;
  139. char *jobname;
  140. char *class;
  141. char *username;
  142. char *servername;
  143. {
  144.     tcp_Socket socketdata, *s;
  145.     longword filesize;
  146.     longword host;
  147.     int status = 0;
  148.     int connected = 0;
  149.     int completed = 0;
  150.     int localport;
  151.     word seqnum, i;
  152.  
  153.     char buffer[ 1024 ], *b2;
  154.     char remotename[ 80 ];
  155.     char cmdfile[ 1024 ];
  156.     longword remaining, found, bufsize;
  157.     FILE *f;
  158.  
  159.     if (( *class == 0 ) || (*class == ' ')) class = NULL;
  160.  
  161.     for ( i = 0; i < MFCF_COUNT ; ++i ) {
  162.         if ( mfcf[ i ] )
  163.             printf("Extra option: %s\n", mfcf[i] );
  164.     }
  165.  
  166.     if (!(f = fopen( filename, "rb"))) {
  167.     printf("Unable to open file '%s'\n", filename );
  168.     return( 3 );
  169.     }
  170.  
  171.     sock_init();
  172.     s = &socketdata;
  173.     if (!(host = resolve( rhostname ))) {
  174.         printf( "lpq: unknown host %s\n",rhostname);
  175.         return(1);
  176.     }
  177.  
  178.     localport = 255 + (MsecClock() & 255);
  179.     if ( !tcp_open( s, localport, host, LPQ_PORT, NULL)) {
  180.       printf("Unable to open socket.");
  181.       tcp_shutdown();
  182.       return(1);
  183.    }
  184.  
  185.    printf( "connecting...\r");
  186.    sock_wait_established( s, sock_delay, NULL, &status );
  187.    connected = 1;
  188.    printf("connection established\n");
  189.  
  190.    /* is there an opening message - non-standard but possible */
  191.  
  192.    if (sock_dataready( s )) {
  193.        sock_fastread( s, buffer, sizeof( buffer ));
  194.        sock_tick( s, &status );    /* in case above message closed port */
  195.    }
  196.  
  197.    /* use ipnumber/time  */
  198.    seqnum = getsequence();
  199.    sprintf(remotename,"fA%03u%s", seqnum, localhostname );
  200.    
  201.    /* we are connected */
  202.    sprintf( buffer, "\2%s\n", printer );    /* select a printer */
  203.    sock_puts( s, buffer );
  204.  
  205.    /* state #2 */
  206.  
  207.    sock_wait_input( s, sock_delay, NULL, &status );
  208.  
  209.    sock_fastread( s, buffer, sizeof( buffer ));
  210.  
  211.    switch (*buffer) {
  212.        case 0 : break;
  213.        case 1 : printf("Printer '%s' is not available\n", printer);
  214.         goto close_it;
  215.        default: rip( buffer );
  216.                 printf("ERROR: %s\n", buffer);
  217.         goto close_it;
  218.    }
  219.  
  220.    /* printer is accepted, printing file */
  221.    filesize = filelength( fileno( f ));
  222.  
  223.  
  224.    sprintf( buffer, "\3%ld d%s\n", filesize, remotename );
  225.    sock_puts( s , buffer );
  226.    sock_wait_input( s, sock_delay, NULL, &status );
  227.  
  228.    /* state 3, reply from filename */
  229.    sock_fastread( s, buffer, sizeof( buffer ));
  230.  
  231.    switch (*buffer) {
  232.     case 0: break;
  233.         case 1: printf("remote host complains of bad connection");
  234.         goto close_it;
  235.     case 2: puts("remote host out of storage space");
  236.         goto close_it;
  237.     }
  238.  
  239.     /* dump file */
  240.  
  241.     remaining = filesize;
  242.     bufsize = (longword) sizeof( buffer );
  243.     do {
  244.     if (remaining < bufsize ) bufsize = remaining;
  245.     if (!(found = fread( buffer, 1, sizeof(buffer), f)))
  246.         break;
  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 -= 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("Options:\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. main(argc, argv)
  348. int argc;
  349. char **argv;
  350. {
  351.     char remotename[128];
  352.     char *hostname;
  353.     char *filename;
  354.     char *printer;
  355.     char *userid, *server;
  356.     char *title, *jobname;
  357.     char *rhostname;
  358.     char *class;
  359.     int status, i;
  360.  
  361.     for ( i = 0 ; i < MFCF_COUNT ; ++i ) {
  362.         sprintf( remotename, "MFCF%u", i );
  363.         mfcf[i] = getenv( remotename );
  364.         if ( *mfcf[i] == 0 ) mfcf[i] = NULL;
  365.     }
  366.  
  367.  
  368.     userid = server = "UNKNOWN";
  369.     if ((seqwhere = getenv( "SEQUENCE" )) == NULL )
  370.         seqwhere = SEQFILE;
  371.     if ( ( title = getenv( "TITLE" )) ==  NULL )
  372.         title = "stdprn";
  373.     if ( ( jobname = getenv( "JOBNAME" )) == NULL )
  374.         jobname = "job_name";
  375.     class = getenv("CLASS");
  376.  
  377.     puts("LPR using Waterloo TCP");
  378.     switch (argc) {
  379.     case 3:
  380.         /* no printername */
  381.         rhostname = argv[1];
  382.         filename = argv[2];
  383.         printer = "lp";
  384.         break;
  385.     case 6:
  386.         /* whole thing */
  387.         userid = argv[4];
  388.         server = argv[5];        /* and continue on below */
  389.     case 4:
  390.         /* Hostname and printer */
  391.         printer = argv[1];
  392.         rhostname = argv[2];
  393.         filename = argv[3];
  394.         break;
  395.     default:
  396.               printf( "Usage: LPR [printer] host filename [userid server]");
  397.           exit(1);
  398.    }
  399.     hostname = gethostname( NULL, 0 );
  400.     if ( !hostname || !*hostname ) {
  401.         puts("You must define your hostname in the WATTCP.CFG file!");
  402.         exit( 3 );
  403.     }
  404.  
  405.     strlwr( hostname );
  406.     strlwr( userid );
  407.     strlwr( server );
  408.     status = lpr( hostname, printer, rhostname, filename, title, jobname,
  409.         class, userid, server, MFCF_COUNT, mfcf );
  410.  
  411.     return( status ? 3 : 0 );
  412. }
  413.