LCOV - code coverage report
Current view: top level - testing - difflib.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 395 472 83.7 %
Date: 2018-07-12 00:10:09 Functions: 9 12 75.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2018 MonetDB B.V.
       7             :  */
       8             : 
       9             : #include "monetdb_config.h"
      10             : #include "helpers.h"
      11             : #include "difflib.h"
      12             : 
      13             : #include <string.h>
      14             : #include <sys/types.h>
      15             : #include <sys/stat.h>
      16             : #include <fcntl.h>
      17             : #include <ctype.h>
      18             : #ifdef HAVE_UNISTD_H
      19             : # include <unistd.h>
      20             : #endif
      21             : #ifdef HAVE_IO_H
      22             : # include <io.h>
      23             : #endif
      24             : #include <time.h>
      25             : 
      26             : #ifdef NATIVE_WIN32
      27             : #include <windows.h>
      28             : #define DIFF    "diff"                /* --binary */
      29             : #define COPY    "copy /y"     /* "cp -f" */
      30             : #define popen _popen
      31             : #define pclose _pclose
      32             : 
      33             : #ifndef DIR_SEP
      34             : # define DIR_SEP '\\'
      35             : #endif
      36             : 
      37             : #define getpid _getpid
      38             : #else
      39             : #ifndef DIFF
      40             : #define DIFF    "diff"
      41             : #endif
      42             : 
      43             : #ifndef DIR_SEP
      44             : # define DIR_SEP '/'
      45             : #endif
      46             : #endif
      47             : 
      48             : #ifdef DEBUG
      49             : #define UNLINK(x)
      50             : #define ERRHNDL(r,s,t,u) ErrXit(s,t,u)
      51             : #else
      52             : #define UNLINK(x) remove(x)
      53             : #define ERRHNDL(r,s,t,u) return r
      54             : #endif
      55             : 
      56             : #define SETBLACK(f)  if(clr[f]!=0  ) { fprintf(clmn_fp[f],"</span>"); clr[f]=0;   }
      57             : #define SETBLUE(f,m) if(clr[f]!=3-m) { if (clr[f]) fprintf(clmn_fp[f], "</span>"); fprintf(clmn_fp[f],"<span class='%sblue'>",(m?"light":"")); clr[f]=3-m; }
      58             : #define SETRED(f,m)  if(clr[f]!=5-m) { if (clr[f]) fprintf(clmn_fp[f], "</span>"); fprintf(clmn_fp[f],"<span class='%sred'>",(m?"light":"")); clr[f]=5-m; }
      59             : #define SETPINK(f,m) if(clr[f]!=7-m) { if (clr[f]) fprintf(clmn_fp[f], "</span>"); fprintf(clmn_fp[f],"<span class='%spink'>",(m?"light":"")); clr[f]=7-m; }
      60             : 
      61             : #define CMDLEN  8192
      62             : #define BUFLEN  16384
      63             : #define BUFLEN2 32768
      64             : 
      65             : static char *
      66       29757 : HTMLsave(char *s)
      67             : {
      68             :         static char t[BUFLEN2];
      69       29757 :         char *p = t;
      70             : 
      71      215008 :         while (*s && p < t + BUFLEN2 - 7) {
      72      155494 :                 switch (*s) {
      73          11 :                 case '<':
      74          11 :                         *p++ = '&';
      75          11 :                         *p++ = 'l';
      76          11 :                         *p++ = 't';
      77          11 :                         *p++ = ';';
      78          11 :                         break;
      79         159 :                 case '>':
      80         159 :                         *p++ = '&';
      81         159 :                         *p++ = 'g';
      82         159 :                         *p++ = 't';
      83         159 :                         *p++ = ';';
      84         159 :                         break;
      85           0 :                 case '&':
      86           0 :                         *p++ = '&';
      87           0 :                         *p++ = 'a';
      88           0 :                         *p++ = 'm';
      89           0 :                         *p++ = 'p';
      90           0 :                         *p++ = ';';
      91           0 :                         break;
      92      155324 :                 default:
      93      155324 :                         *p++ = *s;
      94      155324 :                         break;
      95             :                 }
      96      155494 :                 s++;
      97             :         }
      98       29757 :         *p++ = 0;
      99             : 
     100       29757 :         return t;
     101             : }
     102             : 
     103             : 
     104             : static void
     105             : markNL(FILE *fp, int k)
     106             : {
     107             :         int i;
     108             : 
     109       10460 :         for (i = 0; i < 6; i++)
     110        9588 :                 fprintf(fp, "@+-%06i\n", k);
     111             : }
     112             : 
     113             : 
     114             : FILE *
     115        5204 : oldnew2u_diff(int mindiff, int context, char *ignore, char *function, char *old_fn, char *new_fn)
     116             : {
     117             :         char command[CMDLEN];
     118        5204 :         char *_d = mindiff ? "-d" : "";
     119             : 
     120             :         TRACE(fprintf(STDERR, "oldnew2u_diff(%i,%i,%s,%s,%s,%s)\n", mindiff, context, ignore, function, old_fn, new_fn));
     121             : 
     122        5204 :         snprintf(command, sizeof(command), "%s %s %s -a %s -U%d %s    %s", DIFF, ignore, function, _d, context, old_fn, new_fn);
     123        5204 :         return popen(command, "r");
     124             : }
     125             : 
     126             : /* oldnew2u_diff */
     127             : 
     128             : 
     129             : int
     130        5204 : u_diff2l_diff(FILE *u_diff_fp, char *l_diff_fn)
     131             : {
     132             :         FILE *l_diff_fp;
     133             :         char *ok, line[BUFLEN];
     134             : 
     135             :         TRACE(fprintf(STDERR, "u_diff2l_diff(%p,%s)\n", u_diff_fp, l_diff_fn));
     136             : 
     137        5204 :         if (!(ok = fgets(line, BUFLEN, u_diff_fp))) {
     138        5174 :                 pclose(u_diff_fp);
     139        5174 :                 ERRHNDL(0, "empty file in u_diff2l_diff:", "pipe", 1);
     140             :         }
     141             : 
     142          30 :         l_diff_fp = Wfopen(l_diff_fn);
     143         120 :         while (ok && strncmp(line, "@@ -", 4)) {
     144          60 :                 fprintf(l_diff_fp, "%s", line);
     145          60 :                 ok = fgets(line, BUFLEN, u_diff_fp);
     146             :         }
     147         193 :         while (ok) {
     148             :                 do {
     149         163 :                         fprintf(l_diff_fp, "%s", line);
     150         163 :                 } while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, u_diff_fp)));
     151        3726 :                 while (ok && (ok = fgets(line, BUFLEN, u_diff_fp)) && strchr(" -+", line[0])) {
     152        1700 :                         char l0 = line[0];
     153        1700 :                         if (line[1] == '\n')
     154         101 :                                 snprintf(line + 1, sizeof(line) - 1, "\2\n");
     155             :                         do {
     156        1700 :                                 fprintf(l_diff_fp, "%s", line);
     157        1700 :                         } while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, u_diff_fp)));
     158        1700 :                         fprintf(l_diff_fp, "%c\1\n", l0);
     159             :                 }
     160             :         }
     161          30 :         fflush(l_diff_fp);
     162          30 :         fclose(l_diff_fp);
     163             : 
     164          30 :         pclose(u_diff_fp);
     165          30 :         return 1;
     166             : }
     167             : 
     168             : /* u_diff2l_diff */
     169             : 
     170             : 
     171             : static int
     172          15 : lw_diff2wc_diff(int mindiff, int doChar, char *lw_diff_fn, char *wc_diff_fn)
     173             : {
     174             :         FILE *lw_diff_fp, *wc_diff_fp, *fp[2], *pipe_fp;
     175             :         char line[BUFLEN], command[CMDLEN], pipe_ln[BUFLEN];
     176             :         char *ok, *fn[2];
     177             :         size_t i;
     178             :         int j;
     179             :         int space, alpha_, digit, l[2], k[2];
     180             :         char wc_old_fn[CMDLEN], wc_new_fn[CMDLEN];
     181          15 :         char *_d = mindiff ? "-d" : "";
     182             : 
     183             :         TRACE(fprintf(STDERR, "lw_diff2wc_diff(%i,%i,%s,%s)\n", mindiff, doChar, lw_diff_fn, wc_diff_fn));
     184             : 
     185          15 :         lw_diff_fp = Rfopen(lw_diff_fn);
     186          15 :         if (!(ok = fgets(line, BUFLEN, lw_diff_fp))) {
     187           0 :                 fclose(lw_diff_fp);
     188           0 :                 ERRHNDL(0, "empty file in lw_diff2wc_diff:", lw_diff_fn, 1);
     189             :         }
     190             : 
     191          30 :         snprintf(wc_old_fn, sizeof(wc_old_fn), "%s%c.difflib-%ld-lw_diff2wc_diff-old", tmpdir(), DIR_SEP, (long) getpid());
     192          15 :         fn[0] = wc_old_fn;
     193          30 :         snprintf(wc_new_fn, sizeof(wc_new_fn), "%s%c.difflib-%ld-lw_diff2wc_diff-new", tmpdir(), DIR_SEP, (long) getpid());
     194          15 :         fn[1] = wc_new_fn;
     195             : 
     196          15 :         wc_diff_fp = Wfopen(wc_diff_fn);
     197          60 :         while (ok && strncmp(line, "@@ -", 4)) {
     198          30 :                 fprintf(wc_diff_fp, "%s", line);
     199          30 :                 ok = fgets(line, BUFLEN, lw_diff_fp);
     200             :         }
     201          15 :         fflush(wc_diff_fp);
     202          15 :         fclose(wc_diff_fp);
     203             : 
     204         158 :         while (ok) {
     205         128 :                 wc_diff_fp = Afopen(wc_diff_fn);
     206             :                 do {
     207         128 :                         fprintf(wc_diff_fp, "%s", line);
     208         128 :                 } while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, lw_diff_fp)));
     209         128 :                 fflush(wc_diff_fp);
     210         128 :                 fclose(wc_diff_fp);
     211             : 
     212         128 :                 l[0] = l[1] = k[0] = k[1] = 0;
     213         384 :                 for (j = 0; j < 2; j++)
     214         256 :                         fp[j] = Wfopen(fn[j]);
     215        4428 :                 while (ok && (ok = fgets(line, BUFLEN, lw_diff_fp)) && strchr(" -+", line[0])) {
     216        2086 :                         if (line[0] == ' ') {
     217         634 :                                 char l1 = line[1];
     218        1362 :                                 while (k[0] < k[1]) {
     219         188 :                                         markNL(fp[0], k[0]++);
     220          94 :                                         l[0]++;
     221             :                                 }
     222         778 :                                 while (k[1] < k[0]) {
     223         288 :                                         markNL(fp[1], k[1]++);
     224         144 :                                         l[1]++;
     225             :                                 }
     226             :                                 i = 1;
     227             :                                 do {
     228        1902 :                                         for (j = 0; j < 2; j++) {
     229        2536 :                                                 fprintf(fp[j], "%s", line+i);
     230             :                                         }
     231         634 :                                         i = 0;
     232         634 :                                 } while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, lw_diff_fp)));
     233        1902 :                                 for (j = 0; j < 2; j++) {
     234        1268 :                                         l[j]++;
     235             :                                 }
     236         634 :                                 if (l1 == '\1')
     237         634 :                                         for (j = 0; j < 2; j++) {
     238        1268 :                                                 markNL(fp[j], k[j]++);
     239         634 :                                                 l[j]++;
     240             :                                         }
     241             :                         } else {
     242        1452 :                                 if (line[0] == '-')
     243             :                                         j = 0;
     244             :                                 else
     245         676 :                                         j = 1;
     246        1452 :                                 if (line[1] == '\1') {
     247        1452 :                                         fprintf(fp[j], "\1\n");
     248        1452 :                                         markNL(fp[j], k[j]++);
     249         726 :                                         l[j] += 2;
     250         726 :                                 } else if (doChar) {
     251             :                                         i = 1;
     252             :                                         do {
     253           0 :                                                 for (; line[i] != '\n' && line[i] != '\0'; i++) {
     254           0 :                                                         fprintf(fp[j], "%c\n", line[i]);
     255           0 :                                                         l[j]++;
     256             :                                                 }
     257           0 :                                                 i = 0;
     258           0 :                                         } while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, lw_diff_fp)));
     259             :                                 } else {
     260         726 :                                         space = isspace((unsigned char) (line[1]));
     261         726 :                                         alpha_ = isalpha((unsigned char) (line[1]));
     262         726 :                                         digit = isdigit((unsigned char) (line[1]));
     263         726 :                                         i = 1;
     264             :                                         do {
     265       62783 :                                                 for (; line[i] != '\n' && line[i] != '\0'; i++) {
     266       62783 :                                                         if ((space && !isspace((unsigned char) line[i])) ||
     267       46629 :                                                             (!space && isspace((unsigned char) line[i])) ||
     268       30220 :                                                             (alpha_ && !isalpha((unsigned char) line[i])) ||
     269       20747 :                                                             (!alpha_ && isalpha((unsigned char) line[i])) ||
     270        1722 :                                                             (digit && !isdigit((unsigned char) line[i])) ||
     271       77806 :                                                             (!digit && isdigit((unsigned char) line[i])) ||
     272       39228 :                                                             (!isspace((unsigned char) line[i]) &&
     273        5054 :                                                              !isalpha((unsigned char) line[i]) &&
     274             :                                                              !isdigit((unsigned char) line[i]))) {
     275       55064 :                                                                 fprintf(fp[j], "\n");
     276       27532 :                                                                 space = isspace((unsigned char) line[i]);
     277       27532 :                                                                 alpha_ = isalpha((unsigned char) line[i]);
     278       27532 :                                                                 digit = isdigit((unsigned char) line[i]);
     279       27532 :                                                                 l[j]++;
     280             :                                                         }
     281      125566 :                                                         fprintf(fp[j], "%c", line[i]);
     282             :                                                 }
     283         726 :                                                 i = 0;
     284         726 :                                         } while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, lw_diff_fp)));
     285        1452 :                                         fprintf(fp[j], "\n");
     286         726 :                                         l[j]++;
     287             :                                 }
     288             :                         }
     289             :                 }
     290         384 :                 for (j = 0; j < 2; j++) {
     291         256 :                         fflush(fp[j]);
     292         256 :                         fclose(fp[j]);
     293             :                 }
     294             : 
     295             : /*
     296             :       snprintf(command, sizeof(command),
     297             :               "%s -a %s -u%i %s %s | egrep -v '^(@@ \\-|\\+\\+\\+ |\\-\\-\\- |[ \\+\\-]@\\+\\-)' >> %s",
     298             :               DIFF,_d,MAX(l[0],l[1]),fn[0],fn[1],wc_diff_fn);
     299             :       SYSTEM(command);
     300             : */
     301             : 
     302         256 :                 snprintf(command, sizeof(command), "%s -a %s -U%d %s %s", DIFF, _d, MAX(l[0], l[1]), fn[0], fn[1]);
     303             : 
     304         128 :                 pipe_fp = popen(command, "r");
     305             : 
     306         128 :                 wc_diff_fp = Afopen(wc_diff_fn);
     307       25964 :                 while (fgets(pipe_ln, BUFLEN, pipe_fp)) {
     308       51288 :                         if (strncmp(pipe_ln, "@@ -", 4) &&
     309       51032 :                             strncmp(pipe_ln, "+++ ", 4) &&
     310       50776 :                             strncmp(pipe_ln, "--- ", 4) &&
     311       46322 :                             strncmp(pipe_ln, " @+-", 4) &&
     312       41528 :                             strncmp(pipe_ln, "+@+-", 4) &&
     313       20530 :                             strncmp(pipe_ln, "-@+-", 4)) {
     314             :                                 fprintf(wc_diff_fp, "%s", pipe_ln);
     315             :                         }
     316             :                 }
     317         128 :                 fflush(wc_diff_fp);
     318         128 :                 fclose(wc_diff_fp);
     319         128 :                 pclose(pipe_fp);
     320             :         }
     321          15 :         UNLINK(wc_old_fn);
     322          15 :         UNLINK(wc_new_fn);
     323             : 
     324          15 :         fclose(lw_diff_fp);
     325          15 :         return 1;
     326             : }
     327             : 
     328             : /* lw_diff2wc_diff */
     329             : 
     330             : 
     331             : int
     332           0 : l_diff2w_diff(int mindiff, char *l_diff_fn, char *w_diff_fn)
     333             : {
     334             :         TRACE(fprintf(STDERR, "l_diff2w_diff(%i,%s,%s)\n", mindiff, l_diff_fn, w_diff_fn));
     335             : 
     336          15 :         return lw_diff2wc_diff(mindiff, 0, l_diff_fn, w_diff_fn);
     337             : }
     338             : 
     339             : /* l_diff2w_diff */
     340             : 
     341             : 
     342             : int
     343           0 : w_diff2c_diff(int mindiff, char *w_diff_fn, char *c_diff_fn)
     344             : {
     345             :         TRACE(fprintf(STDERR, "w_diff2c_diff(%i,%s,%s)\n", mindiff, w_diff_fn, c_diff_fn));
     346             : 
     347           0 :         return lw_diff2wc_diff(mindiff, 1, w_diff_fn, c_diff_fn);
     348             : }
     349             : 
     350             : /* w_diff2c_diff */
     351             : 
     352             : 
     353             : 
     354             : int
     355        5204 : oldnew2l_diff(int mindiff, int context, char *ignore, char *function, char *old_fn, char *new_fn, char *l_diff_fn)
     356             : {
     357             :         FILE *u_diff_fp;
     358        5204 :         int rtrn = 0;
     359             : 
     360             :         TRACE(fprintf(STDERR, "oldnew2l_diff(%i,%i,%s,%s,%s,%s,%s)\n", mindiff, context, ignore, function, old_fn, new_fn, l_diff_fn));
     361             : 
     362        5204 :         if ((u_diff_fp = oldnew2u_diff(mindiff, context, ignore, function, old_fn, new_fn)) == NULL) {
     363             :                 ERRHNDL(0, "oldnew2u_diff returns 0 in oldnew2l_diff", "", 1);
     364             :         }
     365             : 
     366        5204 :         rtrn = u_diff2l_diff(u_diff_fp, l_diff_fn);
     367             : 
     368        5204 :         return rtrn;
     369             : }
     370             : 
     371             : /* oldnew2l_diff */
     372             : 
     373             : 
     374             : int
     375        4043 : oldnew2w_diff(int mindiff, int context, char *ignore, char *function, char *old_fn, char *new_fn, char *w_diff_fn)
     376             : {
     377             :         char l_diff_fn[CMDLEN];
     378        4043 :         int rtrn = 0;
     379             : 
     380             :         TRACE(fprintf(STDERR, "oldnew2w_diff(%i,%i,%s,%s,%s,%s,%s)\n", mindiff, context, ignore, function, old_fn, new_fn, w_diff_fn));
     381             : 
     382        8086 :         snprintf(l_diff_fn, sizeof(l_diff_fn), "%s%c.difflib-%ld-oldnew2w_diff-l_diff", tmpdir(), DIR_SEP, (long) getpid());
     383             : 
     384        4043 :         if (!oldnew2l_diff(mindiff, context, ignore, function, old_fn, new_fn, l_diff_fn)) {
     385        4028 :                 UNLINK(l_diff_fn);
     386        4028 :                 ERRHNDL(0, "oldnew2l_diff returns 0 in oldnew2w_diff", "", 1);
     387             :         }
     388             : 
     389          15 :         rtrn = l_diff2w_diff(mindiff, l_diff_fn, w_diff_fn);
     390             : 
     391          15 :         UNLINK(l_diff_fn);
     392          15 :         return rtrn;
     393             : }
     394             : 
     395             : /* oldnew2w_diff */
     396             : 
     397             : 
     398             : int
     399           0 : oldnew2c_diff(int mindiff, int context, char *ignore, char *function, char *old_fn, char *new_fn, char *c_diff_fn)
     400             : {
     401             :         char w_diff_fn[CMDLEN];
     402           0 :         int rtrn = 0;
     403             : 
     404             :         TRACE(fprintf(STDERR, "oldnew2c_diff(%i,%i,%s,%s,%s,%s,%s)\n", mindiff, context, ignore, function, old_fn, new_fn, c_diff_fn));
     405             : 
     406           0 :         snprintf(w_diff_fn, sizeof(w_diff_fn), "%s%c.difflib-%ld-oldnew2c_diff-w_diff", tmpdir(), DIR_SEP, (long) getpid());
     407             : 
     408           0 :         if (!oldnew2w_diff(mindiff, context, ignore, function, old_fn, new_fn, w_diff_fn)) {
     409           0 :                 UNLINK(w_diff_fn);
     410           0 :                 ERRHNDL(0, "oldnew2w_diff returns 0 in oldnew2c_diff", "", 1);
     411             :         }
     412             : 
     413           0 :         rtrn = w_diff2c_diff(mindiff, w_diff_fn, c_diff_fn);
     414             : 
     415           0 :         UNLINK(w_diff_fn);
     416           0 :         return rtrn;
     417             : }
     418             : 
     419             : /* oldnew2c_diff */
     420             : 
     421             : 
     422             : int
     423        5204 : oldnew2lwc_diff(int mindiff, int LWC, int context, char *ignore, char *function, char *old_fn, char *new_fn, char *lwc_diff_fn)
     424             : {
     425             :         TRACE(fprintf(STDERR, "oldnew2lwc_diff(%i,%i,%i,%s,%s,%s,%s,%s)\n", mindiff, LWC, context, ignore, function, old_fn, new_fn, lwc_diff_fn));
     426             : 
     427        5204 :         switch (LWC) {
     428        1161 :         case 0:
     429        1161 :                 return oldnew2l_diff(mindiff, context, ignore, function, old_fn, new_fn, lwc_diff_fn);
     430        4043 :         case 1:
     431        4043 :                 return oldnew2w_diff(mindiff, context, ignore, function, old_fn, new_fn, lwc_diff_fn);
     432           0 :         case 2:
     433           0 :                 return oldnew2c_diff(mindiff, context, ignore, function, old_fn, new_fn, lwc_diff_fn);
     434           0 :         default:
     435           0 :                 ErrXit("oldnew2lwc_diff called with wrong LWC", "", 1);
     436             :         }
     437             :         return 0;
     438             : }
     439             : 
     440             : /* oldnew2lwc_diff */
     441             : 
     442             : 
     443             : static char *doctype = "\
     444             : <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'\n\
     445             :                       'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n\
     446             : ";
     447             : static char *stylesheet = "\
     448             : .blue { color: #0000ff; }\n\
     449             : .lightblue { color: #00aaff; }\n\
     450             : .red { color: #ff0000; }\n\
     451             : .lightred { color: #ffaa00; }\n\
     452             : .pink { color: #ff00ff; }\n\
     453             : .lightpink { color: #ffaaff; }\n\
     454             : .colhead { font-family: monospace; color: blue; font-size: medium; }\n\
     455             : caption { font-weight: bold; font-family: sans-serif; }\n\
     456             : body { background-color: white; color: black; }\n\
     457             : a:link { color: green; }\n\
     458             : a:visited { color: darkgreen; }\n\
     459             : a:active { color: lime; }\n\
     460             : td { color: black; font-size: xx-small; font-family: monospace; white-space: pre-wrap; vertical-align: baseline; }\n\
     461             : ";
     462             : 
     463             : int
     464        5204 : lwc_diff2html(char *old_fn, char *new_fn, char *lwc_diff_fn, char *html_fn, char *caption, char *revision)
     465             : {
     466             :         FILE *html_fp, *lwc_diff_fp, *clmn_fp[5];
     467             :         char line[BUFLEN], fn_clmn[CMDLEN], *clmn_fn[5], c[3], *ok;
     468        5204 :         char *old = NULL, *new = NULL, *old_time, *new_time, olns[24], nlns[24];
     469        5204 :         int oln, nln, orn, nrn, i, clr[5], newline, newline_, minor = 0, Minor = 0, Major = 0;
     470             : 
     471             :         TRACE(fprintf(STDERR, "lwc_diff2html(%s,%s,%s,%s)\n", lwc_diff_fn, html_fn, caption, revision));
     472             : 
     473        5204 :         lwc_diff_fp = Rfopen(lwc_diff_fn);
     474             : 
     475        5204 :         if (!(ok = fgets(line, BUFLEN, lwc_diff_fp)))
     476             :                 /*{ fclose(lwc_diff_fp); ERRHNDL(0,"empty file in lwc_diff2html:",lwc_diff_fn,1); } */
     477             :         {
     478        5174 :                 html_fp = Wfopen(html_fn);
     479             : /*
     480             :       fprintf(html_fp,"Content-type: text/html\n\n");
     481             : */
     482       10348 :                 fprintf(html_fp, "%s<html>\n<head>\n<style>\n%s</style>\n</head>\n<body>\n<table align='center' border='1' cellspacing='0' cellpadding='1'>\n", doctype, stylesheet);
     483        5174 :                 if (*caption)
     484             :                         fprintf(html_fp, "<caption>%s</caption>\n", caption);
     485        5174 :                 fprintf(html_fp, "<tr>");
     486        5174 :                 if (!new_fn)
     487             :                         fprintf(html_fp, "<th></th>");
     488       10348 :                 fprintf(html_fp, "<th class='colhead'><a href='%s'>%s%s</a></th>", filename(old_fn), filename(old_fn), revision);
     489        5174 :                 fprintf(html_fp, "<th></th>");
     490        5174 :                 if (new_fn)
     491             :                         fprintf(html_fp, "<th class='colhead'><a href='%s'>%s</a></th>", new_fn, new_fn);
     492        5174 :                 fprintf(html_fp, "</tr>\n");
     493        5174 :                 fprintf(html_fp, "<tr><th colspan='3' align='center'>No differences.</th></tr>\n");
     494        5174 :                 fprintf(html_fp, "</table>\n");
     495        5174 :                 fprintf(html_fp, "<hr/>\n");
     496        5174 :                 fprintf(html_fp, "</body>\n</html>\n");
     497        5174 :                 fprintf(html_fp, "<!--NoDiffs-->\n");
     498        5174 :                 fflush(html_fp);
     499        5174 :                 fclose(html_fp);
     500        5174 :                 return 0;
     501             :         }
     502             : 
     503          60 :         snprintf(fn_clmn, sizeof(fn_clmn), "%s%c.difflib-%ld-lwc_diff2html-clmn-0-0", tmpdir(), DIR_SEP, (long) getpid());
     504         180 :         for (i = 0; i < 5; i++) {
     505         150 :                 clmn_fn[i] = strdup(fn_clmn);
     506         150 :                 clmn_fn[i][strlen(clmn_fn[i]) - 3] += i;
     507             :         }
     508             : 
     509          30 :         html_fp = Wfopen(html_fn);
     510             : /*
     511             :   fprintf(html_fp,"Content-type: text/html\n\n");
     512             : */
     513          60 :         fprintf(html_fp, "%s<html>\n<head>\n<style>\n%s</style>\n</head>\n<body>\n", doctype, stylesheet);
     514          30 :         fprintf(html_fp, "<table align='center' border='1' cellspacing='0' cellpadding='1' rules='groups'>\n");
     515          30 :         if (*caption)
     516             :                 fprintf(html_fp, "<caption>%s</caption>\n", caption);
     517             : 
     518          30 :         fprintf(html_fp, "<colgroup/><colgroup/><colgroup/><colgroup/><colgroup/><colgroup/><colgroup/>\n");
     519             : 
     520          30 :         line[strlen(line) - 1] = '\0';
     521         120 :         while (ok && strncmp(line, "@@ -", 4)) {
     522          60 :                 if (!strncmp(line, "--- ", 4))
     523          30 :                         old = strdup(line + 4);
     524          30 :                 else if (!strncmp(line, "+++ ", 4))
     525          30 :                         new = strdup(line + 4);
     526             :                 else
     527           0 :                         fprintf(html_fp, "<tbody><tr><td colspan='7'>%s</td></tr></tbody>\n", HTMLsave(line));
     528          60 :                 ok = fgets(line, BUFLEN, lwc_diff_fp);
     529          60 :                 line[strlen(line) - 1] = '\0';
     530             :         }
     531          30 :         old_time = strchr(old, '\t');
     532          30 :         *old_time++ = '\0';
     533          30 :         new_time = strchr(new, '\t');
     534          30 :         *new_time++ = '\0';
     535          60 :         fprintf(html_fp, "<thead><tr><th colspan='3' align='center' class='colhead'><a href='%s'>%s%s</a> %s</th>", filename(old), filename(old_fn), revision, old_time);
     536          30 :         fprintf(html_fp, "<th></th>");
     537          30 :         fprintf(html_fp, "<th colspan='3' align='center' class='colhead'><a href='%s'>%s</a> %s</th></tr></thead>\n", new, new_fn, new_time);
     538          30 :         free(old);
     539          30 :         free(new);
     540         223 :         while (ok) {
     541             : 
     542         815 :                 for (i = 0; i < 5; i++)
     543         815 :                         clmn_fp[i] = Wfopen(clmn_fn[i]);
     544         163 :                 sscanf(line, "@@ -%s +%s @@", olns, nlns);
     545         163 :                 oln = atoi(olns);
     546         163 :                 nln = atoi(nlns);
     547         163 :                 if ((oln > 1) && (nln > 1)) {
     548         135 :                         fprintf(html_fp, "<tbody><tr><td align='center'>...</td>");
     549         135 :                         fprintf(html_fp, "<td align='center'>...</td>");
     550         135 :                         fprintf(html_fp, "<td align='center'>...</td>");
     551         135 :                         fprintf(html_fp, "<td></td>");
     552         135 :                         fprintf(html_fp, "<td align='center'>...</td>");
     553         135 :                         fprintf(html_fp, "<td align='center'>...</td>");
     554             :                         fprintf(html_fp, "<td align='center'>...</td></tr></tbody>\n");
     555             :                 }
     556         652 :                 for (i = 0; i < 3 && ok; i++)
     557         489 :                         ok = strchr(ok+1, '@');
     558         163 :                 if (ok && ok[1] == ' ')
     559         135 :                         fprintf(html_fp, "<td colspan='7' align='center'>%s</td>\n", HTMLsave(ok + 2));
     560         815 :                 for (i = 0; i < 5; i++)
     561         815 :                         clr[i] = 0;
     562         163 :                 orn = nrn = 0;
     563         163 :                 newline_ = 1;
     564         163 :                 newline = 1;
     565         163 :                 snprintf(c, sizeof(c), "  ");
     566         163 :                 ok = line;
     567       43241 :                 while (ok && (ok = fgets(line, BUFLEN, lwc_diff_fp)) && strchr(" -+", line[0])) {
     568       21376 :                         if (line[1] != '\3') {
     569       21376 :                                 size_t sl = strlen(line) - 1;
     570       21376 :                                 char nl = line[sl], l0, l1;
     571             : 
     572       21376 :                                 if (newline_ || newline)
     573        2906 :                                         Minor |= (minor = (strchr("#=\n\2", line[1]) != NULL));
     574       21376 :                                 line[sl] = '\0';
     575       21376 :                                 if (line[1] == '\2')
     576         101 :                                         snprintf(line + 1, sizeof(line) - 1, " ");
     577       21376 :                                 l0 = line[0];
     578       21376 :                                 l1 = line[1];
     579       21376 :                                 if (line[0] == ' ') {
     580       10336 :                                         if (newline && (nrn < orn)) {
     581         321 :                                                 while (nrn < orn) {
     582         678 :                                                         SETBLUE(1, minor);
     583         452 :                                                         fprintf(clmn_fp[1], "%i", oln++);
     584         452 :                                                         SETBLACK(1);
     585         452 :                                                         fprintf(clmn_fp[1], "\n");
     586         678 :                                                         SETBLUE(2, minor);
     587         452 :                                                         fprintf(clmn_fp[2], "-");
     588         452 :                                                         SETBLACK(2);
     589         452 :                                                         fprintf(clmn_fp[2], "\n");
     590         452 :                                                         fprintf(clmn_fp[3], "\n");
     591         452 :                                                         fprintf(clmn_fp[4], "\n");
     592         226 :                                                         nrn++;
     593             :                                                 }
     594             :                                         }
     595       11102 :                                         SETBLACK(0);
     596       11115 :                                         SETBLACK(4);
     597             :                                 }
     598       21376 :                                 if (line[0] == '-') {
     599        7361 :                                         c[0] = '-';
     600        9967 :                                         SETBLUE(0, minor);
     601             :                                 }
     602       21376 :                                 if (line[0] == '+') {
     603        3679 :                                         c[1] = '+';
     604        6671 :                                         SETRED(4, minor);
     605             :                                 }
     606       21376 :                                 if (line[1] != '\1') {
     607       19923 :                                         line[sl] = nl;
     608       19923 :                                         line[sl+1] = '\0';
     609       19923 :                                         i = 1;
     610             :                                         do {
     611       19923 :                                                 sl = strlen(line) - 1;
     612       19923 :                                                 nl = line[sl];
     613       19923 :                                                 if (nl == '\n') {
     614       19923 :                                                         line[sl] = '\0';
     615             :                                                 }
     616       19923 :                                                 if (strchr(" -", l0)) {
     617       16721 :                                                         fprintf(clmn_fp[0], "%s", HTMLsave(line + i));
     618             :                                                 }
     619       19923 :                                                 if (strchr(" +", l0)) {
     620       12901 :                                                         fprintf(clmn_fp[4], "%s", HTMLsave(line + i));
     621             :                                                 }
     622       19923 :                                                 i = 0;
     623       19923 :                                         } while (nl != '\n' && (ok = fgets(line, BUFLEN, lwc_diff_fp)));
     624       19923 :                                         if (strchr(" -", l0)) {
     625       16721 :                                                 Major |= (clr[0] & 1);
     626             :                                         }
     627       19923 :                                         if (strchr(" +", l0)) {
     628       12901 :                                                 Major |= (clr[4] & 1);
     629             :                                         }
     630             :                                 } else {
     631        1453 :                                         if (line[0] == '-') {
     632         678 :                                                 SETBLACK(0);
     633         678 :                                                 fprintf(clmn_fp[0], "\n");
     634         339 :                                                 orn++;
     635             :                                         }
     636        1453 :                                         if (line[0] == '+') {
     637         477 :                                                 if (orn > nrn) {
     638         339 :                                                         SETPINK(1, minor);
     639         226 :                                                         fprintf(clmn_fp[1], "%i", oln++);
     640         226 :                                                         SETBLACK(1);
     641         226 :                                                         fprintf(clmn_fp[1], "\n");
     642         339 :                                                         SETPINK(2, minor);
     643         226 :                                                         fprintf(clmn_fp[2], "!");
     644         226 :                                                         SETBLACK(2);
     645         226 :                                                         fprintf(clmn_fp[2], "\n");
     646         339 :                                                         SETPINK(3, minor);
     647         226 :                                                         fprintf(clmn_fp[3], "%i", nln++);
     648         226 :                                                         SETBLACK(3);
     649         113 :                                                         fprintf(clmn_fp[3], "\n");
     650             :                                                 } else {
     651         364 :                                                         SETBLACK(0);
     652         728 :                                                         fprintf(clmn_fp[0], "\n");
     653         364 :                                                         orn++;
     654         364 :                                                         SETBLACK(1);
     655         728 :                                                         fprintf(clmn_fp[1], "\n");
     656        1092 :                                                         SETRED(2, minor);
     657         728 :                                                         fprintf(clmn_fp[2], "+");
     658         728 :                                                         SETBLACK(2);
     659         728 :                                                         fprintf(clmn_fp[2], "\n");
     660        1092 :                                                         SETRED(3, minor);
     661         728 :                                                         fprintf(clmn_fp[3], "%i", nln++);
     662         728 :                                                         SETBLACK(3);
     663         364 :                                                         fprintf(clmn_fp[3], "\n");
     664             :                                                 }
     665         954 :                                                 SETBLACK(4);
     666         954 :                                                 fprintf(clmn_fp[4], "\n");
     667         477 :                                                 nrn++;
     668             :                                         }
     669        1453 :                                         if (line[0] == ' ') {
     670         637 :                                                 if (!strncmp(c, "  ", 2)) {
     671         369 :                                                         SETBLACK(1);
     672         369 :                                                         SETBLACK(2);
     673         369 :                                                         SETBLACK(3);
     674         369 :                                                         fprintf(clmn_fp[2], "\n");
     675             :                                                 } else {
     676         804 :                                                         SETPINK(1, minor);
     677         804 :                                                         SETPINK(3, minor);
     678             :                                                 }
     679         637 :                                                 if (!strncmp(c, "-+", 2)) {
     680         804 :                                                         SETPINK(2, minor);
     681         536 :                                                         fprintf(clmn_fp[2], "!");
     682         536 :                                                         SETBLACK(2);
     683         268 :                                                         fprintf(clmn_fp[2], "\n");
     684             :                                                 }
     685         637 :                                                 if (!strncmp(c, "- ", 2)) {
     686           0 :                                                         SETBLUE(2, minor);
     687           0 :                                                         fprintf(clmn_fp[2], "-");
     688           0 :                                                         SETBLACK(2);
     689           0 :                                                         fprintf(clmn_fp[2], "\n");
     690             :                                                 }
     691         637 :                                                 if (!strncmp(c, " +", 2)) {
     692           0 :                                                         SETRED(2, minor);
     693           0 :                                                         fprintf(clmn_fp[2], "+");
     694           0 :                                                         SETBLACK(2);
     695           0 :                                                         fprintf(clmn_fp[2], "\n");
     696             :                                                 }
     697        1274 :                                                 fprintf(clmn_fp[1], "%i", oln++);
     698         905 :                                                 SETBLACK(1);
     699        1274 :                                                 fprintf(clmn_fp[1], "\n");
     700        1274 :                                                 fprintf(clmn_fp[3], "%i", nln++);
     701         905 :                                                 SETBLACK(3);
     702        1274 :                                                 fprintf(clmn_fp[3], "\n");
     703         637 :                                                 SETBLACK(0);
     704        1274 :                                                 fprintf(clmn_fp[0], "\n");
     705         637 :                                                 SETBLACK(4);
     706         637 :                                                 fprintf(clmn_fp[4], "\n");
     707             :                                         }
     708             :                                         snprintf(c, sizeof(c), "  ");
     709             :                                 }
     710       21376 :                                 newline_ = newline;
     711       21376 :                                 newline = (l1 == '\1');
     712             :                         }
     713             :                 }
     714             : 
     715         163 :                 if (nrn < orn) {
     716           0 :                         SETBLACK(3);
     717           0 :                         SETBLACK(4);
     718           0 :                         while (nrn < orn) {
     719           0 :                                 SETBLUE(1, minor);
     720           0 :                                 fprintf(clmn_fp[1], "%i", oln++);
     721           0 :                                 SETBLACK(1);
     722           0 :                                 fprintf(clmn_fp[1], "\n");
     723           0 :                                 SETBLUE(2, minor);
     724           0 :                                 fprintf(clmn_fp[2], "-");
     725           0 :                                 SETBLACK(2);
     726           0 :                                 fprintf(clmn_fp[2], "\n");
     727           0 :                                 fprintf(clmn_fp[3], "\n");
     728           0 :                                 fprintf(clmn_fp[4], "\n");
     729           0 :                                 nrn++;
     730             :                         }
     731             :                 }
     732         163 :                 if (orn < nrn) {
     733           0 :                         SETBLACK(0);
     734           0 :                         SETBLACK(1);
     735           0 :                         while (orn < nrn) {
     736           0 :                                 fprintf(clmn_fp[0], "\n");
     737           0 :                                 orn++;
     738           0 :                                 fprintf(clmn_fp[1], "\n");
     739           0 :                                 SETRED(2, minor);
     740           0 :                                 fprintf(clmn_fp[2], "+");
     741           0 :                                 SETBLACK(2);
     742           0 :                                 fprintf(clmn_fp[2], "\n");
     743           0 :                                 SETRED(3, minor);
     744           0 :                                 fprintf(clmn_fp[3], "%i", nln++);
     745           0 :                                 SETBLACK(3);
     746           0 :                                 fprintf(clmn_fp[3], "\n");
     747             :                         }
     748             :                 }
     749             : 
     750         815 :                 for (i = 0; i < 5; i++) {
     751         815 :                         fclose(clmn_fp[i]);
     752         815 :                         clmn_fp[i] = Rfopen(clmn_fn[i]);
     753             :                 }
     754             : 
     755             :                 fprintf(html_fp, "<tbody>\n");
     756        1340 :                 for (;;) {
     757             :                         char ln[BUFLEN], buf1[128], buf2[128], buf3[128];
     758             : 
     759        3006 :                         if (fgets(ln, sizeof(ln), clmn_fp[0]) == NULL)
     760             :                                 break;
     761        2680 :                         if (fgets(buf1, sizeof(buf1), clmn_fp[1]) == NULL)
     762             :                                 break; /* shouldn't happen */
     763        1340 :                         buf1[strlen(buf1) - 1] = 0;
     764        2680 :                         if (fgets(buf2, sizeof(buf2), clmn_fp[2]) == NULL)
     765             :                                 break; /* shouldn't happen */
     766        1340 :                         buf2[strlen(buf2) - 1] = 0;
     767        2680 :                         if (fgets(buf3, sizeof(buf3), clmn_fp[3]) == NULL)
     768             :                                 break; /* shouldn't happen */
     769        1340 :                         buf3[strlen(buf3) - 1] = 0;
     770             : 
     771        1340 :                         fprintf(html_fp, "<tr>");
     772        1340 :                         fprintf(html_fp, "<td align='center'>%s</td>\n", buf1);
     773             :                         fprintf(html_fp, "<td>");
     774        1340 :                         while (ln[strlen(ln) - 1] != '\n') {
     775           0 :                                 fprintf(html_fp, "%s", ln);
     776           0 :                                 if (!fgets(ln, sizeof(ln), clmn_fp[0])) {
     777           0 :                                         ln[0] = '\n';
     778           0 :                                         ln[1] = 0;
     779           0 :                                         break;
     780             :                                 }
     781             :                         }
     782        1340 :                         ln[strlen(ln) - 1] = 0;
     783        1340 :                         fprintf(html_fp, "%s</td>\n", ln);
     784        1340 :                         fprintf(html_fp, "<td align='center'>%s</td>\n", buf1);
     785        1340 :                         fprintf(html_fp, "<td align='center'>%s</td>\n", buf2);
     786        1340 :                         fprintf(html_fp, "<td align='center'>%s</td>\n", buf3);
     787        2680 :                         if (fgets(ln, sizeof(ln), clmn_fp[4]) == NULL)
     788             :                                 break; /* shouldn't happen */
     789             :                         fprintf(html_fp, "<td>");
     790        1340 :                         while (ln[strlen(ln) - 1] != '\n') {
     791           0 :                                 fprintf(html_fp, "%s", ln);
     792           0 :                                 if (!fgets(ln, sizeof(ln), clmn_fp[4])) {
     793           0 :                                         ln[0] = '\n';
     794           0 :                                         ln[1] = 0;
     795           0 :                                         break;
     796             :                                 }
     797             :                         }
     798        1340 :                         ln[strlen(ln) - 1] = 0;
     799        1340 :                         fprintf(html_fp, "%s</td>\n", ln);
     800        1340 :                         fprintf(html_fp, "<td align='center'>%s</td>\n", buf3);
     801        1340 :                         fprintf(html_fp, "</tr>\n");
     802             :                 }
     803         163 :                 fprintf(html_fp, "</tbody>\n");
     804         978 :                 for (i = 0; i < 5; i++)
     805         815 :                         fclose(clmn_fp[i]);
     806             : 
     807             :                 TRACE(for (i = 0; i < 5; i++) clmn_fn[i][strlen(clmn_fn[i]) - 1]++) ;
     808             :         }
     809             : 
     810          30 :         fprintf(html_fp, "</table>\n");
     811          30 :         fprintf(html_fp, "<hr/>\n");
     812          30 :         fprintf(html_fp, "</body>\n</html>\n");
     813          60 :         fprintf(html_fp, "<!--%sDiffs-->\n", Major ? "Major" : (Minor ? "Minor" : "No"));
     814          30 :         fflush(html_fp);
     815          30 :         fclose(html_fp);
     816             : 
     817         180 :         for (i = 0; i < 5; i++) {
     818         150 :                 UNLINK(clmn_fn[i]);
     819         150 :                 free(clmn_fn[i]);
     820             :         }
     821             : 
     822          30 :         fclose(lwc_diff_fp);
     823          30 :         return (Major ? 2 : (Minor != 0));
     824             : }
     825             : 
     826             : /* lwc_diff2html */
     827             : 
     828             : 
     829             : int
     830        5204 : oldnew2html(int mindiff, int LWC, int context, char *ignore, char *function, char *old_fn, char *new_fn, char *html_fn, char *caption, char *revision)
     831             : {
     832             :         char lwc_diff_fn[CMDLEN];
     833             :         int rtrn;
     834             : 
     835             :         TRACE(fprintf(STDERR, "oldnew2html(%i,%i,%i,%s,%s,%s,%s,%s,%s,%s)\n", mindiff, LWC, context, ignore, function, old_fn, new_fn, html_fn, caption, revision));
     836             : 
     837       10408 :         snprintf(lwc_diff_fn, sizeof(lwc_diff_fn), "%s%c.difflib-%ld-oldnew2html-lwc_diff", tmpdir(), DIR_SEP, (long) getpid());
     838             : 
     839        5204 :         if (!oldnew2lwc_diff(mindiff, LWC, context, ignore, function, old_fn, new_fn, lwc_diff_fn))
     840             :                 /* { UNLINK(lwc_diff_fn); ERRHNDL(0,"oldnew2lwc_diff returns 0 in oldnew2html","",1); } */
     841        5174 :                 fclose(Wfopen(lwc_diff_fn));
     842             : 
     843        5204 :         rtrn = lwc_diff2html(old_fn, new_fn, lwc_diff_fn, html_fn, caption, revision);
     844             : 
     845        5204 :         UNLINK(lwc_diff_fn);
     846        5204 :         return rtrn;
     847             : }
     848             : 
     849             : /* oldnew2u_diff */

Generated by: LCOV version 1.12