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