xref: /csrg-svn/contrib/ed/j.c (revision 60663)
157690Sbostic /*-
2*60663Sbostic  * Copyright (c) 1992, 1993
3*60663Sbostic  *	The Regents of the University of California.  All rights reserved.
457690Sbostic  *
557690Sbostic  * This code is derived from software contributed to Berkeley by
657690Sbostic  * Rodney Ruddock of the University of Guelph.
757690Sbostic  *
857690Sbostic  * %sccs.include.redist.c%
957690Sbostic  */
1057690Sbostic 
1157690Sbostic #ifndef lint
12*60663Sbostic static char sccsid[] = "@(#)j.c	8.1 (Berkeley) 05/31/93";
1357690Sbostic #endif /* not lint */
1457690Sbostic 
1557710Sbostic #include <sys/types.h>
1657710Sbostic 
1757710Sbostic #include <regex.h>
1857710Sbostic #include <setjmp.h>
1957710Sbostic #include <stdio.h>
2057710Sbostic #include <stdlib.h>
2157710Sbostic #include <string.h>
2257710Sbostic 
2358315Sbostic #ifdef DBI
2458315Sbostic #include <db.h>
2558315Sbostic #endif
2658315Sbostic 
2757690Sbostic #include "ed.h"
2857710Sbostic #include "extern.h"
2957690Sbostic 
3057690Sbostic /*
3157690Sbostic  * Join the spec'd lines onto one line. Make the new line from the
3257690Sbostic  * old lines and then delete the old ones (undo-able).
3357690Sbostic  */
3457690Sbostic 
3557690Sbostic void
j(inputt,errnum)3657690Sbostic j(inputt, errnum)
3757710Sbostic 	FILE *inputt;
3857710Sbostic 	int *errnum;
3957690Sbostic {
4057710Sbostic 	LINE *l_ptr, *l_temp_line;
4157710Sbostic 	long l_ttl = 0;
4257710Sbostic 	char *l_temp1;
4357690Sbostic 
4458564Sralph 	if (Start_default && End_default) {
4558564Sralph 		Start = current;
4657710Sbostic 		*errnum = 1;
4758564Sralph 		if (Start == NULL) {
4858315Sbostic 			strcpy(help_msg, "buffer empty");
4958315Sbostic 			*errnum = -1;
5058315Sbostic 			return;
5158315Sbostic 		} else
5258564Sralph 			if ((Start->below) != NULL)
5358564Sralph 				End = Start->below;
5457710Sbostic 		else
5557710Sbostic 			return;
5657710Sbostic 	} else
5758564Sralph 		if (Start_default) {
5857710Sbostic 			if (rol(inputt, errnum))
5957710Sbostic 				return;
6057690Sbostic 
6157710Sbostic 			if (End == NULL) {
6257710Sbostic 				strcpy(help_msg, "bad address");
6357710Sbostic 				*errnum = -1;
6457710Sbostic 			} else {
6557690Sbostic #ifdef BSD
6657710Sbostic 				/*
6757710Sbostic 				 * For BSD a 'j' with one address sets
6857710Sbostic 				 * "current" to that line
6957710Sbostic 				 */
7058564Sralph 				if (Start)
7158564Sralph 					current = Start;
7257690Sbostic #endif
7357710Sbostic 				*errnum = 1;
7457710Sbostic 			}
7557710Sbostic 			return;
7657710Sbostic 		}
7758315Sbostic 
7858564Sralph 	if (Start == NULL) {
7958315Sbostic 		strcpy(help_msg, "buffer empty");
8057710Sbostic 		*errnum = -1;
8157710Sbostic 		return;
8257710Sbostic 	}
8358315Sbostic 
8458564Sralph 	Start_default = End_default = 0;
8557690Sbostic 
8657710Sbostic 	if (rol(inputt, errnum))
8757710Sbostic 		return;
8857690Sbostic 
8957710Sbostic 	if (g_flag == 0)
9057710Sbostic 		u_clr_stk();
9157710Sbostic 	u_set = 1;		/* set for d */
9257690Sbostic 
9357710Sbostic 	/* Figure out what the length of the joined lines will be. */
9458564Sralph 	for (l_ptr = Start; l_ptr != (End->below); l_ptr = (l_ptr->below))
9557710Sbostic 		l_ttl = l_ptr->len + l_ttl;
9657690Sbostic 
9757710Sbostic 	if (l_ttl > nn_max) {
9857710Sbostic 		/*
9957710Sbostic 		 * The new line is bigger than any so far, so make more
10057710Sbostic 		 * space.
10157710Sbostic 		 */
10258315Sbostic 		sigspecial++;
10357710Sbostic 		free(text);
10457710Sbostic 		nn_max = l_ttl;
10557710Sbostic 		text = calloc(l_ttl + 2, sizeof(char));
10658315Sbostic 		sigspecial--;
10757710Sbostic 		if (text == NULL) {
10857710Sbostic 			*errnum = -1;
10957710Sbostic 			strcpy(help_msg, "out of memory error");
11057710Sbostic 			return;
11157710Sbostic 		}
11257710Sbostic 	}
11357710Sbostic 	l_temp1 = calloc(l_ttl + 2, sizeof(char));
11458315Sbostic 	if (l_temp1 == NULL) {
11557710Sbostic 		*errnum = -1;
11657710Sbostic 		strcpy(help_msg, "out of memory error");
11757710Sbostic 		return;
11857710Sbostic 	}
11957710Sbostic 	l_temp1[0] = '\0';
12057690Sbostic 
12158564Sralph 	l_ptr = Start;
12258315Sbostic 
12358315Sbostic 	sigspecial++;
12457710Sbostic 	for (;;) {
12557710Sbostic 		/* Get each line and catenate. */
12658315Sbostic 		get_line(l_ptr->handle, l_ptr->len);
12758315Sbostic 		if (sigint_flag && (!sigspecial))
12857710Sbostic 			goto point;
12957710Sbostic 		strcat(l_temp1, text);
13057710Sbostic 		l_ptr = l_ptr->below;
13157710Sbostic 		if (l_ptr == End->below)
13257710Sbostic 			break;
13357710Sbostic 	}
13457690Sbostic 
13557710Sbostic 	l_temp_line = malloc(sizeof(LINE));
13658315Sbostic 	if (l_temp_line == NULL) {
13757710Sbostic 		*errnum = -1;
13857710Sbostic 		strcpy(help_msg, "out of memory error");
13957710Sbostic 		return;
14057710Sbostic 	}
14157710Sbostic 	(l_temp_line->len) = l_ttl;
14257710Sbostic 	/* Add the new line to the buffer. */
14357710Sbostic 	(l_temp_line->handle) = add_line(l_temp1, l_ttl);
14458564Sralph 	if (Start == top) {
14557710Sbostic 		top = l_temp_line;
14657710Sbostic 		(l_temp_line->above) = NULL;
14757710Sbostic 	} else {
14858564Sralph 		(l_temp_line->above) = Start->above;
14957710Sbostic 		u_add_stk(&(l_temp_line->above->below));
15057710Sbostic 		(l_temp_line->above->below) = l_temp_line;
15157710Sbostic 	}
15258564Sralph 	(l_temp_line->below) = Start;
15358564Sralph 	u_add_stk(&(Start->above));
15458564Sralph 	(Start->above) = l_temp_line;
15557690Sbostic 
15658315Sbostic 	sigspecial--;
15758315Sbostic 	if (sigint_flag && (!sigspecial))
15858315Sbostic 		goto mk;
15957710Sbostic 	ungetc(ss, inputt);
16057710Sbostic 	/* Delete the lines used to make the joined line. */
16158315Sbostic 	join_flag = 1;
16257710Sbostic 	d(inputt, errnum);
16358315Sbostic 	join_flag = 0;
16457710Sbostic 	if (*errnum < 0)
16557710Sbostic 		return;
16657710Sbostic 	*errnum = 0;
16758315Sbostic mk:
16857710Sbostic 	current = l_temp_line;
16957690Sbostic 
17057710Sbostic 	*errnum = 1;
17157690Sbostic 
17257710Sbostic point:	u_set = 0;
17357710Sbostic 	free(l_temp1);
17457710Sbostic }
175