forked from Imagelibrary/rtems
2004-04-03 Wilfried Busalski <w.busalski@lancier-monitoring.de>
PR 599/pppd * pppd/chat.c: Fre memory that is allocated to fix leak.
This commit is contained in:
@@ -82,6 +82,18 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* $Id$ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Fixes and some Changes by Wilfried Busalski Lancier-Monitoring GmbH Germany
|
||||||
|
wilfried.busalski@muenster.de
|
||||||
|
|
||||||
|
Fixes: put_string() Free memory allocated by clean()
|
||||||
|
get_string() Free memory allocated by clean()
|
||||||
|
chat_main() Will Distroy's no more the chat-script
|
||||||
|
getnextcommand() sepatator changed to '@'
|
||||||
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
@@ -209,43 +221,53 @@ char *s;
|
|||||||
return dup_mem(s, strlen (s) + 1);
|
return dup_mem(s, strlen (s) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *getnextcommand(char **string)
|
char *getnextcommand(char *string,char *buff)
|
||||||
{
|
{
|
||||||
char *buf=*string,*res;
|
char *token;
|
||||||
res=strchr(buf,'|');
|
int len;
|
||||||
if (res==NULL)
|
|
||||||
|
token=strchr(string,'@');
|
||||||
|
if (token==NULL){
|
||||||
return NULL;
|
return NULL;
|
||||||
*res='\0';
|
}
|
||||||
*string=res+1;
|
len=token-string;
|
||||||
return buf;
|
if(len > 78 ){
|
||||||
|
len=78;
|
||||||
|
}
|
||||||
|
memcpy(buff,string,len);
|
||||||
|
buff[len]=0;
|
||||||
|
return(token+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int chatmain(int fd, int mode, char *pScript)
|
int chatmain(int fd, int mode, char *pScript)
|
||||||
{
|
{
|
||||||
char *arg;
|
char arg[80];
|
||||||
|
char *script;
|
||||||
|
|
||||||
/* initialize exit code */
|
/* initialize exit code */
|
||||||
exit_code = 0;
|
exit_code = 0;
|
||||||
ttyfd = fd;
|
ttyfd = fd;
|
||||||
|
|
||||||
|
script=pScript;
|
||||||
|
|
||||||
if ( debug ) {
|
if ( debug ) {
|
||||||
dbglog("chat_main: %s\n", pScript);
|
dbglog("chat_main: %s\n", script);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get first expect string */
|
/* get first expect string */
|
||||||
arg = getnextcommand(&pScript);
|
script = getnextcommand(script,arg);
|
||||||
while (( arg != NULL ) && ( exit_code == 0 )) {
|
while (( script != NULL ) && ( exit_code == 0 )) {
|
||||||
/* process the expect string */
|
/* process the expect string */
|
||||||
chat_expect(arg);
|
chat_expect(arg);
|
||||||
if ( exit_code == 0 ) {
|
if ( exit_code == 0 ) {
|
||||||
/* get the next send string */
|
/* get the next send string */
|
||||||
arg = getnextcommand(&pScript);
|
script = getnextcommand(script,arg);
|
||||||
if ( arg != NULL ) {
|
if ( script != NULL ) {
|
||||||
/* process the send string */
|
/* process the send string */
|
||||||
chat_send(arg);
|
chat_send(arg);
|
||||||
|
|
||||||
/* get the next expect string */
|
/* get the next expect string */
|
||||||
arg = getnextcommand(&pScript);
|
script = getnextcommand(script,arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -495,43 +517,43 @@ char *s;
|
|||||||
char *reply;
|
char *reply;
|
||||||
|
|
||||||
if (strcmp(s, "HANGUP") == 0) {
|
if (strcmp(s, "HANGUP") == 0) {
|
||||||
++hup_next;
|
++hup_next;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(s, "ABORT") == 0) {
|
if (strcmp(s, "ABORT") == 0) {
|
||||||
++abort_next;
|
++abort_next;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(s, "CLR_ABORT") == 0) {
|
if (strcmp(s, "CLR_ABORT") == 0) {
|
||||||
++clear_abort_next;
|
++clear_abort_next;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(s, "REPORT") == 0) {
|
if (strcmp(s, "REPORT") == 0) {
|
||||||
++report_next;
|
++report_next;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(s, "CLR_REPORT") == 0) {
|
if (strcmp(s, "CLR_REPORT") == 0) {
|
||||||
++clear_report_next;
|
++clear_report_next;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(s, "TIMEOUT") == 0) {
|
if (strcmp(s, "TIMEOUT") == 0) {
|
||||||
++timeout_next;
|
++timeout_next;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(s, "ECHO") == 0) {
|
if (strcmp(s, "ECHO") == 0) {
|
||||||
++echo_next;
|
++echo_next;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(s, "SAY") == 0) {
|
if (strcmp(s, "SAY") == 0) {
|
||||||
++say_next;
|
++say_next;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -598,11 +620,11 @@ register char *s;
|
|||||||
/* char file_data[STR_LEN]; */
|
/* char file_data[STR_LEN]; */
|
||||||
|
|
||||||
if (say_next) {
|
if (say_next) {
|
||||||
say_next = 0;
|
say_next = 0;
|
||||||
s = clean(s, 1);
|
s = clean(s, 1);
|
||||||
write(2, s, strlen(s));
|
write(2, s, strlen(s));
|
||||||
free(s);
|
free(s);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hup_next) {
|
if (hup_next) {
|
||||||
@@ -617,53 +639,56 @@ register char *s;
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (abort_next) {
|
if (abort_next) {
|
||||||
char *s1;
|
char *s1;
|
||||||
|
|
||||||
abort_next = 0;
|
abort_next = 0;
|
||||||
if ( n_aborts < MAX_ABORTS ) {
|
if ( n_aborts < MAX_ABORTS ) {
|
||||||
s1 = clean(s, 0);
|
s1 = clean(s, 0);
|
||||||
if (( strlen(s1) <= strlen(s) ) &&
|
if (( strlen(s1) <= strlen(s) ) && ( strlen(s1) < sizeof(fail_buffer))) {
|
||||||
( strlen(s1) < sizeof(fail_buffer))) {
|
abort_string[n_aborts++] = s1;
|
||||||
|
}
|
||||||
abort_string[n_aborts++] = s1;
|
free(s1);
|
||||||
}
|
}
|
||||||
}
|
return;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clear_abort_next) {
|
if (clear_abort_next) {
|
||||||
clear_abort_next = 0;
|
clear_abort_next = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (report_next) {
|
if (report_next) {
|
||||||
report_next = 0;
|
report_next = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clear_report_next) {
|
if (clear_report_next) {
|
||||||
clear_report_next = 0;
|
clear_report_next = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeout_next) {
|
if (timeout_next) {
|
||||||
timeout_next = 0;
|
timeout_next = 0;
|
||||||
timeout = atoi(s);
|
timeout = atoi(s);
|
||||||
|
|
||||||
if (timeout <= 0)
|
if (timeout <= 0){
|
||||||
timeout = DEFAULT_CHAT_TIMEOUT;
|
timeout = DEFAULT_CHAT_TIMEOUT;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(s, "EOT") == 0)
|
if (strcmp(s, "EOT") == 0){
|
||||||
s = "^D\\c";
|
s = "^D\\c";
|
||||||
else if (strcmp(s, "BREAK") == 0)
|
}
|
||||||
s = "\\K\\c";
|
else{
|
||||||
|
if (strcmp(s, "BREAK") == 0){
|
||||||
|
s = "\\K\\c";
|
||||||
|
}
|
||||||
|
|
||||||
if (!put_string(s)) {
|
if (!put_string(s)) {
|
||||||
exit_code = 2;
|
exit_code = 2;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_char()
|
static int get_char()
|
||||||
@@ -690,16 +715,14 @@ int c;
|
|||||||
{
|
{
|
||||||
char ch = c;
|
char ch = c;
|
||||||
|
|
||||||
write(ttyfd, &ch, 1);
|
return(write(ttyfd, &ch, 1));
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int write_char (c)
|
static int write_char (c)
|
||||||
int c;
|
int c;
|
||||||
{
|
{
|
||||||
if (put_char(c) < 0) {
|
if (put_char(c) < 1) {
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
@@ -707,41 +730,49 @@ int c;
|
|||||||
static int put_string (s)
|
static int put_string (s)
|
||||||
register char *s;
|
register char *s;
|
||||||
{
|
{
|
||||||
|
char *out,*free_ptr=0;
|
||||||
|
|
||||||
quiet = 0;
|
quiet = 0;
|
||||||
s = clean(s, 1);
|
out = free_ptr = clean(s, 1);
|
||||||
while (*s) {
|
while (*out) {
|
||||||
register char c = *s++;
|
register char c = *out++;
|
||||||
|
|
||||||
if (c != '\\') {
|
if (c != '\\') {
|
||||||
if (!write_char (c))
|
if (!write_char (c)){
|
||||||
return 0;
|
free(free_ptr);
|
||||||
continue;
|
return 0;
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
c = *s++;
|
c = *out++;
|
||||||
switch (c) {
|
|
||||||
case 'd':
|
|
||||||
sleep(1);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'K':
|
switch (c) {
|
||||||
break_sequence();
|
case 'd':
|
||||||
break;
|
sleep(1);
|
||||||
|
break;
|
||||||
|
|
||||||
case 'p':
|
case 'K':
|
||||||
|
break_sequence();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'p':
|
||||||
#if 0 /* FIXME!!! */
|
#if 0 /* FIXME!!! */
|
||||||
usleep(10000); /* 1/100th of a second (arg is microseconds) */
|
usleep(10000); /* 1/100th of a second (arg is microseconds) */
|
||||||
#else
|
#else
|
||||||
sleep(1);
|
sleep(1);
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (!write_char (c))
|
if (!write_char (c)){
|
||||||
return 0;
|
free(free_ptr);
|
||||||
break;
|
return 0;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
free(free_ptr);
|
||||||
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
@@ -749,12 +780,13 @@ register char *s;
|
|||||||
/*
|
/*
|
||||||
* 'Wait for' this string to appear on this file descriptor.
|
* 'Wait for' this string to appear on this file descriptor.
|
||||||
*/
|
*/
|
||||||
static int get_string(string)
|
static int get_string(in_string)
|
||||||
register char *string;
|
register char *in_string;
|
||||||
{
|
{
|
||||||
int c, len, minlen;
|
int c, len, minlen;
|
||||||
register char *s = temp2, *end = s + STR_LEN;
|
register char *s = temp2, *end = s + STR_LEN;
|
||||||
char *logged = temp2;
|
char *logged = temp2;
|
||||||
|
char *string=0;
|
||||||
struct termios tios;
|
struct termios tios;
|
||||||
|
|
||||||
memset(temp2, 0, sizeof(temp2));
|
memset(temp2, 0, sizeof(temp2));
|
||||||
@@ -764,13 +796,14 @@ register char *string;
|
|||||||
tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS;
|
tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS;
|
||||||
tcsetattr(ttyfd, TCSANOW, &tios);
|
tcsetattr(ttyfd, TCSANOW, &tios);
|
||||||
|
|
||||||
string = clean(string, 0);
|
string = clean(in_string, 0);
|
||||||
len = strlen(string);
|
len = strlen(string);
|
||||||
minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
|
minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
|
||||||
|
|
||||||
if (len > STR_LEN) {
|
if (len > STR_LEN) {
|
||||||
exit_code = 1;
|
exit_code = 1;
|
||||||
return 0;
|
free(string);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
@@ -780,12 +813,19 @@ register char *string;
|
|||||||
while ( (c = get_char()) >= 0) {
|
while ( (c = get_char()) >= 0) {
|
||||||
int n, abort_len;
|
int n, abort_len;
|
||||||
|
|
||||||
*s++ = c;
|
if(c == '\n' || c == '\r'){
|
||||||
*s=0;
|
s = temp2;
|
||||||
|
*s=0;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
*s++ = c;
|
||||||
|
*s=0;
|
||||||
|
}
|
||||||
|
|
||||||
if (s - temp2 >= len &&
|
if (s - temp2 >= len &&
|
||||||
c == string[len - 1] &&
|
c == string[len - 1] &&
|
||||||
strncmp(s - len, string, len) == 0) {
|
strncmp(s - len, string, len) == 0) {
|
||||||
|
free(string);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -795,6 +835,7 @@ register char *string;
|
|||||||
|
|
||||||
exit_code = n + 4;
|
exit_code = n + 4;
|
||||||
strcpy(fail_reason = fail_buffer, abort_string[n]);
|
strcpy(fail_reason = fail_buffer, abort_string[n]);
|
||||||
|
free(string);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -811,5 +852,7 @@ register char *string;
|
|||||||
}
|
}
|
||||||
|
|
||||||
exit_code = 3;
|
exit_code = 3;
|
||||||
|
free(string);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user