• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* sane - Scanner Access Now Easy.
2    Copyright (C) 1998 David Mosberger
3    This file is part of the SANE package.
4 
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 
18    As a special exception, the authors of SANE give permission for
19    additional uses of the libraries contained in this release of SANE.
20 
21    The exception is that, if you link a SANE library with other files
22    to produce an executable, this does not by itself cause the
23    resulting executable to be covered by the GNU General Public
24    License.  Your use of that executable is in no way restricted on
25    account of linking the SANE library code into it.
26 
27    This exception does not, however, invalidate any other reasons why
28    the executable file might be covered by the GNU General Public
29    License.
30 
31    If you submit changes to SANE to the maintainers to be included in
32    a subsequent release, you agree by submitting the changes that
33    those changes may be distributed with this exception intact.
34 
35    If you write modifications of your own for SANE, it is your choice
36    whether to permit this exception to apply to your modifications.
37    If you do not wish that, delete this exception notice.
38 
39    This file provides generic configuration support.  */
40 
41 #include "../include/sane/config.h"
42 
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #ifdef __BEOS__
49 #include <dirent.h>
50 #include <unistd.h>
51 #include <drivers/USB_scanner.h>
52 #endif
53 
54 #include "../include/sane/sanei.h"
55 #include "../include/sane/sanei_config.h"
56 #include "../include/sane/sanei_scsi.h"
57 
58 /* This logically belongs to sanei_config.c but not every backend that
59    uses sanei_config() wants to depend on this function.  */
60 
61 void
sanei_config_attach_matching_devices(const char * name,SANE_Status (* attach)(const char * dev))62 sanei_config_attach_matching_devices (const char *name,
63 				      SANE_Status (*attach) (const char *dev))
64 {
65   int bus = -1, channel = -1, id = -1, lun = -1;
66   char *vendor = 0, *model = 0, *type = 0, *end;
67 
68   if (strncmp (name, "scsi", 4) == 0)
69     {
70       name += 4;
71 
72       name = sanei_config_skip_whitespace (name);
73       if (*name)
74 	{
75 	  name = sanei_config_get_string (name, &vendor);
76 	  if (vendor && strcmp (vendor, "*") == 0)
77 	    {
78 	      free (vendor);
79 	      vendor = 0;
80 	    }
81 	  name = sanei_config_skip_whitespace (name);
82 	}
83 
84       name = sanei_config_skip_whitespace (name);
85       if (*name)
86 	{
87 	  name = sanei_config_get_string (name, &model);
88 	  if (model && strcmp (model, "*") == 0)
89 	    {
90 	      free (model);
91 	      model = 0;
92 	    }
93 	  name = sanei_config_skip_whitespace (name);
94 	}
95 
96       name = sanei_config_skip_whitespace (name);
97       if (*name)
98 	{
99 	  name = sanei_config_get_string (name, &type);
100 	  if (type && strcmp (type, "*") == 0)
101 	    {
102 	      free (type);
103 	      type = 0;
104 	    }
105 	  name = sanei_config_skip_whitespace (name);
106 	}
107 
108       if (isdigit (*name))
109 	{
110 	  bus = strtol (name, &end, 10);
111 	  name = sanei_config_skip_whitespace (end);
112 	}
113       else if (*name == '*')
114 	name = sanei_config_skip_whitespace (++name);
115 
116       if (isdigit (*name))
117 	{
118 	  channel = strtol (name, &end, 10);
119 	  name = sanei_config_skip_whitespace (end);
120 	}
121       else if (*name == '*')
122 	name = sanei_config_skip_whitespace (++name);
123 
124       if (isdigit (*name))
125 	{
126 	  id = strtol (name, &end, 10);
127 	  name = sanei_config_skip_whitespace (end);
128 	}
129       else if (*name == '*')
130 	name = sanei_config_skip_whitespace (++name);
131 
132       if (isdigit (*name))
133 	{
134 	  lun = strtol (name, &end, 10);
135 	  name = sanei_config_skip_whitespace (end);
136 	}
137       else if (*name == '*')
138 	name = sanei_config_skip_whitespace (++name);
139 
140       sanei_scsi_find_devices (vendor, model, type, bus, channel, id, lun,
141 			       attach);
142 
143       if (vendor)
144 	free (vendor);
145       if (model)
146 	free (model);
147       if (type)
148 	free (type);
149     }
150   else
151     (*attach) (name);
152 }
153