aboutsummaryrefslogtreecommitdiffhomepage
path: root/common.h
blob: af835d6024c42073b079dd867601eb5354382d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/** \file common.h
	Prototypes for various functions, mostly string utilities, that are used by most parts of fish.
*/


/**
   Under curses, tputs expects an int (*func)(char) as its last parameter, but in ncurses, tputs expects a int (*func)(int) as its last parameter. tputs_arg_t is defined to always be what tputs expects. Hopefully.
*/

#ifdef NCURSES_VERSION
typedef int tputs_arg_t;
#else
typedef char tputs_arg_t;
#endif

/**
   Maximum number of bytes in a utf-8 character
*/
#define MAX_UTF8_BYTES 6

/**
   Color code for set_color. Does not update the color.
*/

#define FISH_COLOR_IGNORE -1

/**
   Color code for set_color. Sets the default color.
*/
#define FISH_COLOR_RESET -2

/** Save the shell mode on startup so we can restore them on exit */
extern struct termios shell_modes;      

/**
   The character to use where the text has been truncated. Is an ellipsis on unicode system and a $ on other systems.
*/
extern wchar_t ellipsis_char;

/**
   The maximum number of charset convertion errors to report
*/
extern int error_max;

/**
   The verbosity of fish
*/
extern int debug_level;

/**
   Profiling flag. True if commands should be profiled.
*/
extern char *profile;

/**
   Name of the current program. Should be set at startup. Used by the
   debug function.
*/
extern wchar_t *program_name;
/**
   Take an array_list_t containing wide strings and converts them to a wchar_t **.
*/
wchar_t **list_to_char_arr( array_list_t *l );

/**
   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'\n') character, the null (L'\\0')
   character or the end of file (WEOF) character.
*/
int fgetws2( wchar_t **buff, int *len, FILE *f );

/**
   Sorts a list of wide strings according to the wcsfilecmp-function from the util library
*/
void sort_list( array_list_t *comp );

/**
   Returns a newly allocated wide character string equivalent of the specified multibyte character string
*/
wchar_t *str2wcs( const char *in );

/**
   Returns a newly allocated multibyte character string equivalent of the specified wide character string
*/
char *wcs2str( const wchar_t *in );

/**
   Returns a newly allocated wide character string array equivalent of the specified multibyte character string array
*/
char **wcsv2strv( const wchar_t **in );

/**
   Returns a newly allocated multibyte character string array equivalent of the specified wide character string array
*/
wchar_t **strv2wcsv( const char **in );

/**
   Returns a newly allocated concatenation of the specified wide character strings
*/
wchar_t *wcsdupcat( const wchar_t *a, const wchar_t *b );

/**
   Returns a newly allocated concatenation of the specified wide character strings. The last argument must be a null pointer.
*/
wchar_t *wcsdupcat2( const wchar_t *a, ... );

/**
   Returns a newly allocated wide character string wich is a copy of the string in, but of length c or shorter. The returned string is always null terminated, and the null is not included in the string length.
*/
wchar_t *wcsndup( const wchar_t *in, int c );

/**
   Converts from wide char to digit in the specified base. If d is not
   a valid digit in the specified base, return -1.
*/
long convert_digit( wchar_t d, int base );


/**
   Convert a wide character string to a number in the specified
   base. This functions is the wide character string equivalent of
   strtol. For bases of 10 or lower, 0..9 are used to represent
   numbers. For bases below 36, a-z and A-Z are used to represent
   numbers higher than 9. Higher bases than 36 are not supported.
*/
long wcstol(const wchar_t *nptr,
			wchar_t **endptr,
			int base);

size_t
wcslcat(wchar_t *dst, const wchar_t *src, size_t siz);

size_t
wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz);

/**
   Create a dublicate string. Wide string version of strdup.
*/
wchar_t *wcsdup(const wchar_t *in);

/**
   Case insensitive string compare function. Wide string version of
   strcasecmp.

   This implementation of wcscasecmp does not take into account
   esoteric locales where uppercase and lowercase do not cleanly
   transform between each other. Hopefully this should be fine since
   fish only uses this function with one of the strings supplied by
   fish and guaranteed to be a sane, english word.
*/
int wcscasecmp( const wchar_t *a, const wchar_t *b );

/**
   Test if the given string is a valid variable name
*/

int wcsvarname( wchar_t *str );

/**
   The prototype for this function is missing in some libc
   implementations. Fish has a fallback implementation in case the
   implementation is missing altogether.
*/
int wcwidth( wchar_t c );


/**
   A wcswidth workalike. Fish uses this since the regular wcswidth seems flaky.
*/
int my_wcswidth( const wchar_t *c );

/**
   This functions returns the end of a quoted substring. It can handle nested single and double quotes.
*/
wchar_t *quote_end( const wchar_t *in );

/**
   A call to this function will reset the error counter. Some
   functions print out non-critical error messages. These should check
   the error_count before, and skip printing the message if
   MAX_ERROR_COUNT messages have been printed. The error_reset()
   should be called after each interactive command executes, to allow
   new messages to be printed.
*/
void error_reset();

/**
   Set the locale, also change the ellipsis character
*/
void fish_setlocale(int category, const wchar_t *locale);

/**
   Checks if \c needle is included in the list of strings specified

   \param needle the string to search for in the list 
*/
int contains_str( const wchar_t *needle, ... );

/**
   Call read while blocking the SIGCHLD signal. Should only be called
   if you _know_ there is data available for reading.
*/
int read_blocked(int fd, void *buf, size_t count);

/**
   This is for writing process notification messages. Has to write to
   stdout, so clr_eol and such functions will work correctly. Not an
   issue since this function is only used in interactive mode anyway.
*/
int writeb( tputs_arg_t b );

void die_mem();

/**
  Clean up 
*/
void common_destroy();

/**
   Issue a debug message
   
   \param level the priority of the message. Lower number means higher priority. Messages with too high priority number will be discarded.
   \param the message. 
*/
void debug( int level, wchar_t *msg, ... );

/**
   Replace special characters with escape sequences. Newline is
   replaced with \n, etc. 

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

wchar_t *escape( wchar_t *in, 
				 int escape_all );

wchar_t *unescape( wchar_t * in, int escape_special );

void block();
void unblock();