diff options
author | nytpu <alex@nytpu.com> | 2021-06-19 10:44:47 -0600 |
---|---|---|
committer | nytpu <alex@nytpu.com> | 2021-06-19 10:44:47 -0600 |
commit | b93036d7fc0fe23df54c769ab4e8d16f3e78db84 (patch) | |
tree | bf316c602e41414e48bad513214e429b15385347 | |
parent | add clang format (diff) | |
download | ed-b93036d7fc0fe23df54c769ab4e8d16f3e78db84.tar.bz2 ed-b93036d7fc0fe23df54c769ab4e8d16f3e78db84.zip |
reformat with clang-format
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | buffer.c | 13 | ||||
-rw-r--r-- | command.c | 31 | ||||
-rw-r--r-- | command.h | 6 | ||||
-rw-r--r-- | common.h | 14 | ||||
-rw-r--r-- | io.c | 35 | ||||
-rw-r--r-- | io.h | 1 | ||||
-rw-r--r-- | main.c | 50 |
8 files changed, 82 insertions, 70 deletions
@@ -65,7 +65,7 @@ clean: rm -rf $(OBJS) $(PROGNAME) $(TESTS) format: - find . (-name '*.[ch]' -a (! -name 'linenoise.*')) -exec clang-format -style=file -i {} \; + find . \( -name '*.[ch]' -a \( ! -name 'linenoise.*' \) \) -exec clang-format -style=file -i {} \; install: $(BINS) $(MANS) mkdir -p $(BINDIR) $(MANDIR)/man1 @@ -4,6 +4,7 @@ */ #include "buffer.h" + #include "common.h" #include <stdbool.h> @@ -20,13 +21,14 @@ in_bounds(RowNum at, RowNum top) void insert_row(RowNum at, char *r, size_t len) { - if (!in_bounds(at, E.numrows+1)) return; + if (!in_bounds(at, E.numrows + 1)) return; --at; - E.rows = realloc(E.rows, (E.numrows+1) * sizeof(struct Row)); + E.rows = realloc(E.rows, (E.numrows + 1) * sizeof(struct Row)); if (at != E.numrows) { - memmove(&E.rows[at+1], &E.rows[at], (E.numrows-at) * sizeof(struct Row)); - for (RowNum i = at+1; i <= E.numrows; ++i) ++E.rows[i].idx; + memmove(&E.rows[at + 1], &E.rows[at], + (E.numrows - at) * sizeof(struct Row)); + for (RowNum i = at + 1; i <= E.numrows; ++i) ++E.rows[i].idx; } E.rows[at].size = len; @@ -44,7 +46,8 @@ delete_row(RowNum at) --at; free(E.rows[at].chars); - memmove(&E.rows[at], &E.rows[at+1], (E.numrows-at-1) * sizeof(struct Row)); + memmove(&E.rows[at], &E.rows[at + 1], + (E.numrows - at - 1) * sizeof(struct Row)); for (RowNum i = at; i < E.numrows; ++i) --E.rows[i].idx; --E.numrows; E.dirty = true; @@ -4,6 +4,7 @@ */ #include "command.h" + #include "common.h" #include <ctype.h> @@ -21,14 +22,19 @@ static char *l; * Also see LICENSE_FREEBSD */ -#define SKIP_BLANKS() while (*l != '\0' && isspace(*l)) ++l -#define MUST_BE_FIRST() if (!first) {error("Invalid address"); return -1;} -#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#define SKIP_BLANKS() \ + while (*l != '\0' && isspace(*l)) ++l +#define MUST_BE_FIRST() \ + if (!first) { \ + error("Invalid address"); \ + return -1; \ + } +#define MIN(a, b) ((a) < (b) ? (a) : (b)) static RowNum parse_num(void) { - RowNum num = (RowNum) strtol(l, &l, 10); + RowNum num = (RowNum)strtol(l, &l, 10); if (num == 0) { if (errno == EINVAL) { error("Invalid address"); @@ -64,9 +70,16 @@ parse_address(struct command *c) addr += (ch == '-' || ch == '^') ? -1 : 1; } break; - case '0': case '1': case '2': - case '3': case '4': case '5': - case '6': case '7': case '8': case '9': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': MUST_BE_FIRST(); addr = parse_num(); break; @@ -88,9 +101,7 @@ parse_address(struct command *c) ++c->cnt; c->end = (ch == ';') ? E.crow : 1; RowNum a = parse_address(c); - if (a < 0) { - addr = E.numrows; - } + if (a < 0) { addr = E.numrows; } break; } // fallthrough @@ -15,10 +15,10 @@ struct command { // addresses // TODO: add mark support and regex. should marks and regex be // evaluated when parsing or by the command loop? - int cnt; // Number of addresses in command: 0, 1, or 2 + int cnt; // Number of addresses in command: 0, 1, or 2 RowNum start; // start of range - RowNum end; // end of range. also use for individual addresses, as - // single-addr commands should use end if range is given + RowNum end; // end of range. also use for individual addresses, as + // single-addr commands should use end if range is given // commands char command; // \0 is the so-called "null" command @@ -28,15 +28,15 @@ typedef long RowNum; struct Row { - RowNum idx; // Row number (zero-based) - size_t size; // Size of row (excluding trailing \0) - char *chars; // Row content (no trailing \n) -- MUST FREE + RowNum idx; // Row number (zero-based) + size_t size; // Size of row (excluding trailing \0) + char *chars; // Row content (no trailing \n) -- MUST FREE }; // doubles as an editor state/editor config struct as well struct Editor { /* actual buffer stuff */ - char *filename; // -- MUST FREE + char *filename; // -- MUST FREE RowNum numrows; struct Row *rows; // -- MUST FREE @@ -48,9 +48,9 @@ struct Editor { bool loose; /* misc state information */ - bool dirty; // has the buffer been modified since last write? - int scols; // number of columns we can display on screen - RowNum crow; // current row address - ONE INDEXED + bool dirty; // has the buffer been modified since last write? + int scols; // number of columns we can display on screen + RowNum crow; // current row address - ONE INDEXED const char *last_err; // verbose error message for previous error }; @@ -4,6 +4,7 @@ */ #include "io.h" + #include "buffer.h" #include "common.h" @@ -19,9 +20,7 @@ static char * rows_to_string(long *buflen) { long len = 0; - for (RowNum i = 0; i < E.numrows; ++i) { - len += E.rows[i].size + 1; - } + for (RowNum i = 0; i < E.numrows; ++i) { len += E.rows[i].size + 1; } *buflen = len; char *buf = malloc(len); @@ -38,9 +37,7 @@ rows_to_string(long *buflen) void open_file(const char *fn, bool force) { - if (E.dirty && !force) { - error("Buffer modified"); - } + if (E.dirty && !force) { error("Buffer modified"); } free(E.filename); E.filename = strdup(fn); @@ -49,9 +46,7 @@ open_file(const char *fn, bool force) if (!f) { // filename is already saved so we could potentially write in // the future - if (errno != ENOENT) { - die("Opening file"); - } + if (errno != ENOENT) { die("Opening file"); } return; } @@ -60,8 +55,8 @@ open_file(const char *fn, bool force) ssize_t llen; long tot = 0; while ((llen = getline(&l, &lcap, f)) != -1) { - while (llen > 0 && (l[llen-1] == '\n')) --llen; - insert_row(E.numrows+1, l, llen); + while (llen > 0 && (l[llen - 1] == '\n')) --llen; + insert_row(E.numrows + 1, l, llen); tot += llen + 1; } free(l); @@ -81,9 +76,9 @@ write_file(const char *fn) error("No current filename"); return -1; } - fd = open(E.filename, O_RDWR|O_CREAT, 0644); + fd = open(E.filename, O_RDWR | O_CREAT, 0644); } else { - fd = open(fn, O_RDWR|O_CREAT, 0644); + fd = open(fn, O_RDWR | O_CREAT, 0644); } if (fd == -1) { error("Failed to open file for writing"); @@ -115,10 +110,9 @@ print_rows(RowNum from, RowNum to) { if (!in_bounds(from, E.numrows)) return -1; if (!in_bounds(to, E.numrows)) return -1; - --from; --to; - for (; from <= to; ++from) { - printf("%s\n", E.rows[from].chars); - } + --from; + --to; + for (; from <= to; ++from) { printf("%s\n", E.rows[from].chars); } E.crow = from + 1; return 0; } @@ -128,9 +122,10 @@ print_rows_numbered(RowNum from, RowNum to) { if (!in_bounds(from, E.numrows)) return -1; if (!in_bounds(to, E.numrows)) return -1; - --from; --to; + --from; + --to; for (; from <= to; ++from) { - printf("%ld\t%s\n", from+1, E.rows[from].chars); + printf("%ld\t%s\n", from + 1, E.rows[from].chars); } E.crow = from + 1; return 0; @@ -151,7 +146,7 @@ insert_mode(RowNum at) size_t lcap = 0; ssize_t llen; while ((llen = getline(&l, &lcap, stdin)) != -1) { - while (llen > 0 && (l[llen-1] == '\n')) --llen; + while (llen > 0 && (l[llen - 1] == '\n')) --llen; if (llen == 1 && l[0] == '.') break; insert_row(at, l, llen); ++at; @@ -8,6 +8,7 @@ #define _IO_H #include "common.h" + #include <stdbool.h> // load the specified file into buffer. will re-initialize buffer @@ -31,18 +31,22 @@ static void usage(const char *cmd) { fprintf(stderr, -"Usage: %s [-hlsvV] [-p string] [file]\n\n" -"Flags:\n" -"\t-h Display this usage prompt.\n" -"\t-l Use a \"loose exit status\"; i.e. exit 0 even if a command fails.\n" -"\t-s Suppress diagnostics (if enabled), byte counts when reading\n" -"\t and writing, and '!' prompt.\n" -"\t-v Be verbose, equivalent to the 'H' command.\n" -"\t-V Display version and copyright information\n" -"\t-p 'string' Use 'string' as the interactive prompt string in command\n" -"\t mode. Also enables prompt mode. (default '*')\n" -"\nFor more help, see ed(1), ed(1p), and <https://git.nytpu.com/ed>\n", - cmd); + "Usage: %s [-hlsvV] [-p string] [file]\n\n" + "Flags:\n" + "\t-h Display this usage prompt.\n" + "\t-l Use a \"loose exit status\"; i.e. exit 0 even " + "if a command fails.\n" + "\t-s Suppress diagnostics (if enabled), byte counts " + "when reading\n" + "\t and writing, and '!' prompt.\n" + "\t-v Be verbose, equivalent to the 'H' command.\n" + "\t-V Display version and copyright information\n" + "\t-p 'string' Use 'string' as the interactive prompt string " + "in command\n" + "\t mode. Also enables prompt mode. (default '*')\n" + "\nFor more help, see ed(1), ed(1p), and " + "<https://git.nytpu.com/ed>\n", + cmd); } int @@ -70,13 +74,12 @@ main(int argc, char *argv[]) E.verbose = true; break; case 'V': - printf( - "nytpu's ed v%s\n\n" - "Copyright (c) 2021 nytpu <alex@nytpu.com>\n" - "SPDX-License-Identifier: GPL-3.0-only\n" - "For more license details, see LICENSE in the source repo <https://git.nytpu.com/ed>\n", - VERSION - ); + printf("nytpu's ed v%s\n\n" + "Copyright (c) 2021 nytpu <alex@nytpu.com>\n" + "SPDX-License-Identifier: GPL-3.0-only\n" + "For more license details, see LICENSE in the " + "source repo <https://git.nytpu.com/ed>\n", + VERSION); return EXIT_SUCCESS; default: usage(argv[0]); @@ -84,9 +87,7 @@ main(int argc, char *argv[]) } } - if (optind < argc) { - open_file(argv[optind], true); - } + if (optind < argc) { open_file(argv[optind], true); } char *line = NULL; while ((line = linenoise(E.prompt_enabled ? E.prompt : "")) != NULL) { @@ -96,7 +97,7 @@ main(int argc, char *argv[]) switch (cmd.command) { case 'a': - insert_mode(cmd.end+1); + insert_mode(cmd.end + 1); break; case 'c': if (cmd.start == 0) cmd.start = 1; @@ -174,7 +175,8 @@ editor_init(void) E.prompt = "*"; E.scols = get_width(); E.last_err = ""; - // all other fields are automatically initialized to zero, false, or NULL + // all other fields are automatically initialized to zero, false, or + // NULL // linenoise setup linenoiseHistorySetMaxLen(50); |