File: str.c

package info (click to toggle)
cdcd 0.6.6-13.3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,768 kB
  • sloc: ansic: 4,891; sh: 2,142; makefile: 17
file content (501 lines) | stat: -rw-r--r-- 10,105 bytes parent folder | download | duplicates (6)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
cdcd - Command Driven CD player
Copyright (C) 1998-99 Tony Arcieri
Copyright (C) 2001, 2002, 2003 Fabrice Bauzac

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "config.h"

#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdio.h>

#ifdef HAVE_VASPRINTF
/* Should be declared in <stdio.h>, but I need this line for an
   unknown reason.  */
extern int vasprintf (char **, const char *, va_list);
#else
# include <glib.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

#include "cdcd.h"
#include "str.h"

#ifdef va_copy
#elif defined (__va_copy)
# define va_copy(va2, va) __va_copy ((va2), (va));
#else
# define va_copy(va2, va) memcpy (&(va2), &(va), sizeof (va));
#endif

/* Returns a malloc()ed string containing the lowercase version of S */
char *
strlowera (const char *s)
{
  char *r, *t;
  t = r = (char *) xmalloc (strlen (s) + 1);
  while (*s)
    *t++ = tolower (*s++);
  *t = 0;
  return r;
}

#if ! defined(HAVE_STRCASESTR)

/* case-insensitive strstr() */
const char *
strcasestr (const char *u, const char *v)
{
  char *uu, *vv, *ret;
  int i;
  uu = strlowera (u);
  vv = strlowera (v);
  ret = strstr (uu, vv);	/* ret is a pointer inside uu.  */
  if (!ret)
    {
      free (uu);
      free (vv);
      return NULL;
    }
  else
    {
      i = ret - uu;		/* i is an index.  */
      free (uu);
      free (vv);
      return u + i;		/* u+i is a pointer inside u.  */
    }
}

#endif

/* I don't know why, but these seem to not always work */

#if 0
/* Change a whole string to lower case */
int
strnlower (char *dest, const char *src, int len)
{
  int index;

  for (index = 0; index < len; index++)
    {
      dest[index] = tolower (src[index]);
      if (src[index] == '\0')
	return index;
    }

  return index;
}
#endif

#if 0
/* Case insensitive substring search with size limits*/
int
strncasestr (const char *haystack, int haystacklen,
	     const char *needle, int needlelen)
{
  char *haystacklower, *needlelower;
  int r;

  haystacklower = (char *) xmalloc (strlen (haystack) + 1);
  needlelower = (char *) xmalloc (strlen (needle) + 1);

  strnlower (haystacklower, haystack, haystacklen);
  strnlower (needlelower, needle, needlelen);

  r = strstr (haystacklower, needlelower) ? 1 : 0;
  free (haystacklower);
  free (needlelower);

  return r;
}
#endif

#if 0
/* Case insensitive substring search */
int
strcasestr (const char *haystack, const char *needle)
{
  int r;
  r = strncasestr (haystack, strlen (haystack), needle, strlen (needle));
  printf ("strcasestr({%s}, {%s}) = %d\n", haystack, needle, r);
  return r;
}
#endif



static int
space_needed (int wlen, struct pprintfd *ppd)
{
  int ret = wlen;
  if (ppd->col)
    ret += ppd->liad ? 2 : 1;
  return ret;
}

static void
print_word (int width, char *w, struct pprintfd *ppd)
{
  int l;
  int needed;

  l = strlen (w);

  needed = space_needed (l, ppd);
  if (ppd->col && ppd->col + needed > width)
    {
      putchar ('\n');
      ppd->col = 0;
      needed = space_needed (l, ppd);
    }

  if (ppd->col)
    {
      if (ppd->liad)
	putchar (' ');
      putchar (' ');
    }

  fputs (w, stdout);

  ppd->col += needed;

  ppd->liad = w[l - 1] == '.' ? 1 : 0;
}

#if ! HAVE_VASPRINTF
# if HAVE_VSNPRINTF

/* Replacement function for vasprintf, using vsnprintf.  */
static void
my_vasprintf (char **str, char *fmt, va_list va)
{
  /* Capacity (number of allocated bytes) of *STR.  */
  size_t capacity = 1;

  /* Number of chars (except final 0) to write to STR.  */
  int total_len;

  va_list va2;

  *str = (char *) xmalloc (capacity);

  va_copy (va2, va);

  /* vsnprintf returns the strlen of the string that would be
     generated (ie: we must allocate at least strlen+1 bytes).  */
  total_len = vsnprintf (*str, capacity, fmt, va);
  if (total_len >= capacity)
    {
      capacity = total_len + 1;
      *str = (char *) xrealloc (*str, capacity);
      vsnprintf (*str, capacity, fmt, va2);
    }

  va_end (va);
  va_end (va2);
}

# endif	 /* HAVE_VSNPRINTF */
#endif	 /* ! HAVE_VASPRINTF */

/* These are the magic functions.  */

/* vpdprintf does a printf() with word breaks; the width is MAXCOL.
   Be careful, no \n is appended at the end of the last line, and all
   \n's are removed from FMT ! */
void
vpdprintf (int maxcol, struct pprintfd *ppd, char *fmt, va_list v)
{
  char *s, *p;
  char *word = NULL;

#if HAVE_VASPRINTF
  vasprintf (&s, fmt, v);
#elif HAVE_VSNPRINTF
  my_vasprintf (&s, fmt, v);
#elif HAVE_GLIB
  s = g_strdup_vprintf (fmt, v);
#endif

  for (p = s; *p; p++)
    {
      if (!word && !isspace (*p))	/* We have just found a new word */
	word = p;
      else if (word && isspace (*p))	/* The current word is finished */
	{
	  *p = 0;
	  print_word (maxcol, word, ppd);
	  word = NULL;
	}
    }

  if (word)
    print_word (maxcol, word, ppd);

#if HAVE_VASPRINTF
  free (s);
#else /* Strictly speaking, memory allocated by Glib should be freed
         with g_free().  */
  g_free (s);
#endif
}

void
pdprintf (int maxcol, struct pprintfd *ppd, char *fmt, ...)
{
  va_list v;
  va_start (v, fmt);
  vpdprintf (maxcol, ppd, fmt, v);
  va_end (v);
}

void
vpprintf (int maxcol, char *fmt, va_list v)
{
  struct pprintfd ppd = { 0, 0 };
  vpdprintf (maxcol, &ppd, fmt, v);
}

void
pprintf (int maxcol, char *fmt, ...)
{
  va_list v;
  va_start (v, fmt);
  vpprintf (maxcol, fmt, v);
  va_end (v);
}

/* cv2v0 converts argc+argv style parameters to argv0 style parameters */
/* argv0 is a NULL-terminated array of pointers to strings (char*) */
char **
cv2v0 (int argc, char **argv)
{
  char **r;
  int i;
  r = (char **) xmalloc ((argc + 1) * sizeof (*r));	/* don't forget the
							   NULL pointer */
  for (i = 0; i < argc; ++i)
    {
      r[i] = (char *) xmalloc (strlen (argv[i]) + 1);
      strcpy (r[i], argv[i]);
    }
  r[i] = NULL;
  return r;
}

/* freev0 frees a argv0 array */
void
freev0 (char **v)
{
  char **p = v;

  if (!v)
    return;

  while (*p)
    free (*p++);
  free (v);
}

/* Tokenization with ", ', `.  This is meant to be a (limited?)
   replacement of history_tokenize ().  However we don't use
   history_tokenize() because of its (bogus?) behaviour on empty lines
   (it returns NULL).  */
char **
my_tokenize (char *s)
{
  char **p;
  char *ss = s;
  int q = 0;
  char *word = NULL;
  int p_size = 0;

  p = (char **) xmalloc (sizeof (*p) * ++p_size);
  *p = NULL;

  while (*ss)
    {
      if (!isspace (*ss) && !word)
	word = ss;
      if (isspace (*ss) && word && !q)
	{
	  *ss = 0;
	  p = (char **) xrealloc (p, sizeof (*p) * ++p_size);
	  p[p_size - 2] = (char *) xmalloc (strlen (word) + 1);
	  strcpy (p[p_size - 2], word);
	  p[p_size - 1] = NULL;
	  word = NULL;
	}
      else if (*ss == '\'' || *ss == '\"' || *ss == '`')
	{
	  if (q == *ss)
	    q = 0;
	  else
	    q = *ss;
	}
      else if (*ss == '\\')
	ss++;

      ss++;
    }

  if (word)
    {
      p = (char **) xrealloc (p, sizeof (*p) * ++p_size);
      p[p_size - 2] = (char *) xmalloc (strlen (word) + 1);
      strcpy (p[p_size - 2], word);
      p[p_size - 1] = NULL;
    }

  return p;
}

/*
  It should be easier to understand this with some examples:
  hello "           becomes hello (there is no need to quote)
  hello world "     becomes "hello world"
  hello world '     becomes 'hello world'
  it's nice "       becomes "it's nice"
  it's nice '       becomes 'it\'s nice'
  dumb thing\'"` `  becomes `dumb thing\\'"\`` (only \ and ` are backslashed)

  Q must be one of ', ", `.

  This function has not been extensively tested, it should behave like
  the previous examples.  If not, correct the function, not the examples!
*/
char *
quote_armor (char *s, char q)
{
  char *ret, *t;
  int need_quotes = 0;

  {
    /* Add quotes if and only if the following characters are in S */
    char *forbidden = " \t\\'\"`";
    int i;
    for (i = 0; forbidden[i]; ++i)
      if (strchr (s, forbidden[i]))
	{
	  need_quotes = 1;
	  break;
	}
  }

  /* At max, s is filled with q or '\', so everything must be
     backslashed: strlen(s) * 2 */
  t = ret = (char *) xmalloc (2 * strlen (s) + (need_quotes ? 2 : 0) + 1);

  if (need_quotes)
    *t++ = q;

  while (*s)
    {
      if (*s == '\\' || *s == q)
	{
	  /* We know that need_quotes is set to 1 because there is '\\'
	     or q inside.  */
	  *t++ = '\\';
	  *t++ = *s++;
	}
      else
	*t++ = *s++;
    }

  if (need_quotes)
    *t++ = q;

  *t = 0;
  ret = (char *) xrealloc (ret, strlen (ret) + 1);	/* shrink */
  return ret;
}

/* Unquote one string: suppress non-protected quotes and some
   backslashes */
char *
unquote_armor (char *s)
{
  int q = 0;
  char *ret;
  char *p;
  p = ret = (char *) xmalloc (strlen (s) + 1);

  while (*s)
    {
      if (*s == '\'' || *s == '\"' || *s == '`')
	{
	  if (q == *s)
	    {
	      s++;
	      q = 0;
	    }
	  else if (!q)
	    {
	      q = *s;
	      s++;
	    }
	  else
	    *p++ = *s++;
	}
      else if (*s == '\\')
	{
	  s++;
	  if (*s)
	    *p++ = *s++;
	}
      else
	*p++ = *s++;
    }

  *p = 0;
  return ret;
}

/* Unquote an entire parameter list */
char **
unquote_params (char **argv)
{
  char **ret;
  int argc;

  /* Count upwards... */
  for (argc = 0; argv[argc]; argc++)
    ;

  ret = (char **) xmalloc (sizeof (*ret) * (argc + 1));
  ret[argc] = NULL;

  if (!argc)
    return ret;

  /* This only works if argc > 0.  */

  /* ... and downwards.  */
  for (; argc; --argc)
    ret[argc - 1] = unquote_armor (argv[argc - 1]);

  return ret;
}