Shell: No such file or directory, but the file exists

Example File:

# File Name : tools.csh

source $PROJ_DIR/scripts/getOS.csh

 

Commands run from terminal:

$> echo $SHELL

tcsh

$> pwd

project1

$> cd work

$> source ../tools.csh

/home/myname/myprojects/project1/scripts/getOS.csh: Command not found

$> ls ../scripts/getOS.sh

../scripts/getOS.sh

$> ls /home/myname/myprojects/project1/scripts/getOS.csh

/home/myname/myprojects/project1/scripts/getOS.csh: No such file or directory

From the above, it seems like, the file exists if accessed using relative path.

Same file is not accessible using absolute path.

 

Fix:

Check $PROJ_DIR path carefully starting from ‘/’

There may be typo making it to point to different path, which results in such errors from Shell.

Preprocess text with embedded perl code

Create my first project at Github:

This script can be downloaded from

https://github.com/agraja/text2perl/blob/master/text2perl.pl

#!/usr/bin/perl

# Tiny preprocessor:
# Example: Paste this example in a file
# Pass it as argument to this script
# some text /* for(1..5) { */
# this line will be printed 5 times
# /* } */
open(P,”|$^X”);
print P map {
if(/^\/\*(.*)/){
$1
}else{
s/([\\\’])/\\$1/g;
“print ‘$_’;”
}
}map{
s/\*\//\r/g;
split(/(\/\*[^\r]*\r)/)
}<>;
close(P);

Identity Monad in Perl

#!/usr/bin/perl

sub wrap_value {
my $value=shift;
return sub {
return $value;
}
}

sub bind_func {
my ($value,$func)=@_;
return $func->($value);
}

sub lift_func {
my $func=shift;
return sub {
my $value=shift;
&bind_func($value,$func);
}
}

sub prefix {
my $value=shift;
return &wrap_value(“prefix ” . $value->());
}

sub suffix {
my $value=shift;
return &wrap_value($value->() . ” suffix”);
}

print &wrap_value(‘raja’)->(),”\n”;

print &bind_func(&wrap_value(‘raja’),\&prefix)->(),”\n”;

print &lift_func(\&prefix)->(&wrap_value(‘xyz’))->(),”\n”;

my $v=&wrap_value(‘123’);
my $p=&lift_func(\&suffix)->($v);
my $s=&lift_func(\&prefix)->($p);

print $s->(),”\n”;

Preprocessor for Verilog, SystemVerilog

DownLoad PDF Version Here

Contents

  1. Introduction
  2. Operation
  3. Syntax, Keywords
  4. Command Line options
  5. Example of input file
  6. Output of vpp.pl
  7. Complete script: vpp.pl

Introduction

This is a generic Preprocessor / Macro Processor / Template Processor.

Syntax is different from m4, C preprocessor, Verilog preprocessor.

Since this is a generic text preprocessor, can be used for C, Verilog …

vpp.pl includes documentation of Syntax of input file. Jump to last section for the script.

Why to have another preprocessor for SystemVerilog?

SV is a verbose language. This preprocessor can be used to create template for reuse.

Vpp.pl has more features like for loop, can use perl data types, etc.

For example, UVM testbench skeleton can be made as a template; Use vpp.pl to generate files for agent, driver, monitor, … with uniform naming convention.

Operation

vpp.pl converts input files into perl code. Vpp.pl -p will show generated perl code.

The generated perl code is written as input to perl.

If there are no syntax errors, output is printed. If -o file option is used,

output is written to file instead of stdout.

If there are any syntax errors, error message may be about {, }, (, ), bad words, …,

Exact error in input file can’t be shown. Only location of line, file will be printed.

Syntax, Keywords

Syntax All Keywords Must start at begin of line and end with newline char

Leading spaces will be removed.

Exceptions: $get, $esc, macro calls – $user_defined_macro(…)

Keywords Meaning and Usage

$dnl Delete till newline character. Can be used to write comments

Example: This a line^ $dnl Read all comments. Leading spaces removed

This line will get joined to previous line, immediately after ‘^’

$macro Start of macro definition. Example:

$macro name $dnl Use ‘name’ for definition, Use $name for macro call

arg1 is $get($_[0])

arg2 is $get($_[1])

$end

Macro call: $name(‘arg1′,’arg2’)

$for Example:

$for my $i (1..10)

Iterator is $get($i)

$end

$set Perl Statement that ends with a ‘;’

Example:

$set $x = 5 $dnl Omit ending ‘;’

Value of $x is $get($x)

$begin Use this for perl statements that have ‘{‘

Example:

$begin if($_[0])

$begin while(1)

infinite loop

$end

$end

$begin elsif($_[1])

$begin if($_[2])

true part

$end

$begin else

false part

$end

$end

$end $macro, $for, $begin must end with matching $end

$esc Used to escape characters: ‘$’, ‘(‘, ‘)’

Example:

Use $name(‘$esc(left paran’) in place of $name(‘(left paran’)

Escape keywords: $esc$macro becomes $macro

$get Used to insert value of variables in plain text. See above examples

Can’t be used in lines with $macro, $for, $begin, $set, $end

Example: $get(‘string’) For ‘(‘, ‘)’ within $get, $esc must be used

Same rules of $get apply for user-defined-macro calls

Command line options

USAGE ./vpp.pl files [-o file] [-p] [-s] [-h]

-o Write/Over-Write to file

-p Show Generated Perl Code

-s Show Syntax for input file

-h Show Command line options

Example of input file

******* input.v begins after this line *******

1 abcdefghijklmnopqrstuvwxyz

$macro func

2 ab$get($_[0])cdefg$get (“$esc)”)hijklmnopqrstuvwxyz

$end

3 abcdefghi<<$esc$display>>jklmnopqrstuvwxyz

4 abcdefghijklmnopqrstuvwxyz

5 abcdefg<<$esc($esc)>>hijklmnopqrstuvwxyz

6 abcdefghijklmnopqrstuvwxyz

7 abcdefgh$esc$$$ijklmnopqrs$dnltuvwxyz

8 abcdefghijklmnopqrstuvwxy$esc$escz

9 abcdefghijklmnopqrstuvwxyz

$func(‘FIRST_ARGUMENT’)

$for(1..10) $dnl

$set $x = “argument”

$get($x) is $get($_)

$end

Printing all keyboard characters

a b c d e f g h i j k l m n o p q r s t u v w x y z

A B C D E F G H I J K L M N O P Q R S T U V W Z Y Z

1 2 3 4 5 6 7 8 9 0

! @ # $ % ^ & * ( )

` ~ – _ = +

[ { ] } \ |

; : ‘ ”

, < . > / ?

Output of vpp.pl

1 abcdefghijklmnopqrstuvwxyz

3 abcdefghi<<$display>>jklmnopqrstuvwxyz

4 abcdefghijklmnopqrstuvwxyz

5 abcdefg<<()>>hijklmnopqrstuvwxyz

6 abcdefghijklmnopqrstuvwxyz

7 abcdefgh$$$ijklmnopqrs8 abcdefghijklmnopqrstuvwxy$escz

9 abcdefghijklmnopqrstuvwxyz

2 abFIRST_ARGUMENTcdefg)hijklmnopqrstuvwxyz

argument is 1

argument is 2

argument is 3

argument is 4

argument is 5

argument is 6

argument is 7

argument is 8

argument is 9

argument is 10

Printing all keyboard characters

a b c d e f g h i j k l m n o p q r s t u v w x y z

A B C D E F G H I J K L M N O P Q R S T U V W Z Y Z

1 2 3 4 5 6 7 8 9 0

! @ # $ % ^ & * ( )

` ~ – _ = +

[ { ] } \ |

; : ‘ “

, < . > / ?

Complete Script: vpp.pl

******* vpp.pl begins after this line *******

#!/usr/bin/perl

# Author : A.G.Raja

# Description : m4 < This Preprocessor <= perl

############## PART1 : Parse Arguments #########################

my $outf = “STDOUT”; my $args = “”; my @infiles; my $genpl=0; my @cmd_out;

sub help { print STDERR “\n$_[0]\n\nUSAGE\t$0 files [-o file] [-p] [-s] [-h]\n\n”;

print STDERR “\t-o\tWrite/Over-Write to file\n”;

print STDERR “\t-p\tShow Generated Perl Code\n”;

print STDERR “\t-s\tShow Syntax for input file\n”;

print STDERR “\t-h\tShow Command line options\n”;

exit; }

sub parse_arguments {

for(my $in=0;$in<=$#ARGV;$in++) {

if($ARGV[$in] =~ /^\-o/) { $in++; $outf=$ARGV[$in]; }

elsif($ARGV[$in] =~ /^\-h/) { &help(“$0 converts input->perl_code->output”); }

elsif($ARGV[$in] =~ /^\-s/) { while(<DATA>) {print;} &help(“Command line options”); }

elsif($ARGV[$in] =~ /^\-p/) { $genpl=1; }

elsif($ARGV[$in] =~ /^\-/) { &help(“ERROR\tInvalid option ‘$ARGV[$in]’\n”); }

else { $args .= “$ARGV[$in] “; push(@infiles,$ARGV[$in]); } }

if($args !~ /\w+/) { &help(“ERROR\tNo input files specified\n”); }

if($outf =~ />\s*$/) { &help(“ERROR\tNo output file specified\n”); } }

############## PART2 : Text2Perl Conversion ####################

my $tt = “TEXT”; my $ee = “ESCAPE”; my $bt=”BEGIN$tt”; my $et=”END$tt”;

my $esn = $ee.”N”; my $esl = $ee.”L”; my $esr = $ee.”R”; my $esd = $ee.”D”;

my %hash; $hash{“get”}=”print”;

sub define_macro { my $n = shift; $n=~ s/\s//g; $hash{$n}=$n; }

sub macro_call {

my $out=””; my $func=””; my @l;

@l = split(/(\$\w+\s*\(|\))/,shift);

for(my $i=0;$i<=$#l;$i++) {

if($l[$i]=~/\$(\w+)/) {

$func=$hash{$1};

if($func ne “”) {

$out .= “$et $func(” ; $i++;

$out .= $l[$i] ; $i++;

$out .= “$l[$i]; $bt”; next; } }

$out .= $l[$i]; }

return $out; }

sub print_text {

my $in = shift;

if($in eq “”) { return “”; }

$in =~ s/\\/\\\\/g;

$in =~ s/\’/\\\’/g;

return “print ‘$in’;”; }

sub perl_code {

my ($prefix,$middle,$suffix,) = @_; my $ret;

$ret = “$et $prefix”;

if($middle =~ /(.*)$et($esn$bt)/) {

$ret .= “$1 $suffix$2”;

if($prefix eq “sub”) { &define_macro($1); } }

else {

if($prefix eq “sub”) { &define_macro($middle); }

$ret .= “$middle $suffix\n$bt”; }

return $ret; }

sub parse {

my $l = shift;

#$l=~s/\r//; # Uncomment This Line to run on Windows OS

$l=~s/[\$]esc\$/$esd/g;

$l=~s/[\$]esc\(/$esl/g;

$l=~s/[\$]esc\)/$esr/g;

$l=~s/[\$]dnl.*\n/$et$esn$bt/;

if($l=~/^\s*[\$]macro(.*)/) { $l = &perl_code(“sub”,$1,”{“); }

elsif($l=~/^\s*[\$]for(.*)/) { $l = &perl_code(“for”,$1,”{“); }

elsif($l=~/^\s*[\$]begin(.*)/) { $l = &perl_code(“” ,$1,”{“); }

elsif($l=~/^\s*[\$]set(.*)/) { $l = &perl_code(“” ,$1,”;”); }

elsif($l=~/^\s*[\$]end.*/) { $l = &perl_code(“}” ,””,”” ); }

else { $l = &macro_call($l); }

$l=~s/$esl/(/g;

$l=~s/$esr/)/g;

$l=~s/$esd/\$/g;

$l=~s/$esn/\n/g;

return $l; }

sub text2perl {

my $Text = shift;

my $out=””; my $pl=0; my @l;

@l = split(/($bt|$et|\n)/,$$Text);

for(@l) {

if($_ eq “$bt”) { $pl=0; }

elsif($_ eq “$et”) { $pl=1; }

elsif($_ eq “\n”) { $out .= $pl ? “\n” : “print \”\\n\”;\n”; }

else { $out .= $pl ? $_ : &print_text($_); } }

print “$out print \”Text2Perl Completed\”;”; }

############## PART3 : VPP – Generate Output ###################

sub vpp {

@cmd_out = `$0 -p $args | perl 2>&1`;

if($cmd_out[$#cmd_out]=~/Text2Perl Completed$/) {

$cmd_out[$#cmd_out]=~s/Text2Perl Completed$//;

if($outf eq “STDOUT”) { for(@cmd_out) {print;} exit; }

open(FILE,”>$outf”) or &help(“Can’t Write file ‘$outf'”);

for(@cmd_out) {print FILE;}

close(FILE);

return 1; }

return 0; }

my @b; my @e;

sub file_name {

my $ln = shift; my $act;

for(my $i=0;$i<=$#b;$i++) {

if( ($ln>$b[$i]) && ($ln<=$e[$i]+1) ) {

$act = $ln-$b[$i];

return “$act in ‘$infiles[$i]'”; } }

return “$ln”; }

sub report_errors {

my $line; my $curr=0;

for my $inf (@infiles) {

push(@b,$curr);

open(TEMP,”<$inf”) or last;

$line=0; while(<TEMP>) {$line++;}

close(TEMP);

$curr+=$line;

push(@e,$curr); }

for(@cmd_out) {

if(/\-\s*line\s*(\d+)/) {

$actual = &file_name($1);

s/(\-\s*line\s*)\d+/$1$actual/; }

print $_; } }

############## PART4 : Select Text2Perl / VPP ##################

&parse_arguments();

if(!$genpl) {

if(!&vpp()) { &report_errors(); }

exit; }

my $text=””;

for my $inf (@infiles) {

open(TEMP,”<$inf”) or last;

while(<TEMP>) { $text.=&parse($_); }

close(TEMP); }

&text2perl(\$text);

__DATA__

Syntax, Keywords, Usage Examples

Syntax All Keywords Must start at begin of line and end with newline char

Leading spaces will be removed.

Exceptions: $get, $esc, macro calls – $user_defined_macro(…)

Keywords Meaning and Usage

$dnl Delete till newline character. Can be used to write comments

Example: This a line^ $dnl Read all comments. Leading spaces removed

This line will get joined to previous line, immediately after ‘^’

$macro Start of macro definition. Example:

$macro name $dnl Use ‘name’ for definition, Use $name for macro call

arg1 is $get($_[0])

arg2 is $get($_[1])

$end

Macro call: $name(‘arg1′,’arg2’)

$for Example:

$for my $i (1..10)

Iterator is $get($i)

$end

$set Perl Statement that ends with a ‘;’

Example:

$set $x = 5 $dnl Omit ending ‘;’

Value of $x is $get($x)

$begin Use this for perl statements that have ‘{‘

Example:

$begin if($_[0])

$begin while(1)

infinite loop

$end

$end

$begin elsif($_[1])

$begin if($_[2])

true part

$end

$begin else

false part

$end

$end

$end $macro, $for, $begin must end with matching $end

$esc Used to escape characters: ‘$’, ‘(‘, ‘)’

Example:

Use $name(‘$esc(left paran’) in place of $name(‘(left paran’)

Escape keywords: $esc$macro becomes $macro

$get Used to insert value of variables in plain text. See above examples

Can’t be used in lines with $macro, $for, $begin, $set, $end

Example: $get(‘string’) For ‘(‘, ‘)’ within $get, $esc must be used

Same rules of $get apply for user-defined-macro calls

Laptop plugged in, not charging

Laptop plugged in, but not charging:

This was a really annoying issue, since I had bought the laptop just 3 months back.

I searched in HP forums, (mine is a HP laptop), there were a range of suggestions

1) Uninstall Battery adapter from Device manager

2) Restart !!

3) Re-install the power manager software

4) Replace the battery

5) Contact the manufacturer

Mine has AMD processor, but the laptop was very hot; something very unusual.

Silly reason: the laptop was placed on a soft couch; there was absolutely no ventilation.

I don’t see a connection between ventilation to battery charging; Anyway, the below worked.

Solution: Lifted the laptop so that the air vents on the bottom, sides are not blocked. It started charging !!

OOP Design Patterns Summary

Summary of OOPs Design Patterns from GoF Book

Note: Syntax, keywords of C++ used in example snippets.

Download: DesignPatterns_Summary

Creational Patterns

Singleton

Object is created only once using a static Create() method; new is not called directly.

First call of Create() method creates a static object using new.

Subsequent calls to the Create() method returns the pointer to static object already created.

Factory Method / Virtual Constructor

Objects are created using Create method instead of calling new directly.

BaseClass: abc

DerivedClasses: A,B,C

Factory:

abc CreateChar(char id) {

switch(id) {

case ‘a’: return new A();

case ‘b’: return new B();

case ‘c’: return new C();

}

}

Abstract Factory

BaseClass has CreateChar, CreateNum virtual functions

Factory1:

abc CreateChar() {

return A;

}

num CreateNum() {

return N1;

}

Factory2:

abc CreateChar() {

return B;

}

num CreateNum() {

return N2;

}

Builder

It takes many steps to create a concrete object.

Eg. Creating a tree, linked list, etc.

Prototype

Clone() method is used to copy self

Concrete objects are cloned from prototype and can be cloned again.

Inheritance: Derived class object can access base class variables, functions

Prototyping: Cloned object can access protype class variables only.

Structural Patterns

Adapter

Convert interface of a class as clients expect.

Make systems work together after they are designed.

Bridge

Similar to adapter but the interface is known in advance.

class systemA {

systemB *s;

void set(systemB *ss) { s = ss; }

void exec() { s->run(); }

}

Composite

Creating a hierarchical tree

Every node can create its own child(branch/leaf).

Each branch/leaf can be of a different type.

Decorator / Wrapper

Similar to Adapter. But interface doesn’t change. Wrapper can do additional operations.

class wrapper {

original o;

void func() {

decorate();

o.func();

}

}

Façade

Provide a higher level interface to combine many complex interfaces.

Eg. Compile() =  Scan() + Parse() + ByteCode() + Interpret()

Proxy / Surrogate

Smaller object acts on behalf of a larger object.

Eg. Document with an image. Proxy object can have only path to image file, pointer to ImageDisplay object. ImageDisplay object need not created unless Document.display() is called.

Flyweight

Use sharing to improve efficiency.

Example:

Not-Efficient:

class character {

char c;

int row, col;

}

Instead of storing a character object for every position,

Store position of every character in the alphabet.

Efficient:

class position {  int row, col; }

class character {

char c;

position p_array[];

}

 

Behavioral Patterns

 

Chain of Responsibility

Request is passed along a chain until an object finally handles it.

Split complex tasks into simpler tasks and make the system modular.

Eg. BuildHouse() calls BuildRooms();

BuildRooms() calls BuildFloors(), BuildWalls();

BuildWalls() calls BuildDoors(), BuildWindows().

Command

Encapsulate a request as an object. Each request type can have a different command object.

Iterator / Cursor

Provide a way to access elements without exposing underlying implementation.

Example:

int array[10]; // Every element can be accessed as array[i];

Iterator implementation:

class iterator {

int array[10];

int i=0;

int get() { return array[i]; i++; }

int end() { if(i<10) return 0; else return 1; }

}

Interpreter

Create a tree and execute each node recursively starting from root.

Mediator

Defines an object that encapsulates how a set objects interact. Objects need not interact by referring the other object explicitly. In Bridge pattern, two systems know the interface of the other. In Mediator pattern each system knows its interface to Mediator object.

Memento / Token

Store internal state of an object to restore later.

Eg. Moving a cursor on screen needs the position fields to be changed without changing other fields.

Memento object can store the position using get_position() method of cursor.

Modify memento with new position. Again use the memento to set_position() of cursor.

Instead of accessing internal variables of cursor, get, set methods are used along with memento.

Observer / Publish-Subscribe

Every subject keeps a list of observers. Any change in the subject updates all the observers.

Eg. Same Data can be represented as table, bar-graph, pie-chart.

State

Equivalent of having a state object for every case in a switch statement.

A state object implements task in current state, returns name of next state.

Client initializes its state-variable, calls methods in every state, changes state variable after every state.

Strategy / Policy

Do similar task using different algorithms. The family of algorithms are encapsulated and made interchangeable.

Template-Method

Define skeleton of algorithm in an operation, Let sub-class define the algorithm without changing the algorithm’s structure.

Visitor

Create a parallel tree with a visitor on each node.

Eg. Main tree does execution operation of a compiler. Visitor objects corresponding to each node does type-checking / syntax-checking.

 

  Design Patterns Summary
Creational Pattern Constructs
Singleton Object only once
Factory Method Object by ID
Abstract Factory Group of Objects by ID
Builder Object in many steps
Prototype Object by cloning another
Structural Pattern Connects
Adapter Two existing systems with different interfaces
Bridge Two systems by defining an interface
Composite Using same interface to create a tree
Wrapper To Object with same interface but more functions
Façade Multiple objects by merging interfaces
Flyweight Objects by separating fields that vary lesser
Proxy Object with light-weight interface
Behavioral Pattern Encapsulates
Chain-of-responsibility Modular sub-tasks to perform a complex task
Command Requests to perform different tasks
Iterator Private Data & Implementation with simpler interface
Interpreter Different tasks assigned to objects of a tree
Mediator Interfaces to different systems
Memento Interfaces to change private state variables
Observer Interface to tasks of different observers within broadcaster
State State specific tasks and next-state logic
Strategy Family of inter-changeable algorithms
Template Steps in algorithms by defining a skeleton
Visitor Auxiliary tasks for visitors of every node of a tree