Saturday, December 27, 2014

Getopt

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;

opterr = 0;
  while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c)
{
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
  printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);

for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}

Here are some examples showing what this program prints with different combinations of arguments:

% testopt
aflag = 0, bflag = 0, cvalue = (null)

% testopt -a -b
aflag = 1, bflag = 1, cvalue = (null)

% testopt -ab
aflag = 1, bflag = 1, cvalue = (null)

% testopt -c foo
aflag = 0, bflag = 0, cvalue = foo

% testopt -cfoo
aflag = 0, bflag = 0, cvalue = foo

% testopt arg1
aflag = 0, bflag = 0, cvalue = (null)
Non-option argument arg1

% testopt -a arg1
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument arg1

% testopt -c foo arg1
aflag = 0, bflag = 0, cvalue = foo
Non-option argument arg1

% testopt -a -- -b
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument -b

% testopt -a -
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument -

An option character in this string can be followed by a colon (‘:’) to indicate that it takes a required argument. If an option character is followed by two colons (‘::’), its argument is optional.


This optarg is set by getopt to point at the value of the option argument, for those options that accept arguments.


 


Reference


[1] http://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html


[2] http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html


[3] http://en.wikipedia.org/wiki/Getopt

Wednesday, December 24, 2014

MAKEFILE built-in function

How to implement built-in function as below

Text-processing function

https://www.gnu.org/software/make/manual/html_node/Text-Functions.html#Text-Functions

$(patsubst %.c,%.o,x.c.c bar.c)

Result: ‘x.c.o bar.o’

$(sort foo bar lose)

Result:‘bar foo lose’

$(word 2, foo bar baz)

Result:‘bar’

Filename manipulate function

https://www.gnu.org/software/make/manual/html_node/File-Name-Functions.html

$(dir src/foo.c hacks)

Result:‘src/ ./’

$(notdir src/foo.c hacks)

Result:‘foo.c hacks’

$(suffix src/foo.c src-1.0/bar.c hacks)

Result:‘.c .c’

$(addprefix src/,foo bar)

Result:‘src/foo src/bar’

Friday, December 19, 2014

linux command – grep

 

 

dmesg | grep -i usb

[option]

“-i” ignore the case.

cat /var/log/dmesg | less

 

 

Reference

http://manpages.ubuntu.com/manpages/utopic/man1/dmesg.1.html

Monday, December 1, 2014

Understanding lvalues and rvalues in C and C++

lvalues stands for left-values and rvalues is right-values[2]

lvalue is an expression referring to an object

Data storage for rvalues describes in ASM.[1]

mov #1, n

we could see 1 is a contant number, not a variable. the rvalue 1 never appears as an object in the data space.

 

Reference

[1] http://ieng9.ucsd.edu/~cs30x/Lvalues%20and%20Rvalues.htm

[2] http://www.c4learn.com/c-programming/c-r-value-expression/

[3] http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c experitment

Sunday, November 30, 2014

Array initialization

Basic initialization
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
other syntax 
int myArray[10] = { [4] = 5, [2] = 5 }; 
is equivalent to
int myArray[6] = { 0, 0, 5, 0, 5, 0 };
initialize a range of elements to the same value
int myArray[10] = {[0 ... 9] = 5};
 
Elements with missing values will be initialized to 0:
int myArray[10] = { 1, 2 }; // initialize to 1,2,0,0,0...

So this will initialize all elements to 0:
int myArray[10] = { 0 }; // all elements 0(only for Zero initial)

Remember that objects with static storage duration will initialize to 0 if no initializer is specified:
static int myArray[10]; // all elements 0

In C++, an empty initialization list will also initialize every element to 0. This is not allowed with C:
int myArray[10] = {}; // all elements 0 in C++


And that "0" doesn't necessarily mean "all-bits-zero", so using the above is better and more portable than memset(). (Floating point values will be initialized to +0, pointers to null value, etc.)
Reference
[1] http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c
[2] https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
[3] http://www.lemoda.net/c/array-initialization/ (experiment)






Sunday, November 16, 2014

Inline Functions versus Macros

add my commend into the content

  1. Inline functions follow all the protocols of type safety enforced on normal functions.
    • more safe then the macro, especially, for the type defination.
    • for the type check, macro is checked by prepocessor, and inline functions is checked by compilar.
  2. Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration.
    • only difference is keyword “inline”
  3. Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.
    • more variable for arguments
  4. There is no risk if called multiple times. But there is risk in macros which can be dangerous when the argument is an expression.
  5. functions can include multiple lines of code without trailing backlashes.
  6. functions have thier own scope for variables and they can return a value.
  7. debuging

Reference

[1] http://msdn.microsoft.com/en-us/library/bf6bf4cf.aspx

[2] http://www.thegeekstuff.com/2013/04/c-macros-inline-functions/

Register Transfer Level Design with Verilog (1) [ebook]

設計程式之所以有趣不外乎是它的千變萬化,同樣的結果卻有不同的寫法。 但這些不同寫法當中也並沒有分誰對誰錯,也沒有制定標準來規範何事該用何解。 這也就是我們設計者的珍貴!! [1] Primitive Instantiations 在Verilog中最基本的邏輯...