pch.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. /* reading patches */
  2. /* $Id: pch.c,v 1.26 1997/07/21 17:59:46 eggert Exp $ */
  3. /*
  4. Copyright 1986, 1987, 1988 Larry Wall
  5. Copyright 1990, 1991, 1992, 1993, 1997 Free Software Foundation, Inc.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING.
  16. If not, write to the Free Software Foundation,
  17. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #define XTERN extern
  20. #include <common.h>
  21. #include <backupfile.h>
  22. #include <inp.h>
  23. #include <util.h>
  24. #undef XTERN
  25. #define XTERN
  26. #include <pch.h>
  27. #define INITHUNKMAX 125 /* initial dynamic allocation size */
  28. /* Patch (diff listing) abstract type. */
  29. static FILE *pfp; /* patch file pointer */
  30. static int p_says_nonexistent[2]; /* [0] for old file, [1] for new;
  31. value is 0 for nonempty, 1 for empty, 2 for nonexistent */
  32. static int p_rfc934_nesting; /* RFC 934 nesting level */
  33. static time_t p_timestamp[2]; /* timestamps in patch headers */
  34. static off_t p_filesize; /* size of the patch file */
  35. static LINENUM p_first; /* 1st line number */
  36. static LINENUM p_newfirst; /* 1st line number of replacement */
  37. static LINENUM p_ptrn_lines; /* # lines in pattern */
  38. static LINENUM p_repl_lines; /* # lines in replacement text */
  39. static LINENUM p_end = -1; /* last line in hunk */
  40. static LINENUM p_max; /* max allowed value of p_end */
  41. static LINENUM p_prefix_context; /* # of prefix context lines */
  42. static LINENUM p_suffix_context; /* # of suffix context lines */
  43. static LINENUM p_input_line; /* current line # from patch file */
  44. static char **p_line; /* the text of the hunk */
  45. static size_t *p_len; /* line length including \n if any */
  46. static char *p_Char; /* +, -, and ! */
  47. static LINENUM hunkmax = INITHUNKMAX; /* size of above arrays */
  48. static int p_indent; /* indent to patch */
  49. static file_offset p_base; /* where to intuit this time */
  50. static LINENUM p_bline; /* line # of p_base */
  51. static file_offset p_start; /* where intuit found a patch */
  52. static LINENUM p_sline; /* and the line number for it */
  53. static LINENUM p_hunk_beg; /* line number of current hunk */
  54. static LINENUM p_efake = -1; /* end of faked up lines--don't free */
  55. static LINENUM p_bfake = -1; /* beg of faked up lines */
  56. enum nametype { OLD, NEW, INDEX, NONE };
  57. static enum diff intuit_diff_type PARAMS ((void));
  58. static enum nametype best_name PARAMS ((char * const *, int const *));
  59. static int prefix_components PARAMS ((char *, int));
  60. static size_t pget_line PARAMS ((int, int));
  61. static size_t get_line PARAMS ((void));
  62. static bool incomplete_line PARAMS ((void));
  63. static bool grow_hunkmax PARAMS ((void));
  64. static void malformed PARAMS ((void)) __attribute__ ((noreturn));
  65. static void next_intuit_at PARAMS ((file_offset, LINENUM));
  66. static void skip_to PARAMS ((file_offset, LINENUM));
  67. /* Prepare to look for the next patch in the patch file. */
  68. void
  69. re_patch()
  70. {
  71. p_first = 0;
  72. p_newfirst = 0;
  73. p_ptrn_lines = 0;
  74. p_repl_lines = 0;
  75. p_end = -1;
  76. p_max = 0;
  77. p_indent = 0;
  78. }
  79. /* Open the patch file at the beginning of time. */
  80. void
  81. open_patch_file(filename)
  82. char const *filename;
  83. {
  84. file_offset file_pos = 0;
  85. struct stat st;
  86. if (!filename || !*filename || strEQ (filename, "-"))
  87. {
  88. file_offset stdin_pos;
  89. #if HAVE_SETMODE
  90. if (binary_transput)
  91. {
  92. if (isatty (STDIN_FILENO))
  93. fatal ("cannot read binary data from tty on this platform");
  94. setmode (STDIN_FILENO, O_BINARY);
  95. }
  96. #endif
  97. if (fstat (STDIN_FILENO, &st) != 0)
  98. pfatal ("fstat");
  99. if (S_ISREG (st.st_mode) && (stdin_pos = file_tell (stdin)) != -1)
  100. {
  101. pfp = stdin;
  102. file_pos = stdin_pos;
  103. }
  104. else
  105. {
  106. size_t charsread;
  107. pfp = fopen (TMPPATNAME, "w+b");
  108. if (!pfp)
  109. pfatal ("can't create `%s'", TMPPATNAME);
  110. for (st.st_size = 0;
  111. (charsread = fread (buf, 1, bufsize, stdin)) != 0;
  112. st.st_size += charsread)
  113. if (fwrite (buf, 1, charsread, pfp) != charsread)
  114. write_fatal ();
  115. if (ferror (stdin) || fclose (stdin) != 0)
  116. read_fatal ();
  117. if (fflush (pfp) != 0
  118. || file_seek (pfp, (file_offset) 0, SEEK_SET) != 0)
  119. write_fatal ();
  120. }
  121. }
  122. else
  123. {
  124. pfp = fopen (filename, binary_transput ? "rb" : "r");
  125. if (!pfp)
  126. pfatal ("can't open patch file `%s'", filename);
  127. if (fstat (fileno (pfp), &st) != 0)
  128. pfatal ("fstat");
  129. }
  130. p_filesize = st.st_size;
  131. if (p_filesize != (file_offset) p_filesize)
  132. fatal ("patch file is too long");
  133. next_intuit_at (file_pos, (LINENUM) 1);
  134. set_hunkmax();
  135. }
  136. /* Make sure our dynamically realloced tables are malloced to begin with. */
  137. void
  138. set_hunkmax()
  139. {
  140. if (!p_line)
  141. p_line = (char **) malloc (hunkmax * sizeof *p_line);
  142. if (!p_len)
  143. p_len = (size_t *) malloc (hunkmax * sizeof *p_len);
  144. if (!p_Char)
  145. p_Char = malloc (hunkmax * sizeof *p_Char);
  146. }
  147. /* Enlarge the arrays containing the current hunk of patch. */
  148. static bool
  149. grow_hunkmax()
  150. {
  151. hunkmax *= 2;
  152. assert (p_line && p_len && p_Char);
  153. if ((p_line = (char **) realloc (p_line, hunkmax * sizeof (*p_line)))
  154. && (p_len = (size_t *) realloc (p_len, hunkmax * sizeof (*p_len)))
  155. && (p_Char = realloc (p_Char, hunkmax * sizeof (*p_Char))))
  156. return TRUE;
  157. if (!using_plan_a)
  158. memory_fatal ();
  159. /* Don't free previous values of p_line etc.,
  160. since some broken implementations free them for us.
  161. Whatever is null will be allocated again from within plan_a (),
  162. of all places. */
  163. return FALSE;
  164. }
  165. /* True if the remainder of the patch file contains a diff of some sort. */
  166. bool
  167. there_is_another_patch()
  168. {
  169. if (p_base != 0 && p_base >= p_filesize) {
  170. if (verbosity == VERBOSE)
  171. say ("done\n");
  172. return FALSE;
  173. }
  174. if (verbosity == VERBOSE)
  175. say ("Hmm...");
  176. diff_type = intuit_diff_type();
  177. if (diff_type == NO_DIFF) {
  178. if (verbosity == VERBOSE)
  179. say (p_base
  180. ? " Ignoring the trailing garbage.\ndone\n"
  181. : " I can't seem to find a patch in there anywhere.\n");
  182. if (! p_base && p_filesize)
  183. fatal ("Only garbage was found in the patch input.");
  184. return FALSE;
  185. }
  186. if (skip_rest_of_patch)
  187. {
  188. Fseek (pfp, p_start, SEEK_SET);
  189. p_input_line = p_sline - 1;
  190. return TRUE;
  191. }
  192. if (verbosity == VERBOSE)
  193. say (" %sooks like %s to me...\n",
  194. (p_base == 0 ? "L" : "The next patch l"),
  195. diff_type == UNI_DIFF ? "a unified diff" :
  196. diff_type == CONTEXT_DIFF ? "a context diff" :
  197. diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
  198. diff_type == NORMAL_DIFF ? "a normal diff" :
  199. "an ed script" );
  200. if (verbosity != SILENT)
  201. {
  202. if (p_indent)
  203. say ("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
  204. if (! inname)
  205. {
  206. say ("can't find file to patch at input line %ld\n",
  207. p_sline);
  208. say (strippath == INT_MAX
  209. ? "Perhaps you should have used the -p or --strip option?\n"
  210. : "Perhaps you used the wrong -p or --strip option?\n");
  211. }
  212. }
  213. skip_to(p_start,p_sline);
  214. while (!inname) {
  215. if (force || batch) {
  216. say ("No file to patch. Skipping patch.\n");
  217. skip_rest_of_patch = TRUE;
  218. return TRUE;
  219. }
  220. ask ("File to patch: ");
  221. inname = fetchname (buf, 0, (time_t *) 0);
  222. if (inname)
  223. {
  224. if (stat (inname, &instat) == 0)
  225. {
  226. inerrno = 0;
  227. invc = -1;
  228. }
  229. else
  230. {
  231. perror (inname);
  232. free (inname);
  233. inname = 0;
  234. }
  235. }
  236. if (!inname) {
  237. ask ("Skip this patch? [y] ");
  238. if (*buf != 'n') {
  239. if (verbosity != SILENT)
  240. say ("Skipping patch.\n");
  241. skip_rest_of_patch = TRUE;
  242. return TRUE;
  243. }
  244. }
  245. }
  246. return TRUE;
  247. }
  248. /* Determine what kind of diff is in the remaining part of the patch file. */
  249. static enum diff
  250. intuit_diff_type()
  251. {
  252. register char *s;
  253. register char *t;
  254. register int indent;
  255. register file_offset this_line = 0;
  256. register file_offset previous_line;
  257. register file_offset first_command_line = -1;
  258. LINENUM fcl_line = 0; /* Pacify `gcc -W'. */
  259. register bool last_line_was_command = FALSE;
  260. register bool this_is_a_command = FALSE;
  261. register bool stars_last_line = FALSE;
  262. register bool stars_this_line = FALSE;
  263. enum nametype i;
  264. char *name[3];
  265. struct stat st[3];
  266. int stat_errno[3];
  267. int version_controlled[3];
  268. register enum diff retval;
  269. name[OLD] = name[NEW] = name[INDEX] = 0;
  270. version_controlled[OLD] = -1;
  271. version_controlled[NEW] = -1;
  272. version_controlled[INDEX] = -1;
  273. p_rfc934_nesting = 0;
  274. p_timestamp[OLD] = p_timestamp[NEW] = (time_t) -1;
  275. p_says_nonexistent[OLD] = p_says_nonexistent[NEW] = 0;
  276. Fseek (pfp, p_base, SEEK_SET);
  277. p_input_line = p_bline - 1;
  278. for (;;) {
  279. previous_line = this_line;
  280. last_line_was_command = this_is_a_command;
  281. stars_last_line = stars_this_line;
  282. this_line = file_tell (pfp);
  283. indent = 0;
  284. if (! pget_line (0, 0)) {
  285. if (first_command_line >= 0) {
  286. /* nothing but deletes!? */
  287. p_start = first_command_line;
  288. p_sline = fcl_line;
  289. retval = ED_DIFF;
  290. goto scan_exit;
  291. }
  292. else {
  293. p_start = this_line;
  294. p_sline = p_input_line;
  295. return NO_DIFF;
  296. }
  297. }
  298. for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
  299. if (*s == '\t')
  300. indent = (indent + 8) & ~7;
  301. else
  302. indent++;
  303. }
  304. for (t = s; ISDIGIT (*t) || *t == ','; t++)
  305. continue;
  306. this_is_a_command = (ISDIGIT (*s) &&
  307. (*t == 'd' || *t == 'c' || *t == 'a') );
  308. if (first_command_line < 0 && this_is_a_command) {
  309. first_command_line = this_line;
  310. fcl_line = p_input_line;
  311. p_indent = indent; /* assume this for now */
  312. }
  313. if (!stars_last_line && strnEQ(s, "*** ", 4))
  314. name[OLD] = fetchname (s+4, strippath, &p_timestamp[OLD]);
  315. else if (strnEQ(s, "+++ ", 4))
  316. /* Swap with NEW below. */
  317. name[OLD] = fetchname (s+4, strippath, &p_timestamp[OLD]);
  318. else if (strnEQ(s, "Index:", 6))
  319. name[INDEX] = fetchname (s+6, strippath, (time_t *) 0);
  320. else if (strnEQ(s, "Prereq:", 7)) {
  321. for (t = s + 7; ISSPACE ((unsigned char) *t); t++)
  322. continue;
  323. revision = t;
  324. for (t = revision; *t && !ISSPACE ((unsigned char) *t); t++)
  325. continue;
  326. if (t == revision)
  327. revision = 0;
  328. else {
  329. char oldc = *t;
  330. *t = '\0';
  331. revision = savestr (revision);
  332. *t = oldc;
  333. }
  334. } else
  335. {
  336. for (t = s; t[0] == '-' && t[1] == ' '; t += 2)
  337. continue;
  338. if (strnEQ(t, "--- ", 4))
  339. {
  340. time_t timestamp = (time_t) -1;
  341. name[NEW] = fetchname (t+4, strippath, &timestamp);
  342. if (timestamp != (time_t) -1)
  343. {
  344. p_timestamp[NEW] = timestamp;
  345. p_rfc934_nesting = (t - s) >> 1;
  346. }
  347. }
  348. }
  349. if ((diff_type == NO_DIFF || diff_type == ED_DIFF) &&
  350. first_command_line >= 0 &&
  351. strEQ(s, ".\n") ) {
  352. p_indent = indent;
  353. p_start = first_command_line;
  354. p_sline = fcl_line;
  355. retval = ED_DIFF;
  356. goto scan_exit;
  357. }
  358. if ((diff_type == NO_DIFF || diff_type == UNI_DIFF)
  359. && strnEQ(s, "@@ -", 4)) {
  360. /* `name' and `p_timestamp' are backwards; swap them. */
  361. time_t ti = p_timestamp[OLD];
  362. p_timestamp[OLD] = p_timestamp[NEW];
  363. p_timestamp[NEW] = ti;
  364. t = name[OLD];
  365. name[OLD] = name[NEW];
  366. name[NEW] = t;
  367. s += 4;
  368. if (! atol (s))
  369. p_says_nonexistent[OLD] = 1 + ! p_timestamp[OLD];
  370. while (*s != ' ' && *s != '\n')
  371. s++;
  372. while (*s == ' ')
  373. s++;
  374. if (! atol (s))
  375. p_says_nonexistent[NEW] = 1 + ! p_timestamp[NEW];
  376. p_indent = indent;
  377. p_start = this_line;
  378. p_sline = p_input_line;
  379. retval = UNI_DIFF;
  380. if (! ((name[OLD] || ! p_timestamp[OLD])
  381. && (name[NEW] || ! p_timestamp[NEW])))
  382. say ("missing header for unified diff at line %ld of patch\n",
  383. p_sline);
  384. goto scan_exit;
  385. }
  386. stars_this_line = strnEQ(s, "********", 8);
  387. if ((diff_type == NO_DIFF
  388. || diff_type == CONTEXT_DIFF
  389. || diff_type == NEW_CONTEXT_DIFF)
  390. && stars_last_line && strnEQ (s, "*** ", 4)) {
  391. s += 4;
  392. if (! atol (s))
  393. p_says_nonexistent[OLD] = 1 + ! p_timestamp[OLD];
  394. /* if this is a new context diff the character just before */
  395. /* the newline is a '*'. */
  396. while (*s != '\n')
  397. s++;
  398. p_indent = indent;
  399. p_start = previous_line;
  400. p_sline = p_input_line - 1;
  401. retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
  402. {
  403. /* Scan the first hunk to see whether the file contents
  404. appear to have been deleted. */
  405. file_offset saved_p_base = p_base;
  406. LINENUM saved_p_bline = p_bline;
  407. Fseek (pfp, previous_line, SEEK_SET);
  408. p_input_line -= 2;
  409. if (another_hunk (retval, 0)
  410. && ! p_repl_lines && p_newfirst == 1)
  411. p_says_nonexistent[NEW] = 1 + ! p_timestamp[NEW];
  412. next_intuit_at (saved_p_base, saved_p_bline);
  413. }
  414. if (! ((name[OLD] || ! p_timestamp[OLD])
  415. && (name[NEW] || ! p_timestamp[NEW])))
  416. say ("missing header for context diff at line %ld of patch\n",
  417. p_sline);
  418. goto scan_exit;
  419. }
  420. if ((diff_type == NO_DIFF || diff_type == NORMAL_DIFF) &&
  421. last_line_was_command &&
  422. (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
  423. p_start = previous_line;
  424. p_sline = p_input_line - 1;
  425. p_indent = indent;
  426. retval = NORMAL_DIFF;
  427. goto scan_exit;
  428. }
  429. }
  430. scan_exit:
  431. /* To intuit `inname', the name of the file to patch,
  432. use the algorithm specified by POSIX 1003.2b/D11 section 5.22.7.2
  433. (with some modifications if posixly_correct is zero):
  434. - Take the old and new names from the context header if present,
  435. and take the index name from the `Index:' line if present and
  436. if either the old and new names are both absent
  437. or posixly_correct is nonzero.
  438. Consider the file names to be in the order (old, new, index).
  439. - If some named files exist, use the first one if posixly_correct
  440. is nonzero, the best one otherwise.
  441. - If patch_get is nonzero, and no named files exist,
  442. but an RCS or SCCS master file exists,
  443. use the first named file with an RCS or SCCS master.
  444. - If no named files exist, no RCS or SCCS master was found,
  445. some names are given, posixly_correct is zero,
  446. and the patch appears to create a file, then use the best name
  447. requiring the creation of the fewest directories.
  448. - Otherwise, report failure by setting `inname' to 0;
  449. this causes our invoker to ask the user for a file name. */
  450. i = NONE;
  451. if (!inname)
  452. {
  453. enum nametype i0 = NONE;
  454. if (! posixly_correct && (name[OLD] || name[NEW]) && name[INDEX])
  455. {
  456. free (name[INDEX]);
  457. name[INDEX] = 0;
  458. }
  459. for (i = OLD; i <= INDEX; i++)
  460. if (name[i])
  461. {
  462. if (i0 != NONE && strcmp (name[i0], name[i]) == 0)
  463. {
  464. /* It's the same name as before; reuse stat results. */
  465. stat_errno[i] = stat_errno[i0];
  466. if (! stat_errno[i])
  467. st[i] = st[i0];
  468. }
  469. else if (stat (name[i], &st[i]) != 0)
  470. stat_errno[i] = errno;
  471. else
  472. {
  473. stat_errno[i] = 0;
  474. if (posixly_correct)
  475. break;
  476. }
  477. i0 = i;
  478. }
  479. if (! posixly_correct)
  480. {
  481. i = best_name (name, stat_errno);
  482. if (i == NONE && patch_get)
  483. {
  484. enum nametype nope = NONE;
  485. for (i = OLD; i <= INDEX; i++)
  486. if (name[i])
  487. {
  488. char const *cs;
  489. char *getbuf;
  490. char *diffbuf;
  491. int readonly = outfile && strcmp (outfile, name[i]) != 0;
  492. if (nope == NONE || strcmp (name[nope], name[i]) != 0)
  493. {
  494. cs = (version_controller
  495. (name[i], readonly, (struct stat *) 0,
  496. &getbuf, &diffbuf));
  497. version_controlled[i] = !! cs;
  498. if (cs)
  499. {
  500. if (version_get (name[i], cs, 0, readonly,
  501. getbuf, &st[i]))
  502. stat_errno[i] = 0;
  503. else
  504. version_controlled[i] = 0;
  505. free (getbuf);
  506. free (diffbuf);
  507. if (! stat_errno[i])
  508. break;
  509. }
  510. }
  511. nope = i;
  512. }
  513. }
  514. if (p_says_nonexistent[reverse ^ (i == NONE || st[i].st_size == 0)])
  515. {
  516. assert (i0 != NONE);
  517. if (ok_to_reverse
  518. ("The next patch%s would %s the file `%s',\nwhich %s!",
  519. reverse ? ", when reversed," : "",
  520. (i == NONE ? "delete"
  521. : st[i].st_size == 0 ? "empty out"
  522. : "create"),
  523. name[i == NONE || st[i].st_size == 0 ? i0 : i],
  524. (i == NONE ? "does not exist"
  525. : st[i].st_size == 0 ? "is already empty"
  526. : "already exists")))
  527. reverse ^= 1;
  528. }
  529. if (i == NONE && p_says_nonexistent[reverse])
  530. {
  531. int newdirs[3];
  532. int newdirs_min = INT_MAX;
  533. int distance_from_minimum[3];
  534. for (i = OLD; i <= INDEX; i++)
  535. if (name[i])
  536. {
  537. newdirs[i] = (prefix_components (name[i], 0)
  538. - prefix_components (name[i], 1));
  539. if (newdirs[i] < newdirs_min)
  540. newdirs_min = newdirs[i];
  541. }
  542. for (i = OLD; i <= INDEX; i++)
  543. if (name[i])
  544. distance_from_minimum[i] = newdirs[i] - newdirs_min;
  545. i = best_name (name, distance_from_minimum);
  546. }
  547. }
  548. }
  549. if (i == NONE)
  550. inerrno = -1;
  551. else
  552. {
  553. inname = name[i];
  554. name[i] = 0;
  555. inerrno = stat_errno[i];
  556. invc = version_controlled[i];
  557. instat = st[i];
  558. }
  559. for (i = OLD; i <= INDEX; i++)
  560. if (name[i])
  561. free (name[i]);
  562. return retval;
  563. }
  564. /* Count the path name components in FILENAME's prefix.
  565. If CHECKDIRS is nonzero, count only existing directories. */
  566. static int
  567. prefix_components (filename, checkdirs)
  568. char *filename;
  569. int checkdirs;
  570. {
  571. int count = 0;
  572. struct stat stat_buf;
  573. int stat_result;
  574. char *f = filename + FILESYSTEM_PREFIX_LEN (filename);
  575. if (*f)
  576. while (*++f)
  577. if (ISSLASH (f[0]) && ! ISSLASH (f[-1]))
  578. {
  579. if (checkdirs)
  580. {
  581. *f = '\0';
  582. stat_result = stat (filename, &stat_buf);
  583. *f = '/';
  584. if (! (stat_result == 0 && S_ISDIR (stat_buf.st_mode)))
  585. break;
  586. }
  587. count++;
  588. }
  589. return count;
  590. }
  591. /* Return the index of the best of NAME[OLD], NAME[NEW], and NAME[INDEX].
  592. Ignore null names, and ignore NAME[i] if IGNORE[i] is nonzero.
  593. Return NONE if all names are ignored. */
  594. static enum nametype
  595. best_name (name, ignore)
  596. char *const *name;
  597. int const *ignore;
  598. {
  599. enum nametype i;
  600. int components[3];
  601. int components_min = INT_MAX;
  602. size_t basename_len[3];
  603. size_t basename_len_min = (size_t) -1;
  604. size_t len[3];
  605. size_t len_min = (size_t) -1;
  606. for (i = OLD; i <= INDEX; i++)
  607. if (name[i] && !ignore[i])
  608. {
  609. /* Take the names with the fewest prefix components. */
  610. components[i] = prefix_components (name[i], 0);
  611. if (components_min < components[i])
  612. continue;
  613. components_min = components[i];
  614. /* Of those, take the names with the shortest basename. */
  615. basename_len[i] = strlen (base_name (name[i]));
  616. if (basename_len_min < basename_len[i])
  617. continue;
  618. basename_len_min = basename_len[i];
  619. /* Of those, take the shortest names. */
  620. len[i] = strlen (name[i]);
  621. if (len_min < len[i])
  622. continue;
  623. len_min = len[i];
  624. }
  625. /* Of those, take the first name. */
  626. for (i = OLD; i <= INDEX; i++)
  627. if (name[i] && !ignore[i]
  628. && components[i] == components_min
  629. && basename_len[i] == basename_len_min
  630. && len[i] == len_min)
  631. break;
  632. return i;
  633. }
  634. /* Remember where this patch ends so we know where to start up again. */
  635. static void
  636. next_intuit_at(file_pos,file_line)
  637. file_offset file_pos;
  638. LINENUM file_line;
  639. {
  640. p_base = file_pos;
  641. p_bline = file_line;
  642. }
  643. /* Basically a verbose fseek() to the actual diff listing. */
  644. static void
  645. skip_to(file_pos,file_line)
  646. file_offset file_pos;
  647. LINENUM file_line;
  648. {
  649. register FILE *i = pfp;
  650. register FILE *o = stdout;
  651. register int c;
  652. assert(p_base <= file_pos);
  653. if ((verbosity == VERBOSE || !inname) && p_base < file_pos) {
  654. Fseek (i, p_base, SEEK_SET);
  655. say ("The text leading up to this was:\n--------------------------\n");
  656. while (file_tell (i) < file_pos)
  657. {
  658. putc ('|', o);
  659. do
  660. {
  661. if ((c = getc (i)) == EOF)
  662. read_fatal ();
  663. putc (c, o);
  664. }
  665. while (c != '\n');
  666. }
  667. say ("--------------------------\n");
  668. }
  669. else
  670. Fseek (i, file_pos, SEEK_SET);
  671. p_input_line = file_line - 1;
  672. }
  673. /* Make this a function for better debugging. */
  674. static void
  675. malformed ()
  676. {
  677. fatal ("malformed patch at line %ld: %s", p_input_line, buf);
  678. /* about as informative as "Syntax error" in C */
  679. }
  680. /* 1 if there is more of the current diff listing to process;
  681. 0 if not; -1 if ran out of memory. */
  682. int
  683. another_hunk (difftype, rev)
  684. enum diff difftype;
  685. int rev;
  686. {
  687. register char *s;
  688. register LINENUM context = 0;
  689. register size_t chars_read;
  690. while (p_end >= 0) {
  691. if (p_end == p_efake)
  692. p_end = p_bfake; /* don't free twice */
  693. else
  694. free(p_line[p_end]);
  695. p_end--;
  696. }
  697. assert(p_end == -1);
  698. p_efake = -1;
  699. p_max = hunkmax; /* gets reduced when --- found */
  700. if (difftype == CONTEXT_DIFF || difftype == NEW_CONTEXT_DIFF) {
  701. file_offset line_beginning = file_tell (pfp);
  702. /* file pos of the current line */
  703. LINENUM repl_beginning = 0; /* index of --- line */
  704. register LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */
  705. register LINENUM fillsrc; /* index of first line to copy */
  706. register LINENUM filldst; /* index of first missing line */
  707. bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */
  708. bool some_context = FALSE; /* (perhaps internal) context seen */
  709. register bool repl_could_be_missing = TRUE;
  710. bool repl_missing = FALSE; /* we are now backtracking */
  711. file_offset repl_backtrack_position = 0;
  712. /* file pos of first repl line */
  713. LINENUM repl_patch_line; /* input line number for same */
  714. LINENUM repl_context; /* context for same */
  715. LINENUM ptrn_prefix_context = -1; /* lines in pattern prefix context */
  716. LINENUM ptrn_suffix_context = -1; /* lines in pattern suffix context */
  717. LINENUM repl_prefix_context = -1; /* lines in replac. prefix context */
  718. register LINENUM ptrn_copiable = 0;
  719. /* # of copiable lines in ptrn */
  720. /* Pacify `gcc -Wall'. */
  721. fillsrc = filldst = repl_patch_line = repl_context = 0;
  722. chars_read = get_line ();
  723. if (chars_read == (size_t) -1
  724. || chars_read <= 8
  725. || strncmp (buf, "********", 8) != 0) {
  726. next_intuit_at(line_beginning,p_input_line);
  727. return chars_read == (size_t) -1 ? -1 : 0;
  728. }
  729. p_hunk_beg = p_input_line + 1;
  730. while (p_end < p_max) {
  731. chars_read = get_line ();
  732. if (chars_read == (size_t) -1)
  733. return -1;
  734. if (!chars_read) {
  735. if (repl_beginning && repl_could_be_missing) {
  736. repl_missing = TRUE;
  737. goto hunk_done;
  738. }
  739. if (p_max - p_end < 4) {
  740. strcpy (buf, " \n"); /* assume blank lines got chopped */
  741. chars_read = 3;
  742. } else {
  743. fatal ("unexpected end of file in patch");
  744. }
  745. }
  746. p_end++;
  747. if (p_end == hunkmax)
  748. fatal ("unterminated hunk starting at line %ld; giving up at line %ld: %s",
  749. pch_hunk_beg (), p_input_line, buf);
  750. assert(p_end < hunkmax);
  751. p_Char[p_end] = *buf;
  752. p_len[p_end] = 0;
  753. p_line[p_end] = 0;
  754. switch (*buf) {
  755. case '*':
  756. if (strnEQ(buf, "********", 8)) {
  757. if (repl_beginning && repl_could_be_missing) {
  758. repl_missing = TRUE;
  759. goto hunk_done;
  760. }
  761. else
  762. fatal ("unexpected end of hunk at line %ld",
  763. p_input_line);
  764. }
  765. if (p_end != 0) {
  766. if (repl_beginning && repl_could_be_missing) {
  767. repl_missing = TRUE;
  768. goto hunk_done;
  769. }
  770. fatal ("unexpected `***' at line %ld: %s",
  771. p_input_line, buf);
  772. }
  773. context = 0;
  774. p_len[p_end] = strlen (buf);
  775. if (! (p_line[p_end] = savestr (buf))) {
  776. p_end--;
  777. return -1;
  778. }
  779. for (s = buf; *s && !ISDIGIT (*s); s++)
  780. continue;
  781. if (!*s)
  782. malformed ();
  783. if (strnEQ(s,"0,0",3))
  784. remove_prefix (s, 2);
  785. p_first = (LINENUM) atol(s);
  786. while (ISDIGIT (*s))
  787. s++;
  788. if (*s == ',') {
  789. while (*s && !ISDIGIT (*s))
  790. s++;
  791. if (!*s)
  792. malformed ();
  793. p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
  794. }
  795. else if (p_first)
  796. p_ptrn_lines = 1;
  797. else {
  798. p_ptrn_lines = 0;
  799. p_first = 1;
  800. }
  801. p_max = p_ptrn_lines + 6; /* we need this much at least */
  802. while (p_max >= hunkmax)
  803. if (! grow_hunkmax ())
  804. return -1;
  805. p_max = hunkmax;
  806. break;
  807. case '-':
  808. if (buf[1] != '-')
  809. goto change_line;
  810. if (ptrn_prefix_context == -1)
  811. ptrn_prefix_context = context;
  812. ptrn_suffix_context = context;
  813. if (repl_beginning
  814. || (p_end
  815. != p_ptrn_lines + 1 + (p_Char[p_end - 1] == '\n')))
  816. {
  817. if (p_end == 1)
  818. {
  819. /* `Old' lines were omitted. Set up to fill
  820. them in from `new' context lines. */
  821. p_end = p_ptrn_lines + 1;
  822. ptrn_prefix_context = ptrn_suffix_context = -1;
  823. fillsrc = p_end + 1;
  824. filldst = 1;
  825. fillcnt = p_ptrn_lines;
  826. }
  827. else if (! repl_beginning)
  828. fatal ("%s `---' at line %ld; check line numbers at line %ld",
  829. (p_end <= p_ptrn_lines
  830. ? "Premature"
  831. : "Overdue"),
  832. p_input_line, p_hunk_beg);
  833. else if (! repl_could_be_missing)
  834. fatal ("duplicate `---' at line %ld; check line numbers at line %ld",
  835. p_input_line, p_hunk_beg + repl_beginning);
  836. else
  837. {
  838. repl_missing = TRUE;
  839. goto hunk_done;
  840. }
  841. }
  842. repl_beginning = p_end;
  843. repl_backtrack_position = file_tell (pfp);
  844. repl_patch_line = p_input_line;
  845. repl_context = context;
  846. p_len[p_end] = strlen (buf);
  847. if (! (p_line[p_end] = savestr (buf)))
  848. {
  849. p_end--;
  850. return -1;
  851. }
  852. p_Char[p_end] = '=';
  853. for (s = buf; *s && ! ISDIGIT (*s); s++)
  854. continue;
  855. if (!*s)
  856. malformed ();
  857. p_newfirst = (LINENUM) atol (s);
  858. while (ISDIGIT (*s))
  859. s++;
  860. if (*s == ',')
  861. {
  862. do
  863. {
  864. if (!*++s)
  865. malformed ();
  866. }
  867. while (! ISDIGIT (*s));
  868. p_repl_lines = (LINENUM) atol (s) - p_newfirst + 1;
  869. }
  870. else if (p_newfirst)
  871. p_repl_lines = 1;
  872. else
  873. {
  874. p_repl_lines = 0;
  875. p_newfirst = 1;
  876. }
  877. p_max = p_repl_lines + p_end;
  878. while (p_max >= hunkmax)
  879. if (! grow_hunkmax ())
  880. return -1;
  881. if (p_repl_lines != ptrn_copiable
  882. && (p_prefix_context != 0
  883. || context != 0
  884. || p_repl_lines != 1))
  885. repl_could_be_missing = FALSE;
  886. context = 0;
  887. break;
  888. case '+': case '!':
  889. repl_could_be_missing = FALSE;
  890. change_line:
  891. s = buf + 1;
  892. chars_read--;
  893. if (*s == '\n' && canonicalize) {
  894. strcpy (s, " \n");
  895. chars_read = 2;
  896. }
  897. if (*s == ' ' || *s == '\t') {
  898. s++;
  899. chars_read--;
  900. } else if (repl_beginning && repl_could_be_missing) {
  901. repl_missing = TRUE;
  902. goto hunk_done;
  903. }
  904. if (! repl_beginning)
  905. {
  906. if (ptrn_prefix_context == -1)
  907. ptrn_prefix_context = context;
  908. }
  909. else
  910. {
  911. if (repl_prefix_context == -1)
  912. repl_prefix_context = context;
  913. }
  914. chars_read -=
  915. (1 < chars_read
  916. && p_end == (repl_beginning ? p_max : p_ptrn_lines)
  917. && incomplete_line ());
  918. p_len[p_end] = chars_read;
  919. if (! (p_line[p_end] = savebuf (s, chars_read))) {
  920. p_end--;
  921. return -1;
  922. }
  923. context = 0;
  924. break;
  925. case '\t': case '\n': /* assume spaces got eaten */
  926. s = buf;
  927. if (*buf == '\t') {
  928. s++;
  929. chars_read--;
  930. }
  931. if (repl_beginning && repl_could_be_missing &&
  932. (!ptrn_spaces_eaten || difftype == NEW_CONTEXT_DIFF) ) {
  933. repl_missing = TRUE;
  934. goto hunk_done;
  935. }
  936. chars_read -=
  937. (1 < chars_read
  938. && p_end == (repl_beginning ? p_max : p_ptrn_lines)
  939. && incomplete_line ());
  940. p_len[p_end] = chars_read;
  941. if (! (p_line[p_end] = savebuf (buf, chars_read))) {
  942. p_end--;
  943. return -1;
  944. }
  945. if (p_end != p_ptrn_lines + 1) {
  946. ptrn_spaces_eaten |= (repl_beginning != 0);
  947. some_context = TRUE;
  948. context++;
  949. if (!repl_beginning)
  950. ptrn_copiable++;
  951. p_Char[p_end] = ' ';
  952. }
  953. break;
  954. case ' ':
  955. s = buf + 1;
  956. chars_read--;
  957. if (*s == '\n' && canonicalize) {
  958. strcpy (s, "\n");
  959. chars_read = 2;
  960. }
  961. if (*s == ' ' || *s == '\t') {
  962. s++;
  963. chars_read--;
  964. } else if (repl_beginning && repl_could_be_missing) {
  965. repl_missing = TRUE;
  966. goto hunk_done;
  967. }
  968. some_context = TRUE;
  969. context++;
  970. if (!repl_beginning)
  971. ptrn_copiable++;
  972. chars_read -=
  973. (1 < chars_read
  974. && p_end == (repl_beginning ? p_max : p_ptrn_lines)
  975. && incomplete_line ());
  976. p_len[p_end] = chars_read;
  977. if (! (p_line[p_end] = savebuf (buf + 2, chars_read))) {
  978. p_end--;
  979. return -1;
  980. }
  981. break;
  982. default:
  983. if (repl_beginning && repl_could_be_missing) {
  984. repl_missing = TRUE;
  985. goto hunk_done;
  986. }
  987. malformed ();
  988. }
  989. }
  990. hunk_done:
  991. if (p_end >=0 && !repl_beginning)
  992. fatal ("no `---' found in patch at line %ld", pch_hunk_beg ());
  993. if (repl_missing) {
  994. /* reset state back to just after --- */
  995. p_input_line = repl_patch_line;
  996. context = repl_context;
  997. for (p_end--; p_end > repl_beginning; p_end--)
  998. free(p_line[p_end]);
  999. Fseek (pfp, repl_backtrack_position, SEEK_SET);
  1000. /* redundant 'new' context lines were omitted - set */
  1001. /* up to fill them in from the old file context */
  1002. fillsrc = 1;
  1003. filldst = repl_beginning+1;
  1004. fillcnt = p_repl_lines;
  1005. p_end = p_max;
  1006. }
  1007. else if (!some_context && fillcnt == 1) {
  1008. /* the first hunk was a null hunk with no context */
  1009. /* and we were expecting one line -- fix it up. */
  1010. while (filldst < p_end) {
  1011. p_line[filldst] = p_line[filldst+1];
  1012. p_Char[filldst] = p_Char[filldst+1];
  1013. p_len[filldst] = p_len[filldst+1];
  1014. filldst++;
  1015. }
  1016. #if 0
  1017. repl_beginning--; /* this doesn't need to be fixed */
  1018. #endif
  1019. p_end--;
  1020. p_first++; /* do append rather than insert */
  1021. fillcnt = 0;
  1022. p_ptrn_lines = 0;
  1023. }
  1024. p_prefix_context = ((repl_prefix_context == -1
  1025. || (ptrn_prefix_context != -1
  1026. && ptrn_prefix_context < repl_prefix_context))
  1027. ? ptrn_prefix_context : repl_prefix_context);
  1028. p_suffix_context = ((ptrn_suffix_context != -1
  1029. && ptrn_suffix_context < context)
  1030. ? ptrn_suffix_context : context);
  1031. assert (p_prefix_context != -1 && p_suffix_context != -1);
  1032. if (difftype == CONTEXT_DIFF
  1033. && (fillcnt
  1034. || (p_first > 1
  1035. && p_prefix_context + p_suffix_context < ptrn_copiable))) {
  1036. if (verbosity == VERBOSE)
  1037. say ("%s\n%s\n%s\n",
  1038. "(Fascinating -- this is really a new-style context diff but without",
  1039. "the telltale extra asterisks on the *** line that usually indicate",
  1040. "the new style...)");
  1041. diff_type = difftype = NEW_CONTEXT_DIFF;
  1042. }
  1043. /* if there were omitted context lines, fill them in now */
  1044. if (fillcnt) {
  1045. p_bfake = filldst; /* remember where not to free() */
  1046. p_efake = filldst + fillcnt - 1;
  1047. while (fillcnt-- > 0) {
  1048. while (fillsrc <= p_end && fillsrc != repl_beginning
  1049. && p_Char[fillsrc] != ' ')
  1050. fillsrc++;
  1051. if (p_end < fillsrc || fillsrc == repl_beginning)
  1052. fatal ("replacement text or line numbers mangled in hunk at line %ld",
  1053. p_hunk_beg);
  1054. p_line[filldst] = p_line[fillsrc];
  1055. p_Char[filldst] = p_Char[fillsrc];
  1056. p_len[filldst] = p_len[fillsrc];
  1057. fillsrc++; filldst++;
  1058. }
  1059. while (fillsrc <= p_end && fillsrc != repl_beginning)
  1060. {
  1061. if (p_Char[fillsrc] == ' ')
  1062. fatal ("replacement text or line numbers mangled in hunk at line %ld",
  1063. p_hunk_beg);
  1064. fillsrc++;
  1065. }
  1066. if (debug & 64)
  1067. printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
  1068. fillsrc,filldst,repl_beginning,p_end+1);
  1069. assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
  1070. assert(filldst==p_end+1 || filldst==repl_beginning);
  1071. }
  1072. }
  1073. else if (difftype == UNI_DIFF) {
  1074. file_offset line_beginning = file_tell (pfp);
  1075. /* file pos of the current line */
  1076. register LINENUM fillsrc; /* index of old lines */
  1077. register LINENUM filldst; /* index of new lines */
  1078. char ch = '\0';
  1079. chars_read = get_line ();
  1080. if (chars_read == (size_t) -1
  1081. || chars_read <= 4
  1082. || strncmp (buf, "@@ -", 4) != 0) {
  1083. next_intuit_at(line_beginning,p_input_line);
  1084. return chars_read == (size_t) -1 ? -1 : 0;
  1085. }
  1086. s = buf+4;
  1087. if (!*s)
  1088. malformed ();
  1089. p_first = (LINENUM) atol(s);
  1090. while (ISDIGIT (*s))
  1091. s++;
  1092. if (*s == ',') {
  1093. p_ptrn_lines = (LINENUM) atol(++s);
  1094. while (ISDIGIT (*s))
  1095. s++;
  1096. } else
  1097. p_ptrn_lines = 1;
  1098. if (*s == ' ') s++;
  1099. if (*s != '+' || !*++s)
  1100. malformed ();
  1101. p_newfirst = (LINENUM) atol(s);
  1102. while (ISDIGIT (*s))
  1103. s++;
  1104. if (*s == ',') {
  1105. p_repl_lines = (LINENUM) atol(++s);
  1106. while (ISDIGIT (*s))
  1107. s++;
  1108. } else
  1109. p_repl_lines = 1;
  1110. if (*s == ' ') s++;
  1111. if (*s != '@')
  1112. malformed ();
  1113. if (!p_ptrn_lines)
  1114. p_first++; /* do append rather than insert */
  1115. if (!p_repl_lines)
  1116. p_newfirst++;
  1117. p_max = p_ptrn_lines + p_repl_lines + 1;
  1118. while (p_max >= hunkmax)
  1119. if (! grow_hunkmax ())
  1120. return -1;
  1121. fillsrc = 1;
  1122. filldst = fillsrc + p_ptrn_lines;
  1123. p_end = filldst + p_repl_lines;
  1124. sprintf (buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1);
  1125. p_len[0] = strlen (buf);
  1126. if (! (p_line[0] = savestr (buf))) {
  1127. p_end = -1;
  1128. return -1;
  1129. }
  1130. p_Char[0] = '*';
  1131. sprintf (buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1);
  1132. p_len[filldst] = strlen (buf);
  1133. if (! (p_line[filldst] = savestr (buf))) {
  1134. p_end = 0;
  1135. return -1;
  1136. }
  1137. p_Char[filldst++] = '=';
  1138. p_prefix_context = -1;
  1139. p_hunk_beg = p_input_line + 1;
  1140. while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
  1141. chars_read = get_line ();
  1142. if (!chars_read) {
  1143. if (p_max - filldst < 3) {
  1144. strcpy (buf, " \n"); /* assume blank lines got chopped */
  1145. chars_read = 2;
  1146. } else {
  1147. fatal ("unexpected end of file in patch");
  1148. }
  1149. }
  1150. if (chars_read == (size_t) -1)
  1151. s = 0;
  1152. else if (*buf == '\t' || *buf == '\n') {
  1153. ch = ' '; /* assume the space got eaten */
  1154. s = savebuf (buf, chars_read);
  1155. }
  1156. else {
  1157. ch = *buf;
  1158. s = savebuf (buf+1, --chars_read);
  1159. }
  1160. if (!s) {
  1161. while (--filldst > p_ptrn_lines)
  1162. free(p_line[filldst]);
  1163. p_end = fillsrc-1;
  1164. return -1;
  1165. }
  1166. switch (ch) {
  1167. case '-':
  1168. if (fillsrc > p_ptrn_lines) {
  1169. free(s);
  1170. p_end = filldst-1;
  1171. malformed ();
  1172. }
  1173. chars_read -= fillsrc == p_ptrn_lines && incomplete_line ();
  1174. p_Char[fillsrc] = ch;
  1175. p_line[fillsrc] = s;
  1176. p_len[fillsrc++] = chars_read;
  1177. break;
  1178. case '=':
  1179. ch = ' ';
  1180. /* FALL THROUGH */
  1181. case ' ':
  1182. if (fillsrc > p_ptrn_lines) {
  1183. free(s);
  1184. while (--filldst > p_ptrn_lines)
  1185. free(p_line[filldst]);
  1186. p_end = fillsrc-1;
  1187. malformed ();
  1188. }
  1189. context++;
  1190. chars_read -= fillsrc == p_ptrn_lines && incomplete_line ();
  1191. p_Char[fillsrc] = ch;
  1192. p_line[fillsrc] = s;
  1193. p_len[fillsrc++] = chars_read;
  1194. s = savebuf (s, chars_read);
  1195. if (!s) {
  1196. while (--filldst > p_ptrn_lines)
  1197. free(p_line[filldst]);
  1198. p_end = fillsrc-1;
  1199. return -1;
  1200. }
  1201. /* FALL THROUGH */
  1202. case '+':
  1203. if (filldst > p_end) {
  1204. free(s);
  1205. while (--filldst > p_ptrn_lines)
  1206. free(p_line[filldst]);
  1207. p_end = fillsrc-1;
  1208. malformed ();
  1209. }
  1210. chars_read -= filldst == p_end && incomplete_line ();
  1211. p_Char[filldst] = ch;
  1212. p_line[filldst] = s;
  1213. p_len[filldst++] = chars_read;
  1214. break;
  1215. default:
  1216. p_end = filldst;
  1217. malformed ();
  1218. }
  1219. if (ch != ' ') {
  1220. if (p_prefix_context == -1)
  1221. p_prefix_context = context;
  1222. context = 0;
  1223. }
  1224. }/* while */
  1225. if (p_prefix_context == -1)
  1226. malformed ();
  1227. p_suffix_context = context;
  1228. }
  1229. else { /* normal diff--fake it up */
  1230. char hunk_type;
  1231. register int i;
  1232. LINENUM min, max;
  1233. file_offset line_beginning = file_tell (pfp);
  1234. p_prefix_context = p_suffix_context = 0;
  1235. chars_read = get_line ();
  1236. if (chars_read == (size_t) -1 || !chars_read || !ISDIGIT (*buf)) {
  1237. next_intuit_at(line_beginning,p_input_line);
  1238. return chars_read == (size_t) -1 ? -1 : 0;
  1239. }
  1240. p_first = (LINENUM)atol(buf);
  1241. for (s = buf; ISDIGIT (*s); s++)
  1242. continue;
  1243. if (*s == ',') {
  1244. p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
  1245. while (ISDIGIT (*s))
  1246. s++;
  1247. }
  1248. else
  1249. p_ptrn_lines = (*s != 'a');
  1250. hunk_type = *s;
  1251. if (hunk_type == 'a')
  1252. p_first++; /* do append rather than insert */
  1253. min = (LINENUM)atol(++s);
  1254. while (ISDIGIT (*s))
  1255. s++;
  1256. if (*s == ',')
  1257. max = (LINENUM)atol(++s);
  1258. else
  1259. max = min;
  1260. if (hunk_type == 'd')
  1261. min++;
  1262. p_end = p_ptrn_lines + 1 + max - min + 1;
  1263. while (p_end >= hunkmax)
  1264. if (! grow_hunkmax ())
  1265. {
  1266. p_end = -1;
  1267. return -1;
  1268. }
  1269. p_newfirst = min;
  1270. p_repl_lines = max - min + 1;
  1271. sprintf (buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
  1272. p_len[0] = strlen (buf);
  1273. if (! (p_line[0] = savestr (buf))) {
  1274. p_end = -1;
  1275. return -1;
  1276. }
  1277. p_Char[0] = '*';
  1278. for (i=1; i<=p_ptrn_lines; i++) {
  1279. chars_read = get_line ();
  1280. if (chars_read == (size_t) -1)
  1281. {
  1282. p_end = i - 1;
  1283. return -1;
  1284. }
  1285. if (!chars_read)
  1286. fatal ("unexpected end of file in patch at line %ld",
  1287. p_input_line);
  1288. if (buf[0] != '<' || (buf[1] != ' ' && buf[1] != '\t'))
  1289. fatal ("`<' expected at line %ld of patch", p_input_line);
  1290. chars_read -= 2 + (i == p_ptrn_lines && incomplete_line ());
  1291. p_len[i] = chars_read;
  1292. if (! (p_line[i] = savebuf (buf + 2, chars_read))) {
  1293. p_end = i-1;
  1294. return -1;
  1295. }
  1296. p_Char[i] = '-';
  1297. }
  1298. if (hunk_type == 'c') {
  1299. chars_read = get_line ();
  1300. if (chars_read == (size_t) -1)
  1301. {
  1302. p_end = i - 1;
  1303. return -1;
  1304. }
  1305. if (! chars_read)
  1306. fatal ("unexpected end of file in patch at line %ld",
  1307. p_input_line);
  1308. if (*buf != '-')
  1309. fatal ("`---' expected at line %ld of patch", p_input_line);
  1310. }
  1311. sprintf (buf, "--- %ld,%ld\n", min, max);
  1312. p_len[i] = strlen (buf);
  1313. if (! (p_line[i] = savestr (buf))) {
  1314. p_end = i-1;
  1315. return -1;
  1316. }
  1317. p_Char[i] = '=';
  1318. for (i++; i<=p_end; i++) {
  1319. chars_read = get_line ();
  1320. if (chars_read == (size_t) -1)
  1321. {
  1322. p_end = i - 1;
  1323. return -1;
  1324. }
  1325. if (!chars_read)
  1326. fatal ("unexpected end of file in patch at line %ld",
  1327. p_input_line);
  1328. if (buf[0] != '>' || (buf[1] != ' ' && buf[1] != '\t'))
  1329. fatal ("`>' expected at line %ld of patch", p_input_line);
  1330. chars_read -= 2 + (i == p_end && incomplete_line ());
  1331. p_len[i] = chars_read;
  1332. if (! (p_line[i] = savebuf (buf + 2, chars_read))) {
  1333. p_end = i-1;
  1334. return -1;
  1335. }
  1336. p_Char[i] = '+';
  1337. }
  1338. }
  1339. if (rev) /* backwards patch? */
  1340. if (!pch_swap())
  1341. say ("Not enough memory to swap next hunk!\n");
  1342. if (debug & 2) {
  1343. LINENUM i;
  1344. char special;
  1345. for (i=0; i <= p_end; i++) {
  1346. if (i == p_ptrn_lines)
  1347. special = '^';
  1348. else
  1349. special = ' ';
  1350. fprintf (stderr, "%3ld %c %c ", i, p_Char[i], special);
  1351. pch_write_line (i, stderr);
  1352. fflush (stderr);
  1353. }
  1354. }
  1355. if (p_end+1 < hunkmax) /* paranoia reigns supreme... */
  1356. p_Char[p_end+1] = '^'; /* add a stopper for apply_hunk */
  1357. return 1;
  1358. }
  1359. static size_t
  1360. get_line ()
  1361. {
  1362. return pget_line (p_indent, p_rfc934_nesting);
  1363. }
  1364. /* Input a line from the patch file, worrying about indentation.
  1365. Strip up to INDENT characters' worth of leading indentation.
  1366. Then remove up to RFC934_NESTING instances of leading "- ".
  1367. Ignore any partial lines at end of input, but warn about them.
  1368. Succeed if a line was read; it is terminated by "\n\0" for convenience.
  1369. Return the number of characters read, including '\n' but not '\0'.
  1370. Return -1 if we ran out of memory. */
  1371. static size_t
  1372. pget_line (indent, rfc934_nesting)
  1373. int indent;
  1374. int rfc934_nesting;
  1375. {
  1376. register FILE *fp = pfp;
  1377. register int c;
  1378. register int i = 0;
  1379. register char *b;
  1380. register size_t s;
  1381. for (;;)
  1382. {
  1383. c = getc (fp);
  1384. if (c == EOF)
  1385. {
  1386. if (ferror (fp))
  1387. read_fatal ();
  1388. return 0;
  1389. }
  1390. if (indent <= i)
  1391. break;
  1392. if (c == ' ' || c == 'X')
  1393. i++;
  1394. else if (c == '\t')
  1395. i = (i + 8) & ~7;
  1396. else
  1397. break;
  1398. }
  1399. i = 0;
  1400. b = buf;
  1401. while (c == '-' && 0 <= --rfc934_nesting)
  1402. {
  1403. c = getc (fp);
  1404. if (c == EOF)
  1405. goto patch_ends_in_middle_of_line;
  1406. if (c != ' ')
  1407. {
  1408. i = 1;
  1409. b[0] = '-';
  1410. break;
  1411. }
  1412. c = getc (fp);
  1413. if (c == EOF)
  1414. goto patch_ends_in_middle_of_line;
  1415. }
  1416. s = bufsize;
  1417. for (;;)
  1418. {
  1419. if (i == s - 1)
  1420. {
  1421. s *= 2;
  1422. b = realloc (b, s);
  1423. if (!b)
  1424. {
  1425. if (!using_plan_a)
  1426. memory_fatal ();
  1427. return (size_t) -1;
  1428. }
  1429. buf = b;
  1430. bufsize = s;
  1431. }
  1432. b[i++] = c;
  1433. if (c == '\n')
  1434. break;
  1435. c = getc (fp);
  1436. if (c == EOF)
  1437. goto patch_ends_in_middle_of_line;
  1438. }
  1439. b[i] = '\0';
  1440. p_input_line++;
  1441. return i;
  1442. patch_ends_in_middle_of_line:
  1443. if (ferror (fp))
  1444. read_fatal ();
  1445. say ("patch unexpectedly ends in middle of line\n");
  1446. return 0;
  1447. }
  1448. static bool
  1449. incomplete_line ()
  1450. {
  1451. register FILE *fp = pfp;
  1452. register int c;
  1453. register file_offset line_beginning = file_tell (fp);
  1454. if (getc (fp) == '\\')
  1455. {
  1456. while ((c = getc (fp)) != '\n' && c != EOF)
  1457. continue;
  1458. return TRUE;
  1459. }
  1460. else
  1461. {
  1462. /* We don't trust ungetc. */
  1463. Fseek (pfp, line_beginning, SEEK_SET);
  1464. return FALSE;
  1465. }
  1466. }
  1467. /* Reverse the old and new portions of the current hunk. */
  1468. bool
  1469. pch_swap()
  1470. {
  1471. char **tp_line; /* the text of the hunk */
  1472. size_t *tp_len; /* length of each line */
  1473. char *tp_char; /* +, -, and ! */
  1474. register LINENUM i;
  1475. register LINENUM n;
  1476. bool blankline = FALSE;
  1477. register char *s;
  1478. i = p_first;
  1479. p_first = p_newfirst;
  1480. p_newfirst = i;
  1481. /* make a scratch copy */
  1482. tp_line = p_line;
  1483. tp_len = p_len;
  1484. tp_char = p_Char;
  1485. p_line = 0; /* force set_hunkmax to allocate again */
  1486. p_len = 0;
  1487. p_Char = 0;
  1488. set_hunkmax();
  1489. if (!p_line || !p_len || !p_Char) {
  1490. if (p_line)
  1491. free (p_line);
  1492. p_line = tp_line;
  1493. if (p_len)
  1494. free (p_len);
  1495. p_len = tp_len;
  1496. if (p_Char)
  1497. free (p_Char);
  1498. p_Char = tp_char;
  1499. return FALSE; /* not enough memory to swap hunk! */
  1500. }
  1501. /* now turn the new into the old */
  1502. i = p_ptrn_lines + 1;
  1503. if (tp_char[i] == '\n') { /* account for possible blank line */
  1504. blankline = TRUE;
  1505. i++;
  1506. }
  1507. if (p_efake >= 0) { /* fix non-freeable ptr range */
  1508. if (p_efake <= i)
  1509. n = p_end - i + 1;
  1510. else
  1511. n = -i;
  1512. p_efake += n;
  1513. p_bfake += n;
  1514. }
  1515. for (n=0; i <= p_end; i++,n++) {
  1516. p_line[n] = tp_line[i];
  1517. p_Char[n] = tp_char[i];
  1518. if (p_Char[n] == '+')
  1519. p_Char[n] = '-';
  1520. p_len[n] = tp_len[i];
  1521. }
  1522. if (blankline) {
  1523. i = p_ptrn_lines + 1;
  1524. p_line[n] = tp_line[i];
  1525. p_Char[n] = tp_char[i];
  1526. p_len[n] = tp_len[i];
  1527. n++;
  1528. }
  1529. assert(p_Char[0] == '=');
  1530. p_Char[0] = '*';
  1531. for (s=p_line[0]; *s; s++)
  1532. if (*s == '-')
  1533. *s = '*';
  1534. /* now turn the old into the new */
  1535. assert(tp_char[0] == '*');
  1536. tp_char[0] = '=';
  1537. for (s=tp_line[0]; *s; s++)
  1538. if (*s == '*')
  1539. *s = '-';
  1540. for (i=0; n <= p_end; i++,n++) {
  1541. p_line[n] = tp_line[i];
  1542. p_Char[n] = tp_char[i];
  1543. if (p_Char[n] == '-')
  1544. p_Char[n] = '+';
  1545. p_len[n] = tp_len[i];
  1546. }
  1547. assert(i == p_ptrn_lines + 1);
  1548. i = p_ptrn_lines;
  1549. p_ptrn_lines = p_repl_lines;
  1550. p_repl_lines = i;
  1551. if (tp_line)
  1552. free (tp_line);
  1553. if (tp_len)
  1554. free (tp_len);
  1555. if (tp_char)
  1556. free (tp_char);
  1557. return TRUE;
  1558. }
  1559. /* Return whether file WHICH (0 = old, 1 = new) appears to nonexistent.
  1560. Return 1 for empty, 2 for nonexistent. */
  1561. bool
  1562. pch_says_nonexistent (which)
  1563. int which;
  1564. {
  1565. return p_says_nonexistent[which];
  1566. }
  1567. /* Return timestamp of patch header for file WHICH (0 = old, 1 = new),
  1568. or -1 if there was no timestamp or an error in the timestamp. */
  1569. time_t
  1570. pch_timestamp (which)
  1571. int which;
  1572. {
  1573. return p_timestamp[which];
  1574. }
  1575. /* Return the specified line position in the old file of the old context. */
  1576. LINENUM
  1577. pch_first()
  1578. {
  1579. return p_first;
  1580. }
  1581. /* Return the number of lines of old context. */
  1582. LINENUM
  1583. pch_ptrn_lines()
  1584. {
  1585. return p_ptrn_lines;
  1586. }
  1587. /* Return the probable line position in the new file of the first line. */
  1588. LINENUM
  1589. pch_newfirst()
  1590. {
  1591. return p_newfirst;
  1592. }
  1593. /* Return the number of lines in the replacement text including context. */
  1594. LINENUM
  1595. pch_repl_lines()
  1596. {
  1597. return p_repl_lines;
  1598. }
  1599. /* Return the number of lines in the whole hunk. */
  1600. LINENUM
  1601. pch_end()
  1602. {
  1603. return p_end;
  1604. }
  1605. /* Return the number of context lines before the first changed line. */
  1606. LINENUM
  1607. pch_prefix_context ()
  1608. {
  1609. return p_prefix_context;
  1610. }
  1611. /* Return the number of context lines after the last changed line. */
  1612. LINENUM
  1613. pch_suffix_context ()
  1614. {
  1615. return p_suffix_context;
  1616. }
  1617. /* Return the length of a particular patch line. */
  1618. size_t
  1619. pch_line_len(line)
  1620. LINENUM line;
  1621. {
  1622. return p_len[line];
  1623. }
  1624. /* Return the control character (+, -, *, !, etc) for a patch line. */
  1625. char
  1626. pch_char(line)
  1627. LINENUM line;
  1628. {
  1629. return p_Char[line];
  1630. }
  1631. /* Return a pointer to a particular patch line. */
  1632. char *
  1633. pfetch(line)
  1634. LINENUM line;
  1635. {
  1636. return p_line[line];
  1637. }
  1638. /* Output a patch line. */
  1639. bool
  1640. pch_write_line (line, file)
  1641. LINENUM line;
  1642. FILE *file;
  1643. {
  1644. bool after_newline = p_line[line][p_len[line] - 1] == '\n';
  1645. if (! fwrite (p_line[line], sizeof (*p_line[line]), p_len[line], file))
  1646. write_fatal ();
  1647. return after_newline;
  1648. }
  1649. /* Return where in the patch file this hunk began, for error messages. */
  1650. LINENUM
  1651. pch_hunk_beg()
  1652. {
  1653. return p_hunk_beg;
  1654. }
  1655. /* Apply an ed script by feeding ed itself. */
  1656. void
  1657. do_ed_script (ofp)
  1658. FILE *ofp;
  1659. {
  1660. static char const ed_program[] = ed_PROGRAM;
  1661. register char *t;
  1662. register file_offset beginning_of_this_line;
  1663. register bool this_line_is_command = FALSE;
  1664. register FILE *pipefp = 0;
  1665. register size_t chars_read;
  1666. if (!skip_rest_of_patch) {
  1667. assert (! inerrno);
  1668. copy_file (inname, TMPOUTNAME, instat.st_mode);
  1669. sprintf (buf, "%s %s%s", ed_program, verbosity == VERBOSE ? "" : "- ",
  1670. TMPOUTNAME);
  1671. fflush (stdout);
  1672. pipefp = popen(buf, binary_transput ? "wb" : "w");
  1673. if (!pipefp)
  1674. pfatal ("can't open pipe to `%s'", buf);
  1675. }
  1676. for (;;) {
  1677. beginning_of_this_line = file_tell (pfp);
  1678. chars_read = get_line ();
  1679. if (! chars_read) {
  1680. next_intuit_at(beginning_of_this_line,p_input_line);
  1681. break;
  1682. }
  1683. for (t = buf; ISDIGIT (*t) || *t == ','; t++)
  1684. continue;
  1685. this_line_is_command = (ISDIGIT (*buf) &&
  1686. (*t == 'd' || *t == 'c' || *t == 'a' || *t == 'i' || *t == 's') );
  1687. if (this_line_is_command) {
  1688. if (pipefp)
  1689. if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
  1690. write_fatal ();
  1691. if (*t != 'd' && *t != 's') {
  1692. while ((chars_read = get_line ()) != 0) {
  1693. if (pipefp)
  1694. if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
  1695. write_fatal ();
  1696. if (chars_read == 2 && strEQ (buf, ".\n"))
  1697. break;
  1698. }
  1699. }
  1700. }
  1701. else {
  1702. next_intuit_at(beginning_of_this_line,p_input_line);
  1703. break;
  1704. }
  1705. }
  1706. if (!pipefp)
  1707. return;
  1708. if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
  1709. || fflush (pipefp) != 0)
  1710. write_fatal ();
  1711. if (pclose (pipefp) != 0)
  1712. fatal ("%s FAILED", ed_program);
  1713. if (ofp)
  1714. {
  1715. FILE *ifp = fopen (TMPOUTNAME, binary_transput ? "rb" : "r");
  1716. int c;
  1717. if (!ifp)
  1718. pfatal ("can't open `%s'", TMPOUTNAME);
  1719. while ((c = getc (ifp)) != EOF)
  1720. if (putc (c, ofp) == EOF)
  1721. write_fatal ();
  1722. if (ferror (ifp) || fclose (ifp) != 0)
  1723. read_fatal ();
  1724. }
  1725. }