Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# $NetBSD: tzdata2netbsd,v 1.12 2018/03/24 01:54:48 kre Exp $

# For use by NetBSD developers when updating to new versions of tzdata.
#
# 0. Be in an up-to-date checkout of src/external/public-domain/tz
#    from NetBSD-current.
# 1. Make sure that you have Paul Eggert's 4K RSA public key in your
#    keyring (62AA7E34, eggert@cs.ucla.edu)  It is not required that it be trusted.
# 2. Run this script.  You will be prompted for confirmation before
#    anything major (such as a cvs operation).  The tz versions can be
#    specified as args (new version first, and the previous second) if
#    needed to override the calculated values
# 3. If something fails, abort the script and fix it.
# 4. Re-run this script until you are happy.  It's designed to
#    be re-run over and over, and later runs will try not to
#    redo non-trivial work done by earlier runs.
#

VERS_PATTERN='2[0-9][0-9][0-9][a-z]'
# This needs to be updated twice every millennium to allow for the
# new millenium's years.
# First in the late xx90's sometime, allow the new one by changing the leading
# digit from a specific value to the class containing the current and
# following values (eg: in 2098 or so, change '2' to be '[23]').
# Tnen in the following early xx00's sometime, delete the class, and insert
# leave only the current value as valid (eg: in 3001 or 3002,
# change '[23]' to be just '3'
# Doing it this way helps guard against invalid specifications.
# We could automate this, but it is (IMO) not worth the cost, to avoid a
# twice a millenium edit requirement.
# A more significant (and harder) change will be needed in the late 9990's
# If this script lasts until then, send me a postcard, I'll be waiting for it!
# Things get easier again after that until the late 99990's (etc.)

# Note the pattern is used on both the old and new version specifiers,
# so it must be able to cope with the shift from one form (eg 2999g)
# to the new one (eg: 3000a) without failing (or the code that uses it
# below needs to be updated).

# Also note that the option of having a second alpha (1997aa or something)
# to handle years with much activity is handled below, the pattern does not
# need to match those.
# If that convention changes (as of date of writing, it has never been
# exercised) then code changes below will be required.

DIST_HOST=ftp.iana.org
DIST_PATH=tz
DIST_FILES=releases

EDITOR=${EDITOR:-vi}
WORK_PFX=$(pwd)/update-work || fail "Cannot obtain PWD"
UPDATE_FROM=${WORK_PFX}/updating.from.version

usage()
{
	printf >&2 '%s\n'						\
		"Usage: $0 [new-version-id [old-version-id]]"		\
		"     where a version-id is of the form YYYYx (eg: 2018c)" \
		"     or '' for new-version-id (to specify only the old)"
	exit 2
}

fail()
{
	local IFS=' '

	printf >&2 '%s\n'	"Error detected:" "   $*" "Aborting."
	exit 1
}

valid_vers()
{
	case "$2" in
	${VERS_PATTERN} | ${VERS_PATTERN}[a-z] )
		;;
	*)	printf >&2 '%s: %s\n' \
		    "Bad form for $1 version specifier '$2'" \
		    "should (usually) be 'YYYYx'"
		return 1
		;;
	esac
	return 0
}

get_curvers()
{
	local LF=''
	local LIST=iana-listing
	local SED_SCRIPT='
		/tzdata-latest.*-> /{
			s/^.*-> //
			s/\..*$//
			s;^releases/tzdata;;p
			q
		}
		d'

	test -d "${WORK_PFX}" &&
		test -s "${WORK_PFX}/${LIST}" &&
		test "${WORK_PFX}/${LIST}" -nt dist/CVS &&
		LF=$(find "${WORK_PFX}" -name "${LIST}" -mtime -1 -print) &&
		test -n "${LF}" &&
		NEWVER=$(sed -n < "${LF}" "${SED_SCRIPT}") &&
		valid_vers new "${NEWVER}"				||

	ftp >/dev/null 2>&1 -ia "${DIST_HOST}" <<- EOF &&
					dir ${DIST_PATH} ${WORK_PFX}/${LIST}
					quit
					EOF
		test -s "${WORK_PFX}/${LIST}" &&
		NEWVER=$(sed -n < "${WORK_PFX}/${LIST}" "${SED_SCRIPT}") &&
		valid_vers new "${NEWVER}"				||

	{
		rm -f "${WORK_PFX}/${LIST}"
		fail "Cannot fetch current tzdata version from ${DIST_HOST}"
	}

	printf '%s\n' "Updating from ${OLDVER} to ${NEWVER}"
}

argparse()
{
	local OVF

	if OVF=$(find "${WORK_PFX}" -name "${UPDATE_FROM##*/}" -mtime +2 -print)
	then
		# delete anything old
		test -n "${OVF}" && rm -f "${OVF}"
	fi

	case "$#" in
	0|1)
		# once we have obtained OLDVER once, never guess it again.
		test -f "${UPDATE_FROM}" && OLDVER=$(cat "${UPDATE_FROM}") ||

		OLDVER=$(cat dist/version) || {
			printf >&2 '%s\n'  \
			    "Cannot determine current installed version"  \
			    "Specify it on the command line."  \
			    ""
			usage
		}

		valid_vers old "${OLDVER}" ||
			fail "Calculated bad OLDVER, give as 2nd arg"
		;;

	2)	valid_vers old "$2" && OLDVER="$2" || usage
		;;

	*)	usage
		;;
	esac

	case "$#:$1" in
	0: | 1: | 2: )
		;;
	1:?*|2:?*)	
		valid_vers new "$1" && NEWVER="$1" || usage
		;;
	*)	usage
		;;
	esac

	test -z "${NEWVER}" && get_curvers

	test "${NEWVER}" = "${OLDVER}" && {
		printf '%s\n' \
		     "New and old versions both ${NEWVER}: nothing to do"
		exit 0

	}

	printf '%s\n' "${OLDVER}" > "${UPDATE_FROM}" ||
	    fail "Unable to preserve old version ${OLDVER} in ${UPDATE_FROM}"

	test "${#NEWVER}" -gt "${#OLDVER}" ||
		test "${NEWVER}" '>' "${OLDVER}" ||
		{
			local reply

			printf '%s\n' \
			    "Update would revert ${OLDVER} to ${NEWVER}"
			read -p "Is reversion intended? " reply
			case "${reply}" in
			[Yy]*)	;;
			*)	printf '%s\n' OK. Aborted.
				rm -f "${UPDATE_FROM}"
				exit 1
				;;
			esac
		}

	return 0
}

setup_versions()
{
	# Uppercase variants of OLDVER and NEWVER
	OLDVER_UC="$( echo "${OLDVER}" | tr '[a-z]' '[A-Z]' )"
	NEWVER_UC="$( echo "${NEWVER}" | tr '[a-z]' '[A-Z]' )"

	# Tags for use with version control systems
	CVSOLDTAG="TZDATA${OLDVER_UC}"
	CVSNEWTAG="TZDATA${NEWVER_UC}"
	CVSBRANCHTAG="TZDATA"
	GITHUBTAG="${NEWVER}"

	# URLs for fetching distribution files, etc.
	DISTURL="ftp://${DIST_HOST}/${DIST_PATH}/${DIST_FILES}"
	DISTURL="${DISTURL}/tzdata${NEWVER}.tar.gz"
	SIGURL="${DISTURL}.asc"
	NEWSURL="https://github.com/eggert/tz/raw/${GITHUBTAG}/NEWS"

	# Directories
	REPODIR="src/external/public-domain/tz/dist"
				# relative to the NetBSD CVS repo
	TZDISTDIR="$(pwd)/dist" # should be .../external/public-domain/tz/dist
	WORKDIR="${WORK_PFX}/${NEWVER}"
	EXTRACTDIR="${WORKDIR}/extract"

	# Files in the work directory
	DISTFILE="${WORKDIR}/${DISTURL##*/}"
	SIGFILE="${DISTFILE}.asc"
	PGPVERIFYLOG="${WORKDIR}/pgpverify.log"
	NEWSFILE="${WORKDIR}/NEWS"
	NEWSTRIMFILE="${WORKDIR}/NEWS.trimmed"
	IMPORTMSGFILE="${WORKDIR}/import.msg"
	IMPORTDONEFILE="${WORKDIR}/import.done"
	MERGSMSGFILE="${WORKDIR}/merge.msg"
	MERGEDONEFILE="${WORKDIR}/merge.done"
	COMMITMERGEDONEFILE="${WORKDIR}/commitmerge.done"

	printf '%s\n' "${CVSOLDTAG}"  > "${WORK_PFX}/updating_from"
}

DOIT()
{
	local really_do_it=false
	local reply

	echo "In directory $(pwd)"
	echo "ABOUT TO DO:" "$(shell_quote "$@")"
	read -p "Really do it? [yes/no/quit] " reply
	case "${reply}" in
	[yY]*)	really_do_it=true ;;
	[nN]*)	really_do_it=false ;;
	[qQ]*)
		echo "Aborting"
		return 1
		;;
	*)	echo "Huh?"
		return 1
		;;
	esac
	if $really_do_it; then
		echo "REALLY DOING IT NOW..."
		"$@"
	else
		echo "NOT REALLY DOING THE ABOVE COMMAND"
	fi
}

# Quote args to make them safe in the shell.
# Usage: quotedlist="$(shell_quote args...)"
#
# After building up a quoted list, use it by evaling it inside
# double quotes, like this:
#    eval "set -- $quotedlist"
# or like this:
#    eval "\$command $quotedlist \$filename"
#
shell_quote()
(
	local result=''
	local arg qarg
	LC_COLLATE=C ; export LC_COLLATE # so [a-zA-Z0-9] works in ASCII
	for arg in "$@" ; do
		case "${arg}" in
		'')
			qarg="''"
			;;
		*[!-./a-zA-Z0-9]*)
			# Convert each embedded ' to '\'',
			# then insert ' at the beginning of the first line,
			# and append ' at the end of the last line.
			# Finally, elide unnecessary '' pairs at the
			# beginning and end of the result and as part of
			# '\'''\'' sequences that result from multiple
			# adjacent quotes in he input.
			qarg="$(printf '%s\n' "$arg" | \
			    ${SED:-sed} -e "s/'/'\\\\''/g" \
				-e "1s/^/'/" -e "\$s/\$/'/" \
				-e "1s/^''//" -e "\$s/''\$//" \
				-e "s/'''/'/g"
				)"
			;;
		*)
			# Arg is not the empty string, and does not contain
			# any unsafe characters.  Leave it unchanged for
			# readability.
			qarg="${arg}"
			;;
		esac
		result="${result}${result:+ }${qarg}"
	done
	printf '%s\n' "$result"
)

validate_pwd()
{
	local P="$(pwd)" || return 1

	test -d "${P}" &&
	    test -d "${P}/CVS" &&
	    test -d "${P}/dist" &&
	    test -f "${P}/dist/zone.tab" &&
	    test -f "${P}/tzdata2netbsd" || {
		printf >&2 '%s\n' "Please change to the correct directory"
		return 1
	}
	
	test -f "${P}/CVS/Tag" && {

		# Here (for local use only) if needed for private branch work
		# insert tests for the value of $(cat "${P}/CVS/Tag") and
		# allow your private branch tag to pass. Eg:

		#	case "$(cat "${P}/CVS/Tag")" in
		#	my-branch-name)	return 0;;
		#	esac

		# Do not commit a version of this script modified that way,
		# (not even on the private branch) - keep it as a local
		# modified file.  (This script will not commit it.)

		printf >&2 '%s\n' \
		    "This script should be run in a checkout of HEAD only"
		return 1
	}

	return 0
}

findcvsroot()
{
	[ -n "${CVSROOT}" ] && return 0
	CVSROOT="$( cat ./CVS/Root )"
	[ -n "${CVSROOT}" ] && return 0
	echo >&2 "Failed to set CVSROOT value"
	return 1
}

mkworkpfx()
{
	mkdir -p "${WORK_PFX}" || fail "Unable to make missing ${WORK_PFX}"
}
mkworkdir()
{
	mkdir -p "${WORKDIR}" || fail "Unable to make missing ${WORKDIR}"
}

fetch()
{
	[ -f "${DISTFILE}" ] || ftp -o "${DISTFILE}" "${DISTURL}" ||
		fail "fetch of ${DISTFILE} failed"
	[ -f "${SIGFILE}" ] || ftp -o "${SIGFILE}" "${SIGURL}" ||
		fail "fetch of ${SIGFILE} failed"
	[ -f "${NEWSFILE}" ] || ftp -o "${NEWSFILE}" "${NEWSURL}" ||
		fail "fetch of ${NEWSFILE} failed"
}

checksig()
{
	{ gpg --verify "${SIGFILE}" "${DISTFILE}"
	  echo gpg exit status $?
	} 2>&1 | tee "${PGPVERIFYLOG}"

	# The output should contain lines that match all the following regexps
	#
	while read line; do
		if ! grep -E -q -e "^${line}\$" "${PGPVERIFYLOG}"; then
			echo >&2 "Failed to verify signature: ${line}"
			return 1
		fi
	done <<'EOF'
gpg: Signature made .* using RSA key ID (62AA7E34|44AD418C)
gpg: Good signature from "Paul Eggert <eggert@cs.ucla.edu>"
gpg exit status 0
EOF
}

extract()
{
	[ -f "${EXTRACTDIR}/zone.tab" ] && return
	mkdir -p "${EXTRACTDIR}"
	tar -z -xf "${DISTFILE}" -C "${EXTRACTDIR}"
}

addnews()
{
	[ -f "${EXTRACTDIR}/NEWS" ] && return
	cp -p "${NEWSFILE}" "${EXTRACTDIR}"/NEWS
}

# Find the relevant part of the NEWS file for all releases between
# OLDVER and NEWVER, and save them to NEWSTRIMFILE.
#
trimnews()
{
	[ -s "${NEWSTRIMFILE}" ] && return
	awk -v oldver="${OLDVER}" -v newver="${NEWVER}" \
	    '
		BEGIN {inrange = 0}
		/^Release [0-9]+[a-z]+ - .*/ {
			# "Release <version> - <date>"
			# Note: must handle transition from 2018z to 2018aa
			# Assumptions: OLDVER and NEWVER have been sanitized,
			# and format of NEWS file does not alter (and
			# contains valid data)
			inrange = ((length($2) > length(oldver) || \
					$2 > oldver) && \
				(length($2) < newver || $2 <= newver))
		}
		// { if (inrange) print; }
	    ' \
		<"${NEWSFILE}" >"${NEWSTRIMFILE}"
	echo "tzdata-${NEWVER}" > ${TZDISTDIR}/TZDATA_VERSION
}

# Create IMPORTMSGFILE from NEWSTRIMFILE, by ignoring some sections,
# keeping only the first sentence from paragraphs in other sections,
# and changing the format.
#
# The result should be edited by hand before performing a cvs commit.
# A message to that effect is inserted at the beginning of the file.
#
mkimportmsg()
{
	[ -s "${IMPORTMSGFILE}" ] && return
	{ cat <<EOF
EDIT ME: Edit this file and then delete the lines marked "EDIT ME".
EDIT ME: This file will be used as a log message for the "cvs commit" that
EDIT ME: imports tzdata${NEWVER}.  The initial contents of this file were
EDIT ME: generated from ${NEWSFILE}.
EDIT ME: 
EOF
	awk -v oldver="${OLDVER}" -v newver="${NEWVER}" \
	    -v disturl="${DISTURL}" -v newsurl="${NEWSURL}" \
	    '
		BEGIN {
			bullet = "  * ";
			indent = "    ";
			blankline = 0;
			goodsection = 0;
			havesentence = 0;
			print "Import tzdata"newver" from "disturl;
			#print "and NEWS file from "newsurl;
		}
		/^Release/ {
			# "Release <version> - <date>"
			ver = $2;
			date = gensub(".* - ", "", 1, $0);
			print "";
			print "Summary of changes in tzdata"ver \
				" ("date"):";
		}
		/^$/ { blankline = 1; havesentence = 0; }
		/^  Changes affecting/ { goodsection = 0; }
		/^  Changes affecting.*time/ { goodsection = 1; }
		/^  Changes affecting.*data/ { goodsection = 1; }
		/^  Changes affecting.*documentation/ || \
		/^  Changes affecting.*commentary/ {
			t = gensub("^ *", "", 1, $0);
			t = gensub("\\.*$", ".", 1, t);
			print bullet t;
			goodsection = 0;
		}
		/^    .*/ && goodsection {
			# In a paragraph in a "good" section.
			# Ignore leading spaces, and ignore anything
			# after the first sentence.
			# First line of paragraph gets a bullet.
			t = gensub("^ *", "", 1, $0);
			t = gensub("\\. .*", ".", 1, t);
			if (blankline) print bullet t;
			else if (! havesentence) print indent t;
			havesentence = (havesentence || (t ~ "\\.$"));
		}
		/./ { blankline = 0; }
	    ' \
		<"${NEWSTRIMFILE}"
	} >"${IMPORTMSGFILE}"
}

editimportmsg()
{
	if [ -s "${IMPORTMSGFILE}" ] \
	&& ! grep -q '^EDIT' "${IMPORTMSGFILE}"
	then
		return 0 # file has already been edited
	fi
	# Pass both IMPORTMSGFILE and NEWSFILE to the editor, so that the
	# user can easily consult NEWSFILE while editing IMPORTMSGFILE.
	${EDITOR} "${IMPORTMSGFILE}" "${NEWSFILE}"
}

cvsimport()
{
	if [ -e "${IMPORTDONEFILE}" ]; then
		cat >&2 <<EOF
The CVS import has already been performed.
EOF
		return 0
	fi
	if ! [ -s "${IMPORTMSGFILE}" ] \
	|| grep -q '^EDIT' "${IMPORTMSGFILE}"
	then
		cat >&2 <<EOF
The message file ${IMPORTMSGFILE}
has not been properly edited.
Not performing cvs import.
EOF
		return 1
	fi
	( cd "${EXTRACTDIR}" &&
	  DOIT cvs -d "${CVSROOT}" import -m "$(cat "${IMPORTMSGFILE}")" \
		"${REPODIR}" "${CVSBRANCHTAG}" "${CVSNEWTAG}"
	) && touch "${IMPORTDONEFILE}"
}

cvsmerge()
{

	cd "${TZDISTDIR}" || exit 1
	if [ -e "${MERGEDONEFILE}" ]; then
		cat >&2 <<EOF
The CVS merge has already been performed.
EOF
		return 0
	fi
	DOIT cvs -d "${CVSROOT}" update -j"${CVSOLDTAG}" -j"${CVSNEWTAG}" \
	&& touch "${MERGEDONEFILE}"
}

resolveconflicts()
{
	cd "${TZDISTDIR}" || exit 1
	if grep -l '^[<=>][<=>][<=>]' *
	then
		cat <<EOF
There appear to be conflicts in the files listed above.
Resolve conflicts, then re-run this script.
EOF
		return 1
	fi
}

cvscommitmerge()
{
	cd "${TZDISTDIR}" || exit 1
	if grep -l '^[<=>][<=>][<=>]' *
	then
		cat >&2 <<EOF
There still appear to be conflicts in the files listed above.
Not performing cvs commit.
EOF
		return 1
	fi
	if [ -e "${COMMITMERGEDONEFILE}" ]; then
		cat >&2 <<EOF
The CVS commmit (of the merge result) has already been performed.
EOF
		return 0
	fi
	DOIT cvs -d "${CVSROOT}" commit -m "Merge tzdata${NEWVER}" \
	&& touch "${COMMITMERGEDONEFILE}"
}

extra()
{
	cat <<EOF
Also do the following:
 * Edit and commit  src/doc/3RDPARTY
 * Edit and commit  src/doc/CHANGES
 * Edit and commit  src/distrib/sets/lists/base/mi
 *      if the set of installed files altered.
 * Submit pullup requests for all active release branches.
 * rm -rf ${WORK_PFX}  (optional)
 * Verify that
  ${UPDATE_FROM}
 * no longer exists.
EOF
}

main()
{
	set -e
	validate_pwd
	findcvsroot
	mkworkpfx
	argparse "$@"
	setup_versions
	mkworkdir
	fetch
	checksig
	extract
	addnews
	trimnews
	mkimportmsg
	editimportmsg
	cvsimport
	cvsmerge
	resolveconflicts
	cvscommitmerge
	rm -f "${UPDATE_FROM}"
	extra
}

main "$@"