home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / inp.c < prev    next >
C/C++ Source or Header  |  1991-12-24  |  9KB  |  325 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.     Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  91.     if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  92.         Sprintf(buf, CHECKOUT, filename);
  93.         if (verbose)
  94.         say2("Can't find %s--attempting to check it out from RCS.\n",
  95.             filename);
  96.         if (system(buf) || stat(filename, &filestat))
  97.         fatal2("Can't check out %s.\n", filename);
  98.     }
  99.     else {
  100.         Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
  101.         if (stat(s=buf+20, &filestat) >= 0 ||
  102.           stat(s=buf+25, &filestat) >= 0) {
  103.         Sprintf(buf, GET, s);
  104.         if (verbose)
  105.             say2("Can't find %s--attempting to get it from SCCS.\n",
  106.             filename);
  107.         if (system(buf) || stat(filename, &filestat))
  108.             fatal2("Can't get %s.\n", filename);
  109.         }
  110.         else
  111.         fatal2("Can't find %s.\n", filename);
  112.     }
  113.     }
  114.     filemode = filestat.st_mode;
  115.     if ((filemode & S_IFMT) & ~S_IFREG)
  116.     fatal2("%s is not a normal file--can't patch.\n", filename);
  117.     i_size = filestat.st_size;
  118. #ifdef OS2
  119.     if ( i_size > 65500L )
  120.       return FALSE;
  121. #endif
  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, O_BINARY)) < 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, "rb")) == Nullfp)
  214.     fatal2("Can't open file %s\n", filename);
  215.     if ((tifd = open(TMPINNAME, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,
  216.                      S_IWRITE|S_IREAD)) < 0)
  217.     fatal2("Can't open file %s\n", TMPINNAME);
  218.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  219.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  220.         found_revision = TRUE;
  221.     if ((i = strlen(buf)) > maxlen)
  222.         maxlen = i;            /* find longest line */
  223.     }
  224.     if (revision != Nullch) {
  225.     if (!found_revision) {
  226.         if (force) {
  227.         if (verbose)
  228.             say2(
  229. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  230.             revision);
  231.         }
  232.         else {
  233.         ask2(
  234. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  235.             revision);
  236.         if (*buf != 'y')
  237.             fatal1("Aborted.\n");
  238.         }
  239.     }
  240.     else if (verbose)
  241.         say2("Good.  This file appears to be the %s version.\n",
  242.         revision);
  243.     }
  244.     rewind(ifp);          /* rewind file */
  245.     lines_per_buf = BUFFERSIZE / maxlen;
  246.     tireclen = maxlen;
  247.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  248.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  249.     if (tibuf[1] == Nullch)
  250.     fatal1("Can't seem to get enough memory.\n");
  251.     for (i=1; ; i++) {
  252.     if (! (i % lines_per_buf))    /* new block */
  253.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  254.         fatal1("patch: can't write temp file.\n");
  255.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  256.       == Nullch) {
  257.         input_lines = i - 1;
  258.         if (i % lines_per_buf)
  259.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  260.             fatal1("patch: can't write temp file.\n");
  261.         break;
  262.     }
  263.     }
  264.     Fclose(ifp);
  265.     Close(tifd);
  266.  
  267.     if ((tifd = open(TMPINNAME, O_RDONLY|O_BINARY)) < 0) {
  268.     fatal2("Can't reopen file %s\n", TMPINNAME);
  269.     }
  270. }
  271.  
  272. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  273.  
  274. char *
  275. ifetch(line,whichbuf)
  276. Reg1 LINENUM line;
  277. int whichbuf;                /* ignored when file in memory */
  278. {
  279.     if (line < 1 || line > input_lines)
  280.     return "";
  281.     if (using_plan_a)
  282.     return i_ptr[line];
  283.     else {
  284.     LINENUM offline = line % lines_per_buf;
  285.     LINENUM baseline = line - offline;
  286.  
  287.     if (tiline[0] == baseline)
  288.         whichbuf = 0;
  289.     else if (tiline[1] == baseline)
  290.         whichbuf = 1;
  291.     else {
  292.         tiline[whichbuf] = baseline;
  293. #ifndef lint        /* complains of long accuracy */
  294.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  295. #endif
  296.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  297.         fatal2("Error reading tmp file %s.\n", TMPINNAME);
  298.     }
  299.     return tibuf[whichbuf] + (tireclen*offline);
  300.     }
  301. }
  302.  
  303. /* True if the string argument contains the revision number we want. */
  304.  
  305. bool
  306. rev_in_string(string)
  307. char *string;
  308. {
  309.     Reg1 char *s;
  310.     Reg2 int patlen;
  311.  
  312.     if (revision == Nullch)
  313.     return TRUE;
  314.     patlen = strlen(revision);
  315.     if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
  316.     return TRUE;
  317.     for (s = string; *s; s++) {
  318.     if (isspace(*s) && strnEQ(s+1, revision, patlen) &&
  319.         isspace(s[patlen+1] )) {
  320.         return TRUE;
  321.     }
  322.     }
  323.     return FALSE;
  324. }
  325.