2929#include <ctype.h>
3030#include <sys/types.h>
3131#include "gen_code.h"
32+ #include "iio_common.h"
3233
3334#define MY_NAME "iio_attr"
3435
3839#define _strdup strdup
3940#endif
4041
41- enum backend {
42- LOCAL ,
43- XML ,
44- AUTO
45- };
46-
47- /*
48- * internal buffers need to be big enough for attributes
49- * coming back from the kernel. Because of virtual memory,
50- * only the amount of ram that is needed is used.
51- */
52- #define BUF_SIZE 16384
53-
54- static void * xmalloc (size_t n )
55- {
56- void * p = malloc (n );
57-
58- if (!p && n != 0 ) {
59- fprintf (stderr , MY_NAME
60- " fatal error: allocating %zu bytes failed\n" ,n );
61- exit (EXIT_FAILURE );
62- }
63-
64- return p ;
65- }
66-
6742static bool str_match (const char * haystack , char * needle , bool ignore )
6843{
6944 bool ret = false;
@@ -120,62 +95,11 @@ static bool str_match(const char * haystack, char * needle, bool ignore)
12095 return ret ;
12196}
12297
123- static struct iio_context * autodetect_context (bool gen_code )
124- {
125- struct iio_scan_context * scan_ctx ;
126- struct iio_context_info * * info ;
127- struct iio_context * ctx = NULL ;
128- unsigned int i ;
129- ssize_t ret ;
130-
131- scan_ctx = iio_create_scan_context (NULL , 0 );
132- if (!scan_ctx ) {
133- fprintf (stderr , "Unable to create scan context\n" );
134- return NULL ;
135- }
136-
137- ret = iio_scan_context_get_info_list (scan_ctx , & info );
138- if (ret < 0 ) {
139- char * err_str = xmalloc (BUF_SIZE );
140- iio_strerror (- ret , err_str , BUF_SIZE );
141- fprintf (stderr , "Scanning for IIO contexts failed: %s\n" , err_str );
142- free (err_str );
143- goto err_free_ctx ;
144- }
145-
146- if (ret == 0 ) {
147- printf ("No IIO context found.\n" );
148- goto err_free_info_list ;
149- }
150- if (ret == 1 ) {
151- printf ("Using auto-detected IIO context at URI \"%s\"\n" ,
152- iio_context_info_get_uri (info [0 ]));
153- ctx = iio_create_context_from_uri (iio_context_info_get_uri (info [0 ]));
154- if (gen_code )
155- gen_context (iio_context_info_get_uri (info [0 ]));
156- } else {
157- fprintf (stderr , "Multiple contexts found. Please select one using --uri:\n" );
158- for (i = 0 ; i < (size_t ) ret ; i ++ ) {
159- fprintf (stderr , "\t%u: %s [%s]\n" ,
160- i , iio_context_info_get_description (info [i ]),
161- iio_context_info_get_uri (info [i ]));
162- }
163- }
164-
165- err_free_info_list :
166- iio_context_info_list_free (info );
167- err_free_ctx :
168- iio_scan_context_destroy (scan_ctx );
169-
170- return ctx ;
171- }
172-
173-
17498static void dump_device_attributes (const struct iio_device * dev ,
17599 const char * attr , const char * wbuf , bool quiet )
176100{
177101 ssize_t ret ;
178- char * buf = xmalloc (BUF_SIZE );
102+ char * buf = xmalloc (BUF_SIZE , MY_NAME );
179103
180104 if (!wbuf || !quiet ) {
181105 if (!quiet )
@@ -213,7 +137,7 @@ static void dump_buffer_attributes(const struct iio_device *dev,
213137 const char * attr , const char * wbuf , bool quiet )
214138{
215139 ssize_t ret ;
216- char * buf = xmalloc (BUF_SIZE );
140+ char * buf = xmalloc (BUF_SIZE , MY_NAME );
217141
218142 if (!wbuf || !quiet ) {
219143 gen_function ("device_buffer" , "dev" , attr , NULL );
@@ -255,7 +179,7 @@ static void dump_debug_attributes(const struct iio_device *dev,
255179 const char * attr , const char * wbuf , bool quiet )
256180{
257181 ssize_t ret ;
258- char * buf = xmalloc (BUF_SIZE );
182+ char * buf = xmalloc (BUF_SIZE , MY_NAME );
259183
260184 if (!wbuf || !quiet ) {
261185 gen_function ("device_debug" , "dev" , attr , NULL );
@@ -297,7 +221,7 @@ static void dump_channel_attributes(const struct iio_device *dev,
297221 struct iio_channel * ch , const char * attr , const char * wbuf , bool quiet )
298222{
299223 ssize_t ret ;
300- char * buf = xmalloc (BUF_SIZE );
224+ char * buf = xmalloc (BUF_SIZE , MY_NAME );
301225 const char * type_name ;
302226
303227 if (!wbuf || !quiet ) {
@@ -422,7 +346,7 @@ int main(int argc, char **argv)
422346 int c , option_index = 0 ;
423347 int device_index = 0 , channel_index = 0 , attr_index = 0 ;
424348 const char * arg_uri = NULL , * gen_file = NULL ;
425- enum backend backend = LOCAL ;
349+ enum backend backend = IIO_LOCAL ;
426350 bool detect_context = false, search_device = false, ignore_case = false,
427351 search_channel = false, search_buffer = false, search_debug = false,
428352 search_context = false, input_only = false, output_only = false,
@@ -442,7 +366,7 @@ int main(int argc, char **argv)
442366 detect_context = true;
443367 break ;
444368 case 'u' :
445- backend = AUTO ;
369+ backend = IIO_AUTO ;
446370 arg_uri = optarg ;
447371 break ;
448372 /* Attribute type
@@ -623,16 +547,16 @@ int main(int argc, char **argv)
623547 }
624548
625549 if (detect_context )
626- ctx = autodetect_context (gen_code );
627- else if (backend == AUTO ) {
550+ ctx = autodetect_context (true, gen_code , MY_NAME );
551+ else if (backend == IIO_AUTO ) {
628552 ctx = iio_create_context_from_uri (arg_uri );
629553 gen_context (arg_uri );
630554 } else
631555 ctx = iio_create_default_context ();
632556
633557 if (!ctx ) {
634558 if (!detect_context ) {
635- char * buf = xmalloc (BUF_SIZE );
559+ char * buf = xmalloc (BUF_SIZE , MY_NAME );
636560
637561 iio_strerror (errno , buf , BUF_SIZE );
638562 fprintf (stderr , "Unable to create IIO context: %s\n" ,
@@ -659,7 +583,7 @@ int main(int argc, char **argv)
659583 gen_context_attr (key );
660584 }
661585 } else {
662- char * buf = xmalloc (BUF_SIZE );
586+ char * buf = xmalloc (BUF_SIZE , MY_NAME );
663587 iio_strerror (errno , buf , BUF_SIZE );
664588 fprintf (stderr , "Unable to get context attributes: %s (%zd)\n" ,
665589 buf , ret );
0 commit comments