Skip to content

Commit

Permalink
Return with non-zero exit code on error
Browse files Browse the repository at this point in the history
  • Loading branch information
msalau committed May 12, 2018
1 parent a0b49af commit 4f8c9b3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
32 changes: 22 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "rl78.h"
#include "serial.h"
#include "srec.h"
Expand Down Expand Up @@ -84,7 +85,7 @@ int main(int argc, char *argv[])
if (optarg == endp)
{
printf("%s", usage);
return 0;
return EINVAL;
}
break;
case 'm':
Expand All @@ -94,7 +95,7 @@ int main(int argc, char *argv[])
|| MODE_MIN_VALUE > mode)
{
printf("%s", usage);
return 0;
return EINVAL;
}
break;
case 't':
Expand All @@ -103,21 +104,21 @@ int main(int argc, char *argv[])
if (optarg == endp)
{
printf("%s", usage);
return 0;
return EINVAL;
}
break;
case 'p':
if (1 != sscanf(optarg, "%f", &voltage))
{
printf("%s", usage);
return 0;
return EINVAL;
}
if (RL78_MIN_VOLTAGE > voltage
|| RL78_MAX_VOLTAGE < voltage )
{
fprintf(stderr, "Operating voltage is out of range. Operating voltage must be in range %1.1fV..%1.1fV.\n",
RL78_MIN_VOLTAGE, RL78_MAX_VOLTAGE);
return 0;
return EINVAL;
}
break;
case 'v':
Expand Down Expand Up @@ -148,7 +149,7 @@ int main(int argc, char *argv[])
case '?':
default:
printf("%s", usage);
return 0;
return ECANCELED;
}
}
if (invert_reset)
Expand All @@ -167,22 +168,22 @@ int main(int argc, char *argv[])
break;
default:
printf("%s", usage);
return 0;
return EINVAL;
}

// If file is not specified, but required - show error message
if (NULL == filename
&& (1 == write || 1 == verify))
{
fprintf(stderr, "File not specified\n");
return -1;
return ENOENT;
}

port_handle_t fd = serial_open(portname);
int rc = 0;
if (INVALID_HANDLE_VALUE == fd)
{
return -1;
return EBADF;
}

// If no actions are specified - do nothing :)
Expand All @@ -196,6 +197,7 @@ int main(int argc, char *argv[])
return 0;
}

int retcode = 0;
do
{
if (1 == write
Expand All @@ -207,12 +209,14 @@ int main(int argc, char *argv[])
if (0 > rc)
{
fprintf(stderr, "Initialization failed\n");
retcode = EIO;
break;
}
rc = rl78_cmd_reset(fd);
if (0 > rc)
{
fprintf(stderr, "Synchronization failed\n");
retcode = EIO;
break;
}
char device_name[11];
Expand All @@ -221,6 +225,7 @@ int main(int argc, char *argv[])
if (0 > rc)
{
fprintf(stderr, "Silicon signature read failed\n");
retcode = EIO;
break;
}
if (1 == display_info)
Expand All @@ -241,6 +246,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Code flash erase failed\n");
retcode = EIO;
break;
}
}
Expand All @@ -254,6 +260,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Data flash erase failed\n");
retcode = EIO;
break;
}
}
Expand All @@ -272,6 +279,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Read failed\n");
retcode = EIO;
break;
}
}
Expand All @@ -285,6 +293,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Code flash write failed\n");
retcode = EIO;
break;
}
}
Expand All @@ -298,6 +307,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Data flash write failed\n");
retcode = EIO;
break;
}
}
Expand All @@ -311,6 +321,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Code flash verification failed\n");
retcode = EIO;
break;
}
}
Expand All @@ -324,6 +335,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Data flash verification failed\n");
retcode = EIO;
break;
}
}
Expand All @@ -350,5 +362,5 @@ int main(int argc, char *argv[])
while (0);
serial_close(fd);
printf("\n");
return 0;
return retcode;
}
26 changes: 16 additions & 10 deletions src/main_g10.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "rl78g10.h"
#include "serial.h"
#include "srec.h"
Expand Down Expand Up @@ -72,7 +73,7 @@ int main(int argc, char *argv[])
|| MODE_MAX_VALUE < mode)
{
printf("%s", usage);
return 0;
return EINVAL;
}
break;
case 't':
Expand All @@ -81,7 +82,7 @@ int main(int argc, char *argv[])
if (optarg == endp)
{
printf("%s", usage);
return 0;
return EINVAL;
}
break;
case 'v':
Expand All @@ -106,7 +107,7 @@ int main(int argc, char *argv[])
case '?':
default:
printf("%s", usage);
return 0;
return ECANCELED;
}
}
if (invert_reset)
Expand All @@ -129,15 +130,15 @@ int main(int argc, char *argv[])
break;
default:
printf("%s", usage);
return 0;
return EINVAL;
}

// If file is not specified, but required - show error message
if ((NULL == filename || NULL == codesizestr)
&& (1 == write || 1 == verify))
{
fprintf(stderr, "Specify both file and size\n");
return -1;
return EINVAL;
}

int codesize = 0;
Expand All @@ -151,13 +152,13 @@ int main(int argc, char *argv[])
if (codesize & (codesize - 1))
{
fprintf(stderr, "Size (%i) is not valid (not power ot 2)\n", codesize);
return -2;
return EINVAL;
}
/* if codesize is in valid range: 512...64k */
if (codesize < 512 || codesize > 64*1024)
{
fprintf(stderr, "Size (%i) is not valid (not in range)\n", codesize);
return -2;
return EINVAL;
}
}

Expand All @@ -174,16 +175,17 @@ int main(int argc, char *argv[])
int rc = 0;
if (INVALID_HANDLE_VALUE == fd)
{
return -1;
return EBADF;
}
rc = serial_set_parity(fd, ENABLE, ODD);
if (rc < 0)
{
perror("Failed to set port attributes:");
serial_close(fd);
return -1;
return EIO;
}

int retcode = 0;
do
{
if (1 == write || 1 == verify)
Expand All @@ -192,6 +194,7 @@ int main(int argc, char *argv[])
if (0 > rc)
{
fprintf(stderr, "Initialization failed\n");
retcode = EIO;
break;
}
unsigned char code[codesize];
Expand All @@ -205,6 +208,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Read failed\n");
retcode = EIO;
break;
}

Expand All @@ -218,6 +222,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Write failed\n");
retcode = EIO;
break;
}
}
Expand All @@ -231,6 +236,7 @@ int main(int argc, char *argv[])
if (0 != rc)
{
fprintf(stderr, "Verify failed\n");
retcode = EIO;
break;
}
}
Expand All @@ -257,5 +263,5 @@ int main(int argc, char *argv[])
while (0);
serial_close(fd);
printf("\n");
return 0;
return retcode;
}

0 comments on commit 4f8c9b3

Please sign in to comment.