summaryrefslogtreecommitdiff
path: root/utils/cli/ledgersmb_cli.pl
blob: 995682b1aa8a681ddcaa0eb0b34174c4409af2b6 (plain)
  1. #!/usr/bin/perl
  2. #===========================================================================
  3. #
  4. # LedgerSMB Command-line script host
  5. #
  6. #
  7. # LedgerSMB
  8. # Small Medium Business Accounting software
  9. # http://www.ledgersmb.org/
  10. #
  11. #
  12. # Copyright (C) 2006
  13. # This work contains copyrighted information from a number of sources all used
  14. # with permission. It is released under the GNU General Public License
  15. # Version 2 or, at your option, any later version. See COPYRIGHT file for
  16. # details.
  17. #
  18. # This is a simple wrapper that allows you to write simple scripts with LSMB
  19. # See sample for the file format.
  20. #
  21. # THIS IS EXPERIMENTAL AND THE INTERFACE IS SUBJECT TO CHANGE
  22. use Parse::RecDescent;
  23. use LedgerSMB::User;
  24. use LedgerSMB::Form;
  25. use LedgerSMB::Sysconfig;
  26. $form = new Form;
  27. $syntax = << '_END_SYNTAX_';
  28. KEY : /\w[a-z0-9_]*/i
  29. FNKEY : /\w[a-z0-9_]*/i
  30. MODSTR: /\w[a-z0-9_:]*/i
  31. OP : m([-+*/%])
  32. NUMBER : /[+-]?\d*\.?\d+/
  33. ARGSTR : /\w[a-z0-9_,\s]*/i
  34. expression : NUMBER OP expression
  35. { return main::expression(@item) }
  36. | KEY OP expression
  37. { return main::expression(@item) }
  38. | NUMBER
  39. | KEY
  40. assign_instruction : KEY "=" expression
  41. { ${main::stackref}->{$item{key}} = $item{expression} }
  42. call_and_assign : /call/i FNKEY(ARGSTR) /into/i KEY
  43. { main::call_and_assign($item{FNKEY}, $item{ARGSTR}, $item{KEY}) }
  44. call : /call/i FNKEY(ARGSTR)
  45. { main::call($item{FNKEY}, $item{ARGSTR}) }
  46. for : /for/i KEY
  47. { main::push_loop($item{KEY}) }
  48. done : /^\s*done\s*$/
  49. { main::pop_loop() }
  50. if : /^\s*if/i KEY
  51. { main::if_handler($item{KEY} }
  52. # IF is terminated by END IF or FI on its own line
  53. login : /login/i
  54. { main::login() }
  55. module : /module/i MODSTR
  56. { main::load_mod($item{MODSTR} }
  57. instruction : assign_instruction
  58. | call_and_assign
  59. | call
  60. | for
  61. | done
  62. | if
  63. | login
  64. | module
  65. startrule : instruction
  66. _END_SYNTAX_
  67. $::RD_HINT = 1;
  68. $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
  69. $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.`
  70. my $stackref;
  71. my @loopstack;
  72. sub expression {
  73. shift;
  74. my ($lhs,$op,$rhs) = @_;
  75. $lhs = $VARIABLE{$lhs} if $lhs=~/[^-+0-9]/;
  76. return eval "$lhs $op $rhs";
  77. }
  78. sub call {
  79. my ($call, $argstr) = @_;
  80. $argstr =~ s/form/\\\%\$form/;
  81. $argstr =~ s/user/\\\%myconfig/;
  82. my @args = split /,\s/, $argstr;
  83. return $call(@args);
  84. }
  85. sub call_and_assign {
  86. my $key = pop;
  87. $stackref->{key} = call(@_);
  88. }
  89. sub push_loop {
  90. my $key = shift;
  91. push @loopstack, \$stackref->{$key};
  92. $stackref = \$loopstack[$#loopstack];
  93. }
  94. sub pop_loop {
  95. pop @loopstack;
  96. $stackref = \$loopstack[$#loopstack];
  97. }
  98. sub if_handler {
  99. my $key = shift;
  100. if (!$stackref->{$key}){
  101. while ($line !~ /^(\s*FI\s*|\s*END\s+IF\s*)$/ ){
  102. $line = <>;
  103. }
  104. }
  105. }
  106. sub login {
  107. $myconfig = new LedgerSMB::User
  108. "${LedgerSMB::Sysconfig::memberfile}", "$form->{login}";
  109. }
  110. sub load_mod {
  111. my $mod = shift;
  112. $mod =~ s/::/\//;
  113. eval { require "$mod.pm"; };
  114. }
  115. my $scriptparse = new Parse::RecDescent($syntax);
  116. while ($line = <>){
  117. $scriptparse->startrule($line);
  118. }
  119. delete $form->{password};
  120. for (keys %$form){
  121. print "$_ = $form->{$_}\n";
  122. }