1/* -*- c -*- */
2/*****************************************************************************/
3/*  LibreDWG - free implementation of the DWG file format                    */
4/*                                                                           */
5/*  Copyright (C) 2018 Free Software Foundation, Inc.                        */
6/*                                                                           */
7/*  This library is free software, licensed under the terms of the GNU       */
8/*  General Public License as published by the Free Software Foundation,     */
9/*  either version 3 of the License, or (at your option) any later version.  */
10/*  You should have received a copy of the GNU General Public License        */
11/*  along with this program.  If not, see <http://www.gnu.org/licenses/>.    */
12/*****************************************************************************/
13
14/*
15 * common.c: common programs functions. included, not linked.
16 * written by Reini Urban
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23/* returns number of consumed args */
24int verbosity(int argc, char **argv, int i, unsigned int *opts)
25{
26  unsigned int loglevel = 2;
27  char log_str[4];
28  if (!strcmp(argv[i], "--verbose") || // --verbose 2 or --verbose
29      !strcmp(argv[i], "-v")) // -v 0 or -v
30    {
31      int num_args;
32      if (argc > i)
33        {
34          long l = strtol(argv[i+1], NULL, 10);
35          if (l < 0 || l > 10)
36            {
37              printf("Invalid verbosity %s\n", argv[i+1]); // -vbla
38              exit(help());
39            }
40          loglevel = (unsigned int)l;
41          num_args = 2;
42        }
43      else
44        {
45          num_args = 1;
46          loglevel = 2;
47        }
48      *opts = loglevel;
49      sprintf(log_str, "%d", loglevel);
50#if defined(USE_TRACING) && defined(HAVE_SETENV)
51      setenv("LIBREDWG_TRACE", log_str, 1);
52#endif
53      return num_args;
54    }
55
56  if (!strncmp(argv[i], "-v", 2)) // -v0
57    {
58      char *opt = argv[i];
59      long l = strtol(&opt[2], NULL, 10);
60      if (l < 0 || l > 10)
61        {
62          printf("Invalid verbosity %s\n", opt); // -vbla
63          exit(help());
64        }
65      loglevel = (unsigned int)l;
66      *opts = loglevel;
67      sprintf(log_str, "%d", loglevel);
68#if defined(USE_TRACING) && defined(HAVE_SETENV)
69      setenv("LIBREDWG_TRACE", log_str, 1);
70#endif
71      return 1;
72    }
73
74  printf("Invalid verbosity %s\n", argv[i]); // -vbla
75  exit(1);
76}
77
78