12:11 sleep deamon

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[]){
  int result;
  int isDaemon = 0;
  while( (result = getopt(argc, argv, "d")) != -1){
    switch(result){
      case 'd':
        isDaemon = 1;
        break;
      case ':':
      case '?':
        exit(EXIT_FAILURE);
    }
  }
  if (isDaemon){
    daemon(0, 0);
  }
  while(1){
    sleep(1);
  }
  exit(EXIT_SUCCESS);
  return 0;
}

void closeall(int fd){
  int fdlimit = sysconf(_SC_OPEN_MAX);
  while (fd < fdlimit){
    close(fd++);
  }
}

int daemon (int nochdir, int noclose){
  switch (fork()){
    case 0:
      break;
    case -1:
      return -1;
    default:
     exit(0);
  }
  if (setsid()<0){
    return -1;
  }
  switch (fork()){
    case 0:
      break;
    case  -1:
      return -1;
    default:
      exit(0);
  }
  if (!nochdir){
    chdir("/");
  }
  if (!noclose){
    closeall(0);
    open("/dev/null", O_RDWR);
    dup(0); dup(0);
  }
  return 0;
}

// vim: sw=2 ts=2 et:

23:09 MIME-Type を得る

nsIMIMEService

getFromTypeAndExtension(/*string*/aMimeType,/*string*/aFileExt)
return nsIMIMEInfo
getPrimaryExtension(/*string*/aMIMEType,/*string*/aFileExt)
return mimeTypeString
getTypeFromExtension(/*string*/aFileExt)
return mimeTypeString
getTypeFromFile(/*nsIFile*/aFile)
なんか使えない
getTypeFromURI(/*nsIURI*/ aURI)
return mimeTypeString

Example

var fileName = "foo.tar.gz";
var mts = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
var mimeType = mts.getFileFromExtension(fileName);
// "application/x-compressed-tar"
var mimeInfo = mts.getFromTypeAndExtension(mimeType, fileName);
/*
[xpconnect wrapped nsIMIMEInfo]::
MIMEType: "[xpconnect wrapped nsimimeservice]"
QueryInterface: function QueryInterface() { ... }
alwaysAsk: 1
alwaysAskBeforeHandling: true
appendExtension: function appendExtension() { ... }
defaultDescription: "平文テキストドキュメント"
description: "平文テキストドキュメント"
equals: function equals() { ... }
extensionExists: function extensionExists() { ... }
getFileExtensions: function getFileExtensions() { ... }
handleInternally: 3
hasDefaultHandler: false
launchWithFile: function launchWithFile() { ... }
launchWithURI: function launchWithURI() { ... }
macCreator: <no value>
macType: <no value>
possibleApplicationHandlers: [xpconnect wrapped nsIMutableArray]
possibleLocalHandlers: <no value>
preferredAction: 4
preferredApplicationHandler: null
primaryExtension: <no value>
saveToDisk: 0
setFileExtensions: function setFileExtensions() { ... }
type: "[xpconnect wrapped nsimimeservice]"
useHelperApp: 2
useSystemDefault: 4
*/

13:21 HTML DocumentFragmentの生成

var htmlString = "<p>hoge<br>hoge</p>";
var rootElm = document.createElementNS("http://www.w3.org/1999/xhtml","div");
var uhService = Cc["@mozilla.org/feed-unescapehtml;1"]
                    .getService(Ci.nsIScriptableUnescapeHTML);
var htmlFragment = uhService.parseFragment(htmlString, false, null, rootElm); //DocumentFragment

02:00 zsh + screen の設定

.zshrc

SHELL=zsh
setopt prompt_subst
case "$TERM" in
  xterm|kterm|mterm|rxvt*)
  PROMPT='%{^[[33m%}%m%B[%D %T]%b%{^[[m%}\$ '
  RPROMPT='[%{^[[33m%}%4c%{^[[m%}]'
  ;;
 screen*)
  PROMPT='%{^[[33m%}%m%B[%D %T]%b%{^[[m%}\$ '
  RPROMPT='[%{^[[33m%}%4c%{^[[m^[k%c^[\\%}]'
  alias man='set_title man'
  ;;
esac

function set_title(){
  echo -ne "^[[k$@^[[\\"
  "$@"
}
function preexec(){
  echo -ne "^[[k${PWD/${HOME}/~}\$ $1^[[\\"
}
function chpwd(){
  echo -ne "^[[k${PWD/${HOME}/~}^[[\\"
  ls -A
}

.screenrc

shell $SHELL
hardstatus string "[screen $n $t] $h"
captions always "%{= wk} %-w%{=bu dr}%n %t%{-}%+w %= %{=b wb}%y/%m/%d %{=b wb}%c"
shelltitle "$ |$SHELL"

12:09 Firefox3 OpenSearchのsuggest機能

const Cc = Components.classes;
const Ci = Components.interfaces;
const responseType = 'application/x-suggestion+json';
var searchWord = 'hogehoge'
var ss = Cc['@mozilla.org/browser/search-searvice;1']
         .getService(Ci.nsIBrowserSearchService);
var googleSE = ss.getEngineByAlias('google'); //nsISearchEngine
if (googleSE.supportsResponseType(responseType)) {
    var submission = googleSE.getSubmission(searchWord, responseType); //nsISearchSubmission
    var uri = submission.uri; //nsIURI
    var uri_spec = uri.asciiSpec; //'http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=ja&qu=hogehoge
    // ....;
}

< 1 2 3 4 5 >