home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / patch / inp.c < prev    next >
C/C++ Source or Header  |  1990-03-10  |  9KB  |  339 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. #ifdef MSDOS
  83.     int i_res;
  84. #endif
  85.  
  86.     if (ok_to_create_file && stat(filename, &filestat) < 0) {
  87.     if (verbose)
  88.         say2("(Creating file %s...)\n",filename);
  89.     makedirs(filename, TRUE);
  90.     close(creat(filename, 0666));
  91.     }
  92.     if (stat(filename, &filestat) < 0) {
  93.     Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  94.     if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  95.         Sprintf(buf, CHECKOUT, filename);
  96.         if (verbose)
  97.         say2("Can't find %s--attempting to check it out from RCS.\n",
  98.             filename);
  99.         if (system(buf) || stat(filename, &filestat))
  100.         fatal2("Can't check out %s.\n", filename);
  101.     }
  102.     else {
  103.         Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
  104.         if (stat(s=buf+20, &filestat) >= 0 ||
  105.           stat(s=buf+25, &filestat) >= 0) {
  106.         Sprintf(buf, GET, s);
  107.         if (verbose)
  108.             say2("Can't find %s--attempting to get it from SCCS.\n",
  109.             filename);
  110.         if (system(buf) || stat(filename, &filestat))
  111.             fatal2("Can't get %s.\n", filename);
  112.         }
  113.         else
  114.         fatal2("Can't find %s.\n", filename);
  115.     }
  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.     i_size = filestat.st_size;
  121. #ifdef MSDOS
  122.     if ( i_size > 65500L )
  123.       return FALSE;
  124. #endif
  125.     if (out_of_mem) {
  126.     set_hunkmax();        /* make sure dynamic arrays are allocated */
  127.     out_of_mem = FALSE;
  128.     return FALSE;            /* force plan b because plan a bombed */
  129.     }
  130. #ifdef lint
  131.     i_womp = Nullch;
  132. #else
  133.     i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  134.                     /* i_size, but that's okay, I think. */
  135. #endif
  136.     if (i_womp == Nullch)
  137.     return FALSE;
  138.     if ((ifd = open(filename, 0)) < 0)
  139.     fatal2("Can't open file %s\n", filename);
  140. #ifndef lint
  141. #ifdef MSDOS
  142.     if ((i_res = read(ifd, i_womp, (int)i_size)) == -1) {
  143.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  144.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  145.     return FALSE;    /*   undersized. */
  146.     }
  147.     else
  148.       /* cr/lf-translations would bomb here (i_res is != file size)! */
  149.       i_size = i_res;
  150. #else
  151.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  152.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  153.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  154.     return FALSE;    /*   undersized. */
  155.     }
  156. #endif
  157. #endif
  158.     Close(ifd);
  159.     if (i_size && i_womp[i_size-1] != '\n')
  160.     i_womp[i_size++] = '\n';
  161.     i_womp[i_size] = '\0';
  162.  
  163.     /* count the lines in the buffer so we know how many pointers we need */
  164.  
  165.     iline = 0;
  166.     for (s=i_womp; *s; s++) {
  167.     if (*s == '\n')
  168.         iline++;
  169.     }
  170. #ifdef lint
  171.     i_ptr = Null(char**);
  172. #else
  173.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  174. #endif
  175.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  176.     free((char *)i_womp);
  177.     return FALSE;
  178.     }
  179.  
  180.     /* now scan the buffer and build pointer array */
  181.  
  182.     iline = 1;
  183.     i_ptr[iline] = i_womp;
  184.     for (s=i_womp; *s; s++) {
  185.     if (*s == '\n')
  186.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  187.     }
  188.     input_lines = iline - 1;
  189.  
  190.     /* now check for revision, if any */
  191.  
  192.     if (revision != Nullch) {
  193.     if (!rev_in_string(i_womp)) {
  194.         if (force) {
  195.         if (verbose)
  196.             say2(
  197. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  198.             revision);
  199.         }
  200.         else {
  201.         ask2(
  202. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  203.             revision);
  204.         if (*buf != 'y')
  205.         fatal1("Aborted.\n");
  206.         }
  207.     }
  208.     else if (verbose)
  209.         say2("Good.  This file appears to be the %s version.\n",
  210.         revision);
  211.     }
  212.     return TRUE;            /* plan a will work */
  213. }
  214.  
  215. /* Keep (virtually) nothing in memory. */
  216.  
  217. void
  218. plan_b(filename)
  219. char *filename;
  220. {
  221.     Reg3 FILE *ifp;
  222.     Reg1 int i = 0;
  223.     Reg2 int maxlen = 1;
  224.     Reg4 bool found_revision = (revision == Nullch);
  225.  
  226.     using_plan_a = FALSE;
  227.     if ((ifp = fopen(filename, "r")) == Nullfp)
  228.     fatal2("Can't open file %s\n", filename);
  229.     if ((tifd = open(TMPINNAME, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,
  230.                      S_IWRITE|S_IREAD)) < 0)
  231.     fatal2("Can't open file %s\n", TMPINNAME);
  232.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  233.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  234.         found_revision = TRUE;
  235.     if ((i = strlen(buf)) > maxlen)
  236.         maxlen = i;            /* find longest line */
  237.     }
  238.     if (revision != Nullch) {
  239.     if (!found_revision) {
  240.         if (force) {
  241.         if (verbose)
  242.             say2(
  243. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  244.             revision);
  245.         }
  246.         else {
  247.         ask2(
  248. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  249.             revision);
  250.         if (*buf != 'y')
  251.             fatal1("Aborted.\n");
  252.         }
  253.     }
  254.     else if (verbose)
  255.         say2("Good.  This file appears to be the %s version.\n",
  256.         revision);
  257.     }
  258.     rewind(ifp);          /* rewind file */
  259.     lines_per_buf = BUFFERSIZE / maxlen;
  260.     tireclen = maxlen;
  261.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  262.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  263.     if (tibuf[1] == Nullch)
  264.     fatal1("Can't seem to get enough memory.\n");
  265.     for (i=1; ; i++) {
  266.     if (! (i % lines_per_buf))    /* new block */
  267.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  268.         fatal1("patch: can't write temp file.\n");
  269.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  270.       == Nullch) {
  271.         input_lines = i - 1;
  272.         if (i % lines_per_buf)
  273.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  274.             fatal1("patch: can't write temp file.\n");
  275.         break;
  276.     }
  277.     }
  278.     Fclose(ifp);
  279.     Close(tifd);
  280.  
  281.     if ((tifd = open(TMPINNAME, O_RDONLY|O_BINARY)) < 0) {
  282.     fatal2("Can't reopen file %s\n", TMPINNAME);
  283.     }
  284. }
  285.  
  286. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  287.  
  288. char *
  289. ifetch(line,whichbuf)
  290. Reg1 LINENUM line;
  291. int whichbuf;                /* ignored when file in memory */
  292. {
  293.     if (line < 1 || line > input_lines)
  294.     return "";
  295.     if (using_plan_a)
  296.     return i_ptr[line];
  297.     else {
  298.     LINENUM offline = line % lines_per_buf;
  299.     LINENUM baseline = line - offline;
  300.  
  301.     if (tiline[0] == baseline)
  302.         whichbuf = 0;
  303.     else if (tiline[1] == baseline)
  304.         whichbuf = 1;
  305.     else {
  306.         tiline[whichbuf] = baseline;
  307. #ifndef lint        /* complains of long accuracy */
  308.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  309. #endif
  310.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  311.         fatal2("Error reading tmp file %s.\n", TMPINNAME);
  312.     }
  313.     return tibuf[whichbuf] + (tireclen*offline);
  314.     }
  315. }
  316.  
  317. /* True if the string argument contains the revision number we want. */
  318.  
  319. bool
  320. rev_in_string(string)
  321. char *string;
  322. {
  323.     Reg1 char *s;
  324.     Reg2 int patlen;
  325.  
  326.     if (revision == Nullch)
  327.     return TRUE;
  328.     patlen = strlen(revision);
  329.     if (strnEQ(string,revision,patlen) && isspace(s[patlen]))
  330.     return TRUE;
  331.     for (s = string; *s; s++) {
  332.     if (isspace(*s) && strnEQ(s+1, revision, patlen) &&
  333.         isspace(s[patlen+1] )) {
  334.         return TRUE;
  335.     }
  336.     }
  337.     return FALSE;
  338. }
  339.