dilshadmustafa / picat_genutils

General utilities for Picat programming language
Other
1 stars 1 forks source link

The result of `bp.shell` is an atom, not a string #4

Open hakank opened 2 years ago

hakank commented 2 years ago

The following is unexpected: the output value X is an atom, not a string.

Here's a simple query in the emu directory, testing if X is an atom or a string:

Picat> bp.shell(X,"ls *.c")           
X = 'arith.c\nassert.c\nbigint.c\nbuiltins.c\ncfd.c\nclause.c\nclpfd.c\nclpfd_libs.c\ncpreds.c\ndebug.c\ndelay.c\ndis.c\ndomain.c\nespresso_bp.c\nevent.c\nexpand.c\nfile.c\nfindall.c\nfloat1.c\ngcheap.c\ngcqueue.c\ngcstack.c\ngetline.c\nglobal.c\nglpk_bp.c\ninit.c\ninit_sym.c\ninst_inf.c\njmp_table.c\nkapi.c\nkissat_picat.c\nloader.c\nload_inst.c\nmain.c\nmic.c\nnumbervars.c\npicat_utilities.c\nplc_java.c\nqc.c\nsapi.c\nsat_bp.c\ntable.c\ntemp.c\ntoam.c\ntoamprofile.c\ntoken.c\nunify.c\nuniv.c\n'
yes

Picat> bp.shell(X,"ls *.c"), string(X)

no

Picat> bp.shell(X,"ls *.c"), atom(X)  
X = 'arith.c\nassert.c\nbigint.c\nbuiltins.c\ncfd.c\nclause.c\nclpfd.c\nclpfd_libs.c\ncpreds.c\ndebug.c\ndelay.c\ndis.c\ndomain.c\nespresso_bp.c\nevent.c\nexpand.c\nfile.c\nfindall.c\nfloat1.c\ngcheap.c\ngcqueue.c\ngcstack.c\ngetline.c\nglobal.c\nglpk_bp.c\ninit.c\ninit_sym.c\ninst_inf.c\njmp_table.c\nkapi.c\nkissat_picat.c\nloader.c\nload_inst.c\nmain.c\nmic.c\nnumbervars.c\npicat_utilities.c\nplc_java.c\nqc.c\nsapi.c\nsat_bp.c\ntable.c\ntemp.c\ntoam.c\ntoamprofile.c\ntoken.c\nunify.c\nuniv.c\n'
yes

I expected that it would be a string.

A fix (on the Picat level) is:

Picat> import utils
Picat>  bp.shell(X,"ls *.c"), atom_chars(X)=S,split(S)=L,Len=L.len
% ...
Len = 48
hakank commented 2 years ago

An consequence of this is that for larger outputs, Picat throws a buffer overflow error (atom too long):

Picat> bp.shell(X,"ls -latr")                                    
X = total 191Picat> bp.shell(X,"ls -latr")                                    
X = total 19132
-rwxrwxr-x 1 hakank hakank   20075 okt 12 18:03 Makefile.mingw64
-rwxrwxr-x 1 hakank hakank   20391 okt 12 18:03 Makefile.mac64

...

drwxrwxr-x 7 hakank hakank   12288 okt 14 08:06 .
-rwxrwxr-x 1 hakank hakank 4842688 okt 14 08:06 picat
drwxrwxr-x 7 hakank hakank    4096 okt 14 08:08 ..

Stack+Heap:    64,000,000 bytes
  Stack in use:     3,040 bytes
  Heap in use:     66,096 bytes

...

atom too long

When running as a program, the error is:

$ picat genutils_test1.pi
*** buffer overflow detected ***: picat_genutils terminated