=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/less/cmdbuf.c,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- src/usr.bin/less/cmdbuf.c 2014/04/25 13:38:21 1.7 +++ src/usr.bin/less/cmdbuf.c 2015/11/05 22:08:43 1.8 @@ -6,8 +6,11 @@ * * For more information, see the README file. */ +/* + * Modified for use with illumos. + * Copyright 2014 Garrett D'Amore + */ - /* * Functions which manipulate the command buffer. * Used only by command() and related functions. @@ -16,9 +19,7 @@ #include "less.h" #include "cmd.h" #include "charset.h" -#if HAVE_STAT #include -#endif extern int sc_width; extern int utf_mode; @@ -31,8 +32,7 @@ static int literal; /* Next input char should not be interpreted */ static int updown_match = -1; /* Prefix length in up/down movement */ -#if TAB_COMPLETE_FILENAME -static int cmd_complete(); +static int cmd_complete(int); /* * These variables are statics used by cmd_complete. */ @@ -42,22 +42,17 @@ static char *tk_ipoint; static char *tk_trial; static struct textlist tk_tlist; -#endif -static int cmd_left(); -static int cmd_right(); +static int cmd_left(void); +static int cmd_right(void); -#if SPACES_IN_FILENAMES -public char openquote = '"'; -public char closequote = '"'; -#endif +char openquote = '"'; +char closequote = '"'; -#if CMD_HISTORY - /* History file */ -#define HISTFILE_FIRST_LINE ".less-history-file:" -#define HISTFILE_SEARCH_SECTION ".search" -#define HISTFILE_SHELL_SECTION ".shell" +#define HISTFILE_FIRST_LINE ".less-history-file:" +#define HISTFILE_SEARCH_SECTION ".search" +#define HISTFILE_SHELL_SECTION ".shell" /* * A mlist structure represents a command history. @@ -74,31 +69,18 @@ /* * These are the various command histories that exist. */ -struct mlist mlist_search = +struct mlist mlist_search = { &mlist_search, &mlist_search, &mlist_search, NULL, 0 }; -public void * constant ml_search = (void *) &mlist_search; +void * const ml_search = (void *) &mlist_search; -struct mlist mlist_examine = +struct mlist mlist_examine = { &mlist_examine, &mlist_examine, &mlist_examine, NULL, 0 }; -public void * constant ml_examine = (void *) &mlist_examine; +void * const ml_examine = (void *) &mlist_examine; -#if SHELL_ESCAPE || PIPEC -struct mlist mlist_shell = +struct mlist mlist_shell = { &mlist_shell, &mlist_shell, &mlist_shell, NULL, 0 }; -public void * constant ml_shell = (void *) &mlist_shell; -#endif +void * const ml_shell = (void *) &mlist_shell; -#else /* CMD_HISTORY */ - -/* If CMD_HISTORY is off, these are just flags. */ -public void * constant ml_search = (void *)1; -public void * constant ml_examine = (void *)2; -#if SHELL_ESCAPE || PIPEC -public void * constant ml_shell = (void *)3; -#endif - -#endif /* CMD_HISTORY */ - /* * History for the current command. */ @@ -113,8 +95,8 @@ /* * Reset command buffer (to empty). */ - public void -cmd_reset() +void +cmd_reset(void) { cp = cmdbuf; *cp = '\0'; @@ -128,8 +110,8 @@ /* * Clear command line. */ - public void -clear_cmd() +void +clear_cmd(void) { cmd_col = prompt_col = 0; cmd_mbc_buf_len = 0; @@ -139,33 +121,26 @@ /* * Display a string, usually as a prompt for input into the command buffer. */ - public void -cmd_putstr(s) - char *s; +void +cmd_putstr(char *s) { LWCHAR prev_ch = 0; LWCHAR ch; char *endline = s + strlen(s); - while (*s != '\0') - { + while (*s != '\0') { char *ns = s; ch = step_char(&ns, +1, endline); while (s < ns) putchr(*s++); - if (!utf_mode) - { + if (!utf_mode) { cmd_col++; prompt_col++; - } -#if !SMALL - else if (!is_composing_char(ch) && - !is_combining_char(prev_ch, ch)) - { + } else if (!is_composing_char(ch) && + !is_combining_char(prev_ch, ch)) { int width = is_wide_char(ch) ? 2 : 1; cmd_col += width; prompt_col += width; } -#endif /* !SMALL */ prev_ch = ch; } } @@ -173,15 +148,14 @@ /* * How many characters are in the command buffer? */ - public int -len_cmdbuf() +int +len_cmdbuf(void) { char *s = cmdbuf; char *endline = s + strlen(s); int len = 0; - while (*s != '\0') - { + while (*s != '\0') { step_char(&s, +1, endline); len++; } @@ -191,69 +165,51 @@ /* * Common part of cmd_step_right() and cmd_step_left(). */ - static char * -cmd_step_common(p, ch, len, pwidth, bswidth) - char *p; - LWCHAR ch; - int len; - int *pwidth; - int *bswidth; +static char * +cmd_step_common(char *p, LWCHAR ch, int len, int *pwidth, int *bswidth) { char *pr; - if (len == 1) - { - pr = prchar((int) ch); - if (pwidth != NULL || bswidth != NULL) - { + if (len == 1) { + pr = prchar((int)ch); + if (pwidth != NULL || bswidth != NULL) { int len = strlen(pr); if (pwidth != NULL) *pwidth = len; if (bswidth != NULL) *bswidth = len; } - } -#if !SMALL - else - { + } else { pr = prutfchar(ch); - if (pwidth != NULL || bswidth != NULL) - { - if (is_composing_char(ch)) - { + if (pwidth != NULL || bswidth != NULL) { + if (is_composing_char(ch)) { if (pwidth != NULL) *pwidth = 0; if (bswidth != NULL) *bswidth = 0; - } else if (is_ubin_char(ch)) - { + } else if (is_ubin_char(ch)) { int len = strlen(pr); if (pwidth != NULL) *pwidth = len; if (bswidth != NULL) *bswidth = len; - } else - { + } else { LWCHAR prev_ch = step_char(&p, -1, cmdbuf); - if (is_combining_char(prev_ch, ch)) - { + if (is_combining_char(prev_ch, ch)) { if (pwidth != NULL) *pwidth = 0; if (bswidth != NULL) *bswidth = 0; - } else - { + } else { if (pwidth != NULL) *pwidth = is_wide_char(ch) - ? 2 - : 1; + ? 2 : 1; if (bswidth != NULL) *bswidth = 1; } } } } -#endif /* !SMALL */ return (pr); } @@ -261,47 +217,39 @@ /* * Step a pointer one character right in the command buffer. */ - static char * -cmd_step_right(pp, pwidth, bswidth) - char **pp; - int *pwidth; - int *bswidth; +static char * +cmd_step_right(char **pp, int *pwidth, int *bswidth) { char *p = *pp; LWCHAR ch = step_char(pp, +1, p + strlen(p)); - return cmd_step_common(p, ch, *pp - p, pwidth, bswidth); + return (cmd_step_common(p, ch, *pp - p, pwidth, bswidth)); } /* * Step a pointer one character left in the command buffer. */ - static char * -cmd_step_left(pp, pwidth, bswidth) - char **pp; - int *pwidth; - int *bswidth; +static char * +cmd_step_left(char **pp, int *pwidth, int *bswidth) { char *p = *pp; LWCHAR ch = step_char(pp, -1, cmdbuf); - return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth); + return (cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth)); } /* * Repaint the line from cp onwards. * Then position the cursor just after the char old_cp (a pointer into cmdbuf). */ - static void -cmd_repaint(old_cp) - char *old_cp; +static void +cmd_repaint(char *old_cp) { /* * Repaint the line from the current position. */ clear_eol(); - while (*cp != '\0') - { + while (*cp != '\0') { char *np = cp; int width; char *pr = cmd_step_right(&np, &width, NULL); @@ -311,8 +259,7 @@ putstr(pr); cmd_col += width; } - while (*cp != '\0') - { + while (*cp != '\0') { char *np = cp; int width; char *pr = cmd_step_right(&np, &width, NULL); @@ -333,11 +280,10 @@ * Put the cursor at "home" (just after the prompt), * and set cp to the corresponding char in cmdbuf. */ - static void -cmd_home() +static void +cmd_home(void) { - while (cmd_col > prompt_col) - { + while (cmd_col > prompt_col) { int width, bswidth; cmd_step_left(&cp, &width, &bswidth); @@ -352,8 +298,8 @@ /* * Shift the cmdbuf display left a half-screen. */ - static void -cmd_lshift() +static void +cmd_lshift(void) { char *s; char *save_cp; @@ -365,14 +311,12 @@ */ s = cmdbuf + cmd_offset; cols = 0; - while (cols < (sc_width - prompt_col) / 2 && *s != '\0') - { + while (cols < (sc_width - prompt_col) / 2 && *s != '\0') { int width; cmd_step_right(&s, &width, NULL); cols += width; } - while (*s != '\0') - { + while (*s != '\0') { int width; char *ns = s; cmd_step_right(&ns, &width, NULL); @@ -390,8 +334,8 @@ /* * Shift the cmdbuf display right a half-screen. */ - static void -cmd_rshift() +static void +cmd_rshift(void) { char *s; char *save_cp; @@ -404,8 +348,7 @@ */ s = cmdbuf + cmd_offset; cols = 0; - while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf) - { + while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf) { int width; cmd_step_left(&s, &width, NULL); cols += width; @@ -420,15 +363,14 @@ /* * Move cursor right one character. */ - static int -cmd_right() +static int +cmd_right(void) { char *pr; char *ncp; int width; - - if (*cp == '\0') - { + + if (*cp == '\0') { /* Already at the end of the line. */ return (CC_OK); } @@ -441,8 +383,7 @@ cp = ncp; cmd_col += width; putstr(pr); - while (*cp != '\0') - { + while (*cp != '\0') { pr = cmd_step_right(&ncp, &width, NULL); if (width > 0) break; @@ -455,20 +396,18 @@ /* * Move cursor left one character. */ - static int -cmd_left() +static int +cmd_left(void) { char *ncp; int width, bswidth; - - if (cp <= cmdbuf) - { + + if (cp <= cmdbuf) { /* Already at the beginning of the line */ return (CC_OK); } ncp = cp; - while (ncp > cmdbuf) - { + while (ncp > cmdbuf) { cmd_step_left(&ncp, &width, &bswidth); if (width > 0) break; @@ -485,20 +424,17 @@ /* * Insert a char into the command buffer, at the current position. */ - static int -cmd_ichar(cs, clen) - char *cs; - int clen; +static int +cmd_ichar(char *cs, int clen) { char *s; - - if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1) - { + + if (strlen(cmdbuf) + clen >= sizeof (cmdbuf)-1) { /* No room in the command buffer for another char. */ - bell(); + ring_bell(); return (CC_ERROR); } - + /* * Make room for the new character (shift the tail of the buffer right). */ @@ -522,14 +458,13 @@ * Backspace in the command buffer. * Delete the char to the left of the cursor. */ - static int -cmd_erase() +static int +cmd_erase(void) { - register char *s; + char *s; int clen; - if (cp == cmdbuf) - { + if (cp == cmdbuf) { /* * Backspace past beginning of the buffer: * this usually means abort the command. @@ -546,8 +481,7 @@ /* * Remove the char from the buffer (shift the buffer left). */ - for (s = cp; ; s++) - { + for (s = cp; ; s++) { s[0] = s[clen]; if (s[0] == '\0') break; @@ -558,7 +492,7 @@ */ updown_match = -1; cmd_repaint(cp); - + /* * We say that erasing the entire command string causes us * to abort the current command, if CF_QUIT_ON_ERASE is set. @@ -571,11 +505,10 @@ /* * Delete the char under the cursor. */ - static int -cmd_delete() +static int +cmd_delete(void) { - if (*cp == '\0') - { + if (*cp == '\0') { /* At end of string; there is no char under the cursor. */ return (CC_OK); } @@ -590,19 +523,17 @@ /* * Delete the "word" to the left of the cursor. */ - static int -cmd_werase() +static int +cmd_werase(void) { - if (cp > cmdbuf && cp[-1] == ' ') - { + if (cp > cmdbuf && cp[-1] == ' ') { /* * If the char left of cursor is a space, * erase all the spaces left of cursor (to the first non-space). */ while (cp > cmdbuf && cp[-1] == ' ') (void) cmd_erase(); - } else - { + } else { /* * If the char left of cursor is not a space, * erase all the nonspaces left of cursor (the whole "word"). @@ -616,19 +547,17 @@ /* * Delete the "word" under the cursor. */ - static int -cmd_wdelete() +static int +cmd_wdelete(void) { - if (*cp == ' ') - { + if (*cp == ' ') { /* * If the char under the cursor is a space, * delete it and all the spaces right of cursor. */ while (*cp == ' ') (void) cmd_delete(); - } else - { + } else { /* * If the char under the cursor is not a space, * delete it and all nonspaces right of cursor (the whole word). @@ -642,11 +571,10 @@ /* * Delete all chars in the command buffer. */ - static int -cmd_kill() +static int +cmd_kill(void) { - if (cmdbuf[0] == '\0') - { + if (cmdbuf[0] == '\0') { /* Buffer is already empty; abort the current command. */ return (CC_QUIT); } @@ -668,63 +596,52 @@ /* * Select an mlist structure to be the current command history. */ - public void -set_mlist(mlist, cmdflags) - void *mlist; - int cmdflags; +void +set_mlist(void *mlist, int cmdflags) { -#if CMD_HISTORY - curr_mlist = (struct mlist *) mlist; + curr_mlist = (struct mlist *)mlist; curr_cmdflags = cmdflags; /* Make sure the next up-arrow moves to the last string in the mlist. */ if (curr_mlist != NULL) curr_mlist->curr_mp = curr_mlist; -#endif } -#if CMD_HISTORY /* * Move up or down in the currently selected command history list. * Only consider entries whose first updown_match chars are equal to * cmdbuf's corresponding chars. */ - static int -cmd_updown(action) - int action; +static int +cmd_updown(int action) { char *s; struct mlist *ml; - - if (curr_mlist == NULL) - { + + if (curr_mlist == NULL) { /* * The current command has no history list. */ - bell(); + ring_bell(); return (CC_OK); } - if (updown_match < 0) - { + if (updown_match < 0) { updown_match = cp - cmdbuf; } /* * Find the next history entry which matches. */ - for (ml = curr_mlist->curr_mp;;) - { + for (ml = curr_mlist->curr_mp; ; ) { ml = (action == EC_UP) ? ml->prev : ml->next; - if (ml == curr_mlist) - { + if (ml == curr_mlist) { /* * We reached the end (or beginning) of the list. */ break; } - if (strncmp(cmdbuf, ml->string, updown_match) == 0) - { + if (strncmp(cmdbuf, ml->string, updown_match) == 0) { /* * This entry matches; stop here. * Copy the entry into cmdbuf and echo it on the screen. @@ -735,8 +652,8 @@ s = ""; cmd_home(); clear_eol(); - strlcpy(cmdbuf, s, sizeof(cmdbuf)); - for (cp = cmdbuf; *cp != '\0'; ) + strlcpy(cmdbuf, s, sizeof (cmdbuf)); + for (cp = cmdbuf; *cp != '\0'; ) cmd_right(); return (CC_OK); } @@ -744,22 +661,18 @@ /* * We didn't find a history entry that matches. */ - bell(); + ring_bell(); return (CC_OK); } -#endif /* * Add a string to a history list. */ - public void -cmd_addhist(mlist, cmd) - struct mlist *mlist; - char *cmd; +void +cmd_addhist(struct mlist *mlist, const char *cmd) { -#if CMD_HISTORY struct mlist *ml; - + /* * Don't save a trivial command. */ @@ -771,13 +684,12 @@ * last command in the history. */ ml = mlist->prev; - if (ml == mlist || strcmp(ml->string, cmd) != 0) - { + if (ml == mlist || strcmp(ml->string, cmd) != 0) { /* * Did not find command in history. * Save the command and put it at the end of the history list. */ - ml = (struct mlist *) ecalloc(1, sizeof(struct mlist)); + ml = ecalloc(1, sizeof (struct mlist)); ml->string = save(cmd); ml->next = mlist; ml->prev = mlist->prev; @@ -789,17 +701,15 @@ * Thus, an UPARROW will always retrieve the previous command. */ mlist->curr_mp = ml->next; -#endif } /* * Accept the command in the command buffer. * Add it to the currently selected history list. */ - public void -cmd_accept() +void +cmd_accept(void) { -#if CMD_HISTORY /* * Nothing to do if there is no currently selected history list. */ @@ -807,7 +717,6 @@ return; cmd_addhist(curr_mlist, cmdbuf); curr_mlist->modified = 1; -#endif } /* @@ -818,42 +727,32 @@ * CC_OK Line edit function done. * CC_QUIT The char requests the current command to be aborted. */ - static int -cmd_edit(c) - int c; +static int +cmd_edit(int c) { int action; int flags; -#if TAB_COMPLETE_FILENAME #define not_in_completion() in_completion = 0 -#else -#define not_in_completion() -#endif - + /* * See if the char is indeed a line-editing command. */ flags = 0; -#if CMD_HISTORY if (curr_mlist == NULL) /* * No current history; don't accept history manipulation cmds. */ flags |= EC_NOHISTORY; -#endif -#if TAB_COMPLETE_FILENAME if (curr_mlist == ml_search) /* * In a search command; don't accept file-completion cmds. */ flags |= EC_NOCOMPLETE; -#endif action = editchar(c, flags); - switch (action) - { + switch (action) { case EC_RIGHT: not_in_completion(); return (cmd_right()); @@ -910,18 +809,14 @@ case EC_LITERAL: literal = 1; return (CC_OK); -#if CMD_HISTORY case EC_UP: case EC_DOWN: not_in_completion(); return (cmd_updown(action)); -#endif -#if TAB_COMPLETE_FILENAME case EC_F_COMPLETE: case EC_B_COMPLETE: case EC_EXPAND: return (cmd_complete(action)); -#endif case EC_NOACTION: return (CC_OK); default: @@ -930,26 +825,22 @@ } } -#if TAB_COMPLETE_FILENAME /* * Insert a string into the command buffer, at the current position. */ - static int -cmd_istr(str) - char *str; +static int +cmd_istr(char *str) { char *s; int action; char *endline = str + strlen(str); - - for (s = str; *s != '\0'; ) - { + + for (s = str; *s != '\0'; ) { char *os = s; step_char(&s, +1, endline); action = cmd_ichar(os, s - os); - if (action != CC_OK) - { - bell(); + if (action != CC_OK) { + ring_bell(); return (action); } } @@ -962,52 +853,33 @@ * Return pointer to the beginning of the word and put the * cursor at the end of the word. */ - static char * -delimit_word() +static char * +delimit_word(void) { char *word; -#if SPACES_IN_FILENAMES char *p; int delim_quoted = 0; int meta_quoted = 0; char *esc = get_meta_escape(); int esclen = strlen(esc); -#endif - + /* * Move cursor to end of word. */ - if (*cp != ' ' && *cp != '\0') - { + if (*cp != ' ' && *cp != '\0') { /* * Cursor is on a nonspace. * Move cursor right to the next space. */ while (*cp != ' ' && *cp != '\0') cmd_right(); - } else if (cp > cmdbuf && cp[-1] != ' ') - { - /* - * Cursor is on a space, and char to the left is a nonspace. - * We're already at the end of the word. - */ - ; -#if 0 - } else - { - /* - * Cursor is on a space and char to the left is a space. - * Huh? There's no word here. - */ - return (NULL); -#endif } + /* * Find the beginning of the word which the cursor is in. */ if (cp == cmdbuf) return (NULL); -#if SPACES_IN_FILENAMES /* * If we have an unbalanced quote (that is, an open quote * without a corresponding close quote), we return everything @@ -1018,48 +890,41 @@ break; if (word >= cp) return (cp); - for (p = cmdbuf; p < cp; p++) - { - if (meta_quoted) - { + for (p = cmdbuf; p < cp; p++) { + if (meta_quoted) { meta_quoted = 0; } else if (esclen > 0 && p + esclen < cp && - strncmp(p, esc, esclen) == 0) - { + strncmp(p, esc, esclen) == 0) { meta_quoted = 1; p += esclen - 1; - } else if (delim_quoted) - { + } else if (delim_quoted) { if (*p == closequote) delim_quoted = 0; - } else /* (!delim_quoted) */ - { + } else { /* (!delim_quoted) */ if (*p == openquote) delim_quoted = 1; else if (*p == ' ') word = p+1; } } -#endif return (word); } /* * Set things up to enter completion mode. - * Expand the word under the cursor into a list of filenames + * Expand the word under the cursor into a list of filenames * which start with that word, and set tk_text to that list. */ - static void -init_compl() +static void +init_compl(void) { char *word; char c; - + /* * Get rid of any previous tk_text. */ - if (tk_text != NULL) - { + if (tk_text != NULL) { free(tk_text); tk_text = NULL; } @@ -1079,8 +944,8 @@ */ if (tk_original != NULL) free(tk_original); - tk_original = (char *) ecalloc(cp-word+1, sizeof(char)); - strncpy(tk_original, word, cp-word); + tk_original = ecalloc(cp-word+1, sizeof (char)); + (void) strncpy(tk_original, word, cp-word); /* * Get the expanded filename. * This may result in a single filename, or @@ -1088,20 +953,13 @@ */ c = *cp; *cp = '\0'; - if (*word != openquote) - { + if (*word != openquote) { tk_text = fcomplete(word); - } else - { -#if MSDOS_COMPILER - char *qword = NULL; -#else + } else { char *qword = shell_quote(word+1); -#endif - if (qword == NULL) + if (qword == NULL) { tk_text = fcomplete(word+1); - else - { + } else { tk_text = fcomplete(qword); free(qword); } @@ -1112,13 +970,10 @@ /* * Return the next word in the current completion list. */ - static char * -next_compl(action, prev) - int action; - char *prev; +static char * +next_compl(int action, char *prev) { - switch (action) - { + switch (action) { case EC_F_COMPLETE: return (forw_textlist(&tk_tlist, prev)); case EC_B_COMPLETE: @@ -1134,57 +989,50 @@ * remembers whether this call is the first time (create the list), * or a subsequent time (step thru the list). */ - static int -cmd_complete(action) - int action; +static int +cmd_complete(int action) { char *s; - if (!in_completion || action == EC_EXPAND) - { + if (!in_completion || action == EC_EXPAND) { /* - * Expand the word under the cursor and - * use the first word in the expansion + * Expand the word under the cursor and + * use the first word in the expansion * (or the entire expansion if we're doing EC_EXPAND). */ init_compl(); - if (tk_text == NULL) - { - bell(); + if (tk_text == NULL) { + ring_bell(); return (CC_OK); } - if (action == EC_EXPAND) - { + if (action == EC_EXPAND) { /* * Use the whole list. */ tk_trial = tk_text; - } else - { + } else { /* * Use the first filename in the list. */ in_completion = 1; init_textlist(&tk_tlist, tk_text); - tk_trial = next_compl(action, (char*)NULL); + tk_trial = next_compl(action, NULL); } - } else - { + } else { /* * We already have a completion list. * Use the next/previous filename from the list. */ tk_trial = next_compl(action, tk_trial); } - - /* - * Remove the original word, or the previous trial completion. - */ + + /* + * Remove the original word, or the previous trial completion. + */ while (cp > tk_ipoint) (void) cmd_erase(); - - if (tk_trial == NULL) - { + + if (tk_trial == NULL) { /* * There are no more trial completions. * Insert the original (uncompleted) filename. @@ -1192,8 +1040,7 @@ in_completion = 0; if (cmd_istr(tk_original) != CC_OK) goto fail; - } else - { + } else { /* * Insert trial completion. */ @@ -1202,28 +1049,25 @@ /* * If it is a directory, append a slash. */ - if (is_dir(tk_trial)) - { + if (is_dir(tk_trial)) { if (cp > cmdbuf && cp[-1] == closequote) (void) cmd_erase(); s = lgetenv("LESSSEPARATOR"); if (s == NULL) - s = PATHNAME_SEP; + s = "/"; if (cmd_istr(s) != CC_OK) goto fail; } } - + return (CC_OK); - + fail: in_completion = 0; - bell(); + ring_bell(); return (CC_OK); } -#endif /* TAB_COMPLETE_FILENAME */ - /* * Process a single character of a multi-character command, such as * a number, or the pattern of a search command. @@ -1232,56 +1076,48 @@ * CC_QUIT The char requests the command to be aborted. * CC_ERROR The char could not be accepted due to an error. */ - public int -cmd_char(c) - int c; +int +cmd_char(int c) { int action; int len; - if (!utf_mode) - { - cmd_mbc_buf[0] = c; + if (!utf_mode) { + cmd_mbc_buf[0] = c & 0xff; len = 1; - } -#if !SMALL - else - { + } else { /* Perform strict validation in all possible cases. */ - if (cmd_mbc_buf_len == 0) - { - retry: + if (cmd_mbc_buf_len == 0) { +retry: cmd_mbc_buf_index = 1; - *cmd_mbc_buf = c; + *cmd_mbc_buf = c & 0xff; if (IS_ASCII_OCTET(c)) cmd_mbc_buf_len = 1; - else if (IS_UTF8_LEAD(c)) - { + else if (IS_UTF8_LEAD(c)) { cmd_mbc_buf_len = utf_len(c); return (CC_OK); - } else - { + } else { /* UTF8_INVALID or stray UTF8_TRAIL */ - bell(); + ring_bell(); return (CC_ERROR); } - } else if (IS_UTF8_TRAIL(c)) - { - cmd_mbc_buf[cmd_mbc_buf_index++] = c; + } else if (IS_UTF8_TRAIL(c)) { + cmd_mbc_buf[cmd_mbc_buf_index++] = c & 0xff; if (cmd_mbc_buf_index < cmd_mbc_buf_len) return (CC_OK); - if (!is_utf8_well_formed(cmd_mbc_buf)) - { - /* complete, but not well formed (non-shortest form), sequence */ + if (!is_utf8_well_formed(cmd_mbc_buf)) { + /* + * complete, but not well formed + * (non-shortest form), sequence + */ cmd_mbc_buf_len = 0; - bell(); + ring_bell(); return (CC_ERROR); } - } else - { + } else { /* Flush incomplete (truncated) sequence. */ cmd_mbc_buf_len = 0; - bell(); + ring_bell(); /* Handle new char. */ goto retry; } @@ -1289,25 +1125,21 @@ len = cmd_mbc_buf_len; cmd_mbc_buf_len = 0; } -#endif /* !SMALL */ - if (literal) - { + if (literal) { /* * Insert the char, even if it is a line-editing char. */ literal = 0; return (cmd_ichar(cmd_mbc_buf, len)); } - + /* * See if it is a line-editing character. */ - if (in_mca() && len == 1) - { + if (in_mca() && len == 1) { action = cmd_edit(c); - switch (action) - { + switch (action) { case CC_OK: case CC_QUIT: return (action); @@ -1315,7 +1147,7 @@ break; } } - + /* * Insert the char into the command buffer. */ @@ -1325,9 +1157,8 @@ /* * Return the number currently in the command buffer. */ - public LINENUM -cmd_int(frac) - long *frac; +LINENUM +cmd_int(long *frac) { char *p; LINENUM n = 0; @@ -1336,8 +1167,7 @@ for (p = cmdbuf; *p >= '0' && *p <= '9'; p++) n = (n * 10) + (*p - '0'); *frac = 0; - if (*p++ == '.') - { + if (*p++ == '.') { *frac = getfraction(&p, NULL, &err); /* {{ do something if err is set? }} */ } @@ -1347,72 +1177,57 @@ /* * Return a pointer to the command buffer. */ - public char * -get_cmdbuf() +char * +get_cmdbuf(void) { return (cmdbuf); } -#if CMD_HISTORY /* * Return the last (most recent) string in the current command history. */ - public char * -cmd_lastpattern() +char * +cmd_lastpattern(void) { if (curr_mlist == NULL) return (NULL); return (curr_mlist->curr_mp->prev->string); } -#endif -#if CMD_HISTORY /* * Get the name of the history file. */ - static char * -histfile_name() +static char * +histfile_name(void) { char *home; char *name; - int len; - + /* See if filename is explicitly specified by $LESSHISTFILE. */ name = lgetenv("LESSHISTFILE"); - if (name != NULL && *name != '\0') - { + if (name != NULL && *name != '\0') { if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0) - /* $LESSHISTFILE == "-" means don't use a history file. */ + /* $LESSHISTFILE == "-" means don't use history file */ return (NULL); return (save(name)); } /* Otherwise, file is in $HOME if enabled. */ - if (strcmp (LESSHISTFILE, "-") == 0) + if (strcmp(LESSHISTFILE, "-") == 0) return (NULL); home = lgetenv("HOME"); - if (home == NULL || *home == '\0') - { -#if OS2 - home = lgetenv("INIT"); - if (home == NULL || *home == '\0') -#endif - return (NULL); + if (home == NULL || *home == '\0') { + return (NULL); } - len = strlen(home) + strlen(LESSHISTFILE) + 2; - name = (char *) ecalloc(len, sizeof(char)); - SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE); - return (name); + return (easprintf("%s/%s", home, LESSHISTFILE)); } -#endif /* CMD_HISTORY */ /* * Initialize history from a .lesshist file. */ - public void -init_cmdhist() +void +init_cmdhist(void) { -#if CMD_HISTORY struct mlist *ml = NULL; char line[CMDBUF_SIZE]; char *filename; @@ -1426,49 +1241,36 @@ free(filename); if (f == NULL) return; - if (fgets(line, sizeof(line), f) == NULL || - strncmp(line, HISTFILE_FIRST_LINE, strlen(HISTFILE_FIRST_LINE)) != 0) - { - fclose(f); + if (fgets(line, sizeof (line), f) == NULL || + strncmp(line, HISTFILE_FIRST_LINE, + strlen(HISTFILE_FIRST_LINE)) != 0) { + (void) fclose(f); return; } - while (fgets(line, sizeof(line), f) != NULL) - { - for (p = line; *p != '\0'; p++) - { - if (*p == '\n' || *p == '\r') - { + while (fgets(line, sizeof (line), f) != NULL) { + for (p = line; *p != '\0'; p++) { + if (*p == '\n' || *p == '\r') { *p = '\0'; break; } } if (strcmp(line, HISTFILE_SEARCH_SECTION) == 0) ml = &mlist_search; - else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0) - { -#if SHELL_ESCAPE || PIPEC + else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0) { ml = &mlist_shell; -#else - ml = NULL; -#endif - } else if (*line == '"') - { + } else if (*line == '"') { if (ml != NULL) cmd_addhist(ml, line+1); } } - fclose(f); -#endif /* CMD_HISTORY */ + (void) fclose(f); } /* * */ -#if CMD_HISTORY - static void -save_mlist(ml, f) - struct mlist *ml; - FILE *f; +static void +save_mlist(struct mlist *ml, FILE *f) { int histsize = 0; int n; @@ -1481,34 +1283,32 @@ histsize = 100; ml = ml->prev; - for (n = 0; n < histsize; n++) - { + for (n = 0; n < histsize; n++) { if (ml->string == NULL) break; ml = ml->prev; } for (ml = ml->next; ml->string != NULL; ml = ml->next) - fprintf(f, "\"%s\n", ml->string); + (void) fprintf(f, "\"%s\n", ml->string); } -#endif /* CMD_HISTORY */ /* * */ - public void -save_cmdhist() +void +save_cmdhist(void) { -#if CMD_HISTORY char *filename; FILE *f; int modified = 0; + int do_chmod = 1; + struct stat statbuf; + int r; if (mlist_search.modified) modified = 1; -#if SHELL_ESCAPE || PIPEC if (mlist_shell.modified) modified = 1; -#endif if (!modified) return; filename = histfile_name(); @@ -1518,32 +1318,22 @@ free(filename); if (f == NULL) return; -#if HAVE_FCHMOD -{ + /* Make history file readable only by owner. */ - int do_chmod = 1; -#if HAVE_STAT - struct stat statbuf; - int r = fstat(fileno(f), &statbuf); + r = fstat(fileno(f), &statbuf); if (r < 0 || !S_ISREG(statbuf.st_mode)) /* Don't chmod if not a regular file. */ do_chmod = 0; -#endif if (do_chmod) - fchmod(fileno(f), 0600); -} -#endif + (void) fchmod(fileno(f), 0600); - fprintf(f, "%s\n", HISTFILE_FIRST_LINE); + (void) fprintf(f, "%s\n", HISTFILE_FIRST_LINE); - fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION); + (void) fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION); save_mlist(&mlist_search, f); -#if SHELL_ESCAPE || PIPEC - fprintf(f, "%s\n", HISTFILE_SHELL_SECTION); + (void) fprintf(f, "%s\n", HISTFILE_SHELL_SECTION); save_mlist(&mlist_shell, f); -#endif - fclose(f); -#endif /* CMD_HISTORY */ + (void) fclose(f); }