File: plserver.c

package info (click to toggle)
plplot 5.15.0%2Bdfsg-19
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 31,312 kB
  • sloc: ansic: 79,707; xml: 28,583; cpp: 20,033; ada: 19,456; tcl: 12,081; f90: 11,431; ml: 7,276; java: 6,863; python: 6,792; sh: 3,274; perl: 828; lisp: 75; makefile: 50; sed: 34; fortran: 5
file content (333 lines) | stat: -rw-r--r-- 10,191 bytes parent folder | download | duplicates (5)
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
//  Copyright 1993, 1994, 1995
//  Maurice LeBrun			mjl@dino.ph.utexas.edu
//  Institute for Fusion Studies	University of Texas at Austin
//
//  Copyright (C) 2004  Joao Cardoso
//
//  This file is part of PLplot.
//
//  PLplot is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Library General Public License as published
//  by the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  PLplot is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Library General Public License for more details.
//
//  You should have received a copy of the GNU Library General Public License
//  along with PLplot; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//--------------------------------------------------------------------------
//
//  PLplot graphics server.
//
//  Just a front-end to the pltkMain() function.  Structured along the
//  preferred lines for extended wish'es.  Is typically run as a child
//  process from the PLplot TK driver to render output.  Can use either TK
//  send or Tcl-DP RPC for communication, depending on how it is invoked.
//
//  Note that plserver can be used the same way as wish or dpwish, as it
//  contains the functionality of each of these (except the -notk Tcl-DP
//  command-line option is not supported).
//

#define NEED_PLDEBUG
#include "plserver.h"

// Application-specific command-line options
// Variable declarations

static char        *client_name; // Name of client main window
static char        *auto_path;   // addition to auto_path
static int         child;        // set if child of TK driver
#ifdef PLD_dp
static int         dp;           // set if using Tcl-DP to communicate
#endif
static char        *client_host; // Host id for client
static char        *client_port; // Communications port id for client

static Tk_ArgvInfo argTable[] = {
    { "-client_name", TK_ARGV_STRING,   (char *) NULL, (char *) &client_name,
      "Client main window name to connect to" },
    { "-client_host", TK_ARGV_STRING,   (char *) NULL, (char *) &client_host,
      "Client host to connect to" },
    { "-client_port", TK_ARGV_STRING,   (char *) NULL, (char *) &client_port,
      "Client port (Tcl-DP) to connect to" },
    { "-auto_path",   TK_ARGV_STRING,   (char *) NULL, (char *) &auto_path,
      "Additional directory(s) to autoload" },
    { "-child",       TK_ARGV_CONSTANT, (char *) 1,    (char *) &child,
      "Set ONLY when child of PLplot TK driver" },
    { (char *) NULL,  TK_ARGV_END,      (char *) NULL, (char *) NULL,
      (char *) NULL }
};

// PLplot/Tk extension command -- handle exit.

static int
plExitCmd( ClientData clientData, Tcl_Interp *interp, int argc, char **argv );

// Evals the specified command, aborting on an error.

static void
tcl_cmd( Tcl_Interp *interp, const char *cmd );

// Application-specific startup

static int
AppInit( Tcl_Interp *interp );

//--------------------------------------------------------------------------
// main --
//
// Just a stub routine to call pltkMain.  The latter is nice to have
// when building extended wishes, since then you don't have to rely on
// sucking the Tk main out of libtk (which doesn't work correctly on all
// systems/compilers/linkers/etc).  Hopefully in the future Tk will
// supply a sufficiently capable tkMain() type function that can be used
// instead.
//--------------------------------------------------------------------------

int
main( int argc, const char **argv )
{
    int        i, myargc = argc;
    const char **myargv;
    Tcl_Interp *interp;
    const char *helpmsg = "Command-specific options:";

#ifdef DEBUG
    fprintf( stderr, "Program %s called with arguments :\n", argv[0] );
    for ( i = 1; i < argc; i++ )
    {
        fprintf( stderr, "%s ", argv[i] );
    }
    fprintf( stderr, "\n" );
#endif

// Create interpreter just for argument parsing

    interp = Tcl_CreateInterp();

// Save arglist to get around Tk_ParseArgv limitations

#ifdef DEBUG
    fprintf( stderr, "Before myargv\n" );
#endif

    // According to both Linux and Windows documentation,
    // argv is actually argc+1 in length with the last element set to
    // the NULL value.  So leave room for that last element.
    myargv = (const char **) malloc( ( argc + 1 ) * sizeof ( char * ) );
    for ( i = 0; i < argc + 1; i++ )
    {
        myargv[i] = argv[i];
    }

#ifdef DEBUG
    fprintf( stderr, "After myargv\n" );
#endif

// Parse args
// Examine the result string to see if an error return is really an error

    if ( Tk_ParseArgv( interp, (Tk_Window) NULL, &argc, argv,
             argTable, TK_ARGV_NO_DEFAULTS ) != TCL_OK )
    {
#ifdef DEBUG
        fprintf( stderr, "Error in Tk_ParseArgv\n" );
#endif
        fprintf( stderr, "\n(plserver) %s\n\n", Tcl_GetStringResult( interp ) );
        fprintf( stderr, "\
The client_<xxx> and -child options should not be used except via the\n\
PLplot/Tk driver.\n\n(wish) " );
#ifdef DEBUG
        fprintf( stderr, "Before Tcl_SetResult\n" );
#endif
        Tcl_SetResult( interp, (char *) helpmsg, TCL_VOLATILE );
    }

#ifdef DEBUG
    fprintf( stderr, "After Tk_ParseArgv\n" );
#endif

// No longer need interpreter

#if TCL_MAJOR_VERSION < 7 || ( TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION < 5 )
    Tcl_DeleteInterp( interp );
#endif

// Call pltkMain() with original argc/argv list, to make sure -h is seen
// Does not return until program exit

    exit( pltkMain( myargc, myargv, NULL, AppInit ) );
}


//
//--------------------------------------------------------------------------
//
// AppInit --
//
//	This procedure performs application-specific initialization.
//	Most applications, especially those that incorporate additional
//	packages, will have their own version of this procedure.
//
// Results:
//	Returns a standard Tcl completion code, and leaves an error
//	message in interp->result if an error occurs.
//
// Side effects:
//	Depends on the startup script.
//
//--------------------------------------------------------------------------
//

static int
AppInit( Tcl_Interp *interp )
{
    Tk_Window mainWindow = Tk_MainWindow( interp );

//
// Call the init procedures for included packages.  Each call should
// look like this:
//
// if (Mod_Init(interp) == TCL_ERROR) {
//     return TCL_ERROR;
// }
//
// where "Mod" is the name of the module.
//
    if ( Pltk_Init( interp ) == TCL_ERROR )
    {
        return TCL_ERROR;
    }

// Application-specific startup.  That means: for use in plserver ONLY.

// Pass child variable to interpreter if set.

    if ( child != 0 )
        Tcl_SetVar( interp, "child", "1", 0 );

// If client_name is set, TK send is being used to communicate.
// If client_port is set, Tcl-DP RPC is being used to communicate.
// The "dp" variable determines which style communication is used

    if ( client_name != NULL )
    {
        Tcl_SetVar( interp, "client_name", client_name, 0 );
        tcl_cmd( interp, "set dp 0" );
#ifdef PLD_dp
        dp = 0;
#endif
    }
    else if ( client_port != NULL )
    {
#ifdef PLD_dp
        Tcl_SetVar( interp, "client_port", client_port, 0 );
        if ( client_host != NULL )
            Tcl_SetVar( interp, "client_host", client_host, 0 );
        dp = 1; tcl_cmd( interp, "set dp 1" );
#else
        Tcl_AppendResult( interp,
            "no Tcl-DP support in this version of plserver",
            (char *) NULL );
        return TCL_ERROR;
#endif
    }

// Add user-specified directory(s) to auto_path

    if ( auto_path != NULL )
    {
        Tcl_SetVar( interp, "dir", auto_path, 0 );
        tcl_cmd( interp, "lappend auto_path $dir" );
    }

// Rename "exit" to "tkexit", and insert custom exit handler

    tcl_cmd( interp, "rename exit tkexit" );

    Tcl_CreateCommand( interp, "exit", (Tcl_CmdProc *) plExitCmd,
        (ClientData) mainWindow, (Tcl_CmdDeleteProc *) NULL );

    return TCL_OK;
}

//--------------------------------------------------------------------------
// plExitCmd
//
// PLplot/Tk extension command -- handle exit.
// The reason for overriding the normal exit command is so we can tell the
// client to abort.
//--------------------------------------------------------------------------

static int
plExitCmd( ClientData PL_UNUSED( clientData ), Tcl_Interp *interp, int argc, char **argv )
{
    int        value = 0;
    const char *res;

// Print error message if one given

    res = Tcl_GetStringResult( interp );
    if ( res[0] != '\0' )
        fprintf( stderr, "%s\n", res );

// Best to check the syntax before proceeding

    if ( ( argc != 1 ) && ( argc != 2 ) )
    {
        Tcl_AppendResult( interp, "wrong # args: should be \"", argv[0],
            " ?returnCode?\"", (char *) NULL );
        return TCL_ERROR;
    }
    if ( ( argc != 1 ) && ( Tcl_GetInt( interp, argv[1], &value ) != TCL_OK ) )
    {
        Tcl_AppendResult( interp, "non-integer return code: \"", argv[1],
            "\"", (char *) NULL );
        return TCL_ERROR;
    }

// If client exists, tell it to self destruct

    Tcl_VarEval( interp, "plserver_link_end", (char **) NULL );

// Now really exit
// (Note: this function is actually deprecated, but as it is only used here
// at the end of the program, let's leave it.)

    if ( argc == 1 )
    {
        return Tcl_VarEval( interp, "tkexit", (char **) NULL );
    }
    else
    {
        return Tcl_VarEval( interp, "tkexit", argv[1], (char **) NULL );
    }
}

//--------------------------------------------------------------------------
// tcl_cmd
//
// Evals the specified command, aborting on an error.
//--------------------------------------------------------------------------

static void
tcl_cmd( Tcl_Interp *interp, const char *cmd )
{
    int result;

    dbug_enter( "tcl_cmd" );
    pldebug( "tcl_cmd", "evaluating command %s\n", cmd );

    result = Tcl_VarEval( interp, cmd, (char **) NULL );
    if ( result != TCL_OK )
    {
        Tcl_Eval( interp, "exit" );
        exit( 1 );
    }
}