toybox cleanup

# Index

- Introduction
- Advice
- Commands:

- uuencode
- uudecode
- ifconfig
- stat

# Cleaning up the toybox code.

Toybox hasn't always taken proper advantage of external contributions. Lots of people want to help, but their contributions languish out of tree or in the "pending" directory, awaiting cleanup.

Toybox's design goals require simpler, tighter, and more explicit code than most other implementations, among other reasons to allow better security auditing. Writing "another" implementation of standard command line tools isn't very interesting, they should be _better_ implementations. Unfortunately, this means existing, working commands often take more effort to clean up to Toybox's standards than writing a new one from scratch, not because they don't work, but because we aim for an unusual level of polish.

In hopes of teaching more people how to do this cleanup work, I've started breaking cleanup changes into smaller chunks and posting explanations of each change to the mailing list. Below are indexes of such cleanup series. Each commit and post are meant to be read together: each description explains what the corresponding patch was trying to accomplish.

Line/byte totals of completed series are given for scale, but the point of this work is simplicity and compactness, not size per se.

(Note: mercurial's web viewer doesn't follow renames, so although each command name links to a commit list with the bulk of the changes, it may not include the final version of each file moved from the "pending" directory to its final location. The summaries link the initial and cleaned versions of each file when giving line counts.)

# General advice and/or policy.

The design of toybox page and the coding style section at the start of the source code walkthrough don't cover everything. Here are some links to mailing list messages that cover various programming topics not directly related to a specific cleanup series:

- Error messages and internationalization.
- Why not "const"? (Exception: global variables outside of GLOBALS can be static const, to go in rodata instead of data. This means the pages can be shared between instances.)
- Why not "bool"? (explanation from Linus Torvalds)
- Why not to check in debug code.
- Relationship between /proc and /sys (/proc isn't obsolete and /sys is an ABI)
- "Hiding numbers that are used just once into defines to put them out of sight does not really help readability." - Pavel Machek
- "Infrastructure in search of a user" is a bad thing, so don't put code in lib/ that doesn't already have at least two users. Don't preemptively factor stuff out, it's easy enough to rewrite it uin future if it needs to change. The "extreme programming" fad called this "You Ain't Gonna Need It" (while inexplicably screaming at cans of Mountain Dew, back in the 90's). Here's Linus Torvalds' take.

# uuencode

This is an example of cleaning up something most projects would be quite happy with. The initial submission of uuencode and uudecode was high quality code, written by a seasoned developer who did an excellent job, but it was still possible to shrink the result almost by half:

- old total: 116 lines (2743 bytes) in 7 functions
- new total: 67 lines (1440 bytes) in 1 function

- commit: 830: first pass, description: part 1, part 2
- commit: 831, second pass, description: part 3
- commit: 837, description: fix test suite.
- commit: 853, description: bugfix.

Status: COMPLETE

# uudecode

The uudecode cleanup was my second "explain as I go along" cleanup, and I tried to do it in smaller stages so it was easier to see what changed from the diff:

- old: 175 lines (4534 bytes) in 9 functions
- new: 107 lines (2300 bytes) in 1 function

- commit: 833, description: preparatory adjustments to test suite.
- commit: 835, description: Redo command line parsing, redo parsing loop.
- commit: 838, description: Redo b64_1byte, b64_4bytes, and uu_line()
- commit: 839, description: todo

- commit: 840, description: todo (finish, move pending->posix, default y)

Status: COMPLETE

# ifconfig

This series describes a thorough cleanup that took a while to do.

When ifconfig was submitted, it touched a half-dozen files. I glued it together into a single self-contained file, which needed a lot of cleanup. The final version is about 1/3 the size of the original.

- old total: 1504 lines (44268 bytes) in 38 functions
- new total: 521 lines (15963 bytes) in 4 functions

This was the first command I started cleaning up with the intent of describing the process, and especially the first few entries in this series describe many of the low hanging fruit techniques used to clean up a codebase.

- commit: 843, description: Infrastructure in search of a user, code proximity, unnecessary #defines, shorter code is easier to read.
- commit: 844, description: Headers, replacing repeated code with loops, logical operators guaranteed to return 0 or 1, math on string constants, removing unnecessary variables and tests.
- commit: 852, description: hg commit numbers, documenting the obvious, ordering to avoid prototypes, returning void, collate local declarations, use error_exit(), unnecessary parentheses, inline to remove variables/functions used only once, using *var instead of var[0], unnecessary typecasts, xprintf("\n") vs xputc('\n')
- commit: 856, description: one line portability fix from Isaac Dunham
- commit: 861 and 863, description: Help infrastructure cleanup from Isaac Dunham (which I mis-applied and then fixed plus some whitespace changes)
- commit: 862, description: remove unused headers and function, replace local buffer with toybuf, perror_exit(), avoid unnecessary assignment.
- commit: 864, description: use common linked list functions, inline set_data, add xioctl, clean up error messages, whitespace and comment tweaks, remove NOP return statements
- commit: 866, description: move standalone globals into GLOBALS() block, collate structs into iface_list. Inline/rewrite/remove field_format, iface_flags_str, omit_whitespace(), print_iface_flags(), print_media(), get_ifconfig_info(), and clear_list(). Merge duplicate function calls. Show get_proc_info() version field can't matter in 2.6 or newer kernel, and that SIOCGIFMAP has been there since 1994 so doesn't need an #ifdef. Loop simplification in readconf() and show_iface().
- commit: 869 and 870, description: 869: reorder to eliminate prototypes, put command_main() at end of file, 870: long repeated variable prefixes, replacing struct+sscanf()+printf with a loop and a table (from iface_list, get_proc_info(), display_ifconfig()), use lib/xwrap.c functions to return void, why xstrcpy() fails closed, (functional comment: why multicast failed, CSLIP obsolecense), not being _too_ clever.
- commit: 878 and 879: description: 878: add xsocket(), free(NULL) is a safe NOP (posix!). 879: inline three functions and simplify, some simplifications only show up after repeated inlining
- commit: 883, description: move some common code to lib/ and posix headers to toys.h.
- commit: 898, description: Argument parsing. (Replace ifconfig_main() if/else staircase with a loop over an array, genericize minus prefix logic, inline a use of set_flags().)
- commit: 905, description: remove unnecessary wrapper function, inlining more functions, relying on the values of constants that don't change across architectures (binary backwards compatability), more ifconfig_main table work, man ioctl_list.
- commit: 906, description: More ifconfig_main() table work, remove vestigial arguments to functions, "a frenzy of inlining", slightly better error reporting, don't reinvent libc functions.
- commit: 907, inlining show_ip_addr() with a loop and a table, inlining hex_to_binary() and set_hw_address(), stop validating data we supplied, remove a table that's overkill (two entries, we don't even loop over it), when you don't need a NULL terminator for array, remove unnecessary memcpy(offsetof()) with assignment, trusting -funsigned-char.
- commit: 919, todo whitespace damage, introduce IFREQ_OFFSZ() and poke() to ifconfig_main() table logic to fold more if/else parts into the table
- Status update: Entering the home stretch on ifconfig (and a note about infiniband)
- commit: 921, description: Inline a couple more functions and make sockfd a global.
- commit: 957, description: Remove unused socklen and addr_to_len(), cleanup so we can merge get_device_info()/display_ifconfig().
- commit: 958, description: This commit removes struct if_list by unifying get_device_info() and display_ifconfig().
- commit: 1104, description: Merge toynet into toys.h: musl supports it and micromanaging uClibc config options isn't very interesting anymore.
- commit: 1127, description: Start tacling ipv6 issues, beginning with display_ifconfig().
- commit: 1128, description: More ipv6, make struct sockaddr_with_len go away, merge more arguments into the table in main().
- commit: 1132, description: promotion from pending to other
- commit: 1133, description: cleanup help text, remove obsolete/NOP commands

Status: COMPLETE.

Status: in progress.

# stat

A lot of the stat cleanup was done by Felix Janda.

- commit 747: initial submission

- commit 810: whitespace
- commit 811: description in commit message.
- commit 871: whitespace (reindent from 4 spaces to 2)
- commit 872: Felix Janda - cleanup (with discussion thread)
- commit 885: Felix Janda - move permission formatting (777 -> -rwxrwxrwx) from ls to lib so stat can reuse it.
- commit 886: Felix Janda - remove unimplemented options and clean up help text
- commit 910: Felix Janda - Add support for stating multiple files.
- commit 911: Felix Janda - Separate stat and statfs, give stat_main() a ds[2] array to distinguish FLAG_f vs not cases, and some whitespace changes.
- commit 912: description in commit message (also here)
- design pondering (leading to peek() function in lib/)
- commit 914: description in commit message.
- commit 916: description in commit message.
- commit: 917: description in commit message.
- commit: 918, description: done: move pending to posix, default y, no other changes
.
- summary

Status: COMPLETE.