mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Off topic question about shebang and exec()
@ 2023-03-02 14:15 Paul Schutte
  2023-03-02 16:32 ` Markus Wichmann
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Schutte @ 2023-03-02 14:15 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 621 bytes --]

Hi all

I apologize for abusing the knowledge of the people on this list, but I
know they will know the answer. Google does not provide a usable answer.

I am busy writing a toy language and I would like it to be used as both a
compiler and "interpreter"

I would like it to compile the source and then run the resulting binary
when the source file is called via the shebang and it should just do a
normal compile when called with "compile code.src"

argv[0] contains the path to the compiler in both cases, which makes sense.

Is there any way to determine which method was used to call the compiler?

Kind Regards
Paul

[-- Attachment #2: Type: text/html, Size: 818 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [musl] Off topic question about shebang and exec()
  2023-03-02 14:15 [musl] Off topic question about shebang and exec() Paul Schutte
@ 2023-03-02 16:32 ` Markus Wichmann
  2023-03-02 17:52   ` A. Wilcox
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Wichmann @ 2023-03-02 16:32 UTC (permalink / raw)
  To: musl

On Thu, Mar 02, 2023 at 04:15:38PM +0200, Paul Schutte wrote:
> Hi all
>
> I apologize for abusing the knowledge of the people on this list, but I
> know they will know the answer. Google does not provide a usable answer.
>
> I am busy writing a toy language and I would like it to be used as both a
> compiler and "interpreter"
>
> I would like it to compile the source and then run the resulting binary
> when the source file is called via the shebang and it should just do a
> normal compile when called with "compile code.src"
>
> argv[0] contains the path to the compiler in both cases, which makes sense.
>
> Is there any way to determine which method was used to call the compiler?
>
> Kind Regards
> Paul

Not to my knowledge. I would also consider it poor design to use a trick
like that. The normal assumption is that a shebang and just running the
command from command line are equivalent.

Normal solution here is to have a command line switch to select one
behavior or the other. That switch can be added to the shebang or the
command line, whatever you choose.

Ciao,
Markus

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [musl] Off topic question about shebang and exec()
  2023-03-02 16:32 ` Markus Wichmann
@ 2023-03-02 17:52   ` A. Wilcox
  2023-03-03 11:17     ` [musl] " Valery Ushakov
  0 siblings, 1 reply; 4+ messages in thread
From: A. Wilcox @ 2023-03-02 17:52 UTC (permalink / raw)
  To: musl

On Mar 2, 2023, at 10:32 AM, Markus Wichmann <nullplan@gmx.net> wrote:
> 
> On Thu, Mar 02, 2023 at 04:15:38PM +0200, Paul Schutte wrote:
>> Hi all
>> 
>> I apologize for abusing the knowledge of the people on this list, but I
>> know they will know the answer. Google does not provide a usable answer.
>> 
>> I am busy writing a toy language and I would like it to be used as both a
>> compiler and "interpreter"
>> 
>> I would like it to compile the source and then run the resulting binary
>> when the source file is called via the shebang and it should just do a
>> normal compile when called with "compile code.src"
>> 
>> argv[0] contains the path to the compiler in both cases, which makes sense.
>> 
>> Is there any way to determine which method was used to call the compiler?
>> 
>> Kind Regards
>> Paul
> 
> Not to my knowledge. I would also consider it poor design to use a trick
> like that. The normal assumption is that a shebang and just running the
> command from command line are equivalent.
> 
> Normal solution here is to have a command line switch to select one
> behavior or the other. That switch can be added to the shebang or the
> command line, whatever you choose.
> 
> Ciao,
> Markus

An even better solution is hinted in OP's problem description: argv[0].

Have two entry points, like a multi call binary, based on that.

You could use a softlink or hardlink named `lang` to `langc`.

Best,
-A.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [musl] Re: Off topic question about shebang and exec()
  2023-03-02 17:52   ` A. Wilcox
@ 2023-03-03 11:17     ` Valery Ushakov
  0 siblings, 0 replies; 4+ messages in thread
From: Valery Ushakov @ 2023-03-03 11:17 UTC (permalink / raw)
  To: musl

A. Wilcox <awilfox@adelielinux.org> wrote:

>> On Thu, Mar 02, 2023 at 04:15:38PM +0200, Paul Schutte wrote:
>>
>>> I apologize for abusing the knowledge of the people on this list, but I
>>> know they will know the answer. Google does not provide a usable answer.
>>> 
>>> I am busy writing a toy language and I would like it to be used as both a
>>> compiler and "interpreter"
>>> 
>>> I would like it to compile the source and then run the resulting binary
>>> when the source file is called via the shebang and it should just do a
>>> normal compile when called with "compile code.src"
>>> 
>>> argv[0] contains the path to the compiler in both cases, which makes sense.
>>> 
>>> Is there any way to determine which method was used to call the compiler?
>> 
>> Not to my knowledge. I would also consider it poor design to use a trick
>> like that. The normal assumption is that a shebang and just running the
>> command from command line are equivalent.
>> 
>> Normal solution here is to have a command line switch to select one
>> behavior or the other. That switch can be added to the shebang or the
>> command line, whatever you choose.
> 
> An even better solution is hinted in OP's problem description: argv[0].
> 
> Have two entry points, like a multi call binary, based on that.
> 
> You could use a softlink or hardlink named `lang` to `langc`.

argv[0] tricks are cute but, if employed, they cannot be the only way
for the binary to infer its "identity".  Because sooner or later you
will need to have a copy of (or a symlink to) the binary called
foo.old or something and the heuristic will break down.

What's the problem with separate binaries?  Off the top of my head I
can think of e.g. UCB Pascal (pi, px, pix) and Icon (icont, iconx,
icon).  Even cc(1) is kinda in this category too (except it doesn't
execute the result, but it arranges for cpp/cc1//as/ld stages to be
run in proper sequence).

Crunched binaries like NetBSD's /rescue or busybox are trying to save
every byte of space b/c they need to fit tight constraints, like e.g.
a memory disk.  But is there really a good reason to try to save those
few pages in the case of the compiler/interpreter (that are likely
dynamically linked to begin with, so "manually" sharing library bits
is not really a problem)?

-uwe


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-03-03 11:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-02 14:15 [musl] Off topic question about shebang and exec() Paul Schutte
2023-03-02 16:32 ` Markus Wichmann
2023-03-02 17:52   ` A. Wilcox
2023-03-03 11:17     ` [musl] " Valery Ushakov

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).