summaryrefslogtreecommitdiff
path: root/utils/cli/ledgersmb_cli.pl
blob: 4652c2d538eb90f31e9cccfa16bd432e3a902275 (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. | INTEGER
  39. | VARIABLE
  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. my $stackref;
  68. my @loopstack;
  69. sub call {
  70. my ($call, $argstr) = @_;
  71. $argstr =~ s/form/\\\%\$form/;
  72. $argstr =~ s/user/\\\%myconfig/;
  73. my @args = split /,\s/, $argstr;
  74. return $call(@args);
  75. }
  76. sub call_and_assign {
  77. my $key = pop;
  78. $stackref->{key} = call(@_);
  79. }
  80. sub push_loop {
  81. my $key = shift;
  82. push @loopstack, \$stackref->{$key};
  83. $stackref = \$loopstack[$#loopstack];
  84. }
  85. sub pop_loop {
  86. pop @loopstack;
  87. $stackref = \$loopstack[$#loopstack];
  88. }
  89. sub if_handler {
  90. my $key = shift;
  91. if (!$stackref->{$key}){
  92. while ($line !~ /^(\s*FI\s*|\s*END\s+IF\s*)$/ ){
  93. $line = <>;
  94. }
  95. }
  96. }
  97. sub login {
  98. $myconfig = new LedgerSMB::User
  99. "${LedgerSMB::Sysconfig::memberfile}", "$form->{login}";
  100. }
  101. sub load_mod {
  102. my $mod = shift;
  103. $mod =~ s/::/\//;
  104. eval { require "$mod.pm"; };
  105. }
  106. my $scriptparse = new Parse::RecDescent($grammer);
  107. while ($line = <>){
  108. $scriptparse->instruction($line);
  109. }
  110. delete $form->{password};
  111. for (keys %$form){
  112. print "$_ = $form->{$_}\n";
  113. }