summaryrefslogtreecommitdiff
path: root/src/embim/embim-context.c
blob: 66d82273a03bd32a62495f863d54e8e01ce2be87 (plain)
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * embim -- Command line interface to control MBIM devices
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) 2015 Aleksander Morgado <aleksander@aleksander.es>
 */

#include "config.h"

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

#include "embim-context.h"

/******************************************************************************/

static void
print_help (void)
{
    printf ("\n"
            "Usage: " PROGRAM_NAME " [options]\n"
            "\n"
            "Application options:\n"
            "  -d, --device=[PATH]   Specify device path.\n"
            "  --noop                Don't run any command.\n"
            "  -h, --help            Show help.\n"
            "  -V, --version         Show version.\n"
            "\n");
}

static void
print_version (void)
{
    printf ("\n"
            PROGRAM_NAME " " PROGRAM_VERSION
            "\n"
            "Copyright (2015) Aleksander Morgado <aleksander@aleksander.es>\n"
            "\n"
            "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n"
            "This is free software: you are free to change and redistribute it.\n"
            "There is NO WARRANTY, to the extent permitted by law.\n"
            "\n");
}

/******************************************************************************/

static char *device_path;

const char *
embim_context_get_device (void)
{
    return device_path;
}

/******************************************************************************/

static bool noop;

bool
embim_context_get_noop (void)
{
    return noop;
}

/******************************************************************************/

int
embim_context_init (int *argc, char ***argv)
{
    static const struct option longopts[] = {
        { "device",  required_argument, 0, 'd' },
        { "noop",    no_argument,       0, '1' },
        { "version", no_argument,       0, 'V' },
        { "help",    no_argument,       0, 'h' },
        { 0,         0,                 0, 0   }
    };
    int idx;
    int iarg = 0;

    /* turn off getopt error message */
    opterr = 1;
    while (iarg != -1) {
        iarg = getopt_long (*argc, *argv, "d:hV", longopts, &idx);
        switch (iarg) {
        case '1':
            noop = true;
            break;
        case 'd':
            if (device_path) {
                fprintf (stderr, "error: device path redefined.\n");
                return -1;
            }
            device_path = strdup (optarg);
            break;
        case 'h':
            print_help ();
            exit (0);
        case 'v':
            print_version ();
            exit (0);
        }
    }

    if (!device_path) {
        fprintf (stderr, "error: device path not given.\n");
        return -2;
    }

    return 0;
}

void
embim_context_shutdown (void)
{
    free (device_path);
}