File: WcInvoke.c

package info (click to toggle)
moxftp 2.2-18.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,160 kB
  • ctags: 3,837
  • sloc: ansic: 43,420; makefile: 353; perl: 262; sh: 192
file content (293 lines) | stat: -rw-r--r-- 8,005 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <X11/Wc/COPY>

/*
* SCCS_data: @(#) WcInvoke.c 1.5 92/03/18 11:02:21
*
* Widget Creation Library - WcInvoke.c
*
* This module implements functions which allow an action to easily invoke 
* an equivalent callback, and a callback to easily invoke an equivalent
* action.

* WcInvokeCallback converts action arguments (params and num_params) into
* callback arguments (a single string).  Any params with whitespace are 
* enclosed in double quotes( "like this").  The params are separated by single
* blanks.  It then invokes the XtCallbackProc.

* WcInvokeAction converts callback arguments (a single string) into action 
* arguments (params[] and num_params).  It then invokes the XtActionProc.
* The clientData string is broken up on whitespace and commas.  Repeated
* commas with only whitespace between causes NULL params.

* WcInvokeNamedAction does the same, but it invokes XtCallAction to 
* allow actions to be named, rather than requiring a pointer to an 
* XtActionProc.

*******************************************************************************
*/

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Wc/WcCreateP.h>

/*  -- Conversion Methods
=========================
*/

static char* WcxCvtParamsToClientData ( params, num_params )
    char**    params;
    Cardinal* num_params;
{
    char *cp, *pp, *clientData;
    int  len, par, needToQuote;

    if ( *num_params == 0 || params == NULL )
	return NULL;

    /* Compute the total length of the required equivalent string.
    ** Note that we need a separating blank, and we may need to
    ** quote the arg, hence the `+ 3' as we increment the len.
    */
    for ( len = par = 0  ;  par < *num_params  ;  par++ )
	if ( *(pp = params[par]) != '\0' )
	    for ( len +=3  ;  *pp  ;  pp++ )
		++len; /*looking for end of each parameter*/

    clientData = cp = (char*)XtMalloc( len+1 );

    /* Concatenate and possibly quote parameters into the clientData.
    */
    for ( par = 0  ;  par < *num_params  ;  par++ )
    {
	/* See if there are any special chars in
	** the param[par] which will require quoting.
	*/
	for ( needToQuote = 0, pp = params[par]  ;  *pp  ;  pp++ )
	{
	    if ( *pp == '\t' || *pp == ')' || *pp == ' '
	      || *pp == '\n' || *pp == ',' )
            {
		needToQuote++;
		break;
	    }
	}
	if ( needToQuote ) *cp++ = '"';

	for ( pp = params[par]  ;  *pp  ;  pp++ )
	    *cp++ = *pp;

	if ( needToQuote ) *cp++ = '"';

	*cp++ = ' ';
    }
    *--cp = '\0';	/* change final blank character to NUL */

    return clientData;
}

typedef struct _WcxParamsStruct {
    char**	params;
    Cardinal	num_params;
} WcxParamsStruct, *WcxParams;

static void WcxCvtClientDataToParams ( widget, clientData, wcxParams )
    Widget	widget;
    char*	clientData;
    WcxParams	wcxParams;	/* values filled in and returned */
{
    char*	params[MAX_ARGS];	/* must copy into alloc'd params[] */
    Cardinal	num_params	= 0;
    char*	data		= clientData;
    int		len;
    char	*cp, *end, quote;

    /* Skip initial whitespace.
    */
    while (*data  == ' ' || *data == '\t' || *data == '\n')
        data++;

    while ( *data && num_params < MAX_ARGS )
    {
	/* Check first character of each argument.
	*/
	switch (*data)
	{
	case ',' :
	    /* -- Null Argument is NULL param - NOT XtMalloc'd !!
	    */
	    params[num_params] = NULL;
	    data++;				/* eat the comma */
	    break;

	case '\"' : case '\'' :
	    /* -- Quoted argument: drop the quotes.
	    */
	    quote = *data;
	    data++;				/* skip quote */

	    for ( len = 0, end = data ; *end && *end != quote ; end++ )
		++len; /* find length and end of quoted string */

	    if (len == 0)
	    {
		/* nothing inside quotes is same as null argument.
		*/
		params[num_params] = NULL;
	    }
	    else
	    {
		/* end points to either the quote or the NULL terminator.
		*/
		cp = params[num_params] = XtMalloc( len+1 );
		while ( data < end )
		    *cp++ = *data++;
		*cp = '\0';
	    }

	    /* Now data points to either the quote or the NULL terminator.
	    */
	    if ( *data == NULL )
	    {
		WcWARN1( widget, "WcInvokeAction", "unbalQuote",
			"SomeCallback(%s) - unbalanced quotes.", clientData );
	    }
	    else
	    {
		/* Skip trailing quote, whitespace, optional comma:
		*/
		data++;
		while (*data && *data  == ' ' || *data == '\t' || *data == '\n')
		    data++;
		if ( *data == ',')
		    data++;
	    }

	    break;

	default:
	    /* -- Non-quoted, non-null argument.  Always take first character,
	    ** then everything up to whitespace or parens as the parameter.
	    */
	    for  ( len = 1, end = data, end++ ; 
		   *end && *end != '\t' && *end != '\n' && *end != ' '
			&& *end != ','  && *end != '('  && *end != ')' ; )
		end++, len++;
	    cp = params[num_params] = XtMalloc( len+1 );
	    while ( data < end )
		*cp++ = *data++;
	    *cp = '\0';

	    /* skip whitespace, optional comma
	    */
	    while (*data && *data  == ' ' || *data == '\t' || *data == '\n')
		data++;
	    if ( *data == ',')
		data++;

	    break;
	}

	/* skip whitespace which may follow optional comma we've already skipped
	*/
	while (*data && *data  == ' ' || *data == '\t' || *data == '\n')
	    data++;

	++num_params;
    }
    wcxParams->num_params = num_params;

    if ( 0 == num_params )
	wcxParams->params = (char**)NULL;
    else
    {
	/* Malloc storage for params, and copy from automatic params.
	*/
	wcxParams->params = (char**)XtMalloc(sizeof(char*) * num_params);

	while( num_params-- )
	wcxParams->params[num_params] = params[num_params];
    }
}

/*
*******************************************************************************
* Public definitions of WcInvokeCallback, WcInvokeAction, WcInvokeNamedAction
*******************************************************************************
*/

/*  -- Invoke XtActionProc from XtCallbackProc 
*******************************************************************************
*/

void WcInvokeCallback( Callback, widget, params, num_params )
    XtCallbackProc	Callback;
    Widget   		widget;
    char**		params;
    Cardinal*		num_params;
{
    char* clientData = WcxCvtParamsToClientData( params, num_params );

    Callback( widget, clientData, (XtPointer)NULL );

    if ( NULL != clientData )
	XtFree( clientData );
}

/*  -- Invoke XtActionProc from XtCallbackProc 
*******************************************************************************
*/

void WcInvokeAction( Action, widget, clientData )
    XtActionProc Action;
    Widget       widget;
    char*        clientData;
{
    WcxParamsStruct	wcxParams;
    Cardinal		num_params;	/* must copy cuz Action can change */

    WcxCvtClientDataToParams( widget, clientData, &wcxParams );
    num_params = wcxParams.num_params;

    Action( widget, (XEvent*)NULL, wcxParams.params, &num_params );

    if ( NULL != wcxParams.params )
    {
	while ( wcxParams.num_params-- )
	    if ( NULL != wcxParams.params[wcxParams.num_params] )
		XtFree( wcxParams.params[wcxParams.num_params] );
	XtFree( (char*)wcxParams.params );
    }
}

/*  -- Invoke Named XtActionProc from XtCallbackProc 
*******************************************************************************
*/

void WcInvokeNamedAction( actionName, widget, clientData )
    char*  actionName;
    Widget widget;
    char*  clientData;
{
#ifdef XtSpecificationRelease
    WcxParamsStruct	wcxParams;
    Cardinal		num_params;	/* must copy cuz Action can change */

    WcxCvtClientDataToParams( widget, clientData, &wcxParams );
    num_params = wcxParams.num_params;

    XtCallActionProc( widget, actionName,
		      (XEvent*)NULL, wcxParams.params, num_params );

    if ( NULL != wcxParams.params )
    {
	while ( wcxParams.num_params-- )
	    if ( NULL != wcxParams.params[wcxParams.num_params] )
		XtFree( wcxParams.params[wcxParams.num_params] );
	XtFree( (char*)wcxParams.params );
    }
#else
    WcWARN1( widget, "WcInvokeNamedAction", "oldXt",
	   "WcInvokeNamedAction(%s) requires Xt version 4.0 or later.",
	    actionName );
#endif
}