Game Development Community

*nix BROWSER environment variable

by Gary "ChunkyKs" Briggs · 12/01/2004 (3:58 pm) · 0 comments

Download Code File

Currently, calling Platform::openWebBrowser on *nix first attempts to get a torque pref, then takes some educated guesses. This is a patch to make it honor the BROWSER environment setting, and imrpove the education of the guesses.

The standard is here: www.catb.org/~esr/BROWSER/

Mainly I did this because starting konqueror on my machine is a hassle [I don't run kde, so starting konq breaks my sound device, slows the whole machine down, and generally causes problems], but also because this is better standard support and I like firefox :-)

The attachment is a unified diff against version 1.3 of x86UNIXWindow.cc. Alternatively, here's the salient bit of code:

char *b_env; /* the result from getenv, which we'll strdup if valid */
char *currb; /* the current one. Get by strtok'ing b_env */

char* argv[3];
argv[0] = "";
argv[1] = const_cast<char*>(webAddress);
argv[2] = NULL;

b_env = getenv("BROWSER");
if(b_env  && *b_env) { // Found something valid
   if(NULL != (b_env = strdup(b_env))) { // strtok molests this in-place
      currb = dStrtok(b_env,":");
      while( currb && *currb ) {
         char *ps; // percent sign
         if(NULL != (ps = dStrstr(currb, "%"))) {
            int foundps = 0; // number of percent-s's we found

            do {
               if('s' == *(ps+1))  foundps++;
               else if('%' != *(ps+1)) continue; // Invalid string
            } while(NULL != (ps = dStrstr(ps+2, "%")));

            if(1 == foundps) {
               char bcommand[1024];
               dSprintf(bcommand, sizeof(bcommand), currb, const_cast<char*>(webAddress));
               if(0 == system(bcommand)) exit(0);
            } else if(0 == foundps) {
               char bcommand[1024];
               dSprintf(bcommand, sizeof(bcommand), "%s %s", currb, const_cast<char*>(webAddress));
               if(0 == system(bcommand)) exit(0);
            } // foundps >= 2 is against the standard; ignore
         } else {
            argv[0] = currb;
            execvp(currb, argv);
         }
         currb = dStrtok(NULL,":");
      }
      free(b_env);
   }
}

Gary (-;