src/print.c
static int
parse_line(char *line, SpecialWord *words, int maxwords)
{
	int		nwords = 0;
	int		pos = 0;
	bool	first_nonspace = true;

	/*
	 * When text starts on line start and first char is upper
	 * char, then mark this text until double colon.
	 *
	 * like "Usage:"
	 */
	if (is_upper_char(line) || strncmp(line, "psql", 4) == 0)
	{
		char   *aux_line = line;

		words[0].start_pos = 0;
		words[0].typ = 3;

		while (*line != '\0' && *line != ':')
		{
			pos += dsplen(line);
			line += charlen(line);
		}

		if (!is_upper_char(aux_line + 1))
		{
			words[0].end_pos = pos - 1;
			words[0].typ = 3;
			nwords = 1;
			first_nonspace = false;
		}
		else
		{
			line = aux_line;
			pos = 0;
		}
	}

	while (*line)
	{
		while (*line == ' ')
		{
			line += 1;
			pos += 1;
		}

		/* psql's backslash commands */
		if (*line == '\\')
		{
			words[nwords].start_pos = pos;
			words[nwords].typ = 1;

			while (*line != ' ' && *line != '\0')
			{
				pos += dsplen(line);
				line += charlen(line);
			}
		}
		/* psql's shell options */
		else if (*line == '-')
		{
			words[nwords].start_pos = pos;
			words[nwords].typ = 1;

			while (*line == '-')
			{
				line += 1;
				pos += 1;
			}

			if (pos - words[nwords].start_pos > 2)
				continue;

			if (!is_ascii_alnum(*line))
				continue;

			while (is_ascii_alnum(*line) || *line == '-')
			{
				line += 1;
				pos += 1;
			}

			words[nwords].typ = 1;
			goto fin;
		}
		/* psql's variables and \pset variables */
		else if (is_ascii_alpha(*line))
		{
			bool	only_upper = true;
			char   *start = line;

			words[nwords].start_pos = pos;

			while (is_ascii_alnum(*line) || *line == '_')
			{
				if (islower(*line))
					only_upper = false;

				line += 1;
				pos += 1;
			}

			if (!only_upper)
			{
				if (first_nonspace && words[nwords].start_pos == 2 && *line == '\0')
				{
					words[nwords].typ = 2;
					goto fin;
				}
				else if (isupper(*start) && *line == ':')
				{
					words[nwords].typ = 3;
					goto fin;
				}

				continue;
			}

			if (strncmp(start, "SQL", pos - words[nwords].start_pos) == 0)
				continue;

			if (*line == ':')
				continue;

			if ((pos - words[nwords].start_pos) == 1)
				continue;

			words[nwords].typ = 2;
		}
		else
		{
			pos += dsplen(line);
			line += charlen(line);
			first_nonspace = false;
			continue;
		}

fin:

		words[nwords].end_pos = pos - 1;

		if (++nwords == maxwords)
			return nwords;
	}

	return nwords;
}
/*
 * Try to identify words in line that should be highlighted
 *
 */
Try to identify words in line that should be highlighted
Try to identify words in line that should be highlighted
static int
parse_line(char *line, SpecialWord *words, int maxwords)
{
	int		nwords = 0;
	int		pos = 0;
	bool	first_nonspace = true;
	if (is_upper_char(line) || strncmp(line, "psql", 4) == 0)
	{
		char   *aux_line = line;
		words[0].start_pos = 0;
		words[0].typ = 3;
		while (*line != '\0' && *line != ':')
		{
			pos += dsplen(line);
			line += charlen(line);
		}
		if (!is_upper_char(aux_line + 1))
		{
			words[0].end_pos = pos - 1;
			words[0].typ = 3;
			nwords = 1;
			first_nonspace = false;
		}
		else
		{
			line = aux_line;
			pos = 0;
		}
	}
	while (*line)
	{
		while (*line == ' ')
		{
			line += 1;
			pos += 1;
		}
		if (*line == '\\')
		{
			words[nwords].start_pos = pos;
			words[nwords].typ = 1;
			while (*line != ' ' && *line != '\0')
			{
				pos += dsplen(line);
				line += charlen(line);
			}
		}
		else if (*line == '-')
		{
			words[nwords].start_pos = pos;
			words[nwords].typ = 1;
			while (*line == '-')
			{
				line += 1;
				pos += 1;
			}
			if (pos - words[nwords].start_pos > 2)
				continue;
			if (!is_ascii_alnum(*line))
				continue;
			while (is_ascii_alnum(*line) || *line == '-')
			{
				line += 1;
				pos += 1;
			}
			words[nwords].typ = 1;
			goto fin;
		}
		else if (is_ascii_alpha(*line))
		{
			bool	only_upper = true;
			char   *start = line;
			words[nwords].start_pos = pos;
			while (is_ascii_alnum(*line) || *line == '_')
			{
				if (islower(*line))
					only_upper = false;
				line += 1;
				pos += 1;
			}
			if (!only_upper)
			{
				if (first_nonspace && words[nwords].start_pos == 2 && *line == '\0')
				{
					words[nwords].typ = 2;
					goto fin;
				}
				else if (isupper(*start) && *line == ':')
				{
					words[nwords].typ = 3;
					goto fin;
				}
				continue;
			}
			if (strncmp(start, "SQL", pos - words[nwords].start_pos) == 0)
				continue;
			if (*line == ':')
				continue;
			if ((pos - words[nwords].start_pos) == 1)
				continue;
			words[nwords].typ = 2;
		}
		else
		{
			pos += dsplen(line);
			line += charlen(line);
			first_nonspace = false;
			continue;
		}
fin:
		words[nwords].end_pos = pos - 1;
		if (++nwords == maxwords)
			return nwords;
	}
	return nwords;
}
static int parse_line ( char * line , SpecialWord * words , int maxwords ) { int nwords = 0 ; int pos = 0 ; bool first_nonspace = true ; if ( is_upper_char ( line ) || strncmp ( line , " " , 4 ) == 0 ) { char * aux_line = line ; words [ 0 ] . start_pos = 0 ; words [ 0 ] . typ = 3 ; while ( * line != ' \0 ' && * line != ' ' ) { pos += dsplen ( line ) ; line += charlen ( line ) ; } if ( ! is_upper_char ( aux_line + 1 ) ) { words [ 0 ] . end_pos = pos - 1 ; words [ 0 ] . typ = 3 ; nwords = 1 ; first_nonspace = false ; } else { line = aux_line ; pos = 0 ; } } while ( * line ) { while ( * line == ' ' ) { line += 1 ; pos += 1 ; } if ( * line == ' \\ ' ) { words [ nwords ] . start_pos = pos ; words [ nwords ] . typ = 1 ; while ( * line != ' ' && * line != ' \0 ' ) { pos += dsplen ( line ) ; line += charlen ( line ) ; } } else if ( * line == ' ' ) { words [ nwords ] . start_pos = pos ; words [ nwords ] . typ = 1 ; while ( * line == ' ' ) { line += 1 ; pos += 1 ; } if ( pos - words [ nwords ] . start_pos > 2 ) continue ; if ( ! is_ascii_alnum ( * line ) ) continue ; while ( is_ascii_alnum ( * line ) || * line == ' ' ) { line += 1 ; pos += 1 ; } words [ nwords ] . typ = 1 ; goto fin ; } else if ( is_ascii_alpha ( * line ) ) { bool only_upper = true ; char * start = line ; words [ nwords ] . start_pos = pos ; while ( is_ascii_alnum ( * line ) || * line == ' ' ) { if ( islower ( * line ) ) only_upper = false ; line += 1 ; pos += 1 ; } if ( ! only_upper ) { if ( first_nonspace && words [ nwords ] . start_pos == 2 && * line == ' \0 ' ) { words [ nwords ] . typ = 2 ; goto fin ; } else if ( isupper ( * start ) && * line == ' ' ) { words [ nwords ] . typ = 3 ; goto fin ; } continue ; } if ( strncmp ( start , " " , pos - words [ nwords ] . start_pos ) == 0 ) continue ; if ( * line == ' ' ) continue ; if ( ( pos - words [ nwords ] . start_pos ) == 1 ) continue ; words [ nwords ] . typ = 2 ; } else { pos += dsplen ( line ) ; line += charlen ( line ) ; first_nonspace = false ; continue ; } fin : words [ nwords ] . end_pos = pos - 1 ; if ( ++ nwords == maxwords ) return nwords ; } return nwords ; }
Try to identify words in line that should be highlighted
Try to identify words in line that should be highlighted
/*
	 * When text starts on line start and first char is upper
	 * char, then mark this text until double colon.
	 *
	 * like "Usage:"
	 */ /* psql's backslash commands */ /* psql's shell options */ /* psql's variables and \pset variables */