From: Nicolas Boisselier Date: Thu, 7 Dec 2017 06:06:38 +0000 (+0000) Subject: share/db/shell_function X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=156822b0700a750d9dce6df8a25405fb6e003521;p=nb.git share/db/shell_function --- diff --git a/share/db/shell_function b/share/db/shell_function new file mode 100755 index 00000000..8d3e03a6 --- /dev/null +++ b/share/db/shell_function @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +use strict; +use warnings; +$|=1; + +my $TABLE = 'shell_function'; +my $q = chr(39); + +print '' + ."SELECT 'Update shell_function';\n" + ."DROP TABLE IF EXISTS $TABLE;\n" + ."CREATE TABLE $TABLE (name VARCHAR(255) PRIMARY KEY,code BLOB);\n" + ."BEGIN TRANSACTION;\n" +; +END { print "COMMIT;\n"; } + +#@ARGV = ("bash --login -c 'for f in `shell_functions`; do type \$f || continue; done' |"); +@ARGV = ("bash -norc -c 'for i in $ENV{NB_ROOT}/etc/profile.d/*; do . \$i; done; for f in `shell_functions`; do type \$f || continue; done' |"); +$? and die; +my @fct = ( '', () ); +while ( <> ) { + + if (/^(\S+) is a function$/) { + sql(@fct); + @fct = ( $1, () ); + next; + } + + next unless $fct[0]; + push @{$fct[1]}, $_; + +} +sql(@fct); + +exit 0; + +sub sql { + #use Data::Dumper; print Dumper(\@_),"\n"; return; + return if !@_ or !$_[0]; + my $k = $_[0]; + my $v = join('',map { + s/$q/$q.$q/ge; $_ + } @{ $_[1] }); + print qq|INSERT INTO $TABLE VALUES ($q$k$q,$q$v$q);\n|; + return; + + while (@{ $_[1] }) { + my $k = shift @{ $_[1] }; + next unless $k; + + my $v = shift @{ $_[1] }; + $v =~ s/$q/$q.$q/ge; + + #print qq|INSERT INTO $t VALUES ($q$k$q,$q$v$q);\n|; + } +}