9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] scripting sam
@ 2000-12-22 14:21 rob pike
  2001-01-02 17:24 ` cLIeNUX user
  0 siblings, 1 reply; 18+ messages in thread
From: rob pike @ 2000-12-22 14:21 UTC (permalink / raw)
  To: 9fans

Sam prints messages to stderr, so unlike the silly '-' argument to silence
ed, you can say
	sam -d foo >[2] /dev/null <<!
		...
	!

Here is an example of the combined power and idiocy of this approach.
The code is radically dependent on details of the HTML, which changes
about once a year, inviting me to dive back in to this enchanting world.


% cat /bin/weather
#!/bin/rc

rfork e

fn  sigint sighup sigterm{
	rm -f /tmp/w$pid
	exit 1
}

fn  sigexit{
	rm -f /tmp/w$pid
	exit ''
}

fn usage{
	echo usage:
	echo '	'weather known-name
	echo '		'known-names: '
			newark
			new york
			sf
			san fransisco
			la
			los angeles'
	echo '	'weather New_York NY
	echo '	'weather Toronto CN
	echo for a list of cities in new york, say
	echo '	' weather list NY
	exit
}

city = Newark
state = _NJ
country = US

switch($*){
case '?'
	usage
case newark local ''
	city = Newark; state = _NJ; country = US
case 'new york'
	city = New_York; state = _NY
case 'sf' 'san francisco'
	city = San_Francisco; state = _CA
case 'la' 'los angeles'
	city = Los_Angeles; state = _CA
case 'north pole'
	city = North_Pole_Nwt; state = ''; country = CN
case 'list '*
	city = list
	state = $2
case *
	if(! ~ $#* 2) usage
	city = $1
	state = $2
	switch($state){
	case AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA DC WV WI WY PR
		state = _$state
		country = US
	case *
		country = $state
		state = ''
	}
}


if(~ $city list){
	if (! hget http://weather.yahoo.com/regional/US/^$state^.html > /tmp/w$pid) exit $status
	grep 'href="forecast' /tmp/w$pid | sed -n 's/.*forecast.//
		s/_'$state'_.*$//
		s/.*/weather & '$state'/p'
	exit
}

if (! hget http://weather.yahoo.com/forecast/^$city^$state^_^$country^_f.html > /tmp/w$pid) exit $status

#cat /tmp/w$pid
sam -d /tmp/w$pid >[2] /dev/null <<'!'
,s/&#176;/°/g
,s/&nbsp;//g
/^<title>.*\n/ {
	y!</?title>!p
	1,.d
}
f MAIN
B LOCAL
b MAIN
/Local Forecast/,$m "LOCAL"
b MAIN
,x/<[aA] ([^>]|\n)+>/ .,/<\/[aA]>/d
,x/<([^>]|\n)+>/d
,x/>/d
,x/Yahoo!.+\n/d
/^F°.+\n/d
/^&#139;10/;/^100\+\n/d
0;0/^ *-.+\n/d
0/.*[0-9]+°/,$-/^(Sunset|Barometer).+\n/m0/^Today\n/
/^Today/,$-/(^Sunset|Barometer)/ x/\n\n+/c/\n/
/^Today/;.+3 s/\n/ /g
/^Current conditions/,/^Neighboring.+\n/d
,x/\n *(\n *)+/c/\n\n/
,x/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)$/ a/:/
0/^(Haze|Snow|Windy|Rain|Showers|Sunny|((Most|Part)ly *)?Cloudy)/m0/:$/
0/^(Haze|Snow|Windy|Rain|Showers|Sunny|((Most|Part)ly *)?Cloudy)/m0/:$/
0/^(Haze|Snow|Windy|Rain|Showers|Sunny|((Most|Part)ly *)?Cloudy)/m0/:$/
0/^(Haze|Snow|Windy|Rain|Showers|Sunny|((Most|Part)ly *)?Cloudy)/m0/:$/
,x/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat):.*/ a/:/
0/^Hi .*/m0/:$/
0/^Hi .*/m0/:$/
0/^Hi .*/m0/:$/
0/^Hi .*/m0/:$/
,x/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat):.*/ a/:/
0/^Lo .*/m0/:$/
0/^Lo .*/m0/:$/
0/^Lo .*/m0/:$/
0/^Lo .*/m0/:$/
/^Copyright.+\n/d
,x/ *-/d
,x/&gt;/d
,x/ *=/d
,x/\n *(\n *)+/c/\n\n/
0/\n+/d
,p
b LOCAL
,x/<[aA] [^>]+>/ .,/<\/[aA]>/d
,x/<([^>]|\n)+>/d
/^Copyright.+\n/d
,x/ *-/d
,x/ *=/d
,x/\n *(\n *)+/c/\n\n/
,p
!



^ permalink raw reply	[flat|nested] 18+ messages in thread
* Re: [9fans] scripting sam
@ 2001-01-03  0:34 Russ Cox
  0 siblings, 0 replies; 18+ messages in thread
From: Russ Cox @ 2001-01-03  0:34 UTC (permalink / raw)
  To: 9fans

eww.

don't you mean

____________________________________________________________________________
cLIeNUX sam/tp text processing language cheatsheet
cheatsheet markup...
(sam: more in /tmp/sam.err)

russ


^ permalink raw reply	[flat|nested] 18+ messages in thread
* Re: [9fans] scripting sam
@ 2000-12-23 19:34 anothy
  2001-01-02 17:35 ` cLIeNUX user
  0 siblings, 1 reply; 18+ messages in thread
From: anothy @ 2000-12-23 19:34 UTC (permalink / raw)
  To: 9fans

//plan 9 has been around for nearly 10 years.  all this
//'why can't i have my favourite toy on it?' mail is a
//bit much.

and i've been at least a casual user for almost half of
that. how is asking about sam "why can't i have my favorite
toy"? i was asking how to better use the tools Plan 9
provides. yeah, you can write telnet with echo and cat; how
exactly is that relavant to the issue at hand again? i'm
well aware of the power of the system... which is exactly
why i was asking how to use it, and the tools it provides,
better, rather than sticking with sed (which can do what i
want, but i'd need to use the hold spaces, which are ugly).

much thanks to everyone who responded helpfully.
-α.


^ permalink raw reply	[flat|nested] 18+ messages in thread
[parent not found: <200012222249.XAA04024@unix12.planete.net>]
* Re: [9fans] scripting sam
@ 2000-12-23  1:13 forsyth
  0 siblings, 0 replies; 18+ messages in thread
From: forsyth @ 2000-12-23  1:13 UTC (permalink / raw)
  To: 9fans

>>just remember:  charlie don't surf...

certainly not if i can help it.



^ permalink raw reply	[flat|nested] 18+ messages in thread
[parent not found: <200012222214.XAA00448@unix12.planete.net>]
[parent not found: <rsc@plan9.bell-labs.com>]
* [9fans] scripting sam
@ 2000-12-22  6:33 anothy
  2000-12-22 14:14 ` Mark C. Otto
  2000-12-22 22:07 ` Boyd Roberts
  0 siblings, 2 replies; 18+ messages in thread
From: anothy @ 2000-12-22  6:33 UTC (permalink / raw)
  To: 9fans

can the sam command line editor be
scripted, for automated batch
processing? i guess what i really
want is "ssam" (s^sam, as in s^ed),
but any pointers would be helpful.
-α.


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2001-01-03  0:34 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-22 14:21 [9fans] scripting sam rob pike
2001-01-02 17:24 ` cLIeNUX user
  -- strict thread matches above, loose matches on Subject: below --
2001-01-03  0:34 Russ Cox
2000-12-23 19:34 anothy
2001-01-02 17:35 ` cLIeNUX user
     [not found] <200012222249.XAA04024@unix12.planete.net>
2000-12-23  3:46 ` Boyd Roberts
2000-12-23  9:53   ` matt
2000-12-23 11:26     ` Boyd Roberts
2000-12-23 11:54       ` matt
2000-12-24  1:18         ` Boyd Roberts
2000-12-23  1:13 forsyth
     [not found] <200012222214.XAA00448@unix12.planete.net>
2000-12-22 22:34 ` kilgore
2000-12-22 22:48 ` James A. Robinson
     [not found] <rsc@plan9.bell-labs.com>
2000-12-22  6:37 ` Russ Cox
2000-12-23  2:51   ` Scott Schwartz
2000-12-22  6:33 anothy
2000-12-22 14:14 ` Mark C. Otto
2000-12-22 22:07 ` Boyd Roberts

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).