I usually use postscript terminal with eps output for papers. My best setting up to now is to use "set term postscript eps enhanced". You can also easily generate your eps file by using "set output" command. If you use pdflatex for your paper, you will need pdf files instead of eps files. In this case, you might want to run
epstopdf <your_eps_file>
The above command outputs a pdf file from your eps file.

Here, I introduce my gnuplot template file, which can be useful for your paper writing.
(* You must change most of the properties by hand. *)
  1. ## GNUPLOT script by sangkilc
  2. ## File Name
  3. fname = "yourfilename"
  4.  
  5. ## start setting values
  6. set term postscript eps enhanced monochrome font "Helvetica" 24  # set terminal
  7. set output fname . ".eps" # set output file
  8.  
  9. ## Margins
  10. set bmargin 1
  11. set tmargin 1
  12. set rmargin 1
  13. set lmargin 1
  14.  
  15. ## Border
  16. set border 15     # bottom 1, left 2, top 4, right 8 (e.g. bottom + left = 3)
  17.  
  18. ## Tics
  19. set xtics nomirror                    # no 2nd axis
  20. set ytics nomirror                    # no 2nd axis , tic interval is
  21. #set x2tics 0, 5
  22. #set y2tics 0, 5
  23.  
  24. ## Ranges
  25. set xrange [0:256]
  26. #set yrange [1:200]                    # set y-axis range
  27. #set log y                                   # set log scale
  28.  
  29. ## Labels
  30. #set xlabel "xlabel"
  31. set ylabel "ylabel" offset 1
  32. #set x2label "x2label"
  33. #set y2label "y2label"
  34.  
  35. ## Legend
  36. set key left top                      # set legend
  37.  
  38. ## Plot style
  39. ## This is only for the histogram style plotting
  40. set style data histogram              # Plot histogram
  41. set style histogram clustered gap 1   # clustered histogram with gap 1
  42. set style fill border 0               # histogram filling pattern and border
  43. set boxwidth 0.9                      # histogram box width
  44.  
  45. set pointsize 2                       # plot point size
  46.  
  47. ## Format
  48. #set format x "%2X"
  49.  
  50. ## Plot
  51. ## This part must be modified for each plot.
  52. ## Also, there must be a .dat file, which contains all the data for the plot.
  53. #plot fname . ".dat" \
  54. #       using 2:xticlabels(1) title "A" lw 2 lt 1 fs solid 0.1,\
  55. #    "" using 3 title "B" lw 2 lt 1 fs solid 0.3,\
  56. #    "" using 4 title "C" lw 2 lt 1 fs solid 0.6,\
  57. #    "" using 5 title "D" lw 2 lt 1 fs solid 0.9
2010/08/15 23:15 2010/08/15 23:15
Posted by 차상길.

SED 메뉴얼의 2탄!

5.4 SED의 커맨드

앞에서는 (안내서 1편은 여기 클릭) SED의 문법 중에 p, s 와 같은 커맨드를 알아 보았는데요, 그 외에는 d (delete), q (quit) 등의 커맨드가 더 존재합니다. 예제를 보면서 SED의 커맨드를 정리해 보겠습니다.
sed -n '1,10 p' input.txt
p 커맨드는 print (출력) 입니다. 즉, 위의 예제는 1번부터 10번째 라인까지만 프린트 하기입니다. (-n 옵션 때문에 매칭이 되는 1~10 라인 외에는 출력되지 않는다.) 만약 sed '1,10 p' 라고만 한다면, 1~10번째 라인이 출력된 후에 다시 1번부터 끝까지의 모든 라인이 출력됩니다.
sed -n '11,$ !p' input.txt
커맨드 앞에 ! (느낌표)를 붙여서 반대의 기능을 수행하는 경우가 있습니다. 즉 여기서, !p 는 매칭되는 라인에 대해서만 프린트를 하지 않는다 라는 뜻 입니다. 따라서 11번째 줄부터 끝까지에 대해서는 출력하지 않는 것이 되고, 출력하지 않는 다라는 전체 커맨드에 대해 -n 옵션이 붙어서 결국은 1~10번째 줄만 출력하라는 위의 예제와 동일한 결과를 나타내게 됩니다.
sed '11,$ d' input.txt
d 커맨드는 delete (삭제) 입니다. 즉, 위의 예제에서는 11번째 줄부터 끝까지에 대해서 삭제를 한 결과를 출력합니다. 다시 말해 1~10번 줄만을 출력하는 것으로 결국은 앞에서의 예제와 같은 기능입니다.
sed '1,10 !d' input.txt
역시 느낌표를 붙여서 1~10번 줄에 대해 삭제를 하지 않는다라는 것에 대한 결과로, 위의 예제 역시 1~10번 줄만을 출력하는 명령어 입니다. 차이점은 -n 옵션이 없더라도 삭제하지 않는 것에 대한 매칭인 1~10번 줄만 출력한다는 것이죠. 말이 좀 어렵긴 한데, 예를 들면 sed -n '1,10 !d' 는 아무것도 출력하지 않습니다.
sed -e '10,$ p' -e '5 q' input.txt
q는 quit (종료) 입니다. 즉, 간단하게 매칭되는 부분 까지 수행하고 매칭을 종료할 때 사용합니다. 위의 예제는 10번부터 끝까지 라인을 출력한 후에 그 결과에 대해서 앞의 5개의 라인만을 출력하는 예제입니다. '5 q'는 총 5개의 라인을 처리하고 종료하라는 뜻 입니다.
sed '10 r blah' input.txt
r은 read (읽기) 입니다. r 다음에 오는 문자열이 파일명을 나타냅니다. 즉 r blah는 blah 라는 파일을 읽으라는 뜻이고, 위에 예제에서는 input.txt 를 읽어서 출력을 하다가 10번째 라인에 도달하면 blah라는 파일을 읽어서 결과를 출력하게 됩니다. 즉, 최종 결과는 input.txt의 1~10번 라인이 나오고, blah 파일의 내용이 나온 뒤 다시 input.txt의 11번 부터 끝까지의 내용이 출력됩니다.
sed '1,5 w out.txt' input.txt
w는 write (쓰기) 입니다. r과 비슷하게 파일명이 커맨드 다음에 나오고, 매칭된 결과값을 파일에 쓸 수 있습니다. 위의 예제는 input.txt 의 1~5번 라인을 out.txt 에 저장하는 예제 입니다.
sed '1,5 a hello' input.txt


a는 append (덧붙이기) 입니다. a 커맨드 다음의 문자열을 매칭된 패턴 다음에 붙이는 역할을 합니다. 위의 예제에서는 1~5번 라인에 대해 hello라는 문자열 줄을 삽입하는 예제 입니다.
sed '1,5 i hello' input.txt

a가 패턴 다음에 붙이는 역할이라면, i는 패턴 이전에 삽입하는 역할입니다 (insert). 즉 위의 예제는 1~5번 줄에 대하여 hello라는 문자열 줄을 input.txt의 1~5번 각각의 줄의 이전에 삽입하는 역할 입니다.
sed '/abc/ c hello' input.txt

c는 줄 전체를 바꾸는 (change) 커맨드 입니다. 즉 매칭된 패턴이 있는 줄을 c 다음에 오는 문자열로 치환하는 것이죠. 위의 예제에서는 abc라는 패턴이 존재하는 줄에 대하여 그 줄을 hello라는 문자열로 치환합니다.

이밖에 s는 치환 (substitute)을 나타내며, 튜토리얼 1탄에서 자세히 다루었으므로 생략합니다.

5.5 그룹 지정

그룹 지정은 중괄호 {} 를 통해서 합니다. 여러개의 커맨드를 중괄호 사이에 묶어 한방에 처리할 수 있는 강력한 기능입니다. 단 주의할 점은 각 커맨드는 각각 새로운 줄에 위치해야만 한다는 것이죠. 다음의 예제는 1~5번 줄을 읽어서 abc패턴을 삭제하고 빈줄을 삭제한 뒤 해당 라인을 출력하는 예제입니다.
sed -n '1,5 {
    s/abc//
    /^$/ d
    p
    }' input.txt
6. 마치며

sed 문법은 자주 안쓰면 금방 또 잊게 되어서, 이렇게 정리해 보았습니다. 다음엔 awk를 정리해 봐야겠습니다.
2010/06/21 18:40 2010/06/21 18:40
Posted by 차상길.
TAGS ,

I have a doubt about the usefulness of this trick in Makefile, but I will just put it here.

If there is a minus sign at the beginning of the command in Makefile, we will ignore the status code from the command.

Here is a good example.

all: blah
rebuild: clean all

blah: blah.c blah.h

.PHONY: clean all rebuild
clean:
          -rm *.o blah

So what is the minus sign in this case??
Apparently, "make clean" will execute "rm *.o blah" without the minus sign. Right?
But, when we do "make rebuild" the result of the execution of "clean" will not affect others. That is, when we do not have files to remove (in this case, *.o and blah), the rm command will return 1 instead of 0. However, we still want to run the target "all". Thus, if we put a minus sign in front of the command, rebuild will always work whether there is a file to clean or not.

Well, we can use "rm -f" anyway in the above case, and I prefer to use -f option. Is there another useful example of using this trick?

I recently found a real good usage of this trick.
Use the minus sign in front of the ctags !!! :)
2010/02/20 20:54 2010/02/20 20:54
Posted by 차상길.
TAGS

SED (stream editor) 에 대해서 간단한 튜토리얼을 만들고자 합니다.

0. 이 안내서의 대상
나 자신, 그리고 리눅스, 정규식에 대해서 약간의 지식은 있는 분들!

1. SED란?

리눅스에서 화면에 출력되는 문자열이나, 파일 입출력은 모두 스트림으로 이루어지는데요, 그때의 스트림을 파이프로 입력받아 실시간으로 수정하기 위한 툴이 바로 sed 입니다. 예를 들면, "1,2,3,4,5" 라는 문자열이 들어있는 파일 foo.txt 를 생각해 봅시다. 여기서 cat foo.txt 를 하면 문자열이 그대로 출력되지요. 그런데 여기서 원본 파일은 그대로 둔 상태에서 출력되는 문자열을 "1:2:3:4:5" 바꾸고 싶다면 어떻게 해야할까요? 이럴 때 바로 sed를 쓰게 됩니다.

요즘에는 강력한 스크립트 언어 (ruby, perl, python) 들 때문에 사용을 점점 하지 않고 있지만, 간단한 작업에는 아직도 매우 유용한 툴 입니다. 특히 텍스트 데이터를 가지고 간단한 처리를 할 때 스크립트를 짜서 돌리는 것보다는 한 줄로 된 커맨드 라인으로 해결하는게 훨씬 빠르지요. :D

2. 기본 명령어
sed "sed command" input.txt
위의 명령어는 stdout으로 input.txt 파일을 처리한 결과를 뿌려줍니다.
다음의 두 가지 방법은 형태는 다르지만 동일한 기능을 하는 커맨드 입니다.
sed "sed command" < input.txt
cat input.txt | sed "sed command"
<노트> 이렇게 기본 명령어만 사용하게 되면 BRE라 불리는 Baisc Regular Expression 만을 지원하게 됩니다. Modern Regular Expression (혹은 Extended Regular Expression) 에 익숙한 사람들은 "-E" 옵션을 붙여주면 더욱 편하게 사용할 수 있습니다. 즉, sed -E "regex" <inputfile> 과 같이 사용하면 됩니다.

3. 치환 (substitution)

가장 많이 쓰는 명령어가 바로 치환 입니다. 정규표현식과 동일한 문법을 가지고 있습니다.
e.g.) ABC 를 DEF로 치환
sed /ABC/DEF/s input.txt
여기서 슬래쉬(/) 기호는 delimiter 를 나타내고, s는 치환 명령을 나타냅니다.
만약 delimiter를 바꾸고 싶다면 커맨드 s를 앞으로 빼내는 방법이 있습니다. 즉, 콜론(:)을 delimiter로 바꾸는 경우:
sed "s:ABC:DEF:" input.txt
와 같이 나타낼 수 있습니다. 이렇게 명령어 형태가 다른 이유는 다음과 같습니다. 즉, 슬래쉬가 delimiter일 경우에 패턴에 슬래쉬를 나타내기 위해서는 \/ 와 같이 나타내야 하지만, delimiter를 바꾸게 되면 백슬래쉬 인코딩을 하지 않고도 처리가 가능하기 때문이죠.

4. 삽입

찾은 패턴에 문자를 더하고자 하는 경우에 & 기호를 쓰게 됩니다. 즉 a 또는 b를 찾아서 (a) 또는 (b)로 만들고 싶다고 치면 다음과 같이 하면 됩니다.
sed "s/[ab]/\(&\)/" input.txt
때로는 \1, \2 와 같은 매칭된 부분을 따로 지정할 수 도 있습니다. 이 때 주의할 점은, 항상 백슬래쉬를 괄호 앞에 붙여주어야만 정상 작동한다는 사실이지요. 예를 들어, "영어-숫자" 쌍이 있을 때 "숫자-영어"로 바꿔주고 싶다면 어떻게 할까요? 입력 데이터가 다음과 같이 주어졌다고 합시다.
abc 123
blah 447
zdf 987
위의 입력으로부터 우리가 원하는 출력값을 얻기 위해서는 다음과 같이 합니다.
 sed "s/\([a-z][a-z]*\) \([0-9][0-9]*\)/\2 \1/" input.txt
5. 고급 기능들
위에서는 기본적인 Regular Expression과는 다를 게 없었습니다. 이제부터 SED만의 강력한 기능들에 대해서 하나씩 알아봅시다.

5.1. 매칭된 라인만 프린트 하기

먼저 "-n" 옵션에 대해서 알아보겠습니다.
이 옵션은 출력을 하지 말라는 (no print) 의미를 가지고 있습니다. 그런데 만약 우리가 매칭 정규식의 마지막 부분에 p옵션을 주게 되면 얘기가 달라집니다. 즉, 매칭된 라인만을 출력하라는 뜻이 되죠. 예를 들면 다음과 같습니다.
sed -n "s/pattern/&/p" input.txt
위의 명령은 우리가 흔히 쓰는 grep 과 동일한 명령이라고 볼 수 있습니다. 왜냐하면 &는 매칭된 패턴을 그대로 리턴하고, 마지막에 쓰인 p 때문에 매칭된 라인만 출력이 되고 매칭되지 않은 라인은 -n 옵션에 의해 출력이 되지 않기 때문이죠.

그렇다면 반대의 경우는 어떻게 할까요? 매칭되지 않은 라인만 프린트 하기:
sed -n "s/pattern/&/!p" input.txt


5.2. 여러개의 Regular Expression 동시에 연동시키기

여러개의 규칙을 동시에 사용하는 것도 가능합니다. 이 때는 파이프를 통해 sed 자체를 두번 쓰는 방법도 있지만 한방에 해결하는 방법을 소개하도록 하겠습니다. 즉, "-e"옵션을 쓰는 방법인데요. 다음의 예제를 보면,
sed -e "s/a/A/" -e "s/b/B" input.txt
-e 옵션 뒤에 규칙을 하나씩 써서 두 개의 정규식을 동시에 사용한 것을 볼 수 있습니다. 위의 명령은 소문자 a를 대문자로, 또한 b를 대문자로 바꾸어 출력을 합니다.

5.3. 영역 지정하기

sed가 유용한 가장 큰 이유 중의 하나인 영역 지정하기 입니다. 지금까지 배운 명령은 치환, 삭제 등에 불과했지만, 실제로는 이것을 특정 라인, 텍스트상의 특정 영역에만 지정해서 실행하고 싶은 경우가 더 많다는 것이죠. 그래서 이 섹션에서는 영역 지정을 하는 법을 정리해 보았습니다.

(1) 라인 지정
a를 A로 치환할 때 이것을 입력 상의 10번째 라인에 적용하고자 할 때: 가장 앞에 라인 번호를 붙여줍니다.
sed "3 s/a/A/" input.txt
100번에서 200번 라인만 a를 A로 치환하고자 할 때:
sed "100,200 s/a/A/" input.txt
100번라인부터 마지막 라인까지 a를 A로 치환하고자 할 때:
sed "100,$ s/a/A/" input.txt
(2) 패턴에 대한 영역 지정
#으로 시작하는 문자열에 대해서만 a를 A로 치환하고자 할 때:
sed "/^#/ s/a/A/" input.txt
X라는 패턴부터 Y라는 패턴 사이에서 #으로 시작하는 모든 라인을 삭제:
sed "/X/,/Y/ s/^#.*//" input.txt
위의 예제는 아주 유용할 때가 많은데요, X가 나타난 이후부터 치환 명령이 적용되다가... 다시 Y가 나타난 이후로는 치환이 중단되는 것이라고 생각하면 됩니다.

5.4 그룹 지정, 리드, 라이트 에 대해서는 다음에...
2010/02/10 18:18 2010/02/10 18:18
Posted by 차상길.
TAGS ,

I have started to use nasm (Netwide Assembler) for about a month. NASM has several nice features that GNU assembler does not have. One of the most compelling features should be the automatic synchronization of mixed code (16bit and 32bit codes). For instance, when we want to jump to 32 bit address in 16 bit mode (e.g. in bootstrap), NASM will automatically attach the prefix (0x66) to the instruction.

In GAS (GNU as) we use as follows:
.byte 0x66, 0xea
.long 0x1000
.word __KERNEL_CS


In NASM we can do:
jmp DWORD 0x100000:__KERNEL_CS
2010/01/21 21:41 2010/01/21 21:41
Posted by 차상길.
TAGS ,

Here, I present an example for Hangul (Korean).

First, run this command. :D

sudo aptitude install scim scim-hangul scim-tables-ko

You can easily do this for other languages by changing from hangul to other lnguages. For instance, you can install scim-chinese scim-tables-zh, to use Chinese.

Second, put the following three lines in $HOME/.xinitrc or $HOME/.xsession.

export XMODIFIERS=@im=SCIM
export GTK_IM_MODULE="scim"
export QT_IM_MODULE="scim"
2010/01/19 21:20 2010/01/19 21:20
Posted by 차상길.
TAGS , ,

In genral, the objdump only supports x86, or your machine's native architecture. The easiest way to objdump different architecture's binary is to use binutils.

Binutils contain many popular programs including readelf, objcopy, and objdump. The only thing you need to do is enable different targets when you compile the binutils. Currently, latest stable version of binutils is 2.20.

So, how do we actually enable various targets? The answer is "--enable-targets" option. The simplest way is "--enable-targets=all", which will compile all possible architectures including ARM, MIPS, PowerPC, and so forth. However, this will generate more than 1 GB of object files inside your source code directory!

So, choose your favorite architectures, and use comma to separate them.

For example, if you want to enable ARM and MIPS for your objdump, use the following command.
./configure --enable-targets=arm-elf,mips

After make, and make install, you will be able to see a different message (objdump --help) from your objdump. Your objdump will show several possible targets for ARM and MIPS.

You can also use pe-i386 target to objdump PE files in your linux box. :D
2010/01/19 21:14 2010/01/19 21:14
Posted by 차상길.
TAGS ,