Codebase list libconvert-uulib-perl / HEAD UUlib.xs
HEAD

Tree @HEAD (Download .tar.gz)

UUlib.xs @HEADraw · history · blame

  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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "perlmulticore.h"

#include "uulib/fptools.h"
#include "uulib/uudeview.h"
#include "uulib/uuint.h"

static int perlinterp_released;

#define RELEASE do { perlinterp_released = 1; perlinterp_release (); } while (0)
#define ACQUIRE do { perlinterp_acquire (); perlinterp_released = 0; } while (0)

#define TEMP_ACQUIRE if (perlinterp_released) perlinterp_acquire ();
#define TEMP_RELEASE if (perlinterp_released) perlinterp_release ();

static void
uu_msg_callback (void *cb, char *msg, int level)
{
  TEMP_ACQUIRE {

    dSP;
   
    ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, 2);

    PUSHs (sv_2mortal (newSVpv (msg, 0)));
    PUSHs (sv_2mortal (newSViv (level)));

    PUTBACK; (void) perl_call_sv ((SV *)cb, G_VOID|G_DISCARD); SPAGAIN;
    PUTBACK; FREETMPS; LEAVE;

  } TEMP_RELEASE;
}

static int
uu_busy_callback (void *cb, uuprogress *uup)
{
  int retval;

  TEMP_ACQUIRE {

    dSP;
    int count;
   
    ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, 6);

    PUSHs (sv_2mortal (newSViv (uup->action)));
    PUSHs (sv_2mortal (newSVpv (uup->curfile, 0)));
    PUSHs (sv_2mortal (newSViv (uup->partno)));
    PUSHs (sv_2mortal (newSViv (uup->numparts)));
    PUSHs (sv_2mortal (newSViv (uup->fsize)));
    PUSHs (sv_2mortal (newSViv (uup->percent)));

    PUTBACK; count = perl_call_sv ((SV *)cb, G_SCALAR); SPAGAIN;

    if (count != 1)
      croak ("busycallback perl callback returned more than one argument");

    retval = POPi;

    PUTBACK; FREETMPS; LEAVE;

  } TEMP_RELEASE;

  return retval;
}

static char *
uu_fnamefilter_callback (void *cb, char *fname)
{
  static char *str;

  TEMP_ACQUIRE {

    dSP;
    int count;
   
    ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, 1);

    PUSHs (sv_2mortal (newSVpv (fname, 0)));

    PUTBACK; count = perl_call_sv ((SV *)cb, G_SCALAR); SPAGAIN;

    if (count != 1)
      croak ("fnamefilter perl callback MUST return a single filename exactly");

    FP_free (str); str = FP_strdup (SvPV_nolen (TOPs));

    PUTBACK; FREETMPS; LEAVE;

  } TEMP_RELEASE;

  return str;
}

static int
uu_file_callback (void *cb, char *id, char *fname, int retrieve)
{
  int retval;

  TEMP_ACQUIRE {

    dSP;
    int count;
    SV *xfname = newSVpv ("", 0);
   
    ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, 3);

    PUSHs (sv_2mortal (newSVpv (id, 0)));
    PUSHs (sv_2mortal (xfname));
    PUSHs (sv_2mortal (newSViv (retrieve)));

    PUTBACK; count = perl_call_sv ((SV *)cb, G_SCALAR); SPAGAIN;

    if (count != 1)
      croak ("filecallback perl callback must return a single return status");

    strcpy (fname, SvPV_nolen (xfname));
    retval = POPi;

    PUTBACK; FREETMPS; LEAVE;

  } TEMP_RELEASE;

  return retval;
}

static char *
uu_filename_callback (void *cb, char *subject, char *filename)
{
  TEMP_ACQUIRE {

    dSP;
    int count;
   
    ENTER; SAVETMPS; PUSHMARK (SP); EXTEND (SP, 2);

    PUSHs (sv_2mortal(newSVpv(subject, 0)));
    PUSHs (filename ? sv_2mortal(newSVpv(filename, 0)) : &PL_sv_undef);

    PUTBACK; count = perl_call_sv ((SV *)cb, G_ARRAY); SPAGAIN;

    if (count > 1)
      croak ("filenamecallback perl callback must return nothing or a single filename");

    if (count)
      {
        FP_free (filename);

        filename = SvOK (TOPs)
           ? FP_strdup (SvPV_nolen (TOPs))
           : 0;
      }

    PUTBACK; FREETMPS; LEAVE;

  } TEMP_RELEASE;

  return filename;
}

static SV *uu_msg_sv, *uu_busy_sv, *uu_file_sv, *uu_fnamefilter_sv, *uu_filename_sv;

#define FUNC_CB(cb) (void *)(sv_setsv (cb ## _sv, func), cb ## _sv), func ? cb ## _callback : NULL

static int
uu_info_file (void *cb, char *info)
{
  int retval;

  TEMP_ACQUIRE {

    dSP;
    int count;
   
    ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP,1);

    PUSHs(sv_2mortal(newSVpv(info,0)));

    PUTBACK; count = perl_call_sv ((SV *)cb, G_SCALAR); SPAGAIN;

    if (count != 1)
      croak ("info_file perl callback returned more than one argument");

    retval = POPi;

    PUTBACK; FREETMPS; LEAVE;

  } TEMP_RELEASE;

  return retval;
}

static int
uu_opt_isstring (int opt)
{
  switch (opt)
    {
      case UUOPT_VERSION:
      case UUOPT_SAVEPATH:
      case UUOPT_ENCEXT:
         return 1;
      default:
         return 0;
    }
}

static void
initialise (void)
{
  int retval = UUInitialize ();

  if (retval != UURET_OK)
    croak ("unable to initialize uudeview library (%s)", UUstrerror (retval));
}

MODULE = Convert::UUlib		PACKAGE = Convert::UUlib		PREFIX = UU

PROTOTYPES: ENABLE

void
UUCleanUp ()
	CODE:
        UUCleanUp ();
        initialise ();

SV *
UUGetOption (opt)
	int	opt
        CODE:
{
        if (opt == UUOPT_PROGRESS)
          croak ("GetOption(UUOPT_PROGRESS) is not yet implemented");
        else if (uu_opt_isstring (opt))
          {
	    char cval[8192];

            UUGetOption (opt, 0, cval, sizeof cval);
            RETVAL = newSVpv (cval, 0);
          }
        else
          {
            RETVAL = newSViv (UUGetOption (opt, 0, 0, 0));
          }
}
        OUTPUT:
        RETVAL

int
UUSetOption (opt, val)
	int	opt
        SV *	val
        CODE:
{
        STRLEN dc;

        if (uu_opt_isstring (opt))
          RETVAL = UUSetOption (opt, 0, SvPV (val, dc));
        else
          RETVAL = UUSetOption (opt, SvIV (val), (void *)0);
}
        OUTPUT:
        RETVAL

char *
UUstrerror (errcode)
	int	errcode

void
UUSetMsgCallback (func = 0)
	SV *	func
	CODE:
	UUSetMsgCallback (FUNC_CB (uu_msg));

void
UUSetBusyCallback (func = 0,msecs = 1000)
	SV *	func
        long	msecs
	CODE:
	UUSetBusyCallback (FUNC_CB (uu_busy), msecs);

void
UUSetFileCallback (func = 0)
	SV *	func
	CODE:
	UUSetFileCallback (FUNC_CB (uu_file));

void
UUSetFNameFilter (func = 0)
	SV *	func
	CODE:
	UUSetFNameFilter (FUNC_CB (uu_fnamefilter));

void
UUSetFileNameCallback (func = 0)
	SV *	func
	CODE:
	UUSetFileNameCallback (FUNC_CB (uu_filename));

char *
UUFNameFilter (fname)
	char *	fname

void
UULoadFile (fname, id = 0, delflag = 0, partno = -1)
	char *	fname
	char *	id
	int	delflag
        int	partno
        PPCODE:
{
	int count;
        IV ret;

        RELEASE;
        ret = UULoadFileWithPartNo (fname, id, delflag, partno, &count);
        ACQUIRE;

	XPUSHs (sv_2mortal (newSViv (ret)));
        if (GIMME_V == G_ARRAY)
          XPUSHs (sv_2mortal (newSViv (count)));
}

int
UUSmerge (pass)
	int	pass

int
UUQuickDecode(datain,dataout,boundary,maxpos)
	FILE *	datain
	FILE *	dataout
	char *	boundary
	long	maxpos

int
UUEncodeMulti(outfile,infile,infname,encoding,outfname,mimetype,filemode)
	FILE *	outfile
	FILE *	infile
	char *	infname
	int	encoding
	char *	outfname
	char *	mimetype
	int	filemode

int
UUEncodePartial(outfile,infile,infname,encoding,outfname,mimetype,filemode,partno,linperfile)
	FILE *	outfile
	FILE *	infile
	char *	infname
	int	encoding
	char *	outfname
	char *	mimetype
	int	filemode
	int	partno
	long	linperfile

int
UUEncodeToStream(outfile,infile,infname,encoding,outfname,filemode)
	FILE *	outfile
	FILE *	infile
	char *	infname
	int	encoding
	char *	outfname
	int	filemode

int
UUEncodeToFile(infile,infname,encoding,outfname,diskname,linperfile)
	FILE *	infile
	char *	infname
	int	encoding
	char *	outfname
	char *	diskname
	long	linperfile

int
UUE_PrepSingle(outfile,infile,infname,encoding,outfname,filemode,destination,from,subject,isemail)
	FILE *	outfile
	FILE *	infile
	char *	infname
	int	encoding
	char *	outfname
	int	filemode
	char *	destination
	char *	from
	char *	subject
	int	isemail

int
UUE_PrepPartial(outfile,infile,infname,encoding,outfname,filemode,partno,linperfile,filesize,destination,from,subject,isemail)
	FILE *	outfile
	FILE *	infile
	char *	infname
	int	encoding
	char *	outfname
	int	filemode
        int	partno
        long	linperfile
        long	filesize
	char *	destination
	char *	from
	char *	subject
	int	isemail

uulist *
UUGetFileListItem (num)
	int	num

void
GetFileList ()
	PPCODE:
{
	uulist *iter;

        for (iter = UUGlobalFileList; iter; iter = iter->NEXT)
	  XPUSHs (sv_setref_pv (sv_newmortal (), "Convert::UUlib::Item", iter));
}

MODULE = Convert::UUlib		PACKAGE = Convert::UUlib::Item

int
rename (item, newname)
	uulist *item
	char *	newname
        CODE:
        RETVAL = UURenameFile (item, newname);
	OUTPUT:
        RETVAL

int
decode_temp (item)
	uulist *item
        CODE:
        RELEASE;
        RETVAL = UUDecodeToTemp (item);
        ACQUIRE;
	OUTPUT:
        RETVAL

int
remove_temp (item)
	uulist *item
        CODE:
        RELEASE;
        RETVAL = UURemoveTemp (item);
        ACQUIRE;
	OUTPUT:
        RETVAL

int
decode (item, target = 0)
	uulist *item
	char *	target
        CODE:
        RELEASE;
        RETVAL = UUDecodeFile (item, target);
        ACQUIRE;
	OUTPUT:
        RETVAL

void
info (item, func)
	uulist *item
	SV *	func
        CODE:
        RELEASE;
        UUInfoFile (item, (void *)func, uu_info_file);
        ACQUIRE;

short
state(li)
	uulist *li
        CODE:
        RETVAL = li->state;
        OUTPUT:
        RETVAL

short
mode(li,newmode=0)
	uulist *li
        short	newmode
        CODE:
        if (newmode)
	  li->mode = newmode;
        RETVAL = li->mode;
        OUTPUT:
        RETVAL

short
uudet(li)
	uulist *li
        CODE:
        RETVAL = li->uudet;
        OUTPUT:
        RETVAL

long
size(li)
	uulist *li
        CODE:
        RETVAL = li->size;
        OUTPUT:
        RETVAL

char *
filename (li, newfilename = 0)
	uulist *li
        char *	newfilename
        CODE:
        if (newfilename)
	  {
            FP_free (li->filename);
	    li->filename = FP_strdup (newfilename);
          }
        RETVAL = li->filename;
        OUTPUT:
        RETVAL

char *
subfname (li)
	uulist *li
        CODE:
        RETVAL = li->subfname;
        OUTPUT:
        RETVAL

char *
mimeid (li)
	uulist *li
        CODE:
        RETVAL = li->mimeid;
        OUTPUT:
        RETVAL

char *
mimetype (li)
	uulist *li
        CODE:
        RETVAL = li->mimetype;
        OUTPUT:
        RETVAL

char *
binfile (li)
	uulist *li
        CODE:
        RETVAL = li->binfile;
        OUTPUT:
        RETVAL

# methods accessing internal data(!)

void
parts (li)
	uulist *li
        PPCODE:
{
	struct _uufile *p = li->thisfile;

        while (p)
          {
            HV *pi = newHV ();

                                  hv_store (pi, "partno"  , 6, newSViv (p->partno)         , 0);
            if (p->filename     ) hv_store (pi, "filename", 8, newSVpv (p->filename, 0)    , 0);
            if (p->subfname     ) hv_store (pi, "subfname", 8, newSVpv (p->subfname, 0)    , 0);
            if (p->mimeid       ) hv_store (pi, "mimeid"  , 6, newSVpv (p->mimeid  , 0)    , 0);
            if (p->mimetype     ) hv_store (pi, "mimetype", 8, newSVpv (p->mimetype, 0)    , 0);
            if (p->data->subject) hv_store (pi, "subject" , 7, newSVpv (p->data->subject,0), 0);
            if (p->data->origin ) hv_store (pi, "origin"  , 6, newSVpv (p->data->origin ,0), 0);
            if (p->data->sfname ) hv_store (pi, "sfname"  , 6, newSVpv (p->data->sfname ,0), 0);

            XPUSHs (sv_2mortal (newRV_noinc ((SV *)pi)));

            p = p->NEXT;
          }
}

BOOT:
{
  HV *stash = GvSTASH (CvGV (cv));

  static const struct {
    const char *name;
    IV iv;
  } *civ, const_iv[] = {
#   define const_iv(name, value) { # name, (IV) value },
    const_iv (ACT_COPYING  , UUACT_COPYING)
    const_iv (ACT_DECODING , UUACT_DECODING)
    const_iv (ACT_ENCODING , UUACT_ENCODING)
    const_iv (ACT_IDLE     , UUACT_IDLE)
    const_iv (ACT_SCANNING , UUACT_SCANNING)
    const_iv (FILE_DECODED , UUFILE_DECODED)
    const_iv (FILE_ERROR   , UUFILE_ERROR)
    const_iv (FILE_MISPART , UUFILE_MISPART)
    const_iv (FILE_NOBEGIN , UUFILE_NOBEGIN)
    const_iv (FILE_NODATA  , UUFILE_NODATA)
    const_iv (FILE_NOEND   , UUFILE_NOEND)
    const_iv (FILE_OK      , UUFILE_OK)
    const_iv (FILE_READ    , UUFILE_READ)
    const_iv (FILE_TMPFILE , UUFILE_TMPFILE)
    const_iv (MSG_ERROR    , UUMSG_ERROR)
    const_iv (MSG_FATAL    , UUMSG_FATAL)
    const_iv (MSG_MESSAGE  , UUMSG_MESSAGE)
    const_iv (MSG_NOTE     , UUMSG_NOTE)
    const_iv (MSG_PANIC    , UUMSG_PANIC)
    const_iv (MSG_WARNING  , UUMSG_WARNING)
    const_iv (OPT_VERSION  , UUOPT_VERSION)
    const_iv (OPT_FAST     , UUOPT_FAST)
    const_iv (OPT_DUMBNESS , UUOPT_DUMBNESS)
    const_iv (OPT_BRACKPOL , UUOPT_BRACKPOL)
    const_iv (OPT_VERBOSE  , UUOPT_VERBOSE)
    const_iv (OPT_DESPERATE, UUOPT_DESPERATE)
    const_iv (OPT_IGNREPLY , UUOPT_IGNREPLY)
    const_iv (OPT_OVERWRITE, UUOPT_OVERWRITE)
    const_iv (OPT_SAVEPATH , UUOPT_SAVEPATH)
    const_iv (OPT_IGNMODE  , UUOPT_IGNMODE)
    const_iv (OPT_DEBUG    , UUOPT_DEBUG)
    const_iv (OPT_ERRNO    , UUOPT_ERRNO)
    const_iv (OPT_PROGRESS , UUOPT_PROGRESS)
    const_iv (OPT_USETEXT  , UUOPT_USETEXT)
    const_iv (OPT_PREAMB   , UUOPT_PREAMB)
    const_iv (OPT_TINYB64  , UUOPT_TINYB64)
    const_iv (OPT_ENCEXT   , UUOPT_ENCEXT)
    const_iv (OPT_REMOVE   , UUOPT_REMOVE)
    const_iv (OPT_MOREMIME , UUOPT_MOREMIME)
    const_iv (OPT_DOTDOT   , UUOPT_DOTDOT)
    const_iv (OPT_RBUF     , UUOPT_RBUF)
    const_iv (OPT_WBUF     , UUOPT_WBUF)
    const_iv (OPT_AUTOCHECK, UUOPT_AUTOCHECK)
    const_iv (RET_CANCEL   , UURET_CANCEL)
    const_iv (RET_CONT     , UURET_CONT)
    const_iv (RET_EXISTS   , UURET_EXISTS)
    const_iv (RET_ILLVAL   , UURET_ILLVAL)
    const_iv (RET_IOERR    , UURET_IOERR)
    const_iv (RET_NODATA   , UURET_NODATA)
    const_iv (RET_NOEND    , UURET_NOEND)
    const_iv (RET_NOMEM    , UURET_NOMEM)
    const_iv (RET_OK       , UURET_OK)
    const_iv (RET_UNSUP    , UURET_UNSUP)
    const_iv (B64_ENCODED  , B64ENCODED)
    const_iv (BH_ENCODED   , BH_ENCODED)
    const_iv (PT_ENCODED   , PT_ENCODED)
    const_iv (QP_ENCODED   , QP_ENCODED)
    const_iv (UU_ENCODED   , UU_ENCODED)
    const_iv (XX_ENCODED   , XX_ENCODED)
    const_iv (YENC_ENCODED , YENC_ENCODED)
  };

  for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
    newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv));

  uu_msg_sv		= newSVsv (&PL_sv_undef);
  uu_busy_sv		= newSVsv (&PL_sv_undef);
  uu_file_sv		= newSVsv (&PL_sv_undef);
  uu_fnamefilter_sv	= newSVsv (&PL_sv_undef);
  uu_filename_sv	= newSVsv (&PL_sv_undef);

  initialise ();
}