home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 278.lha / Patch_v0.91 / inp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-06  |  9.3 KB  |  324 lines

  1. /* $Header: inp.c,v 2.0.1.1 88/06/03 15:06:13 lwall Locked $
  2.  *
  3.  * $Log:        inp.c,v $
  4.  * Revision 2.0.1.1  88/06/03  15:06:13  lwall
  5.  * patch10: made a little smarter about sccs files
  6.  * 
  7.  * Revision 2.0  86/09/17  15:37:02  lwall
  8.  * Baseline for netwide release.
  9.  *
  10.  */
  11.  
  12. #include "EXTERN.h"
  13. #include "common.h"
  14. #include "util.h"
  15. #include "pch.h"
  16. #include "INTERN.h"
  17. #include "inp.h"
  18.  
  19. /* Input-file-with-indexable-lines abstract type */
  20.  
  21. static long i_size;                     /* size of the input file */
  22. static char *i_womp;                    /* plan a buffer for entire file */
  23. static char **i_ptr;                    /* pointers to lines in i_womp */
  24.  
  25. static int tifd = -1;                   /* plan b virtual string array */
  26. static char *tibuf[2];                  /* plan b buffers */
  27. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  28. static LINENUM lines_per_buf;           /* how many lines per buffer */
  29. static int tireclen;                    /* length of records in tmp file */
  30.  
  31. /* New patch--prepare to edit another file. */
  32.  
  33. void
  34. re_input()
  35. {
  36.     if (using_plan_a) {
  37.         i_size = 0;
  38. #ifndef lint
  39.         if (i_ptr != Null(char**))
  40.             free((char *)i_ptr);
  41. #endif
  42.         if (i_womp != Nullch)
  43.             free(i_womp);
  44.         i_womp = Nullch;
  45.         i_ptr = Null(char **);
  46.     }
  47.     else {
  48.         using_plan_a = TRUE;            /* maybe the next one is smaller */
  49.         Close(tifd);
  50.         tifd = -1;
  51.         free(tibuf[0]);
  52.         free(tibuf[1]);
  53.         tibuf[0] = tibuf[1] = Nullch;
  54.         tiline[0] = tiline[1] = -1;
  55.         tireclen = 0;
  56.     }
  57. }
  58.  
  59. /* Constuct the line index, somehow or other. */
  60.  
  61. void
  62. scan_input(filename)
  63. char *filename;
  64. {
  65.     if (!plan_a(filename))
  66.         plan_b(filename);
  67.     if (verbose) {
  68.         say3("Patching file %s using Plan %s...\n", filename,
  69.           (using_plan_a ? "A" : "B") );
  70.     }
  71. }
  72.  
  73. /* Try keeping everything in memory. */
  74.  
  75. bool
  76. plan_a(filename)
  77. char *filename;
  78. {
  79.     int ifd;
  80.     Reg1 char *s;
  81.     Reg2 LINENUM iline;
  82.  
  83.     if (ok_to_create_file && stat(filename, &filestat) < 0) {
  84.         if (verbose)
  85.             say2("(Creating file %s...)\n",filename);
  86.         makedirs(filename, TRUE);
  87.         close(creat(filename, 0666));
  88.     }
  89.     if (stat(filename, &filestat) < 0) {
  90. #ifndef AMIGA   /* Amiga doesn't have RCS, so don't bother: */
  91.         Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  92.         if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  93.             Sprintf(buf, CHECKOUT, filename);
  94.             if (verbose)
  95.                 say2("Can't find %s--attempting to check it out from RCS.\n",
  96.                     filename);
  97.             if (system(buf) || stat(filename, &filestat))
  98.                 fatal2("Can't check out %s.\n", filename);
  99.         }
  100.         else {
  101.         Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
  102.         if (stat(s=buf+20, &filestat) >= 0 ||
  103.           stat(s=buf+25, &filestat) >= 0) {
  104.         Sprintf(buf, GET, s);
  105.                 if (verbose)
  106.                     say2("Can't find %s--attempting to get it from SCCS.\n",
  107.                         filename);
  108.                 if (system(buf) || stat(filename, &filestat))
  109.                     fatal2("Can't get %s.\n", filename);
  110.             }
  111.             else
  112. #endif
  113.                 fatal2("Can't find %s.\n", filename);
  114.         }
  115. #ifndef AMIGA
  116.     }
  117.     filemode = filestat.st_mode;
  118.     if ((filemode & S_IFMT) & ~S_IFREG)
  119.         fatal2("%s is not a normal file--can't patch.\n", filename);
  120. #endif
  121.     i_size = filestat.st_size;
  122.     if (out_of_mem) {
  123.         set_hunkmax();          /* make sure dynamic arrays are allocated */
  124.         out_of_mem = FALSE;
  125.         return FALSE;                   /* force plan b because plan a bombed */
  126.     }
  127. #ifdef lint
  128.     i_womp = Nullch;
  129. #else
  130.     i_womp = malloc((MEM)(i_size+2));   /* lint says this may alloc less than */
  131.                                         /* i_size, but that's okay, I think. */
  132. #endif
  133.     if (i_womp == Nullch)
  134.         return FALSE;
  135.     if ((ifd = open(filename, 0)) < 0)
  136.         fatal2("Can't open file %s\n", filename);
  137. #ifndef lint
  138.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  139.         Close(ifd);     /* probably means i_size > 15 or 16 bits worth */
  140.         free(i_womp);   /* at this point it doesn't matter if i_womp was */
  141.         return FALSE;   /*   undersized. */
  142.     }
  143. #endif
  144.     Close(ifd);
  145.     if (i_size && i_womp[i_size-1] != '\n')
  146.         i_womp[i_size++] = '\n';
  147.     i_womp[i_size] = '\0';
  148.  
  149.     /* count the lines in the buffer so we know how many pointers we need */
  150.  
  151.     iline = 0;
  152.     for (s=i_womp; *s; s++) {
  153.         if (*s == '\n')
  154.             iline++;
  155.     }
  156. #ifdef lint
  157.     i_ptr = Null(char**);
  158. #else
  159.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  160. #endif
  161.     if (i_ptr == Null(char **)) {       /* shucks, it was a near thing */
  162.         free((char *)i_womp);
  163.         return FALSE;
  164.     }
  165.  
  166.     /* now scan the buffer and build pointer array */
  167.  
  168.     iline = 1;
  169.     i_ptr[iline] = i_womp;
  170.     for (s=i_womp; *s; s++) {
  171.         if (*s == '\n')
  172.             i_ptr[++iline] = s+1;       /* these are NOT null terminated */
  173.     }
  174.     input_lines = iline - 1;
  175.  
  176.     /* now check for revision, if any */
  177.  
  178.     if (revision != Nullch) {
  179.         if (!rev_in_string(i_womp)) {
  180.             if (force) {
  181.                 if (verbose)
  182.             say2(
  183. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  184.                         revision);
  185.             }
  186.             else {
  187.         ask2(
  188. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  189.                     revision);
  190.             if (*buf != 'y')
  191.                 fatal1("Aborted.\n");
  192.             }
  193.         }
  194.         else if (verbose)
  195.             say2("Good.  This file appears to be the %s version.\n",
  196.                 revision);
  197.     }
  198.     return TRUE;                        /* plan a will work */
  199. }
  200.  
  201. /* Keep (virtually) nothing in memory. */
  202.  
  203. void
  204. plan_b(filename)
  205. char *filename;
  206. {
  207.     Reg3 FILE *ifp;
  208.     Reg1 int i = 0;
  209.     Reg2 int maxlen = 1;
  210.     Reg4 bool found_revision = (revision == Nullch);
  211.  
  212.     using_plan_a = FALSE;
  213.     if ((ifp = fopen(filename, "r")) == Nullfp)
  214.         fatal2("Can't open file %s\n", filename);
  215.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  216.         fatal2("Can't open file %s\n", TMPINNAME);
  217.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  218.         if (revision != Nullch && !found_revision && rev_in_string(buf))
  219.             found_revision = TRUE;
  220.         if ((i = strlen(buf)) > maxlen)
  221.             maxlen = i;                 /* find longest line */
  222.     }
  223.     if (revision != Nullch) {
  224.         if (!found_revision) {
  225.             if (force) {
  226.                 if (verbose)
  227.             say2(
  228. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  229.                         revision);
  230.             }
  231.             else {
  232.         ask2(
  233. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  234.                     revision);
  235.                 if (*buf != 'y')
  236.                     fatal1("Aborted.\n");
  237.             }
  238.         }
  239.         else if (verbose)
  240.             say2("Good.  This file appears to be the %s version.\n",
  241.                 revision);
  242.     }
  243.     Fseek(ifp, 0L, 0);          /* rewind file */
  244.     lines_per_buf = BUFFERSIZE / maxlen;
  245.     tireclen = maxlen;
  246.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  247.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  248.     if (tibuf[1] == Nullch)
  249.         fatal1("Can't seem to get enough memory.\n");
  250.     for (i=1; ; i++) {
  251.         if (! (i % lines_per_buf))      /* new block */
  252.             if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  253.                 fatal1("patch: can't write temp file.\n");
  254.         if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  255.           == Nullch) {
  256.             input_lines = i - 1;
  257.             if (i % lines_per_buf)
  258.                 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  259.                     fatal1("patch: can't write temp file.\n");
  260.             break;
  261.         }
  262.     }
  263.     Fclose(ifp);
  264.     Close(tifd);
  265.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  266.         fatal2("Can't reopen file %s\n", TMPINNAME);
  267.     }
  268. }
  269.  
  270. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  271.  
  272. char *
  273. ifetch(line,whichbuf)
  274. Reg1 LINENUM line;
  275. int whichbuf;                           /* ignored when file in memory */
  276. {
  277.     if (line < 1 || line > input_lines)
  278.         return "";
  279.     if (using_plan_a)
  280.         return i_ptr[line];
  281.     else {
  282.         LINENUM offline = line % lines_per_buf;
  283.         LINENUM baseline = line - offline;
  284.  
  285.         if (tiline[0] == baseline)
  286.             whichbuf = 0;
  287.         else if (tiline[1] == baseline)
  288.             whichbuf = 1;
  289.         else {
  290.             tiline[whichbuf] = baseline;
  291. #ifndef lint            /* complains of long accuracy */
  292.             Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  293. #endif
  294.             if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  295.                 fatal2("Error reading tmp file %s.\n", TMPINNAME);
  296.         }
  297.         return tibuf[whichbuf] + (tireclen*offline);
  298.     }
  299. }
  300.  
  301. /* True if the string argument contains the revision number we want. */
  302.  
  303. bool
  304. rev_in_string(string)
  305. char *string;
  306. {
  307.     Reg1 char *s;
  308.     Reg2 int patlen;
  309.  
  310.     if (revision == Nullch)
  311.         return TRUE;
  312.     patlen = strlen(revision);
  313.     if (strnEQ(string,revision,patlen) && isspace(s[patlen]))
  314.     return TRUE;
  315.     for (s = string; *s; s++) {
  316.         if (isspace(*s) && strnEQ(s+1, revision, patlen) &&
  317.                 isspace(s[patlen+1] )) {
  318.             return TRUE;
  319.         }
  320.     }
  321.     return FALSE;
  322. }
  323.  
  324.