Friday, February 8, 2013

Concatenate lines in a file into a single line with PowerShell

Assuming you have a input file in.txt with following lines:

a
b
c

and you'd like to concatenate these 3 lines into a single line to a file out.txt i.e.

abc

You can use the following PowerShell command:

-join (cat in.txt) | out-file out.txt

Basically, the cat in.txt is executed first and returned the content of in.txt as an array of strings. The -join cmdlet then joins the lines together into a single line, which is then piped (|) into the out-file cmdlet to produce the out.txt file.

Sunday, April 22, 2012

Replace xscreensaver in CrunchBang Linux 10 "Startler"

Deep down in my heart I am a minimalist and to me CrunchBang Linux has the right balance between minimalism and usefulness. However, one thing I've always struggled with it is the choice of xscreensaver for the simple lock screen task because it also does monitor power management, which has to be turned off to avoid conflict with xfce4 power manager.

Last week I finally decided to bite the bullet and replaced it with much simpler slock and here's how I did it.

Step 1 - Remove xscreensaver and install the screen locker of your choice

To remove xscreensaver simply:

sudo apt-get purge xscreensaver

Next, you'll need to install xautolock to lock screen automatically after certain idle time (mine's set to 1 minute):

sudo apt-get install xautolock

Now you have to choose a screen locker. I use slock, which is probably the simplest screen lock ever because all you get is a blank lock screen. There's also xtrlock, which is installed by apt automatically when I installed xautolock and i3lock, which allows you to choose a background image for the lock screen.

Once you've decided which one you wanna use, here's the hackery part of the job. You need to:

cd /usr/bin
sudo ln -s slock xflock4

Right now you might be thinking, "gee this is a hack, there must be a better way". The truth is there really isn't because the XFce4 Power Manager is hard-coded to try a list of screen lockers in the source code:

xfpm_lock_screen (void)
{
    gboolean ret = g_spawn_command_line_async ("xflock4", NULL);

    if ( !ret )
    {
        g_spawn_command_line_async ("gnome-screensaver-command -l", NULL);
    }

    if ( !ret )
    {
        /* this should be the default*/
        ret = g_spawn_command_line_async ("xdg-screensaver lock", NULL);
    }

    if ( !ret )
    {
        ret = g_spawn_command_line_async ("xscreensaver-command -lock", NULL);
    }

    if ( !ret )
    {
        g_critical ("Connot lock screen\n");
    }
}

Step 2 - Auto start xautolock

You need to add the following line to ~/.config/openbox/autostart to auto start the xautolock with right idle interval in minutes and screen locker name:

xautolock -time 1 -locker "slock" &

Remember to replace slock with the screen locker of your choice.

Step 3 - Update lock screen keyboard shortcut

Finally, find the following section in the ~/.config/openbox/rc.xml and enter the screen locker name in the <Command></Command> section.


  
    
      true
        Lock screen
    
    cb-lock
  

Wednesday, February 16, 2011

Enable PowerShell Remoting

On the remote machine, start a Administrator PowerShell session and run following commands:

Enable-PSRemoting
cd wsman:
cd localhost\client
set-item trustedhosts *

Just answer "Y" for any prompt.

To start a remote Powershell session, on your local PowerShell session, enter:

Enter-PSSession <the-remote-machine-hostname>

For some reason, Enter-PSSession can't seems to resolve DNS aliases (CNAME records) and you'll get the following error if you used it instead of the hostname.

Enter-PSSession : Connecting to remote server failed with the following error message : WinRM cannot process the request. The following error occured while using Kerberos authentication: The network path was not found.

Saturday, February 12, 2011

Running CrunchBang Linux 10 "Statler" on HP mini 1000

I was a big Ubuntu fan until they decided to use Unity in netbook remix and moved windows border icons to the left. I then switched to LinuxMint, it was alright but I'm a Chrome user and I hate the fact that they won't let you remove firefox (the package manager forces you to install "abrowser", which is still firefox but with brand removed).

Now CrunchBang Linux is my new favorite Linux distro. I hesitated before puting it on my HP mini 1000 netbook because the latest version "Statler" is based on debian, which is famous for being "Pure Open Source" and hence lack of proprietary drivers. However, I was totally wrong, nearly all hardware worked out of the box for me except for the usual wireless driver, network port and bluetooth.

Wireless

To be fair the b43 driver did work but it was fairly unstable and disconnects randomly. The solution turns out is pretty simple, all you have to do is to enter:

options b43 pio=1 qos=0

in the following file:

/etc/modprobe.d/b43.conf

and reboot.

Bluetooth

My cheap bluetooth dongle was detected but there were no software installed to interact with it. Therefore, I installed blueman by:

sudo apt-get install blueman

and add blueman-applet to the end of autostart.sh:

## Launch blueman applet
(sleep 4s && blueman-applet) &

Ethernet

Finally, the ethernet port has got the same problem as Ubuntu where the cable needed to be plugged before the system was booted up and it will freeze the system if you unplug it. To get network working properly you need to open up /etc/default/grub, find the line

GRUB_CMDLINE_LINUX_DEFAULT="quiet"

add option acpi_os_name=Linux

GRUB_CMDLINE_LINUX_DEFAULT="quiet acpi_os_name=Linux"

save the file and then run

sudo update-grub

reboot your laptop and the ethernet port should work properly now.

Saturday, October 9, 2010

AOP with Enterprise Library Policy Injection Block

I used to think aspect oriented programming (AOP) is not a very useful idea because quite often it only saves few keystrokes but requires a massive configuration file. I only came to realise the true value of AOP recently when we have to convert 4+ million lines of legacy COBOL code to .NET with Microfocus’ Visual COBOL compiler. Manually adding exception handling and logging code to these existing COBOL subroutines will not only take a lot of time but also makes code merging task much harder later.

I started experimenting with the policy injection block in the Enterprise Library to see if it will make this job simpler. I tried to google for AOP and Enterprise Library tutorials but I found most tutorials on the web are either old (most were done using Enterprise Library 3) or over-complicated. Especially many of them use the Enterprise Library Logging Block to demonstrate the policy injection concept, which requires lots of configurations itself.

The policy injection in Enterprise Library is actually pretty easy to use and therefore in this tutorial I decided to demonstrate it with a simple custom call handler that requires no configuration. This is done so that you won’t be distracted by those extra settings in the App.config. Also, I’ll be using the Enterprise Library 5 configuration GUI editor, which looks quite different from the old one.

So let’s start with a basic console program with a simple interface IGreeter, which has a single operation SayHello:

public interface IGreeter
{
    void SayHello(string to);
}

and a very basic implementation, which prints a hello message to whatever name that was passed in:

public class Greeter : IGreeter
{
    public void SayHello(string to)
    {
        Console.WriteLine("hello " + to);
    }
}

Next, the Main method simply creates a greeter instance and then says hello to “bob”.

class Program
{
    static void Main(string[] args)
    {
        var greeter = new Greeter();
        greeter.SayHello("bob");
    }
}

Finally, below is a simple console logger call handler. It sits in a separate DLL (CustomCallHandlers.dll) and will be applied to the SayHello method above using the policy injection block.

[ConfigurationElementType(typeof(CustomCallHandlerData))]
public class ConsoleCallLogger : ICallHandler
{
    public ConsoleCallLogger(NameValueCollection collection) 
    { 
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        Console.WriteLine("Before " + input.MethodBase.Name);
        var returnValue = getNext()(input, getNext);
        Console.WriteLine("After " + input.MethodBase.Name);
        return returnValue;
    }

    public int Order { get; set; }
}

The implementation of the Invoke method should be pretty self-explanatory, it basically prints a before and after message around the method invocation. Check this link out if you would like to know more about how to implement the ICallHandler interface.

A couple of things to note about the ConsoleCallLogger class above. Firstly It needs to implement the ICallHandler interface and have the ConfigurationElementType class attribute. Following assembly references needs to be added to the project in order for Visual Studio to resolve these classes:

  • Microsoft.Practices.EnterpriseLibrary.Common
  • Microsoft.Practices.Unity.Interception
  • System.configuration

Secondly, a custom call handler class is required to have a constructor that accepts a NameValueCollection argument. As you’ll see when we start configuring the App.config file, you can pass name/value pair values to the custom call logger from the configuration file.

Now, to apply the ConsoleCallLogger to the SayHello method, we need to configure the policy injection block with app.config. Before we start let’s have a look what the file will look like at the end (note: I have added space in class names so it will wrap properly):

<configuration>
  <configSections>
    <section name="policyInjection" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration. PolicyInjectionSettings, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <policyInjection>
    <policies>
      <add name="My Custom Logging">
        <matchingRules>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules. MemberNameMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              name="Member Name Matching Rule">
            <matches>
              <add match="SayHello" />
            </matches>
          </add>
        </matchingRules>
        <handlers>
          <add type="CustomCallHandlers.ConsoleCallLogger, CustomCallHandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="ConsoleCallLogger" />
        </handlers>
      </add>
    </policies>
  </policyInjection>
</configuration>

Other than the ugly long class names, it's not too bad is it? You can pretty much just code this up by hand. However, since the enterprise library comes with a configuration editor, let's make use of it.

First of all we need to add a policy injection settings block in our file:

EntlibConfig1

Next, right click on the policy injection block heading (“My Custom Logging” in my example) and add a member name matching rule block.

EntlibConfig2

Enter our our target method name SayHello into the member name match field.

EntlibConfig3

After telling the policy injection block how to find the target method (i.e. our SayHello method), we need to configure the call handler (i.e. the ConsoleCallLogger class). Again, right clean on our policy injection block header and choose “Add Custom Call Handler”.

EntlibConfig4

This will popup a dialog box and if you choose “Add From File”, you can then select the location of the DLL that contains the ConsoleCallLogger class. After selecting the DLL, the ConsoleCallLogger class should show up on the tree control as shown below:

EntlibConfig5

I did run into few problems when I was adding my DLL. Here are some tips if you run into problems too:

  1. Check your build settings, if you are using 64-bits EntLib configuration editor you should build 64-bits DLL too.
  2. Check your class, is it public? did you implement the ICallHandler interface? did you remember to add the ConfigurationElementType attribute? and did you pass typeof(CustomCallHandlerData) to it?
  3. Save and restart your configuration editor. I think there might be some bugs in the configuration editor because the first time I did this I just couldn’t get the ConsoleCallLogger class to show in the tree control no matter what I did. I end up saving and restarting a couple of times and eventually it came up.

After the call handler has been added, your setting should look something similar to this:

EntlibConfig6

Now, save the app.config to the console program and add it to the project. To enable policy injection block in the console program, we first need to add following assemblies to the project:

  • Microsoft.Practices.EnterpriseLibrary.PolicyInjection
  • Microsoft.Practices.Unity.Interception
  • CustomCallHandlers (the DLL that contains the ConsoleCallLogger class).

Next we need to replace the direct object instantiation:

var greeter = new Greeter();

with PolicyInjection.Create in the Main method:

var greeter = PolicyInjection.Create<Greeter, IGreeter>();

If everything’s been configured properly, when you run the console program it’ll show the following output:

Before SayHello
hello bob
After SayHello

As you can see, the ConsoleCallLogger class has been magically applied to the SayHello method.

AOP is a very useful technique if you have to extend some legacy code without actually having to touch those code. Enterprise library already shipped with a few useful call handlers for common cross cutting concerns such as the logging and security. If none of them fits your needs you can always follow what I did in this tutorial and write your own call handlers.

Finally, you can download codes in this tutorial with the SkyDrive link below:

Thursday, September 30, 2010

COBOL Tutorial 00300 – Edited Fields

As I have mentioned in the last tutorial, you use edited fields in COBOL to format data fields into human-readable display strings. Let’s start with a numeric field:

01 NUMERIC-FIELD PIC 999999V99.

and some COBOL code that set and display the field value:

MOVE 1234.5 TO NUMERIC-FIELD.
DISPLAY NUMERIC-FIELD: ' NUMERIC-FIELD.

As we’ve demonstrated in the previous tutorial, unused digits are padded with ugly zeros:

NUMERIC-FIELD: 001234.50

Let me put my C# programmer hat on again (apologies to Java, ruby, python, C/C++, assembly and many other programmers who don’t like C#), when we have to format a variable for display, we often use the string.Format method with a formatting string containing special formatting characters, which is “0,0.00” in the following example:

// returns 1,234.50
string.Format("{0:0,0.00}", 1234.5)

Now let’s come back to COBOL, an edited field is basically a normal COBOL data field with a formatting string in the picture clause instead of the “A”, “X” or “9” data type specifiers. The edit field's formating string is based on similar ideas as C#’s one. To achieve the same output as the C# code above, I use “ZZZ,ZZZ.99” formating string as shown in the following example:

01 EDITED-NUMERIC-FIELD PIC ZZZ,ZZZ.99.

Unlike the place holding character “9”, each unused “Z” in the picture clause is not filled with “0” and the “,” just inserts a comma in the display value. Therefore, if we move the value of NUMERIC-FIELD to the EDITED-NUMERIC-FIELD and then display the content of EDIT-NUMERIC-FIELD:

MOVE NUMERIC-FIELD TO EDITED-NUMERIC-FIELD.
DISPLAY 'EDITED-NUMERIC-FIELD: ' EDITED-NUMERIC-FIELD.

The result is a much more readable output:

EDITED-NUMERIC-FIELD:   1,234.50

Keep in mind that an edited field is basically an alpha-numeric field, so you cannot perform arithmetic calculation with it. For example if you add following line of code to your program:

ADD 1 TO EDITED-NUMERIC-FIELD.

You will get the following compile error:

Error: 'EDITED-NUMERIC-FIELD' is not numeric name

This is a summary of commonly used formatting special characters:

  • “B” – Inserts a blank space.
  • “Z” – Place holder for a numeric character or space if unused.
  • “,” – inserts a comma.
  • “/” – Inserts a slash.
  • “0” – Inserts a zero.

Here are some examples to demonstrate how to use them:

Picture Clause Input Output
9999/99/99 20100101 2010/01/01
9999B99B99 20100101 2010 01 01
-ZZZ,ZZZ.99 -1234.5 -1234.50
ZZZ,ZZZ.99- -1234.5 1234.50-
X0X0X0X ABCD A0B0C0D

Finally, here's the COBOL program I used to develop the example code in this tutorial.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EDITED-FIELD.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUMERIC-FIELD PIC 999999V99.
       01 EDITED-NUMERIC-FIELD PIC ZZZ,ZZZ.99.
       01 EDITED-NEGATIVE-FIELD1 PIC -ZZZ,ZZZ.99.
       01 EDITED-NEGATIVE-FIELD2 PIC ZZZ,ZZZ.99-.
       01 EDITED-DATE-FIELD1 PIC 9999/99/99.
       01 EDITED-DATE-FIELD2 PIC 9999B99B99.
       01 EDITED-ZERO-FIELD PIC X0X0X0X.

       PROCEDURE DIVISION.
            MOVE 1234.5 TO NUMERIC-FIELD.
            DISPLAY '       NUMERIC-FIELD: ' NUMERIC-FIELD.
            
            MOVE NUMERIC-FIELD TO EDITED-NUMERIC-FIELD.
            DISPLAY 'EDITED-NUMERIC-FIELD: ' EDITED-NUMERIC-FIELD.

            MOVE -1234.5 TO EDITED-NEGATIVE-FIELD1.
            DISPLAY 'EDITED-NEGATIVE-FIELD1: ' EDITED-NEGATIVE-FIELD1.

            MOVE -1234.5 TO EDITED-NEGATIVE-FIELD2.
            DISPLAY 'EDITED-NEGATIVE-FIELD2: ' EDITED-NEGATIVE-FIELD2.

            MOVE 20100101 TO EDITED-DATE-FIELD1.
            DISPLAY 'EDITED-DATE-FIELD1: ' EDITED-DATE-FIELD1.

            MOVE 20100101 TO EDITED-DATE-FIELD2.
            DISPLAY 'EDITED-DATE-FIELD2: ' EDITED-DATE-FIELD2.
            
            MOVE 'ABCD' TO EDITED-ZERO-FIELD.
            DISPLAY 'EDITED-ZERO-FIELD: ' EDITED-ZERO-FIELD.
            STOP RUN.

Sunday, September 26, 2010

COBOL Tutorial 000200 – Data Fields

Variables are called Fields in COBOL and definitions of variables are declared in the Picture clause (can be abbreviated with PIC). Why is it called the Picture Clause? According to the book Sams Teach Yourself COBOL in 24 Hours, this is because it “paints a picture of how a field looks by defining every details and characteristic of the field”, still doesn’t quite make sense to me but anyway.

Let’s start by talking about what data fields (variables) look like in C#. When we declare a variable, the first thing we have to think about is the data type, which determines what kind of data can it hold. Normally we wouldn’t worry about the number of digits or length of the string unless we know their values can get ridiculously large or long.

int integerVariable = 12345678;
string stringVariable = "abcd1234";
decimal decimalVariable = 1234.5678m;

In a COBOL world, however, the size does matter and you have to specify both the type and the size for each data field at the same time with a special character mask in its “Picture Clause” (the PIC keyword) as shown in code below:

01 ALPHA-FIELD         PIC AAAAAAAAAA.
01 NUMERIC-FIELD       PIC 9999999999.
01 ALPHA-NUMERIC-FIELD PIC XXXXXXXXXX.

There are really only 3 types of data field in COBOL: literal, numeric and alpha-numeric and as their names suggest, they hold alphabetical, numeric and alpha-numeric characters respectively.

Let’s look at the ALPHA-FIELD first, its picture clause specifies a “AAAAAAAAAA” mask, each “A” character is a place holder for a single alphabetical character, so the ALPHA-FIELD can be used to store 10 alphabetical characters. Similarly, each “9” is a place holder for a single digit and a “X” is a place holder for a single alpha-numeric character. Therefore, the NUMERIC-FIELD and ALPHA-NUMERIC-FIELD can hold 10 digits and 10 alpha-numeric characters respectively.

To declare a decimal data field, we need to add the special character “V” in the numeric data field mask, which specifies the decimal point location. For example, the DECIMAL-NUMERIC-FIELD data field declared below let you store decimal values with up to 5 digits before and after the decimal point.

01 DECIMAL-NUMERIC-FIELD PIC 99999V99999.

As you have probably noticed, the format mask can get very ugly for large fields and hence COBOL allows you to abbreviate it with the special character followed by the number of appearances in round brackets, so for example we can abbreviate “XXXXXXXXXX” to X(10) and “99999V99999” to 9(5)V9(5).

Let’s put these all together into a small program:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EDITED.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 ALPHA-FIELD PIC A(10).
       01 NUMERIC-FIELD PIC 9(10).
       01 ALPHA-NUMERIC-FIELD PIC X(10).
       01 DECIMAL-NUMERIC-FIELD PIC 9(5)V9(5).

       PROCEDURE DIVISION.
            MOVE 'ABCEFG' TO ALPHA-FIELD
            DISPLAY '         ALPHA-FIELD: ' ALPHA-FIELD.

            MOVE 123456 TO NUMERIC-FIELD.
            DISPLAY '       NUMERIC-FIELD: ' NUMERIC-FIELD.

            MOVE 'ABC123' TO ALPHA-NUMERIC-FIELD
            DISPLAY ' ALPHA-NUMERIC-FIELD: ' ALPHA-NUMERIC-FIELD.


            MOVE 1234.5 TO DECIMAL-NUMERIC-FIELD.
            DISPLAY 'EDITED-NUMERIC-FIELD: ' DECIMAL-NUMERIC-FIELD.
            
            STOP RUN.

Notice that I’ve used the abbreviated picture clause mask (e.g. X(10) instead of XXXXXXXXXX) in the example above. Also, we haven’t covered the MOVE command yet but basically that’s how you move (assign) values into data fields in COBOL. If you compile and run this program you’ll see the following outputs in the console:

         ALPHA-FIELD: ABCEFG    
       NUMERIC-FIELD: 0000123456
 ALPHA-NUMERIC-FIELD: ABC123    
EDITED-NUMERIC-FIELD: 01234.50000

As you have probably noticed, unused digits in numeric fields are padded with zeros, which is quite ugly. We’ll cover how to make it look prettier with Edited Fields in the next tutorial.

Update: This is the 3rd revision of this COBOL data fields tutorial, I’ve decided to cut this tutorial into two parts because it was getting too long and messy.