1*12876Sralph /* lpq.c 4.4 83/06/02 */ 212108Sralph /* 312108Sralph * Spool Queue examination program 412108Sralph * 512108Sralph * lpq [+[n]] [-Pprinter] [user...] [job...] 612108Sralph * 712108Sralph * + means continually scan queue until empty 812108Sralph * -P used to identify printer as per lpr/lprm 912108Sralph */ 1012108Sralph 1112108Sralph #include "lp.h" 1212108Sralph 1312108Sralph char *user[MAXUSERS]; /* users to process */ 1412108Sralph int users; /* # of users in user array */ 1512108Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 1612108Sralph int requests; /* # of spool requests */ 1712108Sralph 18*12876Sralph static int repeat; /* + flag indicator */ 19*12876Sralph static int slptime = 30; /* pause between screen refereshes */ 20*12876Sralph static int lflag; /* long output option */ 2112108Sralph 2212108Sralph /* 2312108Sralph * Termcap stuff for fancy display 2412108Sralph */ 2512108Sralph #ifdef TERMCAP 2612108Sralph struct sgttyb sbuf; 27*12876Sralph static unsigned ospeed; 28*12876Sralph static int dumb; /* whether to use capabilities */ 29*12876Sralph static char PC; /* pad character for output */ 30*12876Sralph static char *UP; /* up one line */ 31*12876Sralph static char *BC; /* backspace character, other than \b */ 32*12876Sralph static char *CM; /* cursor motion */ 33*12876Sralph static char *CL; /* clear display */ 34*12876Sralph static char *TI; /* terminal init for CM */ 35*12876Sralph static char *TE; /* terminal clear for CM */ 36*12876Sralph static char *SO; /* stand out start */ 37*12876Sralph static char *SE; /* stand out end */ 3812108Sralph 3912108Sralph char *tgetstr(); 4012108Sralph int putch(); /* for tputs' */ 4112108Sralph #endif 4212108Sralph 4312108Sralph main(argc, argv) 4412108Sralph char *argv[]; 4512108Sralph { 4612108Sralph register char *arg; 4712108Sralph register int n; 4812108Sralph 4912431Sralph name = argv[0]; 5012431Sralph gethostname(host, sizeof(host)); 5112431Sralph 5212108Sralph while (--argc) { 5312108Sralph if ((arg = *++argv)[0] == '+') { 5412108Sralph if (arg[1] != '\0') 5512108Sralph if ((n = atoi(&arg[1])) > 0) 5612108Sralph slptime = n; 5712108Sralph repeat++; 5812108Sralph } else if (arg[0] == '-') 5912108Sralph switch (arg[1]) { 6012108Sralph case 'P': /* printer name */ 6112730Sralph if (arg[2]) 6212730Sralph printer = &arg[2]; 6312730Sralph else if (argc > 1) { 6412730Sralph argc--; 6512730Sralph printer = *++argv; 6612730Sralph } 6712108Sralph break; 6812108Sralph 6912108Sralph case 'l': /* long output */ 7012108Sralph lflag++; 7112108Sralph break; 7212108Sralph 7312108Sralph default: 7412108Sralph usage(); 7512108Sralph } else { 7612108Sralph if (isdigit(arg[0])) { 7712108Sralph if (requests >= MAXREQUESTS) 7812108Sralph fatal("too many requests"); 7912108Sralph requ[requests++] = atoi(arg); 8012108Sralph } else { 8112108Sralph if (users >= MAXUSERS) 8212108Sralph fatal("too many users"); 8312108Sralph user[users++] = arg; 8412108Sralph } 8512108Sralph } 8612108Sralph } 8712108Sralph if (printer == NULL && (printer = getenv("PRINTER")) == NULL) 8812108Sralph printer = DEFLP; 8912108Sralph #ifdef TERMCAP 9012108Sralph dumb = termcap(); 9112108Sralph #endif 9212108Sralph 9312108Sralph if (repeat) { 9412108Sralph #ifdef TERMCAP 9512108Sralph if (TI) 9612108Sralph tputs(TI, 0, putch); 9712108Sralph #endif 9812108Sralph do { 9912108Sralph #ifdef TERMCAP 10012108Sralph if (!dumb) { 10112108Sralph tputs(CL, 0, putch); 10212108Sralph tputs(tgoto(CM, 0, 0), 0, putch); 10312108Sralph } 10412108Sralph #endif 10512108Sralph if ((n = displayq(lflag)) > 0) 10612108Sralph sleep(slptime); 10712108Sralph } while (n > 0); 10812108Sralph #ifdef TERMCAP 10912108Sralph if (!dumb) { 11012108Sralph standout(stdout, "Hit return to continue"); 11112108Sralph while (getchar() != '\n'); 11212108Sralph if (TE) 11312108Sralph tputs(TE, 0, putch); 11412108Sralph } 11512108Sralph #endif 11612108Sralph } else 11712108Sralph displayq(lflag); 11812108Sralph } 11912108Sralph 120*12876Sralph static 12112108Sralph usage() 12212108Sralph { 12312108Sralph printf("usage: lpq [-Pprinter] [-l] [+[n]] [user...] [job...]\n"); 12412108Sralph exit(1); 12512108Sralph } 12612108Sralph 12712108Sralph /* 12812108Sralph * If we have the capability, print this in standout mode 12912108Sralph */ 130*12876Sralph static 13112108Sralph standout(f, s, a1, a2) 13212108Sralph FILE *f; 13312108Sralph char *s; 13412108Sralph { 13512108Sralph #ifdef TERMCAP 13612108Sralph if (SO) 13712108Sralph tputs(SO, 0, putch); 13812108Sralph fprintf(f, s, a1, a2); 13912108Sralph if (SO && SE) 14012108Sralph tputs(SE, 0, putch); 14112108Sralph #else 14212108Sralph fprintf(f, s, a1, a2); 14312108Sralph #endif 14412108Sralph } 14512108Sralph 14612108Sralph #ifdef TERMCAP 147*12876Sralph static char * 14812108Sralph capstrings[] = { 14912108Sralph "bc", "cl", "cm", "so", "se", "ti", "te", "up", 15012108Sralph 0 15112108Sralph }; 15212108Sralph 153*12876Sralph static char ** 15412108Sralph caps[] = { 15512108Sralph &BC, &CL, &CM, &SO, &SE, &TI, &TE, &UP, 15612108Sralph }; 15712108Sralph 15812108Sralph /* 15912108Sralph * All we need from termcap is to clear screen and 16012108Sralph * position cursor at the top; if these aren't available 16112108Sralph * we say the terminal is dumb and let things scroll 16212108Sralph */ 163*12876Sralph static 16412108Sralph termcap() 16512108Sralph { 16612108Sralph char *term, tbuf[BUFSIZ]; 16712108Sralph static char buf[BUFSIZ/2]; 16812108Sralph register short columns; 16912108Sralph char *bp = buf; 17012108Sralph register char **p, ***q, *cp; 17112108Sralph 17212108Sralph ioctl(0, TIOCGETP, (char *)&sbuf); 17312108Sralph ospeed = sbuf.sg_ospeed; 17412108Sralph if ((term = getenv("TERM")) != NULL && tgetent(tbuf, term) > 0) { 17512108Sralph for (p = capstrings, q = caps; *p != NULL; p++, q++) 17612108Sralph **q = tgetstr(*p, &bp); 17712108Sralph if ((cp = tgetstr("pc", &bp)) != NULL) 17812108Sralph PC = *cp; 17912108Sralph } 18012108Sralph return(CL == NULL || CM == NULL); 18112108Sralph } 18212108Sralph 18312108Sralph /* 18412108Sralph * Putchar writearound for tputs 18512108Sralph */ 186*12876Sralph static 18712108Sralph putch(c) 18812108Sralph char c; 18912108Sralph { 19012108Sralph putchar(c); 19112108Sralph } 19212108Sralph #endif 193