1162Sroot # include	"../hdr/defines.h"
2162Sroot # include	"../hdr/had.h"
3162Sroot 
4*19941Ssam SCCSID(@(#)delta.c	4.7);
5162Sroot USXALLOC();
6162Sroot 
715766Sralph # ifdef LOGDELTA
815766Sralph char	LogFile[] = "/usr/adm/sccs-log";
915766Sralph FILE	*Logf;
1015766Sralph # endif
1115766Sralph 
1215766Sralph char	Diffpgm[] = "/usr/local/bdiff";
13162Sroot FILE	*Diffin;
14162Sroot int	Debug	0;
15162Sroot struct packet gpkt;
16162Sroot struct sid sid;
17162Sroot int	num_files;
18162Sroot char	had[26];
19162Sroot char	*ilist, *elist, *glist;
20162Sroot char	*Comments, *Mrs;
21162Sroot int	Domrs;
2215766Sralph int	verbosity;
23162Sroot int	Did_id;
24162Sroot long	Szqfile;
25162Sroot char	Pfilename[FILESIZE];
26162Sroot FILE	*Xiop;
27162Sroot int	Xcreate;
28162Sroot 
29162Sroot main(argc,argv)
30162Sroot int argc;
31162Sroot register char *argv[];
32162Sroot {
33162Sroot 	register int i;
34162Sroot 	register char *p;
35162Sroot 	char c;
36162Sroot 	int testmore;
37162Sroot 	extern delta();
38162Sroot 	extern int Fcnt;
39162Sroot 
40162Sroot 	Fflags = FTLEXIT | FTLMSG | FTLCLN;
41162Sroot 	for(i=1; i<argc; i++)
42162Sroot 		if(argv[i][0] == '-' && (c=argv[i][1])) {
43162Sroot 			p = &argv[i][2];
44162Sroot 			testmore = 0;
45162Sroot 			switch (c) {
46162Sroot 
47162Sroot 			case 'r':
48162Sroot 				if (!p[0]) {
49162Sroot 					argv[i] = 0;
50162Sroot 					continue;
51162Sroot 				}
52162Sroot 				chksid(sid_ab(p,&sid),&sid);
53162Sroot 				break;
54162Sroot 			case 'g':
55162Sroot 				glist = p;
56162Sroot 				break;
57162Sroot 			case 'y':
58162Sroot 				Comments = p;
59162Sroot 				break;
60162Sroot 			case 'm':
61162Sroot 				Mrs = p;
62162Sroot 				break;
63162Sroot 			case 'p':
64162Sroot 			case 'n':
65162Sroot 			case 's':
66162Sroot 				testmore++;
67162Sroot 				break;
68162Sroot 			default:
69162Sroot 				fatal("unknown key letter (cm1)");
70162Sroot 			}
71162Sroot 
72162Sroot 			if (testmore) {
73162Sroot 				testmore = 0;
74162Sroot 				if (*p)
75162Sroot 					fatal(sprintf(Error,
76162Sroot 					  "value after %c arg (cm7)",c));
77162Sroot 			}
78162Sroot 			if (had[c - 'a']++)
79162Sroot 				fatal("key letter twice (cm2)");
80162Sroot 			argv[i] = 0;
81162Sroot 		}
82162Sroot 		else num_files++;
83162Sroot 
8415766Sralph 	if (num_files == 0)
85162Sroot 		fatal("missing file arg (cm3)");
86162Sroot 	if (!HADS)
87162Sroot 		verbosity = -1;
8815766Sralph # ifdef LOGDELTA
8915815Sralph 	Logf = fopen(LogFile, "a");
9015766Sralph # endif
91162Sroot 	setsig();
92162Sroot 	Fflags =& ~FTLEXIT;
93162Sroot 	Fflags =| FTLJMP;
94162Sroot 	for (i=1; i<argc; i++)
95162Sroot 		if (p=argv[i])
96162Sroot 			do_file(p,delta);
9715766Sralph # ifdef LOGDELTA
9815815Sralph 	if (Logf != NULL)
9915815Sralph 		fclose(Logf);
10015766Sralph # endif
101162Sroot 	exit(Fcnt ? 1 : 0);
102162Sroot }
103162Sroot 
104162Sroot 
105162Sroot delta(file)
10615766Sralph char *file;
107162Sroot {
108162Sroot 	static int first 1;
109162Sroot 	register char *p;
110162Sroot 	int n, linenum;
111162Sroot 	char type;
112162Sroot 	register int ser;
113162Sroot 	extern char had_dir, had_standinp;
114162Sroot 	extern char *Sflags[];
115162Sroot 	char dfilename[FILESIZE];
116162Sroot 	char gfilename[FILESIZE];
117162Sroot 	char line[512];
118162Sroot 	FILE *gin;
119162Sroot 	struct stats stats;
120162Sroot 	struct pfile *pp;
121162Sroot 	int inserted, deleted, orig;
122162Sroot 	int newser;
123162Sroot 	int status;
124162Sroot 	int diffloop;
125162Sroot 	int difflim;
126162Sroot 
127162Sroot 	if (setjmp(Fjmp))
128162Sroot 		return;
129162Sroot 	if (first) {
130162Sroot 		first = 0;
131162Sroot 		dohist(file);
132162Sroot 	}
133162Sroot 	sinit(&gpkt,file,1);
134162Sroot 	if (lockit(auxf(gpkt.p_file,'z'),2,getpid()))
135162Sroot 		fatal("cannot create lock file (cm4)");
136162Sroot 	gpkt.p_reopen = 1;
137162Sroot 	gpkt.p_stdout = stdout;
138162Sroot 	copy(auxf(gpkt.p_file,'g'),gfilename);
139162Sroot 	gin = xfopen(gfilename,0);
140162Sroot 	pp = rdpfile(&gpkt,&sid);
141162Sroot 	gpkt.p_cutoff = pp->pf_date;
142162Sroot 	ilist = pp->pf_ilist;
143162Sroot 	elist = pp->pf_elist;
144162Sroot 
145162Sroot 	if (dodelt(&gpkt,&stats,0,0) == 0)
146162Sroot 		fmterr(&gpkt);
147162Sroot 	if ((ser = sidtoser(&pp->pf_gsid,&gpkt)) == 0 ||
148162Sroot 		sidtoser(&pp->pf_nsid,&gpkt))
149162Sroot 			fatal("invalid sid in p-file (de3)");
150162Sroot 	doie(&gpkt,ilist,elist,glist);
151162Sroot 	setup(&gpkt,ser);
152162Sroot 	finduser(&gpkt);
153162Sroot 	doflags(&gpkt);
154*19941Ssam 	bcopy(&pp->pf_nsid,&gpkt.p_reqsid,sizeof(gpkt.p_reqsid));
155162Sroot 	permiss(&gpkt);
156162Sroot 	flushto(&gpkt,EUSERTXT,1);
157162Sroot 	gpkt.p_chkeof = 1;
158162Sroot 	copy(auxf(gpkt.p_file,'d'),dfilename);
159162Sroot 	gpkt.p_gout = xfcreat(dfilename,0444);
160162Sroot 	while(readmod(&gpkt)) {
161162Sroot 		chkid(gpkt.p_line);
162162Sroot 		fputs(gpkt.p_line,gpkt.p_gout);
163162Sroot 	}
164162Sroot 	fclose(gpkt.p_gout);
165162Sroot 	orig = gpkt.p_glnno;
166162Sroot 	gpkt.p_glnno = 0;
167162Sroot 	gpkt.p_verbose = verbosity;
168162Sroot 	Did_id = 0;
169162Sroot 	while (fgets(line,sizeof(line),gin) != NULL && !chkid(line))
170162Sroot 		;
171162Sroot 	fclose(gin);
172162Sroot 	if (gpkt.p_verbose && (num_files > 1 || had_dir || had_standinp))
173162Sroot 		fprintf(gpkt.p_stdout,"\n%s:\n",gpkt.p_file);
174162Sroot 	if (!Did_id)
175162Sroot 		if (Sflags[IDFLAG - 'a'])
176162Sroot 			fatal("no id keywords (cm6)");
177162Sroot 		else if (gpkt.p_verbose)
178162Sroot 			fprintf(stderr,"No id keywords (cm7)\n");
179162Sroot 
180162Sroot 	/*
181162Sroot 	The following while loop executes 'bdiff' on g-file and
182162Sroot 	d-file. If 'bdiff' fails (usually because segmentation
183162Sroot 	limit it is using is too large for 'diff'), it is
184162Sroot 	invoked again, with a lower segmentation limit.
185162Sroot 	*/
186162Sroot 	difflim = 3500;
187162Sroot 	diffloop = 0;
188162Sroot 	while (1) {
189162Sroot 		inserted = deleted = 0;
190162Sroot 		gpkt.p_glnno = 0;
191162Sroot 		gpkt.p_upd = 1;
192162Sroot 		gpkt.p_wrttn = 1;
193162Sroot 		getline(&gpkt);
194162Sroot 		gpkt.p_wrttn = 1;
195162Sroot 		newser = mkdelt(&gpkt,&pp->pf_nsid,&pp->pf_gsid,
196162Sroot 						diffloop,orig);
197162Sroot 		diffloop = 1;
198162Sroot 		flushto(&gpkt,EUSERTXT,0);
199162Sroot 		Diffin = dodiff(auxf(gpkt.p_file,'g'),dfilename,difflim);
200162Sroot 		while (n = getdiff(&type,&linenum)) {
201162Sroot 			if (type == INS) {
202162Sroot 				inserted =+ n;
203162Sroot 				insert(&gpkt,linenum,n,newser);
204162Sroot 			}
205162Sroot 			else {
206162Sroot 				deleted =+ n;
207162Sroot 				delete(&gpkt,linenum,n,newser);
208162Sroot 			}
209162Sroot 		}
210162Sroot 		fclose(Diffin);
211162Sroot 		if (gpkt.p_iop)
212162Sroot 			while (readmod(&gpkt))
213162Sroot 				;
214162Sroot 		wait(&status);
215162Sroot 		if (status) {		/* diff failed */
216162Sroot 			/*
217162Sroot 			Check top byte (exit code of child).
218162Sroot 			*/
219162Sroot 			if (((status >> 8) & 0377) == 32) /* 'execl' failed */
220162Sroot 				fatal(sprintf(Error,
221162Sroot 						"cannot execute '%s' (de12)",
222162Sroot 						Diffpgm));
223162Sroot 			/*
224162Sroot 			Re-try.
225162Sroot 			*/
226162Sroot 			if (difflim =- 500) {	/* reduce segmentation */
227162Sroot 				fprintf(stderr,
228162Sroot 			"'%s' failed, re-trying, segmentation = %d (de13)\n",
229162Sroot 					Diffpgm,difflim);
230162Sroot 				fclose(Xiop);	/* set up */
231162Sroot 				Xiop = 0;	/* for new x-file */
232162Sroot 				Xcreate = 0;
233162Sroot 				/*
234162Sroot 				Re-open s-file.
235162Sroot 				*/
236162Sroot 				gpkt.p_iop = xfopen(gpkt.p_file,0);
237162Sroot 				setbuf(gpkt.p_iop,gpkt.p_buf);
238162Sroot 				/*
239162Sroot 				Reset counters.
240162Sroot 				*/
241162Sroot 				gpkt.p_slnno = 0;
242162Sroot 				gpkt.p_ihash = 0;
243162Sroot 				gpkt.p_chash = 0;
244162Sroot 				gpkt.p_nhash = 0;
245162Sroot 				gpkt.p_keep = 0;
246162Sroot 			}
247162Sroot 			else
248162Sroot 				/* tried up to 500 lines, can't go on */
249162Sroot 				fatal("diff failed (de4)");
250162Sroot 		}
251162Sroot 		else {		/* no need to try again, worked */
252162Sroot 			break;			/* exit while loop */
253162Sroot 		}
254162Sroot 	}
255162Sroot 	unlink(dfilename);
256162Sroot 	stats.s_ins = inserted;
257162Sroot 	stats.s_del = deleted;
258162Sroot 	stats.s_unc = orig - deleted;
259162Sroot 	if (gpkt.p_verbose) {
260162Sroot 		fprintf(gpkt.p_stdout,"%u inserted\n",stats.s_ins);
261162Sroot 		fprintf(gpkt.p_stdout,"%u deleted\n",stats.s_del);
262162Sroot 		fprintf(gpkt.p_stdout,"%u unchanged\n",stats.s_unc);
263162Sroot 	}
264162Sroot 	flushline(&gpkt,&stats);
265162Sroot 	rename(auxf(gpkt.p_file,'x'),gpkt.p_file);
266162Sroot 	if (Szqfile)
267162Sroot 		rename(auxf(&gpkt.p_file,'q'),Pfilename);
268162Sroot 	else {
269162Sroot 		xunlink(Pfilename);
270162Sroot 		xunlink(auxf(&gpkt.p_file,'q'));
271162Sroot 	}
272162Sroot 	clean_up(0);
273162Sroot 	if (!HADN) {
274162Sroot 		setuid(getuid());
275162Sroot 		unlink(gfilename);
276162Sroot 	}
277162Sroot }
278162Sroot 
279162Sroot 
280162Sroot mkdelt(pkt,sp,osp,diffloop,orig_nlines)
281162Sroot struct packet *pkt;
282162Sroot struct sid *sp, *osp;
283162Sroot int diffloop;
284162Sroot int orig_nlines;
285162Sroot {
286162Sroot 	extern long Timenow;
287162Sroot 	struct deltab dt;
288162Sroot 	char str[128];
289162Sroot 	int newser;
290162Sroot 	extern char *Sflags[];
291162Sroot 	register char *p;
292162Sroot 	int ser_inc, opred, nulldel;
293162Sroot 
294162Sroot 	if (!diffloop && pkt->p_verbose) {
295162Sroot 		sid_ba(sp,str);
296162Sroot 		fprintf(pkt->p_stdout,"%s\n",str);
297162Sroot 	}
298162Sroot 	putline(pkt,sprintf(str,"%c%c00000\n",CTLCHAR,HEAD));
299162Sroot 	newstats(pkt,str,"0");
300*19941Ssam 	bcopy(sp,&dt.d_sid,sizeof(dt.d_sid));
301162Sroot 
302162Sroot 	/*
303162Sroot 	Check if 'null' deltas should be inserted
304162Sroot 	(only if 'null' flag is in file and
305162Sroot 	releases are being skipped) and set
306162Sroot 	'nulldel' indicator appropriately.
307162Sroot 	*/
308162Sroot 	if (Sflags[NULLFLAG - 'a'] && (sp->s_rel > osp->s_rel + 1) &&
309162Sroot 			!sp->s_br && !sp->s_seq &&
310162Sroot 			!osp->s_br && !osp->s_seq)
311162Sroot 		nulldel = 1;
312162Sroot 	else
313162Sroot 		nulldel = 0;
314162Sroot 	/*
315162Sroot 	Calculate how many serial numbers are needed.
316162Sroot 	*/
317162Sroot 	if (nulldel)
318162Sroot 		ser_inc = sp->s_rel - osp->s_rel;
319162Sroot 	else
320162Sroot 		ser_inc = 1;
321162Sroot 	/*
322162Sroot 	Find serial number of the new delta.
323162Sroot 	*/
324162Sroot 	newser = dt.d_serial = maxser(pkt) + ser_inc;
325162Sroot 	/*
326162Sroot 	Find old predecessor's serial number.
327162Sroot 	*/
328162Sroot 	opred = sidtoser(osp,pkt);
329162Sroot 	if (nulldel)
330162Sroot 		dt.d_pred = newser - 1;	/* set predecessor to 'null' delta */
331162Sroot 	else
332162Sroot 		dt.d_pred = opred;
333162Sroot 	dt.d_datetime = Timenow;
3346173Seric 	substr(logname(),dt.d_pgmr,0,LNLNAM);
335162Sroot 	dt.d_type = 'D';
336162Sroot 	del_ba(&dt,str);
337162Sroot 	putline(pkt,str);
33815766Sralph # ifdef LOGDELTA
33915815Sralph 	if (Logf != NULL) {
34015815Sralph 		if (pkt->p_file[0] != '/') {
34115815Sralph 			char buf[1024];
34215766Sralph 
34315815Sralph 			if (getwd(buf) != NULL)
34415815Sralph 				fprintf(Logf, "%s/", buf);
34515815Sralph 		}
34615815Sralph 		fprintf(Logf, "%s:\n%s%s\n", pkt->p_file, str + 5, Comments);
34715766Sralph 	}
34815766Sralph # endif
349162Sroot 	if (ilist)
350162Sroot 		mkixg(pkt,INCLUSER,INCLUDE);
351162Sroot 	if (elist)
352162Sroot 		mkixg(pkt,EXCLUSER,EXCLUDE);
353162Sroot 	if (glist)
354162Sroot 		mkixg(pkt,IGNRUSER,IGNORE);
355162Sroot 	if (Mrs) {
356162Sroot 		if (!(p = Sflags[VALFLAG - 'a']))
357162Sroot 			fatal("MRs not allowed (de8)");
358162Sroot 		if (*p && !diffloop && valmrs(pkt,p))
359162Sroot 			fatal("invalid MRs (de9)");
360162Sroot 		putmrs(pkt);
361162Sroot 	}
362162Sroot 	else if (Sflags[VALFLAG - 'a'])
363162Sroot 		fatal("MRs required (de10)");
364162Sroot 	putline(pkt,sprintf(str,"%c%c ",CTLCHAR,COMMENTS));
365162Sroot 	putline(pkt,Comments);
366162Sroot 	putline(pkt,"\n");
367162Sroot 	putline(pkt,sprintf(str,CTLSTR,CTLCHAR,EDELTAB));
368162Sroot 	if (nulldel)			/* insert 'null' deltas */
369162Sroot 		while (--ser_inc) {
370162Sroot 			putline(pkt,sprintf(str,"%c%c %s/%s/%05u\n",
371162Sroot 				CTLCHAR, STATS,
372162Sroot 				"00000", "00000", orig_nlines));
373162Sroot 			dt.d_sid.s_rel =- 1;
374162Sroot 			dt.d_serial =- 1;
375162Sroot 			if (ser_inc != 1)
376162Sroot 				dt.d_pred =- 1;
377162Sroot 			else
378162Sroot 				dt.d_pred = opred;	/* point to old pred */
379162Sroot 			del_ba(&dt,str);
380162Sroot 			putline(pkt,str);
381162Sroot 			putline(pkt,sprintf(str,"%c%c ",CTLCHAR,COMMENTS));
382162Sroot 			putline(pkt,"AUTO NULL DELTA\n");
383162Sroot 			putline(pkt,sprintf(str,CTLSTR,CTLCHAR,EDELTAB));
384162Sroot 		}
385162Sroot 	return(newser);
386162Sroot }
387162Sroot 
388162Sroot 
389162Sroot mkixg(pkt,reason,ch)
390162Sroot struct packet *pkt;
391162Sroot int reason;
392162Sroot char ch;
393162Sroot {
394162Sroot 	int n;
395162Sroot 	char str[512];
396162Sroot 
397162Sroot 	putline(pkt,sprintf(str,"%c%c",CTLCHAR,ch));
398162Sroot 	for (n = maxser(pkt); n; n--) {
399162Sroot 		if (pkt->p_apply[n].a_reason == reason)
400162Sroot 			putline(pkt,sprintf(str," %u",n));
401162Sroot 	}
402162Sroot 	putline(pkt,"\n");
403162Sroot }
404162Sroot 
405162Sroot 
406162Sroot putmrs(pkt)
407162Sroot struct packet *pkt;
408162Sroot {
409162Sroot 	register char **argv;
410162Sroot 	char str[64];
411162Sroot 	extern char *Varg[];
412162Sroot 
413162Sroot 	for (argv = &Varg[VSTART]; *argv; argv++)
414162Sroot 		putline(pkt,sprintf(str,"%c%c %s\n",CTLCHAR,MRNUM,*argv));
415162Sroot }
416162Sroot 
417162Sroot 
418162Sroot rdpfile(pkt,sp)
419162Sroot register struct packet *pkt;
420162Sroot struct sid *sp;
421162Sroot {
422162Sroot 	char *user;
423162Sroot 	struct pfile pf;
424162Sroot 	static struct pfile goodpf;
425162Sroot 	char line[512];
4266636Ssam 	int cnt, root;
427162Sroot 	FILE *in, *out;
428162Sroot 
429162Sroot 	cnt = -1;
430162Sroot 	user = logname();
431*19941Ssam 	bzero(&goodpf,sizeof(goodpf));
432162Sroot 	in = xfopen(auxf(pkt->p_file,'p'),0);
433162Sroot 	out = xfcreat(auxf(pkt->p_file,'q'),0644);
4346636Ssam 	root = getuid() == 0;
435162Sroot 	while (fgets(line,sizeof(line),in) != NULL) {
436162Sroot 		pf_ab(line,&pf,1);
4376636Ssam 		if (root || equal(pf.pf_user,user)) {
438162Sroot 			if (sp->s_rel == 0) {
439162Sroot 				if (++cnt) {
440162Sroot 					fclose(out);
441162Sroot 					fclose(in);
442162Sroot 					fatal("missing -r argument (de1)");
443162Sroot 				}
444*19941Ssam 				bcopy(&pf,&goodpf,sizeof(pf));
445162Sroot 				continue;
446162Sroot 			}
447162Sroot 			else if (sp->s_rel == pf.pf_gsid.s_rel &&
448162Sroot 				sp->s_lev == pf.pf_gsid.s_lev &&
449162Sroot 				sp->s_br == pf.pf_gsid.s_br &&
450162Sroot 				sp->s_seq == pf.pf_gsid.s_seq) {
451*19941Ssam 					bcopy(&pf,&goodpf,sizeof(pf));
452162Sroot 					continue;
453162Sroot 			}
454162Sroot 		}
455162Sroot 		fputs(line,out);
456162Sroot 	}
457162Sroot 	fflush(out);
458162Sroot 	fstat(fileno(out),&Statbuf);
459162Sroot 	Szqfile = Statbuf.st_size;
460162Sroot 	copy(auxf(pkt->p_file,'p'),Pfilename);
461162Sroot 	fclose(out);
462162Sroot 	fclose(in);
463162Sroot 	if (!goodpf.pf_user[0])
464162Sroot 		fatal("not in p-file (de2)");
465162Sroot 	return(&goodpf);
466162Sroot }
467162Sroot 
468162Sroot 
469162Sroot dodiff(newf,oldf,difflim)
470162Sroot char *newf, *oldf;
471162Sroot int difflim;
472162Sroot {
473162Sroot 	register int i;
474162Sroot 	int pfd[2];
475162Sroot 	FILE *iop;
476162Sroot 	extern char Diffpgm[];
477162Sroot 	char num[10];
478162Sroot 
479162Sroot 	xpipe(pfd);
480162Sroot 	if ((i = fork()) < 0) {
481162Sroot 		close(pfd[0]);
482162Sroot 		close(pfd[1]);
483162Sroot 		fatal("cannot fork, try again (de11)");
484162Sroot 	}
485162Sroot 	else if (i == 0) {
486162Sroot 		close(pfd[0]);
487*19941Ssam 		dup2(pfd[1], 1);
488*19941Ssam 		if (pfd[1] != 1)
489*19941Ssam 			close(pfd[1]);
490*19941Ssam 		for (i = getdtablesize(); i > 4; i--)
491162Sroot 			close(i);
492162Sroot 		sprintf(num,"%d",difflim);
493162Sroot 		execl(Diffpgm,Diffpgm,oldf,newf,num,"-s",0);
494162Sroot 		close(1);
495162Sroot 		exit(32);	/* tell parent that 'execl' failed */
496162Sroot 	}
497162Sroot 	else {
498162Sroot 		close(pfd[1]);
499*19941Ssam 		iop = fdopen(pfd[0],"r");
500162Sroot 		return(iop);
501162Sroot 	}
502162Sroot }
503162Sroot 
504162Sroot 
505162Sroot getdiff(type,plinenum)
506162Sroot register char *type;
507162Sroot register int *plinenum;
508162Sroot {
509162Sroot 	char line[512];
510162Sroot 	register char *p;
511162Sroot 	int num_lines;
512162Sroot 	static int chg_num, chg_ln;
513162Sroot 	int lowline, highline;
514162Sroot 
515*19941Ssam 	if ((p = rddiff(line,sizeof (line))) == NULL)
516162Sroot 		return(0);
517162Sroot 
518162Sroot 	if (*p == '-') {
519162Sroot 		*type = INS;
520162Sroot 		*plinenum = chg_ln;
521162Sroot 		num_lines = chg_num;
522162Sroot 	}
523162Sroot 	else {
524162Sroot 		p = linerange(p,&lowline,&highline);
525162Sroot 		*plinenum = lowline;
526162Sroot 
527162Sroot 		switch(*p++) {
528162Sroot 		case 'd':
529162Sroot 			num_lines = highline - lowline + 1;
530162Sroot 			*type = DEL;
5312167Seric 			skiplines(line,num_lines);
532162Sroot 			break;
533162Sroot 
534162Sroot 		case 'a':
535162Sroot 			linerange(p,&lowline,&highline);
536162Sroot 			num_lines = highline - lowline + 1;
537162Sroot 			*type = INS;
538162Sroot 			break;
539162Sroot 
540162Sroot 		case 'c':
541162Sroot 			chg_ln = lowline;
542162Sroot 			num_lines = highline - lowline + 1;
543162Sroot 			linerange(p,&lowline,&highline);
544162Sroot 			chg_num = highline - lowline + 1;
545162Sroot 			*type = DEL;
5462167Seric 			skiplines(line,num_lines);
547162Sroot 			break;
548162Sroot 		}
549162Sroot 	}
550162Sroot 
551162Sroot 	return(num_lines);
552162Sroot }
553162Sroot 
554162Sroot 
555162Sroot insert(pkt,linenum,n,ser)
556162Sroot register struct packet *pkt;
557162Sroot register int linenum;
558162Sroot register int n;
559162Sroot int ser;
560162Sroot {
561162Sroot 	char str[512];
562162Sroot 
563162Sroot 	after(pkt,linenum);
564162Sroot 	putline(pkt,sprintf(str,"%c%c %u\n",CTLCHAR,INS,ser));
565162Sroot 	for (++n; --n; ) {
566162Sroot 		rddiff(str,sizeof(str));
567162Sroot 		putline(pkt,&str[2]);
568162Sroot 	}
569162Sroot 	putline(pkt,sprintf(str,"%c%c %u\n",CTLCHAR,END,ser));
570162Sroot }
571162Sroot 
572162Sroot 
573162Sroot delete(pkt,linenum,n,ser)
574162Sroot register struct packet *pkt;
575162Sroot register int linenum;
576162Sroot int n;
577162Sroot register int ser;
578162Sroot {
579162Sroot 	char str[512];
580162Sroot 
581162Sroot 	before(pkt,linenum);
582162Sroot 	putline(pkt,sprintf(str,"%c%c %u\n",CTLCHAR,DEL,ser));
583162Sroot 	after(pkt,linenum + n - 1);
584162Sroot 	putline(pkt,sprintf(str,"%c%c %u\n",CTLCHAR,END,ser));
585162Sroot }
586162Sroot 
587162Sroot 
588162Sroot after(pkt,n)
589162Sroot register struct packet *pkt;
590162Sroot register int n;
591162Sroot {
592162Sroot 	before(pkt,n);
593162Sroot 	if (pkt->p_glnno == n)
594162Sroot 		putline(pkt,0);
595162Sroot }
596162Sroot 
597162Sroot 
598162Sroot before(pkt,n)
599162Sroot register struct packet *pkt;
600162Sroot register int n;
601162Sroot {
602162Sroot 	while (pkt->p_glnno < n) {
603162Sroot 		if (!readmod(pkt))
604162Sroot 			break;
605162Sroot 	}
606162Sroot }
607162Sroot 
608162Sroot 
609162Sroot linerange(cp,low,high)
610162Sroot register char *cp;
611162Sroot register int *low, *high;
612162Sroot {
613162Sroot 	cp = satoi(cp,low);
614162Sroot 	if (*cp == ',')
615162Sroot 		cp = satoi(++cp,high);
616162Sroot 	else
617162Sroot 		*high = *low;
618162Sroot 
619162Sroot 	return(cp);
620162Sroot }
621162Sroot 
622162Sroot 
623162Sroot skiplines(lp,num)
624162Sroot register char *lp;
625162Sroot register int num;
626162Sroot {
627162Sroot 	for (++num;--num;)
628162Sroot 		rddiff(lp,512);
629162Sroot }
630162Sroot 
631162Sroot 
632162Sroot rddiff(s,n)
633162Sroot register char *s;
634162Sroot register int n;
635162Sroot {
636162Sroot 	register int r;
637162Sroot 
638162Sroot 	if ((r = fgets(s,n,Diffin)) != NULL && HADP)
639162Sroot 		fputs(s,gpkt.p_stdout);
640162Sroot 	return(r);
641162Sroot }
642162Sroot 
643162Sroot 
644162Sroot enter(pkt,ch,n,sidp)
645162Sroot struct packet *pkt;
646162Sroot char ch;
647162Sroot int n;
648162Sroot struct sid *sidp;
649162Sroot {
650162Sroot 	char str[32];
651162Sroot 	register struct apply *ap;
652162Sroot 
653162Sroot 	sid_ba(sidp,str);
654162Sroot 	ap = &pkt->p_apply[n];
655162Sroot 	if (pkt->p_cutoff > pkt->p_idel[n].i_datetime)
656162Sroot 		switch(ap->a_code) {
657162Sroot 
658162Sroot 		case EMPTY:
659162Sroot 			switch (ch) {
660162Sroot 			case INCLUDE:
661162Sroot 				condset(ap,APPLY,INCLUSER);
662162Sroot 				break;
663162Sroot 			case EXCLUDE:
664162Sroot 				condset(ap,NOAPPLY,EXCLUSER);
665162Sroot 				break;
666162Sroot 			case IGNORE:
667162Sroot 				condset(ap,EMPTY,IGNRUSER);
668162Sroot 				break;
669162Sroot 			}
670162Sroot 			break;
671162Sroot 		case APPLY:
672162Sroot 			fatal("internal error in delta/enter() (de5)");
673162Sroot 			break;
674162Sroot 		case NOAPPLY:
675162Sroot 			fatal("internal error in delta/enter() (de6)");
676162Sroot 			break;
677162Sroot 		default:
678162Sroot 			fatal("internal error in delta/enter() (de7)");
679162Sroot 			break;
680162Sroot 		}
681162Sroot }
682162Sroot 
683162Sroot 
684162Sroot escdodelt()	/* dummy routine for dodelt() */
685162Sroot {
686162Sroot }
687162Sroot 
688162Sroot 
689162Sroot clean_up(n)
690162Sroot {
691162Sroot 	if (gpkt.p_file[0])
692162Sroot 		unlockit(auxf(gpkt.p_file,'z'),getpid());
693162Sroot 	xrm(&gpkt);
694162Sroot 	xfreeall();
695162Sroot }
696