aboutsummaryrefslogtreecommitdiff
path: root/pkg/xtools/xttxtfio.x
blob: 882966706010d32428e15feebb60a8c2cddea14b (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
65
66
67
68
69
70
71
define	TXT_MAXFD	64		# Maximum FD for stropen.


# XT_TXTOPEN -- Open a READ_ONLY text file which is possibly compiled into a
# procedure.
# 
# This is used to allow text files to be incorported in binaries but still use
# FIO.  The text file must be compiled into a program which is linked with
# into the binary (see txtcompile).  A file name of the form proc:nnnn, where
# nnnn is a number returned by locpr, calls the procedure which is expected to
# allocate a string buffer.  In this case the string buffer is opened with
# stropen.  Any other file name is opened as a READ_ONLY TEXT_FILE with
# normal FIO.

int procedure xt_txtopen (fname)

char	fname[ARB]		#I File name or proc:nnnn reference
int	fd			#R Null to open and non-null to close

int	ip, procptr, strncmp(), ctoi(), open(), stropen()
pointer	strbuf
errchk	zcall1, open, stropen

int	firsttime
data	firsttime/YES/

pointer	buf[TXT_MAXFD]
common	/xttxtn_com/ buf

begin
	# Make sure array of string buffer pointers is initialized.
	if (firsttime==YES) {
	    call aclri (buf, TXT_MAXFD)
	    firsttime = NO
	}

	# Determine type of open to use.
	if (strncmp (fname, "proc:", 5) == 0) {
	    ip = 1
	    if (ctoi (fname[6], ip, procptr) == 0)
		call error (1, "xt_txtopen: bad file specification")
	    call zcall1 (procptr, strbuf)
	    fd = stropen (Memc[strbuf], ARB, READ_ONLY)
	    if (fd > TXT_MAXFD) {
	        call close (fd)
		call mfree (strbuf, TY_CHAR)
		call error (1, "xt_txtopen: Too many file descriptors")
	    }
	    buf[fd] = strbuf
	} else
	    fd = open (fname, READ_ONLY, TEXT_FILE)

	return (fd)
end


# XT_TXTCLOSE -- Close procedure.

procedure xt_txtclose (fd)

int	fd			#O Null to open and non-null to close

pointer	buf[TXT_MAXFD]
common	/xttxtn_com/ buf

begin
	# Close file descriptor.
	call close (fd); fd = NULL
	if (fd <= TXT_MAXFD)
	    call mfree (buf[fd], TY_CHAR)
end