Initial commit of newLISP.
[newlisp.git] / util / nclocal.c
blobf61ce593aa18d01a99ca44ff79843fa2c4b70abf
1 /*
2 // nclocal - copy stdin to local-domain UNIX socket and output response
3 // to std.out
4 //
5 // to make:
6 // gcc nclocal.c -o nclocal
7 //
8 // to test, start server:
9 // newlisp -c -d /tmp/mysocket &
11 // verify functioning:
12 // newlisp -e '(net-eval "/tmp/mysocket" 0 "(symbols)")'
14 // then use nclocal:
15 // echo '(symbols)(exit)' | ./nclocal /tmp/mysocket
17 // for multiline send a [cmd] before and a [/cmd] after the code
18 // each on an extra line.
19 //
20 // Copyright (C) 2007 Lutz Mueller <lutz@nuevatec.com>
21 //
22 // This program is free software; you can redistribute it and/or modify
23 // it under the terms of the GNU General Public License version 2, 1991,
24 // as published by the Free Software Foundation.
26 // This program is distributed in the hope that it will be useful,
27 // but WITHOUT ANY WARRANTY; without even the implied warranty of
28 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 // GNU General Public License for more details.
30 //
31 // You should have received a copy of the GNU General Public License
32 // along with this program; if not, write to the Free Software
33 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/un.h>
45 int main(int argc, char * argv[])
47 int s, t, len;
48 struct sockaddr_un remote_sun;
49 char str[102];
50 char * sock_path;
52 if(argc < 2)
54 printf("nclocal - (c) Lutz Mueller, 2007\n");
55 printf("Send stdin to <socket-path> and output response to stdout\n\n");
56 printf("USAGE: nclocal <socket-path> < message-file\n");
57 exit(0);
60 sock_path = argv[1];
62 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
64 perror("socket");
65 exit(1);
68 remote_sun.sun_family = AF_UNIX;
69 strncpy(remote_sun.sun_path, sock_path, sizeof(remote_sun.sun_path) - 1);
70 remote_sun.sun_path[sizeof (remote_sun.sun_path) - 1] = '\0';
72 if (connect(s, (struct sockaddr *)&remote_sun, SUN_LEN(&remote_sun)) == -1)
74 perror("connect");
75 exit(1);
78 while(fgets(str, 100, stdin), !feof(stdin))
80 if (send(s, str, strlen(str), 0) == -1)
82 perror("send");
83 exit(1);
87 while((t = recv(s, str, 100, 0)) > 0)
89 str[t] = '\0';
90 printf("%s", str);
93 if(t < 0) perror("recv");
95 close(s);
96 return 0;