]> git.nbdom.net Git - nb.git/commitdiff
share/db/shell_function
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 7 Dec 2017 06:06:38 +0000 (06:06 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 7 Dec 2017 06:06:38 +0000 (06:06 +0000)
share/db/shell_function [new file with mode: 0755]

diff --git a/share/db/shell_function b/share/db/shell_function
new file mode 100755 (executable)
index 0000000..8d3e03a
--- /dev/null
@@ -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|;
+       }
+}