autom4te and m4sh for the impatient

autom4te is the engine beneath autoconf, but you can use it to generate your own portable shell scripts for other purposes.  For example, create this test.m4sh:

# test.m4sh
m4_errprintn([before init])
AS_INIT
m4_errprintn([before copyright])
AS_COPYRIGHT([CC0])
AS_WARN([warning])
AS_ECHO([Hello!])

# draws a box on stdout
AS_BOX([Test foo])

# Puts a box in the output as a comment
m4_text_box([testing foo])

foo=1
AS_IF([test "x$foo" = 'x1'],[
echo Foo is 1
])
m4_errprintn([at end])

Run autom4te:

$ autom4te -l M4sh -Wall -o test.sh test.m4sh 
before init
before copyright
at end

(Note that the m4_errprintn calls print while autom4te is running.)

Now you have test.sh:

$ ./test.sh
test.sh: WARNING: warning
Hello!
## -------- ##
## Test foo ##
## -------- ##
Foo is 1

Stunning, right? ūüėČ

The number one gotcha

If you forget AS_INIT, you will get no output and no error message.  You have been warned.

Commands used in this example

  • AS_INIT: required.¬† At the top of the input file.
  • AS_COPYRIGHT: puts the text you give it into the output as a comment at the top of the generated script file.
  • AS_WARN: prints a warning when the generated script runs
  • AS_ECHO: prints to stdout when the generated script runs
  • m4_errprintn: prints to stderr when autom4te runs.¬† No effect on the generated script.
  • AS_BOX: prints the “Test foo” box in the example output, when the generated script runs
  • m4_text_box: puts into the generated script file a comment like this one:
## ----------- ##
## testing foo ##
## ----------- ##
  • AS_IF: a shell conditional, but more portable.
  • test: the clunky, old-school (but very portable) way of checking conditionals.¬† For maximum portability, don’t let any argument to test be the empty string (hence the xs in the test command above)

Happy hacking!

Lightweight Markdown-to-PDF converter: pfft

I just released the first version of my new Markdown-to-PDF converter, pfft.  It runs on Linux, at least as far back as Ubuntu 18.04.

Why yet another converter when pandoc and many others already do the job?¬† Size.¬† Pandoc uses TeX to generate PDFs.¬† All the other converters I found use a Web browser in one way or another to make the PDF.¬† Pandoc is 50 MB by itself, not counting TeX!¬† In less than 1.2 MB (a 5.25″ floppy ūüėČ ) and a single file, pfft will do the job.

Of course, there is a catch: pfft uses Pango and Cairo to make the PDFs.  Those have their own dependencies, but are installed on many Ubuntu systems by default!  So pfft itself does not add to the dependency load those systems already carry.

(By the way, I use and appreciate both TeX and Pandoc.¬† They are great tools!¬† But, like all tools, there are some use cases they are not great at.¬† I wanted a small converter, and now I have one! ūüôā )

Please give pfft a try and let me know how it works for you!¬† And, if you’re planning to do Hacktoberfest this year, know that I am happy to receive pull requests — there’s lots of room for features and polish in pfft.¬† Check out the issues list for some ideas, and feel free to add suggestions of your own.¬† Happy hacking!

Generating Roman numerals in Vala

I have recently been learning and enjoying the Vala programming language.¬† I am writing a lightweight markdown-to-PDF converter and wanted to be able to automatically number list items in Roman numerals.¬† Here, in case anyone wants it, is Knuth’s algorithm for producing the Roman numeral for a number.¬† I converted this to Vala from the original WEB source, part of TeX, as quoted by Hans Wennborg.¬† Enjoy!

string roman(uint num)
{
    // Knuth's algorithm for Roman numerals, from TeX.  Quoted by
    // Hans Wennborg at https://www.hanshq.net/roman-numerals.html.
    // Converted to Vala by Chris White (github.com/cxw42).  CC-BY 4.0 Intl.

    var sb = new StringBuilder();

    string control = "m2d5c2l5x2v5i";
    int j, k;   // mysterious indices into `control`
    uint u, v;  // mysterious numbers
    j = 0;
    v = 1000;

    while(true) {
        while(num >= v) {
            sb.append_c(control[j]);
            num -= v;
        }
        if(num <= 0) {  // nonpositive input produces no output
            break;
        }

        k = j+2;
        u = v / control[k-1].digit_value();
        if(control[k-1] == '2') {
            k += 2;
            u /= control[k-1].digit_value();
        }

        if(num+u >= v) {
            sb.append_c(control[k]);
            num += u;
        } else {
            j += 2;
            v /= control[j-1].digit_value();
        }
    }

    return sb.str;
} // roman()

(not extensively tested ‚ÄĒ use at your own risk. No warranty. License details here.)

Word VBA: for speed, avoid Document.Paragraphs(idx)

In Word VBA, calling doc.Paragraphs(idx) to get a paragraph by its index is very slow.  Instead, once you have a Paragraph object, use para.Next and para.Previous to navigate, and your code will be much faster!  I just refactored an old routine this way and received a dramatic speedup.

I think doc.Paragraphs()¬†must be iterating through the document starting from the beginning, but I don’t know that for sure.¬† I do know that adding paragraphs before the region my code was working with made the code run perceptibly slower, even though the code was never touching the added paragraphs.

Here are some simple helpers, in case you want to manually check for errors rather than throwing when you run off the end of the document:

Private Function NextParaOrNothing_(p As Paragraph) As Paragraph
    Set NextParaOrNothing_ = Nothing
    On Error Resume Next
    Set NextParaOrNothing_ = p.Next
End Function 'NextParaOrNothing_

Private Function PrevParaOrNothing_(p As Paragraph) As Paragraph
    Set PrevParaOrNothing_ = Nothing
    On Error Resume Next
    Set PrevParaOrNothing_ = p.Previous
End Function 'PrevParaOrNothing_

 

I installed Rakudo Star

It’s the reference implementation of the Raku programming language. https://rakudo.org/

The installer ran very quickly and the REPL worked out of the box. Nothing else to report yet, but I’m sure I’ll have more to say in the future.

Happy hacking!

Updated 1: Rakudo won’t build from source on Cygwin because libuv is still a bit dicey there.¬† However,¬†cascent/neovim-cygwin¬†looks like a promising step forward.

What are the Most Disliked Programming Languages? – Stack Overflow Blog

Should I be worried?¬† The three most-disliked programming languages in¬† Stack Overflow’s latest blog post are Perl, Delphi, and VBA.¬† And those three languages all have a special place in my heart.¬† Oops…

Perl I have been enjoying recently and VBA is most of my day-to-day coding (since what they actually pay me for is driving MS Office).  Delphi remains the only piece of software I have ever bought based on one marketing presentation.  After QBASIC, I moved up to Turbo Pascal 6.  When Delphi came out, I only had to see it on screen to be hooked.

Delphi is also what taught me that the magic in a toolchain is really the linker.  As I was learning C, I was able to write Windows programs using my DOS version of Turbo C++, linked with the Delphi linker.

Excerpts from Stack Overflow’s analysis follow.

Continue reading

Some bookmarklets

Edit: For the “welcome to Stack Overflow” bookmarklet, go to this jsFiddle, hit Run, and then drag the resulting link to your bookmarks bar.

A custom tea timer for https://smithtea.com/pages/know-your-tea#brewing-instructions:

javascript:(function(){var timestr=window.prompt("Time in sec?","120"); if(timestr===null) /*Cancel*/ return; clock.stop(); clock.reset(); var t = +parseInt(timestr,10) || 0; /*https://stackoverflow.com/a/7540412/2877364*/ if(t<=0) return; clock.setTime(t); clock.start(); })();

A message I have found myself typing frequently on Stack Overflow: ūüôā

javascript:!function(){var t=document.querySelector.bind(document),o=t(".edit-post");if(o){var e=t(".js-add-link.comments-link");if(e){e.click();var a=t('textarea[name="comment"]');a&&(a.value="Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for more about asking questions that will attract quality answers. You can [edit your question]("+o.href+") to include more information.",a.scrollTop=a.scrollHeight,a.focus())}}}();

As compressed by https://jscompress.com/; non-minified is:

javascript:(function(){
 var qsel = document.querySelector.bind(document);
 var edit_link = qsel('.edit-post');
 if(!edit_link) return;
 var add_comment = qsel('.js-add-link.comments-link');
 if(!add_comment) return;
 add_comment.click();
 var textbox = qsel('textarea[name="comment"]');
 if(!textbox) return;
 textbox.value =
"Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) "+
"and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for "+
"more about asking questions that will attract quality answers. You can "+
"[edit your question](" + edit_link.href + ") to include more information."
 textbox.scrollTop = textbox.scrollHeight;
 textbox.focus();
})();

These are the first two I have written.  I  hope they are useful to you, or serve as helpful examples if you are writing your own bookmarklets!

Vim: joining lines having continuation markers

This handy file-renaming script at Perl Monks has source code with red “+” marks marking wrapped lines. I pasted it into Vim and wanted to wrap those lines back to the way they should be. After a bit of fiddling, I got:

:g/^+/execute "norm 0x" | .-1,.j!

The `g/^+/’ finds the lines beginning with a “+”. Then the `execute “norm 0x”‘ deletes the “+” (“0” moves to beginning of line; “x” deletes), “|” marks the next command, and `.-1,.j!’ joins (“j”) the current line (“.”) with the previous line (“.-1”) without whitespace (“!”) added between the lines.

I leave it to you whether I have too much time on my hands.

When programmers get it right

Technology makes our lives better to the extent we don’t have to think about it. ¬†Robert Pirsig makes this point in¬†Zen and the Art of Motorcycle Maintenance, and it’s still true: technology that draws attention to itself has failed (except to a very hard-core audience of which I am probably part ūüėČ ).

For the last two years, I have been using Google Chrome to view PDFs at work.  Dozens a day, usually with at least five open at a time.  For the last two years, every time I have rotated the view clockwise or counterclockwise, my page has shifted off-screen and I have had to scroll to get back to it.  Has that stopped me from doing my job?  No.  Has it drawn undue attention to the underlying technology?  Yes.  Very yes.

I cannot express my delight at finding,¬†after a recent Chrome update, that I wasn’t the only one with this problem ‚ÄĒ and that it has been fixed! ¬†I rotated my PDF and reached for the scrollbar, but then I noticed the underlying technology in a¬†good way: I was still looking at the same page I had been. ¬†Now each day is a bit smoother, and my job is a bit easier, because the fix has reduced, rather than increased, my mental workload.

My sincere thanks to everyone involved in pdfium bug 116297.  Special thanks to thestig at chromium.org for making the fix!  If you read this, thestig, please know you have succeeded.

Note to programmers: sometimes small fixes¬†are big wins. ¬†It took thestig¬†two lines of code to save me several minutes of wasted effort per work day, every work day, possibly for the rest of my career. ¬†Fix the small things ‚ÄĒ your users will thank you!

Why Assumptions are Important

Every piece of technology is built to work provided its assumptions hold.  Every piece of electronics, for example, assumes it will have power.  No power => no function (as famously noted by The IT Crowd).  By breaking those assumptions, you can do some amazing things:

In this video (~36 min), scanlime finds exactly the right microsecond¬†to drop the power supply — just enough! — to cause the processor built into a graphics tablet to dump its entire memory over a USB connection. ¬†This is not a function the tablet was supposed to provide!

By painstakingly, scientifically defeating the assumption of a stable power supply, the tablet’s firmware control program,¬†intended to be kept within the tablet, becomes¬†available to inspect. ¬†And, as scanlime points out at the end of the video, that control program may well open other doors. ¬†Analyzing¬†the control program may reveal¬†other assumptions the tablet makes — assumptions that can be broken to change¬†the tablet’s function to what she wants it to be.

Every system is only functional, secure, reliable, or any good at all,¬†as long as its assumptions hold. ¬†Whenever you think about the latest gizmo,¬†don’t just ask what it will do for you. ¬†Ask what it won’t do for you when you least expect it — when the gizmo¬†assumes wrong.