File: gp-params.c

package info (click to toggle)
gphoto2 2.4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,416 kB
  • sloc: sh: 10,890; ansic: 6,219; perl: 89; makefile: 70; sed: 16
file content (557 lines) | stat: -rw-r--r-- 15,112 bytes parent folder | download
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/* gp-params.c
 *
 * Copyright  2002 Lutz Mller <lutz@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include "gp-params.h"
#include "i18n.h"

/* This needs to disappear. */
#include "globals.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#include "spawnve.h"

#ifndef MAX
#define MAX(x, y) (((x)>(y))?(x):(y))
#endif
#ifndef MIN
#define MIN(x, y) (((x)<(y))?(x):(y))
#endif


#ifdef __GNUC__
#define __unused__ __attribute__((unused))
#else
#define __unused__
#endif


static void
#ifdef __GNUC__
__attribute__((__format__(printf,2,0)))
#endif
ctx_status_func (GPContext __unused__ *context, const char *format, va_list args,
                 void __unused__ *data)
{
        vfprintf (stderr, format, args);
        fprintf  (stderr, "\n");
        fflush   (stderr);
}

static void
#ifdef __GNUC__
__attribute__((__format__(printf,2,0)))
#endif
ctx_error_func (GPContext __unused__ *context, const char *format, va_list args,
                void __unused__ *data)
{
        fprintf  (stderr, "\n");
        fprintf  (stderr, _("*** Error ***              \n"));
        vfprintf (stderr, format, args);
        fprintf  (stderr, "\n");
        fflush   (stderr);
}

#define MAX_PROGRESS_STATES 16
#define MAX_MSG_LEN 1024

static struct {
        char message[MAX_MSG_LEN + 1];
        float target;
        unsigned long int count;
        struct {
                float  current;
                time_t time, left;
        } last;
} progress_states[MAX_PROGRESS_STATES];

static unsigned int
#ifdef __GNUC__
__attribute__((__format__(printf,3,0)))
#endif
ctx_progress_start_func (GPContext __unused__ *context, float target,
                         const char *format, va_list args, void *data)
{
	GPParams *p = data;
        unsigned int id, len;
        static unsigned char initialized = 0;

        if (!initialized) {
                memset (progress_states, 0, sizeof (progress_states));
                initialized = 1;
        }

	if (p->flags & FLAGS_QUIET)
		return 0;

        /*
         * If the message is too long, we will shorten it. If we have less
         * than 4 cols available, we won't display any message.
         */
        len = (p->cols * 0.5 < 4) ? 0 : MIN (p->cols * 0.5, MAX_MSG_LEN);

        /* Remember message and target. */
        for (id = 0; id < MAX_PROGRESS_STATES; id++)
                if (!progress_states[id].target)
                        break;
        if (id == MAX_PROGRESS_STATES)
                id--;
        progress_states[id].target = target;
        vsnprintf (progress_states[id].message, len + 1, format, args);
        progress_states[id].count = 0;
        progress_states[id].last.time = time (NULL);
        progress_states[id].last.current = progress_states[id].last.left = 0.;

        /* If message is too long, shorten it. */
        if (progress_states[id].message[len - 1]) {
                progress_states[id].message[len - 1] = '\0';
                progress_states[id].message[len - 2] = '.';
                progress_states[id].message[len - 3] = '.';
                progress_states[id].message[len - 4] = '.';
        }

        return (id);
}

static void
ctx_progress_update_func (GPContext __unused__ *context, unsigned int id,
                          float current, void *data)
{
	GPParams *p = data;
        static const char spinner[] = "\\|/-";
        unsigned int i, width, pos;
        float rate;
        char remaining[10], buf[4];
        time_t sec = 0;

        /* Guard against buggy camera drivers */
        if (id >= MAX_PROGRESS_STATES || ((int)id) < 0)
                return;

	if (p->flags & FLAGS_QUIET)
		return;

        /* How much time until completion? */
        if ((time (NULL) - progress_states[id].last.time > 0) &&
            (current - progress_states[id].last.current > 0)) {
                rate = (time (NULL) - progress_states[id].last.time) /
                       (current - progress_states[id].last.current);
                sec = (MAX (0, progress_states[id].target - current)) * rate;
                if (progress_states[id].last.left) {
                        sec += progress_states[id].last.left;
                        sec /= 2.;
                }
                progress_states[id].last.time = time (NULL);
                progress_states[id].last.current = current;
                progress_states[id].last.left = sec;
        } else
                sec = progress_states[id].last.left;
        memset (remaining, 0, sizeof (remaining));
        if ((int) sec >= 3600) {
                snprintf (buf, sizeof (buf), "%2ih", (int) sec / 3600);
                sec -= ((int) (sec / 3600) * 3600);
                strncat (remaining, buf, sizeof (remaining) - strlen (remaining) - 1);
        }
        if ((int) sec >= 60) {
                snprintf (buf, sizeof (buf), "%2im", (int) sec / 60);
                sec -= ((int) (sec / 60) * 60);
                strncat (remaining, buf, sizeof (remaining) - strlen (remaining) - 1);
        }
        if ((int) sec) {
                snprintf (buf, sizeof (buf), "%2is", (int) sec);
                strncat (remaining, buf, sizeof (remaining) - strlen (remaining) - 1);
        }

        /* Determine the width of the progress bar and the current position */
        width = MAX (0, (int) (p->cols -
			       strlen (progress_states[id].message) - 20));
        pos = MIN (width, (MIN (current / progress_states[id].target, 100.) * width) + 0.5);

        /* Print the progress bar */
        printf ("%s |", progress_states[id].message);
        for (i = 0; i < width; i++)
                putchar ((i < pos) ? '-' : ' ');
        if (pos == width)
	        putchar ('|');
        else
                putchar (spinner[progress_states[id].count & 0x03]);
        progress_states[id].count++;

        printf (" %5.1f%% %9.9s\r",
		current / progress_states[id].target * 100., remaining);
        fflush (stdout);
}

static void
ctx_progress_stop_func (GPContext __unused__ *context, unsigned int id, 
			void *data)
{
	GPParams *p = data;
        unsigned int i;

        /* Guard against buggy camera drivers */
        if (id >= MAX_PROGRESS_STATES || ((int)id) < 0)
                return;

	if (p->flags & FLAGS_QUIET)
		return;

        /* Clear the progress bar. */
        for (i = 0; i < p->cols; i++)
                putchar (' ');
        putchar ('\r');
        fflush (stdout);

        progress_states[id].target = 0.;
}

static GPContextFeedback
ctx_cancel_func (GPContext __unused__ *context, void __unused__ *data)
{
        if (glob_cancel) {
                return (GP_CONTEXT_FEEDBACK_CANCEL);
                glob_cancel = 0;
        } else
                return (GP_CONTEXT_FEEDBACK_OK);
}

static void
#ifdef __GNUC__
__attribute__((__format__(printf,2,0)))
#endif
ctx_message_func (GPContext __unused__ *context, const char *format, 
		  va_list args, void __unused__ *data)
{
        int c;

        vprintf (format, args);
        putchar ('\n');

        /* Only ask for confirmation if the user can give it. */
        if (isatty (STDOUT_FILENO) && isatty (STDIN_FILENO)) {
                printf (_("Press any key to continue.\n"));
                fflush (stdout);
                c = fgetc (stdin);
        } else
                fflush (stdout);
}


/** gp_params_init
 * @param envp: The third char ** parameter of the main() function
 */

void
gp_params_init (GPParams *p, char **envp)
{
	if (!p)
		return;

	memset (p, 0, sizeof (GPParams));

	p->folder = strdup ("/");
	if (!p->folder) {
		fprintf (stderr, _("Not enough memory."));
		fputc ('\n', stderr);
		exit (1);
	}

	gp_camera_new (&p->camera);

	p->cols = 79;
	p->flags = FLAGS_RECURSE;

	/* Create a context. Report progress only if users will see it. */
	p->context = gp_context_new ();
	gp_context_set_cancel_func    (p->context, ctx_cancel_func,  p);
	gp_context_set_error_func     (p->context, ctx_error_func,   p);
	gp_context_set_status_func    (p->context, ctx_status_func,  p);
	gp_context_set_message_func   (p->context, ctx_message_func, p);
	if (isatty (STDOUT_FILENO))
		gp_context_set_progress_funcs (p->context,
			ctx_progress_start_func, ctx_progress_update_func,
			ctx_progress_stop_func, p);

	p->_abilities_list = NULL;

	p->debug_func_id = -1;

	p->envp = envp;
}


CameraAbilitiesList *
gp_params_abilities_list (GPParams *p)
{
	/* If p == NULL, the behaviour of this function is as undefined as
	 * the expression p->abilities_list would have been. */
	if (p->_abilities_list == NULL) {
		gp_abilities_list_new (&p->_abilities_list);
		gp_abilities_list_load (p->_abilities_list, p->context);
	}
	return p->_abilities_list;
}


void
gp_params_exit (GPParams *p)
{
	if (!p)
		return;

	if (p->_abilities_list)
		gp_abilities_list_free (p->_abilities_list);
	if (p->camera)
		gp_camera_unref (p->camera);
	if (p->folder)
		free (p->folder);
	if (p->filename)
		free (p->filename);
	if (p->context)
		gp_context_unref (p->context);
	if (p->hook_script)
		free (p->hook_script);
	if (p->portinfo_list)
		gp_port_info_list_free (p->portinfo_list);
	memset (p, 0, sizeof (GPParams));
}


/* If CALL_VIA_SYSTEM is defined, use insecure system(3) instead of execve(2) */
/* #define CALL_VIA_SYSTEM */

static int
internal_run_hook(const char *const hook_script, 
		  const char *const action, const char *const argument,
		  char **envp);


int
gp_params_run_hook (GPParams *params, const char *action, const char *argument)
{
	/* printf("gp_params_run_hook(params, \"%s\", \"%s\")\n", 
	   action, argument);
	*/
	if (params->hook_script == NULL) {
		return 0;
	}
	return internal_run_hook(params->hook_script,
				 action, argument,
				 params->envp);
}


#ifdef CALL_VIA_SYSTEM
static void
internal_putenv(const char *const varname, const char *const value)
{
	if (NULL != varname) {
		if (NULL != value) {
			const size_t varname_size = strlen(varname);
			const size_t value_size = strlen(value);
			/* '=' and '\0' */
			const size_t buffer_size = varname_size + value_size + 2;
			char buffer[buffer_size];
			strcpy(buffer, varname);
			strcat(buffer, "=");
			strcat(buffer, value);
			printf("putenv(\"%s\")\n", buffer);
			if (0 != putenv(buffer)) {
				printf("putenv error\n");
			}
		} else {
			/* clear variable */
			if (unsetenv(varname)) {
				int my_errno = errno;
				fprintf(stderr, "unsetenv(\"%s\"): %s", 
					varname, strerror(my_errno));
			}
		}
	}
	printf("%% %s=%s\n", varname, getenv(varname));
}
#endif


#ifndef CALL_VIA_SYSTEM

#define ASSERT(cond)					\
  do {							\
    if (!(cond)) {					\
      fprintf(stderr, "%s:%d: Assertion failed: %s\n",	\
	      __FILE__, __LINE__, #cond);		\
      exit(13);						\
    }							\
  } while(0)


static char *
alloc_envar(const char *varname, const char *value)
{
  const size_t varname_size = strlen(varname);
  const size_t value_size = strlen(value);
  const size_t buf_size = varname_size + 1 + value_size + 1;
  char *result_buf = malloc(buf_size);

  if (!result_buf) return NULL;
  strcpy(result_buf, varname);
  strcat(result_buf, "=");
  strcat(result_buf, value);
  return result_buf;
}
#endif


static int
internal_run_hook(const char *const hook_script, 
		  const char *const action, const char *const argument,
		  char **envp)
{
#ifdef CALL_VIA_SYSTEM
	int retcode;

	/* run hook using system(3) */
	internal_putenv("ACTION", action);
	internal_putenv("ARGUMENT", argument);
	
	retcode = system(hook_script);
	if (retcode != 0) {
		fprintf(stderr, "Hook script returned error code %d (0x%x)\n",
			retcode, retcode);
		return 1;
	}
	return 0;
#else
	/* spawnve() based implementation of internal_run_hook()
	 *
	 * Most of the code here creates and destructs the
	 * char *child_argv[] and char *child_envp[] to be passed to 
	 * spawnve() and thus execve().
	 *
	 * Error handling is simple:
	 *  * If malloc() or calloc() fail, abort the whole program.
	 */

	/* A note on program memory layout:
	 *
	 * child_argv and child_envp MUST be in writable memory, so we
	 * malloc() them.
	 */
	
	char *my_hook_script = strdup(hook_script);
	unsigned int i;

	/* run hook using execve(2) */
	char **child_argv = calloc(2, sizeof(child_argv[0]));

	/* envars not to copy */
	const char *const varlist[] = {
		"ACTION", "ARGUMENT", NULL
	};

	/* environment variables for child process, and index going through them */
	char **child_envp;
	unsigned int envi = 0;

	int retcode;
	
	/* count number of environment variables currently set */
	unsigned int envar_count;
	for (envar_count=0; envp[envar_count] != NULL; envar_count++) {
		/* printf("%3d: \"%s\"\n", envar_count, envp[envar_count]); */
	}

	ASSERT(my_hook_script != NULL);
	child_argv[0] = my_hook_script;

	/* Initialize environment. Start with newly defined vars, then copy
	 * all the existing ones. calloc() does the initialization with NULL.
	 * Total amount of char* is
	 *     number of existing envars (envar_count)
	 *   + max number of new envars (2)
	 *   + NULL list terminator (1)
	 */
	child_envp = calloc(envar_count+((sizeof(varlist)/sizeof(varlist[0]))-1)+1, 
			    sizeof(child_envp[0]));
	ASSERT(child_envp != NULL);

	/* own envars */
	if (NULL != action) {
		char *envar = alloc_envar("ACTION", action);
		ASSERT(envar != NULL);
		child_envp[envi++] = envar;
	}
	if (NULL != argument) {
		char *envar = alloc_envar("ARGUMENT", argument);
		ASSERT(envar != NULL);
		child_envp[envi++] = envar;
	}
	
	/* copy envars except for those in varlist */
	for (i=0; i<envar_count; i++) {
		int skip = 0;
		unsigned int n;
		for (n=0; varlist[n] != NULL; n++) {
			const char *varname = varlist[n];
			const char *start = strstr(envp[i], varname);
			if ((envp[i] == start) &&  (envp[i][strlen(varname)] == '=')) {
				skip = 1;
				break;
			}
		}
		if (!skip) {
			child_envp[envi++] = strdup(envp[i]);
		}
	}
	
	/* Actually run the hook script */
	retcode = spawnve(hook_script, child_argv, child_envp);
		
	/* Free all memory */
	for (i=0; child_envp[i] != NULL; i++) {
		free(child_envp[i]);
	}
	free(child_envp);
	for (i=0; child_argv[i] != NULL; i++) {
		free(child_argv[i]);
	}
	free(child_argv);

	/* And finally return to caller */
	if (retcode != 0) {
		fprintf(stderr, "Hook script returned error code %d (0x%x)\n",
			retcode, retcode);
		return 1;
	}
	return 0;
#endif
}


/*
 * Local Variables:
 * c-file-style:"linux"
 * indent-tabs-mode:t
 * End:
 */