News for hackers. Stuff that matters.
Re:Notes on the M4 Macro Language (2008) by dang on Thursday May 19, 2022 @07:40PM (#31441636)
Discussed at the time:

Notes on the M4 Macro Language (2008) - https://news.ycombinator.com/item?id=22757645 - April 2020 (59 comments)

Related:

M4 as a templating language (2020) - https://news.ycombinator.com/item?id=27994092 - July 2021 (112 comments)

M4 (computer language) - https://news.ycombinator.com/item?id=17781062 - Aug 2018 (26 comments)

Ask HN: Do you use M4 in production? - https://news.ycombinator.com/item?id=15870100 - Dec 2017 (1 comment)

M4BASIC – A BASIC to C converter written in M4 - https://news.ycombinator.com/item?id=10748131 - Dec 2015 (1 comment)

Re:Notes on the M4 Macro Language (2008) by enasterosophes on Thursday May 19, 2022 @06:30PM (#31441004)
I use m4 to generate my ssh config files. Otherwise managing the complexity of hundreds of servers over multiple subdomains all with different requirements for access and authentication would be prohibitive, especially given that openssh's config format only has a cursory understanding of how to define patterns with regex or cidr.
Re:Notes on the M4 Macro Language (2008) by harshreality on Friday May 20, 2022 @12:46AM (#31443453)
They have different design philosophies, but dhall[1] will generally do that sort of thing, and anyone who has dabbled with functional programming and hates baroque syntax common to many languages from the early days of computing may prefer dhall over something like m4.

[1] https://dhall-lang.org/

Re:Notes on the M4 Macro Language (2008) by hdjjhhvvhga on Friday May 20, 2022 @03:26AM (#31444349)
Of course you are right and hundreds of such tools exist. The only reason we are talking about m4 here is that is basically everywhere for legacy reasons.
Re:Notes on the M4 Macro Language (2008) by enasterosophes on Sunday May 22, 2022 @05:59PM (#31473223)
Correct. I was motivated to use m4 to solve the problem I had, because m4 is ubiquitous. I wanted to know more about it, and I wanted my config files to be generatable on any machine where I spend some time.
Re:Notes on the M4 Macro Language (2008) by perfecthjrjth on Thursday May 26, 2022 @11:30AM (#31519574)
Can you please share a sample m4 script to generate ssh config? Just curious how you have solved complicated stuff, patterns, etc.
Re:Notes on the M4 Macro Language (2008) by e40 on Thursday May 19, 2022 @07:39PM (#31441625)
I considered that just the other day. Ended up using cpp instead.
Re:Notes on the M4 Macro Language (2008) by imglorp on Thursday May 19, 2022 @08:01PM (#31441786)
Another simple option is envsubst which does one thing well.
Re:Notes on the M4 Macro Language (2008) by nick__m on Thursday May 19, 2022 @08:54PM (#31442144)
I used envsubst when I can but as far as I know you cannot do loop using envsubst. When you use the c preprocessor or php, m4, etc..., you can.
Re:Notes on the M4 Macro Language (2008) by imglorp on Friday May 20, 2022 @12:45PM (#31449842)
Nah, that's what I meant. It does $-expansion and that's it. If you need conditionals and loop you need templates and macros like others are saying: m4, cpp, handlebars, whatever.

I use it as a low-brain template engine to do only that.

Re:Notes on the M4 Macro Language (2008) by morelisp on Thursday May 19, 2022 @11:51PM (#31443156)
I use envsubst a lot, but without - and + forms it's hard to say it does it well.
Re:Notes on the M4 Macro Language (2008) by patrck on Thursday May 19, 2022 @11:41PM (#31443100)
Schools don't teach macro languages is probably the biggest problem.

Before YAML or Dockerfiles, it was the way to declare state and reduce complexity for sysadmins. Basically, any time you had data that needed multiple projections (eg. hosts to forward and reverse zonefiles), you reached for m4. The same went for unwieldy configs (apache, sendmail, etc.).

Culturally, macro languages seem to be just learned ad-hoc, ie. how many here have had a class in m4 or TeX?

Re:Notes on the M4 Macro Language (2008) by jamesfinlayson on Friday May 20, 2022 @01:51AM (#31443834)
I had a class that "strongly recommended" Latex - it was for formal proofs (CSP was one thing we learned - I can't remember the others now).
Re:Notes on the M4 Macro Language (2008) by yjftsjthsd-h on Friday May 20, 2022 @12:35AM (#31443387)
> how many here have had a class in m4 or TeX?

I haven't, but could be interested; are there any good tutorials or such that you might suggest?

Re:Notes on the M4 Macro Language (2008) by patrck on Friday May 20, 2022 @02:36AM (#31444053)
Gnu M4 manual works well, while for TeX there's the Gentle Introduction.

For an abstract, design/implementation introduction, though? There isn't. We don't learn about macro languages just as we don't learn about domain specific languages.

Re:Notes on the M4 Macro Language (2008) by jjice on Thursday May 19, 2022 @08:18PM (#31441901)
Wanted a template script recently. I thought for a bit about it and realized that PHP is pretty perfect for the job. Worked well for simple dynamic templates, and no dependencies outside of PHP.
Re:Notes on the M4 Macro Language (2008) by jamesfinlayson on Thursday May 19, 2022 @10:15PM (#31442601)
I remember a sysadmin doing the same thing at a previous job (I think he used it for generating PXE boot scripts or something like that) - as you say, great for simple dynamic templates and PHP was the only dependency.
Re:Notes on the M4 Macro Language (2008) by kreetx on Friday May 20, 2022 @01:31AM (#31443724)
Did a bigger school project in m4, learnt it for the task, only later realizing that PHP which I already knew would have been much better.
Re:Notes on the M4 Macro Language (2008) by dmux on Thursday May 19, 2022 @08:01PM (#31441792)
Is anyone aware of other general purpose text manipulation tools concerning documentation within source files? I've been trying to find something like JSDoc that I can use to get some editor hints around parameter types, return values, etc.
Re:Notes on the M4 Macro Language (2008) by yjftsjthsd-h on Friday May 20, 2022 @12:38AM (#31443411)
I don't know if this is what you're looking for, but that vaguely sounds like literate programming, and in particular tools like https://en.wikipedia.org/wiki/Web_(programming_system)#CWEB and https://en.wikipedia.org/wiki/Noweb

(Or maybe you're looking for something totally different; I'm not aware of anything that uses literate programming to feed hints back into an editor, although I'm sure someone has made emacs do it, probably using org mode;])