1#
2# rlimits.awk: {g,n}awk script to generate rlimits.h
3#
4# NB: On SunOS 4.1.3 - user-functions don't work properly, also \" problems
5# Without 0 + hacks some nawks compare numbers as strings
6#
7BEGIN {limidx = 0}
8
9/^[\t ]*(#[\t ]*define[\t _]*RLIMIT_[A-Z_]*[\t ]*[0-9][0-9]*|RLIMIT_[A-Z_]*,[\t ]*|_*RLIMIT_[A-Z_]*[\t ]*=[\t ]*[0-9][0-9]*,[\t ]*)/ {
10    limindex = index($0, "RLIMIT_")
11    limtail = substr($0, limindex, 80)
12    split(limtail, tmp)
13    limnam = substr(tmp[1], 8, 20)
14    limnum = tmp[2]
15    # in this case I assume GNU libc resourcebits.h
16    if (limnum == "") {
17	limnum = limidx++
18	limindex = index($0, ",")
19	limnam = substr(limnam, 1, limindex-1)
20    }
21    if (limnum == "=") {
22	if (tmp[3] ~ /^[0-9]/) {
23	    limnum = tmp[3] + 0
24	} else {
25	    limnum = limidx++
26	}
27	limindex = index($0, ",")
28	limnam = substr(limnam, 1, limindex-1)
29    }
30    limrev[limnam] = limnum
31    if (lim[limnum] == "") {
32	lim[limnum] = limnam
33	if (limnum ~ /^[0-9]*$/) {
34	    if (limnam == "AIO_MEM") { msg[limnum] = "Maiomemorylocked" }
35	    if (limnam == "AIO_OPS") { msg[limnum] = "Naiooperations" }
36	    if (limnam == "AS")      { msg[limnum] = "Maddressspace" }
37	    if (limnam == "CORE")    { msg[limnum] = "Mcoredumpsize" }
38	    if (limnam == "CPU")     { msg[limnum] = "Tcputime" }
39	    if (limnam == "DATA")    { msg[limnum] = "Mdatasize" }
40	    if (limnam == "FSIZE")   { msg[limnum] = "Mfilesize" }
41	    if (limnam == "LOCKS")   { msg[limnum] = "Nmaxfilelocks" }
42	    if (limnam == "MEMLOCK") { msg[limnum] = "Mmemorylocked" }
43	    if (limnam == "NOFILE")  { msg[limnum] = "Ndescriptors" }
44	    if (limnam == "NPROC")   { msg[limnum] = "Nmaxproc" }
45	    if (limnam == "NTHR")    { msg[limnum] = "Nmaxpthreads" }
46	    if (limnam == "OFILE")   { msg[limnum] = "Ndescriptors" }
47	    if (limnam == "PTHREAD") { msg[limnum] = "Nmaxpthreads" }
48	    if (limnam == "RSS")     { msg[limnum] = "Mresident" }
49	    if (limnam == "SBSIZE")  { msg[limnum] = "Msockbufsize" }
50	    if (limnam == "STACK")   { msg[limnum] = "Mstacksize" }
51	    if (limnam == "TCACHE")  { msg[limnum] = "Ncachedthreads" }
52	    if (limnam == "VMEM")    { msg[limnum] = "Mvmemorysize" }
53	    if (limnam == "SIGPENDING") { msg[limnum] = "Nsigpending" }
54	    if (limnam == "MSGQUEUE") { msg[limnum] = "Nmsgqueue" }
55	    if (limnam == "NICE") { msg[limnum] = "Nnice" }
56	    if (limnam == "RTPRIO") { msg[limnum] = "Nrt_priority" }
57	    if (limnam == "RTTIME") { msg[limnum] = "Urt_time" }
58	    if (limnam == "POSIXLOCKS") { msg[limnum] = "Nposixlocks" }
59	    if (limnam == "NPTS")    { msg[limnum] = "Npseudoterminals" }
60	    if (limnam == "SWAP")    { msg[limnum] = "Mswapsize" }
61	    if (limnam == "KQUEUES") { msg[limnum] = "Nkqueues" }
62        }
63    }
64}
65/^[\t ]*#[\t ]*define[\t _]*RLIM_NLIMITS[\t ]*[0-9][0-9]*/ {
66    limindex = index($0, "RLIM_")
67    limtail = substr($0, limindex, 80)
68    split(limtail, tmp)
69    nlimits = tmp[2]
70}
71# in case of GNU libc
72/^[\t ]*RLIM_NLIMITS[\t ]*=[\t ]*RLIMIT_NLIMITS/ {
73    if(!nlimits) { nlimits = limidx }
74}
75/^[\t _]*RLIM(IT)?_NLIMITS[\t ]*=[\t ]*[0-9][0-9]*/ {
76    limindex = index($0, "=")
77    limtail = substr($0, limindex, 80)
78    split(limtail, tmp)
79    nlimits = tmp[2]
80}
81
82END {
83    if (limrev["MEMLOCK"] != "") {
84        irss = limrev["RSS"]
85        msg[irss] = "Mmemoryuse"
86    }
87    ps = "%s"
88
89    printf("%s\n%s\n\n", "/** rlimits.h                              **/", "/** architecture-customized limits for zsh **/")
90    printf("#define ZSH_NLIMITS %d\n\nstatic char const *recs[ZSH_NLIMITS] = {\n", 0 + nlimits)
91
92    for (i = 0; i < 0 + nlimits; i++)
93	if (msg[i] == "")
94            printf("\t%c%s%c,\n", 34, lim[i], 34)
95	else
96	    printf("\t%c%s%c,\n", 34, substr(msg[i], 2, 30), 34)
97    print "};"
98    print ""
99    print "static int limtype[ZSH_NLIMITS] = {"
100    for (i = 0; i < 0 + nlimits; i++) {
101	if (msg[i] == "")
102	    limtype = "UNKNOWN"
103	else {
104	    limtype = substr(msg[i], 1, 1)
105	    if(limtype == "M") { limtype = "MEMORY" }
106	    if(limtype == "N") { limtype = "NUMBER" }
107	    if(limtype == "T") { limtype = "TIME" }
108	    if(limtype == "U") { limtype = "MICROSECONDS" }
109	}
110	printf("\tZLIMTYPE_%s,\n", limtype)
111    }
112    print "};"
113
114    exit(0)
115}
116