386BSD 0.1 development
[unix-history] / usr / src / sbin / dmesg / dmesg.c
CommitLineData
c17a1afc
WJ
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
35char 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
41static 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
53struct nlist nl[] = {
54#define X_MSGBUFP 0
55 { "_msgbufp" },
56 { NULL },
57};
58
59void usage(), vputc();
60void err __P((const char *, ...));
61
62main(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 int msgbufat;
70 char *core, *namelist;
71
72 core = namelist = NULL;
73 while ((ch = getopt(argc, argv, "M:N:")) != EOF)
74 switch(ch) {
75 case 'M':
76 core = optarg;
77 break;
78 case 'N':
79 namelist = optarg;
80 break;
81 case '?':
82 default:
83 usage();
84 }
85 argc -= optind;
86 argv += optind;
87
88 /* Read in kernel message buffer, do sanity checks. */
89 if (kvm_openfiles(namelist, core, NULL) == -1)
90 err("kvm_openfiles: %s", kvm_geterr());
91 if (kvm_nlist(nl) == -1)
92 err("kvm_nlist: %s", kvm_geterr());
93 if (nl[X_MSGBUFP].n_type == 0)
94 err("msgbufp not found namelist");
95
96 kvm_read((void *)nl[X_MSGBUFP].n_value, (void *)&msgbufat, sizeof(msgbufat));
97 kvm_read((void *)msgbufat, (void *)&cur, sizeof(cur));
98 if (cur.msg_magic != MSG_MAGIC)
99 err("magic number incorrect");
100 if (cur.msg_bufx >= MSG_BSIZE)
101 cur.msg_bufx = 0;
102
103 /*
104 * The message buffer is circular; start at the read pointer, and
105 * go to the write pointer - 1.
106 */
107 p = cur.msg_bufc + cur.msg_bufx;
108 ep = cur.msg_bufc + cur.msg_bufx - 1;
109 for (newl = skip = 0; p != ep; ++p) {
110 if (p == cur.msg_bufc + MSG_BSIZE)
111 p = cur.msg_bufc;
112 ch = *p;
113 /* Skip "\n<.*>" syslog sequences. */
114 if (skip) {
115 if (ch == '>')
116 newl = skip = 0;
117 continue;
118 }
119 if (newl && ch == '<') {
120 skip = 1;
121 continue;
122 }
123 if (ch == '\0')
124 continue;
125 newl = (ch = *p) == '\n';
126 vputc(ch);
127 }
128 if (!newl)
129 (void)putchar('\n');
130 exit(0);
131}
132
133void
134vputc(ch)
135 register int ch;
136{
137 int meta;
138
139 if (!isascii(ch)) {
140 (void)putchar('M');
141 (void)putchar('-');
142 ch = toascii(ch);
143 meta = 1;
144 } else
145 meta = 0;
146 if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
147 (void)putchar(ch);
148 else {
149 (void)putchar('^');
150 (void)putchar(ch == '\177' ? '?' : ch | 0100);
151 }
152}
153
154#if __STDC__
155#include <stdarg.h>
156#else
157#include <varargs.h>
158#endif
159
160void
161#if __STDC__
162err(const char *fmt, ...)
163#else
164err(fmt, va_alist)
165 char *fmt;
166 va_dcl
167#endif
168{
169 va_list ap;
170#if __STDC__
171 va_start(ap, fmt);
172#else
173 va_start(ap);
174#endif
175 (void)fprintf(stderr, "dmesg: ");
176 (void)vfprintf(stderr, fmt, ap);
177 va_end(ap);
178 (void)fprintf(stderr, "\n");
179 exit(1);
180 /* NOTREACHED */
181}
182
183void
184usage()
185{
186 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
187 exit(1);
188}