title: Untangling Lua & C code to make Lua dependency optional
body: <p>I have written a little program in C to manage a bibliography database in SQLite3.  So far, this is only a command line tool that allows importing and exporting BibTeX data.  To make the export/import functionality more customizable (e.g. always combine year&amp;month fields in the date field on import, or skip certain fields on export) these functions are written in Lua and called from C.  <strong>Now I want Lua to be an optional dependency. What's a good way to do that?</strong> I.e. if at compile time Lua is not found, fall back to more basic import/export functionality.</p>
  
  <p>Right now, e.g. on export I get the data from SQL (sqlite3_exec) and write it directly to a Lua table in the callback function, like so (stripped stackoverflow-handling ;-) ):</p>
  
  <pre><code>int db_meta_cb(void *udata, int n, char **cval, char **ckey) {
    while (n-- &gt; 0) {
      lua_pushstring(L, cval[n]);
      lua_setfield(L, -2, ckey[n]);
    };
  };
  </code></pre>
  
  <p>Then there is a Lua function that takes such a table as an argument and pretty-prints a BibTeX entry.  Similar story on import: the BibTeX parser (Lex/YACC generated C code) writes a Lua table and calls a 'cleanup' Lua function, then reads the result from the same Lua table and inserts it into the database.</p>
  
  <p>Now this feels wrong in the sense that I use Lua too much, I guess because the data consists of (key,value)-pairs.</p>
  
  <p>Note that one reason for this project is to experiment with/learn about embedding Lua in C, so please don't suggest (a) using one of the available Bibliography managers, or (b) rewriting everything in Python or Lua entirely... (Which I have already done btw)</p>