Improved help error messages

The help and man commands now issue an error message when
the help file cannot be opened: say because there is a
help command typo and there is no such help file.
This commit is contained in:
Landon Curt Noll
2021-11-06 12:47:19 -07:00
parent 5fe5ab1c4b
commit dbf5acf5e8
2 changed files with 40 additions and 21 deletions

View File

@@ -243,6 +243,11 @@ The following are the changes from calc version 2.13.0.1 to 2.13.0.1:
Updated HOWTO.INSTALL to mention Makefile.local. Updated HOWTO.INSTALL to mention Makefile.local.
Fizzbin is better. :-)
The help and man builtin commands now return an error when a
help file cannot be opened, such as when there is no help file.
The following are the changes from calc version 2.13.0.0 to 2.13.0.0: The following are the changes from calc version 2.13.0.0 to 2.13.0.0:

56
help.c
View File

@@ -29,6 +29,7 @@
#include <ctype.h> #include <ctype.h>
#include <sys/types.h> #include <sys/types.h>
#include <signal.h> #include <signal.h>
#include <sys/errno.h>
#include "calc.h" #include "calc.h"
#include "conf.h" #include "conf.h"
@@ -262,6 +263,7 @@ givehelp(char *type)
} }
snprintf(helppath, snprintf_len, "%s/%s", calc_helpdir, type); snprintf(helppath, snprintf_len, "%s/%s", calc_helpdir, type);
helppath[snprintf_len] = '\0'; /* paranoia */ helppath[snprintf_len] = '\0'; /* paranoia */
errno = 0;
stream = fopen(helppath, "r"); stream = fopen(helppath, "r");
if (stream != NULL) { if (stream != NULL) {
@@ -276,30 +278,42 @@ givehelp(char *type)
* open the helpfile (looking in CUSTOMHELPDIR last) * open the helpfile (looking in CUSTOMHELPDIR last)
*/ */
} else { } else {
char *cust_helppath; /* path to the custom help file */ char *cust_helppath; /* path to the custom help file */
size_t cust_snprintf_len; /* malloced custom snprintf buf len */ size_t cust_snprintf_len; /* malloced custom snprintf buf len */
cust_snprintf_len = strlen(calc_customhelpdir)+1+strlen(type) + 1; cust_snprintf_len =
cust_helppath = (char *)malloc(cust_snprintf_len+1); strlen(calc_customhelpdir)+1+strlen(type) + 1;
if (cust_helppath == NULL) { cust_helppath = (char *)malloc(cust_snprintf_len+1);
fprintf(stderr, "malloc failure for givehelp #1\n"); if (cust_helppath == NULL) {
return; fprintf(stderr, "malloc failure for givehelp #1\n");
} return;
snprintf(cust_helppath, cust_snprintf_len, }
"%s/%s", calc_customhelpdir, type); snprintf(cust_helppath, cust_snprintf_len,
cust_helppath[cust_snprintf_len] = '\0'; /* paranoia */ "%s/%s", calc_customhelpdir, type);
stream = fopen(cust_helppath, "r"); cust_helppath[cust_snprintf_len] = '\0'; /* paranoia */
if (stream != NULL) { errno = 0;
stream = fopen(cust_helppath, "r");
if (stream != NULL) {
/* /*
* we have the help file open, now display it * we have the help file open, now display it
*/ */
page_file(stream); page_file(stream);
(void) fclose(stream); (void) fclose(stream);
}
free(cust_helppath);
cust_helppath = NULL;
/* unable to open help file */
} else {
fprintf(stderr, "unable to open help file: %s - %s\n",
type, strerror(errno));
}
free(cust_helppath);
cust_helppath = NULL;
#else /* CUSTOM */
/* unable to open help file */
} else {
fprintf(stderr, "unable to open help file: %s - %s\n",
type, strerror(errno));
#endif /* CUSTOM */ #endif /* CUSTOM */
} }