summaryrefslogtreecommitdiffstats
path: root/src/common.c
blob: 19ec4f1ba2a146b27f8dc1a609fd8ca9e3abd8e3 (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
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
/* --------------------------------------------------------------------------
 * Copyright 2003-2014 (inclusive) Nathan Angelacos 
 *                   (nangel@users.sourceforge.net)
 * 
 *   This file is part of haserl.
 *
 *   Haserl is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2,
 *   as published by the Free Software Foundation.
 *
 *   Haserl 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 haserl.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ------------------------------------------------------------------------ */
#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "common.h"
#include "h_error.h"


/* we define this here, but should use the header files instead. */
void *xrealloc (void *buf, size_t size);


/*
 * split a string into an argv[] array, and return the number of elements.
 * Warning:  Overwrites instr with nulls (to make ASCIIZ strings) and mallocs
 * space for the argv array.  The argv array will point to the offsets in
 * instr where the elements occur.  The calling function must free the argv
 * array, and should do so before freeing instr memory.
 *
 * If comment points to a non-null string, then any character in the string
 * will mark the beginning of a comment until the end-of-line:
 *
 * comment="#;"
 * foo bar     # This is a comment
 * foo baz     ; This is a comment as well
 *
 * Example of use:
 *
 * int argc, count; argv_t *argv; char string[2000];
 *
 * strcpy (string, "This\\ string will be \"separated into\" '6' elements.", "");
 * argc = argc_argv (string, &argv);
 * for (count = 0; count < argc; count++) {
 * 	printf ("%03d: %s\n", count, argv[count].string);
 * 	}
 * free (argv);
 *
 */


int
argc_argv (char *instr, argv_t ** argv, char *commentstr)
{
  char quote = '\0';
  int arg_count = 0;
  enum state_t
  {
    WHITESPACE, WORDSPACE, TOKENSTART
  } state = WHITESPACE;
  argv_t *argv_array = NULL;
  int argc_slots = 0;
  size_t len, pos;

  len = strlen (instr);
  pos = 0;
  char quoted = 0;

  while (pos < len)
    {

      // printf ("%3d of %3d: %s\n", pos, len, instr);
 
      /* Comments are really, really special */
      if ((state == WHITESPACE) && (strchr (commentstr, *instr)))
	{
	  while ((*instr != '\n') && (*instr != '\0'))
	    {
	      instr++;
	      pos++;
	    }
	}

      switch (*instr)
	{

	  /* quoting */
	case '"':
	case '\'':
	  /* Begin quoting */
	  if (state == WHITESPACE)
	    {
	      quote = *instr;
	      state = TOKENSTART;
	      quoted = -1;
	      if (*(instr + 1) == quote)
		{		/* special case for NULL quote */
		  *instr = '\0';
		}
	      else
		{
		  instr++;
		  pos++;
		}
	    }
	  else
	    {			/* WORDSPACE, so quotes end or quotes within quotes */
	      /* Is it the same kind of quote? */
	      if ( (*instr == quote) && quoted )
		{
		  quote = '\0';
		  *instr = '\0';
		  state = WHITESPACE;
		}
	    }
	  break;

	  /* backslash - if escaping a quote within a quote  */
	case '\\':
	  if ((quote) && (*(instr + 1) == quote))
	    {
	      memmove (instr, instr + 1, strlen (instr));
	      len--;
	    }
	  /* otherwise, its just a normal character */
	  else
	    {
	      if (state == WHITESPACE)
		{
		  state = TOKENSTART;
		}
	    }
	  break;


	  /* whitepsace */
	case ' ':
	case '\t':
	case '\r':
	case '\n':
	  if ((state == WORDSPACE) && (quote == '\0'))
	    {
	      state = WHITESPACE;
	      *instr = '\0';
	    }
	  break;

	case '\0':
	  break;

	default:
	  if (state == WHITESPACE)
	    {
	      state = TOKENSTART;
	    }

	}			/* end switch */

      if (state == TOKENSTART)
	{
	  arg_count++;
	  if (arg_count > argc_slots)
	    {
	      argc_slots += ALLOC_CHUNK;
	      argv_array =
		(argv_t *) xrealloc (argv_array,
				     sizeof (argv_t) * (argc_slots +
							ALLOC_CHUNK));
	    }

	  if (argv_array == NULL)
	    {
	      return (-1);
	    }
	  argv_array[arg_count - 1].string = instr;
	  argv_array[arg_count - 1].quoted = quoted;
	  state = WORDSPACE;
	  quoted = 0;
	}

      instr++;
      pos++;
    }

  if ( arg_count == 0 ) return (0);

  argv_array[arg_count].string = NULL;
  *argv = argv_array;
  return (arg_count);
}

/* Expandable Buffer is a reimplementation based on buffer.c in GCC 
   originally by Per Bother */

void
haserl_buffer_init (buffer_t * buf)
{
  buf->data = NULL;
  buf->ptr = NULL;
  buf->limit = NULL;
}

void
buffer_destroy (buffer_t * buf)
{
  if (buf->data)
    {
      free (buf->data);
    }
  haserl_buffer_init (buf);
}

/* don't reallocate - just forget about the current contents */
void
buffer_reset (buffer_t * buf)
{
  if (buf->data)
    {
      buf->ptr = buf->data;
    }
  else
    {
      buf->ptr = NULL;
    }
}


void
buffer_add (buffer_t * buf, const void *data, unsigned long size)
{
  unsigned long newsize;
  unsigned long index;

  /* if we need to grow the buffer, do so now */
  if ((buf->ptr + size) >= buf->limit)
    {
      index = (buf->limit - buf->data);
      newsize = index;
      while (newsize <= index + size)
	{
	  newsize += 1024;
	}
      index = buf->ptr - buf->data;
      buf->data = realloc (buf->data, newsize);
	if ( buf->data == NULL ) 
          {
	   die_with_message ( NULL, NULL, "Memory allocation error");
          }
      buf->limit = buf->data + newsize;
      buf->ptr = buf->data + index;
    }

  memcpy (buf->ptr, data, size);
  buf->ptr += size;
}

#ifndef JUST_LUACSHELL

/* uppercase an entire string, using toupper */
void
uppercase (char *instr)
{
  while (*instr != '\0')
    {
      *instr = toupper (*instr);
      instr++;
    }
}


/* lowercase an entire string, using tolower */
void
lowercase (char *instr)
{
  while (*instr != '\0')
    {
      *instr = tolower (*instr);
      instr++;
    }
}

/* return ptr to first non-whitespace character */
char *
skip_whitespace (char *instr)
{
  while (isspace (*instr) && *instr)
    instr++;
  return instr;
}


/* return ptr to first whitespace character */
char *
find_whitespace (char *instr)
{
  while (!isspace (*instr) && *instr)
    instr++;
  return instr;
}



/* Counts the number of newlines in a buffer */
int
count_lines (char *instr, size_t len, char *where)
{
  size_t line = 1;
  while ((where > instr) && (len))
    {
      if (*instr == '\n')
	line++;
      len--;
      instr++;
    }
  return line;
}

#endif

#ifdef TEST_FRAMEWORK

main ()
{

  int argc, count;
  argv_t *argv;
  char string[2000];


  strcpy (string,
	  "\\This\\ string will be  '' \"separated into\"  \"'\\\"'\" ' 16 ' elements.\n"
	  "' including a multi-line\n"
	  "element' with a comment.  # This should not be parsed\n"
	  ";Nor should this\n" "The End.");


  argc = argc_argv (string, &argv, "#;");
  printf ("%s\n", string);

  for (count = 0; count < argc; count++)
    {
      printf ("%03d: [%s] ", count, argv[count].string, "");
      if (argv[count].quoted)
	{
	  printf ("(it was quoted)");
	}
      printf ("\n");
    }
  if (argc != 15)
    {
      puts ("Test FAILED");
    }
  else
    {
      puts ("Test PASSED");
    }
  free (argv);
}

#endif