xref: /original-bsd/sbin/dmesg/dmesg.c (revision 1cfaf997)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)dmesg.c	5.9 (Berkeley) 05/02/91";
16 #endif /* not lint */
17 
18 #include <sys/cdefs.h>
19 #include <sys/msgbuf.h>
20 #include <time.h>
21 #include <nlist.h>
22 #include <kvm.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 
27 struct nlist nl[] = {
28 #define	X_MSGBUF	0
29 	{ "_msgbuf" },
30 	{ NULL },
31 };
32 
33 void usage(), vputc();
34 void err __P((const char *, ...));
35 
36 main(argc, argv)
37 	int argc;
38 	char **argv;
39 {
40 	register int ch, newl, skip;
41 	register char *p, *ep;
42 	struct msgbuf cur;
43 	char *core, *namelist;
44 
45 	core = namelist = NULL;
46 	while ((ch = getopt(argc, argv, "M:N:")) != EOF)
47 		switch(ch) {
48 		case 'M':
49 			core = optarg;
50 			break;
51 		case 'N':
52 			namelist = optarg;
53 			break;
54 		case '?':
55 		default:
56 			usage();
57 		}
58 	argc -= optind;
59 	argv += optind;
60 
61 	/* Read in kernel message buffer, do sanity checks. */
62 	if (kvm_openfiles(namelist, core, NULL) == -1)
63 		err("kvm_openfiles: %s", kvm_geterr());
64 	if (kvm_nlist(nl) == -1)
65 		err("kvm_nlist: %s", kvm_geterr());
66 	if (nl[X_MSGBUF].n_type == 0)
67 		err("msgbuf not found namelist");
68 
69         kvm_read((void *)nl[X_MSGBUF].n_value, (void *)&cur, sizeof(cur));
70 	if (cur.msg_magic != MSG_MAGIC)
71 		err("magic number incorrect");
72 	if (cur.msg_bufx >= MSG_BSIZE)
73 		cur.msg_bufx = 0;
74 
75 	/*
76 	 * The message buffer is circular; start at the read pointer, and
77 	 * go to the write pointer - 1.
78 	 */
79 	p = cur.msg_bufc + cur.msg_bufx;
80 	ep = cur.msg_bufc + cur.msg_bufx - 1;
81 	for (newl = skip = 0; p != ep; ++p) {
82 		if (p == cur.msg_bufc + MSG_BSIZE)
83 			p = cur.msg_bufc;
84 		ch = *p;
85 		/* Skip "\n<.*>" syslog sequences. */
86 		if (skip) {
87 			if (ch == '>')
88 				newl = skip = 0;
89 			continue;
90 		}
91 		if (newl && ch == '<') {
92 			skip = 1;
93 			continue;
94 		}
95 		if (ch == '\0')
96 			continue;
97 		newl = (ch = *p) == '\n';
98 		vputc(ch);
99 	}
100 	if (!newl)
101 		(void)putchar('\n');
102 	exit(0);
103 }
104 
105 void
106 vputc(ch)
107 	register int ch;
108 {
109 	int meta;
110 
111 	if (!isascii(ch)) {
112 		(void)putchar('M');
113 		(void)putchar('-');
114 		ch = toascii(ch);
115 		meta = 1;
116 	} else
117 		meta = 0;
118 	if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
119 		(void)putchar(ch);
120 	else {
121 		(void)putchar('^');
122 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
123 	}
124 }
125 
126 #if __STDC__
127 #include <stdarg.h>
128 #else
129 #include <varargs.h>
130 #endif
131 
132 void
133 #if __STDC__
134 err(const char *fmt, ...)
135 #else
136 err(fmt, va_alist)
137 	char *fmt;
138         va_dcl
139 #endif
140 {
141 	va_list ap;
142 #if __STDC__
143 	va_start(ap, fmt);
144 #else
145 	va_start(ap);
146 #endif
147 	(void)fprintf(stderr, "dmesg: ");
148 	(void)vfprintf(stderr, fmt, ap);
149 	va_end(ap);
150 	(void)fprintf(stderr, "\n");
151 	exit(1);
152 	/* NOTREACHED */
153 }
154 
155 void
156 usage()
157 {
158 	(void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
159 	exit(1);
160 }
161