Commit Graph

17 Commits

Author SHA1 Message Date
Hiltjo Posthuma b6731ae35b slightly improve some comments 2022-07-20 20:37:14 +02:00
Hiltjo Posthuma ebc33326e1 Revert "rm sys/types.h include and improve portability"
This reverts commit db1dcafd03.

This is needed. Tested on an (old) Slackware 11 install.
2022-03-27 14:57:05 +02:00
Hiltjo Posthuma db1dcafd03 rm sys/types.h include and improve portability
This include is not needed. It was intended for ssize_t but this is already
defined by stdio.h for getline().
2022-03-25 23:18:58 +01:00
Hiltjo Posthuma fad48ffa27 stricter error checking in file streams (input, output)
This also makes the programs exit with a non-zero status when a read or write
error occurs.

This makes checking the exit status more reliable in scripts.

A simple example to simulate a disk with no space left:

	curl -s 'https://codemadness.org/atom.xml' | sfeed > f
	/mnt/test: write failed, file system is full
	echo $?
	0

Which now produces:

	curl -s 'https://codemadness.org/atom.xml' | sfeed > f
	/mnt/test: write failed, file system is full
	write error: <stdout>
	echo $?
	1

Tested with a small mfs on OpenBSD, fstab entry:

	swap /mnt/test mfs rw,nodev,nosuid,-s=1M 0 0
2022-03-15 14:46:46 +01:00
Hiltjo Posthuma b6f7a3fe15 improve time(NULL) error checking
Use errx, time(NULL) does not set errno. For sfeed_curses reset errno so it
doesn't print a random error if it failed.

POSIX recommends checking against (time_t)-1 on failure.

Note that some implementation, like the OpenBSD man page says time() cannot
fail.
2022-03-14 13:25:52 +01:00
Hiltjo Posthuma bb25cc51a8 sfeed_gopher: unveil: show path when it failed 2021-06-01 18:33:29 +02:00
Hiltjo Posthuma 55ac2338fc portability and standards: add BSD-like err() and errx() functions
These are BSD functions.
- HaikuOS now compiles without having to use libbsd.
- Tested on SerenityOS (for fun), which doesn't have these functions (yet).
  With a small change to support wcwidth() sfeed works on SerenityOS.
2021-06-01 18:21:08 +02:00
Hiltjo Posthuma f305b032bc util: improve/refactor URI parsing and formatting
Removed/rewritten the functions:
absuri, parseuri, and encodeuri() for percent-encoding.

The functions are now split separately with the following purpose:

- uri_format: format struct uri into a string.
- uri_hasscheme: quick check if a string is absolute or not.
- uri_makeabs: make a URI absolute using a base uri and the original URI.
- uri_parse: parse a string into a struct uri.

The following URLs are better parsed:

- URLs with extra "/"'s in the path prepended are kept as is, no "/" is added
  either for empty paths.
- URLs like "http://codemadness.org" are not changed to
  "http://codemadness.org/" anymore (paths are kept as is, unless they are
  non-empty and not start with "/").
- Paths are not percent-encoded anymore.
- URLs with userinfo field (username, password) are parsed.
  like: ftp://user:password@[2001:db8::7]:2121/rfc/rfc1808.txt
- Non-authoritive URLs like mailto:some@email.org, magnet URIs, ISBN URIs/urn,
  like: urn:isbn:0-395-36341-1 are allowed and parsed correctly.
- Both local (file:///) and non-local (file://) are supported.
- Specifying a base URL with a port will now only use it when the relative URL
  has no host and port set and follows RFC3986 5.2.2 more closely.
- Parsing numeric port: parse as signed long and check <= 0, empty port is
  allowed.
- Parsing URIs containing query, fragment, but no path separator (/) will now
  parse the component properly.

For sfeed:

- Parse the baseURI only once (no need to do it every time for making absolute
  URIs).
- If a link/enclosure is absolute already or if there is no base URL specified
  then just print the link directly. There have also been other small performance
  improvements related to handling URIs.

References:
- https://tools.ietf.org/html/rfc3986
  - Section "5.2.2. Transform References" have also been helpful.
2021-03-01 18:41:27 +01:00
Hiltjo Posthuma f18f4818ed optimize converting UNIX timestamp to localtime
Make a huge difference (cuts the time in half to process the same amount of
lines) on atleast glibc 2.30 on Void Linux.  Seems to make no difference on
OpenBSD.

- This removes atleast one heap allocation per line (checked with valgrind).
  This is because glibc will strdup() the environment variable $TZ and free it
  each time, which is pointless here and wasteful.
- localtime_r does not require to set the variables like tzname.

In glibc-2.30/time/tzset.c in __tz_convert is the following code and comment:

  /* Update internal database according to current TZ setting.
     POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
     This is a good idea since this allows at least a bit more parallelism.  */
  tzset_internal (tp == &_tmbuf && use_localtime);

This makes it always tzset() and inspect the environment $TZ etc. While with
localtime_r it will only initialize it once:

	static void tzset_internal (int always) {
	[...]
	if (is_initialized && !always)
		return;
2021-01-10 18:49:21 +01:00
Hiltjo Posthuma c7e3ec5f37 sfeed_gopher: optimize common output character function
Same reason as the previous commit (allow to expand to macros).
2021-01-08 12:05:14 +01:00
Hiltjo Posthuma aec6b5c35a sfeed_gopher: tighten filesystem permissions on OpenBSD using unveil(2)
sfeed_gopher must be able to write in the current directory, but does not need
write permissions outside it. It could read from any place in the filesystem
(to read feed files).

Prompted by a suggestion from vejetaryenvampir, thanks!
2021-01-01 22:59:14 +01:00
Hiltjo Posthuma 276d5789fd format tools: don't skip items with a missing/invalid timestamp field
Handle it appropriately in the context of each format tool. Output the item but
keep it blanked.

NOTE: maybe in sfeed_twtxt it should use the current time instead?
2020-07-05 15:57:02 +02:00
Hiltjo Posthuma 414b896c2d sfeed_gopher: if a gopher url cannot be parsed then show it anyway as a "URL:"
This should never be able to happen though in practise because sfeed parses the
uri aswell.
2020-05-13 19:27:14 +02:00
Hiltjo Posthuma f1e17c612e sfeed_gopher: do not use URL: prefix for gopher:// urls.
Support the Gopher protocol directly and use the specified Gopher type.

Idea by adc, thanks!
2020-05-13 19:06:41 +02:00
Hiltjo Posthuma 7c99000c20 util: improve/cleanup parseline()
- remove a check that has no use/can never happen.
- remove the return value as it's unused and the input size is known.
- fix an old comment that doesn't reflect what the function does anymore.
2020-04-01 18:49:24 +02:00
Hiltjo Posthuma d4c8c60b38 cleanup some includes 2020-01-24 02:14:57 +01:00
Hiltjo Posthuma 009f6e1aff add sfeed_gopher: generic gopher formatting program, remove sfeed_gph 2020-01-18 20:09:42 +01:00