Posts tagged os x

lua

Embedding Lua script into a C program

1

Just for curiosity I wanted to try embedding a Lua script into a little C program and surprisingly it resulted in a pretty quick and easy task.

Lua provides C API and everything is done within a few lines of code.

Let’s assume that our Lua script just prints “hello world” once it is executed and contains also a function to be called later.

Save a file as “hello.lua” and fill in with:

print("Hello world!");

function foo()
 print("Hi I am the foo function!");
end

Then create the C program that will run Lua ans save it as “embed_hello.c” with the following content:

#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

/* lua interpreter */
lua_State* l;

int main () {
  int dofile;

  /* initialize lua */
  l = lua_open();

  /* load lua libraries */
  luaL_openlibs(l);

  /* run the hello.lua script */
  dofile = luaL_dofile(l, "hello.lua");

  if (dofile == 0) {
    /* call foo */
    lua_getglobal(l,"foo");
    lua_call(l,0,0);
  }
  else {
    printf("Error, unable to run hello.lua\n");
  }

  /* cleanup Lua */
  lua_close(l);

  return 0;
}

Compile:

$ gcc -o embed_hello -Wall -L/opt/local/lib -I/opt/local/include \
-llua embed_hello.c

That’s all. This example works for OS X and Lua installed from macports. For other OSs and configurations just adapt the gcc command.

Execute it

$ ./embed_hello
Hello world!
Hi I am the foo function!


References:

Macbook 13″ and Slackware 13 dual boot

0

Important: the procedure can destroy all your system’s data if you don’t pay attention. First of all create a full backup. Time machine could be a good choice.

There are plenty of HOW-TOs to help in making a Macbook dual bootable with pictures and screenshot how to use disk utility and bootcamp so please refer to those documents, I’ll give a short overview and focus the attention on the installation process.
This covers the Slackware 13 but it’s applicable to any otherĀ  distro I guess.

Initial condition:

  • Macbook white, 2007 series with standard Leopard installation and some free space on the disk;
  • Slackware 13 installation disks (are sufficient the first and second CD or the DVD).

Reserve space for Slackware

The first step is to create a bootcamp partition. The process is quite simple and straightforward with the above mentioned bootcamp.

GNU/Linux Slackware installation

It’s time to play. Insert the first Slack CD and boot the Mac holding the “Command” key (left Alt) to start from the CD and begin a normal installation.

Partitioning disk

Use fdisk or cfdisk to partition the disk (I’m an fdisk’s fan since old time)

# fdisk /dev/sda

The “b” type labeled W95 FAT32 partition should be shown. It’s bootcamp.

Delete it and create the “Swap partition” and the “Linux root” partition (simplest way), save, and start the normal setup.

Be careful with the disk partitioning tool to not delete OS X!

After the packages are installed, it’s time to set a minimum configuration.

Follow some settings that worked for me:

Console framebuffer: 1024×768 256 colors

Lilo installed in the MBR

Slackware logo: yes, of course!

Reboot

After the reboot I was surprised by the impossibility to hold down “Command” and choose the OS I’d like to boot.

Now it’s time to install rEFIt

Reboot again and… surprise surprise, you don’t se rEFIt menu’. Shutdown the Mac and boot again.

Now rEFIt boot menu’ should show up, choose Linux and hopefully it boots. My way was a bit harder and got the message: “no bootable disk”.

After some additional shutdown and boot up, it started working.

Running Slackware 13 on Macbook

Ethernet and WiFi cards seem to work perfectly just after the first boot.

The keyboard has few problem:

  • Function keys need to press fn;
  • Scrolling up and down is possible with the combination of fn + shift + up/down arrow

Other devices to be checked:
- graphic card
- cd/dvd
- iSight cam
- soundcard
- mouse pad

The rest works quite fine.

Go to Top