jul
22
2010

Shortening URL

There are many URL shortening services. I just picked one (u.nu) to use from command line interface and chose something really quick and simple. Here’s the code:

# -*- coding: utf-8 -*-
from urllib import urlencode
import httplib
import sys

api_url="u.nu"
var = urlencode({'url':sys.argv[1]})
args = "/unu-api-simple?%s" % (var)
conn = httplib.HTTPConnection(api_url)
conn.request("GET",args)
ret = conn.getresponse()
print ret.read()
Download the script here.

Just run passing the URL, for example:

$ python u.py www.coding.com.br

http://u.nu/72mpd

Easy your life by putting the script on your $PATH and execution (+x) file mode. Worth check the API from other services:

jul
13
2010

A brief on “designated initializers”

The GNU Compiler Collection (GCC) for C language doesn’t initialize variables zeroed. For simple variables types like int or float is just equal to 0 or 0.0 respectively. Now, suppose that you have a “large” struct and doesn’t want to set each member  individually… you could just type “={0}” which means that the first member is explicitly initialized to zero and the remaining members are implicitly initialized, also zero. Let’s see an example:

typedef struct
{
int a;
char c;
char s[10];
int *ptr;
} data;

When you initialize with:

data d;

You got some random value like:

{a = -1208298748, c = -12 ‘\364′, s = “\317\372\267\230\353\377\277\351\203\004″, ptr = 0xb7e94cc5}

When you type:

data d = {0};

You’ll have each struct member initialized to zero.

{a = 0, c = 0 ‘\000′, s = “\000\000\000\000\000\000\000\000\000″, ptr = 0×0}

You can learn much more about designated inits [section 6.26] on GCC docs.

jul
03
2010

Embed Python inside Latex

After a video demostrating the use of Python inside a Tex document. I decided to do a document explaining how to use,  check it:

Embed Python Inside Latex (pdf ~ 100KB)

Update: I also published online through Scribd, check here.

jun
24
2010

Opcode stats

I was trying some GCC options and decided see how they affect assembly code generated. I’ve created a simple Python script that parses .s output and put in a human readable way. Suppose the classical Hello World program. The output will be something like:

$ python stats_opcodes.py file

For Cortex-m3 (check previous post):

At my laptop (x86):

Download the script here.

jun
23
2010

Cortex-M3 and Qemu

Just a small tip to test code generated for ARM Cortex-M3 using QEmu:

$ arm-none-eabi-gcc main.c -ggdb -mcpu=cortex-m3 -mthumb -T generic-m-hosted.ld
$ qemu-arm -cpu cortex-m3 ./a.out

You can download Code Sourcery toolchain here and QEmu here (also available from package management system of your favorite Linux distribution). Just unpack the toolchain associating the arm-201XqX/bin with your $PATH and run qemu.

jun
21
2010

Nginx rewrite for Wikka Wiki

I’m posting it here, because I started a few weeks ago working with Nginx (if you don’t know that, you really should), a very powerful and fast webserver (lets leave it for another post), but I faced a problem with redirects for Wikka Wiki, I’m not a beginner on regular expressions or mod_rewrite, but sometimes we get in trouble working on something new.

I haven’t found the solution for my problem (for wordpress, drupal and joomla there are so many) so I resolved it and now I’m posting if somebody can’t do it or just wanna some copy/paste ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
 location ~ ^/wiki {
 	root /path/to/wiki;
 	index wikka.php;
 
 	if (!-e $request_filename) {
 		rewrite ^/wiki/images/(.*)$ /images/$1 break;
		rewrite ^/wiki/templates/(.*)$ /templates/$1 break;
 		rewrite ^/wiki/3rdparty/plugins/freemind/(.*)$ /3rdparty/plugins/freemind/$1 break;
 		rewrite ^/wiki/3rdparty/plugins/wikkaedit/(.*)$ /3rdparty/plugins/wikkaedit/$1 break;
 		rewrite ^/wiki/(.*)$ /wiki/wikka.php?wakka=$1 break;
 		break;
 	}
 }

A brief explanation:

on line 1 we setup where our wiki is located, on our case /wiki, so in line 2 we give full path to where the wiki’s file are located on the filesystem and line 3 we says our index will be wikka.php once Wikka Wiki just redirect index.php to wikka.php (ok I don’t mind why they don’t put the contents of wikka.php on index.php and its over ;) ).

line 5-12 we configure the rewrite rules, on line 5 is the condition what to do when there is no file or symbolic link when a URL is acessed, if its matched the following rules are processed:
line 6-10: rules to ensure that static content like css, js and images won’t be redirected to wikka.php.
line 11: the main rule, that will redirect all URLs to wikka.php so it process and show the output for each wiki page.

I don’t why (I’m new to Nginx) even using the condition if(!-e $request_filename) I need to put the rules to ensure that static content will be reached, if I discover, I update this post, but these rules are working for me under Nginx 0.7.67 and Wikka 1.2-p1

jun
13
2010

Licenças para módulos no Linux

O arquivo /usr/src/linux/include/linux/module.h define as licenças aceitáveis para um módulo (ou driver) seja reconhecido como software livre.

  • GPLGNU Public License v2 or later
  • GPL v2GNU Public License v2
  • GPL and additional rightsGNU Public License v2 rights and more
  • Dual BSD/GPLGNU Public License v2  or BSD license choice
  • Dual MIT/GPLGNU Public License v2 or MIT license choice
  • Dual MPL/GPL - GNU Public License v2 or Mozilla license choice
Também há um espaço (infelizmente) para licenças proprietárias:
  • Proprietary - Non free products

Como vocês podem notar, há componentes que podem ser definidos com licenças duplas, contudo quando executado no Linux apenas a GPL é relevante. Algumas razões para definir a licença:

  1. O modinfo pode mostrar informações para usuários que desejam avaliar as licenças dos módulos sua instalação.
  2. A comunidade pode ignorar relatórios de bugs dos módulos proprietários.
  3. Os fabricantes podem fazer o mesmo com suas próprias políticas.

Para inserir a licença, basta colocar no seu código-fonte a macro  ”MODULE_LICENCE”. Exemplo:

  MODULE_LICENCE("GPL");

Lembre-se que alguns recursos do kernel são disponíveis apenas se seu código é livre.

Um exemplo é o sysfs (através da macro EXPORT_SYMBOL_GPL) que por questões de manutenção e consistência exige que você licencie seu módulo em alguma licença compatível com a GPL.

jun
08
2010

NFS errors

If you use NFS to mount your root filesystem you should probably faced with this problem:

Root-NFS: Unable to get mountd port number from server, using default
Root-NFS: Server returned error -5 while mounting /my/nfs/server/path/
VFS: Unable to mount root fs via NFS, trying floppy.

The -5 and -13 are the most common on my daily usage, but I always forgot what this number means… So, I decided to get it from source by checking nfs-utils package. The following values where extracted  from utils/mount/error.c.

EPERM  ........................................ -1

ENOENT ........................................ -2

EIO ........................................... -3

ENXIO ......................................... -4

EACCESS ....................................... -5 

EEXIST ........................................ -6

ENODEV ........................................ -7

ENOTDIR ....................................... -8

EISDIR ........................................ -9

EINVAL ........................................ -10

EFBIG ......................................... -11

ENOSPC  ....................................... -12

EROFS ......................................... -13

ENAMETOOLONG .................................. -14

ENOTEMPTY ..................................... -15

EDQUOT ........................................ -16

ESTALE ........................................ -17

EWFLUSH ....................................... -18

Knowing what each error status means allow you quick fix the problems.