/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|   sel <selector>                                                             |
|                                                                              |
| This is a filter that excludes files based on the initial 2  bytes  of  each |
| line. A <selector> describes the initial two bytes, and consists of 3 chars: |
|   1.  The flag char in col 1 that marks this as a line for selection.        |
|   2.  A single digit that is the selection level.                            |
|   3.  '+' or '-', indicating whether to produce a blank line.                |
|                                                                              |
| This program is useful for dealing with source files for which you want some |
| sections  in  some  targets  and  not in others.  For instance, you might be |
| surrounding debug output with C preprocessor junk:                           |
|   #if DEBUG > 2                                                              |
|      printf("x=%d.",x);                                                      |
|   #endif                                                                     |
| With sel, this can be reduced to one line:                                   |
|   :3 printf("x=%d.",x);                                                      |
| This solves the problem with C compilers, in which you try to save space  by |
| using:                                                                       |
|      if (DEBUG>2) printf("x=%d.",x);                                         |
| If  compiled  with "#define DEBUG 1", most C compilers will omit the call on |
| printf(), but they will  still  generate  the  quoted  string  as  a  global |
| (unreferenced) constant, thus wasting a *lot* of space. The only solution to |
| this problem is to use the #  directives  (and  having  a  much  bigger  and |
| messier  source  file),  or to delete the lines entirely.  This is in effect |
| what can be done with sel.  In your makefile, you can put entries like:      |
|   .SUFFIXES:  .b .c .s .i .o                                                 |
|   .b.c:  ;sel :9 <$*.b >$@                                                   |
| and  use  foo.b  for  the  source.   When you decide that you want a version |
| without all the debugging stuff with :3 and higher  at  the  left,  you  can |
| change the makefile line to                                                  |
|   .b.c:  ;sel :2 <$*.b >$@                                                   |
| and do a "make clean" and a "make all".  Your "clean" entry will, of course, |
| delete all the .c files that are derived from .b files, and this  will  then |
| remake them with the :3 thru :9 lines blanked out.  By saying                |
|   .b.c:  ;sel :2- <$*.b >$@                                                  |
| you can get them deleted entirely.                                           |
|                                                                              |
| Some examples are in order:                                                  |
|                                                                              |
|   sel :2+                                                                    |
| This would look for lines with ':' in column 1; if column 2 is a digit  0-2, |
| the line will be included in the output, if it is a digit 3-9, the line will |
| be rewritten as just a newline (because of the '+').                         |
|                                                                              |
|   sel %3-                                                                    |
| This  would look for lines with '%' in column 1; if column 3 is a digit 0-3, |
| the line will be included in the output, if it is a digit 4-9, the line will |
| be omitted entirely (because of the '-').                                    |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "V.h"
#include "sys_limits.h"

char buf[BUFSIZ];
int  lines = 0;
char selchr = ':';
char sellvl = '9';
char seldel =  0 ;

main(ac,av)
	char**av;
{	int   a;
	char  c0, c1, c2;

/*	ac = Vinit(ac,av); */
	for (a=1; a<ac; a++) {
		if (c0 = av[a][0]) {
			selchr = av[a][0];
			if ((c1 = av[a][1]) && isdigit(c1)) {
				sellvl = c1;
				if (c2 = av[a][2]) {
					if (c2 == '+') seldel = 0; else
					if (c2 == '-') seldel = 1; else
					fprintf(stderr,"%s ### Bad char 3 in \"%s\"\n",av[0],av[a]);
				}
			} else
				fprintf(stderr,"%s ### Bad char 2 in \"%s\"\n",av[0],av[a]);
		}
	}
	while (Fgets(buf,BUFSIZ,stdin)) {
		if (lines++ == 0)
			adjdate(buf);
		if ((c0 = buf[0]) == selchr) {
			if ((c1 = buf[1]) && isdigit(c1) && (c1 <= sellvl)) {
				puts(buf+2);
			} else
			if (!seldel)
				putchar('\n');
		} else {
			puts(buf);
		}
	}
	exit(0);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This routine checks for the string:
*    %D";
* on the end of a line, and if it is found, replaces the %D with the current
* date.  Note that we always use UT here; some users might prefer that the
* local time zone be used, but UT makes things more comparable.  Also, note
* that we don't bother with the time of day.
*/
adjdate(buf)
	char*buf;
{	char*p;
	int  c;
	char *ctime();
	struct tm *tp;
	long   currtime;
	struct tm *gmtime();

	for (p = buf; c = *p; p++) {
		if ((c == '%') && (strcmp(p,"%D\";") == 0)) {
			currtime = time(0L);
			tp = gmtime(&currtime);
			sprintf(p,"%4d/%02d/%02d\";",
				tp->tm_year + 1900,
				tp->tm_mon  + 1,
				tp->tm_mday);
		}
	}
}
