xref: /vim-8.2.3635/runtime/doc/doctags.c (revision 68e6560b)
1 /* vim:set ts=4 sw=4:
2  *
3  * This program makes a tags file for help text.
4  *
5  * Usage: doctags *.txt ... >tags
6  *
7  * A tag in this context is an identifier between stars, e.g. *c_files*
8  */
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <stdlib.h>
14 
15 #define LINELEN 200
16 
17 	int
main(int argc,char ** argv)18 main(int argc, char **argv)
19 {
20 	char	line[LINELEN];
21 	char	*p1, *p2;
22 	char	*p;
23 	FILE	*fd;
24 
25 	if (argc <= 1)
26 	{
27 		fprintf(stderr, "Usage: doctags docfile ... >tags\n");
28 		exit(1);
29 	}
30 	printf("help-tags\ttags\t1\n");
31 	while (--argc > 0)
32 	{
33 		++argv;
34 		fd = fopen(argv[0], "r");
35 		if (fd == NULL)
36 		{
37 			fprintf(stderr, "Unable to open %s for reading\n", argv[0]);
38 			continue;
39 		}
40 		while (fgets(line, LINELEN, fd) != NULL)
41 		{
42 			p1 = strchr(line, '*');				/* find first '*' */
43 			while (p1 != NULL)
44 			{
45 				p2 = strchr(p1 + 1, '*');		/* find second '*' */
46 				if (p2 != NULL && p2 > p1 + 1)	/* skip "*" and "**" */
47 				{
48 					for (p = p1 + 1; p < p2; ++p)
49 						if (*p == ' ' || *p == '\t' || *p == '|')
50 							break;
51 					/*
52 					 * Only accept a *tag* when it consists of valid
53 					 * characters, there is white space before it and is
54 					 * followed by a white character or end-of-line.
55 					 */
56 					if (p == p2
57 							&& (p1 == line || p1[-1] == ' ' || p1[-1] == '\t')
58 								&& (strchr(" \t\n\r", p[1]) != NULL
59 									|| p[1] == '\0'))
60 					{
61 						*p2 = '\0';
62 						++p1;
63 						printf("%s\t%s\t/*", p1, argv[0]);
64 						while (*p1)
65 						{
66 							/* insert backslash before '\\' and '/' */
67 							if (*p1 == '\\' || *p1 == '/')
68 								putchar('\\');
69 							putchar(*p1);
70 							++p1;
71 						}
72 						printf("*\n");
73 						p2 = strchr(p2 + 1, '*');		/* find next '*' */
74 					}
75 				}
76 				p1 = p2;
77 			}
78 		}
79 		fclose(fd);
80 	}
81 	return 0;
82 }
83