diff --git a/src/shell/linenoise/linenoise.c b/src/shell/linenoise/linenoise.c index 683fd6f820..fce14a7c53 100644 --- a/src/shell/linenoise/linenoise.c +++ b/src/shell/linenoise/linenoise.c @@ -119,15 +119,15 @@ #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_MAX_LINE 4096 -static char *unsupported_term[] = {"dumb", "cons25", "emacs", NULL}; +static char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; static linenoiseCompletionCallback *completionCallback = NULL; static linenoiseHintsCallback *hintsCallback = NULL; static linenoiseFreeHintsCallback *freeHintsCallback = NULL; static struct termios orig_termios; /* In order to restore at exit.*/ -static int rawmode = 0; /* For atexit() function to check if restore is needed*/ -static int mlmode = 0; /* Multi line mode. Default is single line. */ -static int atexit_registered = 0; /* Register atexit just 1 time. */ +static int rawmode = 0; /* For atexit() function to check if restore is needed*/ +static int mlmode = 0; /* Multi line mode. Default is single line. */ +static int atexit_registered = 0; /* Register atexit just 1 time. */ static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN; static int history_len = 0; static char **history = NULL; @@ -135,8 +135,7 @@ static char **history = NULL; /* The linenoiseState structure represents the state during line editing. * We pass this state to functions implementing specific editing * functionalities. */ -struct linenoiseState -{ +struct linenoiseState { int ifd; /* Terminal stdin file descriptor. */ int ofd; /* Terminal stdout file descriptor. */ char *buf; /* Edited line buffer. */ @@ -151,27 +150,26 @@ struct linenoiseState int history_index; /* The history index we are currently editing. */ }; -enum KEY_ACTION -{ - KEY_NULL = 0, /* NULL */ - CTRL_A = 1, /* Ctrl+a */ - CTRL_B = 2, /* Ctrl-b */ - CTRL_C = 3, /* Ctrl-c */ - CTRL_D = 4, /* Ctrl-d */ - CTRL_E = 5, /* Ctrl-e */ - CTRL_F = 6, /* Ctrl-f */ - CTRL_H = 8, /* Ctrl-h */ - TAB = 9, /* Tab */ - CTRL_K = 11, /* Ctrl+k */ - CTRL_L = 12, /* Ctrl+l */ - ENTER = 13, /* Enter */ - CTRL_N = 14, /* Ctrl-n */ - CTRL_P = 16, /* Ctrl-p */ - CTRL_T = 20, /* Ctrl-t */ - CTRL_U = 21, /* Ctrl+u */ - CTRL_W = 23, /* Ctrl+w */ - ESC = 27, /* Escape */ - BACKSPACE = 127 /* Backspace */ +enum KEY_ACTION{ + KEY_NULL = 0, /* NULL */ + CTRL_A = 1, /* Ctrl+a */ + CTRL_B = 2, /* Ctrl-b */ + CTRL_C = 3, /* Ctrl-c */ + CTRL_D = 4, /* Ctrl-d */ + CTRL_E = 5, /* Ctrl-e */ + CTRL_F = 6, /* Ctrl-f */ + CTRL_H = 8, /* Ctrl-h */ + TAB = 9, /* Tab */ + CTRL_K = 11, /* Ctrl+k */ + CTRL_L = 12, /* Ctrl+l */ + ENTER = 13, /* Enter */ + CTRL_N = 14, /* Ctrl-n */ + CTRL_P = 16, /* Ctrl-p */ + CTRL_T = 20, /* Ctrl-t */ + CTRL_U = 21, /* Ctrl+u */ + CTRL_W = 23, /* Ctrl+w */ + ESC = 27, /* Escape */ + BACKSPACE = 127 /* Backspace */ }; static void linenoiseAtExit(void); @@ -181,23 +179,17 @@ static void refreshLine(struct linenoiseState *l); /* Debugging macro. */ #if 0 FILE *lndebug_fp = NULL; -#define lndebug(...) \ - do { \ - if (lndebug_fp == NULL) { \ - lndebug_fp = fopen("/tmp/lndebug.txt", "a"); \ - fprintf(lndebug_fp, \ - "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \ - (int)l->len, \ - (int)l->pos, \ - (int)l->oldpos, \ - plen, \ - rows, \ - rpos, \ - (int)l->maxrows, \ - old_rows); \ - } \ - fprintf(lndebug_fp, ", " __VA_ARGS__); \ - fflush(lndebug_fp); \ +#define lndebug(...) \ + do { \ + if (lndebug_fp == NULL) { \ + lndebug_fp = fopen("/tmp/lndebug.txt","a"); \ + fprintf(lndebug_fp, \ + "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \ + (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \ + (int)l->maxrows,old_rows); \ + } \ + fprintf(lndebug_fp, ", " __VA_ARGS__); \ + fflush(lndebug_fp); \ } while (0) #else #define lndebug(fmt, ...) @@ -206,38 +198,34 @@ FILE *lndebug_fp = NULL; /* ======================= Low level terminal handling ====================== */ /* Set if to use or not the multi line mode. */ -void linenoiseSetMultiLine(int ml) { mlmode = ml; } +void linenoiseSetMultiLine(int ml) { + mlmode = ml; +} /* Return true if the terminal name is in the list of terminals we know are * not able to understand basic escape sequences. */ -static int isUnsupportedTerm(void) -{ +static int isUnsupportedTerm(void) { char *term = getenv("TERM"); int j; - if (term == NULL) - return 0; + if (term == NULL) return 0; for (j = 0; unsupported_term[j]; j++) - if (!strcasecmp(term, unsupported_term[j])) - return 1; + if (!strcasecmp(term,unsupported_term[j])) return 1; return 0; } /* Raw mode: 1960 magic shit. */ -static int enableRawMode(int fd) -{ +static int enableRawMode(int fd) { struct termios raw; - if (!isatty(STDIN_FILENO)) - goto fatal; + if (!isatty(STDIN_FILENO)) goto fatal; if (!atexit_registered) { atexit(linenoiseAtExit); atexit_registered = 1; } - if (tcgetattr(fd, &orig_termios) == -1) - goto fatal; + if (tcgetattr(fd,&orig_termios) == -1) goto fatal; - raw = orig_termios; /* modify the original mode */ + raw = orig_termios; /* modify the original mode */ /* input modes: no break, no CR to NL, no parity check, no strip char, * no start/stop output control. */ raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); @@ -250,12 +238,10 @@ static int enableRawMode(int fd) raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); /* control chars - set return condition: min number of bytes and timer. * We want read to return every single byte, without timeout. */ - raw.c_cc[VMIN] = 1; - raw.c_cc[VTIME] = 0; /* 1 byte, no timer */ + raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */ /* put terminal in raw mode after flushing */ - if (tcsetattr(fd, TCSAFLUSH, &raw) < 0) - goto fatal; + if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal; rawmode = 1; return 0; @@ -264,48 +250,40 @@ static int enableRawMode(int fd) return -1; } -static void disableRawMode(int fd) -{ +static void disableRawMode(int fd) { /* Don't even check the return value as it's too late. */ - if (rawmode && tcsetattr(fd, TCSAFLUSH, &orig_termios) != -1) + if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1) rawmode = 0; } /* Use the ESC [6n escape sequence to query the horizontal cursor position * and return it. On error -1 is returned, on success the position of the * cursor. */ -static int getCursorPosition(int ifd, int ofd) -{ +static int getCursorPosition(int ifd, int ofd) { char buf[32]; int cols, rows; unsigned int i = 0; /* Report cursor location */ - if (write(ofd, "\x1b[6n", 4) != 4) - return -1; + if (write(ofd, "\x1b[6n", 4) != 4) return -1; /* Read the response: ESC [ rows ; cols R */ - while (i < sizeof(buf) - 1) { - if (read(ifd, buf + i, 1) != 1) - break; - if (buf[i] == 'R') - break; + while (i < sizeof(buf)-1) { + if (read(ifd,buf+i,1) != 1) break; + if (buf[i] == 'R') break; i++; } buf[i] = '\0'; /* Parse it. */ - if (buf[0] != ESC || buf[1] != '[') - return -1; - if (sscanf(buf + 2, "%d;%d", &rows, &cols) != 2) - return -1; + if (buf[0] != ESC || buf[1] != '[') return -1; + if (sscanf(buf+2,"%d;%d",&rows,&cols) != 2) return -1; return cols; } /* Try to get the number of columns in the current terminal, or assume 80 * if it fails. */ -static int getColumns(int ifd, int ofd) -{ +static int getColumns(int ifd, int ofd) { struct winsize ws; if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { @@ -313,22 +291,19 @@ static int getColumns(int ifd, int ofd) int start, cols; /* Get the initial position so we can restore it later. */ - start = getCursorPosition(ifd, ofd); - if (start == -1) - goto failed; + start = getCursorPosition(ifd,ofd); + if (start == -1) goto failed; /* Go to right margin and get position. */ - if (write(ofd, "\x1b[999C", 6) != 6) - goto failed; - cols = getCursorPosition(ifd, ofd); - if (cols == -1) - goto failed; + if (write(ofd,"\x1b[999C",6) != 6) goto failed; + cols = getCursorPosition(ifd,ofd); + if (cols == -1) goto failed; /* Restore position. */ if (cols > start) { char seq[32]; - snprintf(seq, 32, "\x1b[%dD", cols - start); - if (write(ofd, seq, strlen(seq)) == -1) { + snprintf(seq,32,"\x1b[%dD",cols-start); + if (write(ofd,seq,strlen(seq)) == -1) { /* Can't recover... */ } } @@ -342,17 +317,15 @@ static int getColumns(int ifd, int ofd) } /* Clear the screen. Used to handle ctrl+l */ -void linenoiseClearScreen(void) -{ - if (write(STDOUT_FILENO, "\x1b[H\x1b[2J", 7) <= 0) { +void linenoiseClearScreen(void) { + if (write(STDOUT_FILENO,"\x1b[H\x1b[2J",7) <= 0) { /* nothing to do, just to avoid warning. */ } } /* Beep, used for completion when there is nothing to complete or when all * the choices were already shown. */ -static void linenoiseBeep(void) -{ +static void linenoiseBeep(void) { fprintf(stderr, "\x7"); fflush(stderr); } @@ -360,8 +333,7 @@ static void linenoiseBeep(void) /* ============================== Completion ================================ */ /* Free a list of completion option populated by linenoiseAddCompletion(). */ -static void freeCompletions(linenoiseCompletions *lc) -{ +static void freeCompletions(linenoiseCompletions *lc) { size_t i; for (i = 0; i < lc->len; i++) free(lc->cvec[i]); @@ -375,19 +347,18 @@ static void freeCompletions(linenoiseCompletions *lc) * * The state of the editing is encapsulated into the pointed linenoiseState * structure as described in the structure definition. */ -static int completeLine(struct linenoiseState *ls) -{ - linenoiseCompletions lc = {0, NULL}; +static int completeLine(struct linenoiseState *ls) { + linenoiseCompletions lc = { 0, NULL }; int nread, nwritten; char c = 0; - completionCallback(ls->buf, &lc); + completionCallback(ls->buf,&lc); if (lc.len == 0) { linenoiseBeep(); } else { size_t stop = 0, i = 0; - while (!stop) { + while(!stop) { /* Show completion or original buffer */ if (i < lc.len) { struct linenoiseState saved = *ls; @@ -402,32 +373,30 @@ static int completeLine(struct linenoiseState *ls) refreshLine(ls); } - nread = read(ls->ifd, &c, 1); + nread = read(ls->ifd,&c,1); if (nread <= 0) { freeCompletions(&lc); return -1; } - switch (c) { - case 9: /* tab */ - i = (i + 1) % (lc.len + 1); - if (i == lc.len) - linenoiseBeep(); - break; - case 27: /* escape */ - /* Re-show original buffer */ - if (i < lc.len) - refreshLine(ls); - stop = 1; - break; - default: - /* Update buffer and return */ - if (i < lc.len) { - nwritten = snprintf(ls->buf, ls->buflen, "%s", lc.cvec[i]); - ls->len = ls->pos = nwritten; - } - stop = 1; - break; + switch(c) { + case 9: /* tab */ + i = (i+1) % (lc.len+1); + if (i == lc.len) linenoiseBeep(); + break; + case 27: /* escape */ + /* Re-show original buffer */ + if (i < lc.len) refreshLine(ls); + stop = 1; + break; + default: + /* Update buffer and return */ + if (i < lc.len) { + nwritten = snprintf(ls->buf,ls->buflen,"%s",lc.cvec[i]); + ls->len = ls->pos = nwritten; + } + stop = 1; + break; } } } @@ -437,30 +406,34 @@ static int completeLine(struct linenoiseState *ls) } /* Register a callback function to be called for tab-completion. */ -void linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) { completionCallback = fn; } +void linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) { + completionCallback = fn; +} /* Register a hits function to be called to show hits to the user at the * right of the prompt. */ -void linenoiseSetHintsCallback(linenoiseHintsCallback *fn) { hintsCallback = fn; } +void linenoiseSetHintsCallback(linenoiseHintsCallback *fn) { + hintsCallback = fn; +} /* Register a function to free the hints returned by the hints callback * registered with linenoiseSetHintsCallback(). */ -void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *fn) { freeHintsCallback = fn; } +void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *fn) { + freeHintsCallback = fn; +} /* This function is used by the callback function registered by the user * in order to add completion options given the input string when the * user typed . See the example.c source code for a very easy to * understand example. */ -void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) -{ +void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) { size_t len = strlen(str); char *copy, **cvec; - copy = malloc(len + 1); - if (copy == NULL) - return; - memcpy(copy, str, len + 1); - cvec = realloc(lc->cvec, sizeof(char *) * (lc->len + 1)); + copy = malloc(len+1); + if (copy == NULL) return; + memcpy(copy,str,len+1); + cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1)); if (cvec == NULL) { free(copy); return; @@ -475,55 +448,49 @@ void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) * allocated string where we can append to. This is useful in order to * write all the escape sequences in a buffer and flush them to the standard * output in a single call, to avoid flickering effects. */ -struct abuf -{ +struct abuf { char *b; int len; }; -static void abInit(struct abuf *ab) -{ +static void abInit(struct abuf *ab) { ab->b = NULL; ab->len = 0; } -static void abAppend(struct abuf *ab, const char *s, int len) -{ - char *new = realloc(ab->b, ab->len + len); +static void abAppend(struct abuf *ab, const char *s, int len) { + char *new = realloc(ab->b,ab->len+len); - if (new == NULL) - return; - memcpy(new + ab->len, s, len); + if (new == NULL) return; + memcpy(new+ab->len,s,len); ab->b = new; ab->len += len; } -static void abFree(struct abuf *ab) { free(ab->b); } +static void abFree(struct abuf *ab) { + free(ab->b); +} /* Helper of refreshSingleLine() and refreshMultiLine() to show hints * to the right of the prompt. */ -void refreshShowHints(struct abuf *ab, struct linenoiseState *l, int plen) -{ +void refreshShowHints(struct abuf *ab, struct linenoiseState *l, int plen) { char seq[64]; - if (hintsCallback && plen + l->len < l->cols) { + if (hintsCallback && plen+l->len < l->cols) { int color = -1, bold = 0; - char *hint = hintsCallback(l->buf, &color, &bold); + char *hint = hintsCallback(l->buf,&color,&bold); if (hint) { int hintlen = strlen(hint); - int hintmaxlen = l->cols - (plen + l->len); - if (hintlen > hintmaxlen) - hintlen = hintmaxlen; - if (bold == 1 && color == -1) - color = 37; + int hintmaxlen = l->cols-(plen+l->len); + if (hintlen > hintmaxlen) hintlen = hintmaxlen; + if (bold == 1 && color == -1) color = 37; if (color != -1 || bold != 0) - snprintf(seq, 64, "\033[%d;%d;49m", bold, color); - abAppend(ab, seq, strlen(seq)); - abAppend(ab, hint, hintlen); + snprintf(seq,64,"\033[%d;%d;49m",bold,color); + abAppend(ab,seq,strlen(seq)); + abAppend(ab,hint,hintlen); if (color != -1 || bold != 0) - abAppend(ab, "\033[0m", 4); + abAppend(ab,"\033[0m",4); /* Call the function to free the hint returned. */ - if (freeHintsCallback) - freeHintsCallback(hint); + if (freeHintsCallback) freeHintsCallback(hint); } } } @@ -532,8 +499,7 @@ void refreshShowHints(struct abuf *ab, struct linenoiseState *l, int plen) * * Rewrite the currently edited line accordingly to the buffer content, * cursor position, and number of columns of the terminal. */ -static void refreshSingleLine(struct linenoiseState *l) -{ +static void refreshSingleLine(struct linenoiseState *l) { char seq[64]; size_t plen = strlen(l->prompt); int fd = l->ofd; @@ -542,32 +508,31 @@ static void refreshSingleLine(struct linenoiseState *l) size_t pos = l->pos; struct abuf ab; - while ((plen + pos) >= l->cols) { + while((plen+pos) >= l->cols) { buf++; len--; pos--; } - while (plen + len > l->cols) { + while (plen+len > l->cols) { len--; } abInit(&ab); /* Cursor to left edge */ - snprintf(seq, 64, "\r"); - abAppend(&ab, seq, strlen(seq)); + snprintf(seq,64,"\r"); + abAppend(&ab,seq,strlen(seq)); /* Write the prompt and the current buffer content */ - abAppend(&ab, l->prompt, strlen(l->prompt)); - abAppend(&ab, buf, len); + abAppend(&ab,l->prompt,strlen(l->prompt)); + abAppend(&ab,buf,len); /* Show hits if any. */ - refreshShowHints(&ab, l, plen); + refreshShowHints(&ab,l,plen); /* Erase to right */ - snprintf(seq, 64, "\x1b[0K"); - abAppend(&ab, seq, strlen(seq)); + snprintf(seq,64,"\x1b[0K"); + abAppend(&ab,seq,strlen(seq)); /* Move cursor to original position. */ - snprintf(seq, 64, "\r\x1b[%dC", (int)(pos + plen)); - abAppend(&ab, seq, strlen(seq)); - if (write(fd, ab.b, ab.len) == -1) { - } /* Can't recover from write error. */ + snprintf(seq,64,"\r\x1b[%dC", (int)(pos+plen)); + abAppend(&ab,seq,strlen(seq)); + if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */ abFree(&ab); } @@ -575,94 +540,92 @@ static void refreshSingleLine(struct linenoiseState *l) * * Rewrite the currently edited line accordingly to the buffer content, * cursor position, and number of columns of the terminal. */ -static void refreshMultiLine(struct linenoiseState *l) -{ +static void refreshMultiLine(struct linenoiseState *l) { char seq[64]; int plen = strlen(l->prompt); - int rows = (plen + l->len + l->cols - 1) / l->cols; /* rows used by current buf. */ - int rpos = (plen + l->oldpos + l->cols) / l->cols; /* cursor relative row. */ - int rpos2; /* rpos after refresh. */ - int col; /* colum position, zero-based. */ + int rows = (plen+l->len+l->cols-1)/l->cols; /* rows used by current buf. */ + int rpos = (plen+l->oldpos+l->cols)/l->cols; /* cursor relative row. */ + int rpos2; /* rpos after refresh. */ + int col; /* colum position, zero-based. */ int old_rows = l->maxrows; int fd = l->ofd, j; struct abuf ab; /* Update maxrows if needed. */ - if (rows > (int)l->maxrows) - l->maxrows = rows; + if (rows > (int)l->maxrows) l->maxrows = rows; /* First step: clear all the lines used before. To do so start by * going to the last row. */ abInit(&ab); - if (old_rows - rpos > 0) { - lndebug("go down %d", old_rows - rpos); - snprintf(seq, 64, "\x1b[%dB", old_rows - rpos); - abAppend(&ab, seq, strlen(seq)); + if (old_rows-rpos > 0) { + lndebug("go down %d", old_rows-rpos); + snprintf(seq,64,"\x1b[%dB", old_rows-rpos); + abAppend(&ab,seq,strlen(seq)); } /* Now for every row clear it, go up. */ - for (j = 0; j < old_rows - 1; j++) { + for (j = 0; j < old_rows-1; j++) { lndebug("clear+up"); - snprintf(seq, 64, "\r\x1b[0K\x1b[1A"); - abAppend(&ab, seq, strlen(seq)); + snprintf(seq,64,"\r\x1b[0K\x1b[1A"); + abAppend(&ab,seq,strlen(seq)); } /* Clean the top line. */ lndebug("clear"); - snprintf(seq, 64, "\r\x1b[0K"); - abAppend(&ab, seq, strlen(seq)); + snprintf(seq,64,"\r\x1b[0K"); + abAppend(&ab,seq,strlen(seq)); /* Write the prompt and the current buffer content */ - abAppend(&ab, l->prompt, strlen(l->prompt)); - abAppend(&ab, l->buf, l->len); + abAppend(&ab,l->prompt,strlen(l->prompt)); + abAppend(&ab,l->buf,l->len); /* Show hits if any. */ - refreshShowHints(&ab, l, plen); + refreshShowHints(&ab,l,plen); /* If we are at the very end of the screen with our prompt, we need to * emit a newline and move the prompt to the first column. */ - if (l->pos && l->pos == l->len && (l->pos + plen) % l->cols == 0) { + if (l->pos && + l->pos == l->len && + (l->pos+plen) % l->cols == 0) + { lndebug(""); - abAppend(&ab, "\n", 1); - snprintf(seq, 64, "\r"); - abAppend(&ab, seq, strlen(seq)); + abAppend(&ab,"\n",1); + snprintf(seq,64,"\r"); + abAppend(&ab,seq,strlen(seq)); rows++; - if (rows > (int)l->maxrows) - l->maxrows = rows; + if (rows > (int)l->maxrows) l->maxrows = rows; } /* Move cursor to right position. */ - rpos2 = (plen + l->pos + l->cols) / l->cols; /* current cursor relative row. */ + rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */ lndebug("rpos2 %d", rpos2); /* Go up till we reach the expected positon. */ - if (rows - rpos2 > 0) { - lndebug("go-up %d", rows - rpos2); - snprintf(seq, 64, "\x1b[%dA", rows - rpos2); - abAppend(&ab, seq, strlen(seq)); + if (rows-rpos2 > 0) { + lndebug("go-up %d", rows-rpos2); + snprintf(seq,64,"\x1b[%dA", rows-rpos2); + abAppend(&ab,seq,strlen(seq)); } /* Set column. */ - col = (plen + (int)l->pos) % (int)l->cols; - lndebug("set col %d", 1 + col); + col = (plen+(int)l->pos) % (int)l->cols; + lndebug("set col %d", 1+col); if (col) - snprintf(seq, 64, "\r\x1b[%dC", col); + snprintf(seq,64,"\r\x1b[%dC", col); else - snprintf(seq, 64, "\r"); - abAppend(&ab, seq, strlen(seq)); + snprintf(seq,64,"\r"); + abAppend(&ab,seq,strlen(seq)); lndebug("\n"); l->oldpos = l->pos; - if (write(fd, ab.b, ab.len) == -1) { - } /* Can't recover from write error. */ + if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */ abFree(&ab); } /* Calls the two low level functions refreshSingleLine() or * refreshMultiLine() according to the selected mode. */ -static void refreshLine(struct linenoiseState *l) -{ +static void refreshLine(struct linenoiseState *l) { if (mlmode) refreshMultiLine(l); else @@ -672,24 +635,22 @@ static void refreshLine(struct linenoiseState *l) /* Insert the character 'c' at cursor current position. * * On error writing to the terminal -1 is returned, otherwise 0. */ -int linenoiseEditInsert(struct linenoiseState *l, char c) -{ +int linenoiseEditInsert(struct linenoiseState *l, char c) { if (l->len < l->buflen) { if (l->len == l->pos) { l->buf[l->pos] = c; l->pos++; l->len++; l->buf[l->len] = '\0'; - if ((!mlmode && l->plen + l->len < l->cols && !hintsCallback)) { + if ((!mlmode && l->plen+l->len < l->cols && !hintsCallback)) { /* Avoid a full update of the line in the * trivial case. */ - if (write(l->ofd, &c, 1) == -1) - return -1; + if (write(l->ofd,&c,1) == -1) return -1; } else { refreshLine(l); } } else { - memmove(l->buf + l->pos + 1, l->buf + l->pos, l->len - l->pos); + memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos); l->buf[l->pos] = c; l->len++; l->pos++; @@ -701,8 +662,7 @@ int linenoiseEditInsert(struct linenoiseState *l, char c) } /* Move cursor on the left. */ -void linenoiseEditMoveLeft(struct linenoiseState *l) -{ +void linenoiseEditMoveLeft(struct linenoiseState *l) { if (l->pos > 0) { l->pos--; refreshLine(l); @@ -710,8 +670,7 @@ void linenoiseEditMoveLeft(struct linenoiseState *l) } /* Move cursor on the right. */ -void linenoiseEditMoveRight(struct linenoiseState *l) -{ +void linenoiseEditMoveRight(struct linenoiseState *l) { if (l->pos != l->len) { l->pos++; refreshLine(l); @@ -719,8 +678,7 @@ void linenoiseEditMoveRight(struct linenoiseState *l) } /* Move cursor to the start of the line. */ -void linenoiseEditMoveHome(struct linenoiseState *l) -{ +void linenoiseEditMoveHome(struct linenoiseState *l) { if (l->pos != 0) { l->pos = 0; refreshLine(l); @@ -728,8 +686,7 @@ void linenoiseEditMoveHome(struct linenoiseState *l) } /* Move cursor to the end of the line. */ -void linenoiseEditMoveEnd(struct linenoiseState *l) -{ +void linenoiseEditMoveEnd(struct linenoiseState *l) { if (l->pos != l->len) { l->pos = l->len; refreshLine(l); @@ -740,8 +697,7 @@ void linenoiseEditMoveEnd(struct linenoiseState *l) * entry as specified by 'dir'. */ #define LINENOISE_HISTORY_NEXT 0 #define LINENOISE_HISTORY_PREV 1 -void linenoiseEditHistoryNext(struct linenoiseState *l, int dir) -{ +void linenoiseEditHistoryNext(struct linenoiseState *l, int dir) { if (history_len > 1) { /* Update the current history entry before to * overwrite it with the next one. */ @@ -753,11 +709,11 @@ void linenoiseEditHistoryNext(struct linenoiseState *l, int dir) l->history_index = 0; return; } else if (l->history_index >= history_len) { - l->history_index = history_len - 1; + l->history_index = history_len-1; return; } - strncpy(l->buf, history[history_len - 1 - l->history_index], l->buflen); - l->buf[l->buflen - 1] = '\0'; + strncpy(l->buf,history[history_len - 1 - l->history_index],l->buflen); + l->buf[l->buflen-1] = '\0'; l->len = l->pos = strlen(l->buf); refreshLine(l); } @@ -765,10 +721,9 @@ void linenoiseEditHistoryNext(struct linenoiseState *l, int dir) /* Delete the character at the right of the cursor without altering the cursor * position. Basically this is what happens with the "Delete" keyboard key. */ -void linenoiseEditDelete(struct linenoiseState *l) -{ +void linenoiseEditDelete(struct linenoiseState *l) { if (l->len > 0 && l->pos < l->len) { - memmove(l->buf + l->pos, l->buf + l->pos + 1, l->len - l->pos - 1); + memmove(l->buf+l->pos,l->buf+l->pos+1,l->len-l->pos-1); l->len--; l->buf[l->len] = '\0'; refreshLine(l); @@ -776,10 +731,9 @@ void linenoiseEditDelete(struct linenoiseState *l) } /* Backspace implementation. */ -void linenoiseEditBackspace(struct linenoiseState *l) -{ +void linenoiseEditBackspace(struct linenoiseState *l) { if (l->pos > 0 && l->len > 0) { - memmove(l->buf + l->pos - 1, l->buf + l->pos, l->len - l->pos); + memmove(l->buf+l->pos-1,l->buf+l->pos,l->len-l->pos); l->pos--; l->len--; l->buf[l->len] = '\0'; @@ -789,17 +743,16 @@ void linenoiseEditBackspace(struct linenoiseState *l) /* Delete the previosu word, maintaining the cursor at the start of the * current word. */ -void linenoiseEditDeletePrevWord(struct linenoiseState *l) -{ +void linenoiseEditDeletePrevWord(struct linenoiseState *l) { size_t old_pos = l->pos; size_t diff; - while (l->pos > 0 && l->buf[l->pos - 1] == ' ') + while (l->pos > 0 && l->buf[l->pos-1] == ' ') l->pos--; - while (l->pos > 0 && l->buf[l->pos - 1] != ' ') + while (l->pos > 0 && l->buf[l->pos-1] != ' ') l->pos--; diff = old_pos - l->pos; - memmove(l->buf + l->pos, l->buf + old_pos, l->len - old_pos + 1); + memmove(l->buf+l->pos,l->buf+old_pos,l->len-old_pos+1); l->len -= diff; refreshLine(l); } @@ -838,16 +791,14 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, * initially is just an empty string. */ linenoiseHistoryAdd(""); - if (write(l.ofd, prompt, l.plen) == -1) - return -1; - while (1) { + if (write(l.ofd,prompt,l.plen) == -1) return -1; + while(1) { char c; int nread; char seq[3]; - nread = read(l.ifd, &c, 1); - if (nread <= 0) - return l.len; + nread = read(l.ifd,&c,1); + if (nread <= 0) return l.len; /* Only autocomplete when the callback is set. It returns < 0 when * there was an error reading from fd. Otherwise it will return the @@ -855,19 +806,16 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, if (c == 9 && completionCallback != NULL) { c = completeLine(&l); /* Return on errors */ - if (c < 0) - return l.len; + if (c < 0) return l.len; /* Read next character when 0 */ - if (c == 0) - continue; + if (c == 0) continue; } - switch (c) { - case ENTER: /* enter */ + switch(c) { + case ENTER: /* enter */ history_len--; free(history[history_len]); - if (mlmode) - linenoiseEditMoveEnd(&l); + if (mlmode) linenoiseEditMoveEnd(&l); if (hintsCallback) { /* Force a refresh without hints to leave the previous * line as the user typed it after a newline. */ @@ -877,15 +825,15 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, hintsCallback = hc; } return (int)l.len; - case CTRL_C: /* ctrl-c */ + case CTRL_C: /* ctrl-c */ errno = EAGAIN; return -1; - case BACKSPACE: /* backspace */ - case 8: /* ctrl-h */ + case BACKSPACE: /* backspace */ + case 8: /* ctrl-h */ linenoiseEditBackspace(&l); break; - case CTRL_D: /* ctrl-d, remove char at right of cursor, or if the - line is empty, act as end-of-file. */ + case CTRL_D: /* ctrl-d, remove char at right of cursor, or if the + line is empty, act as end-of-file. */ if (l.len > 0) { linenoiseEditDelete(&l); } else { @@ -894,52 +842,48 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, return -1; } break; - case CTRL_T: /* ctrl-t, swaps current character with previous. */ + case CTRL_T: /* ctrl-t, swaps current character with previous. */ if (l.pos > 0 && l.pos < l.len) { - int aux = buf[l.pos - 1]; - buf[l.pos - 1] = buf[l.pos]; + int aux = buf[l.pos-1]; + buf[l.pos-1] = buf[l.pos]; buf[l.pos] = aux; - if (l.pos != l.len - 1) - l.pos++; + if (l.pos != l.len-1) l.pos++; refreshLine(&l); } break; - case CTRL_B: /* ctrl-b */ + case CTRL_B: /* ctrl-b */ linenoiseEditMoveLeft(&l); break; - case CTRL_F: /* ctrl-f */ + case CTRL_F: /* ctrl-f */ linenoiseEditMoveRight(&l); break; - case CTRL_P: /* ctrl-p */ + case CTRL_P: /* ctrl-p */ linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV); break; - case CTRL_N: /* ctrl-n */ + case CTRL_N: /* ctrl-n */ linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT); break; - case ESC: /* escape sequence */ + case ESC: /* escape sequence */ /* Read the next two bytes representing the escape sequence. * Use two calls to handle slow terminals returning the two * chars at different times. */ - if (read(l.ifd, seq, 1) == -1) - break; - if (read(l.ifd, seq + 1, 1) == -1) - break; + if (read(l.ifd,seq,1) == -1) break; + if (read(l.ifd,seq+1,1) == -1) break; /* ESC [ sequences. */ if (seq[0] == '[') { if (seq[1] >= '0' && seq[1] <= '9') { /* Extended escape, read additional byte. */ - if (read(l.ifd, seq + 2, 1) == -1) - break; + if (read(l.ifd,seq+2,1) == -1) break; if (seq[2] == '~') { - switch (seq[1]) { + switch(seq[1]) { case '3': /* Delete key. */ linenoiseEditDelete(&l); break; } } } else { - switch (seq[1]) { + switch(seq[1]) { case 'A': /* Up */ linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV); break; @@ -964,7 +908,7 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, /* ESC O sequences. */ else if (seq[0] == 'O') { - switch (seq[1]) { + switch(seq[1]) { case 'H': /* Home */ linenoiseEditMoveHome(&l); break; @@ -975,8 +919,7 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, } break; default: - if (linenoiseEditInsert(&l, c)) - return -1; + if (linenoiseEditInsert(&l,c)) return -1; break; case CTRL_U: /* Ctrl+u, delete the whole line. */ buf[0] = '\0'; @@ -1009,28 +952,25 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, /* This special mode is used by linenoise in order to print scan codes * on screen for debugging / development purposes. It is implemented * by the linenoise_example program using the --keycodes option. */ -void linenoisePrintKeyCodes(void) -{ +void linenoisePrintKeyCodes(void) { char quit[4]; printf("Linenoise key codes debugging mode.\n" - "Press keys to see scan codes. Type 'quit' at any time to exit.\n"); - if (enableRawMode(STDIN_FILENO) == -1) - return; - memset(quit, ' ', 4); - while (1) { + "Press keys to see scan codes. Type 'quit' at any time to exit.\n"); + if (enableRawMode(STDIN_FILENO) == -1) return; + memset(quit,' ',4); + while(1) { char c; int nread; - nread = read(STDIN_FILENO, &c, 1); - if (nread <= 0) - continue; - memmove(quit, quit + 1, sizeof(quit) - 1); /* shift string to left. */ - quit[sizeof(quit) - 1] = c; /* Insert current char on the right. */ - if (memcmp(quit, "quit", sizeof(quit)) == 0) - break; + nread = read(STDIN_FILENO,&c,1); + if (nread <= 0) continue; + memmove(quit,quit+1,sizeof(quit)-1); /* shift string to left. */ + quit[sizeof(quit)-1] = c; /* Insert current char on the right. */ + if (memcmp(quit,"quit",sizeof(quit)) == 0) break; - printf("'%c' %02x (%d) (type quit to exit)\n", isprint(c) ? c : '?', (int)c, (int)c); + printf("'%c' %02x (%d) (type quit to exit)\n", + isprint(c) ? c : '?', (int)c, (int)c); printf("\r"); /* Go left edge manually, we are in raw mode. */ fflush(stdout); } @@ -1039,8 +979,7 @@ void linenoisePrintKeyCodes(void) /* This function calls the line editing function linenoiseEdit() using * the STDIN file descriptor set in raw mode. */ -static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) -{ +static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) { int count; if (buflen == 0) { @@ -1048,8 +987,7 @@ static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) return -1; } - if (enableRawMode(STDIN_FILENO) == -1) - return -1; + if (enableRawMode(STDIN_FILENO) == -1) return -1; count = linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, buflen, prompt); disableRawMode(STDIN_FILENO); printf("\n"); @@ -1061,21 +999,18 @@ static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) * program using linenoise is called in pipe or with a file redirected * to its standard input. In this case, we want to be able to return the * line regardless of its length (by default we are limited to 4k). */ -static char *linenoiseNoTTY(void) -{ +static char *linenoiseNoTTY(void) { char *line = NULL; size_t len = 0, maxlen = 0; - while (1) { + while(1) { if (len == maxlen) { - if (maxlen == 0) - maxlen = 16; + if (maxlen == 0) maxlen = 16; maxlen *= 2; char *oldval = line; - line = realloc(line, maxlen); + line = realloc(line,maxlen); if (line == NULL) { - if (oldval) - free(oldval); + if (oldval) free(oldval); return NULL; } } @@ -1100,8 +1035,7 @@ static char *linenoiseNoTTY(void) * for a blacklist of stupid terminals, and later either calls the line * editing function or uses dummy fgets() so that you will be able to type * something even in the most desperate of the conditions. */ -char *linenoise(const char *prompt) -{ +char *linenoise(const char *prompt) { char buf[LINENOISE_MAX_LINE]; int count; @@ -1112,20 +1046,18 @@ char *linenoise(const char *prompt) } else if (isUnsupportedTerm()) { size_t len; - printf("%s", prompt); + printf("%s",prompt); fflush(stdout); - if (fgets(buf, LINENOISE_MAX_LINE, stdin) == NULL) - return NULL; + if (fgets(buf,LINENOISE_MAX_LINE,stdin) == NULL) return NULL; len = strlen(buf); - while (len && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) { + while(len && (buf[len-1] == '\n' || buf[len-1] == '\r')) { len--; buf[len] = '\0'; } return strdup(buf); } else { - count = linenoiseRaw(buf, LINENOISE_MAX_LINE, prompt); - if (count == -1) - return NULL; + count = linenoiseRaw(buf,LINENOISE_MAX_LINE,prompt); + if (count == -1) return NULL; return strdup(buf); } } @@ -1134,14 +1066,15 @@ char *linenoise(const char *prompt) * the linenoise returned buffer is freed with the same allocator it was * created with. Useful when the main program is using an alternative * allocator. */ -void linenoiseFree(void *ptr) { free(ptr); } +void linenoiseFree(void *ptr) { + free(ptr); +} /* ================================ History ================================= */ /* Free the history, but does not reset it. Only used when we have to * exit() to avoid memory leaks are reported by valgrind & co. */ -static void freeHistory(void) -{ +static void freeHistory(void) { if (history) { int j; @@ -1152,8 +1085,7 @@ static void freeHistory(void) } /* At exit we'll try to fix the terminal to the initial conditions. */ -static void linenoiseAtExit(void) -{ +static void linenoiseAtExit(void) { disableRawMode(STDIN_FILENO); freeHistory(); } @@ -1165,33 +1097,28 @@ static void linenoiseAtExit(void) * histories, but will work well for a few hundred of entries. * * Using a circular buffer is smarter, but a bit more complex to handle. */ -int linenoiseHistoryAdd(const char *line) -{ +int linenoiseHistoryAdd(const char *line) { char *linecopy; - if (history_max_len == 0) - return 0; + if (history_max_len == 0) return 0; /* Initialization on first call. */ if (history == NULL) { - history = malloc(sizeof(char *) * history_max_len); - if (history == NULL) - return 0; - memset(history, 0, (sizeof(char *) * history_max_len)); + history = malloc(sizeof(char*)*history_max_len); + if (history == NULL) return 0; + memset(history,0,(sizeof(char*)*history_max_len)); } /* Don't add duplicated lines. */ - if (history_len && !strcmp(history[history_len - 1], line)) - return 0; + if (history_len && !strcmp(history[history_len-1], line)) return 0; /* Add an heap allocated copy of the line in the history. * If we reached the max length, remove the older line. */ linecopy = strdup(line); - if (!linecopy) - return 0; + if (!linecopy) return 0; if (history_len == history_max_len) { free(history[0]); - memmove(history, history + 1, sizeof(char *) * (history_max_len - 1)); + memmove(history,history+1,sizeof(char*)*(history_max_len-1)); history_len--; } history[history_len] = linecopy; @@ -1203,29 +1130,25 @@ int linenoiseHistoryAdd(const char *line) * if there is already some history, the function will make sure to retain * just the latest 'len' elements if the new history length value is smaller * than the amount of items already inside the history. */ -int linenoiseHistorySetMaxLen(int len) -{ +int linenoiseHistorySetMaxLen(int len) { char **new; - if (len < 1) - return 0; + if (len < 1) return 0; if (history) { int tocopy = history_len; - new = malloc(sizeof(char *) * len); - if (new == NULL) - return 0; + new = malloc(sizeof(char*)*len); + if (new == NULL) return 0; /* If we can't copy everything, free the elements we'll not use. */ if (len < tocopy) { int j; - for (j = 0; j < tocopy - len; j++) - free(history[j]); + for (j = 0; j < tocopy-len; j++) free(history[j]); tocopy = len; } - memset(new, 0, sizeof(char *) * len); - memcpy(new, history + (history_len - tocopy), sizeof(char *) * tocopy); + memset(new,0,sizeof(char*)*len); + memcpy(new,history+(history_len-tocopy), sizeof(char*)*tocopy); free(history); history = new; } @@ -1237,19 +1160,17 @@ int linenoiseHistorySetMaxLen(int len) /* Save the history in the specified file. On success 0 is returned * otherwise -1 is returned. */ -int linenoiseHistorySave(const char *filename) -{ - mode_t old_umask = umask(S_IXUSR | S_IRWXG | S_IRWXO); +int linenoiseHistorySave(const char *filename) { + mode_t old_umask = umask(S_IXUSR|S_IRWXG|S_IRWXO); FILE *fp; int j; - fp = fopen(filename, "w"); + fp = fopen(filename,"w"); umask(old_umask); - if (fp == NULL) - return -1; - chmod(filename, S_IRUSR | S_IWUSR); + if (fp == NULL) return -1; + chmod(filename,S_IRUSR|S_IWUSR); for (j = 0; j < history_len; j++) - fprintf(fp, "%s\n", history[j]); + fprintf(fp,"%s\n",history[j]); fclose(fp); return 0; } @@ -1259,22 +1180,18 @@ int linenoiseHistorySave(const char *filename) * * If the file exists and the operation succeeded 0 is returned, otherwise * on error -1 is returned. */ -int linenoiseHistoryLoad(const char *filename) -{ - FILE *fp = fopen(filename, "r"); +int linenoiseHistoryLoad(const char *filename) { + FILE *fp = fopen(filename,"r"); char buf[LINENOISE_MAX_LINE]; - if (fp == NULL) - return -1; + if (fp == NULL) return -1; - while (fgets(buf, LINENOISE_MAX_LINE, fp) != NULL) { + while (fgets(buf,LINENOISE_MAX_LINE,fp) != NULL) { char *p; - p = strchr(buf, '\r'); - if (!p) - p = strchr(buf, '\n'); - if (p) - *p = '\0'; + p = strchr(buf,'\r'); + if (!p) p = strchr(buf,'\n'); + if (p) *p = '\0'; linenoiseHistoryAdd(buf); } fclose(fp); diff --git a/src/shell/linenoise/linenoise.h b/src/shell/linenoise/linenoise.h index abeed28313..ed20232c57 100644 --- a/src/shell/linenoise/linenoise.h +++ b/src/shell/linenoise/linenoise.h @@ -43,14 +43,13 @@ extern "C" { #endif -typedef struct linenoiseCompletions -{ - size_t len; - char **cvec; +typedef struct linenoiseCompletions { + size_t len; + char **cvec; } linenoiseCompletions; typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *); -typedef char *(linenoiseHintsCallback)(const char *, int *color, int *bold); +typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold); typedef void(linenoiseFreeHintsCallback)(void *); void linenoiseSetCompletionCallback(linenoiseCompletionCallback *); void linenoiseSetHintsCallback(linenoiseHintsCallback *); diff --git a/src/shell/sds/sds.c b/src/shell/sds/sds.c index e6877a37b9..e3eaaef395 100644 --- a/src/shell/sds/sds.c +++ b/src/shell/sds/sds.c @@ -39,33 +39,31 @@ #include "sds.h" #include "sdsalloc.h" -static inline int sdsHdrSize(char type) -{ - switch (type & SDS_TYPE_MASK) { - case SDS_TYPE_5: - return sizeof(struct sdshdr5); - case SDS_TYPE_8: - return sizeof(struct sdshdr8); - case SDS_TYPE_16: - return sizeof(struct sdshdr16); - case SDS_TYPE_32: - return sizeof(struct sdshdr32); - case SDS_TYPE_64: - return sizeof(struct sdshdr64); +static inline int sdsHdrSize(char type) { + switch(type&SDS_TYPE_MASK) { + case SDS_TYPE_5: + return sizeof(struct sdshdr5); + case SDS_TYPE_8: + return sizeof(struct sdshdr8); + case SDS_TYPE_16: + return sizeof(struct sdshdr16); + case SDS_TYPE_32: + return sizeof(struct sdshdr32); + case SDS_TYPE_64: + return sizeof(struct sdshdr64); } return 0; } -static inline char sdsReqType(size_t string_size) -{ - if (string_size < 1 << 5) +static inline char sdsReqType(size_t string_size) { + if (string_size < 1<<5) return SDS_TYPE_5; - if (string_size < 1 << 8) + if (string_size < 1<<8) return SDS_TYPE_8; - if (string_size < 1 << 16) + if (string_size < 1<<16) return SDS_TYPE_16; #if (LONG_MAX == LLONG_MAX) - if (string_size < 1ll << 32) + if (string_size < 1ll<<32) return SDS_TYPE_32; #endif return SDS_TYPE_64; @@ -83,58 +81,55 @@ static inline char sdsReqType(size_t string_size) * You can print the string with printf() as there is an implicit \0 at the * end of the string. However the string is binary safe and can contain * \0 characters in the middle, as the length is stored in the sds header. */ -sds sdsnewlen(const void *init, size_t initlen) -{ +sds sdsnewlen(const void *init, size_t initlen) { void *sh; sds s; char type = sdsReqType(initlen); /* Empty strings are usually created in order to append. Use type 8 * since type 5 is not good at this. */ - if (type == SDS_TYPE_5 && initlen == 0) - type = SDS_TYPE_8; + if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8; int hdrlen = sdsHdrSize(type); unsigned char *fp; /* flags pointer. */ - sh = s_malloc(hdrlen + initlen + 1); - if (sh == NULL) - return NULL; + sh = s_malloc(hdrlen+initlen+1); + if (sh == NULL) return NULL; if (!init) - memset(sh, 0, hdrlen + initlen + 1); - s = (char *)sh + hdrlen; - fp = ((unsigned char *)s) - 1; - switch (type) { - case SDS_TYPE_5: { - *fp = type | (initlen << SDS_TYPE_BITS); - break; - } - case SDS_TYPE_8: { - SDS_HDR_VAR(8, s); - sh->len = initlen; - sh->alloc = initlen; - *fp = type; - break; - } - case SDS_TYPE_16: { - SDS_HDR_VAR(16, s); - sh->len = initlen; - sh->alloc = initlen; - *fp = type; - break; - } - case SDS_TYPE_32: { - SDS_HDR_VAR(32, s); - sh->len = initlen; - sh->alloc = initlen; - *fp = type; - break; - } - case SDS_TYPE_64: { - SDS_HDR_VAR(64, s); - sh->len = initlen; - sh->alloc = initlen; - *fp = type; - break; - } + memset(sh, 0, hdrlen+initlen+1); + s = (char*)sh+hdrlen; + fp = ((unsigned char*)s)-1; + switch(type) { + case SDS_TYPE_5: { + *fp = type | (initlen << SDS_TYPE_BITS); + break; + } + case SDS_TYPE_8: { + SDS_HDR_VAR(8,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + case SDS_TYPE_16: { + SDS_HDR_VAR(16,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + case SDS_TYPE_32: { + SDS_HDR_VAR(32,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } + case SDS_TYPE_64: { + SDS_HDR_VAR(64,s); + sh->len = initlen; + sh->alloc = initlen; + *fp = type; + break; + } } if (initlen && init) memcpy(s, init, initlen); @@ -144,24 +139,25 @@ sds sdsnewlen(const void *init, size_t initlen) /* Create an empty (zero length) sds string. Even in this case the string * always has an implicit null term. */ -sds sdsempty(void) { return sdsnewlen("", 0); } +sds sdsempty(void) { + return sdsnewlen("",0); +} /* Create a new sds string starting from a null terminated C string. */ -sds sdsnew(const char *init) -{ +sds sdsnew(const char *init) { size_t initlen = (init == NULL) ? 0 : strlen(init); return sdsnewlen(init, initlen); } /* Duplicate an sds string. */ -sds sdsdup(const sds s) { return sdsnewlen(s, sdslen(s)); } +sds sdsdup(const sds s) { + return sdsnewlen(s, sdslen(s)); +} /* Free an sds string. No operation is performed if 's' is NULL. */ -void sdsfree(sds s) -{ - if (s == NULL) - return; - s_free((char *)s - sdsHdrSize(s[-1])); +void sdsfree(sds s) { + if (s == NULL) return; + s_free((char*)s-sdsHdrSize(s[-1])); } /* Set the sds string length to the length as obtained with strlen(), so @@ -178,8 +174,7 @@ void sdsfree(sds s) * The output will be "2", but if we comment out the call to sdsupdatelen() * the output will be "6" as the string was modified but the logical length * remains 6 bytes. */ -void sdsupdatelen(sds s) -{ +void sdsupdatelen(sds s) { size_t reallen = strlen(s); sdssetlen(s, reallen); } @@ -188,8 +183,7 @@ void sdsupdatelen(sds s) * However all the existing buffer is not discarded but set as free space * so that next append operations will not require allocations up to the * number of bytes previously available. */ -void sdsclear(sds s) -{ +void sdsclear(sds s) { sdssetlen(s, 0); s[0] = '\0'; } @@ -200,8 +194,7 @@ void sdsclear(sds s) * * Note: this does not change the *length* of the sds string as returned * by sdslen(), but only the free buffer space we have. */ -sds sdsMakeRoomFor(sds s, size_t addlen) -{ +sds sdsMakeRoomFor(sds s, size_t addlen) { void *sh, *newsh; size_t avail = sdsavail(s); size_t len, newlen; @@ -209,12 +202,11 @@ sds sdsMakeRoomFor(sds s, size_t addlen) int hdrlen; /* Return ASAP if there is enough space left. */ - if (avail >= addlen) - return s; + if (avail >= addlen) return s; len = sdslen(s); - sh = (char *)s - sdsHdrSize(oldtype); - newlen = (len + addlen); + sh = (char*)s-sdsHdrSize(oldtype); + newlen = (len+addlen); if (newlen < SDS_MAX_PREALLOC) newlen *= 2; else @@ -225,24 +217,21 @@ sds sdsMakeRoomFor(sds s, size_t addlen) /* Don't use type 5: the user is appending to the string and type 5 is * not able to remember empty space, so sdsMakeRoomFor() must be called * at every appending operation. */ - if (type == SDS_TYPE_5) - type = SDS_TYPE_8; + if (type == SDS_TYPE_5) type = SDS_TYPE_8; hdrlen = sdsHdrSize(type); - if (oldtype == type) { - newsh = s_realloc(sh, hdrlen + newlen + 1); - if (newsh == NULL) - return NULL; - s = (char *)newsh + hdrlen; + if (oldtype==type) { + newsh = s_realloc(sh, hdrlen+newlen+1); + if (newsh == NULL) return NULL; + s = (char*)newsh+hdrlen; } else { /* Since the header size changes, need to move the string forward, * and can't use realloc */ - newsh = s_malloc(hdrlen + newlen + 1); - if (newsh == NULL) - return NULL; - memcpy((char *)newsh + hdrlen, s, len + 1); + newsh = s_malloc(hdrlen+newlen+1); + if (newsh == NULL) return NULL; + memcpy((char*)newsh+hdrlen, s, len+1); s_free(sh); - s = (char *)newsh + hdrlen; + s = (char*)newsh+hdrlen; s[-1] = type; sdssetlen(s, len); } @@ -256,13 +245,12 @@ sds sdsMakeRoomFor(sds s, size_t addlen) * * After the call, the passed sds string is no longer valid and all the * references must be substituted with the new pointer returned by the call. */ -sds sdsRemoveFreeSpace(sds s) -{ +sds sdsRemoveFreeSpace(sds s) { void *sh, *newsh; char type, oldtype = s[-1] & SDS_TYPE_MASK; int hdrlen, oldhdrlen = sdsHdrSize(oldtype); size_t len = sdslen(s); - sh = (char *)s - oldhdrlen; + sh = (char*)s-oldhdrlen; /* Check what would be the minimum SDS header that is just good enough to * fit this string. */ @@ -273,18 +261,16 @@ sds sdsRemoveFreeSpace(sds s) * required, we just realloc(), letting the allocator to do the copy * only if really needed. Otherwise if the change is huge, we manually * reallocate the string to use the different header type. */ - if (oldtype == type || type > SDS_TYPE_8) { - newsh = s_realloc(sh, oldhdrlen + len + 1); - if (newsh == NULL) - return NULL; - s = (char *)newsh + oldhdrlen; + if (oldtype==type || type > SDS_TYPE_8) { + newsh = s_realloc(sh, oldhdrlen+len+1); + if (newsh == NULL) return NULL; + s = (char*)newsh+oldhdrlen; } else { - newsh = s_malloc(hdrlen + len + 1); - if (newsh == NULL) - return NULL; - memcpy((char *)newsh + hdrlen, s, len + 1); + newsh = s_malloc(hdrlen+len+1); + if (newsh == NULL) return NULL; + memcpy((char*)newsh+hdrlen, s, len+1); s_free(sh); - s = (char *)newsh + hdrlen; + s = (char*)newsh+hdrlen; s[-1] = type; sdssetlen(s, len); } @@ -299,15 +285,16 @@ sds sdsRemoveFreeSpace(sds s) * 3) The free buffer at the end if any. * 4) The implicit null term. */ -size_t sdsAllocSize(sds s) -{ +size_t sdsAllocSize(sds s) { size_t alloc = sdsalloc(s); - return sdsHdrSize(s[-1]) + alloc + 1; + return sdsHdrSize(s[-1])+alloc+1; } /* Return the pointer of the actual SDS allocation (normally SDS strings * are referenced by the start of the string buffer). */ -void *sdsAllocPtr(sds s) { return (void *)(s - sdsHdrSize(s[-1])); } +void *sdsAllocPtr(sds s) { + return (void*) (s-sdsHdrSize(s[-1])); +} /* Increment the sds length and decrements the left free space at the * end of the string according to 'incr'. Also set the null term @@ -332,49 +319,43 @@ void *sdsAllocPtr(sds s) { return (void *)(s - sdsHdrSize(s[-1])); } * ... check for nread <= 0 and handle it ... * sdsIncrLen(s, nread); */ -void sdsIncrLen(sds s, ssize_t incr) -{ +void sdsIncrLen(sds s, ssize_t incr) { unsigned char flags = s[-1]; size_t len; - switch (flags & SDS_TYPE_MASK) { - case SDS_TYPE_5: { - unsigned char *fp = ((unsigned char *)s) - 1; - unsigned char oldlen = SDS_TYPE_5_LEN(flags); - assert((incr > 0 && oldlen + incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr))); - *fp = SDS_TYPE_5 | ((oldlen + incr) << SDS_TYPE_BITS); - len = oldlen + incr; - break; - } - case SDS_TYPE_8: { - SDS_HDR_VAR(8, s); - assert((incr >= 0 && sh->alloc - sh->len >= incr) || - (incr < 0 && sh->len >= (unsigned int)(-incr))); - len = (sh->len += incr); - break; - } - case SDS_TYPE_16: { - SDS_HDR_VAR(16, s); - assert((incr >= 0 && sh->alloc - sh->len >= incr) || - (incr < 0 && sh->len >= (unsigned int)(-incr))); - len = (sh->len += incr); - break; - } - case SDS_TYPE_32: { - SDS_HDR_VAR(32, s); - assert((incr >= 0 && sh->alloc - sh->len >= (unsigned int)incr) || - (incr < 0 && sh->len >= (unsigned int)(-incr))); - len = (sh->len += incr); - break; - } - case SDS_TYPE_64: { - SDS_HDR_VAR(64, s); - assert((incr >= 0 && sh->alloc - sh->len >= (uint64_t)incr) || - (incr < 0 && sh->len >= (uint64_t)(-incr))); - len = (sh->len += incr); - break; - } - default: - len = 0; /* Just to avoid compilation warnings. */ + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: { + unsigned char *fp = ((unsigned char*)s)-1; + unsigned char oldlen = SDS_TYPE_5_LEN(flags); + assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr))); + *fp = SDS_TYPE_5 | ((oldlen+incr) << SDS_TYPE_BITS); + len = oldlen+incr; + break; + } + case SDS_TYPE_8: { + SDS_HDR_VAR(8,s); + assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); + len = (sh->len += incr); + break; + } + case SDS_TYPE_16: { + SDS_HDR_VAR(16,s); + assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); + len = (sh->len += incr); + break; + } + case SDS_TYPE_32: { + SDS_HDR_VAR(32,s); + assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); + len = (sh->len += incr); + break; + } + case SDS_TYPE_64: { + SDS_HDR_VAR(64,s); + assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr))); + len = (sh->len += incr); + break; + } + default: len = 0; /* Just to avoid compilation warnings. */ } s[len] = '\0'; } @@ -384,18 +365,15 @@ void sdsIncrLen(sds s, ssize_t incr) * * if the specified length is smaller than the current length, no operation * is performed. */ -sds sdsgrowzero(sds s, size_t len) -{ +sds sdsgrowzero(sds s, size_t len) { size_t curlen = sdslen(s); - if (len <= curlen) - return s; - s = sdsMakeRoomFor(s, len - curlen); - if (s == NULL) - return NULL; + if (len <= curlen) return s; + s = sdsMakeRoomFor(s,len-curlen); + if (s == NULL) return NULL; /* Make sure added region doesn't contain garbage */ - memset(s + curlen, 0, (len - curlen + 1)); /* also set trailing \0 byte */ + memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ sdssetlen(s, len); return s; } @@ -405,16 +383,14 @@ sds sdsgrowzero(sds s, size_t len) * * After the call, the passed sds string is no longer valid and all the * references must be substituted with the new pointer returned by the call. */ -sds sdscatlen(sds s, const void *t, size_t len) -{ +sds sdscatlen(sds s, const void *t, size_t len) { size_t curlen = sdslen(s); - s = sdsMakeRoomFor(s, len); - if (s == NULL) - return NULL; - memcpy(s + curlen, t, len); - sdssetlen(s, curlen + len); - s[curlen + len] = '\0'; + s = sdsMakeRoomFor(s,len); + if (s == NULL) return NULL; + memcpy(s+curlen, t, len); + sdssetlen(s, curlen+len); + s[curlen+len] = '\0'; return s; } @@ -422,22 +398,24 @@ sds sdscatlen(sds s, const void *t, size_t len) * * After the call, the passed sds string is no longer valid and all the * references must be substituted with the new pointer returned by the call. */ -sds sdscat(sds s, const char *t) { return sdscatlen(s, t, strlen(t)); } +sds sdscat(sds s, const char *t) { + return sdscatlen(s, t, strlen(t)); +} /* Append the specified sds 't' to the existing sds 's'. * * After the call, the modified sds string is no longer valid and all the * references must be substituted with the new pointer returned by the call. */ -sds sdscatsds(sds s, const sds t) { return sdscatlen(s, t, sdslen(t)); } +sds sdscatsds(sds s, const sds t) { + return sdscatlen(s, t, sdslen(t)); +} /* Destructively modify the sds string 's' to hold the specified binary * safe string pointed by 't' of length 'len' bytes. */ -sds sdscpylen(sds s, const char *t, size_t len) -{ +sds sdscpylen(sds s, const char *t, size_t len) { if (sdsalloc(s) < len) { - s = sdsMakeRoomFor(s, len - sdslen(s)); - if (s == NULL) - return NULL; + s = sdsMakeRoomFor(s,len-sdslen(s)); + if (s == NULL) return NULL; } memcpy(s, t, len); s[len] = '\0'; @@ -447,7 +425,9 @@ sds sdscpylen(sds s, const char *t, size_t len) /* Like sdscpylen() but 't' must be a null-termined string so that the length * of the string is obtained with strlen(). */ -sds sdscpy(sds s, const char *t) { return sdscpylen(s, t, strlen(t)); } +sds sdscpy(sds s, const char *t) { + return sdscpylen(s, t, strlen(t)); +} /* Helper for sdscatlonglong() doing the actual number -> string * conversion. 's' must point to a string with room for at least @@ -456,8 +436,7 @@ sds sdscpy(sds s, const char *t) { return sdscpylen(s, t, strlen(t)); } * The function returns the length of the null-terminated string * representation stored at 's'. */ #define SDS_LLSTR_SIZE 21 -int sdsll2str(char *s, long long value) -{ +int sdsll2str(char *s, long long value) { char *p, aux; unsigned long long v; size_t l; @@ -467,19 +446,18 @@ int sdsll2str(char *s, long long value) v = (value < 0) ? -value : value; p = s; do { - *p++ = '0' + (v % 10); + *p++ = '0'+(v%10); v /= 10; - } while (v); - if (value < 0) - *p++ = '-'; + } while(v); + if (value < 0) *p++ = '-'; /* Compute length and add null term. */ - l = p - s; + l = p-s; *p = '\0'; /* Reverse the string. */ p--; - while (s < p) { + while(s < p) { aux = *s; *s = *p; *p = aux; @@ -490,8 +468,7 @@ int sdsll2str(char *s, long long value) } /* Identical sdsll2str(), but for unsigned long long type. */ -int sdsull2str(char *s, unsigned long long v) -{ +int sdsull2str(char *s, unsigned long long v) { char *p, aux; size_t l; @@ -499,17 +476,17 @@ int sdsull2str(char *s, unsigned long long v) * an reversed string. */ p = s; do { - *p++ = '0' + (v % 10); + *p++ = '0'+(v%10); v /= 10; - } while (v); + } while(v); /* Compute length and add null term. */ - l = p - s; + l = p-s; *p = '\0'; /* Reverse the string. */ p--; - while (s < p) { + while(s < p) { aux = *s; *s = *p; *p = aux; @@ -523,45 +500,40 @@ int sdsull2str(char *s, unsigned long long v) * * sdscatprintf(sdsempty(),"%lld\n", value); */ -sds sdsfromlonglong(long long value) -{ +sds sdsfromlonglong(long long value) { char buf[SDS_LLSTR_SIZE]; - int len = sdsll2str(buf, value); + int len = sdsll2str(buf,value); - return sdsnewlen(buf, len); + return sdsnewlen(buf,len); } /* Like sdscatprintf() but gets va_list instead of being variadic. */ -sds sdscatvprintf(sds s, const char *fmt, va_list ap) -{ +sds sdscatvprintf(sds s, const char *fmt, va_list ap) { va_list cpy; char staticbuf[1024], *buf = staticbuf, *t; - size_t buflen = strlen(fmt) * 2; + size_t buflen = strlen(fmt)*2; /* We try to start using a static buffer for speed. * If not possible we revert to heap allocation. */ if (buflen > sizeof(staticbuf)) { buf = s_malloc(buflen); - if (buf == NULL) - return NULL; + if (buf == NULL) return NULL; } else { buflen = sizeof(staticbuf); } /* Try with buffers two times bigger every time we fail to * fit the string in the current buffer size. */ - while (1) { - buf[buflen - 2] = '\0'; - va_copy(cpy, ap); + while(1) { + buf[buflen-2] = '\0'; + va_copy(cpy,ap); vsnprintf(buf, buflen, fmt, cpy); va_end(cpy); - if (buf[buflen - 2] != '\0') { - if (buf != staticbuf) - s_free(buf); + if (buf[buflen-2] != '\0') { + if (buf != staticbuf) s_free(buf); buflen *= 2; buf = s_malloc(buflen); - if (buf == NULL) - return NULL; + if (buf == NULL) return NULL; continue; } break; @@ -569,8 +541,7 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) /* Finally concat the obtained string to the SDS string and return it. */ t = sdscat(s, buf); - if (buf != staticbuf) - s_free(buf); + if (buf != staticbuf) s_free(buf); return t; } @@ -590,12 +561,11 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) * * s = sdscatprintf(sdsempty(), "... your format ...", args); */ -sds sdscatprintf(sds s, const char *fmt, ...) -{ +sds sdscatprintf(sds s, const char *fmt, ...) { va_list ap; char *t; va_start(ap, fmt); - t = sdscatvprintf(s, fmt, ap); + t = sdscatvprintf(s,fmt,ap); va_end(ap); return t; } @@ -616,86 +586,85 @@ sds sdscatprintf(sds s, const char *fmt, ...) * %U - 64 bit unsigned integer (unsigned long long, uint64_t) * %% - Verbatim "%" character. */ -sds sdscatfmt(sds s, char const *fmt, ...) -{ +sds sdscatfmt(sds s, char const *fmt, ...) { size_t initlen = sdslen(s); const char *f = fmt; long i; va_list ap; - va_start(ap, fmt); - f = fmt; /* Next format specifier byte to process. */ + va_start(ap,fmt); + f = fmt; /* Next format specifier byte to process. */ i = initlen; /* Position of the next byte to write to dest str. */ - while (*f) { + while(*f) { char next, *str; size_t l; long long num; unsigned long long unum; /* Make sure there is always space for at least 1 char. */ - if (sdsavail(s) == 0) { - s = sdsMakeRoomFor(s, 1); + if (sdsavail(s)==0) { + s = sdsMakeRoomFor(s,1); } - switch (*f) { + switch(*f) { case '%': - next = *(f + 1); + next = *(f+1); f++; - switch (next) { + switch(next) { case 's': case 'S': - str = va_arg(ap, char *); + str = va_arg(ap,char*); l = (next == 's') ? strlen(str) : sdslen(str); if (sdsavail(s) < l) { - s = sdsMakeRoomFor(s, l); + s = sdsMakeRoomFor(s,l); } - memcpy(s + i, str, l); - sdsinclen(s, l); + memcpy(s+i,str,l); + sdsinclen(s,l); i += l; break; case 'i': case 'I': if (next == 'i') - num = va_arg(ap, int); + num = va_arg(ap,int); else - num = va_arg(ap, long long); + num = va_arg(ap,long long); { char buf[SDS_LLSTR_SIZE]; - l = sdsll2str(buf, num); + l = sdsll2str(buf,num); if (sdsavail(s) < l) { - s = sdsMakeRoomFor(s, l); + s = sdsMakeRoomFor(s,l); } - memcpy(s + i, buf, l); - sdsinclen(s, l); + memcpy(s+i,buf,l); + sdsinclen(s,l); i += l; } break; case 'u': case 'U': if (next == 'u') - unum = va_arg(ap, unsigned int); + unum = va_arg(ap,unsigned int); else - unum = va_arg(ap, unsigned long long); + unum = va_arg(ap,unsigned long long); { char buf[SDS_LLSTR_SIZE]; - l = sdsull2str(buf, unum); + l = sdsull2str(buf,unum); if (sdsavail(s) < l) { - s = sdsMakeRoomFor(s, l); + s = sdsMakeRoomFor(s,l); } - memcpy(s + i, buf, l); - sdsinclen(s, l); + memcpy(s+i,buf,l); + sdsinclen(s,l); i += l; } break; default: /* Handle %% and generally %. */ s[i++] = next; - sdsinclen(s, 1); + sdsinclen(s,1); break; } break; default: s[i++] = *f; - sdsinclen(s, 1); + sdsinclen(s,1); break; } f++; @@ -721,22 +690,18 @@ sds sdscatfmt(sds s, char const *fmt, ...) * * Output will be just "Hello World". */ -sds sdstrim(sds s, const char *cset) -{ +sds sdstrim(sds s, const char *cset) { char *start, *end, *sp, *ep; size_t len; sp = start = s; - ep = end = s + sdslen(s) - 1; - while (sp <= end && strchr(cset, *sp)) - sp++; - while (ep > sp && strchr(cset, *ep)) - ep--; - len = (sp > ep) ? 0 : ((ep - sp) + 1); - if (s != sp) - memmove(s, sp, len); + ep = end = s+sdslen(s)-1; + while(sp <= end && strchr(cset, *sp)) sp++; + while(ep > sp && strchr(cset, *ep)) ep--; + len = (sp > ep) ? 0 : ((ep-sp)+1); + if (s != sp) memmove(s, sp, len); s[len] = '\0'; - sdssetlen(s, len); + sdssetlen(s,len); return s; } @@ -756,55 +721,46 @@ sds sdstrim(sds s, const char *cset) * s = sdsnew("Hello World"); * sdsrange(s,1,-1); => "ello World" */ -void sdsrange(sds s, ssize_t start, ssize_t end) -{ +void sdsrange(sds s, ssize_t start, ssize_t end) { size_t newlen, len = sdslen(s); - if (len == 0) - return; + if (len == 0) return; if (start < 0) { - start = len + start; - if (start < 0) - start = 0; + start = len+start; + if (start < 0) start = 0; } if (end < 0) { - end = len + end; - if (end < 0) - end = 0; + end = len+end; + if (end < 0) end = 0; } - newlen = (start > end) ? 0 : (end - start) + 1; + newlen = (start > end) ? 0 : (end-start)+1; if (newlen != 0) { if (start >= (ssize_t)len) { newlen = 0; } else if (end >= (ssize_t)len) { - end = len - 1; - newlen = (start > end) ? 0 : (end - start) + 1; + end = len-1; + newlen = (start > end) ? 0 : (end-start)+1; } } else { start = 0; } - if (start && newlen) - memmove(s, s + start, newlen); + if (start && newlen) memmove(s, s+start, newlen); s[newlen] = 0; - sdssetlen(s, newlen); + sdssetlen(s,newlen); } /* Apply tolower() to every character of the sds string 's'. */ -void sdstolower(sds s) -{ +void sdstolower(sds s) { size_t len = sdslen(s), j; - for (j = 0; j < len; j++) - s[j] = tolower(s[j]); + for (j = 0; j < len; j++) s[j] = tolower(s[j]); } /* Apply toupper() to every character of the sds string 's'. */ -void sdstoupper(sds s) -{ +void sdstoupper(sds s) { size_t len = sdslen(s), j; - for (j = 0; j < len; j++) - s[j] = toupper(s[j]); + for (j = 0; j < len; j++) s[j] = toupper(s[j]); } /* Compare two sds strings s1 and s2 with memcmp(). @@ -818,17 +774,15 @@ void sdstoupper(sds s) * If two strings share exactly the same prefix, but one of the two has * additional characters, the longer string is considered to be greater than * the smaller one. */ -int sdscmp(const sds s1, const sds s2) -{ +int sdscmp(const sds s1, const sds s2) { size_t l1, l2, minlen; int cmp; l1 = sdslen(s1); l2 = sdslen(s2); minlen = (l1 < l2) ? l1 : l2; - cmp = memcmp(s1, s2, minlen); - if (cmp == 0) - return l1 > l2 ? 1 : (l1 < l2 ? -1 : 0); + cmp = memcmp(s1,s2,minlen); + if (cmp == 0) return l1>l2? 1: (l1= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); +int is_hex_digit(char c) { + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || + (c >= 'A' && c <= 'F'); } /* Helper function for sdssplitargs() that converts a hex digit into an * integer from 0 to 15 */ -int hex_digit_to_int(char c) -{ - switch (c) { - case '0': - return 0; - case '1': - return 1; - case '2': - return 2; - case '3': - return 3; - case '4': - return 4; - case '5': - return 5; - case '6': - return 6; - case '7': - return 7; - case '8': - return 8; - case '9': - return 9; - case 'a': - case 'A': - return 10; - case 'b': - case 'B': - return 11; - case 'c': - case 'C': - return 12; - case 'd': - case 'D': - return 13; - case 'e': - case 'E': - return 14; - case 'f': - case 'F': - return 15; - default: - return 0; +int hex_digit_to_int(char c) { + switch(c) { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'a': case 'A': return 10; + case 'b': case 'B': return 11; + case 'c': case 'C': return 12; + case 'd': case 'D': return 13; + case 'e': case 'E': return 14; + case 'f': case 'F': return 15; + default: return 0; } } @@ -1030,129 +941,112 @@ int hex_digit_to_int(char c) * quotes or closed quotes followed by non space characters * as in: "foo"bar or "foo' */ -sds *sdssplitargs(const char *line, int *argc) -{ +sds *sdssplitargs(const char *line, int *argc) { const char *p = line; char *current = NULL; char **vector = NULL; *argc = 0; - while (1) { + while(1) { /* skip blanks */ - while (*p && isspace(*p)) - p++; + while(*p && isspace(*p)) p++; if (*p) { /* get a token */ - int inq = 0; /* set to 1 if we are in "quotes" */ - int insq = 0; /* set to 1 if we are in 'single quotes' */ - int done = 0; + int inq=0; /* set to 1 if we are in "quotes" */ + int insq=0; /* set to 1 if we are in 'single quotes' */ + int done=0; - if (current == NULL) - current = sdsempty(); - while (!done) { + if (current == NULL) current = sdsempty(); + while(!done) { if (inq) { - if (*p == '\\' && *(p + 1) == 'x' && is_hex_digit(*(p + 2)) && - is_hex_digit(*(p + 3))) { + if (*p == '\\' && *(p+1) == 'x' && + is_hex_digit(*(p+2)) && + is_hex_digit(*(p+3))) + { unsigned char byte; - byte = (hex_digit_to_int(*(p + 2)) * 16) + hex_digit_to_int(*(p + 3)); - current = sdscatlen(current, (char *)&byte, 1); + byte = (hex_digit_to_int(*(p+2))*16)+ + hex_digit_to_int(*(p+3)); + current = sdscatlen(current,(char*)&byte,1); p += 3; - } else if (*p == '\\' && *(p + 1)) { + } else if (*p == '\\' && *(p+1)) { char c; p++; - switch (*p) { - case 'n': - c = '\n'; - break; - case 'r': - c = '\r'; - break; - case 't': - c = '\t'; - break; - case 'b': - c = '\b'; - break; - case 'a': - c = '\a'; - break; - default: - c = *p; - break; + switch(*p) { + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'b': c = '\b'; break; + case 'a': c = '\a'; break; + default: c = *p; break; } - current = sdscatlen(current, &c, 1); + current = sdscatlen(current,&c,1); } else if (*p == '"') { /* closing quote must be followed by a space or * nothing at all. */ - if (*(p + 1) && !isspace(*(p + 1))) - goto err; - done = 1; + if (*(p+1) && !isspace(*(p+1))) goto err; + done=1; } else if (!*p) { /* unterminated quotes */ goto err; } else { - current = sdscatlen(current, p, 1); + current = sdscatlen(current,p,1); } } else if (insq) { - if (*p == '\\' && *(p + 1) == '\'') { + if (*p == '\\' && *(p+1) == '\'') { p++; - current = sdscatlen(current, "'", 1); + current = sdscatlen(current,"'",1); } else if (*p == '\'') { /* closing quote must be followed by a space or * nothing at all. */ - if (*(p + 1) && !isspace(*(p + 1))) - goto err; - done = 1; + if (*(p+1) && !isspace(*(p+1))) goto err; + done=1; } else if (!*p) { /* unterminated quotes */ goto err; } else { - current = sdscatlen(current, p, 1); + current = sdscatlen(current,p,1); } } else { - switch (*p) { + switch(*p) { case ' ': case '\n': case '\r': case '\t': case '\0': - done = 1; + done=1; break; case '"': - inq = 1; + inq=1; break; case '\'': - insq = 1; + insq=1; break; default: - current = sdscatlen(current, p, 1); + current = sdscatlen(current,p,1); break; } } - if (*p) - p++; + if (*p) p++; } /* add the token to the vector */ - vector = s_realloc(vector, ((*argc) + 1) * sizeof(char *)); + vector = s_realloc(vector,((*argc)+1)*sizeof(char*)); vector[*argc] = current; (*argc)++; current = NULL; } else { /* Even on empty input string return something not NULL. */ - if (vector == NULL) - vector = s_malloc(sizeof(void *)); + if (vector == NULL) vector = s_malloc(sizeof(void*)); return vector; } } err: - while ((*argc)--) + while((*argc)--) sdsfree(vector[*argc]); s_free(vector); - if (current) - sdsfree(current); + if (current) sdsfree(current); *argc = 0; return NULL; } @@ -1166,8 +1060,7 @@ sds *sdssplitargs(const char *line, int *argc) * * The function returns the sds string pointer, that is always the same * as the input pointer since no resize is needed. */ -sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) -{ +sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) { size_t j, i, l = sdslen(s); for (j = 0; j < l; j++) { @@ -1183,29 +1076,25 @@ sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) /* Join an array of C strings using the specified separator (also a C string). * Returns the result as an sds string. */ -sds sdsjoin(char **argv, int argc, char *sep) -{ +sds sdsjoin(char **argv, int argc, char *sep) { sds join = sdsempty(); int j; for (j = 0; j < argc; j++) { join = sdscat(join, argv[j]); - if (j != argc - 1) - join = sdscat(join, sep); + if (j != argc-1) join = sdscat(join,sep); } return join; } /* Like sdsjoin, but joins an array of SDS strings. */ -sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) -{ +sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) { sds join = sdsempty(); int j; for (j = 0; j < argc; j++) { join = sdscatsds(join, argv[j]); - if (j != argc - 1) - join = sdscatlen(join, sep, seplen); + if (j != argc-1) join = sdscatlen(join,sep,seplen); } return join; } @@ -1216,7 +1105,7 @@ sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) * the programs SDS is linked to, if they want to touch the SDS internals * even if they use a different allocator. */ void *sds_malloc(size_t size) { return s_malloc(size); } -void *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr, size); } +void *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr,size); } void sds_free(void *ptr) { s_free(ptr); } #if defined(SDS_TEST_MAIN) @@ -1225,118 +1114,129 @@ void sds_free(void *ptr) { s_free(ptr); } #include "limits.h" #define UNUSED(x) (void)(x) -int sdsTest(void) -{ +int sdsTest(void) { { sds x = sdsnew("foo"), y; test_cond("Create a string and obtain the length", - sdslen(x) == 3 && memcmp(x, "foo\0", 4) == 0) + sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0) - sdsfree(x); - x = sdsnewlen("foo", 2); + sdsfree(x); + x = sdsnewlen("foo",2); test_cond("Create a string with specified length", - sdslen(x) == 2 && memcmp(x, "fo\0", 3) == 0) + sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0) - x = sdscat(x, "bar"); - test_cond("Strings concatenation", sdslen(x) == 5 && memcmp(x, "fobar\0", 6) == 0); + x = sdscat(x,"bar"); + test_cond("Strings concatenation", + sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0); - x = sdscpy(x, "a"); + x = sdscpy(x,"a"); test_cond("sdscpy() against an originally longer string", - sdslen(x) == 1 && memcmp(x, "a\0", 2) == 0) + sdslen(x) == 1 && memcmp(x,"a\0",2) == 0) - x = sdscpy(x, "xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk"); + x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk"); test_cond("sdscpy() against an originally shorter string", - sdslen(x) == 33 && memcmp(x, "xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0", 33) == 0) + sdslen(x) == 33 && + memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0) - sdsfree(x); - x = sdscatprintf(sdsempty(), "%d", 123); + sdsfree(x); + x = sdscatprintf(sdsempty(),"%d",123); test_cond("sdscatprintf() seems working in the base case", - sdslen(x) == 3 && memcmp(x, "123\0", 4) == 0) + sdslen(x) == 3 && memcmp(x,"123\0",4) == 0) - sdsfree(x); + sdsfree(x); x = sdsnew("--"); - x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN, LLONG_MAX); + x = sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX); test_cond("sdscatfmt() seems working in the base case", - sdslen(x) == 60 && memcmp(x, - "--Hello Hi! World -9223372036854775808," - "9223372036854775807--", - 60) == 0) printf("[%s]\n", x); + sdslen(x) == 60 && + memcmp(x,"--Hello Hi! World -9223372036854775808," + "9223372036854775807--",60) == 0) + printf("[%s]\n",x); sdsfree(x); x = sdsnew("--"); x = sdscatfmt(x, "%u,%U--", UINT_MAX, ULLONG_MAX); test_cond("sdscatfmt() seems working with unsigned numbers", - sdslen(x) == 35 && memcmp(x, "--4294967295,18446744073709551615--", 35) == 0) + sdslen(x) == 35 && + memcmp(x,"--4294967295,18446744073709551615--",35) == 0) - sdsfree(x); + sdsfree(x); x = sdsnew(" x "); - sdstrim(x, " x"); - test_cond("sdstrim() works when all chars match", sdslen(x) == 0) + sdstrim(x," x"); + test_cond("sdstrim() works when all chars match", + sdslen(x) == 0) - sdsfree(x); + sdsfree(x); x = sdsnew(" x "); - sdstrim(x, " "); - test_cond("sdstrim() works when a single char remains", sdslen(x) == 1 && x[0] == 'x') + sdstrim(x," "); + test_cond("sdstrim() works when a single char remains", + sdslen(x) == 1 && x[0] == 'x') - sdsfree(x); + sdsfree(x); x = sdsnew("xxciaoyyy"); - sdstrim(x, "xy"); + sdstrim(x,"xy"); test_cond("sdstrim() correctly trims characters", - sdslen(x) == 4 && memcmp(x, "ciao\0", 5) == 0) + sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0) - y = sdsdup(x); - sdsrange(y, 1, 1); - test_cond("sdsrange(...,1,1)", sdslen(y) == 1 && memcmp(y, "i\0", 2) == 0) + y = sdsdup(x); + sdsrange(y,1,1); + test_cond("sdsrange(...,1,1)", + sdslen(y) == 1 && memcmp(y,"i\0",2) == 0) - sdsfree(y); + sdsfree(y); y = sdsdup(x); - sdsrange(y, 1, -1); - test_cond("sdsrange(...,1,-1)", sdslen(y) == 3 && memcmp(y, "iao\0", 4) == 0) + sdsrange(y,1,-1); + test_cond("sdsrange(...,1,-1)", + sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) - sdsfree(y); + sdsfree(y); y = sdsdup(x); - sdsrange(y, -2, -1); - test_cond("sdsrange(...,-2,-1)", sdslen(y) == 2 && memcmp(y, "ao\0", 3) == 0) + sdsrange(y,-2,-1); + test_cond("sdsrange(...,-2,-1)", + sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0) - sdsfree(y); + sdsfree(y); y = sdsdup(x); - sdsrange(y, 2, 1); - test_cond("sdsrange(...,2,1)", sdslen(y) == 0 && memcmp(y, "\0", 1) == 0) + sdsrange(y,2,1); + test_cond("sdsrange(...,2,1)", + sdslen(y) == 0 && memcmp(y,"\0",1) == 0) - sdsfree(y); + sdsfree(y); y = sdsdup(x); - sdsrange(y, 1, 100); - test_cond("sdsrange(...,1,100)", sdslen(y) == 3 && memcmp(y, "iao\0", 4) == 0) + sdsrange(y,1,100); + test_cond("sdsrange(...,1,100)", + sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) - sdsfree(y); + sdsfree(y); y = sdsdup(x); - sdsrange(y, 100, 100); - test_cond("sdsrange(...,100,100)", sdslen(y) == 0 && memcmp(y, "\0", 1) == 0) + sdsrange(y,100,100); + test_cond("sdsrange(...,100,100)", + sdslen(y) == 0 && memcmp(y,"\0",1) == 0) - sdsfree(y); + sdsfree(y); sdsfree(x); x = sdsnew("foo"); y = sdsnew("foa"); - test_cond("sdscmp(foo,foa)", sdscmp(x, y) > 0) + test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0) - sdsfree(y); + sdsfree(y); sdsfree(x); x = sdsnew("bar"); y = sdsnew("bar"); - test_cond("sdscmp(bar,bar)", sdscmp(x, y) == 0) + test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0) - sdsfree(y); + sdsfree(y); sdsfree(x); x = sdsnew("aar"); y = sdsnew("bar"); - test_cond("sdscmp(bar,bar)", sdscmp(x, y) < 0) + test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0) - sdsfree(y); + sdsfree(y); sdsfree(x); - x = sdsnewlen("\a\n\0foo\r", 7); - y = sdscatrepr(sdsempty(), x, sdslen(x)); - test_cond("sdscatrepr(...data...)", memcmp(y, "\"\\a\\n\\x00foo\\r\"", 15) == 0) + x = sdsnewlen("\a\n\0foo\r",7); + y = sdscatrepr(sdsempty(),x,sdslen(x)); + test_cond("sdscatrepr(...data...)", + memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0) { unsigned int oldfree; @@ -1352,34 +1252,34 @@ int sdsTest(void) * SDS header types. */ for (i = 0; i < 10; i++) { int oldlen = sdslen(x); - x = sdsMakeRoomFor(x, step); - int type = x[-1] & SDS_TYPE_MASK; + x = sdsMakeRoomFor(x,step); + int type = x[-1]&SDS_TYPE_MASK; test_cond("sdsMakeRoomFor() len", sdslen(x) == oldlen); if (type != SDS_TYPE_5) { test_cond("sdsMakeRoomFor() free", sdsavail(x) >= step); oldfree = sdsavail(x); } - p = x + oldlen; + p = x+oldlen; for (j = 0; j < step; j++) { - p[j] = 'A' + j; + p[j] = 'A'+j; } - sdsIncrLen(x, step); + sdsIncrLen(x,step); } test_cond("sdsMakeRoomFor() content", - memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGH" - "IJABCDEFGHIJABCDEFGHIJABCDEFGHIJ", - x, - 101) == 0); - test_cond("sdsMakeRoomFor() final length", sdslen(x) == 101); + memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ",x,101) == 0); + test_cond("sdsMakeRoomFor() final length",sdslen(x)==101); sdsfree(x); } } - test_report() return 0; + test_report() + return 0; } #endif #ifdef SDS_TEST_MAIN -int main(void) { return sdsTest(); } +int main(void) { + return sdsTest(); +} #endif diff --git a/src/shell/sds/sds.h b/src/shell/sds/sds.h index f2f6a88430..6e07714ec7 100644 --- a/src/shell/sds/sds.h +++ b/src/shell/sds/sds.h @@ -33,7 +33,7 @@ #ifndef __SDS_H #define __SDS_H -#define SDS_MAX_PREALLOC (1024 * 1024) +#define SDS_MAX_PREALLOC (1024*1024) #include #include @@ -47,182 +47,174 @@ typedef char *sds; /* Note: sdshdr5 is never used, we just access the flags byte directly. * However is here to document the layout of type 5 SDS strings. */ -struct __attribute__((__packed__)) sdshdr5 -{ +struct __attribute__ ((__packed__)) sdshdr5 { unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ char buf[]; }; -struct __attribute__((__packed__)) sdshdr8 -{ - uint8_t len; /* used */ - uint8_t alloc; /* excluding the header and null terminator */ +struct __attribute__ ((__packed__)) sdshdr8 { + uint8_t len; /* used */ + uint8_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; -struct __attribute__((__packed__)) sdshdr16 -{ - uint16_t len; /* used */ - uint16_t alloc; /* excluding the header and null terminator */ +struct __attribute__ ((__packed__)) sdshdr16 { + uint16_t len; /* used */ + uint16_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; -struct __attribute__((__packed__)) sdshdr32 -{ - uint32_t len; /* used */ - uint32_t alloc; /* excluding the header and null terminator */ +struct __attribute__ ((__packed__)) sdshdr32 { + uint32_t len; /* used */ + uint32_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; -struct __attribute__((__packed__)) sdshdr64 -{ - uint64_t len; /* used */ - uint64_t alloc; /* excluding the header and null terminator */ +struct __attribute__ ((__packed__)) sdshdr64 { + uint64_t len; /* used */ + uint64_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ char buf[]; }; -#define SDS_TYPE_5 0 -#define SDS_TYPE_8 1 +#define SDS_TYPE_5 0 +#define SDS_TYPE_8 1 #define SDS_TYPE_16 2 #define SDS_TYPE_32 3 #define SDS_TYPE_64 4 #define SDS_TYPE_MASK 7 #define SDS_TYPE_BITS 3 -#define SDS_HDR_VAR(T, s) \ - struct sdshdr##T *sh = (struct sdshdr##T *)((s) - (sizeof(struct sdshdr##T))); -#define SDS_HDR(T, s) ((struct sdshdr##T *)((s) - (sizeof(struct sdshdr##T)))) -#define SDS_TYPE_5_LEN(f) ((f) >> SDS_TYPE_BITS) +#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))); +#define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T)))) +#define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS) -static inline size_t sdslen(const sds s) -{ +static inline size_t sdslen(const sds s) { unsigned char flags = s[-1]; - switch (flags & SDS_TYPE_MASK) { - case SDS_TYPE_5: - return SDS_TYPE_5_LEN(flags); - case SDS_TYPE_8: - return SDS_HDR(8, s)->len; - case SDS_TYPE_16: - return SDS_HDR(16, s)->len; - case SDS_TYPE_32: - return SDS_HDR(32, s)->len; - case SDS_TYPE_64: - return SDS_HDR(64, s)->len; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + return SDS_TYPE_5_LEN(flags); + case SDS_TYPE_8: + return SDS_HDR(8,s)->len; + case SDS_TYPE_16: + return SDS_HDR(16,s)->len; + case SDS_TYPE_32: + return SDS_HDR(32,s)->len; + case SDS_TYPE_64: + return SDS_HDR(64,s)->len; } return 0; } -static inline size_t sdsavail(const sds s) -{ +static inline size_t sdsavail(const sds s) { unsigned char flags = s[-1]; - switch (flags & SDS_TYPE_MASK) { - case SDS_TYPE_5: { - return 0; - } - case SDS_TYPE_8: { - SDS_HDR_VAR(8, s); - return sh->alloc - sh->len; - } - case SDS_TYPE_16: { - SDS_HDR_VAR(16, s); - return sh->alloc - sh->len; - } - case SDS_TYPE_32: { - SDS_HDR_VAR(32, s); - return sh->alloc - sh->len; - } - case SDS_TYPE_64: { - SDS_HDR_VAR(64, s); - return sh->alloc - sh->len; - } + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: { + return 0; + } + case SDS_TYPE_8: { + SDS_HDR_VAR(8,s); + return sh->alloc - sh->len; + } + case SDS_TYPE_16: { + SDS_HDR_VAR(16,s); + return sh->alloc - sh->len; + } + case SDS_TYPE_32: { + SDS_HDR_VAR(32,s); + return sh->alloc - sh->len; + } + case SDS_TYPE_64: { + SDS_HDR_VAR(64,s); + return sh->alloc - sh->len; + } } return 0; } -static inline void sdssetlen(sds s, size_t newlen) -{ +static inline void sdssetlen(sds s, size_t newlen) { unsigned char flags = s[-1]; - switch (flags & SDS_TYPE_MASK) { - case SDS_TYPE_5: { - unsigned char *fp = ((unsigned char *)s) - 1; - *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS); - } break; - case SDS_TYPE_8: - SDS_HDR(8, s)->len = newlen; - break; - case SDS_TYPE_16: - SDS_HDR(16, s)->len = newlen; - break; - case SDS_TYPE_32: - SDS_HDR(32, s)->len = newlen; - break; - case SDS_TYPE_64: - SDS_HDR(64, s)->len = newlen; - break; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + { + unsigned char *fp = ((unsigned char*)s)-1; + *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS); + } + break; + case SDS_TYPE_8: + SDS_HDR(8,s)->len = newlen; + break; + case SDS_TYPE_16: + SDS_HDR(16,s)->len = newlen; + break; + case SDS_TYPE_32: + SDS_HDR(32,s)->len = newlen; + break; + case SDS_TYPE_64: + SDS_HDR(64,s)->len = newlen; + break; } } -static inline void sdsinclen(sds s, size_t inc) -{ +static inline void sdsinclen(sds s, size_t inc) { unsigned char flags = s[-1]; - switch (flags & SDS_TYPE_MASK) { - case SDS_TYPE_5: { - unsigned char *fp = ((unsigned char *)s) - 1; - unsigned char newlen = SDS_TYPE_5_LEN(flags) + inc; - *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS); - } break; - case SDS_TYPE_8: - SDS_HDR(8, s)->len += inc; - break; - case SDS_TYPE_16: - SDS_HDR(16, s)->len += inc; - break; - case SDS_TYPE_32: - SDS_HDR(32, s)->len += inc; - break; - case SDS_TYPE_64: - SDS_HDR(64, s)->len += inc; - break; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + { + unsigned char *fp = ((unsigned char*)s)-1; + unsigned char newlen = SDS_TYPE_5_LEN(flags)+inc; + *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS); + } + break; + case SDS_TYPE_8: + SDS_HDR(8,s)->len += inc; + break; + case SDS_TYPE_16: + SDS_HDR(16,s)->len += inc; + break; + case SDS_TYPE_32: + SDS_HDR(32,s)->len += inc; + break; + case SDS_TYPE_64: + SDS_HDR(64,s)->len += inc; + break; } } /* sdsalloc() = sdsavail() + sdslen() */ -static inline size_t sdsalloc(const sds s) -{ +static inline size_t sdsalloc(const sds s) { unsigned char flags = s[-1]; - switch (flags & SDS_TYPE_MASK) { - case SDS_TYPE_5: - return SDS_TYPE_5_LEN(flags); - case SDS_TYPE_8: - return SDS_HDR(8, s)->alloc; - case SDS_TYPE_16: - return SDS_HDR(16, s)->alloc; - case SDS_TYPE_32: - return SDS_HDR(32, s)->alloc; - case SDS_TYPE_64: - return SDS_HDR(64, s)->alloc; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + return SDS_TYPE_5_LEN(flags); + case SDS_TYPE_8: + return SDS_HDR(8,s)->alloc; + case SDS_TYPE_16: + return SDS_HDR(16,s)->alloc; + case SDS_TYPE_32: + return SDS_HDR(32,s)->alloc; + case SDS_TYPE_64: + return SDS_HDR(64,s)->alloc; } return 0; } -static inline void sdssetalloc(sds s, size_t newlen) -{ +static inline void sdssetalloc(sds s, size_t newlen) { unsigned char flags = s[-1]; - switch (flags & SDS_TYPE_MASK) { - case SDS_TYPE_5: - /* Nothing to do, this type has no total allocation info. */ - break; - case SDS_TYPE_8: - SDS_HDR(8, s)->alloc = newlen; - break; - case SDS_TYPE_16: - SDS_HDR(16, s)->alloc = newlen; - break; - case SDS_TYPE_32: - SDS_HDR(32, s)->alloc = newlen; - break; - case SDS_TYPE_64: - SDS_HDR(64, s)->alloc = newlen; - break; + switch(flags&SDS_TYPE_MASK) { + case SDS_TYPE_5: + /* Nothing to do, this type has no total allocation info. */ + break; + case SDS_TYPE_8: + SDS_HDR(8,s)->alloc = newlen; + break; + case SDS_TYPE_16: + SDS_HDR(16,s)->alloc = newlen; + break; + case SDS_TYPE_32: + SDS_HDR(32,s)->alloc = newlen; + break; + case SDS_TYPE_64: + SDS_HDR(64,s)->alloc = newlen; + break; } } @@ -240,7 +232,8 @@ sds sdscpy(sds s, const char *t); sds sdscatvprintf(sds s, const char *fmt, va_list ap); #ifdef __GNUC__ -sds sdscatprintf(sds s, const char *fmt, ...) __attribute__((format(printf, 2, 3))); +sds sdscatprintf(sds s, const char *fmt, ...) + __attribute__((format(printf, 2, 3))); #else sds sdscatprintf(sds s, const char *fmt, ...); #endif