Je t’embrasse Salutations from Silicon Valley, California

11Oct/120

FC15: Synergy across multiple users

I was in the market to setup synergy on one of my desktop machines at work today. Synergy itself, no problem. However, it was running in the foreground of only one of my user accounts, which meant that logout/login would be impossible...

Looking for a way around this, I came across a guide which would get me about 99% of the way there. Here is the steps that I did, and what worked for me:

Server Side (Windows 7):

  • First, I setup Synergy 1.4.10Beta on my Windows 7 laptop (named 'lappy'). I was setting this machine up as my Synergy server, so clicked the "server" bullet, then the "Configure server" button, and finally added my soon-to-be client (named 'bashful') to the setup.
  • I opened up the Windows command prompt and ran "ipconfig /all" to identify my IP address for the client to connect to

Client Side (Fedora 15):

  • In a terminal, with root permissions, I edited the "/etc/gdm/Init/Default", and added the line: "/usr/bin/synergyc --no-tray -n bashful {SERVER_IP}:24800" at the very end of the file, just before the "exit 0" line. (not exactly the same command as the above linked instructions)
  • A reboot proved that I somehow didn't have my Ethernet interface enabled by default. Easily solved. (You can do this in the GUI by clicking on the network icon in the upper right and selecting "Network Settings". After the interface is turned on, you can click on "Options" and select "Connect Automatically")
Filed under: Linux No Comments
11Sep/120

Exploring Web Apps

So I have been exploring the idea of making an HTML5-esque app. Something that can be cross-platform, and potentially do something useful at the same time. So within 4 days, I put together an HTML5 app that showcases some of my new skills (if you can call them that). Below is the list of things I worked on:

  • Building (in real-time) HTML5 inside of jsFiddle
  • Connecting to an external JSON-style API
  • Dynamically building/redrawing jQuery Mobile elements
  • Identifying and use of Geo-location
  • Addition of mobile "touch" events

This is all demonstrated in the app that I have just completed: Twitter Search (within 10 miles) The idea was to get used to the workflow of creating an HTML5 app, and I think I can say that was achieved.

Honestly I think the app is terrible, and there is a reason it is suffixed with "_00". Things to consider for the next version:

  • Dump tweets into an array
  • create new list items only on array change
  • refresh only populates items not already in array
  • auto-refresh timer
  • if new tweets in auto-refresh, push the list-item on the top (visible)
  • if the list is going to keep growing, add a pause button

Have a look at the source, or the original jsFiddle can be found here: TwitterSearch jsFiddle

In the meantime, I will try to start some code postings explaining some of the tricky bits that I ran into.

Filed under: HTML5 No Comments
20Jan/120

std::list iterate + erase

I had an interesting problem the other day where I needed to iterate over an std::list, removing invalid items from that list. The only problem was that the list is a list of pointers, each pointing to allocated values that must be freed.

Here is what I started with:

#include <stdio.h>
#include <list>
using std::list;

struct COORD { int x,y; };

int i,j;
struct COORD *c;
list<struct COORD *> coordList;
list<struct COORD *>::iterator it1;

/* Prep the list */
for (i=0, j=0; i<10, j<10; i++, j++) {
    c = (struct COORD *)malloc(sizeof(struct COORD));
    c->x = i; c->y = j;
    coordList.push_back(c);
}

/* Display while erasing all - for loop */
for (it1=coordList.begin(); it1!=coordList.end(); it1++) {
    c = (*it1);
    printf("A: (%d,%d)\n", c->x, c->y);
    coordList.erase(it1); /* segfault */
    free(c); 
}

As you can see, the problem with this is in the .erase() call. As soon as the item is taken out of the list, "it" still points to that item. Even if you get lucky once or twice, eventually "it1" will point to a value outside of the bounds of coordList.begin() and coordList.end(). At this point, you will continue, because it1!=coordList.end()... and you will segfault when you try to erase the now-invalid iterator.

The fix is to not auto-increment the iterator.

/* Display while erasing all - for loop */
for (it=coordList.begin(); it!=coordList.end(); ) {
    c = (*it);
    printf("A: (%d,%d)\n", c->x, c->y);
    coordList.erase(it++);
    free(c);
}

Note that by removing the increment step from the loop argument, we have to do the increment ourselves.

Of course, you could also use the power of the .erase() method, which states that the return value is:

A bidirectional iterator pointing to the new location of the element that followed the last element erased by the function call, which is the list end if the operation erased the last element in the sequence.

Because of that we can do something a bit fancier:

/* Display while erasing some - for loop #2 */
for (it=coordList.begin(); it!=coordList.end(); ) {
    c = (*it);
    printf("B: (%d,%d)\n", c->x, c->y);
    it = coordList.erase(it);
    free(c);
}

See how we set the iterator value to the next available one by using the return value of the .erase() method. This is a relatively elegant solution, but that also is because we are always updating the iterator through use of the .erase() method. If we had to limit the erase, only deleting odd values (for example), we would have to remember to increment that iterator (otherwise the loop would stay pointing at the same list element forever).

/* Display while erasing some - for loop #3 */
for (it=coordList.begin(); it!=coordList.end(); ) {
    c = (*it);

    if ((c->x % 2) == 1) {
        printf("C: (%d,%d)\n", c->x, c->y);
        it = coordList.erase(it);
        free(c);
    }
    else {
        ++it;
    }
}

That's it. My trial-and-error wisdom, passed on.

Filed under: C/C++ No Comments
24Dec/110

Traversing a file in C

I can not begin to tell you how useful the following code has been in my endeavors to do complicated forward-backward grep-esque searching. Along with the regular-expression matching that I have put together previously, this will round off pretty much everything you need to do your own fancy-grepping.

The code below is based upon the fgetc() and fgets() functions. The first two, rgetc() and rgets() are essentially the reverse of the original functions. They read from the file, but instead of moving the file-pointer forward, they move it back. Thus, you could start at end-of-file, and traverse all the way back to beginning-of-file.

int
rgetc(FILE *stream)
{
  if (fseek(stream, -2, SEEK_CUR) == -1) return EOF;
  return fgetc(stream);
}

int
rgets(char *s, int size, FILE *stream)
{
  int n=0;
  int c;

  while (1) {
    if ((c = rgetc(stream)) == EOF) {
      /* if we are too close to BOF to rgetc() */
      if ((ftell(stream) <= 2) && (n+1 == 2)) {
        rewind(stream);
        n=2;
      }
      /* otherwise EOF == ERROR */
      else return EINVAL;
    }
    if (c == '\n') n++;
    if (n == 2) break;
  }

  if (fgets(s, size, stream) == NULL) return EINVAL;
  return 0;
}

Finally, there is always the time in which what you really want is to read without moving the file-pointer at all. This way, you get a character/line into a buffer, but you still have the same character/line pointed to at the end as you did at the beginning. (Very useful when you need to double-parse a line)

int
tgetc(FILE *stream)
{
  if (fseek(stream, -1, SEEK_CUR) == -1) return EOF;
  return fgetc(stream);
}

int
tgets(char *s, int size, FILE *stream)
{
  int n=0;
  int c;

  while (1) {
    if ((c = rgetc(stream)) == EOF) {
      /* if we are too close to BOF to rgetc() */
      if ((ftell(stream) <= 2) && (n+1 == 2)) {
        rewind(stream);
        n=2;
      }
      /* otherwise EOF == ERROR */
      else return EINVAL;
    }
    if (c == '\n') n++;
    if (n == 1) break;
  }

  /* Look for a newline, otherwise EOF */
  if (fgets(s, size, stream) == NULL) return EINVAL;
  if (strstr(s, "\n") == NULL) return EOF;
  return 0;
}

Trust me, if you want to grep through logs, going forward till X, backward from there till Y, find Z & re-grep entire file for Z, and then locate the 3rd occurrence of the word "ERROR" also associated with Z... anyway, you get the point. Grep is useless. My functions RULE!

Filed under: C/C++ No Comments
18Nov/110

C/C++ Regular Expressions

I had to do some regular-expression matching the other day (a bit of a grep-esque application)

#include <stdio.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>

The basic idea behind the "match" function is to provide a case-insensitive means of telling whether the "string" matches the "pattern". This is why I made this particular function a boolean. I find that this becomes much more powerful when used with a tokenizer, such that specific tokens are matched. (For example log-grepping for a specific user-level or process name)

bool
match(const char *string, const char *pattern)
{
  int status;
  char msg[1024];
  regex_t re;

  if(regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE) != 0) {
    regerror(status, &re, msg, 1024);
    fprintf(stderr, "Error analyzing regular expression '%s': %s.\n",
            pattern, msg);
    return false;
  }

  status = regexec(&re, string, (size_t)0, NULL, 0);
  regfree(&re);

  if (status == REG_NOMATCH) return false;
  else if (status != 0) {
    regerror(status, &re, msg, 1024);
    fprintf(stderr, "Error analyzing regular expression '%s': %s.\n",
            pattern, msg);
    return false;
  }

  return true;
}

Of course, sometimes matching does not go far enough (or in the previous example, sometimes your tokens are not always aligned). Which is why I created the regular-expression version of strstr(). This does exactly what you think, and searches for the regex "pattern" inside of "string". The only difference is that if it is found, we want to return the substring. Likewise if not found, we return NULL.

char *
reSubstring(const char *string, const char *pattern)
{
  int        status, size, i;
  char       msg[1024];
  regex_t    re;
  regmatch_t pmatch[1];
  char       *p_buf;

  if(regcomp(&re, pattern, REG_EXTENDED|REG_ICASE) != 0) {
    regerror(status, &re, msg, 1024);
    fprintf(stderr, "Error analyzing regular expression '%s': %s.\n",
            pattern, msg);
    return NULL;
  }

  status = regexec(&re, string, (size_t)1, pmatch, 0);
  regfree(&re);

  if (status == REG_NOMATCH) return NULL;
  else if (status != 0) {
    regerror(status, &re, msg, 1024);
    fprintf(stderr, "Error analyzing regular expression '%s': %s.\n",
            pattern, msg);
    return NULL;
  }

  size = (pmatch[0].rm_eo - pmatch[0].rm_so) + 1;
  p_buf = (char *)malloc(size);
  memset(p_buf, 0, size);

  for (i=pmatch[0].rm_so; i<pmatch[0].rm_eo; i++)
    p_buf[i-pmatch[0].rm_so] = string[i];

  return p_buf;
}
Filed under: C/C++ No Comments
14Sep/110

Pythons getoutput() ported to C/C++

I ran into an issue the other day where, being an avid Python programmer, I was trying my best in C/C++ to mimic the command-output-capturing capabilities of Python.
In an effort to produce a C/C++ version of the Python commands.getoutput() function, I ended up with the following:

#include <stdio.h>
#include <string.h>
#include <sys/wait.h>

int
command_output(const char *cmd, char *output, int readlen)
{
    FILE *fp;
    int retval=0;
    memset(output, 0, readlen);

    /* return error if pipe error */
    if ( !(fp = (FILE*)popen(cmd, "r")) ) return 1;

    if (fread(output, readlen, 1, fp) != 1) {
        retval = pclose(fp);
        if (WIFEXITED(retval)) return 0;
        /* Command did not execute correctly, or was killed */
        return 2;
    }

    /* guarantee NULL termination */
    output[readlen - 1] = '\0';
    pclose(fp);
    return 0;
}

Although it does require a bit more information than the Python equivalent, it is extremely handy to have in one's toolbox. Especially when interactions are forced between your C/C++ internals, and preexisting applications. Note that this code operates with the same permissions as the application, thus cannot execute anything that the application does not have permission to execute.

Filed under: C/C++ No Comments
20May/110

commands to subprocess transition

The Python Documentation goes a long way in describing how to transition old code, but for some reason, fails to mention how to transition the legacy "commands.getoutput()" and "commands.getstatusoutput()" functionality into the new Python 2.6+ model of using the subprocess module.

Here is the code required (direct pull/modification of original 2.5 commands module code):

# Get the output from a shell command into a string.
# The exit status is ignored; a trailing newline is stripped.
# Assume the command will work with '{ ... ; } 2>&1' around it..
def getoutput(cmd):
    return getstatusoutput(cmd)[1]

# Ditto but preserving the exit status.
# Returns a pair (sts, output)
def getstatusoutput(cmd):
    import subprocess, os
    p = subprocess.Popen('{ ' + cmd + '; } 2>&1',
                         shell=True, stdout=subprocess.PIPE)
    sts = os.waitpid(p.pid, 0)[1]
    text = p.stdout.read()
    p.stdout.close()
    if text[-1:] == '\n': text = text[:-1]
    return sts, text
Filed under: Python No Comments
13Apr/110

Pretty-Print for Python Dict/List/Tuple

I wrote this a while ago to give me a better view into complex nested structures Dict-of-List-of-Dict, etc.
The benefit here is that these functions can be wrapped as a Python module, and imported to all your projects for debug.

Enjoy.

import sys

def newline():
    sys.stdout.write('\n')
    sys.stdout.flush()

def __type_quote(this):
    if type(this) == type(1): return str(this)
    else: return "'"+str(this)+"'"

def __has_children(parent):
    answer = False
    valid = [type([1,2]), type((1,2)), type({1:2})]
    if type(parent) == type([1,2]):
        for each in parent:
            if type(each) in valid: answer = True
    elif type(parent) == type((1,2)):
        answer = __has_children(list(parent))
    elif type(parent) == type({1:2}):
        for (key, value) in parent.items():
            if type(value) in valid: answer = True
    return answer

def __print_list(list, n=0, shrink=False, opener="[", closer="]"):
    assert type(list) == type([])
    sp = "".join([" "]*n)
    valid = [type([1,2]), type((1,2)), type({1:2})]

    # find the max index (for spacing of the format string)
    size = len(list)
    fstring = "%s  %-"+str(size)+"s: "

    # start printing stuff
    sys.stdout.write(opener)
    sys.stdout.flush()
    list.sort()
    for index in range(len(list)):
        newline()
        sys.stdout.write(fstring%(sp, index))
        sys.stdout.flush()
        this = list[index]
        if not __has_children(this) and type(this) in valid:
            if type(this) == type([1,2]) and len(this) > 10 and shrink == True:
                sys.stdout.write("['%s', ... ,'%s']"%(this[0], this[-1]))
                sys.stdout.flush()
            else:
                sys.stdout.write(str(this))
                sys.stdout.flush()
        elif type(this) == type([1,2]): __print_list(this, n+4+size)
        elif type(this) == type({1:2}): __print_dict(this, n+4+size)
        elif type(this) == type((1,2)): __print_tuple(this, n+4+size)
        else:
            sys.stdout.write(str(this))
            sys.stdout.flush()
    if len(list) > 0:
        newline()
        sys.stdout.write("%s%s"%(sp, closer))
    else: sys.stdout.write("%s"%closer)
    sys.stdout.flush()

def __print_dict(dict, n=0, shrink=False):
    assert type(dict) == type({1:2})
    sp = "".join([" "]*n)
    valid = [type([1,2]), type((1,2)), type({1:2})]

    # find the max key-size (for spacing of the format string)
    size = 0
    for key in dict.keys():
        if len(str(key)) > size: size = len(str(key))
    fstring = "%s  %-"+str(size)+"s: "

    # start printing stuff
    sys.stdout.write("{")
    sys.stdout.flush()
    keys = dict.keys()
    keys.sort()
    for key in keys:
        newline()
        sys.stdout.write(fstring%(sp, key))
        sys.stdout.flush()
        this = dict[key]
        if not __has_children(this) and type(this) in valid:
            if type(this) == type([1,2]) and len(this) > 10 and shrink == True:
                sys.stdout.write("[%s, ... ,%s]"%(__type_quote(this[0]),
                                                  __type_quote(this[-1])))
                sys.stdout.flush()
            else:
                sys.stdout.write(str(this))
                sys.stdout.flush()
        elif type(this) == type([1,2]): __print_list(this, n+4+size)
        elif type(this) == type({1:2}): __print_dict(this, n+4+size)
        elif type(this) == type((1,2)): __print_tuple(this, n+4+size)
        else:
            sys.stdout.write(__type_quote(this))
            sys.stdout.flush()
    if len(dict.keys()) > 0:
        newline()
        sys.stdout.write("%s}"%sp)
    else: sys.stdout.write("}")
    sys.stdout.flush()

def __print_tuple(tup, n=0, shrink=False):
    assert type(tup) == type((1,2))
    __print_list(list(tup), n, shrink, opener="(", closer=")")

def print_dict(d):
    __print_dict(d, shrink=True)
    newline()

def print_list(l):
    __print_list(l, shrink=True)
    newline()

def print_tuple(t):
    __print_tuple(t, shrink=True)
    newline()
Filed under: Python No Comments
23Feb/110

Parallel DHCP Clients

There (as of 4.1.1-P1) is a bug in DHCP (at least the public release from the ISC) that can cause multiple DHCP client instances to accidentally generate the same "random" XIDs (transaction ID) and therefore mangle communications when operating in parallel. The problem is that they seed their randomness using "current time in seconds" + "some seed". And as you quickly note, launching processes in parallel almost guarantees more than one process per second...

srandom(seed + cur_time);

The fix, as I filed under [ISC-Bugs #21497] (thanks to Jeff Haran for the idea) was simple... Just add getpid().

srandom(seed + (unsigned)cur_time + (unsigned)getpid());

Of course, fixing this 1 issue brought 2 more to the surface. Read on to see what I mean.

  1. At least in Linux, there is a notion of "Primary" and "Secondary" addresses. (Aliases being one kind of secondary address) The problem is that with removal of any Primary address (which happens in several different DHCP client state changes) all Secondary addresses are lost. This plays out very badly if any of the secondary addresses are controlled by a separate instance of a DHCP client state machine. Not to mention that if you lose state on a running DHCP client (like removing its address from under it), you may have to wait for a lease renewal to reactivate the state machine.

  2. Which brings up the next issue, which is the lack of shared state between parallel instances of DHCP clients. Seems unnecessary until you realize that because there isn't any shared state, the Primary address is not guaranteed to arrive first. This also seems of little consequence until you realize that any Secondary addresses, assigned before the Primary, are obliterated (another loss of state) in the setting of the Primary address. Oops?

Now, after about 3 weeks of working on this issue, I think I discovered a solution to the Primary/Secondary problem.  Hey, 50% isn't too bad.
The fix is to set a kernel networking flag called "promote_secondaries", although you will note that this is only for IPv4.

echo 1 >/proc/sys/net/ipv4/conf/all/promote_secondaries

It is just that simple. Now as soon as you remove a Primary address, the next Secondary address will take its place.

Filed under: Uncategorized No Comments
26Dec/100

Christopsomo (Greek Christmas Bread)

This recipe comes from my mother, via the "Sunset Cook Book of Breads" (1984).

Ready for Last Rise
Ingredients:
2 Tbps Active Dry Yeast
1/2 Cup Warm Water
1/2 Cup Scalded & Cooled Milk
1 Cup Butter
4 Eggs, beaten
3/4 Cup Granulated Sugar
2 teaspoons crushed Anise seed
1 teaspoon salt
7 Cups all-purpose flour

Directions:
Mix yeast with warm water & set aside for about 5 minutes. Mix together yeast mixture, milk, butter, eggs, salt, sugar, and anise thoroughly before adding flour. Gradually add flour one cup at a time mixing/massaging the flour in evenly.

Kneed the dough until the elasticity of it pushes back, and the dough is smooth. Roll into a ball in a large greased bowl, making sure to get the surface of the dough covered in the oil/grease. Set aside to rise (for the first time) in a warm/moist location. (Or if you are lazy like me, put it in an empty oven, with some almost-boiling water in a adjacent pan)

Ready to EatWhen the dough has doubled in size (1-2 hours) punch it down & divide it up into 2 even halves. Additionally cut off about 1 Cup worth of dough from each, and set aside (will be used later to decorate). Kneed each half into a smooth round, and place on a flat baking sheet. Shape the 1 Cup of dough into two equal-length ropes, cutting down each end of the ropes to create the traditional Greek cross shape. Finally, garnish the holes with walnuts or candied cherries, and wipe the top with an egg-white to add shine.

Once shaped, let rise again (the last time) until almost double in size. At this point, place in a 350-degree oven for about 45 minutes. (If you are me, and have risen the bread IN the oven, no worries, just turn on the oven to 350, and add 10 minutes for it to preheat WITH the bread already in there)

Alternatives:
You can make this same recipe into a single large loaf, but it is a bit unwieldy, and relatively hard to find a non-pizza pan for.

Filed under: Recipes No Comments