mailing list of musl libc
 help / color / mirror / code / Atom feed
From: uwe@stderr.spb.ru (Valery Ushakov)
To: musl@lists.openwall.com
Subject: [musl] Re: Off topic question about shebang and exec()
Date: Fri, 3 Mar 2023 11:17:48 -0000 (UTC)	[thread overview]
Message-ID: <ttsl0r$14l3$1@ciao.gmane.io> (raw)
In-Reply-To: <8CC35F99-22CF-42A8-B326-987171DEDF06@adelielinux.org>

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


      reply	other threads:[~2023-03-03 11:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-02 14:15 [musl] " Paul Schutte
2023-03-02 16:32 ` Markus Wichmann
2023-03-02 17:52   ` A. Wilcox
2023-03-03 11:17     ` Valery Ushakov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='ttsl0r$14l3$1@ciao.gmane.io' \
    --to=uwe@stderr.spb.ru \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).