summaryrefslogtreecommitdiff
path: root/cleopatre/application/spidinit/src/spidinit.c
blob: 05a775f38894e645be0e8cfacbbb3e59e2fef31b (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
/*
 * cleopatre/application/spidinit/src/spidinit.c
 *
 * (C) Copyright 2009 SPiDCOM Technologies
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "spidinit.h"

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

struct command {
  char lname[32];
  char sname;
  char desc[64];
  char use_arg;
  int (*func)(char*);
};

int do_all_commands(char *arg);
int do_update_img_desc(char *arg);

struct command cmd_list[] = {
    { "descupd", 'd', "update image descriptor", 0, &do_update_img_desc },
    { "all"    , 'a', "start all commands", 0, &do_all_commands } /* must be the last one */
};

static void print_usage(const char *cmd)
{
    int i;
    fprintf(stderr, "Usage : %s\n", cmd);
    for(i=0 ; i<ARRAY_SIZE(cmd_list) ; i++)
    {
        fprintf(stderr, "  --%s :   %s\n", cmd_list[i].lname, cmd_list[i].desc);
    }
}

int do_all_commands(char *arg)
{
    int i;
    int result;
    /* Loop for all commands except the last one : all */
    for(i=0 ; i<ARRAY_SIZE(cmd_list)-1 ; i++)
    {
        result = cmd_list[i].func(arg);
        if(result)
            return -1;
    }
    return 0;
}

int do_update_img_desc(char *arg)
{
    return update_img_desc();
}

int main(int argc, char **argv)
{
    int i, c, err;
    int opt_index = 0;
    char short_opts[(2*ARRAY_SIZE(cmd_list))+1];
    struct option long_opts[ARRAY_SIZE(cmd_list)];

    /* No option start all commands */
    if(argc <= 1)
        return do_all_commands(NULL);

    /* Fill allowed options */
    for(i=0 ; i<ARRAY_SIZE(cmd_list) ; i++)
    {
        short_opts[i] = cmd_list[i].sname;
        if(cmd_list[i].use_arg)
        {
            short_opts[i+1] = ':';
            short_opts[i+2] = '\0';
        }
        else
        {
            short_opts[i+1] = '\0';
        }
        long_opts[i].name = (const char*)cmd_list[i].lname;
        long_opts[i].has_arg = cmd_list[i].use_arg;
        long_opts[i].flag = NULL;
        long_opts[i].val = cmd_list[i].sname;
    }

    /* Apply options */
    while((c = getopt_long_only(argc, argv, short_opts, long_opts, &opt_index)) != -1)
    {
        switch(c)
        {
        case 'd' : /* update image descriptor */
            err = do_update_img_desc(optarg); break;
        case 'a' : /* start all functions */
            err = do_all_commands(optarg); break;
        default :
            print_usage(argv[0]);
            return 1;
        }
        if(err)
            return -1;
    }

    /* Check there's no arguments left */
    if (optind < argc)
    {
        printf("Unknown argument %s\n\n", argv[optind]);
        print_usage(argv[0]);
        return -1;
    }

    return 0;
}