tUse suckless arg.h script for handling command-line options - ve - a minimal text editor (work in progress) (HTM) git clone git://src.adamsgaard.dk/ve (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 5a9f61927211165c1e0da5706c60d333fff846a2 (DIR) parent dc3d4c4dbb62543b349bd8a6f47e6a8f734efea3 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Sun, 11 Aug 2019 21:01:24 +0200 Use suckless arg.h script for handling command-line options Diffstat: A arg.h | 51 +++++++++++++++++++++++++++++++ M ve.c | 58 ++++++++++++++++++++++++------- 2 files changed, 96 insertions(+), 13 deletions(-) --- (DIR) diff --git a/arg.h b/arg.h t@@ -0,0 +1,50 @@ +/* + * ISC-License + * + * Copyright 2004-2017 Christoph Lohmann <20h@r-36.net> + * Copyright 2017-2018 Laslo Hunhold <dev@frign.de> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#ifndef ARG_H +#define ARG_H + +extern char *argv0; + +/* int main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, *argv ? (argc--, argv++) : ((void *)0); \ + *argv && (*argv)[0] == '-' && (*argv)[1]; argc--, argv++) { \ + int i_, argused_; \ + if ((*argv)[1] == '-' && !(*argv)[2]) { \ + argc--, argv++; \ + break; \ + } \ + for (i_ = 1, argused_ = 0; (*argv)[i_]; i_++) { \ + switch((*argv)[i_]) +#define ARGEND if (argused_) { \ + if ((*argv)[i_ + 1]) { \ + break; \ + } else { \ + argc--, argv++; \ + break; \ + } \ + } \ + } \ + } +#define ARGC() ((*argv)[i_]) +#define ARGF_(x) (((*argv)[i_ + 1]) ? (argused_ = 1, &((*argv)[i_ + 1])) : \ + (*(argv + 1)) ? (argused_ = 1, *(argv + 1)) : (x)) +#define EARGF(x) ARGF_(((x), exit(1), (char *)0)) +#define ARGF() ARGF_((char *)0) + +#endif +\ No newline at end of file (DIR) diff --git a/ve.c b/ve.c t@@ -15,6 +15,7 @@ #include <time.h> #include <unistd.h> +#include "arg.h" /* MACROS */ #define ABUF_INIT {NULL, 0} t@@ -68,7 +69,7 @@ void editor_set_status_message(const char *fmt, ...); /* GLOBAL VARIABLES */ struct editor_config E; - +char *argv0; /* FUNCTION DEFINITIONS */ t@@ -415,6 +416,7 @@ editor_concatenate_rows(int *buflen) /** FILE IO **/ +/* open file and read lines into memory */ void file_open(char *filename) { t@@ -940,13 +942,14 @@ editor_process_keypress() break; case 'q': if (E.file_changed) { - editor_set_status_message("error: " - "file has unsaved changes! " - "Press ;q! to confirm quit"); + editor_set_status_message( + "error: file has unsaved changes! " + "Press :q! to confirm quit"); break; } else { - /*write(STDOUT_FILENO, "\x1b[2J", 4);*/ /* clear screen */ - /* write(STDOUT_FILENO, "\x1b[H", 3); */ + /* clear screen */ + write(STDOUT_FILENO, "\x1b[2J", 4); + write(STDOUT_FILENO, "\x1b[H", 3); exit(0); break; } t@@ -1083,7 +1086,8 @@ deinit_editor() { /* set editor state variables, make room for status */ void -init_editor() { +init_editor() +{ E.cursor_x = 0; E.cursor_y = 0; E.cursor_rx = 0; t@@ -1101,21 +1105,49 @@ init_editor() { E.find_query = NULL; } +void +usage() +{ + printf("%s: %s [OPTIONS] [FILES]\n" + "\nOptional arguments:\n" + " -h show this message\n" + " -v show version information\n", + __func__, PROGNAME); +} + +void +version() +{ + printf("%s version %s\n" + "Licensed under the GNU Public License, v3+\n" + "written by Anders Damsgaard, anders@adamsgaard.dk\n" + "https://gitlab.com/admesg/ve\n", + PROGNAME, VERSION); +} /** MAIN **/ int -main(int argc, char* argv[]) +main(int argc, char *argv[]) { + ARGBEGIN { + case 'h': + usage(); + return 0; + case 'v': + version(); + return 0; + default: + usage(); + } ARGEND; + enable_raw_mode(); init_editor(); - /* TODO: proper argument handling */ - if (argc >= 2) { - file_open(argv[1]); - } - editor_set_status_message("%s %s", PROGNAME, VERSION); + if (argv[0]) + file_open(argv[0]); + while (1) { editor_update_screen_size(); editor_refresh_screen();