- use bb_error_msg
[oweals/busybox.git] / editors / vi.c
index bd169b09d8fdedf06e7c701df19aeb0588599a15..5bce4272f419b36527bec00af0ff681277ba288b 100644 (file)
 #if ENABLE_LOCALE_SUPPORT
 #define Isprint(c) isprint((c))
 #else
-#define Isprint(c) ( (c) >= ' ' && (c) != 127 && (c) != ((unsigned char)'\233') )
+/* 0x9b is Meta-ESC */
+#define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b)
 #endif
 
 #define MAX_SCR_COLS           BUFSIZ
 
 // Misc. non-Ascii keys that report an escape sequence
-#define VI_K_UP                        128     // cursor key Up
-#define VI_K_DOWN              129     // cursor key Down
-#define VI_K_RIGHT             130     // Cursor Key Right
-#define VI_K_LEFT              131     // cursor key Left
-#define VI_K_HOME              132     // Cursor Key Home
-#define VI_K_END               133     // Cursor Key End
-#define VI_K_INSERT            134     // Cursor Key Insert
-#define VI_K_PAGEUP            135     // Cursor Key Page Up
-#define VI_K_PAGEDOWN          136     // Cursor Key Page Down
-#define VI_K_FUN1              137     // Function Key F1
-#define VI_K_FUN2              138     // Function Key F2
-#define VI_K_FUN3              139     // Function Key F3
-#define VI_K_FUN4              140     // Function Key F4
-#define VI_K_FUN5              141     // Function Key F5
-#define VI_K_FUN6              142     // Function Key F6
-#define VI_K_FUN7              143     // Function Key F7
-#define VI_K_FUN8              144     // Function Key F8
-#define VI_K_FUN9              145     // Function Key F9
-#define VI_K_FUN10             146     // Function Key F10
-#define VI_K_FUN11             147     // Function Key F11
-#define VI_K_FUN12             148     // Function Key F12
+#define VI_K_UP                        (char)128       // cursor key Up
+#define VI_K_DOWN              (char)129       // cursor key Down
+#define VI_K_RIGHT             (char)130       // Cursor Key Right
+#define VI_K_LEFT              (char)131       // cursor key Left
+#define VI_K_HOME              (char)132       // Cursor Key Home
+#define VI_K_END               (char)133       // Cursor Key End
+#define VI_K_INSERT            (char)134       // Cursor Key Insert
+#define VI_K_PAGEUP            (char)135       // Cursor Key Page Up
+#define VI_K_PAGEDOWN          (char)136       // Cursor Key Page Down
+#define VI_K_FUN1              (char)137       // Function Key F1
+#define VI_K_FUN2              (char)138       // Function Key F2
+#define VI_K_FUN3              (char)139       // Function Key F3
+#define VI_K_FUN4              (char)140       // Function Key F4
+#define VI_K_FUN5              (char)141       // Function Key F5
+#define VI_K_FUN6              (char)142       // Function Key F6
+#define VI_K_FUN7              (char)143       // Function Key F7
+#define VI_K_FUN8              (char)144       // Function Key F8
+#define VI_K_FUN9              (char)145       // Function Key F9
+#define VI_K_FUN10             (char)146       // Function Key F10
+#define VI_K_FUN11             (char)147       // Function Key F11
+#define VI_K_FUN12             (char)148       // Function Key F12
 
 /* vt102 typical ESC sequence */
 /* terminal standout start/normal ESC sequence */
@@ -259,7 +260,9 @@ static void crash_dummy();
 static void crash_test();
 static int crashme = 0;
 #endif
+#if ENABLE_FEATURE_VI_COLON
 static char *initial_cmds[] = { NULL, NULL , NULL }; // currently 2 entries, NULL terminated
+#endif
 
 
 static void write1(const char *out)
@@ -306,12 +309,14 @@ int vi_main(int argc, char **argv)
        //  1-  process $HOME/.exrc file (not inplemented yet)
        //  2-  process EXINIT variable from environment
        //  3-  process command line args
+#if ENABLE_FEATURE_VI_COLON
        {
                char *p = getenv("EXINIT");
                if (p && *p)
                        initial_cmds[0] = xstrdup(p);
        }
-       while ((c = getopt(argc, argv, "hCRc:")) != -1) {
+#endif
+       while ((c = getopt(argc, argv, "hCR" USE_FEATURE_VI_COLON("c:"))) != -1) {
                switch (c) {
 #if ENABLE_FEATURE_VI_CRASHME
                case 'C':
@@ -327,11 +332,13 @@ int vi_main(int argc, char **argv)
                        //case 'r':     // recover flag-  ignore- we don't use tmp file
                        //case 'x':     // encryption flag- ignore
                        //case 'c':     // execute command first
+#if ENABLE_FEATURE_VI_COLON
                case 'c':               // cmd line vi command
                        if (*optarg)
                                initial_cmds[initial_cmds[0] != 0] = xstrdup(optarg);
                        break;
                        //case 'h':     // help -- just use default
+#endif
                default:
                        show_help();
                        return 1;
@@ -430,6 +437,7 @@ static void edit_file(char * fn)
        redraw(FALSE);                  // dont force every col re-draw
        show_status_line();
 
+#if ENABLE_FEATURE_VI_COLON
        {
                char *p, *q;
                int n = 0;
@@ -449,6 +457,7 @@ static void edit_file(char * fn)
                        n++;
                }
        }
+#endif
        //------This is the main Vi cmd handling loop -----------------------
        while (editing > 0) {
 #if ENABLE_FEATURE_VI_CRASHME
@@ -840,7 +849,7 @@ static void colon(char * buf)
                        int c_is_no_print;
 
                        c = *q;
-                       c_is_no_print = c > 127 && !Isprint(c);
+                       c_is_no_print = (c & 0x80) && !Isprint(c);
                        if (c_is_no_print) {
                                c = '.';
                                standout_start();
@@ -937,7 +946,9 @@ static void colon(char * buf)
                }
 #if ENABLE_FEATURE_VI_SET
        } else if (strncasecmp(cmd, "set", i) == 0) {   // set or clear features
+#if ENABLE_FEATURE_VI_SETOPTS
                char *argp;
+#endif
                i = 0;                  // offset into args
                // only blank is regarded as args delmiter. What about tab '\t' ?
                if (!args[0] || strcasecmp(args, "all") == 0) {
@@ -965,7 +976,7 @@ static void colon(char * buf)
                }
 #if ENABLE_FEATURE_VI_SETOPTS
                argp = args;
-               while (*argp) { 
+               while (*argp) {
                        if (strncasecmp(argp, "no", 2) == 0)
                                i = 2;          // ":set noautoindent"
                        setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
@@ -1922,7 +1933,7 @@ static inline void print_literal(char * buf, const char * s) // copy s to buf, c
                int c_is_no_print;
 
                c = *s;
-               c_is_no_print = c > 127 && !Isprint(c);
+               c_is_no_print = (c & 0x80) && !Isprint(c);
                if (c_is_no_print) {
                        strcat(buf, SOn);
                        c = '.';
@@ -2735,7 +2746,7 @@ static void format_line(char *dest, char *src, int li)
        char c;
 
        for (co = 0; co < MAX_SCR_COLS; co++) {
-               c= ' ';         // assume blank
+               c = ' ';                // assume blank
                if (li > 0 && co == 0) {
                        c = '~';        // not first line, assume Tilde
                }
@@ -2745,10 +2756,10 @@ static void format_line(char *dest, char *src, int li)
                }
                if (c == '\n')
                        break;
-               if (c > 127 && !Isprint(c)) {
+               if ((c & 0x80) && !Isprint(c)) {
                        c = '.';
                }
-               if (c < ' ' || c == 127) {
+               if ((unsigned char)(c) < ' ' || c == 0x7f) {
                        if (c == '\t') {
                                c = ' ';
                                //       co %    8     !=     7
@@ -2757,7 +2768,7 @@ static void format_line(char *dest, char *src, int li)
                                }
                        } else {
                                dest[co++] = '^';
-                               if (c == 127)
+                               if (c == 0x7f)
                                        c = '?';
                                else
                                        c += '@';       // make it visible
@@ -2939,7 +2950,8 @@ static void do_cmd(char c)
 
        if (cmd_mode == 2) {
                //  flip-flop Insert/Replace mode
-               if (c == VI_K_INSERT) goto dc_i;
+               if (c == VI_K_INSERT)
+                       goto dc_i;
                // we are 'R'eplacing the current *dot with new char
                if (*dot == '\n') {
                        // don't Replace past E-o-l
@@ -3048,7 +3060,7 @@ static void do_cmd(char c)
        case 'h':                       // h- move left
        case VI_K_LEFT: // cursor key Left
        case 8:         // ctrl-H- move left    (This may be ERASE char)
-       case 127:       // DEL- move left   (This may be ERASE char)
+       case 0x7f:      // DEL- move left   (This may be ERASE char)
                if (cmdcnt-- > 1) {
                        do_cmd(c);
                }                               // repeat cnt
@@ -3381,7 +3393,7 @@ static void do_cmd(char c)
                                        editing = 0;
                                }
                        }
-               } else if (strncasecmp(p, "file", cnt) == 0 ) {
+               } else if (strncasecmp(p, "file", cnt) == 0) {
                        last_status_cksum = 0;  // force status update
                } else if (sscanf(p, "%d", &j) > 0) {
                        dot = find_line(j);             // go to line # j