filter lpc(1) output intelligently

Ed Carp erc at Apple.COM
Wed Jun 26 09:27:21 AEST 1991


/*
*
* lpcfilt - filter lpc(1) output and display on screen using curses
*
* To compile:
*         cc -s -o lpcfilt lpcfilt.c -lcurses [-lterm{cap|info}] -O
*
* Written 06/25/91 by Ed Carp
*
* Copyright 1991, by Ed Carp
*
* You can do anything you like with this code, except sell it or pretend
* you wrote it.
*
*/

#include <stdio.h>
#include <string.h>
#include <curses.h>
main(argc, argv)
   int             argc;
   char          **argv;
{
   FILE           *in;
   char            linein[512];
   char            printer[50];
   char            q[10];
   char            pr[10];
   int             entries;
   int             score;
   int             all;
   int             prcnt;
   int             statlen;
   char            status[512];
   char           *ptr;
   char           *ptr2;
   char            c;

   if (argc > 1)
   {
      if (strcmp(argv[1], "-a") == 0)
	 all = 1;
      else
	 all = 0;
   } else
      all = 0;
   puts("Please wait...");
   initscr();
   statlen = COLS - 46;		/* length of status line */
   while (1)
   {
      if ((in = popen("lpc stat", "r")) == (FILE *) NULL)
      {
	 endwin();
	 perror("lpc");
	 exit(1);
      }
      erase();
      prcnt = 0;
      printw("%-20s %-8s %-8s %-5s    %s\n\n",
	     "Printer", "Queuing", "Printing", "Entry", "Status");
      while (fgets(linein, 510, in) != (char *) NULL)
      {
	 if (prcnt > LINES)
	    break;
	 linein[strlen(linein) - 1] = NULL;	/* get rid of \n */
	 score = 0;
	 score += all;
	 if (strlen(linein) < 2)
	    continue;
	 linein[strlen(linein) - 1] = NULL;	/* get rid of ":" */
   prx:
	 strcpy(printer, linein);	/* get printer name */
	 printer[20] = NULL;	/* shorten name */
	 fgets(linein, 510, in);
	 linein[strlen(linein) - 1] = NULL;	/* get rid of \n */
	 strcpy(q, linein + 12);/* is queuing enabled? */
	 if (strcmp(q, "disabled") == 0)
	    score++;
	 fgets(linein, 510, in);
	 linein[strlen(linein) - 1] = NULL;	/* get rid of \n */
	 strcpy(pr, linein + 13);	/* is printing enabled? */
	 if (strcmp(pr, "disabled") == 0)
	    score++;
	 fgets(linein, 510, in);
	 linein[strlen(linein) - 1] = NULL;	/* get rid of \n */
	 if (*(linein + 1) == 'n' || atoi(linein + 1) > 0)
	 {
	    entries = atoi(linein + 1);	/* how many entries? */
	    if (entries < 0)
	       entries = 0;
	    score += entries;
	    fgets(linein, 510, in);
	    linein[strlen(linein) - 1] = NULL;	/* get rid of \n */
	 } else
	    entries = 0;
	 strcpy(status, linein + 1);	/* what's the status? */
	 if (strcmp(status, "no daemon present") != 0)
	    score++;
	 if (score == 0)
	    continue;
	 printw("%-20s %-8s %-8s %-5d ", printer, q, pr, entries);
	 prcnt++;
	 if (strlen(status) < statlen + 1)
	    printw("%s\n", status);
	 else
	 {
	    ptr = status;
	    ptr2 = ptr + statlen;
	    while ((*ptr2 != ' ') && (ptr2 != ptr))
	       ptr2--;
	    if (ptr2 == ptr)
	       ptr2 = ptr + statlen;
	    c = *ptr2;
	    *ptr2 = NULL;
	    printw("%s\n", ptr);
	    *ptr2 = c;
	    ptr = ptr2 + 1;
	    while (strlen(ptr) > statlen)
	    {
	       ptr2 = ptr + statlen;
	       while ((*ptr2 != ' ') && (ptr2 != ptr))
		  ptr2--;
	       if (ptr2 == ptr)
		  ptr2 = ptr + statlen;
	       c = *ptr2;
	       *ptr2 = NULL;
	       printw("                                             %s\n",
		      ptr);
	       *ptr2 = c;
	       ptr = ptr2 + 1;
	       prcnt++;
	    }
	    printw("                                             %s\n",
		   ptr);
	    prcnt++;
	 }
      }
      pclose(in);
      clrtobot();
      refresh();
      sleep(5);
   }
}
-- 
Ed Carp  N7EKG/6	erc at khijol.UUCP		...uunet!khijol!erc
Packet:  N7EKG @ N6IIU.#NOCAL.CA.US
UUWEST Consulting	Alameda, CA		415/814-0550

Computers HAVE caused a revolution in how much information we
can safely ignore!    --robs at ux1.cso.uiuc.edu (Rob Schaeffer)

-- Absolutely unabashed Gates McFadden groupie! --



More information about the Alt.sources mailing list