summary refs log tree commit diff stats
path: root/tests/stdlib/tmath.nim
blob: 66c1f8ca0913792b2826785d30725a1be998369f (plain) (blame)
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
discard """
  targets: "c cpp js"
  matrix:"; -d:danger"
"""

# xxx: there should be a test with `-d:nimTmathCase2 -d:danger --passc:-ffast-math`,
# but it requires disabling certain lines with `when not defined(nimTmathCase2)`

import std/math
import std/assertions


# Function for approximate comparison of floats
proc `==~`(x, y: float): bool = abs(x - y) < 1e-9


template main() =
  block:
    when not defined(js):
      # check for no side effect annotation
      proc mySqrt(num: float): float {.noSideEffect.} =
        # xxx unused
        sqrt(num)

      # check gamma function
      doAssert gamma(5.0) == 24.0 # 4!
      doAssert almostEqual(gamma(0.5), sqrt(PI))
      doAssert almostEqual(gamma(-0.5), -2 * sqrt(PI))
      doAssert lgamma(1.0) == 0.0 # ln(1.0) == 0.0
      doAssert almostEqual(lgamma(0.5), 0.5 * ln(PI))
      doAssert erf(6.0) > erf(5.0)
      doAssert erfc(6.0) < erfc(5.0)

  block: # sgn() tests
    doAssert sgn(1'i8) == 1
    doAssert sgn(1'i16) == 1
    doAssert sgn(1'i32) == 1
    doAssert sgn(1'i64) == 1
    doAssert sgn(1'u8) == 1
    doAssert sgn(1'u16) == 1
    doAssert sgn(1'u32) == 1
    doAssert sgn(1'u64) == 1
    doAssert sgn(-12342.8844'f32) == -1
    doAssert sgn(123.9834'f64) == 1
    doAssert sgn(0'i32) == 0
    doAssert sgn(0'f32) == 0
    doAssert sgn(-0.0'f64) == 0
    doAssert sgn(NegInf) == -1
    doAssert sgn(Inf) == 1
    doAssert sgn(NaN) == 0

  block: # fac() tests
    when nimvm: discard
    else:
      try:
        discard fac(-1)
      except AssertionDefect:
        discard

    doAssert fac(0) == 1
    doAssert fac(1) == 1
    doAssert fac(2) == 2
    doAssert fac(3) == 6
    doAssert fac(4) == 24
    doAssert fac(5) == 120

  block: # floorMod/floorDiv
    doAssert floorDiv(8, 3) == 2
    doAssert floorMod(8, 3) == 2

    doAssert floorDiv(8, -3) == -3
    doAssert floorMod(8, -3) == -1

    doAssert floorDiv(-8, 3) == -3
    doAssert floorMod(-8, 3) == 1

    doAssert floorDiv(-8, -3) == 2
    doAssert floorMod(-8, -3) == -2

    doAssert floorMod(8.0, -3.0) == -1.0
    doAssert floorMod(-8.5, 3.0) == 0.5

  block: # euclDiv/euclMod
    doAssert euclDiv(8, 3) == 2
    doAssert euclMod(8, 3) == 2

    doAssert euclDiv(8, -3) == -2
    doAssert euclMod(8, -3) == 2

    doAssert euclDiv(-8, 3) == -3
    doAssert euclMod(-8, 3) == 1

    doAssert euclDiv(-8, -3) == 3
    doAssert euclMod(-8, -3) == 1

    doAssert euclMod(8.0, -3.0) == 2.0
    doAssert euclMod(-8.5, 3.0) == 0.5

    doAssert euclDiv(9, 3) == 3
    doAssert euclMod(9, 3) == 0

    doAssert euclDiv(9, -3) == -3
    doAssert euclMod(9, -3) == 0

    doAssert euclDiv(-9, 3) == -3
    doAssert euclMod(-9, 3) == 0

    doAssert euclDiv(-9, -3) == 3
    doAssert euclMod(-9, -3) == 0

  block: # ceilDiv
    doAssert ceilDiv(8,  3) ==  3
    doAssert ceilDiv(8,  4) ==  2
    doAssert ceilDiv(8,  5) ==  2
    doAssert ceilDiv(11, 3) ==  4
    doAssert ceilDiv(12, 3) ==  4
    doAssert ceilDiv(13, 3) ==  5
    doAssert ceilDiv(41, 7) ==  6
    doAssert ceilDiv(0,  1) ==  0
    doAssert ceilDiv(1,  1) ==  1
    doAssert ceilDiv(1,  2) ==  1
    doAssert ceilDiv(2,  1) ==  2
    doAssert ceilDiv(2,  2) ==  1
    doAssert ceilDiv(0, high(int)) == 0
    doAssert ceilDiv(1, high(int)) == 1
    doAssert ceilDiv(0, high(int) - 1) == 0
    doAssert ceilDiv(1, high(int) - 1) == 1
    doAssert ceilDiv(high(int) div 2, high(int) div 2 + 1) == 1
    doAssert ceilDiv(high(int) div 2, high(int) div 2 + 2) == 1
    doAssert ceilDiv(high(int) div 2 + 1, high(int) div 2) == 2
    doAssert ceilDiv(high(int) div 2 + 2, high(int) div 2) == 2
    doAssert ceilDiv(high(int) div 2 + 1, high(int) div 2 + 1) == 1
    doAssert ceilDiv(high(int), 1) == high(int)
    doAssert ceilDiv(high(int) - 1, 1) == high(int) - 1
    doAssert ceilDiv(high(int) - 1, 2) == high(int) div 2
    doAssert ceilDiv(high(int) - 1, high(int)) == 1
    doAssert ceilDiv(high(int) - 1, high(int) - 1) == 1
    doAssert ceilDiv(high(int) - 1, high(int) - 2) == 2
    doAssert ceilDiv(high(int), high(int)) == 1
    doAssert ceilDiv(high(int), high(int) - 1) == 2
    doAssert ceilDiv(255'u8,  1'u8) == 255'u8
    doAssert ceilDiv(254'u8,  2'u8) == 127'u8
    when not defined(danger):
      doAssertRaises(AssertionDefect): discard ceilDiv(41,  0)
      doAssertRaises(AssertionDefect): discard ceilDiv(41, -1)
      doAssertRaises(AssertionDefect): discard ceilDiv(-1,  1)
      doAssertRaises(AssertionDefect): discard ceilDiv(-1, -1)
      doAssertRaises(AssertionDefect): discard ceilDiv(254'u8, 3'u8)
      doAssertRaises(AssertionDefect): discard ceilDiv(255'u8, 2'u8)

  block: # splitDecimal() tests
    doAssert splitDecimal(54.674).intpart == 54.0
    doAssert splitDecimal(54.674).floatpart ==~ 0.674
    doAssert splitDecimal(-693.4356).intpart == -693.0
    doAssert splitDecimal(-693.4356).floatpart ==~ -0.4356
    doAssert splitDecimal(0.0).intpart == 0.0
    doAssert splitDecimal(0.0).floatpart == 0.0

  block: # trunc tests for vcc
    doAssert trunc(-1.1) == -1
    doAssert trunc(1.1) == 1
    doAssert trunc(-0.1) == -0
    doAssert trunc(0.1) == 0

    # special case
    doAssert classify(trunc(1e1000000)) == fcInf
    doAssert classify(trunc(-1e1000000)) == fcNegInf
    when not defined(nimTmathCase2):
      doAssert classify(trunc(0.0/0.0)) == fcNan
    doAssert classify(trunc(0.0)) == fcZero

    # trick the compiler to produce signed zero
    let
      f_neg_one = -1.0
      f_zero = 0.0
      f_nan = f_zero / f_zero

    doAssert classify(trunc(f_neg_one*f_zero)) == fcNegZero

    doAssert trunc(-1.1'f32) == -1
    doAssert trunc(1.1'f32) == 1
    doAssert trunc(-0.1'f32) == -0
    doAssert trunc(0.1'f32) == 0
    doAssert classify(trunc(1e1000000'f32)) == fcInf
    doAssert classify(trunc(-1e1000000'f32)) == fcNegInf
    when not defined(nimTmathCase2):
      doAssert classify(trunc(f_nan.float32)) == fcNan
    doAssert classify(trunc(0.0'f32)) == fcZero

  block: # log
    doAssert log(4.0, 3.0) ==~ ln(4.0) / ln(3.0)
    doAssert log2(8.0'f64) == 3.0'f64
    doAssert log2(4.0'f64) == 2.0'f64
    doAssert log2(2.0'f64) == 1.0'f64
    doAssert log2(1.0'f64) == 0.0'f64
    doAssert classify(log2(0.0'f64)) == fcNegInf

    doAssert log2(8.0'f32) == 3.0'f32
    doAssert log2(4.0'f32) == 2.0'f32
    doAssert log2(2.0'f32) == 1.0'f32
    doAssert log2(1.0'f32) == 0.0'f32
    doAssert classify(log2(0.0'f32)) == fcNegInf

  block: # cumsum
    block: # cumsum int seq return
      let counts = [1, 2, 3, 4]
      doAssert counts.cumsummed == @[1, 3, 6, 10]
      let empty: seq[int] = @[]
      doAssert empty.cumsummed == @[]

    block: # cumsum float seq return
      let counts = [1.0, 2.0, 3.0, 4.0]
      doAssert counts.cumsummed == @[1.0, 3.0, 6.0, 10.0]
      let empty: seq[float] = @[]
      doAssert empty.cumsummed == @[]

    block: # cumsum int in-place
      var counts = [1, 2, 3, 4]
      counts.cumsum
      doAssert counts == [1, 3, 6, 10]
      var empty: seq[int] = @[]
      empty.cumsum
      doAssert empty == @[]

    block: # cumsum float in-place
      var counts = [1.0, 2.0, 3.0, 4.0]
      counts.cumsum
      doAssert counts == [1.0, 3.0, 6.0, 10.0]
      var empty: seq[float] = @[]
      empty.cumsum
      doAssert empty == @[]

  block: # ^ compiles for valid types
    doAssert: compiles(5 ^ 2)
    doAssert: compiles(5.5 ^ 2)
    doAssert: compiles(5.5 ^ 2.int8)
    doAssert: compiles(5.5 ^ 2.uint)
    doAssert: compiles(5.5 ^ 2.uint8)
    doAssert: not compiles(5.5 ^ 2.2)

  block: # isNaN
    doAssert NaN.isNaN
    doAssert not Inf.isNaN
    doAssert isNaN(Inf - Inf)
    doAssert not isNaN(0.0)
    doAssert not isNaN(3.1415926)
    doAssert not isNaN(0'f32)

  block: # signbit
    doAssert not signbit(0.0)
    doAssert signbit(-0.0)
    doAssert signbit(-0.1)
    doAssert not signbit(0.1)

    doAssert not signbit(Inf)
    doAssert signbit(-Inf)
    doAssert not signbit(NaN)

    let x1 = NaN
    let x2 = -NaN
    let x3 = -x1

    doAssert isNaN(x1)
    doAssert isNaN(x2)
    doAssert isNaN(x3)
    doAssert not signbit(x1)
    doAssert signbit(x2)
    doAssert signbit(x3)

  block: # copySign
    doAssert copySign(10.0, 1.0) == 10.0
    doAssert copySign(10.0, -1.0) == -10.0
    doAssert copySign(-10.0, -1.0) == -10.0
    doAssert copySign(-10.0, 1.0) == 10.0
    doAssert copySign(float(10), -1.0) == -10.0

    doAssert copySign(10.0'f64, 1.0) == 10.0
    doAssert copySign(10.0'f64, -1.0) == -10.0
    doAssert copySign(-10.0'f64, -1.0) == -10.0
    doAssert copySign(-10.0'f64, 1.0) == 10.0
    doAssert copySign(10'f64, -1.0) == -10.0

    doAssert copySign(10.0'f32, 1.0) == 10.0
    doAssert copySign(10.0'f32, -1.0) == -10.0
    doAssert copySign(-10.0'f32, -1.0) == -10.0
    doAssert copySign(-10.0'f32, 1.0) == 10.0
    doAssert copySign(10'f32, -1.0) == -10.0

    doAssert copySign(Inf, -1.0) == -Inf
    doAssert copySign(-Inf, 1.0) == Inf
    doAssert copySign(Inf, 1.0) == Inf
    doAssert copySign(-Inf, -1.0) == -Inf
    doAssert copySign(Inf, 0.0) == Inf
    doAssert copySign(Inf, -0.0) == -Inf
    doAssert copySign(-Inf, 0.0) == Inf
    doAssert copySign(-Inf, -0.0) == -Inf
    doAssert copySign(1.0, -0.0) == -1.0
    doAssert copySign(0.0, -0.0) == -0.0
    doAssert copySign(-1.0, 0.0) == 1.0
    doAssert copySign(10.0, 0.0) == 10.0
    doAssert copySign(-1.0, NaN) == 1.0
    doAssert copySign(10.0, NaN) == 10.0

    doAssert copySign(NaN, NaN).isNaN
    doAssert copySign(-NaN, NaN).isNaN
    doAssert copySign(NaN, -NaN).isNaN
    doAssert copySign(-NaN, -NaN).isNaN
    doAssert copySign(NaN, 0.0).isNaN
    doAssert copySign(NaN, -0.0).isNaN
    doAssert copySign(-NaN, 0.0).isNaN
    doAssert copySign(-NaN, -0.0).isNaN

    doAssert copySign(-1.0, NaN) == 1.0
    doAssert copySign(-1.0, -NaN) == -1.0
    doAssert copySign(1.0, copySign(NaN, -1.0)) == -1.0

  block: # almostEqual
    doAssert almostEqual(3.141592653589793, 3.1415926535897936)
    doAssert almostEqual(1.6777215e7'f32, 1.6777216e7'f32)
    doAssert almostEqual(Inf, Inf)
    doAssert almostEqual(-Inf, -Inf)
    doAssert not almostEqual(Inf, -Inf)
    doAssert not almostEqual(-Inf, Inf)
    doAssert not almostEqual(Inf, NaN)
    doAssert not almostEqual(NaN, NaN)

  block: # round
    block: # Round to 0 decimal places
      doAssert round(54.652) == 55.0
      doAssert round(54.352) == 54.0
      doAssert round(-54.652) == -55.0
      doAssert round(-54.352) == -54.0
      doAssert round(0.0) == 0.0
      doAssert 1 / round(0.0) == Inf
      doAssert 1 / round(-0.0) == -Inf
      doAssert round(Inf) == Inf
      doAssert round(-Inf) == -Inf
      doAssert round(NaN).isNaN
      doAssert round(-NaN).isNaN
      doAssert round(-0.5) == -1.0
      doAssert round(0.5) == 1.0
      doAssert round(-1.5) == -2.0
      doAssert round(1.5) == 2.0
      doAssert round(-2.5) == -3.0
      doAssert round(2.5) == 3.0
      doAssert round(2.5'f32) == 3.0'f32
      doAssert round(2.5'f64) == 3.0'f64

    block: # func round*[T: float32|float64](x: T, places: int): T
      doAssert round(54.345, 0) == 54.0
      template fn(x) =
        doAssert round(x, 2).almostEqual 54.35
        doAssert round(x, 2).almostEqual 54.35
        doAssert round(x, -1).almostEqual 50.0
        doAssert round(x, -2).almostEqual 100.0
        doAssert round(x, -3).almostEqual 0.0
      fn(54.346)
      fn(54.346'f32)

  block: # abs
    doAssert 1.0 / abs(-0.0) == Inf
    doAssert 1.0 / abs(0.0) == Inf
    doAssert -1.0 / abs(-0.0) == -Inf
    doAssert -1.0 / abs(0.0) == -Inf
    doAssert abs(0.0) == 0.0
    doAssert abs(0.0'f32) == 0.0'f32

    doAssert abs(Inf) == Inf
    doAssert abs(-Inf) == Inf
    doAssert abs(NaN).isNaN
    doAssert abs(-NaN).isNaN

  block: # classify
    doAssert classify(0.3) == fcNormal
    doAssert classify(-0.3) == fcNormal
    doAssert classify(5.0e-324) == fcSubnormal
    doAssert classify(-5.0e-324) == fcSubnormal
    doAssert classify(0.0) == fcZero
    doAssert classify(-0.0) == fcNegZero
    doAssert classify(NaN) == fcNan
    doAssert classify(0.3 / 0.0) == fcInf
    doAssert classify(Inf) == fcInf
    doAssert classify(-0.3 / 0.0) == fcNegInf
    doAssert classify(-Inf) == fcNegInf

  block: # sum
    let empty: seq[int] = @[]
    doAssert sum(empty) == 0
    doAssert sum([1, 2, 3, 4]) == 10
    doAssert sum([-4, 3, 5]) == 4

  block: # prod
    let empty: seq[int] = @[]
    doAssert prod(empty) == 1
    doAssert prod([1, 2, 3, 4]) == 24
    doAssert prod([-4, 3, 5]) == -60
    doAssert almostEqual(prod([1.5, 3.4]), 5.1)
    let x: seq[float] = @[]
    doAssert prod(x) == 1.0

  block: # clamp range
    doAssert clamp(10, 1..5) == 5
    doAssert clamp(3, 1..5) == 3
    doAssert clamp(5, 1..5) == 5
    doAssert clamp(42.0, 1.0 .. 3.1415926535) == 3.1415926535
    doAssert clamp(NaN, 1.0 .. 2.0).isNaN
    doAssert clamp(-Inf, -Inf .. -1.0) == -Inf
    type A = enum a0, a1, a2, a3, a4, a5
    doAssert a1.clamp(a2..a4) == a2
    doAssert clamp((3, 0), (1, 0) .. (2, 9)) == (2, 9)

  block: # edge cases
    doAssert sqrt(-4.0).isNaN

    doAssert ln(0.0) == -Inf
    doAssert ln(-0.0) == -Inf
    doAssert ln(-12.0).isNaN

    doAssert log10(0.0) == -Inf
    doAssert log10(-0.0) == -Inf
    doAssert log10(-12.0).isNaN

    doAssert log2(0.0) == -Inf
    doAssert log2(-0.0) == -Inf
    doAssert log2(-12.0).isNaN

    when nimvm: discard
    else:
      doAssert frexp(0.0) == (0.0, 0)
      doAssert frexp(-0.0) == (-0.0, 0)
      doAssert classify(frexp(-0.0)[0]) == fcNegZero

    when not defined(js):
      doAssert gamma(0.0) == Inf
      doAssert gamma(-0.0) == -Inf
      doAssert gamma(-1.0).isNaN

      doAssert lgamma(0.0) == Inf
      doAssert lgamma(-0.0) == Inf
      doAssert lgamma(-1.0) == Inf


static: main()
main()
tes = atol(eq); if (mc->maxbytes < 0) mc->maxbytes = 0; } else if (strcmp(arg, "notes")) { /* IGNORE notes field */ if (*arg) CTrace((tfp, "ProcessMailcapEntry: Ignoring mailcap flag '%s'.\n", arg)); } } FREE(mallocd_string); s = t; } assign_presentation: FREE(rawentry); if (PassesTest(mc)) { CTrace((tfp, "ProcessMailcapEntry Setting up conversion %s : %s\n", mc->contenttype, mc->command)); HTSetPresentation(mc->contenttype, mc->command, mc->testcommand, mc->quality, 3.0, 0.0, mc->maxbytes, media); } FREE(mc->command); FREE(mc->testcommand); FREE(mc->contenttype); return (1); } #define L_CURL '{' #define R_CURL '}' static const char *LYSkipQuoted(const char *s) { int escaped = 0; ++s; /* skip first quote */ while (*s != 0) { if (escaped) { escaped = 0; } else if (*s == ESCAPE) { escaped = 1; } else if (*s == DQUOTE) { ++s; break; } ++s; } return s; } /* * Note: the tspecials[] here are those defined for Content-Type header, so * this function is not really general-purpose. */ static const char *LYSkipToken(const char *s) { static const char tspecials[] = "\"()<>@,;:\\/[]?.="; while (*s != '\0' && !WHITE(*s) && StrChr(tspecials, *s) == 0) { ++s; } return s; } static const char *LYSkipValue(const char *s) { if (*s == DQUOTE) s = LYSkipQuoted(s); else s = LYSkipToken(s); return s; } /* * Copy the value from the source, dequoting if needed. */ static char *LYCopyValue(const char *s) { const char *t; char *result = 0; int j, k; if (*s == DQUOTE) { t = LYSkipQuoted(s); StrAllocCopy(result, s + 1); result[t - s - 2] = '\0'; for (j = k = 0;; ++j, ++k) { if (result[j] == ESCAPE) { ++j; } if ((result[k] = result[j]) == '\0') break; } } else { t = LYSkipToken(s); StrAllocCopy(result, s); result[t - s] = '\0'; } return result; } /* * The "Content-Type:" field, contains zero or more parameters after a ';'. * Return the value of the named parameter, or null. */ static char *LYGetContentType(const char *name, const char *params) { char *result = 0; if (params != 0) { if (name != 0) { size_t length = strlen(name); const char *test = StrChr(params, ';'); /* skip type/subtype */ const char *next; while (test != 0) { BOOL found = FALSE; ++test; /* skip the ';' */ test = LYSkipCBlanks(test); next = LYSkipToken(test); if ((next - test) == (int) length && !StrNCmp(test, name, length)) { found = TRUE; } test = LYSkipCBlanks(next); if (*test == '=') { ++test; test = LYSkipCBlanks(test); if (found) { result = LYCopyValue(test); break; } else { test = LYSkipValue(test); } test = LYSkipCBlanks(test); } if (*test != ';') { break; /* we're lost */ } } } else { /* return the content-type */ StrAllocCopy(result, params); *LYSkipNonBlanks(result) = '\0'; } } return result; } /* * Check if the command uses a "%s" substitution. We need to know this, to * decide when to create temporary files, etc. */ BOOL LYMailcapUsesPctS(const char *controlstring) { BOOL result = FALSE; const char *from; const char *next; int prefixed = 0; int escaped = 0; for (from = controlstring; *from != '\0'; from++) { if (escaped) { escaped = 0; } else if (*from == ESCAPE) { escaped = 1; } else if (prefixed) { prefixed = 0; switch (*from) { case '%': /* not defined */ case 'n': case 'F': case 't': break; case 's': result = TRUE; break; case L_CURL: next = StrChr(from, R_CURL); if (next != 0) { from = next; break; } /* FALLTHRU */ default: break; } } else if (*from == '%') { prefixed = 1; } } return result; } /* * Build the command string for testing or executing a mailcap entry. * If a substitution from the Content-Type header is requested but no * parameters are available, return -1, otherwise 0. * * This does not support multipart %n or %F (does this apply to lynx?) */ static int BuildCommand(HTChunk *cmd, const char *controlstring, const char *TmpFileName, const char *params) { int result = 0; size_t TmpFileLen = strlen(TmpFileName); const char *from; const char *next; char *name, *value; int prefixed = 0; int escaped = 0; for (from = controlstring; *from != '\0'; from++) { if (escaped) { escaped = 0; HTChunkPutc(cmd, UCH(*from)); } else if (*from == ESCAPE) { escaped = 1; } else if (prefixed) { prefixed = 0; switch (*from) { case '%': /* not defined */ HTChunkPutc(cmd, UCH(*from)); break; case 'n': /* FALLTHRU */ case 'F': CTrace((tfp, "BuildCommand: Bad mailcap \"test\" clause: %s\n", controlstring)); break; case 't': if ((value = LYGetContentType(NULL, params)) != 0) { HTChunkPuts(cmd, value); FREE(value); } break; case 's': if (TmpFileLen) { HTChunkPuts(cmd, TmpFileName); } break; case L_CURL: next = StrChr(from, R_CURL); if (next != 0) { if (params != 0) { ++from; name = 0; HTSprintf0(&name, "%.*s", (int) (next - from), from); if ((value = LYGetContentType(name, params)) != 0) { HTChunkPuts(cmd, value); FREE(value); } else if (name) { if (!strcmp(name, "charset")) { HTChunkPuts(cmd, "ISO-8859-1"); } else { CTrace((tfp, "BuildCommand no value for %s\n", name)); } } FREE(name); } else { result = -1; } from = next; break; } /* FALLTHRU */ default: CTrace((tfp, "BuildCommand: Ignoring unrecognized format code in mailcap file '%%%c'.\n", *from)); break; } } else if (*from == '%') { prefixed = 1; } else { HTChunkPutc(cmd, UCH(*from)); } } HTChunkTerminate(cmd); return result; } /* * Build the mailcap test-command and execute it. This is only invoked when * we cannot tell just by looking at the command if it would succeed. * * Returns 0 for success, -1 for error and 1 for deferred. */ int LYTestMailcapCommand(const char *testcommand, const char *params) { int result; char TmpFileName[LY_MAXPATH]; HTChunk *expanded = 0; if (LYMailcapUsesPctS(testcommand)) { if (LYOpenTemp(TmpFileName, HTML_SUFFIX, "w") == 0) ExitWithError(CANNOT_OPEN_TEMP); LYCloseTemp(TmpFileName); } else { /* We normally don't need a temp file name - kw */ TmpFileName[0] = '\0'; } expanded = HTChunkCreate(1024); if (BuildCommand(expanded, testcommand, TmpFileName, params) != 0) { result = 1; CTrace((tfp, "PassesTest: Deferring test command: %s\n", expanded->data)); } else { CTrace((tfp, "PassesTest: Executing test command: %s\n", expanded->data)); if ((result = LYSystem(expanded->data)) != 0) { result = -1; CTrace((tfp, "PassesTest: Test failed!\n")); } else { CTrace((tfp, "PassesTest: Test passed!\n")); } } HTChunkFree(expanded); (void) LYRemoveTemp(TmpFileName); return result; } char *LYMakeMailcapCommand(const char *command, const char *params, const char *filename) { HTChunk *expanded = 0; char *result = 0; expanded = HTChunkCreate(1024); BuildCommand(expanded, command, filename, params); StrAllocCopy(result, expanded->data); HTChunkFree(expanded); return result; } #define RTR_forget 0 #define RTR_lookup 1 #define RTR_add 2 static int RememberTestResult(int mode, char *cmd, int result) { struct cmdlist_s { char *cmd; int result; struct cmdlist_s *next; }; static struct cmdlist_s *cmdlist = NULL; struct cmdlist_s *cur; switch (mode) { case RTR_forget: while (cmdlist) { cur = cmdlist->next; FREE(cmdlist->cmd); FREE(cmdlist); cmdlist = cur; } break; case RTR_lookup: for (cur = cmdlist; cur; cur = cur->next) if (!strcmp(cmd, cur->cmd)) return cur->result; return -1; case RTR_add: cur = typecalloc(struct cmdlist_s); if (cur == NULL) outofmem(__FILE__, "RememberTestResult"); cur->next = cmdlist; StrAllocCopy(cur->cmd, cmd); cur->result = result; cmdlist = cur; break; } return 0; } /* FIXME: this sometimes used caseless comparison, e.g., strcasecomp */ #define SameCommand(tst,ref) !strcmp(tst,ref) static int PassesTest(struct MailcapEntry *mc) { int result; /* * Make sure we have a command */ if (!mc->testcommand) return (1); /* * Save overhead of system() calls by faking these. - FM */ if (SameCommand(mc->testcommand, "test \"$DISPLAY\"") || SameCommand(mc->testcommand, "test \"$DISPLAY\" != \"\"") || SameCommand(mc->testcommand, "test -n \"$DISPLAY\"")) { FREE(mc->testcommand); CTrace((tfp, "PassesTest: Testing for XWINDOWS environment.\n")); if (LYgetXDisplay() != NULL) { CTrace((tfp, "PassesTest: Test passed!\n")); return (0 == 0); } else { CTrace((tfp, "PassesTest: Test failed!\n")); return (-1 == 0); } } if (SameCommand(mc->testcommand, "test -z \"$DISPLAY\"")) { FREE(mc->testcommand); CTrace((tfp, "PassesTest: Testing for NON_XWINDOWS environment.\n")); if (LYgetXDisplay() == NULL) { CTrace((tfp, "PassesTest: Test passed!\n")); return (0 == 0); } else { CTrace((tfp, "PassesTest: Test failed!\n")); return (-1 == 0); } } /* * Why do anything but return success for this one! - FM */ if (SameCommand(mc->testcommand, "test -n \"$LYNX_VERSION\"")) { FREE(mc->testcommand); CTrace((tfp, "PassesTest: Testing for LYNX environment.\n")); CTrace((tfp, "PassesTest: Test passed!\n")); return (0 == 0); } else /* * ... or failure for this one! - FM */ if (SameCommand(mc->testcommand, "test -z \"$LYNX_VERSION\"")) { FREE(mc->testcommand); CTrace((tfp, "PassesTest: Testing for non-LYNX environment.\n")); CTrace((tfp, "PassesTest: Test failed!\n")); return (-1 == 0); } result = RememberTestResult(RTR_lookup, mc->testcommand, 0); if (result == -1) { result = LYTestMailcapCommand(mc->testcommand, NULL); RememberTestResult(RTR_add, mc->testcommand, result ? 1 : 0); } /* * Free the test command as well since * we won't be needing it anymore. */ if (result != 1) FREE(mc->testcommand); if (result < 0) { CTrace((tfp, "PassesTest: Test failed!\n")); } else if (result == 0) { CTrace((tfp, "PassesTest: Test passed!\n")); } return (result >= 0); } static int ProcessMailcapFile(char *file, AcceptMedia media) { struct MailcapEntry mc; FILE *fp; CTrace((tfp, "ProcessMailcapFile: Loading file '%s'.\n", file)); if ((fp = fopen(file, TXT_R)) == NULL) { CTrace((tfp, "ProcessMailcapFile: Could not open '%s'.\n", file)); return (-1 == 0); } while (fp && !feof(fp)) { ProcessMailcapEntry(fp, &mc, media); } LYCloseInput(fp); RememberTestResult(RTR_forget, NULL, 0); return (0 == 0); } static int ExitWithError(const char *txt) { if (txt) fprintf(tfp, "Lynx: %s\n", txt); exit_immediately(EXIT_FAILURE); return (-1); } /* Reverse the entries from each mailcap after it has been read, so that * earlier entries have precedence. Set to 0 to get traditional lynx * behavior, which means that the last match wins. - kw */ static int reverse_mailcap = 1; static int HTLoadTypesConfigFile(char *fn, AcceptMedia media) { int result = 0; HTList *saved = HTPresentations; if (reverse_mailcap) { /* temporarily hide existing list */ HTPresentations = NULL; } result = ProcessMailcapFile(fn, media); if (reverse_mailcap) { if (result && HTPresentations) { HTList_reverse(HTPresentations); HTList_appendList(HTPresentations, saved); FREE(saved); } else { HTPresentations = saved; } } return result; } /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ /* Define a basic set of suffixes * ------------------------------ * * The LAST suffix for a type is that used for temporary files * of that type. * The quality is an apriori bias as to whether the file should be * used. Not that different suffixes can be used to represent files * which are of the same format but are originals or regenerated, * with different values. */ /* * Additional notes: the encoding parameter may be taken into account when * looking for a match; for that purpose "7bit", "8bit", and "binary" are * equivalent. * * Use of mixed case and of pseudo MIME types with embedded spaces should be * avoided. It was once necessary for getting the fancy strings into type * labels in FTP directory listings, but that can now be done with the * description field (using HTSetSuffix5). AFAIK the only effect of such * "fancy" (and mostly invalid) types that cannot be reproduced by using a * description fields is some statusline messages in SaveToFile (HTFWriter.c). * And showing the user an invalid MIME type as the 'Content-type:' is not such * a hot idea anyway, IMO. Still, if you want it, it is still possible (even * in lynx.cfg now), but use of it in the defaults below has been reduced. * * Case variations rely on peculiar behavior of HTAtom.c for matching. They * lead to surprising behavior, Lynx retains the case of a string in the form * first encountered after starting up. So while later suffix rules generally * override or modify earlier ones, the case used for a MIME time is determined * by the first suffix rule (or other occurrence). Matching in HTAtom_for is * effectively case insensitive, except for the first character of the string * which is treated as case-sensitive by the hash function there; best not to * rely on that, rather convert MIME types to lowercase on input as is already * done in most places (And HTAtom could become consistently case-sensitive, as * in newer W3C libwww). * - kw 1999-10-12 */ void HTFileInit(void) { #ifdef BUILTIN_SUFFIX_MAPS if (LYUseBuiltinSuffixes) { CTrace((tfp, "HTFileInit: Loading default (HTInit) extension maps.\n")); /* default suffix interpretation */ SET_SUFFIX1("*", STR_PLAINTEXT, "8bit"); SET_SUFFIX1("*.*", STR_PLAINTEXT, "8bit"); #ifdef EXEC_SCRIPTS /* * define these extensions for exec scripts. */ #ifndef VMS /* for csh exec links */ HTSetSuffix(".csh", "application/x-csh", "8bit", 0.8); HTSetSuffix(".sh", "application/x-sh", "8bit", 0.8); HTSetSuffix(".ksh", "application/x-ksh", "8bit", 0.8); #else HTSetSuffix(".com", "application/x-VMS_script", "8bit", 0.8); #endif /* !VMS */ #endif /* EXEC_SCRIPTS */ /* * Some of the old incarnation of the mappings is preserved and can be had * by defining TRADITIONAL_SUFFIXES. This is for some cases where I felt * the old rules might be preferred by someone, for some reason. It's not * done consistently. A lot more of this stuff could probably be changed * too or omitted, now that nearly the equivalent functionality is * available in lynx.cfg. - kw 1999-10-12 */ /* *INDENT-OFF* */ SET_SUFFIX1(".saveme", "application/x-Binary", "binary"); SET_SUFFIX1(".dump", "application/x-Binary", "binary"); SET_SUFFIX1(".bin", "application/x-Binary", "binary"); SET_SUFFIX1(".arc", "application/x-Compressed", "binary"); SET_SUFFIX1(".alpha-exe", "application/x-Executable", "binary"); SET_SUFFIX1(".alpha_exe", "application/x-Executable", "binary"); SET_SUFFIX1(".AXP-exe", "application/x-Executable", "binary"); SET_SUFFIX1(".AXP_exe", "application/x-Executable", "binary"); SET_SUFFIX1(".VAX-exe", "application/x-Executable", "binary"); SET_SUFFIX1(".VAX_exe", "application/x-Executable", "binary"); SET_SUFFIX5(".exe", STR_BINARY, "binary", "Executable"); #ifdef TRADITIONAL_SUFFIXES SET_SUFFIX1(".exe.Z", "application/x-Comp. Executable", "binary"); SET_SUFFIX1(".Z", "application/UNIX Compressed", "binary"); SET_SUFFIX1(".tar_Z", "application/UNIX Compr. Tar", "binary"); SET_SUFFIX1(".tar.Z", "application/UNIX Compr. Tar", "binary"); #else SET_SUFFIX5(".Z", "application/x-compress", "binary", "UNIX Compressed"); SET_SUFFIX5(".Z", NULL, "compress", "UNIX Compressed"); SET_SUFFIX5(".exe.Z", STR_BINARY, "compress", "Executable"); SET_SUFFIX5(".tar_Z", "application/x-tar", "compress", "UNIX Compr. Tar"); SET_SUFFIX5(".tar.Z", "application/x-tar", "compress", "UNIX Compr. Tar"); #endif #ifdef TRADITIONAL_SUFFIXES SET_SUFFIX1("-gz", "application/GNU Compressed", "binary"); SET_SUFFIX1("_gz", "application/GNU Compressed", "binary"); SET_SUFFIX1(".gz", "application/GNU Compressed", "binary"); SET_SUFFIX5(".tar.gz", "application/x-tar", "binary", "GNU Compr. Tar"); SET_SUFFIX5(".tgz", "application/x-tar", "gzip", "GNU Compr. Tar"); #else SET_SUFFIX5("-gz", "application/x-gzip", "binary", "GNU Compressed"); SET_SUFFIX5("_gz", "application/x-gzip", "binary", "GNU Compressed"); SET_SUFFIX5(".gz", "application/x-gzip", "binary", "GNU Compressed"); SET_SUFFIX5("-gz", NULL, "gzip", "GNU Compressed"); SET_SUFFIX5("_gz", NULL, "gzip", "GNU Compressed"); SET_SUFFIX5(".gz", NULL, "gzip", "GNU Compressed"); SET_SUFFIX5(".tar.gz", "application/x-tar", "gzip", "GNU Compr. Tar"); SET_SUFFIX5(".tgz", "application/x-tar", "gzip", "GNU Compr. Tar"); #endif #ifdef TRADITIONAL_SUFFIXES SET_SUFFIX1(".src", "application/x-WAIS-source", "8bit"); SET_SUFFIX1(".wsrc", "application/x-WAIS-source", "8bit"); #else SET_SUFFIX5(".wsrc", "application/x-wais-source", "8bit", "WAIS-source"); #endif SET_SUFFIX5(".zip", "application/zip", "binary", "Zip File"); SET_SUFFIX1(".zz", "application/x-deflate", "binary"); SET_SUFFIX1(".zz", "application/deflate", "binary"); SET_SUFFIX1(".bz2", "application/x-bzip2", "binary"); SET_SUFFIX1(".bz2", "application/bzip2", "binary"); SET_SUFFIX1(".br", "application/x-brotli", "binary"); SET_SUFFIX1(".xz", "application/x-xz", "binary"); SET_SUFFIX1(".lz", "application/x-lzip", "binary"); SET_SUFFIX1(".lzma", "application/x-lzma", "binary"); #ifdef TRADITIONAL_SUFFIXES SET_SUFFIX1(".uu", "application/x-UUencoded", "8bit"); SET_SUFFIX1(".hqx", "application/x-Binhex", "8bit"); SET_SUFFIX1(".o", "application/x-Prog. Object", "binary"); SET_SUFFIX1(".a", "application/x-Prog. Library", "binary"); #else SET_SUFFIX5(".uu", "application/x-uuencoded", "7bit", "UUencoded"); SET_SUFFIX5(".hqx", "application/mac-binhex40", "8bit", "Mac BinHex"); HTSetSuffix5(".o", STR_BINARY, "binary", "Prog. Object", 0.5); HTSetSuffix5(".a", STR_BINARY, "binary", "Prog. Library", 0.5); HTSetSuffix5(".so", STR_BINARY, "binary", "Shared Lib", 0.5); #endif SET_SUFFIX5(".oda", "application/oda", "binary", "ODA"); SET_SUFFIX5(".pdf", "application/pdf", "binary", "PDF"); SET_SUFFIX5(".eps", "application/postscript", "8bit", "Postscript"); SET_SUFFIX5(".ai", "application/postscript", "8bit", "Postscript"); SET_SUFFIX5(".ps", "application/postscript", "8bit", "Postscript"); SET_SUFFIX5(".rtf", "application/rtf", "8bit", "RTF"); SET_SUFFIX5(".dvi", "application/x-dvi", "8bit", "DVI"); SET_SUFFIX5(".hdf", "application/x-hdf", "8bit", "HDF"); SET_SUFFIX1(".cdf", "application/x-netcdf", "8bit"); SET_SUFFIX1(".nc", "application/x-netcdf", "8bit"); #ifdef TRADITIONAL_SUFFIXES SET_SUFFIX1(".latex", "application/x-Latex", "8bit"); SET_SUFFIX1(".tex", "application/x-Tex", "8bit"); SET_SUFFIX1(".texinfo", "application/x-Texinfo", "8bit"); SET_SUFFIX1(".texi", "application/x-Texinfo", "8bit"); #else SET_SUFFIX5(".latex", "application/x-latex", "8bit", "LaTeX"); SET_SUFFIX5(".tex", "text/x-tex", "8bit", "TeX"); SET_SUFFIX5(".texinfo", "application/x-texinfo", "8bit", "Texinfo"); SET_SUFFIX5(".texi", "application/x-texinfo", "8bit", "Texinfo"); #endif #ifdef TRADITIONAL_SUFFIXES SET_SUFFIX1(".t", "application/x-Troff", "8bit"); SET_SUFFIX1(".tr", "application/x-Troff", "8bit"); SET_SUFFIX1(".roff", "application/x-Troff", "8bit"); SET_SUFFIX1(".man", "application/x-Troff-man", "8bit"); SET_SUFFIX1(".me", "application/x-Troff-me", "8bit"); SET_SUFFIX1(".ms", "application/x-Troff-ms", "8bit"); #else SET_SUFFIX5(".t", "application/x-troff", "8bit", "Troff"); SET_SUFFIX5(".tr", "application/x-troff", "8bit", "Troff"); SET_SUFFIX5(".roff", "application/x-troff", "8bit", "Troff"); SET_SUFFIX5(".man", "application/x-troff-man", "8bit", "Man Page"); SET_SUFFIX5(".me", "application/x-troff-me", "8bit", "Troff me"); SET_SUFFIX5(".ms", "application/x-troff-ms", "8bit", "Troff ms"); #endif SET_SUFFIX1(".zoo", "application/x-Zoo File", "binary"); #if defined(TRADITIONAL_SUFFIXES) || defined(VMS) SET_SUFFIX1(".bak", "application/x-VMS BAK File", "binary"); SET_SUFFIX1(".bkp", "application/x-VMS BAK File", "binary"); SET_SUFFIX1(".bck", "application/x-VMS BAK File", "binary"); SET_SUFFIX5(".bkp_gz", STR_BINARY, "gzip", "GNU BAK File"); SET_SUFFIX5(".bkp-gz", STR_BINARY, "gzip", "GNU BAK File"); SET_SUFFIX5(".bck_gz", STR_BINARY, "gzip", "GNU BAK File"); SET_SUFFIX5(".bck-gz", STR_BINARY, "gzip", "GNU BAK File"); SET_SUFFIX5(".bkp-Z", STR_BINARY, "compress", "Comp. BAK File"); SET_SUFFIX5(".bkp_Z", STR_BINARY, "compress", "Comp. BAK File"); SET_SUFFIX5(".bck-Z", STR_BINARY, "compress", "Comp. BAK File"); SET_SUFFIX5(".bck_Z", STR_BINARY, "compress", "Comp. BAK File"); #else HTSetSuffix5(".bak", NULL, "binary", "Backup", 0.5); SET_SUFFIX5(".bkp", STR_BINARY, "binary", "VMS BAK File"); SET_SUFFIX5(".bck", STR_BINARY, "binary", "VMS BAK File"); #endif #if defined(TRADITIONAL_SUFFIXES) || defined(VMS) SET_SUFFIX1(".hlb", "application/x-VMS Help Libr.", "binary"); SET_SUFFIX1(".olb", "application/x-VMS Obj. Libr.", "binary"); SET_SUFFIX1(".tlb", "application/x-VMS Text Libr.", "binary"); SET_SUFFIX1(".obj", "application/x-VMS Prog. Obj.", "binary"); SET_SUFFIX1(".decw$book", "application/x-DEC BookReader", "binary"); SET_SUFFIX1(".mem", "application/x-RUNOFF-MANUAL", "8bit"); #else SET_SUFFIX5(".hlb", STR_BINARY, "binary", "VMS Help Libr."); SET_SUFFIX5(".olb", STR_BINARY, "binary", "VMS Obj. Libr."); SET_SUFFIX5(".tlb", STR_BINARY, "binary", "VMS Text Libr."); SET_SUFFIX5(".obj", STR_BINARY, "binary", "Prog. Object"); SET_SUFFIX5(".decw$book", STR_BINARY, "binary", "DEC BookReader"); SET_SUFFIX5(".mem", "text/x-runoff-manual", "8bit", "RUNOFF-MANUAL"); #endif SET_SUFFIX1(".vsd", "application/visio", "binary"); SET_SUFFIX5(".lha", "application/x-lha", "binary", "lha File"); SET_SUFFIX5(".lzh", "application/x-lzh", "binary", "lzh File"); SET_SUFFIX5(".sea", "application/x-sea", "binary", "sea File"); #ifdef TRADITIONAL_SUFFIXES SET_SUFFIX5(".sit", "application/x-sit", "binary", "sit File"); #else SET_SUFFIX5(".sit", "application/x-stuffit", "binary", "StuffIt"); #endif SET_SUFFIX5(".dms", "application/x-dms", "binary", "dms File"); SET_SUFFIX5(".iff", "application/x-iff", "binary", "iff File"); SET_SUFFIX1(".bcpio", "application/x-bcpio", "binary"); SET_SUFFIX1(".cpio", "application/x-cpio", "binary"); #ifdef TRADITIONAL_SUFFIXES SET_SUFFIX1(".gtar", "application/x-gtar", "binary"); #endif SET_SUFFIX1(".shar", "application/x-shar", "8bit"); SET_SUFFIX1(".share", "application/x-share", "8bit"); #ifdef TRADITIONAL_SUFFIXES SET_SUFFIX1(".sh", "application/x-sh", "8bit"); /* xtra */ #endif SET_SUFFIX1(".sv4cpio", "application/x-sv4cpio", "binary"); SET_SUFFIX1(".sv4crc", "application/x-sv4crc", "binary"); SET_SUFFIX5(".tar", "application/x-tar", "binary", "Tar File"); SET_SUFFIX1(".ustar", "application/x-ustar", "binary"); SET_SUFFIX1(".snd", "audio/basic", "binary"); SET_SUFFIX1(".au", "audio/basic", "binary"); SET_SUFFIX1(".aifc", "audio/x-aiff", "binary"); SET_SUFFIX1(".aif", "audio/x-aiff", "binary"); SET_SUFFIX1(".aiff", "audio/x-aiff", "binary"); SET_SUFFIX1(".wav", "audio/x-wav", "binary"); SET_SUFFIX1(".midi", "audio/midi", "binary"); SET_SUFFIX1(".mod", "audio/mod", "binary"); SET_SUFFIX1(".gif", "image/gif", "binary"); SET_SUFFIX1(".ief", "image/ief", "binary"); SET_SUFFIX1(".jfif", "image/jpeg", "binary"); /* xtra */ SET_SUFFIX1(".jfif-tbnl", "image/jpeg", "binary"); /* xtra */ SET_SUFFIX1(".jpe", "image/jpeg", "binary"); SET_SUFFIX1(".jpg", "image/jpeg", "binary"); SET_SUFFIX1(".jpeg", "image/jpeg", "binary"); SET_SUFFIX1(".tif", "image/tiff", "binary"); SET_SUFFIX1(".tiff", "image/tiff", "binary"); SET_SUFFIX1(".ham", "image/ham", "binary"); SET_SUFFIX1(".ras", "image/x-cmu-rast", "binary"); SET_SUFFIX1(".pnm", "image/x-portable-anymap", "binary"); SET_SUFFIX1(".pbm", "image/x-portable-bitmap", "binary"); SET_SUFFIX1(".pgm", "image/x-portable-graymap", "binary"); SET_SUFFIX1(".ppm", "image/x-portable-pixmap", "binary"); SET_SUFFIX1(".png", "image/png", "binary"); SET_SUFFIX1(".rgb", "image/x-rgb", "binary"); SET_SUFFIX1(".xbm", "image/x-xbitmap", "binary"); SET_SUFFIX1(".xpm", "image/x-xpixmap", "binary"); SET_SUFFIX1(".xwd", "image/x-xwindowdump", "binary"); SET_SUFFIX1(".rtx", "text/richtext", "8bit"); SET_SUFFIX1(".tsv", "text/tab-separated-values", "8bit"); SET_SUFFIX1(".etx", "text/x-setext", "8bit"); SET_SUFFIX1(".mpg", "video/mpeg", "binary"); SET_SUFFIX1(".mpe", "video/mpeg", "binary"); SET_SUFFIX1(".mpeg", "video/mpeg", "binary"); SET_SUFFIX1(".mov", "video/quicktime", "binary"); SET_SUFFIX1(".qt", "video/quicktime", "binary"); SET_SUFFIX1(".avi", "video/x-msvideo", "binary"); SET_SUFFIX1(".movie", "video/x-sgi-movie", "binary"); SET_SUFFIX1(".mv", "video/x-sgi-movie", "binary"); SET_SUFFIX1(".mime", "message/rfc822", "8bit"); SET_SUFFIX1(".c", STR_PLAINTEXT, "8bit"); SET_SUFFIX1(".cc", STR_PLAINTEXT, "8bit"); SET_SUFFIX1(".c++", STR_PLAINTEXT, "8bit"); SET_SUFFIX1(".css", STR_PLAINTEXT, "8bit"); SET_SUFFIX1(".h", STR_PLAINTEXT, "8bit"); SET_SUFFIX1(".pl", STR_PLAINTEXT, "8bit"); SET_SUFFIX1(".text", STR_PLAINTEXT, "8bit"); SET_SUFFIX1(".txt", STR_PLAINTEXT, "8bit"); SET_SUFFIX1(".php", STR_HTML, "8bit"); SET_SUFFIX1(".php3", STR_HTML, "8bit"); SET_SUFFIX1(".html3", STR_HTML, "8bit"); SET_SUFFIX1(".ht3", STR_HTML, "8bit"); SET_SUFFIX1(".phtml", STR_HTML, "8bit"); SET_SUFFIX1(".shtml", STR_HTML, "8bit"); SET_SUFFIX1(".sht", STR_HTML, "8bit"); SET_SUFFIX1(".htmlx", STR_HTML, "8bit"); SET_SUFFIX1(".htm", STR_HTML, "8bit"); SET_SUFFIX1(".html", STR_HTML, "8bit"); /* *INDENT-ON* */ } else { /* LYSuffixRules */ /* * Note that even .html -> text/html, .htm -> text/html are omitted if * default maps are compiled in but then skipped because of a * configuration file directive. Whoever changes the config file in * this way can easily also add the SUFFIX rules there. - kw */ CTrace((tfp, "HTFileInit: Skipping all default (HTInit) extension maps!\n")); } /* LYSuffixRules */ #else /* BUILTIN_SUFFIX_MAPS */ CTrace((tfp, "HTFileInit: Default (HTInit) extension maps not compiled in.\n")); /* * The following two are still used if BUILTIN_SUFFIX_MAPS was undefined. * Without one of them, lynx would always need to have a mapping specified * in a lynx.cfg or mime.types file to be usable for local HTML files at * all. That includes many of the generated user interface pages. - kw */ SET_SUFFIX1(".htm", STR_HTML, "8bit"); SET_SUFFIX1(".html", STR_HTML, "8bit"); #endif /* BUILTIN_SUFFIX_MAPS */ if (LYisAbsPath(global_extension_map)) { /* These should override the default extensions as necessary. */ HTLoadExtensionsConfigFile(global_extension_map); } /* * Load the local maps. */ if (IsOurFile(LYAbsOrHomePath(&personal_extension_map)) && LYCanReadFile(personal_extension_map)) { /* These should override everything else. */ HTLoadExtensionsConfigFile(personal_extension_map); } } /* -------------------- Extension config file reading --------------------- */ /* * The following is lifted from NCSA httpd 1.0a1, by Rob McCool; * NCSA httpd is in the public domain, as is this code. * * Modified Oct 97 - KW */ #define MAX_STRING_LEN 256 static int HTGetLine(char *s, int n, FILE *f) { register int i = 0, r; if (!f) return (1); while (1) { r = fgetc(f); s[i] = (char) r; if (s[i] == CR) { r = fgetc(f); if (r == LF) s[i] = (char) r; else if (r != EOF) ungetc(r, f); } if ((r == EOF) || (s[i] == LF) || (s[i] == CR) || (i == (n - 1))) { s[i] = '\0'; return (feof(f) ? 1 : 0); } ++i; } } static void HTGetWord(char *word, char *line, int stop, int stop2) { int x = 0, y; for (x = 0; (line[x] && UCH(line[x]) != UCH(stop) && UCH(line[x]) != UCH(stop2)); x++) { word[x] = line[x]; } word[x] = '\0'; if (line[x]) ++x; y = 0; while ((line[y++] = line[x++])) { ; } return; } static int HTLoadExtensionsConfigFile(char *fn) { char line[MAX_STRING_LEN]; char word[MAX_STRING_LEN]; char *ct; FILE *f; int count = 0; CTrace((tfp, "HTLoadExtensionsConfigFile: Loading file '%s'.\n", fn)); if ((f = fopen(fn, TXT_R)) == NULL) { CTrace((tfp, "HTLoadExtensionsConfigFile: Could not open '%s'.\n", fn)); return count; } while (!(HTGetLine(line, (int) sizeof(line), f))) { HTGetWord(word, line, ' ', '\t'); if (line[0] == '\0' || word[0] == '#') continue; ct = NULL; StrAllocCopy(ct, word); LYLowerCase(ct); while (line[0]) { HTGetWord(word, line, ' ', '\t'); if (word[0] && (word[0] != ' ')) { char *ext = NULL; HTSprintf0(&ext, ".%s", word); LYLowerCase(ext); CTrace((tfp, "setting suffix '%s' to '%s'.\n", ext, ct)); if (strstr(ct, "tex") != NULL || strstr(ct, "postscript") != NULL || strstr(ct, "sh") != NULL || strstr(ct, "troff") != NULL || strstr(ct, "rtf") != NULL) SET_SUFFIX1(ext, ct, "8bit"); else SET_SUFFIX1(ext, ct, "binary"); count++; FREE(ext); } } FREE(ct); } LYCloseInput(f); return count; }