/* * ":sign define {name} ..." command */ static void sign_define_cmd(char_u *sign_name, char_u *cmdline) { char_u *arg; char_u *p = cmdline; char_u *icon = NULL; char_u *text = NULL; char_u *linehl = NULL; char_u *texthl = NULL; int failed = FALSE; // set values for a defined sign. for (;;) { arg = skipwhite(p); if (*arg == NUL) break; p = skiptowhite_esc(arg); if (STRNCMP(arg, "icon=", 5) == 0) { arg += 5; icon = vim_strnsave(arg, (int)(p - arg)); } else if (STRNCMP(arg, "text=", 5) == 0) { arg += 5; text = vim_strnsave(arg, (int)(p - arg)); } else if (STRNCMP(arg, "linehl=", 7) == 0) { arg += 7; linehl = vim_strnsave(arg, (int)(p - arg)); } else if (STRNCMP(arg, "texthl=", 7) == 0) { arg += 7; texthl = vim_strnsave(arg, (int)(p - arg)); } else { semsg(_(e_invarg2), arg); failed = TRUE; break; } } if (!failed) sign_define_by_name(sign_name, icon, linehl, text, texthl); vim_free(icon); vim_free(text); vim_free(linehl); vim_free(texthl); }
/* * Find item "key[len]" in Dictionary "d". * If "len" is negative use strlen(key). * Returns NULL when not found. */ dictitem_T * dict_find(dict_T *d, char_u *key, int len) { #define AKEYLEN 200 char_u buf[AKEYLEN]; char_u *akey; char_u *tofree = NULL; hashitem_T *hi; if (d == NULL) return NULL; if (len < 0) akey = key; else if (len >= AKEYLEN) { tofree = akey = vim_strnsave(key, len); if (akey == NULL) return NULL; } else { /* Avoid a malloc/free by using buf[]. */ vim_strncpy(buf, key, len); akey = buf; } hi = hash_find(&d->dv_hashtab, akey); vim_free(tofree); if (HASHITEM_EMPTY(hi)) return NULL; return HI2DI(hi); }
char *os_getenvname_at_index(size_t index) { # if defined(AMIGA) || defined(__MRC__) || defined(__SC__) // No environ[] on the Amiga and on the Mac (using MPW). return NULL; # else # if defined(HAVE__NSGETENVIRON) char **environ = *_NSGetEnviron(); # elif !defined(__WIN32__) // Borland C++ 5.2 has this in a header file. extern char **environ; # endif // check if index is inside the environ array for (size_t i = 0; i < index; i++) { if (environ[i] == NULL) { return NULL; } } char *str = environ[index]; if (str == NULL) { return NULL; } int namesize = 0; while (str[namesize] != '=' && str[namesize] != NUL) { namesize++; } char *name = (char *)vim_strnsave((char_u *)str, namesize); return name; # endif }
/* * Duplicate the menu item text and then process to see if a mnemonic key * and/or accelerator text has been identified. * Returns a pointer to allocated memory, or NULL for failure. * If mnemonic != NULL, *mnemonic is set to the character after the first '&'. * If actext != NULL, *actext is set to the text after the first TAB. */ static char_u *menu_text(char_u *str, int *mnemonic, char_u **actext) { char_u *p; char_u *text; /* Locate accelerator text, after the first TAB */ p = vim_strchr(str, TAB); if (p != NULL) { if (actext != NULL) *actext = vim_strsave(p + 1); text = vim_strnsave(str, (int)(p - str)); } else text = vim_strsave(str); /* Find mnemonic characters "&a" and reduce "&&" to "&". */ for (p = text; p != NULL; ) { p = vim_strchr(p, '&'); if (p != NULL) { if (p[1] == NUL) /* trailing "&" */ break; if (mnemonic != NULL && p[1] != '&') *mnemonic = p[1]; STRMOVE(p, p + 1); p = p + 1; } } return text; }
/* * Initialize the text for a new sign */ static int sign_define_init_text(sign_T *sp, char_u *text) { char_u *s; char_u *endp; int cells; int len; endp = text + (int)STRLEN(text); // Remove backslashes so that it is possible to use a space. for (s = text; s + 1 < endp; ++s) if (*s == '\\') { STRMOVE(s, s + 1); --endp; } // Count cells and check for non-printable chars if (has_mbyte) { cells = 0; for (s = text; s < endp; s += (*mb_ptr2len)(s)) { if (!vim_isprintc((*mb_ptr2char)(s))) break; cells += (*mb_ptr2cells)(s); } } else { for (s = text; s < endp; ++s) if (!vim_isprintc(*s)) break; cells = (int)(s - text); } // Currently sign text must be one or two display cells if (s != endp || cells < 1 || cells > 2) { semsg(_("E239: Invalid sign text: %s"), text); return FAIL; } vim_free(sp->sn_text); // Allocate one byte more if we need to pad up // with a space. len = (int)(endp - text + ((cells == 1) ? 1 : 0)); sp->sn_text = vim_strnsave(text, len); // For single character sign text, pad with a space. if (sp->sn_text != NULL && cells == 1) STRCPY(sp->sn_text + len - 1, " "); return OK; }
/* * Modify a menu name starting with "PopUp" to include the mode character. * Returns the name in allocated memory. */ static char_u *popup_mode_name(char_u *name, int idx) { int len = (int)STRLEN(name); char_u *p = vim_strnsave(name, len + 1); memmove(p + 6, p + 5, (size_t)(len - 4)); p[5] = menu_mode_chars[idx]; return p; }
/* * Modify a menu name starting with "PopUp" to include the mode character. * Returns the name in allocated memory. */ static char_u *popup_mode_name(char_u *name, int idx) { size_t len = STRLEN(name); assert(len >= 4); char_u *p = vim_strnsave(name, len + 1); memmove(p + 6, p + 5, len - 4); p[5] = menu_mode_chars[idx]; return p; }
/* * ":menutrans". * This function is also defined without the +multi_lang feature, in which * case the commands are ignored. */ void ex_menutranslate(exarg_T *eap) { char_u *arg = eap->arg; menutrans_T *tp; char_u *from, *from_noamp, *to; if (menutrans_ga.ga_itemsize == 0) ga_init(&menutrans_ga, (int)sizeof(menutrans_T), 5); /* * ":menutrans clear": clear all translations. */ if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5))) { tp = (menutrans_T *)menutrans_ga.ga_data; for (int i = 0; i < menutrans_ga.ga_len; ++i) { free(tp[i].from); free(tp[i].from_noamp); free(tp[i].to); } ga_clear(&menutrans_ga); /* Delete all "menutrans_" global variables. */ del_menutrans_vars(); } else { /* ":menutrans from to": add translation */ from = arg; arg = menu_skip_part(arg); to = skipwhite(arg); *arg = NUL; arg = menu_skip_part(to); if (arg == to) EMSG(_(e_invarg)); else { ga_grow(&menutrans_ga, 1); tp = (menutrans_T *)menutrans_ga.ga_data; from = vim_strsave(from); from_noamp = menu_text(from, NULL, NULL); to = vim_strnsave(to, (int)(arg - to)); if (from_noamp != NULL) { menu_translate_tab_and_shift(from); menu_translate_tab_and_shift(to); menu_unescape_name(from); menu_unescape_name(to); tp[menutrans_ga.ga_len].from = from; tp[menutrans_ga.ga_len].from_noamp = from_noamp; tp[menutrans_ga.ga_len].to = to; ++menutrans_ga.ga_len; } else { free(from); free(from_noamp); free(to); } } } }
/* * Make a copy of "str" and append it as an item to list "l". * When "len" >= 0 use "str[len]". * Returns FAIL when out of memory. */ int list_append_string(list_T *l, char_u *str, int len) { listitem_T *li = listitem_alloc(); if (li == NULL) return FAIL; list_append(l, li); li->li_tv.v_type = VAR_STRING; li->li_tv.v_lock = 0; if (str == NULL) li->li_tv.vval.v_string = NULL; else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len) : vim_strsave(str))) == NULL) return FAIL; return OK; }
/* * ":menutrans". * This function is also defined without the +multi_lang feature, in which * case the commands are ignored. */ void ex_menutranslate(exarg_T *eap) { char_u *arg = eap->arg; char_u *from, *from_noamp, *to; if (menutrans_ga.ga_itemsize == 0) ga_init(&menutrans_ga, (int)sizeof(menutrans_T), 5); /* * ":menutrans clear": clear all translations. */ if (STRNCMP(arg, "clear", 5) == 0 && ends_excmd(*skipwhite(arg + 5))) { GA_DEEP_CLEAR(&menutrans_ga, menutrans_T, FREE_MENUTRANS); /* Delete all "menutrans_" global variables. */ del_menutrans_vars(); } else { /* ":menutrans from to": add translation */ from = arg; arg = menu_skip_part(arg); to = skipwhite(arg); *arg = NUL; arg = menu_skip_part(to); if (arg == to) EMSG(_(e_invarg)); else { from = vim_strsave(from); from_noamp = menu_text(from, NULL, NULL); to = vim_strnsave(to, (int)(arg - to)); if (from_noamp != NULL) { menu_translate_tab_and_shift(from); menu_translate_tab_and_shift(to); menu_unescape_name(from); menu_unescape_name(to); menutrans_T* tp = GA_APPEND_VIA_PTR(menutrans_T, &menutrans_ga); tp->from = from; tp->from_noamp = from_noamp; tp->to = to; } else { free(from); free(to); } } } }
/* * Return the line at mark "mp". Truncate to fit in window. * The returned string has been allocated. */ static char_u *mark_line(pos_T *mp, int lead_len) { char_u *s, *p; int len; if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count) return vim_strsave((char_u *)"-invalid-"); s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (int)Columns); /* Truncate the line to fit it in the window */ len = 0; for (p = s; *p != NUL; mb_ptr_adv(p)) { len += ptr2cells(p); if (len >= Columns - lead_len) break; } *p = NUL; return s; }
/* * Duplicate the menu item text and then process to see if a mnemonic key * and/or accelerator text has been identified. * Returns a pointer to allocated memory, or NULL for failure. * If mnemonic != NULL, *mnemonic is set to the character after the first '&'. * If actext != NULL, *actext is set to the text after the first TAB. */ static char_u *menu_text(char_u *str, int *mnemonic, char_u **actext) { char_u *p; char_u *text; /* Locate accelerator text, after the first TAB */ p = vim_strchr(str, TAB); if (p != NULL) { if (actext != NULL) *actext = vim_strsave(p + 1); text = vim_strnsave(str, (int)(p - str)); } else text = vim_strsave(str); /* Find mnemonic characters "&a" and reduce "&&" to "&". */ for (p = text; p != NULL; ) { p = vim_strchr(p, '&'); if (p != NULL) { if (p[1] == NUL) /* trailing "&" */ break; if (mnemonic != NULL && p[1] != '&') #if !defined(__MVS__) || defined(MOTIF390_MNEMONIC_FIXED) *mnemonic = p[1]; #else { /* * Well there is a bug in the Motif libraries on OS390 Unix. * The mnemonic keys needs to be converted to ASCII values * first. * This behavior has been seen in 2.8 and 2.9. */ char c = p[1]; __etoa_l(&c, 1); *mnemonic = c; } #endif STRMOVE(p, p + 1); p = p + 1; } } return text; }
/* * Decode one item and put it in "res". If "res" is NULL only advance. * Must already have skipped white space. * * Return FAIL for a decoding error (and give an error). * Return MAYBE for an incomplete message. */ static int json_decode_item(js_read_T *reader, typval_T *res, int options) { char_u *p; int len; int retval; garray_T stack; typval_T item; typval_T *cur_item; json_dec_item_T *top_item; char_u key_buf[NUMBUFLEN]; ga_init2(&stack, sizeof(json_dec_item_T), 100); cur_item = res; init_tv(&item); if (res != NULL) init_tv(res); fill_numbuflen(reader); p = reader->js_buf + reader->js_used; for (;;) { top_item = NULL; if (stack.ga_len > 0) { top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1; json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p == NUL) { retval = MAYBE; if (top_item->jd_type == JSON_OBJECT) /* did get the key, clear it */ clear_tv(&top_item->jd_key_tv); goto theend; } if (top_item->jd_type == JSON_OBJECT_KEY || top_item->jd_type == JSON_ARRAY) { /* Check for end of object or array. */ if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}')) { ++reader->js_used; /* consume the ']' or '}' */ --stack.ga_len; if (stack.ga_len == 0) { retval = OK; goto theend; } if (cur_item != NULL) cur_item = &top_item->jd_tv; goto item_end; } } } if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY && (options & JSON_JS) && reader->js_buf[reader->js_used] != '"' && reader->js_buf[reader->js_used] != '\'' && reader->js_buf[reader->js_used] != '[' && reader->js_buf[reader->js_used] != '{') { char_u *key; /* accept an object key that is not in quotes */ key = p = reader->js_buf + reader->js_used; while (*p != NUL && *p != ':' && *p > ' ') ++p; if (cur_item != NULL) { cur_item->v_type = VAR_STRING; cur_item->vval.v_string = vim_strnsave(key, (int)(p - key)); top_item->jd_key = cur_item->vval.v_string; } reader->js_used += (int)(p - key); } else { switch (*p) { case '[': /* start of array */ if (top_item && top_item->jd_type == JSON_OBJECT_KEY) { retval = FAIL; break; } if (ga_grow(&stack, 1) == FAIL) { retval = FAIL; break; } if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_NONE; retval = FAIL; break; } ++reader->js_used; /* consume the '[' */ top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len; top_item->jd_type = JSON_ARRAY; ++stack.ga_len; if (cur_item != NULL) { top_item->jd_tv = *cur_item; cur_item = &item; } continue; case '{': /* start of object */ if (top_item && top_item->jd_type == JSON_OBJECT_KEY) { retval = FAIL; break; } if (ga_grow(&stack, 1) == FAIL) { retval = FAIL; break; } if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_NONE; retval = FAIL; break; } ++reader->js_used; /* consume the '{' */ top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len; top_item->jd_type = JSON_OBJECT_KEY; ++stack.ga_len; if (cur_item != NULL) { top_item->jd_tv = *cur_item; cur_item = &top_item->jd_key_tv; } continue; case '"': /* string */ retval = json_decode_string(reader, cur_item, *p); break; case '\'': if (options & JSON_JS) retval = json_decode_string(reader, cur_item, *p); else { EMSG(_(e_invarg)); retval = FAIL; } break; case ',': /* comma: empty item */ if ((options & JSON_JS) == 0) { EMSG(_(e_invarg)); retval = FAIL; break; } /* FALLTHROUGH */ case NUL: /* empty */ if (cur_item != NULL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_NONE; } retval = OK; break; default: if (VIM_ISDIGIT(*p) || *p == '-') { #ifdef FEAT_FLOAT char_u *sp = p; if (*sp == '-') { ++sp; if (*sp == NUL) { retval = MAYBE; break; } if (!VIM_ISDIGIT(*sp)) { EMSG(_(e_invarg)); retval = FAIL; break; } } sp = skipdigits(sp); if (*sp == '.' || *sp == 'e' || *sp == 'E') { if (cur_item == NULL) { float_T f; len = string2float(p, &f); } else { cur_item->v_type = VAR_FLOAT; len = string2float(p, &cur_item->vval.v_float); } } else #endif { varnumber_T nr; vim_str2nr(reader->js_buf + reader->js_used, NULL, &len, 0, /* what */ &nr, NULL, 0); if (cur_item != NULL) { cur_item->v_type = VAR_NUMBER; cur_item->vval.v_number = nr; } } reader->js_used += len; retval = OK; break; } if (STRNICMP((char *)p, "false", 5) == 0) { reader->js_used += 5; if (cur_item != NULL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_FALSE; } retval = OK; break; } if (STRNICMP((char *)p, "true", 4) == 0) { reader->js_used += 4; if (cur_item != NULL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_TRUE; } retval = OK; break; } if (STRNICMP((char *)p, "null", 4) == 0) { reader->js_used += 4; if (cur_item != NULL) { cur_item->v_type = VAR_SPECIAL; cur_item->vval.v_number = VVAL_NULL; } retval = OK; break; } #ifdef FEAT_FLOAT if (STRNICMP((char *)p, "NaN", 3) == 0) { reader->js_used += 3; if (cur_item != NULL) { cur_item->v_type = VAR_FLOAT; cur_item->vval.v_float = NAN; } retval = OK; break; } if (STRNICMP((char *)p, "Infinity", 8) == 0) { reader->js_used += 8; if (cur_item != NULL) { cur_item->v_type = VAR_FLOAT; cur_item->vval.v_float = INFINITY; } retval = OK; break; } #endif /* check for truncated name */ len = (int)(reader->js_end - (reader->js_buf + reader->js_used)); if ( (len < 5 && STRNICMP((char *)p, "false", len) == 0) #ifdef FEAT_FLOAT || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0) || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0) #endif || (len < 4 && (STRNICMP((char *)p, "true", len) == 0 || STRNICMP((char *)p, "null", len) == 0))) retval = MAYBE; else retval = FAIL; break; } /* We are finished when retval is FAIL or MAYBE and when at the * toplevel. */ if (retval == FAIL) break; if (retval == MAYBE || stack.ga_len == 0) goto theend; if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY && cur_item != NULL) { top_item->jd_key = get_tv_string_buf_chk(cur_item, key_buf); if (top_item->jd_key == NULL) { clear_tv(cur_item); EMSG(_(e_invarg)); retval = FAIL; goto theend; } } } item_end: top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1; switch (top_item->jd_type) { case JSON_ARRAY: if (res != NULL) { listitem_T *li = listitem_alloc(); if (li == NULL) { clear_tv(cur_item); retval = FAIL; goto theend; } li->li_tv = *cur_item; list_append(top_item->jd_tv.vval.v_list, li); } if (cur_item != NULL) cur_item = &item; json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p == ',') ++reader->js_used; else if (*p != ']') { if (*p == NUL) retval = MAYBE; else { EMSG(_(e_invarg)); retval = FAIL; } goto theend; } break; case JSON_OBJECT_KEY: json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p != ':') { if (cur_item != NULL) clear_tv(cur_item); if (*p == NUL) retval = MAYBE; else { EMSG(_(e_invarg)); retval = FAIL; } goto theend; } ++reader->js_used; json_skip_white(reader); top_item->jd_type = JSON_OBJECT; if (cur_item != NULL) cur_item = &item; break; case JSON_OBJECT: if (cur_item != NULL && dict_find(top_item->jd_tv.vval.v_dict, top_item->jd_key, -1) != NULL) { EMSG2(_("E938: Duplicate key in JSON: \"%s\""), top_item->jd_key); clear_tv(&top_item->jd_key_tv); clear_tv(cur_item); retval = FAIL; goto theend; } if (cur_item != NULL) { dictitem_T *di = dictitem_alloc(top_item->jd_key); clear_tv(&top_item->jd_key_tv); if (di == NULL) { clear_tv(cur_item); retval = FAIL; goto theend; } di->di_tv = *cur_item; di->di_tv.v_lock = 0; if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL) { dictitem_free(di); retval = FAIL; goto theend; } } json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p == ',') ++reader->js_used; else if (*p != '}') { if (*p == NUL) retval = MAYBE; else { EMSG(_(e_invarg)); retval = FAIL; } goto theend; } top_item->jd_type = JSON_OBJECT_KEY; if (cur_item != NULL) cur_item = &top_item->jd_key_tv; break; } } /* Get here when parsing failed. */ if (res != NULL) { clear_tv(res); res->v_type = VAR_SPECIAL; res->vval.v_number = VVAL_NONE; } EMSG(_(e_invarg)); theend: ga_clear(&stack); return retval; }
/// ":loadkeymap" command: load the following lines as the keymap. /// /// @param eap void ex_loadkeymap(exarg_T *eap) { char_u *line; char_u *p; char_u *s; #define KMAP_LLEN 200 // max length of "to" and "from" together char_u buf[KMAP_LLEN + 11]; char_u *save_cpo = p_cpo; if (!getline_equal(eap->getline, eap->cookie, getsourceline)) { EMSG(_("E105: Using :loadkeymap not in a sourced file")); return; } // Stop any active keymap and clear the table. keymap_unload(); curbuf->b_kmap_state = 0; ga_init(&curbuf->b_kmap_ga, (int)sizeof(kmap_T), 20); // Set 'cpoptions' to "C" to avoid line continuation. p_cpo = (char_u *)"C"; // Get each line of the sourced file, break at the end. for (;;) { line = eap->getline(0, eap->cookie, 0); if (line == NULL) { break; } p = skipwhite(line); if ((*p != '"') && (*p != NUL)) { kmap_T *kp = GA_APPEND_VIA_PTR(kmap_T, &curbuf->b_kmap_ga); s = skiptowhite(p); kp->from = vim_strnsave(p, (size_t)(s - p)); p = skipwhite(s); s = skiptowhite(p); kp->to = vim_strnsave(p, (size_t)(s - p)); if ((STRLEN(kp->from) + STRLEN(kp->to) >= KMAP_LLEN) || (*kp->from == NUL) || (*kp->to == NUL)) { if (*kp->to == NUL) { EMSG(_("E791: Empty keymap entry")); } xfree(kp->from); xfree(kp->to); --curbuf->b_kmap_ga.ga_len; } } xfree(line); } // setup ":lnoremap" to map the keys for (int i = 0; i < curbuf->b_kmap_ga.ga_len; ++i) { vim_snprintf((char *)buf, sizeof(buf), "<buffer> %s %s", ((kmap_T *)curbuf->b_kmap_ga.ga_data)[i].from, ((kmap_T *)curbuf->b_kmap_ga.ga_data)[i].to); (void)do_map(2, buf, LANGMAP, FALSE); } p_cpo = save_cpo; curbuf->b_kmap_state |= KEYMAP_LOADED; status_redraw_curbuf(); }
/* * Initialization routine for vim_findfile(). * * Returns the newly allocated search context or NULL if an error occurred. * * Don't forget to clean up by calling vim_findfile_cleanup() if you are done * with the search context. * * Find the file 'filename' in the directory 'path'. * The parameter 'path' may contain wildcards. If so only search 'level' * directories deep. The parameter 'level' is the absolute maximum and is * not related to restricts given to the '**' wildcard. If 'level' is 100 * and you use '**200' vim_findfile() will stop after 100 levels. * * 'filename' cannot contain wildcards! It is used as-is, no backslashes to * escape special characters. * * If 'stopdirs' is not NULL and nothing is found downward, the search is * restarted on the next higher directory level. This is repeated until the * start-directory of a search is contained in 'stopdirs'. 'stopdirs' has the * format ";*<dirname>*\(;<dirname>\)*;\=$". * * If the 'path' is relative, the starting dir for the search is either VIM's * current dir or if the path starts with "./" the current files dir. * If the 'path' is absolute, the starting dir is that part of the path before * the first wildcard. * * Upward search is only done on the starting dir. * * If 'free_visited' is TRUE the list of already visited files/directories is * cleared. Set this to FALSE if you just want to search from another * directory, but want to be sure that no directory from a previous search is * searched again. This is useful if you search for a file at different places. * The list of visited files/dirs can also be cleared with the function * vim_findfile_free_visited(). * * Set the parameter 'find_what' to FINDFILE_DIR if you want to search for * directories only, FINDFILE_FILE for files only, FINDFILE_BOTH for both. * * A search context returned by a previous call to vim_findfile_init() can be * passed in the parameter "search_ctx_arg". This context is reused and * reinitialized with the new parameters. The list of already visited * directories from this context is only deleted if the parameter * "free_visited" is true. Be aware that the passed "search_ctx_arg" is freed * if the reinitialization fails. * * If you don't have a search context from a previous call "search_ctx_arg" * must be NULL. * * This function silently ignores a few errors, vim_findfile() will have * limited functionality then. */ void * vim_findfile_init ( char_u *path, char_u *filename, char_u *stopdirs, int level, int free_visited, int find_what, void *search_ctx_arg, int tagfile, /* expanding names of tags files */ char_u *rel_fname /* file name to use for "." */ ) { char_u *wc_part; ff_stack_T *sptr; ff_search_ctx_T *search_ctx; /* If a search context is given by the caller, reuse it, else allocate a * new one. */ if (search_ctx_arg != NULL) search_ctx = search_ctx_arg; else { search_ctx = xcalloc(1, sizeof(ff_search_ctx_T)); } search_ctx->ffsc_find_what = find_what; search_ctx->ffsc_tagfile = tagfile; /* clear the search context, but NOT the visited lists */ ff_clear(search_ctx); /* clear visited list if wanted */ if (free_visited == TRUE) vim_findfile_free_visited(search_ctx); else { /* Reuse old visited lists. Get the visited list for the given * filename. If no list for the current filename exists, creates a new * one. */ search_ctx->ffsc_visited_list = ff_get_visited_list(filename, &search_ctx->ffsc_visited_lists_list); if (search_ctx->ffsc_visited_list == NULL) goto error_return; search_ctx->ffsc_dir_visited_list = ff_get_visited_list(filename, &search_ctx->ffsc_dir_visited_lists_list); if (search_ctx->ffsc_dir_visited_list == NULL) goto error_return; } if (ff_expand_buffer == NULL) { ff_expand_buffer = xmalloc(MAXPATHL); } /* Store information on starting dir now if path is relative. * If path is absolute, we do that later. */ if (path[0] == '.' && (vim_ispathsep(path[1]) || path[1] == NUL) && (!tagfile || vim_strchr(p_cpo, CPO_DOTTAG) == NULL) && rel_fname != NULL) { int len = (int)(path_tail(rel_fname) - rel_fname); if (!vim_isAbsName(rel_fname) && len + 1 < MAXPATHL) { /* Make the start dir an absolute path name. */ STRLCPY(ff_expand_buffer, rel_fname, len + 1); search_ctx->ffsc_start_dir = FullName_save(ff_expand_buffer, FALSE); } else search_ctx->ffsc_start_dir = vim_strnsave(rel_fname, len); if (*++path != NUL) ++path; } else if (*path == NUL || !vim_isAbsName(path)) { #ifdef BACKSLASH_IN_FILENAME /* "c:dir" needs "c:" to be expanded, otherwise use current dir */ if (*path != NUL && path[1] == ':') { char_u drive[3]; drive[0] = path[0]; drive[1] = ':'; drive[2] = NUL; if (vim_FullName(drive, ff_expand_buffer, MAXPATHL, TRUE) == FAIL) goto error_return; path += 2; } else #endif if (os_dirname(ff_expand_buffer, MAXPATHL) == FAIL) goto error_return; search_ctx->ffsc_start_dir = vim_strsave(ff_expand_buffer); #ifdef BACKSLASH_IN_FILENAME /* A path that starts with "/dir" is relative to the drive, not to the * directory (but not for "//machine/dir"). Only use the drive name. */ if ((*path == '/' || *path == '\\') && path[1] != path[0] && search_ctx->ffsc_start_dir[1] == ':') search_ctx->ffsc_start_dir[2] = NUL; #endif } /* * If stopdirs are given, split them into an array of pointers. * If this fails (mem allocation), there is no upward search at all or a * stop directory is not recognized -> continue silently. * If stopdirs just contains a ";" or is empty, * search_ctx->ffsc_stopdirs_v will only contain a NULL pointer. This * is handled as unlimited upward search. See function * ff_path_in_stoplist() for details. */ if (stopdirs != NULL) { char_u *walker = stopdirs; int dircount; while (*walker == ';') walker++; dircount = 1; search_ctx->ffsc_stopdirs_v = xmalloc(sizeof(char_u *)); do { char_u *helper; void *ptr; helper = walker; ptr = xrealloc(search_ctx->ffsc_stopdirs_v, (dircount + 1) * sizeof(char_u *)); search_ctx->ffsc_stopdirs_v = ptr; walker = vim_strchr(walker, ';'); if (walker) { search_ctx->ffsc_stopdirs_v[dircount-1] = vim_strnsave(helper, (int)(walker - helper)); walker++; } else /* this might be "", which means ascent till top * of directory tree. */ search_ctx->ffsc_stopdirs_v[dircount-1] = vim_strsave(helper); dircount++; } while (walker != NULL); search_ctx->ffsc_stopdirs_v[dircount-1] = NULL; } search_ctx->ffsc_level = level; /* split into: * -fix path * -wildcard_stuff (might be NULL) */ wc_part = vim_strchr(path, '*'); if (wc_part != NULL) { int llevel; int len; char *errpt; /* save the fix part of the path */ search_ctx->ffsc_fix_path = vim_strnsave(path, (int)(wc_part - path)); /* * copy wc_path and add restricts to the '**' wildcard. * The octet after a '**' is used as a (binary) counter. * So '**3' is transposed to '**^C' ('^C' is ASCII value 3) * or '**76' is transposed to '**N'( 'N' is ASCII value 76). * For EBCDIC you get different character values. * If no restrict is given after '**' the default is used. * Due to this technique the path looks awful if you print it as a * string. */ len = 0; while (*wc_part != NUL) { if (len + 5 >= MAXPATHL) { EMSG(_(e_pathtoolong)); break; } if (STRNCMP(wc_part, "**", 2) == 0) { ff_expand_buffer[len++] = *wc_part++; ff_expand_buffer[len++] = *wc_part++; llevel = strtol((char *)wc_part, &errpt, 10); if ((char_u *)errpt != wc_part && llevel > 0 && llevel < 255) ff_expand_buffer[len++] = llevel; else if ((char_u *)errpt != wc_part && llevel == 0) /* restrict is 0 -> remove already added '**' */ len -= 2; else ff_expand_buffer[len++] = FF_MAX_STAR_STAR_EXPAND; wc_part = (char_u *)errpt; if (*wc_part != NUL && !vim_ispathsep(*wc_part)) { EMSG2(_( "E343: Invalid path: '**[number]' must be at the end of the path or be followed by '%s'."), PATHSEPSTR); goto error_return; } } else ff_expand_buffer[len++] = *wc_part++; } ff_expand_buffer[len] = NUL; search_ctx->ffsc_wc_path = vim_strsave(ff_expand_buffer); } else search_ctx->ffsc_fix_path = vim_strsave(path); if (search_ctx->ffsc_start_dir == NULL) { /* store the fix part as startdir. * This is needed if the parameter path is fully qualified. */ search_ctx->ffsc_start_dir = vim_strsave(search_ctx->ffsc_fix_path); search_ctx->ffsc_fix_path[0] = NUL; } /* create an absolute path */ if (STRLEN(search_ctx->ffsc_start_dir) + STRLEN(search_ctx->ffsc_fix_path) + 3 >= MAXPATHL) { EMSG(_(e_pathtoolong)); goto error_return; } STRCPY(ff_expand_buffer, search_ctx->ffsc_start_dir); add_pathsep(ff_expand_buffer); { size_t eb_len = STRLEN(ff_expand_buffer); char_u *buf = xmalloc(eb_len + STRLEN(search_ctx->ffsc_fix_path) + 1); STRCPY(buf, ff_expand_buffer); STRCPY(buf + eb_len, search_ctx->ffsc_fix_path); if (os_isdir(buf)) { STRCAT(ff_expand_buffer, search_ctx->ffsc_fix_path); add_pathsep(ff_expand_buffer); } else { char_u *p = path_tail(search_ctx->ffsc_fix_path); char_u *wc_path = NULL; char_u *temp = NULL; int len = 0; if (p > search_ctx->ffsc_fix_path) { len = (int)(p - search_ctx->ffsc_fix_path) - 1; STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len); add_pathsep(ff_expand_buffer); } else len = (int)STRLEN(search_ctx->ffsc_fix_path); if (search_ctx->ffsc_wc_path != NULL) { wc_path = vim_strsave(search_ctx->ffsc_wc_path); temp = xmalloc(STRLEN(search_ctx->ffsc_wc_path) + STRLEN(search_ctx->ffsc_fix_path + len) + 1); } if (temp == NULL || wc_path == NULL) { free(buf); free(temp); free(wc_path); goto error_return; } STRCPY(temp, search_ctx->ffsc_fix_path + len); STRCAT(temp, search_ctx->ffsc_wc_path); free(search_ctx->ffsc_wc_path); free(wc_path); search_ctx->ffsc_wc_path = temp; } free(buf); } sptr = ff_create_stack_element(ff_expand_buffer, search_ctx->ffsc_wc_path, level, 0); ff_push(search_ctx, sptr); search_ctx->ffsc_file_to_search = vim_strsave(filename); return search_ctx; error_return: /* * We clear the search context now! * Even when the caller gave us a (perhaps valid) context we free it here, * as we might have already destroyed it. */ vim_findfile_cleanup(search_ctx); return NULL; }
/* * Get the text and position to be evaluated for "beval". * If "getword" is true the returned text is not the whole line but the * relevant word in allocated memory. * Returns OK or FAIL. */ int get_beval_info( BalloonEval *beval, int getword, win_T **winp, linenr_T *lnump, char_u **textp, int *colp) { win_T *wp; int row, col; char_u *lbuf; linenr_T lnum; *textp = NULL; row = Y_2_ROW(beval->y); col = X_2_COL(beval->x); #ifdef FEAT_WINDOWS wp = mouse_find_win(&row, &col); #else wp = firstwin; #endif if (wp != NULL && row < wp->w_height && col < W_WIDTH(wp)) { /* Found a window and the cursor is in the text. Now find the line * number. */ if (!mouse_comp_pos(wp, &row, &col, &lnum)) { /* Not past end of the file. */ lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE); if (col <= win_linetabsize(wp, lbuf, (colnr_T)MAXCOL)) { /* Not past end of line. */ if (getword) { /* For Netbeans we get the relevant part of the line * instead of the whole line. */ int len; pos_T *spos = NULL, *epos = NULL; if (VIsual_active) { if (lt(VIsual, curwin->w_cursor)) { spos = &VIsual; epos = &curwin->w_cursor; } else { spos = &curwin->w_cursor; epos = &VIsual; } } col = vcol2col(wp, lnum, col); if (VIsual_active && wp->w_buffer == curwin->w_buffer && (lnum == spos->lnum ? col >= (int)spos->col : lnum > spos->lnum) && (lnum == epos->lnum ? col <= (int)epos->col : lnum < epos->lnum)) { /* Visual mode and pointing to the line with the * Visual selection: return selected text, with a * maximum of one line. */ if (spos->lnum != epos->lnum || spos->col == epos->col) return FAIL; lbuf = ml_get_buf(curwin->w_buffer, VIsual.lnum, FALSE); len = epos->col - spos->col; if (*p_sel != 'e') len += MB_PTR2LEN(lbuf + epos->col); lbuf = vim_strnsave(lbuf + spos->col, len); lnum = spos->lnum; col = spos->col; } else { /* Find the word under the cursor. */ ++emsg_off; len = find_ident_at_pos(wp, lnum, (colnr_T)col, &lbuf, FIND_IDENT + FIND_STRING + FIND_EVAL); --emsg_off; if (len == 0) return FAIL; lbuf = vim_strnsave(lbuf, len); } } *winp = wp; *lnump = lnum; *textp = lbuf; *colp = col; beval->ts = wp->w_buffer->b_p_ts; return OK; } } } return FAIL; }
static int list_join_inner( garray_T *gap, /* to store the result in */ list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap) /* to keep each list item string */ { int i; join_T *p; int len; int sumlen = 0; int first = TRUE; char_u *tofree; char_u numbuf[NUMBUFLEN]; listitem_T *item; char_u *s; /* Stringify each item in the list. */ for (item = l->lv_first; item != NULL && !got_int; item = item->li_next) { s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID, echo_style, restore_copyID, !echo_style); if (s == NULL) return FAIL; len = (int)STRLEN(s); sumlen += len; (void)ga_grow(join_gap, 1); p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++); if (tofree != NULL || s != numbuf) { p->s = s; p->tofree = tofree; } else { p->s = vim_strnsave(s, len); p->tofree = p->s; } line_breakcheck(); if (did_echo_string_emsg) /* recursion error, bail out */ break; } /* Allocate result buffer with its total size, avoid re-allocation and * multiple copy operations. Add 2 for a tailing ']' and NUL. */ if (join_gap->ga_len >= 2) sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1); if (ga_grow(gap, sumlen + 2) == FAIL) return FAIL; for (i = 0; i < join_gap->ga_len && !got_int; ++i) { if (first) first = FALSE; else ga_concat(gap, sep); p = ((join_T *)join_gap->ga_data) + i; if (p->s != NULL) ga_concat(gap, p->s); line_breakcheck(); } return OK; }
static int json_decode_object(js_read_T *reader, typval_T *res, int options) { char_u *p; typval_T tvkey; typval_T item; dictitem_T *di; char_u buf[NUMBUFLEN]; char_u *key = NULL; int ret; if (res != NULL && rettv_dict_alloc(res) == FAIL) { res->v_type = VAR_SPECIAL; res->vval.v_number = VVAL_NONE; return FAIL; } ++reader->js_used; /* consume the '{' */ while (TRUE) { json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p == NUL) return MAYBE; if (*p == '}') { ++reader->js_used; /* consume the '}' */ break; } if ((options & JSON_JS) && reader->js_buf[reader->js_used] != '"') { /* accept a key that is not in quotes */ key = p = reader->js_buf + reader->js_used; while (*p != NUL && *p != ':' && *p > ' ') ++p; tvkey.v_type = VAR_STRING; tvkey.vval.v_string = vim_strnsave(key, (int)(p - key)); reader->js_used += (int)(p - key); key = tvkey.vval.v_string; } else { ret = json_decode_item(reader, res == NULL ? NULL : &tvkey, options); if (ret != OK) return ret; if (res != NULL) { key = get_tv_string_buf_chk(&tvkey, buf); if (key == NULL || *key == NUL) { clear_tv(&tvkey); return FAIL; } } } json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p != ':') { if (res != NULL) clear_tv(&tvkey); if (*p == NUL) return MAYBE; return FAIL; } ++reader->js_used; json_skip_white(reader); ret = json_decode_item(reader, res == NULL ? NULL : &item, options); if (ret != OK) { if (res != NULL) clear_tv(&tvkey); return ret; } if (res != NULL) { di = dictitem_alloc(key); clear_tv(&tvkey); if (di == NULL) { clear_tv(&item); return FAIL; } di->di_tv = item; if (dict_add(res->vval.v_dict, di) == FAIL) { dictitem_free(di); return FAIL; } } json_skip_white(reader); p = reader->js_buf + reader->js_used; if (*p == ',') ++reader->js_used; else if (*p != '}') { if (*p == NUL) return MAYBE; return FAIL; } } return OK; }