Monday, September 19, 2016

nano syntax highlighting for javascript ES2015/ES6 and Node.js

Not able to find an up-to-date nano syntax file for javascript and node.js, thus I manually modified an old version as below:

## BL's version for ES6 or ES2015
syntax "JavaScript" "\.js$"

color brightgreen  "\<[-+]?([1-9][0-9]*|0[0-7]*|0x[0-9a-fA-F]+)([uU][lL]?|[lL][uU]?)?\>"
color brightgreen  "\<[-+]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([EePp][+-]?[0-9]+)?[fFlL]?"
color brightgreen  "\<[-+]?([0-9]+[EePp][+-]?[0-9]+)[fFlL]?"
color brightgreen  "\<(null|undefined|NaN|Infinity)\>"
color brightgreen  "\<(true|false)\>"
color cyan  "\<(break|case|catch|class|const|continue|debugger|default|delete|do|)\>"
color cyan  "\<(else|export|extends|finally|for|function|if|import|in|instanceof|new|return)\>"
color cyan  "\<(super|switch|this|throw|try|typeof|var|void|while|with|yield)\>"
color cyan  "\<(enum|implements|interface|let|package|private|protected|pubic|static)\>"
color cyan  "\<(async|await)\>"
color brightcyan "\<(global|process|console|setTimeout|clearTimeout|setInterval|clearInterval)\>"
color brightcyan "\<(module|exports|require)\>"
color brightcyan "\<(eval|uneval|isFinite|isNaN|parseFloat|parseInt|decodeURI)\>"
color brightcyan "\<(decodeURIComponent|encodeURI|encodeURIComponent)\>"
color brightcyan "\<(Array|Date|Math|Number|Object|String)\>"
color brightmagenta  "/[^*]([^/]|(\\/))*[^\\]/[gim]*"
color brightblack start="/\*" end="\*/"
color brightblack "//.*$"
color yellow ""(\\.|[^"])*"|'(\\.|[^'])*'"
color yellow start="`" end="`"

Friday, September 2, 2016

Making a Debian persistence bootable USB without separate USB drive tool

Making a read-only bootable USB drive for Debian is straightforward, but need much more effort to make persistence version if not using 3rd-party tools.  As it is not easy to make Windows XP create multiple partition of USB drive required, the work-around is to use a normal read-only bootable Debian and use that environment to do all the work.  Most helpful resources found in the web is Dirk-Jan's blog:

http://www.vleeuwen.net/2014/01/create-a-persistent-debian-live-usb-flash-drive

My steps are based on this procedure with some updates for Debian 8.5.0.
Below are the summarized steps after many on-line searching and trail-and-error attempts:
Prerequisites:

  • A PC with at least 2 free USB ports.
  •  2 USB drives: One already installed normal read-only Debian 8.5.0 (xfce).  Another one with at least 4G space for installing the persistence version.
  • The PC has the Internet access and also have ISO file image already download in internal hard disk.

Boot the PC with the read-only Debian USB drive, and then plug in the new USB for installing the persistence version.
Make sure you identify the correct device file of the new USB ready to install the persistence version (e.g. use parted).  In this case, the device is identified as /dev/sdc.

Execute bash command (to install the necessary tools):
apt-get install mbr syslinux gparted

Also need packages: p7zip-full and dosfstools but these should already installed by default.

Use graphical partition tool:
sudo gparted /dev/sdc

Right-click Delect
Select unallocated partition
Right-click New
Create as: Primary Partition
New size (MiB): 1536
File system: fat32
Label: live
Select "Add"

Right-click New
New size (MiB): <remaining size>
Create as: Primary Partition
File system: ext4
Label: persist
Select "Add"
Click the "green tick" icon
Close to exit.

sudo parted /dev/sdc
print
...
(parted) mkpart primary fat32 1 1024M
(parted) mkpart primary ext4 1024M 12G
(parted) set 1 boot on
(parted) p 
...
1 1049kB 1612MB 1611MB primary fat32 boot
2 1612MB 15.6GB 14.0GB primary ext4
(parted) quit

sudo mkdosfs -n live /dev/sdc1
sudo mkfs.ext4 -L persistence /dev/sdc2
sudo mkdir /mnt/live
sudo mount /dev/sdc1 /mnt/live
sudo mkdir /mnt/persistence
sudo mount /dev/sdc2 /mnt/persistence
cd /mnt/live
sudo 7z x /media/user/Data/temp/debian-live-8.5.0-xfce-desktop.iso
sudo nano isolinux/live.cfg
Modify lines (adding "persistence noeject"): append boot=live persistence noeject components quiet splash
Save file and exit.
sudo install-mbr /dev/sdc
sudo syslinux -i /dev/sdc1
sudo mv isolinux syslinux
sudo mv syslinux/isolinux.cfg syslinux/syslinux.cfg
sudo mv syslinux/isolinux.bin syslinux/syslinux.bin
cd /mnt/persistence
sudo nano persistence.conf
Just type in:/ union
Save file and exit.
sudo umount /mnt/live
sudo shutdown now

Remove the read-only USB drive and ready to test the newly created persistence version USB drive.

Notes: optionally append network configuration to file /live/isolinux/live.cfg (not tested): ip=eth0:192.168.1.100:255.255.255.0:192.168.1.1:217.149.196.6

Sunday, February 7, 2016

Debian 8 basic configurations

Configure static IP address:

1. Use root access the system.
2. Edit the network configuration file:

nano /etc/network/interfaces

3. Sample static IP config:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface

#changed allow-hotplug eth0
#changed iface eth0 inet dhcp

#Static IP configurations:
auto eth0
iface eth0 inet static
        address 192.168.1.200
        netmask 255.255.255.0
        network 192.168.1.0
#       broadcast 192.168.1.255
        gateway 192.168.1.100

#DNS configurations:
dns-nameservers 8.8.4.4
dns-nameservers 8.8.8.8

4. Save the file and restart network service:

service networking restart
or
/etc/init.d/networking restart


Update application package, e.g. Node.js:

1. Use root access the system or use sudo command.

2. Make sure the network is connected to the public Internet.  If needed, restart the network interface:

service networking restart

3. If curl is not installed, install it first:

apt-get install curl

4. Remove the package first:

apt-get remove nodejs

5. Get the latest source (say for v4.x):

curl -sL https://deb.nodesource.com/setup_4.x | bash -

If not using root account:

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

6. Install the package:

apt-get install nodejs

___ END ___

Thursday, November 5, 2015

Counting how many of a specific weekday in a specific month

Other than use brute force method to loop through the month to count days, it can be calculated as below (in Java Script):

Math.floor((dayInMonth - ((w - weekdayOfDay1 + 7) % 7) + 6) / 7)

Where dayInMonth is the number of days in that month, w is the day of week (e.g. Sunday) we want to count and the weekdayOfDay1 is the day of week on the first day of that month.  Notes, all "day of week" are mapped as 1 for Monday, 2 for Tuesday…, 7 for Sunday.

As the set of results for is limited, pre-calculated values can be used and simple arrays or mappings can be loaded in programs to get the result directly as an alternative method.  

The mapping table is pretty small (196 entries):

7 (possible day of week to be counted) X 7 (possible day of week on the first day of the month) X 4 (possible number of days in a month) .

e.g. table for first day of the month is Monday:


wday\nday 28 29 30 31
1 4 5 5 5
2 4 4 5 5
3 4 4 4 5
4 4 4 4 4
5 4 4 4 4
6 4 4 4 4
7 4 4 4 4

Not sure about the performance differences.

BL