public inbox for linux-omap@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC] DSPBRIDGE: Kill CAMelCaSiNg
@ 2009-09-17 14:23 Nishanth Menon
  2009-09-21 12:05 ` Felipe Contreras
  0 siblings, 1 reply; 4+ messages in thread
From: Nishanth Menon @ 2009-09-17 14:23 UTC (permalink / raw)
  To: linux-omap@vger.kernel.org

Hi All,
Ref [1] w.r.t some patchset seen on linux driver project having a 
similar condition of ton of camelcasing and [2],

Can we consider running the following script for DSPBridge code? or if 
there are some more improvements folks can think of..

-----
#!/bin/bash
# Silly little script to replace CaMELCaSInG to lower_casing
# Half a zillion thoughts later,
# why not use ctags which has c interpreter already to do
# function identification for us?
if [ ! -r 'tags'  ]; then
echo "Sorry.. no tags file!"
exit 1
fi
if [ ! -r "$1" ]; then
echo "I need a directory or a file for replacing.."
exit 2
fi
DIR_FILE=`echo "$1"|sed -e "s/\//\\\//g"`
# tags have the following notation in field 4 (tab seperated)
# e - enum value
# d - define
# f - function
# F - File itself
# g - enum type
# m - structure parameter
# p - prototype
# r - kconfig define(refered)
# s - structure type
# t - typedefined variable
# v - variable
# x - extern defined variable
# we can choose to have different rules:
# rename rule i am going to follow:
# Any define -> convert camelcase to lower_case_define->convert to caps
# Any function, enum,struct param,prototype,struct type, typedef 
var,extern def ->
# camel_case to lower_case_define
# All files will be made unix file only (no point in dos + unix mixtures..)

#**
#* @brief - changes camel casing to ones with smaller case
#*
camel_to_norm(){
echo $1|tr '\_' '+' |sed 's/\([A-Z]\)/_\l\1/g' | sed 's/^_\([a-z]\)/\1/g' |\
  tr 'A-Z' 'a-z' | sed "s/\([a-z][a-z]\+\)/-\1-/g"|tr -d '_'|\
  tr '-' '_'|sed -e "s/_$//g"|sed -e "s/^_//g" | tr '+' '_' |\
  sed -e "s/__*/_/g"|sed -e "s/_\([0-9][0-9]*\)/\1/g"|\
  sed -e "s/^[a-z]\_\([a-z][a-z]\+\)/\1/g"


}

#**
#* @brief - all_to_upper - Moves all to upper case
#*
all_to_upper(){
camel_to_norm $1 | tr 'a-z' 'A-Z'
}

#**
#* @brief generate_list - will generate list of symbols from tags matching
#*
generate_list() {
cut -d' ' -f1,4 $TMPFILE|grep "$1\$"|cut -d' ' -f1|sort|uniq
}

#**
#* @brief find_files - finds list of files from tag file matching usage
#*
find_files(){
cut -d' ' -f1,2 $TMPFILE|grep "^$1"|cut -d' ' -f2|sort|uniq
}
TMPFILE=/tmp/cleaner.$$.tmp
TMPFILE1=/tmp/cleaner.$$.1.tmp
# grab the output for only the ones we want..
grep  "$DIR_FILE" tags |grep -v "^\!"|sed -e 
"s/\/\^.*\"/REPLACE_1/g">$TMPFILE

replace_pattern(){
echo "=====>Search and replace for pattern $1 with $2"
# Convert the functions back to smaller case and replace them in the files
for token in `generate_list "[$1]"`
do
  if [ $2 -eq "1" ]; then
   new=`camel_to_norm $token`
  else
   new=`all_to_upper $token`
  fi
  if [ "$new" != "$token" ]; then
   echo "==>Replacing $token with $low"
   for file in `find_files $low`
   do
    echo "->replacing $token to $new in file $file"
    cp $file $TMPFILE1
    dos2unix $TMPFILE1
    sed -e "s/\(W*\)$token\(\W*\)/\1$new\2/g" $TMPFILE1>$file
   done
  else
   echo "==>not replacing $token"
  fi
done
}
echo "================= Replacing Defines ==============="
replace_pattern d 2
echo "================= Replacing Functions ==============="
replace_pattern efgmpstvx 1
rm -f $TMPFILE $TMPFILE1

-----

-- 
Regards,
Nishanth Menon
Ref:
[1] 
http://driverdev.linuxdriverproject.org/pipermail/devel/2009-September/002482.html
[2] 
http://nishanthmenon.blogspot.com/2009/08/replace-camelcasing-with-lowercasing.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] DSPBRIDGE: Kill CAMelCaSiNg
  2009-09-17 14:23 [RFC] DSPBRIDGE: Kill CAMelCaSiNg Nishanth Menon
@ 2009-09-21 12:05 ` Felipe Contreras
  2009-09-21 13:12   ` Andy Shevchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Felipe Contreras @ 2009-09-21 12:05 UTC (permalink / raw)
  To: Nishanth Menon; +Cc: linux-omap@vger.kernel.org

On Thu, Sep 17, 2009 at 5:23 PM, Nishanth Menon <nm@ti.com> wrote:
> Hi All,
> Ref [1] w.r.t some patchset seen on linux driver project having a similar
> condition of ton of camelcasing and [2],
>
> Can we consider running the following script for DSPBridge code? or if there
> are some more improvements folks can think of..

Yes, I think we should do that, and make some sort of release,
possibly squashing all the changes... my current branch counts 218
commits.

> -----
> #!/bin/bash
> # Silly little script to replace CaMELCaSInG to lower_casing
> # Half a zillion thoughts later,
> # why not use ctags which has c interpreter already to do
> # function identification for us?
> if [ ! -r 'tags'  ]; then
> echo "Sorry.. no tags file!"
> exit 1
> fi
> if [ ! -r "$1" ]; then
> echo "I need a directory or a file for replacing.."
> exit 2
> fi
> DIR_FILE=`echo "$1"|sed -e "s/\//\\\//g"`
> # tags have the following notation in field 4 (tab seperated)
> # e - enum value
> # d - define
> # f - function
> # F - File itself
> # g - enum type
> # m - structure parameter
> # p - prototype
> # r - kconfig define(refered)
> # s - structure type
> # t - typedefined variable
> # v - variable
> # x - extern defined variable
> # we can choose to have different rules:
> # rename rule i am going to follow:
> # Any define -> convert camelcase to lower_case_define->convert to caps
> # Any function, enum,struct param,prototype,struct type, typedef var,extern
> def ->
> # camel_case to lower_case_define
> # All files will be made unix file only (no point in dos + unix mixtures..)
>
> #**
> #* @brief - changes camel casing to ones with smaller case
> #*
> camel_to_norm(){
> echo $1|tr '\_' '+' |sed 's/\([A-Z]\)/_\l\1/g' | sed 's/^_\([a-z]\)/\1/g' |\
>  tr 'A-Z' 'a-z' | sed "s/\([a-z][a-z]\+\)/-\1-/g"|tr -d '_'|\
>  tr '-' '_'|sed -e "s/_$//g"|sed -e "s/^_//g" | tr '+' '_' |\
>  sed -e "s/__*/_/g"|sed -e "s/_\([0-9][0-9]*\)/\1/g"|\
>  sed -e "s/^[a-z]\_\([a-z][a-z]\+\)/\1/g"
>
>
> }
>
> #**
> #* @brief - all_to_upper - Moves all to upper case
> #*
> all_to_upper(){
> camel_to_norm $1 | tr 'a-z' 'A-Z'
> }
>
> #**
> #* @brief generate_list - will generate list of symbols from tags matching
> #*
> generate_list() {
> cut -d' ' -f1,4 $TMPFILE|grep "$1\$"|cut -d' ' -f1|sort|uniq
> }

It looks like my tag file was tabs instead of spaces, so that line
makes everything explode... how about:
awk "\$4 ~ /[$1]/ { print \$1 }" $TMPFILE | sort | uniq

> #**
> #* @brief find_files - finds list of files from tag file matching usage
> #*
> find_files(){
> cut -d' ' -f1,2 $TMPFILE|grep "^$1"|cut -d' ' -f2|sort|uniq
> }

Maybe:
awk "\$1 ~ /^$1/ { print \$2 }" $TMPFILE | sort | uniq

> TMPFILE=/tmp/cleaner.$$.tmp
> TMPFILE1=/tmp/cleaner.$$.1.tmp
> # grab the output for only the ones we want..
> grep  "$DIR_FILE" tags |grep -v "^\!"|sed -e
> "s/\/\^.*\"/REPLACE_1/g">$TMPFILE
>
> replace_pattern(){
> echo "=====>Search and replace for pattern $1 with $2"
> # Convert the functions back to smaller case and replace them in the files
> for token in `generate_list "[$1]"`
> do
>  if [ $2 -eq "1" ]; then
>  new=`camel_to_norm $token`
>  else
>  new=`all_to_upper $token`
>  fi
>  if [ "$new" != "$token" ]; then
>  echo "==>Replacing $token with $low"

There's no $low, I guess you meant $new.

>  for file in `find_files $low`

Did you mean $token?

>  do
>   echo "->replacing $token to $new in file $file"
>   cp $file $TMPFILE1
>   dos2unix $TMPFILE1

This is simpler:
dos2unix -n $file $TMPFILE1

However, I think dos2unix should be run separately.

>   sed -e "s/\(W*\)$token\(\W*\)/\1$new\2/g" $TMPFILE1>$file

This is simpler:
sed -e "s/\<$token\>/$new/g" $TMPFILE1 > $file

>  done
>  else
>  echo "==>not replacing $token"

No need to say that.

>  fi
> done
> }
> echo "================= Replacing Defines ==============="
> replace_pattern d 2
> echo "================= Replacing Functions ==============="
> replace_pattern efgmpstvx 1
> rm -f $TMPFILE $TMPFILE1

I did a few more cleanups (yeah, I'm too picky) and here's the result.
Unfortunately I still think it needs some work in the camel conversion
(E_FOO, __FOOBAR__, etc.)

----
#!/bin/bash

# Silly little script to replace CaMELCaSInG to lower_casing
# Half a zillion thoughts later,
# why not use ctags which has c interpreter already to do
# function identification for us?

if [ ! -r 'tags'  ]; then
	echo "Sorry.. no tags file!"
	exit 1
fi

if [ ! -r "$1" ]; then
	echo "I need a directory or a file for replacing.."
	exit 2
fi

DIR_FILE=`echo "$1" | sed -e "s/\//\\\//g"`

# tags have the following notation in field 4 (tab seperated)
# e - enum value
# d - define
# f - function
# F - File itself
# g - enum type
# m - structure parameter
# p - prototype
# r - kconfig define(refered)
# s - structure type
# t - typedefined variable
# v - variable
# x - extern defined variable
# we can choose to have different rules:
# rename rule i am going to follow:
# Any define -> convert camelcase to lower_case_define->convert to caps
# Any function, enum,struct param,prototype,struct type, typedef
var,extern def ->
# camel_case to lower_case_define
# All files will be made unix file only (no point in dos + unix mixtures..)

#**
#* @brief - changes camel casing to ones with smaller case
#*
camel_to_norm() {
	echo $1 | tr '\_' '+' | sed 's/\([A-Z]\)/_\l\1/g' | sed
's/^_\([a-z]\)/\1/g' | \
	tr 'A-Z' 'a-z' | sed "s/\([a-z][a-z]\+\)/-\1-/g" | tr -d '_'| \
	tr '-' '_' | sed -e "s/_$//g" | sed -e "s/^_//g" | tr '+' '_' | \
	sed -e "s/__*/_/g" | sed -e "s/_\([0-9][0-9]*\)/\1/g" | \
	sed -e "s/^[a-z]\_\([a-z][a-z]\+\)/\1/g"
}

#**
#* @brief - all_to_upper - Moves all to upper case
#*
all_to_upper() {
	camel_to_norm $1 | tr 'a-z' 'A-Z'
}

#**
#* @brief generate_list - will generate list of symbols from tags matching
#*
generate_list() {
	awk "\$4 ~ /[$1]/ { print \$1 }" $TMPFILE | sort | uniq
}

#**
#* @brief find_files - finds list of files from tag file matching usage
#*
find_files() {
	awk "\$1 ~ /^$1/ { print \$2 }" $TMPFILE | sort | uniq
}

TMPFILE=/tmp/cleaner.$$.tmp
TMPFILE1=/tmp/cleaner.$$.1.tmp

# grab the output for only the ones we want..
grep "$DIR_FILE" tags | grep -v "^\!" | sed -e
"s/\/\^.*\"/REPLACE_1/g" > $TMPFILE

replace_pattern() {
	echo "=====> Search and replace for pattern $1 with $2"
	# Convert the functions back to smaller case and replace them in the files
	for token in `generate_list $1`
	do
		if [ $2 -eq "1" ]; then
			new=`camel_to_norm $token`
		else
			new=`all_to_upper $token`
		fi
		if [ "$new" != "$token" ]; then
			# echo "==> Replacing '$token' with '$new'"
			for file in `find_files $token`
			do
				echo "==> Replacing $token to $new in file $file"
				dos2unix -n $file $TMPFILE1
				sed -e "s/\<$token\>/$new/g" $TMPFILE1 > $file
			done
		fi
	done
}

echo "================= Replacing Defines ==============="
replace_pattern d 2
echo "================= Replacing Functions ==============="
replace_pattern efgmpstvx 1

rm -f $TMPFILE $TMPFILE1
----

Cheers.

-- 
Felipe Contreras
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] DSPBRIDGE: Kill CAMelCaSiNg
  2009-09-21 12:05 ` Felipe Contreras
@ 2009-09-21 13:12   ` Andy Shevchenko
  2009-09-21 13:35     ` Felipe Contreras
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2009-09-21 13:12 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: Nishanth Menon, linux-omap@vger.kernel.org

On Mon, Sep 21, 2009 at 3:05 PM, Felipe Contreras
<felipe.contreras@gmail.com> wrote:
> Yes, I think we should do that, and make some sort of release,
> possibly squashing all the changes... my current branch counts 218
> commits.
I think it should be somehow announced before. Because, f.e., my
patches is so big to rewrite them
(and most of them are accepted already, although I can't see commits
in public repository)

> I did a few more cleanups (yeah, I'm too picky) and here's the result.
Actually even this could be simplified :-)

> #**
> #* @brief - changes camel casing to ones with smaller case
> #*
> camel_to_norm() {
>        echo $1 | tr '\_' '+' | sed 's/\([A-Z]\)/_\l\1/g' | sed
> 's/^_\([a-z]\)/\1/g' | \
I suppose there is could be used code like: sed -e '<expr1>; <expr2>'
instead of two or more sed calls.

>        tr 'A-Z' 'a-z' | sed "s/\([a-z][a-z]\+\)/-\1-/g" | tr -d '_'| \
tr -d '_' is the 's/_//g' expression for sed

>        tr '-' '_' | sed -e "s/_$//g" | sed -e "s/^_//g" | tr '+' '_' | \
>        sed -e "s/__*/_/g" | sed -e "s/_\([0-9][0-9]*\)/\1/g" | \
>        sed -e "s/^[a-z]\_\([a-z][a-z]\+\)/\1/g"
The similar. It could be optimized to a few sed calls (perhaps even to
one call with all expressions in a sequence).

> # grab the output for only the ones we want..
> grep "$DIR_FILE" tags | grep -v "^\!" | sed -e
> "s/\/\^.*\"/REPLACE_1/g" > $TMPFILE
sed could match and skip lines like grep -v 'smth.': sed -e '/^!/d; ...'

>                                dos2unix -n $file $TMPFILE1
>                                sed -e "s/\<$token\>/$new/g" $TMPFILE1 > $file
dos2unix actually one expression for sed, AFAIK.
And new sed versions (starting from 3.8?) have -i which means 'in place'.

P.S. And personally I prefer to use perl :-)

-- 
With Best Regards,
Andy Shevchenko
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] DSPBRIDGE: Kill CAMelCaSiNg
  2009-09-21 13:12   ` Andy Shevchenko
@ 2009-09-21 13:35     ` Felipe Contreras
  0 siblings, 0 replies; 4+ messages in thread
From: Felipe Contreras @ 2009-09-21 13:35 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Nishanth Menon, linux-omap@vger.kernel.org

On Mon, Sep 21, 2009 at 4:12 PM, Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
> On Mon, Sep 21, 2009 at 3:05 PM, Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
>> Yes, I think we should do that, and make some sort of release,
>> possibly squashing all the changes... my current branch counts 218
>> commits.
> I think it should be somehow announced before. Because, f.e., my
> patches is so big to rewrite them
> (and most of them are accepted already, although I can't see commits
> in public repository)

Indeed. I'm planning on making my own "release" and pick your patches,
but for a more "official" release we (or Omar) should make an official
announcement so people send their patches before drawing the line.

>> I did a few more cleanups (yeah, I'm too picky) and here's the result.
> Actually even this could be simplified :-)
>
>> #**
>> #* @brief - changes camel casing to ones with smaller case
>> #*
>> camel_to_norm() {
>>        echo $1 | tr '\_' '+' | sed 's/\([A-Z]\)/_\l\1/g' | sed
>> 's/^_\([a-z]\)/\1/g' | \
> I suppose there is could be used code like: sed -e '<expr1>; <expr2>'
> instead of two or more sed calls.
>
>>        tr 'A-Z' 'a-z' | sed "s/\([a-z][a-z]\+\)/-\1-/g" | tr -d '_'| \
> tr -d '_' is the 's/_//g' expression for sed
>
>>        tr '-' '_' | sed -e "s/_$//g" | sed -e "s/^_//g" | tr '+' '_' | \
>>        sed -e "s/__*/_/g" | sed -e "s/_\([0-9][0-9]*\)/\1/g" | \
>>        sed -e "s/^[a-z]\_\([a-z][a-z]\+\)/\1/g"
> The similar. It could be optimized to a few sed calls (perhaps even to
> one call with all expressions in a sequence).

Yeah, I think this code should be re-factored, but for that I guess
we'll need a list of simple checks to make sure that we get what we
want.

>> # grab the output for only the ones we want..
>> grep "$DIR_FILE" tags | grep -v "^\!" | sed -e
>> "s/\/\^.*\"/REPLACE_1/g" > $TMPFILE
> sed could match and skip lines like grep -v 'smth.': sed -e '/^!/d; ...'
>
>>                                dos2unix -n $file $TMPFILE1
>>                                sed -e "s/\<$token\>/$new/g" $TMPFILE1 > $file
> dos2unix actually one expression for sed, AFAIK.

Yes, but dos2unix (or equivalent) will generate a *huge* diff, that's
why I think it should be done in a different step.

> And new sed versions (starting from 3.8?) have -i which means 'in place'.

That would be better IMHO.

> P.S. And personally I prefer to use perl :-)

For this kind of usage I would prefer it as well (if the code doesn't
become obfuscated), but I'd prefer ruby myself :)

Cheers.

-- 
Felipe Contreras
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-09-21 13:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-17 14:23 [RFC] DSPBRIDGE: Kill CAMelCaSiNg Nishanth Menon
2009-09-21 12:05 ` Felipe Contreras
2009-09-21 13:12   ` Andy Shevchenko
2009-09-21 13:35     ` Felipe Contreras

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox