mktool 1.5.8

General purpose utility to enhance pkgsrc/mk infrastructure
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
/*
 * Copyright (c) 2024 Jonathan Perkin <jonathan@perkin.org.uk>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

extern crate glob;

use crate::scrub_ctrl;
use clap::Args;
use std::collections::HashSet;
use std::fs;
use std::io::{BufRead, BufReader, Read};
use walkdir::WalkDir;

#[derive(Args, Debug)]
pub struct Cmd {}

fn check_random(line: &str) -> bool {
    let mut rv = false;
    let bytes = line.as_bytes();
    for (start, _) in line.match_indices("$RANDOM") {
        let next = start + "$RANDOM".len();

        /*
         * $RANDOM mixed with PID ($$) is commonly found in GNU configure
         * scripts, and because they are always executed using a compatible
         * shell then are considered acceptable.  Turning this off produces
         * lots of false positives in e.g. config.guess.
         */
        if start >= 3 && &bytes[start - 3..start] == b"$$-" {
            return false;
        }
        if bytes.get(next..next + 3) == Some(b"-$$") {
            return false;
        }

        /*
         * Trailing A-Z_, i.e. a variable that starts "$RANDOM.." such as
         * $RANDOMIZE is considered acceptable, but only if there is no bare
         * $RANDOM elsewhere on the line, so continue to other matches.
         */
        if let Some(&b) = bytes.get(next) {
            if b.is_ascii_uppercase() || b == b'_' {
                continue;
            }
        }

        /*
         * If we're still here then there's another $RANDOM on the line and
         * we didn't already exit early for the acceptable cases.  Set exit
         * status that will be used unless we exit early later.
         */
        rv = true;
    }

    rv
}

fn check_test_eq(line: &str) -> bool {
    let words: Vec<_> = line.split_whitespace().collect();
    let mut idx = 2;
    while idx < words.len() {
        if words[idx] == "=="
            && (words[idx - 2] == "test" || words[idx - 2] == "[")
        {
            return true;
        }
        idx += 1;
    }
    false
}

fn print_random_warning() {
    let msg = r#"
Explanation:
===========================================================================
The variable $RANDOM is not required for a POSIX-conforming shell, and
many implementations of /bin/sh do not support it. It should therefore
not be used in shell programs that are meant to be portable across a
large number of POSIX-like systems.
===========================================================================
    "#;
    println!("{msg}");
}

fn print_test_eq_error() {
    let msg = r#"
Explanation:
===========================================================================
The "test" command, as well as the "[" command, are not required to know
the "==" operator. Only a few implementations like bash and some
versions of ksh support it.

When you run "test foo == foo" on a platform that does not support the
"==" operator, the result will be "false" instead of "true". This can
lead to unexpected behavior.

There are two ways to fix this error message. If the file that contains
the "test ==" is needed for building the package, you should create a
patch for it, replacing the "==" operator with "=". If the file is not
needed, add its name to the CHECK_PORTABILITY_SKIP variable in the
package Makefile.
===========================================================================
    "#;
    println!("{msg}");
}

impl Cmd {
    pub fn run(&self) -> Result<i32, Box<dyn std::error::Error>> {
        let mut rv = 0;

        /*
         * File globs to skip specified in CHECK_PORTABILITY_SKIP.
         */
        let mut skipglob = vec![];
        if let Ok(paths) = std::env::var("CHECK_PORTABILITY_SKIP") {
            for p in paths.split_whitespace().collect::<Vec<&str>>() {
                match glob::Pattern::new(p) {
                    Ok(g) => skipglob.push(g),
                    Err(e) => {
                        eprintln!(
                            "WARNING: invalid CHECK_PORTABILITY_SKIP glob '{p}': {e}"
                        );
                    }
                }
            }
        }

        /*
         * List of file extensions to skip.  These are plain strings rather
         * than adding to skipglob as it's faster.  Based on the lists in
         * check-portability.sh but with some additions.
         */
        const SKIPEXT: &[&str] = &[
            "~",
            ".1",
            ".3",
            ".C",
            ".a",
            ".ac",
            ".c",
            ".cc",
            ".css",
            ".cxx",
            ".docbook",
            ".dtd",
            ".el",
            ".f",
            ".gif",
            ".gn",
            ".go",
            ".gz",
            ".h",
            ".hpp",
            ".htm",
            ".html",
            ".hxx",
            ".idl",
            ".inc",
            ".jpg",
            ".js",
            ".json",
            ".kicad_mod",
            ".m4",
            ".map",
            ".md",
            ".mo",
            ".ogg",
            ".orig",
            ".page",
            ".php",
            ".pl",
            ".png",
            ".po",
            ".properties",
            ".py",
            ".rb",
            ".result",
            ".svg",
            ".test",
            ".tfm",
            ".ts",
            ".txt",
            ".vf",
            ".xml",
            ".xpm",
        ];

        /*
         * Get list of patched files.
         */
        let mut patched: HashSet<String> = HashSet::new();
        if let Ok(patchdir) = std::env::var("PATCHDIR") {
            for patch in
                WalkDir::new(patchdir).into_iter().filter_map(|e| e.ok())
            {
                if !patch.file_type().is_file() {
                    continue;
                }
                if !patch.file_name().to_string_lossy().starts_with("patch-") {
                    continue;
                }
                let pfile = fs::File::open(patch.path())?;
                let reader = BufReader::new(pfile);
                for line in reader.lines() {
                    let line = line?;
                    if line.starts_with("+++") {
                        let v: Vec<&str> =
                            line.splitn(2, char::is_whitespace).collect();
                        if v.len() == 2 {
                            patched.insert(v[1].to_string());
                        }
                        break;
                    }
                }
            }
        }

        'nextfile: for entry in
            WalkDir::new(".").into_iter().filter_map(|e| e.ok())
        {
            if !entry.file_type().is_file() {
                continue;
            }

            /*
             * Skip extensions we aren't interested in.
             */
            let fname: &str = &entry.file_name().to_string_lossy();
            for ext in SKIPEXT {
                if fname.ends_with(ext) {
                    continue 'nextfile;
                }
            }

            /*
             * If this filename ends ".in" and we already have a patch for the
             * non-".in" filename then skip it, no need to patch both.
             */
            if let Some(p) =
                entry.file_name().to_string_lossy().strip_suffix(".in")
            {
                if patched.contains(p) {
                    continue 'nextfile;
                }
            }

            let path = entry.path();

            /*
             * Remove leading "./" from walkdir path entries as all
             * CHECK_PORTABILITY_SKIP matches are relative to WRKDIR.
             */
            let mpath = match path.strip_prefix("./") {
                Ok(p) => p,
                Err(_) => path,
            };
            for g in &skipglob {
                if g.matches_path(mpath) {
                    continue 'nextfile;
                }
            }

            /*
             * Verify that the file starts with a shell hashbang, otherwise
             * skip to avoid wasting time with non-shell files.
             *
             * XXX If CHECK_PORTABILITY_EXPERIMENTAL is enabled then we
             * should continue to check Makefiles (see shell version),
             * however that is not currently supported and may never be,
             * given I don't know anyone who enables it.
             */
            let file = fs::File::open(path)?;
            let mut reader = BufReader::with_capacity(1024, file);
            let head = reader.fill_buf()?;

            /*
             * Perform the simple and fast hashbang check first.
             */
            if !head.starts_with(b"#!") {
                continue 'nextfile;
            }

            /*
             * More complicated check for "/bin/sh" somewhere on first line
             * next.
             */
            let binsh = b"/bin/sh";
            let Some(newline) = head.iter().position(|&c| c == b'\n') else {
                continue 'nextfile;
            };
            let first = &head[..newline];
            if !first.windows(binsh.len()).any(|win| win == binsh) {
                continue 'nextfile;
            }

            for (i, line) in reader.by_ref().lines().enumerate() {
                /*
                 * Silently skip any non-UTF-8 lines; the hashbang gate
                 * already filters out binary files in practice.
                 */
                let Ok(line) = line else {
                    continue;
                };
                /*
                 * Remove all leading and trailing whitespace to simplify
                 * matches, and ignore comments.
                 */
                let line = line.trim();
                if line.starts_with('#') {
                    continue;
                }
                if check_random(line) {
                    eprintln!("WARNING: [check-portability] => Found $RANDOM:");
                    eprintln!(
                        "WARNING: [check-portability] {}:{}: {}",
                        mpath.display(),
                        i + 1,
                        scrub_ctrl(line)
                    );
                    print_random_warning();
                }
                if check_test_eq(line) {
                    eprintln!(
                        "ERROR: [check-portability] => Found test ... == ...:"
                    );
                    eprintln!(
                        "ERROR: [check-portability] {}:{}: {}",
                        mpath.display(),
                        i + 1,
                        scrub_ctrl(line)
                    );
                    print_test_eq_error();
                    rv = 1;
                }
            }
        }

        Ok(rv)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_random() {
        assert!(check_random("$RANDOM"));

        /*
         * Only exact matches for prefix/suffix "$$" are valid.
         */
        assert!(check_random("-$RANDOM"));
        assert!(check_random("$-$RANDOM"));
        assert!(check_random("$RANDOM-"));
        assert!(check_random("$RANDOM-$"));
        assert!(!check_random("$$-$RANDOM"));
        assert!(!check_random("$RANDOM-$$"));

        /*
         * If we see GNU-style $$-$RANDOM anywhere then all other matches are
         * effectively ignored.
         */
        assert!(!check_random("$RANDOM-$$ $RANDOM"));
        assert!(!check_random("$RANDOM $RANDOM-$$"));

        /*
         * $RANDOM at the start of a variable name is fine, unless we also see
         * a bare $RANDOM too (this differs from check-portability.awk which
         * is first-match-wins).
         */
        assert!(!check_random("$RANDOMIZE"));
        assert!(!check_random("$RANDOM_ISH"));
        assert!(check_random("$RANDOMIZE $RANDOM"));

        /*
         * Commented matches are fine.  Unfortunately we strip commented
         * lines prior to calling check_random() currently, so this should
         * go into an integration test.
         */
        //assert_eq!(check_random("# $RANDOM"), false);
        //assert_eq!(check_random("   # $RANDOM"), false);

        /*
         * Misc non-matches.
         */
        assert!(!check_random(""));
        assert!(!check_random("RANDOM"));
        assert!(!check_random("$ RANDOM"));
    }

    #[test]
    fn test_eq() {
        assert!(check_test_eq("if [ foo == bar ]; then"));

        /* XXX: No support for whitespace in variable at present.  */
        assert!(!check_test_eq("if [ 'foo bar' == ojnk ]; then"));

        /*
         * Misc non-matches.
         */
        assert!(!check_test_eq(""));
        assert!(!check_test_eq("foo == bar"));
        assert!(!check_test_eq("if foo == bar"));
        assert!(!check_test_eq("if [ foo = bar ]; then"));
    }
}