Re: XPCE process invocation and character encoding

Jan Wielemaker <[email protected]> Wed, 9 Jul 2014 20:14:12 +0200
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 07/09/2014 07:51 PM, [email protected] wrote:
> Thank you Jan,
> =

> I did as you suggested with the wrapper around swish-e and could see no
> problem. What goes in, comes out.
> =

> I am now trying on Windows (AMD Turion DualCore box with Windows Vista),
> and it does not even work:
> =

> 1 ?- shell('dir').
> false.

`dir` is not a Windows executable, but a command that can be executed
by cmd.exe.  The rest is mainly the same story.

	Cheers --- Jan

> =

> 2 ?- call_unix_right(dir, [], A).
> Correct to: "cea:call_unix_right(dir,[],A)"? yes
> [PCE: error: dir: Failed to execute: Unknown WINAPI error 2]
> false.
> =

> 3 ?- [PCE: error: dir: Process exit status -1]
> 3 ?- call_unix(dir, [], A).
> Correct to: "cea:call_unix(dir,[],A)"? yes
> [PCE: error: dir: Failed to execute: Unknown WINAPI error 2]
> false.
> =

> 4 ?- [PCE: error: dir: Process exit status -1]
> 4 ?- call_unix_temp(dir, [], A).
> Correct to: "cea:call_unix_temp(dir,[],A)"? yes
> ERROR: delete_file/1: No permission to delete file
> `c:/users/jacinto/appdata/local/temp/26' (Permission denied)
> =

> % from Jan Wielemaker
> call_unix_right(Utl, Args, Output) :-
>         NewTerm =3D.. [process, Utl | Args],
>         new(P, NewTerm),
>         send(P, use_tty, @off),
>         new(OS, string),
>         send(P, record_separator, @nil),
>         send(P, input_message, message(OS, append, @arg1)),
>         send(P, open),
>         send(P, wait),
>         get(OS, value, Output),
>         send(P, done),
>         send(OS, done).
> =

> call_unix(Utl, Args, Output) :-
> 	NewTerm =3D.. [process, Utl | Args],
> 	new(P, NewTerm),
> 	%send(P, environment, 'LANG', 'es_VE.UTF-8'),
> 	%send(P, environment, 'LANGUAGE', 'es_VE:es'),
> 	send(P, use_tty, @off),
> 	new(OS, string),
> 	send(P, record_separator, @nil),
> 	send(P, input_message, message(OS, append, @arg1)),
> 	send(P, open),
> 	send(P, wait),
> 	pce_string_to_list(OS, Output),
> 	send(P, done),
> 	send(OS, done)
> =

> call_unix_temp(Utl, Args, Output) :-
> 	tmp_file_stream(utf8, File, Stream),
> 	close(Stream),
> 	%File =3D 'temporal.tmp',
> 	%(exists_file(File) -> delete_file(File) ; true),
> 	append([Utl|Args], [' > ', File], Lista),
> 	concatena(Lista, Command),
>          shell(Command, _Status),
> 	new(F, file(File, utf8)),
> 	send(F, open, read),
> 	new(OS, string),
> 	repeat,
> 	(
> 	get(F, read_line, Line)
> 	-> send(OS, append, Line),
> 	fail
> 	;
> 	!
> 	),
> 	pce_string_to_list(OS, Output),
> 	delete_file(File).
> =

> pce_string_to_list(S, L) :-
> 	pce_string_to_list(S, 0, L).
> pce_string_to_list(S, I, [C|T]) :-
> 	get(S, character, I, C), !,
> 	NI is I + 1,
> 	pce_string_to_list(S, NI, T).
> pce_string_to_list(_, _, []).
> =

> Any further hints?
> =

> Best regards,
> =

> Jacinto
> =

> =

> Jan Wielemaker escribi=F3:
>> [please keep discussions on the list]
>> On 07/01/2014 02:30 PM, Jacinto D=C3=A1vila wrote:
>>> Thanks Jan. I am working in GNU/Linux in different Ubuntu boxes. But it
> didn't work on a Win64 box either.
>>> We are trying to provide users with a light app to call the search on
> her own machine. Web would be fine, except that it is messier to set up
> and unnecessary for standalone use.
>> There is indeed still some value in stand alone apps ...
>> But (on Ubuntu 13.04, locale is en_GB.UTF-8):
>> Slightly adapted call_unix (there is no pce_string_to_list/2):
>> call_unix(Utl, Args, Output) :-
>> 	NewTerm =3D.. [process, Utl | Args],
>> 	new(P, NewTerm),
>> 	send(P, use_tty, @off),
>> 	new(OS, string),
>> 	send(P, record_separator, @nil),
>> 	send(P, input_message, message(OS, append, @arg1)),
>> 	send(P, open),
>> 	send(P, wait),
>> 	get(OS, value, Output),
>> 	send(P, done),
>> 	send(OS, done).
>> Now,
>> 3 ?- call_unix(echo, [aap, 'Asociaci=C3=B3n'], L).
>> L =3D 'aap Asociaci=C3=B3n\n'.
>> So, it seems that UTF-8 characters are passed around nicely.  This is
> also suggested by your swish-e call, which says:
>>  >> # Search words: Asociaci=C3=B3n de Profesores del Instituto Pedag=C3=
=B3gico
> de
>>  >> Caracas UPEL
>> also in the case that goes wrong.  So, I think the problem is somewhere
> else.  Possibly in the environment?  I'd write a shell script around
> swish-e and dump arguments, environment, etc. from there.
>> 	Cheers --- Jan
>>> Regards,
>>> Jacinto
>>> On 06/28/2014 04:54 AM, j.wielemaker wrote:
>>>> Dunno.  Which OS?  Environment?  Might also be a bug.  I'd consider
> using process_create/3 and friends.
>>>> In fact, although xpce will be around for quite a while, it should be
> considered end-of-life.  I think that web-based is the future for
> interactive Prolog applications.
>>>>      Cheers --- [email protected] wrote:Dear All,
>>>> I am writing a XPCE based GUI for swish-e which I will release as free
> software, but I am having problems passing latin characters to a
> process
>>>> using this code (from Jan's XPCE userguide):
>>>> call_unix(Utl, Args, Output) :-
>>>> NewTerm =3D.. [process, Utl | Args],
>>>> new(P, NewTerm),
>>>> %send(P, environment, 'LANG', 'es_VE.UTF-8'),
>>>> %send(P, environment, 'LANGUAGE', 'es_VE:es'),
>>>> send(P, use_tty, @off),
>>>> new(OS, string),
>>>> send(P, record_separator, @nil),
>>>> send(P, input_message, message(OS, append, @arg1)),
>>>> send(P, open),
>>>> send(P, wait),
>>>> pce_string_to_list(OS, Output),
>>>> send(P, done),
>>>> send(OS, done).
>>>> The problem can be seen in the following traces of the tests:
>>>> test_swish1 :-
>>>>     shell('swish-e -w "Asociaci=C3=B3n de Profesores del Instituto
>>>> Pedag=C3=B3gico de
>>>> Caracas UPEL"', L).
>>>> which uses the shell directly, and
>>>> test_swish2 :-
>>>>     call_unix('swish-e', ['-w' ,'Asociaci=C3=B3n de Profesores del
>>>> Instituto
>>>> Pedag=C3=B3gico de Caracas UPEL'], Output), name(A, Output), writeln(A=
).
> the first gives the right answer:
>>>> ?- cea:test_swish1.
>>>> # SWISH format: 2.4.7
>>>> # Search words: Asociaci=C3=B3n de Profesores del Instituto Pedag=C3=
=B3gico de
> Caracas UPEL
>>>> # Removed stopwords:
>>>> # Number of hits: 11
>>>> # Search time: 0,073 seconds
>>>> # Run time: 0,092 seconds
>>>> 1000 ./pdfs/v8n25/articulo14.pdf "articulo14.pdf" 334273
>>>> 946 ./pdfs/v13n46/articulo28.pdf "articulo28.pdf" 734363
>>>> 920 ./pdfs/v11n38/articulo18.pdf "articulo18.pdf" 512974
>>>> 907 ./pdfs/v10n32/articulo21.pdf "articulo21.pdf" 373669
>>>> 901 ./pdfs/v5n13/articulo5-13-5.pdf "articulo5-13-5.pdf" 193489 898
> ./pdfs/v13n46/articulo10.pdf "articulo10.pdf" 1349930
>>>> 888 ./pdfs/v12n42/articulo16.pdf "articulo16.pdf" 1030090
>>>> 863 ./pdfs/v13n44/articulo17.pdf "articulo17.pdf" 415354
>>>> 799 ./pdfs/v13n46/articulo3.pdf "articulo3.pdf" 1036807
>>>> 739 ./pdfs/v13n44/articulo14.pdf "articulo14.pdf" 400571
>>>> 504 ./pdfs/v4n11/articulo4-11-14d.pdf "articulo4-11-14d.pdf" 27258 .
>>>> true.
>>>> but the second does not:
>>>> ?- cea:test_swish2.
>>>> # SWISH format: 2.4.7
>>>> # Search words: Asociaci=C3=B3n de Profesores del Instituto Pedag=C3=
=B3gico de
> Caracas UPEL
>>>> # Removed stopwords:
>>>> err: no results
>>>> .
>>>> true.
>>>> I am pretty sure the problem is that the process is not receiving
> those
>>>> "=C3=B3" with the proper encoding. But I don't see how to change that
> behaviour. As you can see in the comments in the first code, I tried
> setting the locale environment variables. I didn't work.
>>>> Any hint?
>>>> Best regards,
>>>> Jacinto
> =

> =