Schol-R-LEA wrote:
I would recommend decomposing this into two functions which kprintf() would call: ksprintf() for generating the formatted string, and kputs() to print it out (depending on how you do them, you may want a kputc() and/or a memcpy() as well).
I thought about it, and changed things around so that ITextStream accepts characters instead of entire strings, and buffers them before sending them to KernelDisplay. Sort of analagous to using kputc() instead of kputs(). This has simplified the logic a lot -- thanks for suggesting it!
The logic in KOut for parsing the % format specifiers is now a neat and tidy little table-driven state machine.
i actually have gone as far as to implement a pretty much complete libc in kernel land as I want anything i could even think to use to be available. so for my case my printf is entirely ansi compliant (short of printing floats).
Wow, that must have taken a lot of effort. I had enough trouble (i.e. -- too much complex logic) just getting this limited formatting syntax working:
Is your kernel a microkernel? If not, I could see why having the full functionality could be useful. If so, do you actually use many of these features?
For example, when considering whether to include support for '-' (left-align) and '*' (width specifier) in my kprintf()-equivalent, I thought about the number of places where I would actually need to print stuff in the microkernel. The big one that leapt to mind was the panic()-related things that dump register and stack contents to the screen. That would benefit from having padding/field width support. But then again, if that is the only major usage of it, could I not just hard-code enough spaces and tabs in the format string? It's a tradeoff and I'm having trouble deciding one way or the other -- keep the extra logic in the subsystem (more code that needs to be tested, but makes printing stuff easier) versus ditching the logic (less code to test that panic() depends on, but harder to get the formatting just right). Argh... It's the trival decisions that get me stuck the most.