Skip to content

Instantly share code, notes, and snippets.

@vermiculus
Last active August 29, 2015 14:07
Show Gist options
  • Save vermiculus/46b7faf4a5936540c459 to your computer and use it in GitHub Desktop.
Save vermiculus/46b7faf4a5936540c459 to your computer and use it in GitHub Desktop.
*~
\#*\#
.\#*
dnl MACRO: M_Host
dnl
dnl Formats $1 as a hostname macro name.
dnl
define(`M_Host',`M_$1__HOSTNAME')dnl
dnl
dnl
dnl MACRO: M__DeclareHost
dnl
dnl Define M_Host($1) as a macro for $2
dnl
define(`M__DeclareHost',`define(M_Host($1),$2)')dnl
dnl
M__DeclareHost(HostA, a-host)dnl
M__DeclareHost(HostB, b-host)dnl
M__DeclareHost(HostC, c-host)dnl
M__DeclareHost(HostD, d-host)dnl
M__DeclareHost(HostE, e-host)dnl
dnl MACRO: shiftn
dnl
dnl Shift arguments by $1 steps
dnl
define(`shiftn',`ifelse($1,0,`shift($@)',`$0(decr($1),shift(shift($@)))')')dnl
dnl
dnl
dnl MACRO: select
dnl
dnl a switch statement
dnl
define(`select',`ifelse($#,1,,$#,2,`$2',
`ifelse(`$1',`$2',`$3',
`$0(`$1',shiftn(3,$@))')')')dnl
dnl
dnl
dnl MACRO: map
dnl
dnl Map macro $1 onto $2. When mapping macros that take more than
dnl one argument, group the argument sets with quotes.
dnl
define(`map',`ifelse(`$#',2,indir(`$1',$2),
`indir(`$1',$2),$0(`$1',shiftn(2, $@))')')dnl
dnl
dnl
dnl MACRO: join
dnl
dnl Join each subsequent argument with $1
dnl
define(`join',`$2`'select(`$#',1,,2,,`$1`'$0(`$1',shiftn(2, $@))')')dnl
dnl
dnl
dnl MACRO: printlist
dnl
dnl Print each argument on a new line.
dnl
define(`printlist',`join(`
',$@)')dnl
dnl
dnl
dnl MACRO: iforthenelse
dnl
dnl an `or'-ing construction
dnl
define(`iforthenelse',`select($#,1,,2,,
3,ifelse($1,$2,$3),
4,ifelse($1,$2,$3,$4),
`ifelse(`$1',`$2',
`$0($1,$2,shiftn(3, $@))',
`$0($1,shiftn(2, $@))')')')dnl
dnl
dnl
dnl MACRO: assert
dnl
dnl assert truth
dnl
define(`assert',`dnl
select($1,
!=, `ifelse($2, $3, `define(ASSERT_FAIL)')',
==, `ifelse($2, $3,, `define(ASSERT_FAIL)')',)dnl
ifdef(`ASSERT_FAIL',
`errprint(__file__:__line__`: assertion failure: ($2 $1 $3) => FALSE
')m4exit(1)')')dnl
dnl
dnl
dnl MACRO: copy
dnl
dnl Copy the definition of macro $1 to $2.
dnl
define(`copy', `define(`$2', defn(`$1'))')dnl
dnl
dnl
dnl MACRO: rename
dnl
dnl Rename macro $1 to $2
dnl
define(`rename',`copy($@)`'undefine(`$1')')dnl
include(logic.m4)dnl
dnl
dnl
dnl MACRO: M_MigrationGroupHosts
dnl
dnl Formats $1 as a migration group macro name.
dnl
define(`M_MigrationGroupHosts',`M_$1__MIGRATION_GROUP_HOSTS')dnl
dnl
dnl
dnl MACRO: M__DeclareMigrationGroup
dnl
dnl Defines M_MigrationGroupHosts($1) as a collection of hostnames
dnl M_Host($2..$n).
dnl
define(`M__DeclareMigrationGroup',`define(M_MigrationGroupHosts($1),`map(`M_Host',shift($@))')')dnl
dnl
M__DeclareMigrationGroup(group,HostA,HostC)dnl
include(logic.m4)dnl
define(`test_logic', ``$1': ifelse(`$2',`$3',Pass,Fail
``$1'': Call: "``$2''"
``$1'': Expected: "`$3'"
``$1'': Received: "`$2'")')dnl
test_logic(`printlist', printlist(1, 2, 3, 4, 5),
`1
2
3
4
5')
define(`tmp',`test $1')dnl
test_logic(`map', `map(`tmp', 1, 2, 3)', `test 1,test 2,test 3')
test_logic(`select',
select(1,
2, two,
3, three,
1, jello,
4, four,
none), `jello')
test_logic(`iforthenelse',iforthenelse(1,2,3,2,,true,false),`false')
test_logic(`iforthenelse',iforthenelse(1,2,3,1,,2,,true,false),`true')
test_logic(`join',join(` + ',1,2,3,4),`1 + 2 + 3 + 4')
rename(`test_logic', `huehue')dnl
huehue(`rename',test_logic(hi),`test_logic(hi)')
rename(`huehue', `test_logic')dnl
include(hostnames.m4)dnl
include(migration.m4)dnl
dnl
define(`print host',`Host $1 is M_Host($1)')dnl
dnl
printlist(map(`print host',HostA,HostB,HostC,HostD,HostE))
Migration group is "M_MigrationGroupHosts(group)"
Hostnames are "map(`M_Host', HostA, HostB, HostC, HostD, HostE)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment