Saturday, July 26, 2008

T9 Dictionary Implementation in C, BASH

/*
t9.c
Dependency : t9.dic
A file with lots of words to populate our t9 trie structure.
All in small letter no spaces no other characters
Terminated by a line with only a 0 (zero)
=================
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct t9
{
struct t9 * node[27];
};

struct t9 * t9_new()
{
struct t9 * r = (struct t9 *)malloc(sizeof(struct t9));

int i=0;
for(;i<27;i++) r->node[i] = (struct t9 *)0;

return r;
}

void t9_free(struct t9 * root)
{
if(root)
{
int i=0;
for(;i<27;i++)
t9_free(root->node[i]);
free(root);
}
}

struct t9 * t9_insert(struct t9 * root ,char *val)
{

if(!root){ root = t9_new(); }

if(!*val) return root;
*val |= ('A' ^ 'a');
char c = *val - 'a';
root->node[c] = t9_insert(root->node[c] ,++val);

return root;
}

void t9_print(char *pre, struct t9 * root,int depth)
{
int i=0,flag=0;
for(;i<27;i++)
{
if(root->node[i])
{
pre[depth]='a'+i;flag=1;
t9_print(pre,root->node[i],depth+1);
pre[depth]=0;
}
}
if(flag == 0)
{
pre[depth]=0;
printf("%s\n",pre);
}
}

int in_mob_ks(struct t9 * root,char val,int o)
{

int a[]={0,3,6,9,12,15,19,22,26};
/* 2=>0 1 2
3=>3 4 5
4=>6 7 8
5=>9 10 11
6=>12 13 14
7=>15 16 17 18
8=>19 20 21
9=>22 23 24 25
*/
if(o && o>=a[val+1]) return -1;


int s=o?o:a[val];
int e=a[val+1];
//printf("From %d-%d",s,e);
for(;s<e;s++)
if(root->node[s])
return s;
return -1;
}

void t9_search_mob(char *pre, struct t9 * root,int depth,char *val)
{

if(*(val+depth)==0)
{
pre[depth]=0;
t9_print(pre,root,depth);
return;
}

int i=in_mob_ks(root,*(val+depth)-'2',0);
if(i==-1)
{
pre[depth]=0;
//printf("%s\n",pre);
}
while(i>=0)
{
pre[depth]=i+'a';
t9_search_mob(pre,root->node[i],depth+1,val);
pre[depth]=0;
i=in_mob_ks(root,*(val+depth)-'2',i+1);
}
}


struct t9 * t9_search(struct t9 * root, char *val)
{
while(*val)
{
if(root->node[*val-'a'])
{
root = root->node[*val-'a'];
val++;
}
else return NULL;
}
return root;
}

int main()
{
struct t9 * root = (struct t9 *) 0;
char a[100],b[100];int i;
FILE *fp = fopen("t9.dic","r");
while(!feof(fp))
{
fscanf(fp,"%s",&a);
if(a[0]=='0')break;
root=t9_insert(root,a);
}

while(1)
{
printf("mob keys 2-9:");
scanf("%s",&a);
if(a[0]=='0')break;
t9_search_mob(b,root,0,a);
}
t9_free(root);
}



V2
Nothin's changed except for the input method and output limited:

/*
t9.c
Dependency : t9.dic
A file with lots of words to populate our t9 trie structure.
All in small letter no spaces no other characters
Terminated by a line with only a 0 (zero)
Input :
2-9 mobile keys
0 for a new word
q to quit
=================
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <termios.h>
#include <unistd.h>

int getch( )
{
struct termios oldt,newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}

int maxdepth = 0;

struct t9
{
struct t9 * node[27];
};

struct t9 * t9_new()
{
struct t9 * r = (struct t9 *)malloc(sizeof(struct t9));

int i=0;
for(;i<27;i++) r->node[i] = (struct t9 *)0;

return r;
}

void t9_free(struct t9 * root)
{
if(root)
{
int i=0;
for(;i<27;i++)
t9_free(root->node[i]);
free(root);
}
}

struct t9 * t9_insert(struct t9 * root ,char *val)
{

if(!root){ root = t9_new(); }

if(!*val) return root;
*val |= ('A' ^ 'a');
char c = *val - 'a';
root->node[c] = t9_insert(root->node[c] ,++val);

return root;
}

void t9_print(char *pre, struct t9 * root,int depth)
{
int i=0,flag=0;

if(depth < maxdepth)
for(;i<27;i++)
{
if(root->node[i])
{
pre[depth]='a'+i;flag=1;
t9_print(pre,root->node[i],depth+1);
pre[depth]=0;
}
}
if(flag == 0)
{
pre[depth]=0;
printf("%s\n",pre);
}
}

int in_mob_ks(struct t9 * root,char val,int o)
{

int a[]={0,3,6,9,12,15,19,22,26};
/* 2=>0 1 2
3=>3 4 5
4=>6 7 8
5=>9 10 11
6=>12 13 14
7=>15 16 17 18
8=>19 20 21
9=>22 23 24 25
*/
if(o && o>=a[val+1]) return -1;


int s=o?o:a[val];
int e=a[val+1];
//printf("From %d-%d",s,e);
for(;s<e;s++)
if(root->node[s])
return s;
return -1;
}

void t9_search_mob(char *pre, struct t9 * root,int depth,char *val)
{

if(*(val+depth)==0)
{
pre[depth]=0;
t9_print(pre,root,depth);
return;
}

int i=in_mob_ks(root,*(val+depth)-'2',0);
if(i==-1)
{
pre[depth]=0;
//printf("%s\n",pre);
}
while(i>=0)
{
pre[depth]=i+'a';
t9_search_mob(pre,root->node[i],depth+1,val);
pre[depth]=0;
i=in_mob_ks(root,*(val+depth)-'2',i+1);
}
}


struct t9 * t9_search(struct t9 * root, char *val)
{
while(*val)
{
if(root->node[*val-'a'])
{
root = root->node[*val-'a'];
val++;
}
else return NULL;
}
return root;
}

int main()
{
struct t9 * root = (struct t9 *) 0;
char a[100],b[100];int i;
FILE *fp = fopen("t9.dic","r");
while(!feof(fp))
{
fscanf(fp,"%s",&a);
if(a[0]=='0')break;
root=t9_insert(root,a);
}

i=0;
while((a[i]=getch()) != 'q')
{
if(a[i]=='0'){i=0;continue;}
if(a[i] >= '2' && a[i] <= '9')
{
i++;
a[i]=0;
maxdepth = i;
printf("%s:\n",a);
t9_search_mob(b,root,0,a);
fflush(stdout);
}
}
t9_free(root);
}


t9.dic

T9 Dictionary sample file

Now in BASH!

#!/bin/bash
# bash script does something outrageous.
# It attempts to reproduce the T9 capability of the Mobile Phones !

# And does it in fewer commands than before!
# Adapted from Rahul's Original T9.sh

umask 077
NUMN=/tmp/numn.$$
trap "exit 1" HUP INT PIPE QUIT TERM
trap "rm -f $DICTN $NUMN" EXIT

cat t9.dic | tr a-z 22233344455566677778889999 > $NUMN

if [ $# -eq 0 ] ; then
echo "Usage : t9.sh "
exit 1;
else
paste -d: $NUMN t9.dic | grep "^$1" | cut -d":" -f2 | sort
fi

Sunday, July 20, 2008

Server Not Found, Fix DNS Problems.

Sometimes wierd things happen,
Server Not Found www.google.com
Or you can get www.kitiyo.com but not rgb.kitiyo.com
Or you cannot get www.somesite.com but your friend can!
Silly DNS Problems.

When we browser and enter a url in the browser's address bar, one of the first step is to resolve the domain name. ie convert the www.server-name.com to the IP address.

If our dns server settings are wrong or if the dns server does not respond, we get the familiar
Server Not Found page.

There are some ways to resolve this.

  1. Get the correcet DNS IP address from the ISP, and get more than one.
  2. Get some free DNS IP address, some of which are given below
    67.138.54.100
    207.225.209.66
    208.67.220.220
    208.67.222.222
  3. Install a local caching dns server that caches servernames and ip address so that subsequent visits can be made faster since resolving is done locally.
    like Ubuntu debian package dnsmasq


Adding name servers
Windows :

  • Click TCP/IP and then click Properties.
  • Add Primary and Secondary DNS Servers.


Linux:
Add the lines to /etc/resolv.conf
nameserver IP.ADD.RES.SSS
nameserver 67.138.54.100


Any more ideas ?

Wednesday, July 16, 2008

Fix the Brightness up/down Button on ubuntu 8.04 running on Compaq Presario V3000

Replace the contents in the following files given by the code given below.

Replace /etc/acpi/video_brightnessup.sh with the following code


#!/bin/bash

CURRENT=$(grep "current:" /proc/acpi/video/VGA/LCD/brightness |awk '{print $2}')


case "$CURRENT" in

100)
echo -n 100 > /proc/acpi/video/VGA/LCD/brightness;
;;
92)
echo -n 100 > /proc/acpi/video/VGA/LCD/brightness;
;;
84)
echo -n 92 > /proc/acpi/video/VGA/LCD/brightness;
;;
76)
echo -n 84 > /proc/acpi/video/VGA/LCD/brightness;
;;
68)
echo -n 76 > /proc/acpi/video/VGA/LCD/brightness;
;;
60)
echo -n 68 > /proc/acpi/video/VGA/LCD/brightness;
;;
52)
echo -n 60 > /proc/acpi/video/VGA/LCD/brightness;
;;
44)
echo -n 52 > /proc/acpi/video/VGA/LCD/brightness;
;;
36)
echo -n 44 > /proc/acpi/video/VGA/LCD/brightness;
;;
28)
echo -n 36 > /proc/acpi/video/VGA/LCD/brightness;
;;
20)
echo -n 28 > /proc/acpi/video/VGA/LCD/brightness;
;;

*) #default case
echo -n 68 > /proc/acpi/video/VGA/LCD/brightness ;
;;
esac


Replace /etc/acpi/video_brightnessdown.sh with

#!/bin/bash

CURRENT=$(grep "current:" /proc/acpi/video/VGA/LCD/brightness |awk '{print $2}')


case "$CURRENT" in

100)
echo -n 92 > /proc/acpi/video/VGA/LCD/brightness;
;;
92)
echo -n 84 > /proc/acpi/video/VGA/LCD/brightness;
;;
84)
echo -n 76 > /proc/acpi/video/VGA/LCD/brightness;
;;
76)
echo -n 68 > /proc/acpi/video/VGA/LCD/brightness;
;;
68)
echo -n 60 > /proc/acpi/video/VGA/LCD/brightness;
;;
60)
echo -n 52 > /proc/acpi/video/VGA/LCD/brightness;
;;
52)
echo -n 44 > /proc/acpi/video/VGA/LCD/brightness;
;;
44)
echo -n 36 > /proc/acpi/video/VGA/LCD/brightness;
;;
36)
echo -n 28 > /proc/acpi/video/VGA/LCD/brightness;
;;
28)
echo -n 20 > /proc/acpi/video/VGA/LCD/brightness;
;;
20)
echo -n 20 > /proc/acpi/video/VGA/LCD/brightness;
;;

*) #default case
echo -n 68 > /proc/acpi/video/VGA/LCD/brightness ;
;;
esac


for other laptops
run as root
 grep "levels:" /proc/acpi/video/VGA/LCD/brightness


And replace the brightness values in the files above.

:)

Monday, July 7, 2008

Hosting a Site at your Home using Dataone

Intro

This is just a timepass for anyone who would want to try making their home PC to a server. The details are for my BSNL Dataone Router WA300XXX, But you can do it on any router provided your ISP supports, it works for BSNL (Bharath Sanchar Nigam Limited, INDIA) (Hurray!) .

So the first step is to setup apache, HTTP Server. I have covered this topic in my earlier post and doesnt want to repeat

Quick install for Linux/Debian way :

sudo apt-get install apache2


On the router..
Once having done that next step is to open a PORT in the router and forward the port to our PORT 80 on your computer. Port 80 is the default HTTP port. You can open up any port in the router, say 2345.

I did it using telnet and the way to open up a port differs from router to router.
Most modem hosts a site and runs a server internally at port 80, so you can try http://192.168.1.1
or the IP address of the router.

It will ask for a username and password.
The default username and password for the router is admin

Find out NAT
Here is my screen shot of the page


Add an entry similar to that

Name : http # Whats in a name ?
External Port : 2345 #You choose, 80 may not work
Protocol : TCP
Internal Port : 80
Server IP : 192.168.1.232 #thts me


Ok I guess thats it!

Restart your modem
Find your IP
and suppose your ip is AAA.BBB.CCC.DDD

People outside can access your site http://AAA.BBB.CCC.DDD:2345/
(as long as your ip is valid and your computer is on)

What abt something like www.yourname.com?

For that you gotta pay, Since remebering numbers is not so good when compared to memorizing a name a DNS system is used. And to get into a DNS you will need a static IP and pay to point a www.yourname.com to your static IP. There are workarounds for everything, if anyone is generous to comment on this, please help. :)

One more thing
You cant access the site from http://AAA.BBB.CCC.DDD:2345/, for that http://localhost is better.

Try it out...