File: sysdep_8c-source.html

package info (click to toggle)
discover 2.1.2-10.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,756 kB
  • sloc: sh: 7,862; ansic: 7,280; xml: 1,809; makefile: 679
file content (308 lines) | stat: -rw-r--r-- 20,543 bytes parent folder | download | duplicates (9)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>discover: /home/pere/src/debiancvs/pkg-discover/discover/trunk/lib/sysdep.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.2 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a></div>
<div class="nav">
<a class="el" href="dir_000002.html">lib</a></div>
<h1>sysdep.c</h1><a href="sysdep_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 
00010 <span class="comment">/* $Progeny$</span>
00011 <span class="comment"> *</span>
00012 <span class="comment"> * AUTHOR: John R. Daily &lt;jdaily@progeny.com&gt;</span>
00013 <span class="comment"> *</span>
00014 <span class="comment"> * Copyright 2002 Progeny Linux Systems, Inc.</span>
00015 <span class="comment"> *</span>
00016 <span class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</span>
00017 <span class="comment"> * copy of this software and associated documentation files (the "Software"),</span>
00018 <span class="comment"> * to deal in the Software without restriction, including without limitation</span>
00019 <span class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</span>
00020 <span class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</span>
00021 <span class="comment"> * Software is furnished to do so, subject to the following conditions:</span>
00022 <span class="comment"> *</span>
00023 <span class="comment"> * The above copyright notice and this permission notice shall be included in</span>
00024 <span class="comment"> * all copies or substantial portions of the Software.</span>
00025 <span class="comment"> *</span>
00026 <span class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
00027 <span class="comment"> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
00028 <span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL</span>
00029 <span class="comment"> * THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
00030 <span class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</span>
00031 <span class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</span>
00032 <span class="comment"> * DEALINGS IN THE SOFTWARE.</span>
00033 <span class="comment"> */</span>
00034 <span class="preprocessor">#include &lt;assert.h&gt;</span>
00035 
00036 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
00037 <span class="preprocessor">#include &lt;string.h&gt;</span>
00038 
00039 <span class="preprocessor">#include &lt;<a class="code" href="discover_8h.html">discover/discover.h</a>&gt;</span>
00040 <span class="preprocessor">#include &lt;<a class="code" href="discover-conf_8h.html">discover/discover-conf.h</a>&gt;</span>
00041 <span class="preprocessor">#include &lt;<a class="code" href="discover-xml_8h.html">discover/discover-xml.h</a>&gt;</span>
00042 <span class="preprocessor">#include &lt;<a class="code" href="sysdep_8h.html">discover/sysdep.h</a>&gt;</span>
00043 
00044 <span class="preprocessor">#include &lt;discover/device.h&gt;</span>
00045 <span class="preprocessor">#include &lt;discover/utils.h&gt;</span>
00046 
00047 
<a name="l00049"></a><a class="code" href="sysdep_8c.html#a0">00049</a> <span class="keyword">typedef</span> <a class="code" href="structdiscover__sysdep__data.html">discover_sysdep_data_t</a> *(<a class="code" href="sysdep_8c.html#a0">raw_sysdep_function_t</a>)(void);
00050 
00051 <span class="keyword">static</span> <a class="code" href="sysdep_8c.html#a0">raw_sysdep_function_t</a> *raw_map[] = {
00052     <a class="code" href="group__sysdeps.html#ga0">_discover_get_ata_raw</a>,
00053     <a class="code" href="group__sysdeps.html#ga1">_discover_get_pci_raw</a>,
00054     <a class="code" href="group__sysdeps.html#ga2">_discover_get_pcmcia_raw</a>,
00055     <a class="code" href="group__sysdeps.html#ga3">_discover_get_scsi_raw</a>,
00056     <a class="code" href="group__sysdeps.html#ga4">_discover_get_usb_raw</a>
00057 };
00058 
00059 
00060 <span class="comment">/*</span>
00061 <span class="comment"> * Utility functions</span>
00062 <span class="comment"> */</span>
00063 
00065 <a class="code" href="structdiscover__sysdep__data.html">discover_sysdep_data_t</a> *
<a name="l00066"></a><a class="code" href="sysdep_8h.html#a2">00066</a> <a class="code" href="sysdep_8c.html#a3">_discover_sysdep_data_new</a>(<span class="keywordtype">void</span>)
00067 {
00068     <a class="code" href="structdiscover__sysdep__data.html">discover_sysdep_data_t</a> *node =
00069         _discover_xmalloc(<span class="keyword">sizeof</span>(<a class="code" href="structdiscover__sysdep__data.html">discover_sysdep_data_t</a>));
00070     node-&gt;<a class="code" href="structdiscover__sysdep__data.html#o0">busclass</a> = NULL;
00071     node-&gt;<a class="code" href="structdiscover__sysdep__data.html#o1">vendor</a> = NULL;
00072     node-&gt;<a class="code" href="structdiscover__sysdep__data.html#o2">model</a> = NULL;
00073     node-&gt;<a class="code" href="structdiscover__sysdep__data.html#o3">data</a> = NULL;
00074     node-&gt;<a class="code" href="structdiscover__sysdep__data.html#o4">next</a> = NULL;
00075     <span class="keywordflow">return</span> node;
00076 }
00077 
00079 <a class="code" href="structdiscover__sysdep__device__data.html">discover_sysdep_device_data_t</a> *
<a name="l00080"></a><a class="code" href="sysdep_8h.html#a3">00080</a> <a class="code" href="sysdep_8c.html#a4">_discover_sysdep_device_data_new</a>(<span class="keywordtype">void</span>)
00081 {
00082     <a class="code" href="structdiscover__sysdep__device__data.html">discover_sysdep_device_data_t</a> *node =
00083         _discover_xmalloc(<span class="keyword">sizeof</span>(<a class="code" href="structdiscover__sysdep__device__data.html">discover_sysdep_device_data_t</a>));
00084     node-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o0">path</a> = NULL;
00085     node-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o1">value</a> = NULL;
00086     node-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o2">next</a> = NULL;
00087     <span class="keywordflow">return</span> node;
00088 }
00089 
00091 <span class="keywordtype">void</span>
<a name="l00092"></a><a class="code" href="sysdep_8h.html#a4">00092</a> <a class="code" href="sysdep_8c.html#a5">_discover_free_sysdep_data</a>(<a class="code" href="structdiscover__sysdep__data.html">discover_sysdep_data_t</a> *head)
00093 {
00094     <a class="code" href="structdiscover__sysdep__data.html">discover_sysdep_data_t</a> *node;
00095 
00096     <span class="keywordflow">while</span> (head) {
00097         node = head-&gt;<a class="code" href="structdiscover__sysdep__data.html#o4">next</a>;
00098         <span class="keywordflow">if</span> (head-&gt;<a class="code" href="structdiscover__sysdep__data.html#o1">vendor</a>) {
00099             free(head-&gt;<a class="code" href="structdiscover__sysdep__data.html#o1">vendor</a>);
00100         }
00101         <span class="keywordflow">if</span> (head-&gt;<a class="code" href="structdiscover__sysdep__data.html#o2">model</a>) {
00102             free(head-&gt;<a class="code" href="structdiscover__sysdep__data.html#o2">model</a>);
00103         }
00104         <span class="keywordflow">if</span> (head-&gt;<a class="code" href="structdiscover__sysdep__data.html#o0">busclass</a>) {
00105             free(head-&gt;<a class="code" href="structdiscover__sysdep__data.html#o0">busclass</a>);
00106         }
00107         <span class="keywordflow">if</span> (head-&gt;<a class="code" href="structdiscover__sysdep__data.html#o3">data</a>) {
00108             <a class="code" href="sysdep_8c.html#a6">_discover_free_sysdep_device_data</a>(head-&gt;<a class="code" href="structdiscover__sysdep__data.html#o3">data</a>);
00109         }
00110         free(head);
00111         head = node;
00112     }
00113 }
00114 
00116 <span class="keywordtype">void</span>
<a name="l00117"></a><a class="code" href="sysdep_8h.html#a5">00117</a> <a class="code" href="sysdep_8c.html#a6">_discover_free_sysdep_device_data</a>(<a class="code" href="structdiscover__sysdep__device__data.html">discover_sysdep_device_data_t</a> *head)
00118 {
00119     <a class="code" href="structdiscover__sysdep__device__data.html">discover_sysdep_device_data_t</a> *node;
00120     <span class="keywordflow">while</span> (head) {
00121         node = head-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o2">next</a>;
00122         <span class="keywordflow">if</span> (head-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o0">path</a>) {
00123             free(head-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o0">path</a>);
00124         }
00125         <span class="keywordflow">if</span> (head-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o1">value</a>) {
00126             free(head-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o1">value</a>);
00127         }
00128         free(head);
00129         head = node;
00130     }
00131 }
00132 
00134 <a class="code" href="group__types.html#ga4">discover_data_t</a> *
<a name="l00135"></a><a class="code" href="sysdep_8c.html#a7">00135</a> <a class="code" href="sysdep_8c.html#a7">_discover_convert_device_data</a>(<a class="code" href="structdiscover__sysdep__device__data.html">discover_sysdep_device_data_t</a> *head)
00136 {
00137     <a class="code" href="group__types.html#ga4">discover_data_t</a> *data_head, *data_current, *data_new;
00138     <a class="code" href="structdiscover__sysdep__device__data.html">discover_sysdep_device_data_t</a> *node;
00139     <span class="keywordtype">char</span> *buffer, *path, *token;
00140 
00142     data_head = <a class="code" href="group__device.html#ga13">discover_data_new</a>();
00143     data_head-&gt;discover_class = _discover_xstrdup(<span class="stringliteral">"device"</span>);
00144 
00145     <span class="comment">/*</span>
00146 <span class="comment">     * Step through the device data nodes returned by the sysdeps.</span>
00147 <span class="comment">     * Each node will have a path relative to the "device" toplevel,</span>
00148 <span class="comment">     * separated by slashes, as well as a value.  We need to set</span>
00149 <span class="comment">     * each one up to look like it came from the XML data.</span>
00150 <span class="comment">     */</span>
00151     <span class="keywordflow">for</span> (node = head; node; node = head-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o2">next</a>) {
00153         data_current = NULL;
00154 
00156         path = _discover_xstrdup(node-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o0">path</a>);
00157         buffer = _discover_xstrdup(node-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o0">path</a>);
00158 
00160         <span class="keywordflow">for</span> (token = strtok_r(path, <span class="stringliteral">"/"</span>, &amp;buffer); token;
00161              token = strtok_r(NULL, <span class="stringliteral">"/"</span>, &amp;buffer)) {
00163             data_new = <a class="code" href="group__device.html#ga13">discover_data_new</a>();
00164             data_new-&gt;discover_class = _discover_xstrdup(token);
00165 
00167             <span class="keywordflow">if</span> (data_current == NULL) {
00169                 <span class="keywordflow">if</span> (data_head-&gt;child == NULL) {
00170                     data_head-&gt;child = data_new;
00171                 } <span class="keywordflow">else</span> {
00172                     data_current = data_head-&gt;child;
00173                     <span class="keywordflow">while</span> (data_current-&gt;next)
00174                         data_current = data_current-&gt;next;
00175                     data_current-&gt;next = data_new;
00176                 }
00177             } <span class="keywordflow">else</span> {
00179                 data_current-&gt;child = data_new;
00180                 data_new-&gt;parent = data_current;
00181             }
00182 
00183             data_current = data_new;
00184         }
00185 
00186         <span class="comment">/*</span>
00187 <span class="comment">         * We've gotten to the very last path element; set the </span>
00188 <span class="comment">         * node's value here.</span>
00189 <span class="comment">         */</span>
00190         data_current-&gt;text = _discover_xstrdup(node-&gt;<a class="code" href="structdiscover__sysdep__device__data.html#o1">value</a>);
00191 
00193         free(path);
00194         free(buffer);
00195     }
00196 
00197     <span class="keywordflow">return</span> data_head;
00198 }
00199 
00200 <span class="comment">/*</span>
00201 <span class="comment"> * Functions that access the sysdeps</span>
00202 <span class="comment"> */</span>
00203 <span class="keyword">static</span> <a class="code" href="group__types.html#ga3">discover_device_t</a> *devices[<a class="code" href="group__types.html#ga14">BUS_COUNT</a>];
00204 
00205 <a class="code" href="group__types.html#ga3">discover_device_t</a> *
<a name="l00206"></a><a class="code" href="discover_8h.html#a33">00206</a> <a class="code" href="sysdep_8c.html#a8">discover_get_devices</a>(<a class="code" href="group__types.html#ga16">discover_bus_t</a> bus, <a class="code" href="structdiscover__error.html">discover_error_t</a> *status)
00207 {
00208     <a class="code" href="group__types.html#ga3">discover_device_t</a> *device, *last;
00209     <a class="code" href="group__types.html#ga3">discover_device_t</a> *xml_devices;
00210     <a class="code" href="structdiscover__bus__map.html">discover_bus_map_t</a> *busmap;
00211     <a class="code" href="structdiscover__sysdep__data.html">discover_sysdep_data_t</a> *head, *node;
00212     <a class="code" href="group__types.html#ga4">discover_data_t</a> *data_head, *data_node;
00213 
00214     assert(status);
00215 
00216     status-&gt;<a class="code" href="structdiscover__error.html#o0">code</a> = <a class="code" href="group__types.html#gga15a15">DISCOVER_SUCCESS</a>;
00217     device = last = NULL;
00218 
00219     busmap = <a class="code" href="group__conf.html#ga4">discover_conf_get_bus_map</a>(bus, status);
00220     <span class="keywordflow">if</span> (status-&gt;<a class="code" href="structdiscover__error.html#o0">code</a> != 0) {
00221         <span class="keywordflow">return</span> NULL;
00222     }
00223 
00224     <span class="keywordflow">if</span> (busmap-&gt;<a class="code" href="structdiscover__bus__map.html#o2">scan_never</a>) {
00225         status-&gt;<a class="code" href="structdiscover__error.html#o0">code</a> = <a class="code" href="group__types.html#gga15a19">DISCOVER_EBUSDISABLED</a>;
00226         <span class="keywordflow">return</span> NULL;
00227     }
00228 
00229     <span class="keywordflow">if</span> (devices[bus]) {
00230         <span class="keywordflow">return</span> devices[bus];
00231     }
00232 
00233     xml_devices = <a class="code" href="group__device__xml.html#ga1">discover_xml_get_devices</a>(bus, status);
00234     <span class="keywordflow">if</span> (!xml_devices) {
00235         <span class="keywordflow">return</span> NULL;
00236     }
00237 
00238     <span class="comment">/*</span>
00239 <span class="comment">     * Allow overrides of this function.</span>
00240 <span class="comment">     */</span>
00241     <span class="keywordflow">if</span> (busmap-&gt;<a class="code" href="structdiscover__bus__map.html#o3">get_raw</a>) {
00242         head = node = busmap-&gt;<a class="code" href="structdiscover__bus__map.html#o3">get_raw</a>();
00243     } <span class="keywordflow">else</span> {
00244         head = node = raw_map[bus]();
00245     }
00246 
00247     <span class="keywordflow">while</span> (node) {
00248         <span class="comment">/*</span>
00249 <span class="comment">         * The variable names are ambiguous:</span>
00250 <span class="comment">         *   node   -  Hardware data retrieved from the OS</span>
00251 <span class="comment">         *   device -  Matching hardware data from our database</span>
00252 <span class="comment">         */</span>
00253         device =
00254             <a class="code" href="group__device__xml.html#ga5">discover_xml_get_matching_devices</a>(xml_devices, node-&gt;vendor,
00255                                               node-&gt;model, status);
00256 
00257         <span class="keywordflow">if</span> (!device) {
00258             device = <a class="code" href="group__device.html#ga22">discover_device_new</a>();
00259             device-&gt;model_id = strdup(node-&gt;model);
00260             device-&gt;vendor_id = strdup(node-&gt;vendor);
00261         }
00262 
00263         <span class="comment">/*</span>
00264 <span class="comment">         * If we get a busclass from the hardware, treat it as</span>
00265 <span class="comment">         * canonical.</span>
00266 <span class="comment">         */</span>
00267         <span class="keywordflow">if</span> (node-&gt;busclass != NULL) {
00268             <span class="keywordflow">if</span> (device-&gt;busclass != NULL) {
00269                 free(device-&gt;busclass);
00270             }
00271             device-&gt;busclass = strdup(node-&gt;busclass);
00272         }
00273 
00274         <span class="comment">/* Add sysdep data to the data nodes. */</span>
00275         <span class="keywordflow">if</span> (node-&gt;data != NULL) {
00276             data_head = <a class="code" href="sysdep_8c.html#a7">_discover_convert_device_data</a>(node-&gt;data);
00277             <span class="keywordflow">if</span> (data_head != NULL) {
00278                 <span class="keywordflow">for</span> (data_node = data_head; data_node-&gt;next != NULL; 
00279                      data_node = data_node-&gt;next) ;
00280                 data_node-&gt;next = device-&gt;data;
00281                 device-&gt;data = data_head;
00282             }
00283         }
00284 
00285         <span class="keywordflow">if</span> (last) {
00286             last-&gt;next = device;
00287             last = device;
00288         } <span class="keywordflow">else</span> {
00289             devices[bus] = last = device;
00290         }
00291 
00292         node = node-&gt;next;
00293     }
00294 
00295     <a class="code" href="sysdep_8c.html#a5">_discover_free_sysdep_data</a>(head);
00296 
00297     <span class="keywordflow">return</span> devices[bus];
00298 }
00299 
00300 <span class="keywordtype">void</span>
<a name="l00301"></a><a class="code" href="discover_8h.html#a34">00301</a> <a class="code" href="sysdep_8c.html#a9">discover_free_devices</a>(<span class="keywordtype">void</span>)
00302 {
00303     <span class="keywordtype">int</span> i;
00304 
00305     <span class="keywordflow">for</span> (i = 0; i &lt; <a class="code" href="group__types.html#ga14">BUS_COUNT</a>; i++) {
00306         <a class="code" href="group__device.html#ga23">discover_device_free</a>(devices[i], 0);
00307         devices[i] = NULL;
00308     }
00309 }
00310 
00311 <span class="comment">/*</span>
00312 <span class="comment"> * Local variables:</span>
00313 <span class="comment"> * c-file-style: "progeny"</span>
00314 <span class="comment"> * indent-tabs-mode: nil</span>
00315 <span class="comment"> * End:</span>
00316 <span class="comment"> */</span>
00317 <span class="comment">/* vim: set cin fo=tcroq sw=4 et sts=4 tw=75: */</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Jul 28 03:38:00 2005 for discover by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
</body>
</html>