2008-12-20

Autocompletion in the Windows command processor

Registry key:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor (for all users)
or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor (for the current user)

Set
CompletionChar to 9 to have TAB complete file names,
PathCompletionChar to 9 to have TAB complete folder names.

Nuff said.

2008-12-14

HTML Help Gotcha on toc.hhc

If you're trying to create MS HTML Help files (.CHM) programmatically, you may need to generate your own TOC.

The documentation available from Microsoft does its best to keep you in the dark: They tell you how to generate it in Help Workshop using their point-and-click GUI. It's serviceable but not much fun to use. No wonder there's such a huge cottage industry of MS Help front ends!

Paul Wise bravely documents what he could re-engineer about the innards of the .CHM file but only devotes a rather short section to the Sitemap format of the TOC, which I quote here:

These formats are based on HTML and use the following doctype:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">

The <HEAD> tag contains a <meta> tag providing information on the program that generated the files and a comment indicating the version of the file. e.g.:

<meta name="GENERATOR"content="Microsoft® HTML Help Workshop 4.1">
<!-- Sitemap 1.0 -->

The <BODY> tag contains an <OBJECT> tag that stores properties of the file in <param> tags, followed by a <UL> tag, whose <LI> tags have <OBJECT> tags that store the properties of the Contents/Index items in <param> tags. e.g.:

<BODY>
<OBJECT type="text/site properties">
<param name="Property Name" value="Property Value">

</OBJECT>
<UL>
<LI> <OBJECT type="text/sitemap">
<param name="Property Name" value="Property Value">

</OBJECT>

</UL>
</BODY>

Note that the Property Names and Property Values and tags are not case-sensitive, but HHW will always write all three in the default capitilization, when appropriate.

Note that the tags are mostly in uppercase and the <LI> tag is not closed; this is in compliance with the doctype.

I spent some days generating the TOC from a different structure. To help with eyeball debugging, my HTML was nicely indented. I started a new line for each tag - not entirely unreasonable. Without the indentation, here's an example of what I created:
<HTML>
<BODY>
<UL>
<LI>
<OBJECT type="text/sitemap">
<param name="Name" value="Heading 1">
<param name="Local" value="h1.htm">
</OBJECT>
</LI>
</UL>
</BODY>
</HTML>

Try as I might, HHW (the Workshop) would not display such a TOC, and HHC (the Compiler) would not produce a working .CHM from it. I spent a long time figuring out what's needed to make it work, as in the following:
<HTML>
<BODY>
<UL>
<LI><OBJECT type="text/sitemap">
<param name="Name" value="Heading 1">
<param name="Local" value="h1.htm">
</OBJECT>
</LI>
</UL>
</BODY>
</HTML>

This appears to be one of the best kept secrets of the HTML Help generating industry:

If you don't put <LI> on the same line as <OBJECT>, the TOC parser will fail!

So the accepted format is essentially an HTML Unordered List of Objects with details in Params, give or take a horrible bug in the scanner/parser.

On the bright side, in my experience the following bits are not essential:

  • As Paul states, tags and even attribute names are accepted in upper or lower case.

  • The stuff in the <HEAD> isn't needed and can be dispensed with.

  • The text/site properties OBJECT is only needed if you want to fiddle with the options

  • Tags may be consistently closed (XML fashion), including those that HTML is inconsistent about, such as <meta> and <param. Similarly, it's OK to have closing </LI>'s.

2008-12-12

Running HTML Help Workshop under Wine

Help Workshop is a Microsoft product that creates somewhat compressed help files (.CHM) for use in/with MS programs. The Workshop "main" is a GUI application but there's also a standalone compiler.

Installed and run under Wine in a modern Linux (Ubuntu 8.04), hhw.exe and hhc.exe kept telling me:
HHC5010: Error: Cannot open "C:\windows\temp\TFS483e.tmp". Compilation stopped.

I found a simple fix at The MRPT Project:

You are advised to open "Wine Configuration," set up new configs for hhw.exe and/or hhc.exe . For either or both, in the Libraries tab, add a new override for "itss," then edit it to select "native."

My Wine installation already had an itss.dll in .wine/drive_c/windows/system32, but it was a tiny thing a little over 4 KB in size. I found a "real" itss.dll on the 'net of about 134 KB and plunked that over the installed one.

Both hhw and hhc work for me now.

2008-11-16

Tomcat permissions

As packaged with Ubuntu 8.04, Tomcat is set up pretty tight on security. The configured permissions are quite minimal, so many things you'd want to do in your servlets or JSPs needs to be explicitly permitted.

This isn't necessarily surprising; but it can be a pain.

First, some helpful links: The Security Manager HOW-TO in Apache's Tomcat documentation explains the basics. For specifics on the format of permission entries, see Sun's documentation on Default Policy Implementation and Policy File Syntax.

Notes:


  • Tomcat's policy settings are (on my Ubuntu box) in /usr/share/tomcat5.5/conf/catalina.policy. But heed the warning in the file's header: If your system is Debian-esque (and that includes Ubuntu) then that file is auto-generated from a set of files in conf/policy.d, and those are the ones you want to be editing. Permissions you want to add for your web apps will likely best fit in 50user.policy.


  • The policy files are concatenated and the settings initialized when Tomcat starts. Hence, you need to restart Tomcat in order for permission changes to have an effect.


  • The codeBase string is a URL, so it always takes forward slashes. File names for FilePermission may be operating system dependent, so you should use ${file.separator} instead there.


  • As explained in the Sun doc, an asterisk (*) in a path for FilePermission (roughly) means "everything in this directory", while a dash (-) means "everything in this directory and recursively in all subdirectories". Likewise, the beginning of a host address for SocketPermission may be wildcarded with an asterisk. In the extreme case, the entire host address may be an asterisk and so will apply to "any host".


  • If you're having trouble getting permissions to take effect, you can try narrowing down the problem by making either the codeBase universal (surprisingly, the "codeBase" descriptor is optional, so it's quite legal to have a stanza that starts with "permit {" or the permission all-encompassing: that would be "java.security.AllPermission". Once you get stuff working with this gaping security hole, you will want to go back and try to tighten it up again.

2008-11-15

Document is null

I tried to read an XML configuration file into a DOM, like so:

InputStream configInputStream = this.getClass().getClassLoader().getResourceAsStream(CONF_FILE_NAME);
log("configInputStream = " + configInputStream);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(configInputStream);
log("Found and read configuration: " + doc);
configInputStream.close();

...and the log said:
INFO: ToonGrator: Found and read configuration: [#document: null]

I guess the square brackets and the extra text should have tipped me off, but I sure thought
I had read in a null document!

It isn't really. It turns out that the root (or document) node always contains no text, so
whatever its toString() returns will say "null". Oh well, back to work!

2008-11-12

Writing files from JavaScript

In Internet Explorer, you can use the ActiveXObject "microsoft.xmldom" to load an XML file into a DOM that can be inspected and manipulated from JavaScript. So far so good. But what if you want to write an XML file to the local file system? XmlDom has a save() method and the argument lets you specify a local file path. But in IE6 and later, the attempt will always fail.

The 'net is full of forum posts with frantic pleas for help with titles like "XML DOM save - File Permission denied". In almost all cases, the answers fell into the following categories:

"Tough luck, that's the way it is" / "the cookie crumbles."

True enough, but not helpful.

"You need to have write permission on the folder you're writing to!"

True enough, but I'll bet everyone with half a brain checked that before posting. Even if that was the problem, you'd run up against the previous one.

"I have the same problem. Please email me the answer!"

Me too!

"You can't do that from HTML, you need a HTA

True enough, but if the requester could get a user to download and execute an application he wouldn't have this problem in the first place.

"In ASP on the server you do this..."

That doesn't help with saving files on the client's file system.



It was a long time before I came across a post that mentioned FileSystemObject. It turns out that while xmldom.save() is never allowed to write to the local file system (apparently no amount of tweaking any permissions will change that), FileSystemObject can create, read, write and even delete files, subject to permissions given in IE's Internet Options.

It's no big deal to pull the XML from the DOM as a String and to save it using a TextStream object created by FileSystemObject.CreateTextFile(). Here's a code sample of mine that does it:


function save2()
{
var xmlDoc = new ActiveXObject("microsoft.xmldom");
xmlDoc.load(path + "stuff.xml");
alert("stuff.xml loaded");
fso = new ActiveXObject("Scripting.FileSystemObject");
f2 = fso.CreateTextFile(path + "stuff2.xml", true);
f2.write(xmlDoc.xml);
f2.Close();
alert("stuff2.xml saved");
}


I surmise that more readers of those forums knew the "right" answer but chose not to tell because they suspected malicious intent on part of the requesters. When you can write to the user's file system you can do many evil things.

My take on this is: Any user surfing the Web with IE and permissions set to "trusting" is asking for trouble anyway; and the more professional malware authors already know how to exploit this. Microsoft's choice to never allow XmlDom to save looks silly in view of the fact that there's a perfectly workable back door.

My own use of this feature is legitimate, of course: I'm working with a JavaScript application that wants to store some user settings on the user's machine. More information than will fit into a cookie, and there's no server to persist the information on. Also, it's a closed environment, even to the user, with no Internet connection, so it can safely run with full permissions.

2008-09-03

Namespaces in XSLT

XSLT tutorials and references usually pretend that namespaces don't exist. However, when trying to process an XML document (an XHTML document in this case) that declared a namespace, none of my element node references matched!

XSLT (rightly) distinguishes between element names with no namespace qualifier (which are processed as belonging in the "no namespace" namespace) and names with an explicit qualifier, which of course are treated as belonging to that namespace.

The solution is to

  • declare a prefix for the source document's namespace(s) in the XSLT document node, like (e.g.)

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">

  • use these prefixes in your patterns, like (e.g.)

    <xsl:template match="xhtml:p">



My thanks to Jeni Tennison for explaining it very clearly in this post.

2008-09-02

Parsing XML with an Internet connection

I'm trying to use XSLT (XALAN-J in my case) to extract and re-format some information from some XML files. These files refer to some external entities such as the XHTML definition at http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd. The SAX parser, even with validation turned off, insists on going out on the Web to fetch that DTD and its brethren.

I'm at my workplace, and access to the Web is through a proxy. There are Java system properties I could set to give my application Web access, but I'd have to include my proxy user ID and password, and the external access might still slow my application down.

I tore my hair out for some time and Googling took a long time, this time, to bring me toward a solution.

The solution, in a nutshell, works like this:

  • Manually download all required files to a local directory that will later be included with the app;

  • Provide a catalog redirecting references from their customary URIs to local ones;

  • Include the Apache Commons Resolver library;

  • Provide a CatalogResolver.properties file to tell the Resolver where to find the catalog;

  • attach a new CatalogResolver() to the XML reader.


This is helpfully pointed out and very well explained here.

2008-08-24

Windows 2000 driver for ATI Rage 128 PRO

My mother's PC broke down quite suddenly about a week ago. PC would boot but the screen said "NO SIGNAL". Guessing the video card to be at fault, I packed up an old AGP card and some other stuff and drove down to see what I could do.

Bingo! Replaced the card and got Windows visible on the screen again. But it was displaying in 800x600 and 16 colors. Windows 2000 wouldn't recognize the card so it defaulted to "VGA compatible."

Thus began my hunt for a driver. The sticker on the card says "ATI RAGE 128 PRO 32MB". Well, ATI is a major player in the video card business, so it shouldn't be a problem, should it?

"www.ati.com@ redirects to ati.amd.com . Seems ATI wasn't big enough to avoid being swallowed by AMD. OK, so far so good.

There's a "find your driver" page with a nested menu that leads to "legacy drivers" leads to a driver allegedly good for Windows 2000. A half hour download on my mom's 56K line.

What's this? "You need DirectX 8 to run this installer?" I cursed a bit at this craziness and proceeded. Eventually got to the installer and... "This driver is not compatible with the card(s) in your computer."

I spent 4 hours with Google and downloading drivers from other sites. Many sites, such as softpedia.com, give you menus and descriptions and make your mouth all watery... only to send you back to ATI/AMD's page.

Finally found a "reference driver" at www.treiber-world.de that worked. I thank the good folks there, and hope my reference to them doesn't get them in trouble with AMD.

2008-08-18

Date Arithmetic

In 1996 I wrote a log file management utility on a UNISYS mainframe. It's written in SSG, an obscure script language that has no built-in date arithmetic. It worked mostly OK with my own home-cobbled date arithmetic routines, but every now and then I get complaints about it blowing up due to date-related problems. Time for correct and robust date arithmetic!

I came across these algorithms by Gary Katch. They are short, fast and easily portable. All that's required is integer arithmetic in about 32 bits.

There's no copyright notice on Gary's page, so assuming his consent I'll excerpt the bare bones here, just in case his page gets lost on the 'net:


Calculate day number from date


Given integer y, m, d, calculate day number g as:

function g(y,m,d)
m = (m + 9) % 12
y = y - m/10
return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + d - 1 )

Calculate date from day number


Given day number g, calculate year, month, and day:

function d(g)
y = (10000*g + 14780)/3652425
ddd = g - (365*y + y/4 - y/100 + y/400)
if (ddd < 0) then
y = y - 1
ddd = g - (365*y + y/4 - y/100 + y/400)
endif
mi = (100*ddd + 52)/3060
mm = (mi + 2)%12 + 1
y = y + (mi + 2)/12
dd = ddd - (mi*306 + 5)/10 + 1
return y, mm, dd



In these routines, "%" means modulo, and all division is integer (and thus truncated). I've found them to work well and can recommend them.

2008-08-15

Teamspeak under Linux

I play World of Warcraft on a machine running Kubuntu 8.04 . TeamSpeak is practically required for in-game voice communication.

Sadly, TeamSpeak doesn't play nicely with sound systems under Linux - it blocks an entire sound device.

Some solid information and advice on how to deal with the problem, the best I've found yet, is found on the Linux Gamers site.