<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://www.usualcoding.eu/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>UsualCoding.eu - code</title>
  <link>http://www.usualcoding.eu/</link>
  <description>Use(less|ful) stuff</description>
  <language>en</language>
  <pubDate>Wed, 21 Jul 2010 22:55:36 +0200</pubDate>
  <copyright></copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>NFQueue python bingings</title>
    <link>http://www.usualcoding.eu/post/2008/07/07/NFQueue-python-bingings</link>
    <guid isPermaLink="false">urn:md5:672a5772a4116e133c9f43b911284013</guid>
    <pubDate>Mon, 07 Jul 2008 14:04:00 +0200</pubDate>
    <dc:creator>Seb</dc:creator>
        <category>Patches</category>
        <category>code</category><category>netfilter</category><category>nfqueue</category><category>patches</category><category>python</category>    
    <description>    &lt;p&gt;A few month ago I needed python bindings for the nfqueue netfilter library (libnetfilter_queue).&lt;/p&gt;
&lt;p&gt;I was going to write them myself since I found none, but then I stumbled across &lt;a href=&quot;http://lists.netfilter.org/pipermail/netfilter-devel/2006-June/024672.html&quot;&gt;this&lt;/a&gt; on the netfilter mailing list.&lt;/p&gt;
&lt;p&gt;I like this &lt;a href=&quot;http://mathieu.geli.free.fr/code/nfqueue.tar.gz&quot;&gt;wrapper&lt;/a&gt; because it is simple and the code is clear. I was able to adapt my work to them very quickly.&lt;/p&gt;
&lt;p&gt;There were some little problems though.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;strong&gt;First &lt;/strong&gt;of all the unchanged code cannot be used in multithreaded programs (see &lt;a href=&quot;http://www.python.org/doc/api/threads.html&quot;&gt;http://www.python.org/doc/api/threads.html&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;So I wrote a little patch which adds the necessary Py_BEGIN_ALLOW_THREADS and a &quot;stop&quot; function to stop the queue.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The code can be found here:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://www.usualcoding.eu/public/nfqueue/nfqueue-multithreading.patch&quot;&gt;nfqueue-multithreading.patch&lt;br /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;br /&gt;The &lt;strong&gt;second &lt;/strong&gt;problem appears only on Linux &amp;gt;=2.6.23 (see &lt;a href=&quot;http://www.spinics.net/lists/netfilter/msg42063.html&quot;&gt;http://www.spinics.net/lists/netfilter/msg42063.html&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Here is the patch:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://www.usualcoding.eu/public/nfqueue/nfqueue-2.6.23-fix.patch&quot;&gt;nfqueue-2.6.23-fix.patch&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;Special thanks to Mathieu Geli for this wrapper, it helped me a lot and saved time in my work.&lt;br /&gt;&lt;br /&gt;By the way, I found new &lt;a href=&quot;http://software.inl.fr/trac/wiki/nfqueue-bindings&quot;&gt;nfqueue bindings based on SWIG&lt;/a&gt; that look nice. They are brand new, but I haven't tested them yet.&lt;br /&gt;&lt;br /&gt;Links:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://mathieu.geli.free.fr/code/nfqueue.tar.gz&quot;&gt;nfqueue.tar.gz&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://software.inl.fr/trac/wiki/nfqueue-bindings&quot;&gt;SWIG nfqueue-bindings&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
    
          <enclosure url="http://www.usualcoding.eu/public/nfqueue/nfqueue-multithreading.patch"
      length="1753" type="text/plain" />
          <enclosure url="http://www.usualcoding.eu/public/nfqueue/nfqueue-2.6.23-fix.patch"
      length="717" type="text/plain" />
    
    
          <comments>http://www.usualcoding.eu/post/2008/07/07/NFQueue-python-bingings#comment-form</comments>
      <wfw:comment>http://www.usualcoding.eu/post/2008/07/07/NFQueue-python-bingings#comment-form</wfw:comment>
      <wfw:commentRss>http://www.usualcoding.eu/feed/rss2/comments/9</wfw:commentRss>
      </item>
    
  <item>
    <title>MP3Camp</title>
    <link>http://www.usualcoding.eu/post/2007/10/26/Mp3Camp</link>
    <guid isPermaLink="false">urn:md5:17f660de17f15d6056e67fc0da69b883</guid>
    <pubDate>Fri, 26 Oct 2007 22:13:00 +0200</pubDate>
    <dc:creator>Seb</dc:creator>
        <category>Code</category>
        <category>camping</category><category>code</category><category>mp3</category><category>musicplayer</category><category>ruby</category><category>streaming</category><category>xspf</category>    
    <description>    I have been using &lt;a href=&quot;http://www.gnu.org/software/gnump3d/&quot;&gt;GNUMP3d&lt;/a&gt; for some time now, but a few things bother me.&lt;br /&gt;For example, it uses a standalone web server to stream the music, which is useful in some cases, but requires you to set it up on a different port if you already run a web server. I know I could setup a mod_proxy or something similar, but I like it when things work right out of the box.&lt;br /&gt;&lt;br /&gt;There are things out there like &lt;a href=&quot;http://www.ampache.org/&quot;&gt;ampache&lt;/a&gt;, but nothing as light as GNUMP3d. That is why I coded a small streamer of my own using &lt;a href=&quot;http://code.whytheluckystiff.net/camping/&quot;&gt;camping&lt;/a&gt; a Micro framework in ruby. There is not much code yet but it is already usable.&lt;br /&gt;&lt;br /&gt;One of the advantages of using camping, is that it can work as a standalone server, but it can also easily be setup in a virtual host on apache, lighttpd or whatever.&lt;br /&gt;&lt;br /&gt;Here is a screenshot of the latest (pre alpha) version:&lt;br /&gt;&lt;p&gt;&lt;img style=&quot;margin: 0 auto; display: block;&quot; alt=&quot;&quot; src=&quot;http://www.usualcoding.eu/post/2007/10/26/../public/mp3camp/mp3camp.png&quot; /&gt;&lt;/p&gt;
As you can see it is very much like GNUMP3d in the way that it gives you access to playlists you can download and use in any music player.&lt;br /&gt;I also added an embedded flash &lt;a href=&quot;http://musicplayer.sourceforge.net/&quot;&gt;music player&lt;/a&gt; you can use to just listen in quickly.&lt;br /&gt;MP3Camp supports m3u and &lt;a href=&quot;http://www.xspf.org/&quot;&gt;xspf&lt;/a&gt; playlists and really needs some CSS job.&lt;br /&gt;&lt;br /&gt;You can grab this early version on the svn (&lt;a href=&quot;svn://svn.sig11.org/mp3camp&quot;&gt;svn://svn.sig11.org/mp3camp&lt;/a&gt;).&lt;br /&gt;</description>
    
    
    
          <comments>http://www.usualcoding.eu/post/2007/10/26/Mp3Camp#comment-form</comments>
      <wfw:comment>http://www.usualcoding.eu/post/2007/10/26/Mp3Camp#comment-form</wfw:comment>
      <wfw:commentRss>http://www.usualcoding.eu/feed/rss2/comments/7</wfw:commentRss>
      </item>
    
  <item>
    <title>Building a reentrant parser in C with Flex/Bison</title>
    <link>http://www.usualcoding.eu/post/2007/09/03/Building-a-reentrant-parser-in-C-with-Flex/Bison</link>
    <guid isPermaLink="false">urn:md5:60be796c09ff71e29f779f36a12675eb</guid>
    <pubDate>Mon, 03 Sep 2007 20:51:00 +0000</pubDate>
    <dc:creator>Seb</dc:creator>
        <category>Code</category>
        <category>bison</category><category>C</category><category>code</category><category>flex</category><category>lex</category><category>parser</category><category>reentrant</category><category>yacc</category>    
    <description>    &lt;p&gt;It is not easy for beginners to find all the right information on how to create a reentrant parser with tools like flex and bison. Most examples out there are out of date or not very clear.&lt;br /&gt;
Once I found out, I wrote a small example of how to do it. The objective is not yet another flex/bison tutorial, but to focus solely on the reentrant code aspects.&lt;br /&gt;
It is a simple caculator that evaluates arithmetic expressions and it is separated in three small files:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;calc.h (common header file for lexer and parser)&lt;/li&gt;
&lt;li&gt;lexer.l (obviously the lexer)&lt;/li&gt;
&lt;li&gt;parser.y (the parser and the main function)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;First, lets see the header:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    typedef struct      parse_parm_s&lt;br /&gt;    {&lt;br /&gt;        void            *yyscanner;&lt;br /&gt;        char            *buf;&lt;br /&gt;        int             pos;&lt;br /&gt;        int             length;&lt;br /&gt;        double          result;&lt;br /&gt;    }                   parse_parm;&lt;br /&gt;&lt;br /&gt;    void    parse(char *buf, double *result);&lt;br /&gt;&lt;br /&gt;    #define YYSTYPE         double&lt;br /&gt;    #define YY_EXTRA_TYPE   parse_parm *&lt;br /&gt;&lt;br /&gt;    int     yylex(YYSTYPE *, void *);&lt;br /&gt;    int     yylex_init(void **);&lt;br /&gt;    int     yylex_destroy(void *);&lt;br /&gt;    void    yyset_extra(YY_EXTRA_TYPE, void *);&lt;br /&gt;    int     yyparse(parse_parm *, void *);&lt;br /&gt;    void    yyerror();&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Lets focus on the &lt;code&gt;parse_parm&lt;/code&gt; structure. What we need is a way to share data between the lexer and the parser without the use of global variables. That is where this structure comes in. In this one we have:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;yyscanner&lt;/code&gt; which is an uninitialized pointer that will be used by the lexer for internal suff (instead of globals)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;buf&lt;/code&gt; is the buffer that contains the data to be parsed&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pos&lt;/code&gt; is the current position in the buffer&lt;/li&gt;
&lt;li&gt;&lt;code&gt;length&lt;/code&gt; is the size of the buffer&lt;/li&gt;
&lt;li&gt;&lt;code&gt;result&lt;/code&gt; will be the result of the evaluated expression&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;parse&lt;/code&gt; is our main parsing function. We will see it in the parser.&lt;br /&gt;
&lt;code&gt;YYSTYPE&lt;/code&gt; is the type of &lt;code&gt;yylval&lt;/code&gt; but you should know that otherwise go back to basics.&lt;br /&gt;
&lt;code&gt;YY_EXTRA_TYPE&lt;/code&gt; is the type of the extra data which will be given to the lexer thanks to the &lt;code&gt;yyset_extra&lt;/code&gt; function. In this case it is our structure.&lt;/p&gt;
&lt;p&gt;The prototypes are forward declaration of lexer/parser functions so the compiler shuts up about warnings. I think I could generate a .h with all that but I was lazy.&lt;/p&gt;
&lt;p&gt;Here comes the lexer:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    %{&lt;br /&gt;    #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;    #include &amp;lt;string.h&amp;gt;&lt;br /&gt;&lt;br /&gt;    #include &quot;calc.h&quot;&lt;br /&gt;    #include &quot;parser.tab.h&quot;&lt;br /&gt;&lt;br /&gt;    #define PARM    yyget_extra(yyscanner)&lt;br /&gt;&lt;br /&gt;    #define YY_INPUT(buffer, res, max_size)             \&lt;br /&gt;    do {                                                \&lt;br /&gt;        if (PARM-&amp;gt;pos &amp;gt;= PARM-&amp;gt;length)                  \&lt;br /&gt;            res = YY_NULL;                              \&lt;br /&gt;        else                                            \&lt;br /&gt;        {                                               \&lt;br /&gt;            res = PARM-&amp;gt;length - PARM-&amp;gt;pos;             \&lt;br /&gt;            res &amp;gt; (int)max_size ? res = max_size : 0;   \&lt;br /&gt;            memcpy(buffer, PARM-&amp;gt;buf + PARM-&amp;gt;pos, res); \&lt;br /&gt;            PARM-&amp;gt;pos += res;                           \&lt;br /&gt;        }                                               \&lt;br /&gt;    } while (0)&lt;br /&gt;&lt;br /&gt;    %}&lt;br /&gt;&lt;br /&gt;    %option reentrant bison-bridge&lt;br /&gt;    %option noyywrap&lt;br /&gt;    %option nounput&lt;br /&gt;&lt;br /&gt;    %%&lt;br /&gt;&lt;br /&gt;    [+\-*/()] { return (*yytext); }&lt;br /&gt;&lt;br /&gt;    [0-9]+ { *yylval = atoi(yytext); return (INT); }&lt;br /&gt;&lt;br /&gt;    ([0-9]*.[0-9]+) { *yylval = atof(yytext); return (FLOAT); }&lt;br /&gt;&lt;br /&gt;    [ \t\r\n] ;&lt;br /&gt;&lt;br /&gt;    %%&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;parse_parm&lt;/code&gt; structure is extra data so we have to access it with &lt;code&gt;yyget_extra(yyscanner)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since we read from a buffer and not stdin or a file, we have to to redefine the &lt;code&gt;YY_INPUT&lt;/code&gt; macro (see section 10 of the flex manual 'The generated scanner').&lt;/p&gt;
&lt;p&gt;We want the scanner to be reentrant, therefore generate no global variables. That is what the &lt;code&gt;reentrant&lt;/code&gt; option is for. &lt;code&gt;bison-bridge&lt;/code&gt; is used to create a bison compatible scanner and share &lt;code&gt;yylval&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally the parser:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    %{&lt;br /&gt;    #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;    #include &amp;lt;string.h&amp;gt;&lt;br /&gt;&lt;br /&gt;    #include &quot;calc.h&quot;&lt;br /&gt;&lt;br /&gt;    void    parse(char *buf, double *result)&lt;br /&gt;    {&lt;br /&gt;        parse_parm  pp;&lt;br /&gt;&lt;br /&gt;        pp.buf = buf;&lt;br /&gt;        pp.length = strlen(buf);&lt;br /&gt;        pp.pos = 0;&lt;br /&gt;        *result = 0;&lt;br /&gt;        yylex_init(&amp;amp;pp.yyscanner);&lt;br /&gt;        yyset_extra(&amp;amp;pp, pp.yyscanner);&lt;br /&gt;        yyparse(&amp;amp;pp, pp.yyscanner);&lt;br /&gt;        *result = pp.result;&lt;br /&gt;        yylex_destroy(pp.yyscanner);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    %}&lt;br /&gt;&lt;br /&gt;    %pure_parser&lt;br /&gt;    %parse-param {parse_parm *parm}&lt;br /&gt;    %parse-param {void *scanner}&lt;br /&gt;    %lex-param {yyscan_t *scanner}&lt;br /&gt;&lt;br /&gt;    %token INT FLOAT&lt;br /&gt;&lt;br /&gt;    %left '-' '+'&lt;br /&gt;    %left '*' '/'&lt;br /&gt;    %left NEG POS&lt;br /&gt;&lt;br /&gt;    %%&lt;br /&gt;&lt;br /&gt;    expr: math { parm-&amp;gt;result = $1; }&lt;br /&gt;        ;&lt;br /&gt;&lt;br /&gt;    math: math '+' math { $$ = $1 + $3; }&lt;br /&gt;        | math '-' math { $$ = $1 - $3; }&lt;br /&gt;        | math '*' math { $$ = $1 * $3; }&lt;br /&gt;        | math '/' math { $$ = $1 / $3; }&lt;br /&gt;        | '-' math %prec NEG { $$ = -$2; }&lt;br /&gt;        | '+' math %prec POS { $$ = $2; }&lt;br /&gt;        | '(' math ')' { $$ = $2; }&lt;br /&gt;        | INT { $$ = $1; }&lt;br /&gt;        | FLOAT { $$ = $1; }&lt;br /&gt;        ;&lt;br /&gt;&lt;br /&gt;    %%&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;yylex_init&lt;/code&gt; and &lt;code&gt;yylex_destroy&lt;/code&gt; have to be called to initialize the lexer and free its ressources after the parsing.
&lt;code&gt;yyset_extra&lt;/code&gt; is used to pass the &lt;code&gt;parse_parm&lt;/code&gt; structure to the lexer.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;pure_parser&lt;/code&gt; option tells bison to use no global variables and create a reentrant parser.&lt;br /&gt;
&lt;code&gt;yyparse&lt;/code&gt; gets two new parameters with the &lt;code&gt;parse-param&lt;/code&gt;. The &lt;code&gt;parse_parm&lt;/code&gt; structure and the pointer for the lexer.&lt;br /&gt;
&lt;code&gt;yylex&lt;/code&gt; gets a new parameter thanks to &lt;code&gt;lex-param&lt;/code&gt; so the parser can pass him the pointer.&lt;/p&gt;
&lt;p&gt;Now the parse function can be called any time to evaluate an expression and get the result. Even in multiple threads.&lt;/p&gt;
&lt;p&gt;Here is a link to the sources in this tutorial: &lt;a href=&quot;http://www.sig11.org/~seb/calc.tar.gz&quot;&gt;calc.tar.gz&lt;/a&gt;. You can compile it with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    $ bison -d parser.y&lt;br /&gt;    $ flex lexer.l&lt;br /&gt;    $ gcc lex.yy.c parser.tab.c -ly -ll&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Other links:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://flex.sourceforge.net/&quot;&gt;Flex&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://flex.sourceforge.net/manual/&quot;&gt;Flex Manual&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.gnu.org/software/bison/&quot;&gt;Bison&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.gnu.org/software/bison/manual/index.html&quot;&gt;Bison Manual&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
    
          <enclosure url="http://www.usualcoding.eu/public/calc/calc.tar.gz"
      length="1532" type="application/x-gzip" />
    
    
          <comments>http://www.usualcoding.eu/post/2007/09/03/Building-a-reentrant-parser-in-C-with-Flex/Bison#comment-form</comments>
      <wfw:comment>http://www.usualcoding.eu/post/2007/09/03/Building-a-reentrant-parser-in-C-with-Flex/Bison#comment-form</wfw:comment>
      <wfw:commentRss>http://www.usualcoding.eu/feed/rss2/comments/1</wfw:commentRss>
      </item>
    
  <item>
    <title>Patching Around</title>
    <link>http://www.usualcoding.eu/post/2007/06/08/Patching-Around</link>
    <guid isPermaLink="false">urn:md5:1a1c360a2a5d70da50825613a3b8cde0</guid>
    <pubDate>Fri, 08 Jun 2007 20:14:00 +0000</pubDate>
    <dc:creator>Seb</dc:creator>
        <category>Patches</category>
        <category>code</category><category>pamusb</category><category>patches</category><category>trac</category>    
    <description>    &lt;p&gt;The new &lt;a href=&quot;http://www.pamusb.org/&quot;&gt;pam_usb&lt;/a&gt; release has a new feature called the pamusb agent. It is basically a python daemon that you can run in your user session. It allows you to execute all kinds of events when your key is plugged in or out.&lt;br /&gt;
What I wanted was a small notification to know when I’m authenticated. Unfortunately there is nothing as cool as &lt;a href=&quot;http://growl.info/&quot;&gt;GROWL&lt;/a&gt; on Linux (as far as I know) so I decided to add a small tray icon to the pamusb agent.  &lt;/p&gt;
&lt;p&gt;If you are interested you can find the patched version here:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a&gt;svn://svn.sig11.org/pamusb_tray/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Currently it is just GTK because I already had some code lying around but I’ll add a QT version later.&lt;br /&gt;
This thing gave me some ideas like adding a notification bubble and a small graphical interface to add &amp;amp; remove events. Maybe later…&lt;/p&gt;
&lt;p&gt;This also reminds me that I have a small patch for Trac lying around.&lt;br /&gt;
Sometimes you want to use Trac as a private project management system denying access to anyone except registered users.&lt;/p&gt;
&lt;p&gt;This patch allows you to release some pages as public ones by adding “Public” to the page name (example: “TestPublic”).&lt;br /&gt;
You can find it &lt;a href=&quot;http://www.trac-hacks.org/wiki/WikiPublicPatch&quot;&gt;here&lt;/a&gt;&lt;/p&gt;</description>
    
    
    
          <comments>http://www.usualcoding.eu/post/2007/06/08/Patching-Around#comment-form</comments>
      <wfw:comment>http://www.usualcoding.eu/post/2007/06/08/Patching-Around#comment-form</wfw:comment>
      <wfw:commentRss>http://www.usualcoding.eu/feed/rss2/comments/3</wfw:commentRss>
      </item>
    
</channel>
</rss>