Skip to content

Commit b093db9

Browse files
committed
mamake, bltins: Introduce usage of C99 bools
- Transition the prdata struct to use C99 style initialization. - Introduce usage of C99 bools, starting with mamake and the builtins. - Also use bitfields in the mamake struct for lower memory consumption. - Remove obsolete C89 usage of the struct hack.
1 parent b5c3c5a commit b093db9

File tree

19 files changed

+125
-143
lines changed

19 files changed

+125
-143
lines changed

src/cmd/INIT/mamake.c

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ static const char usage[] =
133133
#if !_PACKAGE_ast
134134
#include <errno.h>
135135
#include <signal.h>
136+
#include <stdbool.h>
136137
#include <stdlib.h>
137138
#include <string.h>
138139
#endif
@@ -201,11 +202,7 @@ typedef struct Dict_item_s /* dictionary item */
201202
struct Dict_item_s *left; /* left child */
202203
struct Dict_item_s *right; /* right child */
203204
void *value; /* user defined value */
204-
#if __STDC_VERSION__ >= 199901L
205205
char name[]; /* 0 terminated name */
206-
#else
207-
char name[1];/* 0 terminated name */
208-
#endif
209206
} Dict_item_t;
210207

211208
typedef struct Dict_s /* dictionary handle */
@@ -248,11 +245,7 @@ typedef struct View_s /* viewpath level */
248245
{
249246
struct View_s *next; /* next level in viewpath */
250247
size_t node; /* viewpath node path length */
251-
#if __STDC_VERSION__ >= 199901L
252248
char dir[]; /* viewpath level dir prefix */
253-
#else
254-
char dir[1]; /* viewpath level dir prefix */
255-
#endif
256249
} View_t;
257250

258251
typedef struct Makestate_s /* make() shareable state */
@@ -285,21 +278,21 @@ static struct /* program state */
285278
char *packageroot; /* %{PACKAGEROOT} */
286279

287280
int active; /* targets currently active */
288-
int chaos; /* don't save up parallel logs */
289281
int debug; /* negative of debug level */
290282
int exitstatus; /* > 0 if error(s) occurred */
291-
int exec; /* execute actions */
292-
int explain; /* explain actions */
293-
int force; /* all targets out of date */
294-
int ignore; /* ignore command errors */
295283
int indent; /* debug indent */
296-
int keepgoing; /* do siblings on error */
297-
int never; /* never execute */
298-
int probed; /* probe already done */
299-
int verified; /* don't bother with verify() */
300284
int jobs, maxjobs; /* for parallel sh execution */
301285
size_t installrootlen; /* strlen of %{INSTALLROOT} */
302286
size_t packagerootlen; /* strlen of %{PACKAGEROOT} */
287+
bool explain:1; /* explain actions */
288+
bool ignore:1; /* ignore command errors */
289+
bool keepgoing:1; /* do siblings on error */
290+
bool force:1; /* all targets out of date */
291+
bool exec:1; /* execute actions */
292+
bool never:1; /* never execute */
293+
bool probed:1; /* probe already done */
294+
bool verified:1; /* don't bother with verify() */
295+
bool chaos:1; /* don't save up parallel logs */
303296

304297
Stream_t streams[4]; /* input file stream stack */
305298
Stream_t *sp; /* input stream stack pointer */
@@ -401,11 +394,7 @@ static void report(int level, char *text, char *item, Rule_t *r)
401394
fprintf(stderr, "%s: ", item);
402395
fprintf(stderr, "%s", text);
403396
if (r && r->time && state.debug <= -2)
404-
#if __STDC_VERSION__ >= 199901L
405397
fprintf(stderr, " %llu", (unsigned long long)r->time);
406-
#else
407-
fprintf(stderr, " %lu", (unsigned long)r->time);
408-
#endif
409398
fprintf(stderr, "\n");
410399
if (level > 2)
411400
exit_wait(level - 2);
@@ -1602,15 +1591,16 @@ static void run(Rule_t *r, char *s)
16021591
{
16031592
Rule_t *q;
16041593
char *t;
1605-
int c, i, j, x;
1594+
int c, i, j;
1595+
bool x;
16061596
Buf_t *buf;
16071597

16081598
if (r->flags & RULE_error)
16091599
return;
16101600
buf = buffer();
16111601
if (!strncmp(s, "mamake -r ", 10))
16121602
{
1613-
state.verified = 1;
1603+
state.verified = true;
16141604
x = !state.never;
16151605
}
16161606
else
@@ -2069,7 +2059,7 @@ static void propagate(Rule_t *q, Rule_t *r, time_t *modtime)
20692059

20702060
static void exit_wait(int e)
20712061
{
2072-
state.keepgoing = 1;
2062+
state.keepgoing = true;
20732063
walk(state.rules, wreap);
20742064
if (state.exitstatus > e)
20752065
e = state.exitstatus;
@@ -2479,7 +2469,7 @@ static void make(Rule_t *r, Makestate_t *parentstate)
24792469
}
24802470
if (!state.probed && strcmp(t, "CC") == 0)
24812471
{
2482-
state.probed = 1;
2472+
state.probed = true;
24832473
probe(r, &st);
24842474
}
24852475
continue;
@@ -2781,7 +2771,7 @@ int main(int argc, char **argv)
27812771

27822772
state.id = argv[0];
27832773
state.active = 1;
2784-
state.exec = 1;
2774+
state.exec = true;
27852775
state.file = mamfile;
27862776
state.opt = buffer();
27872777
state.shim_buf = buffer();
@@ -2801,15 +2791,15 @@ int main(int argc, char **argv)
28012791
{
28022792
case 'c':
28032793
append(state.opt, " -c");
2804-
state.chaos = 1;
2794+
state.chaos = true;
28052795
continue;
28062796
case 'e':
28072797
append(state.opt, " -e");
2808-
state.explain = 1;
2798+
state.explain = true;
28092799
continue;
28102800
case 'i':
28112801
append(state.opt, " -i");
2812-
state.ignore = 1;
2802+
state.ignore = true;
28132803
continue;
28142804
case 'j':
28152805
append(state.opt, " -j");
@@ -2818,18 +2808,18 @@ int main(int argc, char **argv)
28182808
continue;
28192809
case 'k':
28202810
append(state.opt, " -k");
2821-
state.keepgoing = 1;
2811+
state.keepgoing = true;
28222812
continue;
28232813
case 'N':
2824-
state.never = 1;
2814+
state.never = true;
28252815
/* FALLTHROUGH */
28262816
case 'n':
28272817
append(state.opt, " -n");
2828-
state.exec = 0;
2818+
state.exec = false;
28292819
continue;
28302820
case 'F':
28312821
append(state.opt, " -F");
2832-
state.force = 1;
2822+
state.force = true;
28332823
continue;
28342824
case 'V':
28352825
return !(write(1, id + 10, strlen(id) - 12) > 0 && putchar('\n') == '\n');
@@ -2918,30 +2908,30 @@ int main(int argc, char **argv)
29182908
break;
29192909
case 'c':
29202910
append(state.opt, " -c");
2921-
state.chaos = 1;
2911+
state.chaos = true;
29222912
continue;
29232913
case 'e':
29242914
append(state.opt, " -e");
2925-
state.explain = 1;
2915+
state.explain = true;
29262916
continue;
29272917
case 'i':
29282918
append(state.opt, " -i");
2929-
state.ignore = 1;
2919+
state.ignore = true;
29302920
continue;
29312921
case 'k':
29322922
append(state.opt, " -k");
2933-
state.keepgoing = 1;
2923+
state.keepgoing = true;
29342924
continue;
29352925
case 'N':
2936-
state.never = 1;
2926+
state.never = true;
29372927
/* FALLTHROUGH */
29382928
case 'n':
29392929
append(state.opt, " -n");
2940-
state.exec = 0;
2930+
state.exec = false;
29412931
continue;
29422932
case 'F':
29432933
append(state.opt, " -F");
2944-
state.force = 1;
2934+
state.force = true;
29452935
continue;
29462936
case 'G':
29472937
append(state.opt, " -G");
@@ -3009,7 +2999,7 @@ int main(int argc, char **argv)
30092999
*/
30103000

30113001
if (state.force)
3012-
state.explain = 0;
3002+
state.explain = false;
30133003
if (state.recurse)
30143004
state.maxjobs = 0;
30153005

@@ -3133,7 +3123,7 @@ int main(int argc, char **argv)
31333123

31343124
if (!state.active && !state.verified)
31353125
{
3136-
state.keepgoing = 1;
3126+
state.keepgoing = true;
31373127
walk(state.rules, verify);
31383128
}
31393129

src/cmd/ksh93/bltins/alarm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static void trap_timeout(void* handle)
130130
struct tevent *tp = (struct tevent*)handle;
131131
sh.trapnote |= SH_SIGALRM;
132132
if(!(tp->flags&R_FLAG))
133-
tp->timeout = 0;
133+
tp->timeout = NULL;
134134
tp->flags |= L_FLAG;
135135
if(sh_isstate(SH_TTYWAIT))
136136
sh_timetraps();
@@ -160,13 +160,13 @@ void sh_timetraps(void)
160160
int states = sh.st.states;
161161
char *dbg = sh.st.trap[SH_DEBUGTRAP];
162162
Lex_t *lexp = sh.lex_context, savelex = *lexp;
163-
char jc = job.jobcontrol;
163+
bool jc = job.jobcontrol;
164164
int savesig = job.savesig;
165165
struct process *pw = job.pwlist;
166166
Fcin_t savefc;
167167
int oerrno = errno;
168168
fcsave(&savefc);
169-
job.jobcontrol = 0;
169+
job.jobcontrol = false;
170170
job.pwlist = NULL; /* avoid external commands in the disc funct affecting job list */
171171
sh_lexopen(lexp,0); /* fully reset lexer state */
172172
sh_offoption(SH_XTRACE);

src/cmd/ksh93/bltins/cd_pwd.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ static void rehash(Namval_t *np,void *data)
5353
*/
5454
int sh_diropenat(int dir, const char *path)
5555
{
56-
int fd, needs_cloexec = 0;
56+
int fd;
57+
bool needs_cloexec = false;
5758
if((fd = openat(dir, path, O_DIRECTORY|O_NONBLOCK|O_CLOEXEC|O_SEARCH)) < 0)
5859
{
5960
#if !_openat_enotdir
@@ -75,11 +76,11 @@ int sh_diropenat(int dir, const char *path)
7576
if(shfd < 0)
7677
return shfd;
7778
if(F_DUPFD_CLOEXEC == F_DUPFD)
78-
needs_cloexec = 1;
79+
needs_cloexec = true;
7980
fd = shfd;
8081
}
8182
else if(O_CLOEXEC == 0)
82-
needs_cloexec = 1;
83+
needs_cloexec = true;
8384
if(needs_cloexec)
8485
fcntl(fd,F_SETFD,FD_CLOEXEC);
8586
sh.fdstatus[fd] = (IOREAD|IOCLEX);
@@ -92,8 +93,9 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
9293
char *dir;
9394
Pathcomp_t *cdpath = 0;
9495
const char *dp;
95-
int saverrno=0;
96-
int rval,pflag=0,eflag=0,ret=1,saverr;
96+
int saverrno = 0;
97+
int rval,ret=1,saverr;
98+
bool eflag = false, pflag = false;
9799
char *oldpwd, *cp;
98100
Namval_t *opwdnod, *pwdnod;
99101
#if _lib_openat
@@ -103,13 +105,13 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
103105
while((rval = optget(argv,sh_optcd))) switch(rval)
104106
{
105107
case 'e':
106-
eflag = 1;
108+
eflag = true;
107109
break;
108110
case 'L':
109-
pflag = 0;
111+
pflag = false;
110112
break;
111113
case 'P':
112-
pflag = 1;
114+
pflag = true;
113115
break;
114116
case ':':
115117
if(sh_isoption(SH_RESTRICTED))
@@ -344,17 +346,18 @@ int b_cd(int argc, char *argv[],Shbltin_t *context)
344346

345347
int b_pwd(int argc, char *argv[],Shbltin_t *context)
346348
{
347-
int n, flag = 0;
349+
int n;
350+
bool physical = false;
348351
char *cp;
349352
NOT_USED(argc);
350353
NOT_USED(context);
351354
while((n = optget(argv,sh_optpwd))) switch(n)
352355
{
353356
case 'L':
354-
flag = 0;
357+
physical = false;
355358
break;
356359
case 'P':
357-
flag = 1;
360+
physical = true;
358361
break;
359362
case ':':
360363
errormsg(SH_DICT,2, "%s", opt_info.arg);
@@ -374,7 +377,7 @@ int b_pwd(int argc, char *argv[],Shbltin_t *context)
374377
errormsg(SH_DICT,ERROR_system(1), e_pwd);
375378
UNREACHABLE();
376379
}
377-
if(flag)
380+
if(physical)
378381
{
379382
cp = strcpy(stkseek(sh.stk,(ptrdiff_t)strlen(cp)+PATH_MAX),cp);
380383
pathcanon(cp,PATH_PHYSICAL);

src/cmd/ksh93/bltins/cflow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
int b_return(int n, char *argv[],Shbltin_t *context)
4545
{
4646
/* 'return' outside of function, dotscript and profile behaves like 'exit' */
47-
const int do_exit = **argv=='e' || sh.fn_depth==0 && sh.dot_depth==0 && (!sh_isstate(SH_PROFILE) || sh.realsubshell);
47+
const bool do_exit = **argv=='e' || sh.fn_depth==0 && sh.dot_depth==0 && (!sh_isstate(SH_PROFILE) || sh.realsubshell);
4848
NOT_USED(context);
4949
while((n = optget(argv, **argv=='e' ? sh_optexit : sh_optreturn))) switch(n)
5050
{
@@ -104,7 +104,7 @@ int b_return(int n, char *argv[],Shbltin_t *context)
104104
int b_break(int n, char *argv[],Shbltin_t *context)
105105
{
106106
char *arg;
107-
int cont= **argv=='c';
107+
bool cont = **argv=='c';
108108
NOT_USED(context);
109109
while((n = optget(argv,cont?sh_optcont:sh_optbreak))) switch(n)
110110
{

src/cmd/ksh93/bltins/enum.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct Enum
104104
{
105105
Namfun_t hdr;
106106
ptrdiff_t nelem;
107-
char iflag;
107+
bool iflag;
108108
const char *values[1];
109109
};
110110

@@ -212,7 +212,7 @@ const Namdisc_t ENUM_disc = { 0, put_enum, get_enum, nv_getn, 0, 0, clone_enum }
212212
int b_enum(int argc, char** argv, Shbltin_t *context)
213213
{
214214
int i;
215-
char iflag = 0;
215+
bool iflag = false;
216216
size_t sz,n;
217217
Namval_t *np, *tp;
218218
Namarr_t *ap;
@@ -229,7 +229,7 @@ int b_enum(int argc, char** argv, Shbltin_t *context)
229229
switch (optget(argv, sh_optenum))
230230
{
231231
case 'i':
232-
iflag = 'i';
232+
iflag = true;
233233
continue;
234234
case '?':
235235
/* self-doc: write to standard output */

0 commit comments

Comments
 (0)