Thanks Bakul, that's interesting.

Impetus for the obfuscated c contest?! The same contest where a guy wrote a pdp11 emulator capable of running v0, v6, and 2.9bsd in 50 lines of c (Mills, 2018)? I still get a kick out of that code. Particularly because you can run Mullender's 1984 entry on the 2.9 bsd instance it hosts (it also works in v7, but it's too fast, even with stty 300).

Thankfully, macros, while adding an additional layer of complexity, aren't as impenetrable as the ioccc stuff:

mills's prog.c snippet:
struct timeval F,G; struct termios H,U={ T} ; enum{ N=64,a=N<<7,b=a-1,c=a*32,d
=c-1,    e=c/    2,f=    a*2,    g=a/2,h =g/2,j =h/ 2,Q=V*j*5} ; char*s=P,K,M;
int*      p,      l[      a]      ,m,n,J,o=A,    O=j,E,R,i,k,t,r,q,u,v,w,x,y,z
,B,C,    *D,Z    ;int    main    (){ for(D=mmap...

mullender's mullender.c in it's entirety (requires v7 or better and pdp11):
short main[] = {
    277, 04735, -4129, 25, 0, 477, 1019, 0xbef, 0, 12800,
    -113, 21119, 0x52d7, -1006, -7151, 0, 0x4bc, 020004,
    14880, 10541, 2056, 04010, 4548, 3044, -6716, 0x9,
    4407, 6, 5568, 1, -30460, 0, 0x9, 5570, 512, -30419,
    0x7e82, 0760, 6, 0, 4, 02400, 15, 0, 4, 1280, 4, 0,
    4, 0, 0, 0, 0x8, 0, 4, 0, ',', 0, 12, 0, 4, 0, '#',
    0, 020, 0, 4, 0, 30, 0, 026, 0, 0x6176, 120, 25712,
    'p', 072163, 'r', 29303, 29801, 'e'
};

Later,

Will



On 2/19/22 10:06 AM, Bakul Shah wrote:
https://research.swtch.com/shmacro

On Feb 19, 2022, at 7:44 AM, Will Senn <will.senn@gmail.com> wrote:

 I have been poring through the v7 source code lately, and came across an oddity I would like to know more about. Specifically, in sh. The code for sh is c, but it makes extensive use of of macros, for example:
/usr/src/cmd/sh/word.c
...
WHILE (c=nextc(0), space(c)) DONE
...

The macros for sh are defined in mac.h:
/usr/src/cmd/sh/mac.h
...
#define BEGIN   {
#define END     }
#define SWITCH  switch(
#define IN      ){
#define ENDSW   }
#define FOR     for(
#define WHILE   while(
#define DO      ){
#define OD      ;}
#define REP     do{
#define PER     }while(
#define DONE    );
...
I can read the resultant code through the lens of my experience coding c, but I'm curious why the macros and how this came about? In v6, the sh source is straight up c. Is there a story behind it worth knowing?

Thanks,

Will