home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / patch212.zip / inp.c < prev    next >
C/C++ Source or Header  |  1991-02-18  |  9KB  |  340 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, O_BINARY)) < 0)
  139.     fatal2("Can't open file %s\n", filename);
  140. #ifndef lint
  141. #ifdef DUMMY_MSDOS
  142.     /* this hack is now obsolete in binary mode */
  143.     if ((i_res = read(ifd, i_womp, (int)i_size)) == -1) {
  144.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  145.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  146.     return FALSE;    /*   undersized. */
  147.     }
  148.     else
  149.       /* cr/lf-translations would bomb here (i_res is != file size)! */
  150.       i_size = i_res;
  151. #else
  152.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  153.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  154.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  155.     return FALSE;    /*   undersized. */
  156.     }
  157. #endif
  158. #endif
  159.     Close(ifd);
  160.     if (i_size && i_womp[i_size-1] != '\n')
  161.     i_womp[i_size++] = '\n';
  162.     i_womp[i_size] = '\0';
  163.  
  164.     /* count the lines in the buffer so we know how many pointers we need */
  165.  
  166.     iline = 0;
  167.     for (s=i_womp; *s; s++) {
  168.     if (*s == '\n')
  169.         iline++;
  170.     }
  171. #ifdef lint
  172.     i_ptr = Null(char**);
  173. #else
  174.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  175. #endif
  176.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  177.     free((char *)i_womp);
  178.     return FALSE;
  179.     }
  180.  
  181.     /* now scan the buffer and build pointer array */
  182.  
  183.     iline = 1;
  184.     i_ptr[iline] = i_womp;
  185.     for (s=i_womp; *s; s++) {
  186.     if (*s == '\n')
  187.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  188.     }
  189.     input_lines = iline - 1;
  190.  
  191.     /* now check for revision, if any */
  192.  
  193.     if (revision != Nullch) {
  194.     if (!rev_in_string(i_womp)) {
  195.         if (force) {
  196.         if (verbose)
  197.             say2(
  198. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  199.             revision);
  200.         }
  201.         else {
  202.         ask2(
  203. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  204.             revision);
  205.         if (*buf != 'y')
  206.         fatal1("Aborted.\n");
  207.         }
  208.     }
  209.     else if (verbose)
  210.         say2("Good.  This file appears to be the %s version.\n",
  211.         revision);
  212.     }
  213.     return TRUE;            /* plan a will work */
  214. }
  215.  
  216. /* Keep (virtually) nothing in memory. */
  217.  
  218. void
  219. plan_b(filename)
  220. char *filename;
  221. {
  222.     Reg3 FILE *ifp;
  223.     Reg1 int i = 0;
  224.     Reg2 int maxlen = 1;
  225.     Reg4 bool found_revision = (revision == Nullch);
  226.  
  227.     using_plan_a = FALSE;
  228.     if ((ifp = fopen(filename, "rb")) == Nullfp)
  229.     fatal2("Can't open file %s\n", filename);
  230.     if ((tifd = open(TMPINNAME, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,
  231.                      S_IWRITE|S_IREAD)) < 0)
  232.     fatal2("Can't open file %s\n", TMPINNAME);
  233.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  234.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  235.         found_revision = TRUE;
  236.     if ((i = strlen(buf)) > maxlen)
  237.         maxlen = i;            /* find longest line */
  238.     }
  239.     if (revision != Nullch) {
  240.     if (!found_revision) {
  241.         if (force) {
  242.         if (verbose)
  243.             say2(
  244. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  245.             revision);
  246.         }
  247.         else {
  248.         ask2(
  249. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  250.             revision);
  251.         if (*buf != 'y')
  252.             fatal1("Aborted.\n");
  253.         }
  254.     }
  255.     else if (verbose)
  256.         say2("Good.  This file appears to be the %s version.\n",
  257.         revision);
  258.     }
  259.     rewind(ifp);          /* rewind file */
  260.     lines_per_buf = BUFFERSIZE / maxlen;
  261.     tireclen = maxlen;
  262.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  263.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  264.     if (tibuf[1] == Nullch)
  265.     fatal1("Can't seem to get enough memory.\n");
  266.     for (i=1; ; i++) {
  267.     if (! (i % lines_per_buf))    /* new block */
  268.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  269.         fatal1("patch: can't write temp file.\n");
  270.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  271.       == Nullch) {
  272.         input_lines = i - 1;
  273.         if (i % lines_per_buf)
  274.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  275.             fatal1("patch: can't write temp file.\n");
  276.         break;
  277.     }
  278.     }
  279.     Fclose(ifp);
  280.     Close(tifd);
  281.  
  282.     if ((tifd = open(TMPINNAME, O_RDONLY|O_BINARY)) < 0) {
  283.     fatal2("Can't reopen file %s\n", TMPINNAME);
  284.     }
  285. }
  286.  
  287. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  288.  
  289. char *
  290. ifetch(line,whichbuf)
  291. Reg1 LINENUM line;
  292. int whichbuf;                /* ignored when file in memory */
  293. {
  294.     if (line < 1 || line > input_lines)
  295.     return "";
  296.     if (using_plan_a)
  297.     return i_ptr[line];
  298.     else {
  299.     LINENUM offline = line % lines_per_buf;
  300.     LINENUM baseline = line - offline;
  301.  
  302.     if (tiline[0] == baseline)
  303.         whichbuf = 0;
  304.     else if (tiline[1] == baseline)
  305.         whichbuf = 1;
  306.     else {
  307.         tiline[whichbuf] = baseline;
  308. #ifndef lint        /* complains of long accuracy */
  309.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  310. #endif
  311.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  312.         fatal2("Error reading tmp file %s.\n", TMPINNAME);
  313.     }
  314.     return tibuf[whichbuf] + (tireclen*offline);
  315.     }
  316. }
  317.  
  318. /* True if the string argument contains the revision number we want. */
  319.  
  320. bool
  321. rev_in_string(string)
  322. char *string;
  323. {
  324.     Reg1 char *s;
  325.     Reg2 int patlen;
  326.  
  327.     if (revision == Nullch)
  328.     return TRUE;
  329.     patlen = strlen(revision);
  330.     if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
  331.     return TRUE;
  332.     for (s = string; *s; s++) {
  333.     if (isspace(*s) && strnEQ(s+1, revision, patlen) &&
  334.         isspace(s[patlen+1] )) {
  335.         return TRUE;
  336.     }
  337.     }
  338.     return FALSE;
  339. }
  340.