Sun Jul 9 04:54:00 2017 UTC ()
Ensure that command output is always followed by newline,
even when filemon is not being used.

Tweak MAKE_META_IGNORE_PATTERNS matching to avoid using path name
with :L as it does not handle ':' in path names.

fgetLine: an extra check to avoid shrinking the buffer.


(sjg)
diff -r1.67 -r1.68 src/usr.bin/make/meta.c

cvs diff -r1.67 -r1.68 src/usr.bin/make/meta.c (expand / switch to unified diff)

--- src/usr.bin/make/meta.c 2016/08/17 15:52:42 1.67
+++ src/usr.bin/make/meta.c 2017/07/09 04:54:00 1.68
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: meta.c,v 1.67 2016/08/17 15:52:42 sjg Exp $ */ 1/* $NetBSD: meta.c,v 1.68 2017/07/09 04:54:00 sjg Exp $ */
2 2
3/* 3/*
4 * Implement 'meta' mode. 4 * Implement 'meta' mode.
5 * Adapted from John Birrell's patches to FreeBSD make. 5 * Adapted from John Birrell's patches to FreeBSD make.
6 * --sjg 6 * --sjg
7 */ 7 */
8/* 8/*
9 * Copyright (c) 2009-2016, Juniper Networks, Inc. 9 * Copyright (c) 2009-2016, Juniper Networks, Inc.
10 * Portions Copyright (c) 2009, John Birrell. 10 * Portions Copyright (c) 2009, John Birrell.
11 *  11 *
12 * Redistribution and use in source and binary forms, with or without 12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions  13 * modification, are permitted provided that the following conditions
14 * are met:  14 * are met:
@@ -713,27 +713,27 @@ void @@ -713,27 +713,27 @@ void
713meta_job_error(Job *job, GNode *gn, int flags, int status) 713meta_job_error(Job *job, GNode *gn, int flags, int status)
714{ 714{
715 char cwd[MAXPATHLEN]; 715 char cwd[MAXPATHLEN];
716 BuildMon *pbm; 716 BuildMon *pbm;
717 717
718 if (job != NULL) { 718 if (job != NULL) {
719 pbm = &job->bm; 719 pbm = &job->bm;
720 if (!gn) 720 if (!gn)
721 gn = job->node; 721 gn = job->node;
722 } else { 722 } else {
723 pbm = &Mybm; 723 pbm = &Mybm;
724 } 724 }
725 if (pbm->mfp != NULL) { 725 if (pbm->mfp != NULL) {
726 fprintf(pbm->mfp, "*** Error code %d%s\n", 726 fprintf(pbm->mfp, "\n*** Error code %d%s\n",
727 status, 727 status,
728 (flags & JOB_IGNERR) ? 728 (flags & JOB_IGNERR) ?
729 "(ignored)" : ""); 729 "(ignored)" : "");
730 } 730 }
731 if (gn) { 731 if (gn) {
732 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0); 732 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
733 } 733 }
734 getcwd(cwd, sizeof(cwd)); 734 getcwd(cwd, sizeof(cwd));
735 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0); 735 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
736 if (pbm->meta_fname[0]) { 736 if (pbm->meta_fname[0]) {
737 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0); 737 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
738 } 738 }
739 meta_job_finish(job); 739 meta_job_finish(job);
@@ -768,42 +768,43 @@ meta_job_output(Job *job, char *cp, cons @@ -768,42 +768,43 @@ meta_job_output(Job *job, char *cp, cons
768 cp = strchr(cp+1, '\n'); 768 cp = strchr(cp+1, '\n');
769 if (!cp++) 769 if (!cp++)
770 return; 770 return;
771 } 771 }
772 } 772 }
773 fprintf(pbm->mfp, "%s%s", cp, nl); 773 fprintf(pbm->mfp, "%s%s", cp, nl);
774 } 774 }
775} 775}
776 776
777int 777int
778meta_cmd_finish(void *pbmp) 778meta_cmd_finish(void *pbmp)
779{ 779{
780 int error = 0; 780 int error = 0;
781#ifdef USE_FILEMON 
782 BuildMon *pbm = pbmp; 781 BuildMon *pbm = pbmp;
783 int x; 782 int x;
784 783
785 if (!pbm) 784 if (!pbm)
786 pbm = &Mybm; 785 pbm = &Mybm;
787 786
 787#ifdef USE_FILEMON
788 if (pbm->filemon_fd >= 0) { 788 if (pbm->filemon_fd >= 0) {
789 if (close(pbm->filemon_fd) < 0) 789 if (close(pbm->filemon_fd) < 0)
790 error = errno; 790 error = errno;
791 x = filemon_read(pbm->mfp, pbm->mon_fd); 791 x = filemon_read(pbm->mfp, pbm->mon_fd);
792 if (error == 0 && x != 0) 792 if (error == 0 && x != 0)
793 error = x; 793 error = x;
794 pbm->filemon_fd = pbm->mon_fd = -1; 794 pbm->filemon_fd = pbm->mon_fd = -1;
795 } 795 } else
796#endif 796#endif
 797 fprintf(pbm->mfp, "\n"); /* ensure end with newline */
797 return error; 798 return error;
798} 799}
799 800
800int 801int
801meta_job_finish(Job *job) 802meta_job_finish(Job *job)
802{ 803{
803 BuildMon *pbm; 804 BuildMon *pbm;
804 int error = 0; 805 int error = 0;
805 int x; 806 int x;
806 807
807 if (job != NULL) { 808 if (job != NULL) {
808 pbm = &job->bm; 809 pbm = &job->bm;
809 } else { 810 } else {
@@ -847,26 +848,28 @@ fgetLine(char **bufp, size_t *szp, int o @@ -847,26 +848,28 @@ fgetLine(char **bufp, size_t *szp, int o
847 if (buf[x - 1] == '\n') 848 if (buf[x - 1] == '\n')
848 return x; 849 return x;
849 /* 850 /*
850 * We need to grow the buffer. 851 * We need to grow the buffer.
851 * The meta file can give us a clue. 852 * The meta file can give us a clue.
852 */ 853 */
853 if (fstat(fileno(fp), &fs) == 0) { 854 if (fstat(fileno(fp), &fs) == 0) {
854 size_t newsz; 855 size_t newsz;
855 char *p; 856 char *p;
856 857
857 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ); 858 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
858 if (newsz <= bufsz) 859 if (newsz <= bufsz)
859 newsz = ROUNDUP(fs.st_size, BUFSIZ); 860 newsz = ROUNDUP(fs.st_size, BUFSIZ);
 861 if (newsz <= bufsz)
 862 return x; /* truncated */
860 if (DEBUG(META))  863 if (DEBUG(META))
861 fprintf(debug_file, "growing buffer %zu -> %zu\n", 864 fprintf(debug_file, "growing buffer %zu -> %zu\n",
862 bufsz, newsz); 865 bufsz, newsz);
863 p = bmake_realloc(buf, newsz); 866 p = bmake_realloc(buf, newsz);
864 if (p) { 867 if (p) {
865 *bufp = buf = p; 868 *bufp = buf = p;
866 *szp = bufsz = newsz; 869 *szp = bufsz = newsz;
867 /* fetch the rest */ 870 /* fetch the rest */
868 if (!fgets(&buf[x], bufsz - x, fp)) 871 if (!fgets(&buf[x], bufsz - x, fp))
869 return x; /* truncated! */ 872 return x; /* truncated! */
870 goto check_newline; 873 goto check_newline;
871 } 874 }
872 } 875 }
@@ -934,30 +937,30 @@ meta_ignore(GNode *gn, const char *p) @@ -934,30 +937,30 @@ meta_ignore(GNode *gn, const char *p)
934 if (Lst_ForEach(metaIgnorePaths, prefix_match, fname)) { 937 if (Lst_ForEach(metaIgnorePaths, prefix_match, fname)) {
935#ifdef DEBUG_META_MODE 938#ifdef DEBUG_META_MODE
936 if (DEBUG(META)) 939 if (DEBUG(META))
937 fprintf(debug_file, "meta_oodate: ignoring path: %s\n", 940 fprintf(debug_file, "meta_oodate: ignoring path: %s\n",
938 p); 941 p);
939#endif 942#endif
940 return TRUE; 943 return TRUE;
941 } 944 }
942 } 945 }
943 946
944 if (metaIgnorePatterns) { 947 if (metaIgnorePatterns) {
945 char *pm; 948 char *pm;
946 949
947 snprintf(fname, sizeof(fname), 950 Var_Set(".p.", p, gn, 0);
948 "${%s:@m@${%s:L:M$m}@}", 951 pm = Var_Subst(NULL,
949 MAKE_META_IGNORE_PATTERNS, p); 952 "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}",
950 pm = Var_Subst(NULL, fname, gn, VARF_WANTRES); 953 gn, VARF_WANTRES);
951 if (*pm) { 954 if (*pm) {
952#ifdef DEBUG_META_MODE 955#ifdef DEBUG_META_MODE
953 if (DEBUG(META)) 956 if (DEBUG(META))
954 fprintf(debug_file, "meta_oodate: ignoring pattern: %s\n", 957 fprintf(debug_file, "meta_oodate: ignoring pattern: %s\n",
955 p); 958 p);
956#endif 959#endif
957 free(pm); 960 free(pm);
958 return TRUE; 961 return TRUE;
959 } 962 }
960 free(pm); 963 free(pm);
961 } 964 }
962 965
963 if (metaIgnoreFilter) { 966 if (metaIgnoreFilter) {