diff --git a/include/editline.h b/include/editline.h index 3ba0e2b..6d15820 100644 --- a/include/editline.h +++ b/include/editline.h @@ -4,8 +4,8 @@ /* Assign these to get command completion, see cli.c for * example usage. */ -char *(*rl_complete)(char *token, int *match); -int (*rl_list_possib)(char *token, char ***av); +extern char *(*rl_complete)(char *token, int *match); +extern int (*rl_list_possib)(char *token, char ***av); /* ** For compatibility with FSF readline. diff --git a/src/complete.c b/src/complete.c index bfc516f..4cc813f 100755 --- a/src/complete.c +++ b/src/complete.c @@ -5,32 +5,32 @@ #include "editline.h" -#if defined(NEED_STRDUP) +#if defined(NEED_STRDUP) /* ** Return an allocated copy of a string. */ char * strdup(p) - char *p; + char *p; { - char *new; + char *new; if ((new = NEW(char, strlen(p) + 1)) != NULL) - (void)strcpy(new, p); + (void)strcpy(new, p); return new; } -#endif /* defined(NEED_STRDUP) */ +#endif /* defined(NEED_STRDUP) */ /* ** strcmp-like sorting predicate for qsort. */ -STATIC int +static int compare(p1, p2) - CONST void *p1; - CONST void *p2; + CONST void *p1; + CONST void *p2; { - CONST char **v1; - CONST char **v2; + CONST char **v1; + CONST char **v2; v1 = (CONST char **)p1; v2 = (CONST char **)p2; @@ -41,25 +41,25 @@ compare(p1, p2) ** Fill in *avp with an array of names that match file, up to its length. ** Ignore . and .. . */ -STATIC int +static int FindMatches(dir, file, avp) - char *dir; - char *file; - char ***avp; + char *dir; + char *file; + char ***avp; { - char **av; - char **new; - char *p; - DIR *dp; - DIRENTRY *ep; - SIZE_T ac; - SIZE_T len; - SIZE_T choices; - SIZE_T total; -#define MAX_TOTAL (256 << sizeof(char *)) + char **av; + char **new; + char *p; + DIR *dp; + DIRENTRY *ep; + SIZE_T ac; + SIZE_T len; + SIZE_T choices; + SIZE_T total; +#define MAX_TOTAL (256 << sizeof(char *)) if ((dp = opendir(dir)) == NULL) - return 0; + return 0; av = NULL; ac = 0; @@ -67,56 +67,56 @@ FindMatches(dir, file, avp) choices = 0; total = 0; while ((ep = readdir(dp)) != NULL) { - p = ep->d_name; - if (p[0] == '.' && (p[1] == '\0' || (p[1] == '.' && p[2] == '\0'))) - continue; - if (len && strncmp(p, file, len) != 0) - continue; + p = ep->d_name; + if (p[0] == '.' && (p[1] == '\0' || (p[1] == '.' && p[2] == '\0'))) + continue; + if (len && strncmp(p, file, len) != 0) + continue; - choices++; - if ((total += strlen(p)) > MAX_TOTAL) { - /* This is a bit too much. */ - while (ac > 0) DISPOSE(av[--ac]); - continue; - } + choices++; + if ((total += strlen(p)) > MAX_TOTAL) { + /* This is a bit too much. */ + while (ac > 0) DISPOSE(av[--ac]); + continue; + } - if ((ac % MEM_INC) == 0) { - if ((new = NEW(char*, ac + MEM_INC)) == NULL) { - total = 0; - break; - } - if (ac) { - COPYFROMTO(new, av, ac * sizeof (char **)); - DISPOSE(av); - } - *avp = av = new; - } + if ((ac % MEM_INC) == 0) { + if ((new = NEW(char*, ac + MEM_INC)) == NULL) { + total = 0; + break; + } + if (ac) { + COPYFROMTO(new, av, ac * sizeof (char **)); + DISPOSE(av); + } + *avp = av = new; + } - if ((av[ac] = strdup(p)) == NULL) { - if (ac == 0) - DISPOSE(av); - total = 0; - break; - } - ac++; + if ((av[ac] = strdup(p)) == NULL) { + if (ac == 0) + DISPOSE(av); + total = 0; + break; + } + ac++; } /* Clean up and return. */ (void)closedir(dp); if (total > MAX_TOTAL) { - char many[sizeof(total) * 3]; - p = many + sizeof(many); - *--p = '\0'; - while (choices > 0) { - *--p = '0' + choices % 10; - choices /= 10; - } - while (p > many + sizeof(many) - 8) *--p = ' '; - if ((p = strdup(p)) != NULL) av[ac++] = p; - if ((p = strdup("choices")) != NULL) av[ac++] = p; + char many[sizeof(total) * 3]; + p = many + sizeof(many); + *--p = '\0'; + while (choices > 0) { + *--p = '0' + choices % 10; + choices /= 10; + } + while (p > many + sizeof(many) - 8) *--p = ' '; + if ((p = strdup(p)) != NULL) av[ac++] = p; + if ((p = strdup("choices")) != NULL) av[ac++] = p; } else { - if (ac) - qsort(av, ac, sizeof (char **), compare); + if (ac) + qsort(av, ac, sizeof (char **), compare); } return ac; } @@ -124,32 +124,32 @@ FindMatches(dir, file, avp) /* ** Split a pathname into allocated directory and trailing filename parts. */ -STATIC int +static int SplitPath(path, dirpart, filepart) - char *path; - char **dirpart; - char **filepart; + char *path; + char **dirpart; + char **filepart; { - static char DOT[] = "."; - char *dpart; - char *fpart; + static char DOT[] = "."; + char *dpart; + char *fpart; if ((fpart = strrchr(path, '/')) == NULL) { - if ((dpart = strdup(DOT)) == NULL) - return -1; - if ((fpart = strdup(path)) == NULL) { - DISPOSE(dpart); - return -1; - } + if ((dpart = strdup(DOT)) == NULL) + return -1; + if ((fpart = strdup(path)) == NULL) { + DISPOSE(dpart); + return -1; + } } else { - if ((dpart = strdup(path)) == NULL) - return -1; - dpart[fpart - path + 1] = '\0'; - if ((fpart = strdup(++fpart)) == NULL) { - DISPOSE(dpart); - return -1; - } + if ((dpart = strdup(path)) == NULL) + return -1; + dpart[fpart - path + 1] = '\0'; + if ((fpart = strdup(++fpart)) == NULL) { + DISPOSE(dpart); + return -1; + } } *dirpart = dpart; *filepart = fpart; @@ -162,69 +162,69 @@ SplitPath(path, dirpart, filepart) */ char * default_rl_complete(pathname, unique) - char *pathname; - int *unique; + char *pathname; + int *unique; { - char **av; - char *dir; - char *file; - char *new; - char *p; - SIZE_T ac; - SIZE_T end; - SIZE_T i; - SIZE_T j; - SIZE_T len; + char **av; + char *dir; + char *file; + char *new; + char *p; + SIZE_T ac; + SIZE_T end; + SIZE_T i; + SIZE_T j; + SIZE_T len; if (SplitPath(pathname, &dir, &file) < 0) - return NULL; + return NULL; if ((ac = FindMatches(dir, file, &av)) == 0) { - DISPOSE(dir); - DISPOSE(file); - return NULL; + DISPOSE(dir); + DISPOSE(file); + return NULL; } p = NULL; len = strlen(file); if (ac == 1) { - /* Exactly one match -- finish it off. */ - *unique = 1; - j = strlen(av[0]) - len + 2; - if ((p = NEW(char, j + 1)) != NULL) { - COPYFROMTO(p, av[0] + len, j); - if ((new = NEW(char, strlen(dir) + strlen(av[0]) + 2)) != NULL) { - (void)strcpy(new, dir); - (void)strcat(new, "/"); - (void)strcat(new, av[0]); - rl_add_slash(new, p); - DISPOSE(new); - } - } + /* Exactly one match -- finish it off. */ + *unique = 1; + j = strlen(av[0]) - len + 2; + if ((p = NEW(char, j + 1)) != NULL) { + COPYFROMTO(p, av[0] + len, j); + if ((new = NEW(char, strlen(dir) + strlen(av[0]) + 2)) != NULL) { + (void)strcpy(new, dir); + (void)strcat(new, "/"); + (void)strcat(new, av[0]); + rl_add_slash(new, p); + DISPOSE(new); + } + } } else { - *unique = 0; - if (len) { - /* Find largest matching substring. */ - for (i = len, end = strlen(av[0]); i < end; i++) - for (j = 1; j < ac; j++) - if (av[0][i] != av[j][i]) - goto breakout; + *unique = 0; + if (len) { + /* Find largest matching substring. */ + for (i = len, end = strlen(av[0]); i < end; i++) + for (j = 1; j < ac; j++) + if (av[0][i] != av[j][i]) + goto breakout; breakout: - if (i > len) { - j = i - len + 1; - if ((p = NEW(char, j)) != NULL) { - COPYFROMTO(p, av[0] + len, j); - p[j - 1] = '\0'; - } - } - } + if (i > len) { + j = i - len + 1; + if ((p = NEW(char, j)) != NULL) { + COPYFROMTO(p, av[0] + len, j); + p[j - 1] = '\0'; + } + } + } } /* Clean up and return. */ DISPOSE(dir); DISPOSE(file); for (i = 0; i < ac; i++) - DISPOSE(av[i]); + DISPOSE(av[i]); DISPOSE(av); return p; } @@ -234,15 +234,15 @@ default_rl_complete(pathname, unique) */ int default_rl_list_possib(pathname, avp) - char *pathname; - char ***avp; + char *pathname; + char ***avp; { - char *dir; - char *file; - int ac; + char *dir; + char *file; + int ac; if (SplitPath(pathname, &dir, &file) < 0) - return 0; + return 0; ac = FindMatches(dir, file, avp); DISPOSE(dir); DISPOSE(file); diff --git a/src/editline.c b/src/editline.c index 3596de6..9a9b28c 100755 --- a/src/editline.c +++ b/src/editline.c @@ -10,92 +10,93 @@ /* ** Manifest constants. */ -#define SCREEN_WIDTH 80 -#define SCREEN_ROWS 24 -#define NO_ARG (-1) -#define DEL 127 -#define CTL(x) ((x) & 0x1F) -#define ISCTL(x) ((x) && (x) < ' ') -#define UNCTL(x) ((x) + 64) -#define META(x) ((x) | 0x80) -#define ISMETA(x) ((x) & 0x80) -#define UNMETA(x) ((x) & 0x7F) -#define MAPSIZE 33 -#define METAMAPSIZE 17 -#if !defined(HIST_SIZE) -#define HIST_SIZE 20 -#endif /* !defined(HIST_SIZE) */ +#define SCREEN_WIDTH 80 +#define SCREEN_ROWS 24 +#define NO_ARG (-1) +#define DEL 127 +#define CTL(x) ((x) & 0x1F) +#define ISCTL(x) ((x) && (x) < ' ') +#define UNCTL(x) ((x) + 64) +#define META(x) ((x) | 0x80) +#define ISMETA(x) ((x) & 0x80) +#define UNMETA(x) ((x) & 0x7F) +#define MAPSIZE 33 +#define METAMAPSIZE 17 +#if !defined(HIST_SIZE) +#define HIST_SIZE 20 +#endif /* !defined(HIST_SIZE) */ +#define SEPS "\"#$&'()*:;<=>?[\\]^`{|}~\n\t " /* ** Command status codes. */ -typedef enum _STATUS { +typedef enum { CSdone, CSeof, CSmove, CSdispatch, CSstay, CSsignal -} STATUS; +} el_status_t; /* ** The type of case-changing to perform. */ -typedef enum _CASE { +typedef enum { TOupper, TOlower -} CASE; +} el_case_t; /* ** Key to command mapping. */ -typedef struct _KEYMAP { - CHAR Key; - STATUS (*Function)(); -} KEYMAP; +typedef struct { + int Key; + el_status_t (*Function)(); +} el_keymap_t; /* ** Command history structure. */ -typedef struct _HISTORY { - int Size; - int Pos; - CHAR *Lines[HIST_SIZE]; -} HISTORY; +typedef struct { + int Size; + int Pos; + const char *Lines[HIST_SIZE]; +} el_hist_t; /* ** Globals. */ -int rl_eof; -int rl_erase; -int rl_intr; -int rl_kill; -int rl_quit; -#if defined(DO_SIGTSTP) -int rl_susp; -#endif /* defined(DO_SIGTSTP) */ +int rl_eof; +int rl_erase; +int rl_intr; +int rl_kill; +int rl_quit; +#if defined(DO_SIGTSTP) +int rl_susp; +#endif /* defined(DO_SIGTSTP) */ -STATIC CHAR NIL[] = ""; -STATIC CONST CHAR *Input = NIL; -STATIC CHAR *Line; -STATIC CONST char *Prompt; -STATIC CHAR *Yanked; -STATIC char *Screen; -STATIC char NEWLINE[]= CRLF; -STATIC HISTORY H; -STATIC int Repeat; -STATIC int End; -STATIC int Mark; -STATIC int OldPoint; -STATIC int Point; -STATIC int PushBack; -STATIC int Pushed; -STATIC int Signal; -FORWARD KEYMAP Map[MAPSIZE]; -FORWARD KEYMAP MetaMap[METAMAPSIZE]; -STATIC SIZE_T Length; -STATIC SIZE_T ScreenCount; -STATIC SIZE_T ScreenSize; -STATIC char *backspace; -STATIC int TTYwidth; -STATIC int TTYrows; +static const char NIL[] = ""; +static const char *Input = NIL; +static char *Line; +static const char *Prompt; +static char *Yanked; +static char *Screen; +static char NEWLINE[]= CRLF; +static el_hist_t H; +static int Repeat; +static int End; +static int Mark; +static int OldPoint; +static int Point; +static int PushBack; +static int Pushed; +static int Signal; +static el_keymap_t Map[MAPSIZE]; +static el_keymap_t MetaMap[METAMAPSIZE]; +static SIZE_T Length; +static SIZE_T ScreenCount; +static SIZE_T ScreenSize; +static char *backspace; +static int tty_width; +static int tty_rows; /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */ -int rl_meta_chars = 1; +int rl_meta_chars = 1; /* User definable callbacks. */ char *(*rl_complete)(char *token, int *match); @@ -105,157 +106,144 @@ int (*rl_list_possib)(char *token, char ***av); /* ** Declarations. */ -STATIC CHAR *editinput(); -#if defined(USE_TERMCAP) -extern char *getenv(); -extern char *tgetstr(); -extern int tgetent(); -extern int tgetnum(); -#endif /* defined(USE_TERMCAP) */ +static char *editinput(void); +#if defined(USE_TERMCAP) +extern char *getenv(void); +extern char *tgetstr(void); +extern int tgetent(void); +extern int tgetnum(void); +#endif /* defined(USE_TERMCAP) */ /* ** TTY input/output functions. */ -STATIC void -TTYflush() +static void tty_flush(void) { if (ScreenCount) { - (void)write(1, Screen, ScreenCount); - ScreenCount = 0; + (void)write(1, Screen, ScreenCount); + ScreenCount = 0; } } -STATIC void -TTYput(c) - CONST CHAR c; +static void tty_put(const char c) { Screen[ScreenCount] = c; if (++ScreenCount >= ScreenSize - 1) { - ScreenSize += SCREEN_INC; - RENEW(Screen, char, ScreenSize); + ScreenSize += SCREEN_INC; + RENEW(Screen, char, ScreenSize); } } -STATIC void -TTYputs(p) - CHAR *p; +static void tty_puts(const char *p) { while (*p) - TTYput(*p++); + tty_put(*p++); } -STATIC void -TTYshow(c) - CHAR c; +static void tty_show(char c) { if (c == DEL) { - TTYput('^'); - TTYput('?'); + tty_put('^'); + tty_put('?'); } else if (ISCTL(c)) { - TTYput('^'); - TTYput(UNCTL(c)); + tty_put('^'); + tty_put(UNCTL(c)); } else if (rl_meta_chars && ISMETA(c)) { - TTYput('M'); - TTYput('-'); - TTYput(UNMETA(c)); + tty_put('M'); + tty_put('-'); + tty_put(UNMETA(c)); } else - TTYput(c); + tty_put(c); } -STATIC void -TTYstring(p) - CHAR *p; +static void tty_string(char *p) { while (*p) - TTYshow(*p++); + tty_show(*p++); } -STATIC int -TTYget() +static int tty_get(void) { - CHAR c; + char c; int r; - TTYflush(); + tty_flush(); if (Pushed) { - Pushed = 0; - return PushBack; + Pushed = 0; + return PushBack; } if (*Input) - return *Input++; + return *Input++; do { - r= read(0, &c, (SIZE_T)1); + r= read(0, &c, (SIZE_T)1); } while (r == -1 && errno == EINTR); return r == 1 ? c : EOF; } -#define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b')) +#define tty_back() (backspace ? tty_puts(backspace) : tty_put('\b')) -STATIC void -TTYbackn(n) - int n; +static void tty_backn(int n) { while (--n >= 0) - TTYback(); + tty_back(); } -STATIC void -TTYinfo() +static void tty_info(void) { - static int init; -#if defined(USE_TERMCAP) - char *term; - char buff[2048]; - char *bp; -#endif /* defined(USE_TERMCAP) */ -#if defined(TIOCGWINSZ) - struct winsize W; -#endif /* defined(TIOCGWINSZ) */ + static int init; +#if defined(USE_TERMCAP) + char *term; + char buff[2048]; + char *bp; +#endif /* defined(USE_TERMCAP) */ +#if defined(TIOCGWINSZ) + struct winsize W; +#endif /* defined(TIOCGWINSZ) */ if (init) { -#if defined(TIOCGWINSZ) - /* Perhaps we got resized. */ - if (ioctl(0, TIOCGWINSZ, &W) >= 0 - && W.ws_col > 0 && W.ws_row > 0) { - TTYwidth = (int)W.ws_col; - TTYrows = (int)W.ws_row; - } -#endif /* defined(TIOCGWINSZ) */ - return; +#if defined(TIOCGWINSZ) + /* Perhaps we got resized. */ + if (ioctl(0, TIOCGWINSZ, &W) >= 0 + && W.ws_col > 0 && W.ws_row > 0) { + tty_width = (int)W.ws_col; + tty_rows = (int)W.ws_row; + } +#endif /* defined(TIOCGWINSZ) */ + return; } init++; - TTYwidth = TTYrows = 0; -#if defined(USE_TERMCAP) + tty_width = tty_rows = 0; +#if defined(USE_TERMCAP) bp = &buff[0]; if ((term = getenv("TERM")) == NULL) - term = "dumb"; + term = "dumb"; if (tgetent(buff, term) < 0) { - TTYwidth = SCREEN_WIDTH; - TTYrows = SCREEN_ROWS; + tty_width = SCREEN_WIDTH; + tty_rows = SCREEN_ROWS; return; } if ((backspace = tgetstr("le", &bp)) != NULL) - backspace = strdup(backspace); - TTYwidth = tgetnum("co"); - TTYrows = tgetnum("li"); -#endif /* defined(USE_TERMCAP) */ + backspace = strdup(backspace); + tty_width = tgetnum("co"); + tty_rows = tgetnum("li"); +#endif /* defined(USE_TERMCAP) */ -#if defined(TIOCGWINSZ) +#if defined(TIOCGWINSZ) if (ioctl(0, TIOCGWINSZ, &W) >= 0) { - TTYwidth = (int)W.ws_col; - TTYrows = (int)W.ws_row; + tty_width = (int)W.ws_col; + tty_rows = (int)W.ws_row; } -#endif /* defined(TIOCGWINSZ) */ +#endif /* defined(TIOCGWINSZ) */ - if (TTYwidth <= 0 || TTYrows <= 0) { - TTYwidth = SCREEN_WIDTH; - TTYrows = SCREEN_ROWS; + if (tty_width <= 0 || tty_rows <= 0) { + tty_width = SCREEN_WIDTH; + tty_rows = SCREEN_ROWS; } } @@ -263,271 +251,244 @@ TTYinfo() /* ** Print an array of words in columns. */ -STATIC void -columns(ac, av) - int ac; - CHAR **av; +static void columns(int ac, char **av) { - CHAR *p; - int i; - int j; - int k; - int len; - int skip; - int longest; - int cols; + char *p; + int i; + int j; + int k; + int len; + int skip; + int longest; + int cols; /* Find longest name, determine column count from that. */ for (longest = 0, i = 0; i < ac; i++) - if ((j = strlen((char *)av[i])) > longest) - longest = j; - cols = TTYwidth / (longest + 3); + if ((j = strlen((char *)av[i])) > longest) + longest = j; + cols = tty_width / (longest + 3); - TTYputs((CHAR *)NEWLINE); + tty_puts(NEWLINE); for (skip = ac / cols + 1, i = 0; i < skip; i++) { - for (j = i; j < ac; j += skip) { - for (p = av[j], len = strlen((char *)p), k = len; --k >= 0; p++) - TTYput(*p); - if (j + skip < ac) - while (++len < longest + 3) - TTYput(' '); - } - TTYputs((CHAR *)NEWLINE); + for (j = i; j < ac; j += skip) { + for (p = av[j], len = strlen((char *)p), k = len; --k >= 0; p++) + tty_put(*p); + if (j + skip < ac) + while (++len < longest + 3) + tty_put(' '); + } + tty_puts(NEWLINE); } } -STATIC void -reposition() +static void reposition(void) { - int i; - CHAR *p; + int i; + char *p; - TTYput('\r'); - TTYputs((CONST CHAR *)Prompt); + tty_put('\r'); + tty_puts(Prompt); for (i = Point, p = Line; --i >= 0; p++) - TTYshow(*p); + tty_show(*p); } -STATIC void -left(Change) - STATUS Change; +static void left(el_status_t Change) { - TTYback(); + tty_back(); if (Point) { - if (ISCTL(Line[Point - 1])) - TTYback(); + if (ISCTL(Line[Point - 1])) + tty_back(); else if (rl_meta_chars && ISMETA(Line[Point - 1])) { - TTYback(); - TTYback(); - } + tty_back(); + tty_back(); + } } if (Change == CSmove) - Point--; + Point--; } -STATIC void -right(Change) - STATUS Change; +static void right(el_status_t Change) { - TTYshow(Line[Point]); + tty_show(Line[Point]); if (Change == CSmove) - Point++; + Point++; } -STATIC STATUS -ring_bell() +static el_status_t ring_bell(void) { - TTYput('\07'); - TTYflush(); + tty_put('\07'); + tty_flush(); return CSstay; } -STATIC STATUS -do_macro(c) - unsigned int c; +static el_status_t do_macro(int c) { - CHAR name[4]; + char name[4]; name[0] = '_'; name[1] = c; name[2] = '_'; name[3] = '\0'; - if ((Input = (CHAR *)getenv((char *)name)) == NULL) { - Input = NIL; - return ring_bell(); + if ((Input = (char *)getenv((char *)name)) == NULL) { + Input = NIL; + return ring_bell(); } return CSstay; } -STATIC STATUS -do_forward(move) - STATUS move; +static el_status_t do_forward(el_status_t move) { - int i; - CHAR *p; + int i; + char *p; i = 0; do { - p = &Line[Point]; - for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++) - if (move == CSmove) - right(CSstay); + p = &Line[Point]; + for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++) + if (move == CSmove) + right(CSstay); - for (; Point < End && isalnum(*p); Point++, p++) - if (move == CSmove) - right(CSstay); + for (; Point < End && isalnum(*p); Point++, p++) + if (move == CSmove) + right(CSstay); - if (Point == End) - break; + if (Point == End) + break; } while (++i < Repeat); return CSstay; } -STATIC STATUS -do_case(type) - CASE type; +static el_status_t do_case(el_case_t type) { - int i; - int end; - int count; - CHAR *p; + int i; + int end; + int count; + char *p; (void)do_forward(CSstay); if (OldPoint != Point) { - if ((count = Point - OldPoint) < 0) - count = -count; - Point = OldPoint; - if ((end = Point + count) > End) - end = End; - for (i = Point, p = &Line[i]; i < end; i++, p++) { - if (type == TOupper) { - if (islower(*p)) - *p = toupper(*p); - } - else if (isupper(*p)) - *p = tolower(*p); - right(CSmove); - } + if ((count = Point - OldPoint) < 0) + count = -count; + Point = OldPoint; + if ((end = Point + count) > End) + end = End; + for (i = Point, p = &Line[i]; i < end; i++, p++) { + if (type == TOupper) { + if (islower(*p)) + *p = toupper(*p); + } + else if (isupper(*p)) + *p = tolower(*p); + right(CSmove); + } } return CSstay; } -STATIC STATUS -case_down_word() +static el_status_t case_down_word(void) { return do_case(TOlower); } -STATIC STATUS -case_up_word() +static el_status_t case_up_word(void) { return do_case(TOupper); } -STATIC void -ceol() +static void ceol(void) { - int extras; - int i; - CHAR *p; + int extras; + int i; + char *p; for (extras = 0, i = Point, p = &Line[i]; i <= End; i++, p++) { - TTYput(' '); - if (ISCTL(*p)) { - TTYput(' '); - extras++; - } - else if (rl_meta_chars && ISMETA(*p)) { - TTYput(' '); - TTYput(' '); - extras += 2; - } + tty_put(' '); + if (ISCTL(*p)) { + tty_put(' '); + extras++; + } + else if (rl_meta_chars && ISMETA(*p)) { + tty_put(' '); + tty_put(' '); + extras += 2; + } } for (i += extras; i > Point; i--) - TTYback(); + tty_back(); } -STATIC void -clear_line() +static void clear_line(void) { Point = -strlen(Prompt); - TTYput('\r'); + tty_put('\r'); ceol(); Point = 0; End = 0; Line[0] = '\0'; } -STATIC STATUS -insert_string(p) - CHAR *p; +static el_status_t insert_string(const char *p) { - SIZE_T len; - int i; - CHAR *new; - CHAR *q; + SIZE_T len; + int i; + char *new; + char *q; len = strlen((char *)p); if (End + len >= Length) { - if ((new = NEW(CHAR, Length + len + MEM_INC)) == NULL) - return CSstay; - if (Length) { - COPYFROMTO(new, Line, Length); - DISPOSE(Line); - } - Line = new; - Length += len + MEM_INC; + if ((new = NEW(char, Length + len + MEM_INC)) == NULL) + return CSstay; + if (Length) { + COPYFROMTO(new, Line, Length); + DISPOSE(Line); + } + Line = new; + Length += len + MEM_INC; } for (q = &Line[Point], i = End - Point; --i >= 0; ) - q[len + i] = q[i]; + q[len + i] = q[i]; COPYFROMTO(&Line[Point], p, len); End += len; Line[End] = '\0'; - TTYstring(&Line[Point]); + tty_string(&Line[Point]); Point += len; return Point == End ? CSstay : CSmove; } -STATIC STATUS -redisplay() +static el_status_t redisplay(void) { - TTYputs((CONST CHAR *)NEWLINE); - TTYputs((CONST CHAR *)Prompt); - TTYstring(Line); + tty_puts(NEWLINE); + tty_puts(Prompt); + tty_string(Line); return CSmove; } -STATIC STATUS -toggle_meta_mode() +static el_status_t toggle_meta_mode(void) { rl_meta_chars = ! rl_meta_chars; return redisplay(); } -STATIC CHAR * -next_hist() +static const char *next_hist(void) { return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos]; } -STATIC CHAR * -prev_hist() +static const char *prev_hist(void) { return H.Pos == 0 ? NULL : H.Lines[--H.Pos]; } -STATIC STATUS -do_insert_hist(p) - CHAR *p; +static el_status_t do_insert_hist(const char *p) { if (p == NULL) - return ring_bell(); + return ring_bell(); Point = 0; reposition(); ceol(); @@ -535,41 +496,35 @@ do_insert_hist(p) return insert_string(p); } -STATIC STATUS -do_hist(move) - CHAR *(*move)(); +static el_status_t do_hist(const char *(*move)()) { - CHAR *p; - int i; + const char *p; + int i; i = 0; do { - if ((p = (*move)()) == NULL) - return ring_bell(); + if ((p = (*move)()) == NULL) + return ring_bell(); } while (++i < Repeat); return do_insert_hist(p); } -STATIC STATUS -h_next() +static el_status_t h_next(void) { return do_hist(next_hist); } -STATIC STATUS -h_prev() +static el_status_t h_prev(void) { return do_hist(prev_hist); } -STATIC STATUS -h_first() +static el_status_t h_first(void) { return do_insert_hist(H.Lines[H.Pos = 0]); } -STATIC STATUS -h_last() +static el_status_t h_last(void) { return do_insert_hist(H.Lines[H.Pos = H.Size - 1]); } @@ -577,13 +532,9 @@ h_last() /* ** Return zero if pat appears as a substring in text. */ -STATIC int -substrcmp(text, pat, len) - char *text; - char *pat; - int len; +static int substrcmp(char *text, char *pat, int len) { - char c; + char c; if ((c = *pat) == '\0') return *text == '\0'; @@ -593,205 +544,192 @@ substrcmp(text, pat, len) return 1; } -STATIC CHAR * -search_hist(search, move) - CHAR *search; - CHAR *(*move)(); +static const char *search_hist(const char *search, const char *(*move)()) { - static CHAR *old_search; - int len; - int pos; - int (*match)(); - char *pat; + static char *old_search; + int len; + int pos; + int (*match)(); + char *pat; /* Save or get remembered search pattern. */ if (search && *search) { - if (old_search) - DISPOSE(old_search); - old_search = (CHAR *)strdup((char *)search); + if (old_search) + DISPOSE(old_search); + old_search = (char *)strdup((char *)search); } else { - if (old_search == NULL || *old_search == '\0') + if (old_search == NULL || *old_search == '\0') return NULL; - search = old_search; + search = old_search; } /* Set up pattern-finder. */ if (*search == '^') { - match = strncmp; - pat = (char *)(search + 1); + match = strncmp; + pat = (char *)(search + 1); } else { - match = substrcmp; - pat = (char *)search; + match = substrcmp; + pat = (char *)search; } len = strlen(pat); for (pos = H.Pos; (*move)() != NULL; ) - if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0) + if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0) return H.Lines[H.Pos]; H.Pos = pos; return NULL; } -STATIC STATUS -h_search() +static el_status_t h_search(void) { - static int Searching; - CONST char *old_prompt; - CHAR *(*move)(); - CHAR *p; + static int Searching; + const char *old_prompt; + const char *(*move)(); + const char *p; if (Searching) - return ring_bell(); + return ring_bell(); Searching = 1; clear_line(); old_prompt = Prompt; Prompt = "Search: "; - TTYputs((CONST CHAR *)Prompt); + tty_puts(Prompt); move = Repeat == NO_ARG ? prev_hist : next_hist; p = editinput(); Prompt = old_prompt; Searching = 0; - TTYputs((CONST CHAR *)Prompt); + tty_puts(Prompt); if (p == NULL && Signal > 0) { - Signal = 0; - clear_line(); - return redisplay(); + Signal = 0; + clear_line(); + return redisplay(); } p = search_hist(p, move); clear_line(); if (p == NULL) { - (void)ring_bell(); - return redisplay(); + (void)ring_bell(); + return redisplay(); } return do_insert_hist(p); } -STATIC STATUS -fd_char() +static el_status_t fd_char(void) { - int i; + int i; i = 0; do { - if (Point >= End) - break; - right(CSmove); + if (Point >= End) + break; + right(CSmove); } while (++i < Repeat); return CSstay; } -STATIC void -save_yank(begin, i) - int begin; - int i; +static void save_yank(int begin, int i) { if (Yanked) { - DISPOSE(Yanked); - Yanked = NULL; + DISPOSE(Yanked); + Yanked = NULL; } if (i < 1) - return; + return; - if ((Yanked = NEW(CHAR, (SIZE_T)i + 1)) != NULL) { - COPYFROMTO(Yanked, &Line[begin], i); - Yanked[i] = '\0'; + if ((Yanked = NEW(char, (SIZE_T)i + 1)) != NULL) { + COPYFROMTO(Yanked, &Line[begin], i); + Yanked[i] = '\0'; } } -STATIC STATUS -delete_string(count) - int count; +static el_status_t delete_string(int count) { - int i; - CHAR *p; + int i; + char *p; if (count <= 0 || End == Point) - return ring_bell(); + return ring_bell(); if (count == 1 && Point == End - 1) { - /* Optimize common case of delete at end of line. */ - End--; - p = &Line[Point]; - i = 1; - TTYput(' '); - if (ISCTL(*p)) { - i = 2; - TTYput(' '); - } - else if (rl_meta_chars && ISMETA(*p)) { - i = 3; - TTYput(' '); - TTYput(' '); - } - TTYbackn(i); - *p = '\0'; - return CSmove; + /* Optimize common case of delete at end of line. */ + End--; + p = &Line[Point]; + i = 1; + tty_put(' '); + if (ISCTL(*p)) { + i = 2; + tty_put(' '); + } + else if (rl_meta_chars && ISMETA(*p)) { + i = 3; + tty_put(' '); + tty_put(' '); + } + tty_backn(i); + *p = '\0'; + return CSmove; } if (Point + count > End && (count = End - Point) <= 0) - return CSstay; + return CSstay; if (count > 1) - save_yank(Point, count); + save_yank(Point, count); for (p = &Line[Point], i = End - (Point + count) + 1; --i >= 0; p++) - p[0] = p[count]; + p[0] = p[count]; ceol(); End -= count; - TTYstring(&Line[Point]); + tty_string(&Line[Point]); return CSmove; } -STATIC STATUS -bk_char() +static el_status_t bk_char(void) { - int i; + int i; i = 0; do { - if (Point == 0) - break; - left(CSmove); + if (Point == 0) + break; + left(CSmove); } while (++i < Repeat); return CSstay; } -STATIC STATUS -bk_del_char() +static el_status_t bk_del_char(void) { - int i; + int i; i = 0; do { - if (Point == 0) - break; - left(CSmove); + if (Point == 0) + break; + left(CSmove); } while (++i < Repeat); return delete_string(i); } -STATIC STATUS -kill_line() +static el_status_t kill_line(void) { - int i; + int i; if (Repeat != NO_ARG) { - if (Repeat < Point) { - i = Point; - Point = Repeat; - reposition(); - (void)delete_string(i - Point); - } - else if (Repeat > Point) { - right(CSmove); - (void)delete_string(Repeat - Point - 1); - } - return CSmove; + if (Repeat < Point) { + i = Point; + Point = Repeat; + reposition(); + (void)delete_string(i - Point); + } + else if (Repeat > Point) { + right(CSmove); + (void)delete_string(Repeat - Point - 1); + } + return CSmove; } save_yank(Point, End - Point); @@ -801,26 +739,24 @@ kill_line() return CSstay; } -STATIC STATUS -insert_char(c) - int c; +static el_status_t insert_char(int c) { - STATUS s; - CHAR buff[2]; - CHAR *p; - CHAR *q; - int i; + el_status_t s; + char buff[2]; + char *p; + char *q; + int i; if (Repeat == NO_ARG || Repeat < 2) { - buff[0] = c; - buff[1] = '\0'; - return insert_string(buff); + buff[0] = c; + buff[1] = '\0'; + return insert_string(buff); } - if ((p = NEW(CHAR, Repeat + 1)) == NULL) - return CSstay; + if ((p = NEW(char, Repeat + 1)) == NULL) + return CSstay; for (i = Repeat, q = p; --i >= 0; ) - *q++ = c; + *q++ = c; *q = '\0'; Repeat = 0; s = insert_string(p); @@ -828,197 +764,188 @@ insert_char(c) return s; } -STATIC STATUS -meta() +static el_status_t meta(void) { - int c; - KEYMAP *kp; + int c; + el_keymap_t *kp; - if ((c = TTYget()) == EOF) - return CSeof; -#if defined(ANSI_ARROWS) + if ((c = tty_get()) == EOF) + return CSeof; +#if defined(ANSI_ARROWS) /* Also include VT-100 arrows. */ if (c == '[' || c == 'O') - switch (c = TTYget()) { - default: return ring_bell(); - case EOF: return CSeof; - case 'A': return h_prev(); - case 'B': return h_next(); - case 'C': return fd_char(); - case 'D': return bk_char(); - } -#endif /* defined(ANSI_ARROWS) */ + switch (c = tty_get()) { + default: return ring_bell(); + case EOF: return CSeof; + case 'A': return h_prev(); + case 'B': return h_next(); + case 'C': return fd_char(); + case 'D': return bk_char(); + } +#endif /* defined(ANSI_ARROWS) */ if (isdigit(c)) { - for (Repeat = c - '0'; (c = TTYget()) != EOF && isdigit(c); ) - Repeat = Repeat * 10 + c - '0'; - Pushed = 1; - PushBack = c; - return CSstay; + for (Repeat = c - '0'; (c = tty_get()) != EOF && isdigit(c); ) + Repeat = Repeat * 10 + c - '0'; + Pushed = 1; + PushBack = c; + return CSstay; } if (isupper(c)) - return do_macro(c); + return do_macro(c); for (kp = MetaMap; kp->Function; kp++) - if (kp->Key == c) - return (*kp->Function)(); + if (kp->Key == c) + return (*kp->Function)(); return ring_bell(); } -STATIC STATUS -emacs(c) - unsigned int c; +static el_status_t emacs(int c) { - STATUS s; - KEYMAP *kp; + el_status_t s; + el_keymap_t *kp; #if 0 /* Debian patch removes this to be able to handle 8-bit input */ /* This test makes it impossible to enter eight-bit characters when * meta-char mode is enabled. */ OldPoint = Point; if (rl_meta_chars && ISMETA(c)) { - Pushed = 1; - PushBack = UNMETA(c); - return meta(); + Pushed = 1; + PushBack = UNMETA(c); + return meta(); } #endif /* Debian patch removal. */ for (kp = Map; kp->Function; kp++) - if (kp->Key == c) - break; + if (kp->Key == c) + break; s = kp->Function ? (*kp->Function)() : insert_char((int)c); if (!Pushed) - /* No pushback means no repeat count; hacky, but true. */ - Repeat = NO_ARG; + /* No pushback means no repeat count; hacky, but true. */ + Repeat = NO_ARG; return s; } -STATIC STATUS -TTYspecial(c) - int c; +static el_status_t tty_special(int c) { if (rl_meta_chars && ISMETA(c)) - return CSdispatch; + return CSdispatch; if (c == rl_erase || c == DEL) - return bk_del_char(); + return bk_del_char(); if (c == rl_kill) { - if (Point != 0) { - Point = 0; - reposition(); - } - Repeat = NO_ARG; - return kill_line(); + if (Point != 0) { + Point = 0; + reposition(); + } + Repeat = NO_ARG; + return kill_line(); } if (c == rl_eof && Point == 0 && End == 0) - return CSeof; + return CSeof; if (c == rl_intr) { - Signal = SIGINT; - return CSsignal; + Signal = SIGINT; + return CSsignal; } if (c == rl_quit) { - Signal = SIGQUIT; - return CSeof; + Signal = SIGQUIT; + return CSeof; } -#if defined(DO_SIGTSTP) +#if defined(DO_SIGTSTP) if (c == rl_susp) { - Signal = SIGTSTP; - return CSsignal; + Signal = SIGTSTP; + return CSsignal; } -#endif /* defined(DO_SIGTSTP) */ +#endif /* defined(DO_SIGTSTP) */ return CSdispatch; } -STATIC CHAR * -editinput() +static char *editinput(void) { - int c; + int c; Repeat = NO_ARG; OldPoint = Point = Mark = End = 0; Line[0] = '\0'; Signal = -1; - while ((c = TTYget()) != EOF) - switch (TTYspecial(c)) { - case CSdone: - return Line; - case CSeof: - return NULL; - case CSsignal: - return (CHAR *)""; - case CSmove: - reposition(); - break; - case CSdispatch: - switch (emacs(c)) { - case CSdone: - return Line; - case CSeof: - return NULL; - case CSsignal: - return (CHAR *)""; - case CSmove: - reposition(); - break; - case CSdispatch: - case CSstay: - break; - } - break; - case CSstay: - break; - } + while ((c = tty_get()) != EOF) + switch (tty_special(c)) { + case CSdone: + return Line; + case CSeof: + return NULL; + case CSsignal: + return (char *)""; + case CSmove: + reposition(); + break; + case CSdispatch: + switch (emacs(c)) { + case CSdone: + return Line; + case CSeof: + return NULL; + case CSsignal: + return (char *)""; + case CSmove: + reposition(); + break; + case CSdispatch: + case CSstay: + break; + } + break; + case CSstay: + break; + } return NULL; } -STATIC void -hist_add(p) - CHAR *p; +static void hist_add(const char *p) { - int i; + int i; - if ((p = (CHAR *)strdup((char *)p)) == NULL) - return; + if ((p = (char *)strdup((char *)p)) == NULL) + return; if (H.Size < HIST_SIZE) - H.Lines[H.Size++] = p; + H.Lines[H.Size++] = p; else { - DISPOSE(H.Lines[0]); - for (i = 0; i < HIST_SIZE - 1; i++) - H.Lines[i] = H.Lines[i + 1]; - H.Lines[i] = p; + DISPOSE(H.Lines[0]); + for (i = 0; i < HIST_SIZE - 1; i++) + H.Lines[i] = H.Lines[i + 1]; + H.Lines[i] = p; } H.Pos = H.Size - 1; } -STATIC char * -read_redirected(void) +static char *read_redirected(void) { - int size = MEM_INC; - char *p; - char *line; - char *end; + int size = MEM_INC; + char *p; + char *line; + char *end; p = line = NEW(char, size); end = p + size; while (1) { - if (p == end) { + if (p == end) { int oldpos = end - line; - size += MEM_INC; - p = RENEW(line, char, size); - end = p + size; + size += MEM_INC; + p = RENEW(line, char, size); + end = p + size; p += oldpos; /* Continue where we left off... */ - } - if (read(0, p, 1) <= 0) { - /* Ignore "incomplete" lines at EOF, just like we do for a tty. */ - free(line); - return NULL; - } - if (*p == '\n') - break; + } + if (read(0, p, 1) <= 0) { + /* Ignore "incomplete" lines at EOF, just like we do for a tty. */ + free(line); + return NULL; + } + if (*p == '\n') + break; p++; } *p = '\0'; @@ -1029,14 +956,11 @@ read_redirected(void) ** For compatibility with FSF readline. */ /* ARGSUSED0 */ -void -rl_reset_terminal(p) - char *p __attribute__((__unused__)); +void rl_reset_terminal(char *p __attribute__((__unused__))) { } -void -rl_initialize(void) +void rl_initialize(void) { #ifdef COMPLETE int done = 0; @@ -1054,150 +978,139 @@ rl_initialize(void) #endif } -char * -readline(prompt) - CONST char *prompt; +char *readline(const char *prompt) { - CHAR *line; - int s; + char *line; + int s; /* Unless called by the user already. */ rl_initialize (); if (!isatty(0)) { - TTYflush(); - return read_redirected(); + tty_flush(); + return read_redirected(); } if (Line == NULL) { - Length = MEM_INC; - if ((Line = NEW(CHAR, Length)) == NULL) - return NULL; + Length = MEM_INC; + if ((Line = NEW(char, Length)) == NULL) + return NULL; } - TTYinfo(); + tty_info(); rl_ttyset(0); hist_add(NIL); ScreenSize = SCREEN_INC; Screen = NEW(char, ScreenSize); - Prompt = prompt ? prompt : (char *)NIL; - TTYputs((CONST CHAR *)Prompt); + Prompt = prompt ? prompt : NIL; + tty_puts(Prompt); if ((line = editinput()) != NULL) { - line = (CHAR *)strdup((char *)line); - TTYputs((CHAR *)NEWLINE); - TTYflush(); + line = strdup(line); + tty_puts(NEWLINE); + tty_flush(); } rl_ttyset(1); DISPOSE(Screen); DISPOSE(H.Lines[--H.Size]); if (line != NULL && *line != '\0' -#if defined(UNIQUE_HISTORY) - && !(H.Pos && strcmp((char *) line, (char *) H.Lines[H.Pos - 1]) == 0) -#endif /* defined(UNIQUE_HISTORY) */ - && !(H.Size && strcmp((char *) line, (char *) H.Lines[H.Size - 1]) == 0) +#if defined(UNIQUE_HISTORY) + && !(H.Pos && strcmp((char *) line, (char *) H.Lines[H.Pos - 1]) == 0) +#endif /* defined(UNIQUE_HISTORY) */ + && !(H.Size && strcmp((char *) line, (char *) H.Lines[H.Size - 1]) == 0) ) { - hist_add(line); + hist_add(line); } if (Signal > 0) { - s = Signal; - Signal = 0; - (void)kill(getpid(), s); + s = Signal; + Signal = 0; + (void)kill(getpid(), s); } return (char *)line; } -void -add_history(p) - char *p __attribute__ ((unused)); +void add_history(char *p __attribute__ ((unused))) { -#ifdef obsolete /* Made part of readline(). -- kjb */ +#ifdef obsolete /* Made part of readline(). -- kjb */ if (p == NULL || *p == '\0') - return; + return; -#if defined(UNIQUE_HISTORY) +#if defined(UNIQUE_HISTORY) if (H.Pos && strcmp(p, (char *) H.Lines[H.Pos - 1]) == 0) return; -#endif /* defined(UNIQUE_HISTORY) */ +#endif /* defined(UNIQUE_HISTORY) */ if (H.Size && strcmp(p, (char *) H.Lines[H.Size - 1]) == 0) - return; - hist_add((CHAR *)p); + return; + hist_add((char *)p); #endif } -STATIC STATUS -beg_line() +static el_status_t beg_line(void) { if (Point) { - Point = 0; - return CSmove; + Point = 0; + return CSmove; } return CSstay; } -STATIC STATUS -del_char() +static el_status_t del_char(void) { return delete_string(Repeat == NO_ARG ? 1 : Repeat); } -STATIC STATUS -end_line() +static el_status_t end_line(void) { if (Point != End) { - Point = End; - return CSmove; + Point = End; + return CSmove; } return CSstay; } -STATIC char SEPS[] = "\"#$&'()*:;<=>?[\\]^`{|}~\n\t "; - /* ** Move back to the beginning of the current word and return an ** allocated copy of it. */ -STATIC CHAR * -find_word() +static char *find_word(void) { - CHAR *p, *q; - CHAR *new; - SIZE_T len; + char *p, *q; + char *new; + SIZE_T len; p = &Line[Point]; while (p > Line) { - p--; - if (p > Line && p[-1] == '\\') { - p--; - } else { - if (strchr(SEPS, (char) *p) != NULL) { - p++; - break; - } - } + p--; + if (p > Line && p[-1] == '\\') { + p--; + } else { + if (strchr(SEPS, (char) *p) != NULL) { + p++; + break; + } + } } len = Point - (p - Line) + 1; - if ((new = NEW(CHAR, len)) == NULL) - return NULL; + if ((new = NEW(char, len)) == NULL) + return NULL; q = new; while (p < &Line[Point]) { - if (*p == '\\') { - if (++p == &Line[Point]) break; - } - *q++ = *p++; + if (*p == '\\') { + if (++p == &Line[Point]) break; + } + *q++ = *p++; } *q = '\0'; return new; } -STATIC STATUS -c_possible(void) +static el_status_t c_possible(void) { - CHAR **av; - CHAR *word; - int ac; + char **av; + char *word; + int ac; if (!rl_list_possib) { return ring_bell(); @@ -1206,346 +1119,328 @@ c_possible(void) word = find_word(); ac = rl_list_possib((char *)word, (char ***)&av); if (word) - DISPOSE(word); + DISPOSE(word); if (ac) { - columns(ac, av); - while (--ac >= 0) - DISPOSE(av[ac]); - DISPOSE(av); - return CSmove; + columns(ac, av); + while (--ac >= 0) + DISPOSE(av[ac]); + DISPOSE(av); + return CSmove; } return ring_bell(); } -STATIC STATUS -c_complete(void) +static el_status_t c_complete(void) { - CHAR *p, *q; - CHAR *word, *new; - SIZE_T len; - int unique; - STATUS s; + char *p, *q; + char *word, *new; + SIZE_T len; + int unique; + el_status_t s; if (!rl_complete) { return ring_bell(); } word = find_word(); - p = (CHAR *)rl_complete((char *)word, &unique); + p = (char *)rl_complete((char *)word, &unique); if (word) - DISPOSE(word); + DISPOSE(word); if (p) { - len = strlen((char *)p); - word = p; - new = q = NEW(CHAR, 2 * len + 1); - while (*p) { - if ((*p < ' ' || strchr(SEPS, (char) *p) != NULL) - && (!unique || p[1] != 0)) { - *q++ = '\\'; - } - *q++ = *p++; - } - *q = '\0'; - DISPOSE(word); - if (len > 0) { - s = insert_string(new); + len = strlen((char *)p); + word = p; + new = q = NEW(char, 2 * len + 1); + while (*p) { + if ((*p < ' ' || strchr(SEPS, (char) *p) != NULL) + && (!unique || p[1] != 0)) { + *q++ = '\\'; + } + *q++ = *p++; + } + *q = '\0'; + DISPOSE(word); + if (len > 0) { + s = insert_string(new); #if ANNOYING_NOISE - if (!unique) - (void)ring_bell(); + if (!unique) + (void)ring_bell(); #endif - } - DISPOSE(new); - if (len > 0) return s; + } + DISPOSE(new); + if (len > 0) return s; } return c_possible(); } -STATIC STATUS -accept_line() +static el_status_t accept_line(void) { Line[End] = '\0'; return CSdone; } -STATIC STATUS -transpose() +static el_status_t transpose(void) { - CHAR c; + char c; if (Point) { - if (Point == End) - left(CSmove); - c = Line[Point - 1]; - left(CSstay); - Line[Point - 1] = Line[Point]; - TTYshow(Line[Point - 1]); - Line[Point++] = c; - TTYshow(c); + if (Point == End) + left(CSmove); + c = Line[Point - 1]; + left(CSstay); + Line[Point - 1] = Line[Point]; + tty_show(Line[Point - 1]); + Line[Point++] = c; + tty_show(c); } return CSstay; } -STATIC STATUS -quote() +static el_status_t quote(void) { - int c; + int c; - return (c = TTYget()) == EOF ? CSeof : insert_char((int)c); + return (c = tty_get()) == EOF ? CSeof : insert_char((int)c); } -STATIC STATUS -wipe() +static el_status_t wipe(void) { - int i; + int i; if (Mark > End) - return ring_bell(); + return ring_bell(); if (Point > Mark) { - i = Point; - Point = Mark; - Mark = i; - reposition(); + i = Point; + Point = Mark; + Mark = i; + reposition(); } return delete_string(Mark - Point); } -STATIC STATUS -mk_set() +static el_status_t mk_set(void) { Mark = Point; return CSstay; } -STATIC STATUS -exchange() +static el_status_t exchange(void) { - int c; + int c; - if ((c = TTYget()) != CTL('X')) - return c == EOF ? CSeof : ring_bell(); + if ((c = tty_get()) != CTL('X')) + return c == EOF ? CSeof : ring_bell(); if ((c = Mark) <= End) { - Mark = Point; - Point = c; - return CSmove; + Mark = Point; + Point = c; + return CSmove; } return CSstay; } -STATIC STATUS -yank() +static el_status_t yank(void) { if (Yanked && *Yanked) - return insert_string(Yanked); + return insert_string(Yanked); return CSstay; } -STATIC STATUS -copy_region() +static el_status_t copy_region(void) { if (Mark > End) - return ring_bell(); + return ring_bell(); if (Point > Mark) - save_yank(Mark, Point - Mark); + save_yank(Mark, Point - Mark); else - save_yank(Point, Mark - Point); + save_yank(Point, Mark - Point); return CSstay; } -STATIC STATUS -move_to_char() +static el_status_t move_to_char(void) { - int c; - int i; - CHAR *p; + int c; + int i; + char *p; - if ((c = TTYget()) == EOF) - return CSeof; + if ((c = tty_get()) == EOF) + return CSeof; for (i = Point + 1, p = &Line[i]; i < End; i++, p++) - if (*p == c) { - Point = i; - return CSmove; - } + if (*p == c) { + Point = i; + return CSmove; + } return CSstay; } -STATIC STATUS -fd_word() +static el_status_t fd_word(void) { return do_forward(CSmove); } -STATIC STATUS -fd_kill_word() +static el_status_t fd_kill_word(void) { - int i; + int i; (void)do_forward(CSstay); if (OldPoint != Point) { - i = Point - OldPoint; - Point = OldPoint; - return delete_string(i); + i = Point - OldPoint; + Point = OldPoint; + return delete_string(i); } return CSstay; } -STATIC STATUS -bk_word() +static el_status_t bk_word(void) { - int i; - CHAR *p; + int i; + char *p; i = 0; do { - for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--) - left(CSmove); + for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--) + left(CSmove); - for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--) - left(CSmove); + for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--) + left(CSmove); - if (Point == 0) - break; + if (Point == 0) + break; } while (++i < Repeat); return CSstay; } -STATIC STATUS -bk_kill_word() +static el_status_t bk_kill_word(void) { (void)bk_word(); if (OldPoint != Point) - return delete_string(OldPoint - Point); + return delete_string(OldPoint - Point); return CSstay; } -STATIC int -argify(line, avp) - CHAR *line; - CHAR ***avp; +static int argify(char *line, char ***avp) { - CHAR *c; - CHAR **p; - CHAR **new; - int ac; - int i; + char *c; + char **p; + char **new; + int ac; + int i; i = MEM_INC; - if ((*avp = p = NEW(CHAR*, i))== NULL) - return 0; + if ((*avp = p = NEW(char *, i))== NULL) + return 0; for (c = line; isspace(*c); c++) - continue; + continue; if (*c == '\n' || *c == '\0') - return 0; + return 0; for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) { - if (isspace(*c)) { - *c++ = '\0'; - if (*c && *c != '\n') { - if (ac + 1 == i) { - new = NEW(CHAR*, i + MEM_INC); - if (new == NULL) { - p[ac] = NULL; - return ac; - } - COPYFROMTO(new, p, i * sizeof (char **)); - i += MEM_INC; - DISPOSE(p); - *avp = p = new; - } - p[ac++] = c; - } - } - else - c++; + if (isspace(*c)) { + *c++ = '\0'; + if (*c && *c != '\n') { + if (ac + 1 == i) { + new = NEW(char *, i + MEM_INC); + if (new == NULL) { + p[ac] = NULL; + return ac; + } + COPYFROMTO(new, p, i * sizeof (char **)); + i += MEM_INC; + DISPOSE(p); + *avp = p = new; + } + p[ac++] = c; + } + } + else + c++; } *c = '\0'; p[ac] = NULL; return ac; } -STATIC STATUS -last_argument() +static el_status_t last_argument(void) { - CHAR **av; - CHAR *p; - STATUS s; - int ac; + char **av; + char *p; + el_status_t s; + int ac; - if (H.Size == 1 || (p = H.Lines[H.Size - 2]) == NULL) - return ring_bell(); + if (H.Size == 1 || (p = (char *)H.Lines[H.Size - 2]) == NULL) + return ring_bell(); - if ((p = (CHAR *)strdup((char *)p)) == NULL) - return CSstay; + if ((p = strdup(p)) == NULL) + return CSstay; ac = argify(p, &av); if (Repeat != NO_ARG) - s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell(); + s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell(); else - s = ac ? insert_string(av[ac - 1]) : CSstay; + s = ac ? insert_string(av[ac - 1]) : CSstay; if (ac) - DISPOSE(av); + DISPOSE(av); DISPOSE(p); return s; } -STATIC KEYMAP Map[33] = { - { CTL('@'), mk_set }, - { CTL('A'), beg_line }, - { CTL('B'), bk_char }, - { CTL('D'), del_char }, - { CTL('E'), end_line }, - { CTL('F'), fd_char }, - { CTL('G'), ring_bell }, - { CTL('H'), bk_del_char }, - { CTL('I'), c_complete }, - { CTL('J'), accept_line }, - { CTL('K'), kill_line }, - { CTL('L'), redisplay }, - { CTL('M'), accept_line }, - { CTL('N'), h_next }, - { CTL('O'), ring_bell }, - { CTL('P'), h_prev }, - { CTL('Q'), ring_bell }, - { CTL('R'), h_search }, - { CTL('S'), ring_bell }, - { CTL('T'), transpose }, - { CTL('U'), ring_bell }, - { CTL('V'), quote }, - { CTL('W'), bk_kill_word }, - { CTL('X'), exchange }, - { CTL('Y'), yank }, - { CTL('Z'), end_line }, - { CTL('['), meta }, - { CTL(']'), move_to_char }, - { CTL('^'), ring_bell }, - { CTL('_'), ring_bell }, - { 0, NULL } +static el_keymap_t Map[33] = { + { CTL('@'), mk_set }, + { CTL('A'), beg_line }, + { CTL('B'), bk_char }, + { CTL('D'), del_char }, + { CTL('E'), end_line }, + { CTL('F'), fd_char }, + { CTL('G'), ring_bell }, + { CTL('H'), bk_del_char }, + { CTL('I'), c_complete }, + { CTL('J'), accept_line }, + { CTL('K'), kill_line }, + { CTL('L'), redisplay }, + { CTL('M'), accept_line }, + { CTL('N'), h_next }, + { CTL('O'), ring_bell }, + { CTL('P'), h_prev }, + { CTL('Q'), ring_bell }, + { CTL('R'), h_search }, + { CTL('S'), ring_bell }, + { CTL('T'), transpose }, + { CTL('U'), ring_bell }, + { CTL('V'), quote }, + { CTL('W'), bk_kill_word }, + { CTL('X'), exchange }, + { CTL('Y'), yank }, + { CTL('Z'), end_line }, + { CTL('['), meta }, + { CTL(']'), move_to_char }, + { CTL('^'), ring_bell }, + { CTL('_'), ring_bell }, + { 0, NULL } }; -STATIC KEYMAP MetaMap[17]= { - { CTL('H'), wipe }, - { DEL, wipe }, - { ' ', mk_set }, - { '.', last_argument }, - { '<', h_first }, - { '>', h_last }, - { '?', c_possible }, - { 'b', bk_word }, - { 'd', fd_kill_word }, - { 'f', fd_word }, - { 'l', case_down_word }, - { 'm', toggle_meta_mode }, - { 'u', case_up_word }, - { 'y', yank }, - { 'w', copy_region }, - { 0, NULL } +static el_keymap_t MetaMap[17]= { + { CTL('H'), wipe }, + { DEL, wipe }, + { ' ', mk_set }, + { '.', last_argument }, + { '<', h_first }, + { '>', h_last }, + { '?', c_possible }, + { 'b', bk_word }, + { 'd', fd_kill_word }, + { 'f', fd_word }, + { 'l', case_down_word }, + { 'm', toggle_meta_mode }, + { 'u', case_up_word }, + { 'y', yank }, + { 'w', copy_region }, + { 0, NULL } }; /* diff --git a/src/editline.h b/src/editline.h index 2a43ac9..43f6925 100755 --- a/src/editline.h +++ b/src/editline.h @@ -35,21 +35,6 @@ typedef unsigned char CHAR; -#if defined(HIDE) -#define STATIC static -#else -#define STATIC /* NULL */ -#endif /* !defined(HIDE) */ - -#if !defined(CONST) -#if defined(__STDC__) -#define CONST const -#else -#define CONST -#endif /* defined(__STDC__) */ -#endif /* !defined(CONST) */ - - #define MEM_INC 64 #define SCREEN_INC 256 diff --git a/src/sysunix.c b/src/sysunix.c index 056874e..6f5c798 100755 --- a/src/sysunix.c +++ b/src/sysunix.c @@ -4,69 +4,69 @@ */ #include "editline.h" -#if defined(HAVE_TCGETATTR) +#if defined(HAVE_TCGETATTR) #include void rl_ttyset(Reset) - int Reset; + int Reset; { - static struct termios old; - struct termios new; + static struct termios old; + struct termios new; if (Reset == 0) { - if (tcgetattr(0, &old) < 0) perror("tcgetattr"); - rl_erase = old.c_cc[VERASE]; - rl_kill = old.c_cc[VKILL]; - rl_eof = old.c_cc[VEOF]; - rl_intr = old.c_cc[VINTR]; - rl_quit = old.c_cc[VQUIT]; -#if defined(DO_SIGTSTP) - rl_susp = old.c_cc[VSUSP]; -#endif /* defined(DO_SIGTSTP) */ + if (tcgetattr(0, &old) < 0) perror("tcgetattr"); + rl_erase = old.c_cc[VERASE]; + rl_kill = old.c_cc[VKILL]; + rl_eof = old.c_cc[VEOF]; + rl_intr = old.c_cc[VINTR]; + rl_quit = old.c_cc[VQUIT]; +#if defined(DO_SIGTSTP) + rl_susp = old.c_cc[VSUSP]; +#endif /* defined(DO_SIGTSTP) */ - new = old; - new.c_lflag &= ~(ECHO | ICANON | ISIG); - new.c_iflag &= ~(ISTRIP | INPCK); - new.c_cc[VMIN] = 1; - new.c_cc[VTIME] = 0; - if (tcsetattr(0, TCSADRAIN, &new) < 0) perror("tcsetattr"); + new = old; + new.c_lflag &= ~(ECHO | ICANON | ISIG); + new.c_iflag &= ~(ISTRIP | INPCK); + new.c_cc[VMIN] = 1; + new.c_cc[VTIME] = 0; + if (tcsetattr(0, TCSADRAIN, &new) < 0) perror("tcsetattr"); } else - (void)tcsetattr(0, TCSADRAIN, &old); + (void)tcsetattr(0, TCSADRAIN, &old); } #else -#if defined(HAVE_TERMIO_H) +#if defined(HAVE_TERMIO_H) #include void rl_ttyset(Reset) - int Reset; + int Reset; { - static struct termio old; - struct termio new; + static struct termio old; + struct termio new; if (Reset == 0) { - (void)ioctl(0, TCGETA, &old); - rl_erase = old.c_cc[VERASE]; - rl_kill = old.c_cc[VKILL]; - rl_eof = old.c_cc[VEOF]; - rl_intr = old.c_cc[VINTR]; - rl_quit = old.c_cc[VQUIT]; -#if defined(DO_SIGTSTP) - rl_susp = old.c_cc[VSUSP]; -#endif /* defined(DO_SIGTSTP) */ + (void)ioctl(0, TCGETA, &old); + rl_erase = old.c_cc[VERASE]; + rl_kill = old.c_cc[VKILL]; + rl_eof = old.c_cc[VEOF]; + rl_intr = old.c_cc[VINTR]; + rl_quit = old.c_cc[VQUIT]; +#if defined(DO_SIGTSTP) + rl_susp = old.c_cc[VSUSP]; +#endif /* defined(DO_SIGTSTP) */ - new = old; - new.c_lflag &= ~(ECHO | ICANON | ISIG); - new.c_iflag &= ~(ISTRIP | INPCK); - new.c_cc[VMIN] = 1; - new.c_cc[VTIME] = 0; - (void)ioctl(0, TCSETAW, &new); + new = old; + new.c_lflag &= ~(ECHO | ICANON | ISIG); + new.c_iflag &= ~(ISTRIP | INPCK); + new.c_cc[VMIN] = 1; + new.c_cc[VTIME] = 0; + (void)ioctl(0, TCSETAW, &new); } else - (void)ioctl(0, TCSETAW, &old); + (void)ioctl(0, TCSETAW, &old); } #else @@ -74,61 +74,61 @@ rl_ttyset(Reset) void rl_ttyset(Reset) - int Reset; + int Reset; { - static struct sgttyb old_sgttyb; - static struct tchars old_tchars; - struct sgttyb new_sgttyb; - struct tchars new_tchars; -#if defined(DO_SIGTSTP) - struct ltchars old_ltchars; -#endif /* defined(DO_SIGTSTP) */ + static struct sgttyb old_sgttyb; + static struct tchars old_tchars; + struct sgttyb new_sgttyb; + struct tchars new_tchars; +#if defined(DO_SIGTSTP) + struct ltchars old_ltchars; +#endif /* defined(DO_SIGTSTP) */ if (Reset == 0) { - (void)ioctl(0, TIOCGETP, &old_sgttyb); - rl_erase = old_sgttyb.sg_erase; - rl_kill = old_sgttyb.sg_kill; + (void)ioctl(0, TIOCGETP, &old_sgttyb); + rl_erase = old_sgttyb.sg_erase; + rl_kill = old_sgttyb.sg_kill; - (void)ioctl(0, TIOCGETC, &old_tchars); - rl_eof = old_tchars.t_eofc; - rl_intr = old_tchars.t_intrc; - rl_quit = old_tchars.t_quitc; + (void)ioctl(0, TIOCGETC, &old_tchars); + rl_eof = old_tchars.t_eofc; + rl_intr = old_tchars.t_intrc; + rl_quit = old_tchars.t_quitc; -#if defined(DO_SIGTSTP) - (void)ioctl(0, TIOCGLTC, &old_ltchars); - rl_susp = old_ltchars.t_suspc; -#endif /* defined(DO_SIGTSTP) */ +#if defined(DO_SIGTSTP) + (void)ioctl(0, TIOCGLTC, &old_ltchars); + rl_susp = old_ltchars.t_suspc; +#endif /* defined(DO_SIGTSTP) */ - new_sgttyb = old_sgttyb; - new_sgttyb.sg_flags &= ~ECHO; - new_sgttyb.sg_flags |= RAW; -#if defined(PASS8) - new_sgttyb.sg_flags |= PASS8; -#endif /* defined(PASS8) */ - (void)ioctl(0, TIOCSETP, &new_sgttyb); + new_sgttyb = old_sgttyb; + new_sgttyb.sg_flags &= ~ECHO; + new_sgttyb.sg_flags |= RAW; +#if defined(PASS8) + new_sgttyb.sg_flags |= PASS8; +#endif /* defined(PASS8) */ + (void)ioctl(0, TIOCSETP, &new_sgttyb); - new_tchars = old_tchars; - new_tchars.t_intrc = -1; - new_tchars.t_quitc = -1; - (void)ioctl(0, TIOCSETC, &new_tchars); + new_tchars = old_tchars; + new_tchars.t_intrc = -1; + new_tchars.t_quitc = -1; + (void)ioctl(0, TIOCSETC, &new_tchars); } else { - (void)ioctl(0, TIOCSETP, &old_sgttyb); - (void)ioctl(0, TIOCSETC, &old_tchars); + (void)ioctl(0, TIOCSETP, &old_sgttyb); + (void)ioctl(0, TIOCSETC, &old_tchars); } } -#endif /* defined(HAVE_TERMIO_H) */ -#endif /* defined(HAVE_TCGETATTR) */ +#endif /* defined(HAVE_TERMIO_H) */ +#endif /* defined(HAVE_TCGETATTR) */ void rl_add_slash(path, p) - char *path; - char *p; + char *path; + char *p; { - struct stat Sb; + struct stat Sb; if (stat(path, &Sb) >= 0) - (void)strcat(p, S_ISDIR(Sb.st_mode) ? "/" : " "); + (void)strcat(p, S_ISDIR(Sb.st_mode) ? "/" : " "); } /*