added comments to document non-obvious things.
This commit is contained in:
parent
ae1e7979c7
commit
2599bbb6b8
|
@ -11,12 +11,14 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static const char doc[] =
|
static const char doc[] =
|
||||||
"Reads a serial terminal and outputs to a log file while also printing contents.\n"
|
"Reads a serial terminal and outputs to a log file while also printing "
|
||||||
|
"contents.\n"
|
||||||
"Any options that have a negation as well as an assertion will use "
|
"Any options that have a negation as well as an assertion will use "
|
||||||
"defaults for the provided tty if neither is provided.\n"
|
"defaults for the provided tty if neither is provided.\n"
|
||||||
"If LOG_FILE is not given, will only output to stdout";
|
"If LOG_FILE is not given, will only output to stdout";
|
||||||
static const char args_doc[] = "INPUT_FILE";
|
static const char args_doc[] = "INPUT_FILE";
|
||||||
|
|
||||||
|
// for the parsed CLI args, we want to know if the user explicitly set them, because if not, we dont want to touch them.
|
||||||
typedef enum { TRUE, FALSE, DEFAULT } OptionalBool;
|
typedef enum { TRUE, FALSE, DEFAULT } OptionalBool;
|
||||||
|
|
||||||
// starts at 128 to make sure none of them are ascii printable.
|
// starts at 128 to make sure none of them are ascii printable.
|
||||||
|
@ -31,6 +33,7 @@ typedef enum {
|
||||||
NO_IGNCR
|
NO_IGNCR
|
||||||
} longOnlyOpts;
|
} longOnlyOpts;
|
||||||
|
|
||||||
|
// for storing the parsed CLI args.
|
||||||
struct Arguments {
|
struct Arguments {
|
||||||
char* inFile;
|
char* inFile;
|
||||||
char* logFile;
|
char* logFile;
|
||||||
|
@ -57,7 +60,8 @@ static struct argp_option options[] = {
|
||||||
{"igncr", ENABLE_IGNCR, 0, 0, "ignore cr", 0},
|
{"igncr", ENABLE_IGNCR, 0, 0, "ignore cr", 0},
|
||||||
{"no-igncr", NO_IGNCR, 0, 0, "do not ignore cr", 0},
|
{"no-igncr", NO_IGNCR, 0, 0, "do not ignore cr", 0},
|
||||||
{"baud-rate", 'r', "BAUDRATE", 0,
|
{"baud-rate", 'r', "BAUDRATE", 0,
|
||||||
"baud rate of the terminal.\nMust be a standard baud rate: 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, "
|
"baud rate of the terminal.\nMust be a standard baud rate: 50, 75, 110, "
|
||||||
|
" 134, 150, 200, 300, 600, 1200, 1800, "
|
||||||
"2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, or 460800",
|
"2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, or 460800",
|
||||||
0},
|
0},
|
||||||
{0},
|
{0},
|
||||||
|
@ -129,23 +133,6 @@ static error_t parseArgs(int key, char* arg, struct argp_state* state) {
|
||||||
|
|
||||||
static struct argp argp = {options, parseArgs, args_doc, doc, 0, 0, 0};
|
static struct argp argp = {options, parseArgs, args_doc, doc, 0, 0, 0};
|
||||||
|
|
||||||
// static bool parseArgs(int argc, char* argv[], struct Arguments* arguments) {
|
|
||||||
// bool succsess = true;
|
|
||||||
// switch (argc) {
|
|
||||||
// case 3:
|
|
||||||
// arguments->logFile = argv[2];
|
|
||||||
// // fallthrough
|
|
||||||
// case 2:
|
|
||||||
// arguments->inFile = argv[1];
|
|
||||||
// break;
|
|
||||||
// default:
|
|
||||||
// printf("%s\n%s\n", args_doc, doc);
|
|
||||||
// succsess = false;
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// return succsess;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// puts the current time into buff in the form of YYYY-MM-DD HH:MM:SS.
|
// puts the current time into buff in the form of YYYY-MM-DD HH:MM:SS.
|
||||||
// buff MUST BE at least 20 chars long.
|
// buff MUST BE at least 20 chars long.
|
||||||
static bool getTimeString(char* buff) {
|
static bool getTimeString(char* buff) {
|
||||||
|
@ -164,6 +151,7 @@ static bool getTimeString(char* buff) {
|
||||||
return succsess;
|
return succsess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// puts the given string into the logfile alongside a basic timestamp.
|
||||||
static bool sendToLog(FILE* logFile, char* lineBuff) {
|
static bool sendToLog(FILE* logFile, char* lineBuff) {
|
||||||
bool succsess = true;
|
bool succsess = true;
|
||||||
char currentTime[20] = "";
|
char currentTime[20] = "";
|
||||||
|
@ -367,12 +355,16 @@ static bool setTTY(struct termios* tty, struct Arguments* arguments) {
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool initInputFile(FILE** inFile, struct Arguments* arguments) {
|
// opens the TTY file, and applies any options to the tty through termios that are non-DEFAULT in arguments.
|
||||||
|
static bool initTTY(FILE** inFile, struct Arguments* arguments) {
|
||||||
bool succsess = true;
|
bool succsess = true;
|
||||||
*inFile = fopen(arguments->inFile, "r");
|
*inFile = fopen(arguments->inFile, "r");
|
||||||
|
// the tcgetattr and dcsetattr require POSIX file descriptors, but our reading and writing functions require file
|
||||||
|
// pointers. (I dont want to re-implement buffered input myself)
|
||||||
int fd = fileno(*inFile);
|
int fd = fileno(*inFile);
|
||||||
if (inFile != NULL || fd >= 0) {
|
if (inFile != NULL || fd >= 0) {
|
||||||
struct termios tty;
|
struct termios tty;
|
||||||
|
// initalize the tty struct with current settings. This is required by POSIX.
|
||||||
if (tcgetattr(fd, &tty) == 0) {
|
if (tcgetattr(fd, &tty) == 0) {
|
||||||
if (setTTY(&tty, arguments) != 0) {
|
if (setTTY(&tty, arguments) != 0) {
|
||||||
// Save tty settings, also checking for error
|
// Save tty settings, also checking for error
|
||||||
|
@ -431,23 +423,17 @@ static void processLines(FILE* inFile, FILE* logFile) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
// for some reason, when doing this with a serial port, log.log never gets
|
|
||||||
// written to. I think this is becasue the program is terminated with
|
|
||||||
// ctrl-c, so the file descriptors dont close? adding fflushes fixes it, but
|
|
||||||
// I shouldnt need to add that.
|
|
||||||
|
|
||||||
int ret = EXIT_SUCCESS;
|
int ret = EXIT_SUCCESS;
|
||||||
|
// an argument that is left DEFAULT will not change the tty from whatever state it was in before the start of the
|
||||||
|
// program.
|
||||||
static struct Arguments arguments = {NULL, NULL, DEFAULT, DEFAULT, 0, DEFAULT, DEFAULT, DEFAULT, 0};
|
static struct Arguments arguments = {NULL, NULL, DEFAULT, DEFAULT, 0, DEFAULT, DEFAULT, DEFAULT, 0};
|
||||||
argp_parse(&argp, argc, argv, 0, 0, &arguments);
|
argp_parse(&argp, argc, argv, 0, 0, &arguments);
|
||||||
FILE* inFile = NULL;
|
FILE* inFile = NULL;
|
||||||
if (initInputFile(&inFile, &arguments)) {
|
if (initTTY(&inFile, &arguments)) {
|
||||||
FILE* outFile = NULL;
|
FILE* outFile = NULL;
|
||||||
if (initLogFile(&outFile, arguments.logFile)) {
|
if (initLogFile(&outFile, arguments.logFile)) {
|
||||||
processLines(inFile, outFile);
|
processLines(inFile, outFile);
|
||||||
|
|
||||||
// these things should be done by the OS when we exit.
|
|
||||||
// fclose(inFile);
|
|
||||||
// fclose(outFile);
|
|
||||||
} else {
|
} else {
|
||||||
ret = EXIT_FAILURE;
|
ret = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue