hacksql team mailing list archive
-
hacksql team
-
Mailing list archive
-
Message #00009
[Merge] lp:~typoon/hacksql/gilgamesh into lp:hacksql
Henrique M. Decaria has proposed merging lp:~typoon/hacksql/gilgamesh into lp:hacksql.
Requested reviews:
HackSQL team (hacksql)
I have made several changes in the server and client. As Higor has fixed the autotools stuff, this should compile without problems. Server now parses the query (real early stages, but it looks damn cool and fun).
--
https://code.launchpad.net/~typoon/hacksql/gilgamesh/+merge/4041
Your team HackSQL team is subscribed to branch lp:hacksql.
=== modified file 'hacksql/hacksql.h'
--- hacksql/hacksql.h 2009-02-27 16:45:22 +0000
+++ hacksql/hacksql.h 2009-02-28 06:08:32 +0000
@@ -32,8 +32,9 @@
/* Header for internacionalization _() */
#include <libintl.h>
-#define HACKSQL_DEFAULT_SERVER "localhost"
-#define HACKSQL_DEFAULT_PORT "4225"
+#define HACKSQL_DEFAULT_SERVER "localhost"
+#define HACKSQL_DEFAULT_PORT "4225"
+#define HACKSQL_DEFAULT_USERNAME "hacksql"
#define HACKSQL_VERSION "0.1 pre-alpha"
#define HACKSQL_NUM_VERSION 0001
=== modified file 'hacksqlc/src/main.c'
--- hacksqlc/src/main.c 2009-02-25 15:59:07 +0000
+++ hacksqlc/src/main.c 2009-02-28 06:08:32 +0000
@@ -51,12 +51,15 @@
void usage();
int parse_options(int argc, char **argv);
int hs_connect();
+void hs_disconnect();
+int hs_send_query(char *query);
/* Control variables */
-char *username;
+char *username = HACKSQL_DEFAULT_USERNAME;
char *password;
-char *host;
-char *port;
+char *host = HACKSQL_DEFAULT_SERVER;
+char *port = HACKSQL_DEFAULT_PORT;
+int my_sock;
/* Some command handlers. */
int cmd_help(int argc, char **argv, char *raw);
@@ -73,13 +76,13 @@
{"quit", cmd_quit, "Quit HackSQL client."},
{"ed", cmd_ed, "Edit a query string using $EDITOR."},
{"ping", cmd_ping, "Sends a 'Ping' request to the server."},
- {NULL, (cmd_func_t *) NULL, NULL}
+ {NULL, NULL, NULL}
};
command_t sqlcommands[] = {
{"select", cmd_run_sql, NULL},
{"update", cmd_run_sql, NULL},
- {NULL, (cmd_func_t *) NULL, NULL}
+ {NULL, NULL, NULL}
};
/* Wheter to end the main loop. */
@@ -133,7 +136,7 @@
/* If its not a finished sequence, read a bit more and parse the input again. */
if (input[strlen(input) - 1] != ';') {
- sprintf(input, "%s %s", input, readline(" > "));
+ sprintf(input, "%s %s", input, readline("\t> "));
return parse_input(input);
}
@@ -149,9 +152,7 @@
return 1;
}
} else { /* maybe its an sql command? */
-
- /* send the command to the server */
-
+ hs_send_query(input);
return 1;
}
@@ -203,6 +204,7 @@
int cmd_quit(int argc, char **argv, char *raw)
{
quit_hacksql = 1;
+ hs_disconnect();
return 0;
}
@@ -418,16 +420,15 @@
int hs_connect()
{
- int my_sock;
struct addrinfo info;
struct addrinfo *server;
- char buf[255];
+ char buf[4096];
memset(&info, 0, sizeof(info));
info.ai_family = AF_INET; /* IPv4 for now */
info.ai_socktype = SOCK_STREAM;
- if (getaddrinfo(host, port ? port : HACKSQL_DEFAULT_PORT, &info, &server) != 0) {
+ if (getaddrinfo(host, port, &info, &server) != 0) {
printf(_("Error on getaddrinfo\n"));
return -1;
}
@@ -446,9 +447,28 @@
freeaddrinfo(server);
+ /* Determine some handshake here to authenticate the user */
+
sprintf(buf, "Hello from %s", username);
send(my_sock, buf, strlen(buf), 0);
return 0;
}
+
+void hs_disconnect()
+{
+ const char *buf = "close_hs_connection";
+
+ if (my_sock >= 0) {
+ send(my_sock, buf, strlen(buf), 0);
+ close(my_sock);
+ }
+}
+
+int hs_send_query(char *query)
+{
+ if (my_sock >= 0) {
+ send(my_sock, query, strlen(query), 0);
+ }
+}
=== modified file 'hacksqlc/src/utils.c'
--- hacksqlc/src/utils.c 2009-02-25 15:59:07 +0000
+++ hacksqlc/src/utils.c 2009-02-28 06:08:32 +0000
@@ -74,8 +74,9 @@
char **toks = NULL;
char *token;
int count = 0;
+ char *buf = strdup(str);
- token = strtok(str, delimiters);
+ token = strtok(buf, delimiters);
while (token != NULL) {
toks = (char **) realloc(toks, sizeof(char *) * count + 1);
=== modified file 'hacksqld/src/hacksqld.c'
--- hacksqld/src/hacksqld.c 2009-02-25 15:59:07 +0000
+++ hacksqld/src/hacksqld.c 2009-02-28 13:02:20 +0000
@@ -24,94 +24,104 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
-
-#define HACKSQL_PORT "4225"
+#include <unistd.h>
+
+#include "hacksql/hs_parser_common.h"
+#include "hacksql/hs_sql_parser.h"
+
+#define HACKSQL_PORT "4225"
#define HACKSQL_BACKLOG 20
-#define HACKSQL_BUFFER 255
+#define HACKSQL_BUFFER 255
int main(int argc, char **argv)
{
- int my_sock, their_sock;
- int received;
-
- /*
- struct addrinfo {
- int ai_flags;
- int ai_family;
- int ai_socktype;
- int ai_protocol;
- size_t ai_addrlen;
- struct sockaddr *ai_addr;
- char *ai_canonname;
- struct addrinfo *ai_next;
- };
- */
- struct addrinfo *local;
- struct addrinfo info;
-
- struct sockaddr_storage remote;
- socklen_t remotelen;
-
- char buf[HACKSQL_BUFFER+1];
-
- memset(&info, 0, sizeof(info));
- info.ai_family = AF_INET; /* For now, only IPV4 */
- info.ai_socktype = SOCK_STREAM;
- info.ai_flags = AI_PASSIVE;
-
- if( getaddrinfo(NULL, HACKSQL_PORT, &info, &local) != 0 ) {
- printf("Error on getaddrinfo\n");
- return -1;
- }
-
- my_sock = socket(local->ai_family, local->ai_socktype, local->ai_protocol);
-
- if( my_sock < 0 ) {
- printf("Error creating the socket\n");
- return -1;
- }
-
- bind(my_sock, local->ai_addr, local->ai_addrlen);
- listen(my_sock, HACKSQL_BACKLOG);
-
- while(1) {
- remotelen = sizeof(remote);
- their_sock = accept(my_sock, (struct sockaddr *)&remote, &remotelen);
-
- if (their_sock == -1) {
- perror("Error accepting connection\n");
- continue;
- }
-
- if(!fork()) { // Child process goes in
- // I am the child, daddy wont come here :)
- close(my_sock);
-
- while(1) {
- received = recv(their_sock, buf, HACKSQL_BUFFER, 0);
- buf[received] = '\0';
-
- /* Here we pass buf to the parser. Note that buf might
- * not contain the whole query. A logic needs to be added
- * to determine when the client finished sending the query
- * so we can pass it to the parser */
-
- printf("Received data %s\n", buf);
-
- if( strcmp(buf,"exit\n\0") == 0 ) {
- break;
- }
- }
-
- /* Child process is finished with its work, so exit */
- close(their_sock);
- return 0;
- }
-
- }
-
- freeaddrinfo(local);
-
- close(my_sock);
-
+ int my_sock, their_sock;
+ int received;
+
+ /*
+ struct addrinfo {
+ int ai_flags;
+ int ai_family;
+ int ai_socktype;
+ int ai_protocol;
+ size_t ai_addrlen;
+ struct sockaddr *ai_addr;
+ char *ai_canonname;
+ struct addrinfo *ai_next;
+ };
+ */
+ struct addrinfo *local;
+ struct addrinfo info;
+
+ struct sockaddr_storage remote;
+ socklen_t remotelen;
+
+ char buf[HACKSQL_BUFFER+1];
+
+ memset(&info, 0, sizeof(info));
+ info.ai_family = AF_INET; /* For now, only IPV4 */
+ info.ai_socktype = SOCK_STREAM;
+ info.ai_flags = AI_PASSIVE;
+
+ if( getaddrinfo(NULL, HACKSQL_PORT, &info, &local) != 0 ) {
+ printf("Error on getaddrinfo\n");
+ return -1;
+ }
+
+ my_sock = socket(local->ai_family, local->ai_socktype, local->ai_protocol);
+
+ if( my_sock < 0 ) {
+ printf("Error creating the socket\n");
+ return -1;
+ }
+
+ bind(my_sock, local->ai_addr, local->ai_addrlen);
+ listen(my_sock, HACKSQL_BACKLOG);
+
+ while(1) {
+ remotelen = sizeof(remote);
+ their_sock = accept(my_sock, (struct sockaddr *)&remote, &remotelen);
+
+ if (their_sock == -1) {
+ perror("Error accepting connection\n");
+ continue;
+ }
+
+ if(!fork()) { // Child process goes in
+ // I am the child, daddy wont come here :)
+ close(my_sock);
+
+ while(1) {
+ received = recv(their_sock, buf, HACKSQL_BUFFER, 0);
+ buf[received] = '\0';
+
+ /* Here we pass buf to the parser. Note that buf might
+ * not contain the whole query. A logic needs to be added
+ * to determine when the client finished sending the query
+ * so we can pass it to the parser */
+
+ printf("Received data %s: their_sock is %d; received is %d\n", buf, their_sock,received);
+
+ if( strcmp(buf,"close_hs_connection") == 0 ) {
+ break;
+ } else {
+ printf("Parsing query %s\n",buf);
+ hs_parser_query(buf);
+ }
+
+ if (received == 0)
+ break;
+ }
+
+ /* Child process is finished with its work, so exit */
+ close(their_sock);
+ return 0;
+ }
+
+ }
+
+ freeaddrinfo(local);
+
+ close(my_sock);
+
}
=== modified file 'hacksqld/src/hs_sql_scanner.l'
--- hacksqld/src/hs_sql_scanner.l 2009-02-28 17:49:26 +0000
+++ hacksqld/src/hs_sql_scanner.l 2009-02-28 23:01:27 +0000
@@ -36,6 +36,10 @@
%%
+";" {
+ return T_SEMI_COLON;
+}
+
{WHITESPACE}+ {
/* Ignore */ ;
}
Follow ups