Wednesday, November 29, 2006

Cron and MySql Database Backups

This is nothing very special or exciting, it's just a script that I run as a cron job to make the backup of a mysql database. It can be customized according to several parameters. The script is intended to be run once a day, in fact the file name that is created contains the date in the shape of month_day_year. If you need to run this script more than once per day, you need to add also the time in the file name (otherwise the previous backup in the same day gets overwritten).

You can also customize the parameters that you pass to the programs, e.g., mysqldump and gzip. And of course, you need to set the right values for the username, the password and the database name (and also the path where the backup will be save, which, by default it's the backup directory in your home directory, so please make sure this directory exists).

Here's the script, hoping you find it useful somehow :-)

#!/bin/sh

DATABASE=bibliography
USER=root
PASSWORD=pippo
FILENAME=backup_
PATH=$HOME/backup
MYSQLDUMP=/usr/bin/mysqldump
GZIP="/bin/gzip --best"

BACKUP=$PATH/$FILENAME`/bin/date +%m_%d_%y`.sql.gz

if $MYSQLDUMP -u $USER -p$PASSWORD $DATABASE | $GZIP > $BACKUP; then
echo "backup successful: $BACKUP";
true;
else
echo "errors during backup";
false;
fi

Monday, November 27, 2006

Problem with PHP $HTTP_POST_VARS

In my program PhpBibliography, http://phpbibliography.sourceforge.net, see also the announce on this blog, I've always used the same authentication code that had always worked and I never noticed that it was still using the array $HTTP_POST_VARS, instead of $_POST.

As far as I knew they worked the same... well, a user told me he experienced problems in authenticating, and after some time spent on thinking it was due to sessions and cookies we realized it was due to the fact that the form did not pass anything... the $HTTP_POST_VARS was not defined.

Well, also the documentation of PHP 5.0 says that this array may be disabled. Now everything uses $_POST :-)

Tuesday, November 21, 2006

Linus Torvalds a Hero!

I was really happy when I read in this Time article that Linus Torvalds, also known as the father of Linux, is considered a hero! He really deserves it!

He not only created Linux, which is already a wonderful thing: he also managed to make a huge number of people spread ALL around the world collaborate on it, giving birth to one of the most impressive project in the history of computer science (this is not only my opinion :-)

I started using Linux in 1996 (if I remember well) with a Slackware 1.2.3; the pre-installed kernel did not even support my cdrom, so I had to install the system using 20 floppy disks; then I discovered the magic moment of kernel compilation (in order to make my cdrom work) and it was really emotionally geeky! ;-)

I had already used Xenix few years before so it was like going back home; but there was more: there was that impressive availability of software for free (and of course, open source, and this really matters).

I'm using Linux now most of the time (going back to Windows only to test my software also in that environment, and when I can't use Linux for some hardware or some not working drivers).

It's also a very pleasant and comfortable environment for programmers and nowadays can surely be used for anything... if only people were not too lazy to start using it :-P

Thanks Linus!

Tuesday, November 14, 2006

GNU Gengen (GENerator GENerator)

Gengen has become GNU Gengen! Here's the new link: http://www.gnu.org/software/gengen. Gengen (GENerator GENerator) is a tool that, starting from a parameterized text, called template, generates a text generator that can substitute parameters with values.

At the moment Gengen can generate C++ or C code; however other target languages are under development (e.g., Java).


Say you are writing a C/C++ program and at some point your program has to generate the following code:

if (i < 10)
printf("the value of i is %d", i);

It is not so difficult to write this piece of C++ code:

cout << "if (i < 10)" << endl;
cout << " printf(\"the value of i is %d\", i);" << endl;

or the C code:

printf("if (i < 10)\n");
printf(" printf(\"the value of i is %%d\", i);\n");

provided that you remember to escape the " (and in the C code, also the %).

Suppose now that the previous piece of code has to be generated many times by your program, and every time instead of i another symbol has to be generated (decided at run time). In this case, supposing that this value is contained in a variable symb,
the code for generating this code would be a little bit more complex:

cout << "if (" << symb << "< 10)" << endl;
cout << " printf(\"the value of " << symb << " is %d\", "
<< symb << ");" << endl;

And the C version would be even more obfuscated.

Probably you didn't even realize that you forgot to leave a space before the < 10; basically this is due to the fact that this piece of code mixes the code that has to be generated with the code that generates it, and this tends to make this part
of program less easy to maintain. Especially if some day you have to change the code that has to be generated, you'll have to act on this part of the program, and probably you'll have to
execute some tests in order to be sure that you did it right.

If the code that you have to generate is a slightly more complex, the task may easily become a pain in the neck!

Wouldn't it be nice if you could write the code to be generated in a separate file, let's call it template, say test1.cc_skel
this way


if (@i@ < 10)
printf("the value of @i@ is %d", @i@);

and have a tool that generates a generator, that you can instantiate at run-time with the value that has to be substituted to the parameter i? If such a tool existed, and it generated a file test1_c.h with a C struct test1_gen_struct, then you could write simply this code, in another file, say test1_gen_c.c:

#include <stdio.h>

#include "test1_c.h"

int
main()
{
struct test1_gen_struct gen_struct;
gen_struct.i = "foo";
generate_test1(stdout, &gen_struct, 0);
printf("\n");
gen_struct.i = "bar";
generate_test1(stdout, &gen_struct, 0);
printf("\n");

return 0;
}

Alternatively, if it generated a file test1.h with a C++ class test1_gen_class, then you could write simply this code, in
another file, say test1_gen.cc:

#include <iostream>

using std::cout;
using std::endl;

#include "test1.h"

int
main()
{
test1_gen_class gen_class;
gen_class.set_i("foo");
gen_class.generate_test1(cout);
cout << endl;
gen_class.set_i("bar");
gen_class.generate_test1(cout);
cout << endl;

return 0;
}

and when you run it you would obtain the expected output:

    if (foo < 10)
printf("the value of foo is %d", foo);
if (bar < 10)
printf("the value of bar is %d", bar);

Well, Gengen does right this! Now the code that has to be generated and the code that generates it are separated and they can be maintained more easily: if you want to change the code that has to be generated you act on the file test1.cc_skel; alternatively, say you need to change the value that will be substituted for i, you just change the file test1_gen.cc or test1_gen_c.c.

I hope you try gengen, and have fun! :-)

Friday, November 03, 2006

KLcdDimmer: A KDE Applet to adjust LCD brightness

KlcdDimmer is a small KDE panel applet to adjust the LCD brightness; I built it for my laptop, a Sony Vaio, because I still haven't succeeded in making the Fn keys work.

I also built it as an exercise in KDE programming. Actually KDE programming (and especially Qt Programming) is really cool and funny. The Qt libraries are really well done and their mechanism of signal and slots makes programming with events a pleasure (I cannot say the same about Java events, but that's another story ;-) I only wish there could be some more documentation about KDE applet programming...

Notice that this applet relies on an external program to get/set the brightness. For instance, I've used

SmartDimmer http://www.acc.umu.se/~erikw/program/
NVclock http://www.linuxhardware.org/nvclock/

but it is supposed to work also with others, since you can configure the applet.

The home page of this applet is http://klcddimmer.sourceforge.net. In case you use it, I hope you like it! KDE rocks!!! :-D

Thursday, November 02, 2006

IMP, Horde and UW-IMAP

IMP is a very nice software that provides a webmail access to IMAP and POP3 servers. It is based on the framework Horde. I've been using for some time now and it always worked fine. Then, one day, after an apt-get upgrade, IMP is updated to a new version and it stopped working. In particular, I wasn't able to login (and the password was reported correctly in the system log): after a long timeout, I received as response the empty file redirect.php.

I decided to reinstall everything from scratch (Horde and IMP). Unfortunately I soon experienced problems with Horde itself, due to the default cookie directory which was '/horde', but, following the installation path, it must be '/horde3', see also this bug: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=391493. But this was solved.

The problem with IMP was still there, though! After struggling with it a whole afternoon, I came through this nice wiki, and in particular with this page, which sounded really related to my case (since I'm using UW-IMAP server). Well the problem was really that!!!

As a solution, I chose this one (which is not supported by IMP developers, but it works):

'imap_config' => array(
'children' => false,
'namespace' => array(
'main/' => array(
'name' => 'mail/',
'delimiter' => '/',
'type' => 'personal',
'hidden' => false,
),
),
'search_charset' => array(
'UTF-8' => true
)
)


It looks the problem is only due to this particular IMAP server...

I also updated a related bug report: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=383715.