This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

dynamic pages..

Old posts that have not been replied to for several years.
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

dynamic pages..

Post by Ofloo »

i am rewriting a script of mine so it has dynamic webpage support..

can anyone tell me if there is a beter way of doing this ?

Code: Select all

proc evalCode {b} {
  set i 0
  while {$i < 1} {
    if {[regexp {(.*)\x3C\x3F\x20(.*)\x20\x3F\x3E(.*)} $b a b c d]} {
      if {[info exists return]} {
        set return "[eval $c]$d$return"
      } else {
      	set return "[eval $c]$d"
      } 
    } else {
      set return $b$return
      set i 1
    }
  }
  return $return
}
XplaiN but think of me as stupid
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

you applying for the anual obfuscated TCL code contest? ;)
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Quick and dirty way using 'string first' (could be improved by making it accept other kinds of whitespace after/before the "embedded code tags"
Executing the code in the scope of the proc is probably not what you want - I moved the execution to the global namespace using 'uplevel'
(I use a global variable for the result to be able to append data from within the embedded code...)

Code: Select all

proc evalML html {
	global return
	set return ""
	set 0 [set 1 [set 2 0]]
	while 1 {
		if {$1>[set 1 [string first "<? " $html $2]]} break
		if {$2>[set 2 [string first " ?>" $html $1]]} break
		append return [string range $html $0 [expr {$1-1}]]
		set code [string range $html [incr 1 3] [expr {$2-1}]]
		catch {uplevel #0 $code} res;
		append return $res
		set 0 [incr 2 3]
	}
	append return [string range $html $2 end]
}

proc echo what {append ::return $what; set what ""}

evalML {
<HTML>
<HEAD><TITLE><? return "My title" ?></TITLE></HEAD>
<BODY>
<? for {set i 0} {$i<10} {incr i} {echo "<P>$i</P>\n"} ?></BODY>
</HTML><? echo "<!-- the end. -->" ?>}
Returns
<HTML>
<HEAD><TITLE>My title</TITLE></HEAD>
<BODY>
<P>0</P>
<P>1</P>
<P>2</P>
<P>3</P>
<P>4</P>
<P>5</P>
<P>6</P>
<P>7</P>
<P>8</P>
<P>9</P>
</BODY>
</HTML><!-- the end. -->
Have you ever read "The Manual"?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

not exactly but i think i can modify it a bit would have to do some lookups but.. tnx for the answer it might give me some ideas tho..
% evalML "blah <? set return hmm ?>"
hmmhmm
% evalCode "blah <? set return hmm ?>"
blah hmm
%
it overwrites the .. variables & looses white spaces..
XplaiN but think of me as stupid
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

bah so does mine when it comes to i .. ok gone have to look for a different approach .. on that
XplaiN but think of me as stupid
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

The result you got was what you asked for. My version appends what ever the evaluation of the code block returns to the return value, so if you want to OVERWRITE the return variable directly like that, make sure you return an empty value at the end of the code. Take a look at my echo proc (or USE it?) :P

Keeping the whitespace around the code inside the code tag (if that's what you meant) makes no sense...if you want whitespace, add it from within the code or outside the tag.

Here's another proc that does the same thing but looks better (and it also works with different kinds of whitespace around the code)

Code: Select all

proc MLeval html {
	global buffer
	set buffer ""
	regsub -all {<\?\s(.*?)\s\?>} $html \0\\1\0 html
	foreach {html code} [split $html \0] {
		append buffer $html
		catch {uplevel #0 $code} result
		append buffer $result
	}
	set buffer
}

# helper procs for the embedded code
proc void args {}
proc echo what {append ::buffer $what; void}

# test
MLeval {
<HTML>
	<HEAD>
		<TITLE><? echo "huh?" ?></TITLE>
	</HEAD>
	<BODY>
		<? append buffer "<P>Don't modify the 'buffer' variable unless you understand what will happen.</P>"; void ?>
	</BODY>
</HTML>
}
Have you ever read "The Manual"?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

hmm i don't wana give a quick reply cause i don't understand it fully ill try to understand first ;) ill post when i got a question..

no i don't wana overwrite return it was just a quick test

* i wana read a page like php but then tcl but not overwrite the procs variable when evaluating them..
XplaiN but think of me as stupid
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Ofloo wrote:i wana read a page like php but then tcl but not overwrite the procs variable when evaluating them..
Something like this?

Your httpd recieves a request.
You extract the filename from the request uri to determine how to proceed.
If the file exists and is of a type where embedded tcl should be executed...
1) create environment to execute the code in (child namespace or interpreter) and environment variables you find useful (get/post variables, cookies etc.)
2) evaluate the embedded code in this new environment (appending output to output buffer) and output the end result to your client.
3) destroy environment
Have you ever read "The Manual"?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

i am trying to understand name space for a while thing i could understand from it was that it was like a script where you put crap in then if you need it you can call upon it and execute the code later on ..

the name space part is a bit where i am stuck on not sur how to use it ..
XplaiN but think of me as stupid
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

namespaces simply encapsulate names which otherwise would have been global, thus preventing global name space polution - nothing more, nothing less
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

that part i figur .. its like class in php .. i suppose .. kinda .. its like

proc <= name space instead ..

proc
}

}
XplaiN but think of me as stupid
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

i think i understand how to use echo and i like it besides the fact that you make use of global variables but then again it won't matter when you work with name spaces .. i have no clue what void is supposed to do .. what i understood from uplevel is that it is like a pointer in C .. could be wrong there tho..

i am currently looking for a good example of how to use name spaces .. i remember i had a website of wiki something for syntax in tcl but lost it while i installed bsd i axidently format everything :/ to use the whole drive was a and quit was q and having a azerty keyboard those 2 keys are switched so when i wanted to quit i was removing the hd .. anyway and i remember you wrote something like a word counter .. or something .. that i remember was with name spaces ..
XplaiN but think of me as stupid
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

I don't know if this is a good namespace example (surely not better than some professional stuff you'd find on wiki.tcl.tk and elsewhere - after all, I'm primarily C/C++ developer and not scripter), but my ircd bridge/emulator for eggdrop uses namespace and uplevel
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

hmm its nice script.. but all i can make up from that script is that name spaces won't allow to over write global variables from one to an other script .. like writing all global variables into an array.. then export only the array..
XplaiN but think of me as stupid
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

yep, that was my reasoning; since I had a lot of vars & procs with pretty common names that would otherwise clash with other scripts (even with eggdrop's build-in names) and I didn't feel like introducing a common prefix and delimiter, using namespace was the obvious alternative (albeit a simplistic use; namespaces can be nested and you can build namespace hierarchies)
Locked