~ubuntu-branches/ubuntu/feisty/sl-modem/feisty

« back to all changes in this revision

Viewing changes to modem/modem_cmdline.c

  • Committer: Bazaar Package Importer
  • Author(s): Eduard Bloch
  • Date: 2004-09-06 18:17:31 UTC
  • Revision ID: james.westby@ubuntu.com-20040906181731-si44puez4f5pzd8s
Tags: upstream-2.9.9
ImportĀ upstreamĀ versionĀ 2.9.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 *
 
4
 *    Copyright (c) 2002, Smart Link Ltd.
 
5
 *    All rights reserved.
 
6
 *
 
7
 *    Redistribution and use in source and binary forms, with or without
 
8
 *    modification, are permitted provided that the following conditions
 
9
 *    are met:
 
10
 *
 
11
 *        1. Redistributions of source code must retain the above copyright
 
12
 *           notice, this list of conditions and the following disclaimer.
 
13
 *        2. Redistributions in binary form must reproduce the above
 
14
 *           copyright notice, this list of conditions and the following
 
15
 *           disclaimer in the documentation and/or other materials provided
 
16
 *           with the distribution.
 
17
 *        3. Neither the name of the Smart Link Ltd. nor the names of its
 
18
 *           contributors may be used to endorse or promote products derived
 
19
 *           from this software without specific prior written permission.
 
20
 *
 
21
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
22
 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
23
 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
24
 *    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
25
 *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
26
 *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
27
 *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
28
 *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
29
 *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
30
 *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
31
 *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
32
 *
 
33
 */
 
34
 
 
35
/*
 
36
 *
 
37
 *    modem_cmdline.c  --  simple command line processor,
 
38
 *                         define config parameters.
 
39
 *
 
40
 *    Author: Sasha K (sashak@smlink.com)
 
41
 *
 
42
 *
 
43
 */
 
44
 
 
45
#include <unistd.h>
 
46
#include <stdlib.h>
 
47
#include <stdio.h>
 
48
#include <string.h>
 
49
#include <errno.h>
 
50
#include <ctype.h>
 
51
 
 
52
#include <modem.h>
 
53
#include <modem_homolog.h>
 
54
#include <modem_debug.h>
 
55
 
 
56
#define PR_INFO(fmt...) fprintf(stderr, fmt )
 
57
 
 
58
 
 
59
/* global config data */
 
60
 
 
61
/* modem.c */
 
62
extern const char *modem_default_country;
 
63
 
 
64
/* modem_debug.c */
 
65
extern unsigned int modem_debug_level;
 
66
extern unsigned int modem_debug_logging;
 
67
 
 
68
/* config parameters */
 
69
const char *modem_dev_name = "/dev/slamr0";
 
70
const char *modem_alsa_dev_name = "hw:1";
 
71
unsigned int need_realtime = 1;
 
72
unsigned int use_alsa = 0;
 
73
const char *modem_group = "uucp";
 
74
mode_t modem_perm  = 0660;
 
75
 
 
76
 
 
77
enum {
 
78
        OPT_HELP = 0,
 
79
        OPT_USAGE,
 
80
        OPT_VERSION,
 
81
        OPT_COUNTRY,
 
82
        OPT_COUNTRYLIST,
 
83
        OPT_ALSA,
 
84
        OPT_GROUP,
 
85
        OPT_PERM,
 
86
        OPT_USER,
 
87
        OPT_DEBUG,
 
88
        OPT_LOG,
 
89
        OPT_LAST
 
90
};
 
91
 
 
92
static struct opt {
 
93
        int  ch;
 
94
        const char *name;
 
95
        const char *desc;
 
96
        enum {NONE=0,MANDATORY,OPTIONAL} arg;
 
97
        enum {INTEGER,STRING} arg_type;
 
98
        const char *arg_val;
 
99
        unsigned found;
 
100
} opt_list[] = {
 
101
        {'h',"help","this usage"},
 
102
        {'u',"usage","this usage"},
 
103
        {'v',"version","show version and exit"},
 
104
        {'c',"country","default modem country name",MANDATORY,STRING,"USA"},
 
105
        { 0 ,"countrylist","show list of supported countries"},
 
106
        {'a',"alsa","ALSA mode (see README for howto)"},
 
107
        {'g',"group","Modem TTY group",MANDATORY,STRING,"uucp"},
 
108
        {'p',"perm","Modem TTY permission",MANDATORY,INTEGER,"0660"},
 
109
        {'n',"nortpriority","run with regular priority"},
 
110
        {'d',"debug","debug level",OPTIONAL,INTEGER,"0"},
 
111
        {'l',"log","logging mode",OPTIONAL,INTEGER,"5"},
 
112
        {}
 
113
};
 
114
 
 
115
 
 
116
 
 
117
static void usage(const char *prog_name)
 
118
{
 
119
        struct opt *opt;
 
120
        PR_INFO("Usage: %s [option...] <device>\n"
 
121
                "Where 'device' is name of modem device (default `%s')\n"
 
122
                "  and 'option' may be:\n", prog_name, modem_dev_name);
 
123
        for (opt = opt_list ; opt->name ; opt++ ) {
 
124
                int n = 0;
 
125
                if(opt->ch)
 
126
                        n = PR_INFO("  -%c, ",opt->ch);
 
127
                else
 
128
                        n = PR_INFO("      ");
 
129
                n += PR_INFO("--%s%s ",opt->name,opt->arg?"=VAL":"");
 
130
                n += PR_INFO("%*s%s",24-n,"",opt->desc);
 
131
                if(opt->arg) {
 
132
                        n+= PR_INFO(" (default `%s')",opt->arg_val);
 
133
                }
 
134
                PR_INFO("\n");
 
135
        }
 
136
        exit(1);
 
137
}
 
138
 
 
139
 
 
140
static void opt_parse(int argc, char *argv[])
 
141
{
 
142
        const char *prog_name;
 
143
        const char *dev_name = NULL;
 
144
        struct opt *opt;
 
145
        char *p, *arg;
 
146
        int found, i;
 
147
        
 
148
        prog_name = argv[0];
 
149
 
 
150
        for( i = 1 ; i < argc ; i++ ) {
 
151
                p = argv[i];
 
152
                if(*p != '-') {
 
153
                        if(dev_name)
 
154
                                usage(prog_name);
 
155
                        dev_name = p;
 
156
                        continue;
 
157
                }
 
158
                found = 0;
 
159
                arg = NULL;
 
160
                do { p++; } while( *p == '-' );
 
161
                for (opt = opt_list ; opt->name ; opt++ ) {
 
162
                        if(!strncmp(p,opt->name,strlen(opt->name)) ||
 
163
                           (opt->ch && *p == opt->ch) ) {
 
164
                                char *q;
 
165
                                if(!strncmp(p,opt->name,strlen(opt->name)))
 
166
                                        q = p + strlen(opt->name);
 
167
                                else
 
168
                                        q = p + 1;
 
169
                                if( *q == '\0' )
 
170
                                        found = 1;
 
171
                                else if (*q == '=' && opt->arg ) {
 
172
                                        arg = q + 1;
 
173
                                        found = 1;
 
174
                                }
 
175
                                else if(isdigit(*q) && opt->arg ) {
 
176
                                        arg = q;
 
177
                                        found = 1;
 
178
                                }
 
179
                        }
 
180
 
 
181
                        if ( !found )
 
182
                                continue;
 
183
                        if ( arg && !opt->arg )
 
184
                                usage(prog_name);
 
185
 
 
186
                        if (!arg && opt->arg &&
 
187
                            (i < argc - 1 && *argv[i+1] != '-') &&
 
188
                            (!opt->arg_type == INTEGER || isdigit(*argv[i+1])))
 
189
                                arg = argv[++i];
 
190
 
 
191
                        if(!arg && opt->arg == MANDATORY)
 
192
                                usage(prog_name);
 
193
 
 
194
                        opt->found++;
 
195
                        if(opt->arg && arg)
 
196
                                opt->arg_val = arg;
 
197
                        break;
 
198
                }
 
199
                if(!found)
 
200
                        usage(prog_name);
 
201
        }
 
202
 
 
203
        if(dev_name)
 
204
                modem_dev_name = dev_name;
 
205
}
 
206
 
 
207
 
 
208
 
 
209
void modem_cmdline(int argc, char *argv[])
 
210
{
 
211
        const char *prog_name = argv[0];
 
212
        int val;
 
213
 
 
214
        opt_parse(argc,argv);
 
215
 
 
216
        if(opt_list[OPT_HELP].found ||
 
217
           opt_list[OPT_USAGE].found )
 
218
                usage(prog_name);
 
219
 
 
220
        if(opt_list[OPT_VERSION].found) {
 
221
                extern void modem_print_version();
 
222
                modem_print_version();
 
223
                exit(0);
 
224
        }
 
225
 
 
226
        if(opt_list[OPT_COUNTRYLIST].found) {
 
227
                const struct homolog_set *s ;
 
228
                for( s = homolog_set; s->name ; s++ ) {
 
229
                        PR_INFO("%02x: %s\n",s->id,s->name);
 
230
                }
 
231
                exit(0);
 
232
        }
 
233
        if(opt_list[OPT_COUNTRY].found)
 
234
                modem_default_country = opt_list[OPT_COUNTRY].arg_val;
 
235
        if(opt_list[OPT_ALSA].found) {
 
236
#ifndef SUPPORT_ALSA
 
237
                PR_INFO("ALSA support is not compiled in (see README for howto).\n");
 
238
                exit(1);
 
239
#endif
 
240
                modem_dev_name = modem_alsa_dev_name;
 
241
                use_alsa = 1;
 
242
        }
 
243
        if(opt_list[OPT_GROUP].found)
 
244
                modem_group = opt_list[OPT_GROUP].arg_val;
 
245
        if(opt_list[OPT_PERM].found) {
 
246
                val = strtol(opt_list[OPT_PERM].arg_val,NULL,8);
 
247
                if (val <= 0)
 
248
                        usage(prog_name);
 
249
                modem_perm = val;
 
250
        }
 
251
        if(opt_list[OPT_USER].found)
 
252
                need_realtime = 0;
 
253
        if(opt_list[OPT_DEBUG].found) {
 
254
                modem_debug_level = 1 ;
 
255
                if(opt_list[OPT_DEBUG].arg_val &&
 
256
                   (val= strtol(opt_list[OPT_DEBUG].arg_val,NULL,0)) > 0 )
 
257
                        modem_debug_level = val;
 
258
        }
 
259
        if(opt_list[OPT_LOG].found) {
 
260
                modem_debug_logging = 5;
 
261
                if(opt_list[OPT_LOG].arg_val &&
 
262
                   (val= strtol(opt_list[OPT_LOG].arg_val,NULL,0)) > 0 )
 
263
                        modem_debug_logging = val;
 
264
        }
 
265
}
 
266