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 count = 0;
for (i = 0; list[i]; i++)
{
for (i = 0; list[i]; i++) {
int partlen = strlen (token); /* Part of token */
if (!strncmp (list[i], token, partlen))
{
if (!strncmp (list[i], token, partlen)) {
index = i;
matchlen = partlen;
count ++;
}
}
if (count == 1)
{
if (count == 1) {
*match = 1;
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++)
;
copy = (char **) malloc (num * sizeof(char *));
for (i = 0; i < num; i++)
{
if (!strncmp (list[i], token, strlen (token)))
{
for (i = 0; i < num; i++) {
if (!strncmp (list[i], token, strlen (token))) {
copy[total] = strdup (list[i]);
total ++;
}
@ -120,7 +116,7 @@ el_status_t do_suspend(void)
return CSstay;
}
int main(int ac __attribute__ ((unused)), char *av[] __attribute__ ((unused)))
int main(void)
{
char *line;
char *prompt = "cli> ";
@ -143,3 +139,10 @@ int main(int ac __attribute__ ((unused)), char *av[] __attribute__ ((unused)))
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:
* version-control: t
* indent-tabs-mode: t
* c-file-style: "ellemtel"
* c-file-style: "k&r"
* c-basic-offset: 4
* End:
*/

View File

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

View File

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

View File

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

View File

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