aboutsummaryrefslogtreecommitdiff
path: root/sys/imio/iki/ikiaccess.x
blob: 83736b4f652a0b6c6b51afb445940e5ef547851f (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
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <imhdr.h>
include "iki.h"

# IKI_ACCESS -- Determine if the named image exists, and if so, return the
# the index of the IKI kernel to be used to access the image, else return 0 if
# the named image is not found.  If multiple images exist with the same name
# (e.g. but with different image types) then ERR is returned.  An NEW_IMAGE
# access mode may be specified to associate an extension with an image kernel
# without testing for the existence of an image.  If the input image name did
# not specify an extension or what appeared to be an extension was just a .
# delimited field, we will patch up the ROOT and EXTN strings to the real
# values.

int procedure iki_access (image, root, extn, acmode)

char	image[ARB]		#I image/group name
char	root[ARB]		#O image/group file name
char	extn[ARB]		#O image/group file extension
int	acmode

bool	first_time
int	i, k, status, op
pointer	sp, osroot, fname, textn, fextn, ip
data	first_time /true/

bool	fnullfile()
int	gstrcpy(), strlen()
errchk	fpathname, syserrs
include	"iki.com"

begin
	call smark (sp)
	call salloc (osroot, SZ_PATHNAME, TY_CHAR)
	call salloc (fname, SZ_PATHNAME, TY_CHAR)
	call salloc (textn, MAX_LENEXTN, TY_CHAR)
	call salloc (fextn, MAX_LENEXTN, TY_CHAR)

	# The first call makes sure the IKI kernels are loaded into the kernel
	# table.

	if (first_time) {
	    call iki_init()
	    first_time = false
	}

	call iki_parse (image, root, extn)
	if (fnullfile (root)) {
	    call sfree (sp)
	    return (1)
	}

	repeat {
	    # Convert to absolute pathname to render names like file
	    # and ./file equivalent.  Add a dummy file extension first
	    # to cause escape sequence encoding of any .DDDD etc. files
	    # which may be part of the root image name.

	    op = gstrcpy (root, Memc[fname], SZ_PATHNAME)
	    call strcpy (".x", Memc[fname+op], SZ_PATHNAME-op+1)
	    call fpathname (Memc[fname], Memc[osroot], SZ_PATHNAME)
	    Memc[osroot+strlen(Memc[osroot])-2] = EOS

	    # Escape any $ in the pathname since we are still in VOS land.
	    op = 1
	    for (ip=osroot;  Memc[ip] != EOS;  ip=ip+1) {
		if (Memc[ip] == '$' || Memc[ip] == '[') {
		    root[op] = '\\'
		    op = op + 1
		}
		root[op] = Memc[ip]
		op = op + 1
	    }

	    root[op] = EOS

	    # Select an image kernel by calling the access function in each
	    # loaded kernel until somebody claims the image.  If multiple
	    # kernels claim the image the image specification (name) is
	    # ambiguous and we don't know which image was intended, an error.
	    # Note that in the case of a new image, the access function
	    # tests only the legality of the extn.

	    k = 0
	    for (i=1;  i <= k_nkernels;  i=i+1) {
		call strcpy (extn, Memc[textn], MAX_LENEXTN)
		call zcall5 (IKI_ACCESS(i), i,root,Memc[textn],acmode,status)

		if (status == YES) {
		    if (k == 0) {
			# Stop on the first access if an explicit extension
			# was given.

			k = i
			call strcpy (Memc[textn], Memc[fextn], MAX_LENEXTN)
			if (extn[1] != EOS)
			    break

		    } else {
			# The image name is ambiguous.
			call sfree (sp)
			return (ERR)
		    }
		}
	    }

	    # Valid image using kernel K.
	    if (k != 0) {
		call strcpy (Memc[fextn], extn, MAX_LENEXTN)
		call sfree (sp)
		return (k)
	    }

	    # If the search failed and an extension was given, maybe what
	    # we thought was an extension was really just part of the root
	    # filename.  Try again with the extn folded into the root.

	    if (status == NO && extn[1] != EOS) {
		call strcpy (image, root, SZ_PATHNAME)
		extn[1] = EOS
	    } else
		break
	}

	call sfree (sp)
	return (0)
end