File: server_monitor.c

package info (click to toggle)
lyx 1.1.4-7
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,708 kB
  • ctags: 7,042
  • sloc: cpp: 63,191; sh: 8,634; ansic: 3,411; perl: 3,323; makefile: 636; tcl: 163; sed: 93; yacc: 38
file content (274 lines) | stat: -rw-r--r-- 7,782 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * LyXServer monitor. To send command to a running instance of LyX
 * and receive information from LyX.
 * 
 * To build it type:
 * > gcc -g server_monitor.c -o monitor -lforms -L/usr/X11/lib -lX11 -lm
 * > ./monitor
 * Before you run lyx uncomment the line "\serverpipe" from your
 * ~/.lyx/lyxrc file, according with your home path.
 * 
 * Created: 970531
 * Copyright (C) 1997 Alejandro Aguilar Sierra (asierra@servidor.unam.mx)
 * Updated: 980104
 * Copyright (C) 1998 Asger Alstrup (alstrup@diku.dk)
 */


/* Form definition file generated with fdesign. */

#include "forms.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>    

int lyx_listen = 0;
int  pipein=-1, pipeout=-1;
char pipename[100];        
char const *clientname = "monitor";

/**** Forms and Objects ****/

typedef struct {
	FL_FORM *server;
	void *vdata;
	long ldata;
	FL_OBJECT *pipename;
	FL_OBJECT *command;
	FL_OBJECT *arg;
	FL_OBJECT *info;
	FL_OBJECT *client;
	FL_OBJECT *submit;
	FL_OBJECT *notify;
} FD_server;


enum { LS_DUMMY=1, LS_OPEN, LS_CLOSE, LS_SUBMIT };

static FD_server *fd_server;

void closepipe();
void server_cb(FL_OBJECT *ob, long data);


FD_server *create_form_server(void)
{
  FL_OBJECT *obj;
  FD_server *fdui = (FD_server *) fl_calloc(1, sizeof(*fdui));

  fdui->server = fl_bgn_form(FL_NO_BOX, 620, 260);
  obj = fl_add_box(FL_UP_BOX,0,0,620,260,"");
  fdui->pipename = obj = fl_add_input(FL_NORMAL_INPUT,110,10,190,40,"Pipe name");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_callback(obj,server_cb,0);
    fl_set_object_shortcut(obj,"#P",1);
  fdui->client = obj = fl_add_input(FL_NORMAL_INPUT,410,10,190,40,"Client name");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_callback(obj,server_cb,0);
    fl_set_object_shortcut(obj,"#N",1);
  fdui->command = obj = fl_add_input(FL_NORMAL_INPUT,110,60,190,40,"Command");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_callback(obj,server_cb,0);
    fl_set_object_shortcut(obj,"#C",1);
  fdui->arg = obj = fl_add_input(FL_NORMAL_INPUT,410,60,190,40,"Argument");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_callback(obj,server_cb,0);
    fl_set_object_shortcut(obj,"#A",1);
  obj = fl_add_text(FL_NORMAL_TEXT,10,110,100,40,"Info");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_lalign(obj,FL_ALIGN_RIGHT|FL_ALIGN_INSIDE);
  fdui->info = obj = fl_add_text(FL_NORMAL_TEXT,110,110,490,40,"");
    fl_set_object_boxtype(obj,FL_DOWN_BOX);
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_lalign(obj,FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
  obj = fl_add_text(FL_NORMAL_TEXT,10,160,100,40,"Notify");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_lalign(obj,FL_ALIGN_RIGHT|FL_ALIGN_INSIDE);
  fdui->notify = obj = fl_add_text(FL_NORMAL_TEXT,110,160,490,40,"");
    fl_set_object_boxtype(obj,FL_DOWN_BOX);
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_lalign(obj,FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
  obj = fl_add_button(FL_NORMAL_BUTTON,10,210,140,40,"Open pipes");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_lstyle(obj,FL_BOLD_STYLE);
    fl_set_object_callback(obj,server_cb,LS_OPEN);
    fl_set_object_shortcut(obj,"#O",1);
  obj = fl_add_button(FL_NORMAL_BUTTON,160,210,140,40,"Close pipes");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_lstyle(obj,FL_BOLD_STYLE);
    fl_set_object_callback(obj,server_cb,LS_CLOSE);
    fl_set_object_shortcut(obj,"#l",1);
  fdui->submit = obj = fl_add_button(FL_NORMAL_BUTTON,310,210,170,40,"Submit Command");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_lstyle(obj,FL_BOLD_STYLE);
    fl_set_object_callback(obj,server_cb,LS_SUBMIT);
    fl_set_object_shortcut(obj,"#S",1);
  obj = fl_add_button(FL_NORMAL_BUTTON,490,210,120,40,"Done");
    fl_set_object_lsize(obj,FL_NORMAL_SIZE);
    fl_set_object_lstyle(obj,FL_BOLD_STYLE);
    fl_set_object_shortcut(obj,"#D^[",1);
  fl_end_form();

  fdui->server->fdui = fdui;

  return fdui;
}
/*---------------------------------------*/


void io_cb(int fd, void *data)
{
    int n;
    char s[255];

    n = read(fd, &s[0], 200);
    if (n>=0)
      s[n] = 0;
    fprintf(stderr, "monitor: Coming: %s\n", s);
    if (strncmp(s, "LYXSRV:", 7)==0) {
	if (strstr(s, "bye")) {
	    lyx_listen = 0;
	    fprintf(stderr, "monitor: LyX has closed connection!\n");
	    closepipe();
	    return;
	}
	if (strstr(s, "hello")) {
	    lyx_listen = 1;
	    fprintf(stderr, "monitor: LyX is listening!\n");
	    fl_set_object_lcol(fd_server->submit,FL_BLACK);
	    fl_activate_object(fd_server->submit);
	}                            
    }
    if (s[0]=='I')
      fl_set_object_label(fd_server->info, s);
    else
      fl_set_object_label(fd_server->notify, s); 
}

void openpipe()
{
    char buf[100];
    clientname = fl_get_input(fd_server->client);
    if (pipein==-1) {
        char *pipename = strdup(fl_get_input(fd_server->pipename));
        char s[255];

        fprintf(stderr, "monitor: Opening pipes ");
        strcpy(s, pipename);
        strcat(s, ".in");
        fprintf(stderr, "[%s] and ", s);
        pipein = open(s, O_RDWR);
        strcpy(s, pipename);
        strcat(s, ".out");
        fprintf(stderr, "[%s]\n", s);
        pipeout = open(s, O_RDONLY|O_NONBLOCK);
        fprintf(stderr, "monitor: Opened %d %d\n", pipein, pipeout);

	if (pipein<0 || pipeout<0) {
	    perror("monitor: Couldn't open the pipes");
	    pipein = pipeout = -1;
	    return;
	}
	fl_add_io_callback(pipeout, FL_READ, io_cb, 0);
	
	// greet LyX
	sprintf(buf, "LYXSRV:%s:hello", clientname);
	write(pipein, buf, strlen(buf));
	free(pipename);
    } else 
        fprintf(stderr, "monitor: Pipes already opened, close them first\n");
}             


void closepipe()
{
    char buf[100];
    
    if (pipein==-1 && pipeout==-1) {
	fprintf(stderr, "monitor: Pipes are not opened\n");
	return;
    }
	
    if (pipein>=0) {
	close(pipein);
    }
	      
    if (pipeout>=0) {
	if (lyx_listen) {
	    lyx_listen = 0;
	    /* Say goodbye */
	    sprintf(buf, "LYXSRV:%s:bye\n", clientname);
	    write(pipeout, buf, strlen(buf));
	}
	close(pipeout);
	fl_remove_io_callback(pipeout, FL_READ, io_cb);
    }     
    pipein = pipeout = -1;
    fl_set_object_lcol(fd_server->submit,FL_INACTIVE);
    fl_deactivate_object(fd_server->submit);    
}


void submit()
{
    char s[255];
 
    const char *clientname = fl_get_input(fd_server->client);
    const char *argument = fl_get_input(fd_server->arg);
    const char *command = fl_get_input(fd_server->command);
    
    sprintf(s, "LYXCMD:%s:%s:%s\n", clientname, command, argument);
    fprintf(stderr, "monitor: command: %s\n", s);
    if (pipein>=0) 
      write(pipein, s, strlen(s));
    else
      fprintf(stderr, "monitor: Pipe is not opened\n");
}


/* callbacks for form server */
void server_cb(FL_OBJECT *ob, long data)
{

    switch (data) {
     case 0: break;
     case LS_OPEN:
	openpipe();
	break;
     case LS_CLOSE:
	closepipe();
	break;
     case LS_SUBMIT:
	submit();
	break;
     case LS_DUMMY:
	break;
    }
}


int main(int argc, char *argv[])
{
    fl_initialize(&argc, argv, 0, 0, 0);
    fd_server = create_form_server();

    strcpy(pipename, getenv("HOME"));
    strcat(pipename, "/.lyxpipe"),
    
    /* fill-in form initialization code */
    fl_set_input(fd_server->pipename, pipename);
    fl_set_input(fd_server->client, clientname);
    fl_deactivate_object(fd_server->submit);
    fl_set_object_lcol(fd_server->submit,FL_INACTIVE);
    
    /* show the first form */
    fl_show_form(fd_server->server,FL_PLACE_MOUSE,FL_FULLBORDER,"LyX Server Monitor");
    fl_do_forms();
    if (pipein != -1)
      closepipe();
    return 0;
}