arzmq-sys 0.6.3

Low-level bindings to the zeromq library
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*********************************************************************
 *
 * AUTHORIZATION TO USE AND DISTRIBUTE
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: 
 *
 * (1) source code distributions retain this paragraph in its entirety, 
 *  
 * (2) distributions including binary code include this paragraph in
 *     its entirety in the documentation or other materials provided 
 *     with the distribution, and 
 *
 * (3) all advertising materials mentioning features or use of this 
 *     software display the following acknowledgment:
 * 
 *      "This product includes software written and developed 
 *       by Brian Adamson and Joe Macker of the Naval Research 
 *       Laboratory (NRL)." 
 *         
 *  The name of NRL, the name(s) of NRL  employee(s), or any entity
 *  of the United States Government may not be used to endorse or
 *  promote  products derived from this software, nor does the 
 *  inclusion of the NRL written and developed software  directly or
 *  indirectly suggest NRL or United States  Government endorsement
 *  of this product.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 ********************************************************************/
 
// This file contains code for various UNIX MDP post processor options
        
        
/*******************************************************************
 * Netscape[tm] post processor routines
 */     
        
// Some portions of this code 
// Copyright � 1996 Netscape Communications Corporation, 
// all rights reserved.
        
#include "normPostProcess.h"
#include "protoDebug.h"

#include <errno.h>
#include <string.h>  // for strerror()
#include <signal.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>  // for exit()
#include <stdio.h>

#ifndef sighandler_t
#ifndef sig_t
typedef void (*sighandler_t)(int);
#else
typedef sig_t sighandler_t;
#endif // if/else !sig_t
#endif // !sighandler_t

#ifdef NETSCAPE_SUPPORT
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xmu/WinUtil.h>	/* for XmuClientWindow() */

/* vroot.h is a header file which lets a client get along with `virtual root'
   window managers like swm, tvtwm, olvwm, etc.  If you don't have this header
   file, you can find it at "http://home.netscape.com/newsref/std/vroot.h".
   If you don't care about supporting virtual root window managers, you can
   comment this line out.
 */
#include "vroot.h"
#define MOZILLA_VERSION_PROP   "_MOZILLA_VERSION"
static Atom XA_MOZILLA_VERSION  = 0;
bool CheckForNetscape(const char* cmd);
bool NetscapeIsRunning(Window *result);
bool NetscapeCheckWindow(Window window);
#endif // NETSCAPE_SUPPORT

#define MAX_SUBPROCESSES 32

static void OnSIGCHLD(int signum);

class UnixPostProcessor : public NormPostProcessor
{
    public:
        ~UnixPostProcessor();
        bool IsActive();
        bool ProcessFile(const char* path);
        void Kill();
        
        void ClearPid(int pid);
        
    private:
        friend class NormPostProcessor;
        UnixPostProcessor();
        bool IsActive(unsigned int array_element);
        unsigned int GetOpenProcessIdSlot();
        void Kill(unsigned int array_element);
        
        int     process_id[MAX_SUBPROCESSES];
#ifdef NETSCAPE_SUPPORT
        Window  window_id;
#endif // NETSCAPE_SUPPORT
};  // end class UnixPostProcessor 

UnixPostProcessor* theprocessor;

UnixPostProcessor::UnixPostProcessor()
 : process_id()
#ifdef NETSCAPE_SUPPORT
   ,window_id(0)
#endif // NETSCAPE_SUPPORT
{
    signal(SIGCHLD, OnSIGCHLD);
}

NormPostProcessor* NormPostProcessor::Create()
{
    //return static_cast<NormPostProcessor*>(new UnixPostProcessor);   
    theprocessor = new UnixPostProcessor;
    return static_cast<NormPostProcessor*>(theprocessor);
}  // end NormPostProcessor::Create()

UnixPostProcessor::~UnixPostProcessor()
{
    if (IsActive()) Kill();   
}

bool UnixPostProcessor::IsActive()
{
    for (unsigned int i=0;i<MAX_SUBPROCESSES;i++)
    {
        if (0 != process_id[i]) return true;
    }
    return false;
}

bool UnixPostProcessor::IsActive(unsigned int array_element)
{
    return (0 != process_id[array_element]);
}

unsigned int UnixPostProcessor::GetOpenProcessIdSlot()
{
    for (unsigned int i=0;i<MAX_SUBPROCESSES;i++)
    {
        if (0 == process_id[i]) return i;
    }
    //No available slot, kill something
    Kill(0);
    return 0;
}

bool UnixPostProcessor::ProcessFile(const char* path)
{
    const char** argv = (const char**)process_argv;
    int argc = process_argc;
#ifdef NETSCAPE_SUPPORT  
    // Special support for finding a Netscape window and
    // use the openURL remote command to display received file
#define USE_ACTIVE_WINDOW 0xffffffff  
    const char* myArgs[32];
    char wid_text[32], url_text[PATH_MAX+64];
    if (CheckForNetscape(argv[0]))
    {
        int i = 0;
        myArgs[i++] = process_argv[0];
        Window w = 0;
        // (TBD) We could put in a hack to look for the old
        // window if we made "w" static when "window_id" = 0xffffffff
        // This would help us capture the proper window for file display
        // It's still a hack but it might work a little better without
        // too much trouble
        if (NetscapeIsRunning(&w))
        {       
            // Use -remote command to display file in current
            // or new window
            if (window_id)
            {
                if (NetscapeCheckWindow(window_id))
                {
                    // Use the same window as last time
                    myArgs[i++] = "-id";
                    sprintf(wid_text, "0x%lx", window_id);
                    myArgs[i++] = wid_text;
                    myArgs[i++] = "-noraise";
                    myArgs[i++] = "-remote";
                    sprintf(url_text, "openURL(file://%s)", path);
                    myArgs[i++] = url_text;
                }
                else if (USE_ACTIVE_WINDOW == window_id)
                {
                    // Capture the open window we found
                    myArgs[i++] = "-id";
                    sprintf(wid_text, "0x%lx", w);
                    myArgs[i++] = wid_text;
                    window_id = w;
                    myArgs[i++] = "-noraise"; 
                    myArgs[i++] = "-remote";
                    sprintf(url_text, "openURL(file://%s)", path);
                    myArgs[i++] = url_text;         
                }
                else  // user must have closed old window so open another
                {
                    window_id = USE_ACTIVE_WINDOW;
                    //myArgs[i++] = "-raise";
                    myArgs[i++] = "-remote";
                    sprintf(url_text, "openURL(file://%s,new-window)", path);
                    myArgs[i++] = url_text;
                }                
                
            }
            else
            {
                // We're starting fresh, open a new window 
                window_id = USE_ACTIVE_WINDOW;
                //myArgs[i++] = "-raise";
                myArgs[i++] = "-remote";
                sprintf(url_text, "openURL(file://%s,new-window)", path);
                myArgs[i++] = url_text;
            }
            argv = myArgs;
     
        }
        else
        {
            //if (IsActive()) Kill();
            window_id = USE_ACTIVE_WINDOW;
            myArgs[i++] = path;
        }  // end if/else (NetscapeIsRunning())
        myArgs[i] = NULL;
        argc = i - 1;
        argv = myArgs;
    }
    else
#endif // NETSCAPE_SUPPORT
    {
        //if (IsActive()) Kill();
        // GetOpenProcessIdSlot() below will do this for us now...
        argv[argc] = path;
    }
    
    // 1) temporarily disable signal handling
    sighandler_t sigtermHandler = signal(SIGTERM, SIG_DFL);
    sighandler_t sigintHandler = signal(SIGINT, SIG_DFL);
    sighandler_t sigchldHandler = signal(SIGCHLD, SIG_DFL);
      
    unsigned int idnum = GetOpenProcessIdSlot();
    switch((process_id[idnum] = fork()))
    {
        case -1:    // error
            DMSG(0, "UnixPostProcessor::ProcessFile fork() error: %s\n", strerror(errno));
            process_id[idnum] = 0;
            process_argv[process_argc] = NULL;
            return false;

        case 0:     // child
            if (execvp((char*)argv[0], (char**)argv) < 0)
            {
		        DMSG(0, "UnixPostProcessor::ProcessFile execvp() error: %s\n", strerror(errno));
                exit(-1);
            }
            break;

        default:    // parent
            process_argv[process_argc] = NULL;
            // Restore signal handlers for parent
            signal(SIGTERM, sigtermHandler);
            signal(SIGINT, sigintHandler);
            // The use of "waitpid()" here is a work-around
            // for an IRIX SIGCHLD issue
            //int status;
            //while (waitpid(-1, &status, WNOHANG) > 0);
            signal(SIGCHLD, sigchldHandler);
            break;
    }
    return true;
}  // end UnixPostProcessor::ProcessFile()

void UnixPostProcessor::Kill()
{
    for (unsigned int i=0;i<MAX_SUBPROCESSES;i++)
    {
        if (IsActive(i)) Kill(i);
    }
}

void UnixPostProcessor::Kill(unsigned int array_element)
{
    if (!IsActive(array_element)) return;
    int count = 0;
    while((kill(process_id[array_element], SIGTERM) != 0) && count < 10)
	{ 
	    if (errno == ESRCH) break;
	    count++;
	    DMSG(0, "UnixPostProcessor::Kill kill() error: %s\n", strerror(errno));
	}
	count = 0;
    int status;
	while((waitpid(process_id[array_element], &status, 0) != process_id[array_element]) && count < 10)
	{
	    if (errno == ECHILD) break;
	    count++;
	    DMSG(0, "UnixPostProcessor::Kill waitpid() error: %s\n", strerror(errno));
	}
	process_id[array_element] = 0;
}  // end UnixPostProcessor::Kill()

void UnixPostProcessor::ClearPid(int pid)
{
    for (unsigned int i=0;i<MAX_SUBPROCESSES;i++)
    {
        if (pid == process_id[i]) process_id[i] = 0;
    }
}

static void OnSIGCHLD(int signum)
{
    // See if post processor exited itself
    int status;
    pid_t p;
    //int wait_rtn = wait(&status);
    while (true)
    {
        p = waitpid(-1, &status, WNOHANG);
        if (p == -1)
        {
           if (errno == EINTR) continue;
           break;
	}
	// check for no more children
        else if (p == 0) break;
	// else handle returned PID
	theprocessor->ClearPid(p);
    }
}

#ifdef NETSCAPE_SUPPORT
bool CheckForNetscape(const char* cmd)
{
    // See if it's Netscape post processing 
    // for which we provide special support
    char txt[8];
    const char *ptr = strrchr(cmd, PROTO_PATH_DELIMITER);
    if (ptr)
        ptr++;
    else
        ptr = cmd;
    strncpy(txt, ptr, 8);
    for (int i=0; i<8; i++) txt[i] = toupper(txt[i]);
    if (!strncmp(txt, "NETSCAPE", 8)) 
        return true;
    else
        return false;
}  // end CheckForNetscape()

bool NetscapeIsRunning(Window *result)
{
    int i;
    
    Window root2, parent, *kids;
    unsigned int nkids;
    *result = 0;
    
    Display *dpy = XOpenDisplay(NULL);
    if (!dpy) 
    {
        fprintf(stderr, "mdp: XOpenDisplay() error\n");
        return false;
    }
    
    if (!XA_MOZILLA_VERSION) 
        XA_MOZILLA_VERSION = XInternAtom(dpy, MOZILLA_VERSION_PROP, False);
    
    Window root = RootWindowOfScreen(DefaultScreenOfDisplay(dpy));
    
    if (!XQueryTree (dpy, root, &root2, &parent, &kids, &nkids))
    {
        fprintf(stderr, "mdp: XQueryTree failed on display %s\n", DisplayString (dpy));
        XCloseDisplay(dpy);
        return false;
    }

    // Note: root != root2 is possible with virtual root WMs.
    if (!(kids && nkids))
    {
        fprintf(stderr, "mdp: root window has no children on display %s\n",
             DisplayString (dpy));
        XCloseDisplay(dpy);
        return false;
    }

    for (i = nkids-1; i >= 0; i--)
    {
        Atom type;
        int format;
        unsigned long nitems, bytesafter;
        unsigned char *version = 0;
        Window w = XmuClientWindow (dpy, kids[i]);
        int status = XGetWindowProperty (dpy, w, XA_MOZILLA_VERSION,
			           0, (65536 / sizeof (long)),
			           False, XA_STRING,
			           &type, &format, &nitems, &bytesafter,
			           &version);
        if (! version) continue;
        XFree (version);
        if (status == Success && type != None)
	    {
	        *result = w;
	        break;
	    }
    }
    XCloseDisplay(dpy);       
    return (*result ? true : false);
  
}  // end NetscapeIsRunning()

bool NetscapeCheckWindow(Window window)
{
    int i;   
    Window root2, parent, *kids;
    unsigned int nkids;
    
    Display *dpy = XOpenDisplay(NULL);
    if (!dpy) 
    {
        fprintf(stderr, "mdp: XOpenDisplay() error\n");
        return false;
    }
    
    if (!XA_MOZILLA_VERSION) 
        XA_MOZILLA_VERSION = XInternAtom(dpy, MOZILLA_VERSION_PROP, False);
    
    Window root = RootWindowOfScreen(DefaultScreenOfDisplay (dpy));
    
    if (!XQueryTree (dpy, root, &root2, &parent, &kids, &nkids))
    {
        fprintf(stderr, "mdp: XQueryTree failed on display %s\n", DisplayString (dpy));
        XCloseDisplay(dpy);
        return false;
    }

    // Note: root != root2 is possible with virtual root WMs.
    if (!(kids && nkids)) return false;

    for (i = nkids-1; i >= 0; i--)
    {
        Atom type;
        int format;
        unsigned long nitems, bytesafter;
        unsigned char *version = 0;
        Window w = XmuClientWindow (dpy, kids[i]);
        int status = XGetWindowProperty (dpy, w, XA_MOZILLA_VERSION,
			           0, (65536 / sizeof (long)),
			           False, XA_STRING,
			           &type, &format, &nitems, &bytesafter,
			           &version);
        if (!version) continue;
        XFree (version);
        if (status == Success && type != None)
	    {
	        if (w == window)
            {
                XCloseDisplay(dpy);
                return true;
            }
            else
            {
                continue;
            }
	    }
    }
    XCloseDisplay(dpy);
    return false;
    
}  // end NetscapeCheckWindow()
#endif // NETSCAPE_SUPPORT