File: atcmd.c

package info (click to toggle)
modemu 0.0.1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 208 kB
  • ctags: 266
  • sloc: ansic: 1,860; lex: 146; makefile: 75; sh: 12
file content (395 lines) | stat: -rw-r--r-- 8,582 bytes parent folder | download | duplicates (4)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdio.h>	/*stderr,(sscanf,sprintf)*/
#include <string.h>	/*(strncpy)*/
#include <stdlib.h>	/*(getenv)*/
#include <sys/time.h>	/*->ttybuf.h (timeval)*/
#include <arpa/telnet.h>/*TELOPT_xxx*/

#include "defs.h"	/*->atcmd.h (uchar)*/
#include "atcmd.h"	/*atcmd*/
#include "sock.h"	/*sockIsAlive*/
#include "ttybuf.h"	/*(putTty1)*/
#include "telopt.h"	/*telOpt*/
#include "cmdlex.h"	/*(cmdLex)*/
#include "cmdarg.h"	/*cmdarg*/
#include "verbose.h"	/*VERB_MISC*/

#ifdef BINMODE_AS_DEFAULT
# define BINCMD "%B0=1%B1=1"
#else
# define BINCMD
#endif

#define INITSTR "AT" \
	"S2=43"		/* escape char = '+' */ \
	"S3=13"		/* CR */ \
	"S4=10"		/* LF */ \
	"S5=8"		/* BS */ \
	"S7=20"		/* timelimit for non-blocking connect */ \
	"S12=50"	/* escape sequence guard time */ \
	BINCMD		/* binary mode */ \
	"%T1"		/* terminal-type = $TERM */ \
	"&W"		/* write to NVRAM */

void
atcmdInit(void)
{
    Cmdstat s;

    /*memset(atcmd, 0, sizeof(atcmd));*/
    if (cmdLex(INITSTR) != CMDST_OK
	|| ( (s = cmdLex(getenv("MODEMU"))) != CMDST_OK && s != CMDST_NOAT )
	|| ( (s = cmdLex(cmdarg.atcmd)) != CMDST_OK && s != CMDST_NOAT )
	) {
	fprintf(stderr, "Error in initialization commands.\r\n");
	CHAR_CR = '\r'; /* force normal settings */
	CHAR_LF = '\n';
    }
}

/* LIT(A) -> "10" */
#define LIT_(s) #s
#define LIT(s) LIT_(s)

/* D */
/* dial command */
void
atcmdD(const char *s, AtdAType at, AtdPType pt)
{
    /*fprintf(stderr,"<%s>,%d,%d\r\n",s,at,pt);*/
    if (*s == '"') s++;
    /* "%[^:\"]:%[^\"]" */
    sscanf(s, "%" LIT(ADDR_MAX) "[^:\"]:%" LIT(PORT_MAX) "[^\"]",
	   atcmd.d.addr.str, atcmd.d.port.str);
    atcmd.d.addr.type = at;
    atcmd.d.port.type = pt;
    /*fprintf(stderr,"<%s>:<%s>\r\n",atcmd.d.addr.str, atcmd.d.port.str);*/
}

/* "x0" or "x" -> 0, "x1" -> 1, ... */
static int
getNumArg(const char *s)
{
#define isdigit(c) ('0' <= (c) && (c) <= '9')
    for ( ; *s != '\0'; s++) if (isdigit(*s)) return atoi(s);
    return 0;
}

/* fake command */
/* ("x1","012") -> 0, ("y3","012") -> 1 (just a range check) */
int
atcmdFake(const char *s, const char *vals)
{
    int i;

    i = getNumArg(s) + '0';
    for (; *vals != '\0'; vals++) if (i == *vals) return 0;
    return 1;
}

/* Hn */
/* n: 0(disconnect) */
int
atcmdH(const char *s)
{
    if (getNumArg(s) != 0) return 1;
    if (sockIsAlive()) {
	sockClose();
	verboseOut(VERB_MISC, "Connection closed with ATH.\r\n");
    }
    return 0;
}


/* In */
/* n: 4(show current settings), 5(show '&W'ed settings) or */
/*    6(show telnet option states) */

static void
prPercent(Atcmd *atcmdp)
{
    char buf[64];

    sprintf(buf, "%c%c%%B0=%d  %%B1=%d  %%D%d  %%L%d  %%R%d",
	    CHAR_CR, CHAR_LF,
	    atcmdp->pb[0], atcmdp->pb[1], atcmdp->pd, atcmdp->pl, atcmdp->pr);
    putTtyN(buf, strlen(buf));
    if (atcmdp->pt.wont) {
	putTtyStr("  %T0");
    } else {
	sprintf(buf, "  %%T=\"%s\"", atcmdp->pt.str);
	putTtyN(buf, strlen(buf));
    }
    sprintf(buf, "  %%V%d", atcmdp->pv);
    putTtyN(buf, strlen(buf));
}

static void
prSreg(uchar *s)
{
    int i;
    char buf[8];

    for (i = 0; i <= SREG_MAX; i++,s++) {
	if (i % 8 == 0) {
	    putTty1(CHAR_CR); putTty1(CHAR_LF);
	} else putTtyStr("  ");
	sprintf(buf, "S%02d=%03d", i, *s);
	putTtyN(buf, 7);
    }
}

static void
prOption(void)
{
    static char *onoff[]={"off", "on "};
    char buf[64];

    putTty1(CHAR_CR); putTty1(CHAR_LF);
    putTtyStr("MODEMU telnet option states:");
    putTty1(CHAR_CR); putTty1(CHAR_LF);
    putTty1(CHAR_CR); putTty1(CHAR_LF);
    putTtyStr(   "OPTION  LOCAL  REMOTE");
    sprintf(buf, "Binary   %s    %s",
	    onoff[telOpt.stTab[TELOPT_BINARY]->local.state],
	    onoff[telOpt.stTab[TELOPT_BINARY]->remote.state]);
    putTty1(CHAR_CR); putTty1(CHAR_LF); putTtyN(buf, strlen(buf));
    sprintf(buf, "Echo     %s    %s",
	    onoff[telOpt.stTab[TELOPT_ECHO]->local.state],
	    onoff[telOpt.stTab[TELOPT_ECHO]->remote.state]);
    putTty1(CHAR_CR); putTty1(CHAR_LF); putTtyN(buf, strlen(buf));
    sprintf(buf, "SGA      %s    %s",
	    onoff[telOpt.stTab[TELOPT_SGA]->local.state],
	    onoff[telOpt.stTab[TELOPT_SGA]->remote.state]);
    putTty1(CHAR_CR); putTty1(CHAR_LF); putTtyN(buf, strlen(buf));
    sprintf(buf, "TType    %s    %s",
	    onoff[telOpt.stTab[TELOPT_TTYPE]->local.state],
	    onoff[telOpt.stTab[TELOPT_TTYPE]->remote.state]);
    putTty1(CHAR_CR); putTty1(CHAR_LF); putTtyN(buf, strlen(buf));
    putTty1(CHAR_CR); putTty1(CHAR_LF);
}

static void
prVersion(void)
{
    putTty1(CHAR_CR); putTty1(CHAR_LF);
    putTtyStr("modemu version " LIT(VERSION_MAJOR) "." LIT(VERSION_MINOR));
    putTty1(CHAR_CR); putTty1(CHAR_LF);
}

int
atcmdI(const char *s)
{
    int idx;

    idx = getNumArg(s);
    switch (idx) {
    case 4:
	putTty1(CHAR_CR); putTty1(CHAR_LF);
	putTtyStr("MODEMU current settings:");
	putTty1(CHAR_CR); putTty1(CHAR_LF);
	prPercent(&atcmd);
	prSreg(atcmd.s);
	putTty1(CHAR_CR); putTty1(CHAR_LF);
	break;
    case 5:
	putTty1(CHAR_CR); putTty1(CHAR_LF);
	putTtyStr("MODEMU '&W'ed settings:");
	putTty1(CHAR_CR); putTty1(CHAR_LF);
	prPercent(&atcmdNV);
	prSreg(atcmdNV.s);
	putTty1(CHAR_CR); putTty1(CHAR_LF);
	break;
    case 6:
	prOption();
	break;
    case 7:
	prVersion();
	break;
    default:
	return 1;
    }
    return 0;
}



/* Sn? */
/* n: S register number */
int
atcmdSQuery(const char *s)
{
    int idx;
    char buf[4];

    idx = getNumArg(s);
    if (idx > SREG_MAX) return 1;
    putTty1(CHAR_CR); putTty1(CHAR_LF);
    sprintf(buf, "%03u", atcmd.s[idx]);
    putTtyN(buf, 3);
    putTty1(CHAR_CR); putTty1(CHAR_LF); /*at least Courier does*/
    return 0;
}

/* "1=2" -> (1,2), "1=" -> (1,0), "=2" -> (0,2), "=" -> (0,0) */
static void
getNumArg2(const char *s, int *n1p, int *n2p)
{
    char *s2;

    *n1p = strtol(s, &s2, 10);
    *n2p = strtol(s2+1, NULL, 10);
}

/* Sn=m */
/* n: S register number */
/* m: value 0-255 */
int
atcmdSSet(const char *s)
{
    int idx, val;

    getNumArg2(s+1, &idx, &val);
    if (idx > SREG_MAX || val > 255) return 1;
    atcmd.s[idx] = val;
    return 0;
}

/* Z */
/* recover &Wed settings and disconnect */
void
atcmdZ(void)
{
    atcmd = atcmdNV;
    if (sockIsAlive()) {
	sockClose();
	verboseOut(VERB_MISC, "Connection closed with ATZ.\r\n");
    }
}

/* &W */
/* save current settings */
void
atcmdAW(void)
{
    atcmdNV = atcmd;
}

/* %Bn=m */
/* n: 0(local) or 1(remote) */
/* m: 0(better non-binary), 1(better binary), */
/*    2(must non-binary) or 3(must binary) */
int
atcmdPB(const char *s)
{
    int idx, val;

    getNumArg2(s+2, &idx, &val);
    if (idx > 1 || val > 3) return 1;
    atcmd.pb[idx] = val;
    telOpt.sentReqs = 0; /* renegotiate when returning online */
    return 0;
}

/* %Dn */
/* n: 0(enable dial canceling) or 1(disable dial canceling) */
int
atcmdPD(const char *s)
{
    int i;

    i = getNumArg(s);
    if (i > 1) return 1;
    atcmd.pd = i;
    return 0;
}

/* %Ln */
/* n: 0(better char mode), 1(better line mode), */
/*    2(must char mode) or 3(must line mode) */
int
atcmdPL(const char *s)
{
    int i;

    i = getNumArg(s);
    if (i > 3) return 1;
    atcmd.pl = i;
    telOpt.sentReqs = 0; /* renegotiate when returning online */
    return 0;
}

/* %Q */
/* quit modemu */
void
atcmdPQ(void)
{
    sockShutdown(); /* may discard unsent chars in kernel,
		       or do ATH before quitting */
    exit(0);
}

/* %Rn */
/* n: 0(cooked?? mode) or 1(raw mode: 8bit thru, no IAC handling) */
/* overrides %B and %L settings */
int
atcmdPR(const char *s)
{
    int i;

    i = getNumArg(s);
    if (i > 1) return 1;
    atcmd.pr = i;
    return 0;
}

/* %Tn */
/* n: 0(dont support telnet term-type option) or */
/*    1(send $TERM for term-type option request) */
int
atcmdPT(const char *s)
{
    int i;

    i = getNumArg(s);
    switch (i) {
    case 0:
	atcmd.pt.wont = 1;
	break;
    case 1:
	strncpy(atcmd.pt.str, getenv("TERM"), PT_MAX);
	atcmd.pt.len = strlen(atcmd.pt.str);
	atcmd.pt.wont = 0;
	break;
    default:
	return 1;
    }
    return 0;
}

/* %T="xxxx" */
/* send xxxx for term-type option request */
int
atcmdPTSet(const char *s)
{
    sscanf(s+4, "%" LIT(PT_MAX) "[^\"]", atcmd.pt.str);
    /*strncpy(atcmd.pt.str, s+3, PT_MAX);*/
    atcmd.pt.len = strlen(atcmd.pt.str);
    /*telOpt.sentReqs = 0; renegotiation will be of no effect*/
    atcmd.pt.wont = 0;
    return 0;
}

/* %Vn */
/* n: 0-255 */
/*    bit0: output misc info to make up for less descriptive ATX0 indication */
/*    bit1: output telnet option negotioation */
int
atcmdPV(const char *s)
{
    int i;

    i = getNumArg(s);
    if (i > 255) return 1;
    atcmd.pv = i;
    return 0;
}