File: Glib.xs

package info (click to toggle)
libglib-perl 3:1.329.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,320 kB
  • sloc: perl: 4,950; ansic: 923; makefile: 7
file content (601 lines) | stat: -rw-r--r-- 16,451 bytes parent folder | download | duplicates (3)
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
 * Copyright (C) 2003-2005, 2012-2013 by the gtk2-perl team (see the file
 * AUTHORS for the full list)
 *
 * This library 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.1 of the License, or (at your
 * option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * $Id$
 */

#include "gperl.h"

=head2 Miscellaneous

Various useful utilities defined in Glib.xs.

=over

=item GPERL_CALL_BOOT(name)

call the boot code of a module by symbol rather than by name.

in a perl extension which uses several xs files but only one pm, you
need to bootstrap the other xs files in order to get their functions
exported to perl.  if the file has MODULE = Foo::Bar, the boot symbol
would be boot_Foo__Bar.

=item void _gperl_call_XS (pTHX_ void (*subaddr) (pTHX_ CV *), CV * cv, SV ** mark);

never use this function directly.  see C<GPERL_CALL_BOOT>.

for the curious, this calls a perl sub by function pointer rather than
by name; call_sv requires that the xsub already be registered, but we
need this to call a function which will register xsubs.  this is an
evil hack and should not be used outside of the GPERL_CALL_BOOT macro.
it's implemented as a function to avoid code size bloat, and exported
so that extension modules can pull the same trick.

=cut
void
_gperl_call_XS (pTHX_ void (*subaddr) (pTHX_ CV *), CV * cv, SV ** mark)
{
	dSP;
	PUSHMARK (mark);
	(*subaddr) (aTHX_ cv);
	PUTBACK;	/* forget return values */
}


=item gpointer gperl_alloc_temp (int nbytes)

Allocate and return a pointer to an I<nbytes>-long, zero-initialized,
temporary buffer that will be reaped at the next garbage collection sweep.
This is handy for allocating things that need to be alloc'ed before a croak
(since croak doesn't return and give you the chance to free them).  The
trick is that the memory is allocated in a mortal perl scalar.  See the
perl online manual for notes on using this technique.

Do B<not> under any circumstances attempt to call g_free(), free(), or any
other deallocator on this pointer, or you will crash the interpreter.

=cut
/*
 * taken from pgtk_alloc_temp in Gtk-Perl-0.7008/Gtk/MiscTypes.c
 */
gpointer
gperl_alloc_temp (int nbytes)
{
	dTHR;
	SV * s;

	g_return_val_if_fail (nbytes > 0, NULL);

	s = sv_2mortal (NEWSV (0, nbytes));
	memset (SvPVX (s), 0, nbytes);
	return SvPVX (s);
}

=item gchar *gperl_filename_from_sv (SV *sv)

Return a localized version of the filename in the sv, using
g_filename_from_utf8 (and consequently this function might croak). The
memory is allocated using gperl_alloc_temp.

=cut
gchar *
gperl_filename_from_sv (SV *sv)
{
        dTHR;

        GError *error = NULL;
        gchar *lname = NULL;
        gsize output_length = 0;
        STRLEN input_length = 0;
        gchar *filename = SvPVutf8 (sv, input_length);

        lname = g_filename_from_utf8 (filename, input_length,
                                      0, &output_length, &error);
        if (!lname)
        	gperl_croak_gerror (NULL, error);

        filename = gperl_alloc_temp (output_length + 1);
        memcpy (filename, lname, output_length);
        g_free (lname);

        return filename;
}

=item SV *gperl_sv_from_filename (const gchar *filename)

Convert the filename into an utf8 string as used by gtk/glib and perl.

=cut
SV *
gperl_sv_from_filename (const gchar *filename)
{
	GError *error = NULL;
        SV *sv;
	gsize len;
        gchar *str = g_filename_to_utf8 (filename, -1, NULL, &len, &error);

        if (!str)
        	gperl_croak_gerror (NULL, error);

        sv = newSVpv (str, len);
        g_free (str);

        SvUTF8_on (sv);
        return sv;
}

=item gboolean gperl_str_eq (const char * a, const char * b);

Compare a pair of ascii strings, considering '-' and '_' to be equivalent.
Used for things like enum value nicknames and signal names.

=cut
gboolean
gperl_str_eq (const char * a,
              const char * b)
{
	while (*a && *b) {
		if (*a == *b ||
		    ((*a == '-' || *a == '_') && (*b == '-' || *b == '_'))) {
			a++;
			b++;
		} else
			return FALSE;
	}
	return *a == *b;
}

=item guint gperl_str_hash (gconstpointer key)

Like g_str_hash(), but considers '-' and '_' to be equivalent.

=cut
guint
gperl_str_hash (gconstpointer key)
{
	const char *p = key;
	guint h = *p;

	if (h)
		for (p += 1; *p != '\0'; p++)
			h = (h << 5) - h + (*p == '-' ? '_' : *p);

	return h;
}

/* --- GPerlArgv ----------------------------------------------------------- */

typedef struct {
	/* Shadow copies of the pointers to the copies of the strings in argv.
	 * Used to free the copied strings reliably even if they are removed
	 * from argv. */
	char **shadows;
	 /* Hash table (pointer (not string) -> utf8 flag) so we can completely
	  * restore PVs from the strings.  We cannot simply use an array of
	  * utf8 flags because strings might be removed from argv, in which
	  * case we wouldn't know which entry in the utf8 flag array
	  * corresponds to which string. */
	GHashTable *utf8_flags;
} GPerlArgvPriv;

=item GPerlArgv * gperl_argv_new ()

Creates a new Perl argv object whose members can then be passed to functions
that request argc and argv style arguments.

If the called function(s) modified argv, you can call L<gperl_argv_update> to
update Perl's @ARGV in the same way.

Remember to call L<gperl_argv_free> when you're done.

=cut
GPerlArgv*
gperl_argv_new ()
{
	AV * ARGV;
	SV * ARGV0;
	int len, i;
	GPerlArgv *pargv;
	GPerlArgvPriv *priv;

	pargv = g_new (GPerlArgv, 1);

	/*
	 * heavily borrowed from gtk-perl.
	 *
	 * given the way perl handles the refcounts on SVs and the strings
	 * to which they point, i'm not certain that the g_strdup'ing of
	 * the string values is entirely necessary; however, this compiles
	 * and runs and doesn't appear either to leak or segfault, so i'll
	 * leave it.
	 */

	ARGV = get_av ("ARGV", FALSE);
	ARGV0 = get_sv ("0", FALSE);

	/* 
	 * construct the argv argument... we'll have to prepend @ARGV with $0
	 * to make it look real.  an important wrinkle: client code may strip
	 * arguments it processes without freeing them (argv is statically
	 * allocated in conventional usage).  thus, we need to keep a shadow
	 * copy of argv so we can keep from leaking the stripped strings.
	 */

	len = av_len (ARGV) + 1;

	pargv->argc = len + 1;
	pargv->argv = g_new0 (char*, pargv->argc);

	priv = g_new (GPerlArgvPriv, 1);
	priv->shadows = g_new0 (char*, pargv->argc);
	priv->utf8_flags = g_hash_table_new (NULL, NULL);
	pargv->priv = priv;

	pargv->argv[0] = SvPV_nolen (ARGV0);

	for (i = 0 ; i < len ; i++) {
		SV ** svp = av_fetch (ARGV, i, 0);
		if (svp && gperl_sv_is_defined (*svp)) {
			const char *arg = SvPV_nolen (*svp);
			gboolean utf8_flag = !!SvUTF8 (*svp);
			priv->shadows[i] = pargv->argv[i+1]
			                 = g_strdup (arg);
			g_hash_table_insert (priv->utf8_flags,
			                     pargv->argv[i+1],
			                     GINT_TO_POINTER (utf8_flag));
		}
	}

	return pargv;
}

=item void gperl_argv_update (GPerlArgv *pargv)

Updates @ARGV to resemble the stored argv array.

=cut
void
gperl_argv_update (GPerlArgv *pargv)
{
	GPerlArgvPriv *priv = pargv->priv;
	AV * ARGV;
	int i;

	ARGV = get_av ("ARGV", FALSE);

	/* clear and refill @ARGV with whatever gtk_init didn't steal. */
	av_clear (ARGV);
	for (i = 1 ; i < pargv->argc ; i++) {
		SV *sv;
		const char *arg = pargv->argv[i];
		gboolean utf8_flag = !!g_hash_table_lookup (priv->utf8_flags, arg);
		sv = newSVpv (arg, 0);
		if (utf8_flag)
			SvUTF8_on (sv);
		av_push (ARGV, sv);
	}
}

=item void gperl_argv_free (GPerlArgv *pargv)

Frees any resources associated with I<pargv>.

=cut
void
gperl_argv_free (GPerlArgv *pargv)
{
	GPerlArgvPriv *priv = pargv->priv;
	g_strfreev (priv->shadows);
	g_hash_table_destroy (priv->utf8_flags);
	g_free (pargv->priv);
	g_free (pargv->argv);
	g_free (pargv);
}

/* ------------------------------------------------------------------------- */

=item char * gperl_format_variable_for_output (SV * sv)

Formats the variable stored in I<sv> for output in error messages.  Like
SvPV_nolen(), but ellipsizes real strings (i.e., not stringified references)
at 20 chars to trim things down for error messages.

=cut
char *
gperl_format_variable_for_output (SV * sv)
{
	if (sv) {
		/* disambiguate undef */
		if (!gperl_sv_is_defined (sv))
			return SvPV_nolen (sv_2mortal (newSVpv ("undef", 5)));
		/* don't truncate references... */
		if (SvROK (sv))
			return SvPV_nolen (sv);
		/* and quote everything else to disambiguate empty strings
		 * and the like. */
		return form (sv_len (sv) > 20 ? "`%.20s...'" : "`%s'",
		             SvPV_nolen (sv));
	}

	return NULL;
}

=item gboolean gperl_sv_is_defined (SV *sv)

Checks the SV I<sv> for definedness just like Perl's I<defined()> would do.
Most importantly, it correctly handles "magical" SVs, unlike bare I<SvOK>.
It's also NULL-safe.

=cut
gboolean
gperl_sv_is_defined (SV *sv)
{
	/* This is adapted from PP(pp_defined) in perl's pp.c */

	if (!sv || !SvANY(sv))
		return FALSE;

	switch (SvTYPE(sv)) {
	    case SVt_PVAV:
		if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
		    || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
			return TRUE;
		break;
	    case SVt_PVHV:
		if (HvARRAY(sv) || SvGMAGICAL(sv)
		    || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
			return TRUE;
		break;
	    case SVt_PVCV:
		if (CvROOT(sv) || CvXSUB(sv))
			return TRUE;
		break;
	    default:
		if (SvGMAGICAL(sv))
			mg_get(sv);
		if (SvOK(sv))
			return TRUE;
	}

	return FALSE;
}

=item void gperl_hv_take_sv (HV *hv, const char *key, size_t key_length, SV *sv)

Tries to store I<sv> in I<hv>.  Decreases I<sv>'s reference count if something
goes wrong.

=cut
void
gperl_hv_take_sv (HV *hv, const char *key, size_t key_length, SV *sv)
{
	if (!hv_store (hv, key, key_length, sv, 0)) {
		sv_free (sv);
	}
}

=back

=cut

/*
 * Thread-safety stuff.
 */
static PerlInterpreter *gperl_master_interp = NULL;
G_LOCK_DEFINE_STATIC (gperl_master_interp);

void
_gperl_set_master_interp (PerlInterpreter *interp)
{
	G_LOCK (gperl_master_interp);
	gperl_master_interp = interp;
	G_UNLOCK (gperl_master_interp);
}

PerlInterpreter *
_gperl_get_master_interp (void)
{
	return gperl_master_interp;
}

#ifndef PERL_IMPLICIT_CONTEXT
/* If perl doesn't use thread-local storage, then we store the main thread's ID
 * at BOOT time so that GClosure.xs can later find out whether we've been
 * called from a foreign thread. */
static GThread *gperl_main_tid = NULL;

static void
_gperl_fetch_main_tid (void)
{
	gperl_main_tid = g_thread_self ();
}

GThread *
_gperl_get_main_tid (void)
{
	return gperl_main_tid;
}
#endif

MODULE = Glib		PACKAGE = Glib		PREFIX = g_

BOOT:
#if !GLIB_CHECK_VERSION (2, 32, 0) && defined(G_THREADS_ENABLED) && !defined(GPERL_DISABLE_THREADSAFE)
	/* g_thread_init() is a deprecated no-op */
	/*warn ("calling g_thread_init (NULL)");*/
	if (!g_thread_supported ())
		g_thread_init (NULL);
#endif
#if !GLIB_CHECK_VERSION (2, 36, 0)
	/* g_type_init() is a deprecated no-op */
	g_type_init ();
#endif
	_gperl_set_master_interp (PERL_GET_INTERP);
#ifndef PERL_IMPLICIT_CONTEXT
	_gperl_fetch_main_tid ();
#endif
	/* boot all in one go.  other modules may not want to do it this
	 * way, if they prefer instead to perform demand loading. */
	GPERL_CALL_BOOT (boot_Glib__Utils);
	GPERL_CALL_BOOT (boot_Glib__Error);
	GPERL_CALL_BOOT (boot_Glib__Log);
	GPERL_CALL_BOOT (boot_Glib__Type);
	GPERL_CALL_BOOT (boot_Glib__Boxed);
	GPERL_CALL_BOOT (boot_Glib__Object);
	GPERL_CALL_BOOT (boot_Glib__Signal);
	GPERL_CALL_BOOT (boot_Glib__Closure);
	GPERL_CALL_BOOT (boot_Glib__MainLoop);
	GPERL_CALL_BOOT (boot_Glib__ParamSpec);
	GPERL_CALL_BOOT (boot_Glib__IO__Channel);
#if GLIB_CHECK_VERSION (2, 6, 0)
	GPERL_CALL_BOOT (boot_Glib__KeyFile);
	GPERL_CALL_BOOT (boot_Glib__Option);
#endif /* GLIB_CHECK_VERSION (2, 6, 0) */
#if GLIB_CHECK_VERSION (2, 12, 0)
	GPERL_CALL_BOOT (boot_Glib__BookmarkFile);
#endif /* GLIB_CHECK_VERSION (2, 12, 0) */
#if GLIB_CHECK_VERSION (2, 24, 0)
	GPERL_CALL_BOOT (boot_Glib__Variant);
#endif /* GLIB_CHECK_VERSION (2, 24, 0) */
	/* make sure that we're running/linked against a version at least as 
	 * new as we built against, otherwise bad things will happen. */
/*
	if ((((int)glib_major_version) < GLIB_MAJOR_VERSION)
	    ||
	    (glib_major_version == GLIB_MAJOR_VERSION && 
	     ((int)glib_minor_version) < GLIB_MINOR_VERSION)
	    ||
	    (glib_major_version == GLIB_MAJOR_VERSION && 
	     glib_minor_version == GLIB_MINOR_VERSION &&
	     ((int)glib_micro_version) < GLIB_MICRO_VERSION))
		warn ("*** This build of Glib was compiled with glib %d.%d.%d,"
		      " but is currently running with %d.%d.%d, which is too"
		      " old.  We'll continue, but expect problems!\n",
		    GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION,
		    glib_major_version, glib_minor_version, glib_micro_version);
     */

##
## NOTE: in order to avoid overwriting the docs for the main Glib.pm, 
##       all xsubs in this section must be either assigned to other
##       packages or marked as hidden.
##

=for apidoc __hide__
=cut
const char *
filename_from_unicode (class_or_filename, filename=NULL)
	GPerlFilename_const class_or_filename
	GPerlFilename_const filename
    PROTOTYPE: $
    CODE:
	RETVAL = items < 2 ? class_or_filename : filename;
    OUTPUT:
        RETVAL

=for apidoc __hide__
=cut
GPerlFilename_const
filename_to_unicode (const char * class_or_filename, const char *filename=NULL)
    PROTOTYPE: $
    CODE:
	RETVAL = items < 2 ? class_or_filename : filename;
    OUTPUT:
        RETVAL

=for apidoc __hide__
=cut
void
filename_from_uri (...)
    PROTOTYPE: $
    PREINIT:
	gchar * filename = NULL;
	const char * uri;
	char * hostname = NULL;
	GError * error = NULL;
    PPCODE:
	/* support multiple call syntaxes. */
	uri = items < 2 ? SvPVutf8_nolen (ST (0)) : SvPVutf8_nolen (ST (1));
	filename = g_filename_from_uri (uri,
	                                GIMME_V == G_ARRAY ? &hostname : NULL, 
	                                &error);
	if (!filename)
		gperl_croak_gerror (NULL, error);
	PUSHs (sv_2mortal (newSVpv (filename, 0)));
	if (GIMME_V == G_ARRAY && hostname) {
		/* The g_filename_from_uri() docs say hostname is utf8,
		 * hence newSVGChar, though as of glib circa 2.16
		 * hostname_validate() only actually allows ascii
		 * alphanumerics, so utf8 doesn't actually come out.
		 */
		XPUSHs (sv_2mortal (newSVGChar (hostname)));
	}
	g_free (filename);
	if (hostname) g_free (hostname);

=for apidoc __hide__
=cut
gchar_own *
filename_to_uri (...)
    PROTOTYPE: $$
    PREINIT:
	char * filename = NULL;
	char * hostname = NULL;
	GError * error = NULL;
    CODE:
	/* The g_filename_to_uri() docs say hostname is utf8, hence SvGChar,
	 * though as of glib circa 2.16 hostname_validate() only actually
	 * allows ascii alphanumerics, so you can't in fact pass in utf8.
	 */
	if (items == 2) {
		filename = SvPV_nolen (ST (0));
		hostname = gperl_sv_is_defined (ST (1)) ? SvGChar (ST (1)) : NULL;
	} else if (items == 3) {
		filename = SvPV_nolen (ST (1));
		hostname = gperl_sv_is_defined (ST (2)) ? SvGChar (ST (2)) : NULL;
	} else {
		croak ("Usage: Glib::filename_to_uri (filename, hostname)\n"
		       " -or-  Glib->filename_to_uri (filename, hostname)\n"
		       "  wrong number of arguments");
	}
	RETVAL = g_filename_to_uri (filename, hostname, &error);
	if (!RETVAL)
		gperl_croak_gerror (NULL, error);
    OUTPUT:
	RETVAL


## XXX i'd prefer to have local fallbacks so that we don't need this version hack.
##     unfortunately, these functions are nontrivial.

#if GLIB_CHECK_VERSION(2, 6, 0)

### note the use of raw const char* here.

# from gconvert.h.  Pod is in Glib.pm.
=for apidoc __hide__
=cut
gchar_own * g_filename_display_name (const char * filename);

# from gconvert.h.  Pod is in Glib.pm.
=for apidoc __hide__
=cut
gchar_own * g_filename_display_basename (const char * filename);

#endif