home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sbin / dmesg / dmesg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-02  |  4.5 KB  |  187 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)dmesg.c    5.9 (Berkeley) 5/2/91";
  42. #endif /* not lint */
  43.  
  44. #include <sys/cdefs.h>
  45. #include <sys/msgbuf.h>
  46. #include <time.h>
  47. #include <nlist.h>
  48. #include <kvm.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <ctype.h>
  52.  
  53. struct nlist nl[] = {
  54. #define    X_MSGBUF    0
  55.     { "_msgbuf" },
  56.     { NULL },
  57. };
  58.  
  59. void usage(), vputc();
  60. void err __P((const char *, ...));
  61.  
  62. main(argc, argv)
  63.     int argc;
  64.     char **argv;
  65. {
  66.     register int ch, newl, skip;
  67.     register char *p, *ep;
  68.     struct msgbuf cur;
  69.     char *core, *namelist;
  70.  
  71.     core = namelist = NULL;
  72.     while ((ch = getopt(argc, argv, "M:N:")) != EOF)
  73.         switch(ch) {
  74.         case 'M':
  75.             core = optarg;
  76.             break;
  77.         case 'N':
  78.             namelist = optarg;
  79.             break;
  80.         case '?':
  81.         default:
  82.             usage();
  83.         }
  84.     argc -= optind;
  85.     argv += optind;
  86.  
  87.     /* Read in kernel message buffer, do sanity checks. */
  88.     if (kvm_openfiles(namelist, core, NULL) == -1)
  89.         err("kvm_openfiles: %s", kvm_geterr());
  90.     if (kvm_nlist(nl) == -1)
  91.         err("kvm_nlist: %s", kvm_geterr());
  92.     if (nl[X_MSGBUF].n_type == 0)
  93.         err("msgbuf not found namelist");
  94.  
  95.         kvm_read((void *)nl[X_MSGBUF].n_value, (void *)&cur, sizeof(cur));
  96.     if (cur.msg_magic != MSG_MAGIC)
  97.         err("magic number incorrect");
  98.     if (cur.msg_bufx >= MSG_BSIZE)
  99.         cur.msg_bufx = 0;
  100.  
  101.     /*
  102.      * The message buffer is circular; start at the read pointer, and
  103.      * go to the write pointer - 1.
  104.      */
  105.     p = cur.msg_bufc + cur.msg_bufx;
  106.     ep = cur.msg_bufc + cur.msg_bufx - 1;
  107.     for (newl = skip = 0; p != ep; ++p) {
  108.         if (p == cur.msg_bufc + MSG_BSIZE)
  109.             p = cur.msg_bufc;
  110.         ch = *p;
  111.         /* Skip "\n<.*>" syslog sequences. */
  112.         if (skip) {
  113.             if (ch == '>')
  114.                 newl = skip = 0;
  115.             continue;
  116.         }
  117.         if (newl && ch == '<') {
  118.             skip = 1;
  119.             continue;
  120.         }
  121.         if (ch == '\0')
  122.             continue;
  123.         newl = (ch = *p) == '\n';
  124.         vputc(ch);
  125.     }
  126.     if (!newl)
  127.         (void)putchar('\n');
  128.     exit(0);
  129. }
  130.  
  131. void
  132. vputc(ch)
  133.     register int ch;
  134. {
  135.     int meta;
  136.  
  137.     if (!isascii(ch)) {
  138.         (void)putchar('M');
  139.         (void)putchar('-');
  140.         ch = toascii(ch);
  141.         meta = 1;
  142.     } else
  143.         meta = 0;
  144.     if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
  145.         (void)putchar(ch);
  146.     else {
  147.         (void)putchar('^');
  148.         (void)putchar(ch == '\177' ? '?' : ch | 0100);
  149.     }
  150. }
  151.  
  152. #if __STDC__
  153. #include <stdarg.h>
  154. #else
  155. #include <varargs.h>
  156. #endif
  157.  
  158. void
  159. #if __STDC__
  160. err(const char *fmt, ...)
  161. #else
  162. err(fmt, va_alist)
  163.     char *fmt;
  164.         va_dcl
  165. #endif
  166. {
  167.     va_list ap;
  168. #if __STDC__
  169.     va_start(ap, fmt);
  170. #else
  171.     va_start(ap);
  172. #endif
  173.     (void)fprintf(stderr, "dmesg: ");
  174.     (void)vfprintf(stderr, fmt, ap);
  175.     va_end(ap);
  176.     (void)fprintf(stderr, "\n");
  177.     exit(1);
  178.     /* NOTREACHED */
  179. }
  180.  
  181. void
  182. usage()
  183. {
  184.     (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
  185.     exit(1);
  186. }
  187.