Mi blog lah! Το ιστολόγιό μου

22Jun/095

The bashrc bash configuration files

The default shell in most Linux distributions is the bash shell. Contrary to all the usability work that has been done to the GUI, the shell is most neglected area.

Current bashrc shell (shows prompt only)

Depicting a shell is not an easy task; in the screenshot above we only show the default prompt. It has the following disadvantages,

  1. It does not differentiate visually between the username and hostname.
  2. It shows the relative path only, making it difficult to realize quickly the full path for the current working directory.
  3. Cannot copy the path using the mouse by double-clicking on it. The ~ is not included in the highlighted text, that one needs to paste and add the remaining part of the path (such as /home/user/)
  4. The point of input changes position on the command line, depending on the size of the path. As you cd into directories, the point of input moves further to the right.

The bashrc project shell

This is the prompt with the bashrc project configuration files. It solves the problems described with the default configuration files found in Linux distributions.

Obviously, there are more to the shell's configuration files than a usable prompt. For example,

  • the ability to show the partial matches when you press Tab for the first time
  • enabling the shopt options to reasonable values
  • have reasonable aliases for . .. ... / -
  • adding --verbose, --interactive to basic utilities such as cp, mv, rm
  • show the exit value of an application if it is other than 0 ($?)

There is a EnhancedBash project for the Ubuntu Linux distribution which might be able to break apart and provide better default configuration files.

If you want to help and add more to the proposed configuration, visit http://github.com/simos/bashrc/

To use the bashrc shell, you need to

  1. Download the latest package from http://github.com/simos/bashrc/ (note the Download button).
  2. Extract the package, open a terminal window and enter the newly created directory.
  3. Run make install
  4. Open a new shell window. The new settings should be activated.
16Nov/078

Droid fonts from Google (Android SDK)

Update 10Feb2009: The Droid fonts are now available from android.git.kernel.org (Download tar.gz archive), under the Apache License, Version 2.0. Ascender (the company who created Droid), has now a dedicated website at http://www.droidfonts.com/ (thanks Rex!). At this dedicated website, Ascender presents the Droid Pro family which has several additions to Droid. For the open-source crowd, it is important to have the initial Droid font family dual-licensed under the “OpenFont License”, which would enable the best use with the rest of the OFL licensed fonts.

Two years ago, Google bought a start-up called Android in order to deliver an open platform for mobile applications. A few days ago the Android SDK has been released and you can develop now Android applications that can run in the emulator. Android handsets are expected at some point next year.

Even if you do not plan to develop applications for Android, you can still run the emulator which is functional, includes quite a few samples, and comes with a browser shown above. To get it, download the Android SDK for your system, uncompress it and run

./android_sdk_linux_m3-rc20a/tools/emulator

An interesting aspect of Android is that it comes with a set of fonts that have been specially designed for mobile devices, the Droid fonts. The fonts are embedded in the Android image, in android_sdk_linux_m3-rc20a/tools/lib/images/system.img, a clever guy managed to extract them and a modest guy corrected me (Damien's blog to download).

The fonts are probably licensed under the same license as the SDK (Apache License), however it is better to hear from Google first.

In the meantime, here is a screenshot of Ubuntu 7.10 with Droid.

Update: To extract the fonts from the SDK, run the emulator with the -console parameter. The emulator starts and at the same time you get a shell to the filesystem of the running emulator. You can locate the fonts in system/fonts/. Once located the full path of a file, you can extract with ./adb pull system/fonts/DroidSans.ttf /tmp/DroidSans.ttf (thanks cosmix for the tip).

11Nov/070

Localisation issues in home directory folders (xdg-user-dirs)

In new distributions such as Ubuntu 7.10 there is now support for folder names of personal data in your local language. What this means is that ~/Desktop can now be called ~/Επιφάνεια εργασίας. You also get a few more default folders, including ~/Music, ~/Documents, ~/Pictures and so on.

This functionality of localised home folders has become available thanks to a new FreeDesktop standard, XDG-USER-DIRS. xdg-user-dirs can be localised, and the current localisations are available at xdg-user-dirs/po.

A potential issue arises when a user logs in with different locales; how does the system switch between the localised versions of the folder names? For GNOME there is a migration tool; as soon as you login into your account with a different locale, the system will prompt whether you wish to switch the names from one language to another. This is available through the xdg-user-dirs-gtk application.

Another issue is with users who use the command line quite often; switching between two languages (for those languages that use a script other than latin) tends to become cumbersome, especially if you have not setup your shell for intelligent completion. In addition, when you connect remotely using SSH, you may not be able to type in the local language at the initial computer which would make work very annoying.

Furthermore, there have been reports with KDE applications not working; if someone can bug report it and post the link it would be great. The impression I got was that some installations of KDE did not read off the filesystem in UTF-8 but in a legacy 8-bit encoding. This requires further investigation.

Moreover, OpenOffice.org requires some integration work to follow the xdg-user-dirs standard; apparently it has its own option as to which folder it will save into any newly created files. I believe this will be resolved in the near future.

Now, if we just installed Ubuntu 7.10 or Fedora 8, and we got, by default, localised subfolders in our home directory (which we may not prefer), what can we do to revert to non-localised folders?

The lazy way is to logout, choose an English locale as the default locale for the system and log in. You will be presented with the xdg-user-dirs-gtk migration tool (shown above) that will give you the option to switch to English folder names for those personal folders.

Clarification: It is implied for this workaround (logout and login thing), you then log out again, set the language to the localised one (i.e. Greek) and log in. This time, when the system asks to rename the personal folders, you simply answer no, and you end up with a localised desktop but personal folders in English. Mission really accomplished.

If you are of the tinkering type, the files to change manually are

$ cat ~/.config/user-dirs.locale

el_GR

$

and

$ cat ~/.config/user-dirs.dirs

# This file is written by xdg-user-dirs-update
# If you want to change or add directories, just edit the line you're
# interested in. All local changes will be retained on the next run
# Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped
# homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an
# absolute path. No other format is supported.
#
XDG_DESKTOP_DIR="$HOME/Επιφάνεια εργασίας"
XDG_DOWNLOAD_DIR="$HOME/Επιφάνεια εργασίας"
XDG_TEMPLATES_DIR="$HOME/Πρότυπα"
XDG_PUBLICSHARE_DIR="$HOME/δημόσιο"
XDG_DOCUMENTS_DIR="$HOME/Έγγραφα"
XDG_MUSIC_DIR="$HOME/Μουσική"
XDG_PICTURES_DIR="$HOME/Εικόνες"
XDG_VIDEOS_DIR="$HOME/Βίντεο"

Personally I believe that having localised names appear under the home folder is good for the majority of users, as they will be able to match what is shown in Locations with the actual names on the filesystem.

There will be cases that software has to be updated and bugs fixed (such as in backup tools). As we proceed with more advanced internationalisation/localisation support in Linux, it is desirable to follow forward, and fix problematic software.

However, if enough popular support arises with clear arguments (am referring to Greek-speaking users and a current discussion) for default folder names in the English languages, we could follow the popular demand.

Also see the relevant blog post New Dirs in Gutsy: Documents, Music, Pictures, Blah, Blah by Moving to Freedom.

7May/066

Domains and Webhosting #2

There are loads of companies that offer webhosting services. We distinguish between dedicated webhosting (you have a server on your own at the datacentre), virtual private server (VPS) (a server is partitioned to virtual private servers through VMWare, Xen or other) and shared webhosting (you share the server, you get your own virtual host in Apache).

The most affordable of the three is shared webhosting and prices start at around 20 euros per year for facilities that allow you to run your own WordPress blog.

To run your own WordPress blog, you need

  • Apache (Linux, etc but preferably not Windows)
  • mysql database
  • ability to upload files (typically FTP upload)
  • PHP
  • mod_rewrite is preferable
  • safe_mod off for PHP is preferable
  • disk space at least 20MB for WordPress
  • monthly bandwidth at least 2GB (for typical non-mainstream blog)

It is important that the webhosting provider allows you to administer your webhosting options and offer a tool like cPanel.

Normally you do not get shell access. In some cases you do, as long as you provide a photo ID.
Normally you do not get a backup option as it tends to be complicated to apply. For a WordPress blog, the typical changes are applied in the mysql database so there are no files that change over time (apart from plugin installation). Therefore, it is good to have a way to backup your mysql database. You could use phpmyadmin to backup your database, however it is more suitable to use something like the WordPress Database Backup plugin.
If you have recommendations for webhosting, post them as comments.

3Oct/050

Τι εκτυπώνει το πρόγραμμα; [Λύση]

#include <stdio.h>

int calculate(void);

int calculate(void)
{
int i;

int p[9];

for ( i = 0; i <= 3; i++ )
p[i*3+2]+=16;
}

int main(int argc, char** argv)
{

calculate();
printf("Hello, World!\n");

return 0;
}

Μεταγλωττίζουμε το πρόγραμμα της προηγούμενης εγγραφής και εκτελούμε (πλατφόρμα Linux/i386, gcc 4.0.x).

simos@home /tmp
$ gcc calculate.c -o calculate

simos@home /tmp
$ ./calculate

simos@home /tmp
$ _

(Αν η προτροπή του φλοιού (shell) φαίνεται παράξενη, χρησιμοποιώ tcsh με tcshrc).

Όπως βλέπουμε, το πρόγραμμα calculate δεν εκτυπώνει τίποτα, ενώ στον κώδικα υπάρχει αναφορά να εκτυπώσει Hello, World!. Στον κώδικα δεν υπάρχει κάποια εντολή που να παρακάμπτει την εκτύπωση του μηνύματος. Τι συμβαίνει; Μπορεί η κλήση της αθώας συνάρτησης calculate() να επηρεάσει την ροή του προγράμματος;

Ας δούμε τι γίνεται στη συνάρτηση calculate(). Όταν η μεταβλητή i παίρνει την τιμή 3, γίνεται τροποποίηση του πεδίου p[11] του πίνακα p. Ο πίνακας p διαθέτει 9 θέσεις (p[0] μέχρι p[8]), οπότε σε τι πράγμα τροποποιήσαμε την τιμή;

Η C/C++ είναι μια γλώσσα untyped, που σημαίνει μεταξύ άλλων ότι είναι καθήκον και υποχρέωση του προγραμματιστή να προσέχει να μην γράφει σε θέσεις μνήμης που δεν έχει δεσμεύσει.

Οπότε, στο παράδειγμά μας πού γράψαμε στην μνήμη;

Με την εντολή objdump -d calculate μπορούμε να δούμε το πρόγραμμα σε μορφή γλώσσας μηχανής.
Ακολουθεί απόσπασμα από τον κώδικα της συνάρτησης main().

...
80483c4: b8 00 00 00 00 mov $0x0,%eax
80483c9: 83 c0 0f add $0xf,%eax
80483cc: 83 c0 0f add $0xf,%eax
80483cf: c1 e8 04 shr $0x4,%eax
80483d2: c1 e0 04 shl $0x4,%eax
80483d5: 29 c4 sub %eax,%esp
80483d7: e8 a0 ff ff ff call 804837c <calculate>
80483dc: 83 ec 0c sub $0xc,%esp
80483df: 68 98 84 04 08 push $0x8048498
80483e4: e8 bf fe ff ff call 80482a8 <puts @plt>
80483e9: 83 c4 10 add $0x10,%esp
80483ec: b8 00 00 00 00 mov $0x0,%eax
80483f1: c9 leave
80483f2: c3 ret
80483f3: 90 nop
-- τέλος της main() --

Προσέξτε την κλήση της συνάρτησης calculate() [θέση 80483d7]. Στον δομημένο προγραμματισμό έχουμε κλήσεις συναρτήσεων/υπορουτινών που μεταφέρουν την ροή εκτέλεσης σε ένα άλλο κομμάτι προγράμματος, και μετά επιστρέφουν για να προχωρήσουν στην επόμενη εντολή. Πως θυμάται το πρόγραμμα σε εκτέλεση ποια είναι η επόμενη εντολή (στην περίπτωση μας, η εντολή στην διεύθυνση 80483dc);

Την θυμάται επειδή την τοποθετεί στην στοίβα, σε μια περιοχή μνήμης που είναι εύκολα προσβάσιμη από την καλούμενη συνάρτηση (calculate() στην περίπτωσή μας). Στο παράδειγμα

int calculate(void)
{
int i;

int p[9];

το p[11] δείχνει στην διεύθυνση μνήμης που περιέχει την επόμενη εντολή που θα εκτελέσει ο επεξεργαστής μόλις ολοκληρώσει την εκτέλεση της συνάρτησης calculate(). Ένα ενδιαφέρον σημείο είναι ότι το p[9] είναι το ίδιο με την μεταβλητή i! Δηλαδή αν τροποποιήσουμε κατά λάθος το p[9], θα αλλάξει και το i, διότι δείχνουν στην ίδια περιοχή της μνήμης.

Στο πρόγραμμά μας προσθέτουμε 16 στο p[11]. Γιατί 16; Η σωστή τιμή του p[11] είναι 0x80483dc, εμείς όμως θέλουμε να παρακάμψουμε την εκτύπωση του μηνύματος (printf()). Θέλουμε δηλαδή να πάμε στην διεύθυνση 0x80483ec (εντολή return 0;).

0x80483ec - 0x80483dc = 0x10 ή 16 στο δεκαδικό σύστημα, οπότε πρέπει να προσθέσουμε 16 στην διεύθυνση επιστροφής.