Skip to content

Instantly share code, notes, and snippets.

View sebres's full-sized avatar
🛠️
WiP

Sergey G. Brester sebres

🛠️
WiP
View GitHub Profile
@sebres
sebres / embedeval.c
Created September 30, 2021 10:29
embedeval.c -- example of tcl-code embedding
/*
* embedeval.c -- example of tcl-code embedding
*
* Compile:
* mingw: gcc -O2 -DUSE_TCL_STUBS=1 -I$tcl/win -I$tcl/generic embedeval.c -shared -o embedeval.dll libtclstub86.a
* *nix: gcc -O2 -DUSE_TCL_STUBS=1 -I$tcl/unix -I$tcl/generic embedeval.c -shared -o embedeval.so libtclstub86.a
*
* Usage:
* $ tclsh86
* % load embedeval
@sebres
sebres / bot-bridge-nicks.lua
Last active June 1, 2021 13:19
bot-bridge-nicks.lua -- HexChat plug-in to wrap IRC bots to real user nicknames
hexchat.register("bot-bridge-nicks", "0.1", "Rewrites messages of bots to display bridge users as IRC users")
-- ijchain, ischain, liberachain are bridges:
botspattern = {"i.*chain", "^[<«](.-)[>»] (.*)$", "^<?(.-)>? (.*)$"}
botspattern2 = {"l.*chain", "^[<«](.-)[>»] (.*)$", "^%* (.-) (.*)$"}
local function fullname(user, nick)
return user:gsub("%s+", "-").."!"..user:gsub("%s+", "_").."@behind.the.bridge/"..nick
end
@sebres
sebres / cm.tcl
Created October 9, 2020 12:40
cm.tcl -- rich (data mining & science) console mode example (shorten, wrap or suppress an output return value in tcl)
proc cm args {
array set in {-out auto}
array set in $args
set scr ""
puts -nonewline "%cm% "; flush stdout
while {[gets stdin line] >= 0 && ($line ni {/cm /exit} || $scr ne "")} {
append scr $line "\n"
if {![info complete $scr]} {
puts -nonewline " ... "; flush stdout
continue
@sebres
sebres / poparse.tcl
Created October 7, 2020 11:19
poparse.tcl -- PO files (Portable Object) (GNU gettext format) parser
#**************************************************************************
# parse
# Internally used proc to parse PO files (Portable Object) (GNU gettext
# format).
# Parameters:
# f_ --
#**************************************************************************
proc parse { f_ } {
## TODO [SB]: file should be read partially, possible with other RE (non greedy ".*?" - could be slow by big files) :
set text_ [read $f_]
@sebres
sebres / lpop.tcl
Created May 11, 2020 10:53
lpop -- definition for lpop command (compatible to TIP 523 implementation)
if {[namespace which -command ::lpop] eq {}} {
proc ::lpop {listvar {index end} args} {
upvar $listvar l
if {![info exists l]} {
return -code error "can't read \"$listvar\": no such variable"
}
if {![llength $args]} {
if {$index eq "end"} {
if {![llength $l]} {
return -code error -errorcode {TCL VALUE INDEX OUTOFRANGE} "index \"$index\" out of range"
@sebres
sebres / test-concurrent-ipset.tcl
Last active April 3, 2020 12:30
test-concurrent-ipset.tcl -- test script concurrently flooding IPs in ipset(s)
#!/usr/bin/env tclsh
package require Thread
array set in {-max-threads 4 -single-set 0 -max-ips 255 -iterations 3 -debug 0}
array set in $::argv
set commoncode {
if {$in(-single-set)} {
proc thipset {n} {return "f2b-test"}
@sebres
sebres / threads-communication.tcl
Created March 3, 2020 13:17
threads-communication.tcl -- simplest example how workers can communicate with master
# task params for all threads
tsv::array set params {one 1 two 2}
# worker task:
set th_task {
if {[catch {
# do some work (here sum of params):
set res [expr {[tsv::get params one] + [tsv::get params two]}]
# provide result to master:
thread::send -async $master [list lappend th_res $res]
@sebres
sebres / collatz-conjecture-iter.py
Last active January 8, 2020 01:47
collatz-conjecture-iter.py -- simplest Collatz conjecture iterator in python
# collatz-conjecture-iter.py -- simplest Collatz conjecture iterator in python
# Author: Serg G. Brester aka sebres
import math
f = lambda x: 3*x+1 if x & 1 else x/2
def fcci(x):
"""single collatz conjecture iteration starting by given x
"""
@sebres
sebres / range.tcl
Created October 23, 2019 13:00
(experimental) range -- create a list generating a sequence of numbers or strings
if {[namespace which -command ::tcl::mathfunc::strincr] eq ""} {
proc ::tcl::mathfunc::strincr {v {increment}} {
string trimleft [binary format I* [expr {"0b[binary scan $v B* v; set v]" + $increment}]] \x00
}
}
proc range args {
set op "<"; set step 1
switch -exact -- [llength $args] \
1 { lassign $args to; set from [expr {$to*0}] } \
2 { lassign $args from to;
@sebres
sebres / ldeclare.tcl
Created October 17, 2019 22:13
ldeclare -- multi-line list declaration with simple substitution (and comments)
# -------------------------------------------------------------------------------
# ldeclare -- multi-line list declaration with simple substitution (and comments)
# -------------------------------------------------------------------------------
proc ldeclare l {
uplevel "list [string map [list \n "\\\n"] \
[regsub -all -line {^\s*\#[^\n]*} $l {}]
]"
}
# -------------------------------------------------------------------------------