File: modem_cmdline.c

package info (click to toggle)
sl-modem 2.9.11~20110321-11
  • links: PTS, VCS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd
  • size: 3,448 kB
  • ctags: 2,972
  • sloc: ansic: 11,002; sh: 809; makefile: 232; python: 40; perl: 11
file content (287 lines) | stat: -rw-r--r-- 7,364 bytes parent folder | download | duplicates (6)
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

/*
 *
 *    Copyright (c) 2002, Smart Link Ltd.
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions
 *    are met:
 *
 *        1. Redistributions of source code must retain the above copyright
 *           notice, this list of conditions and the following disclaimer.
 *        2. Redistributions in binary form must reproduce the above
 *           copyright notice, this list of conditions and the following
 *           disclaimer in the documentation and/or other materials provided
 *           with the distribution.
 *        3. Neither the name of the Smart Link Ltd. nor the names of its
 *           contributors may be used to endorse or promote products derived
 *           from this software without specific prior written permission.
 *
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
 *
 *    modem_cmdline.c  --  simple command line processor,
 *                         define config parameters.
 *
 *    Author: Sasha K (sashak@smlink.com)
 *
 *
 */

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

#include <modem.h>
#include <modem_homolog.h>
#include <modem_debug.h>

#define PR_INFO(fmt...) fprintf(stderr, fmt )


/* global config data */

/* modem.c */
extern const char *modem_default_country;

/* modem_debug.c */
extern unsigned int modem_debug_level;
extern unsigned int modem_debug_logging;

/* config parameters */
const char *modem_dev_name = NULL;
const char *modem_default_dev_name = "/dev/slamr0";
const char *modem_alsa_dev_name = "modem:1";
unsigned int need_realtime = 1;
#ifdef MODEM_CONFIG_RING_DETECTOR
unsigned int ring_detector = 0;
#endif
unsigned int use_alsa = 0;
const char *modem_group = "dialout";
unsigned int use_short_buffer = 0;
mode_t modem_perm  = 0660;


enum {
	OPT_HELP = 0,
	OPT_USAGE,
	OPT_VERSION,
	OPT_COUNTRY,
	OPT_COUNTRYLIST,
	OPT_ALSA,
	OPT_GROUP,
	OPT_PERM,
#ifdef MODEM_CONFIG_RING_DETECTOR
	OPT_RINGDET,
#endif
	OPT_USER,
	OPT_SHORTBUF,
	OPT_DEBUG,
	OPT_LOG,
	OPT_LAST
};

static struct opt {
	int  ch;
	const char *name;
	const char *desc;
	enum {NONE=0,MANDATORY,OPTIONAL} arg;
	enum {INTEGER,STRING} arg_type;
	const char *arg_val;
	unsigned found;
} opt_list[] = {
	{'h',"help","this usage"},
	{'u',"usage","this usage"},
	{'v',"version","show version and exit"},
	{'c',"country","default modem country name",MANDATORY,STRING,"USA"},
	{ 0 ,"countrylist","show list of supported countries"},
	{'a',"alsa","ALSA mode (see README for howto)"},
	{'g',"group","Modem TTY group",MANDATORY,STRING,"dialout"},
	{'p',"perm","Modem TTY permission",MANDATORY,INTEGER,"0660"},
#ifdef MODEM_CONFIG_RING_DETECTOR
	{'r',"ringdetector","with internal ring detector (software)"},
#endif
	{'n',"nortpriority","run with regular priority"},
	{'s',"shortbuffer","use short buffer (4 periods length)"},
	{'d',"debug","debug level (developers only, for ./sl...)",OPTIONAL,INTEGER,"0"},
	{'l',"log","logging mode",OPTIONAL,INTEGER,"5"},
	{}
};



static void usage(const char *prog_name)
{
	struct opt *opt;
	PR_INFO("Usage: %s [option...] <device>\n"
		"Where 'device' is name of modem device (default `%s')\n"
		"  and 'option' may be:\n", prog_name, modem_default_dev_name);
	for (opt = opt_list ; opt->name ; opt++ ) {
		int n = 0;
		if(opt->ch)
			n = PR_INFO("  -%c, ",opt->ch);
		else
			n = PR_INFO("      ");
		n += PR_INFO("--%s%s ",opt->name,opt->arg?"=VAL":"");
		n += PR_INFO("%*s%s",24-n,"",opt->desc);
		if(opt->arg) {
			n+= PR_INFO(" (default `%s')",opt->arg_val);
		}
		PR_INFO("\n");
	}
	exit(1);
}


static void opt_parse(int argc, char *argv[])
{
	const char *prog_name;
	const char *dev_name = NULL;
	struct opt *opt;
	char *p, *arg;
	int found, i;
	
	prog_name = argv[0];

	for( i = 1 ; i < argc ; i++ ) {
		p = argv[i];
		if(*p != '-') {
			if(dev_name)
				usage(prog_name);
			dev_name = p;
			continue;
		}
		found = 0;
		arg = NULL;
		do { p++; } while( *p == '-' );
		for (opt = opt_list ; opt->name ; opt++ ) {
			if(!strncmp(p,opt->name,strlen(opt->name)) ||
			   (opt->ch && *p == opt->ch) ) {
				char *q;
				if(!strncmp(p,opt->name,strlen(opt->name)))
					q = p + strlen(opt->name);
				else
					q = p + 1;
				if( *q == '\0' )
					found = 1;
				else if (*q == '=' && opt->arg ) {
					arg = q + 1;
					found = 1;
				}
				else if(isdigit(*q) && opt->arg ) {
					arg = q;
					found = 1;
				}
			}

			if ( !found )
				continue;
			if ( arg && !opt->arg )
				usage(prog_name);

			if (!arg && opt->arg &&
			    (i < argc - 1 && *argv[i+1] != '-') &&
			    (!opt->arg_type == INTEGER || isdigit(*argv[i+1])))
				arg = argv[++i];

			if(!arg && opt->arg == MANDATORY)
				usage(prog_name);

			opt->found++;
			if(opt->arg && arg)
				opt->arg_val = arg;
			break;
		}
		if(!found)
			usage(prog_name);
	}

	if(dev_name)
		modem_dev_name = dev_name;
}



void modem_cmdline(int argc, char *argv[])
{
	const char *prog_name = argv[0];
	int val;

	opt_parse(argc,argv);

	if(opt_list[OPT_HELP].found ||
	   opt_list[OPT_USAGE].found )
		usage(prog_name);

	if(opt_list[OPT_VERSION].found) {
		extern void modem_print_version();
		modem_print_version();
		exit(0);
	}

	if(opt_list[OPT_COUNTRYLIST].found) {
		const struct homolog_set *s ;
		for( s = homolog_set; s->name ; s++ ) {
			PR_INFO("%02x: %s\n",s->id,s->name);
		}
		exit(0);
	}
	if(opt_list[OPT_COUNTRY].found)
		modem_default_country = opt_list[OPT_COUNTRY].arg_val;
	if(opt_list[OPT_ALSA].found) {
#ifndef SUPPORT_ALSA
		PR_INFO("ALSA support is not compiled in (see README for howto).\n");
		exit(1);
#endif
		use_alsa = 1;
	}
	if(opt_list[OPT_GROUP].found)
		modem_group = opt_list[OPT_GROUP].arg_val;
	if(opt_list[OPT_PERM].found) {
		val = strtol(opt_list[OPT_PERM].arg_val,NULL,8);
		if (val <= 0)
			usage(prog_name);
		modem_perm = val;
	}
#ifdef MODEM_CONFIG_RING_DETECTOR
	if(opt_list[OPT_RINGDET].found)
		ring_detector = 1;
#endif
	if(opt_list[OPT_USER].found)
		need_realtime = 0;
	if(opt_list[OPT_SHORTBUF].found)
		use_short_buffer = 1;
	if(opt_list[OPT_DEBUG].found) {
		modem_debug_level = 1 ;
		if(opt_list[OPT_DEBUG].arg_val &&
		   (val= strtol(opt_list[OPT_DEBUG].arg_val,NULL,0)) > 0 )
			modem_debug_level = val;
	}
	if(opt_list[OPT_LOG].found) {
		modem_debug_logging = 5;
		if(opt_list[OPT_LOG].arg_val &&
		   (val= strtol(opt_list[OPT_LOG].arg_val,NULL,0)) > 0 )
			modem_debug_logging = val;
	}
	if(!modem_dev_name) {
		modem_dev_name = use_alsa ? modem_alsa_dev_name : modem_default_dev_name;
	}
}