summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/nfsmount.h
blob: a27e8a8d70bb0d8803c6a1b2bee4904ba52a9421 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Please do not edit this file.
 * It was generated using rpcgen.
 */

#ifndef _NFSMOUNT_H_RPCGEN
#define _NFSMOUNT_H_RPCGEN

#include <rpc/rpc.h>

int nfsmount_prepare(const char *spec, char **mount_opts);


#ifdef __cplusplus
extern "C" {
#endif

/*
 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
 * unrestricted use provided that this legend is included on all tape
 * media and as a part of the software program in whole or part.  Users
 * may copy or modify Sun RPC without charge, but are not authorized
 * to license or distribute it to anyone else except as part of a product or
 * program developed by the user or with the express written consent of
 * Sun Microsystems, Inc.
 *
 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
 *
 * Sun RPC is provided with no support and without any obligation on the
 * part of Sun Microsystems, Inc. to assist in its use, correction,
 * modification or enhancement.
 *
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
 * OR ANY PART THEREOF.
 *
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
 * or profits or other special, indirect and consequential damages, even if
 * Sun has been advised of the possibility of such damages.
 *
 * Sun Microsystems, Inc.
 * 2550 Garcia Avenue
 * Mountain View, California  94043
 */
/*
 * Copyright (c) 1985, 1990 by Sun Microsystems, Inc.
 */

/* from @(#)mount.x	1.3 91/03/11 TIRPC 1.0 */
#ifndef _rpcsvc_mount_h
#define _rpcsvc_mount_h
#include <asm/types.h>
#define MNTPATHLEN 1024
#define MNTNAMLEN 255
#define FHSIZE 32
#define FHSIZE3 64

typedef char fhandle[FHSIZE];

typedef struct {
	u_int fhandle3_len;
	char *fhandle3_val;
} fhandle3;

enum mountstat3 {
	MNT_OK = 0,
	MNT3ERR_PERM = 1,
	MNT3ERR_NOENT = 2,
	MNT3ERR_IO = 5,
	MNT3ERR_ACCES = 13,
	MNT3ERR_NOTDIR = 20,
	MNT3ERR_INVAL = 22,
	MNT3ERR_NAMETOOLONG = 63,
	MNT3ERR_NOTSUPP = 10004,
	MNT3ERR_SERVERFAULT = 10006,
};
typedef enum mountstat3 mountstat3;

struct fhstatus {
	u_int fhs_status;
	union {
		fhandle fhs_fhandle;
	} fhstatus_u;
};
typedef struct fhstatus fhstatus;

struct mountres3_ok {
	fhandle3 fhandle;
	struct {
		u_int auth_flavours_len;
		int *auth_flavours_val;
	} auth_flavours;
};
typedef struct mountres3_ok mountres3_ok;

struct mountres3 {
	mountstat3 fhs_status;
	union {
		mountres3_ok mountinfo;
	} mountres3_u;
};
typedef struct mountres3 mountres3;

typedef char *dirpath;

typedef char *name;

typedef struct mountbody *mountlist;

struct mountbody {
	name ml_hostname;
	dirpath ml_directory;
	mountlist ml_next;
};
typedef struct mountbody mountbody;

typedef struct groupnode *groups;

struct groupnode {
	name gr_name;
	groups gr_next;
};
typedef struct groupnode groupnode;

typedef struct exportnode *exports;

struct exportnode {
	dirpath ex_dir;
	groups ex_groups;
	exports ex_next;
};
typedef struct exportnode exportnode;

struct ppathcnf {
	int pc_link_max;
	short pc_max_canon;
	short pc_max_input;
	short pc_name_max;
	short pc_path_max;
	short pc_pipe_buf;
	u_char pc_vdisable;
	char pc_xxx;
	short pc_mask[2];
};
typedef struct ppathcnf ppathcnf;
#endif /*!_rpcsvc_mount_h*/

#define MOUNTPROG 100005
#define MOUNTVERS 1

#if defined(__STDC__) || defined(__cplusplus)
#define MOUNTPROC_NULL 0
extern  void * mountproc_null_1(void *, CLIENT *);
extern  void * mountproc_null_1_svc(void *, struct svc_req *);
#define MOUNTPROC_MNT 1
extern  fhstatus * mountproc_mnt_1(dirpath *, CLIENT *);
extern  fhstatus * mountproc_mnt_1_svc(dirpath *, struct svc_req *);
#define MOUNTPROC_DUMP 2
extern  mountlist * mountproc_dump_1(void *, CLIENT *);
extern  mountlist * mountproc_dump_1_svc(void *, struct svc_req *);
#define MOUNTPROC_UMNT 3
extern  void * mountproc_umnt_1(dirpath *, CLIENT *);
extern  void * mountproc_umnt_1_svc(dirpath *, struct svc_req *);
#define MOUNTPROC_UMNTALL 4
extern  void * mountproc_umntall_1(void *, CLIENT *);
extern  void * mountproc_umntall_1_svc(void *, struct svc_req *);
#define MOUNTPROC_EXPORT 5
extern  exports * mountproc_export_1(void *, CLIENT *);
extern  exports * mountproc_export_1_svc(void *, struct svc_req *);
#define MOUNTPROC_EXPORTALL 6
extern  exports * mountproc_exportall_1(void *, CLIENT *);
extern  exports * mountproc_exportall_1_svc(void *, struct svc_req *);
extern int mountprog_1_freeresult (SVCXPRT *, xdrproc_t, caddr_t);

#else /* K&R C */
#define MOUNTPROC_NULL 0
extern  void * mountproc_null_1();
extern  void * mountproc_null_1_svc();
#define MOUNTPROC_MNT 1
extern  fhstatus * mountproc_mnt_1();
extern  fhstatus * mountproc_mnt_1_svc();
#define MOUNTPROC_DUMP 2
extern  mountlist * mountproc_dump_1();
extern  mountlist * mountproc_dump_1_svc();
#define MOUNTPROC_UMNT 3
extern  void * mountproc_umnt_1();
extern  void * mountproc_umnt_1_svc();
#define MOUNTPROC_UMNTALL 4
extern  void * mountproc_umntall_1();
extern  void * mountproc_umntall_1_svc();
#define MOUNTPROC_EXPORT 5
extern  exports * mountproc_export_1();
extern  exports * mountproc_export_1_svc();
#define MOUNTPROC_EXPORTALL 6
extern  exports * mountproc_exportall_1();
extern  exports * mountproc_exportall_1_svc();
extern int mountprog_1_freeresult ();
#endif /* K&R C */
#define MOUNTVERS_POSIX 2

#if defined(__STDC__) || defined(__cplusplus)
extern  void * mountproc_null_2(void *, CLIENT *);
extern  void * mountproc_null_2_svc(void *, struct svc_req *);
extern  fhstatus * mountproc_mnt_2(dirpath *, CLIENT *);
extern  fhstatus * mountproc_mnt_2_svc(dirpath *, struct svc_req *);
extern  mountlist * mountproc_dump_2(void *, CLIENT *);
extern  mountlist * mountproc_dump_2_svc(void *, struct svc_req *);
extern  void * mountproc_umnt_2(dirpath *, CLIENT *);
extern  void * mountproc_umnt_2_svc(dirpath *, struct svc_req *);
extern  void * mountproc_umntall_2(void *, CLIENT *);
extern  void * mountproc_umntall_2_svc(void *, struct svc_req *);
extern  exports * mountproc_export_2(void *, CLIENT *);
extern  exports * mountproc_export_2_svc(void *, struct svc_req *);
extern  exports * mountproc_exportall_2(void *, CLIENT *);
extern  exports * mountproc_exportall_2_svc(void *, struct svc_req *);
#define MOUNTPROC_PATHCONF 7
extern  ppathcnf * mountproc_pathconf_2(dirpath *, CLIENT *);
extern  ppathcnf * mountproc_pathconf_2_svc(dirpath *, struct svc_req *);
extern int mountprog_2_freeresult (SVCXPRT *, xdrproc_t, caddr_t);

#else /* K&R C */
extern  void * mountproc_null_2();
extern  void * mountproc_null_2_svc();
extern  fhstatus * mountproc_mnt_2();
extern  fhstatus * mountproc_mnt_2_svc();
extern  mountlist * mountproc_dump_2();
extern  mountlist * mountproc_dump_2_svc();
extern  void * mountproc_umnt_2();
extern  void * mountproc_umnt_2_svc();
extern  void * mountproc_umntall_2();
extern  void * mountproc_umntall_2_svc();
extern  exports * mountproc_export_2();
extern  exports * mountproc_export_2_svc();
extern  exports * mountproc_exportall_2();
extern  exports * mountproc_exportall_2_svc();
#define MOUNTPROC_PATHCONF 7
extern  ppathcnf * mountproc_pathconf_2();
extern  ppathcnf * mountproc_pathconf_2_svc();
extern int mountprog_2_freeresult ();
#endif /* K&R C */
#define MOUNT_V3 3

#if defined(__STDC__) || defined(__cplusplus)
#define MOUNTPROC3_NULL 0
extern  void * mountproc3_null_3(void *, CLIENT *);
extern  void * mountproc3_null_3_svc(void *, struct svc_req *);
#define MOUNTPROC3_MNT 1
extern  mountres3 * mountproc3_mnt_3(dirpath *, CLIENT *);
extern  mountres3 * mountproc3_mnt_3_svc(dirpath *, struct svc_req *);
#define MOUNTPROC3_DUMP 2
extern  mountlist * mountproc3_dump_3(void *, CLIENT *);
extern  mountlist * mountproc3_dump_3_svc(void *, struct svc_req *);
#define MOUNTPROC3_UMNT 3
extern  void * mountproc3_umnt_3(dirpath *, CLIENT *);
extern  void * mountproc3_umnt_3_svc(dirpath *, struct svc_req *);
#define MOUNTPROC3_UMNTALL 4
extern  void * mountproc3_umntall_3(void *, CLIENT *);
extern  void * mountproc3_umntall_3_svc(void *, struct svc_req *);
#define MOUNTPROC3_EXPORT 5
extern  exports * mountproc3_export_3(void *, CLIENT *);
extern  exports * mountproc3_export_3_svc(void *, struct svc_req *);
extern int mountprog_3_freeresult (SVCXPRT *, xdrproc_t, caddr_t);

#else /* K&R C */
#define MOUNTPROC3_NULL 0
extern  void * mountproc3_null_3();
extern  void * mountproc3_null_3_svc();
#define MOUNTPROC3_MNT 1
extern  mountres3 * mountproc3_mnt_3();
extern  mountres3 * mountproc3_mnt_3_svc();
#define MOUNTPROC3_DUMP 2
extern  mountlist * mountproc3_dump_3();
extern  mountlist * mountproc3_dump_3_svc();
#define MOUNTPROC3_UMNT 3
extern  void * mountproc3_umnt_3();
extern  void * mountproc3_umnt_3_svc();
#define MOUNTPROC3_UMNTALL 4
extern  void * mountproc3_umntall_3();
extern  void * mountproc3_umntall_3_svc();
#define MOUNTPROC3_EXPORT 5
extern  exports * mountproc3_export_3();
extern  exports * mountproc3_export_3_svc();
extern int mountprog_3_freeresult ();
#endif /* K&R C */

/* the xdr functions */

#if defined(__STDC__) || defined(__cplusplus)
extern  bool_t xdr_fhandle (XDR *, fhandle);
extern  bool_t xdr_fhandle3 (XDR *, fhandle3*);
extern  bool_t xdr_mountstat3 (XDR *, mountstat3*);
extern  bool_t xdr_fhstatus (XDR *, fhstatus*);
extern  bool_t xdr_mountres3_ok (XDR *, mountres3_ok*);
extern  bool_t xdr_mountres3 (XDR *, mountres3*);
extern  bool_t xdr_dirpath (XDR *, dirpath*);
extern  bool_t xdr_name (XDR *, name*);
extern  bool_t xdr_mountlist (XDR *, mountlist*);
extern  bool_t xdr_mountbody (XDR *, mountbody*);
extern  bool_t xdr_groups (XDR *, groups*);
extern  bool_t xdr_groupnode (XDR *, groupnode*);
extern  bool_t xdr_exports (XDR *, exports*);
extern  bool_t xdr_exportnode (XDR *, exportnode*);
extern  bool_t xdr_ppathcnf (XDR *, ppathcnf*);

#else /* K&R C */
extern bool_t xdr_fhandle ();
extern bool_t xdr_fhandle3 ();
extern bool_t xdr_mountstat3 ();
extern bool_t xdr_fhstatus ();
extern bool_t xdr_mountres3_ok ();
extern bool_t xdr_mountres3 ();
extern bool_t xdr_dirpath ();
extern bool_t xdr_name ();
extern bool_t xdr_mountlist ();
extern bool_t xdr_mountbody ();
extern bool_t xdr_groups ();
extern bool_t xdr_groupnode ();
extern bool_t xdr_exports ();
extern bool_t xdr_exportnode ();
extern bool_t xdr_ppathcnf ();

#endif /* K&R C */

#ifdef __cplusplus
}
#endif

#endif /* !_NFSMOUNT_H_RPCGEN */

href='#n1400'>1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
/* -*- mode: C; mode: fold; -*- */
/* string manipulation functions for S-Lang. */
/* Copyright (c) 1992, 1999, 2001 John E. Davis
 * This file is part of the S-Lang library.
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Perl Artistic License.
 */

#include "slinclud.h"
/*{{{ Include Files */

#include <time.h>

#ifndef __QNX__
# if defined(__GO32__) || defined(__WATCOMC__)
#  include <dos.h>
#  include <bios.h>
# endif
#endif

#if SLANG_HAS_FLOAT
#include <math.h>
#endif

#include <string.h>
#include <stdarg.h>
#include <ctype.h>

#ifndef isdigit
# define isdigit(x) (((x) >= '0') && ((x) <= '9'))
#endif

#include "slang.h"
#include "_slang.h"

/*}}}*/

#define USE_ALLOC_STSTRING 1

/*{{{ Utility Functions */

static char Utility_Char_Table [256];
static unsigned char WhiteSpace_Lut[256];

static void set_utility_char_table (char *pos) /*{{{*/
{
   register char *t = Utility_Char_Table, *tmax;
   register unsigned char ch;

   tmax = t + 256;
   while (t < tmax) *t++ = 0;

   t = Utility_Char_Table;
   while ((ch = (unsigned char) *pos++) != 0) t[ch] = 1;
}

/*}}}*/

_INLINE_
static unsigned char *make_whitespace_lut (void)
{
   if (WhiteSpace_Lut[' '] != 1)
     {
	WhiteSpace_Lut[' '] = WhiteSpace_Lut['\r'] 
	  = WhiteSpace_Lut ['\n'] = WhiteSpace_Lut['\t']
	  = WhiteSpace_Lut ['\f'] = 1;
     }
   return WhiteSpace_Lut;
}

static unsigned char *make_lut (unsigned char *s, unsigned char *lut)
{
   int reverse = 0;
   
   if (*s == '^')
     {
	reverse = 1;
	s++;
     }
   SLmake_lut (lut, s, reverse);
   return lut;
}

static unsigned int do_trim (char **beg, int do_beg, 
			     char **end, int do_end, 
			     char *white) /*{{{*/
{
   unsigned int len;
   char *a, *b;

   set_utility_char_table (white);

   a = *beg;
   len = strlen (a);
   b = a + len;

   if (do_beg)
     while (Utility_Char_Table[(unsigned char) *a]) a++;

   if (do_end)
     {
	b--;
	while ((b >= a) && (Utility_Char_Table[(unsigned char) *b])) b--;
	b++;
     }

   len = (unsigned int) (b - a);
   *beg = a;
   *end = b;
   return len;
}

/*}}}*/

/*}}}*/

static int pop_3_strings (char **a, char **b, char **c)
{
   *a = *b = *c = NULL;
   if (-1 == SLpop_string (c))
     return -1;
   
   if (-1 == SLpop_string (b))
     {
	SLfree (*c);
	*c = NULL;
	return -1;
     }

   if (-1 == SLpop_string (a))
     {
	SLfree (*b);
	SLfree (*c);
	*b = *c = NULL;
	return -1;
     }
   
   return 0;
}

static void free_3_strings (char *a, char *b, char *c)
{
   SLfree (a);
   SLfree (b);
   SLfree (c);
}

static void strcat_cmd (void) /*{{{*/
{
   char *c, *c1;
   int nargs;
   int i;
   char **ptrs;
   unsigned int len;
#if !USE_ALLOC_STSTRING
   char buf[256];
#endif
   nargs = SLang_Num_Function_Args;
   if (nargs <= 0) nargs = 2;

   if (NULL == (ptrs = (char **)SLmalloc (nargs * sizeof (char *))))
     return;

   memset ((char *) ptrs, 0, sizeof (char *) * nargs);

   c = NULL;
   i = nargs;
   len = 0;
   while (i != 0)
     {
	char *s;

	i--;
	if (-1 == SLang_pop_slstring (&s))
	  goto free_and_return;
	ptrs[i] = s;
	len += strlen (s);
     }
#if USE_ALLOC_STSTRING
   if (NULL == (c = _SLallocate_slstring (len)))
     goto free_and_return;
#else
   len++;			       /* \0 char */
   if (len <= sizeof (buf))
     c = buf;
   else if (NULL == (c = SLmalloc (len)))
     goto free_and_return;
#endif

   c1 = c;
   for (i = 0; i < nargs; i++)
     {
	strcpy (c1, ptrs[i]);
	c1 += strlen (c1);
     }
   
   free_and_return:
   for (i = 0; i < nargs; i++)
     SLang_free_slstring (ptrs[i]);
   SLfree ((char *) ptrs);

#if USE_ALLOC_STSTRING
   (void) _SLpush_alloced_slstring (c, len);
#else
   if (c != buf)
     (void) SLang_push_malloced_string (c);   /* NULL ok */
   else
     (void) SLang_push_string (c);
#endif
}

/*}}}*/

static int _SLang_push_nstring (char *a, unsigned int len)
{
   a = SLang_create_nslstring (a, len);
   if (a == NULL)
     return -1;
   
   return _SLang_push_slstring (a);
}


static void strtrim_cmd_internal (char *str, int do_beg, int do_end)
{
   char *beg, *end, *white;
   int free_str;
   unsigned int len;

   /* Go through SLpop_string to get a private copy since it will be
    * modified.
    */
   
   free_str = 0;
   if (SLang_Num_Function_Args == 2)
     {
	white = str;
	if (-1 == SLang_pop_slstring (&str))
	  return;
	free_str = 1;
     }
   else white = " \t\f\r\n";

   beg = str;
   len = do_trim (&beg, do_beg, &end, do_end, white);
   
   (void) _SLang_push_nstring (beg, len);
   if (free_str)
     SLang_free_slstring (str);
}

   
static void strtrim_cmd (char *str)
{
   strtrim_cmd_internal (str, 1, 1);
}

static void strtrim_beg_cmd (char *str)
{
   strtrim_cmd_internal (str, 1, 0);
}

static void strtrim_end_cmd (char *str)
{
   strtrim_cmd_internal (str, 0, 1);
}


static void strcompress_cmd (void) /*{{{*/
{
   char *str, *white, *c;
   unsigned char *s, *beg, *end;
   unsigned int len;
   char pref_char;

   if (SLpop_string (&white)) return;
   if (SLpop_string (&str))
     {
	SLfree (white);
	return;
     }

   /* The first character of white is the preferred whitespace character */
   pref_char = *white;

   beg = (unsigned char *) str;
   (void) do_trim ((char **) &beg, 1, (char **) &end, 1, white);
   SLfree (white);

   /* Determine the effective length */
   len = 0;
   s = (unsigned char *) beg;
   while (s < end)
     {
	len++;
	if (Utility_Char_Table[*s++])
	  {
	     while ((s < end) && Utility_Char_Table[*s]) s++;
	  }
     }

#if USE_ALLOC_STSTRING
   c = _SLallocate_slstring (len);
#else
   c = SLmalloc (len + 1);
#endif
   if (c == NULL)
     {
	SLfree (str);
	return;
     }
   
   s = (unsigned char *) c;

   while (beg < end)
     {
	unsigned char ch = *beg++;
	
	if (0 == Utility_Char_Table[ch])
	  {
	     *s++ = ch;
	     continue;
	  }
	
	*s++ = (unsigned char) pref_char;
	
	while ((beg < end) && Utility_Char_Table[*beg]) 
	  beg++;
     }

   *s = 0;
   
#if USE_ALLOC_STSTRING
   (void) _SLpush_alloced_slstring (c, len);
#else
   SLang_push_malloced_string(c);
#endif

   SLfree(str);
}

/*}}}*/

static int str_replace_cmd_1 (char *orig, char *match, char *rep, unsigned int max_num_replaces,
			      char **new_strp) /*{{{*/
{
   char *s, *t, *new_str;
   unsigned int rep_len, match_len, new_len;
   unsigned int num_replaces;

   *new_strp = NULL;

   match_len = strlen (match);

   if (match_len == 0)
     return 0;

   num_replaces = 0;
   s = orig;
   while (num_replaces < max_num_replaces)
     {
	s = strstr (s, match);
	if (s == NULL)
	  break;
	s += match_len;
	num_replaces++;
     }

   if (num_replaces == 0)
     return 0;

   max_num_replaces = num_replaces;

   rep_len = strlen (rep);

   new_len = (strlen (orig) - num_replaces * match_len) + num_replaces * rep_len;
   new_str = SLmalloc (new_len + 1);
   if (new_str == NULL)
     return -1;
   
   s = orig;
   t = new_str;
   
   for (num_replaces = 0; num_replaces < max_num_replaces; num_replaces++)
     {
	char *next_s;
	unsigned int len;

	next_s = strstr (s, match);    /* cannot be NULL */
	len = (unsigned int) (next_s - s);
	strncpy (t, s, len);
	t += len;
	strcpy (t, rep);
	t += rep_len;
	
	s = next_s + match_len;
     }
   strcpy (t, s);
   *new_strp = new_str;

   return (int) num_replaces;
}

/*}}}*/

static void reverse_string (char *a)
{
   char *b;
   
   b = a + strlen (a);
   while (b > a)
     {
	char ch;

	b--;
	ch = *a;
	*a++ = *b;
	*b = ch;
     }
}

static int strreplace_cmd (int *np)
{   
   char *orig, *match, *rep;
   char *new_str;
   int max_num_replaces;
   int ret;

   max_num_replaces = *np;

   if (-1 == pop_3_strings (&orig, &match, &rep))
     return -1;

   if (max_num_replaces < 0)
     {
	reverse_string (orig);
	reverse_string (match);
	reverse_string (rep);
	ret = str_replace_cmd_1 (orig, match, rep, -max_num_replaces, &new_str);
	if (ret > 0) reverse_string (new_str);
	else if (ret == 0)
	  reverse_string (orig);
     }
   else ret = str_replace_cmd_1 (orig, match, rep, max_num_replaces, &new_str);
   
   if (ret == 0)
     {
	if (-1 == SLang_push_malloced_string (orig))
	  ret = -1;
	orig = NULL;
     }
   else if (ret > 0)
     {
	if (-1 == SLang_push_malloced_string (new_str))
	  ret = -1;
     }

   free_3_strings (orig, match, rep);
   return ret;
}

static int str_replace_cmd (char *orig, char *match, char *rep)
{
   char *s;
   int ret;

   ret = str_replace_cmd_1 (orig, match, rep, 1, &s);
   if (ret == 1)
     (void) SLang_push_malloced_string (s);
   return ret;
}

	
     
static void strtok_cmd (char *str)
{
   _SLString_List_Type sl;
   unsigned char white_buf[256];
   char *s;
   unsigned char *white;
   
   if (SLang_Num_Function_Args == 1)
     white = make_whitespace_lut ();
   else
     {
	white = white_buf;
	make_lut ((unsigned char *)str, white);
	if (-1 == SLang_pop_slstring (&str))
	  return;
     }

   if (-1 == _SLstring_list_init (&sl, 256, 1024))
     goto the_return;

   s = str;
   while (*s != 0)
     {
	char *s0;

	s0 = s;
	/* Skip whitespace */
	while ((*s0 != 0) && (0 != white[(unsigned char)*s0]))
	  s0++;

	if (*s0 == 0)
	  break;

	s = s0;
	while ((*s != 0) && (0 == white[(unsigned char) *s]))
	  s++;

	/* sl deleted upon failure */
	if (-1 == _SLstring_list_append (&sl, SLang_create_nslstring (s0, (unsigned int) (s - s0))))
	  goto the_return;
     }

   /* Deletes sl */
   (void) _SLstring_list_push (&sl);

   the_return:
   if (white == white_buf)
     SLang_free_slstring (str);
}

/* This routine returns the string with text removed between single character
   comment delimiters from the set b and e. */

static void str_uncomment_string_cmd (char *str, char *b, char *e) /*{{{*/
{
   unsigned char chb, che;
   unsigned char *s, *cbeg, *mark;

   if (strlen(b) != strlen(e))
     {
	SLang_doerror ("Comment delimiter length mismatch.");
	return;
     }

   set_utility_char_table (b);

   if (NULL == (str = (char *) SLmake_string(str))) return;

   s = (unsigned char *) str;

   while ((chb = *s++) != 0)
     {
	if (Utility_Char_Table [chb] == 0) continue;

	mark = s - 1;

	cbeg = (unsigned char *) b;
	while (*cbeg != chb) cbeg++;

	che = (unsigned char) *(e + (int) (cbeg - (unsigned char *) b));

	while (((chb = *s++) != 0) && (chb != che));

	if (chb == 0)
	  {
	     /* end of string and end not found.  Just truncate it a return; */
	     *mark = 0;
	     break;
	  }

	strcpy ((char *) mark, (char *)s);
	s = mark;
     }
   SLang_push_malloced_string (str);
}

/*}}}*/

static void str_quote_string_cmd (char *str, char *quotes, int *slash_ptr) /*{{{*/
{
   char *q;
   int slash;
   unsigned int len;
   register char *t, *s, *q1;
   register unsigned char ch;

   slash = *slash_ptr;

   if ((slash > 255) || (slash < 0))
     {
	SLang_Error = SL_INVALID_PARM;
	return;
     }

   /* setup the utility table to have 1s at quote char postitions. */
   set_utility_char_table (quotes);

   t = Utility_Char_Table;
   t[(unsigned int) slash] = 1;

   /* calculate length */
   s = str;
   len = 0;
   while ((ch = (unsigned char) *s++) != 0) if (t[ch]) len++;
   len += (unsigned int) (s - str);

   if (NULL != (q = SLmalloc(len)))
     {
	s = str; q1 = q;
	while ((ch = (unsigned char) *s++) != 0)
	  {
	     if (t[ch]) *q1++ = slash;
	     *q1++ = (char) ch;
	  }
	*q1 = 0;
	SLang_push_malloced_string(q);
     }
}

/*}}}*/

/* returns the position of substrin in a string or null */
static int issubstr_cmd (char *a, char *b) /*{{{*/
{
   char *c;

   if (NULL == (c = (char *) strstr(a, b)))
     return 0;

   return 1 + (int) (c - a);
}

/*}}}*/

/* returns to stack string at pos n to n + m of a */
static void substr_cmd (char *a, int *n_ptr, int *m_ptr) /*{{{*/
{
   int n, m;
   int lena;

   n = *n_ptr;
   m = *m_ptr;

   lena = strlen (a);
   if (n > lena) n = lena + 1;
   if (n < 1)
     {
	SLang_Error = SL_INVALID_PARM;
	return;
     }

   n--;
   if (m < 0) m = lena;
   if (n + m > lena) m = lena - n;
   
   (void) _SLang_push_nstring (a + n, (unsigned int) m);
}

/*}}}*/

/* substitute char m at positin string n in string*/
static void strsub_cmd (int *nptr, int *mptr) /*{{{*/
{
   char *a;
   int n, m;
   unsigned int lena;

   if (-1 == SLpop_string (&a))
     return;

   n = *nptr;
   m = *mptr;

   lena = strlen (a);

   if ((n <= 0) || (lena < (unsigned int) n))
     {
	SLang_Error = SL_INVALID_PARM;
	SLfree(a);
	return;
     }

   a[n - 1] = (char) m;

   SLang_push_malloced_string (a);
}

/*}}}*/

static void strup_cmd(void) /*{{{*/
{
   unsigned char c, *a;
   char *str;

   if (SLpop_string (&str))
     return;

   a = (unsigned char *) str;
   while ((c = *a) != 0)
     {
	/* if ((*a >= 'a') && (*a <= 'z')) *a -= 32; */
	*a = UPPER_CASE(c);
	a++;
     }

   SLang_push_malloced_string (str);
}

/*}}}*/

static int isdigit_cmd (char *what) /*{{{*/
{
   return isdigit((unsigned char)*what);
}

/*}}}*/
static int toupper_cmd (int *ch) /*{{{*/
{
   return UPPER_CASE(*ch);
}

/*}}}*/

static int tolower_cmd (int *ch) /*{{{*/
{
   return LOWER_CASE(*ch);
}

/*}}}*/

static void strlow_cmd (void) /*{{{*/
{
   unsigned char c, *a;
   char *str;

   if (SLpop_string(&str)) return;
   a = (unsigned char *) str;
   while ((c = *a) != 0)
     {
	/* if ((*a >= 'a') && (*a <= 'z')) *a -= 32; */
	*a = LOWER_CASE(c);
	a++;
     }

   SLang_push_malloced_string ((char *) str);
}

/*}}}*/

static SLang_Array_Type *do_strchop (char *str, int delim, int quote)
{
   int count;
   char *s0, *elm;
   register char *s1;
   register unsigned char ch;
   int quoted;
   SLang_Array_Type *at;
   char **data;

   if ((quote < 0) || (quote > 255)
       || (delim <= 0) || (delim > 255))
     {
	SLang_Error = SL_INVALID_PARM;
	return NULL;
     }

   s1 = s0 = str;

   quoted = 0;
   count = 1;			       /* at least 1 */
   while (1)
     {
	ch = (unsigned char) *s1++;
	if ((ch == quote) && quote)
	  {
	     if (*s1 == 0)
	       break;

	     s1++;
	     continue;
	  }

	if (ch == delim)
	  {
	     count++;
	     continue;
	  }

	if (ch == 0)
	  break;
     }

   if (NULL == (at = SLang_create_array (SLANG_STRING_TYPE, 0, NULL, &count, 1)))
     return NULL;

   data = (char **)at->data;

   count = 0;
   s1 = s0;

   while (1)
     {
	ch = (unsigned char) *s1;

	if ((ch == quote) && quote)
	  {
	     s1++;
	     if (*s1 != 0) s1++;
	     quoted = 1;
	     continue;
	  }

	if ((ch == delim) || (ch == 0))
	  {
	     if (quoted == 0)
	       elm = SLang_create_nslstring (s0, (unsigned int) (s1 - s0));
	     else
	       {
		  register char ch1, *p, *p1;
		  char *tmp;

		  tmp = SLmake_nstring (s0, (unsigned int)(s1 - s0));
		  if (tmp == NULL)
		    break;

		  /* Now unquote it */
		  p = p1 = tmp;
		  do
		    {
		       ch1 = *p1++;
		       if (ch1 == '\\') ch1 = *p1++;
		       *p++ = ch1;
		    }
		  while (ch1 != 0);
		  quoted = 0;

		  elm = SLang_create_slstring (tmp);
		  SLfree (tmp);
	       }

	     if (elm == NULL)
	       break;

	     data[count] = elm;
	     count++;

	     if (ch == 0)
	       return at;

	     s1++;		       /* skip past delim */
	     s0 = s1;		       /* and reset */
	  }
	else s1++;
     }

   SLang_free_array (at);
   return NULL;
}

static void strchop_cmd (char *str, int *q, int *d)
{
   (void) SLang_push_array (do_strchop (str, *q, *d), 1);
}

static void strchopr_cmd (char *str, int *q, int *d)
{
   SLang_Array_Type *at;

   if (NULL != (at = do_strchop (str, *q, *d)))
     {
	char **d0, **d1;

	d0 = (char **) at->data;
	d1 = d0 + (at->num_elements - 1);

	while (d0 < d1)
	  {
	     char *tmp;

	     tmp = *d0;
	     *d0 = *d1;
	     *d1 = tmp;
	     d0++;
	     d1--;
	  }
     }
   SLang_push_array (at, 1);
}

static int strcmp_cmd (char *a, char *b) /*{{{*/
{
   return strcmp(a, b);
}

/*}}}*/

static int strncmp_cmd (char *a, char *b, int *n) /*{{{*/
{
   return strncmp(a, b, (unsigned int) *n);
}

/*}}}*/

static int strlen_cmd (char *s) /*{{{*/
{
   return (int) strlen (s);
}
/*}}}*/

static void extract_element_cmd (char *list, int *nth_ptr, int *delim_ptr)
{
   char buf[1024], *b;

   b = buf;
   if (-1 == SLextract_list_element (list, *nth_ptr, *delim_ptr, buf, sizeof(buf)))
     b = NULL;

   SLang_push_string (b);
}

/* sprintf functionality for S-Lang */

static char *SLdo_sprintf (char *fmt) /*{{{*/
{
   register char *p = fmt, ch;
   char *out = NULL, *outp = NULL;
   char dfmt[1024];	       /* used to hold part of format */
   char *f;
   VOID_STAR varp;
   int want_width, width, precis, use_varp, int_var;
   long long_var;
   unsigned int len = 0, malloc_len = 0, dlen;
   int do_free, guess_size;
#if SLANG_HAS_FLOAT
   int tmp1, tmp2, use_double;
   double x;
#endif
   int use_long = 0;

   while (1)
     {
	while ((ch = *p) != 0)
	  {
	     if (ch == '%')
	       break;
	     p++;
	  }

	/* p points at '%' or 0 */

	dlen = (unsigned int) (p - fmt);

	if (len + dlen >= malloc_len)
	  {
	     malloc_len = len + dlen;
	     if (out == NULL) outp = SLmalloc(malloc_len + 1);
	     else outp = SLrealloc(out, malloc_len + 1);
	     if (NULL == outp)
	       return out;
	     out = outp;
	     outp = out + len;
	  }

	strncpy(outp, fmt, dlen);
	len += dlen;
	outp = out + len;
	*outp = 0;
	if (ch == 0) break;

	/* bump it beyond '%' */
	++p;
	fmt = p;

	f = dfmt;
	*f++ = ch;
	/* handle flag char */
	ch = *p++;

	/* Make sure cases such as "% #g" can be handled. */
	if ((ch == '-') || (ch == '+') || (ch == ' ') || (ch == '#'))
	  {
	     *f++ = ch;
	     ch = *p++;
	     if ((ch == '-') || (ch == '+') || (ch == ' ') || (ch == '#'))
	       {
		  *f++ = ch;
		  ch = *p++;
	       }
	  }


	/* width */
	/* I have got to parse it myself so that I can see how big it needs
	 * to be.
	 */
	want_width = width = 0;
	if (ch == '*')
	  {
	     if (SLang_pop_integer(&width)) return (out);
	     want_width = 1;
	     ch = *p++;
	  }
	else
	  {
	     if (ch == '0')
	       {
		  *f++ = '0';
		  ch = *p++;
	       }

	     while ((ch <= '9') && (ch >= '0'))
	       {
		  width = width * 10 + (ch - '0');
		  ch = *p++;
		  want_width = 1;
	       }
	  }

	if (want_width)
	  {
	     sprintf(f, "%d", width);
	     f += strlen (f);
	  }
	precis = 0;
	/* precision -- also indicates max number of chars from string */
	if (ch == '.')
	  {
	     *f++ = ch;
	     ch = *p++;
	     want_width = 0;
	     if (ch == '*')
	       {
		  if (SLang_pop_integer(&precis)) return (out);
		  ch = *p++;
		  want_width = 1;
	       }
	     else while ((ch <= '9') && (ch >= '0'))
	       {
		  precis = precis * 10 + (ch - '0');
		  ch = *p++;
		  want_width = 1;
	       }
	     if (want_width)
	       {
		  sprintf(f, "%d", precis);
		  f += strlen (f);
	       }
	     else precis = 0;
	  }

	long_var = 0;
	int_var = 0;
	varp = NULL;
	guess_size = 32;
#if SLANG_HAS_FLOAT
	use_double = 0;
#endif
	use_long = 0;
	use_varp = 0;
	do_free = 0;

	if (ch == 'l')
	  {
	     use_long = 1;
	     ch = *p++;
	  }
	else if (ch == 'h') ch = *p++; /* not supported */

	/* Now the actual format specifier */
	switch (ch)
	  {
	   case 'S':
	     _SLstring_intrinsic ();
	     ch = 's';
	     /* drop */
	   case 's':
	     if (SLang_pop_slstring((char **) &varp)) return (out);
	     do_free = 1;
	     guess_size = strlen((char *) varp);
	     use_varp = 1;
	     break;

	   case '%':
	     guess_size = 1;
	     do_free = 0;
	     use_varp = 1;
	     varp = (VOID_STAR) "%";
	     break;

	   case 'c': guess_size = 1;
	     use_long = 0;
	     /* drop */
	   case 'd':
	   case 'i':
	   case 'o':
	   case 'u':
	   case 'X':
	   case 'x':
	     if (SLang_pop_long (&long_var)) return(out);
	     if (use_long == 0)
	       int_var = (int) long_var;
	     else
	       *f++ = 'l';
	     break;

	   case 'f':
	   case 'e':
	   case 'g':
	   case 'E':
	   case 'G':
#if SLANG_HAS_FLOAT
	     if (SLang_pop_double(&x, &tmp1, &tmp2)) return (out);
	     use_double = 1;
	     guess_size = 256;
	     (void) tmp1; (void) tmp2;
	     use_long = 0;
	     break;
#endif
	   case 'p':
	     guess_size = 32;
	     /* Pointer type?? Why?? */
	     if (-1 == SLdo_pop ())
	       return out;
	     varp = (VOID_STAR) _SLStack_Pointer;
	     use_varp = 1;
	     use_long = 0;
	     break;

	   default:
	     SLang_doerror("Invalid Format.");
	     return(out);
	  }
	*f++ = ch; *f = 0;

	width = width + precis;
	if (width > guess_size) guess_size = width;

	if (len + guess_size > malloc_len)
	  {
	     outp = (char *) SLrealloc(out, len + guess_size + 1);
	     if (outp == NULL)
	       {
		  SLang_Error = SL_MALLOC_ERROR;
		  return (out);
	       }
	     out = outp;
	     outp = out + len;
	     malloc_len = len + guess_size;
	  }

	if (use_varp)
	  {
	     sprintf(outp, dfmt, varp);
	     if (do_free) SLang_free_slstring ((char *)varp);
	  }
#if SLANG_HAS_FLOAT
	else if (use_double) sprintf(outp, dfmt, x);
#endif
	else if (use_long) sprintf (outp, dfmt, long_var);
	else sprintf(outp, dfmt, int_var);

	len += strlen(outp);
	outp = out + len;
	fmt = p;
     }

   if (out != NULL)
     {
	outp = SLrealloc (out, (unsigned int) (outp - out) + 1);
	if (outp != NULL) out = outp;
     }

   return (out);
}

/*}}}*/

int _SLstrops_do_sprintf_n (int n) /*{{{*/
{
   char *p;
   char *fmt;
   SLang_Object_Type *ptr;
   int ofs;

   if (-1 == (ofs = SLreverse_stack (n + 1)))
     return -1;

   ptr = _SLRun_Stack + ofs;

   if (SLang_pop_slstring(&fmt))
     return -1;

   p = SLdo_sprintf (fmt);
   SLang_free_slstring (fmt);

   while (_SLStack_Pointer > ptr)
     SLdo_pop ();

   if (SLang_Error)
     {
	SLfree (p);
	return -1;
     }
   
   return SLang_push_malloced_string (p);
}

/*}}}*/

static void sprintf_n_cmd (int *n)
{
   _SLstrops_do_sprintf_n (*n);
}

static void sprintf_cmd (void)
{
   _SLstrops_do_sprintf_n (SLang_Num_Function_Args - 1);    /* do not include format */
}

/* converts string s to a form that can be used in an eval */