# Patch: sane_init() may fail

Petter Reinholdtsen (pere@hungry.com)
Wed, 28 Apr 1999 00:32:11 +0200

- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
- Next message: Petter Reinholdtsen: "Patch: Autogenerate lsm entry"
- Previous message: Petter Reinholdtsen: "Re: May sane_init fail?"

Here is my patch to document and implement the changes required to let
sane_init() fail. It is relative to SANE 1.0.1.

diff -ur sane-1.0.1/ChangeLog sane-1.0.1-pere/ChangeLog
--- sane-1.0.1/ChangeLog Mon Apr 19 18:21:33 1999
+++ sane-1.0.1-pere/ChangeLog Wed Apr 28 00:28:01 1999
@@ -1,3 +1,10 @@
+1999-04-28 Petter Reinholdtsen <pere@td.org.uit.no>
+
+ * doc/sane.tex frontend/scanimage.c frontend/xcam.c
+ frontend/xscanimage.c: Specified valid return values for
+ sane_init(), and made sure all our frontends checks for the
+ correct return value before using other methods in the backend.
+
1999-04-19 David Mosberger-Tang <David.Mosberger@acm.org>
* Version 1.0.1 released.
diff -ur sane-1.0.1/doc/sane.tex sane-1.0.1-pere/doc/sane.tex
--- sane-1.0.1/doc/sane.tex Sat Apr 3 23:16:07 1999
+++ sane-1.0.1-pere/doc/sane.tex Wed Apr 28 00:17:31 1999
@@ -1052,9 +1052,11 @@
This function must be called before any other SANE function can be
called. The behavior of a SANE backend is undefined if this function
-is not called first. The version code of the backend is returned in
-the value pointed to by \code{version\_code}. If that pointer is
-\code{NULL}, no version code is returned.
+is not called first. The behavior of a backend is also undefined if
+this function returns anything other then \code{SANE\_STATUS\_GOOD}.
+The version code of the backend is returned in the value pointed to by
+\code{version\_code}. If that pointer is \code{NULL}, no version code
+is returned.
Argument \code{authorize} is either a pointer to a function that is
invoked when the backend requires authentication for a specific
resource or \code{NULL} if the frontend does not support
@@ -1064,6 +1066,16 @@
SANE_Status sane_init (SANE_Int * version_code,
SANE_Authorization_Callback authorize);
\end{verbatim}
+\end{quote}
+
+This function may fail with one of the following status codes.
+\begin{quote}
+\begin{description}
+\item[\code{SANE\_STATUS\_UNSUPPORTED}:] The backend is not supported on
+ this machines current configuration.
+\item[\code{SANE\_STATUS\_NO\_MEM}:] An insufficent amount of memory
+ is available to complete. Try later when more memory is available.
+\end{description}
\end{quote}
The authorization function may be called by a backend in response to
diff -ur sane-1.0.1/frontend/scanimage.c sane-1.0.1-pere/frontend/scanimage.c
--- sane-1.0.1/frontend/scanimage.c Fri Mar 5 07:13:49 1999
+++ sane-1.0.1-pere/frontend/scanimage.c Wed Apr 28 00:28:44 1999
@@ -73,6 +73,7 @@
static SANE_Handle device;
static int verbose;
static int test;
+static int list;
static int help;
static const char * prog_name;
static SANE_Option_Descriptor window_option[2];
@@ -1041,6 +1042,29 @@
free (image.data);
}
+void
+list_devices(void)
+{
+ const SANE_Device ** device_list;
+ SANE_Status status;
+ int i;
+
+ status = sane_get_devices (&device_list, SANE_FALSE);
+ if (status != SANE_STATUS_GOOD)
+ {
+ fprintf (stderr, "%s: sane_get_devices() failed: %s\n",
+ prog_name, sane_strstatus (status));
+ return;
+ }
+
+ for (i = 0; device_list[i]; ++i)
+ {
+ printf ("device `%s' is a %s %s %s\n",
+ device_list[i]->name, device_list[i]->vendor,
+ device_list[i]->model, device_list[i]->type);
+ }
+}
+
int
main (int argc, char **argv)
{
@@ -1060,8 +1084,6 @@
else
prog_name = argv[0];
- sane_init (0, 0);
-
/* make a first pass through the options with error printing and argument
permutation disabled: */
opterr = 0;
@@ -1079,27 +1101,7 @@
case 'h': help = 1; break;
case 'v': ++verbose; break;
case 'T': test= 1; break;
- case 'L':
- {
- int i;
-
- status = sane_get_devices (&device_list, SANE_FALSE);
- if (status != SANE_STATUS_GOOD)
- {
- fprintf (stderr, "%s: sane_get_devices() failed: %s\n",
- prog_name, sane_strstatus (status));
- exit (1);
- }
-
- for (i = 0; device_list[i]; ++i)
- {
- printf ("device `%s' is a %s %s %s\n",
- device_list[i]->name, device_list[i]->vendor,
- device_list[i]->model, device_list[i]->type);
- }
- exit (0);
- }
-
+ case 'L': list = 1; break;
case 'V':
printf ("scanimage (%s) %s\n", PACKAGE, VERSION);
exit (0);
@@ -1122,6 +1124,18 @@
-v, --verbose give even more status messages\n\
-V, --version print version information\n",
prog_name);
+
+ if (SANE_STATUS_GOOD != sane_init (NULL, NULL))
+ {
+ fprintf(stderr,"sane_init() failed. Unable to do anything. Exiting\n");
+ return(1);
+ }
+
+ if (list)
+ {
+ list_devices();
+ return(0);
+ }
if (!devname)
{
diff -ur sane-1.0.1/frontend/xcam.c sane-1.0.1-pere/frontend/xcam.c
--- sane-1.0.1/frontend/xcam.c Sat Apr 4 06:39:20 1998
+++ sane-1.0.1-pere/frontend/xcam.c Tue Apr 27 23:54:56 1999
@@ -894,7 +894,11 @@
/* turn on by default as we don't support graphical geometry selection */
preferences.advanced = 1;
- sane_init (NULL, 0);
+ if (SANE_STATUS_GOOD != sane_init (NULL, NULL))
+ {
+ fprintf(stderr,"sane_init() failed. Unable to do anything. Exiting\n");
+ return(1);
+ }
gdk_set_show_events (0);
gtk_init (&argc, &argv);
diff -ur sane-1.0.1/frontend/xscanimage.c sane-1.0.1-pere/frontend/xscanimage.c
--- sane-1.0.1/frontend/xscanimage.c Sat Apr 3 06:07:57 1999
+++ sane-1.0.1-pere/frontend/xscanimage.c Tue Apr 27 23:56:42 1999
@@ -257,7 +257,12 @@
nargs, nreturn_vals,
args, return_vals);
- sane_init (0, 0);
+ if (SANE_STATUS_GOOD != sane_init (NULL, NULL))
+ {
+ fprintf(stderr,"sane_init() failed. Unable to do anything. Exiting\n");
+ return(1);
+ }
+
sane_get_devices (&devlist, SANE_FALSE);
for (i = 0; devlist[i]; ++i)

    --
    ##>  Petter Reinholdtsen <##    | pere@td.org.uit.no
    http://www.hungry.com/~pere/ O- | Go Mozilla, go! Go!

    --
    Source code, list archive, and docs: http://www.mostang.com/sane/
    To unsubscribe: echo unsubscribe sane-devel | mail majordomo@mostang.com

     Next message: Petter Reinholdtsen: "Patch: Autogenerate lsm entry"
     Previous message: Petter Reinholdtsen: "Re: May sane_init fail?"