00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "config.h"
00024 #include <glib.h>
00025 #include <netinet/in.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <sys/stat.h>
00029 #include <sys/un.h>
00030 #include <unistd.h>
00031
00032 int main ()
00033 {
00034 struct stat statbuf;
00035 char inbuf[256], *start = NULL;
00036 int index, cur, length;
00037 char *usr = getenv ("USER");
00038 char *path = malloc (strlen ("/tmp/babelsocket-") + strlen (usr) + 1);
00039 strcpy (path, "/tmp/babelsocket-");
00040 strcat (path, usr);
00041 if (strlen (path) >= 107) {
00042 puts ("path too long");
00043 return -1;
00044 }
00045 if (stat (path, &statbuf)) {
00046 char *args[] = {LIBEXECDIR"/babelserver", NULL};
00047 GError *error = NULL;
00048 g_spawn_async (NULL, (char **) args, NULL, 0, NULL, NULL, NULL, &error);
00049 if (error) {
00050 g_error_free (error);
00051 error = NULL;
00052 free (path);
00053 return -1;
00054 }
00055 while (stat (path, &statbuf));
00056 }
00057 int babelsocket = socket (AF_UNIX, SOCK_STREAM, 0);
00058 if (babelsocket == -1) {
00059 perror ("Could not create the socket");
00060 free (path);
00061 return -1;
00062 }
00063 struct sockaddr_un adr_serv;
00064 adr_serv.sun_family = AF_UNIX;
00065 strcpy (adr_serv.sun_path, path);
00066 free (path);
00067 if (connect (babelsocket, (const struct sockaddr*) &adr_serv, sizeof (struct sockaddr_un)) == -1) {
00068 perror ("Connexion failed");
00069 return -1;
00070 }
00071 char const *buf = "-i xyz -o inchi ";
00072 write (babelsocket, buf, strlen (buf));
00073 buf = "5\n\nC 0 0 0\nH 0 1.093 0\nH 1.030490282 -0.364333333 0\nH -0.515245141 -0.364333333 0.892430763\nH -0.515245141 -0.364333333 -0.892430763";
00074 char *size = g_strdup_printf ("-l %u -D", strlen (buf));
00075 write (babelsocket, size, strlen (size));
00076 write (babelsocket, buf, strlen (buf));
00077 while (1) {
00078 if ((cur = read (babelsocket, inbuf + index, 255 - index))) {
00079 index += cur;
00080 inbuf[index] = 0;
00081 if (start == NULL) {
00082 if ((start = strchr (inbuf, ' '))) {
00083 length = strtol (inbuf, NULL, 10);
00084 start++;
00085 }
00086 }
00087 if (index - (start - inbuf) == length) {
00088 printf ("answer is: %s\n", start);
00089 break;
00090 }
00091 }
00092 }
00093 close (babelsocket);
00094 return 0;
00095 }