Lines Matching refs:c
81 #define BUFLEFT(c) (sizeof (c)->buf - ((c)->bufend - (c)->buf)) argument
89 struct chat *c = (struct chat *)v; in chat_PauseTimer() local
90 timer_Stop(&c->pause); in chat_PauseTimer()
91 c->pause.load = 0; in chat_PauseTimer()
95 chat_Pause(struct chat *c, u_long load) in chat_Pause() argument
97 timer_Stop(&c->pause); in chat_Pause()
98 c->pause.load += load; in chat_Pause()
99 c->pause.func = chat_PauseTimer; in chat_Pause()
100 c->pause.name = "chat pause"; in chat_Pause()
101 c->pause.arg = c; in chat_Pause()
102 timer_Start(&c->pause); in chat_Pause()
108 struct chat *c = (struct chat *)v; in chat_TimeoutTimer() local
109 timer_Stop(&c->timeout); in chat_TimeoutTimer()
110 c->TimedOut = 1; in chat_TimeoutTimer()
114 chat_SetTimeout(struct chat *c) in chat_SetTimeout() argument
116 timer_Stop(&c->timeout); in chat_SetTimeout()
117 if (c->TimeoutSec > 0) { in chat_SetTimeout()
118 c->timeout.load = SECTICKS * c->TimeoutSec; in chat_SetTimeout()
119 c->timeout.func = chat_TimeoutTimer; in chat_SetTimeout()
120 c->timeout.name = "chat timeout"; in chat_SetTimeout()
121 c->timeout.arg = c; in chat_SetTimeout()
122 timer_Start(&c->timeout); in chat_SetTimeout()
142 struct chat *c = descriptor2chat(d); in chat_UpdateSet() local
144 int TimedOut = c->TimedOut; in chat_UpdateSet()
147 if (c->pause.state == TIMER_RUNNING) in chat_UpdateSet()
152 if (c->nargptr == NULL) in chat_UpdateSet()
153 c->state = CHAT_FAILED; in chat_UpdateSet()
156 c->argptr = &arg_term; in chat_UpdateSet()
161 c->bufstart = c->bufend; in chat_UpdateSet()
163 c->TimedOut = 0; in chat_UpdateSet()
166 if (c->state != CHAT_EXPECT && c->state != CHAT_SEND) in chat_UpdateSet()
171 if (c->arg < c->argc && (c->arg < 0 || *c->argptr == '\0')) { in chat_UpdateSet()
173 if (c->arg < 0 || c->state == CHAT_SEND) in chat_UpdateSet()
174 c->state = CHAT_EXPECT; in chat_UpdateSet()
176 c->state = CHAT_SEND; in chat_UpdateSet()
179 while (special && (c->nargptr || c->arg < c->argc - 1)) { in chat_UpdateSet()
180 if (c->arg < 0 || (!TimedOut && c->state == CHAT_SEND)) in chat_UpdateSet()
181 c->nargptr = NULL; in chat_UpdateSet()
183 if (c->nargptr != NULL) { in chat_UpdateSet()
185 c->argptr = c->nargptr; in chat_UpdateSet()
187 c->nargptr[-1] = '-'; in chat_UpdateSet()
188 c->nargptr = chat_NextChar(c->nargptr, '-'); in chat_UpdateSet()
189 if (c->nargptr != NULL) in chat_UpdateSet()
190 *c->nargptr++ = '\0'; in chat_UpdateSet()
194 if ((c->argptr = c->argv[++c->arg]) == NULL) { in chat_UpdateSet()
196 c->state = CHAT_DONE; in chat_UpdateSet()
200 if (c->state == CHAT_EXPECT) { in chat_UpdateSet()
202 c->nargptr = c->argptr; in chat_UpdateSet()
204 while ((c->nargptr = chat_NextChar(c->nargptr, '-'))) { in chat_UpdateSet()
205 c->nargptr++; in chat_UpdateSet()
211 " '-' chars, all ignored\n", c->argptr); in chat_UpdateSet()
213 c->nargptr = chat_NextChar(c->argptr, '-'); in chat_UpdateSet()
214 *c->nargptr++ = '\0'; in chat_UpdateSet()
225 needcr = c->state == CHAT_SEND && in chat_UpdateSet()
226 (*c->argptr != '!' || c->argptr[1] == '!'); in chat_UpdateSet()
229 ExpandString(c, c->argptr, c->exp + 2, sizeof c->exp - 2, needcr); in chat_UpdateSet()
236 if (c->abort.num < MAXABORTS) { in chat_UpdateSet()
239 len = strlen(c->exp+2); in chat_UpdateSet()
240 for (i = 0; i < c->abort.num; i++) in chat_UpdateSet()
241 if (len > c->abort.string[i].len) { in chat_UpdateSet()
244 for (last = c->abort.num; last > i; last--) { in chat_UpdateSet()
245 c->abort.string[last].data = c->abort.string[last-1].data; in chat_UpdateSet()
246 c->abort.string[last].len = c->abort.string[last-1].len; in chat_UpdateSet()
250 c->abort.string[i].len = len; in chat_UpdateSet()
251 if ((c->abort.string[i].data = (char *)malloc(len+1)) != NULL) { in chat_UpdateSet()
252 memcpy(c->abort.string[i].data, c->exp+2, len+1); in chat_UpdateSet()
253 c->abort.num++; in chat_UpdateSet()
259 c->TimeoutSec = atoi(c->exp + 2); in chat_UpdateSet()
260 if (c->TimeoutSec <= 0) in chat_UpdateSet()
261 c->TimeoutSec = 30; in chat_UpdateSet()
263 } else if (c->nargptr == NULL && !strcmp(c->exp+2, "ABORT")) in chat_UpdateSet()
265 else if (c->nargptr == NULL && !strcmp(c->exp+2, "TIMEOUT")) in chat_UpdateSet()
268 if (c->exp[2] == '!' && c->exp[3] != '!') in chat_UpdateSet()
269 ExecStr(c->physical, c->exp + 3, c->exp + 3, sizeof c->exp - 3); in chat_UpdateSet()
271 if (c->exp[2] == '\0') { in chat_UpdateSet()
273 c->argptr = &arg_term; in chat_UpdateSet()
288 c->state = CHAT_DONE; in chat_UpdateSet()
293 c->argptr = c->exp + (c->exp[2] == '!' ? 3 : 2); in chat_UpdateSet()
294 c->arglen = strlen(c->argptr); in chat_UpdateSet()
296 if (c->state == CHAT_EXPECT) { in chat_UpdateSet()
300 end = c->bufend - c->arglen + 1; in chat_UpdateSet()
301 if (end < c->bufstart) in chat_UpdateSet()
302 end = c->bufstart; in chat_UpdateSet()
303 for (begin = c->bufstart; begin < end; begin++) in chat_UpdateSet()
304 if (!strncmp(begin, c->argptr, c->arglen)) { in chat_UpdateSet()
305 c->bufstart = begin + c->arglen; in chat_UpdateSet()
306 c->argptr += c->arglen; in chat_UpdateSet()
307 c->arglen = 0; in chat_UpdateSet()
312 log_Printf(LogCHAT, "Expect(%d): %s\n", c->TimeoutSec, c->argptr); in chat_UpdateSet()
313 chat_SetTimeout(c); in chat_UpdateSet()
323 if (c->state == CHAT_EXPECT) in chat_UpdateSet()
324 return physical_doUpdateSet(&c->physical->desc, r, NULL, e, n, 1); in chat_UpdateSet()
326 return physical_doUpdateSet(&c->physical->desc, NULL, w, e, n, 1); in chat_UpdateSet()
332 struct chat *c = descriptor2chat(d); in chat_IsSet() local
333 return c->argptr && physical_IsSet(&c->physical->desc, fdset); in chat_IsSet()
337 chat_UpdateLog(struct chat *c, int in) in chat_UpdateLog() argument
350 end = ptr = c->bufend; in chat_UpdateLog()
352 ptr = c->bufend - in; in chat_UpdateLog()
353 for (end = c->bufend - 1; end >= ptr; end--) in chat_UpdateLog()
359 for (ptr = c->bufend - (in == -1 ? 1 : in + 1); ptr >= c->bufstart; ptr--) in chat_UpdateLog()
382 struct chat *c = descriptor2chat(d); in chat_Read() local
384 if (c->state == CHAT_EXPECT) { in chat_Read()
393 in = BUFLEFT(c); in chat_Read()
394 if (in > (ssize_t)sizeof c->buf / 2) in chat_Read()
395 in = sizeof c->buf / 2; in chat_Read()
397 in = physical_Read(c->physical, c->bufend, in); in chat_Read()
402 ebegin = c->bufend - c->arglen + 1; in chat_Read()
404 if (ebegin < c->bufstart) in chat_Read()
405 ebegin = c->bufstart; in chat_Read()
407 if (c->abort.num) { in chat_Read()
408 abegin = c->bufend - c->abort.string[0].len + 1; in chat_Read()
409 aend = c->bufend - c->abort.string[c->abort.num-1].len + in + 1; in chat_Read()
410 if (abegin < c->bufstart) in chat_Read()
411 abegin = c->bufstart; in chat_Read()
419 c->bufend += in; in chat_Read()
421 chat_UpdateLog(c, in); in chat_Read()
423 if (c->bufend > c->buf + sizeof c->buf / 2) { in chat_Read()
427 for (chop = begin - c->buf; chop; chop--) in chat_Read()
428 if (c->buf[chop] == '\n') in chat_Read()
433 chop = begin - c->buf; in chat_Read()
438 to = c->buf; in chat_Read()
440 while (from < c->bufend) in chat_Read()
442 c->bufstart -= chop; in chat_Read()
443 c->bufend -= chop; in chat_Read()
455 !strncmp(begin, c->argptr, c->arglen)) { in chat_Read()
457 timer_Stop(&c->timeout); in chat_Read()
458 if (memchr(begin + c->arglen - 1, '\n', in chat_Read()
459 c->bufend - begin - c->arglen + 1) == NULL) { in chat_Read()
461 end = c->bufend; in chat_Read()
462 c->bufend = begin + c->arglen; in chat_Read()
463 chat_UpdateLog(c, -1); in chat_Read()
464 c->bufend = end; in chat_Read()
466 c->bufstart = begin + c->arglen; in chat_Read()
467 c->argptr += c->arglen; in chat_Read()
468 c->arglen = 0; in chat_Read()
471 for (n = c->abort.num - 1; n >= 0; n--) { in chat_Read()
472 if (begin + c->abort.string[n].len > c->bufend) in chat_Read()
474 if (!strncmp(begin, c->abort.string[n].data, in chat_Read()
475 c->abort.string[n].len)) { in chat_Read()
476 if (memchr(begin + c->abort.string[n].len - 1, '\n', in chat_Read()
477 c->bufend - begin - c->abort.string[n].len + 1) == NULL) { in chat_Read()
479 end = c->bufend; in chat_Read()
480 c->bufend = begin + c->abort.string[n].len; in chat_Read()
481 chat_UpdateLog(c, -1); in chat_Read()
482 c->bufend = end; in chat_Read()
484 c->bufstart = begin + c->abort.string[n].len; in chat_Read()
485 c->state = CHAT_FAILED; in chat_Read()
497 struct chat *c = descriptor2chat(d); in chat_Write() local
500 if (c->state == CHAT_SEND) { in chat_Write()
503 if (strstr(c->argv[c->arg], "\\P")) /* Don't log the password */ in chat_Write()
504 log_Printf(LogCHAT, "Send: %s\n", c->argv[c->arg]); in chat_Write()
508 sz = c->arglen - 1; in chat_Write()
509 while (sz >= 0 && c->argptr[sz] == '\n') in chat_Write()
511 log_Printf(LogCHAT, "Send: %.*s\n", sz + 1, c->argptr); in chat_Write()
514 if (physical_IsSync(c->physical)) { in chat_Write()
520 c->argptr -= 2; in chat_Write()
521 c->arglen += 2; in chat_Write()
522 memcpy(c->argptr, "\377\003", 2); /* Prepend HDLC header */ in chat_Write()
525 wrote = physical_Write(c->physical, c->argptr, c->arglen); in chat_Write()
532 if (physical_IsSync(c->physical)) { in chat_Write()
533 c->argptr += 2; in chat_Write()
534 c->arglen -= 2; in chat_Write()
536 } else if (wrote < 2 && physical_IsSync(c->physical)) { in chat_Write()
538 c->argptr += 2; in chat_Write()
539 c->arglen -= 2; in chat_Write()
541 c->argptr += wrote; in chat_Write()
542 c->arglen -= wrote; in chat_Write()
550 chat_Init(struct chat *c, struct physical *p) in chat_Init() argument
552 c->desc.type = CHAT_DESCRIPTOR; in chat_Init()
553 c->desc.UpdateSet = chat_UpdateSet; in chat_Init()
554 c->desc.IsSet = chat_IsSet; in chat_Init()
555 c->desc.Read = chat_Read; in chat_Init()
556 c->desc.Write = chat_Write; in chat_Init()
557 c->physical = p; in chat_Init()
558 *c->script = '\0'; in chat_Init()
559 c->argc = 0; in chat_Init()
560 c->arg = -1; in chat_Init()
561 c->argptr = NULL; in chat_Init()
562 c->nargptr = NULL; in chat_Init()
563 c->bufstart = c->bufend = c->buf; in chat_Init()
565 memset(&c->pause, '\0', sizeof c->pause); in chat_Init()
566 memset(&c->timeout, '\0', sizeof c->timeout); in chat_Init()
570 chat_Setup(struct chat *c, const char *data, const char *phone) in chat_Setup() argument
572 c->state = CHAT_EXPECT; in chat_Setup()
575 *c->script = '\0'; in chat_Setup()
576 c->argc = 0; in chat_Setup()
578 strncpy(c->script, data, sizeof c->script - 1); in chat_Setup()
579 c->script[sizeof c->script - 1] = '\0'; in chat_Setup()
580 c->argc = MakeArgs(c->script, c->argv, VECSIZE(c->argv), PARSE_NOHASH); in chat_Setup()
583 c->arg = -1; in chat_Setup()
584 c->argptr = NULL; in chat_Setup()
585 c->nargptr = NULL; in chat_Setup()
587 c->TimeoutSec = 30; in chat_Setup()
588 c->TimedOut = 0; in chat_Setup()
589 c->phone = phone; in chat_Setup()
590 c->abort.num = 0; in chat_Setup()
592 timer_Stop(&c->pause); in chat_Setup()
593 timer_Stop(&c->timeout); in chat_Setup()
595 return c->argc >= 0; in chat_Setup()
599 chat_Finish(struct chat *c) in chat_Finish() argument
601 timer_Stop(&c->pause); in chat_Finish()
602 timer_Stop(&c->timeout); in chat_Finish()
603 while (c->abort.num) in chat_Finish()
604 free(c->abort.string[--c->abort.num].data); in chat_Finish()
605 c->abort.num = 0; in chat_Finish()
609 chat_Destroy(struct chat *c) in chat_Destroy() argument
611 chat_Finish(c); in chat_Destroy()
627 ExpandString(struct chat *c, const char *str, char *result, int reslen, int cr) in ExpandString() argument
641 chat_Pause(c, 2 * SECTICKS); in ExpandString()
644 chat_Pause(c, SECTICKS / 4); in ExpandString()
663 strncpy(result, c->physical->dl->bundle->cfg.auth.key, reslen); in ExpandString()
669 if (c->phone) { in ExpandString()
670 strncpy(result, c->phone, reslen); in ExpandString()
677 strncpy(result, c->physical->dl->bundle->cfg.auth.name, reslen); in ExpandString()