home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / NCSATELN / TEL23SRC.ZIP / LPR / LPQ.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-30  |  9.0 KB  |  270 lines

  1. /*  -------------------------------------------------------------------
  2.     lpq - line printer queue
  3.  
  4.     Used to display the queue of printer jobs managed by an LPD daemon
  5.     running on a remote machine.
  6.     Built on top of the NCSA TCP/IP package (version 2.2tn for MS-DOS).
  7.  
  8.     Paul Hilchey   May 1989
  9.  
  10.     Copyright (C) 1989  The University of British Columbia
  11.     All rights reserved.
  12.  
  13.     history:
  14.     8/15/89    allow spaces after -P and -S options
  15.     1/6/89     port to Microsoft C 5.1 by Heeren Pathak (NCSA)
  16.     -------------------------------------------------------------------
  17. */
  18.  
  19. #define LPR
  20.  
  21. #include <stdio.h>
  22. #include <conio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <dos.h>
  26. #include <ctype.h>
  27. #ifdef MSC
  28. #include <signal.h>
  29. #include <time.h>
  30. #endif
  31.  
  32. #define WINMASTER
  33.  
  34. #ifdef MEMORY_DEBUG
  35. #include "memdebug.h"
  36. #endif
  37. #include "netevent.h"
  38. #include "hostform.h"
  39. #include "whatami.h"
  40. #include "windat.h"
  41. #include "lp.h"
  42. #include "externs.h"
  43.  
  44. int debug = 0;            /* enable with -D option */
  45. int ftppassword,        /* not used; just to avoid unresolved external */
  46.     bypass_passwd=0;    /* whether to bypass the password check */
  47.  
  48. unsigned char path_name[_MAX_DRIVE+_MAX_DIR],        /* character storage for the path name */
  49.     temp_str[20],s[_MAX_DIR],temp_data[30];
  50.  
  51. #define DEFAULT_INTERVAL        10      /* 10 second interval for update */
  52. #define REQUEST_BUFF_SIZE       200     /* size of buffer used to build request sent to the server */
  53. #define JOB_AND_USER_BUFF_SIZE  150     /* size of buffer for job numbers and user names specified on the command line */
  54.  
  55. /* Function Prototypes */
  56. void main(int argc, char *argv[]);
  57. static void query_server(char *remote_host, char *request, int interval);
  58. static void clrscr(void );
  59. static void randomize(void );
  60.  
  61. /****************************************************************
  62.  *  Main program.                                               *
  63.  *     lpq  [option ...] [job# ...] [username ...]              *
  64.  ****************************************************************/
  65. void main(int argc, char *argv[])
  66. {
  67.     int i;  /* index for command line arguments */
  68.     static char request[REQUEST_BUFF_SIZE] = "";
  69.  
  70.     /* buffer of the job numbers and user names specified on the command line */
  71.     static char job_and_user_buff[JOB_AND_USER_BUFF_SIZE] = "";
  72.     int job_and_user_buff_len = 0;
  73.  
  74.     char request_code = LPD_DISPLAY_SHORT_QUEUE;  /* short list is default */
  75.     int  interval = 0;         /* delay interval for periodic update */
  76.     char *ptr;
  77.  
  78.     char *remote_name,       /* printer name on remote system */
  79.          *remote_host;       /* address of remote host        */
  80.  
  81.     _splitpath(argv[0],path_name,s,temp_str,temp_data);    /* split the full path name of telbin.exe into it's components */
  82.     strcat(path_name,s);    /* append the real path name to the drive specifier */
  83.  
  84. #ifdef MSC
  85.     signal(SIGINT,breakstop);        /* Microsoft intercept of break */
  86. #else
  87.     ctrlbrk(breakstop);     /* set up ctrl-c handler */
  88. #endif
  89.  
  90.  
  91.     /* Do session initialization.  Snetinit reads config file. */
  92.     ptr = getenv("CONFIG.TEL");
  93.     if (ptr != NULL) Shostfile(ptr);
  94.     if(i=Snetinit()) {
  95.         if(i==-2)        /* check for BOOTP server not responding */
  96.             netshut();    /* release network */
  97.         crash("network initialization failed.");
  98.       }    /* end if */
  99.  
  100.     /* get printer and server from environment variables */
  101.     remote_name = getenv("PRINTER");
  102.     if (remote_name == NULL) remote_name = DEFAULT_PRINTER;
  103.     remote_host = getenv("SERVER");
  104.  
  105.     /*****************************************
  106.        Loop through command line arguments
  107.      *****************************************/
  108.     for (i=1; i<argc; ++i)
  109.  
  110.         if (0 == strncmp(argv[i],"-P",2))          /* select printer */
  111.             if (argv[i][2])
  112.                 remote_name = &argv[i][2];
  113.             else if (i+1 < argc)
  114.                 remote_name = argv[++i];
  115.             else ;
  116.  
  117.         else if (0 == strncmp(argv[i],"-S",2))      /* select server */
  118.             if (argv[i][2])
  119.                 remote_host = &argv[i][2];
  120.             else if (i+1 < argc)
  121.                 remote_host = argv[++i];
  122.             else ;
  123.  
  124.         else if (0 == strncmp(argv[i],"-l",2))      /* long form */
  125.             request_code = LPD_DISPLAY_LONG_QUEUE;
  126.  
  127.         else if (0 == strncmp(argv[i],"-D",2))      /* debug mode */
  128.             debug = 1;
  129.  
  130.         else if (argv[i][0] == '+')  {              /* repeat interval */
  131.             if (0 >= sscanf(argv[i]+1,"%d",&interval))
  132.                 interval=DEFAULT_INTERVAL;
  133.             else
  134.                 interval = max(interval,0);
  135.         }
  136.  
  137.         else {                                          /* job number or user name */
  138.             job_and_user_buff_len += strlen(argv[i])+1; /* include space to separate items */
  139.             if (job_and_user_buff_len >= JOB_AND_USER_BUFF_SIZE)
  140.                 crash("too many command line arguments.");
  141.             strcat(job_and_user_buff," ");
  142.             strcat(job_and_user_buff,argv[i]);     /* append to buffer */
  143.         };
  144.  
  145.     if (remote_host == NULL) crash("server not specified.");
  146.  
  147.     /**************************************
  148.        build request to send to the daemon
  149.      **************************************/
  150.     if ((strlen(remote_name)+job_and_user_buff_len+2) >= REQUEST_BUFF_SIZE)
  151.         crash("your command line arguments are too long.");
  152.     sprintf(request,"%c%s%s\n",request_code,remote_name,job_and_user_buff);
  153.     if (debug) printf(request);
  154.  
  155.     /* do it */
  156.     query_server(remote_host,request,interval);
  157.  
  158.     netshut();   /* shut down all network stuff */
  159. }
  160.  
  161. /****************************************************************
  162.  * query_server                                                 *
  163.  * Open the connection, send the request, and display the       *
  164.  * text the server sends us.  If interval is non-zero, the      *
  165.  * request is repeated with a screen clear between updates.     *
  166.  * Periodic updating is terminated by a keypress or a           *
  167.  * "No entries" message from the server.                        *
  168.  *                                                              *
  169.  * parameters: null terminated name or ip address of the server *
  170.  *             null terminated string to send to the server to  *
  171.  *                request the queue list                        *
  172.  *             the delay interval in seconds between updates    *
  173.  ****************************************************************/
  174. static void query_server(char *remote_host, char *request, int interval)
  175. {
  176.     int clear_screen_pending;
  177.     int source_port;
  178.     int server_connection_id;
  179.     struct machinfo *server_info_record;
  180.     char buff[132];         /* text returned from the server */
  181.     int len;                /* length of text read from server */
  182.     int i;                  /* loop counter */
  183.  
  184.     /* pick a source port at random from the set of privileged ports */
  185.     randomize();
  186.     source_port = rand() % MAX_PRIV_PORT;
  187.  
  188.     /* do name lookup for server */
  189.     server_info_record = lookup(remote_host);
  190.     if (server_info_record == 0)
  191.         crash("domain lookup failed for %s.",remote_host);
  192.  
  193.     do {
  194.         /* increment source port number for each connection we open */
  195.         source_port = (source_port + 1) % MAX_PRIV_PORT;
  196.  
  197.         /* open the connection */
  198.         server_connection_id = open_connection(server_info_record, source_port, PRINTER_PORT);
  199.         if (server_connection_id < 0) crash ("unable to open connection.");
  200.  
  201.         /* send the request */
  202.         netwrite(server_connection_id, request, strlen(request));
  203.  
  204.         /* we wait until we get something from the server before clearing the screen */
  205.         clear_screen_pending =((interval > 0) && (!debug));
  206.  
  207.         while(1) {
  208.             len = nread(server_connection_id, buff, 132);
  209.             if (len <=0 ) break;
  210.             if (clear_screen_pending) {
  211.                 clrscr();
  212.                 clear_screen_pending = 0;
  213.             }
  214.             printf("%.*s",len,buff);
  215.             if (strnicmp(buff,"No entries",10) == 0) interval = 0;
  216.         }
  217.  
  218.         netclose(server_connection_id);
  219.  
  220.         /* delay before repeating query */
  221.         for (i=1; i<=interval; i++) {
  222.             netsleep(1);   /* one second */
  223.             checkerr();    /* check for any error events */
  224.             if (kbhit()) interval = 0;
  225.         }
  226.     } while (interval != 0);
  227. }
  228.  
  229. #ifdef MSC
  230.  
  231. /******************************************************************
  232. *
  233. * randomize()
  234. *
  235. * replicates the randomize function of Turbo C
  236. * MSC 5.1 does not contain it so we have to write it ourselves.
  237. *
  238. */
  239.  
  240. static void randomize(void )
  241. {
  242.     srand((unsigned)time(NULL));
  243. }
  244.  
  245. /*******************************************************************
  246. *
  247. * clrscr()
  248. *
  249. * just clear the screen.  Use interrupt 0x10(hex) subfunction 6
  250. *
  251. */
  252.  
  253. static void clrscr(void )
  254. {
  255.     union REGS regs;
  256.  
  257.     regs.h.al=0;
  258.     regs.h.ah=6;
  259.     regs.h.cl=0;
  260.     regs.h.ch=0;
  261.     regs.h.dl=79;
  262.     regs.h.dh=24;
  263.     regs.h.bh=0;
  264.     int86(0x10,®s,®s);
  265. }
  266.  
  267. #endif
  268.  
  269.  
  270.