Sunday, August 12, 2018

Quick and easy code searches

Getting presented with a large and unfamiliar code base can sometimes prove daunting.  One of the biggest challenges is how to efficiently search for things when you need to make changes.

Since I'm a big fan of platform independent tools, I had been using recursive grep searches but those frequently return too many results.  It can be difficult to limit the files searched to just the source files of interest.  Perhaps you have a unit test directory you don't wish to search.  You might also want to skip directories containing library files or other binaries.  I prefer a tool called ack to help limit the unwanted files being searched.  That prevents lots of false positives which you need to wade through.

Creating a file called .ackrc in your home directory is an easy way of setting the default settings for ack.  The following sample .ackrc file configures ack to ignore a few directories which normally don't contain code of interest.  It also sets an alternate file extension for makefiles and configures the default source languages of interest.

--ignore-dir=.svn
--ignore-dir=dist
--ignore-dir=docs
--ignore-dir=examples
--ignore-dir=lib
--ignore-dir=tests
--type-add=make:ext:make
--asm
--cc
--make

Using ack and an appropriately configured .ackrc file can significantly reduce the number of lines of matches you must check when searching your source tree for a keyword.

Tuesday, July 17, 2018

Making code searches easier


When modifying source code I'm not familiar with, it's usually necessary to do some recursive searches to figure out what to change.  You can use a simple recursive grep but I prefer a tool called ack.  You can download ack in one of several forms.  I prefer the all-in-one perl script version.  I prefer ack because it can be configured via .rc (run command) file or command line to limit the searches to files of interest.  You can ignore files with certain extensions or entire directories.  Few things are more disheartening than searching for a term within the code only to find out it appears on many hundreds of lines.  Who feels like wading through hundreds of lines to find what you're actually looking for?
 
I also use the following bash function to pipe ack's output to gvim so I can weed out unwanted matches quickly.  This is handy if the search term is a common word which is also used as a variable or function name.  I delete lines containing any variations which aren't of interest which helps reduce the job to something more manageable.

The ack script may be replaced with a simple recursive grep if you'd like.  You can also use a different editor if you don't care for Vim/Gvim.  You'll just need an editor capable of accepting input from stdin.  The following function also tells gvim to highlight the search term which I find to be a time saver.

function    ackvr    # invoke ack perl script and edits the match
{
    if [ -z "$1" ]; then
        echo "Usage -- ackvr SearchPattern
    else
        echo "ack searching for $1"
        ack "$1" | gvim -c "/$1" -

    fi
}

Saturday, November 04, 2017

Using RSS feeds to save time

I've long been a fan of RSS feeds as a way to save time when monitoring blogs.  It affords me the ability to see the headlines of new posts (and only the new posts) as they appear.  I can choose to visit the blog if the headline sounds interesting or if none of the articles catches my eye, I can mark all headlines read which makes the web site disappear in my RSS reader until the next time they add content.  It is so much faster than actually visiting a bunch of web sites that it's not funny.

I have three separate lists of RSS feeds I monitor.  I use a cross-platform app called QuiteRSS on both my Mac at home and on my Windows laptop at work.  On my phone and tablet device, I use a mobile app called Feedly to monitor a third set of RSS feeds.

At home I follow RSS feeds for beer news, book releases, and information about RetroComputing.  I don't read about these topics at work because I don't feel right about asking a company to pay me to do that.

At work I follow a number of technical RSS feeds on computer security, software development, embedded system, computer communications, and a few other tech topics.  These are all directly or indirectly related to my job.  Having these RSS feeds to read allows me to take small breaks at work while my software is building or when I'm waiting for code to download to one of my development boxes.  I'm a firm believe in the studies that say these micro-breaks improve productivity.

Whenever I have a bit of downtime during the day such as while waiting at an appointment or in line at a store, I can look at the local news sites I monitor in the Feedly app on my phone.  Feedly synchronizes feeds so when I look at those same feeds at home on my tablet device, I only see articles I haven't marked read on either device.

Thursday, October 19, 2017

Preventing strange colors for the "ls" command

I occasionally need to connect to a Linux server via ssh.  Since I use an ssh client which has a dark background, the default colors used by the ls command frequently makes directory listings difficult to read.  Fortunately you can solve this one of two ways.


For a solution which will persist throughout the ssh session, you can issue the "unalias ls" command.  That removes the alias which causes the odd coloration.  It will reappear the next time you log into that server.


For a non-persistent solution you can preface the ls command with a backslash.  Using "\ls" prevents any command alias from being used.

Thursday, October 12, 2017

Using Vim's folding feature to hide inactive conditional code

I was recently looking at some C code which had lots of conditionally compiled sections (#if, #else, #endif).  I don't care for code like that but sometimes it's a necessity when source code must support multiple hardware platforms.  In any case, it makes the code harder to read.  So I've whipped up a little macro which if placed on the #if or #else line, will use Vim's folding feature to hide that portion of code.


map    \fc   mx^%:'x,. fold


To unhide the folded section, you can issue the zd command.  I've buried that in a macro invoked by "\fd" since the more mnemonic macros are easier for me to remember.

Sunday, May 03, 2015

Gaining productivity with smartphones

I'm often surprised at how many people load their smartphones up with time wasters like games.  There were a few problem solving desktop games which appealed to me years ago but first person shooters and the mindlessly repetitive games I've seen on phones don't hold any interest for me.  Instead I've got my iPhone loaded with apps which increase my productivity and I thought others might find an idea or two in this list which might help them as well.

Cloud Storage (DropBox) - My storage needs are modest so the free account suffices for me.  This is the easiest method for sharing files between multiple desktop/laptop computers and tablets/phones.  I don't keep any sensitive data on here long term.  For times when I need to temporarily transfer such data, I create a TrueCrypt volume to store such data and transfer that up to DropBox.

Database (Bento) - Sadly, this handy little flat-file database program has been discontinued.  I use it to store all sorts of information such as books I want to read and/or purchase, books that I own (to prevent duplicate purchases in paper form), house maintenance tasks, car maintenance done, etc.  Since Microsoft has made Excel freely available, people who want similar capabilities might choose to use it instead.  It has most of the capabilities of a simple database like sorting by different fields.  If you combine it with Dropbox, you can even access your data from either your phone or a desktop PC.

Text Editor (DropText) - This gives me the ability to create and edit text files which can be shared with a desktop or laptop PC when combined with DropBox.  It allows me to maintain a paperless existence at work.  Staying paperless allows easier searching of notes.  Making notes isn't useful if you can't easily find specific data within them.

Password Manager (1Password) - Between work and home, I have around 600 unique username/password combinations to keep track of.  1Password allows me to do this gracefully while storing them securely.  I choose the WiFi Sync option so my password data isn't accessible on the cloud because I don't trust any company's cloud security.

Web Content read later app (Instapaper) - Occasionally I'll find something in a browser or one of my smartphone apps which I'd like to read later when I'll have less distractions.  Instapaper lets me store these URLs to read later.  It's available as a bookmarklet which works in all browsers.  It also works in some of my smartphone apps.

Library e-book/e-audiobook borrowing (Overdrive) - I use it mostly for borrowing e-audiobooks from our county's library system.  It keeps my 40-70 minute commute (each way) from getting boring.  Buying this many audibooks would get quite expensive in a hurry.

Podcast app (Overcast) - This makes keeping up with podcasts painless.  I've not found another podcast app which works nearly so well.

e-book reader (Kindle app) - Often I find myself unexpectedly stuck waiting somewhere like a dealer service department or a doctor's office where my choices of entertainment are old People magazines or whatever inane channel they have the TV tuned to.  Instead I opt to read whatever Kindle book is currently occupying me at home.  Since the Kindle app automatically syncs between devices, I don't even have to remember which page number was the last one I read.

PDF/ePub document reader (iBooks) - For the last 5 years or so, I've been buying all my technical books from O'Reilly in ePub format.  That allows me to have them with me on my phone in case I need to look something up quickly.  Searching is always better than trying to use the index in a paper book.  Plus I have access to my tech books both at work and at home without having to lug them home in my backpack each night.

RSS reader (Feedly app) - I keep up with tech blogs via RSS.  It lets me know when new articles are published and a glance at the title lets me know whether I'm interested enough to spend the time reading them.

Dynamic e-magazine (Zite or Flipboard) - Reading a fixed set of blogs sometimes lets interesting items sneak past me.  Zite allows me to tailor the type of information I'm interested in reading.  There's just enough serendipity in their matching that I'm usually amused or entertained by a few articles it finds for me.

So that's my list of the smartphone apps I use most often.  Are there any I've missed which you like?

Sunday, April 12, 2015

USB hubs

Many years ago while working for U.S. Robotics, I developed USB device drivers for a living and learned a fair amount about USB.  The big takeaway from that experience is I'm disappointed that Firewire didn't win the external bus contest.  It's a much better designed bus.

One of the necessities for using USB is a hub since few computers include enough USB ports for anything other than the least demanding user.  Since this is the case, it pays to understand a bit about USB hubs since you'll probably need one.

First off, there are multiple flavors of USB - 1.x, 2x, and the recent 3.X.  The key difference as far as users are concerned is the speed.  So the first concern would be to purchase a USB hub which supports the port on your computer.  Unless you've got a fairly recent computer, chances are you've got the USB 2.x flavor.  There is some degree of backwards compatibility so you could theoretically get a USB 1.1 hub but USB 1.x was so slow it's not useful for connecting anything except a mouse or keyboard.

The tricky part comes in choosing which type of power you want your hub to have.  Like most things in USB, there are multiple choices.  You can get a bus powered hub which will derive all its power from the port it's connected to or you can choose a self powered hub which needs an external power supply (AKA wall wart) to power it.  If you don't read any further, take this bit of advice - Never choose a bus powered hub!  To explain why, we'll need a little background about how USB distributes power.

The USB 2.x standard states that USB hubs (and even the ports on the back of your computer are connected to a hub) have to supply 5 volts and up to 500 milliamps (Ma) to each port.  Most likely the ports on the back of your computer offer the full 500 Ma (although a few forward thinking manufacturers offer higher current for those devices which require it).  Worst case if you plug your newly purchased USB bus powered hub with 4 ports into a port on your computer, that new hub will only have 500 Ma to power itself as well as to distribute to its 4 "downstream" ports.  A rule of thumb is that the hub itself takes 100 Ma leaving only 100 Ma for each of its 4 ports.  If you're not familiar with current ratings of devices, 100 Ma isn't a lot.  There are some USB thumbdrives which require more than 100 Ma to function.  So a bus powered hub isn't much good for devices other than a mouse or keyboard or perhaps a low powered USB thumbdrive.

That makes a pretty strong case for choosing a self powered USB hub.  This can also be trickier than it sounds.  Those wall warts aren't always adequate on cheaper USB hubs.  I've seen 8 port USB hubs with a 1000 Ma power supply.  That doesn't leave the user much better off than using a bus powered hub.  So the rule of thumb here is look for a self powered USB hub with a minimum of 500 Ma per port which means an 8 port hub would require 4000 Ma or a 4 amp power supply.

You don't need to buy the most expensive USB hub but it can save time, money, and headaches in the long run if you choose something better than the least expensive.