<!-- d function#1 <p><bf>$1</bf>\label{$1}<p><descrip> --> <!doctype linuxdoc system> <book> <title> S-Lang Run-Time Library Reference: Version 1.4.0 <author> John E. Davis, <tt>davis@space.mit.edu</tt> <date> Sat Feb 3 15:48:19 2001 <toc> <chapt>Array Functions<p> <sect><bf>_reshape</bf><label id="_reshape"><p><descrip> <tag> Synopsis </tag> Copy an array to a new shape <tag> Usage </tag> <tt>Array_Type _reshape (Array_Type A, Array_Type I)</tt> <tag> Description </tag> The <tt>_reshape</tt> function creates a copy of an array <tt>A</tt>, reshapes it to the form specified by <tt>I</tt> and returns the result. The elements of <tt>I</tt> specify the new dimensions of the copy of <tt>A</tt> and must be consistent with the number of elements <tt>A</tt>. <tag> Example </tag> If <tt>A</tt> is a <tt>100</tt> element 1-d array, a new array 2-d array of size <tt>20</tt> by <tt>5</tt> may be created from the elements of <tt>A</tt> by <tscreen><verb> A = _reshape (A, [20, 5]); </verb></tscreen> In this example, the original array was no longer needed. Hence, it is preferable to make use of the <tt>__tmp</tt> operator to avoid the creation of a new array, i.e., <tscreen><verb> A = _reshape (__tmp(A), [20,5]); </verb></tscreen> <tag> Notes </tag> The <tt>reshape</tt> function performs a similar function to <tt>_reshape</tt>. In fact, the <tt>_reshape</tt> function could have been implemented via: <tscreen><verb> define _reshape (a, i) { a = @a; % Make a new copy reshape (a, i); return a; } </verb></tscreen> <tag> See Also </tag> <tt>reshape, array_info</tt> </descrip><p> <sect><bf>array_info</bf><label id="array_info"><p><descrip> <tag> Synopsis </tag> Returns information about an array <tag> Usage </tag> <tt>(Array_Type, Integer_Type, DataType_Type) array_info (Array_Type a)</tt> <tag> Description </tag> The <tt>array_info</tt> function returns information about the array <tt>a</tt>. It returns three values: an 1-d integer array array specifying the size of each dimension of <tt>a</tt>, the number of dimensions of <tt>a</tt>, and the data type of <tt>a</tt>. <tag> Example </tag> The <tt>array_info</tt> function may be used to find the number of rows of an array: <tscreen><verb> define num_rows (a) { variable dims, num_dims, data_type; (dims, num_dims, data_type) = array_info (a); return dims [0]; } </verb></tscreen> For 1-d arrays, this information is more easily obtained from the <tt>length</tt> function. <tag> See Also </tag> <tt>typeof, reshape, length, _reshape</tt> </descrip><p> <sect><bf>array_map</bf><label id="array_map"><p><descrip> <tag> Synopsis </tag> Apply a function to each element of an array <tag> Usage </tag> <tt>Array_Type array_map (type, func, arg0, ...)</tt> <tscreen><verb> DataType_Type type; Ref_Type func; </verb></tscreen> <tag> Description </tag> The <tt>array_map</tt> function may be used to apply a function to each element of an array and returns the result as an array of a specified type. The <tt>type</tt> parameter indicates what kind of array should be returned and generally corresponds to the return type of the function. The <tt>arg0</tt> parameter should be an array and is used to determine the dimensions of the resulting array. If any subsequent arguments correspond to an array of the same size, then those array elements will be passed in parallel with the first arrays arguments. <tag> Example </tag> The first example illustrates how to apply the <tt>strlen</tt> function to an array of strings: <tscreen><verb> S = ["", "Train", "Subway", "Car"]; L = array_map (Integer_Type, &strlen, S); </verb></tscreen> This is equivalent to: <tscreen><verb> S = ["", "Train", "Subway", "Car"]; L = Integer_Type [length (S)]; for (i = 0; i < length (S); i++) L[i] = strlen (S[i]); </verb></tscreen> Now consider an example involving the <tt>strcat</tt> function: <tscreen><verb> files = ["slang", "slstring", "slarray"]; exts = ".c"; cfiles = array_map (String_Type, &strcat, files, exts); % ==> cfiles = ["slang.c slstring.c slarray.c"]; exts = [".a",".b",".c"]; xfiles = array_map (String_Type, &strcat, files, exts); % ==> xfiles = ["slang.a", "slstring.b", "slarray.c"]; </verb></tscreen> <tag> Notes </tag> Many mathemetical functions already work transparantly on arrays. For example, the following two statements produce identical results: <tscreen><verb> B = sin (A); B = array_map (Double_Type, &sin, A); </verb></tscreen> <tag> See Also </tag> <tt>array_info, strlen, strcat, sin</tt> </descrip><p> <sect><bf>array_sort</bf><label id="array_sort"><p><descrip> <tag> Synopsis </tag> Sort an array <tag> Usage </tag> <tt>Array_Type array_sort (Array_Type a [, String_Type or Ref_Type f])</tt> <tag> Description </tag> <tt>array_sort</tt> sorts the array <tt>a</tt> into ascending order and returns an integer array that represents the result of the sort. If the optional second parameter <tt>f</tt> is present, the function specified by <tt>f</tt> will be used to compare elements of <tt>a</tt>; otherwise, a built-in sorting function will be used. If <tt>f</tt> is present, then it must be either a string representing the name of the comparison function, or a reference to the function. The sort function represented by <tt>f</tt> must be a <bf>S-lang</bf> user-defined function that takes two arguments. The function must return an integer that is less than zero if the first parameter is considered to be less than the second, zero if they are equal, and a value greater than zero if the first is greater than the second. If the comparision function is not specified, then a built-in comparison function appropriate for the data type will be used. For example, if <tt>a</tt> is an array of character strings, then the sort will be preformed using <tt>strcmp</tt>. The integer array returned by this function is simply an index that indicates the order of the sorted array. The input array <tt>a</tt> is not changed. <tag> Example </tag> An array of strings may be sorted using the <tt>strcmp</tt> function since it fits the specification for the sorting function described above: <tscreen><verb> variable A = String_Type [3]; A[0] = "gamma"; A[1] = "alpha"; A[2] = "beta"; variable I = array_sort (A, &strcmp); </verb></tscreen> Alternatively, one may use <tscreen><verb> variable I = array_sort (A); </verb></tscreen> to use the built-in comparison function. After the <tt>array_sort</tt> has executed, the variable <tt>I</tt> will have the values <tt>[2, 0, 1]</tt>. This array can be used to re-shuffle the elements of <tt>A</tt> into the sorted order via the array index expression <tt>A = A[I]</tt>. <tag> See Also </tag> <tt>strcmp</tt> </descrip><p> <sect><bf>init_char_array</bf><label id="init_char_array"><p><descrip> <tag> Synopsis </tag> Initialize an array of characters <tag> Usage </tag> <tt>init_char_array (Array_Type a, String_Type s)</tt> <tag> Description </tag> The <tt>init_char_array</tt> function may be used to initialize a character array <tt>a</tt> by setting the elements of the array <tt>a</tt> to the corresponding characters of the string <tt>s</tt>. <tag> Example </tag> The statements <tscreen><verb> variable a = Char_Type [10]; init_char_array (a, "HelloWorld"); </verb></tscreen> creates an character array and initializes its elements to the characters in the string <tt>"HelloWorld"</tt>. <tag> Notes </tag> The character array must be large enough to hold all the characters of the initialization string. <tag> See Also </tag> <tt>bstring_to_array, strlen, strcat</tt> </descrip><p> <sect><bf>length</bf><label id="length"><p><descrip> <tag> Synopsis </tag> Get the length of an object <tag> Usage </tag> <tt>Integer_Type length (obj)</tt> <tag> Description </tag> The <tt>length</tt> function may be used to get information about the length of an object. For simple scalar data-types, it returns <tt>1</tt>. For arrays, it returns the total number of elements of the array. <tag> Notes </tag> If <tt>obj</tt> is a string, <tt>length</tt> returns <tt>1</tt> because a <tt>String_Type</tt> object is considered to be a scalar. To get the number of characters in a string, use the <tt>strlen</tt> function. <tag> See Also </tag> <tt>array_info, typeof, strlen</tt> </descrip><p> <sect><bf>reshape</bf><label id="reshape"><p><descrip> <tag> Synopsis </tag> Reshape an array <tag> Usage </tag> <tt>reshape (Array_Type A, Array_Type I)</tt> <tag> Description </tag> The <tt>reshape</tt> function changes the size of <tt>A</tt> to have the size specified by the 1-d integer array <tt>I</tt>. The elements of <tt>I</tt> specify the new dimensions of <tt>A</tt> and must be consistent with the number of elements <tt>A</tt>. <tag> Example </tag> If <tt>A</tt> is a <tt>100</tt> element 1-d array, it can be changed to a 2-d <tt>20</tt> by <tt>5</tt> array via <tscreen><verb> reshape (A, [20, 5]); </verb></tscreen> However, <tt>reshape(A, [11,5])</tt> will result in an error because the the <tt>[11,5]</tt> array specifies <tt>55</tt> elements. <tag> Notes </tag> Since <tt>reshape</tt> modifies the shape of an array, and arrays are treated as references, then all references to the array will reference the new shape. If this effect is unwanted, then use the <tt>_reshape</tt> function instead. <tag> See Also </tag> <tt>_reshape, array_info</tt> </descrip><p> <sect><bf>transpose</bf><label id="transpose"><p><descrip> <tag> Synopsis </tag> Transpose a 2d array <tag> Usage </tag> <tt>Array_Type transpose (Array_Type a)</tt> <tag> Description </tag> The <tt>transpose</tt> function returns the transpose of a specified array. By definition, the transpose of an array, say one with elements <tt>a[i,j,...k]</tt> is an array whose elements are <tt>a[k,...,j,i]</tt>. <tag> See Also </tag> <tt>_reshape, reshape, array_info</tt> </descrip><p> <sect><bf>where</bf><label id="where"><p><descrip> <tag> Synopsis </tag> Get indices where an integer array is non-zero <tag> Usage </tag> <tt>Array_Type where (Array_Type a)</tt> <tag> Description </tag> The <tt>where</tt> function examines an integer array <tt>a</tt> and returns a 2-d integer array whose rows are the indices of <tt>a</tt> where the corresponding element of <tt>a</tt> is non-zero. <tag> Example </tag> Consider the following: <tscreen><verb> variable X = [0.0:10.0:0.01]; variable A = sin (X); variable I = where (A < 0.0); A[I] = cos (X) [I]; </verb></tscreen> Here the variable <tt>X</tt> has been assigned an array of doubles whose elements range from <tt>0.0</tt> through <tt>10.0</tt> in increments of <tt>0.01</tt>. The second statement assigns <tt>A</tt> to an array whose elements are the <tt>sin</tt> of the elements of <tt>X</tt>. The third statement uses the where function to get the indices of the elements of <tt>A</tt> that are less than <tt>0.0</tt>. Finally, the last statement substitutes into <tt>A</tt> the <tt>cos</tt> of the elements of <tt>X</tt> at the positions of <tt>A</tt> where the corresponding <tt>sin</tt> is less than <tt>0</tt>. The end result is that the elements of <tt>A</tt> are a mixture of sines and cosines. <tag> See Also </tag> <tt>array_info, sin, cos</tt> </descrip><p> <chapt>Associative Array Functions<p> <sect><bf>assoc_delete_key</bf><label id="assoc_delete_key"><p><descrip> <tag> Synopsis </tag> Delete a key from an Associative Array <tag> Usage </tag> <tt>assoc_delete_key (Assoc_Type a, String_Type k)</tt> <tag> Description </tag> The <tt>assoc_delete_key</tt> function deletes a key given by <tt>k</tt> from the associative array <tt>a</tt>. If the specified key does not exist in <tt>a</tt>, then this function has no effect. <tag> See Also </tag> <tt>assoc_key_exists, assoc_get_keys</tt> </descrip><p> <sect><bf>assoc_get_keys</bf><label id="assoc_get_keys"><p><descrip> <tag> Synopsis </tag> Return all the key names of an Associative Array <tag> Usage </tag> <tt>String_Type[] assoc_get_keys (Assoc_Type a)</tt> <tag> Description </tag> This function returns all the key names of an associative array <tt>a</tt> as an ordinary one dimensional array of strings. If the associative array contains no keys, an empty array will be returned. <tag> Example </tag> The following function computes the number of keys in an associative array: <tscreen><verb> define get_num_elements (a) { return length (assoc_get_keys (a)); } </verb></tscreen> <tag> See Also </tag> <tt>assoc_get_values, assoc_key_exists, assoc_delete_key, length</tt> </descrip><p> <sect><bf>assoc_get_values</bf><label id="assoc_get_values"><p><descrip> <tag> Synopsis </tag> Return all the values of an Associative Array <tag> Usage </tag> <tt>Array_Type assoc_get_keys (Assoc_Type a)</tt> <tag> Description </tag> This function returns all the values in the associative array <tt>a</tt> as an array of proper type. If the associative array contains no keys, an empty array will be returned. <tag> Example </tag> Suppose that <tt>a</tt> is an associative array of type <tt>Integer_Type</tt>, i.e., it was created via <tscreen><verb> variable a = Assoc_Type[Integer_Type]; </verb></tscreen> The the following may be used to print the values of the array in ascending order: <tscreen><verb> static define int_sort_fun (x, y) { return sign (x - y); } define sort_and_print_values (a) { variable i, v; v = assoc_get_values (a); i = array_sort (v, &int_sort_fun); v = v[i]; foreach (v) { variable vi = (); () = fprintf (stdout, "%d\n", vi); } } </verb></tscreen> <tag> See Also </tag> <tt>assoc_get_values, assoc_key_exists, assoc_delete_key, array_sort</tt> </descrip><p> <sect><bf>assoc_key_exists</bf><label id="assoc_key_exists"><p><descrip> <tag> Synopsis </tag> Check to see whether a key exists in an Associative Array <tag> Usage </tag> <tt>Integer_Type assoc_key_exists (Assoc_Type a, String_Type k)</tt> <tag> Description </tag> The <tt>assoc_key_exists</tt> function may be used to determine whether or not a specified key <tt>k</tt> exists in an associative array <tt>a</tt>. It returns <tt>1</tt> if the key exists, or <tt>0</tt> if it does not. <tag> See Also </tag> <tt>assoc_get_keys, assoc_get_values, assoc_delete_key</tt> </descrip><p> <chapt>Functions that Operate on Strings<p> <sect><bf>Sprintf</bf><label id="Sprintf"><p><descrip> <tag> Synopsis </tag> Format objects into a string <tag> Usage </tag> <tt>String_Type Sprintf (String_Type format, ..., Integer_Type n)</tt> <tag> Description </tag> <tt>Sprintf</tt> formats a string from <tt>n</tt> objects according to <tt>format</tt>. Unlike <tt>sprintf</tt>, the <tt>Sprintf</tt> function requires the number of items to format. The format string is a C library <tt>sprintf</tt> style format descriptor. Briefly, the format string may consist of ordinary characters (not including the <tt>%</tt> character), which are copied into the output string as-is, and a conversion specification introduced by the <tt>%</tt> character. The <tt>%</tt> character must be followed by at least one other character to specify the conversion: <tscreen><verb> s value is a string f value is a floating point number e print float in exponential form, e.g., 2.345e08 g print float as e or g, depending upon its value c value is an ascii character % print the percent character d print a signed decimal integer u print an unsigned decimal integer o print an integer as octal X print an integer as hexadecimal S convert value to a string and format as string </verb></tscreen> Note that <tt>%S</tt> is a <bf>S-lang</bf> extension which will cause the value to be formatted as string. In fact, <tt>sprintf("%S",x)</tt> is equivalent to <tt>sprintf("%s",string(x))</tt>. <tscreen><verb> s = Sprintf("%f is greater than %f but %s is better than %s\n", PI, E, "Cake" "Pie", 4); </verb></tscreen> The final argument to <tt>Sprintf</tt> is the number of items to format; in this case, there are 4 items. <tag> See Also </tag> <tt>sprintf, string, sscanf</tt> </descrip><p> <sect><bf>create_delimited_string</bf><label id="create_delimited_string"><p><descrip> <tag> Synopsis </tag> Concatenate strings using a delimiter <tag> Usage </tag> <tt>String_Type create_delimited_string (delim, s_1, s_2, ..., s_n, n)</tt> <tscreen><verb> String_Type delim, s_1, ..., s_n Integer_Type n </verb></tscreen> <tag> Description </tag> <tt>create_delimited_string</tt> performs a concatenation operation on the <tt>n</tt> strings <tt>s_1</tt>, ...,<tt>s_n</tt>, using the string <tt>delim</tt> as a delimiter. The resulting string is equivalent to one obtained via <tscreen><verb> s_1 + delim + s_2 + delim + ... + s_n </verb></tscreen> <tag> Example </tag> One use for this function is to construct path names, e.g., <tscreen><verb> create_delimited_string ("/", "user", "local", "bin", 3); </verb></tscreen> will produce <tt>"usr/local/bin"</tt>. <tag> Notes </tag> The expression <tt>strcat(a,b)</tt> is equivalent to <tt>create_delimited_string("", a, b, 2)</tt>. <tag> See Also </tag> <tt>strjoin, is_list_element, extract_element, strchop, strcat</tt> </descrip><p> <sect><bf>extract_element</bf><label id="extract_element"><p><descrip> <tag> Synopsis </tag> Extract the nth element of a string with delimiters <tag> Usage </tag> <tt>String_Type extract_element (String_Type list, Integer_Type nth, Integer_Type delim);</tt> <tag> Description </tag> The <tt>extract_element</tt> function may be used to extract the <tt>nth</tt> element of the <tt>delim</tt> delimited list of strings <tt>list</tt>. The function will return the <tt>nth</tt> element of the list, unless <tt>nth</tt> specifies more elements than the list contains, in which case <tt>NULL</tt> will be returned. Elements in the list are numbered from <tt>0</tt>. <tag> Example </tag> The expression <tscreen><verb> extract_element ("element 0, element 1, element 2", 1, ',') </verb></tscreen> returns the string <tt>" element 1"</tt>, whereas <tscreen><verb> extract_element ("element 0, element 1, element 2", 1, ' ') </verb></tscreen> returns <tt>"0,"</tt>. The following function may be used to compute the number of elements in the list: <tscreen><verb> define num_elements (list, delim) { variable nth = 0; while (NULL != extract_element (list, nth, delim)) nth++; return nth; } </verb></tscreen> Alternatively, the <tt>strchop</tt> function may be more useful. In fact, <tt>extract_element</tt> may be expressed in terms of the function <tt>strchop</tt> as <tscreen><verb> define extract_element (list, nth, delim) { list = strchop(list, delim, 0); if (nth >= length (list)) return NULL; else return list[nth]; } </verb></tscreen> and the <tt>num_elements</tt> function used above may be recoded more simply as: <tscreen><verb> define num_elements (list, delim) { return length (strchop (length, delim, 0)); } </verb></tscreen> <tag> See Also </tag> <tt>is_list_element, is_substr, strtok, strchop, create_delimited_string</tt> </descrip><p> <sect><bf>is_list_element</bf><label id="is_list_element"><p><descrip> <tag> Synopsis </tag> Test whether a delimited string contains a specific element <tag> Usage </tag> <tt>Integer_Type is_list_element (String_Type list, String_Type elem, Integer_Type delim)</tt> <tag> Description </tag> The <tt>is_list_element</tt> function may be used to determine whether or not a delimited list of strings, <tt>list</tt>, contains the element <tt>elem</tt>. If <tt>elem</tt> is not an element of <tt>list</tt>, the function will return zero, otherwise, it returns 1 plus the matching element number. <tag> Example </tag> The expression <tscreen><verb> is_list_element ("element 0, element 1, element 2", "0,", ' '); </verb></tscreen> returns <tt>2</tt> since <tt>"0,"</tt> is element number one of the list (numbered from zero). <tag> See Also </tag> <tt>extract_element, is_substr, create_delimited_string</tt> </descrip><p> <sect><bf>is_substr</bf><label id="is_substr"><p><descrip> <tag> Synopsis </tag> Test for a specified substring within a string. <tag> Usage </tag> <tt>Integer_Type is_substr (String_Type a, String_Type b)</tt> <tag> Description </tag> This function may be used to determine if <tt>a</tt> contains the string <tt>b</tt>. If it does not, the function returns 0; otherwise it returns the position of the first occurance of <tt>b</tt> in <tt>a</tt>. <tag> Notes </tag> It is important to remember that the first character of a string corresponds to a position value of <tt>1</tt>. <tag> See Also </tag> <tt>substr, string_match, strreplace</tt> </descrip><p> <sect><bf>make_printable_string</bf><label id="make_printable_string"><p><descrip> <tag> Synopsis </tag> Format a string suitable for parsing <tag> Usage </tag> <tt>String_Type make_printable_string(String_Type str)</tt> <tag> Description </tag> This function formats a string in such a way that it may be used as an argument to the <tt>eval</tt> function. The resulting string is identical to <tt>str</tt> except that it is enclosed in double quotes and the backslash, newline, and double quote characters are expanded. <tag> See Also </tag> <tt>eval, str_quote_string</tt> </descrip><p> <sect><bf>sprintf</bf><label id="sprintf"><p><descrip> <tag> Synopsis </tag> Format objects into a string <tag> Usage </tag> <tt>String sprintf (String format, ...);</tt> <tag> Description </tag> This function performs a similar task as the C function with the same name. It differs from the <bf>S-lang</bf> function <tt>Sprintf</tt> in that it does not require the number of items to format. See the documentation for <tt>Sprintf</tt> for more information. <tag> See Also </tag> <tt>Sprintf, string, sscanf, vmessage</tt> </descrip><p> <sect><bf>sscanf</bf><label id="sscanf"><p><descrip> <tag> Synopsis </tag> Parse a formatted string <tag> Usage </tag> <tt>Int_Type sscanf (s, fmt, r1, ... rN)</tt> <tscreen><verb> String_Type s, fmt; Ref_Type r1, ..., rN </verb></tscreen> <tag> Description </tag> The <tt>sscanf</tt> function parses the string <tt>s</tt> according to the format <tt>fmt</tt> and sets the variables whose references are given by <tt>r1</tt>, ..., <tt>rN</tt>. The function returns the number of references assigned, or <tt>-1</tt> upon error. The format string <tt>fmt</tt> consists of ordinary characters and conversion specifiers. A conversion specifier begins with the special character <tt>%</tt> and is described more fully below. A white space character in the format string matches any amount of whitespace in the input string. Parsing of the format string stops whenever a match fails. The <tt>%</tt> is used to denote a conversion specifier whose general form is given by <tt>%[*][width][type]format</tt> where the brackets indicate optional items. If <tt>*</tt> is present, then the conversion will be performed by no assignment to a reference will be made. The <tt>width</tt> specifier specifies the maximum field width to use for the conversion. The <tt>type</tt> modifier is used to indicate size of the object, e.g., a short integer, as follows. If <em>type</em> is given as the character <tt>h</tt>, then if the format conversion is for an integer (<tt>dioux</tt>), the object assigned will be a short integer. If <em>type</em> is <tt>l</tt>, then the conversion will be to a long integer for integer conversions, or to a double precession floating point number for floating point conversions. The format specifier is a character that specifies the conversion: <tscreen><verb> % Matches a literal percent character. No assigment is performed. d Matches a signed decimal integer. D Matches a long decimal integer (equiv to `ld') u Matches an unsigned decimal integer U Matches an unsigned long decimal integer (equiv to `lu') i Matches either a hexidecimal integer, decimal integer, or octal integer. I Equivalent to `li'. x Matches a hexidecimal integer. X Matches a long hexidecimal integer (same as `lx'). e,f,g Matches a decimal floating point number (Float_Type). E,F,G Matches a double precision floating point number, same as `lf'. s Matches a string of non-whitespace characters (String_Type). c Matches one character. If width is given, width characters are matched. n Assigns the number of characters scanned so far. [...] Matches zero or more characters from the set of characters enclosed by the square brackets. If '^' is given as the first character, then the complement set is matched. </verb></tscreen> <tag> Example </tag> Suppose that <tt>s</tt> is <tt>"Coffee: (3,4,12.4)"</tt>. Then <tscreen><verb> n = sscanf (s, "%[a-zA-Z]: (%d,%d,%lf)", &item, &x, &y, &z); </verb></tscreen> will set <tt>n</tt> to <tt>4</tt>, <tt>item</tt> to <tt>"Coffee"</tt>, <tt>x</tt> to <tt>3</tt>, <tt>y</tt> to <tt>4</tt>, and <tt>z</tt> to the double precision number <tt>12.4</tt>. However, <tscreen><verb> n = sscanf (s, "%s: (%d,%d,%lf)", &item, &x, &y, &z); </verb></tscreen> will set <tt>n</tt> to <tt>1</tt>, <tt>item</tt> to <tt>"Coffee:"</tt> and the remaining variables will not be assigned. <tag> See Also </tag> <tt>sprintf, unpack, string, atof, int, integer, string_match</tt> </descrip><p> <sect><bf>str_delete_chars</bf><label id="str_delete_chars"><p><descrip> <tag> Synopsis </tag> Delete characters from a string <tag> Usage </tag> <tt>String_Type str_delete_chars (String_Type str, String_Type del_set</tt> <tag> Description </tag> This function may be used to delete the set of characters specified by <tt>del_set</tt> from the string <tt>str</tt>. The result is returned. <tag> Example </tag> <tscreen><verb> str = str_delete_chars (str, "^A-Za-z"); </verb></tscreen> will remove all characters except <tt>A-Z</tt> and <tt>a-z</tt> from <tt>str</tt>. </descrip><p> <sect><bf>str_quote_string</bf><label id="str_quote_string"><p><descrip> <tag> Synopsis </tag> Escape characters in a string. <tag> Usage </tag> <tt>String_Type str_quote_string(String_Type str, String_Type qlis, Integer_Type quote)</tt> <tag> Description </tag> The <tt>str_quote_string</tt> returns a string identical to <tt>str</tt> except that all characters in the set specified by the string <tt>qlis</tt> are escaped with the <tt>quote</tt> character, including the quote character itself. This function is useful for making a string that can be used in a regular expression. <tag> Example </tag> Execution of the statements <tscreen><verb> node = "Is it [the coat] really worth $100?"; tag = str_quote_string (node, "\\^$[]*.+?", '\\'); </verb></tscreen> will result in <tt>tag</tt> having the value: <tscreen><verb> Is it \[the coat\] really worth \$100\? </verb></tscreen> <tag> See Also </tag> <tt>str_uncomment_string, make_printable_string</tt> </descrip><p> <sect><bf>str_replace</bf><label id="str_replace"><p><descrip> <tag> Synopsis </tag> Replace a substring of a string <tag> Usage </tag> <tt>Integer_Type str_replace (String_Type a, String_Type b, String_Type c)</tt> <tag> Description </tag> The <tt>str_replace</tt> function replaces the first occurance of <tt>b</tt> in <tt>a</tt> with <tt>c</tt> and returns an integer that indicates whether a replacement was made or not. If <tt>b</tt> does not occur in <tt>a</tt>, zero is returned. However, if <tt>b</tt> occurs in <tt>a</tt>, a non-zero integer is returned as well as the new string resulting from the replacement. <tag> Notes </tag> This function has been superceded by <tt>strreplace</tt>. <tag> See Also </tag> <tt>strreplace</tt> </descrip><p> <sect><bf>str_uncomment_string</bf><label id="str_uncomment_string"><p><descrip> <tag> Synopsis </tag> Remove comments from a string <tag> Usage </tag> <tt>String_Type str_uncomment_string(String_Type s, String_Type beg, String_Type end)</tt> <tag> Description </tag> This function may be used to remove comments from a string <tt>s</tt>. The parameters, <tt>beg</tt> and <tt>end</tt>, are strings of equal length whose corresponding characters specify the begin and end comment characters, respectively. It returns the uncommented string. <tag> Example </tag> The expression <tscreen><verb> str_uncomment_string ("Hello (testing) 'example' World", "'(", "')") </verb></tscreen> returns the string <tt>"Hello World"</tt>. <tag> Notes </tag> This routine does not handle multicharacter comment delimiters and it assumes that comments are not nested. <tag> See Also </tag> <tt>str_quote_string</tt> </descrip><p> <sect><bf>strcat</bf><label id="strcat"><p><descrip> <tag> Synopsis </tag> Concatenate strings <tag> Usage </tag> <tt>String_Type strcat (String_Type a_1, ..., String_Type a_N)</tt> <tag> Description </tag> The <tt>strcat</tt> function concatenates its N <tt>String_Type</tt> arguments <tt>a_1</tt>, ... <tt>a_N</tt> together and returns the result. <tag> Example </tag> <tscreen><verb> strcat ("Hello", " ", "World"); </verb></tscreen> produces the string <tt>"Hello World"</tt>. <tag> Notes </tag> This function is equivalent to the binary operation <tt>a_1+...+a_N</tt>. However, <tt>strcat</tt> is much faster making it the preferred method to concatenate string. <tag> See Also </tag> <tt>sprintf, create_delimited_string</tt> </descrip><p> <sect><bf>strchop</bf><label id="strchop"><p><descrip> <tag> Synopsis </tag> Chop or split a string into substrings. <tag> Usage </tag> <tt>String_Type[] strchop (String_Type str, Integer_Type delim, Integer_Type quote)</tt> <tag> Description </tag> The <tt>strchop</tt> function may be used to split-up a string <tt>str</tt> that consists of substrings delimited by the character specified by <tt>delim</tt>. If the integer <tt>quote</tt> is non-zero, it will be taken as a quote character for the delimiter. The function returns the substrings as an array. <tag> Example </tag> The following function illustrates how to sort a comma separated list of strings: <tscreen><verb> define sort_string_list (a) { variable i, b, c; b = strchop (a, ',', 0); i = array_sort (b, &strcmp); b = b[i]; % rearrange % Convert array back into comma separated form return strjoin (b, ","); } </verb></tscreen> <tag> Notes </tag> The semantics of this <tt>strchop</tt> and <tt>strchopr</tt> have been changed since version 1.2.x of the interpreter. Old versions of these functions returned the values on the stack, which meant that one could not chop up arbitrarily long strings that consist of many substrings. The function <tt>strchopr</tt> should be used if it is desired to have the string chopped-up in the reverse order. <tag> See Also </tag> <tt>strchopr, extract_element, strjoin, strtok</tt> </descrip><p> <sect><bf>strchopr</bf><label id="strchopr"><p><descrip> <tag> Synopsis </tag> Chop or split a string into substrings. <tag> Usage </tag> <tt>String_Type[] strchopr (String_Type str, String_Type delim, String_Type quote)</tt> <tag> Description </tag> This routine performs exactly the same function as <tt>strchop</tt> except that it returns the substrings in the reverse order. See the documentation for <tt>strchop</tt> for more information. <tag> See Also </tag> <tt>strchop, extract_element, strtok, strjoin</tt> </descrip><p> <sect><bf>strcmp</bf><label id="strcmp"><p><descrip> <tag> Synopsis </tag> Compare two strings <tag> Usage </tag> <tt>Interpret strcmp (String_Type a, String_Type b)</tt> <tag> Description </tag> The <tt>strcmp</tt> function may be used to perform a case-sensitive string comparison, in the lexicongraphic sense, on strings <tt>a</tt> and <tt>b</tt>. It returns 0 if the strings are identical, a negative integer if <tt>a</tt> is less than <tt>b</tt>, or a positive integer if <tt>a</tt> is greater than <tt>b</tt>. <tag> Example </tag> The <tt>strup</tt> function may be used to perform a case-insensitive string comparison: <tscreen><verb> define case_insensitive_strcmp (a, b) { return strcmp (strup(a), strup(b)); } </verb></tscreen> <tag> Notes </tag> One may also use one of the binary comparison operators, e.g., <tt>a > b</tt>. <tag> See Also </tag> <tt>strup, strncmp</tt> </descrip><p> <sect><bf>strcompress</bf><label id="strcompress"><p><descrip> <tag> Synopsis </tag> Remove excess whitespace characters from a string <tag> Usage </tag> <tt>String_Type strcompress (String_Type s, String_Type white)</tt> <tag> Description </tag> The <tt>strcompress</tt> function compresses the string <tt>s</tt> by replacing a sequence of one or more characters from the set <tt>white</tt> by the first character of <tt>white</tt>. In addition, it also removes all leading and trailing characters from <tt>s</tt> that are part of <tt>white</tt>. <tag> Example </tag> The expression <tscreen><verb> strcompress (",;apple,,cherry;,banana", ",;"); </verb></tscreen> returns the string <tt>"apple,cherry,banana"</tt>. <tag> See Also </tag> <tt>strtrim, strtrans</tt> </descrip><p> <sect><bf>string_match</bf><label id="string_match"><p><descrip> <tag> Synopsis </tag> Match a string against a regular expression <tag> Usage </tag> <tt>Integer_Type string_match(String_Type str, String_Type pat, Integer_Type pos)</tt> <tag> Description </tag> The <tt>string_match</tt> function returns zero if <tt>str</tt> does not match regular expression specified by <tt>pat</tt>. This function performs the match starting at position <tt>pos</tt> (numbered from 1) in <tt>str</tt>. This function returns the position of the start of the match. To find the exact substring actually matched, use <tt>string_match_nth</tt>. <tag> See Also </tag> <tt>string_match_nth, strcmp, strncmp</tt> </descrip><p> <sect><bf>string_match_nth</bf><label id="string_match_nth"><p><descrip> <tag> Synopsis </tag> Get the result of the last call to string_match <tag> Usage </tag> <tt>(Integer_Type, Integer_Type) = string_match_nth(Integer_Type nth)</tt> <tag> Description </tag> The <tt>string_match_nth</tt> function returns two integers describing the result of the last call to <tt>string_match</tt>. It returns both the offset into the string and the length of characters matches by the <tt>nth</tt> submatch. By convention, <tt>nth</tt> equal to zero means the entire match. Otherwise, <tt>nth</tt> must be an integer with a value 1 through 9, and refers to the set of characters matched by the <tt>nth</tt> regular expression enclosed by the pairs <tt>\(, \)</tt>. <tag> Example </tag> Consider: <tscreen><verb> variable matched, pos, len; matched = string_match("hello world", "\\([a-z]+\\) \\([a-z]+\\)", 1); if (matched) (pos, len) = string_match_nth(2); </verb></tscreen> This will set <tt>matched</tt> to 1 since a match will be found at the first position, <tt>pos</tt> to 6 since <tt>w</tt> is offset 6 characters from the beginning of the string, and <tt>len</tt> to 5 since <tt>"world"</tt> is 5 characters long. <tag> Notes </tag> The position offset is <em>not</em> affected by the value of the offset parameter to the <tt>string_match</tt> function. For example, if the value of the last parameter to the <tt>string_match</tt> function had been 3, <tt>pos</tt> would still have been set to 6. Note also that <tt>string_match_nth</tt> returns the <em>offset</em> from the beginning of the string and not the position of the match. <tag> See Also </tag> <tt>string_match</tt> </descrip><p> <sect><bf>strjoin</bf><label id="strjoin"><p><descrip> <tag> Synopsis </tag> Concatenate elements of a string array <tag> Usage </tag> <tt>String_Type strjoin (Array_Type a, String_Type delim)</tt> <tag> Description </tag> The <tt>strjoin</tt> function operates on an array of strings by joining successive elements together separated with a delimiter <tt>delim</tt>. If <tt>delim</tt> is the empty string <tt>""</tt>, then the result will simply be the concatenation of the elements. <tag> Example </tag> Suppose that <tscreen><verb> days = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun"]; </verb></tscreen> Then <tt>strjoin (days,"+")</tt> will produce <tt>"Sun+Mon+Tue+Wed+Thu+Fri+Sat+Sun"</tt>. Similarly, <tt>strjoin (["","",""], "X")</tt> will produce <tt>"XX"</tt>. <tag> See Also </tag> <tt>create_delimited_string, strchop, strcat</tt> </descrip><p> <sect><bf>strlen</bf><label id="strlen"><p><descrip> <tag> Synopsis </tag> Compute the length of a string <tag> Usage </tag> <tt>Integer_Type strlen (String_Type a)</tt> <tag> Description </tag> The <tt>strlen</tt> function may be used to compute the length of a string. <tag> Example </tag> After execution of <tscreen><verb> variable len = strlen ("hello"); </verb></tscreen> <tt>len</tt> will have a value of <tt>5</tt>. <tag> See Also </tag> <tt>bstrlen, length, substr</tt> </descrip><p> <sect><bf>strlow</bf><label id="strlow"><p><descrip> <tag> Synopsis </tag> Convert a string to lowercase <tag> Usage </tag> <tt>String_Type strlow (String_Type s)</tt> <tag> Description </tag> The <tt>strlow</tt> function takes a string <tt>s</tt> and returns another string identical to <tt>s</tt> except that all upper case characters that comprise <tt>s</tt> will be converted to lower case. <tag> Example </tag> The function <tscreen><verb> define Strcmp (a, b) { return strcmp (strlow (a), strlow (b)); } </verb></tscreen> performs a case-insensitive comparison operation of two strings by converting them to lower case first. <tag> See Also </tag> <tt>strup, tolower, strcmp, strtrim, define_case</tt> </descrip><p> <sect><bf>strncmp</bf><label id="strncmp"><p><descrip> <tag> Synopsis </tag> Compare the first few characters of two strings <tag> Usage </tag> <tt>Integer_Type strncmp (String_Type a, String_Type b, Integer_Type n)</tt> <tag> Description </tag> This function behaves like <tt>strcmp</tt> except that it compares only the first <tt>n</tt> characters in the strings <tt>a</tt> and <tt>b</tt>. See the documentation for <tt>strcmp</tt> for information about the return value. <tag> Example </tag> The expression <tscreen><verb> strcmp ("apple", "appliance", 3); </verb></tscreen> will return zero since the first three characters match. <tag> See Also </tag> <tt>strcmp, strlen</tt> </descrip><p> <sect><bf>strreplace</bf><label id="strreplace"><p><descrip> <tag> Synopsis </tag> Replace one or more substrings <tag> Usage </tag> <tt>(new, n) = strreplace (a, b, c, max_n)</tt> <tscreen><verb> String_Type a, b, c, rep; Int_Type n, max_n; </verb></tscreen> <tag> Description </tag> The <tt>strreplace</tt> function may be used to replace one or more occurances of <tt>b</tt> in <tt>a</tt> with <tt>c</tt>. If the integer <tt>max_n</tt> is positive, then the first <tt>max_n</tt> occurances of <tt>b</tt> in <tt>a</tt> will be replaced. Otherwise, if <tt>max_n</tt> is negative, then the last <tt>abs(max_n)</tt> occurances will be replaced. The function returns the resulting string and an integer indicating how many replacements were made. <tag> Example </tag> The following function illustrates how <tt>strreplace</tt> may be used to remove all occurances of a specified substring <tscreen><verb> define delete_substrings (a, b) { (a, ) = strreplace (a, b, "", strlen (a)); return a; } </verb></tscreen> <tag> See Also </tag> <tt>is_substr, strsub, strtrim, strtrans, str_delete_chars</tt> </descrip><p> <sect><bf>strsub</bf><label id="strsub"><p><descrip> <tag> Synopsis </tag> Replace a character with another in a string. <tag> Usage </tag> <tt>String_Type strsub (String_Type s, Integer_Type pos, Integer_Type ch)</tt> <tag> Description </tag> The <tt>strsub</tt> character may be used to substitute the character <tt>ch</tt> for the character at position <tt>pos</tt> of the string <tt>s</tt>. The resulting string is returned. <tag> Example </tag> <tscreen><verb> define replace_spaces_with_comma (s) { variable n; while (n = is_substr (s, " "), n) s = strsub (s, n, ','); return s; } </verb></tscreen> For uses such as this, the <tt>strtrans</tt> function is a better choice. <tag> Notes </tag> The first character in the string <tt>s</tt> is specified by <tt>pos</tt> equal to 1. <tag> See Also </tag> <tt>is_substr, strreplace, strlen</tt> </descrip><p> <sect><bf>strtok</bf><label id="strtok"><p><descrip> <tag> Synopsis </tag> Extract tokens from a string <tag> Usage </tag> <tt>String_Type[] strtok (String_Type str [,String_Type white])</tt> <tag> Description </tag> <tt>strtok</tt> breaks the string <tt>str</tt> into a series of tokens and returns them as an array of strings. If the second parameter <tt>white</tt> is present, then it specifies the set of characters that are to be regarded as whitespace when extracting the tokens, and may consist of the whitespace characters or a range of such characters. If the first character of <tt>white</tt> is <tt>'^'</tt>, then the whitespace characters consist of all characters except those in <tt>white</tt>. For example, if <tt>white</tt> is <tt>" \t\n,;."</tt>, then those characters specifiy the whitespace characters. However, if <tt>white</tt> is given by <tt>"^a-zA-Z0-9_"</tt>, then any character is a whitespace character except those in the ranges <tt>a-z</tt>, <tt>A-Z</tt>, <tt>0-9</tt>, and the underscore character. If the second parameter is not present, then it defaults to <tt>" \t\r\n\f"</tt>. <tag> Example </tag> The following example may be used to count the words in a text file: <tscreen><verb> define count_words (file) { variable fp, line, count; fp = fopen (file, "r"); if (fp == NULL) return -1; count = 0; while (-1 != fgets (&line, fp)) { line = strtok (line, "^a-zA-Z"); count += length (line); } () = fclose (fp); return count; } </verb></tscreen> <tag> See Also </tag> <tt>strchop, strcompress, extract_element, strjoin</tt> </descrip><p> <sect><bf>strtrans</bf><label id="strtrans"><p><descrip> <tag> Synopsis </tag> Replace characters in a string <tag> Usage </tag> <tt>String_Type strtrans (str, old_set, new_set)</tt> <tscreen><verb> String_Type str, old_set, new_set; </verb></tscreen> <tag> Description </tag> The <tt>strtrans</tt> function may be used to replace all the characters from the set <tt>old_set</tt> with the corresponding characters from <tt>new_set</tt> in the string <tt>str</tt>. If <tt>new_set</tt> is empty, then the characters in <tt>new_set</tt> will be removed from <tt>str</tt>. This function returns the result. <tag> Example </tag> <tscreen><verb> str = strtrans (str, "A-Z", "a-z"); % lower-case str str = strtrans (str, "^0-9", " "); % Replace anything but 0-9 by space </verb></tscreen> <tag> See Also </tag> <tt>strreplace, strtrim, strup, strlow</tt> </descrip><p> <sect><bf>strtrim</bf><label id="strtrim"><p><descrip> <tag> Synopsis </tag> Remove whitespace from the ends of a string <tag> Usage </tag> <tt>String_Type strtrim (String_Type s [,String_Type w])</tt> <tag> Description </tag> The <tt>strtrim</tt> function removes all leading and trailing whitespace characters from the string <tt>s</tt> and returns the result. The optional second parameter specifies the set of whitespace characters. If the argument is not present, then the set defaults to <tt>" \t\r\n"</tt>. <tag> See Also </tag> <tt>strtrim_beg, strtrim_end, strcompress</tt> </descrip><p> <sect><bf>strtrim_beg</bf><label id="strtrim_beg"><p><descrip> <tag> Synopsis </tag> Remove leading whitespace from a string <tag> Usage </tag> <tt>String_Type strtrim_beg (String_Type s [,String_Type w])</tt> <tag> Description </tag> The <tt>strtrim_beg</tt> function removes all leading whitespace characters from the string <tt>s</tt> and returns the result. The optional second parameter specifies the set of whitespace characters. If the argument is not present, then the set defaults to <tt>" \t\r\n"</tt>. <tag> See Also </tag> <tt>strtrim, strtrim_end, strcompress</tt> </descrip><p> <sect><bf>strtrim_end</bf><label id="strtrim_end"><p><descrip> <tag> Synopsis </tag> Remove trailing whitespace from a string <tag> Usage </tag> <tt>String_Type strtrim_end (String_Type s [,String_Type w])</tt> <tag> Description </tag> The <tt>strtrim_end</tt> function removes all trailing whitespace characters from the string <tt>s</tt> and returns the result. The optional second parameter specifies the set of whitespace characters. If the argument is not present, then the set defaults to <tt>" \t\r\n"</tt>. <tag> See Also </tag> <tt>strtrim, strtrim_beg, strcompress</tt> </descrip><p> <sect><bf>strup</bf><label id="strup"><p><descrip> <tag> Synopsis </tag> Convert a string to uppercase <tag> Usage </tag> <tt>String_Type strup (String_Type s)</tt> <tag> Description </tag> The <tt>strup</tt> function takes a string <tt>s</tt> and returns another string identical to <tt>s</tt> except that all lower case characters that comprise <tt>s</tt> will be converted to upper case. <tag> Example </tag> The function <tscreen><verb> define Strcmp (a, b) { return strcmp (strup (a), strup (b)); } </verb></tscreen> performs a case-insensitive comparison operation of two strings by converting them to upper case first. <tag> See Also </tag> <tt>strlow, toupper, strcmp, strtrim, define_case, strtrans</tt> </descrip><p> <sect><bf>substr</bf><label id="substr"><p><descrip> <tag> Synopsis </tag> Extract a substring from a string <tag> Usage </tag> <tt>String_Type substr (String_Type s, Integer_Type n, Integer_Type len)</tt> <tag> Description </tag> The <tt>substr</tt> function returns a substring with length <tt>len</tt> of the string <tt>s</tt> beginning at position <tt>n</tt>. If <tt>len</tt> is <tt>-1</tt>, the entire length of the string <tt>s</tt> will be used for <tt>len</tt>. The first character of <tt>s</tt> is given by <tt>n</tt> equal to 1. <tag> Example </tag> <tscreen><verb> substr ("To be or not to be", 7, 5); </verb></tscreen> returns <tt>"or no"</tt> <tag> Notes </tag> In many cases it is more convenient to use array indexing rather than the <tt>substr</tt> function. In fact, <tt>substr(s,i+1,strlen(s))</tt> is equivalent to <tt>s[[i:]]</tt>. <tag> See Also </tag> <tt>is_substr, strlen</tt> </descrip><p> <chapt>Functions that Manipulate Structures<p> <sect><bf>_push_struct_field_values</bf><label id="_push_struct_field_values"><p><descrip> <tag> Synopsis </tag> Push the values of a structure's fields onto the stack <tag> Usage </tag> <tt>Integer_Type num = _push_struct_field_values (Struct_Type s)</tt> <tag> Description </tag> The <tt>_push_struct_field_values</tt> function pushes the values of all the fields of a structure onto the stack, returning the number of items pushed. The fields are pushed such that the last field of the structure is pushed first. <tag> See Also </tag> <tt>get_struct_field_names, get_struct_field</tt> </descrip><p> <sect><bf>get_struct_field</bf><label id="get_struct_field"><p><descrip> <tag> Synopsis </tag> Get the value associated with a structure field <tag> Usage </tag> <tt>x = get_struct_field (Struct_Type s, String field_name)</tt> <tag> Description </tag> The <tt>get_struct_field</tt> function gets the value of the field whose name is specified by <tt>field_name</tt> of the structure <tt>s</tt>. <tag> Example </tag> The following example illustrates how this function may be used to to print the value of a structure. <tscreen><verb> define print_struct (s) { variable name; foreach (get_struct_field_names (s)) { name = (); value = get_struct_field (s, name); vmessage ("s.%s = %s\n", name, string(value)); } } </verb></tscreen> <tag> See Also </tag> <tt>set_struct_field, get_struct_field_names, array_info</tt> </descrip><p> <sect><bf>get_struct_field_names</bf><label id="get_struct_field_names"><p><descrip> <tag> Synopsis </tag> Retrieve the field names associated with a structure <tag> Usage </tag> <tt>String_Type[] = get_struct_field_names (Struct_Type s)</tt> <tag> Description </tag> The <tt>get_struct_field_names</tt> function returns an array of strings whose elements specify the names of the fields of the struct <tt>s</tt>. <tag> Example </tag> The following example illustrates how the <tt>get_struct_field_names</tt> function may be used to print the value of a structure. <tscreen><verb> define print_struct (s) { variable name, value; foreach (get_struct_field_names (s)) { name = (); value = get_struct_field (name); vmessage ("s.%s = %s\n", name, string (value)); } } </verb></tscreen> <tag> See Also </tag> <tt>_push_struct_field_values, get_struct_field</tt> </descrip><p> <sect><bf>is_struct_type</bf><label id="is_struct_type"><p><descrip> <tag> Synopsis </tag> Determine whether or not an object is a structure <tag> Usage </tag> <tt>Integer_Type is_struct_type (X)</tt> <tag> Description </tag> The <tt>is_struct_type</tt> function returns <tt>1</tt> if the the parameter refers to a structure or a user-defined type. If the object is neither, <tt>0</tt> will be returned. <tag> See Also </tag> <tt>typeof, _typeof</tt> </descrip><p> <sect><bf>set_struct_field</bf><label id="set_struct_field"><p><descrip> <tag> Synopsis </tag> Set the value associated with a structure field <tag> Usage </tag> <tt>set_struct_field (s, field_name, field_value)</tt> <tscreen><verb> Struct_Type s; String_Type field_name; Generic_Type field_value; </verb></tscreen> <tag> Description </tag> The <tt>set_struct_field</tt> function sets the value of the field whose name is specified by <tt>field_name</tt> of the structure <tt>s</tt> to <tt>field_value</tt>. <tag> See Also </tag> <tt>get_struct_field, get_struct_field_names, set_struct_fields, array_info</tt> </descrip><p> <sect><bf>set_struct_fields</bf><label id="set_struct_fields"><p><descrip> <tag> Synopsis </tag> Set the fields of a structure <tag> Usage </tag> <tt>set_struct_fields (Struct_Type s, ...)</tt> <tag> Description </tag> The <tt>set_struct_fields</tt> function may be used to set zero or more fields of a structure. The fields are set in the order in which they were created when the structure was defined. <tag> Example </tag> <tscreen><verb> variable s = struct { "name", "age", "height" }; set_struct_fields (s, "Bill", 13, 64); </verb></tscreen> <tag> See Also </tag> <tt>set_struct_field, get_struct_field_names</tt> </descrip><p> <chapt>Informational Functions<p> <sect><bf>_NARGS</bf><label id="_NARGS"><p><descrip> <tag> Synopsis </tag> The number of parameters passed to a function <tag> Usage </tag> <tt>Integer_Type _NARGS</tt> The value of the <tt>_NARGS</tt> variable represents the number of arguments passed to the function. This variable is local to each function. <tag> Example </tag> This example uses the <tt>_NARGS</tt> variable to print the list of values passed to the function: <tscreen><verb> define print_values () { variable arg; if (_NARGS == 0) { message ("Nothing to print"); return; } foreach (__pop_args (_NARGS)) { arg = (); vmessage ("Argument value is: %S", arg.value); } } </verb></tscreen> <tag> See Also </tag> <tt>__pop_args, __push_args, typeof</tt> </descrip><p> <sect><bf>__get_defined_symbols</bf><label id="__get_defined_symbols"><p><descrip> <tag> Synopsis </tag> Get the symbols defined by the preprocessor <tag> Usage </tag> <tt>Integer_Type __get_defined_symbols ()</tt> <tag> Description </tag> The <tt>__get_defined_symbols</tt> functions is used to get the list of all the symbols defined by the <bf>S-lang</bf> preprocessor. It pushes each of the symbols on the stack followed by the number of items pushed. <tag> See Also </tag> <tt>is_defined, _apropos</tt> </descrip><p> <sect><bf>__is_initialized</bf><label id="__is_initialized"><p><descrip> <tag> Synopsis </tag> Determine whether or not a variable has a value <tag> Usage </tag> <tt>Integer_Type __is_initialized (Ref_Type r)</tt> <tag> Description </tag> This function returns non-zero of the object referenced by <tt>r</tt> is initialized, i.e., whether it has a value. It returns <tt>0</tt> if the referenced object has not been initialized. <tag> Example </tag> For example, the function: <tscreen><verb> define zero () { variable f; return __is_initialized (&f); } </verb></tscreen> will always return zero, but <tscreen><verb> define one () { variable f = 0; return __is_initialized (&f); } </verb></tscreen> will return one. <tag> Notes </tag> It is easy to see why a reference to the variable must be passed to <tt>__is_initialized</tt> and not the variable itself; otherwise, the value of the variable would be passed and the variable may have no value if it was not initialized. <tag> See Also </tag> <tt>__get_reference, __uninitialize, is_defined, typeof, eval</tt> </descrip><p> <sect><bf>_apropos</bf><label id="_apropos"><p><descrip> <tag> Synopsis </tag> Generate a list of functions and variables <tag> Usage </tag> <tt>Array_Type _apropos (String_Type ns, String_Type s, Integer_Type flags)</tt> <tag> Description </tag> The <tt>_apropos</tt> function may be used to get a list of all defined objects in the namespace <tt>ns</tt> whose name matches the regular expression <tt>s</tt> and whose type matches those specified by <tt>flags</tt>. It returns an array of strings representing the matches. The second parameter <tt>flags</tt> is a bit mapped value whose bits are defined according to the following table <tscreen><verb> 1 Intrinsic Function 2 User-defined Function 4 Intrinsic Variable 8 User-defined Variable </verb></tscreen> <tag> Example </tag> <tscreen><verb> define apropos (s) { variable n, name, a; a = _apropos ("Global", s, 0xF); vmessage ("Found %d matches:", length (a)); foreach (a) { name = (); message (name); } } </verb></tscreen> prints a list of all matches. <tag> Notes </tag> If the namespace specifier <tt>ns</tt> is the empty string <tt>""</tt>, then the namespace will default to the static namespace of the current compilation unit. <tag> See Also </tag> <tt>is_defined, sprintf</tt> </descrip><p> <sect><bf>_function_name</bf><label id="_function_name"><p><descrip> <tag> Synopsis </tag> Returns the name of the currently executing function <tag> Usage </tag> <tt>String_Type _function_name ();</tt> <tag> Description </tag> This function returns the name of the currently executing function. If called from top-level, it returns the empty string. <tag> See Also </tag> <tt>_trace_function, is_defined</tt> </descrip><p> <sect><bf>_slang_doc_dir</bf><label id="_slang_doc_dir"><p><descrip> <tag> Synopsis </tag> Installed documentation directory <tag> Usage </tag> <tt>String_Type _slang_doc_dir;</tt> <tag> Description </tag> The <tt>_slang_doc_dir</tt> variable is a read-only whose value specifies the installation location of the <bf>S-lang</bf> documentation. <tag> See Also </tag> <tt>get_doc_string_from_file</tt> </descrip><p> <sect><bf>_slang_version</bf><label id="_slang_version"><p><descrip> <tag> Synopsis </tag> The S-Lang library version number <tag> Usage </tag> <tt>Integer_Type _slang_version</tt> <tag> Description </tag> The <tt>_slang_version</tt> variable is read-only and and whose value represents the number of the <bf>S-lang</bf> library. <tag> See Also </tag> <tt>_slang_version_string</tt> </descrip><p> <sect><bf>_slang_version_string</bf><label id="_slang_version_string"><p><descrip> <tag> Synopsis </tag> The S-Lang library version number as a string <tag> Usage </tag> <tt>String_Type _slang_version_string</tt> <tag> Description </tag> The <tt>_slang_version_string</tt> variable is read-only and whose value represents the version number of the <bf>S-lang</bf> library. <tag> See Also </tag> <tt>_slang_version</tt> </descrip><p> <sect><bf>get_doc_string_from_file</bf><label id="get_doc_string_from_file"><p><descrip> <tag> Synopsis </tag> Read documentation from a file <tag> Usage </tag> <tt>String_Type get_doc_string_from_file (String_Type f, String_Type t)</tt> <tag> Description </tag> <tt>get_doc_string_from_file</tt> opens the documentation file <tt>f</tt> and searches it for topic <tt>t</tt>. It returns the documentation for <tt>t</tt> upon success, otherwise it returns <tt>NULL</tt> upon error. It will fail if <tt>f</tt> could not be opened or does not contain documentation for the topic. <tag> See Also </tag> <tt>stat_file</tt> <tag> See Also </tag> <tt>_slang_doc_dir</tt> </descrip><p> <sect><bf>is_defined</bf><label id="is_defined"><p><descrip> <tag> Synopsis </tag> Indicate whether a variable or function defined. <tag> Usage </tag> <tt>Integer_Type is_defined (String_Type obj)</tt> <tag> Description </tag> This function is used to determine whether or not a function or variable whose name is <tt>obj</tt> has been defined. If <tt>obj</tt> is not defined, the function returns 0. Otherwise, it returns a non-zero value that defpends on the type of object <tt>obj</tt> represents. Specifically, it returns one of the following values: <tscreen><verb> +1 if an intrinsic function +2 if user defined function -1 if intrinsic variable -2 if user defined variable 0 if undefined </verb></tscreen> <tag> Example </tag> For example, consider the function: <tscreen><verb> define runhooks (hook) { if (2 == is_defined(hook)) eval(hook); } </verb></tscreen> This function could be called from another <bf>S-lang</bf> function to allow customization of that function, e.g., if the function represents a mode, the hook could be called to setup keybindings for the mode. <tag> See Also </tag> <tt>typeof, eval, autoload, __get_reference, __is_initialized</tt> </descrip><p> <chapt>Mathematical Functions<p> <sect><bf>Conj</bf><label id="Conj"><p><descrip> <tag> Synopsis </tag> Compute the complex conjugate of a number <tag> Usage </tag> <tt>z1 = Conj (z)</tt> <tag> Description </tag> The <tt>Conj</tt> function returns the complex conjugate of a number. If its argument is an array, the <tt>Conj</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>Real, Imag, abs</tt> </descrip><p> <sect><bf>Imag</bf><label id="Imag"><p><descrip> <tag> Synopsis </tag> Compute the imaginary part of a number <tag> Usage </tag> <tt>i = Imag (z)</tt> <tag> Description </tag> The <tt>Imag</tt> function returns the imaginary part of a number. If its argument is an array, the <tt>Imag</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>Real, Conj, abs</tt> </descrip><p> <sect><bf>Real</bf><label id="Real"><p><descrip> <tag> Synopsis </tag> Compute the real part of a number <tag> Usage </tag> <tt>r = Real (z)</tt> <tag> Description </tag> The <tt>Real</tt> function returns the real part of a number. If its argument is an array, the <tt>Real</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>Imag, Conj, abs</tt> </descrip><p> <sect><bf>abs</bf><label id="abs"><p><descrip> <tag> Synopsis </tag> Compute the absolute value of a number <tag> Usage </tag> <tt>y = abs(x)</tt> <tag> Description </tag> The <tt>abs</tt> function returns the absolute value of an arithmetic type. If its argument is a complex number (<tt>Complex_Type</tt>), then it returns the modulus. If the argument is an array, a new array will be created whose elements are obtained from the original array by using the <tt>abs</tt> function. <tag> See Also </tag> <tt>sign, sqr</tt> </descrip><p> <sect><bf>acos</bf><label id="acos"><p><descrip> <tag> Synopsis </tag> Compute the arc-cosine of an number <tag> Usage </tag> <tt>y = acos (x)</tt> <tag> Description </tag> The <tt>acos</tt> function computes the arc-cosine of a number and returns the result as an array. If its argument is an array, the <tt>acos</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>acosh</bf><label id="acosh"><p><descrip> <tag> Synopsis </tag> Compute the inverse cosh of an number <tag> Usage </tag> <tt>y = acosh (x)</tt> <tag> Description </tag> The <tt>acosh</tt> function computes the inverse cosh of a number and returns the result as an array. If its argument is an array, the <tt>acosh</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>asin</bf><label id="asin"><p><descrip> <tag> Synopsis </tag> Compute the arc-sine of an number <tag> Usage </tag> <tt>y = asin (x)</tt> <tag> Description </tag> The <tt>asin</tt> function computes the arc-sine of a number and returns the result as an array. If its argument is an array, the <tt>asin</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>asinh</bf><label id="asinh"><p><descrip> <tag> Synopsis </tag> Compute the inverse-sinh of an number <tag> Usage </tag> <tt>y = asinh (x)</tt> <tag> Description </tag> The <tt>asinh</tt> function computes the inverse-sinh of a number and returns the result as an array. If its argument is an array, the <tt>asinh</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>atan</bf><label id="atan"><p><descrip> <tag> Synopsis </tag> Compute the arc-tangent of an number <tag> Usage </tag> <tt>y = atan (x)</tt> <tag> Description </tag> The <tt>atan</tt> function computes the arc-tangent of a number and returns the result as an array. If its argument is an array, the <tt>atan</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>atanh</bf><label id="atanh"><p><descrip> <tag> Synopsis </tag> Compute the inverse-tanh of an number <tag> Usage </tag> <tt>y = atanh (x)</tt> <tag> Description </tag> The <tt>atanh</tt> function computes the inverse-tanh of a number and returns the result as an array. If its argument is an array, the <tt>atanh</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>cos</bf><label id="cos"><p><descrip> <tag> Synopsis </tag> Compute the cosine of an number <tag> Usage </tag> <tt>y = cos (x)</tt> <tag> Description </tag> The <tt>cos</tt> function computes the cosine of a number and returns the result as an array. If its argument is an array, the <tt>cos</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>cosh</bf><label id="cosh"><p><descrip> <tag> Synopsis </tag> Compute the hyperbolic cosine of an number <tag> Usage </tag> <tt>y = cosh (x)</tt> <tag> Description </tag> The <tt>cosh</tt> function computes the hyperbolic cosine of a number and returns the result as an array. If its argument is an array, the <tt>cosh</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>exp</bf><label id="exp"><p><descrip> <tag> Synopsis </tag> Compute the exponential of an number <tag> Usage </tag> <tt>y = exp (x)</tt> <tag> Description </tag> The <tt>exp</tt> function computes the exponential of a number and returns the result as an array. If its argument is an array, the <tt>exp</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>log</bf><label id="log"><p><descrip> <tag> Synopsis </tag> Compute the logarithm of an number <tag> Usage </tag> <tt>y = log (x)</tt> <tag> Description </tag> The <tt>log</tt> function computes the logarithm of a number and returns the result as an array. If its argument is an array, the <tt>log</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>log10</bf><label id="log10"><p><descrip> <tag> Synopsis </tag> Compute the base-10 logarithm of an number <tag> Usage </tag> <tt>y = log10 (x)</tt> <tag> Description </tag> The <tt>log10</tt> function computes the base-10 logarithm of a number and returns the result as an array. If its argument is an array, the <tt>log10</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>mul2</bf><label id="mul2"><p><descrip> <tag> Synopsis </tag> Multiply a number by 2 <tag> Usage </tag> <tt>y = mul2(x)</tt> <tag> Description </tag> The <tt>mul2</tt> function multiplies an arithmetic type by two and returns the result. If its argument is an array, a new array will be created whose elements are obtained from the original array by using the <tt>mul2</tt> function. <tag> See Also </tag> <tt>sqr, abs</tt> </descrip><p> <sect><bf>polynom</bf><label id="polynom"><p><descrip> <tag> Synopsis </tag> Evaluate a polynomial <tag> Usage </tag> <tt>Double_Type polynom(Double_Type a, b, ...c, Integer_Type n, Double_Type x)</tt> <tag> Description </tag> The <tt>polynom</tt> function returns the value of the polynomial expression: <tscreen><verb> ax^n + bx^(n - 1) + ... c </verb></tscreen> <tag> Notes </tag> The <tt>polynom</tt> function should be extended to work with complex and array data types. The current implementation is limited to <tt>Double_Type</tt> quantities. <tag> See Also </tag> <tt>exp</tt> </descrip><p> <sect><bf>set_float_format</bf><label id="set_float_format"><p><descrip> <tag> Synopsis </tag> Set the format for printing floating point values. <tag> Usage </tag> <tt>set_float_format (String_Type fmt)</tt> <tag> Description </tag> The <tt>set_float_format</tt> function is used to set the floating point format to be used when floating point numbers are printed. The routines that use this are the traceback routines and the <tt>string</tt> function. The default value is <tt>"%f"</tt> <tag> Example </tag> <tscreen><verb> s = string (PI); % --> s = "3.14159" set_float_format ("%16.10f"); s = string (PI); % --> s = "3.1415926536" set_float_format ("%10.6e"); s = string (PI); % --> s = "3.141593e+00" </verb></tscreen> <tag> See Also </tag> <tt>string, sprintf, double</tt> </descrip><p> <sect><bf>sign</bf><label id="sign"><p><descrip> <tag> Synopsis </tag> Compute the sign of a number <tag> Usage </tag> <tt>y = sign(x)</tt> <tag> Description </tag> The <tt>sign</tt> function returns the sign of an arithmetic type. If its argument is a complex number (<tt>Complex_Type</tt>), it returns the sign of the imaginary part of the number. If the argument is an array, a new array will be created whose elements are obtained from the original array by using the <tt>sign</tt> function. When applied to a real number or an integer, the <tt>sign</tt> function returns <tt>-1</tt>, <tt>0</tt>, or <tt>+1</tt> according to whether the number is less than zero, equal to zero, or greater than zero, respectively. <tag> See Also </tag> <tt>abs</tt> </descrip><p> <sect><bf>sin</bf><label id="sin"><p><descrip> <tag> Synopsis </tag> Compute the sine of an number <tag> Usage </tag> <tt>y = sin (x)</tt> <tag> Description </tag> The <tt>sin</tt> function computes the sine of a number and returns the result as an array. If its argument is an array, the <tt>sin</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>sinh</bf><label id="sinh"><p><descrip> <tag> Synopsis </tag> Compute the hyperbolic sine of an number <tag> Usage </tag> <tt>y = sinh (x)</tt> <tag> Description </tag> The <tt>sinh</tt> function computes the hyperbolic sine of a number and returns the result as an array. If its argument is an array, the <tt>sinh</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>sqr</bf><label id="sqr"><p><descrip> <tag> Synopsis </tag> Compute the square of a number <tag> Usage </tag> <tt>y = sqr(x)</tt> <tag> Description </tag> The <tt>sqr</tt> function returns the square of an arithmetic type. If its argument is a complex number (<tt>Complex_Type</tt>), then it returns the square of the modulus. If the argument is an array, a new array will be created whose elements are obtained from the original array by using the <tt>sqr</tt> function. <tag> See Also </tag> <tt>abs, mul2</tt> </descrip><p> <sect><bf>sqrt</bf><label id="sqrt"><p><descrip> <tag> Synopsis </tag> Compute the square root of an number <tag> Usage </tag> <tt>y = sqrt (x)</tt> <tag> Description </tag> The <tt>sqrt</tt> function computes the square root of a number and returns the result as an array. If its argument is an array, the <tt>sqrt</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>sqr, cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>tan</bf><label id="tan"><p><descrip> <tag> Synopsis </tag> Compute the tangent of an number <tag> Usage </tag> <tt>y = tan (x)</tt> <tag> Description </tag> The <tt>tan</tt> function computes the tangent of a number and returns the result as an array. If its argument is an array, the <tt>tan</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <sect><bf>tanh</bf><label id="tanh"><p><descrip> <tag> Synopsis </tag> Compute the hyperbolic tangent of an number <tag> Usage </tag> <tt>y = tanh (x)</tt> <tag> Description </tag> The <tt>tanh</tt> function computes the hyperbolic tangent of a number and returns the result as an array. If its argument is an array, the <tt>tanh</tt> function will be applied to each element and the result returned as an array. <tag> See Also </tag> <tt>cos, atan, acosh, cosh</tt> </descrip><p> <chapt>Message and Error Functions<p> <sect><bf>error</bf><label id="error"><p><descrip> <tag> Synopsis </tag> Generate an error condition <tag> Usage </tag> <tt>error (String_Type msg</tt> <tag> Description </tag> The <tt>error</tt> function generates a <bf>S-lang</bf> error condition causing the interpreter to start unwinding to top-level. It takes a single string parameter which is displayed on the stderr output device. The error condition may be cleared via an <tt>ERROR_BLOCK</tt> with the <tt>_clear_error</tt> function. Consult <bf>A Guide to the S-Lang Language</bf> for more information. <tag> Example </tag> <tscreen><verb> define add_txt_extension (file) { if (typeof (file) != String_Type) error ("add_extension: parameter must be a string"); file += ".txt"; return file; } </verb></tscreen> <tag> See Also </tag> <tt>verror, _clear_error, message</tt> </descrip><p> <sect><bf>message</bf><label id="message"><p><descrip> <tag> Synopsis </tag> Print a string onto the message device <tag> Usage </tag> <tt>message (String_Type s</tt> <tag> Description </tag> The <tt>message</tt> function will print the string specified by <tt>s</tt> onto the message device. <tag> Example </tag> <tscreen><verb> define print_current_time () { message (time ()); } </verb></tscreen> <tag> Notes </tag> The message device will depend upon the application. For example, the output message device for the <tt>jed</tt> editor correspond to the line at the bottom of the display window. The default message device is the standard output device. <tag> See Also </tag> <tt>vmessage, sprintf, error</tt> </descrip><p> <sect><bf>usage</bf><label id="usage"><p><descrip> <tag> Synopsis </tag> Generate a usage error <tag> Usage </tag> <tt>usage (String_Type msg)</tt> <tag> Description </tag> The <tt>usage</tt> function generates a usage exception and displays <tt>msg</tt> to the message device. <tag> Example </tag> Suppose that some function <tt>plot</tt> plots an array of <tt>x</tt> and <tt>y</tt> values. The such a function could be written to issue a usage message if the wrong number of arguments were passed: <tscreen><verb> define plot () { variable x, y; if (_NARGS != 2) usage ("plot (x, y)"); (x, y) = (); % Now do the hard part . . } </verb></tscreen> <tag> See Also </tag> <tt>error, message</tt> </descrip><p> <sect><bf>verror</bf><label id="verror"><p><descrip> <tag> Synopsis </tag> Generate an error condition <tag> Usage </tag> <tt>verror (String_Type fmt, ...)</tt> <tag> Description </tag> The <tt>verror</tt> function performs the same role as the <tt>error</tt> function. The only difference is that instead of a single string argument, <tt>verror</tt> takes a sprintf style argument list. <tag> Example </tag> <tscreen><verb> define open_file (file) { variable fp; fp = fopen (file, "r"); if (fp == NULL) verror ("Unable to open %s", file); return fp; } </verb></tscreen> <tag> Notes </tag> In the current implementation, strictly speaking, the <tt>verror</tt> function is not an intrinsic function. Rather it is a predefined <bf>S-lang</bf> function using a combination of <tt>Sprintf</tt> and <tt>error</tt>. <tag> See Also </tag> <tt>error, Sprintf, vmessage</tt> </descrip><p> <sect><bf>vmessage</bf><label id="vmessage"><p><descrip> <tag> Synopsis </tag> Print a formatted string onto the message device <tag> Usage </tag> <tt>vmessage (String_Type fmt, ...)</tt> <tag> Description </tag> The <tt>vmessage</tt> function formats a sprintf style argument list and displays the resulting string onto the message device. <tag> Notes </tag> In the current implementation, strictly speaking, the <tt>vmessage</tt> function is not an intrinsic function. Rather it is a predefined <bf>S-lang</bf> function using a combination of <tt>Sprintf</tt> and <tt>message</tt>. <tag> See Also </tag> <tt>message, Sprintf, verror</tt> </descrip><p> <chapt>Time and Date Functions<p> <sect><bf>_time</bf><label id="_time"><p><descrip> <tag> Synopsis </tag> Get the current time in seconds <tag> Usage </tag> <tt>ULong_Type _time ()</tt> <tag> Description </tag> The <tt>_time</tt> function returns the number of elapsed seconds since 00:00:00 GMT, January 1, 1970. The <tt>ctime</tt> function may be used to convert this into a string representation. <tag> See Also </tag> <tt>ctime, time, localtime, gmtime</tt> </descrip><p> <sect><bf>ctime</bf><label id="ctime"><p><descrip> <tag> Synopsis </tag> Convert a calendar time to a string <tag> Usage </tag> <tt>String_Type ctime(ULong_Type secs)</tt> <tag> Description </tag> This function returns a string representation of the time as given by <tt>secs</tt> seconds since 1970. <tag> See Also </tag> <tt>time, _time, localtime, gmtime</tt> </descrip><p> <sect><bf>gmtime</bf><label id="gmtime"><p><descrip> <tag> Synopsis </tag> Break down a time in seconds to GMT timezone <tag> Usage </tag> <tt>Struct_Type gmtime (Long_Type secs)</tt> <tag> Description </tag> The <tt>gmtime</tt> function is exactly like <tt>localtime</tt> except that the values in the structure it returns are with respect to GMT instead of the local timezone. See the documentation for <tt>localtime</tt> for more information. <tag> Notes </tag> On systems that do not support the <tt>gmtime</tt> C library function, this function is the same as <tt>localtime</tt>. <tag> See Also </tag> <tt>localtime, _time</tt> </descrip><p> <sect><bf>localtime</bf><label id="localtime"><p><descrip> <tag> Synopsis </tag> Break down a time in seconds to local timezone <tag> Usage </tag> <tt>Struct_Type localtime (Long_Type secs)</tt> <tag> Description </tag> The <tt>localtime</tt> function takes a parameter <tt>secs</tt> representing the number of seconds since 00:00:00, January 1 1970 UTC and returns a structure containing information about <tt>secs</tt> in the local timezone. The structure contains the following <tt>Int_Type</tt> fields: <tt>tm_sec</tt> The number of seconds after the minute, normally in the range 0 to 59, but can be up to 61 to allow for leap seconds. <tt>tm_min</tt> The number of minutes after the hour, in the range 0 to 59. <tt>tm_hour</tt> The number of hours past midnight, in the range 0 to 23. <tt>tm_mday</tt> The day of the month, in the range 1 to 31. <tt>tm_mon</tt> The number of months since January, in the range 0 to 11. <tt>tm_year</tt> The number of years since 1900. <tt>tm_wday</tt> The number of days since Sunday, in the range 0 to 6. <tt>tm_yday</tt> The number of days since January 1, in the range 0 to 365. <tt>tm_isdst</tt> A flag that indicates whether daylight saving time is in effect at the time described. The value is positive if daylight saving time is in effect, zero if it is not, and negative if the information is not available. <tag> See Also </tag> <tt>gmtime, _time, ctime</tt> </descrip><p> <sect><bf>tic</bf><label id="tic"><p><descrip> <tag> Synopsis </tag> Start timing <tag> Usage </tag> <tt>void tic ()</tt> <tag> Description </tag> The <tt>tic</tt> function restarts the internal clock used for timing the execution of commands. To get the elapsed time of the clock, use the <tt>toc</tt> function. <tag> See Also </tag> <tt>toc, times</tt> </descrip><p> <sect><bf>time</bf><label id="time"><p><descrip> <tag> Synopsis </tag> Return the current data and time as a string <tag> Usage </tag> <tt>String_Type time ()</tt> <tag> Description </tag> This function returns the current time as a string of the form: <tscreen><verb> Sun Apr 21 13:34:17 1996 </verb></tscreen> <tag> See Also </tag> <tt>ctime, message, substr</tt> </descrip><p> <sect><bf>times</bf><label id="times"><p><descrip> <tag> Synopsis </tag> Get process times <tag> Usage </tag> <tt>Struct_Type times ()</tt> <tag> Description </tag> The <tt>times</tt> function returns a structure containing the following fields: <tscreen><verb> tms_utime (user time) tms_stime (system time) tms_cutime (user time of child processes) tms_cstime (system time of child processes) </verb></tscreen> <tag> Notes </tag> Not all systems support this function. <tag> See Also </tag> <tt>tic, toc, _times</tt> </descrip><p> <sect><bf>toc</bf><label id="toc"><p><descrip> <tag> Synopsis </tag> Get elapsed CPU time <tag> Usage </tag> <tt>Double_Type toc ()</tt> <tag> Description </tag> The <tt>toc</tt> function returns the elapsed CPU time in seconds since the last call to <tt>tic</tt>. The CPU time is the amount of time the CPU spent running the code of the current process. <tag> Example </tag> The <tt>tic</tt> and <tt>toc</tt> functions are ideal for timing the execution of the interpreter: <tscreen><verb> variable a = "hello", b = "world", c, n = 100000, t; tic (); loop (n) c = a + b; t = toc (); vmessage ("a+b took %f seconds\n", t); tic (); loop (n) c = strcat(a,b); t = toc (); vmessage ("strcat took %f seconds\n", t); </verb></tscreen> <tag> Notes </tag> This function may not be available on all systems. The implementation of this function is based upon the <tt>times</tt> system call. The precision of the clock is system dependent. <tag> See Also </tag> <tt>tic, times, _time</tt> </descrip><p> <chapt>Data-Type Conversion Functions<p> <sect><bf>_slang_guess_type</bf><label id="_slang_guess_type"><p><descrip> <tag> Synopsis </tag> Guess the data type that a string represents. <tag> Usage </tag> <tt>DataType_Type _slang_guess_type (String_Type s)</tt> <tag> Description </tag> This function tries to determine whether its argument <tt>s</tt> represents an integer or a floating point number. If it appears to be neither, then a string is assumed. It returns one of three values depending on the format of the string <tt>s</tt>: <tscreen><verb> Integer_Type : If it appears to be an integer Double_Type : If it appears to be a double String_Type : Anything else. </verb></tscreen> For example, <tt>_slang_guess_type("1e2")</tt> returns <tt>Double_Type</tt> but <tt>_slang_guess_type("e12")</tt> returns <tt>String_Type</tt>. <tag> See Also </tag> <tt>integer, string, double</tt> </descrip><p> <sect><bf>_typeof</bf><label id="_typeof"><p><descrip> <tag> Synopsis </tag> Get the data type of an object <tag> Usage </tag> <tt>DataType_Type _typeof (x)</tt> <tag> Description </tag> This function is similar to the <tt>typeof</tt> function except in the case of arrays. If the object <tt>x</tt> is an array, then the data type of the array will be returned. otherwise <tt>_typeof</tt> returns the data type of <tt>x</tt>. <tag> Example </tag> <tscreen><verb> if (Integer_Type == _typeof (x)) message ("x is an integer or an integer array"); </verb></tscreen> <tag> See Also </tag> <tt>typeof, array_info, _slang_guess_type, typecast</tt> </descrip><p> <sect><bf>atof</bf><label id="atof"><p><descrip> <tag> Synopsis </tag> Convert a string to a double precision number <tag> Usage </tag> <tt>Double_Type atof (String_Type s)</tt> <tag> Description </tag> This function converts a string <tt>s</tt> to a double precision value and returns the result. It performs no error checking on the format of the string. The function <tt>_slang_guess_type</tt> may be used to check the syntax of the string. <tag> Example </tag> <tscreen><verb> define error_checked_atof (s) { switch (_slang_guess_type (s)) { case Double_Type: return atof (s); } { case Integer_Type: return double (integer (s)); } verror ("%s is is not a double", s); } </verb></tscreen> <tag> See Also </tag> <tt>typecast, double, _slang_guess_type</tt> </descrip><p> <sect><bf>char</bf><label id="char"><p><descrip> <tag> Synopsis </tag> Convert an ascii value into a string <tag> Usage </tag> <tt>String_Type char (Integer_Type c)</tt> <tag> Description </tag> The <tt>char</tt> function converts an integer ascii value <tt>c</tt> to a string of unit length such that the first character of the string is <tt>c</tt>. For example, <tt>char('a')</tt> returns the string <tt>"a"</tt>. <tag> See Also </tag> <tt>integer, string, typedef</tt> </descrip><p> <sect><bf>define_case</bf><label id="define_case"><p><descrip> <tag> Synopsis </tag> Define upper-lower case conversion. <tag> Usage </tag> <tt>define_case (Integer_Type ch_up, Integer_Type ch_low);</tt> <tag> Description </tag> This function defines an upper and lowercase relationship between two characters specified by the arguments. This relationship is used by routines which perform uppercase and lowercase conversions. The first integer <tt>ch_up</tt> is the ascii value of the uppercase character and the second parameter <tt>ch_low</tt> is the ascii value of its lowercase counterpart. <tag> See Also </tag> <tt>strlow, strup</tt> </descrip><p> <sect><bf>double</bf><label id="double"><p><descrip> <tag> Synopsis </tag> Convert an object to double precision <tag> Usage </tag> <tt>result = double (x)</tt> <tag> Description </tag> The <tt>double</tt> function typecasts an object <tt>x</tt> to double precision. For example, if <tt>x</tt> is an array of integers, an array of double types will be returned. If an object cannot be converted to <tt>Double_Type</tt>, a type-mismatch error will result. <tag> Notes </tag> The <tt>double</tt> function is equivalent to the typecast operation <tscreen><verb> typecast (x, Double_Type) </verb></tscreen> To convert a string to a double precision number, use <tt>atoi</tt> function. <tag> See Also </tag> <tt>typecast, atoi, int</tt> </descrip><p> <sect><bf>int</bf><label id="int"><p><descrip> <tag> Synopsis </tag> Typecast an object to an integer <tag> Usage </tag> <tt>int (s)</tt> <tag> Description </tag> This function performs a typecast of <tt>s</tt> from its data type to an object of <tt>Integer_Type</tt>. If <tt>s</tt> is a string, it returns returns the ascii value value of the first character of the string <tt>s</tt>. If <tt>s</tt> is <tt>Double_Type</tt>, <tt>int</tt> truncates the number to an integer and returns it. <tag> Example </tag> <tt>int</tt> can be used to convert single character strings to integers. As an example, the intrinsic function <tt>isdigit</tt> may be defined as <tscreen><verb> define isdigit (s) { if ((int (s) >= '0') and (int (s) <= '9')) return 1; return 0; } </verb></tscreen> <tag> Notes </tag> This function is equalent to <tt>typecast (s, Integer_Type)</tt>; <tag> See Also </tag> <tt>typecast, double, integer, char, isdigit</tt> </descrip><p> <sect><bf>integer</bf><label id="integer"><p><descrip> <tag> Synopsis </tag> Convert a string to an integer <tag> Usage </tag> <tt>Integer_Type integer (String_Type s)</tt> <tag> Description </tag> The <tt>integer</tt> function converts a string representation of an integer back to an integer. If the string does not form a valid integer, a type-mismatch error will be generated. <tag> Example </tag> <tt>integer ("1234")</tt> returns the integer value <tt>1234</tt>. <tag> Notes </tag> This function operates only on strings and is not the same as the more general <tt>typecast</tt> operator. <tag> See Also </tag> <tt>typecast, _slang_guess_type, string, sprintf, char</tt> </descrip><p> <sect><bf>isdigit</bf><label id="isdigit"><p><descrip> <tag> Synopsis </tag> Tests for a decimal digit character <tag> Usage </tag> <tt>Integer_Type isdigit (String_Type s)</tt> <tag> Description </tag> This function returns a non-zero value if the first character in the string <tt>s</tt> is a digit; otherwise, it returns zero. <tag> Example </tag> A simple, user defined implementation of <tt>isdigit</tt> is <tscreen><verb> define isdigit (s) { return ((s[0] <= '9') and (s[0] >= '0')); } </verb></tscreen> However, the intrinsic function <tt>isdigit</tt> executes many times faster than the equivalent representation defined above. <tag> Notes </tag> Unlike the C function with the same name, the <bf>S-lang</bf> function takes a string argument. <tag> See Also </tag> <tt>int, integer</tt> </descrip><p> <sect><bf>string</bf><label id="string"><p><descrip> <tag> Synopsis </tag> Convert an object to a string representation. <tag> Usage </tag> <tt>Integer_Type string (obj)</tt> <tag> Description </tag> The <tt>string</tt> function may be used to convert an object <tt>obj</tt> of any type to a string representation. For example, <tt>string(12.34)</tt> returns <tt>"12.34"</tt>. <tag> Example </tag> <tscreen><verb> define print_anything (anything) { message (string (anything)); } </verb></tscreen> <tag> Notes </tag> This function is <em>not</em> the same as typecasting to a <tt>String_Type</tt> using the <tt>typecast</tt> function. <tag> See Also </tag> <tt>typecast, sprintf, integer, char</tt> </descrip><p> <sect><bf>tolower</bf><label id="tolower"><p><descrip> <tag> Synopsis </tag> Convert a character to lowercase. <tag> Usage </tag> <tt>Integer_Type lower (Integer_Type ch)</tt> <tag> Description </tag> This function takes an integer <tt>ch</tt> and returns its lowercase equivalent. <tag> See Also </tag> <tt>toupper, strup, strlow, int, char, define_case</tt> </descrip><p> <sect><bf>toupper</bf><label id="toupper"><p><descrip> <tag> Synopsis </tag> Convert a character to uppercase. <tag> Usage </tag> <tt>Integer_Type toupper (Integer_Type ch)</tt> <tag> Description </tag> This function takes an integer <tt>ch</tt> and returns its uppercase equivalent. <tag> See Also </tag> <tt>tolower, strup, strlow, int, char, define_case</tt> </descrip><p> <sect><bf>typecast</bf><label id="typecast"><p><descrip> <tag> Synopsis </tag> Convert an object from one data type to another. <tag> Usage </tag> <tt>typecast (x, new_type)</tt> <tag> Description </tag> The <tt>typecast</tt> function performs a generic typecast operation on <tt>x</tt> to convert it to <tt>new_type</tt>. If <tt>x</tt> represents an array, the function will attempt to convert all elements of <tt>x</tt> to <tt>new_type</tt>. Not all objects can be converted and a type-mismatch error will result upon failure. <tag> Example </tag> <tscreen><verb> define to_complex (x) { return typecast (x, Complex_Type); } </verb></tscreen> defines a function that converts its argument, <tt>x</tt> to a complex number. <tag> See Also </tag> <tt>int, double, typeof</tt> </descrip><p> <sect><bf>typeof</bf><label id="typeof"><p><descrip> <tag> Synopsis </tag> Get the data type of an object. <tag> Usage </tag> <tt>DataType_Type typeof (x)</tt> <tag> Description </tag> This function returns the data type of <tt>x</tt>. <tag> Example </tag> <tscreen><verb> if (Integer_Type == typeof (x)) message ("x is an integer"); </verb></tscreen> <tag> See Also </tag> <tt>_typeof, is_struct_type, array_info, _slang_guess_type, typecast</tt> </descrip><p> <chapt>Stdio File I/O Functions<p> <sect><bf>clearerr</bf><label id="clearerr"><p><descrip> <tag> Synopsis </tag> Clear the error of a file stream <tag> Usage </tag> <tt>clearerr (File_Type fp</tt> <tag> Description </tag> The <tt>clearerr</tt> function clears the error and end-of-file flags associated with the open file stream <tt>fp</tt>. <tag> See Also </tag> <tt>ferror, feof, fopen</tt> </descrip><p> <sect><bf>fclose</bf><label id="fclose"><p><descrip> <tag> Synopsis </tag> Close a file <tag> Usage </tag> <tt>Integer_Type fclose (File_Type fp)</tt> <tag> Description </tag> The <tt>fclose</tt> function may be used to close an open file pointer <tt>fp</tt>. Upon success it returns zero, and upon failure it sets <tt>errno</tt> and returns <tt>-1</tt>. Failure usually indicates a that the file system is full or that <tt>fp</tt> does not refer to an open file. <tag> Notes </tag> Many C programmers call <tt>fclose</tt> without checking the return value. The <bf>S-lang</bf> language requires the programmer to explicitly handle any value returned by a <bf>S-lang</bf> function. The simplest way to handle the return value from <tt>fclose</tt> is to use it as: <tscreen><verb> () = fclose (fp); </verb></tscreen> <tag> See Also </tag> <tt>fopen, fgets, fflush, pclose, errno</tt> </descrip><p> <sect><bf>fdopen</bf><label id="fdopen"><p><descrip> <tag> Synopsis </tag> Convert a FD_Type file descriptor to a stdio File_Type object <tag> Usage </tag> <tt>File_Type fdopen (FD_Type, String_Type mode)</tt> <tag> Description </tag> The <tt>fdopen</tt> function creates and returns a stdio <tt>File_Type</tt> object from the open <tt>FD_Type</tt> descriptor <tt>fd</tt>. The <tt>mode</tt> parameter corresponds to the <tt>mode</tt> parameter of the <tt>fopen</tt> function and must be consistent with the mode of the descriptor <tt>fd</tt>. The function returns <tt>NULL</tt> upon failure and sets <tt>errno</tt>. <tag> Notes </tag> The <tt>fclose</tt> function does not close the <tt>File_Type</tt> object returned from this function. The underlying file object must be closed by the <tt>close</tt> function. <tag> See Also </tag> <tt>fileno, fopen, open, close, fclose</tt> </descrip><p> <sect><bf>feof</bf><label id="feof"><p><descrip> <tag> Synopsis </tag> Get the end-of-file status <tag> Usage </tag> <tt>Integer_Type feof (File_Type fp)</tt> <tag> Description </tag> This function may be used to determine the state of the end-of-file indicator of the open file descriptor <tt>fp</tt>. It returns <tt>0</tt> if the indicator is not set, or non-zero if it is. The end-of-file indicator may be cleared by the <tt>clearerr</tt> function. <tag> See Also </tag> <tt>ferror, clearerr, fopen</tt> </descrip><p> <sect><bf>ferror</bf><label id="ferror"><p><descrip> <tag> Synopsis </tag> Determine the error status of an open file descriptor <tag> Usage </tag> <tt>Integer_Type ferror (File_Type fp)</tt> <tag> Description </tag> This function may be used to determine the state of the error indicator of the open file descriptor <tt>fp</tt>. It returns <tt>0</tt> if the indicator is not set, or non-zero if it is. The error indicator may be cleared by the <tt>clearerr</tt> function. <tag> See Also </tag> <tt>feof, clearerr, fopen</tt> </descrip><p> <sect><bf>fflush</bf><label id="fflush"><p><descrip> <tag> Synopsis </tag> Flush an output stream <tag> Usage </tag> <tt>Integer_Type fflush (File_Type fp)</tt> <tag> Description </tag> The <tt>fflush</tt> function may be used to update the <em>output</em> stream specified by <tt>fp</tt>. It returns <tt>0</tt> upon success, or <tt>-1</tt> upon failure and sets <tt>errno</tt> accordingly. In particular, this function will fail if <tt>fp</tt> does not represent an output stream, or if <tt>fp</tt> is associated with a disk file and there is insufficient disk space. <tag> Example </tag> This example illustrates how to use the <tt>fflush</tt> function without regard to the return value: <tscreen><verb> () = fputs ("Enter value> ", stdout); () = fflush (stdout); </verb></tscreen> <tag> Notes </tag> Many C programmers disregard the return value from the <tt>fflush</tt> function. The above example illustrates how to properly do this in the <bf>S-lang</bf> langauge. <tag> See Also </tag> <tt>fopen, fclose</tt> </descrip><p> <sect><bf>fgets</bf><label id="fgets"><p><descrip> <tag> Synopsis </tag> Read a line from a file. <tag> Usage </tag> <tt>Integer_Type fgets (SLang_Ref_Type ref, File_Type fp)</tt> <tag> Description </tag> <tt>fgets</tt> reads a line from the open file specified by <tt>fp</tt> and places the characters in the variable whose reference is specified by <tt>ref</tt>. It returns <tt>-1</tt> if <tt>fp</tt> is not associated with an open file or an attempt was made to read at the end the file; otherwise, it returns the number of characters read. <tag> Example </tag> The following example returns the lines of a file via a linked list: <tscreen><verb> define read_file (file) { variable buf, fp, root, tail; variable list_type = struct { text, next }; root = NULL; fp = fopen(file, "r"); if (fp == NULL) error("fopen %s failed." file); while (-1 != fgets (&buf, fp)) { if (root == NULL) { root = @list_type; tail = root; } else { tail.next = @list_type; tail = tail.next; } tail.text = buf; tail.next = NULL; } () = fclose (fp); return root; } </verb></tscreen> <tag> See Also </tag> <tt>fopen, fclose, fputs, fread, error</tt> </descrip><p> <sect><bf>fgetslines</bf><label id="fgetslines"><p><descrip> <tag> Synopsis </tag> Read all the lines from an open file <tag> Usage </tag> <tt>String_Type[] fgetslines (File_Type fp)</tt> <tag> Description </tag> The <tt>fgetslines</tt> function returns all the remaining lines as an array of strings in the file specified by the open file pointer <tt>fp</tt>. If the file is empty, an empty string array will be returned. The function returns <tt>NULL</tt> upon error. <tag> Example </tag> The following function returns the number of lines in a file: <tscreen><verb> define count_lines_in_file (file) { variable fp, lines; fp = fopen (file, "r"); if (fp == NULL) return -1; lines = fgetslines (fp); if (lines == NULL) return -1; return length (lines); } </verb></tscreen> Note that the file was implicitly closed by the function. <tag> Notes </tag> This function should not be used if the file contains many lines since that would require that all the lines be read into memory. <tag> See Also </tag> <tt>fgets, fread, fopen</tt> </descrip><p> <sect><bf>fopen</bf><label id="fopen"><p><descrip> <tag> Synopsis </tag> Open a file <tag> Usage </tag> <tt>File_Type fopen (String_Type f, String_Type m)</tt> <tag> Description </tag> The <tt>fopen</tt> function opens a file <tt>f</tt> according to the mode string <tt>m</tt>. Allowed values for <tt>m</tt> are: <tscreen><verb> "r" Read only "w" Write only "a" Append "r+" Reading and writing at the beginning of the file. "w+" Reading and writing. The file is created if it does not exist; otherwise, it is truncated. "a+" Reading and writing at the end of the file. The file is created if it does not already exist. </verb></tscreen> In addition, the mode string can also include the letter <tt>'b'</tt> as the last character to indicate that the file is to be opened in binary mode. Upon success, <tt>fopen</tt> a <tt>File_Type</tt> object which is meant to be used in other operations that require an open file. Upon failure, the function returns <tt>NULL</tt>. <tag> Example </tag> The following function opens a file in append mode and writes a string to it: <tscreen><verb> define append_string_to_file (file, str) { variable fp = fopen (file, "a"); if (fp == NULL) verror ("%s could not be opened", file); () = fputs (string, fp); () = fclose (fp); } </verb></tscreen> Note that the return values from <tt>fputs</tt> and <tt>fclose</tt> are ignored. <tag> Notes </tag> There is no need to explicitly close a file opened with <tt>fopen</tt>. If the returned <tt>File_Type</tt> object goes out of scope, <bf>S-lang</bf> will automatically close the file. However, explicitly closing a file after use is recommended. <tag> See Also </tag> <tt>fclose, fgets, fputs, popen</tt> </descrip><p> <sect><bf>fprintf</bf><label id="fprintf"><p><descrip> <tag> Synopsis </tag> Create and write a formatted string to a file <tag> Usage </tag> <tt>Int_Type fprintf (File_Type fp, String_Type fmt, ...)</tt> <tag> Description </tag> <tt>fprintf</tt> formats the objects specified by the variable argument list according to the format <tt>fmt</tt> and write the result to the open file pointer <tt>fp</tt>. The format string obeys the same syntax and semantics as the <tt>sprintf</tt> format string. See the description of the <tt>sprintf</tt> function for more information. <tt>fprintf</tt> returns the number of characters written to the file, or <tt>-1</tt> upon error. <tag> See Also </tag> <tt>fputs, printf, fwrite, message</tt> </descrip><p> <sect><bf>fputs</bf><label id="fputs"><p><descrip> <tag> Synopsis </tag> Write a string to an open stream <tag> Usage </tag> <tt>Integer_Type fputs (String_Type s, File_Type fp);</tt> <tag> Description </tag> The <tt>fputs</tt> function writes the string <tt>s</tt> to the open file pointer <tt>fp</tt>. It returns -1 upon failure and sets <tt>errno</tt>, otherwise it returns the length of the string. <tag> Example </tag> The following function opens a file in append mode and uses the <tt>fputs</tt> function to write to it. <tscreen><verb> define append_string_to_file (str, file) { variable fp; fp = fopen (file, "a"); if (fp == NULL) verror ("Unable to open %s", file); if ((-1 == fputs (s, fp)) or (-1 == fclose (fp))) verror ("Error writing to %s", file); } </verb></tscreen> <tag> Notes </tag> One must not disregard the return value from the <tt>fputs</tt> function, as many C programmers do. Doing so may lead to a stack overflow error. To write an object that contains embedded null characters, use the <tt>fwrite</tt> function. <tag> See Also </tag> <tt>fclose, fopen, fgets, fwrite</tt> </descrip><p> <sect><bf>fread</bf><label id="fread"><p><descrip> <tag> Synopsis </tag> Read binary data from a file <tag> Usage </tag> <tt>UInt_Type fread (Ref_Type b, DataType_Type t, UInt_Type n, File_Type fp)</tt> <tag> Description </tag> The <tt>fread</tt> function may be used to read <tt>n</tt> objects of type <tt>t</tt> from an open file pointer <tt>fp</tt>. Upon success, it returns the number of objects read from the file and places the objects in the variable specified by <tt>b</tt>. Upon error or end of file, it returns <tt>-1</tt>. If more than one object is read from the file, those objects will be placed in an array of the appropriate size. The exception to this is when reading <tt>Char_Type</tt> or <tt>UChar_Type</tt> objects from a file, in which case the data will be returned as a BString_Type binary string. <tag> Example </tag> The following example illustrates how to read 50 bytes from a file: <tscreen><verb> define read_50_bytes_from_file (file) { variable fp, n, buf; fp = fopen (file, "rb"); if (fp == NULL) error ("Open failed"); n = fread (&buf, Char_Type, 50, fp); if (n == -1) error ("fread failed"); () = fclose (fp); return buf; } </verb></tscreen> <tag> Notes </tag> Use the <tt>pack</tt> and <tt>unpack</tt> functions to read data with a specific byte-ordering. <tag> See Also </tag> <tt>fwrite, fgets, fopen, pack, unpack</tt> </descrip><p> <sect><bf>fseek</bf><label id="fseek"><p><descrip> <tag> Synopsis </tag> Reposition a stream <tag> Usage </tag> <tt>Integer_Type fseek (File_Type fp, Integer_Type ofs, Integer_Type whence</tt> <tag> Description </tag> The <tt>fseek</tt> function may be used to reposition the file position pointer associated with the open file stream <tt>fp</tt>. Specifically, it moves the pointer <tt>ofs</tt> bytes relative to the position indicated by <tt>whence</tt>. If whence is set to one of the symbolic constants <tt>SEEK_SET</tt>, <tt>SEEK_CUR</tt>, or <tt>SEEK_END</tt>, the offset is relative to the start of the file, the current position indicator, or end-of-file, respectively. The function return zero upon success, or <tt>-1</tt> upon failure and sets <tt>errno</tt> accordingly. <tag> Example </tag> define rewind (fp) { if (0 == fseek (fp, 0, SEEK_SET)) return; vmessage ("rewind failed, reason: %s", errno_string (errno)); } <tag> Notes </tag> The current implementation uses an integer to specify the offset. One some systems, a long integer may be required making this function fail for very large files, i.e., files that are longer than the maximum value of an integer. <tag> See Also </tag> <tt>ftell, fopen</tt> </descrip><p> <sect><bf>ftell</bf><label id="ftell"><p><descrip> <tag> Synopsis </tag> Obtain the current position in an open stream <tag> Usage </tag> <tt>Integer_Type ftell (File_Type fp)</tt> <tag> Description </tag> The ftell function may be used to obtain the current position in the stream associated with the open file pointer <tt>fp</tt>. It returns the position of the pointer measured in bytes from the beginning of the file. Upon error, it returns <tt>-1</tt> and sets <tt>errno</tt>. <tag> See Also </tag> <tt>fseek, fopen</tt> </descrip><p> <sect><bf>fwrite</bf><label id="fwrite"><p><descrip> <tag> Synopsis </tag> Write binary data to a file <tag> Usage </tag> <tt>UInt_Type fwrite (b, File_Type fp)</tt> <tag> Description </tag> The <tt>fwrite</tt> may be used to write the object represented by <tt>b</tt> to an open file. If <tt>b</tt> is a string or an array, the function will attempt to write all elements of the object to the file. It returns the number of objects successfully written, otherwise it returns <tt>-1</tt> upon error and sets <tt>errno</tt> accordingly. <tag> Example </tag> The following example illustrates how to write an integer array to a file. In this example, <tt>fp</tt> is an open file descriptor: <tscreen><verb> variable a = [1:50]; % 50 element integer array if (50 != fwrite (a, fp)) error ("fwrite failed"); </verb></tscreen> Here is how to write the array one element at a time: <tscreen><verb> variable a = [1:50]; foreach (a) { variable ai = (); if (1 != fwrite(ai, fp)) error ("fwrite failed"); } </verb></tscreen> <tag> Notes </tag> Not all data types may support the <tt>fwrite</tt> operation. However, it is supported by all vector, scalar, and string objects. <tag> See Also </tag> <tt>fread, fputs, fopen, pack, unpack</tt> </descrip><p> <sect><bf>pclose</bf><label id="pclose"><p><descrip> <tag> Synopsis </tag> Close an object opened with popen <tag> Usage </tag> <tt>Integer_Type pclose (File_Type fp)</tt> <tag> Description </tag> The <tt>pclose</tt> function waits for the process associated with <tt>fp</tt> to exit and the returns the exit status of the command. <tag> See Also </tag> <tt>pclose, fclose</tt> </descrip><p> <sect><bf>popen</bf><label id="popen"><p><descrip> <tag> Synopsis </tag> Open a process <tag> Usage </tag> <tt>File_Type popen (String_Type cmd, String_Type mode)</tt> <tag> Description </tag> The <tt>popen</tt> function executes a process specified by <tt>cmd</tt> and opens a unidirectional pipe to the newly created process. The <tt>mode</tt> indicates whether or not the the pipe is open for reading or writing. Specifically, if <tt>mode</tt> is <tt>"r"</tt>, then the pipe is opened for reading, or if <tt>mode</tt> is <tt>"w"</tt>, then the pipe will be open for writing. Upon success, a <tt>File_Type</tt> pointer will be returned, otherwise the function failed and <tt>NULL</tt> will be returned. <tag> Notes </tag> This function is not available on all systems. <tag> See Also </tag> <tt>pclose, fopen</tt> </descrip><p> <sect><bf>printf</bf><label id="printf"><p><descrip> <tag> Synopsis </tag> Create and write a formatted string to stdout <tag> Usage </tag> <tt>Int_Type printf (String_Type fmt, ...)</tt> <tag> Description </tag> <tt>fprintf</tt> formats the objects specified by the variable argument list according to the format <tt>fmt</tt> and write the result to <tt>stdout</tt>. This function is equivalent to <tt>fprintf</tt> used with the <tt>stdout</tt> file pointer. See <tt>fprintf</tt> for more information. <tt>printf</tt> returns the number of characters written to the file, or <tt>-1</tt> upon error. <tag> Notes </tag> Many C programmers do not check the return status of the <tt>printf</tt> C library function. Make sure that if you do not care about whether or not the function succeeds, then code it as in the following example: <tscreen><verb> () = printf ("%s laid %d eggs\n", chicken_name, num_egg); </verb></tscreen> <tag> See Also </tag> <tt>fputs, printf, fwrite, message</tt> </descrip><p> <chapt>Low-level POSIX I/O functions<p> <sect><bf>close</bf><label id="close"><p><descrip> <tag> Synopsis </tag> Close an open file descriptor <tag> Usage </tag> <tt>Int_Type close (FD_Type fd)</tt> <tag> Description </tag> The <tt>close</tt> function is used to open file descriptor of type <tt>FD_Type</tt>. Upon success <tt>0</tt> is returned, otherwise the function returns <tt>-1</tt> and sets <tt>errno</tt> accordingly. <tag> See Also </tag> <tt>open, fclose, read, write</tt> </descrip><p> <sect><bf>dup_fd</bf><label id="dup_fd"><p><descrip> <tag> Synopsis </tag> Duplicate a file descriptor <tag> Usage </tag> <tt>FD_Type dup_fd (FD_Type fd)</tt> <tag> Description </tag> The <tt>dup_fd</tt> function duplicates and file descriptor and returns its duplicate. If the function fails, <tt>NULL</tt> will be returned and <tt>errno</tt> set accordingly. <tag> Notes </tag> This function is essentually a wrapper around the POSIX <tt>dup</tt> function. <tag> See Also </tag> <tt>open, close</tt> </descrip><p> <sect><bf>fileno</bf><label id="fileno"><p><descrip> <tag> Synopsis </tag> Convert a stdio File_Type object to a FD_Type descriptor <tag> Usage </tag> <tt>FD_Type fileno (File_Type fp)</tt> <tag> Description </tag> The <tt>fileno</tt> function returns the <tt>FD_Type</tt> descriptor associated with the <tt>File_Type</tt> file pointer. Upon failure, <tt>NULL</tt> is returned. <tag> See Also </tag> <tt>fopen, open, fclose, close, dup_fd</tt> </descrip><p> <sect><bf>isatty</bf><label id="isatty"><p><descrip> <tag> Synopsis </tag> Determine if an open file descriptor refers to a terminal <tag> Usage </tag> <tt>Int_Type isatty (FD_Type or File_Type fd)</tt> <tag> Description </tag> This function returns <tt>1</tt> if the file descriptor <tt>fd</tt> refers to a terminal; otherwise it returns <tt>0</tt>. The object <tt>fd</tt> may either be a <tt>File_Type</tt> stdio descriptor or an <tt>FD_Type</tt> object. <tag> See Also </tag> <tt>fopen, fclose, fileno</tt> </descrip><p> <sect><bf>lseek</bf><label id="lseek"><p><descrip> <tag> Synopsis </tag> Reposition a file descriptor's file pointer <tag> Usage </tag> <tt>Long_Type lseek (FD_Type fd, Long_Type ofs, int mode)</tt> The <tt>lseek</tt> function repositions the file pointer associated with the open file descriptor <tt>fp</tt> to offset <tt>ofs</tt> according to the mode parameter. Specifically, <tt>mode</tt> must be one of the values: <tscreen><verb> SEEK_SET Set the offset to ofs SEEK_CUR Add ofs to the current offset SEEK_END Add ofs to the current file size </verb></tscreen> Upon error, <tt>lseek</tt> returns <tt>-1</tt> and sets <tt>errno</tt>. If successful, it returns the new filepointer offset. <tag> Notes </tag> Not all file descriptors are capable of supporting the seek operation, e.g., a descriptor associated with a pipe. By using <tt>SEEK_END</tt> with a positive value of the <tt>ofs</tt> parameter, it is possible to position the file pointer beyond the current size of the file. <tag> See Also </tag> <tt>fseek, ftell, open, close</tt> </descrip><p> <sect><bf>open</bf><label id="open"><p><descrip> <tag> Synopsis </tag> Open a file <tag> Usage </tag> <tt>FD_Type open (String_Type filename, Int_Type flags [,Int_Type mode])</tt> <tag> Description </tag> The <tt>open</tt> function attempts to open a file specified by the <tt>filename</tt> parameter according to the <tt>flags</tt> parameter, which must be one of the following values: <tscreen><verb> O_RDONLY (read-only) O_WRONLY (write-only) O_RDWR (read/write) </verb></tscreen> In addition, <tt>flags</tt> may also be bitwise-or'd with any of the following: <tscreen><verb> O_BINARY (open the file in binary mode) O_TEXT (open the file in text mode) O_CREAT (create file if it does not exists) O_EXCL (fail if the file already exists) O_NOCTTY (do not make the device the controlling terminal) O_TRUNC (truncate the file if it exists) O_APPEND (open the file in append mode) O_NONBLOCK (open the file in non-blocking mode) </verb></tscreen> If <tt>O_CREAT</tt> is used for the <tt>flags</tt> parameterm then the <tt>mode</tt> parameter must be present. <tt>mode</tt> specifies the permissions to use if a new file is created. The actual file permissions will be affected by the process's <tt>umask</tt> via <tt>mode&~umask</tt>. The <tt>mode</tt> parameter's value is constructed via bitwise-or of the following values: <tscreen><verb> S_IRWXU (Owner has read/write/execute permission) S_IRUSR (Owner has read permission) S_IWUSR (Owner has write permission) S_IXUSR (Owner has execute permission) S_IRWXG (Group has read/write/execute permission) S_IRGRP (Group has read permission) S_IWGRP (Group has write permission) S_IXGRP (Group has execute permission) S_IRWXO (Others have read/write/execute permission) S_IROTH (Others have read permission) S_IWOTH (Others have write permission) S_IXOTH (Others have execute permission) </verb></tscreen> Upon success <tt>open</tt> returns a file descriptor object (<tt>FD_Type</tt>), otherwise <tt>NULL</tt> is returned and <tt>errno</tt> is set. <tag> Notes </tag> If you are not familiar with the <tt>open</tt> system call, then it is recommended that you use <tt>fopen</tt> instead. <tag> See Also </tag> <tt>fopen, close, read, write, stat_file</tt> </descrip><p> <sect><bf>read</bf><label id="read"><p><descrip> <tag> Synopsis </tag> Read from an open file descriptor <tag> Usage </tag> <tt>UInt_Type read (FD_Type fd, Ref_Type buf, UInt_Type num)</tt> <tag> Description </tag> The <tt>read</tt> function attempts to read at most <tt>num</tt> bytes into the variable indicated by <tt>buf</tt> from the open file descriptor <tt>fd</tt>. It returns the number of bytes read, or <tt>-1</tt> and sets <tt>errno</tt> upon failure. The number of bytes read may be less than <tt>num</tt>, and will be zero if an attempt is made to read past the end of the file. <tag> Notes </tag> <tt>read</tt> is a low-level function and may return <tt>-1</tt> for a variety of reasons. For example, if non-blocking I/O has been specified for the open file descriptor and no data is available for reading then the function will return <tt>-1</tt> and set <tt>errno</tt> to <tt>EAGAIN</tt>. <tag> See Also </tag> <tt>fread, open, close, write</tt> </descrip><p> <sect><bf>write</bf><label id="write"><p><descrip> <tag> Synopsis </tag> Write to an open file descriptor <tag> Usage </tag> <tt>UInt_Type write (FD_Type fd, BString_Type buf</tt> <tag> Description </tag> The <tt>write</tt> function attempts to write the bytes specified by the <tt>buf</tt> parameter to the open file descriptor <tt>fd</tt>. It returns the number of bytes successfully written, or <tt>-1</tt> and sets <tt>errno</tt> upon failure. The number of bytes written may be less than <tt>length(buf)</tt>. <tag> See Also </tag> <tt>read, fwrite, open, close</tt> </descrip><p> <chapt>Directory Functions<p> <sect><bf>chdir</bf><label id="chdir"><p><descrip> <tag> Synopsis </tag> Change the current working directory. <tag> Usage </tag> <tt>Integer_Type chdir (String_Type dir)</tt> <tag> Description </tag> The <tt>chdir</tt> function may be used to changed the current working directory to the directory specified by <tt>dir</tt>. Upon sucess it returns zero; however, upon failure it returns <tt>-1</tt> and sets <tt>errno</tt> accordingly. <tag> See Also </tag> <tt>mkdir, stat_file</tt> </descrip><p> <sect><bf>chmod</bf><label id="chmod"><p><descrip> <tag> Synopsis </tag> Change the mode of a file <tag> Usage </tag> <tt>Integer_Type chmod (String_Type file, Integer_Type mode)</tt> <tag> Description </tag> The <tt>chmod</tt> function changes the permissions of <tt>file</tt> to those specified by <tt>mode</tt>. It returns <tt>0</tt> upon success, or <tt>-1</tt> upon failure setting <tt>errno</tt> accordingly. See the system specific documentation for the C library function <tt>chmod</tt> for a discussion of the <tt>mode</tt> parameter. <tag> See Also </tag> <tt>chown, stat_file</tt> </descrip><p> <sect><bf>chown</bf><label id="chown"><p><descrip> <tag> Synopsis </tag> Change the owner of a file <tag> Usage </tag> <tt>Integer_Type chown (String_Type file, Integer_Type uid, Integer_Type gid)</tt> <tag> Description </tag> The <tt>chown</tt> function is used to change the user-id and group-id of <tt>file</tt> to <tt>uid</tt> and <tt>gid</tt>, respectively. It returns <tt>zero</tt> upon success and <tt>-1</tt> upon failure, with <tt>errno</tt> set accordingly. <tag> Notes </tag> On most systems, only the super user can change the ownership of a file. Some systems do not support this function. <tag> See Also </tag> <tt>chmod, stat_file</tt> </descrip><p> <sect><bf>getcwd</bf><label id="getcwd"><p><descrip> <tag> Synopsis </tag> Get the current working directory <tag> Usage </tag> <tt>String_Type getcwd ()</tt> <tag> Description </tag> The <tt>getcwd</tt> function returns the absolute pathname of the current working directory. If an error occurs or it cannot determine the working directory, it returns <tt>NULL</tt> and sets <tt>errno</tt> accordingly. <tag> Notes </tag> Under Unix, OS/2, and MSDOS, the pathname returned by this function includes the trailing slash character. Some versions also include the drive specifier. <tag> See Also </tag> <tt>mkdir, chdir, errno</tt> </descrip><p> <sect><bf>listdir</bf><label id="listdir"><p><descrip> <tag> Synopsis </tag> Get a list of the files in a directory <tag> Usage </tag> <tt>String_Type[] listdir (String_Type dir)</tt> <tag> Description </tag> The <tt>listdir</tt> function returns the directory listing of all the files in the specified directory <tt>dir</tt> as an array of strings. It does not return the special files <tt>".."</tt> and <tt>"."</tt> as part of the list. <tag> See Also </tag> <tt>stat_file, stat_is, length</tt> </descrip><p> <sect><bf>lstat_file</bf><label id="lstat_file"><p><descrip> <tag> Synopsis </tag> Get information about a symbolic link <tag> Usage </tag> <tt>Struct_Type lstat_file (String_Type file)</tt> <tag> Description </tag> The <tt>lstat_file</tt> function behaves identically to <tt>stat_file</tt> but if <tt>file</tt> is a symbolic link, <tt>lstat_file</tt> returns information about the link itself, and not the file that it references. See the documentation for <tt>stat_file</tt> for more information. <tag> Notes </tag> On systems that do not support symbolic links, there is no difference between this function and the <tt>stat_file</tt> function. <tag> See Also </tag> <tt>stat_file, readlink</tt> </descrip><p> <sect><bf>mkdir</bf><label id="mkdir"><p><descrip> <tag> Synopsis </tag> Create a new directory <tag> Usage </tag> <tt>Integer_Type mkdir (String_Type dir, Integer_Type mode)</tt> <tag> Description </tag> The <tt>mkdir</tt> function creates a directory whose name is specified by the <tt>dir</tt> parameter with permissions specified by <tt>mode</tt>. Upon success <tt>mkdir</tt> returns zero, or it returns <tt>-1</tt> and sets <tt>errno</tt> accordingly. In particular, if the directory already exists, the function will fail and set errno to <tt>EEXIST</tt>. <tag> Example </tag> <tscreen><verb> define my_mkdir (dir) { if (0 == mkdir (dir, 0777)) return; if (errno == EEXIST) return; verror ("mkdir %s failed: %s", dir, errno_string (errno)); } </verb></tscreen> <tag> Notes </tag> The <tt>mode</tt> parameter may not be meaningful on all systems. On systems where it is meaningful, the actual permissions on the newly created directory are modified by the process's umask. <tag> See Also </tag> <tt>rmdir, getcwd, chdir, fopen, errno</tt> </descrip><p> <sect><bf>readlink</bf><label id="readlink"><p><descrip> <tag> Synopsis </tag> String_Type readlink (String_Type path) <tag> Usage </tag> <tt>Get the value of a symbolic link</tt> <tag> Description </tag> The <tt>readlink</tt> function returns the value of a symbolic link and returns it as a string. Upon failure, <tt>NULL</tt> is returned and <tt>errno</tt> set accordingly. <tag> Notes </tag> Not all systems support this function. <tag> See Also </tag> <tt>lstat_file, stat_file, stat_is</tt> </descrip><p> <sect><bf>remove</bf><label id="remove"><p><descrip> <tag> Synopsis </tag> Delete a file <tag> Usage </tag> <tt>Integer_Type remove (String_Type file)</tt> <tag> Description </tag> The <tt>remove</tt> function deletes a file. It returns <tt>0</tt> upon success, or <tt>-1</tt> upon error and sets <tt>errno</tt> accordingly. <tag> See Also </tag> <tt>rename, rmdir</tt> </descrip><p> <sect><bf>rename</bf><label id="rename"><p><descrip> <tag> Synopsis </tag> Rename a file <tag> Usage </tag> <tt>Integer_Type rename (String_Type old, String_Type new)</tt> <tag> Description </tag> The <tt>rename</tt> function renames a file from <tt>old</tt> to <tt>new</tt> moving it between directories if necessary. This function may fail if the directories do not refer to the same file system. It returns <tt>0</tt> upon success, or <tt>-1</tt> upon error and sets <tt>errno</tt> accordingly. <tag> See Also </tag> <tt>remove, errno</tt> </descrip><p> <sect><bf>rmdir</bf><label id="rmdir"><p><descrip> <tag> Synopsis </tag> Remove a directory <tag> Usage </tag> <tt>Integer_Type rmdir (String_Type dir)</tt> <tag> Description </tag> The <tt>rmdir</tt> function deletes a specified directory. It returns <tt>0</tt> upon success or <tt>-1</tt> upon error and sets <tt>errno</tt> accordingly. <tag> Notes </tag> The directory must be empty before it can be removed. <tag> See Also </tag> <tt>rename, remove, mkdir</tt> </descrip><p> <sect><bf>stat_file</bf><label id="stat_file"><p><descrip> <tag> Synopsis </tag> Get information about a file <tag> Usage </tag> <tt>Struct_Type stat_file (String_Type file)</tt> <tag> Description </tag> The <tt>stat_file</tt> function returns information about <tt>file</tt> through the use of the system <tt>stat</tt> call. If the stat call fails, the function returns <tt>NULL</tt> and sets errno accordingly. If it is successful, it returns a stat structure with the following integer fields: <tscreen><verb> st_dev st_ino st_mode st_nlink st_uid st_gid st_rdev st_size st_atime st_mtime st_ctime </verb></tscreen> See the man page for <tt>stat</tt> for a discussion of these fields. <tag> Example </tag> The following example shows how the <tt>stat_file</tt> function may be used to get the size of a file: <tscreen><verb> define file_size (file) { variable st; st = stat_file(file); if (st == NULL) verror ("Unable to stat %s", file); return st.st_size; } </verb></tscreen> <tag> See Also </tag> <tt>lstat_file, stat_is</tt> </descrip><p> <sect><bf>stat_is</bf><label id="stat_is"><p><descrip> <tag> Synopsis </tag> Parse the <tt>st_mode</tt> field of a stat structure <tag> Usage </tag> <tt>Char_Type stat_is (String_Type type, Integer_Type st_mode)</tt> <tag> Description </tag> The <tt>stat_is</tt> function returns a signed character value about the type of file specified by <tt>st_mode</tt>. Specifically, <tt>type</tt> must be one of the strings: <tscreen><verb> "sock" (socket) "fifo" (fifo) "blk" (block device) "chr" (character device) "reg" (regular file) "lnk" (link) "dir" (dir) </verb></tscreen> It returns a non-zero value if <tt>st_mode</tt> corresponds to <tt>type</tt>. <tag> Example </tag> The following example illustrates how to use the <tt>stat_is</tt> function to determine whether or not a file is a directory: <tscreen><verb> define is_directory (file) { variable st; st = stat_file (file); if (st == NULL) return 0; return stat_is ("dir", st.st_mode); } </verb></tscreen> <tag> See Also </tag> <tt>stat_file, lstat_file</tt> </descrip><p> <chapt>Functions that parse pathnames<p> <sect><bf>path_basename</bf><label id="path_basename"><p><descrip> <tag> Synopsis </tag> Get the basename part of a pathname <tag> Usage </tag> <tt>String_Type path_basename (String_Type path)</tt> <tag> Description </tag> The <tt>path_basename</tt> function returns the basename associated with the <tt>path</tt> parameter. The basename is the non-directory part of the filename, e.g., on unix <tt>c</tt> is the basename of <tt>/a/b/c</tt>. <tag> See Also </tag> <tt>path_dirname, path_extname, path_concat, path_is_absolute</tt> </descrip><p> <sect><bf>path_concat</bf><label id="path_concat"><p><descrip> <tag> Synopsis </tag> Combine elements of a pathname <tag> Usage </tag> <tt>String_Type path_concat (String_Type dir, String_Type basename)</tt> <tag> Description </tag> The <tt>path_concat</tt> function combines the arguments <tt>dir</tt> and <tt>basename</tt> to produce a pathname. For example, on unix is <tt>dir</tt> is <tt>x/y</tt> and <tt>basename</tt> is <tt>z</tt>, then the function will return <tt>x/y/z</tt>. <tag> See Also </tag> <tt>path_dirname, path_basename, path_extname, path_is_absolute</tt> </descrip><p> <sect><bf>path_dirname</bf><label id="path_dirname"><p><descrip> <tag> Synopsis </tag> Get the directory name part of a pathname <tag> Usage </tag> <tt>String_Type path_dirname (String_Type path)</tt> <tag> Description </tag> The <tt>path_dirname</tt> function returns the directory name associated with a specified pathname. <tag> Notes </tag> On systems that include a drive specifier as part of the pathname, the value returned by this function will include the driver specifier. <tag> See Also </tag> <tt>path_basename, path_extname, path_concat, path_is_absolute</tt> </descrip><p> <sect><bf>path_extname</bf><label id="path_extname"><p><descrip> <tag> Synopsis </tag> Return the extension part of a pathname <tag> Usage </tag> <tt>String_Type path_extname (String_Type path)</tt> <tag> Description </tag> The <tt>path_extname</tt> function returns the extension portion of a specified pathname. If an extension is present, this function will also include the dot as part of the extension, i.e., if <tt>path</tt> is <tt>file.c</tt>, then this function returns <tt>".c"</tt>. If no extension is present, the function returns an empty string <tt>""</tt>. <tag> Notes </tag> Under VMS, the file version number is not returned as part of the extension. <tag> See Also </tag> <tt>path_sans_extname, path_dirname, path_basename, path_concat, path_is_absolute</tt> </descrip><p> <sect><bf>path_is_absolute</bf><label id="path_is_absolute"><p><descrip> <tag> Synopsis </tag> Determine whether or not a pathname is absolute <tag> Usage </tag> <tt>Int_Type path_is_absolute (String_Type path)</tt> <tag> Description </tag> The <tt>path_is_absolute</tt> function will return non-zero is <tt>path</tt> refers to an absolute pathname, otherwise it returns zero. <tag> See Also </tag> <tt>path_dirname, path_basename, path_extname, path_concat</tt> </descrip><p> <sect><bf>path_sans_extname</bf><label id="path_sans_extname"><p><descrip> <tag> Synopsis </tag> Strip the extension from a pathname <tag> Usage </tag> <tt>String_Type path_sans_extname (String_Type path)</tt> <tag> Description </tag> The <tt>path_sans_extname</tt> function removes the file name extension (including the dot) from the path and returns the result. <tag> See Also </tag> <tt>path_extname, path_basename, path_dirname, path_concat</tt> </descrip><p> <chapt>System Call Functions<p> <sect><bf>errno</bf><label id="errno"><p><descrip> <tag> Synopsis </tag> Error code set by system functions. <tag> Usage </tag> <tt>Integer_Type errno</tt> <tag> Description </tag> A system function can fail for a variety of reasons. For example, a file operation may fail because lack of disk space, or the process does not have permission to perform the operation. Such functions will return <tt>-1</tt> and set the variable <tt>errno</tt> to an error code describing the reason for failure. Particular values of <tt>errno</tt> may be specified by the following symbolic constants (read-only variables) and the corresponding <tt>errno_string</tt> value: <tscreen><verb> EPERM "Not owner" ENOENT "No such file or directory" ESRCH "No such process" ENXIO "No such device or address" ENOEXEC "Exec format error" EBADF "Bad file number" ECHILD "No children" ENOMEM "Not enough core" EACCES "Permission denied" EFAULT "Bad address" ENOTBLK "Block device required" EBUSY "Mount device busy" EEXIST "File exists" EXDEV "Cross-device link" ENODEV "No such device" ENOTDIR "Not a directory" EISDIR "Is a directory" EINVAL "Invalid argument" ENFILE "File table overflow" EMFILE "Too many open files" ENOTTY "Not a typewriter" ETXTBSY "Text file busy" EFBIG "File too large" ENOSPC "No space left on device" ESPIPE "Illegal seek" EROFS "Read-only file system" EMLINK "Too many links" EPIPE "Broken pipe" ELOOP "Too many levels of symbolic links" ENAMETOOLONG "File name too long" </verb></tscreen> <tag> Example </tag> The <tt>mkdir</tt> function will attempt to create a directory. If that directory already exists, the function will fail and set <tt>errno</tt> to <tt>EEXIST</tt>. <tscreen><verb> define create_dir (dir) { if (0 == mkdir (dir)) return; if (errno != EEXIST) error ("mkdir %s failied: %s", dir, errno_string); } </verb></tscreen> <tag> See Also </tag> <tt>errno_string, error, mkdir</tt> </descrip><p> <sect><bf>errno_string</bf><label id="errno_string"><p><descrip> <tag> Synopsis </tag> Return a string describing an errno. <tag> Usage </tag> <tt>String_Type errno_string (Integer_Type err)</tt> <tag> Description </tag> The <tt>errno_string</tt> function returns a string describing the integer error code <tt>err</tt>. The variable <tt>err</tt> usually corresponds to the <tt>errno</tt> intrinsic function. See the description for <tt>errno</tt> for more information. <tag> Example </tag> The <tt>errno_string</tt> function may be used as follows: <tscreen><verb> define sizeof_file (file) { variable st = stat (file); if (st == NULL) verror ("%s: %s", file, errno_string (errno); return st.st_size; } </verb></tscreen> <tag> See Also </tag> <tt>errno, stat, verror</tt> </descrip><p> <sect><bf>getegid</bf><label id="getegid"><p><descrip> <tag> Synopsis </tag> Get the effective group id <tag> Usage </tag> <tt>Int_Type getegid ()</tt> <tag> Description </tag> The <tt>getegid</tt> function returns the effective group ID of the current process. <tag> Notes </tag> This function is not supported by all systems. <tag> See Also </tag> <tt>getgid, geteuid, setgid</tt> </descrip><p> <sect><bf>geteuid</bf><label id="geteuid"><p><descrip> <tag> Synopsis </tag> Get the effective user-id of the current process <tag> Usage </tag> <tt>Int_Type geteuid ()</tt> <tag> Description </tag> The <tt>geteuid</tt> function returns the effective user-id of the current process. <tag> Notes </tag> This function is not supported by all systems. <tag> See Also </tag> <tt>getuid, setuid, setgid</tt> </descrip><p> <sect><bf>getgid</bf><label id="getgid"><p><descrip> <tag> Synopsis </tag> Get the group id <tag> Usage </tag> <tt>Integer_Type getgid ()</tt> <tag> Description </tag> The <tt>getgid</tt> function returns the real group id of the current process. <tag> Notes </tag> This function is not supported by all systems. <tag> See Also </tag> <tt>getpid, getppid</tt> </descrip><p> <sect><bf>getpid</bf><label id="getpid"><p><descrip> <tag> Synopsis </tag> Get the current process id <tag> Usage </tag> <tt>Integer_Type getpid ()</tt> <tag> Description </tag> The <tt>getpid</tt> function returns the current process identification number. <tag> See Also </tag> <tt>getppid, getgid</tt> </descrip><p> <sect><bf>getppid</bf><label id="getppid"><p><descrip> <tag> Synopsis </tag> Get the parent process id <tag> Usage </tag> <tt>Integer_Type getppid ()</tt> <tag> Description </tag> The <tt>getpid</tt> function returns the process identification number of the parent process. <tag> Notes </tag> This function is not supported by all systems. <tag> See Also </tag> <tt>getpid, getgid</tt> </descrip><p> <sect><bf>getuid</bf><label id="getuid"><p><descrip> <tag> Synopsis </tag> Get the user-id of the current process <tag> Usage </tag> <tt>Int_Type getuid ()</tt> <tag> Description </tag> The <tt>getuid</tt> function returns the user-id of the current process. <tag> Notes </tag> This function is not supported by all systems. <tag> See Also </tag> <tt>getuid, getegid</tt> </descrip><p> <sect><bf>kill</bf><label id="kill"><p><descrip> <tag> Synopsis </tag> Send a signal to a process <tag> Usage </tag> <tt>Integer_Type kill (Integer_Type pid, Integer_Type sig)</tt> <tag> Description </tag> This function may be used to send a signal given by the integer <tt>sig</tt> to the process specified by <tt>pid</tt>. The function returns zero upon sucess and <tt>-1</tt> upon failure setting errno accordingly. <tag> Example </tag> The <tt>kill</tt> function may be used to determine whether or not a specific process exists: <tscreen><verb> define process_exists (pid) { if (-1 == kill (pid, 0)) return 0; % Process does not exist return 1; } </verb></tscreen> <tag> Notes </tag> This function is not supported by all systems. <tag> See Also </tag> <tt>getpid</tt> </descrip><p> <sect><bf>mkfifo</bf><label id="mkfifo"><p><descrip> <tag> Synopsis </tag> Create a named pipe <tag> Usage </tag> <tt>Int_Type mkfifo (String_Type name, Int_Type mode)</tt> <tag> Description </tag> The <tt>mkfifo</tt> attempts to create a named pipe with the specified name and mode (modified by the process's umask). The function returns <tt>0</tt> upon success, or <tt>-1</tt> and sets <tt>errno</tt> upon failure. <tag> Notes </tag> Not all systems support the <tt>mkfifo</tt> function and even on systems that do implement the <tt>mkfifo</tt> system call, the underlying file system may not support the concept of a named pipe, e.g, an NFS filesystem. <tag> See Also </tag> <tt>stat_file</tt> </descrip><p> <sect><bf>setgid</bf><label id="setgid"><p><descrip> <tag> Synopsis </tag> Set the group-id of the current process <tag> Usage </tag> <tt>Int_Type setgid (Int_Type gid)</tt> <tag> Description </tag> The <tt>setgid</tt> function sets the effective group-id of the current process. It returns zero upon success, or <tt>-1</tt> upon error and sets <tt>errno</tt> appropriately. <tag> Notes </tag> This function is not supported by all systems. <tag> See Also </tag> <tt>getgid, setuid</tt> </descrip><p> <sect><bf>setpgid</bf><label id="setpgid"><p><descrip> <tag> Synopsis </tag> Set the process group-id <tag> Usage </tag> <tt>Int_Type setpgid (Int_Type pid, Int_Type gid)</tt> <tag> Description </tag> The <tt>setpgid</tt> function sets the group-id <tt>gid</tt> of the process whose process-id is <tt>pid</tt>. If <tt>pid</tt> is <tt>0</tt>, then the current process-id will be used. If <tt>pgid</tt> is <tt>0</tt>, then the pid of the affected process will be used. If successful zero will be returned, otherwise the function will return <tt>-1</tt> and set <tt>errno</tt> accordingly. <tag> Notes </tag> This function is not supported by all systems. <tag> See Also </tag> <tt>setgid, setuid</tt> </descrip><p> <sect><bf>setuid</bf><label id="setuid"><p><descrip> <tag> Synopsis </tag> Set the user-id of the current process <tag> Usage </tag> <tt>Int_Type setuid (Int_Type id)</tt> <tag> Description </tag> The <tt>setuid</tt> function sets the effective user-id of the current process. It returns zero upon success, or <tt>-1</tt> upon error and sets <tt>errno</tt> appropriately. <tag> Notes </tag> This function is not supported by all systems. <tag> See Also </tag> <tt>setgid, setpgid, getuid, geteuid</tt> </descrip><p> <sect><bf>sleep</bf><label id="sleep"><p><descrip> <tag> Synopsis </tag> Pause for a specified number of seconds <tag> Usage </tag> <tt>sleep (Double_Type n)</tt> <tag> Description </tag> The <tt>sleep</tt> function delays the current process for the specified number of seconds. If it is interrupted by a signal, it will return prematurely. <tag> Notes </tag> Not all system support sleeping for a fractional part of a second. </descrip><p> <sect><bf>system</bf><label id="system"><p><descrip> <tag> Synopsis </tag> Execute a shell command <tag> Usage </tag> <tt>Integer_Type system (String_Type cmd)</tt> <tag> Description </tag> The <tt>system</tt> function may be used to execute the string expression <tt>cmd</tt> in an inferior shell. This function is an interface to the C <tt>system</tt> function which returns an implementation-defined result. On Linux, it returns 127 if the inferior shell could not be invoked, -1 if there was some other error, otherwise it returns the return code for <tt>cmd</tt>. <tag> Example </tag> <tscreen><verb> define dir () { () = system ("DIR"); } </verb></tscreen> displays a directory listing of the current directory under MSDOS or VMS. <tag> See Also </tag> <tt>popen, listdir</tt> </descrip><p> <sect><bf>umask</bf><label id="umask"><p><descrip> <tag> Synopsis </tag> Set the file creation mask <tag> Usage </tag> <tt>Int_Type umask (Int_Type m)</tt> <tag> Description </tag> The <tt>umask</tt> function sets the file creation mask to <tt>m</tt> and returns the previous mask. <tag> See Also </tag> <tt>stat_file</tt> </descrip><p> <sect><bf>uname</bf><label id="uname"><p><descrip> <tag> Synopsis </tag> Get the system name <tag> Usage </tag> <tt>Struct_Tye uname ()</tt> <tag> Description </tag> The <tt>uname</tt> function returns a structure containing information about the operating system. The structure contains the following fields: <tscreen><verb> sysname (Name of the operating system) nodename (Name of the node within the network) release (Release level of the OS) version (Current version of the release) machine (Name of the hardware) </verb></tscreen> <tag> Notes </tag> Not all systems support this function. <tag> See Also </tag> <tt>getenv, pack, unpack</tt> </descrip><p> <chapt>Eval Functions<p> <sect><bf>autoload</bf><label id="autoload"><p><descrip> <tag> Synopsis </tag> Load a function from a file <tag> Usage </tag> <tt>autoload (String_Type funct, String_Type file)</tt> <tag> Description </tag> The <tt>autoload</tt> function is used to declare <tt>funct</tt> to the interpreter and indicate that it should be loaded from <tt>file</tt> when it is actually used. <tag> Example </tag> Suppose <tt>bessel_j0</tt> is a function defined in the file <tt>bessel.sl</tt>. Then the statement <tscreen><verb> autoload ("bessel_j0", "bessel.sl"); </verb></tscreen> will cause <tt>bessel.sl</tt> to be loaded prior to the execution of <tt>bessel_j0</tt> <tag> See Also </tag> <tt>evalfile</tt> </descrip><p> <sect><bf>byte_compile_file</bf><label id="byte_compile_file"><p><descrip> <tag> Synopsis </tag> Compile a file to byte-code for faster loading. <tag> Usage </tag> <tt>byte_compile_file (String_Type file, Integer_Type method)</tt> <tag> Description </tag> The <tt>byte_compile_file</tt> function byte-compiles <tt>file</tt> producing a new file with the same name except a <tt>'c'</tt> is added to the output file name. For example, <tt>file</tt> is <tt>"site.sl"</tt>, then the function produces a new file named <tt>site.slc</tt>. <tag> Notes </tag> The <tt>method</tt> parameter is not used in the current implementation. Its use is reserved for the future. For now, set it to <tt>0</tt>. <tag> See Also </tag> <tt>evalfile</tt> </descrip><p> <sect><bf>eval</bf><label id="eval"><p><descrip> <tag> Synopsis </tag> Interpret a string as <bf>S-lang</bf> code <tag> Usage </tag> <tt>eval (String_Type expression)</tt> <tag> Description </tag> The <tt>eval</tt> function parses a string as S-Lang code and executes the result. This is a useful function in many contexts such as dynamically generating function definitions where there is no way to generate them otherwise. <tag> Example </tag> <tscreen><verb> if (0 == is_defined ("my_function")) eval ("define my_function () { message (\"my_function\"); }"); </verb></tscreen> <tag> See Also </tag> <tt>is_defined, autoload, evalfile</tt> </descrip><p> <sect><bf>evalfile</bf><label id="evalfile"><p><descrip> <tag> Synopsis </tag> Interpret a file containing <bf>S-lang</bf> code. <tag> Usage </tag> <tt>Integer_Type evalfile (String_Type file)</tt> <tag> Description </tag> The <tt>evalfile</tt> function loads <tt>file</tt> into the interpreter. If no errors were encountered, <tt>1</tt> will be returned; otherwise, a <bf>S-lang</bf> error will be generated and the function will return zero. <tag> Example </tag> <tscreen><verb> define load_file (file) { ERROR_BLOCK { _clear_error (); } () = evalfile (file); } </verb></tscreen> <tag> See Also </tag> <tt>eval, autoload</tt> </descrip><p> <chapt>Module Functions<p> <sect><bf>get_import_module_path</bf><label id="get_import_module_path"><p><descrip> <tag> Synopsis </tag> Get the search path for dynamically loadable objects <tag> Usage </tag> <tt>String_Type get_import_module_path ()</tt> <tag> Description </tag> The <tt>get_import_module_path</tt> may be used to get the search path for dynamically shared objects. Such objects may be made accessable to the application via the <tt>import</tt> function. <tag> See Also </tag> <tt>import, set_import_module_path</tt> </descrip><p> <sect><bf>import</bf><label id="import"><p><descrip> <tag> Synopsis </tag> Dynamically link to a specified module <tag> Usage </tag> <tt>import (String_Type module [, String_Type namespace])</tt> <tag> Description </tag> The <tt>import</tt> function causes the run-time linker to dynamically link to the shared object specified by the <tt>module</tt> parameter. It seaches for the shared object as follows: First a search is performed along all module paths specified by the application. Then a search is made along the paths defined via the <tt>set_import_module_path</tt> function. If not found, a search is performed along the paths given by the <tt>SLANG_MODULE_PATH</tt> environment variable. Finally, a system dependent search is performed (e.g., using the <tt>LD_LIBRARY_PATH</tt> environment variable). The optional second parameter may be used to specify a namespace for the intrinsic functions and variables of the module. If this parameter is not present, the intrinsic objects will be placed into the global namespace. This function signals an error if the specified module is not found. <tag> Notes </tag> The <tt>import</tt> function is not available on all systems. <tag> See Also </tag> <tt>set_import_module_path, use_namespace, current_namespace, getenv, evalfile</tt> </descrip><p> <sect><bf>set_import_module_path</bf><label id="set_import_module_path"><p><descrip> <tag> Synopsis </tag> Set the search path for dynamically loadable objects <tag> Usage </tag> <tt>set_import_module_path (String_Type path_list)</tt> <tag> Description </tag> The <tt>set_import_module_path</tt> may be used to set the search path for dynamically shared objects. Such objects may be made accessable to the application via the <tt>import</tt> function. The actual syntax for the specification of the set of paths will vary according to the operating system. Under Unix, a colon character is used to separate paths in <tt>path_list</tt>. For win32 systems a semi-colon is used. <tag> See Also </tag> <tt>import, get_import_module_path</tt> </descrip><p> <chapt>Debugging Functions<p> <sect><bf>_clear_error</bf><label id="_clear_error"><p><descrip> <tag> Synopsis </tag> Clear an error condition <tag> Usage </tag> <tt>_clear_error ()</tt> <tag> Description </tag> This function may be used in error-blocks to clear the error that triggered execution of the error block. Execution resumes following the statement, in the scope of the error-block, that triggered the error. <tag> Example </tag> Consider the following wrapper around the <tt>putenv</tt> function: <tscreen><verb> define try_putenv (name, value) { variable status; ERROR_BLOCK { _clear_error (); status = -1; } status = 0; putenv (sprintf ("%s=%s", name, value); return status; } </verb></tscreen> If <tt>putenv</tt> fails, it generates an error condition, which the <tt>try_putenv</tt> function catches and clears. Thus <tt>try_putenv</tt> is a function that returns <tt>-1</tt> upon failure and <tt>0</tt> upon success. <tag> See Also </tag> <tt>_trace_function, _slangtrace, _traceback</tt> </descrip><p> <sect><bf>_debug_info</bf><label id="_debug_info"><p><descrip> <tag> Synopsis </tag> Configure debugging information <tag> Usage </tag> <tt>Integer_Type _debug_info</tt> <tag> Description </tag> The <tt>_debug_info</tt> variable controls whether or not extra code should be generated for additional debugging and traceback information. Currently, if <tt>_debug_info</tt> is zero, no extra code will be generated; otherwise extra code will be inserted into the compiled bytecode for additional debugging data. The value of this variable is is local to each compilation unit and setting its value in one unit has no effect upon its value in other units. <tag> Example </tag> <tscreen><verb> _debug_info = 1; % Enable debugging information </verb></tscreen> <tag> Notes </tag> Setting this variable to a non-zero value may slow down the interpreter somewhat. <tag> See Also </tag> <tt>_traceback, _slangtrace</tt> </descrip><p> <sect><bf>_slangtrace</bf><label id="_slangtrace"><p><descrip> <tag> Synopsis </tag> Turn function tracing on or off. <tag> Usage </tag> <tt>Integer_Type _slangtrace</tt> <tag> Description </tag> The <tt>_slangtrace</tt> variable is a debugging aid that when set to a non-zero value enables tracing when function declared by <tt>_trace_function</tt> is entered. If the value is greater than zero, both intrinsic and user defined functions will get traced. However, if set to a value less than zero, intrinsic functions will not get traced. <tag> See Also </tag> <tt>_trace_function, _traceback, _print_stack</tt> </descrip><p> <sect><bf>_trace_function</bf><label id="_trace_function"><p><descrip> <tag> Synopsis </tag> Set the function to trace <tag> Usage </tag> <tt>_trace_function (String_Type f)</tt> <tag> Description </tag> <tt>_trace_function</tt> declares that the <bf>S-lang</bf> function with name <tt>f</tt> is to be traced when it is called. Calling <tt>_trace_function</tt> does not in itself turn tracing on. Tracing is turned on only when the variable <tt>_slangtrace</tt> is non-zero. <tag> See Also </tag> <tt>_slangtrace, _traceback</tt> </descrip><p> <sect><bf>_traceback</bf><label id="_traceback"><p><descrip> <tag> Synopsis </tag> Generate a traceback upon error <tag> Usage </tag> <tt>Integer_Type _traceback</tt> <tag> Description </tag> <tt>_traceback</tt> is an intrinsic integer variable whose value controls whether or not a traceback of the call stack is to be generated upon error. If <tt>_traceback</tt> is greater than zero, a full traceback will be generated, which includes the values of local variables. If the value is less than zero, a traceback will be generated without local variable information, and if <tt>_traceback</tt> is zero the traceback will not be generated. Local variables are represented in the form <tt>$n</tt> where <tt>n</tt> is an integer numbered from zero. More explicitly, <tt>$0</tt> represents the first local variable, <tt>$1</tt> represents the second, and so on. Please note that function parameters are local variables and that the first parameter corresponds to <tt>$0</tt>. <tag> See Also </tag> <tt>_slangtrace, error</tt> </descrip><p> <chapt>Stack Functions<p> <sect><bf>__pop_args</bf><label id="__pop_args"><p><descrip> <tag> Synopsis </tag> Remove n function arguments from the stack <tag> Usage </tag> <tt>variable args = __pop_args(Integer_Type n);</tt> <tag> Description </tag> This function together with the companion function <tt>__push_args</tt> is useful for passing the arguments of a function to another function. <tt>__pop_args</tt> returns an array of <tt>n</tt> structures with a single structure field called <tt>value</tt>, which represents the value of the argument. <tag> Example </tag> Consider the following <tt>print</tt> function. It prints all its arguments to <tt>stdout</tt> separated by spaces: <tscreen><verb> define print () { variable i; variable args = __pop_args (_NARGS); for (i = 0; i < _NARGS; i++) { () = fputs (string (args[i].value), stdout); () = fputs (" ", stdout); } () = fputs ("\n", stdout); () = fflush (stdout); } </verb></tscreen> Now consider the problem of defining a function called <tt>ones</tt> that returns a multi-dimensional array with all the elements set to 1. For example, <tt>ones(10)</tt> should return a 1-d array of ones, whereas <tt>ones(10,20)</tt> should return a 10x20 array. <tscreen><verb> define ones () { !if (_NARGS) return 1; variable a; a = __pop_args (_NARGS); return @Array_Type (Integer_Type, [__push_args (a)]) + 1; } </verb></tscreen> Here, <tt>__push_args</tt> was used to push on the arguments passed to the <tt>ones</tt> function onto the stack to be used when dereferencing <tt>Array_Type</tt>. <tag> See Also </tag> <tt>__push_args, typeof, _pop_n</tt> </descrip><p> <sect><bf>__push_args</bf><label id="__push_args"><p><descrip> <tag> Synopsis </tag> Remove n function arguments onto the stack <tag> Usage </tag> <tt>__push_args (Struct_Type args);</tt> <tag> Description </tag> This function together with the companion function <tt>__pop_args</tt> is useful for passing the arguments of one function to another. See the desription of <tt>__pop_args</tt> for more information. <tag> See Also </tag> <tt>__pop_args, typeof, _pop_n</tt> </descrip><p> <sect><bf>_pop_n</bf><label id="_pop_n"><p><descrip> <tag> Synopsis </tag> Remove objects from the stack <tag> Usage </tag> <tt>_pop_n (Integer_Type n);</tt> <tag> Description </tag> The <tt>_pop_n</tt> function pops <tt>n</tt> objects from the top of the stack. <tag> Example </tag> <tscreen><verb> define add3 () { variable x, y, z; if (_NARGS != 3) { _pop_n (_NARGS); error ("add3: Expecting 3 arguments"); } (x, y, z) = (); return x + y + z; } </verb></tscreen> <tag> See Also </tag> <tt>_stkdepth, pop</tt> </descrip><p> <sect><bf>_print_stack</bf><label id="_print_stack"><p><descrip> <tag> Synopsis </tag> print the values on the stack. <tag> Usage </tag> <tt>_print_stack ()</tt> <tag> Description </tag> This function dumps out what is currently on the <bf>S-lang</bf>. It does not alter the stack and it is usually used for debugging purposes. <tag> See Also </tag> <tt>_stkdepth, string</tt> </descrip><p> <sect><bf>_stk_reverse</bf><label id="_stk_reverse"><p><descrip> <tag> Synopsis </tag> Reverse the order of the objects on the stack. <tag> Usage </tag> <tt>_stk_reverse (Integer_Type n)</tt> <tag> Description </tag> The <tt>_stk_reverse</tt> function reverses the order of the top <tt>n</tt> items on the stack. <tag> See Also </tag> <tt>_stkdepth, _stk_roll</tt> </descrip><p> <sect><bf>_stk_roll</bf><label id="_stk_roll"><p><descrip> <tag> Synopsis </tag> Roll items on the stack <tag> Usage </tag> <tt>_stk_roll (Integer_Type n);</tt> <tag> Description </tag> This function may be used to alter the arrangement of objects on the stack. Specifically, if the integer <tt>n</tt> is positive, the top <tt>n</tt> items on the stack are rotated up. If <tt>n</tt> is negative, the top <tt>abs(n)</tt> items on the stack are rotated down. <tag> Example </tag> If the stack looks like: <tscreen><verb> item-0 item-1 item-2 item-3 </verb></tscreen> where <tt>item-0</tt> is at the top of the stack, then <tt>_stk_roll(-3)</tt> will change the stack to: <tscreen><verb> item-2 item-0 item-1 item-3 </verb></tscreen> <tag> Notes </tag> This function only has an effect for <tt>abs(n) > 1</tt>. <tag> See Also </tag> <tt>_stkdepth, _stk_reverse, _pop_n, _print_stack</tt> </descrip><p> <sect><bf>_stkdepth</bf><label id="_stkdepth"><p><descrip> <tag> Usage </tag> <tt>Get the number of objects currently on the stack.</tt> <tag> Synopsis </tag> Integer_Type _stkdepth () <tag> Description </tag> The <tt>_stkdepth</tt> function returns number of items on stack prior to the call of <tt>_stkdepth</tt>. <tag> See Also </tag> <tt>_print_stack, _stk_reverse, _stk_roll</tt> </descrip><p> <sect><bf>dup</bf><label id="dup"><p><descrip> <tag> Synopsis </tag> Duplicate the value at the top of the stack <tag> Usage </tag> <tt>dup ()</tt> <tag> Description </tag> This function returns an exact duplicate of the object on top of the stack. For some objects such as arrays or structures, it creates a new reference to the array. However, for simple scalar S-Lang types such as strings, integers, and doubles, it creates a new copy of the object. <tag> See Also </tag> <tt>pop, typeof</tt> </descrip><p> <sect><bf>exch</bf><label id="exch"><p><descrip> <tag> Synopsis </tag> Exchange two items on the stack <tag> Usage </tag> <tt>exch ()</tt> <tag> Description </tag> The <tt>exch</tt> swaps the two top items on the stack. <tag> See Also </tag> <tt>pop, _stk_reverse, _stk_roll</tt> </descrip><p> <sect><bf>pop</bf><label id="pop"><p><descrip> <tag> Synopsis </tag> Discard an item from the stack <tag> Usage </tag> <tt>pop ()</tt> <tag> Description </tag> The <tt>pop</tt> function removes the top item from the stack. <tag> See Also </tag> <tt>_pop_n</tt> </descrip><p> <chapt>Miscellaneous Functions<p> <sect><bf>__get_reference</bf><label id="__get_reference"><p><descrip> <tag> Synopsis </tag> Get a reference to a global object <tag> Usage </tag> <tt>Ref_Type __get_reference (String_Type nm)</tt> <tag> Description </tag> This function returns a reference to a global variable or function whose name is specified by <tt>nm</tt>. If no such object exists, it returns <tt>NULL</tt>, otherwise it returns a reference. <tag> Example </tag> For example, consider the function: <tscreen><verb> define runhooks (hook) { variable f; f = __get_reference (hook); if (f != NULL) @f (); } </verb></tscreen> This function could be called from another <bf>S-lang</bf> function to allow customization of that function, e.g., if the function represents a mode, the hook could be called to setup keybindings for the mode. <tag> See Also </tag> <tt>is_defined, typeof, eval, autoload, __is_initialized, __uninitialize</tt> </descrip><p> <sect><bf>__uninitialize</bf><label id="__uninitialize"><p><descrip> <tag> Synopsis </tag> Uninitialize a variable <tag> Usage </tag> <tt>__uninitialize (Ref_Type x)</tt> <tag> Description </tag> The <tt>__uninitialize</tt> function may be used to uninitialize the variable referenced by the parameter <tt>x</tt>. <tag> Example </tag> The following two lines are equivalent: <tscreen><verb> () = __tmp(z); __uninitialize (&z); </verb></tscreen> <tag> See Also </tag> <tt>__tmp, __is_initialized</tt> </descrip><p> <sect><bf>_auto_declare</bf><label id="_auto_declare"><p><descrip> <tag> Synopsis </tag> Set automatic variable declaration mode <tag> Usage </tag> <tt>Integer_Type _auto_declare</tt> <tag> Description </tag> The <tt>_auto_declare</tt> may be used to have all undefined variables implicitely declared as <tt>static</tt>. If set to zero, any variable must be declared witha <tt>variable</tt> declaration before it can be used. If set to one, then any undeclared variabled will be declared as a <tt>static</tt> global variable. The <tt>_auto_declare</tt> variable is is local to each compilation unit and setting its value in one unit has no effect upon its value in other units. The value of this variable has no effect upon the variables in a function. <tag> Example </tag> The following code will not compile if <tt>X</tt> not been declared: <tscreen><verb> X = 1; </verb></tscreen> However, <tscreen><verb> _auto_declare = 1; % declare variables as static. X = 1; </verb></tscreen> is equivalent to <tscreen><verb> static variable X = 1; </verb></tscreen> <tag> Notes </tag> This variable should be used sparingly and is intended primarily for interactive applications where one types <bf>S-lang</bf> commands at a prompt. </descrip><p> <sect><bf>getenv</bf><label id="getenv"><p><descrip> <tag> Synopsis </tag> Get the value of an environment variable <tag> Usage </tag> <tt>String_Type getenv(String_Type var)</tt> <tag> Description </tag> The <tt>getenv</tt> function returns a string that represents the value of an environment variable <tt>var</tt>. It will return <tt>NULL</tt> if there is no environment variable whose name is given by <tt>var</tt>. <tag> Example </tag> <tscreen><verb> if (NULL != getenv ("USE_COLOR")) { set_color ("normal", "white", "blue"); set_color ("status", "black", "gray"); USE_ANSI_COLORS = 1; } </verb></tscreen> <tag> See Also </tag> <tt>putenv, strlen, is_defined</tt> </descrip><p> <sect><bf>implements</bf><label id="implements"><p><descrip> <tag> Synopsis </tag> Name a private namespace <tag> Usage </tag> <tt>implements (String_Type name);</tt> <tag> Description </tag> The <tt>implements</tt> function may be used to name the private namespace associated with the current compilation unit. Doing so will enable access to the members of the namespace from outside the unit. The name of the global namespace is <tt>Global</tt>. <tag> Example </tag> Suppose that some file <tt>t.sl</tt> contains: <tscreen><verb> implements ("Ts_Private"); static define message (x) { Global->vmessage ("Ts_Private message: %s", x); } message ("hello"); </verb></tscreen> will produce <tt>"Ts_Private message: hello"</tt>. This <tt>message</tt> function may be accessed from outside via: <tscreen><verb> Ts_Private->message ("hi"); </verb></tscreen> <tag> Notes </tag> Since <tt>message</tt> is an intrinsic function, it is global and may not be redefined in the global namespace. <tag> See Also </tag> <tt>use_namespace, current_namespace, import</tt> </descrip><p> <sect><bf>putenv</bf><label id="putenv"><p><descrip> <tag> Synopsis </tag> Add or change an environment variable <tag> Usage </tag> <tt>putenv (String_Type s)</tt> <tag> Description </tag> This functions adds string <tt>s</tt> to the environment. Typically, <tt>s</tt> should of the form <tt>"name=value"</tt>. The function signals a <bf>S-lang</bf> error upon failure. <tag> Notes </tag> This function is not available on all systems. <tag> See Also </tag> <tt>getenv, sprintf</tt> </descrip><p> <sect><bf>use_namespace</bf><label id="use_namespace"><p><descrip> <tag> Synopsis </tag> Change to another namespace <tag> Usage </tag> <tt>use_namespace (String_Type name)</tt> <tag> Description </tag> The <tt>use_namespace</tt> function changes the current namespace to the one specified by the parameter. If the specified namespace does not exist, an error will be generated. <tag> See Also </tag> <tt>implements, current_namespace, import</tt> </descrip><p> <sect><bf>current_namespace</bf><label id="current_namespace"><p><descrip> <tag> Synopsis </tag> Get the name of the current namespace <tag> Usage </tag> <tt>String_Type current_namespace ()</tt> <tag> Description </tag> The <tt>current_namespace</tt> function returns the name of the current namespace. If the current namespace is anonymous, that is, has not been given a name via the <tt>implements</tt> function, the empty string <tt>""</tt> will be returned. <tag> See Also </tag> <tt>implements, use_namespace, import</tt> </descrip><p> </book>