]> git.mdlowis.com Git - proto/aos.git/commitdiff
rework editor to use liba option parsing
authorMike Lowis <mike.lowis@gentex.com>
Wed, 3 May 2023 17:44:10 +0000 (13:44 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Wed, 3 May 2023 17:44:10 +0000 (13:44 -0400)
bin/editor/main.c

index dbccfaeec6a1b3654606275ce054b4e6170c8ce0..90e49c7981c3675b09ff001fde129e75e9b4df64 100644 (file)
@@ -561,23 +561,6 @@ static void tabs(char* arg)
     ExpandTabs = !ExpandTabs;
 }
 
-static void indent(char* arg)
-{
-    (void)arg;
-    CopyIndent = !CopyIndent;
-}
-
-static void eol_mode(char* arg)
-{
-    (void)arg;
-    DosLineFeed = !DosLineFeed;
-    View* view = win_view(EDIT);
-    view_selectall(view);
-    char* txt = view_getstr(view);
-    view_putstr(view, txt);
-    free(txt);
-}
-
 static void new_win(char* arg)
 {
     (void)arg;
@@ -590,14 +573,6 @@ static void lnexec(char* cmd)
     exec_cmdwarg(cmd, NULL);
 }
 
-static void tag_kill(char* cmd)
-{
-    (void)cmd;
-    Job* job = job_list();
-    if (job && job->fd != ConnectionNumber(X.display))
-        job_kill(job);
-}
-
 static void tag_line(char* cmd)
 {
     (void)cmd;
@@ -623,11 +598,9 @@ Tag* Builtins = (Tag[]){
     { .tag = "Cut",    .action = cut       },
     { .tag = "Copy",   .action = copy      },
     { .tag = "Del",    .action = quit      },
-    { .tag = "Eol",    .action = eol_mode  },
     { .tag = "Find",   .action = find      },
     { .tag = "GoTo",   .action = jump_to   },
     { .tag = "Get",    .action = get       },
-    { .tag = "Indent", .action = indent    },
     { .tag = "New",    .action = new_win   },
     { .tag = "Paste",  .action = paste     },
     { .tag = "Put",    .action = put       },
@@ -635,7 +608,6 @@ Tag* Builtins = (Tag[]){
     { .tag = "Tabs",   .action = tabs      },
     { .tag = "Undo",   .action = tag_undo  },
     { .tag = "Font",   .action = win_font  },
-    { .tag = "Kill",   .action = tag_kill  },
     { .tag = "Line",   .action = tag_line  },
     { .tag = "Scroll", .action = do_scroll },
     { .tag = NULL,     .action = NULL      }
@@ -703,24 +675,11 @@ static KeyBinding* Bindings = (KeyBinding[]){
 };
 
 #ifndef TEST
-static void usage(void)
-{
-    printf(
-        "Usage: %s [FLAGS] [FILE]\n"
-        "\n    -I 0,1  Enable/disable automatic indenting"
-        "\n    -W 0,1  Enable/disable trimming whitespace on save"
-        "\n    -E 0,1  Enable/disable expanding tabs to spaces"
-        "\n    -N 0,1  Enable/disable dos line ending style"
-        "\n    -T str  String to use for the tags region"
-        "\n    -C str  Set the shell to use for command execution\n"
-        "\n    -c cmd  Runs command interactively in the window\n",
-        ARGV0);
-    exit(1);
-}
-
 static void edit_file(char* file, int line_num)
 {
+    printf("%s\n", file);
     file = File_AbsolutePath(file);
+    printf("%s\n", file);
     if (!strcmp("-", file))
     {
         job_readfd(STDIN_FILENO, win_view(EDIT));
@@ -735,27 +694,34 @@ static void edit_file(char* file, int line_num)
     free(file);
 }
 
-int main(int argc, char** argv)
+/* Main Routine
+ ***************************************************************************/
+
+char* termcmd = NULL;
+long int line_num = 0;
+
+Option_T Options[] = {
+    { .s = 'h', .l = "help",  .a = 0, .d = "print this help message" },
+    { .s = 'T', .l = "tags",  .a = 1, .d = "set tag region contents" },
+    { .s = 'S', .l = "shell", .a = 1, .d = "command interpreter to use" },
+    { .s = 'c', .l = "cmd",   .a = 1, .d = "execute given command" },
+    { .s = 'l', .l = "line",  .a = 1, .d = "jump to specified line" },
+    {0}
+};
+
+void set_option(Int s, char* l, char* arg)
 {
-    char* termcmd = NULL;
-    long int line_num = 0;
-    #define BOOLARG() (EOPTARG(usage()), optarg_[0] == '0' ? 0 : 1)
-    #define STRARG()  (EOPTARG(usage()))
-    #define NUMARG()  (strtoul(EOPTARG(usage()),0,0))
-    OPTBEGIN
+    switch(s)
     {
-        case 'I': CopyIndent  = BOOLARG(); break;
-        case 'W': TrimOnSave  = BOOLARG(); break;
-        case 'E': ExpandTabs  = BOOLARG(); break;
-        case 'N': DosLineFeed = BOOLARG(); break;
-        case 'T': TagString   = STRARG();  break;
-        case 'S': ShellCmd[0] = STRARG();  break;
-        case 'c': termcmd     = STRARG();  break;
-        case 'l': line_num    = NUMARG();  break;
-        default:  usage();                 break;
+        case 'T': TagString   = arg;              break;
+        case 'S': ShellCmd[0] = arg;              break;
+        case 'c': termcmd     = arg;              break;
+        case 'l': line_num    = strtoul(arg,0,0); break;
     }
-    OPTEND;
+}
 
+int usermain(int argc, char** argv)
+{
     /* Initialize the window and views */
     exec_init(Builtins);
     win_init();
@@ -767,6 +733,8 @@ int main(int argc, char** argv)
     win_prop_set("TIDE", "", "tide");
     dbc_init(NULL, dumpdata);
 
+    printf("%s\n", *argv);
+
     /* if we still have args left we're going to open it in this instance */
     if (*argv)
     {