tracert.awk

Last edit

Summary: extact basename for tracert/ enable colors

Changed:

< {{{

to

> {{{ awk


#::	C:\_\_u\tracert.awk
#:	2014-07-06  19:47:02	#::  rod.t_2014
#::	extract basename for tracert - pclip prints clipboard
#::  	usage:  pclip | awk64 -f C:\_\_u\tracert.awk
#::	for other cmds, add cmd, eg: 
#::		-v cmd=ping\040-a\040-n\0401\040
#::	for debug output:  -v _debug=1
#::	to force run:  -v run=1  (_debug disables run)
#::	to test:  -v _test=1
#::	functions: _dequote,  _testf,  _env,  _dt
##  ____________________________________________

BEGIN	{
	_nul =  "";	_q = "|";	_sysmul = 1;
	_awkfile = "C:/_/_u/tracert.awk";
	_awk = ARGV[0];
	if(_env("ComSpec"))  {
		system("norm.exe");
#::		restores window if minimized/hidden
		_cmd = "tracert ";	_con = "CON:";
		_awk = tolower(_awk);
		if((_awk !~ "gawk") &&	\
			(_awk !~ "mawk"))	\
			sysmul = 256;
		_dtcmd = "echo.%date%__%time%";
#::		eg:  2014-07-04__18:37:48.51
	}  else  {
		_cmd = "traceroute ";
		_con = "/dev/tty";
		_dtcmd = "date '+%Y-%m-%d__%H:";
		_dtcmd = _dtcmd "%M:%S.00'";
	}
	if(cmd)  _cmd = cmd;
	print("\n\t" _dt() "\t" _awk, "-f " _awkfile, \
		"\n\t_cmd = " (_q)(_cmd)_q,	\
		"\t_con = " (_q)(_con)_q);
	if(_test)  _testf();
}

1	{
	if(NR == 1)  print("  $0  =", (_q)($0)_q);
	else	    print("\n  $0  =", (_q)($0)_q);
	s = $0 = _dequote($0);
	print("  s   =", (_q)($0)_q);

	match(s, /([a-z0-9-]+[.])+[a-z0-9-]+/);
	s = substr(s, RSTART, RLENGTH);

	s = (_cmd)s;
	print("  cmd =", (_q)(s)_q);
	if((! _debug) || run)  {
		_exitcode = _sysmul * system(s);
	}
}

END	{
	t = "\n\tPress Enter key to continue:\t";
	printf("\n\t%s%s", _dt(), t) > _con;
	getline < _con;
	exit(0 + _exitcode);
}

function	_dequote(s,  n)  {
#::	\042 "  \043 #  \047 '
	s = tolower(s);	n = 1;
#::	if(_debug)  print("  s1 = ", (_q)(s)_q);
	while(n)  {
		n = 0;
#::		n += gsub(/(^[ \t]+)|([ \t]+$)/, "", s);
#::		n += sub(/^(\042|\047)/, "", s);
#::		n += sub(/(\042|\047)$/, "", s);
		n += gsub(/[\042\047]+/, "", s)
		n += gsub(/(^[ \t]+)|([ \t]+$)/, "", s);
		n += gsub(/[\t]+/, " ", s);
		n += gsub(/[ ][ ]+/, " ", s);

		n += sub(/^[\043]+[:]*/, "", s);
		if(_debug)  print("  s2 = ", (_q)(s)_q);
		n += gsub(/(^[ ])|([ ]$)/, "", s);

#::		n += sub(/^.*(:)+/, "", s);
#::		yekes - nojoy url with second :
#::		n += sub(/^[^:]*[:]+/, "", s);
#::		nojoy if a : in later
		n += sub(/^[^\/\\]*[:]+/, "", s);
#::		if(_debug)  print("  s5 = ", (_q)(s)_q);

#::		n += gsub(/(^[ \t]+)|([ \t]+$)/, "", s);
		n += gsub(/(^[ ])|([ ]$)/, "", s);
		n += sub(/^[\/\\]+/, "", s);
		if(_debug)  print("  s7 = ", (_q)(s)_q);

#::		n += gsub(/(^[ \t]+)|([ \t]+$)/, "", s);
		n += sub(/[\/\\].*$/, "", s);
#::		n += gsub(/(^[ \t]+)|([ \t]+$)/, "", s);
		n += gsub(/(^[ ])|([ ]$)/, "", s);

		n += gsub(/[.][.]+/, ".", s);
		n += gsub(/(^[.])|([.]$)/, "", s);
#::		if(_debug)  print("  s9 = ", (_q)(s)_q);
#::		beware:  /(anything)*/  is always true
#::		so sub(/(^[ \t]*)/, "") always true
		if(_debug < 2)  break;
	}
	return(s);
}

function	_testf(  cmd, s, t)  {
 ##:	htpp://.www.newegg.com./pr/pr.aspx?item=n82e
##:	tracert  	  ...10..177...0....34.. 	  
 ##:	  "site:.austin-group-bugs.net. '' 
 ##:	rodt@.icx.mtc.com.
 #::	.. 	 ftp:\\..some-thing.123-456.com..\...
#::         Yekes !! goes to 198.105.254.104
##:	".. ' '	 ftp:\\.jqy..jqy...jqy.0..\...:.. " '  	  
##: https://maps.google.com/m?l=3,-1&q=l:3,-1&h=e&
#::         \136 is ^

	s = _awk " -v s=\\136##: $0~s " _awkfile;
	s = s " | " _awk " -f " _awkfile;
	s = s  " -v cmd=_test():\\040 -v _debug=1";
	print("\t_testf():  s =\n" s);
	exit(_exitcode = sysmul * system(s));
}

function	_env(s,  i)  {
	s = tolower(s);
	for(i in ENVIRON)  if(tolower(i) == s)  \
		return(ENVIRON[i]);
	return("");
}

function	_dt(  s)  {
	_dtcmd | getline s;	close(_dtcmd);
	sub(/[.].*/, "", s);
	gsub(" ", "0", s);	gsub(/_/, " ", s);
	return(s);
}
##  ____________________________________________

#:  paradigm:  A,B.. arrays;  s..v strings;  i..n ints;  w..z reals
#:  lines with #:: are printed by  /\043::/{print($0);}
#:  data lines: -v s=\136##:[\040\t]* sub(s,n)==1
#:  regular expression metachars:  \ / ^ $ . [ ] | ( ) * + ?
#:  filenames may Not contain:  \  /  :  *  ?  "  <  >  |
#:  cmd.exe special:  &  "  ^  %  <  >  |  =  (  )  @  !
#:  sh special:  '  "  `  $  \
#:  \a\007  sp\040  !\041  "\042  #\043  $\044  %\045
#:  &\046  '\047  (\050  )\051  *\052  +\053  ,\054  -\055
#:  .\056  /\057  0\060  :\072  ;\073  <\074  =\075  >\076
#:  ?\077  @\100  A\101  [\133  \\134  ]\135  ^\136  _\137
#:  `\140  a\141  {\173  |\174  }\175  ~\176
#:	EOF