Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

common.c File Reference

Various functions, mostly string utilities, that are used by most parts of fish. More...

#include "config.h"
#include <unistd.h>
#include <stropts.h>
#include <stdlib.h>
#include <termios.h>
#include <wchar.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/termios.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <wctype.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <locale.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <execinfo.h>
#include <ncurses.h>
#include <termio.h>
#include <term.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "common.h"
#include "expand.h"
#include "proc.h"
#include "wildcard.h"
#include "parser.h"
#include "util.c"
#include "halloc.c"
#include "halloc_util.c"
#include "fallback.c"

Defines

#define HOST_NAME_MAX   255
 Maximum length of hostname return.
#define LOCKPOLLINTERVAL   10
 The number of milliseconds to wait between polls when attempting to acquire a lockfile.

Functions

void show_stackframe ()
wchar_t ** list_to_char_arr (array_list_t *l)
 Take an array_list_t containing wide strings and converts them to a single null-terminated wchar_t **.
int fgetws2 (wchar_t **b, int *len, FILE *f)
 Read a line from the stream f into the buffer buff of length len.
static int str_cmp (const void *a, const void *b)
 Wrapper for wcsfilecmp.
void sort_list (array_list_t *comp)
 Sorts an array_list of wide strings according to the wcsfilecmp-function from the util library.
wchar_t * str2wcs (const char *in)
 Returns a newly allocated wide character string equivalent of the specified multibyte character string.
wchar_t * str2wcs_internal (const char *in, wchar_t *out)
 Converts the narrow character string in into it's wide equivalent, stored in out.
char * wcs2str (const wchar_t *in)
 Returns a newly allocated multibyte character string equivalent of the specified wide character string.
char * wcs2str_internal (const wchar_t *in, char *out)
 Converts the wide character string in into it's narrow equivalent, stored in out.
char ** wcsv2strv (const wchar_t **in)
 Returns a newly allocated wide character string array equivalent of the specified multibyte character string array.
wchar_t * wcsdupcat_internal (const wchar_t *a,...)
 Returns a newly allocated concatenation of the specified wide character strings.
wchar_t ** strv2wcsv (const char **in)
 Returns a newly allocated multibyte character string array equivalent of the specified wide character string array.
wchar_t * wcsvarname (const wchar_t *str)
 Test if the given string is a valid variable name.
wchar_t * wcsfuncname (const wchar_t *str)
 Test if the given string is a valid function name.
int wcsvarchr (wchar_t chr)
 Test if the given string is valid in a variable name.
int my_wcswidth (const wchar_t *c)
 A wcswidth workalike.
wchar_t * quote_end (const wchar_t *pos)
 This functions returns the end of the quoted substring beginning at in.
const wchar_t * wsetlocale (int category, const wchar_t *locale)
 This function behaves exactly like a wide character equivalent of the C function setlocale, except that it will also try to detect if the user is using a Unicode character set, and if so, use the unicode ellipsis character as ellipsis, instead of '$'.
int contains_internal (const wchar_t *a,...)
 Checks if needle is included in the list of strings specified.
int read_blocked (int fd, void *buf, size_t count)
 Call read while blocking the SIGCHLD signal.
void debug (int level, const wchar_t *msg,...)
 Issue a debug message with printf-style string formating and automatic line breaking.
void write_screen (const wchar_t *msg, string_buffer_t *buff)
 Write paragraph of output to the specified stringbuffer, and redo the linebreaks to fit the current screen.
static wchar_t * escape_simple (const wchar_t *in)
wchar_t * escape (const wchar_t *in_orig, int flags)
 Replace special characters with backslash escape sequences.
wchar_t * unescape (const wchar_t *orig, int flags)
 Expand backslashed escapes and substitute them with their unescaped counterparts.
static int sprint_pid_t (pid_t pid, char *str)
 Writes a pid_t in decimal representation to str.
static int sprint_rand_digits (char *str, int maxlen)
 Writes a pseudo-random number (between one and maxlen) of pseudo-random digits into str.
static char * gen_unique_nfs_filename (const char *filename)
 Generate a filename unique in an NFS namespace by creating a copy of str and appending .
int acquire_lock_file (const char *lockfile, const int timeout, int force)
 Attempt to acquire a lock based on a lockfile, waiting LOCKPOLLINTERVAL milliseconds between polls and timing out after timeout seconds, thereafter forcibly attempting to obtain the lock if force is non-zero.
void common_handle_winch (int signal)
 Handle a window change event by looking up the new window size and saving it in an internal variable used by common_get_wisth and common_get_height().
int common_get_width ()
 Returns the width of the terminal window, so that not all functions that use these values continually have to keep track of it separately.
int common_get_height ()
 Returns the height of the terminal window, so that not all functions that use these values continually have to keep track of it separatly.
void tokenize_variable_array (const wchar_t *val, array_list_t *out)
 Tokenize the specified string into the specified array_list_t.
int create_directory (wchar_t *d)
 Make sure the specified direcotry exists.
void bugreport ()
 Print a short message about how to file a bug report to stderr.
void sb_format_size (string_buffer_t *sb, long long sz)
 Format the specified size (in bytes, kilobytes, etc.

Variables

termios shell_modes
 Save the shell mode on startup so we can restore them on exit.
wchar_t ellipsis_char
 The character to use where the text has been truncated.
char * profile = 0
 Profiling flag.
wchar_t * program_name
 Name of the current program.
int debug_level = 1
 The verbosity level of fish.
static struct winsize termsize
 This struct should be continually updated by signals as the term resizes, and as such always contain the correct current size.
static string_buffer_tsetlocale_buff = 0
 String buffer used by the wsetlocale function.


Detailed Description

Various functions, mostly string utilities, that are used by most parts of fish.


Define Documentation

#define HOST_NAME_MAX   255
 

Maximum length of hostname return.

It is ok if this is too short, getting the actual hostname is not critical, so long as the string is unique in the filesystem namespace.


Function Documentation

int acquire_lock_file const char *  lockfile,
const int  timeout,
int  force
 

Attempt to acquire a lock based on a lockfile, waiting LOCKPOLLINTERVAL milliseconds between polls and timing out after timeout seconds, thereafter forcibly attempting to obtain the lock if force is non-zero.

Returns 1 on success, 0 on failure. To release the lock the lockfile must be unlinked. A unique temporary file named by appending characters to the lockfile name is used; any pre-existing file of the same name is subject to deletion.

int common_get_height  ) 
 

Returns the height of the terminal window, so that not all functions that use these values continually have to keep track of it separatly.

Only works if common_handle_winch is registered to handle winch signals.

int common_get_width  ) 
 

Returns the width of the terminal window, so that not all functions that use these values continually have to keep track of it separately.

Only works if common_handle_winch is registered to handle winch signals.

int contains_internal const wchar_t *  needle,
  ...
 

Checks if needle is included in the list of strings specified.

A warning is printed if needle is zero.

Parameters:
needle the string to search for in the list
Returns:
zero if needle is not found, of if needle is null, non-zero otherwise

int create_directory wchar_t *  d  ) 
 

Make sure the specified direcotry exists.

If needed, try to create it and any currently not existing parent directories..

Returns:
0 if, at the time of function return the directory exists, -1 otherwise.

void debug int  level,
const wchar_t *  msg,
  ...
 

Issue a debug message with printf-style string formating and automatic line breaking.

The string will begin with the string program_name, followed by a colon and a whitespace.

Because debug is often called to tell the user about an error, before using wperror to give a specific error message, debug will never ever modify the value of errno.

Parameters:
level the priority of the message. Lower number means higher priority. Messages with a priority_number higher than debug_level will be ignored..
msg the message format string.
Example:

debug( 1, L"Pi = %.3f", M_PI );

will print the string 'fish: Pi = 3.141', given that debug_level is 1 or higher, and that program_name is 'fish'.

wchar_t* escape const wchar_t *  in,
int  escape_all
 

Replace special characters with backslash escape sequences.

Newline is replaced with
, etc.

Parameters:
in The string to be escaped
escape_all Whether all characters wich hold special meaning in fish (Pipe, semicolon, etc,) should be escaped, or only unprintable characters
Returns:
The escaped string, or 0 if there is not enough memory

int fgetws2 wchar_t **  buff,
int *  len,
FILE *  f
 

Read a line from the stream f into the buffer buff of length len.

If buff is to small, it will be reallocated, and both buff and len will be updated to reflect this. Returns the number of bytes read or -1 on failiure.

If the carriage return character is encountered, it is ignored. fgetws() considers the line to end if reading the file results in either a newline (L'
') character, the null (L'\0') character or the end of file (WEOF) character.

static char* gen_unique_nfs_filename const char *  filename  )  [static]
 

Generate a filename unique in an NFS namespace by creating a copy of str and appending .

{hostname}.{pid} to it. If gethostname() fails then a pseudo- random string is substituted for {hostname} - the randomness of the string should be strong enough across different machines. The main assumption though is that gethostname will not fail and this is just a "safe enough" fallback. The memory returned should be freed using free().

wchar_t** list_to_char_arr array_list_t l  ) 
 

Take an array_list_t containing wide strings and converts them to a single null-terminated wchar_t **.

The array is allocated using malloc, and needs to be fred's by the caller.

int my_wcswidth const wchar_t *  c  ) 
 

A wcswidth workalike.

fish uses this replacement.

wchar_t* quote_end const wchar_t *  in  ) 
 

This functions returns the end of the quoted substring beginning at in.

The type of quoting character is detemrined by examining in. Returns 0 on error.

Parameters:
in the position of the opening quote

int read_blocked int  fd,
void *  buf,
size_t  count
 

Call read while blocking the SIGCHLD signal.

Should only be called if you _know_ there is data available for reading, or the program will hang until there is data.

void sb_format_size string_buffer_t sb,
long long  sz
 

Format the specified size (in bytes, kilobytes, etc.

) into the specified stringbuffer.

static int sprint_pid_t pid_t  pid,
char *  str
[static]
 

Writes a pid_t in decimal representation to str.

str must contain sufficient space. The conservatively approximate maximum number of characters a pid_t will represent is given by: (int)(0.31 * sizeof(pid_t) + CHAR_BIT + 1) Returns the length of the string

static int sprint_rand_digits char *  str,
int  maxlen
[static]
 

Writes a pseudo-random number (between one and maxlen) of pseudo-random digits into str.

str must point to an allocated buffer of size of at least maxlen chars. Returns the number of digits written. Since the randomness in part depends on machine time it has _some_ extra strength but still not enough for use in concurrent locking schemes on a single machine because gettimeofday may not return a different value on consecutive calls when: a) the OS does not support fine enough resolution b) the OS is running on an SMP machine. Additionally, gettimeofday errors are ignored. Excludes chars other than digits since ANSI C only guarantees that digits are consecutive.

wchar_t* str2wcs const char *  in  ) 
 

Returns a newly allocated wide character string equivalent of the specified multibyte character string.

This function encodes illegal character sequences in a reversible way using the private use area.

wchar_t* str2wcs_internal const char *  in,
wchar_t *  out
 

Converts the narrow character string in into it's wide equivalent, stored in out.

out must have enough space to fit the entire string.

This function encodes illegal character sequences in a reversible way using the private use area.

void tokenize_variable_array const wchar_t *  val,
array_list_t out
 

Tokenize the specified string into the specified array_list_t.

Each new element is allocated using malloc and must be freed by the caller.

Parameters:
val the input string. The contents of this string is not changed.
out the list in which to place the elements.

wchar_t* unescape const wchar_t *  in,
int  escape_special
 

Expand backslashed escapes and substitute them with their unescaped counterparts.

Also optionally change the wildcards, the tilde character and a few more into constants which are defined in a private use area of Unicode. This assumes wchar_t is a unicode character set.

The result must be free()d. The original string is not modified. If an invalid sequence is specified, 0 is returned.

char* wcs2str const wchar_t *  in  ) 
 

Returns a newly allocated multibyte character string equivalent of the specified wide character string.

This function decodes illegal character sequences in a reversible way using the private use area.

char* wcs2str_internal const wchar_t *  in,
char *  out
 

Converts the wide character string in into it's narrow equivalent, stored in out.

out must have enough space to fit the entire string.

This function decodes illegal character sequences in a reversible way using the private use area.

wchar_t* wcsdupcat_internal const wchar_t *  a,
  ...
 

Returns a newly allocated concatenation of the specified wide character strings.

The last argument must be a null pointer.

wchar_t* wcsfuncname const wchar_t *  str  ) 
 

Test if the given string is a valid function name.

Returns:
null if this is a valid name, and a pointer to the first invalid character otherwise

int wcsvarchr wchar_t  chr  ) 
 

Test if the given string is valid in a variable name.

Returns:
1 if this is a valid name, 0 otherwise

wchar_t* wcsvarname const wchar_t *  str  ) 
 

Test if the given string is a valid variable name.

Returns:
null if this is a valid name, and a pointer to the first invalid character otherwise


Variable Documentation

int debug_level = 1
 

The verbosity level of fish.

If a call to debug has a severity level higher than debug_level, it will not be printed.

wchar_t ellipsis_char
 

The character to use where the text has been truncated.

Is an ellipsis on unicode system and a $ on other systems.

char* profile = 0
 

Profiling flag.

True if commands should be profiled.

wchar_t* program_name
 

Name of the current program.

Should be set at startup. Used by the debug function.


Generated on Sun Jan 13 02:52:35 2008 for fish by  doxygen 1.4.4