чување c59529a6f28476af63802acec5c1591e72394858
родитељ 884fbe26d1b1c4b5632fe7eb5b68e8b47dd7f8e5
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Wed, 21 Jan 2026 20:06:50 +0100
Tidy up exit error codes
Diffstat:
измењених датотека: 3, додавања: 32(+), брисања: 4(-)
diff --git a/bool.1.in b/bool.1.in
@@ -68,6 +68,16 @@ Parentheses (force operator precedence).
For errors caused by the unsuccessful calls to libc functions setting
.Va errno ,
that value is returned as the exit status.
+Otherwise, the exit status is one of the following:
+.Bl -tag -width Ds -offset indent -compact
+.It Li 201
+Command line argument is not recognized.
+.It Li 202
+.Fn va_arg
+returned NULL (should not happen).
+.It Li 203
+There was an error during the parsing of input, or the root node of AST is NULL.
+.El
.
.Sh EXAMPLES
.Bd -literal
diff --git a/bool.h b/bool.h
@@ -13,6 +13,15 @@ enum Operator {
OP_IMPL
};
+#define BOOL_ERR_CODE(num) (200 + (num))
+
+enum {
+ ERR_NONE = 0,
+ ERR_BAD_CL_ARG = BOOL_ERR_CODE(1),
+ ERR_VA_ARG_NULL = BOOL_ERR_CODE(2),
+ ERR_PARSE_ERROR = BOOL_ERR_CODE(3)
+};
+
extern const char* node_type_ids[];
extern const char* operator_images[];
diff --git a/bool.y b/bool.y
@@ -201,12 +201,12 @@ new_expr(const enum NodeType node_type, const enum Operator operator,
new_expr_malloc_error:
perror("new_expr");
- exit(1);
+ exit(errno);
return NULL;
new_expr_va_arg_error:
yyerror("va_arg is NULL");
- exit(1);
+ exit(ERR_VA_ARG_NULL);
return NULL;
}
@@ -374,8 +374,12 @@ main(int argc, char** argv)
ids_size = IDS_DELTA;
num_ids = 0;
ids = malloc(ids_size * sizeof(char*));
+ if (!ids)
+ goto main_malloc_error;
ids[0] = NULL;
vals = malloc(ids_size);
+ if (!vals)
+ goto main_malloc_error;
vals[0] = 0;
//yydebug = 1;
@@ -400,7 +404,7 @@ main(int argc, char** argv)
exit(0);
case '?':
yyerror("Invalid parameter: -%c", optopt);
- exit(1);
+ exit(ERR_BAD_CL_ARG);
default:;
}
}
@@ -418,7 +422,7 @@ main(int argc, char** argv)
fclose(yyin);
if (!root || result)
- exit(1);
+ exit(ERR_PARSE_ERROR);
if (showtree)
print_node(0, 0, root);
@@ -429,4 +433,9 @@ main(int argc, char** argv)
puts("Result");
simulate(0);
}
+ exit(0);
+
+main_malloc_error:
+ perror("malloc");
+ exit(errno);
}