devopen.c revision 1.3.6.2 1 1.3.6.2 cgd /* $NetBSD: devopen.c,v 1.3.6.2 1997/04/06 08:41:26 cgd Exp $ */
2 1.3.6.2 cgd
3 1.3.6.2 cgd /*-
4 1.3.6.2 cgd * Copyright (c) 1992, 1993
5 1.3.6.2 cgd * The Regents of the University of California. All rights reserved.
6 1.3.6.2 cgd *
7 1.3.6.2 cgd * This code is derived from software contributed to Berkeley by
8 1.3.6.2 cgd * Ralph Campbell.
9 1.3.6.2 cgd *
10 1.3.6.2 cgd * Redistribution and use in source and binary forms, with or without
11 1.3.6.2 cgd * modification, are permitted provided that the following conditions
12 1.3.6.2 cgd * are met:
13 1.3.6.2 cgd * 1. Redistributions of source code must retain the above copyright
14 1.3.6.2 cgd * notice, this list of conditions and the following disclaimer.
15 1.3.6.2 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.6.2 cgd * notice, this list of conditions and the following disclaimer in the
17 1.3.6.2 cgd * documentation and/or other materials provided with the distribution.
18 1.3.6.2 cgd * 3. All advertising materials mentioning features or use of this software
19 1.3.6.2 cgd * must display the following acknowledgement:
20 1.3.6.2 cgd * This product includes software developed by the University of
21 1.3.6.2 cgd * California, Berkeley and its contributors.
22 1.3.6.2 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.3.6.2 cgd * may be used to endorse or promote products derived from this software
24 1.3.6.2 cgd * without specific prior written permission.
25 1.3.6.2 cgd *
26 1.3.6.2 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.3.6.2 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.3.6.2 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.3.6.2 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.3.6.2 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.3.6.2 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.3.6.2 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.3.6.2 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.3.6.2 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.3.6.2 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.3.6.2 cgd * SUCH DAMAGE.
37 1.3.6.2 cgd *
38 1.3.6.2 cgd * @(#)devopen.c 8.1 (Berkeley) 6/10/93
39 1.3.6.2 cgd */
40 1.3.6.2 cgd
41 1.3.6.2 cgd #include <lib/libsa/stand.h>
42 1.3.6.2 cgd
43 1.3.6.2 cgd /*
44 1.3.6.2 cgd * Decode the string 'fname', open the device and return the remaining
45 1.3.6.2 cgd * file name if any.
46 1.3.6.2 cgd */
47 1.3.6.2 cgd devopen(f, fname, file)
48 1.3.6.2 cgd struct open_file *f;
49 1.3.6.2 cgd const char *fname;
50 1.3.6.2 cgd char **file; /* out */
51 1.3.6.2 cgd {
52 1.3.6.2 cgd register char *cp;
53 1.3.6.2 cgd register char *ncp;
54 1.3.6.2 cgd register struct devsw *dp;
55 1.3.6.2 cgd register int c, i;
56 1.3.6.2 cgd int ctlr = 0, unit = 0, part = 0;
57 1.3.6.2 cgd char namebuf[20];
58 1.3.6.2 cgd int rc;
59 1.3.6.2 cgd
60 1.3.6.2 cgd cp = (char *)fname;
61 1.3.6.2 cgd ncp = namebuf;
62 1.3.6.2 cgd
63 1.3.6.2 cgd #if 0
64 1.3.6.2 cgd /* look for a string like '5/rz0/vmunix' or '5/rz3f/vmunix */
65 1.3.6.2 cgd if ((c = *cp) >= '0' && c <= '9') {
66 1.3.6.2 cgd ctlr = c - '0';
67 1.3.6.2 cgd /* skip the '/' */
68 1.3.6.2 cgd if (*++cp != '/')
69 1.3.6.2 cgd return (ENXIO);
70 1.3.6.2 cgd cp++;
71 1.3.6.2 cgd while ((c = *cp) != '\0') {
72 1.3.6.2 cgd if (c == '/')
73 1.3.6.2 cgd break;
74 1.3.6.2 cgd if (c >= '0' && c <= '9') {
75 1.3.6.2 cgd /* read unit number */
76 1.3.6.2 cgd unit = c - '0';
77 1.3.6.2 cgd
78 1.3.6.2 cgd /* look for a partition */
79 1.3.6.2 cgd if ((c = *++cp) >= 'a' && c <= 'h') {
80 1.3.6.2 cgd part = c - 'a';
81 1.3.6.2 cgd c = *++cp;
82 1.3.6.2 cgd }
83 1.3.6.2 cgd if (c != '/')
84 1.3.6.2 cgd return (ENXIO);
85 1.3.6.2 cgd break;
86 1.3.6.2 cgd }
87 1.3.6.2 cgd if (ncp < namebuf + sizeof(namebuf) - 1)
88 1.3.6.2 cgd *ncp++ = c;
89 1.3.6.2 cgd cp++;
90 1.3.6.2 cgd }
91 1.3.6.2 cgd *ncp = '\0';
92 1.3.6.2 cgd /*
93 1.3.6.2 cgd * XXX
94 1.3.6.2 cgd * pulling strchr from the C library, should pull from libkern.
95 1.3.6.2 cgd */
96 1.3.6.2 cgd } else if (strchr(cp, '(')) {
97 1.3.6.2 cgd /* expect a string like 'rz(0,0,0)vmunix' */
98 1.3.6.2 cgd while ((c = *cp) != '\0') {
99 1.3.6.2 cgd if (c == '(') {
100 1.3.6.2 cgd cp++;
101 1.3.6.2 cgd break;
102 1.3.6.2 cgd }
103 1.3.6.2 cgd if (ncp < namebuf + sizeof(namebuf) - 1)
104 1.3.6.2 cgd *ncp++ = c;
105 1.3.6.2 cgd cp++;
106 1.3.6.2 cgd }
107 1.3.6.2 cgd
108 1.3.6.2 cgd /* get controller number */
109 1.3.6.2 cgd if ((c = *cp) >= '0' && c <= '9') {
110 1.3.6.2 cgd ctlr = c - '0';
111 1.3.6.2 cgd c = *++cp;
112 1.3.6.2 cgd }
113 1.3.6.2 cgd
114 1.3.6.2 cgd if (c == ',') {
115 1.3.6.2 cgd /* get SCSI device number */
116 1.3.6.2 cgd if ((c = *++cp) >= '0' && c <= '9') {
117 1.3.6.2 cgd unit = c - '0';
118 1.3.6.2 cgd c = *++cp;
119 1.3.6.2 cgd }
120 1.3.6.2 cgd
121 1.3.6.2 cgd if (c == ',') {
122 1.3.6.2 cgd /* get partition number */
123 1.3.6.2 cgd if ((c = *++cp) >= '0' && c <= '9') {
124 1.3.6.2 cgd part = c - '0';
125 1.3.6.2 cgd c = *++cp;
126 1.3.6.2 cgd }
127 1.3.6.2 cgd }
128 1.3.6.2 cgd }
129 1.3.6.2 cgd if (c != ')')
130 1.3.6.2 cgd return (ENXIO);
131 1.3.6.2 cgd cp++;
132 1.3.6.2 cgd *ncp = '\0';
133 1.3.6.2 cgd } else {
134 1.3.6.2 cgd #endif
135 1.3.6.2 cgd dp = devsw;
136 1.3.6.2 cgd ctlr = unit = part = 0;
137 1.3.6.2 cgd goto fnd;
138 1.3.6.2 cgd #if 0
139 1.3.6.2 cgd }
140 1.3.6.2 cgd
141 1.3.6.2 cgd for (dp = devsw, i = 0; i < ndevs; dp++, i++)
142 1.3.6.2 cgd if (dp->dv_name && strcmp(namebuf, dp->dv_name) == 0)
143 1.3.6.2 cgd goto fnd;
144 1.3.6.2 cgd printf("Unknown device '%s'\nKnown devices are:", namebuf);
145 1.3.6.2 cgd for (dp = devsw, i = 0; i < ndevs; dp++, i++)
146 1.3.6.2 cgd if (dp->dv_name)
147 1.3.6.2 cgd printf(" %s", dp->dv_name);
148 1.3.6.2 cgd printf("\n");
149 1.3.6.2 cgd return (ENXIO);
150 1.3.6.2 cgd #endif
151 1.3.6.2 cgd
152 1.3.6.2 cgd fnd:
153 1.3.6.2 cgd rc = (dp->dv_open)(f, ctlr, unit, part);
154 1.3.6.2 cgd if (rc)
155 1.3.6.2 cgd return (rc);
156 1.3.6.2 cgd
157 1.3.6.2 cgd f->f_dev = dp;
158 1.3.6.2 cgd if (file && *cp != '\0')
159 1.3.6.2 cgd *file = cp;
160 1.3.6.2 cgd return (0);
161 1.3.6.2 cgd }
162