Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save N0NB/c22054a0160b19dae5c88efd71b3d37e to your computer and use it in GitHub Desktop.
Save N0NB/c22054a0160b19dae5c88efd71b3d37e to your computer and use it in GitHub Desktop.
Replace magic numbers with symbolic names in Tlf
From 8bbc09ebd72b8343c1207e89ca74bc8cefd55d28 Mon Sep 17 00:00:00 2001
From: Nate Bargmann <n0nb@n0nb.us>
Date: Thu, 5 Dec 2019 19:20:46 -0600
Subject: [PATCH 1/4] New effort to use symbolic names for keystrokes
Many "magic numbers" exist for various keystrokes handled in the code,
mostly callinput.c and getexchange.c but elsewhere. Even though these
are often commented, symbolic names are more readable.
---
src/Makefile.am | 2 +-
src/callinput.c | 3 ++-
src/keystroke_names.h | 26 ++++++++++++++++++++++++++
3 files changed, 29 insertions(+), 2 deletions(-)
create mode 100644 src/keystroke_names.h
diff --git a/src/Makefile.am b/src/Makefile.am
index 62cc78c..2f526d4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -53,7 +53,7 @@ noinst_HEADERS = \
get_time.h getctydata.h getexchange.h getmessages.h getpx.h \
getsummary.h gettxinfo.h getwwv.h globalvars.h grabspot.h \
ignore_unused.h initial_exchange.h \
- keyer.h \
+ keyer.h keystroke_names.h \
lancode.h last10.h listmessages.h \
log_to_disk.h logit.h logview.h locator2longlat.h \
makelogline.h messagechange.h muf.h \
diff --git a/src/callinput.c b/src/callinput.c
index 55974a2..315df39 100644
--- a/src/callinput.c
+++ b/src/callinput.c
@@ -48,6 +48,7 @@
#include "gettxinfo.h"
#include "grabspot.h"
#include "ignore_unused.h"
+#include "keystroke_names.h"
#include "lancode.h"
#include "muf.h"
#include "netkeyer.h"
@@ -580,7 +581,7 @@ int callinput(void) {
// Semicolon or Alt-n (M-n), insert note in log.
case ';':
- case 238: {
+ case ALT_N: {
include_note();
x = -1;
break;
diff --git a/src/keystroke_names.h b/src/keystroke_names.h
new file mode 100644
index 0000000..cfc52e8
--- /dev/null
+++ b/src/keystroke_names.h
@@ -0,0 +1,26 @@
+/*
+ * Tlf - contest logging program for amateur radio operators
+ * Copyright (C) 2019 Nate Bargmann <n0nb@n0nb.us>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/* ------------------------------------------------------------
+ * Keystroke magic numbers and names as symbolic
+ * constants.
+ *
+ *--------------------------------------------------------------*/
+
+#define ALT_N 238
--
2.20.1
From d0f089d82563d588016e5c910af958ce1c4042ab Mon Sep 17 00:00:00 2001
From: Nate Bargmann <n0nb@n0nb.us>
Date: Thu, 5 Dec 2019 19:58:41 -0600
Subject: [PATCH 2/4] BACKSLASH
---
src/callinput.c | 5 +++--
src/getexchange.c | 15 +++++++++------
src/keyer.c | 3 ++-
src/keystroke_names.h | 6 ++++--
src/logit.c | 5 +++--
5 files changed, 21 insertions(+), 13 deletions(-)
diff --git a/src/callinput.c b/src/callinput.c
index 315df39..daa0f36 100644
--- a/src/callinput.c
+++ b/src/callinput.c
@@ -499,7 +499,8 @@ int callinput(void) {
break;
}
else {
- x = 92; // '\' log without sending message
+ /* Log without sending message. */
+ x = BACKSLASH;
break;
}
}
@@ -1071,7 +1072,7 @@ int callinput(void) {
}
if ((x == '\n' || x == KEY_ENTER) || x == 32 || x == 9 || x == 11
- || x == 44 || x == 92) {
+ || x == 44 || x == BACKSLASH) {
break;
}
diff --git a/src/getexchange.c b/src/getexchange.c
index e20f1ec..31849b8 100644
--- a/src/getexchange.c
+++ b/src/getexchange.c
@@ -32,6 +32,7 @@
#include "addspot.h"
#include "cw_utils.h"
#include "keyer.h"
+#include "keystroke_names.h"
#include "lancode.h"
#include "locator2longlat.h"
#include "logit.h"
@@ -244,8 +245,8 @@ int getexchange(void) {
/* F4 (TU macro) */
send_standard_message(3);
- /* '\' log without additional message */
- x = 92;
+ /* log without additional message */
+ x = BACKSLASH;
}
}
break;
@@ -254,7 +255,7 @@ int getexchange(void) {
/* <Insert>, send exchange in CT mode */
case KEY_IC: {
if (ctcomp != 0) {
- /* F3 (RST macro) */
+ /* F3 (RST macro) */
send_standard_message(2);
}
@@ -349,7 +350,8 @@ int getexchange(void) {
x = -1;
}
else {
- x = 92; // '\'
+ /* Log without sending a message. */
+ x = BACKSLASH;
}
}
break;
@@ -380,7 +382,8 @@ int getexchange(void) {
}
/* <Enter>, <Tab>, Ctl-K, '\' */
- if (x == '\n' || x == KEY_ENTER || x == 9 || x == 11 || x == 92) {
+ if (x == '\n' || x == KEY_ENTER || x == 9
+ || x == 11 || x == BACKSLASH) {
if ((exchange_serial == 1 && comment[0] >= '0'
&& comment[0] <= '9')) { /* align serial nr. */
@@ -495,7 +498,7 @@ int getexchange(void) {
if (strlen(comment) < 5) {
mvprintw(13, 54, "state/prov?");
mvprintw(12, 54, comment);
- if (x == '\n' || x == KEY_ENTER || x == 92) {
+ if (x == '\n' || x == KEY_ENTER || x == BACKSLASH) {
x = 0;
} else {
refreshp();
diff --git a/src/keyer.c b/src/keyer.c
index 40e22b8..9a9b2ec 100644
--- a/src/keyer.c
+++ b/src/keyer.c
@@ -30,6 +30,7 @@
#include <ctype.h>
#include "clear_display.h"
+#include "keystroke_names.h"
#include "netkeyer.h"
#include "nicebox.h" // Includes curses.h
#include "sendbuf.h"
@@ -70,7 +71,7 @@ void keyer(void) {
const char txcontrolstring[2] = { 20, '\0' }; // ^t
const char rxcontrolstring[2] = { 18, '\0' }; // ^r
const char crcontrolstring[2] = { 13, '\0' }; // cr
- const char ctl_c_controlstring[2] = { 92, '\0' }; // '\'
+ const char ctl_c_controlstring[2] = { BACKSLASH, '\0' }; // '\'
if ((trxmode == CWMODE && cwkeyer == NO_KEYER) ||
(trxmode == DIGIMODE && digikeyer == NO_KEYER)) {
diff --git a/src/keystroke_names.h b/src/keystroke_names.h
index cfc52e8..941321d 100644
--- a/src/keystroke_names.h
+++ b/src/keystroke_names.h
@@ -19,8 +19,10 @@
/* ------------------------------------------------------------
* Keystroke magic numbers and names as symbolic
- * constants.
+ * constants. Values are decimal ordinals provided
+ * by the ncurses library. See doc/keynames.txt.
*
*--------------------------------------------------------------*/
-#define ALT_N 238
+#define BACKSLASH 92
+#define ALT_N 238
diff --git a/src/logit.c b/src/logit.c
index a163d97..4f28c47 100644
--- a/src/logit.c
+++ b/src/logit.c
@@ -31,6 +31,7 @@
#include "clear_display.h"
#include "getexchange.h"
#include "keyer.h"
+#include "keystroke_names.h"
#include "log_to_disk.h"
#include "printcall.h"
#include "recall_exchange.h"
@@ -91,7 +92,7 @@ void logit(void) {
if ((trxmode == CWMODE || trxmode == DIGIMODE)
&& (callreturn == '\n') && ctcomp == 1) {
- callreturn = 92; /* '\' */
+ callreturn = BACKSLASH;
strcpy(comment, cqzone);
}
@@ -193,7 +194,7 @@ void logit(void) {
}
}
- if ((callreturn == 92) && (*hiscall != '\0')) {
+ if ((callreturn == BACKSLASH) && (*hiscall != '\0')) {
defer_store = 0;
log_to_disk(false);
--
2.20.1
From 5cbd1d5ecd913834c1f444a075684be3e1113c11 Mon Sep 17 00:00:00 2001
From: Nate Bargmann <n0nb@n0nb.us>
Date: Thu, 5 Dec 2019 20:15:21 -0600
Subject: [PATCH 3/4] CTRL_A
---
src/calledit.c | 3 ++-
src/callinput.c | 2 +-
src/edit_last.c | 3 ++-
src/getexchange.c | 4 ++--
src/keystroke_names.h | 1 +
5 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/src/calledit.c b/src/calledit.c
index 2e6a9ac..7d1f906 100644
--- a/src/calledit.c
+++ b/src/calledit.c
@@ -27,6 +27,7 @@
#include <string.h>
#include "getctydata.h"
+#include "keystroke_names.h"
#include "searchlog.h" // Includes glib.h
#include "showinfo.h"
#include "tlf.h"
@@ -77,7 +78,7 @@ void calledit(void) {
block_part = 0;
// Ctrl-A (^A) or <Home>, move to head of callsign field.
- if (i == 1 || i == KEY_HOME) {
+ if (i == CTRL_A || i == KEY_HOME) {
b = 0;
x = 0;
}
diff --git a/src/callinput.c b/src/callinput.c
index daa0f36..a67a62c 100644
--- a/src/callinput.c
+++ b/src/callinput.c
@@ -893,7 +893,7 @@ int callinput(void) {
}
// Ctrl-A (^A), add a spot and share on LAN.
- case 1: {
+ case CTRL_A: {
addspot();
HideSearchPanel();
showinfo(SHOWINFO_DUMMY);
diff --git a/src/edit_last.c b/src/edit_last.c
index 37f7ea8..d8658ad 100644
--- a/src/edit_last.c
+++ b/src/edit_last.c
@@ -31,6 +31,7 @@
#include "background_process.h"
#include "err_utils.h"
#include "globalvars.h" // Includes glib.h and tlf.h
+#include "keystroke_names.h"
#include "logview.h"
#include "scroll_log.h"
#include "tlf_curses.h"
@@ -114,7 +115,7 @@ void edit_last(void) {
j = key_get();
// Ctrl-A (^A) or <Home>, beginning of line.
- if (j == 1 || j == KEY_HOME) {
+ if (j == CTRL_A || j == KEY_HOME) {
b = 1;
// Ctrl-E (^E) or <End>, end of line.
diff --git a/src/getexchange.c b/src/getexchange.c
index 31849b8..01a720f 100644
--- a/src/getexchange.c
+++ b/src/getexchange.c
@@ -192,7 +192,7 @@ int getexchange(void) {
x = KEY_LEFT;
continue;
}
- case 1: { // Ctl-a (^A)
+ case CTRL_A: { // Ctl-a (^A)
addspot();
*comment = '\0';
x = 9; // <Tab>
@@ -1135,7 +1135,7 @@ void exchange_edit(void) {
i = key_get();
// Ctrl-A (^A) or <Home>, move to beginning of comment field.
- if (i == 1 || i == KEY_HOME) {
+ if (i == CTRL_A || i == KEY_HOME) {
b = 0;
diff --git a/src/keystroke_names.h b/src/keystroke_names.h
index 941321d..017b14e 100644
--- a/src/keystroke_names.h
+++ b/src/keystroke_names.h
@@ -24,5 +24,6 @@
*
*--------------------------------------------------------------*/
+#define CTRL_A 1
#define BACKSLASH 92
#define ALT_N 238
--
2.20.1
From 76c4e1f5d3c132b9b0e986db75f80cbe46303aa1 Mon Sep 17 00:00:00 2001
From: Nate Bargmann <n0nb@n0nb.us>
Date: Thu, 5 Dec 2019 20:18:39 -0600
Subject: [PATCH 4/4] CTRL_B
---
src/callinput.c | 2 +-
src/keystroke_names.h | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/callinput.c b/src/callinput.c
index a67a62c..540204d 100644
--- a/src/callinput.c
+++ b/src/callinput.c
@@ -904,7 +904,7 @@ int callinput(void) {
}
// Ctrl-B (^B), send spot to DX cluster.
- case 2: {
+ case CTRL_B: {
announcefilter = 0;
cluster = CLUSTER;
send_cluster();
diff --git a/src/keystroke_names.h b/src/keystroke_names.h
index 017b14e..6521ded 100644
--- a/src/keystroke_names.h
+++ b/src/keystroke_names.h
@@ -25,5 +25,6 @@
*--------------------------------------------------------------*/
#define CTRL_A 1
+#define CTRL_B 2
#define BACKSLASH 92
#define ALT_N 238
--
2.20.1
@zcsahok
Copy link

zcsahok commented Dec 6, 2019

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment