Merge pull request #63 from krono/master
[ach-master.git] / src / common / configfiles.tex
1 %%%
2 %%% Applied Crypto Hardening
3 %%% bettercrypto.org
4 %%%
5 %%% configfiles.tex -- macros for automatically inserting config file parts
6 %%%
7 %%%
8
9 %%%
10 %%    I need dis
11 %%%
12 \RequirePackage{etoolbox}
13
14 % for convenienc
15 \newwrite\configfileout
16 \immediate\openout\configfileout configfiles.txt
17
18 % do not trace everything
19 \newif\iftracingsections
20 \tracingsectionsfalse
21 \newcommand*\currentsectionname{}%
22 \newcommand*\currentsubsectionname{}%
23
24 \makeatletter
25 \def\@configurationdir{configuration}
26 \newcommand*\configurationdir[1]{%
27   \gdef\@configurationdir{#1}}
28 \def\@configurationbaseurl{}
29 \newcommand*\configurationbaseurl[1]{%
30   \gdef\@configurationbaseurl{#1}}
31
32 % patch the section command to record information for our config files
33
34 \pretocmd{\@sect}{%
35   \iftracingsections%
36     \def\sectn@me{section}%
37     \def\subsectn@me{subsection}%
38     \def\tempa{#1}%
39     \edef\tempn@me{#8}%
40     \ifx\tempa\sectn@me\relax%
41       \expandafter\xdef\expandafter\currentsectionname{\tempn@me}%
42     \else\relax\fi%
43     \ifx\tempa\subsectn@me\relax%
44       \expandafter\xdef\expandafter\currentsubsectionname%
45         {\tempn@me}\else\relax\fi%
46   \fi
47 }{}{}
48
49 %
50 %
51 %  \configfile[args]{filename}{range}
52 %  --> \lstinputlisting[linerange={range},args]{configurations/Section/Subsection/filename}
53 %
54 \newcommand*\configfile[4][]{%
55   \iftracingsections%
56     \edef\configfilen@me{\@configurationdir/\currentsectionname/\currentsubsectionname/#2}
57     \edef\configfilename{\expandafter\detokenize\expandafter{\configfilen@me}}
58     \def\configfilepres{\expandafter\protect\expandafter\path\expandafter{\configfilename}}
59     \immediate\write\configfileout{\configfilename}
60     \lstinputlisting[%
61       linerange={#3},%
62       autounindent,%
63       frame=single,%
64       caption={[#4\\\configfilepres]%
65           #4\\\mbox{\protect\href{\@configurationbaseurl\configfilename}{%
66               {\upshape [\configfilepres]}%
67         }}}
68       #1]{\configfilename}%
69     \marginpar{\vspace*{-2\baselineskip}\protect\href{\@configurationbaseurl\configfilename}{%
70         \includegraphics[height=2\baselineskip]{img/ConfigIcns}}}
71   \else%
72     \@warning{\string\configfile outside a sectiontraced region}%
73   \fi%
74 }
75
76 %
77 % start tracing section commands to record (sub)section names
78 \newcommand*\startconfigfiles{%
79   \tracingsectionstrue%
80 }
81
82 %
83 % stop tracing section commands
84 \newcommand*\stopconfigfiles{%
85   \tracingsectionsfalse%
86   \renewcommand*\currentsectionname{%
87     \@warning{\string\currentsectionname outside sectiontraced region}}%
88   \renewcommand*\currentsubsectionname{%
89     \@warning{\string\currentsubsectionname outside sectiontraced region}}%
90 }%
91
92 % ok, we're done here
93 \makeatother
94
95 %%% Local Variables:
96 %%% mode: latex
97 %%% TeX-master: "../applied-crypto-hardening"
98 %%% End: