From 653d4f43a7280d8fb84173be37b0c4c518cf7a88 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 10 Jul 2013 19:17:47 -0700 Subject: [PATCH] * sysdep.c, term.c, termcap.c, terminal.c: Integer-related minor fixes. * sysdep.c (emacs_get_tty): Return void, since nobody uses the value. (emacs_set_tty): Now static. * sysdep.c (emacs_set_tty, tabs_safe_p, emacs_close): * term.c (tty_capable_p, tty_default_color_capabilities) (get_tty_terminal, term_mouse_movement) (handle_one_term_event, init_tty, maybe_fatal): * termcap.c (tgetst1, struct termcap_buffer, valid_filename_p) (tgetent, scan_file, name_match, compare_contin): * terminal.c (get_terminal): Use bool for boolean. * sysdep.c (init_system_name): Don't overflow stack on huge hostname. Prefer char to unsigned char if either will do. * term.c (OUTPUT, turn_on_face): Omit unnecessary casts to int. (tty_write_glyphs): Prefer int to unsigned. (produce_glyphless_glyph): Remove 2nd (unused) int arg. All callers changed. * termcap.c (tprint, main) [TEST]: Remove non-working test. --- src/ChangeLog | 21 ++++++++++ src/dispextern.h | 7 ++-- src/sysdep.c | 56 +++++++++++++------------ src/term.c | 47 ++++++++++----------- src/termcap.c | 104 ++++++++++++----------------------------------- src/termhooks.h | 2 +- src/terminal.c | 4 +- 7 files changed, 105 insertions(+), 136 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 1231c1adc14..cd109c17f18 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,24 @@ +2013-07-11 Paul Eggert + + * sysdep.c, term.c, termcap.c, terminal.c: Integer-related minor fixes. + * sysdep.c (emacs_get_tty): Return void, since nobody uses the value. + (emacs_set_tty): Now static. + * sysdep.c (emacs_set_tty, tabs_safe_p, emacs_close): + * term.c (tty_capable_p, tty_default_color_capabilities) + (get_tty_terminal, term_mouse_movement) + (handle_one_term_event, init_tty, maybe_fatal): + * termcap.c (tgetst1, struct termcap_buffer, valid_filename_p) + (tgetent, scan_file, name_match, compare_contin): + * terminal.c (get_terminal): + Use bool for boolean. + * sysdep.c (init_system_name): Don't overflow stack on huge hostname. + Prefer char to unsigned char if either will do. + * term.c (OUTPUT, turn_on_face): Omit unnecessary casts to int. + (tty_write_glyphs): Prefer int to unsigned. + (produce_glyphless_glyph): Remove 2nd (unused) int arg. + All callers changed. + * termcap.c (tprint, main) [TEST]: Remove non-working test. + 2013-07-10 Paul Eggert Port to C89. diff --git a/src/dispextern.h b/src/dispextern.h index 1dd96c6638d..e0d04231d3a 100644 --- a/src/dispextern.h +++ b/src/dispextern.h @@ -3298,7 +3298,7 @@ int image_ascent (struct image *, struct face *, struct glyph_slice *); void get_tty_size (int, int *, int *); void request_sigio (void); void unrequest_sigio (void); -int tabs_safe_p (int); +bool tabs_safe_p (int); void init_baud_rate (int); void init_sigio (int); void ignore_sigio (void); @@ -3470,11 +3470,12 @@ extern int string_cost (const char *); extern int per_line_cost (const char *); extern void calculate_costs (struct frame *); extern void produce_glyphs (struct it *); -extern int tty_capable_p (struct tty_display_info *, unsigned, unsigned long, unsigned long); +extern bool tty_capable_p (struct tty_display_info *, unsigned, + unsigned long, unsigned long); extern void set_tty_color_mode (struct tty_display_info *, struct frame *); extern struct terminal *get_named_tty (const char *); extern void create_tty_output (struct frame *); -extern struct terminal *init_tty (const char *, const char *, int); +extern struct terminal *init_tty (const char *, const char *, bool); extern void tty_append_glyph (struct it *); diff --git a/src/sysdep.c b/src/sysdep.c index 2f86b1f8bd5..653cf22b078 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -102,8 +102,8 @@ int _cdecl _getpid (void); #include "syssignal.h" #include "systime.h" -static int emacs_get_tty (int, struct emacs_tty *); -static int emacs_set_tty (int, struct emacs_tty *, int); +static void emacs_get_tty (int, struct emacs_tty *); +static int emacs_set_tty (int, struct emacs_tty *, bool); /* ULLONG_MAX is missing on Red Hat Linux 7.3; see Bug#11781. */ #ifndef ULLONG_MAX @@ -769,31 +769,26 @@ widen_foreground_group (int fd) /* Getting and setting emacs_tty structures. */ -/* Set *TC to the parameters associated with the terminal FD. - Return zero if all's well, or -1 if we ran into an error we - couldn't deal with. */ -int +/* Set *TC to the parameters associated with the terminal FD, + or clear it if the parameters are not available. */ +static void emacs_get_tty (int fd, struct emacs_tty *settings) { /* Retrieve the primary parameters - baud rate, character size, etcetera. */ #ifndef DOS_NT /* We have those nifty POSIX tcmumbleattr functions. */ memset (&settings->main, 0, sizeof (settings->main)); - if (tcgetattr (fd, &settings->main) < 0) - return -1; + tcgetattr (fd, &settings->main); #endif - - /* We have survived the tempest. */ - return 0; } /* Set the parameters of the tty on FD according to the contents of - *SETTINGS. If FLUSHP is non-zero, we discard input. - Return 0 if all went well, and -1 if anything failed. */ + *SETTINGS. If FLUSHP, discard input. + Return 0 if all went well, and -1 (setting errno) if anything failed. */ -int -emacs_set_tty (int fd, struct emacs_tty *settings, int flushp) +static int +emacs_set_tty (int fd, struct emacs_tty *settings, bool flushp) { /* Set the primary parameters - baud rate, character size, etcetera. */ #ifndef DOS_NT @@ -1118,10 +1113,10 @@ init_sys_modes (struct tty_display_info *tty_out) tty_out->term_initted = 1; } -/* Return nonzero if safe to use tabs in output. +/* Return true if safe to use tabs in output. At the time this is called, init_sys_modes has not been done yet. */ -int +bool tabs_safe_p (int fd) { struct emacs_tty etty; @@ -1375,8 +1370,10 @@ init_system_name (void) uname (&uts); Vsystem_name = build_string (uts.nodename); #else /* HAVE_GETHOSTNAME */ - unsigned int hostname_size = 256; - char *hostname = alloca (hostname_size); + char *hostname_alloc = NULL; + char hostname_buf[256]; + ptrdiff_t hostname_size = sizeof hostname_buf; + char *hostname = hostname_buf; /* Try to get the host name; if the buffer is too short, try again. Apparently, the only indication gethostname gives of @@ -1391,8 +1388,8 @@ init_system_name (void) if (strlen (hostname) < hostname_size - 1) break; - hostname_size <<= 1; - hostname = alloca (hostname_size); + hostname = hostname_alloc = xpalloc (hostname_alloc, &hostname_size, 1, + min (PTRDIFF_MAX, SIZE_MAX), 1); } #ifdef HAVE_SOCKETS /* Turn the hostname into the official, fully-qualified hostname. @@ -1437,7 +1434,13 @@ init_system_name (void) } if (it) { - hostname = alloca (strlen (it->ai_canonname) + 1); + ptrdiff_t len = strlen (it->ai_canonname); + if (hostname_size <= len) + { + hostname_size = len + 1; + hostname = hostname_alloc = xrealloc (hostname_alloc, + hostname_size); + } strcpy (hostname, it->ai_canonname); } freeaddrinfo (res); @@ -1484,10 +1487,11 @@ init_system_name (void) } #endif /* HAVE_SOCKETS */ Vsystem_name = build_string (hostname); + xfree (hostname_alloc); #endif /* HAVE_GETHOSTNAME */ { - unsigned char *p; - for (p = SDATA (Vsystem_name); *p; p++) + char *p; + for (p = SSDATA (Vsystem_name); *p; p++) if (*p == ' ' || *p == '\t') *p = '-'; } @@ -2200,8 +2204,8 @@ emacs_fopen (char const *file, char const *mode) int emacs_close (int fd) { - int did_retry = 0; - register int rtnval; + bool did_retry = 0; + int rtnval; while ((rtnval = close (fd)) == -1 && (errno == EINTR)) diff --git a/src/term.c b/src/term.c index 39d143564c6..00a3f6837ee 100644 --- a/src/term.c +++ b/src/term.c @@ -71,13 +71,13 @@ static void tty_turn_off_highlight (struct tty_display_info *); static void tty_show_cursor (struct tty_display_info *); static void tty_hide_cursor (struct tty_display_info *); static void tty_background_highlight (struct tty_display_info *tty); -static struct terminal *get_tty_terminal (Lisp_Object, int); +static struct terminal *get_tty_terminal (Lisp_Object, bool); static void clear_tty_hooks (struct terminal *terminal); static void set_tty_hooks (struct terminal *terminal); static void dissociate_if_controlling_tty (int fd); static void delete_tty (struct terminal *); -static _Noreturn void maybe_fatal (int must_succeed, struct terminal *terminal, - const char *str1, const char *str2, ...) +static _Noreturn void maybe_fatal (bool, struct terminal *, + const char *, const char *, ...) ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5); static _Noreturn void vfatal (const char *str, va_list ap) ATTRIBUTE_FORMAT_PRINTF (1, 0); @@ -85,8 +85,7 @@ static _Noreturn void vfatal (const char *str, va_list ap) #define OUTPUT(tty, a) \ emacs_tputs ((tty), a, \ - (int) (FRAME_LINES (XFRAME (selected_frame)) \ - - curY (tty)), \ + FRAME_LINES (XFRAME (selected_frame)) - curY (tty), \ cmputc) #define OUTPUT1(tty, a) emacs_tputs ((tty), a, 1, cmputc) @@ -695,7 +694,7 @@ tty_write_glyphs (struct frame *f, struct glyph *string, int len) { unsigned char *conversion_buffer; struct coding_system *coding; - size_t n, stringlen; + int n, stringlen; struct tty_display_info *tty = FRAME_TTY (f); @@ -1431,7 +1430,7 @@ static void append_glyph (struct it *); static void append_composite_glyph (struct it *); static void produce_composite_glyph (struct it *); static void append_glyphless_glyph (struct it *, int, const char *); -static void produce_glyphless_glyph (struct it *, int, Lisp_Object); +static void produce_glyphless_glyph (struct it *, Lisp_Object); /* Append glyphs to IT's glyph_row. Called from produce_glyphs for terminal frames if IT->glyph_row != NULL. IT->char_to_display is @@ -1551,7 +1550,7 @@ produce_glyphs (struct it *it) if (it->what == IT_GLYPHLESS) { - produce_glyphless_glyph (it, 0, Qnil); + produce_glyphless_glyph (it, Qnil); goto done; } @@ -1620,7 +1619,7 @@ produce_glyphs (struct it *it) Lisp_Object acronym = lookup_glyphless_char_display (-1, it); eassert (it->what == IT_GLYPHLESS); - produce_glyphless_glyph (it, 1, acronym); + produce_glyphless_glyph (it, acronym); } } @@ -1794,14 +1793,12 @@ append_glyphless_glyph (struct it *it, int face_id, const char *str) the character. See the description of enum glyphless_display_method in dispextern.h for the details. - FOR_NO_FONT is nonzero if and only if this is for a character that - is not supported by the coding system of the terminal. ACRONYM, if - non-nil, is an acronym string for the character. + ACRONYM, if non-nil, is an acronym string for the character. The glyphs actually produced are of type CHAR_GLYPH. */ static void -produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym) +produce_glyphless_glyph (struct it *it, Lisp_Object acronym) { int face_id; int len; @@ -1968,7 +1965,7 @@ turn_on_face (struct frame *f, int face_id) ts = tty->standout_mode ? tty->TS_set_background : tty->TS_set_foreground; if (fg >= 0 && ts) { - p = tparam (ts, NULL, 0, (int) fg, 0, 0, 0); + p = tparam (ts, NULL, 0, fg, 0, 0, 0); OUTPUT (tty, p); xfree (p); } @@ -1976,7 +1973,7 @@ turn_on_face (struct frame *f, int face_id) ts = tty->standout_mode ? tty->TS_set_foreground : tty->TS_set_background; if (bg >= 0 && ts) { - p = tparam (ts, NULL, 0, (int) bg, 0, 0, 0); + p = tparam (ts, NULL, 0, bg, 0, 0, 0); OUTPUT (tty, p); xfree (p); } @@ -2027,11 +2024,11 @@ turn_off_face (struct frame *f, int face_id) } -/* Return non-zero if the terminal on frame F supports all of the +/* Return true if the terminal on frame F supports all of the capabilities in CAPS simultaneously, with foreground and background colors FG and BG. */ -int +bool tty_capable_p (struct tty_display_info *tty, unsigned int caps, unsigned long fg, unsigned long bg) { @@ -2099,7 +2096,7 @@ static char *default_set_background; /* Save or restore the default color-related capabilities of this terminal. */ static void -tty_default_color_capabilities (struct tty_display_info *tty, int save) +tty_default_color_capabilities (struct tty_display_info *tty, bool save) { if (save) @@ -2209,7 +2206,7 @@ set_tty_color_mode (struct tty_display_info *tty, struct frame *f) /* Return the tty display object specified by TERMINAL. */ static struct terminal * -get_tty_terminal (Lisp_Object terminal, int throw) +get_tty_terminal (Lisp_Object terminal, bool throw) { struct terminal *t = get_terminal (terminal, throw); @@ -2519,7 +2516,7 @@ tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row, cursor_to (f, save_y, save_x); } -static int +static bool term_mouse_movement (FRAME_PTR frame, Gpm_Event *event) { /* Has the mouse moved off the glyph it was on at the last sighting? */ @@ -2649,7 +2646,7 @@ handle_one_term_event (struct tty_display_info *tty, Gpm_Event *event, struct in { struct frame *f = XFRAME (tty->top_frame); struct input_event ie; - int do_help = 0; + bool do_help = 0; int count = 0; EVENT_INIT (ie); @@ -2934,7 +2931,7 @@ dissociate_if_controlling_tty (int fd) If MUST_SUCCEED is true, then all errors are fatal. */ struct terminal * -init_tty (const char *name, const char *terminal_type, int must_succeed) +init_tty (const char *name, const char *terminal_type, bool must_succeed) { char *area = NULL; char **address = &area; @@ -2942,7 +2939,7 @@ init_tty (const char *name, const char *terminal_type, int must_succeed) int status; struct tty_display_info *tty = NULL; struct terminal *terminal = NULL; - int ctty = 0; /* 1 if asked to open controlling tty. */ + bool ctty = 0; /* True if asked to open controlling tty. */ if (!terminal_type) maybe_fatal (must_succeed, 0, @@ -3412,10 +3409,10 @@ vfatal (const char *str, va_list ap) /* Auxiliary error-handling function for init_tty. Delete TERMINAL, then call error or fatal with str1 or str2, - respectively, according to whether MUST_SUCCEED is zero or not. */ + respectively, according to whether MUST_SUCCEED is true. */ static void -maybe_fatal (int must_succeed, struct terminal *terminal, +maybe_fatal (bool must_succeed, struct terminal *terminal, const char *str1, const char *str2, ...) { va_list ap; diff --git a/src/termcap.c b/src/termcap.c index 7256eef9e81..5f731077463 100644 --- a/src/termcap.c +++ b/src/termcap.c @@ -213,8 +213,8 @@ tgetst1 (char *ptr, char **area) abbreviation expansion makes that effort a little more hairy than its worth; this is cleaner. */ { - register int last_p_param = 0; - int remove_p_params = 1; + int last_p_param = 0; + bool remove_p_params = 1; struct { char *beg; int len; } cut[11]; for (cut[0].beg = p = ret; p < r - 3; p++) @@ -318,26 +318,26 @@ struct termcap_buffer char *beg; ptrdiff_t size; char *ptr; - int ateof; + bool ateof; ptrdiff_t full; }; /* Forward declarations of static functions. */ -static int scan_file (char *str, int fd, register struct termcap_buffer *bufp); -static char *gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end); -static int compare_contin (register char *str1, register char *str2); -static int name_match (char *line, char *name); +static bool scan_file (char *, int, struct termcap_buffer *); +static char *gobble_line (int, struct termcap_buffer *, char *); +static bool compare_contin (char *, char *); +static bool name_match (char *, char *); -#ifdef MSDOS /* MW, May 1993 */ -static int +static bool valid_filename_p (char *fn) { +#ifdef MSDOS return *fn == '/' || fn[1] == ':'; -} #else -#define valid_filename_p(fn) (*(fn) == '/') + return *fn == '/'; #endif +} /* Find the termcap entry data for terminal type NAME and store it in the block that BP points to. @@ -360,10 +360,10 @@ tgetent (char *bp, const char *name) char *tc_search_point; char *term; ptrdiff_t malloc_size = 0; - register int c; + int c; char *tcenv = NULL; /* TERMCAP value, if it contains :tc=. */ char *indirect = NULL; /* Terminal type in :tc= in TERMCAP value. */ - int filep; + bool filep; #ifdef INTERNAL_TERMINAL /* For the internal terminal we don't want to read any termcap file, @@ -510,10 +510,10 @@ tgetent (char *bp, const char *name) Return 1 if successful, with that line in BUFP, or 0 if no entry is found in the file. */ -static int -scan_file (char *str, int fd, register struct termcap_buffer *bufp) +static bool +scan_file (char *str, int fd, struct termcap_buffer *bufp) { - register char *end; + char *end; bufp->ptr = bufp->beg; bufp->full = 0; @@ -544,13 +544,13 @@ scan_file (char *str, int fd, register struct termcap_buffer *bufp) return 0; } -/* Return nonzero if NAME is one of the names specified +/* Return true if NAME is one of the names specified by termcap entry LINE. */ -static int +static bool name_match (char *line, char *name) { - register char *tem; + char *tem; if (!compare_contin (line, name)) return 1; @@ -562,18 +562,18 @@ name_match (char *line, char *name) return 0; } -static int -compare_contin (register char *str1, register char *str2) +static bool +compare_contin (char *str1, char *str2) { - register int c1, c2; while (1) { - c1 = *str1++; - c2 = *str2++; + int c1 = *str1++; + int c2 = *str2++; while (c1 == '\\' && *str1 == '\n') { str1++; - while ((c1 = *str1++) == ' ' || c1 == '\t'); + while ((c1 = *str1++) == ' ' || c1 == '\t') + continue; } if (c2 == '\0') { @@ -647,57 +647,3 @@ gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end) } return end + 1; } - -#ifdef TEST - -#include - -static void -tprint (char *cap) -{ - char *x = tgetstr (cap, 0); - register char *y; - - printf ("%s: ", cap); - if (x) - { - for (y = x; *y; y++) - if (*y <= ' ' || *y == 0177) - printf ("\\%0o", *y); - else - putchar (*y); - free (x); - } - else - printf ("none"); - putchar ('\n'); -} - -int -main (int argc, char **argv) -{ - char *term; - char *buf; - - term = argv[1]; - printf ("TERM: %s\n", term); - - buf = (char *) tgetent (0, term); - if ((int) buf <= 0) - { - printf ("No entry.\n"); - return 0; - } - - printf ("Entry: %s\n", buf); - - tprint ("cm"); - tprint ("AL"); - - printf ("co: %d\n", tgetnum ("co")); - printf ("am: %d\n", tgetflag ("am")); - - return 0; -} - -#endif /* TEST */ diff --git a/src/termhooks.h b/src/termhooks.h index 4f3fa9cb47f..0190478c254 100644 --- a/src/termhooks.h +++ b/src/termhooks.h @@ -643,7 +643,7 @@ extern struct terminal *terminal_list; (((d)->type != output_termcap && (d)->type != output_msdos_raw) \ || (d)->display_info.tty->input) -extern struct terminal *get_terminal (Lisp_Object terminal, int); +extern struct terminal *get_terminal (Lisp_Object terminal, bool); extern struct terminal *create_terminal (void); extern void delete_terminal (struct terminal *); diff --git a/src/terminal.c b/src/terminal.c index c99c39c64ae..c55fd4eb077 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -199,11 +199,11 @@ ins_del_lines (struct frame *f, int vpos, int n) /* Return the terminal object specified by TERMINAL. TERMINAL may be a terminal object, a frame, or nil for the terminal device of the - current frame. If THROW is zero, return NULL for failure, + current frame. If THROW is false, return NULL for failure, otherwise throw an error. */ struct terminal * -get_terminal (Lisp_Object terminal, int throw) +get_terminal (Lisp_Object terminal, bool throw) { struct terminal *result = NULL; -- 2.39.2