Whitespace + coding style cleanup

- Use Emacs K&R style
- Add missing braces to for() loops
- Fix coding style issues in cli.c example
- Add empty lines to separate sections
- Simplify and de-indent code, check error case first + continue

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2017-11-29 16:40:10 +01:00
parent 6021e54dbc
commit b613db2afb
6 changed files with 227 additions and 174 deletions

View File

@ -37,20 +37,17 @@ static char *my_rl_complete(char *token, int *match)
int matchlen = 0; int matchlen = 0;
int count = 0; int count = 0;
for (i = 0; list[i]; i++) for (i = 0; list[i]; i++) {
{
int partlen = strlen (token); /* Part of token */ int partlen = strlen (token); /* Part of token */
if (!strncmp (list[i], token, partlen)) if (!strncmp (list[i], token, partlen)) {
{
index = i; index = i;
matchlen = partlen; matchlen = partlen;
count ++; count ++;
} }
} }
if (count == 1) if (count == 1) {
{
*match = 1; *match = 1;
return strdup (list[index] + matchlen); return strdup (list[index] + matchlen);
} }
@ -66,11 +63,10 @@ static int my_rl_list_possib(char *token, char ***av)
for (num = 0; list[num]; num++) for (num = 0; list[num]; num++)
; ;
copy = (char **) malloc (num * sizeof(char *)); copy = (char **) malloc (num * sizeof(char *));
for (i = 0; i < num; i++) for (i = 0; i < num; i++) {
{ if (!strncmp (list[i], token, strlen (token))) {
if (!strncmp (list[i], token, strlen (token)))
{
copy[total] = strdup (list[i]); copy[total] = strdup (list[i]);
total ++; total ++;
} }
@ -120,7 +116,7 @@ el_status_t do_suspend(void)
return CSstay; return CSstay;
} }
int main(int ac __attribute__ ((unused)), char *av[] __attribute__ ((unused))) int main(void)
{ {
char *line; char *line;
char *prompt = "cli> "; char *prompt = "cli> ";
@ -143,3 +139,10 @@ int main(int ac __attribute__ ((unused)), char *av[] __attribute__ ((unused)))
return 0; return 0;
} }
/**
* Local Variables:
* c-file-style: "k&r"
* c-basic-offset: 4
* End:
*/

View File

@ -73,9 +73,7 @@ int main(int argc, char *argv[] __attribute__ ((unused)))
/** /**
* Local Variables: * Local Variables:
* version-control: t * c-file-style: "k&r"
* indent-tabs-mode: t
* c-file-style: "ellemtel"
* c-basic-offset: 4 * c-basic-offset: 4
* End: * End:
*/ */

View File

@ -74,6 +74,7 @@ static int FindMatches(char *dir, char *file, char ***avp)
total = 0; total = 0;
break; break;
} }
if (ac) { if (ac) {
memcpy(word, av, ac * sizeof(char *)); memcpy(word, av, ac * sizeof(char *));
free(av); free(av);
@ -94,15 +95,22 @@ static int FindMatches(char *dir, char *file, char ***avp)
closedir(dp); closedir(dp);
if (total > MAX_TOTAL_MATCHES) { if (total > MAX_TOTAL_MATCHES) {
char many[sizeof(total) * 3]; char many[sizeof(total) * 3];
p = many + sizeof(many); p = many + sizeof(many);
*--p = '\0'; *--p = '\0';
while (choices > 0) { while (choices > 0) {
*--p = '0' + choices % 10; *--p = '0' + choices % 10;
choices /= 10; choices /= 10;
} }
while (p > many + sizeof(many) - 8) *--p = ' ';
if ((p = strdup(p)) != NULL) av[ac++] = p; while (p > many + sizeof(many) - 8)
if ((p = strdup("choices")) != NULL) av[ac++] = p; *--p = ' ';
if ((p = strdup(p)) != NULL)
av[ac++] = p;
if ((p = strdup("choices")) != NULL)
av[ac++] = p;
} else { } else {
if (ac) if (ac)
qsort(av, ac, sizeof(char *), compare); qsort(av, ac, sizeof(char *), compare);
@ -121,6 +129,7 @@ static int SplitPath(char *path, char **dirpart, char **filepart)
if ((fpart = strrchr(path, '/')) == NULL) { if ((fpart = strrchr(path, '/')) == NULL) {
if ((dpart = strdup(DOT)) == NULL) if ((dpart = strdup(DOT)) == NULL)
return -1; return -1;
if ((fpart = strdup(path)) == NULL) { if ((fpart = strdup(path)) == NULL) {
free(dpart); free(dpart);
return -1; return -1;
@ -128,6 +137,7 @@ static int SplitPath(char *path, char **dirpart, char **filepart)
} else { } else {
if ((dpart = strdup(path)) == NULL) if ((dpart = strdup(path)) == NULL)
return -1; return -1;
dpart[fpart - path + 1] = '\0'; dpart[fpart - path + 1] = '\0';
if ((fpart = strdup(fpart + 1)) == NULL) { if ((fpart = strdup(fpart + 1)) == NULL) {
free(dpart); free(dpart);
@ -147,6 +157,7 @@ rl_complete_func_t *rl_set_complete_func(rl_complete_func_t *func)
{ {
rl_complete_func_t *old = el_complete_func; rl_complete_func_t *old = el_complete_func;
el_complete_func = func; el_complete_func = func;
return old; return old;
} }
@ -196,10 +207,12 @@ char *el_filename_complete(char *pathname, int *match)
*match = 0; *match = 0;
if (len) { if (len) {
/* Find largest matching substring. */ /* Find largest matching substring. */
for (i = len, end = strlen(av[0]); i < end; i++) for (i = len, end = strlen(av[0]); i < end; i++) {
for (j = 1; j < ac; j++) for (j = 1; j < ac; j++) {
if (av[0][i] != av[j][i]) if (av[0][i] != av[j][i])
goto breakout; goto breakout;
}
}
breakout: breakout:
if (i > len) { if (i > len) {
j = i - len + 1; j = i - len + 1;
@ -277,8 +290,7 @@ int rl_list_possib(char *token, char ***av)
/** /**
* Local Variables: * Local Variables:
* indent-tabs-mode: t * c-file-style: "k&r"
* c-file-style: "ellemtel"
* c-basic-offset: 4 * c-basic-offset: 4
* End: * End:
*/ */

View File

@ -161,7 +161,6 @@ static void tty_flush(void)
if (!el_no_echo) { if (!el_no_echo) {
res = write(el_outfd, Screen, ScreenCount); res = write(el_outfd, Screen, ScreenCount);
if (res > 0) if (res > 0)
ScreenCount = 0; ScreenCount = 0;
} }
@ -229,10 +228,12 @@ int rl_getc(void)
static int tty_get(void) static int tty_get(void)
{ {
tty_flush(); tty_flush();
if (el_pushed) { if (el_pushed) {
el_pushed = 0; el_pushed = 0;
return el_push_back; return el_push_back;
} }
if (*el_input) if (*el_input)
return *el_input++; return *el_input++;
@ -282,9 +283,10 @@ void el_print_columns(int ac, char **av)
int cols; int cols;
/* Find longest name, determine column count from that. */ /* Find longest name, determine column count from that. */
for (longest = 0, i = 0; i < ac; i++) for (longest = 0, i = 0; i < ac; i++) {
if ((j = strlen((char *)av[i])) > longest) if ((j = strlen((char *)av[i])) > longest)
longest = j; longest = j;
}
cols = tty_cols / (longest + 3); cols = tty_cols / (longest + 3);
tty_puts(NEWLINE); tty_puts(NEWLINE);
@ -292,10 +294,13 @@ void el_print_columns(int ac, char **av)
for (j = i; j < ac; j += skip) { for (j = i; j < ac; j += skip) {
for (p = av[j], len = strlen((char *)p), k = len; --k >= 0; p++) for (p = av[j], len = strlen((char *)p), k = len; --k >= 0; p++)
tty_put(*p); tty_put(*p);
if (j + skip < ac)
if (j + skip < ac) {
while (++len < longest + 3) while (++len < longest + 3)
tty_put(' '); tty_put(' ');
} }
}
tty_puts(NEWLINE); tty_puts(NEWLINE);
} }
} }
@ -323,6 +328,7 @@ static void left(el_status_t Change)
tty_back(); tty_back();
} }
} }
if (Change == CSmove) if (Change == CSmove)
rl_point--; rl_point--;
} }
@ -339,6 +345,7 @@ el_status_t el_ring_bell(void)
{ {
tty_put('\07'); tty_put('\07');
tty_flush(); tty_flush();
return CSstay; return CSstay;
} }
@ -355,6 +362,7 @@ static el_status_t do_macro(int c)
el_input = NILSTR; el_input = NILSTR;
return el_ring_bell(); return el_ring_bell();
} }
return CSstay; return CSstay;
} }
@ -369,14 +377,16 @@ static el_status_t do_forward(el_status_t move)
p = &rl_line_buffer[rl_point]; p = &rl_line_buffer[rl_point];
/* Skip to end of word, if inside a word. */ /* Skip to end of word, if inside a word. */
for (; rl_point < rl_end && is_alpha_num(p[0]); rl_point++, p++) for (; rl_point < rl_end && is_alpha_num(p[0]); rl_point++, p++) {
if (move == CSmove) if (move == CSmove)
right(CSstay); right(CSstay);
}
/* Skip to next word, or skip leading white space if outside a word. */ /* Skip to next word, or skip leading white space if outside a word. */
for ( ; rl_point < rl_end && (p[0] == ' ' || !is_alpha_num(p[0])); rl_point++, p++) for ( ; rl_point < rl_end && (p[0] == ' ' || !is_alpha_num(p[0])); rl_point++, p++) {
if (move == CSmove) if (move == CSmove)
right(CSstay); right(CSstay);
}
if (rl_point == rl_end) if (rl_point == rl_end)
break; break;
@ -506,7 +516,8 @@ static el_status_t insert_string(const char *p)
static el_status_t redisplay(void) static el_status_t redisplay(void)
{ {
tty_puts(NEWLINE); /* XXX: Use "\r\e[K" to get really neat effect on ANSI capable terminals. */ /* XXX: Use "\r\e[K" to get really neat effect on ANSI capable terminals. */
tty_puts(NEWLINE);
tty_puts(rl_prompt); tty_puts(rl_prompt);
tty_string(rl_line_buffer); tty_string(rl_line_buffer);
@ -550,13 +561,13 @@ static el_status_t do_insert_hist(const char *p)
static el_status_t do_hist(const char *(*move)(void)) static el_status_t do_hist(const char *(*move)(void))
{ {
const char *p; const char *p;
int i; int i = 0;
i = 0;
do { do {
if ((p = move()) == NULL) if ((p = move()) == NULL)
return el_ring_bell(); return el_ring_bell();
} while (++i < Repeat); } while (++i < Repeat);
return do_insert_hist(p); return do_insert_hist(p);
} }
@ -595,9 +606,12 @@ static int substrcmp(const char *text, const char *pat, size_t len)
if ((c = *pat) == '\0') if ((c = *pat) == '\0')
return *text == '\0'; return *text == '\0';
for ( ; *text; text++)
for ( ; *text; text++) {
if (*text == c && strncmp(text, pat, len) == 0) if (*text == c && strncmp(text, pat, len) == 0)
return 0; return 0;
}
return 1; return 1;
} }
@ -680,9 +694,8 @@ static el_status_t h_search(void)
static el_status_t fd_char(void) static el_status_t fd_char(void)
{ {
int i; int i = 0;
i = 0;
do { do {
if (rl_point >= rl_end) if (rl_point >= rl_end)
break; break;
@ -734,6 +747,7 @@ static el_status_t delete_string(int count)
*p = '\0'; *p = '\0';
return CSmove; return CSmove;
} }
if (rl_point + count > rl_end && (count = rl_end - rl_point) <= 0) if (rl_point + count > rl_end && (count = rl_end - rl_point) <= 0)
return CSstay; return CSstay;
@ -745,14 +759,14 @@ static el_status_t delete_string(int count)
ceol(); ceol();
rl_end -= count; rl_end -= count;
tty_string(&rl_line_buffer[rl_point]); tty_string(&rl_line_buffer[rl_point]);
return CSmove; return CSmove;
} }
static el_status_t bk_char(void) static el_status_t bk_char(void)
{ {
int i; int i = 0;
i = 0;
do { do {
if (rl_point == 0) if (rl_point == 0)
break; break;
@ -764,9 +778,8 @@ static el_status_t bk_char(void)
static el_status_t bk_del_char(void) static el_status_t bk_del_char(void)
{ {
int i; int i = 0;
i = 0;
do { do {
if (rl_point == 0) if (rl_point == 0)
break; break;
@ -790,6 +803,7 @@ static el_status_t kill_line(void)
right(CSmove); right(CSmove);
delete_string(Repeat - rl_point - 1); delete_string(Repeat - rl_point - 1);
} }
return CSmove; return CSmove;
} }
@ -797,6 +811,7 @@ static el_status_t kill_line(void)
rl_line_buffer[rl_point] = '\0'; rl_line_buffer[rl_point] = '\0';
ceol(); ceol();
rl_end = rl_point; rl_end = rl_point;
return CSstay; return CSstay;
} }
@ -811,6 +826,7 @@ static el_status_t insert_char(int c)
if (Repeat == NO_ARG || Repeat < 2) { if (Repeat == NO_ARG || Repeat < 2) {
buff[0] = c; buff[0] = c;
buff[1] = '\0'; buff[1] = '\0';
return insert_string(buff); return insert_string(buff);
} }
@ -834,6 +850,7 @@ static el_status_t beg_line(void)
rl_point = 0; rl_point = 0;
return CSmove; return CSmove;
} }
return CSstay; return CSstay;
} }
@ -843,6 +860,7 @@ static el_status_t end_line(void)
rl_point = rl_end; rl_point = rl_end;
return CSmove; return CSmove;
} }
return CSstay; return CSstay;
} }
@ -891,11 +909,13 @@ static el_status_t meta(void)
for (Repeat = c - '0'; (c = tty_get()) != EOF && isdigit(c); ) for (Repeat = c - '0'; (c = tty_get()) != EOF && isdigit(c); )
Repeat = Repeat * 10 + c - '0'; Repeat = Repeat * 10 + c - '0';
tty_push(c); tty_push(c);
return CSstay; return CSstay;
} }
if (isupper(c)) if (isupper(c))
return do_macro(c); return do_macro(c);
for (kp = MetaMap; kp->Function; kp++) { for (kp = MetaMap; kp->Function; kp++) {
if (kp->Key == c) if (kp->Key == c)
return kp->Function(); return kp->Function();
@ -962,12 +982,14 @@ static el_status_t tty_special(int c)
if (c == rl_erase || c == DEL) if (c == rl_erase || c == DEL)
return bk_del_char(); return bk_del_char();
if (c == rl_kill) { if (c == rl_kill) {
if (rl_point != 0) { if (rl_point != 0) {
rl_point = 0; rl_point = 0;
reposition(); reposition();
} }
Repeat = NO_ARG; Repeat = NO_ARG;
return kill_line(); return kill_line();
} }
@ -1086,11 +1108,13 @@ static char *read_redirected(void)
p += oldpos; /* Continue where we left off... */ p += oldpos; /* Continue where we left off... */
} }
if (read(el_infd, p, 1) <= 0) { if (read(el_infd, p, 1) <= 0) {
/* Ignore "incomplete" lines at EOF, just like we do for a tty. */ /* Ignore "incomplete" lines at EOF, just like we do for a tty. */
free(line); free(line);
return NULL; return NULL;
} }
if (*p == '\n') if (*p == '\n')
break; break;
p++; p++;
@ -1225,6 +1249,7 @@ char *readline(const char *prompt)
if (!isatty(0)) { if (!isatty(0)) {
tty_flush(); tty_flush();
return read_redirected(); return read_redirected();
} }
@ -1246,6 +1271,7 @@ char *readline(const char *prompt)
rl_prompt = prompt ? prompt : NILSTR; rl_prompt = prompt ? prompt : NILSTR;
if (el_no_echo) { if (el_no_echo) {
int old = el_no_echo; int old = el_no_echo;
el_no_echo = 0; el_no_echo = 0;
tty_puts(rl_prompt); tty_puts(rl_prompt);
tty_flush(); tty_flush();
@ -1273,9 +1299,10 @@ char *readline(const char *prompt)
} }
if (el_intr_pending > 0) { if (el_intr_pending > 0) {
int s = el_intr_pending; int signo = el_intr_pending;
el_intr_pending = 0; el_intr_pending = 0;
kill(getpid(), s); kill(getpid(), signo);
} }
return line; return line;
@ -1306,6 +1333,7 @@ int read_history(const char *filename)
while (H.Size < el_hist_size) { while (H.Size < el_hist_size) {
if (!fgets(buf, SCREEN_INC, fp)) if (!fgets(buf, SCREEN_INC, fp))
break; break;
buf[strlen(buf) - 1] = 0; /* Remove '\n' */ buf[strlen(buf) - 1] = 0; /* Remove '\n' */
add_history(buf); add_history(buf);
} }
@ -1480,6 +1508,7 @@ static el_status_t transpose(void)
rl_line_buffer[rl_point++] = c; rl_line_buffer[rl_point++] = c;
tty_show(c); tty_show(c);
} }
return CSstay; return CSstay;
} }
@ -1508,6 +1537,7 @@ static el_status_t exchange(void)
rl_point = c; rl_point = c;
return CSmove; return CSmove;
} }
return CSstay; return CSstay;
} }
@ -1515,6 +1545,7 @@ static el_status_t yank(void)
{ {
if (Yanked && *Yanked) if (Yanked && *Yanked)
return insert_string(Yanked); return insert_string(Yanked);
return CSstay; return CSstay;
} }
@ -1533,17 +1564,19 @@ static el_status_t copy_region(void)
static el_status_t move_to_char(void) static el_status_t move_to_char(void)
{ {
int c; int i, c;
int i;
char *p; char *p;
if ((c = tty_get()) == EOF) if ((c = tty_get()) == EOF)
return CSeof; return CSeof;
for (i = rl_point + 1, p = &rl_line_buffer[i]; i < rl_end; i++, p++)
for (i = rl_point + 1, p = &rl_line_buffer[i]; i < rl_end; i++, p++) {
if (*p == c) { if (*p == c) {
rl_point = i; rl_point = i;
return CSmove; return CSmove;
} }
}
return CSstay; return CSstay;
} }
@ -1562,6 +1595,7 @@ static el_status_t fd_kill_word(void)
rl_point = old_point; rl_point = old_point;
return delete_string(i); return delete_string(i);
} }
return CSstay; return CSstay;
} }
@ -1609,13 +1643,17 @@ static int argify(char *line, char ***avp)
for (c = line; isspace(*c); c++) for (c = line; isspace(*c); c++)
continue; continue;
if (*c == '\n' || *c == '\0') if (*c == '\n' || *c == '\0')
return 0; return 0;
for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) { for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
if (isspace(*c)) { if (!isspace(*c)) {
*c++ = '\0'; c++;
continue;
}
*c++ = '\0';
if (*c && *c != '\n') { if (*c && *c != '\n') {
if (ac + 1 == i) { if (ac + 1 == i) {
arg = malloc(sizeof(char *) * (i + MEM_INC)); arg = malloc(sizeof(char *) * (i + MEM_INC));
@ -1631,9 +1669,6 @@ static int argify(char *line, char ***avp)
} }
p[ac++] = c; p[ac++] = c;
} }
} else {
c++;
}
} }
*c = '\0'; *c = '\0';
@ -1781,8 +1816,7 @@ el_status_t el_bind_key_in_metamap(int key, el_keymap_func_t function)
/** /**
* Local Variables: * Local Variables:
* indent-tabs-mode: t * c-file-style: "k&r"
* c-file-style: "ellemtel"
* c-basic-offset: 4 * c-basic-offset: 4
* End: * End:
*/ */

View File

@ -57,8 +57,7 @@ void rl_add_slash(char *path, char *p)
/** /**
* Local Variables: * Local Variables:
* indent-tabs-mode: t * c-file-style: "k&r"
* c-file-style: "ellemtel"
* c-basic-offset: 4 * c-basic-offset: 4
* End: * End:
*/ */

View File

@ -87,6 +87,7 @@ void rl_ttyset(int Reset)
if (!Reset) { if (!Reset) {
if (-1 == getattr(0, &old)) if (-1 == getattr(0, &old))
perror("Failed tcgetattr()"); perror("Failed tcgetattr()");
rl_erase = old.c_cc[VERASE]; rl_erase = old.c_cc[VERASE];
rl_kill = old.c_cc[VKILL]; rl_kill = old.c_cc[VKILL];
rl_eof = old.c_cc[VEOF]; rl_eof = old.c_cc[VEOF];
@ -124,6 +125,7 @@ void rl_ttyset(int Reset)
if (!Reset) { if (!Reset) {
if (-1 == ioctl_wrap(0, TCGETA, &old)) if (-1 == ioctl_wrap(0, TCGETA, &old))
perror("Failed ioctl(TCGETA)"); perror("Failed ioctl(TCGETA)");
rl_erase = old.c_cc[VERASE]; rl_erase = old.c_cc[VERASE];
rl_kill = old.c_cc[VKILL]; rl_kill = old.c_cc[VKILL];
rl_eof = old.c_cc[VEOF]; rl_eof = old.c_cc[VEOF];
@ -167,11 +169,13 @@ void rl_ttyset(int Reset)
if (!Reset) { if (!Reset) {
if (-1 == ioctl_wrap(0, TIOCGETP, &old_sgttyb)) if (-1 == ioctl_wrap(0, TIOCGETP, &old_sgttyb))
perror("Failed TIOCGETP"); perror("Failed TIOCGETP");
rl_erase = old_sgttyb.sg_erase; rl_erase = old_sgttyb.sg_erase;
rl_kill = old_sgttyb.sg_kill; rl_kill = old_sgttyb.sg_kill;
if (-1 == ioctl_wrap(0, TIOCGETC, &old_tchars)) if (-1 == ioctl_wrap(0, TIOCGETC, &old_tchars))
perror("Failed TIOCGETC"); perror("Failed TIOCGETC");
rl_eof = old_tchars.t_eofc; rl_eof = old_tchars.t_eofc;
rl_intr = old_tchars.t_intrc; rl_intr = old_tchars.t_intrc;
rl_quit = old_tchars.t_quitc; rl_quit = old_tchars.t_quitc;
@ -179,6 +183,7 @@ void rl_ttyset(int Reset)
#ifdef CONFIG_SIGSTOP #ifdef CONFIG_SIGSTOP
if (-1 == ioctl_wrap(0, TIOCGLTC, &old_ltchars)) if (-1 == ioctl_wrap(0, TIOCGLTC, &old_ltchars))
perror("Failed TIOCGLTC"); perror("Failed TIOCGLTC");
rl_susp = old_ltchars.t_suspc; rl_susp = old_ltchars.t_suspc;
#endif #endif
@ -189,8 +194,10 @@ void rl_ttyset(int Reset)
new_sgttyb.sg_flags &= ~PASS8; new_sgttyb.sg_flags &= ~PASS8;
else else
new_sgttyb.sg_flags |= PASS8; new_sgttyb.sg_flags |= PASS8;
if (-1 == ioctl_wrap(0, TIOCSETP, &new_sgttyb)) if (-1 == ioctl_wrap(0, TIOCSETP, &new_sgttyb))
perror("Failed TIOCSETP"); perror("Failed TIOCSETP");
new_tchars = old_tchars; new_tchars = old_tchars;
new_tchars.t_intrc = -1; new_tchars.t_intrc = -1;
new_tchars.t_quitc = -1; new_tchars.t_quitc = -1;
@ -199,6 +206,7 @@ void rl_ttyset(int Reset)
} else { } else {
if (-1 == ioctl_wrap(0, TIOCSETP, &old_sgttyb)) if (-1 == ioctl_wrap(0, TIOCSETP, &old_sgttyb))
perror("Failed TIOCSETP"); perror("Failed TIOCSETP");
if (-1 == ioctl_wrap(0, TIOCSETC, &old_tchars)) if (-1 == ioctl_wrap(0, TIOCSETC, &old_tchars))
perror("Failed TIOCSETC"); perror("Failed TIOCSETC");
} }
@ -232,8 +240,7 @@ void rl_add_slash(char *path, char *p)
/** /**
* Local Variables: * Local Variables:
* indent-tabs-mode: t * c-file-style: "k&r"
* c-file-style: "ellemtel"
* c-basic-offset: 4 * c-basic-offset: 4
* End: * End:
*/ */