[PATCH 1/2] * src/tests/run_test.c: Corrected some memory leacs, changed interface for send_data accondiinly and indented file
Rui Batista
ruiandrebatista at gmail.com
Thu Feb 18 18:13:25 CET 2010
Changed all the code to allocate memory directly on the stack and reuse buffers so new ones don't need to be created for every reply.
The interface to send_data is changed with a new parameter to pass the reply buffer. If wfr argument is 0 reply is not touched so it can be NULL.
Also indented the code.
---
src/tests/run_test.c | 383 ++++++++++++++++++++++++++-----------------------
1 files changed, 203 insertions(+), 180 deletions(-)
diff --git a/src/tests/run_test.c b/src/tests/run_test.c
index 15a00c0..5308f33 100644
--- a/src/tests/run_test.c
+++ b/src/tests/run_test.c
@@ -35,6 +35,9 @@
#include "def.h"
+#define LINE_BUFFER_SIZE 1024
+#define REPLY_BUFFER_SIZE 1000
+
#define FATAL(msg) { printf(msg"\n"); exit(1); }
int sockk;
@@ -42,220 +45,240 @@ int sockk;
#ifdef __SUNPRO_C
/* Added by Willie Walker - strcasestr is a gcc-ism
*/
-char *strcasestr (const char *a, const char *b)
+char *
+strcasestr (const char *a, const char *b)
{
- size_t l;
- char f[3];
-
- snprintf (f, sizeof(f), "%c%c", tolower(*b), toupper(*b));
- for (l = strcspn(a, f); l != strlen(a); l += strcspn(a+l+1, f) + 1)
- if (strncasecmp(a+l, b, strlen(b)) == 0)
- return (a + l);
- return NULL;
+ size_t l;
+ char f[3];
+
+ snprintf (f, sizeof (f), "%c%c", tolower (*b), toupper (*b));
+ for (l = strcspn (a, f); l != strlen (a); l += strcspn (a + l + 1, f) + 1)
+ if (strncasecmp (a + l, b, strlen (b)) == 0)
+ return (a + l);
+ return NULL;
}
#endif /* __SUNPRO_C */
-char*
-send_data(int fd, char *message, int wfr)
+void
+send_data (int fd, const char *message, int wfr, char *reply)
{
- char *reply;
- int bytes;
-
- /* TODO: 1000?! */
- reply = (char*) malloc(sizeof(char) * 1000);
-
- /* write message to the socket */
- write(fd, message, strlen(message));
-
- /* read reply to the buffer */
- if (wfr == 1){
- bytes = read(fd, reply, 1000);
- /* print server reply to as a string */
- reply[bytes] = 0;
- }else{
- return "";
- }
-
- return reply;
+ int bytes;
+
+
+ /* write message to the socket */
+ bytes = write (fd, message, strlen (message));
+ assert (bytes == strlen (message)); /* this also accounts for -1 */
+
+ /* read reply to the buffer */
+ if (wfr == 1)
+ {
+ bytes = read (fd, reply, REPLY_BUFFER_SIZE);
+ assert (bytes > 0);
+ /* print server reply to as a string */
+ reply[bytes] = 0;
+ }
+
}
void
-wait_for(int fd, char* event)
+wait_for (int fd, const char *event)
{
- char * reply;
- int bytes;
-
- printf(" Waiting for: |%s|\n", event);
- reply = (char*) malloc(sizeof(char) * 1000);
- reply[0] = 0;
- while (0 == strcasestr(reply, event)) {
- bytes = read(fd, reply, 1000);
- if (bytes > 0) {
- reply[bytes] = 0;
- printf(" < %s\n", reply);
- fflush(NULL);
- }
+ int bytes;
+ char reply[REPLY_BUFFER_SIZE];
+
+ printf (" Waiting for: |%s|\n", event);
+ reply[0] = 0;
+ while (0 == strcasestr (reply, event))
+ {
+ bytes = read (fd, reply, REPLY_BUFFER_SIZE);
+ if (bytes > 0)
+ {
+ reply[bytes] = 0;
+ printf (" < %s\n", reply);
+ fflush (NULL);
+ }
}
- free(reply);
- printf(" Continuing.\n", reply);
- fflush(NULL);
+ printf (" Continuing.\n");
+ fflush (NULL);
}
-int
-init(char* client_name, char* conn_name)
+int
+init (char *client_name, char *conn_name)
{
int sockfd;
struct sockaddr_in address;
char *env_port;
int port;
- env_port = getenv("SPEECHD_PORT");
+ env_port = getenv ("SPEECHD_PORT");
if (env_port != NULL)
- port = strtol(env_port, NULL, 10);
+ port = strtol (env_port, NULL, 10);
else
- port = SPEECHD_DEFAULT_PORT;
+ port = SPEECHD_DEFAULT_PORT;
- address.sin_addr.s_addr = inet_addr("127.0.0.1");
- address.sin_port = htons(port);
+ address.sin_addr.s_addr = inet_addr ("127.0.0.1");
+ address.sin_port = htons (port);
address.sin_family = AF_INET;
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ sockfd = socket (AF_INET, SOCK_STREAM, 0);
/* connect to server */
- if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == -1)
- return 0;
+ if (connect (sockfd, (struct sockaddr *) &address, sizeof (address)) == -1)
+ return 0;
return sockfd;
}
int
-main(int argc, char* argv[])
+main (int argc, char *argv[])
{
- char* line;
- char* command;
- char* reply;
- int i;
- char *ret;
- FILE* test_file = NULL;
- int delays = 1;
- int indent = 0;
-
- if(argc < 2){
- printf("No test script specified!\n");
- exit(1);
+ char line[LINE_BUFFER_SIZE];
+ char *command;
+ char reply[REPLY_BUFFER_SIZE];
+ int i;
+ char *ret;
+ FILE *test_file = NULL;
+ int delays = 1;
+ int indent = 0;
+
+ if (argc < 2)
+ {
+ printf ("No test script specified!\n");
+ exit (1);
+ }
+
+ if (!strcmp (argv[1], "stdin"))
+ {
+ test_file = stdin;
}
-
- if (!strcmp(argv[1],"stdin")){
- test_file = stdin;
- }else{
- test_file = fopen(argv[1], "r");
- if(test_file == NULL) FATAL("Test file doesn't exist");
- }
-
- if(argc == 3){
- if(!strcmp(argv[2],"fast")) delays = 0;
- else{
- printf("Unrecognized parameter\n");
- exit(1);
- }
+ else
+ {
+ test_file = fopen (argv[1], "r");
+ if (test_file == NULL)
+ FATAL ("Test file doesn't exist");
}
-
- printf("Start of the test.\n");
- printf("==================\n\n");
-
- line = malloc(1024 * sizeof(char));
- reply = malloc(4096 * sizeof(char));
-
- sockk = init("run_test","user_test");
- if(sockk == 0) FATAL("Can't connect to Speech Dispatcher");
-
- assert(line != 0);
-
- while(1){
- ret = fgets(line, 1024, test_file);
- if (ret == NULL) break;
- if (strlen(line) <= 1){
- printf("\n");
- continue;
- }
-
- if (line[0] == '@'){
- command = (char*) strtok(line, "@\r\n");
- if (command == NULL)
- printf("\n");
- else
- printf(" %s\n", command);
- continue;
- }
-
- if (line[0] == '!'){
- command = (char*) strtok(line, "!\r\n");
- strcat(command,"\r\n");
- if (command == NULL) continue;
-
- printf(" >> %s", command);
- fflush(NULL);
- reply = send_data(sockk, command, 1);
- printf(" < %s", reply);
- fflush(NULL);
- continue;
- }
-
- if(line[0] == '.'){
- reply = send_data(sockk, "\r\n.\r\n", 1);
- printf(" < %s", reply);
- continue;
- }
-
- if(line[0] == '+'){
- command = (char*) strtok(&(line[1]), "+\r\n");
- wait_for(sockk, command);
- continue;
- }
-
- if(line[0] == '$'){
- if (delays){
- command = (char*) strtok(&(line[1]), "$\r\n");
- sleep(atoi(command));
- }
- continue;
- }
-
- if(line[0] == '^'){
- if (delays){
- command = (char*) strtok(&(line[1]), "$\r\n");
- usleep(atol(command));
- }
- continue;
- }
-
- if(line[0] == '~'){
- command = (char*) strtok(line, "~\r\n");
- indent = atoi(command);
- continue;
- }
-
-
- if(line[0] == '?'){
- getc(stdin);
- continue;
- }
-
- if(line[0] == '*'){
- system("clear");
- for (i=0; i<=indent - 1; i++){
- printf("\n");
- }
- continue;
- }
-
- send_data(sockk, line, 0);
- printf(" >> %s", line);
+
+ if (argc == 3)
+ {
+ if (!strcmp (argv[2], "fast"))
+ delays = 0;
+ else
+ {
+ printf ("Unrecognized parameter\n");
+ exit (1);
+ }
+ }
+
+ printf ("Start of the test.\n");
+ printf ("==================\n\n");
+
+
+ sockk = init ("run_test", "user_test");
+ if (sockk == 0)
+ FATAL ("Can't connect to Speech Dispatcher");
+
+
+ while (1)
+ {
+ ret = fgets (line, 1024, test_file);
+ if (ret == NULL)
+ break;
+ if (strlen (line) <= 1)
+ {
+ printf ("\n");
+ continue;
+ }
+
+ if (line[0] == '@')
+ {
+ command = (char *) strtok (line, "@\r\n");
+ if (command == NULL)
+ printf ("\n");
+ else
+ printf (" %s\n", command);
+ continue;
+ }
+
+ if (line[0] == '!')
+ {
+ command = (char *) strtok (line, "!\r\n");
+ strcat (command, "\r\n");
+ if (command == NULL)
+ continue;
+
+ printf (" >> %s", command);
+ fflush (NULL);
+ send_data (sockk, command, 1, reply);
+ printf (" < %s", reply);
+ fflush (NULL);
+ continue;
+ }
+
+ if (line[0] == '.')
+ {
+ send_data (sockk, "\r\n.\r\n", 1, reply);
+ printf (" < %s", reply);
+ continue;
+ }
+
+ if (line[0] == '+')
+ {
+ command = (char *) strtok (&(line[1]), "+\r\n");
+ wait_for (sockk, command);
+ continue;
+ }
+
+ if (line[0] == '$')
+ {
+ if (delays)
+ {
+ command = (char *) strtok (&(line[1]), "$\r\n");
+ sleep (atoi (command));
+ }
+ continue;
+ }
+
+ if (line[0] == '^')
+ {
+ if (delays)
+ {
+ command = (char *) strtok (&(line[1]), "$\r\n");
+ usleep (atol (command));
+ }
+ continue;
+ }
+
+ if (line[0] == '~')
+ {
+ command = (char *) strtok (line, "~\r\n");
+ indent = atoi (command);
+ continue;
+ }
+
+
+ if (line[0] == '?')
+ {
+ getc (stdin);
+ continue;
+ }
+
+ if (line[0] == '*')
+ {
+ system ("clear");
+ for (i = 0; i <= indent - 1; i++)
+ {
+ printf ("\n");
+ }
+ continue;
+ }
+
+ send_data (sockk, line, 0, NULL);
+ printf (" >> %s", line);
}
- close(sockk);
+ close (sockk);
- printf("\n==================\n");
- printf("End of the test.\n");
- exit(0);
+ printf ("\n==================\n");
+ printf ("End of the test.\n");
+ exit (0);
}
--
1.6.3.3
More information about the Speechd
mailing list