summaryrefslogtreecommitdiff
path: root/utils/Spiff/spiff.c
blob: eb0c75588704874751422e6e1fe0fc5860f8af2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*                        Copyright (c) 1988 Bellcore
**                            All Rights Reserved
**       Permission is granted to copy or use this program, EXCEPT that it
**       may not be sold for profit, the copyright notice must be reproduced
**       on copies, and credit should be given to Bellcore where it is due.
**       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
*/


#ifndef lint
static char rcsid[]= "$Header$";
#endif


#include <stdio.h>
#include "misc.h"
#include "flagdefs.h"
#include "parse.h"
#include "edit.h"
#include "line.h"
#include "token.h"
#include "tol.h"
#include "command.h"
#include "compare.h"
#include "exact.h"
#include "miller.h"
#include "visual.h"
#include "output.h"

extern void _Y_doargs();

static int _Y_eflag = 0;	/* use exact match algorithm */
static int _Y_vflag = 0;	/* use visual mode */

/*
**	this is the set of flags that gets used throughout the top module
**	as well as being used to communicate between modules.
*/
static int _Y_flags;

main(argc,argv)
int argc;
char *argv[];
{
	E_edit edit_end;
	char *filename[2];

	int max_d; 	/* max number of differences allowed */
	int i;		/* loop counter */

	/*
	**	parse the command line
	*/
	_Y_doargs(argc,argv,&(filename[0]),&(filename[1]),&max_d);

	/*
	**	initialize the default tolerance if it
	**		hasn't been set already.
	*/
	T_initdefault();

	/*
	**	read and then parse the files
	*/

	/*
	**	L_initfile return a code that indicates if the
	**	entire file was read or not
	**
	**	P_fileparse also knows how to start at someplace other
	**		than the first line of file
	**
	**	Taken together, this is enough to do step our way
	**		through the file using an exact match algorithm.
	**
	**	Oh well, someday . . .
	*/
	for(i=0;i<=1;i++)
	{
		/*
		**	read the file into core
		*/
		(void) L_init_file(i,filename[i]);
		K_settmax(i,0);		/* start tokens at 0 */
		/*
		**	and parse the files into tokens
		*/
		P_file_parse(i,0,L_getrlmax(i),_Y_flags);
	}

	if (_Y_vflag)
	{
		return(V_visual(_Y_flags));
	}

	/*
	**	if max_d was not set on the command line
	**		set it to be as large as is possible
	**		since the most changes possible would
	**		be to delete all the tokens in the
	**		first file and add all the tokens from
	**		the second, the max possible is the
	**		sum of the number of tokens in the
	**		two files.
	*/
	if (-1 == max_d)
		max_d = K_gettmax(0) + K_gettmax(1);

	if (_Y_eflag)
	{
		edit_end = Q_do_exact(K_gettmax(0),K_gettmax(1),
					max_d,_Y_flags);
	}
	else
	{
		edit_end = G_do_miller(K_gettmax(0), K_gettmax(1),
				     max_d,_Y_flags);
	}

	if (E_NULL != edit_end)
	{
		O_output(edit_end,_Y_flags);
		return(1);
	}
	return(0);
}

/*
**	break a string into individual lines and feed
**		them to the command module
*/
static void
_Y_cmdlines(from)
char *from;
{
	char buf[Z_LINELEN]; 
	char *to;
	while ('\0' != *from)
	{
		/*
		**	copy line into buf
		*/
		to = buf;
		while (('\0' != *from) && ('\n' != *from))
		{
			*to++ = *from++;
		}
		*to = '\0';	/* terminate the line */

		/*
		**	hand the line to the command module
		*/
		C_addcmd(buf);
		/*
		**	skip the newline
		*/
		if ('\n' == *from)
		{
			from++;
		}
	}
}

/*
**	this useful macro handle arguements that are adjacent
**	to a flag or in the following word e.g --
**
**		-a XXX 
**	and
**		-aXXX 
**
**	both work when SETPTR is used. 
*/
#define SETPTR	{if(strlen(argv[1]) == 2) {argv++;argc--;ptr=argv[1];}else ptr=(&argv[1][2]);}

static void
_Y_doargs(argc,argv,file1,file2,max_d)
int argc;
char *argv[];
char **file1,**file2;
int *max_d;
{
	char *ptr;

	/*
	**	mark maximum number of tokens as being unset
	*/
	*max_d = -1;

	while (argc > 1 && argv[1][0] == '-')
	{
		switch (argv[1][1])
		{
			case 't':
				_Y_flags |= U_TOKENS;
				break;
			case 'w':
				_Y_flags |= U_INCLUDE_WS;
				break;

			case 'b':
				_Y_flags |= U_BYTE_COMPARE;
				break;

			case 'c':
				_Y_flags |= U_NO_CASE;
				break;
			case 'd' :
				_Y_flags |= U_NEED_DECIMAL;
				break;
			case 'm' :
				_Y_flags |= U_INC_SIGN;
				break;
			case 'a':
				SETPTR;
				T_defatol(ptr);
				break;
			case 'r':
				SETPTR;
				T_defrtol(ptr);
				break;
			case 'i':
				T_defitol();
				break;
			case 'e' :
				_Y_eflag = 1;
				break;
			case 'v' :
				_Y_vflag = 1;
				break;
			case 'q' :
				Z_setquiet();
				break;
			case 's' :
				SETPTR;
				_Y_cmdlines(ptr);
				break;
			case 'f' :
			{
				extern FILE *fopen();
				char buf[Z_LINELEN];
				FILE *cmdfile;
				SETPTR;
				if ((FILE*) NULL ==
					(cmdfile = fopen(ptr,"r")))
				{
					Z_fatal("can't open command file\n");
				}
				while ((char*) NULL !=
					(char*) fgets(buf,Z_LINELEN,cmdfile))
				{
					C_addcmd(buf);
				}
				(void) fclose(cmdfile);
				break;
			}
			/*
			**	useful commands for
			**	 the C programming language
			*/
			case 'C' :
				C_addcmd("literal  \"   \"    \\ ");
				C_addcmd("comment  /*  */	 ");
				C_addcmd("literal  &&		 ");
				C_addcmd("literal  ||		 ");
				C_addcmd("literal  <=		 ");
				C_addcmd("literal  >=		 ");
				C_addcmd("literal  !=		 ");
				C_addcmd("literal  ==		 ");
				C_addcmd("literal  --		 ");
				C_addcmd("literal  ++		 ");
				C_addcmd("literal  <<		 ");
				C_addcmd("literal  >>		 ");
				C_addcmd("literal  ->		 ");
				C_addcmd("addalpha _		 ");
				C_addcmd("tol      a0 		 ");
				break;
			/*
			**	useful commands for
			**	 the Bourne shell programming language
			*/
			case 'S' :
				C_addcmd("literal  '    '    \\	");
				C_addcmd("comment  #    $	");
				C_addcmd("tol      a0 		");
				break;
			/*
			**	useful commands for
			**	 the Fortran programming language
			*/
			case 'F' :
				C_addcmd("literal  '	'     ' ");
				C_addcmd("comment  ^C   $	");
				C_addcmd("tol      a0 		");
				break;
			/*
			**	useful commands for
			**	 the Lisp programming language
			*/
			case 'L' :
				C_addcmd("literal  \" 	\"	");
				C_addcmd("comment  ; 	$	");
				C_addcmd("tol      a0 		");
				break;
			/*
			**	useful commands for
			**	 the Modula-2 programming language
			*/
			case 'M' :
				C_addcmd("literal ' 	'	");
				C_addcmd("literal \"	\"	");
				C_addcmd("comment (*	*)	");
				C_addcmd("literal :=		");
				C_addcmd("literal <>		");
				C_addcmd("literal <=		");
				C_addcmd("literal >=		");
				C_addcmd("tol      a0 		");
				break;
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				*max_d = atoi(&argv[1][1]);
				break;
			default:
				Z_fatal("don't understand arguments\n");
		}
		++argv;
		--argc;
	}
	if (argc != 3)
		Z_fatal ("spiff requires two file names.\n");
	*file1 = argv[1];
	*file2 = argv[2];
}