Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/xlint lint: sort functions in calling order



details:   https://anonhg.NetBSD.org/src/rev/c0662f2e18df
branches:  trunk
changeset: 376293:c0662f2e18df
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 09 13:31:11 2023 +0000

description:
lint: sort functions in calling order

No functional change.

diffstat:

 usr.bin/xlint/xlint/xlint.c |  523 +++++++++++++++++++++----------------------
 1 files changed, 259 insertions(+), 264 deletions(-)

diffs (truncated from 583 to 300 lines):

diff -r 38f24f5563f7 -r c0662f2e18df usr.bin/xlint/xlint/xlint.c
--- a/usr.bin/xlint/xlint/xlint.c       Fri Jun 09 13:03:49 2023 +0000
+++ b/usr.bin/xlint/xlint/xlint.c       Fri Jun 09 13:31:11 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: xlint.c,v 1.110 2023/06/09 13:03:49 rillig Exp $ */
+/* $NetBSD: xlint.c,v 1.111 2023/06/09 13:31:11 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: xlint.c,v 1.110 2023/06/09 13:03:49 rillig Exp $");
+__RCSID("$NetBSD: xlint.c,v 1.111 2023/06/09 13:31:11 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -127,11 +127,6 @@ static     const   char *currfn;
 #endif
 static const char target_prefix[] = TARGET_PREFIX;
 
-static void    handle_filename(const char *);
-static void    run_child(const char *, list *, const char *, int);
-static void    find_libs(const list *);
-static bool    file_is_readable(const char *);
-static void    cat(const list *, const char *);
 
 static void
 list_add_ref(list *l, char *s)
@@ -197,6 +192,17 @@ concat2(const char *s1, const char *s2)
        return s;
 }
 
+static void
+set_tmpdir(void)
+{
+       const char *tmp;
+       size_t len;
+
+       tmpdir = (tmp = getenv("TMPDIR")) != NULL && (len = strlen(tmp)) != 0
+           ? concat2(tmp, tmp[len - 1] == '/' ? "" : "/")
+           : xstrdup(_PATH_TMP);
+}
+
 /* Clean up after a signal or at the regular end. */
 static void __attribute__((__noreturn__))
 terminate(int signo)
@@ -226,32 +232,6 @@ terminate(int signo)
        exit(signo != 0 ? 1 : 0);
 }
 
-/*
- * Returns a pointer to the last component of path after delim.
- * Returns path if the string does not contain delim.
- */
-static const char *
-lbasename(const char *path, int delim)
-{
-
-       const char *base = path;
-       for (const char *p = path; *p != '\0'; p++)
-               if (*p == delim)
-                       base = p + 1;
-       return base;
-}
-
-static void
-set_tmpdir(void)
-{
-       const char *tmp;
-       size_t len;
-
-       tmpdir = (tmp = getenv("TMPDIR")) != NULL && (len = strlen(tmp)) != 0
-           ? concat2(tmp, tmp[len - 1] == '/' ? "" : "/")
-           : xstrdup(_PATH_TMP);
-}
-
 static void __attribute__((__noreturn__, __format__(__printf__, 1, 2)))
 usage(const char *fmt, ...)
 {
@@ -282,6 +262,113 @@ usage(const char *fmt, ...)
        terminate(-1);
 }
 
+/*
+ * Returns a pointer to the last component of path after delim.
+ * Returns path if the string does not contain delim.
+ */
+static const char *
+lbasename(const char *path, int delim)
+{
+
+       const char *base = path;
+       for (const char *p = path; *p != '\0'; p++)
+               if (*p == delim)
+                       base = p + 1;
+       return base;
+}
+
+static bool
+is_safe_shell(char ch)
+{
+
+       return ch_isalnum(ch) || ch == '%' || ch == '+' || ch == ',' ||
+              ch == '-' || ch == '.' || ch == '/' || ch == ':' ||
+              ch == '=' || ch == '@' || ch == '_';
+}
+
+static void
+print_sh_quoted(const char *s)
+{
+
+       if (s[0] == '\0')
+               goto needs_quoting;
+       for (const char *p = s; *p != '\0'; p++)
+               if (!is_safe_shell(*p))
+                       goto needs_quoting;
+
+       (void)printf("%s", s);
+       return;
+
+needs_quoting:
+       (void)putchar('\'');
+       for (const char *p = s; *p != '\0'; p++) {
+               if (*p == '\'')
+                       (void)printf("'\\''");
+               else
+                       (void)putchar(*p);
+       }
+       (void)putchar('\'');
+}
+
+static void
+run_child(const char *path, list *args, const char *crfn, int fdout)
+{
+       int status, rv, signo;
+
+       if (Vflag) {
+               print_sh_quoted(args->items[0]);
+               for (size_t i = 1; i < args->len - 1; i++) {
+                       (void)printf(" ");
+                       print_sh_quoted(args->items[i]);
+               }
+               (void)printf("\n");
+       }
+
+       currfn = crfn;
+
+       (void)fflush(stdout);
+
+       switch (vfork()) {
+       case -1:
+               warn("cannot fork");
+               terminate(-1);
+               /* NOTREACHED */
+       default:
+               /* parent */
+               break;
+       case 0:
+               /* child */
+
+               /* set up the standard output if necessary */
+               if (fdout != -1) {
+                       (void)dup2(fdout, STDOUT_FILENO);
+                       (void)close(fdout);
+               }
+               (void)execvp(path, args->items);
+               warn("cannot exec %s", path);
+               _exit(1);
+               /* NOTREACHED */
+       }
+
+       while ((rv = wait(&status)) == -1 && errno == EINTR) ;
+       if (rv == -1) {
+               warn("wait");
+               terminate(-1);
+       }
+       if (WIFSIGNALED(status)) {
+               signo = WTERMSIG(status);
+#if HAVE_DECL_SYS_SIGNAME
+               warnx("%s got SIG%s", path, sys_signame[signo]);
+#else
+               warnx("%s got signal %d", path, signo);
+#endif
+               terminate(-1);
+       }
+       if (WEXITSTATUS(status) != 0)
+               terminate(-1);
+       currfn = NULL;
+}
+
 static void
 run_cpp(const char *name)
 {
@@ -338,6 +425,112 @@ run_lint1(const char *out_fname)
        list_clear(&args);
 }
 
+/*
+ * Read a file name from the command line
+ * and pass it through lint1 if it is a C source.
+ */
+static void
+handle_filename(const char *name)
+{
+       const char *bn, *suff;
+       char *ofn;
+       size_t len;
+       int fd;
+
+       bn = lbasename(name, '/');
+       suff = lbasename(bn, '.');
+
+       if (strcmp(suff, "ln") == 0) {
+               /* only for lint2 */
+               if (!iflag)
+                       list_add(&lint2.infiles, name);
+               return;
+       }
+
+       if (strcmp(suff, "c") != 0 &&
+           (strncmp(bn, "llib-l", 6) != 0 || bn != suff)) {
+               warnx("unknown file type: %s", name);
+               return;
+       }
+
+       if (!iflag || !first)
+               (void)printf("%s:\n", Fflag ? name : bn);
+
+       /* build the name of the output file of lint1 */
+       if (oflag) {
+               ofn = outputfn;
+               outputfn = NULL;
+               oflag = false;
+       } else if (iflag) {
+               len = bn == suff ? strlen(bn) : (size_t)((suff - 1) - bn);
+               ofn = xasprintf("%.*s.ln", (int)len, bn);
+       } else {
+               ofn = xasprintf("%slint1.XXXXXX", tmpdir);
+               fd = mkstemp(ofn);
+               if (fd == -1) {
+                       warn("can't make temp");
+                       terminate(-1);
+               }
+               (void)close(fd);
+       }
+       if (!iflag)
+               list_add(&lint1.outfiles, ofn);
+
+       run_cpp(name);
+       run_lint1(ofn);
+
+       list_add(&lint2.infiles, ofn);
+       free(ofn);
+}
+
+static bool
+file_is_readable(const char *path)
+{
+       struct stat sbuf;
+
+       if (stat(path, &sbuf) == -1)
+               return false;
+       if (!S_ISREG(sbuf.st_mode))
+               return false;
+       if (access(path, R_OK) == -1)
+               return false;
+       return true;
+}
+
+static void
+find_lib(const char *lib)
+{
+       char *lfn;
+
+       for (size_t i = 0; i < libsrchpath.len; i++) {
+               const char *dir = libsrchpath.items[i];
+               lfn = xasprintf("%s/llib-l%s.ln", dir, lib);
+               if (file_is_readable(lfn))
+                       goto found;
+               free(lfn);
+
+               lfn = xasprintf("%s/lint/llib-l%s.ln", dir, lib);
+               if (file_is_readable(lfn))
+                       goto found;
+               free(lfn);
+       }
+
+       warnx("cannot find llib-l%s.ln", lib);
+       return;
+
+found:
+       list_add_ref(&lint2.inlibs, concat2("-l", lfn));
+       free(lfn);
+}
+
+static void
+find_libs(const list *l)
+{



Home | Main Index | Thread Index | Old Index