Skip to content

Commit

Permalink
Add error handling for blaze822_loop to all callers
Browse files Browse the repository at this point in the history
  • Loading branch information
nmeum committed Oct 13, 2024
1 parent dc5636c commit 89e7d05
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 70 deletions.
6 changes: 2 additions & 4 deletions maddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ main(int argc, char *argv[])
xpledge("stdio rpath", "");

if (argc == optind && isatty(0))
blaze822_loop1(":", addr);
return blaze822_loop1(":", addr);
else
blaze822_loop(argc-optind, argv+optind, addr);

return 0;
return blaze822_loop(argc-optind, argv+optind, addr);
}
7 changes: 5 additions & 2 deletions magrep.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,13 @@ main(int argc, char *argv[])
exit(2);
}

int ret;
if (argc == optind && isatty(0))
blaze822_loop1(":", magrep);
ret = blaze822_loop1(":", magrep);
else
blaze822_loop(argc-optind, argv+optind, magrep);
ret = blaze822_loop(argc-optind, argv+optind, magrep);
if (ret)
return 2;

if (cflag && !qflag && !mflag)
printf("%ld\n", matches);
Expand Down
4 changes: 1 addition & 3 deletions mdeliver.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,7 @@ main(int argc, char *argv[])
if (argc == optind + 1 && isatty(0))
goto usage;
else
blaze822_loop(argc - 1 - optind, argv + optind, refile);

return 0;
return blaze822_loop(argc - 1 - optind, argv + optind, refile);
}

int c;
Expand Down
4 changes: 2 additions & 2 deletions mexport.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ main(int argc, char *argv[])
xpledge("stdio rpath", "");

if (argc == optind && isatty(0))
blaze822_loop1(":", export);
status |= blaze822_loop1(":", export);
else
blaze822_loop(argc-optind, argv+optind, export);
status |= blaze822_loop(argc-optind, argv+optind, export);

return status;
}
20 changes: 9 additions & 11 deletions mflag.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,29 @@ main(int argc, char *argv[])

if (vflag) {
if (argc == optind && !isatty(0)) {
blaze822_loop(0, 0, flag); // read from stdin
return 0;
return blaze822_loop(0, 0, flag); // read from stdin
}

args = calloc(argsalloc, sizeof (char *));
if (!args)
exit(-1);

int status;
if (argc == optind)
blaze822_loop1(".", add);
status = blaze822_loop1(".", add);
else
blaze822_loop(argc-optind, argv+optind, add);
status = blaze822_loop(argc-optind, argv+optind, add);

if (isatty(0))
blaze822_loop1(":", flag);
status |= blaze822_loop1(":", flag);
else
blaze822_loop(0, 0, flag);
status |= blaze822_loop(0, 0, flag);

return 0;
return status;
}

if (argc == optind && isatty(0))
blaze822_loop1(".", flag);
return blaze822_loop1(".", flag);
else
blaze822_loop(argc-optind, argv+optind, flag);

return 0;
return blaze822_loop(argc-optind, argv+optind, flag);
}
7 changes: 5 additions & 2 deletions mhdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,13 @@ main(int argc, char *argv[])

xpledge("stdio rpath", "");

int ret;
if (argc == optind && isatty(0))
blaze822_loop1(".", header);
ret = blaze822_loop1(".", header);
else
blaze822_loop(argc-optind, argv+optind, header);
ret = blaze822_loop(argc-optind, argv+optind, header);
if (ret)
return 2;

return status;
}
3 changes: 2 additions & 1 deletion minc.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ main(int argc, char *argv[])
if (optind == argc) {
if (isatty(0))
goto usage;
blaze822_loop(0, 0, inc);
if (blaze822_loop(0, 0, inc))
return 1;
} else {
for (i = optind; i < argc; i++)
inc(argv[i]);
Expand Down
5 changes: 3 additions & 2 deletions mlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,11 @@ main(int argc, char *argv[])
flagsum++;
}

int status = 0;
if (optind == argc) {
if (isatty(0))
goto usage;
blaze822_loop(0, 0, listarg);
status = blaze822_loop(0, 0, listarg);
} else {
for (i = optind; i < argc; i++)
listarg(argv[i]);
Expand All @@ -300,5 +301,5 @@ main(int argc, char *argv[])
tunseen, tflagged, tcount);
}

return 0;
return status;
}
7 changes: 4 additions & 3 deletions mpick.c
Original file line number Diff line number Diff line change
Expand Up @@ -1466,11 +1466,12 @@ main(int argc, char *argv[])

xpledge("stdio rpath wpath cpath proc exec", 0);

int status;
void (*cb)(char *) = need_thr ? collect : oneline;
if (argc == optind && isatty(0))
blaze822_loop1(":", cb);
status = blaze822_loop1(":", cb);
else
blaze822_loop(argc-optind, argv+optind, cb);
status = blaze822_loop(argc-optind, argv+optind, cb);

/* print and free last thread */
if (Tflag && thr)
Expand All @@ -1490,5 +1491,5 @@ main(int argc, char *argv[])
free(files);
}

return 0;
return status;
}
12 changes: 6 additions & 6 deletions mscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,9 @@ main(int argc, char *argv[])

if (nflag) {
if (argc == optind && isatty(0))
blaze822_loop1(":", numline);
return blaze822_loop1(":", numline);
else
blaze822_loop(argc-optind, argv+optind, numline);
return 0;
return blaze822_loop(argc-optind, argv+optind, numline);
}

now = time(0);
Expand Down Expand Up @@ -624,16 +623,17 @@ main(int argc, char *argv[])
fflag = v;
}

int status;
if (argc == optind && isatty(0))
blaze822_loop1(":", oneline);
status = blaze822_loop1(":", oneline);
else
blaze822_loop(argc-optind, argv+optind, oneline);
status = blaze822_loop(argc-optind, argv+optind, oneline);

if (pager_pid > 0)
pipeclose(pager_pid);

if (vflag)
fprintf(stderr, "%ld mails scanned\n", seen);

return 0;
return status;
}
6 changes: 2 additions & 4 deletions msed.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ main(int argc, char *argv[])
optind++;

if (argc == optind && isatty(0))
blaze822_loop1(".", sed);
return blaze822_loop1(".", sed);
else
blaze822_loop(argc-optind, argv+optind, sed);

return 0;
return blaze822_loop(argc-optind, argv+optind, sed);
}
10 changes: 4 additions & 6 deletions mseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,11 @@ main(int argc, char *argv[])

xpledge("stdio rpath wpath cpath", "");

if (cflag)
blaze822_loop1(cflag, overridecur);
if (cflag && blaze822_loop1(cflag, overridecur))
return 1;

if (Cflag) {
blaze822_loop1(Cflag, setcur);
return 0;
}
if (Cflag)
return blaze822_loop1(Cflag, setcur);

if (Sflag && optind != argc) {
fprintf(stderr, "error: -S/-A doesn't take arguments.\n");
Expand Down
21 changes: 11 additions & 10 deletions mshow.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,13 @@ extract_cb(char *file)
blaze822_walk_mime(msg, 0, extract_mime);
}

void
int
extract(char *file, int argc, char **argv, int use_stdout)
{
extract_argc = argc;
extract_argv = argv;
extract_stdout = use_stdout;
blaze822_loop1(file, extract_cb);
return blaze822_loop1(file, extract_cb);
}

static char *newcur;
Expand Down Expand Up @@ -832,21 +832,22 @@ main(int argc, char *argv[])
}
}

int status = 0;
if (xflag) { // extract
xpledge("stdio rpath wpath cpath", NULL);
extract(xflag, argc-optind, argv+optind, 0);
status = extract(xflag, argc-optind, argv+optind, 0);
} else if (Oflag) { // extract to stdout
xpledge("stdio rpath", NULL);
extract(Oflag, argc-optind, argv+optind, 1);
status = extract(Oflag, argc-optind, argv+optind, 1);
} else if (tflag) { // list
xpledge("stdio rpath", NULL);
if (argc == optind && isatty(0))
blaze822_loop1(".", list);
status = blaze822_loop1(".", list);
else
blaze822_loop(argc-optind, argv+optind, list);
status = blaze822_loop(argc-optind, argv+optind, list);
} else if (Rflag) { // render for reply
xpledge("stdio rpath", NULL);
blaze822_loop(argc-optind, argv+optind, reply);
status = blaze822_loop(argc-optind, argv+optind, reply);
} else { // show
/* XXX pledge: still r/w on the whole file-system + fork/exec */
if (!(qflag || rflag || Fflag)) {
Expand All @@ -857,9 +858,9 @@ main(int argc, char *argv[])
filters = blaze822(f);
}
if (argc == optind && isatty(0))
blaze822_loop1(".", show);
status = blaze822_loop1(".", show);
else
blaze822_loop(argc-optind, argv+optind, show);
status = blaze822_loop(argc-optind, argv+optind, show);
if (!nflag) // don't set cur
if (newcur)
blaze822_seq_setcur(newcur);
Expand All @@ -875,5 +876,5 @@ main(int argc, char *argv[])
return 1;
}

return 0;
return status;
}
7 changes: 4 additions & 3 deletions msort.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,11 @@ main(int argc, char *argv[])
if (!mails)
exit(-1);

int status;
if (argc == optind && isatty(0))
blaze822_loop1(":", add);
status = blaze822_loop1(":", add);
else
blaze822_loop(argc-optind, argv+optind, add);
status = blaze822_loop(argc-optind, argv+optind, add);

qsort(mails, idx, sizeof (struct mail), order);

Expand All @@ -337,5 +338,5 @@ main(int argc, char *argv[])
for (i = 0; i < idx; i++)
printf("%s\n", mails[i].file);

return 0;
return status;
}
12 changes: 8 additions & 4 deletions mthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ main(int argc, char *argv[])

while ((c = getopt(argc, argv, "S:prv")) != -1)
switch (c) {
case 'S': blaze822_loop1(optarg, thread); break;
case 'S':
if (blaze822_loop1(optarg, thread))
return 1;
break;
case 'v': vflag = 1; break;
case 'p': pflag = 1; break;
case 'r': rflag = 1; break;
Expand All @@ -432,10 +435,11 @@ main(int argc, char *argv[])

optional = 0;

int status;
if (argc == optind && isatty(0))
blaze822_loop1(":", thread);
status = blaze822_loop1(":", thread);
else
blaze822_loop(argc-optind, argv+optind, thread);
status = blaze822_loop(argc-optind, argv+optind, thread);

// the tree of all toplevel threads has depth -1,
// so toplevel threads have depth 0.
Expand All @@ -446,5 +450,5 @@ main(int argc, char *argv[])
sort_tree(top, -1);
print_tree(top, -1);

return 0;
return status;
}
4 changes: 3 additions & 1 deletion t/1100-mhdr.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
cd ${0%/*}
. ./lib.sh

plan 9
plan 10

cat <<EOF >tmp
Header: foo
Expand All @@ -24,3 +24,5 @@ check_same 'header-Three' 'mhdr -h header-Three ./tmp' 'echo quux'
check_same 'header_Four' 'mhdr -h header_Four ./tmp' 'echo ding'

check 'issue 235' 'mhdr ./tmp |grep -i header_four'
check 'non-existent mail' 'mhdr ./does-not-exist ; [ $? -eq 2 ]'

4 changes: 3 additions & 1 deletion t/1500-maddr.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh -e
cd ${0%/*}
. ./lib.sh
plan 11
plan 13

rm -rf test.dir
mkdir test.dir
Expand Down Expand Up @@ -82,5 +82,7 @@ check_same 'long addr' 'maddr -h long 4' 'echo "heeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
check_same 'decode iso8859' 'maddr -h DecodeISO8859 5' 'echo "Keld Jørn Simonsen <[email protected]>"'
check_same 'decode long iso8859' 'maddr -h DecodeLongISO8859 5' 'echo "\"If you can read this you understand the example. z a b\" <[email protected]>"'
check_same 'decode utf8' 'maddr -h DecodeUTF8 5' 'echo "z’z <[email protected]>"'
check 'non-existent mail' 'maddr -S /does/not/exist ; [ $? -eq 1 ]'
check_same 'keep going on non-existent mail' 'maddr /does/not/exist 3 /does/not/exist || true' 'echo "[email protected]"'

)
3 changes: 2 additions & 1 deletion t/1700-mshow.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh -e
cd ${0%/*}
. ./lib.sh
plan 2
plan 3

# Nested MIME where the outer boundary is a prefix of the inner boundary
cat <<EOF >tmp
Expand Down Expand Up @@ -39,3 +39,4 @@ EOF

check 'nested mail has 5 attachments' 'mshow -t ./tmp | wc -l | grep 6'
check 'nested mail has text/html attachment' 'mshow -t ./tmp | grep text/html'
check 'non-existent mail' 'mshow ./does-not-exist ; [ $? -eq 1 ]'
Loading

0 comments on commit 89e7d05

Please sign in to comment.