home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / upatch.t.Z / upatch.t / inp.c < prev    next >
Text File  |  1988-08-24  |  8KB  |  326 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. extern  int inos9;
  20.  
  21. /* Input-file-with-indexable-lines abstract type */
  22.  
  23. static long i_size;            /* size of the input file */
  24. static char *i_womp;            /* plan a buffer for entire file */
  25. static char **i_ptr;            /* pointers to lines in i_womp */
  26.  
  27. static int tifd = -1;            /* plan b virtual string array */
  28. static char *tibuf[2];            /* plan b buffers */
  29. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  30. static LINENUM lines_per_buf;        /* how many lines per buffer */
  31. static int tireclen;            /* length of records in tmp file */
  32.  
  33. /* New patch--prepare to edit another file. */
  34.  
  35. void
  36. re_input()
  37. {
  38.     if (using_plan_a) {
  39.     i_size = 0;
  40. #ifndef lint
  41.     if (i_ptr != Null(char**))
  42.         free((char *)i_ptr);
  43. #endif
  44.     if (i_womp != Nullch)
  45.         free(i_womp);
  46.     i_womp = Nullch;
  47.     i_ptr = Null(char **);
  48.     }
  49.     else {
  50.     using_plan_a = TRUE;        /* maybe the next one is smaller */
  51.     Close(tifd);
  52.     tifd = -1;
  53.     free(tibuf[0]);
  54.     free(tibuf[1]);
  55.     tibuf[0] = tibuf[1] = Nullch;
  56.     tiline[0] = tiline[1] = -1;
  57.     tireclen = 0;
  58.     }
  59. }
  60.  
  61. /* Constuct the line index, somehow or other. */
  62.  
  63. void
  64. scan_input(filename)
  65. char *filename;
  66. {
  67.     if (!plan_a(filename))
  68.     plan_b(filename);
  69.     if (verbose) {
  70.     say3("Patching file %s using Plan %s...\n", filename,
  71.       (using_plan_a ? "A" : "B") );
  72.     }
  73. }
  74.  
  75. /* Try keeping everything in memory. */
  76.  
  77. bool
  78. plan_a(filename)
  79. char *filename;
  80. {
  81.     int ifd;
  82.     Reg1 char *s;
  83.     Reg2 LINENUM iline;
  84.  
  85.     if (ok_to_create_file && stat(filename, &filestat) < 0) {
  86.     if (verbose)
  87.         say2("(Creating file %s...)\n",filename);
  88.     makedirs(filename, TRUE);
  89.     close(creat(filename, 03));
  90.     }
  91.     if (stat(filename, &filestat) < 0) {
  92.     Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  93.     if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  94.         Sprintf(buf, CHECKOUT, filename);
  95.         if (verbose)
  96.         say2("Can't find %s--attempting to check it out from RCS.\n",
  97.             filename);
  98.         if (system(buf) || stat(filename, &filestat))
  99.         fatal2("Can't check out %s.\n", filename);
  100.     }
  101.     else {
  102.         Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
  103.         if (stat(s=buf+20, &filestat) >= 0 ||
  104.           stat(s=buf+25, &filestat) >= 0) {
  105.         Sprintf(buf, GET, s);
  106.         if (verbose)
  107.             say2("Can't find %s--attempting to get it from SCCS.\n",
  108.             filename);
  109.         if (system(buf) || stat(filename, &filestat))
  110.             fatal2("Can't get %s.\n", filename);
  111.         }
  112.         else
  113.         fatal2("Can't find %s.\n", filename);
  114.     }
  115.     }
  116.     filemode = filestat.st_mode;
  117. #ifdef OSK
  118.     if ( ! filemode)
  119. #else
  120.     if ((filemode & S_IFMT) & ~S_IFREG)
  121. #endif
  122.     fatal2("%s is not a normal file--can't patch.\n", filename);
  123.     i_size = filestat.st_size;
  124.     if (out_of_mem) {
  125.     set_hunkmax();        /* make sure dynamic arrays are allocated */
  126.     out_of_mem = FALSE;
  127.     return FALSE;            /* force plan b because plan a bombed */
  128.     }
  129. #ifdef lint
  130.     i_womp = Nullch;
  131. #else
  132.     i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  133.                     /* i_size, but that's okay, I think. */
  134. #endif
  135.     if (i_womp == Nullch)
  136.     return FALSE;
  137.     if ((ifd = open(filename, 1)) < 0)
  138.     fatal2("Can't open file %s\n", filename);
  139. #ifndef lint
  140.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  141.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  142.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  143.     return FALSE;    /*   undersized. */
  144.     }
  145. #endif
  146.     Close(ifd);
  147.     if (i_size && i_womp[i_size-1] != '\n')
  148.     i_womp[i_size++] = '\n';
  149.     i_womp[i_size] = '\0';
  150.  
  151.     /* count the lines in the buffer so we know how many pointers we need */
  152.  
  153.     iline = 0;
  154.     for (s=i_womp; *s; s++) {
  155.     if (*s == '\n')
  156.         iline++;
  157.     }
  158. #ifdef lint
  159.     i_ptr = Null(char**);
  160. #else
  161.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  162. #endif
  163.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  164.     free((char *)i_womp);
  165.     return FALSE;
  166.     }
  167.     
  168.     /* now scan the buffer and build pointer array */
  169.  
  170.     iline = 1;
  171.     i_ptr[iline] = i_womp;
  172.     for (s=i_womp; *s; s++) {
  173.     if (*s == '\n')
  174.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  175.     }
  176.     input_lines = iline - 1;
  177.  
  178.     /* now check for revision, if any */
  179.  
  180.     if (revision != Nullch) { 
  181.     if (!rev_in_string(i_womp)) {
  182.         if (force) {
  183.         if (verbose)
  184.             say2(
  185. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  186.             revision);
  187.         }
  188.         else {
  189.         ask2(
  190. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  191.             revision);
  192.         if (*buf != 'y')
  193.         fatal1("Aborted.\n");
  194.         }
  195.     }
  196.     else if (verbose)
  197.         say2("Good.  This file appears to be the %s version.\n",
  198.         revision);
  199.     }
  200.     return TRUE;            /* plan a will work */
  201. }
  202.  
  203. /* Keep (virtually) nothing in memory. */
  204.  
  205. void
  206. plan_b(filename)
  207. char *filename;
  208. {
  209.     Reg3 FILE *ifp;
  210.     Reg1 int i = 0;
  211.     Reg2 int maxlen = 1;
  212.     Reg4 bool found_revision = (revision == Nullch);
  213.  
  214.     using_plan_a = FALSE;
  215.     if ((ifp = fopen(filename, "r")) == Nullfp)
  216.     fatal2("Can't open file %s\n", filename);
  217.     if ((tifd = creat(TMPINNAME, 03)) < 0)
  218.     fatal2("Can't open file %s\n", TMPINNAME);
  219.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  220.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  221.         found_revision = TRUE;
  222.     if ((i = strlen(buf)) > maxlen)
  223.         maxlen = i;            /* find longest line */
  224.     }
  225.     if (revision != Nullch) {
  226.     if (!found_revision) {
  227.         if (force) {
  228.         if (verbose)
  229.             say2(
  230. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  231.             revision);
  232.         }
  233.         else {
  234.         ask2(
  235. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  236.             revision);
  237.         if (*buf != 'y')
  238.             fatal1("Aborted.\n");
  239.         }
  240.     }
  241.     else if (verbose)
  242.         say2("Good.  This file appears to be the %s version.\n",
  243.         revision);
  244.     }
  245.     Fseek(ifp, 0L, 0);        /* rewind file */
  246.     lines_per_buf = BUFFERSIZE / maxlen;
  247.     tireclen = maxlen;
  248.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  249.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  250.     if (tibuf[1] == Nullch)
  251.     fatal1("Can't seem to get enough memory.\n");
  252.     for (i=1; ; i++) {
  253.     if (! (i % lines_per_buf))    /* new block */
  254.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  255.         fatal1("patch: can't write temp file.\n");
  256.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  257.       == Nullch) {
  258.         input_lines = i - 1;
  259.         if (i % lines_per_buf)
  260.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  261.             fatal1("patch: can't write temp file.\n");
  262.         break;
  263.     }
  264.     }
  265.     Fclose(ifp);
  266.     Close(tifd);
  267.     if ((inos9 = tifd = open(TMPINNAME, 1)) < 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(s[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.  
  326.