File: debug.c

package info (click to toggle)
similarity-tester 2.77-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 572 kB
  • ctags: 512
  • sloc: ansic: 3,197; lex: 1,562; makefile: 301
file content (132 lines) | stat: -rw-r--r-- 2,015 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*	This file is part of the debugging module DEBUG.
	Written by Dick Grune, Vrije Universiteit, Amsterdam.
	$Id: debug.c,v 1.6 2013-05-12 09:58:34 Gebruiker Exp $
*/

#include	<stdlib.h>
#include	<unistd.h>
#include	<ctype.h>

#include	"debug.h"

/*Library module source prelude */
#undef	_MODULE_CODE_
#ifndef	lint
#define	_MODULE_CODE_
#endif
#ifdef	LIB
#define	_MODULE_CODE_
#endif

#ifdef	_MODULE_CODE_

/* Library module source code */

#ifdef		DEBUG

static void
wr_char(char ch) {
	write(2, &ch, 1);
}

static void
wr_num(int b,int v) {
	if (v >= b) {
		wr_num(b, v/b);
	}
	wr_char("0123456789ABCDEF"[v%b]);
}

static void
wr_str(const char *s) {
	while (*s) {
		wr_char(*s++);
	}
}

void
wr_info(const char *s, int b, int v) {
	/* print the string */
	if (s) {
		int cnt = 0;

		while (*s) {
			int ch = *s++ &0377;

			/* cut short a possibly corrupted string */
			if (cnt++ > 50) {
				wr_str("...");
				break;
			}

			/* put not thy faith in chars, signed or unsigned */
			if (isprint(ch)) {
				wr_char(ch);
			}
			else {
				switch (ch) {
				case '\n': wr_str("\\n"); break;
				case '\t': wr_str("\\t"); break;
				case '\r': wr_str("\\r"); break;
				case '\f': wr_str("\\f"); break;
				default:
					wr_char('\\');
					wr_char(ch / 0100 % 010 + '0');
					wr_char(ch / 010 % 010 + '0');
					wr_char(ch / 01 % 010 + '0');
					break;
				}
			}
		}
	}
	else {
		wr_str("<wr_info: NO STRING>");
	}

	/* print the value */
	if (b != 0) {
		wr_char(' ');
		if (v < 0) {
			wr_char('-');
			v = -v;
		}
		switch (b) {
		case 8:
			wr_char('0');
			wr_num(b, v);
			break;
		default:
			wr_num(10, v);
			break;
		case 16:
			wr_char('#');
			wr_num(b, v);
			break;
		case 128:
			wr_char(v);
			break;
		}
	}

	wr_char('\n');
}

#else

/*ARGSUSED*/
void
wr_info(const char *s, int b, int v) {
}

#endif	/* DEBUG */

/* End library module source code */
#endif	/* _MODULE_CODE_ */

#ifdef	lint
static void
satisfy_lint(void *x) {
	wr_info((char *)x, 0, 0);
	satisfy_lint(x);
}
#endif	/* lint */