File: info.c

package info (click to toggle)
ppxp 0.99120923-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,812 kB
  • ctags: 3,704
  • sloc: ansic: 24,532; tcl: 3,992; makefile: 517; sh: 80
file content (339 lines) | stat: -rw-r--r-- 7,752 bytes parent folder | download | duplicates (3)
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
#include <string.h>
#include <sys/types.h>
#include <termios.h>
#include <sys/ioctl.h>

#include <config.h>
#include <support.h>
#include <xcio.h>
#include "info.h"

struct number_string_s {
    int number;
    char *string;
};

static int
f2s(int flags, struct number_string_s *p, char *buf, int buflen)
{
    int len;

    buf[0] = '\0';
    len = 0;
    for(; p->string; p++){
	if(flags & p->number){
	    len += strlen(p->string) + 1;
	    if(len < buflen){
		strncat(buf, p->string, buflen - len);
		buf[len - 1] = ':';
		buf[len] = '\0';
	    }else{
		break;
	    }
	}
    }
    if(len > 0){
	len--;
	buf[len] = '\0';	/* delete last ":" */
    }
    return(len);
}

static int
n2s(int num, struct number_string_s *p, char *buf, int buflen)
{
    for(; *p->string; p++)
	if(num == p->number)
	    break;

    if(p->string){
	strncpy(buf, p->string, buflen);
    }else{
	buf[0] = '\0';
    }
    return(strlen(buf));
}

/************************************************************/
static struct number_string_s l_stat[] = {
    { LSTAT_PROMPT,	"prompt" },
    { LSTAT_TTY,	"tty"    },
    { LSTAT_CHAT,	"chat"   },
    { LSTAT_PPP,	"ppp"    },
    { LSTAT_NLINK,	"nlink"  },
    { LSTAT_DIAL,	"dial"   },
    { 0, NULL }
};

static struct number_string_s m_flag[] = {
    { MFLAG_AUTO,	"auto" },
    { 0, NULL }
};

static struct number_string_s n_stat[] = {
    { NSTAT_IP,	"ip"  },
    { NSTAT_IPX,	"ipx" },
    { 0, NULL }
};

static struct number_string_s minfo[] = {
    { TIOCM_DTR,	"DTR" },
    { TIOCM_RTS,	"RTS" },
    { TIOCM_CD,		"CD"  },
    { TIOCM_RNG,	"RNG" },
    { TIOCM_DSR,	"DSR" },
    { 0, NULL }
};

static struct number_string_s phase[] = {
    { PS_DEAD,		"dead"		},
    { PS_ESTABLISH,	"establish"	},
    { PS_AUTHENTICATE,	"authenticate"	},
    { PS_CALLBACK,	"callback"	},
    { PS_NETWORK,	"network"	},
    { PS_TERMINATE,	"terminate"	},
    { 0, NULL }
};

#define PPPSTR_BUF_SIZE 64

static void
ppp_setkeyval(struct keyval_s *kv, char *key, int type)
{
    kv->keyval[0] = key;
    kv->type = type;
    if(type == PPXPINFO_STR){
	kv->keyval[1] = (char *)malloc(sizeof(char)*PPPSTR_BUF_SIZE);
    }else if(type == PPXPINFO_INT){
#if PPXP_TCL
	kv->keyval[1] = (char *)malloc(sizeof(char)*PPPSTR_BUF_SIZE);
#endif
    }else if(type == PPXPINFO_LONG){
#if PPXP_TCL
	kv->keyval[1] = (char *)malloc(sizeof(char)*PPPSTR_BUF_SIZE);
#endif
    }
}

static struct keyval_s *
pppinfo_new(void)
{
    struct keyval_s *info;
    int i;

    info = (struct keyval_s *) malloc(sizeof(struct keyval_s)*N_PPPINFO);

    i = 0;	ppp_setkeyval(&info[i],	"phase",	PPXPINFO_STR);
    i++;	ppp_setkeyval(&info[i],	"idle",		PPXPINFO_LONG);
    i++;	ppp_setkeyval(&info[i],	"connect",	PPXPINFO_LONG);
    i++;	ppp_setkeyval(&info[i],	"send.count",	PPXPINFO_INT);
    i++;	ppp_setkeyval(&info[i],	"send.error",	PPXPINFO_INT);
    i++;	ppp_setkeyval(&info[i],	"send.lsize",	PPXPINFO_INT);
    i++;	ppp_setkeyval(&info[i],	"send.nsize",	PPXPINFO_INT);
    i++;	ppp_setkeyval(&info[i],	"recv.count",	PPXPINFO_INT);
    i++;	ppp_setkeyval(&info[i],	"recv.error",	PPXPINFO_INT);
    i++;	ppp_setkeyval(&info[i],	"recv.lsize",	PPXPINFO_INT);
    i++;	ppp_setkeyval(&info[i],	"recv.nsize",	PPXPINFO_INT);
    i++;	ppp_setkeyval(&info[i],	"modeminfo",	PPXPINFO_STR);
    i++;	ppp_setkeyval(&info[i],	"linestat",	PPXPINFO_STR);
    i++;	ppp_setkeyval(&info[i],	"netstat",	PPXPINFO_STR);
    i++;	ppp_setkeyval(&info[i],	"mode",		PPXPINFO_STR);

    return(info);
}
static int
pppinfo_parse(struct keyval_s *info, char *buf, int buflen)
{
    struct pppinfo_s p;
    /* struct pppinfo_s *p = (struct pppinfo_s *)buf; */
    int i;

    memcpy(&p, buf, sizeof(p));

#if PPXP_TCL
#define SETINT(i, s) (sprintf((s), "%d", (i)))
#define SETLONG(i, s) (sprintf((s), "%ld", (i)))
#elif PPXP_PERL
#define SETINT(i, s) (s = (char *)&i)
#define SETLONG(i, s) (s = (char *)&i)
#endif

    i = 0;	n2s(p.phase, phase, info[i].keyval[1], PPPSTR_BUF_SIZE);
    i++;	SETLONG(p.idle, info[i].keyval[1]);
    i++;	SETLONG(p.connect, info[i].keyval[1]);
    i++;	SETINT(p.s.count, info[i].keyval[1]);
    i++;	SETINT(p.s.error, info[i].keyval[1]);
    i++;	SETINT(p.s.lsize, info[i].keyval[1]);
    i++;	SETINT(p.s.nsize, info[i].keyval[1]);
    i++;	SETINT(p.r.count, info[i].keyval[1]);
    i++;	SETINT(p.r.error, info[i].keyval[1]);
    i++;	SETINT(p.r.lsize, info[i].keyval[1]);
    i++;	SETINT(p.r.nsize, info[i].keyval[1]);
    i++;	f2s(p.minfo, minfo, info[i].keyval[1], PPPSTR_BUF_SIZE);
    i++;	f2s(p.l_stat, l_stat, info[i].keyval[1], PPPSTR_BUF_SIZE);
    i++;	f2s(p.n_stat, n_stat, info[i].keyval[1], PPPSTR_BUF_SIZE);
    i++;	f2s(p.m_flag, m_flag, info[i].keyval[1], PPPSTR_BUF_SIZE);
    i++;

    return(i);
}

static void
pppinfo_clear(struct keyval_s *info)
{
}

static void
pppinfo_delete(struct keyval_s *info)
{
    int i;

    for(i = 0; i < N_PPPINFO; i++){
	if(
#if PPXP_TCL
	    1
#elif PPXP_PERL
	    info[i].type == PPXPINFO_STR
#endif
	    )
	    free(info[i].keyval[1]);
    }
    free(info);
}

struct ppxpinfo_ops PPxP_pppInfo = {
    pppinfo_new,
    pppinfo_parse,
    pppinfo_clear,
    pppinfo_delete
};

/************************************************************/
static struct keyval_s *
pwdinfo_new(void)
{
    static char *keywords[N_PWDINFO] = {
	"name", "password", "entry", "group", "script"
    };
    struct keyval_s *pwdinfo;
    int i;

    pwdinfo = (struct keyval_s *)
	malloc(sizeof(struct keyval_s)*N_PWDINFO);
    for(i = 0; i < N_PWDINFO; i++){
	pwdinfo[i].keyval[0] = keywords[i];
	pwdinfo[i].type = PPXPINFO_STR;
    }
    return(pwdinfo);
}

static int
pwdinfo_parse(struct keyval_s *pwdinfo, char *buf, int buflen)
{
    char *cp, *ep;
    int i;

    cp = buf;
    ep = buf + buflen;

    for(i = 0; i < N_PWDINFO && cp < ep; cp += strlen(cp) + 1, i++){
	pwdinfo[i].keyval[1] = cp;
    }
    return(i);
}

static void
pwdinfo_clear(struct keyval_s *pwdinfo)
{
}

static void
pwdinfo_delete(struct keyval_s *pwdinfo)
{
    free(pwdinfo);
}

struct ppxpinfo_ops PPxP_PwdInfo = {
    pwdinfo_new,
    pwdinfo_parse,
    pwdinfo_clear,
    pwdinfo_delete
};

/************************************************************/
#define CONFLAG_BUF_SIZE 32
#define CONID_BUF_SIZE 32

static struct keyval_s *
coninfo_new(void)
{
    static char *keywords[N_CONINFO] = {
	"name", "from", "flags", "cfd"
    };
    struct keyval_s *info;
    int i;

    info = (struct keyval_s *) malloc(sizeof(struct keyval_s)*N_CONINFO);
    for(i = 0; i < N_CONINFO; i++){
	info[i].keyval[0] = keywords[i];
	info[i].type = PPXPINFO_STR;
    }
    info[2].keyval[1] = (char *)malloc(sizeof(char)*CONFLAG_BUF_SIZE);
#if PPXP_TCL
    info[3].keyval[1] = (char *)malloc(sizeof(char)*CONID_BUF_SIZE);
#endif
    info[3].type = PPXPINFO_INT;

    return(info);
}

static struct number_string_s f_console[] = {
    { CONSOLE_AUTOF,	"autof" },
    { CONSOLE_AUTOC,	"autoc" },
    { CONSOLE_CURRENT,	"current" },
    { 0, NULL }
};

static int
coninfo_parse(struct keyval_s *info, char *buf, int buflen)
{
    char *cp, *ep;
    int i, cfd;

    cp = buf;
    info[0].keyval[1] = cp;	/* name */
    cp += strlen(cp) + 1;
    info[1].keyval[1] = cp;	/* from */
    cp += strlen(cp) + 1;
    f2s(*cp, f_console, info[2].keyval[1], CONFLAG_BUF_SIZE);
    cp++;
#if PPXP_PERL
    info[3].keyval[1] = cp;
#elif PPXP_TCL
    memcpy(&cfd, cp, sizeof(cfd));
    sprintf(info[3].keyval[1], "%d", cfd);
#endif
    return(N_CONINFO);
}

static void
coninfo_clear(struct keyval_s *info)
{
}

static void
coninfo_delete(struct keyval_s *info)
{
    free(info[2].keyval[1]);
#if PPXP_TCL
    free(info[2].keyval[1]);
#endif
    free(info);
}

struct ppxpinfo_ops PPxP_ConInfo = {
    coninfo_new,
    coninfo_parse,
    coninfo_clear,
    coninfo_delete
};