Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Friday, December 4, 2009

Getting started with Objective-C and GNUstep on Linux

I couldn't find a simple one-stop guide by example, so here should be enough info to get you up and running with with Objective-C on Linux, including the obligatory hello world example. We will create a HelloWorld class, subclassed from the default object class, and show how to compile an example program.

Installation
This is specific to Arch, but you'll just need to install your distro's packages for gcc Objective-C support and the GNUstep base:

pacman -S gcc-objc gnustep-base

On Arch GNUstep installs to /opt/GNUstep.

Interface
We need to declare the interface for our class.

HelloWorld.h:

// use the GNUstep Foundation library
#import <Foundation/NSObject.h>

// declare interface for HelloWorld class as a subclass of NSObject
@interface HelloWorld : NSObject {

// instance variables
char *string;
}

// method definitions
- (id)setString: (char *)s;
- (char *)string;
- (void)speak;

// end interface declaration
@end


Implementation
Now to implement our simple class by writing the methods. Objective-C implementation files traditionally use the .m extension.


HelloWorld.m:

// include our interface declaration
#import <HelloWorld.h>

// start the implementation of the HelloWorld class
@implementation HelloWorld

// constructor
- (HelloWorld *)init
{
[super init]; // call init from superclass
[self setString: "hello world"]; // set default value
}

// accessor method to get value of string
- (char *)string
{
return(string);
}

// set string value
- (id)setString: (char *)s
{
string = s;
}

// print current value of string
- (void)speak
{
printf("%s\n", string);
}

// end the implementation
@end


Main
As with C, the entry point to our program is the main() function.

main.m

#import <HelloWorld.h>

int main (int argc, const char *argv[]) {

// create instance of our class
HelloWorld *hw = [[HelloWorld alloc] init];

// print the default
[hw speak];

// set new value and print
[hw setString: "goodbye"];
[hw speak];

// release memory
[hw release];

return 0;
}


Makefile
An example Makefile, showing required GNUstep libs. You will need to change the value of GNUSTEP to your distro's install location.

Makefile:

GNUSTEP=/opt/GNUstep
EXEC=hello
OBJS=HelloWorld.o main.o
LIBS=-lgnustep-base
LIBDIRS=-L${GNUSTEP}/Local/Library/Libraries
INCLUDES=-I${GNUSTEP}/Local/Library/Headers -I.

all: ${EXEC}

${EXEC}: ${OBJS}
gcc -o ${EXEC} ${OBJS} ${LIBDIRS} ${LIBS}

main.o: main.m
gcc -c ${INCLUDES} main.m

HelloWorld.o: HelloWorld.m HelloWorld.h
gcc -c ${INCLUDES} HelloWorld.m

clean:
rm -f ${OBJS} ${EXEC}


Compile and run

$ make
$ ./hello
hello world
goodbye


Further reading

Friday, July 3, 2009

Using Unix date command for timezone conversion

I do a lot of work with developers in Bangalore, and am always wanting to do date calculations to figure out what time it is there. Using the TZ environment variable, it is easy to do this using plain old date (I am using 7.4 from GNU coreutils).

For example, what time is it right now in India?

$ TZ=Asia/Calcutta date
Sat Jul 4 00:40:41 IST 2009


I want to hold a meeting at 9am, what time will it be in India?

$ TZ=Asia/Calcutta date -d '9am EDT'
Sat Jul 4 18:30:00 IST 2009


There's a meeting on Friday at 4pm in India, what time will it be here?

$ date -d 'Fri 4pm IST'
Fri Jul 3 06:30:00 EDT 2009


For extra goodness, if you use zsh, do TZ=[TAB] and the shell will complete available timezones for you.

Tuesday, June 16, 2009

Juniper Network Connect VPN on 64-bit Arch Linux

There are several guides out there that describe running Juniper Network Connect VPN client on linux (see particularly MadScientist's guide for Ubuntu). These are just some notes on running the client from the command-line on a 64-bit Arch Linux system.

Since the netsvc client is 32-bit, we need to install some 32-bit support software. The method I use is to install 32-bit compatibility libs (effectively making my system multilib). Another solution would be to install and run netsvc from a 32-bit chroot environment.

To download Network Connect, connect to your VPN server with a browser and click the Network Connect Start button. This will download the software to ~/.juniper_networks.

Install 32-bit libs:

pacman -S lib32-glibc lib32-gcc-libs lib32-zlib


Extract your certificate:

cd ~/.juniper_networks
jar -xf ncLinuxApp.jar getx509certificate.sh
sh getx509certificate.sh your.vpn.server.com certfile


You can now run netsvc as follows:

netsvc -h your.vpn.server.com -u username -f certfile -r realm -p passwd


Your realm can be found from the HTML code of the server page. Look for something like:

<input name="realm" value="Your Realm" type="hidden">


The following simple script takes care of the whole thing:

#!/bin/bash
## Install as vpn.sh.
## Run as 'vpn.sh start' and input your password.
## It should be as simple as that.

## settings
HOST="your.vpn.server.com"
USER="YourUsername"
REALM="Your Realm"
JAR="/opt/java/bin/jar"
JUNIPER="${HOME}/.juniper_networks"
CERT="${JUNIPER}/network_connect/${HOST}.cert"
NCSVC="${JUNIPER}/network_connect/ncsvc"

start () {
## get passwd
read -s -p "Password: " passwd
echo ""

## get server certificate
pushd ${JUNIPER}
if [ ! -x "getx509certificate.sh" ]; then
${JAR} -xf ncLinuxApp.jar getx509certificate.sh
fi
sh getx509certificate.sh "${HOST}" "${CERT}" || die "failed get cert from $HOST"
popd

## run the network connect program
"${NCSVC}" -h "$HOST" -u "$USER" -f "$CERT" -r "$REALM" -p $passwd &
}

stop () {
## kill Network Connect
${NCSVC} -K
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
;;
esac