Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The number of arguments to a varags C function is known at compile time. The number of arguments to a Perl function isn't known until runtime. That is, when you code:

XS_func( char *templ, ... ) { ... vsprintf( templ, x, y, z ); }

The C compiler knows it is passing 4 parameters to the varargs function printf and stacks them appropriately. But when you call the XS function from Perl, you can pass any number of arguments at runtime, so there is no way to write that call to vsprintf() with an appropriate number of arguments.

About the best you could do is code the call to vsprintf() with an arbitrarily large number of arguments and yell if the Perl code passes you more:

#! perl -slw use strict; #use Inline 'FORCE'; use Inline C => 'DATA', NAME => 'varargs', CLEAN_AFTER_BUILD => 0; myPrintf( '%s ' x @ARGV . "\n", @ARGV ); __DATA__ __C__ #include <windows.h> #include <stdio.h> #include <stdarg.h> void eprintf( const char *template, ... ) { va_list ap; va_start( ap, template ); vfprintf( win32_stderr(), template, ap ); va_end( ap ); } void myPrintf( char *templ, ... ) { inline_stack_vars; char* a[20]; int i; if( inline_stack_items > 20 ) croak( "myPrintf can handle at most 20 arguments." ); for( i = 0; i < inline_stack_items; i++ ) { a[ i ] = SvPVX( inline_stack_item( i ) ); } eprintf( a[ 0], a[ 1], a[ 2], a[ 3], a[ 4], a[ 5], a[ 6], a[ 7], a[ 8], + a[ 9], a[10], a[11], a[12], a[13], a[14], a[15], a[16], a[17], a[18], + a[19] ); inline_stack_reset; }

That's crude. it should be checking that the SVs have a PV assigned and lots of other stuff, but it will pass whatever args (upto 20) you give on the command line and print them to stderr via C and eprintf().

It could also inspect the contents of the template and attempt to extract the relevant IV/NV/PV, but then you'd have to do something trick with the declaration of a[].

Another way to do this would be for the XS/Inline C function to push the (relevant parts of) the SVs from the Perl stack, onto the C stack. That is, it would have to emulate the C compilers prologue and epilogue code manually, before transferring control (assembler call instruction) to the varargs C function.

(And it would have to be the relevant prologue code depending upon the calling convention (__stdcall, __cdecl or __fastcall) declared for that function.)

This approach would require compiler dependant asm{} hacks.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: XS/Inline::C and ellipsis syntax by BrowserUk
in thread XS/Inline::C and ellipsis syntax by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [Corion]: And the second rule that I've learned is, that there is no one-off job, so writing a program for it pays off almost immediately. The third rule is to give all my programs numbers and have them reproduce that number in the name of their output files. :)
    [Discipulus]: the true part is that also specification change between years.. but well our job is cheap but dont abuse of us.. ;=)
    [LanX]: Choroba: do you miss chaos with ties? apply at the US government.. ;)
    [ambrus]: Corion: those are good rules.
    [ambrus]: Discipulus: oh sure. the input data has different filenames every time I get them.
    [ambrus]: the directory structure may be 1, 2, or 3 deep, it may have spaces in the filename or not, it has dates in various format, different keywords for the same meanings, and the dates and other keywords are assembled in various ways.
    [Discipulus]: no ambrus by specification i mean for example license per core instead of per socket, so fields are different

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (14)
    As of 2017-03-29 12:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should Pluto Get Its Planethood Back?



      Results (350 votes). Check out past polls.