]> matita.cs.unibo.it Git - helm.git/blob - helm/helmpot/main.c
ocaml 3.09 transition
[helm.git] / helm / helmpot / main.c
1 /*
2  * Copyright (C) 2000-2002, Luca Padovani <luca.padovani@cs.unibo.it>.
3  * 
4  * This file is part of HelmPot, a minimal browser for HELM.
5  * 
6  * HelmPot is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * 
11  * HelmPot is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with HelmPot; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  * 
20  * For details, see the HelmPot World-Wide-Web page,
21  * http://cs.unibo.it/helm/helmpot, or send a mail to
22  * <luca.padovani@cs.unibo.it>
23  */
24
25 #include <config.h>
26
27 #include <glib.h>
28 #include <assert.h>
29 #include <getopt.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40
41 #include "defs.h"
42 #include "guiGTK.h"
43
44 #define BUFFER_SIZE 2048
45
46 PRIVATE gchar app_name[64];
47 PRIVATE gint sockfd;
48 PRIVATE struct sockaddr_in address;
49
50 PRIVATE void
51 error(const gchar* msg)
52 {
53   g_assert(msg != NULL);
54   fprintf(stderr, "%s: fatal error: %s\n", app_name, msg);
55 }
56
57 #if 0
58 PRIVATE void
59 print_version()
60 {
61   printf("%s - written by Luca Padovani (C) 2000.\n", app_name);
62 #ifdef DEBUG
63   printf("Compiled %s %s\n", __DATE__, __TIME__);
64 #endif
65   exit(0);
66 }
67 #endif
68
69 PRIVATE gboolean
70 timeout(gpointer user_data)
71 {
72   static gchar file_name[BUFFER_SIZE];
73
74   if (recv(sockfd, file_name, BUFFER_SIZE, 0) < 0) {
75     if (errno != EAGAIN && errno != EWOULDBLOCK)
76       error("error receving message");
77   } else
78     GUI_load_document(file_name);
79
80   return TRUE;
81 }
82
83 int
84 main(int argc, char *argv[])
85 {
86   sprintf(app_name, "HELM Pot (Plug-OuT) v%s", VERSION);
87
88   if (argc != 2) {
89     fprintf(stderr, "%s\n\n", app_name);
90     fprintf(stderr, "Usage: helmpot URL\n");
91     exit(-1);
92   }
93
94   sockfd = socket(PF_INET, SOCK_DGRAM, 0);
95   if (sockfd < 0) error("could not create socket");
96
97   if (inet_aton("127.0.0.1", &address.sin_addr) < 0)
98     error("could not create address");
99   address.sin_port = 8778;
100   address.sin_family = PF_INET;
101
102   if (bind(sockfd, &address, sizeof(address)) < 0) {
103     if (sendto(sockfd, argv[1], strlen(argv[1]), 0, &address, sizeof(address)) < 0) 
104       error("could not send message");
105     sleep(1);
106     exit(0);
107   }
108
109   if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
110     error("could not set the socket to non-block mode");
111
112   GUI_init(&argc, &argv, app_name, 500, 600, timeout, 500);
113
114   GUI_load_document(argv[1]);
115
116   GUI_run();
117   GUI_uninit();
118   GUI_unload_document();
119
120   return 0;
121 }