aboutsummaryrefslogtreecommitdiff
path: root/sys/fmtio/ctocc.x
blob: b2f4197a961f10046e3c6b3e0498aa5586814468 (plain) (blame)
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<ctype.h>

define	OCTAL		8

# CTOCC -- Convert a character into a printable character constant.
# Printable characters are output as is.  The standard control characters
# (newline, tab, etc.) are output as escape sequences (\n, \t, etc.).
# Other control characters are output in the form '^X'.  Characters which
# are neither printable nor standard control characters are output as
# octal constants of the form '\DDD'.  Note that the ouput string is not
# enclosed in ticks ('\n', etc.), because the generated character constant
# might appear in a quoted string (or someplace other than an explicit
# character constant).

int procedure ctocc (ch, outstr, maxch)

char	ch			# character to be output
char	outstr[ARB]		# output string
int	maxch			# max chars out

int	op, n
int	stridx()
define	output 	{outstr[op]=$1;op=op+1;if(op>maxch)goto overflow_}
define	overflow_ 99
include	"escchars.inc"

begin
	op = 1

	if (maxch > 0) {
	    if (IS_PRINT(ch)) {				# output char as is
		output (ch)
	    } else if (IS_CNTRL (ch)) {
		n = stridx (ch, mapped_chars)
		if (n > 0) {				# '\c'
		    output ('\\')
		    output (escape_chars[n])
		} else {
		    output ('^')			# control chars
		    output (ch + 'A' - 1)
		}

	    } else {					# '\nnn' 
		# Always output 3 digits so that strings like \0405 (a blank
		# followed by a `5') can be interpreted during the reverse
		# encoding operation.

		output ('\\')
		output (TO_DIGIT (mod (ch / 0100B, 010B)))
		output (TO_DIGIT (mod (ch / 0010B, 010B)))
		output (TO_DIGIT (mod (ch / 0001B, 010B)))
	    }
	}

	outstr[op] = EOS
	return (op-1)

overflow_
	outstr[1] = '?'					# no room, print '?'
	outstr[2] = EOS
	return (1)
end