[PATCH] Clean up the int/enum option_enum mixing

Michael Bienia <[email protected]>
Newsgroups gmane.network.tin.devel
Message-ID <[email protected]>
Hello,

the attached patch deals the following TODO entry:
,----
| o  clean up options_menu.c to remove the enum/int mixing (e.g.
|    search_config() returns int instead of option_enum).
|    [20050615 Urs Janssen <[email protected]>]
`----

Changes:
- move "enum option_enum" from tincfg.h to the new file options_menu.h
  makecfg now also creates options_menu.h
- rename TINTBL_H to TINCFG_H to match the filename
- use enum option_enum instead of int where appropiate

Known bug:
- the Makefile isn't yet adapted to the new dependency options_menu.h
  currently you have to build makecfg first, then tincfg.h (and
  options_menu.h) and finally tin
  Can somebody help me out with this?

Michael
patch-20050621.diff (text/plain, 13 KB)
 doc/TODO           |    4 -
 include/proto.h    |   23 ++++++----
 src/Makefile.in    |    2 
 src/makecfg.c      |  114 ++++++++++++++++++++++++++++++++++-------------------
 src/options_menu.c |   18 ++++----
 src/prompt.c       |   10 ++--
 src/search.c       |   22 ++++------
 src/signal.c       |    2 
 8 files changed, 113 insertions(+), 82 deletions(-)
diff -Nurp tin-1.7.9/doc/TODO tin-1.7.9.new/doc/TODO
--- tin-1.7.9/doc/TODO	2005-06-20 11:31:54.000000000 +0200
+++ tin-1.7.9.new/doc/TODO	2005-06-21 20:46:08.903985472 +0200
@@ -532,10 +532,6 @@ o  try to get rid of the magic numbers l
    a sentinel in the arrays instead (match_list is a showstopper here).
    [20030203 Urs Janssen <[email protected]>]
 
-o  clean up options_menu.c to remove the enum/int mixing (e.g.
-   search_config() returns int instead of option_enum).
-   [20050615 Urs Janssen <[email protected]>]
-
 o  check where joinpath() should be used; check joinpath() for overruns.
    (e.g. grep for TMPDIR in the source)
    [Urs Janssen <[email protected]>]
diff -Nurp tin-1.7.9/include/proto.h tin-1.7.9.new/include/proto.h
--- tin-1.7.9/include/proto.h	2005-06-20 11:31:55.000000000 +0200
+++ tin-1.7.9.new/include/proto.h	2005-06-21 19:30:48.977082951 +0200
@@ -41,6 +41,9 @@
 #ifndef KEYMAP_H
 #	include "keymap.h"
 #endif /* !KEYMAP_H */
+#ifndef OPTIONS_MENU_H
+#	include "options_menu.h"
+#endif /* !OPTIONS_MENU_H */
 
 /* This fixes ambiguities on platforms that don't distinguish extern case */
 #ifdef CASE_PROBLEM
@@ -433,12 +436,12 @@ extern int get_newsrcname(char *newsrc_n
 extern void get_nntpserver(char *nntpserver_name, char *nick_name);
 
 /* options_menu.c */
-extern char *fmt_option_prompt(char *dst, size_t len, t_bool editing, int option);
+extern char *fmt_option_prompt(char *dst, size_t len, t_bool editing, enum option_enum option);
 extern int change_config_file(struct t_group *group);
-extern int option_row(int option);
-extern t_bool option_is_visible(int option);
+extern int option_row(enum option_enum option);
+extern t_bool option_is_visible(enum option_enum option);
 extern void check_score_defaults(void);
-extern void refresh_config_page(int act_option);
+extern void refresh_config_page(enum option_enum act_option);
 extern void show_menu_help(const char *help_message);
 
 /* page.c */
@@ -496,11 +499,11 @@ extern int prompt_yn(const char *prompt,
 extern int prompt_msgid(void);
 extern t_bool prompt_default_string(const char *prompt, char *buf, int buf_len, char *default_prompt, int which_hist);
 extern t_bool prompt_menu_string(int line, const char *prompt, char *var);
-extern t_bool prompt_option_char(int option);
-extern t_bool prompt_option_list(int option);
-extern t_bool prompt_option_num(int option);
-extern t_bool prompt_option_on_off(int option);
-extern t_bool prompt_option_string(int option);
+extern t_bool prompt_option_char(enum option_enum option);
+extern t_bool prompt_option_list(enum option_enum option);
+extern t_bool prompt_option_num(enum option_enum option);
+extern t_bool prompt_option_on_off(enum option_enum option);
+extern t_bool prompt_option_string(enum option_enum option);
 extern t_bool prompt_string(const char *prompt, char *buf, int which_hist);
 extern void prompt_continue(void);
 extern void prompt_slk_redraw(void);
@@ -586,7 +589,7 @@ extern int get_search_vectors(int *start
 extern int search(t_function func, int current_art, t_bool repeat);
 extern int search_active(t_bool forward, t_bool repeat);
 extern int search_article(t_bool forward, t_bool repeat, int start_line, int lines, t_lineinfo *line, int reveal_ctrl_l_lines, FILE *fp);
-extern int search_config(t_bool forward, t_bool repeat, int current, int last);
+extern enum option_enum search_config(t_bool forward, t_bool repeat, enum option_enum current, enum option_enum last);
 extern int search_body(struct t_group *group, int current_art, t_bool repeat);
 extern void reset_srch_offsets(void);
 
diff -Nurp tin-1.7.9/src/makecfg.c tin-1.7.9.new/src/makecfg.c
--- tin-1.7.9/src/makecfg.c	2005-06-20 11:31:56.000000000 +0200
+++ tin-1.7.9.new/src/makecfg.c	2005-06-21 19:38:33.484876642 +0200
@@ -4,7 +4,7 @@
  *  Author    : Thomas E. Dickey
  *  Created   : 1997-08-23
  *  Updated   : 2004-06-03
- *  Notes     : #defines and structs for config.c
+ *  Notes     : #defines and structs for options_menu.c
  *
  * Copyright (c) 1997-2005 Thomas E. Dickey <[email protected]>
  * All rights reserved.
@@ -36,6 +36,7 @@
 
 
 #define __BUILD__
+#define PROTO_H 1	/* don't include proto.h */
 #ifndef TIN_H
 #	include "tin.h"
 #endif /* !TIN_H */
@@ -204,16 +205,7 @@ generate_tbl(
 {
 	static const char *const table_1[] =
 	{
-		 ""
-		,"enum option_enum {"
-		,0
-	};
-	static const char *const table_2[] =
-	{
-		"\tVERY_LAST_OPT };"
-		,""
-		,"#define LAST_OPT VERY_LAST_OPT - 1"
-		,""
+		""
 		,"#define OPT_TITLE     0"
 		,"#define OPT_ON_OFF    1"
 		,"#define OPT_LIST      2"
@@ -224,7 +216,7 @@ generate_tbl(
 		,"struct t_option option_table[]={"
 		,0
 	};
-	static const char *const table_3[] =
+	static const char *const table_2[] =
 	{
 		"};"
 		,0
@@ -234,28 +226,8 @@ generate_tbl(
 	MYDATA *p;
 	char temp[BUFSIZ];
 
-	/* generate enumerated type */
-	write_it(ofp, table_1);
-	for (p = all_data; p != 0; p = p->link) {
-		if (p->name[0] == '#')
-			fprintf(ofp, "%s\n", p->name);
-		else {
-			char *s = p->name;
-
-			fprintf(ofp, "\tOPT_");
-			while (*s != '\0') {
-				fprintf(ofp, "%c",
-					 isalpha ((unsigned char)*s) && islower((unsigned char)*s)
-					 ? toupper ((unsigned char)*s)
-					 : *s);
-				s++;
-			}
-			fprintf(ofp, ",\n");
-		}
-	}
-
 	/* generate the access table */
-	write_it(ofp, table_2);
+	write_it(ofp, table_1);
 	for (p = all_data; p != 0; p = p->link) {
 		if (p->name[0] == '#')
 			fprintf(ofp, "%s\n", p->name);
@@ -289,7 +261,46 @@ generate_tbl(
 		}
 	}
 
-	write_it(ofp, table_3);
+	write_it(ofp, table_2);
+}
+
+static void
+generate_enum(
+	FILE *ofp2)
+{
+	MYDATA *p;
+
+	static const char *const table_1[] = {
+		"enum option_enum {",
+		0
+	};
+	static const char *const table_2[] = {
+		"\tSIGNAL_HANDLER };",
+		"",
+		"#define LAST_OPT SIGNAL_HANDLER - 1",
+		0
+	};
+
+	/* generate enumerated type */
+	write_it(ofp2, table_1);
+	for (p = all_data; p != 0; p = p->link) {
+		if (p->name[0] == '#')
+			fprintf(ofp2, "%s\n", p->name);
+		else {
+			char *s = p->name;
+
+			fprintf(ofp2, "\tOPT_");
+			while (*s != '\0') {
+				fprintf(ofp2, "%c",
+					 isalpha ((unsigned char)*s) && islower((unsigned char)*s)
+					 ? toupper ((unsigned char)*s)
+					 : *s);
+				s++;
+			}
+			fprintf(ofp2, ",\n");
+		}
+	}
+	write_it(ofp2, table_2);
 }
 
 static void
@@ -376,7 +387,8 @@ generate_ptr(
 static void
 makecfg(
 	FILE *ifp,
-	FILE *ofp)
+	FILE *ofp,
+	FILE *ofp2)
 {
 	char buffer[BUFSIZ];
 	MYDATA *p, *q;
@@ -385,8 +397,8 @@ makecfg(
 	{
 		"/* This file is generated by MAKECFG */"
 		,""
-		,"#ifndef TINTBL_H"
-		,"#define TINTBL_H 1"
+		,"#ifndef TINCFG_H"
+		,"#define TINCFG_H 1"
 		,""
 		,"/* Macros for defining symbolic offsets that can be ifdef'd */"
 		,"#undef OINX"
@@ -425,9 +437,22 @@ makecfg(
 		,"#undef OEND"
 		,"#undef OTYP"
 		,""
-		,"#endif /* TINTBL_H */"
+		,"#endif /* TINCFG_H */"
 		,0
 	};
+	static const char *const table_3[] = {
+		"/* This file is generated by MAKECFG */",
+		"",
+		"#ifndef OPTIONS_MENU_H",
+		"#define OPTIONS_MENU_H 1",
+		"",
+		0
+	};
+	static const char *const table_4[] = {
+		"",
+		"#endif /* OPTIONS_MENU_H */",
+		0
+	};
 
 	/*
 	 * Process the input file.
@@ -472,6 +497,16 @@ makecfg(
 	write_it(ofp, table_2);
 
 	fclose(ofp);
+
+	if (ofp2) {
+		write_it(ofp2, table_3);
+
+		generate_enum(ofp2);
+
+		write_it(ofp2, table_4);
+
+		fclose(ofp2);
+	}
 }
 
 int
@@ -481,12 +516,13 @@ main(
 {
 	FILE *input = stdin;
 	FILE *output = stdout;
+	FILE *output2 = open_it("options_menu.h", "w");
 
 	if (argc > 1)
 		input = open_it(argv[1], "r");
 	if (argc > 2)
 		output = open_it(argv[2], "w");
-	makecfg(input, output);
+	makecfg(input, output, output2);
 
 	return (0);
 }
diff -Nurp tin-1.7.9/src/Makefile.in tin-1.7.9.new/src/Makefile.in
--- tin-1.7.9/src/Makefile.in	2005-06-20 11:31:57.000000000 +0200
+++ tin-1.7.9.new/src/Makefile.in	2005-06-21 20:55:36.613046616 +0200
@@ -421,7 +421,7 @@ clean :
 	@-$(RM) -f $(OFILES)
 	@-$(RM) -f $(OBJDIR)/parsdate.c
 	@-$(RM) -f LINT *.ln *.out
-	@-$(RM) -f makecfg$X makecfg$o tincfg.h
+	@-$(RM) -f makecfg$X makecfg$o tincfg.h options_menu.h
 	@-$(RM) -f core *~
 	@-$(RM) -f $(EXE)
 @PCREDIR_MAKE@	@-if test -r ../pcre/Makefile ; then $(CD) ../pcre && $(ECHO) "Cleaning Philip Hazel's Perl-compatible regular expressions library ..." && $(MAKE) @cf_cv_makeflags@ clean ; fi
diff -Nurp tin-1.7.9/src/options_menu.c tin-1.7.9.new/src/options_menu.c
--- tin-1.7.9/src/options_menu.c	2005-06-20 14:41:00.000000000 +0200
+++ tin-1.7.9.new/src/options_menu.c	2005-06-21 19:37:46.498989669 +0200
@@ -38,9 +38,9 @@
 #ifndef TIN_H
 #	include "tin.h"
 #endif /* !TIN_H */
-#ifndef TINTBL_H
+#ifndef TINCFG_H
 #	include "tincfg.h"
-#endif /* !TINTBL_H */
+#endif /* !TINCFG_H */
 #ifndef TCURSES_H
 #	include "tcurses.h"
 #endif /* !TCURSES_H */
@@ -85,10 +85,10 @@ static void unhighlight_option(enum opti
  */
 int
 option_row(
-	int option)
+	enum option_enum option)
 {
 	int i = 0;
-	int j = first_option_on_screen;
+	enum option_enum j = first_option_on_screen;
 
 	while (j < option) {
 		if (option_is_visible(j))
@@ -139,7 +139,7 @@ set_option_num(
  */
 t_bool
 option_is_visible(
-	int option)
+	enum option_enum option)
 {
 	switch (option) {
 #ifdef HAVE_COLOR
@@ -214,7 +214,7 @@ fmt_option_prompt(
 	char *dst,
 	size_t len,
 	t_bool editing,
-	int option)
+	enum option_enum option)
 {
 	char *buf;
 	size_t option_width = MAX(35, cCOLS / 2 - 9);
@@ -590,12 +590,12 @@ unhighlight_option(
  */
 void
 refresh_config_page(
-	int act_option)
+	enum option_enum act_option)
 {
-	static int last_option = 0;
+	static enum option_enum last_option = 0;
 	/* t_bool force_redraw = FALSE; */
 
-	if (act_option < 0) {	/* called by signal handler */
+	if (act_option == SIGNAL_HANDLER) {	/* called by signal handler */
 		/* force_redraw = TRUE; */
 		act_option = last_option;
 		set_last_option_on_screen(first_option_on_screen); /* terminal size may have changed */
diff -Nurp tin-1.7.9/src/prompt.c tin-1.7.9.new/src/prompt.c
--- tin-1.7.9/src/prompt.c	2005-06-20 11:31:57.000000000 +0200
+++ tin-1.7.9.new/src/prompt.c	2005-06-21 19:21:55.550531695 +0200
@@ -361,7 +361,7 @@ prompt_list(
  */
 t_bool
 prompt_option_on_off(
-	int option)
+	enum option_enum option)
 {
 	char prompt[LEN];
 	t_bool *variable = OPT_ON_OFF_list[option_table[option].var_index];
@@ -378,7 +378,7 @@ prompt_option_on_off(
  */
 t_bool
 prompt_option_list(
-	int option)
+	enum option_enum option)
 {
 	char prompt[LEN];
 	int *variable = option_table[option].variable;
@@ -400,7 +400,7 @@ prompt_option_list(
  */
 t_bool
 prompt_option_string(
-	int option) /* return value is always ignored */
+	enum option_enum option) /* return value is always ignored */
 {
 	char *variable = OPT_STRING_list[option_table[option].var_index];
 	char prompt[LEN];
@@ -421,7 +421,7 @@ prompt_option_string(
  */
 t_bool
 prompt_option_num(
-	int option) /* return value is always ignored */
+	enum option_enum option) /* return value is always ignored */
 {
 	char prompt[LEN];
 	char number[LEN];
@@ -453,7 +453,7 @@ prompt_option_num(
  */
 t_bool
 prompt_option_char(
-	int option) /* return value is always ignored */
+	enum option_enum option) /* return value is always ignored */
 {
 	char prompt[LEN];
 	char input[2];
diff -Nurp tin-1.7.9/src/search.c tin-1.7.9.new/src/search.c
--- tin-1.7.9/src/search.c	2005-06-20 11:31:57.000000000 +0200
+++ tin-1.7.9.new/src/search.c	2005-06-21 19:55:30.749505566 +0200
@@ -135,17 +135,16 @@ get_search_pattern(
 /*
  * called by config.c
  */
-int
+enum option_enum
 search_config(
 	t_bool forward,
 	t_bool repeat,
-	int current,
-	int last)
+	enum option_enum current,
+	enum option_enum last)
 {
 	char *pattern, *buf;
-	int n;
-	int incr;
-	int result = current;
+	enum option_enum n = current;
+	enum option_enum result = current;
 
 	if (!(pattern = get_search_pattern(&forward, repeat, _(txt_search_forwards), _(txt_search_backwards), tinrc.default_search_config, HIST_CONFIG_SEARCH)))
 		return result;
@@ -153,16 +152,14 @@ search_config(
 	if (tinrc.wildcard && !(compile_regex(pattern, &search_regex, PCRE_CASELESS)))
 		return result;
 
-	incr = forward ? 1 : -1;
-
-	current += incr;
-	n = current;
 	do {
-		if (n < 0)
+		if (n == 0 && !forward)
 			n = last;
 		else {
-			if (n > last)
+			if (n == last && forward)
 				n = 0;
+			else
+				n += forward ? 1 : -1;
 		}
 		/* search only visible options */
 		if (option_is_visible(n)) {
@@ -180,7 +177,6 @@ search_config(
 			}
 			free(buf);
 		}
-		n += incr;
 	} while (n != current);
 
 	clear_message();
diff -Nurp tin-1.7.9/src/signal.c tin-1.7.9.new/src/signal.c
--- tin-1.7.9/src/signal.c	2005-06-20 11:31:57.000000000 +0200
+++ tin-1.7.9.new/src/signal.c	2005-06-21 19:38:03.594037876 +0200
@@ -278,7 +278,7 @@ handle_resize(
 			break;
 
 		case cConfig:
-			refresh_config_page(-1);
+			refresh_config_page(SIGNAL_HANDLER);
 			break;
 
 		case cFilter:
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.