home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / tooltool2.1c / part01 / samples / queue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-06  |  2.3 KB  |  120 lines

  1. #include    <stdio.h>
  2. #include    <signal.h>
  3. #include    <sgtty.h>
  4. #include    <sys/time.h>
  5. #include    <pwd.h>
  6.  
  7. #define        strsave(s)    ((char *) strcpy((char *) malloc(strlen(s) + 1), s))
  8.  
  9. #define        CLEAR        "\014"
  10. #define        BOLD_ON        "\033[7m"
  11. #define        BOLD_OFF    "\033[m"
  12.  
  13. #define        LPQ        "/usr/ucb/lpq"
  14.  
  15. struct    itimerval    interval = {{10, 0}, {10, 0}};
  16. struct    itimerval    zero_timer = {{0, 0}, {0, 0}};
  17.  
  18. char    *user_name;
  19.  
  20. char    *strindex(source, target)
  21.  
  22. char    *source;
  23. char    *target;
  24.  
  25. {    register    int    len;
  26.  
  27.     len = strlen(target);
  28.     for (; *source; source++)
  29.        if (strncmp(source, target, len) == 0)
  30.           return(source);
  31.     return(0);
  32. }
  33.  
  34. poll()
  35.  
  36. {    FILE    *f;
  37.     char    buf[256];
  38.     int    line;
  39.  
  40.     if ((f = popen(LPQ, "r")) == NULL) {
  41.        printf("%s%sCould not execute %s%s\n", CLEAR, BOLD_ON, LPQ, BOLD_OFF);
  42.        return;
  43.        }
  44.     printf(CLEAR);
  45.     for (line = 1; fgets(buf, 256, f) != NULL; line++)
  46.        if (line <= 2 || strindex(buf, user_name))
  47.           printf("%s%s%s", BOLD_ON, buf, BOLD_OFF);
  48.        else
  49.           printf(buf);
  50.     pclose(f);
  51. }
  52.  
  53. start_timer()
  54.  
  55. {
  56.     setitimer(ITIMER_REAL, &interval, NULL);
  57. }
  58.  
  59. stop_timer()
  60.  
  61. {
  62.     setitimer(ITIMER_REAL, &zero_timer, NULL);
  63. }
  64.  
  65. main()
  66.  
  67. {    char    c;
  68.     int    i;
  69.     struct    sgttyb    tty, old_in, old_out;
  70.     struct    passwd    *pp;
  71.  
  72.     pp = getpwuid(getuid());
  73.     user_name = strsave(pp->pw_name);
  74.  
  75.     ioctl(0, TIOCGETP, &old_in);
  76.     tty = old_in;
  77.     tty.sg_flags |= CBREAK;
  78.     tty.sg_flags &= ~ECHO;
  79.     ioctl(0, TIOCSETP, &tty);
  80.  
  81.     ioctl(1, TIOCGETP, &old_out);
  82.     tty = old_out;
  83.     tty.sg_flags |= CBREAK;
  84.     tty.sg_flags &= ~ECHO;
  85.     ioctl(1, TIOCSETP, &tty);
  86.  
  87.     signal(SIGALRM, poll);
  88.     poll();
  89.     start_timer();
  90.     while ((c = getchar()) != EOF)
  91.        switch (c) {
  92.           case 'b' : start_timer();
  93.                    while (getchar() != '\n')
  94.                       ;
  95.                    break;
  96.           case 'e' : stop_timer();
  97.                    while (getchar() != '\n')
  98.                       ;
  99.                    break;
  100.           case 'i' : for (i = 0; (c = getchar()) != '\n'; )
  101.                       if (c >= '0' && c <= '9')
  102.                          i = i * 10 + c - '0';
  103.                    if (i == 0)
  104.                       break;
  105.                    interval.it_value.tv_sec = i;
  106.                    interval.it_value.tv_usec = 0;
  107.                    interval.it_interval.tv_sec = i;
  108.                    interval.it_interval.tv_usec = 0;
  109.                    start_timer();
  110.                    break;
  111.           case 'q' : stop_timer();
  112.              ioctl(0, TIOCSETP, &old_in);
  113.              ioctl(1, TIOCSETP, &old_out);
  114.                    exit(0);
  115.           }
  116.     ioctl(0, TIOCSETP, &old_in);
  117.     ioctl(1, TIOCSETP, &old_out);
  118.     exit(0);
  119. }
  120.