xref: /illumos-gate/usr/src/cmd/vi/misc/ctags.c (revision 45dce8f0)
17c478bd9Sstevel@tonic-gate /*
223a1cceaSRoger A. Faulkner  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate  */
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
67c478bd9Sstevel@tonic-gate /*	All Rights Reserved	*/
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
107c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
117c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate /*
157c478bd9Sstevel@tonic-gate  *   Modify ctags to handle C++ in C_entries(), etc:
167c478bd9Sstevel@tonic-gate  *	-  Handles C++ comment token "//"
177c478bd9Sstevel@tonic-gate  *	-  Handles C++ scope operator "::".
187c478bd9Sstevel@tonic-gate  *		This helps to distinguish between xyz()
197c478bd9Sstevel@tonic-gate  *	   definition and X::xyz() definition.
207c478bd9Sstevel@tonic-gate  *	-  Recognizes C++ reserved word "class" in typedef processing
217c478bd9Sstevel@tonic-gate  *		(for "-t" option)
227c478bd9Sstevel@tonic-gate  *	-  Handles Sun C++ special file name extensions: .c, .C, .cc, and .cxx.
237c478bd9Sstevel@tonic-gate  *	-  Handles overloaded unary/binary operator names
247c478bd9Sstevel@tonic-gate  *   Doesn't handle yet:
257c478bd9Sstevel@tonic-gate  *	-  inline functions in class definition (currently they get
267c478bd9Sstevel@tonic-gate  *		swallowed within a class definition)
277c478bd9Sstevel@tonic-gate  *	-  Tags with scope operator :: with spaces in between,
287c478bd9Sstevel@tonic-gate  *		e.g. classz ::afunc
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  *   Enhance operator functions support:
319b73ad49SToomas Soome  *	-  Control flow involving operator tokens scanning are
327c478bd9Sstevel@tonic-gate  *	   consistent with that of other function tokens - original
337c478bd9Sstevel@tonic-gate  *	   hacking method for 2.0 is removed.  This will accurately
347c478bd9Sstevel@tonic-gate  *	   identify tags for declarations of the form 'operator+()'
357c478bd9Sstevel@tonic-gate  *	   (bugid 1027806) as well as allowing spaces in between
367c478bd9Sstevel@tonic-gate  *	   'operator' and 'oprtk', e.g. 'operator + ()'.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifndef lint
417c478bd9Sstevel@tonic-gate char copyright[] = "@(#) Copyright (c) 1980 Regents of the University of "
427c478bd9Sstevel@tonic-gate 			"California.\nAll rights reserved.\n";
43f6db9f27Scf #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <locale.h>
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
51*45dce8f0SRichard Lowe #include <strings.h>
527c478bd9Sstevel@tonic-gate #include <limits.h>
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <sys/stat.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * ctags: create a tags file
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	bool	char
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	TRUE	(1)
637c478bd9Sstevel@tonic-gate #define	FALSE	(0)
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	CPFLAG	3			/* # of bytes in a flag		*/
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	iswhite(arg)	(_wht[arg])	/* T if char is white		*/
687c478bd9Sstevel@tonic-gate #define	begtoken(arg)	(_btk[arg])	/* T if char can start token	*/
697c478bd9Sstevel@tonic-gate #define	intoken(arg)	(_itk[arg])	/* T if char can be in token	*/
707c478bd9Sstevel@tonic-gate #define	endtoken(arg)	(_etk[arg])	/* T if char ends tokens	*/
717c478bd9Sstevel@tonic-gate #define	isgood(arg)	(_gd[arg])	/* T if char can be after ')'	*/
727c478bd9Sstevel@tonic-gate 
739b73ad49SToomas Soome #define	optoken(arg)	(_opr[arg])	/* T if char can be		*/
747c478bd9Sstevel@tonic-gate 					/* an overloaded operator token	*/
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	max(I1, I2)	(I1 > I2 ? I1 : I2)
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate struct	nd_st {			/* sorting structure			*/
797c478bd9Sstevel@tonic-gate 	char	*entry;			/* function or type name	*/
807c478bd9Sstevel@tonic-gate 	char	*file;			/* file name			*/
817c478bd9Sstevel@tonic-gate 	bool	f;			/* use pattern or line no	*/
827c478bd9Sstevel@tonic-gate 	int	lno;			/* for -x option		*/
837c478bd9Sstevel@tonic-gate 	char	*pat;			/* search pattern		*/
847c478bd9Sstevel@tonic-gate 	bool	been_warned;		/* set if noticed dup		*/
857c478bd9Sstevel@tonic-gate 	struct	nd_st	*left, *right;	/* left and right sons		*/
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate long	ftell();
897c478bd9Sstevel@tonic-gate typedef	struct	nd_st	NODE;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static bool
927c478bd9Sstevel@tonic-gate 	number,				/* T if on line starting with #	*/
937c478bd9Sstevel@tonic-gate 	gotone,				/* found a func already on line	*/
947c478bd9Sstevel@tonic-gate 					/* boolean "func" (see init)	*/
957c478bd9Sstevel@tonic-gate 	_wht[0177], _etk[0177], _itk[0177], _btk[0177], _gd[0177];
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /* boolean array for overloadable operator symbols			*/
987c478bd9Sstevel@tonic-gate static bool	_opr[0177];
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * typedefs are recognized using a simple finite automata,
1027c478bd9Sstevel@tonic-gate 	 * tydef is its state variable.
1037c478bd9Sstevel@tonic-gate 	 */
1047c478bd9Sstevel@tonic-gate typedef enum {none, begin, begin_rec, begin_tag, middle, end } TYST;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static TYST tydef = none;
1077c478bd9Sstevel@tonic-gate 
1089b73ad49SToomas Soome static char	searchar = '/';		/* use /.../ searches		*/
1097c478bd9Sstevel@tonic-gate 
110*45dce8f0SRichard Lowe #define	LINEBUFSIZ	4*BUFSIZ
111*45dce8f0SRichard Lowe 
1127c478bd9Sstevel@tonic-gate static int	lineno;			/* line number of current line */
1137c478bd9Sstevel@tonic-gate static char
114*45dce8f0SRichard Lowe 	line[LINEBUFSIZ],	/* current input line			*/
1157c478bd9Sstevel@tonic-gate 	*curfile,		/* current input file name		*/
1167c478bd9Sstevel@tonic-gate 	*outfile = "tags",	/* output file				*/
1177c478bd9Sstevel@tonic-gate 	*white	= " \f\t\n",	/* white chars				*/
1187c478bd9Sstevel@tonic-gate 	*endtk	= " \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?",
1197c478bd9Sstevel@tonic-gate 				/* token ending chars			*/
1207c478bd9Sstevel@tonic-gate 	*begtk	= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz",
1217c478bd9Sstevel@tonic-gate 				/* token starting chars			*/
1227c478bd9Sstevel@tonic-gate 	*intk	= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
1237c478bd9Sstevel@tonic-gate 		    "0123456789",
1247c478bd9Sstevel@tonic-gate 				/* valid in-token chars			*/
1257c478bd9Sstevel@tonic-gate 	*notgd	= ",;";		/* non-valid after-function chars	*/
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate static char	*oprtk	= " =-+%*/&|^~!<>[]()";	/* overloadable operators */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static int	file_num;	/* current file number			*/
1307c478bd9Sstevel@tonic-gate static int	aflag;		/* -a: append to tags */
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #ifndef XPG4			/* XPG4: handle typedefs by default	*/
1337c478bd9Sstevel@tonic-gate static int	tflag;		/* -t: create tags for typedefs		*/
1347c478bd9Sstevel@tonic-gate #endif /*  !XPG4 */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static int	uflag;		/* -u: update tags			*/
1377c478bd9Sstevel@tonic-gate static int	wflag;		/* -w: suppress warnings		*/
1387c478bd9Sstevel@tonic-gate static int	vflag;		/* -v: create vgrind style index output */
1397c478bd9Sstevel@tonic-gate static int	xflag;		/* -x: create cxref style output	*/
1407c478bd9Sstevel@tonic-gate 
141*45dce8f0SRichard Lowe static char	lbuf[LINEBUFSIZ];
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static FILE
1447c478bd9Sstevel@tonic-gate 	*inf,			/* ioptr for current input file		*/
1457c478bd9Sstevel@tonic-gate 	*outf;			/* ioptr for tags file			*/
1467c478bd9Sstevel@tonic-gate 
1479b73ad49SToomas Soome static long	lineftell;	/* ftell after getc( inf ) == '\n'	*/
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static NODE	*head;		/* the head of the sorted binary tree	*/
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static int	infile_fail;	/* Count of bad opens. Fix bug ID #1082298 */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static char	*dbp = lbuf;
1547c478bd9Sstevel@tonic-gate static int	pfcnt;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static int	mac;		/* our modified argc, after parseargs() */
1577c478bd9Sstevel@tonic-gate static char	**mav;		/* our modified argv, after parseargs() */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /* our local functions:							*/
161*45dce8f0SRichard Lowe static void	init(void);
162*45dce8f0SRichard Lowe static void	find_entries(char *);
163*45dce8f0SRichard Lowe static void	pfnote(char *, int, bool);
164*45dce8f0SRichard Lowe static void	C_entries(void);
165*45dce8f0SRichard Lowe static int	start_entry(char **, char *, int *);
166*45dce8f0SRichard Lowe static void	Y_entries(void);
167*45dce8f0SRichard Lowe static char	*toss_comment(char *);
168*45dce8f0SRichard Lowe static void	getaline(long int);
169*45dce8f0SRichard Lowe static void	free_tree(NODE *);
170*45dce8f0SRichard Lowe static void	add_node(NODE *, NODE *);
171*45dce8f0SRichard Lowe static void	put_entries(NODE *);
172*45dce8f0SRichard Lowe static int	PF_funcs(FILE *);
173*45dce8f0SRichard Lowe static int	tail(char *);
174*45dce8f0SRichard Lowe static void	takeprec(void);
175*45dce8f0SRichard Lowe static void	getit(void);
176*45dce8f0SRichard Lowe static char	*savestr(char *);
177*45dce8f0SRichard Lowe static void	L_funcs(FILE *);
178*45dce8f0SRichard Lowe static void	L_getit(int);
179*45dce8f0SRichard Lowe static int	striccmp(char *, char *);
180*45dce8f0SRichard Lowe static int	first_char(void);
181*45dce8f0SRichard Lowe static void	toss_yysec(void);
182*45dce8f0SRichard Lowe static void	Usage(void);
183*45dce8f0SRichard Lowe static void	parseargs(int, char **);
1847c478bd9Sstevel@tonic-gate 
185f6db9f27Scf int
main(int ac,char * av[])186f6db9f27Scf main(int ac, char *av[])
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	int i;
1897c478bd9Sstevel@tonic-gate 	char cmd[100];
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1927c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1937c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1947c478bd9Sstevel@tonic-gate #endif
1957c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	parseargs(ac, av);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	while ((i = getopt(mac, mav, "aBFtuvwxf:")) != EOF) {
2007c478bd9Sstevel@tonic-gate 		switch (i) {
2017c478bd9Sstevel@tonic-gate 		case 'a':	/* -a: Append output to existing tags file */
2027c478bd9Sstevel@tonic-gate 			aflag++;
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		case 'B':	/* -B: Use backward search patterns (?...?) */
2067c478bd9Sstevel@tonic-gate 			searchar = '?';
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		case 'F':	/* -F: Use forward search patterns (/.../) */
2107c478bd9Sstevel@tonic-gate 			searchar = '/';
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		case 't':	/* -t: Create tags for typedefs.	*/
2147c478bd9Sstevel@tonic-gate 				/* for XPG4 , we silently ignore "-t".	*/
2157c478bd9Sstevel@tonic-gate #ifndef XPG4
2167c478bd9Sstevel@tonic-gate 			tflag++;
2177c478bd9Sstevel@tonic-gate #endif /*  !XPG4 */
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		case 'u':	/* -u: Update the specified tags file	*/
2217c478bd9Sstevel@tonic-gate 			uflag++;
2227c478bd9Sstevel@tonic-gate 			break;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		case 'v':	/* -v: Index listing on stdout		*/
2257c478bd9Sstevel@tonic-gate 			vflag++;
2267c478bd9Sstevel@tonic-gate 			xflag++;
2277c478bd9Sstevel@tonic-gate 			break;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		case 'w':	/* -w: Suppress warnings		*/
2307c478bd9Sstevel@tonic-gate 			wflag++;
2317c478bd9Sstevel@tonic-gate 			break;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		case 'x':	/* -x: Produce a simple index		*/
2347c478bd9Sstevel@tonic-gate 			xflag++;
2357c478bd9Sstevel@tonic-gate 			break;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		case 'f':	/* -f tagsfile: output to tagsfile	*/
2387c478bd9Sstevel@tonic-gate 			outfile = strdup(optarg);
2397c478bd9Sstevel@tonic-gate 			break;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		default:
2427c478bd9Sstevel@tonic-gate 			Usage();	/* never returns		*/
2437c478bd9Sstevel@tonic-gate 			break;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/* if we didn't specify any source code to parse, complain and die. */
2487c478bd9Sstevel@tonic-gate 	if (optind == mac) {
2497c478bd9Sstevel@tonic-gate 		Usage();	/* never returns		*/
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	init();			/* set up boolean "functions"		*/
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * loop through files finding functions
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	for (file_num = optind; file_num < mac; file_num++)
2587c478bd9Sstevel@tonic-gate 		find_entries(mav[file_num]);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if (xflag) {
2617c478bd9Sstevel@tonic-gate 		put_entries(head);
2627c478bd9Sstevel@tonic-gate 		exit(infile_fail > 0 ? 2 : 0); /* Fix for 1082298 */
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	if (uflag) {
2657c478bd9Sstevel@tonic-gate 		for (i = 1; i < mac; i++) {
2667c478bd9Sstevel@tonic-gate 			(void) sprintf(cmd,
2677c478bd9Sstevel@tonic-gate 			"mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
2687c478bd9Sstevel@tonic-gate 				outfile, mav[i], outfile);
2697c478bd9Sstevel@tonic-gate 			(void) system(cmd);
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 		aflag++;
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	outf = fopen(outfile, aflag ? "a" : "w");
2747c478bd9Sstevel@tonic-gate 	if (outf == NULL) {
2757c478bd9Sstevel@tonic-gate 		perror(outfile);
2767c478bd9Sstevel@tonic-gate 		exit(1);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	put_entries(head);
2797c478bd9Sstevel@tonic-gate 	(void) fclose(outf);
2807c478bd9Sstevel@tonic-gate 	if (uflag) {
2817c478bd9Sstevel@tonic-gate 		(void) sprintf(cmd, "sort %s -o %s", outfile, outfile);
2827c478bd9Sstevel@tonic-gate 		(void) system(cmd);
2837c478bd9Sstevel@tonic-gate 	}
284f6db9f27Scf 	return (infile_fail > 0 ? 2 : 0); /* Fix for #1082298 */
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate  * This routine sets up the boolean psuedo-functions which work
2897c478bd9Sstevel@tonic-gate  * by seting boolean flags dependent upon the corresponding character
2907c478bd9Sstevel@tonic-gate  * Every char which is NOT in that string is not a white char.  Therefore,
2917c478bd9Sstevel@tonic-gate  * all of the array "_wht" is set to FALSE, and then the elements
2927c478bd9Sstevel@tonic-gate  * subscripted by the chars in "white" are set to TRUE.  Thus "_wht"
2937c478bd9Sstevel@tonic-gate  * of a char is TRUE if it is the string "white", else FALSE.
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate static void
init(void)296*45dce8f0SRichard Lowe init(void)
2977c478bd9Sstevel@tonic-gate {
298f6db9f27Scf 	char	*sp;
299f6db9f27Scf 	int	i;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	for (i = 0; i < 0177; i++) {
3027c478bd9Sstevel@tonic-gate 		_wht[i] = _etk[i] = _itk[i] = _btk[i] = FALSE;
3037c478bd9Sstevel@tonic-gate 		_opr[i] = FALSE;	/* initialize boolean		*/
3047c478bd9Sstevel@tonic-gate 					/* array of operator symbols	*/
3057c478bd9Sstevel@tonic-gate 		_gd[i] = TRUE;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 	for (sp = white; *sp; sp++)
3087c478bd9Sstevel@tonic-gate 		_wht[*sp] = TRUE;
3097c478bd9Sstevel@tonic-gate 	for (sp = endtk; *sp; sp++)
3107c478bd9Sstevel@tonic-gate 		_etk[*sp] = TRUE;
3117c478bd9Sstevel@tonic-gate 	for (sp = intk; *sp; sp++)
3127c478bd9Sstevel@tonic-gate 		_itk[*sp] = TRUE;
3137c478bd9Sstevel@tonic-gate 	for (sp = begtk; *sp; sp++)
3147c478bd9Sstevel@tonic-gate 		_btk[*sp] = TRUE;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/* mark overloadable operator symbols				*/
3177c478bd9Sstevel@tonic-gate 	for (sp = oprtk; *sp; sp++)
3187c478bd9Sstevel@tonic-gate 		_opr[*sp] = TRUE;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	for (sp = notgd; *sp; sp++)
3217c478bd9Sstevel@tonic-gate 		_gd[*sp] = FALSE;
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * This routine opens the specified file and calls the function
3267c478bd9Sstevel@tonic-gate  * which finds the function and type definitions.
3277c478bd9Sstevel@tonic-gate  */
3287c478bd9Sstevel@tonic-gate static void
find_entries(char * file)329*45dce8f0SRichard Lowe find_entries(char *file)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	char *cp;
3327c478bd9Sstevel@tonic-gate 	struct stat st;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/* skip anything that isn't a regular file */
3357c478bd9Sstevel@tonic-gate 	if (stat(file, &st) == 0 && !S_ISREG(st.st_mode))
3367c478bd9Sstevel@tonic-gate 		return;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if ((inf = fopen(file, "r")) == NULL) {
3397c478bd9Sstevel@tonic-gate 		perror(file);
3407c478bd9Sstevel@tonic-gate 		infile_fail++;		/* Count bad opens. ID #1082298 */
3417c478bd9Sstevel@tonic-gate 		return;
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	curfile = savestr(file);
3447c478bd9Sstevel@tonic-gate 	lineno = 0;
3457c478bd9Sstevel@tonic-gate 	cp = strrchr(file, '.');
3467c478bd9Sstevel@tonic-gate 	/* .l implies lisp or lex source code */
3477c478bd9Sstevel@tonic-gate 	if (cp && cp[1] == 'l' && cp[2] == '\0') {
3489b73ad49SToomas Soome 		if (strchr(";([", first_char()) != NULL)	/* lisp */
3497c478bd9Sstevel@tonic-gate 		{
3507c478bd9Sstevel@tonic-gate 			L_funcs(inf);
3517c478bd9Sstevel@tonic-gate 			(void) fclose(inf);
3527c478bd9Sstevel@tonic-gate 			return;
3537c478bd9Sstevel@tonic-gate 		} else {					/* lex */
3547c478bd9Sstevel@tonic-gate 			/*
3557c478bd9Sstevel@tonic-gate 			 * throw away all the code before the second "%%"
3567c478bd9Sstevel@tonic-gate 			 */
3577c478bd9Sstevel@tonic-gate 			toss_yysec();
35823a1cceaSRoger A. Faulkner 			getaline(lineftell);
3597c478bd9Sstevel@tonic-gate 			pfnote("yylex", lineno, TRUE);
3607c478bd9Sstevel@tonic-gate 			toss_yysec();
3617c478bd9Sstevel@tonic-gate 			C_entries();
3627c478bd9Sstevel@tonic-gate 			(void) fclose(inf);
3637c478bd9Sstevel@tonic-gate 			return;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	/* .y implies a yacc file */
3677c478bd9Sstevel@tonic-gate 	if (cp && cp[1] == 'y' && cp[2] == '\0') {
3687c478bd9Sstevel@tonic-gate 		toss_yysec();
3697c478bd9Sstevel@tonic-gate 		Y_entries();
3707c478bd9Sstevel@tonic-gate 		C_entries();
3717c478bd9Sstevel@tonic-gate 		(void) fclose(inf);
3727c478bd9Sstevel@tonic-gate 		return;
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/*
3767c478bd9Sstevel@tonic-gate 	 * Add in file name extension support for Sun C++ which
3777c478bd9Sstevel@tonic-gate 	 * permits .C/.c (AT&T), .cc (G++) and .cxx (Gloksp.)
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/* if not a .c, .C, .cc, .cxx or .h file, try fortran */
3817c478bd9Sstevel@tonic-gate 	if (cp && (cp[1] != 'C' && cp[1] != 'c' && cp[1] != 'h') &&
3827c478bd9Sstevel@tonic-gate 	    cp[2] == '\0' && (strcmp(cp, ".cc") == 0) &&
3837c478bd9Sstevel@tonic-gate 	    (strcmp(cp, ".cxx") == 0)) {
3847c478bd9Sstevel@tonic-gate 		if (PF_funcs(inf) != 0) {
3857c478bd9Sstevel@tonic-gate 			(void) fclose(inf);
3867c478bd9Sstevel@tonic-gate 			return;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 		rewind(inf);	/* no fortran tags found, try C */
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 	C_entries();
3917c478bd9Sstevel@tonic-gate 	(void) fclose(inf);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static void
pfnote(char * name,int ln,bool f)395*45dce8f0SRichard Lowe pfnote(char *name, int ln, bool f)
3967c478bd9Sstevel@tonic-gate {
397f6db9f27Scf 	char *fp;
398f6db9f27Scf 	NODE *np;
399f6db9f27Scf 	char *nametk;	/* hold temporary tokens from name */
4007c478bd9Sstevel@tonic-gate 	char nbuf[BUFSIZ];
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if ((np = malloc(sizeof (NODE))) == NULL) {
4037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4047c478bd9Sstevel@tonic-gate 				gettext("ctags: too many entries to sort\n"));
4057c478bd9Sstevel@tonic-gate 		put_entries(head);
4067c478bd9Sstevel@tonic-gate 		free_tree(head);
4077c478bd9Sstevel@tonic-gate 		head = np = (NODE *) malloc(sizeof (NODE));
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	if (xflag == 0 && (strcmp(name, "main") == 0)) {
4107c478bd9Sstevel@tonic-gate 		fp = strrchr(curfile, '/');
411*45dce8f0SRichard Lowe 
4127c478bd9Sstevel@tonic-gate 		if (fp == 0)
4137c478bd9Sstevel@tonic-gate 			fp = curfile;
4147c478bd9Sstevel@tonic-gate 		else
4157c478bd9Sstevel@tonic-gate 			fp++;
4167c478bd9Sstevel@tonic-gate 		(void) sprintf(nbuf, "M%s", fp);
4177c478bd9Sstevel@tonic-gate 		fp = strrchr(nbuf, '.');
4187c478bd9Sstevel@tonic-gate 		/* Chop off .cc and .cxx as well as .c, .h, etc		*/
4197c478bd9Sstevel@tonic-gate 		if (fp && ((fp[2] == 0) || (fp[2] == 'c' && fp[3] == 0) ||
4207c478bd9Sstevel@tonic-gate 			    (fp[3] == 'x' && fp[4] == 0)))
4217c478bd9Sstevel@tonic-gate 			*fp = 0;
4227c478bd9Sstevel@tonic-gate 		name = nbuf;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/* remove in-between blanks operator function tags */
4267c478bd9Sstevel@tonic-gate 	if (strchr(name, ' ') != NULL)
4277c478bd9Sstevel@tonic-gate 	{
4284088bb40Sraf 		(void) strcpy(name, strtok(name, " "));
429*45dce8f0SRichard Lowe 		while ((nametk = strtok(0, " ")) != NULL)
4307c478bd9Sstevel@tonic-gate 			(void) strcat(name, nametk);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 	np->entry = savestr(name);
4337c478bd9Sstevel@tonic-gate 	np->file = curfile;
4347c478bd9Sstevel@tonic-gate 	np->f = f;
4357c478bd9Sstevel@tonic-gate 	np->lno = ln;
4367c478bd9Sstevel@tonic-gate 	np->left = np->right = 0;
4377c478bd9Sstevel@tonic-gate 	if (xflag == 0) {
4387c478bd9Sstevel@tonic-gate 		lbuf[50] = 0;
4397c478bd9Sstevel@tonic-gate 		(void) strcat(lbuf, "$");
4407c478bd9Sstevel@tonic-gate 		lbuf[50] = 0;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	np->pat = savestr(lbuf);
4437c478bd9Sstevel@tonic-gate 	if (head == NULL)
4447c478bd9Sstevel@tonic-gate 		head = np;
4457c478bd9Sstevel@tonic-gate 	else
4467c478bd9Sstevel@tonic-gate 		add_node(np, head);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * This routine finds functions and typedefs in C syntax and adds them
4517c478bd9Sstevel@tonic-gate  * to the list.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate static void
C_entries(void)454*45dce8f0SRichard Lowe C_entries(void)
4557c478bd9Sstevel@tonic-gate {
456f6db9f27Scf 	int c;
457f6db9f27Scf 	char *token, *tp;
4587c478bd9Sstevel@tonic-gate 	bool incomm, inquote, inchar, midtoken, isoperator, optfound;
4597c478bd9Sstevel@tonic-gate 	int level;
4607c478bd9Sstevel@tonic-gate 	char *sp;
4617c478bd9Sstevel@tonic-gate 	char tok[BUFSIZ];
4627c478bd9Sstevel@tonic-gate 	long int tokftell;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	number = gotone = midtoken = inquote = inchar =
4657c478bd9Sstevel@tonic-gate 	incomm = isoperator = optfound = FALSE;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	level = 0;
4687c478bd9Sstevel@tonic-gate 	sp = tp = token = line;
4697c478bd9Sstevel@tonic-gate 	lineno++;
4707c478bd9Sstevel@tonic-gate 	lineftell = tokftell = ftell(inf);
4717c478bd9Sstevel@tonic-gate 	for (;;) {
4727c478bd9Sstevel@tonic-gate 		*sp = c = getc(inf);
4737c478bd9Sstevel@tonic-gate 		if (feof(inf))
4747c478bd9Sstevel@tonic-gate 			break;
4757c478bd9Sstevel@tonic-gate 		if (c == '\n') {
4767c478bd9Sstevel@tonic-gate 			lineftell = ftell(inf);
4777c478bd9Sstevel@tonic-gate 			lineno++;
4787c478bd9Sstevel@tonic-gate 		} else if (c == '\\') {
4797c478bd9Sstevel@tonic-gate 			c = *++sp = getc(inf);
4807c478bd9Sstevel@tonic-gate 			if ((c == '\n') || (c == EOF)) { /* c == EOF, 1091005 */
4817c478bd9Sstevel@tonic-gate 				lineftell = ftell(inf);
4827c478bd9Sstevel@tonic-gate 				lineno++;
4837c478bd9Sstevel@tonic-gate 				c = ' ';
4847c478bd9Sstevel@tonic-gate 			}
4857c478bd9Sstevel@tonic-gate 		} else if (incomm) {
4867c478bd9Sstevel@tonic-gate 			if (c == '*') {
4877c478bd9Sstevel@tonic-gate 				while ((*++sp = c = getc(inf)) == '*')
4887c478bd9Sstevel@tonic-gate 					continue;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 				/* c == EOF 1091005			*/
4917c478bd9Sstevel@tonic-gate 				if ((c == '\n') || (c == EOF)) {
4927c478bd9Sstevel@tonic-gate 					lineftell = ftell(inf);
4937c478bd9Sstevel@tonic-gate 					lineno++;
4947c478bd9Sstevel@tonic-gate 				}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 				if (c == '/')
4977c478bd9Sstevel@tonic-gate 					incomm = FALSE;
4987c478bd9Sstevel@tonic-gate 			}
4997c478bd9Sstevel@tonic-gate 		} else if (inquote) {
5007c478bd9Sstevel@tonic-gate 			/*
5017c478bd9Sstevel@tonic-gate 			 * Too dumb to know about \" not being magic, but
5027c478bd9Sstevel@tonic-gate 			 * they usually occur in pairs anyway.
5037c478bd9Sstevel@tonic-gate 			 */
5047c478bd9Sstevel@tonic-gate 			if (c == '"')
5057c478bd9Sstevel@tonic-gate 				inquote = FALSE;
5067c478bd9Sstevel@tonic-gate 			continue;
5077c478bd9Sstevel@tonic-gate 		} else if (inchar) {
5087c478bd9Sstevel@tonic-gate 			if (c == '\'')
5097c478bd9Sstevel@tonic-gate 				inchar = FALSE;
5107c478bd9Sstevel@tonic-gate 			continue;
5117c478bd9Sstevel@tonic-gate 		} else if (midtoken == TRUE) {	/* if white space omitted */
5127c478bd9Sstevel@tonic-gate 			goto dotoken;
5137c478bd9Sstevel@tonic-gate 		} else switch (c) {
5147c478bd9Sstevel@tonic-gate 		    case '"':
5157c478bd9Sstevel@tonic-gate 			inquote = TRUE;
5167c478bd9Sstevel@tonic-gate 			continue;
5177c478bd9Sstevel@tonic-gate 		    case '\'':
5187c478bd9Sstevel@tonic-gate 			inchar = TRUE;
5197c478bd9Sstevel@tonic-gate 			continue;
5207c478bd9Sstevel@tonic-gate 		    case '/':
5217c478bd9Sstevel@tonic-gate 			*++sp = c = getc(inf);
5229b73ad49SToomas Soome 			/* Handles the C++ comment token "//"		*/
5237c478bd9Sstevel@tonic-gate 			if (c == '*')
5247c478bd9Sstevel@tonic-gate 				incomm = TRUE;
5257c478bd9Sstevel@tonic-gate 			else if (c == '/') {
5267c478bd9Sstevel@tonic-gate 				/*
5277c478bd9Sstevel@tonic-gate 				 * Skip over all the characters after
5287c478bd9Sstevel@tonic-gate 				 * "//" until a newline character. Now also
5297c478bd9Sstevel@tonic-gate 				 * includes fix for 1091005, check for EOF.
5307c478bd9Sstevel@tonic-gate 				 */
5317c478bd9Sstevel@tonic-gate 				do  {
5327c478bd9Sstevel@tonic-gate 					c = getc(inf);
5337c478bd9Sstevel@tonic-gate 				/* 1091005:				*/
5347c478bd9Sstevel@tonic-gate 				} while ((c != '\n') && (c != EOF));
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 				/*
5387c478bd9Sstevel@tonic-gate 				 * Fixed bugid 1030014
5397c478bd9Sstevel@tonic-gate 				 * Return the current position of the
5407c478bd9Sstevel@tonic-gate 				 * file after the newline.
5417c478bd9Sstevel@tonic-gate 				 */
5427c478bd9Sstevel@tonic-gate 				lineftell = ftell(inf);
5437c478bd9Sstevel@tonic-gate 				lineno++;
5447c478bd9Sstevel@tonic-gate 				*--sp = c;
5457c478bd9Sstevel@tonic-gate 			}
5467c478bd9Sstevel@tonic-gate 			else
5477c478bd9Sstevel@tonic-gate 				(void) ungetc(*sp, inf);
5487c478bd9Sstevel@tonic-gate 			continue;
5497c478bd9Sstevel@tonic-gate 		    case '#':
5507c478bd9Sstevel@tonic-gate 			if (sp == line)
5517c478bd9Sstevel@tonic-gate 				number = TRUE;
5527c478bd9Sstevel@tonic-gate 			continue;
5537c478bd9Sstevel@tonic-gate 		    case '{':
5547c478bd9Sstevel@tonic-gate 			if ((tydef == begin_rec) || (tydef == begin_tag)) {
5557c478bd9Sstevel@tonic-gate 				tydef = middle;
5567c478bd9Sstevel@tonic-gate 			}
5577c478bd9Sstevel@tonic-gate 			level++;
5587c478bd9Sstevel@tonic-gate 			continue;
5597c478bd9Sstevel@tonic-gate 		    case '}':
5607c478bd9Sstevel@tonic-gate 			/*
5617c478bd9Sstevel@tonic-gate 			 * Heuristic for function or structure end;
5627c478bd9Sstevel@tonic-gate 			 * common for #ifdef/#else blocks to add extra "{"
5637c478bd9Sstevel@tonic-gate 			 */
5647c478bd9Sstevel@tonic-gate 			if (sp == line)
5657c478bd9Sstevel@tonic-gate 				level = 0;	/* reset */
5667c478bd9Sstevel@tonic-gate 			else
5677c478bd9Sstevel@tonic-gate 				level--;
5687c478bd9Sstevel@tonic-gate 			if (!level && tydef == middle) {
5697c478bd9Sstevel@tonic-gate 				tydef = end;
5707c478bd9Sstevel@tonic-gate 			}
5717c478bd9Sstevel@tonic-gate 			if (!level && tydef == none) /* Fix for #1034126 */
5727c478bd9Sstevel@tonic-gate 				goto dotoken;
5737c478bd9Sstevel@tonic-gate 			continue;
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate dotoken:
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		if (!level && !inquote && !incomm && gotone == FALSE) {
5807c478bd9Sstevel@tonic-gate 			if (midtoken) {
5817c478bd9Sstevel@tonic-gate 				if (endtoken(c)) {
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 				/*
5847c478bd9Sstevel@tonic-gate 				 *
5857c478bd9Sstevel@tonic-gate 				 *    ':'  +---> ':' -> midtok
5867c478bd9Sstevel@tonic-gate 				 *
5877c478bd9Sstevel@tonic-gate 				 *    +---> operator{+,-, etc} -> midtok
5887c478bd9Sstevel@tonic-gate 				 *		(continue)
5897c478bd9Sstevel@tonic-gate 				 *    +---> endtok
5907c478bd9Sstevel@tonic-gate 				 */
5917c478bd9Sstevel@tonic-gate 		/*
5927c478bd9Sstevel@tonic-gate 		 * Enhance operator function support and
5937c478bd9Sstevel@tonic-gate 		 *	fix bugid 1027806
5947c478bd9Sstevel@tonic-gate 		 *
5957c478bd9Sstevel@tonic-gate 		 *  For operator token, scanning will continue until
5967c478bd9Sstevel@tonic-gate 		 *  '(' is found.  Spaces between 'operater' and
5977c478bd9Sstevel@tonic-gate 		 *  'oprtk' are allowed (e.g. 'operator + ()'), but
5987c478bd9Sstevel@tonic-gate 		 *  will be removed when the actual entry for the tag
5997c478bd9Sstevel@tonic-gate 		 *  is made.
6007c478bd9Sstevel@tonic-gate 		 *  Note that functions of the form 'operator ()(int)'
6017c478bd9Sstevel@tonic-gate 		 *  will be recognized, but 'operator ()' will not,
6027c478bd9Sstevel@tonic-gate 		 *  even though this is legitimate in C.
6037c478bd9Sstevel@tonic-gate 		 */
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 					if (optoken(c)) {
6067c478bd9Sstevel@tonic-gate 					    if (isoperator) {
6077c478bd9Sstevel@tonic-gate 					    if (optfound) {
6087c478bd9Sstevel@tonic-gate 						    if (c != '(') {
6097c478bd9Sstevel@tonic-gate 						    tp++;
6107c478bd9Sstevel@tonic-gate 						    goto next_char;
6117c478bd9Sstevel@tonic-gate 						    }
6127c478bd9Sstevel@tonic-gate 					    } else {
6137c478bd9Sstevel@tonic-gate 						    if (c != ' ') {
6147c478bd9Sstevel@tonic-gate 						    optfound = TRUE;
6157c478bd9Sstevel@tonic-gate 						    }
6167c478bd9Sstevel@tonic-gate 						    tp++;
6177c478bd9Sstevel@tonic-gate 						    goto next_char;
6187c478bd9Sstevel@tonic-gate 					    }
6197c478bd9Sstevel@tonic-gate 					    } else {
6207c478bd9Sstevel@tonic-gate 				/* start: this code shifted left for cstyle */
6217c478bd9Sstevel@tonic-gate 				char *backptr = tp - 7;
6227c478bd9Sstevel@tonic-gate 				if (strncmp(backptr, "operator", 8) == 0) {
6237c478bd9Sstevel@tonic-gate 					/* This is an overloaded operator */
6247c478bd9Sstevel@tonic-gate 					isoperator = TRUE;
6257c478bd9Sstevel@tonic-gate 					if (c != ' ') {
6267c478bd9Sstevel@tonic-gate 						optfound = TRUE;
6277c478bd9Sstevel@tonic-gate 					}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 					tp++;
6307c478bd9Sstevel@tonic-gate 					goto next_char;
6317c478bd9Sstevel@tonic-gate 				} else if (c == '~') {
6327c478bd9Sstevel@tonic-gate 					/* This is a destructor		*/
6337c478bd9Sstevel@tonic-gate 					tp++;
6347c478bd9Sstevel@tonic-gate 					goto next_char;
6357c478bd9Sstevel@tonic-gate 				}
6367c478bd9Sstevel@tonic-gate 				/* end: above code shifted left for cstyle */
6377c478bd9Sstevel@tonic-gate 					}
6387c478bd9Sstevel@tonic-gate 					} else if (c == ':') {
6397c478bd9Sstevel@tonic-gate 					    if ((*++sp = getc(inf)) == ':') {
6407c478bd9Sstevel@tonic-gate 						tp += 2;
6417c478bd9Sstevel@tonic-gate 						c = *sp;
6427c478bd9Sstevel@tonic-gate 						goto next_char;
6437c478bd9Sstevel@tonic-gate 					    } else {
6447c478bd9Sstevel@tonic-gate 						(void) ungetc (*sp, inf);
6457c478bd9Sstevel@tonic-gate 						--sp;
6467c478bd9Sstevel@tonic-gate 					    }
6477c478bd9Sstevel@tonic-gate 					}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 				/* start: this code shifted left for cstyle */
6507c478bd9Sstevel@tonic-gate 				{
6517c478bd9Sstevel@tonic-gate 				int f;
6527c478bd9Sstevel@tonic-gate 				int pfline = lineno;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 				if (start_entry(&sp, token, &f)) {
6557c478bd9Sstevel@tonic-gate 					(void) strncpy(tok, token, tp-token+1);
6567c478bd9Sstevel@tonic-gate 					tok[tp-token+1] = 0;
65723a1cceaSRoger A. Faulkner 					getaline(tokftell);
6587c478bd9Sstevel@tonic-gate 					pfnote(tok, pfline, f);
6597c478bd9Sstevel@tonic-gate 					gotone = f;	/* function */
6607c478bd9Sstevel@tonic-gate 				}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 				isoperator = optfound = midtoken = FALSE;
6637c478bd9Sstevel@tonic-gate 				token = sp;
6647c478bd9Sstevel@tonic-gate 				}
6657c478bd9Sstevel@tonic-gate 				/* end: above code shifted left for cstyle */
6667c478bd9Sstevel@tonic-gate 				} else if (intoken(c))
6677c478bd9Sstevel@tonic-gate 					tp++;
6687c478bd9Sstevel@tonic-gate 			} else if (begtoken(c)) {
6697c478bd9Sstevel@tonic-gate 				token = tp = sp;
6707c478bd9Sstevel@tonic-gate 				midtoken = TRUE;
6717c478bd9Sstevel@tonic-gate 				tokftell = lineftell;
6727c478bd9Sstevel@tonic-gate 			}
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 	next_char:
6757c478bd9Sstevel@tonic-gate 		if (c == ';' && tydef == end)	/* clean with typedefs */
6767c478bd9Sstevel@tonic-gate 			tydef = none;
6777c478bd9Sstevel@tonic-gate 		sp++;
6787c478bd9Sstevel@tonic-gate 			/* The "c == }" was added to fix #1034126 */
6797c478bd9Sstevel@tonic-gate 		if (c == '\n' ||c == '}'|| sp > &line[sizeof (line) - BUFSIZ]) {
6807c478bd9Sstevel@tonic-gate 			tp = token = sp = line;
6817c478bd9Sstevel@tonic-gate 			number = gotone = midtoken = inquote =
6827c478bd9Sstevel@tonic-gate 			inchar = isoperator = optfound = FALSE;
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate /*
6887c478bd9Sstevel@tonic-gate  * This routine  checks to see if the current token is
6897c478bd9Sstevel@tonic-gate  * at the start of a function, or corresponds to a typedef
6907c478bd9Sstevel@tonic-gate  * It updates the input line * so that the '(' will be
6917c478bd9Sstevel@tonic-gate  * in it when it returns.
6927c478bd9Sstevel@tonic-gate  */
6937c478bd9Sstevel@tonic-gate static int
start_entry(char ** lp,char * token,int * f)694*45dce8f0SRichard Lowe start_entry(char **lp, char *token, int *f)
6957c478bd9Sstevel@tonic-gate {
696f6db9f27Scf 	char	*sp;
697f6db9f27Scf 	int	c;
6987c478bd9Sstevel@tonic-gate 	static	bool	found;
6997c478bd9Sstevel@tonic-gate 	bool	firsttok;	/* T if have seen first token in ()'s	*/
7007c478bd9Sstevel@tonic-gate 	int	bad;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	*f = 1;			/* a function */
7037c478bd9Sstevel@tonic-gate 	sp = *lp;
7047c478bd9Sstevel@tonic-gate 	c = *sp;
7057c478bd9Sstevel@tonic-gate 	bad = FALSE;
7067c478bd9Sstevel@tonic-gate 	if (!number) {		/* space is not allowed in macro defs	*/
7077c478bd9Sstevel@tonic-gate 		while (iswhite(c)) {
7087c478bd9Sstevel@tonic-gate 			*++sp = c = getc(inf);
7097c478bd9Sstevel@tonic-gate 			if ((c == '\n') || (c == EOF)) { /* c==EOF, #1091005 */
7107c478bd9Sstevel@tonic-gate 				lineno++;
7117c478bd9Sstevel@tonic-gate 				lineftell = ftell(inf);
7127c478bd9Sstevel@tonic-gate 				if (sp > &line[sizeof (line) - BUFSIZ])
7137c478bd9Sstevel@tonic-gate 					goto ret;
7147c478bd9Sstevel@tonic-gate 			}
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 	/* the following tries to make it so that a #define	a b(c)	*/
7177c478bd9Sstevel@tonic-gate 	/* doesn't count as a define of b.				*/
7187c478bd9Sstevel@tonic-gate 	} else {
7197c478bd9Sstevel@tonic-gate 		if (strncmp(token, "define", 6) == 0)
7207c478bd9Sstevel@tonic-gate 			found = 0;
7217c478bd9Sstevel@tonic-gate 		else
7227c478bd9Sstevel@tonic-gate 			found++;
7237c478bd9Sstevel@tonic-gate 		if (found >= 2) {
7247c478bd9Sstevel@tonic-gate 			gotone = TRUE;
7257c478bd9Sstevel@tonic-gate badone:			bad = TRUE;
7267c478bd9Sstevel@tonic-gate 			goto ret;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	/* check for the typedef cases		*/
7307c478bd9Sstevel@tonic-gate #ifdef XPG4
7317c478bd9Sstevel@tonic-gate 	if (strncmp(token, "typedef", 7) == 0) {
7327c478bd9Sstevel@tonic-gate #else /*  !XPG4 */
7337c478bd9Sstevel@tonic-gate 	if (tflag && (strncmp(token, "typedef", 7) == 0)) {
7347c478bd9Sstevel@tonic-gate #endif /*  XPG4 */
7357c478bd9Sstevel@tonic-gate 		tydef = begin;
7367c478bd9Sstevel@tonic-gate 		goto badone;
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	/* Handles 'class' besides 'struct' etc.			*/
7397c478bd9Sstevel@tonic-gate 	if (tydef == begin && ((strncmp(token, "struct", 6) == 0) ||
7407c478bd9Sstevel@tonic-gate 			    (strncmp(token, "class", 5) == 0) ||
7417c478bd9Sstevel@tonic-gate 			    (strncmp(token, "union", 5) == 0)||
7427c478bd9Sstevel@tonic-gate 			    (strncmp(token, "enum", 4) == 0))) {
7437c478bd9Sstevel@tonic-gate 		tydef = begin_rec;
7447c478bd9Sstevel@tonic-gate 		goto badone;
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	if (tydef == begin) {
7477c478bd9Sstevel@tonic-gate 		tydef = end;
7487c478bd9Sstevel@tonic-gate 		goto badone;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	if (tydef == begin_rec) {
7517c478bd9Sstevel@tonic-gate 		tydef = begin_tag;
7527c478bd9Sstevel@tonic-gate 		goto badone;
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 	if (tydef == begin_tag) {
7557c478bd9Sstevel@tonic-gate 		tydef = end;
7567c478bd9Sstevel@tonic-gate 		goto gottydef;	/* Fall through to "tydef==end" */
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate gottydef:
7607c478bd9Sstevel@tonic-gate 	if (tydef == end) {
7617c478bd9Sstevel@tonic-gate 		*f = 0;
7627c478bd9Sstevel@tonic-gate 		goto ret;
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	if (c != '(')
7657c478bd9Sstevel@tonic-gate 		goto badone;
7667c478bd9Sstevel@tonic-gate 	firsttok = FALSE;
7677c478bd9Sstevel@tonic-gate 	while ((*++sp = c = getc(inf)) != ')') {
7687c478bd9Sstevel@tonic-gate 		if ((c == '\n') || (c == EOF)) { /* c == EOF Fix for #1091005 */
7697c478bd9Sstevel@tonic-gate 			lineftell = ftell(inf);
7707c478bd9Sstevel@tonic-gate 			lineno++;
7717c478bd9Sstevel@tonic-gate 			if (sp > &line[sizeof (line) - BUFSIZ])
7727c478bd9Sstevel@tonic-gate 				goto ret;
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 		/*
7757c478bd9Sstevel@tonic-gate 		 * This line used to confuse ctags:
7767c478bd9Sstevel@tonic-gate 		 *	int	(*oldhup)();
7777c478bd9Sstevel@tonic-gate 		 * This fixes it. A nonwhite char before the first
7787c478bd9Sstevel@tonic-gate 		 * token, other than a / (in case of a comment in there)
7797c478bd9Sstevel@tonic-gate 		 * makes this not a declaration.
7807c478bd9Sstevel@tonic-gate 		 */
7817c478bd9Sstevel@tonic-gate 		if (begtoken(c) || c == '/')
7827c478bd9Sstevel@tonic-gate 			firsttok = TRUE;
7837c478bd9Sstevel@tonic-gate 		else if (!iswhite(c) && !firsttok)
7847c478bd9Sstevel@tonic-gate 			goto badone;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 	while (iswhite(*++sp = c = getc(inf)))
7877c478bd9Sstevel@tonic-gate 		if ((c == '\n') || (c == EOF)) { /* c == EOF fix for #1091005 */
7887c478bd9Sstevel@tonic-gate 			lineno++;
7897c478bd9Sstevel@tonic-gate 			lineftell = ftell(inf);
7907c478bd9Sstevel@tonic-gate 			if (sp > &line[sizeof (line) - BUFSIZ])
7917c478bd9Sstevel@tonic-gate 				break;
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate ret:
7947c478bd9Sstevel@tonic-gate 	*lp = --sp;
7957c478bd9Sstevel@tonic-gate 	if (c == '\n')
7967c478bd9Sstevel@tonic-gate 		lineno--;
7977c478bd9Sstevel@tonic-gate 	(void) ungetc(c, inf);
7987c478bd9Sstevel@tonic-gate 	return (!bad && (!*f || isgood(c)));
7997c478bd9Sstevel@tonic-gate 					/* hack for typedefs */
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate  * Y_entries:
8047c478bd9Sstevel@tonic-gate  *	Find the yacc tags and put them in.
8057c478bd9Sstevel@tonic-gate  */
8067c478bd9Sstevel@tonic-gate static void
807*45dce8f0SRichard Lowe Y_entries(void)
8087c478bd9Sstevel@tonic-gate {
809f6db9f27Scf 	char	*sp, *orig_sp;
810f6db9f27Scf 	int	brace;
811*45dce8f0SRichard Lowe 	bool	in_rule = FALSE;
812*45dce8f0SRichard Lowe 	size_t	toklen;
813*45dce8f0SRichard Lowe 	char	tok[LINEBUFSIZ];
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	brace = 0;
81623a1cceaSRoger A. Faulkner 	getaline(lineftell);
8177c478bd9Sstevel@tonic-gate 	pfnote("yyparse", lineno, TRUE);
818*45dce8f0SRichard Lowe 	while (fgets(line, sizeof (line), inf) != NULL) {
819*45dce8f0SRichard Lowe 		for (sp = line; *sp; sp++) {
8207c478bd9Sstevel@tonic-gate 			switch (*sp) {
821*45dce8f0SRichard Lowe 			case '\n':
8227c478bd9Sstevel@tonic-gate 				lineno++;
8237c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
824*45dce8f0SRichard Lowe 			case ' ':
825*45dce8f0SRichard Lowe 			case '\t':
826*45dce8f0SRichard Lowe 			case '\f':
827*45dce8f0SRichard Lowe 			case '\r':
8287c478bd9Sstevel@tonic-gate 				break;
829*45dce8f0SRichard Lowe 			case '"':
830*45dce8f0SRichard Lowe 			case '\'': {
831*45dce8f0SRichard Lowe 				char start = *sp;
832*45dce8f0SRichard Lowe 				sp++;
833*45dce8f0SRichard Lowe 
834*45dce8f0SRichard Lowe 				while ((*sp != '\0') && (*sp != start)) {
835*45dce8f0SRichard Lowe 					if (*sp == '\\')
836*45dce8f0SRichard Lowe 						sp++; /* Skip escaped thing */
837*45dce8f0SRichard Lowe 					sp++;
838*45dce8f0SRichard Lowe 				}
839*45dce8f0SRichard Lowe 
840*45dce8f0SRichard Lowe 				if (*sp == '\0')
841*45dce8f0SRichard Lowe 					sp--;
8427c478bd9Sstevel@tonic-gate 				break;
843*45dce8f0SRichard Lowe 			}
844*45dce8f0SRichard Lowe 			case '/':
8457c478bd9Sstevel@tonic-gate 				if (*++sp == '*')
8467c478bd9Sstevel@tonic-gate 					sp = toss_comment(sp);
8477c478bd9Sstevel@tonic-gate 				else
8487c478bd9Sstevel@tonic-gate 					--sp;
8497c478bd9Sstevel@tonic-gate 				break;
850*45dce8f0SRichard Lowe 			case '{':
8517c478bd9Sstevel@tonic-gate 				brace++;
8527c478bd9Sstevel@tonic-gate 				break;
853*45dce8f0SRichard Lowe 			case '}':
8547c478bd9Sstevel@tonic-gate 				brace--;
8557c478bd9Sstevel@tonic-gate 				break;
856*45dce8f0SRichard Lowe 			case '%':
8577c478bd9Sstevel@tonic-gate 				if (sp[1] == '%' && sp == line)
8587c478bd9Sstevel@tonic-gate 					return;
8597c478bd9Sstevel@tonic-gate 				break;
860*45dce8f0SRichard Lowe 			case '|':
861*45dce8f0SRichard Lowe 			case ';':
8627c478bd9Sstevel@tonic-gate 				in_rule = FALSE;
8637c478bd9Sstevel@tonic-gate 				break;
864*45dce8f0SRichard Lowe 			default:
8657c478bd9Sstevel@tonic-gate 				if (brace == 0 && !in_rule && (isalpha(*sp) ||
866*45dce8f0SRichard Lowe 				    *sp == '.' ||
867*45dce8f0SRichard Lowe 				    *sp == '_')) {
8687c478bd9Sstevel@tonic-gate 					orig_sp = sp;
8697c478bd9Sstevel@tonic-gate 					++sp;
8707c478bd9Sstevel@tonic-gate 					while (isalnum(*sp) || *sp == '_' ||
871*45dce8f0SRichard Lowe 					    *sp == '.')
8727c478bd9Sstevel@tonic-gate 						sp++;
8737c478bd9Sstevel@tonic-gate 					toklen = sp - orig_sp;
8747c478bd9Sstevel@tonic-gate 					while (isspace(*sp))
8757c478bd9Sstevel@tonic-gate 						sp++;
8767c478bd9Sstevel@tonic-gate 					if (*sp == ':' || (*sp == '\0' &&
877*45dce8f0SRichard Lowe 					    first_char() == ':')) {
8787c478bd9Sstevel@tonic-gate 						(void) strncpy(tok,
879*45dce8f0SRichard Lowe 						    orig_sp, toklen);
8807c478bd9Sstevel@tonic-gate 						tok[toklen] = '\0';
8817c478bd9Sstevel@tonic-gate 						(void) strcpy(lbuf, line);
8827c478bd9Sstevel@tonic-gate 						lbuf[strlen(lbuf) - 1] = '\0';
8837c478bd9Sstevel@tonic-gate 						pfnote(tok, lineno, TRUE);
8847c478bd9Sstevel@tonic-gate 						in_rule = TRUE;
885*45dce8f0SRichard Lowe 						/*
886*45dce8f0SRichard Lowe 						 * if we read NUL, leave it so
887*45dce8f0SRichard Lowe 						 * we read the next line
888*45dce8f0SRichard Lowe 						 */
889*45dce8f0SRichard Lowe 						if (*sp == '\0')
890*45dce8f0SRichard Lowe 							sp--;
891*45dce8f0SRichard Lowe 					} else {
8927c478bd9Sstevel@tonic-gate 						sp--;
893*45dce8f0SRichard Lowe 					}
8947c478bd9Sstevel@tonic-gate 				}
8957c478bd9Sstevel@tonic-gate 				break;
8967c478bd9Sstevel@tonic-gate 			}
897*45dce8f0SRichard Lowe 		}
898*45dce8f0SRichard Lowe 	}
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate static char *
902*45dce8f0SRichard Lowe toss_comment(char *start)
9037c478bd9Sstevel@tonic-gate {
904f6db9f27Scf 	char	*sp;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/*
9077c478bd9Sstevel@tonic-gate 	 * first, see if the end-of-comment is on the same line
9087c478bd9Sstevel@tonic-gate 	 */
9097c478bd9Sstevel@tonic-gate 	do {
9107c478bd9Sstevel@tonic-gate 		while ((sp = strchr(start, '*')) != NULL)
9117c478bd9Sstevel@tonic-gate 			if (sp[1] == '/')
9127c478bd9Sstevel@tonic-gate 				return (++sp);
9137c478bd9Sstevel@tonic-gate 			else
9147c478bd9Sstevel@tonic-gate 				start = (++sp);
9157c478bd9Sstevel@tonic-gate 		start = line;
9167c478bd9Sstevel@tonic-gate 		lineno++;
9177c478bd9Sstevel@tonic-gate 	} while (fgets(line, sizeof (line), inf) != NULL);
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/*
9207c478bd9Sstevel@tonic-gate 	 * running this through lint revealed that the original version
9217c478bd9Sstevel@tonic-gate 	 * of this routine didn't explicitly return something; while
9227c478bd9Sstevel@tonic-gate 	 * the return value was always used!. so i've added this
9237c478bd9Sstevel@tonic-gate 	 * next line.
9247c478bd9Sstevel@tonic-gate 	 */
9257c478bd9Sstevel@tonic-gate 	return (sp);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate static void
929*45dce8f0SRichard Lowe getaline(long int where)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	long saveftell = ftell(inf);
932f6db9f27Scf 	char *cp;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	(void) fseek(inf, where, 0);
9357c478bd9Sstevel@tonic-gate 	(void) fgets(lbuf, sizeof (lbuf), inf);
9367c478bd9Sstevel@tonic-gate 	cp = strrchr(lbuf, '\n');
9377c478bd9Sstevel@tonic-gate 	if (cp)
9387c478bd9Sstevel@tonic-gate 		*cp = 0;
9397c478bd9Sstevel@tonic-gate 	(void) fseek(inf, saveftell, 0);
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate static void
943*45dce8f0SRichard Lowe free_tree(NODE *node)
9447c478bd9Sstevel@tonic-gate {
945*45dce8f0SRichard Lowe 	NODE *next;
9467c478bd9Sstevel@tonic-gate 	while (node) {
9477c478bd9Sstevel@tonic-gate 		free_tree(node->right);
948*45dce8f0SRichard Lowe 		next = node->left;
9494088bb40Sraf 		free(node);
950*45dce8f0SRichard Lowe 		node = next;
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate static void
955*45dce8f0SRichard Lowe add_node(NODE *node, NODE *cur_node)
9567c478bd9Sstevel@tonic-gate {
957f6db9f27Scf 	int dif;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	dif = strcmp(node->entry, cur_node->entry);
9607c478bd9Sstevel@tonic-gate 	if (dif == 0) {
9617c478bd9Sstevel@tonic-gate 		if (node->file == cur_node->file) {
9627c478bd9Sstevel@tonic-gate 			if (!wflag) {
9637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9647c478bd9Sstevel@tonic-gate 			gettext("Duplicate entry in file %s, line %d: %s\n"),
9657c478bd9Sstevel@tonic-gate 			node->file, lineno, node->entry);
9667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9677c478bd9Sstevel@tonic-gate 					gettext("Second entry ignored\n"));
9687c478bd9Sstevel@tonic-gate 			}
9697c478bd9Sstevel@tonic-gate 			return;
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 		if (!cur_node->been_warned)
9727c478bd9Sstevel@tonic-gate 			if (!wflag) {
9737c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Duplicate "
9747c478bd9Sstevel@tonic-gate 					    "entry in files %s and %s: %s "
9757c478bd9Sstevel@tonic-gate 					    "(Warning only)\n"),
9767c478bd9Sstevel@tonic-gate 					    node->file, cur_node->file,
9777c478bd9Sstevel@tonic-gate 					    node->entry);
9787c478bd9Sstevel@tonic-gate 			}
9797c478bd9Sstevel@tonic-gate 		cur_node->been_warned = TRUE;
9807c478bd9Sstevel@tonic-gate 		return;
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	if (dif < 0) {
9847c478bd9Sstevel@tonic-gate 		if (cur_node->left != NULL)
9857c478bd9Sstevel@tonic-gate 			add_node(node, cur_node->left);
9867c478bd9Sstevel@tonic-gate 		else
9877c478bd9Sstevel@tonic-gate 			cur_node->left = node;
9887c478bd9Sstevel@tonic-gate 		return;
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 	if (cur_node->right != NULL)
9917c478bd9Sstevel@tonic-gate 		add_node(node, cur_node->right);
9927c478bd9Sstevel@tonic-gate 	else
9937c478bd9Sstevel@tonic-gate 		cur_node->right = node;
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate static void
997*45dce8f0SRichard Lowe put_entries(NODE *node)
9987c478bd9Sstevel@tonic-gate {
999f6db9f27Scf 	char	*sp;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (node == NULL)
10027c478bd9Sstevel@tonic-gate 		return;
10037c478bd9Sstevel@tonic-gate 	put_entries(node->left);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * while the code in the following #ifdef section could be combined,
10077c478bd9Sstevel@tonic-gate 	 * it's explicitly separated here to make maintainance easier.
10087c478bd9Sstevel@tonic-gate 	 */
10097c478bd9Sstevel@tonic-gate #ifdef XPG4
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * POSIX 2003: we no longer have a "-t" flag; the logic is
10127c478bd9Sstevel@tonic-gate 	 * automatically assumed to be "turned on" here.
10137c478bd9Sstevel@tonic-gate 	 */
10147c478bd9Sstevel@tonic-gate 	if (xflag == 0) {
10157c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "%s\t%s\t%c^",
10167c478bd9Sstevel@tonic-gate 				node->entry, node->file, searchar);
10177c478bd9Sstevel@tonic-gate 			for (sp = node->pat; *sp; sp++)
10187c478bd9Sstevel@tonic-gate 				if (*sp == '\\')
10197c478bd9Sstevel@tonic-gate 					(void) fprintf(outf, "\\\\");
10207c478bd9Sstevel@tonic-gate 				else if (*sp == searchar)
10217c478bd9Sstevel@tonic-gate 					(void) fprintf(outf, "\\%c", searchar);
10227c478bd9Sstevel@tonic-gate 				else
10237c478bd9Sstevel@tonic-gate 					(void) putc(*sp, outf);
10247c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "%c\n", searchar);
10257c478bd9Sstevel@tonic-gate 	} else if (vflag)
10267c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s %s %d\n",
10277c478bd9Sstevel@tonic-gate 				node->entry, node->file, (node->lno+63)/64);
10287c478bd9Sstevel@tonic-gate 	else
10297c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%-16s %4d %-16s %s\n",
10307c478bd9Sstevel@tonic-gate 			node->entry, node->lno, node->file, node->pat);
10317c478bd9Sstevel@tonic-gate #else /* XPG4 */
10327c478bd9Sstevel@tonic-gate 	/*
10337c478bd9Sstevel@tonic-gate 	 * original way of doing things. "-t" logic is only turned on
10347c478bd9Sstevel@tonic-gate 	 * when the user has specified it via a command-line argument.
10357c478bd9Sstevel@tonic-gate 	 */
10367c478bd9Sstevel@tonic-gate 	if (xflag == 0)
10377c478bd9Sstevel@tonic-gate 		if (node->f) {		/* a function */
10387c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "%s\t%s\t%c^",
10397c478bd9Sstevel@tonic-gate 				node->entry, node->file, searchar);
10407c478bd9Sstevel@tonic-gate 			for (sp = node->pat; *sp; sp++)
10417c478bd9Sstevel@tonic-gate 				if (*sp == '\\')
10427c478bd9Sstevel@tonic-gate 					(void) fprintf(outf, "\\\\");
10437c478bd9Sstevel@tonic-gate 				else if (*sp == searchar)
10447c478bd9Sstevel@tonic-gate 					(void) fprintf(outf, "\\%c", searchar);
10457c478bd9Sstevel@tonic-gate 				else
10467c478bd9Sstevel@tonic-gate 					(void) putc(*sp, outf);
10477c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "%c\n", searchar);
10487c478bd9Sstevel@tonic-gate 		} else {		/* a typedef; text pattern inadequate */
10497c478bd9Sstevel@tonic-gate 			(void) fprintf(outf, "%s\t%s\t%d\n",
10507c478bd9Sstevel@tonic-gate 				node->entry, node->file, node->lno);
10517c478bd9Sstevel@tonic-gate 		} else if (vflag)
10527c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s %s %d\n",
10537c478bd9Sstevel@tonic-gate 				node->entry, node->file, (node->lno+63)/64);
10547c478bd9Sstevel@tonic-gate 	else
10557c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%-16s %4d %-16s %s\n",
10567c478bd9Sstevel@tonic-gate 			node->entry, node->lno, node->file, node->pat);
10577c478bd9Sstevel@tonic-gate #endif /* XPG4 */
10587c478bd9Sstevel@tonic-gate 	put_entries(node->right);
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate static int
1063*45dce8f0SRichard Lowe PF_funcs(FILE *fi)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	pfcnt = 0;
10677c478bd9Sstevel@tonic-gate 	while (fgets(lbuf, sizeof (lbuf), fi)) {
10687c478bd9Sstevel@tonic-gate 		lineno++;
10697c478bd9Sstevel@tonic-gate 		dbp = lbuf;
10707c478bd9Sstevel@tonic-gate 		if (*dbp == '%') dbp++;	/* Ratfor escape to fortran */
10717c478bd9Sstevel@tonic-gate 		while (isspace(*dbp))
10727c478bd9Sstevel@tonic-gate 			dbp++;
10737c478bd9Sstevel@tonic-gate 		if (*dbp == 0)
10747c478bd9Sstevel@tonic-gate 			continue;
10757c478bd9Sstevel@tonic-gate 		switch (*dbp |' ') {
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 		    case 'i':
10787c478bd9Sstevel@tonic-gate 			if (tail("integer"))
10797c478bd9Sstevel@tonic-gate 				takeprec();
10807c478bd9Sstevel@tonic-gate 			break;
10817c478bd9Sstevel@tonic-gate 		    case 'r':
10827c478bd9Sstevel@tonic-gate 			if (tail("real"))
10837c478bd9Sstevel@tonic-gate 				takeprec();
10847c478bd9Sstevel@tonic-gate 			break;
10857c478bd9Sstevel@tonic-gate 		    case 'l':
10867c478bd9Sstevel@tonic-gate 			if (tail("logical"))
10877c478bd9Sstevel@tonic-gate 				takeprec();
10887c478bd9Sstevel@tonic-gate 			break;
10897c478bd9Sstevel@tonic-gate 		    case 'c':
10907c478bd9Sstevel@tonic-gate 			if (tail("complex") || tail("character"))
10917c478bd9Sstevel@tonic-gate 				takeprec();
10927c478bd9Sstevel@tonic-gate 			break;
10937c478bd9Sstevel@tonic-gate 		    case 'd':
10947c478bd9Sstevel@tonic-gate 			if (tail("double")) {
10957c478bd9Sstevel@tonic-gate 				while (isspace(*dbp))
10967c478bd9Sstevel@tonic-gate 					dbp++;
10977c478bd9Sstevel@tonic-gate 				if (*dbp == 0)
10987c478bd9Sstevel@tonic-gate 					continue;
10997c478bd9Sstevel@tonic-gate 				if (tail("precision"))
11007c478bd9Sstevel@tonic-gate 					break;
11017c478bd9Sstevel@tonic-gate 				continue;
11027c478bd9Sstevel@tonic-gate 			}
11037c478bd9Sstevel@tonic-gate 			break;
11047c478bd9Sstevel@tonic-gate 		}
11057c478bd9Sstevel@tonic-gate 		while (isspace(*dbp))
11067c478bd9Sstevel@tonic-gate 			dbp++;
11077c478bd9Sstevel@tonic-gate 		if (*dbp == 0)
11087c478bd9Sstevel@tonic-gate 			continue;
11097c478bd9Sstevel@tonic-gate 		switch (*dbp|' ') {
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 		    case 'f':
11127c478bd9Sstevel@tonic-gate 			if (tail("function"))
11137c478bd9Sstevel@tonic-gate 				getit();
11147c478bd9Sstevel@tonic-gate 			continue;
11157c478bd9Sstevel@tonic-gate 		    case 's':
11167c478bd9Sstevel@tonic-gate 			if (tail("subroutine"))
11177c478bd9Sstevel@tonic-gate 				getit();
11187c478bd9Sstevel@tonic-gate 			continue;
11197c478bd9Sstevel@tonic-gate 		    case 'p':
11207c478bd9Sstevel@tonic-gate 			if (tail("program")) {
11217c478bd9Sstevel@tonic-gate 				getit();
11227c478bd9Sstevel@tonic-gate 				continue;
11237c478bd9Sstevel@tonic-gate 			}
11247c478bd9Sstevel@tonic-gate 			if (tail("procedure"))
11257c478bd9Sstevel@tonic-gate 				getit();
11267c478bd9Sstevel@tonic-gate 			continue;
11277c478bd9Sstevel@tonic-gate 		}
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 	return (pfcnt);
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate static int
1133*45dce8f0SRichard Lowe tail(char *cp)
11347c478bd9Sstevel@tonic-gate {
1135f6db9f27Scf 	int len = 0;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	while (*cp && (*cp&~' ') == ((*(dbp+len))&~' '))
11387c478bd9Sstevel@tonic-gate 		cp++, len++;
11397c478bd9Sstevel@tonic-gate 	if (*cp == 0) {
11407c478bd9Sstevel@tonic-gate 		dbp += len;
11417c478bd9Sstevel@tonic-gate 		return (1);
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 	return (0);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate static void
1147*45dce8f0SRichard Lowe takeprec(void)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate 	while (isspace(*dbp))
11507c478bd9Sstevel@tonic-gate 		dbp++;
11517c478bd9Sstevel@tonic-gate 	if (*dbp != '*')
11527c478bd9Sstevel@tonic-gate 		return;
11537c478bd9Sstevel@tonic-gate 	dbp++;
11547c478bd9Sstevel@tonic-gate 	while (isspace(*dbp))
11557c478bd9Sstevel@tonic-gate 		dbp++;
11567c478bd9Sstevel@tonic-gate 	if (!isdigit(*dbp)) {
11577c478bd9Sstevel@tonic-gate 		--dbp;		/* force failure */
11587c478bd9Sstevel@tonic-gate 		return;
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 	do
11617c478bd9Sstevel@tonic-gate 		dbp++;
11627c478bd9Sstevel@tonic-gate 	while (isdigit(*dbp));
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate static void
1166*45dce8f0SRichard Lowe getit(void)
11677c478bd9Sstevel@tonic-gate {
1168f6db9f27Scf 	char *cp;
11697c478bd9Sstevel@tonic-gate 	char c;
1170*45dce8f0SRichard Lowe 	char nambuf[LINEBUFSIZ];
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	for (cp = lbuf; *cp; cp++)
11737c478bd9Sstevel@tonic-gate 		;
11747c478bd9Sstevel@tonic-gate 	*--cp = 0;	/* zap newline */
11757c478bd9Sstevel@tonic-gate 	while (isspace(*dbp))
11767c478bd9Sstevel@tonic-gate 		dbp++;
11777c478bd9Sstevel@tonic-gate 	if (*dbp == 0 || !isalpha(*dbp) || !isascii(*dbp))
11787c478bd9Sstevel@tonic-gate 		return;
11797c478bd9Sstevel@tonic-gate 	for (cp = dbp+1; *cp && (isalpha(*cp) || isdigit(*cp)); cp++)
11807c478bd9Sstevel@tonic-gate 		continue;
11817c478bd9Sstevel@tonic-gate 	c = cp[0];
11827c478bd9Sstevel@tonic-gate 	cp[0] = 0;
11837c478bd9Sstevel@tonic-gate 	(void) strcpy(nambuf, dbp);
11847c478bd9Sstevel@tonic-gate 	cp[0] = c;
11857c478bd9Sstevel@tonic-gate 	pfnote(nambuf, lineno, TRUE);
11867c478bd9Sstevel@tonic-gate 	pfcnt++;
11877c478bd9Sstevel@tonic-gate }
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate static char *
1190*45dce8f0SRichard Lowe savestr(char *cp)
11917c478bd9Sstevel@tonic-gate {
1192f6db9f27Scf 	int len;
1193f6db9f27Scf 	char *dp;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	len = strlen(cp);
11967c478bd9Sstevel@tonic-gate 	dp = (char *)malloc(len+1);
11977c478bd9Sstevel@tonic-gate 	(void) strcpy(dp, cp);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	return (dp);
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate /*
12037c478bd9Sstevel@tonic-gate  * lisp tag functions
12047c478bd9Sstevel@tonic-gate  * just look for (def or (DEF
12057c478bd9Sstevel@tonic-gate  */
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate static void
1208*45dce8f0SRichard Lowe L_funcs(FILE *fi)
12097c478bd9Sstevel@tonic-gate {
1210f6db9f27Scf 	int	special;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	pfcnt = 0;
12137c478bd9Sstevel@tonic-gate 	while (fgets(lbuf, sizeof (lbuf), fi)) {
12147c478bd9Sstevel@tonic-gate 		lineno++;
12157c478bd9Sstevel@tonic-gate 		dbp = lbuf;
12167c478bd9Sstevel@tonic-gate 		if (dbp[0] == '(' &&
12177c478bd9Sstevel@tonic-gate 		    (dbp[1] == 'D' || dbp[1] == 'd') &&
12187c478bd9Sstevel@tonic-gate 		    (dbp[2] == 'E' || dbp[2] == 'e') &&
12197c478bd9Sstevel@tonic-gate 		    (dbp[3] == 'F' || dbp[3] == 'f')) {
12207c478bd9Sstevel@tonic-gate 			dbp += 4;
12217c478bd9Sstevel@tonic-gate 			if (striccmp(dbp, "method") == 0 ||
12227c478bd9Sstevel@tonic-gate 			    striccmp(dbp, "wrapper") == 0 ||
12237c478bd9Sstevel@tonic-gate 			    striccmp(dbp, "whopper") == 0)
12247c478bd9Sstevel@tonic-gate 				special = TRUE;
12257c478bd9Sstevel@tonic-gate 			else
12267c478bd9Sstevel@tonic-gate 				special = FALSE;
12277c478bd9Sstevel@tonic-gate 			while (!isspace(*dbp))
12287c478bd9Sstevel@tonic-gate 				dbp++;
12297c478bd9Sstevel@tonic-gate 			while (isspace(*dbp))
12307c478bd9Sstevel@tonic-gate 				dbp++;
12317c478bd9Sstevel@tonic-gate 			L_getit(special);
12327c478bd9Sstevel@tonic-gate 		}
12337c478bd9Sstevel@tonic-gate 	}
12347c478bd9Sstevel@tonic-gate }
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate static void
1237*45dce8f0SRichard Lowe L_getit(int special)
12387c478bd9Sstevel@tonic-gate {
1239f6db9f27Scf 	char	*cp;
1240f6db9f27Scf 	char	c;
1241*45dce8f0SRichard Lowe 	char	nambuf[LINEBUFSIZ];
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	for (cp = lbuf; *cp; cp++)
12447c478bd9Sstevel@tonic-gate 		continue;
12457c478bd9Sstevel@tonic-gate 	*--cp = 0;		/* zap newline */
12467c478bd9Sstevel@tonic-gate 	if (*dbp == 0)
12477c478bd9Sstevel@tonic-gate 		return;
12487c478bd9Sstevel@tonic-gate 	if (special) {
12497c478bd9Sstevel@tonic-gate 		if ((cp = strchr(dbp, ')')) == NULL)
12507c478bd9Sstevel@tonic-gate 			return;
12517c478bd9Sstevel@tonic-gate 		while (cp >= dbp && *cp != ':')
12527c478bd9Sstevel@tonic-gate 			cp--;
12537c478bd9Sstevel@tonic-gate 		if (cp < dbp)
12547c478bd9Sstevel@tonic-gate 			return;
12557c478bd9Sstevel@tonic-gate 		dbp = cp;
12567c478bd9Sstevel@tonic-gate 		while (*cp && *cp != ')' && *cp != ' ')
12577c478bd9Sstevel@tonic-gate 			cp++;
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 	else
12607c478bd9Sstevel@tonic-gate 		for (cp = dbp + 1; *cp && *cp != '(' && *cp != ' '; cp++)
12617c478bd9Sstevel@tonic-gate 			continue;
12627c478bd9Sstevel@tonic-gate 	c = cp[0];
12637c478bd9Sstevel@tonic-gate 	cp[0] = 0;
12647c478bd9Sstevel@tonic-gate 	(void) strcpy(nambuf, dbp);
12657c478bd9Sstevel@tonic-gate 	cp[0] = c;
12667c478bd9Sstevel@tonic-gate 	pfnote(nambuf, lineno, TRUE);
12677c478bd9Sstevel@tonic-gate 	pfcnt++;
12687c478bd9Sstevel@tonic-gate }
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate /*
12717c478bd9Sstevel@tonic-gate  * striccmp:
12727c478bd9Sstevel@tonic-gate  *	Compare two strings over the length of the second, ignoring
12737c478bd9Sstevel@tonic-gate  *	case distinctions.  If they are the same, return 0.  If they
12747c478bd9Sstevel@tonic-gate  *	are different, return the difference of the first two different
12757c478bd9Sstevel@tonic-gate  *	characters.  It is assumed that the pattern (second string) is
12767c478bd9Sstevel@tonic-gate  *	completely lower case.
12777c478bd9Sstevel@tonic-gate  */
12787c478bd9Sstevel@tonic-gate static int
1279*45dce8f0SRichard Lowe striccmp(char *str, char *pat)
12807c478bd9Sstevel@tonic-gate {
1281f6db9f27Scf 	int	c1;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	while (*pat) {
12847c478bd9Sstevel@tonic-gate 		if (isupper(*str))
12857c478bd9Sstevel@tonic-gate 			c1 = tolower(*str);
12867c478bd9Sstevel@tonic-gate 		else
12877c478bd9Sstevel@tonic-gate 			c1 = *str;
12887c478bd9Sstevel@tonic-gate 		if (c1 != *pat)
12897c478bd9Sstevel@tonic-gate 			return (c1 - *pat);
12907c478bd9Sstevel@tonic-gate 		pat++;
12917c478bd9Sstevel@tonic-gate 		str++;
12927c478bd9Sstevel@tonic-gate 	}
12937c478bd9Sstevel@tonic-gate 	return (0);
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate /*
12977c478bd9Sstevel@tonic-gate  * first_char:
12987c478bd9Sstevel@tonic-gate  *	Return the first non-blank character in the file.  After
12997c478bd9Sstevel@tonic-gate  *	finding it, rewind the input file so we start at the beginning
13007c478bd9Sstevel@tonic-gate  *	again.
13017c478bd9Sstevel@tonic-gate  */
13027c478bd9Sstevel@tonic-gate static int
1303*45dce8f0SRichard Lowe first_char(void)
13047c478bd9Sstevel@tonic-gate {
1305f6db9f27Scf 	int	c;
1306f6db9f27Scf 	long	off;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	off = ftell(inf);
13097c478bd9Sstevel@tonic-gate 	while ((c = getc(inf)) != EOF)
13107c478bd9Sstevel@tonic-gate 		if (!isspace(c) && c != '\r') {
13117c478bd9Sstevel@tonic-gate 			(void) fseek(inf, off, 0);
13127c478bd9Sstevel@tonic-gate 			return (c);
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 	(void) fseek(inf, off, 0);
13157c478bd9Sstevel@tonic-gate 	return (EOF);
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate /*
13197c478bd9Sstevel@tonic-gate  * toss_yysec:
13207c478bd9Sstevel@tonic-gate  *	Toss away code until the next "%%" line.
13217c478bd9Sstevel@tonic-gate  */
13227c478bd9Sstevel@tonic-gate static void
1323*45dce8f0SRichard Lowe toss_yysec(void)
13247c478bd9Sstevel@tonic-gate {
13257c478bd9Sstevel@tonic-gate 	char		buf[BUFSIZ];
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	for (;;) {
13287c478bd9Sstevel@tonic-gate 		lineftell = ftell(inf);
13297c478bd9Sstevel@tonic-gate 		if (fgets(buf, BUFSIZ, inf) == NULL)
13307c478bd9Sstevel@tonic-gate 			return;
13317c478bd9Sstevel@tonic-gate 		lineno++;
13327c478bd9Sstevel@tonic-gate 		if (strncmp(buf, "%%", 2) == 0)
13337c478bd9Sstevel@tonic-gate 			return;
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate static void
1338*45dce8f0SRichard Lowe Usage(void)
13397c478bd9Sstevel@tonic-gate {
13407c478bd9Sstevel@tonic-gate #ifdef XPG4
13417c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage:\tctags [-aBFuvw] "
13427c478bd9Sstevel@tonic-gate #else /*  !XPG4 */
13437c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage:\tctags [-aBFtuvw] "
13447c478bd9Sstevel@tonic-gate #endif /*  XPG4 */
13457c478bd9Sstevel@tonic-gate 		    "[-f tagsfile] file ...\n"));
13467c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("OR:\tctags [-x] file ...\n"));
13477c478bd9Sstevel@tonic-gate 	exit(1);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate /*
13527c478bd9Sstevel@tonic-gate  * parseargs():		modify the args
13537c478bd9Sstevel@tonic-gate  *	the purpose of this routine is to transform any ancient argument
13547c478bd9Sstevel@tonic-gate  *	usage into a format which is acceptable to getopt(3C), so that we
13557c478bd9Sstevel@tonic-gate  *	retain backwards Solaris 2.[0-4] compatibility.
13567c478bd9Sstevel@tonic-gate  *
13577c478bd9Sstevel@tonic-gate  *	This routine allows us to make full use of getopts, without any
13587c478bd9Sstevel@tonic-gate  *	funny argument processing in main().
13597c478bd9Sstevel@tonic-gate  *
13607c478bd9Sstevel@tonic-gate  *	The other alternative would be to hand-craft the processed arguments
13617c478bd9Sstevel@tonic-gate  *	during and after getopt(3C) - which usually leads to uglier code
13627c478bd9Sstevel@tonic-gate  *	in main(). I've opted to keep the ugliness isolated down here,
13637c478bd9Sstevel@tonic-gate  *	instead of in main().
13647c478bd9Sstevel@tonic-gate  *
13657c478bd9Sstevel@tonic-gate  *	In a nutshell, if the user has used the old Solaris syntax of:
13667c478bd9Sstevel@tonic-gate  *		ctags [-aBFtuvwx] [-f tagsfile] filename ...
13677c478bd9Sstevel@tonic-gate  *	We simply change this into:
13687c478bd9Sstevel@tonic-gate  *		ctags [-a] [-B] [-F] [-t] [-u] [-v] [-w] [-x] [-f tags] file...
13697c478bd9Sstevel@tonic-gate  *
13707c478bd9Sstevel@tonic-gate  *	If the user has specified the new getopt(3C) syntax, we merely
13717c478bd9Sstevel@tonic-gate  *	copy that into our modified argument space.
13727c478bd9Sstevel@tonic-gate  */
13737c478bd9Sstevel@tonic-gate static void
1374*45dce8f0SRichard Lowe parseargs(int ac, char **av)
13757c478bd9Sstevel@tonic-gate {
13767c478bd9Sstevel@tonic-gate 	int i;			/* current argument			*/
13777c478bd9Sstevel@tonic-gate 	int a;			/* used to parse combined arguments	*/
13787c478bd9Sstevel@tonic-gate 	int fflag;		/* 1 = we're only parsing filenames	*/
13797c478bd9Sstevel@tonic-gate 	size_t sz;		/* size of the argument			*/
13807c478bd9Sstevel@tonic-gate 	size_t mav_sz;		/* size of our psuedo argument space	*/
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	i = mac = fflag = 0;	/* proper initializations */
13837c478bd9Sstevel@tonic-gate 
13844088bb40Sraf 	mav_sz = ((ac + 1) * sizeof (char *));
13854088bb40Sraf 	if ((mav = malloc(mav_sz)) == (char **)NULL) {
13867c478bd9Sstevel@tonic-gate 		perror("Can't malloc argument space");
13877c478bd9Sstevel@tonic-gate 		exit(1);
13887c478bd9Sstevel@tonic-gate 	}
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	/* for each argument, see if we need to change things:		*/
13919b73ad49SToomas Soome 	for (; (av[i] != NULL) && (av[i][0] != '\0'); i++) {
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 		if (strcmp(av[i], "--") == 0) {
13947c478bd9Sstevel@tonic-gate 			fflag = 1;	/* just handle filenames now	*/
13957c478bd9Sstevel@tonic-gate 		}
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 		sz = strlen(&av[i][0]);	/* get this arg's size		*/
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 		/*
14007c478bd9Sstevel@tonic-gate 		 * if the argument starts with a "-", and has more than
14017c478bd9Sstevel@tonic-gate 		 * 1 flag, then we have to search through each character,
14027c478bd9Sstevel@tonic-gate 		 * and separate any flags which have been combined.
14037c478bd9Sstevel@tonic-gate 		 *
14047c478bd9Sstevel@tonic-gate 		 * so, if we've found a "-" string which needs separating:
14057c478bd9Sstevel@tonic-gate 		 */
14069b73ad49SToomas Soome 		if (fflag == 0 &&	/* not handling filename args	*/
14077c478bd9Sstevel@tonic-gate 		    av[i][0] == '-' &&	/* and this is a flag		*/
14087c478bd9Sstevel@tonic-gate 		    sz > 2) {		/* and there's more than 1 flag	*/
14097c478bd9Sstevel@tonic-gate 			/* then for each flag after the "-" sign:	*/
14107c478bd9Sstevel@tonic-gate 			for (a = 1; av[i][a]; a++) {
14117c478bd9Sstevel@tonic-gate 				/* copy the flag into mav space.	*/
14127c478bd9Sstevel@tonic-gate 				if (a > 1) {
14137c478bd9Sstevel@tonic-gate 					/*
14147c478bd9Sstevel@tonic-gate 					 * we need to call realloc() after the
14157c478bd9Sstevel@tonic-gate 					 * 1st combined flag, because "ac"
14167c478bd9Sstevel@tonic-gate 					 * doesn't include combined args.
14177c478bd9Sstevel@tonic-gate 					 */
14187c478bd9Sstevel@tonic-gate 					mav_sz += sizeof (char *);
14197c478bd9Sstevel@tonic-gate 					if ((mav = realloc(mav, mav_sz)) ==
14204088bb40Sraf 					    (char **)NULL) {
14217c478bd9Sstevel@tonic-gate 						perror("Can't realloc "
14227c478bd9Sstevel@tonic-gate 							"argument space");
14237c478bd9Sstevel@tonic-gate 						exit(1);
14247c478bd9Sstevel@tonic-gate 					}
14257c478bd9Sstevel@tonic-gate 				}
14267c478bd9Sstevel@tonic-gate 
14274088bb40Sraf 				if ((mav[mac] = malloc((size_t)CPFLAG)) ==
14284088bb40Sraf 				    (char *)NULL) {
14297c478bd9Sstevel@tonic-gate 					perror("Can't malloc argument space");
14307c478bd9Sstevel@tonic-gate 					exit(1);
14317c478bd9Sstevel@tonic-gate 				}
14327c478bd9Sstevel@tonic-gate 				(void) sprintf(mav[mac], "-%c", av[i][a]);
14337c478bd9Sstevel@tonic-gate 				++mac;
14347c478bd9Sstevel@tonic-gate 			}
14357c478bd9Sstevel@tonic-gate 		} else {
14367c478bd9Sstevel@tonic-gate 			/* otherwise, just copy the argument:		*/
14374088bb40Sraf 			if ((mav[mac] = malloc(sz + 1)) == (char *)NULL) {
14387c478bd9Sstevel@tonic-gate 				perror("Can't malloc argument space");
14397c478bd9Sstevel@tonic-gate 				exit(1);
14407c478bd9Sstevel@tonic-gate 			}
14417c478bd9Sstevel@tonic-gate 			(void) strcpy(mav[mac], av[i]);
14427c478bd9Sstevel@tonic-gate 			++mac;
14437c478bd9Sstevel@tonic-gate 		}
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
14464088bb40Sraf 	mav[mac] = (char *)NULL;
14477c478bd9Sstevel@tonic-gate }
1448