summaryrefslogtreecommitdiff
path: root/tex/context/base/core-ctx.tex
blob: 22f2ded4ad60cd32e788d1c6ac7d935cb5cfcb2e (plain)
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
%D \module
%D   [       file=core-ctx,
%D        version=2006.08.16, % old stuff
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Job Control,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{Context Core Macros / Ctx Job Files}

%D After some experimenting this code moved into the core. It
%D overloades a few file reading macros and permits runtime
%D conversion and job control.

% todo: rewrite in lua

\unprotect

\chardef\preprocessmethod 2 % 0=no check 1=present_check 2=log_check
\chardef\preprocessstate  0 % 1=found 2=not_present (skip)
\chardef\preprocesslocal  0 % 0=no 1=yes
\def    \preprocesssuffix {.prep}

\def\loadctxpreplist
  {\begingroup
   \chardef\XMLtokensreduction\plusone
   \ifcase\preprocessmethod
     % no checking
   \or
     % simple checking
   \or
     \doiffileexistselse{./\jobname.ctl}
       {\bgroup
        \defineXMLenvironment[ctx:preplist][local=no]
          {\doifelse{\XMLop{local}}{yes}
              {\global\chardef\preprocesslocal\plusone
               \writestatus\m!systems{loading ctx log file (local)}}%
              {\writestatus\m!systems{loading ctx log file (specified)}}}
          \donothing
        \defineXMLenvironmentsave[ctx:prepfile][done=no]
          \donothing
          {\edef\readfilename{\XMLflush{ctx:prepfile}}%
           \ifcase\preprocesslocal\else
              \splitfilename\readfilename
              \let\readfilename\splitoffbase
           \fi
           \writestatus\m!systems{registering \readfilename\space -> \XMLop{done}}%
           \setxvalue{fp..\readfilename}{\XMLop{done}}}%
        \processXMLfile{./\jobname.ctl}%
        \egroup}%
       {\global\chardef\preprocessmode\plusone}%
   \fi
   \global\let\loadctxpreplist\relax
   \endgroup}

\appendtoks\loadctxpreplist\to\everystarttext % will become: \prependtoks\loadctxpreplist\to\everyjob

\def\docheckprepfile
  {\ifcase\preprocessmethod
     \iftracefiles\writestatus\m!systems{preprocessing - not needed}\fi
     % no preprocessing
   \or
     % only check for existence
     \iftracefiles\writestatus\m!systems{preprocessing - check exitence of \readfilename\preprocesssuffix}\fi
     \doiffileexistselse{\readfilename\preprocesssuffix}
       {\chardef\preprocessstate\plusone}
       \donothing
   \or
     % check when in list, otherwise assume normal file
     \bgroup
     \splitfilename\readfilename
     \ifx\splitofftype\empty
       % saveguard and speed up
       \iftracefiles\writestatus\m!systems{preprocessing - no suffix}\fi
       \egroup
     \else\ifcase\preprocesslocal
       % also ./ strippen
       \iftracefiles\writestatus\m!systems{preprocessing - check presence of \readfilename\preprocesssuffix}\fi
       \doifdefinedelse{fp..\readfilename}
         {\egroup
          \doiffileexistselse{\readfilename\preprocesssuffix}
            {\chardef\preprocessstate\plusone}
            {\chardef\preprocessstate\plustwo}}%
         {\doifdefinedelse{fp.../\readfilename}
            {\egroup
             \edef\readfilename{./\readfilename}%
             \doiffileexistselse{\readfilename\preprocesssuffix}
               {\chardef\preprocessstate\plusone}
               {\chardef\preprocessstate\plustwo}}%
            {\doifdefinedelse{fp..\splitoffbase}%
               {\egroup
                \doiffileexistselse{\readfilename\preprocesssuffix}
                  {\chardef\preprocessstate\plusone}
                  {\chardef\preprocessstate\plustwo}}%
               {\egroup}}}%
     \else
       % maybe just filecheck, why ctl
       \iftracefiles\writestatus\m!systems{preprocessing - check local presence of \readfilename\preprocesssuffix}\fi
       \doifdefinedelse{fp..\readfilename}
         {\egroup
          \doiffileexistselse{\readfilename\preprocesssuffix}
            {\chardef\preprocessstate\plusone}
            {\chardef\preprocessstate\plustwo}}%
         {\egroup}%
     \fi
   \fi\fi}

% beware, \readfilename keeps the original one, but we load and store the
% suffixed with .prep file (if present)

\def\doreadfile#1#2#3#4% beware, this one already works at format generation time!
  {\sanitizefilename#2\to\readfilename
   \ifx\readfilename\empty
     % silently ignore
   \else
     \let\trackedfilename\readfilename
     \chardef\preprocessstate\zerocount
     \ifconditional\trackfilenames
       \doifundefinedelse{fn..\trackedfilename}\donetrue\donefalse
     \else
       \donetrue
     \fi
     \ifdone
       \checkfilename\readfilename
       \ifcase\kindoffile
         % not a full path or url, check for existence
         \doifelsenothing{#1}
           {\iftracefiles\writestatus\m!systems{searching for \readfilename\space on tex path}\fi
            \def\next{\redoreadfile\readfilename{#3}{#4}}}%
           {\iftracefiles\writestatus\m!systems{searching for \readfilename\space on #1}\fi
            \def\next{\redoreadfile{\pathplusfile{#1}{\readfilename}}{#3}{#4}}}%
       \else
         % a full path or url, no further checking done
         \docheckprepfile
         \ifcase\preprocessstate
           \doiffileexistselse\readfilename
             {\iftracefiles\writestatus\m!systems{located \readfilename}\fi
              \def\next{#3\dodoreadfile}}%
             {\iftracefiles\writestatus\m!systems{not found \readfilename}\fi
              \def\next{#4}}%
         \or
           \iftracefiles\writestatus\m!systems{located \readfilename\preprocesssuffix}\fi
           \def\next{#3\dodoreadfile}%
         \or
           \iftracefiles\writestatus\m!systems{not found \readfilename\preprocesssuffix}\fi
           \def\next{#4}%
         \fi
       \fi
     \else
       \edef\readfilename{\getvalue{fn..\readfilename}}% ??? is done !
       \iftracefiles\writestatus\m!systems{already located \readfilename}\fi
       \def\next{#3\dodoreadfile}%
     \fi
     \expandafter\next
   \fi}

\def\redoreadfile#1#2#3%
  {\docheckprepfile
   \ifcase\preprocessstate
     \doiffileexistselse{#1}%
       {\edef\readfilename{#1}%
        \iftracefiles\writestatus\m!systems{#1 located}\fi
        \def\next{#2\dodoreadfile}}%
       {\iftracefiles\writestatus\m!systems{cannot locate #1}\fi
        \advance\readlevel\minusone
        \ifnum\readlevel>\zerocount
          \edef\readfilename{\pathplusfile{\f!parentpath}{\readfilename}}%
          \def\next{\redoreadfile\readfilename{#2}{#3}}%
        \else
          \def\next{#3}%
        \fi}%
   \or
     \ifcase\preprocesslocal
       \edef\readfilename{#1}% nor found
     \else
       % no path prepending
     \fi
     \iftracefiles\writestatus\m!systems{#1\preprocesssuffix\space located}\fi
     \def\next{#2\dodoreadfile}%
   \or
     \def\next{#3}%
   \fi
   \next}

\def\dodoreadfile % we provide hooks, for instance for \enableXML
  {\ifconditional\trackfilenames
     \setxvalue{fn..\trackedfilename}{\readfilename\ifcase\preprocessstate\or\preprocesssuffix\fi}%
   \fi
   \the\everybeforereadfile
   \ifcase\preprocessstate
     % no checking or not found when using method 1
     \def\dodoreadfileindeed{\inputgivenfile\readfilename}%
   \or
     % found when using method 1 or 2
     \def\dodoreadfileindeed{\inputgivenfile{\readfilename\preprocesssuffix}}%
   \or
     % not found when using method 2
     \let\dodoreadfileindeed\relax
   \fi
   \dodoreadfileindeed
   \relax
   \the\everyafterreadfile}

\protect \endinput