Blame src/utils.c

Packit 1ac44c
/**************************************************************************
Packit 1ac44c
 *   utils.c  --  This file is part of GNU nano.                          *
Packit 1ac44c
 *                                                                        *
Packit 1ac44c
 *   Copyright (C) 1999-2011, 2013-2018 Free Software Foundation, Inc.    *
Packit 1ac44c
 *   Copyright (C) 2016-2017 Benno Schulenberg                            *
Packit 1ac44c
 *                                                                        *
Packit 1ac44c
 *   GNU nano is free software: you can redistribute it and/or modify     *
Packit 1ac44c
 *   it under the terms of the GNU General Public License as published    *
Packit 1ac44c
 *   by the Free Software Foundation, either version 3 of the License,    *
Packit 1ac44c
 *   or (at your option) any later version.                               *
Packit 1ac44c
 *                                                                        *
Packit 1ac44c
 *   GNU nano is distributed in the hope that it will be useful,          *
Packit 1ac44c
 *   but WITHOUT ANY WARRANTY; without even the implied warranty          *
Packit 1ac44c
 *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              *
Packit 1ac44c
 *   See the GNU General Public License for more details.                 *
Packit 1ac44c
 *                                                                        *
Packit 1ac44c
 *   You should have received a copy of the GNU General Public License    *
Packit 1ac44c
 *   along with this program.  If not, see http://www.gnu.org/licenses/.  *
Packit 1ac44c
 *                                                                        *
Packit 1ac44c
 **************************************************************************/
Packit 1ac44c
Packit 1ac44c
#include "proto.h"
Packit 1ac44c
Packit 1ac44c
#include <errno.h>
Packit 1ac44c
#ifdef HAVE_PWD_H
Packit 1ac44c
#include <pwd.h>
Packit 1ac44c
#endif
Packit 1ac44c
#include <string.h>
Packit 1ac44c
#include <unistd.h>
Packit 1ac44c
Packit 1ac44c
/* Return the user's home directory.  We use $HOME, and if that fails,
Packit 1ac44c
 * we fall back on the home directory of the effective user ID. */
Packit 1ac44c
void get_homedir(void)
Packit 1ac44c
{
Packit 1ac44c
	if (homedir == NULL) {
Packit 1ac44c
		const char *homenv = getenv("HOME");
Packit 1ac44c
Packit 1ac44c
#ifdef HAVE_PWD_H
Packit 1ac44c
		/* When HOME isn't set, or when we're root, get the home directory
Packit 1ac44c
		 * from the password file instead. */
Packit 1ac44c
		if (homenv == NULL || geteuid() == 0) {
Packit 1ac44c
			const struct passwd *userage = getpwuid(geteuid());
Packit 1ac44c
Packit 1ac44c
			if (userage != NULL)
Packit 1ac44c
				homenv = userage->pw_dir;
Packit 1ac44c
		}
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
		/* Only set homedir if some home directory could be determined,
Packit 1ac44c
		 * otherwise keep homedir NULL. */
Packit 1ac44c
		if (homenv != NULL && *homenv != '\0')
Packit 1ac44c
			homedir = mallocstrcpy(NULL, homenv);
Packit 1ac44c
	}
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Return the filename part of the given path. */
Packit 1ac44c
const char *tail(const char *path)
Packit 1ac44c
{
Packit 1ac44c
	const char *slash = strrchr(path, '/');
Packit 1ac44c
Packit 1ac44c
	if (slash == NULL)
Packit 1ac44c
		return path;
Packit 1ac44c
	else
Packit 1ac44c
		return ++slash;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Return a copy of the two given strings, welded together. */
Packit 1ac44c
char *concatenate(const char *path, const char *name)
Packit 1ac44c
{
Packit 1ac44c
	size_t pathlen = strlen(path);
Packit 1ac44c
	char *joined = charalloc(pathlen + strlen(name) + 1);
Packit 1ac44c
Packit 1ac44c
	strcpy(joined, path);
Packit 1ac44c
	strcpy(joined + pathlen, name);
Packit 1ac44c
Packit 1ac44c
	return joined;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
#ifdef ENABLE_LINENUMBERS
Packit 1ac44c
/* Return the number of digits that the given integer n takes up. */
Packit 1ac44c
int digits(ssize_t n)
Packit 1ac44c
{
Packit 1ac44c
	if (n < 100000) {
Packit 1ac44c
		if (n < 1000) {
Packit 1ac44c
			if (n < 100)
Packit 1ac44c
				return 2;
Packit 1ac44c
			else
Packit 1ac44c
				return 3;
Packit 1ac44c
		} else {
Packit 1ac44c
			if (n < 10000)
Packit 1ac44c
				return 4;
Packit 1ac44c
			else
Packit 1ac44c
				return 5;
Packit 1ac44c
		}
Packit 1ac44c
	} else {
Packit 1ac44c
		if (n < 10000000) {
Packit 1ac44c
			if (n < 1000000)
Packit 1ac44c
				return 6;
Packit 1ac44c
			else
Packit 1ac44c
				return 7;
Packit 1ac44c
		} else {
Packit 1ac44c
			if (n < 100000000)
Packit 1ac44c
				return 8;
Packit 1ac44c
			else
Packit 1ac44c
				return 9;
Packit 1ac44c
		}
Packit 1ac44c
	}
Packit 1ac44c
}
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* Read an integer from str.  If it parses okay, store it in *result
Packit 1ac44c
 * and return TRUE; otherwise, return FALSE. */
Packit 1ac44c
bool parse_num(const char *str, ssize_t *result)
Packit 1ac44c
{
Packit 1ac44c
	char *first_error;
Packit 1ac44c
	ssize_t value;
Packit 1ac44c
Packit 1ac44c
	/* The manual page for strtol() says this is required. */
Packit 1ac44c
	errno = 0;
Packit 1ac44c
Packit 1ac44c
	value = (ssize_t)strtol(str, &first_error, 10);
Packit 1ac44c
Packit 1ac44c
	if (errno == ERANGE || *str == '\0' || *first_error != '\0')
Packit 1ac44c
		return FALSE;
Packit 1ac44c
Packit 1ac44c
	*result = value;
Packit 1ac44c
Packit 1ac44c
	return TRUE;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Read two numbers, separated by a comma, from str, and store them in
Packit 1ac44c
 * *line and *column.  Return FALSE on error, and TRUE otherwise. */
Packit 1ac44c
bool parse_line_column(const char *str, ssize_t *line, ssize_t *column)
Packit 1ac44c
{
Packit 1ac44c
	bool retval;
Packit 1ac44c
	char *firstpart;
Packit 1ac44c
	const char *comma;
Packit 1ac44c
Packit 1ac44c
	while (*str == ' ')
Packit 1ac44c
		str++;
Packit 1ac44c
Packit 1ac44c
	comma = strpbrk(str, "m,. /;");
Packit 1ac44c
Packit 1ac44c
	if (comma == NULL)
Packit 1ac44c
		return parse_num(str, line);
Packit 1ac44c
Packit 1ac44c
	retval = parse_num(comma + 1, column);
Packit 1ac44c
Packit 1ac44c
	if (comma == str)
Packit 1ac44c
		return retval;
Packit 1ac44c
Packit 1ac44c
	firstpart = mallocstrcpy(NULL, str);
Packit 1ac44c
	firstpart[comma - str] = '\0';
Packit 1ac44c
Packit 1ac44c
	retval = parse_num(firstpart, line) && retval;
Packit 1ac44c
Packit 1ac44c
	free(firstpart);
Packit 1ac44c
Packit 1ac44c
	return retval;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Reduce the memory allocation of a string to what is needed. */
Packit 1ac44c
void snuggly_fit(char **str)
Packit 1ac44c
{
Packit 1ac44c
	if (*str != NULL)
Packit 1ac44c
		*str = charealloc(*str, strlen(*str) + 1);
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Null a string at a certain index and align it. */
Packit 1ac44c
void null_at(char **data, size_t index)
Packit 1ac44c
{
Packit 1ac44c
	*data = charealloc(*data, index + 1);
Packit 1ac44c
	(*data)[index] = '\0';
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* For non-null-terminated lines.  A line, by definition, shouldn't
Packit 1ac44c
 * normally have newlines in it, so encode its nulls as newlines. */
Packit 1ac44c
void unsunder(char *str, size_t true_len)
Packit 1ac44c
{
Packit 1ac44c
	for (; true_len > 0; true_len--, str++) {
Packit 1ac44c
		if (*str == '\0')
Packit 1ac44c
			*str = '\n';
Packit 1ac44c
	}
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* For non-null-terminated lines.  A line, by definition, shouldn't
Packit 1ac44c
 * normally have newlines in it, so decode its newlines as nulls. */
Packit 1ac44c
void sunder(char *str)
Packit 1ac44c
{
Packit 1ac44c
	for (; *str != '\0'; str++) {
Packit 1ac44c
		if (*str == '\n')
Packit 1ac44c
			*str = '\0';
Packit 1ac44c
	}
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
#if !defined(ENABLE_TINY) || defined(ENABLE_TABCOMP) || defined(ENABLE_BROWSER)
Packit 1ac44c
/* Free the memory of the given array, which should contain len elements. */
Packit 1ac44c
void free_chararray(char **array, size_t len)
Packit 1ac44c
{
Packit 1ac44c
	if (array == NULL)
Packit 1ac44c
		return;
Packit 1ac44c
Packit 1ac44c
	while (len > 0)
Packit 1ac44c
		free(array[--len]);
Packit 1ac44c
Packit 1ac44c
	free(array);
Packit 1ac44c
}
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* Fix the regex if we're on platforms which require an adjustment
Packit 1ac44c
 * from GNU-style to BSD-style word boundaries. */
Packit 1ac44c
const char *fixbounds(const char *r)
Packit 1ac44c
{
Packit 1ac44c
#ifndef GNU_WORDBOUNDS
Packit 1ac44c
	int i, j = 0;
Packit 1ac44c
	char *r2 = charalloc(strlen(r) * 5);
Packit 1ac44c
	char *r3;
Packit 1ac44c
Packit 1ac44c
	for (i = 0; i < strlen(r); i++) {
Packit 1ac44c
		if (r[i] != '\0' && r[i] == '\\' && (r[i + 1] == '>' || r[i + 1] == '<')) {
Packit 1ac44c
			strcpy(&r2[j], "[[:");
Packit 1ac44c
			r2[j + 3] = r[i + 1];
Packit 1ac44c
			strcpy(&r2[j + 4], ":]]");
Packit 1ac44c
			i++;
Packit 1ac44c
			j += 6;
Packit 1ac44c
		} else
Packit 1ac44c
			r2[j] = r[i];
Packit 1ac44c
		j++;
Packit 1ac44c
	}
Packit 1ac44c
Packit 1ac44c
	r2[j] = '\0';
Packit 1ac44c
	r3 = mallocstrcpy(NULL, r2);
Packit 1ac44c
	free(r2);
Packit 1ac44c
Packit 1ac44c
	return (const char *) r3;
Packit 1ac44c
#endif /* !GNU_WORDBOUNDS */
Packit 1ac44c
Packit 1ac44c
	return r;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
#ifdef ENABLE_SPELLER
Packit 1ac44c
/* Is the word starting at the given position in buf and of the given length
Packit 1ac44c
 * a separate word?  That is: is it not part of a longer word?*/
Packit 1ac44c
bool is_separate_word(size_t position, size_t length, const char *buf)
Packit 1ac44c
{
Packit 1ac44c
	char before[MAXCHARLEN], after[MAXCHARLEN];
Packit 1ac44c
	size_t word_end = position + length;
Packit 1ac44c
Packit 1ac44c
	/* Get the characters before and after the word, if any. */
Packit 1ac44c
	parse_mbchar(buf + move_mbleft(buf, position), before, NULL);
Packit 1ac44c
	parse_mbchar(buf + word_end, after, NULL);
Packit 1ac44c
Packit 1ac44c
	/* If the word starts at the beginning of the line OR the character before
Packit 1ac44c
	 * the word isn't a letter, and if the word ends at the end of the line OR
Packit 1ac44c
	 * the character after the word isn't a letter, we have a whole word. */
Packit 1ac44c
	return ((position == 0 || !is_alpha_mbchar(before)) &&
Packit 1ac44c
				(buf[word_end] == '\0' || !is_alpha_mbchar(after)));
Packit 1ac44c
}
Packit 1ac44c
#endif /* ENABLE_SPELLER */
Packit 1ac44c
Packit 1ac44c
/* Return the position of the needle in the haystack, or NULL if not found.
Packit 1ac44c
 * When searching backwards, we will find the last match that starts no later
Packit 1ac44c
 * than the given start; otherwise, we find the first match starting no earlier
Packit 1ac44c
 * than start.  If we are doing a regexp search, and we find a match, we fill
Packit 1ac44c
 * in the global variable regmatches with at most 9 subexpression matches. */
Packit 1ac44c
const char *strstrwrapper(const char *haystack, const char *needle,
Packit 1ac44c
		const char *start)
Packit 1ac44c
{
Packit 1ac44c
	if (*needle == '\0') {
Packit 1ac44c
#ifndef NANO_TINY
Packit 1ac44c
		statusline(ALERT, "Searching for nothing -- please report a bug");
Packit 1ac44c
#endif
Packit 1ac44c
		return (char *)start;
Packit 1ac44c
	}
Packit 1ac44c
Packit 1ac44c
	if (ISSET(USE_REGEXP)) {
Packit 1ac44c
		if (ISSET(BACKWARDS_SEARCH)) {
Packit 1ac44c
			size_t last_find, ceiling, far_end;
Packit 1ac44c
			size_t floor = 0, next_rung = 0;
Packit 1ac44c
				/* The start of the search range, and the next start. */
Packit 1ac44c
Packit 1ac44c
			if (regexec(&search_regexp, haystack, 1, regmatches, 0) != 0)
Packit 1ac44c
				return NULL;
Packit 1ac44c
Packit 1ac44c
			far_end = strlen(haystack);
Packit 1ac44c
			ceiling = start - haystack;
Packit 1ac44c
			last_find = regmatches[0].rm_so;
Packit 1ac44c
Packit 1ac44c
			/* A result beyond the search range also means: no match. */
Packit 1ac44c
			if (last_find > ceiling)
Packit 1ac44c
				return NULL;
Packit 1ac44c
Packit 1ac44c
			/* Move the start-of-range forward until there is no more match;
Packit 1ac44c
			 * then the last match found is the first match backwards. */
Packit 1ac44c
			while (regmatches[0].rm_so <= ceiling) {
Packit 1ac44c
				floor = next_rung;
Packit 1ac44c
				last_find = regmatches[0].rm_so;
Packit 1ac44c
				/* If this is the last possible match, don't try to advance. */
Packit 1ac44c
				if (last_find == ceiling)
Packit 1ac44c
					break;
Packit 1ac44c
				next_rung = move_mbright(haystack, last_find);
Packit 1ac44c
				regmatches[0].rm_so = next_rung;
Packit 1ac44c
				regmatches[0].rm_eo = far_end;
Packit 1ac44c
				if (regexec(&search_regexp, haystack, 1, regmatches,
Packit 1ac44c
										REG_STARTEND) != 0)
Packit 1ac44c
					break;
Packit 1ac44c
			}
Packit 1ac44c
Packit 1ac44c
			/* Find the last match again, to get possible submatches. */
Packit 1ac44c
			regmatches[0].rm_so = floor;
Packit 1ac44c
			regmatches[0].rm_eo = far_end;
Packit 1ac44c
			if (regexec(&search_regexp, haystack, 10, regmatches,
Packit 1ac44c
										REG_STARTEND) != 0)
Packit 1ac44c
				return NULL;
Packit 1ac44c
Packit 1ac44c
			return haystack + regmatches[0].rm_so;
Packit 1ac44c
		}
Packit 1ac44c
Packit 1ac44c
		/* Do a forward regex search from the starting point. */
Packit 1ac44c
		regmatches[0].rm_so = start - haystack;
Packit 1ac44c
		regmatches[0].rm_eo = strlen(haystack);
Packit 1ac44c
		if (regexec(&search_regexp, haystack, 10, regmatches,
Packit 1ac44c
										REG_STARTEND) != 0)
Packit 1ac44c
			return NULL;
Packit 1ac44c
		else
Packit 1ac44c
			return haystack + regmatches[0].rm_so;
Packit 1ac44c
	}
Packit 1ac44c
Packit 1ac44c
	if (ISSET(CASE_SENSITIVE)) {
Packit 1ac44c
		if (ISSET(BACKWARDS_SEARCH))
Packit 1ac44c
			return revstrstr(haystack, needle, start);
Packit 1ac44c
		else
Packit 1ac44c
			return strstr(start, needle);
Packit 1ac44c
	}
Packit 1ac44c
Packit 1ac44c
	if (ISSET(BACKWARDS_SEARCH))
Packit 1ac44c
		return mbrevstrcasestr(haystack, needle, start);
Packit 1ac44c
	else
Packit 1ac44c
		return mbstrcasestr(start, needle);
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* This is a wrapper for the perror() function.  The wrapper temporarily
Packit 1ac44c
 * leaves curses mode, calls perror() (which writes to stderr), and then
Packit 1ac44c
 * reenters curses mode, updating the screen in the process.  Note that
Packit 1ac44c
 * nperror() causes the window to flicker once. */
Packit 1ac44c
void nperror(const char *s)
Packit 1ac44c
{
Packit 1ac44c
	endwin();
Packit 1ac44c
	perror(s);
Packit 1ac44c
	doupdate();
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* This is a wrapper for the malloc() function that properly handles
Packit 1ac44c
 * things when we run out of memory. */
Packit 1ac44c
void *nmalloc(size_t howmuch)
Packit 1ac44c
{
Packit 1ac44c
	void *r = malloc(howmuch);
Packit 1ac44c
Packit 1ac44c
	if (r == NULL && howmuch != 0)
Packit 1ac44c
		die(_("nano is out of memory!"));
Packit 1ac44c
Packit 1ac44c
	return r;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* This is a wrapper for the realloc() function that properly handles
Packit 1ac44c
 * things when we run out of memory. */
Packit 1ac44c
void *nrealloc(void *ptr, size_t howmuch)
Packit 1ac44c
{
Packit 1ac44c
	void *r = realloc(ptr, howmuch);
Packit 1ac44c
Packit 1ac44c
	if (r == NULL && howmuch != 0)
Packit 1ac44c
		die(_("nano is out of memory!"));
Packit 1ac44c
Packit 1ac44c
	return r;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Allocate and copy the first n characters of the given src string, after
Packit 1ac44c
 * freeing the destination.  Usage: "dest = mallocstrncpy(dest, src, n);". */
Packit 1ac44c
char *mallocstrncpy(char *dest, const char *src, size_t n)
Packit 1ac44c
{
Packit 1ac44c
	if (src == NULL)
Packit 1ac44c
		src = "";
Packit 1ac44c
Packit 1ac44c
#ifndef NANO_TINY
Packit 1ac44c
	if (src == dest)
Packit 1ac44c
		fprintf(stderr, "\r*** Copying a string to itself -- please report a bug ***");
Packit 1ac44c
#endif
Packit 1ac44c
	dest = charealloc(dest, n);
Packit 1ac44c
	strncpy(dest, src, n);
Packit 1ac44c
Packit 1ac44c
	return dest;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Free the dest string and return a malloc'ed copy of src.  Should be used as:
Packit 1ac44c
 * "dest = mallocstrcpy(dest, src);". */
Packit 1ac44c
char *mallocstrcpy(char *dest, const char *src)
Packit 1ac44c
{
Packit 1ac44c
	return mallocstrncpy(dest, src, (src == NULL) ? 1 : strlen(src) + 1);
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Free the string at dest and return the string at src. */
Packit 1ac44c
char *free_and_assign(char *dest, char *src)
Packit 1ac44c
{
Packit 1ac44c
	free(dest);
Packit 1ac44c
	return src;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* When not in softwrap mode, nano scrolls horizontally within a line in
Packit 1ac44c
 * chunks (a bit smaller than the chunks used in softwrapping).  Return the
Packit 1ac44c
 * column number of the first character displayed in the edit window when the
Packit 1ac44c
 * cursor is at the given column.  Note that (0 <= column -
Packit 1ac44c
 * get_page_start(column) < COLS). */
Packit 1ac44c
size_t get_page_start(size_t column)
Packit 1ac44c
{
Packit 1ac44c
	if (column < editwincols - 1 || ISSET(SOFTWRAP) || column == 0)
Packit 1ac44c
		return 0;
Packit 1ac44c
	else if (editwincols > 8)
Packit 1ac44c
		return column - 7 - (column - 7) % (editwincols - 8);
Packit 1ac44c
	else
Packit 1ac44c
		return column - (editwincols - 2);
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Return the placewewant associated with current_x, i.e. the zero-based
Packit 1ac44c
 * column position of the cursor. */
Packit 1ac44c
size_t xplustabs(void)
Packit 1ac44c
{
Packit 1ac44c
	return strnlenpt(openfile->current->data, openfile->current_x);
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Return the index in text of the character that (when displayed) will
Packit 1ac44c
 * not overshoot the given column. */
Packit 1ac44c
size_t actual_x(const char *text, size_t column)
Packit 1ac44c
{
Packit 1ac44c
	const char *start = text;
Packit 1ac44c
		/* From where we start walking through the text. */
Packit 1ac44c
	size_t width = 0;
Packit 1ac44c
		/* The current accumulated span, in columns. */
Packit 1ac44c
Packit 1ac44c
	while (*text != '\0') {
Packit 1ac44c
		int charlen = parse_mbchar(text, NULL, &width);
Packit 1ac44c
Packit 1ac44c
		if (width > column)
Packit 1ac44c
			break;
Packit 1ac44c
Packit 1ac44c
		text += charlen;
Packit 1ac44c
	}
Packit 1ac44c
Packit 1ac44c
	return (text - start);
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* A strnlen() with tabs and multicolumn characters factored in:
Packit 1ac44c
 * how many columns wide are the first maxlen bytes of text? */
Packit 1ac44c
size_t strnlenpt(const char *text, size_t maxlen)
Packit 1ac44c
{
Packit 1ac44c
	size_t width = 0;
Packit 1ac44c
		/* The screen display width to text[maxlen]. */
Packit 1ac44c
Packit 1ac44c
	if (maxlen == 0)
Packit 1ac44c
		return 0;
Packit 1ac44c
Packit 1ac44c
	while (*text != '\0') {
Packit 1ac44c
		size_t charlen = parse_mbchar(text, NULL, &width);
Packit 1ac44c
Packit 1ac44c
		if (maxlen <= charlen)
Packit 1ac44c
			break;
Packit 1ac44c
Packit 1ac44c
		maxlen -= charlen;
Packit 1ac44c
		text += charlen;
Packit 1ac44c
	}
Packit 1ac44c
Packit 1ac44c
	return width;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Return the number of columns that the given text occupies. */
Packit 1ac44c
size_t strlenpt(const char *text)
Packit 1ac44c
{
Packit 1ac44c
	size_t span = 0;
Packit 1ac44c
Packit 1ac44c
	while (*text != '\0')
Packit 1ac44c
		text += parse_mbchar(text, NULL, &span;;
Packit 1ac44c
Packit 1ac44c
	return span;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Append a new magicline to the end of the buffer. */
Packit 1ac44c
void new_magicline(void)
Packit 1ac44c
{
Packit 1ac44c
	openfile->filebot->next = make_new_node(openfile->filebot);
Packit 1ac44c
	openfile->filebot->next->data = mallocstrcpy(NULL, "");
Packit 1ac44c
	openfile->filebot = openfile->filebot->next;
Packit 1ac44c
	openfile->totsize++;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
#if !defined(NANO_TINY) || defined(ENABLE_HELP)
Packit 1ac44c
/* Remove the magicline from the end of the buffer, if there is one and
Packit 1ac44c
 * it isn't the only line in the file. */
Packit 1ac44c
void remove_magicline(void)
Packit 1ac44c
{
Packit 1ac44c
	if (openfile->filebot->data[0] == '\0' &&
Packit 1ac44c
				openfile->filebot != openfile->fileage) {
Packit 1ac44c
		openfile->filebot = openfile->filebot->prev;
Packit 1ac44c
		free_filestruct(openfile->filebot->next);
Packit 1ac44c
		openfile->filebot->next = NULL;
Packit 1ac44c
		openfile->totsize--;
Packit 1ac44c
	}
Packit 1ac44c
}
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
#ifndef NANO_TINY
Packit 1ac44c
/* Set (top, top_x) and (bot, bot_x) to the start and end "coordinates" of
Packit 1ac44c
 * the marked region.  If right_side_up isn't NULL, set it to TRUE when the
Packit 1ac44c
 * mark is at the top of the marked region, and to FALSE otherwise. */
Packit 1ac44c
void mark_order(const filestruct **top, size_t *top_x,
Packit 1ac44c
		const filestruct **bot, size_t *bot_x, bool *right_side_up)
Packit 1ac44c
{
Packit 1ac44c
	if ((openfile->current->lineno == openfile->mark->lineno &&
Packit 1ac44c
				openfile->current_x > openfile->mark_x) ||
Packit 1ac44c
				openfile->current->lineno > openfile->mark->lineno) {
Packit 1ac44c
		*top = openfile->mark;
Packit 1ac44c
		*top_x = openfile->mark_x;
Packit 1ac44c
		*bot = openfile->current;
Packit 1ac44c
		*bot_x = openfile->current_x;
Packit 1ac44c
		if (right_side_up != NULL)
Packit 1ac44c
			*right_side_up = TRUE;
Packit 1ac44c
	} else {
Packit 1ac44c
		*bot = openfile->mark;
Packit 1ac44c
		*bot_x = openfile->mark_x;
Packit 1ac44c
		*top = openfile->current;
Packit 1ac44c
		*top_x = openfile->current_x;
Packit 1ac44c
		if (right_side_up != NULL)
Packit 1ac44c
			*right_side_up = FALSE;
Packit 1ac44c
	}
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Get the set of lines to work on -- either just the current line, or the
Packit 1ac44c
 * first to last lines of the marked region.  When the cursor (or mark) is
Packit 1ac44c
 * at the start of the last line of the region, exclude that line. */
Packit 1ac44c
void get_range(const filestruct **top, const filestruct **bot)
Packit 1ac44c
{
Packit 1ac44c
	if (!openfile->mark) {
Packit 1ac44c
		*top = openfile->current;
Packit 1ac44c
		*bot = openfile->current;
Packit 1ac44c
	} else {
Packit 1ac44c
		size_t top_x, bot_x;
Packit 1ac44c
Packit 1ac44c
		mark_order(top, &top_x, bot, &bot_x, NULL);
Packit 1ac44c
Packit 1ac44c
		if (bot_x == 0 && *bot != *top && !also_the_last)
Packit 1ac44c
			*bot = (*bot)->prev;
Packit 1ac44c
		else
Packit 1ac44c
			also_the_last = TRUE;
Packit 1ac44c
	}
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Given a line number, return a pointer to the corresponding struct. */
Packit 1ac44c
filestruct *fsfromline(ssize_t lineno)
Packit 1ac44c
{
Packit 1ac44c
	filestruct *f = openfile->current;
Packit 1ac44c
Packit 1ac44c
	if (lineno <= openfile->current->lineno)
Packit 1ac44c
		while (f->lineno != lineno && f->prev != NULL)
Packit 1ac44c
			f = f->prev;
Packit 1ac44c
	else
Packit 1ac44c
		while (f->lineno != lineno && f->next != NULL)
Packit 1ac44c
			f = f->next;
Packit 1ac44c
Packit 1ac44c
	if (f->lineno != lineno) {
Packit 1ac44c
		statusline(ALERT, "Gone undo line -- please report a bug");
Packit 1ac44c
		return NULL;
Packit 1ac44c
	}
Packit 1ac44c
Packit 1ac44c
	return f;
Packit 1ac44c
}
Packit 1ac44c
#endif /* !NANO_TINY */
Packit 1ac44c
Packit 1ac44c
/* Count the number of characters from begin to end, and return it. */
Packit 1ac44c
size_t get_totsize(const filestruct *begin, const filestruct *end)
Packit 1ac44c
{
Packit 1ac44c
	const filestruct *line;
Packit 1ac44c
	size_t totsize = 0;
Packit 1ac44c
Packit 1ac44c
	/* Sum the number of characters (plus a newline) in each line. */
Packit 1ac44c
	for (line = begin; line != end->next; line = line->next)
Packit 1ac44c
		totsize += mbstrlen(line->data) + 1;
Packit 1ac44c
Packit 1ac44c
	/* The last line of a file doesn't have a newline -- otherwise it
Packit 1ac44c
	 * wouldn't be the last line -- so subtract 1 when at EOF. */
Packit 1ac44c
	if (line == NULL)
Packit 1ac44c
		totsize--;
Packit 1ac44c
Packit 1ac44c
	return totsize;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
#ifdef DEBUG
Packit 1ac44c
/* Dump the given buffer to stderr. */
Packit 1ac44c
void dump_filestruct(const filestruct *inptr)
Packit 1ac44c
{
Packit 1ac44c
	if (inptr == openfile->fileage)
Packit 1ac44c
		fprintf(stderr, "Dumping file buffer to stderr...\n");
Packit 1ac44c
	else if (inptr == cutbuffer)
Packit 1ac44c
		fprintf(stderr, "Dumping cutbuffer to stderr...\n");
Packit 1ac44c
	else
Packit 1ac44c
		fprintf(stderr, "Dumping a buffer to stderr...\n");
Packit 1ac44c
Packit 1ac44c
	while (inptr != NULL) {
Packit 1ac44c
		fprintf(stderr, "(%zd) %s\n", inptr->lineno, inptr->data);
Packit 1ac44c
		inptr = inptr->next;
Packit 1ac44c
	}
Packit 1ac44c
}
Packit 1ac44c
#endif /* DEBUG */