The Gnome Chemistry Utils  0.14.0
testbabelserver.c
1 /*
2  * Gnome Chemisty Utils
3  * tests/testbabelserver.c
4  *
5  * Copyright (C) 2010-2011 Jean Bréfort <jean.brefort@normalesup.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22 
23 #include "config.h"
24 #include <glib.h>
25 #include <netinet/in.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <sys/un.h>
30 #include <unistd.h>
31 
32 int main ()
33 {
34  struct stat statbuf;
35  char inbuf[256], *start = NULL;
36  int index, cur, length;
37  char *usr = getenv ("USER");
38  char *path = malloc (strlen ("/tmp/babelsocket-") + strlen (usr) + 1);
39  strcpy (path, "/tmp/babelsocket-");
40  strcat (path, usr);
41  if (strlen (path) >= 107) { //WARNING: don't know if this is portable
42  puts ("path too long");
43  return -1;
44  }
45  if (stat (path, &statbuf)) {
46  char *args[] = {LIBEXECDIR"/babelserver", NULL};
47  GError *error = NULL;
48  g_spawn_async (NULL, (char **) args, NULL, 0, NULL, NULL, NULL, &error);
49  if (error) {
50  g_error_free (error);
51  error = NULL;
52  free (path);
53  return -1;
54  }
55  while (stat (path, &statbuf));
56  }
57  int babelsocket = socket (AF_UNIX, SOCK_STREAM, 0);
58  if (babelsocket == -1) {
59  perror ("Could not create the socket");
60  free (path);
61  return -1;
62  }
63  struct sockaddr_un adr_serv;
64  adr_serv.sun_family = AF_UNIX;
65  strcpy (adr_serv.sun_path, path);
66  free (path);
67  if (connect (babelsocket, (const struct sockaddr*) &adr_serv, sizeof (struct sockaddr_un)) == -1) {
68  perror ("Connexion failed");
69  return -1;
70  }
71  char const *buf = "-i xyz -o inchi ";
72  write (babelsocket, buf, strlen (buf));
73  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";
74  char *size = g_strdup_printf ("-l %u -D", strlen (buf));
75  write (babelsocket, size, strlen (size));
76  write (babelsocket, buf, strlen (buf));
77  while (1) {
78  if ((cur = read (babelsocket, inbuf + index, 255 - index))) {
79  index += cur;
80  inbuf[index] = 0;
81  if (start == NULL) {
82  if ((start = strchr (inbuf, ' '))) {
83  length = strtol (inbuf, NULL, 10);
84  start++;
85  }
86  }
87  if (index - (start - inbuf) == length) {
88  printf ("answer is: %s\n", start);
89  break;
90  }
91  }
92  }
93  close (babelsocket);
94  return 0;
95 }