~jamesodhunt/ubuntu/raring/upstart/1.6

« back to all changes in this revision

Viewing changes to nih-dbus-tool/symbol.c

  • Committer: Scott James Remnant
  • Date: 2010-02-04 23:59:00 UTC
  • mfrom: (1185.1.9 upstream)
  • Revision ID: scott@netsplit.com-20100204235900-c0j0frow10azwsus
* New upstream release:
  - libnih has been separated out into its own project.
  - "start on" and "stop on" now support != matches.  LP: #513035.
  - Fixed crash in child when unable to spawn job.  LP: #451917.
  - No longer holds /dev/console open so SAK won't kill init.  LP: #486005.
  - Added missing OPTIONS section to init(8).  LP: #449883.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* nih-dbus-tool
2
 
 *
3
 
 * symbol.c - C symbol generation and validation
4
 
 *
5
 
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
6
 
 * Copyright © 2009 Canonical Ltd.
7
 
 *
8
 
 * This program is free software; you can redistribute it and/or modify
9
 
 * it under the terms of the GNU General Public License version 2, as
10
 
 * published by the Free Software Foundation.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License along
18
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 
 */
21
 
 
22
 
#ifdef HAVE_CONFIG_H
23
 
# include <config.h>
24
 
#endif /* HAVE_CONFIG_H */
25
 
 
26
 
 
27
 
#include <ctype.h>
28
 
#include <stdarg.h>
29
 
#include <string.h>
30
 
 
31
 
#include <nih/macros.h>
32
 
#include <nih/alloc.h>
33
 
#include <nih/string.h>
34
 
#include <nih/logging.h>
35
 
#include <nih/error.h>
36
 
 
37
 
#include "symbol.h"
38
 
 
39
 
 
40
 
/* Prototypes for static functions */
41
 
static char *symbol_strcat_interface (char **str, const void *parent,
42
 
                                      const char *format, ...)
43
 
        __attribute__ ((format (printf, 3, 4), warn_unused_result, malloc));
44
 
static char *symbol_strcat_title     (char **str, const void *parent,
45
 
                                      const char *format, ...)
46
 
        __attribute__ ((format (printf, 3, 4), warn_unused_result, malloc));
47
 
 
48
 
 
49
 
/**
50
 
 * symbol_valid:
51
 
 * @symbol: Symbol to verify.
52
 
 *
53
 
 * Verifies whether @symbol matches the rules for C symbol names.  To make
54
 
 * things easier for ourselves, we only support a subset of what C99 can
55
 
 * really support. ie. no universal character names.
56
 
 *
57
 
 * Returns: TRUE if valid, FALSE if not.
58
 
 **/
59
 
int
60
 
symbol_valid (const char *symbol)
61
 
{
62
 
        nih_assert (symbol != NULL);
63
 
 
64
 
        /* We can get away with just using strlen() here even through symbol
65
 
         * is in UTF-8 because all the valid characters are ASCII.
66
 
         */
67
 
        for (size_t i = 0; i < strlen (symbol); i++) {
68
 
                /* Symbols may contain digits, but not at the beginning. */
69
 
                if ((symbol[i] >= '0') && (symbol[i] <= '9')) {
70
 
                        if (i == 0)
71
 
                                return FALSE;
72
 
 
73
 
                        continue;
74
 
                }
75
 
 
76
 
                /* Valid characters anywhere are [A-Za-z_] */
77
 
                if (   ((symbol[i] < 'A') || (symbol[i] > 'Z'))
78
 
                    && ((symbol[i] < 'a') || (symbol[i] > 'z'))
79
 
                    && (symbol[i] != '_'))
80
 
                        return FALSE;
81
 
        }
82
 
 
83
 
        /* Symbol must be at least 1 character */
84
 
        if (strlen (symbol) < 1)
85
 
                return FALSE;
86
 
 
87
 
        return TRUE;
88
 
}
89
 
 
90
 
 
91
 
/**
92
 
 * symbol_from_name:
93
 
 * @parent: parent object of new string,
94
 
 * @name: name to convert.
95
 
 *
96
 
 * Converts the D-Bus style name @name to C style; basically the name
97
 
 * is lower-cased, and underscores inserted between CamelCase words.
98
 
 *
99
 
 * If @parent is not NULL, it should be a pointer to another object which
100
 
 * will be used as a parent for the returned string.  When all parents
101
 
 * of the returned string are freed, the returned string will also be
102
 
 * freed.
103
 
 *
104
 
 * Returns: newly allocated string or NULL if allocation fails.
105
 
 **/
106
 
char *
107
 
symbol_from_name (const void *parent,
108
 
                  const char *name)
109
 
{
110
 
        char * symbol;
111
 
        char * ptr;
112
 
        size_t len = 0;
113
 
 
114
 
        nih_assert (name != NULL);
115
 
 
116
 
        /* Figure out how long the symbol will need to be by counting
117
 
         * how many places we need to add underscores and adding them
118
 
         * to the length.
119
 
         */
120
 
        len = strlen (name);
121
 
        for (size_t i = 0; i < strlen (name); i++)
122
 
                if ((i > 0) && (name[i] >= 'A') && (name[i] <= 'Z')
123
 
                    && (name[i-1] != '_')
124
 
                    && ((name[i-1] < 'A') || (name[i-1] > 'Z')))
125
 
                        len++;
126
 
 
127
 
        /* Allocate the new string */
128
 
        symbol = nih_alloc (parent, len + 1);
129
 
        if (! symbol)
130
 
                return NULL;
131
 
 
132
 
        /* Copy the characters across, lowercasing as we go and inserting
133
 
         * underscores before any capital unless it follows an underscore.
134
 
         */
135
 
        ptr = symbol;
136
 
        for (size_t i = 0; i < strlen (name); i++) {
137
 
                if ((i > 0) && (name[i] >= 'A') && (name[i] <= 'Z')
138
 
                    && (name[i-1] != '_')
139
 
                    && ((name[i-1] < 'A') || (name[i-1] > 'Z')))
140
 
                        *(ptr++) = '_';
141
 
 
142
 
                *(ptr++) = tolower (name[i]);
143
 
        }
144
 
 
145
 
        *ptr = '\0';
146
 
 
147
 
        return symbol;
148
 
}
149
 
 
150
 
 
151
 
/**
152
 
 * symbol_strcat_interface:
153
 
 * @str: pointer to string to modify,
154
 
 * @parent: parent object of new string,
155
 
 * @format: format to append to @str.
156
 
 *
157
 
 * Replaces periods in the string expanded from @format with underscores
158
 
 * and concatenates it onto @str.  The new string is allocated using
159
 
 * nih_alloc() and @str will be updated to point to the new string; use
160
 
 * the return value simply to check for success.
161
 
 *
162
 
 * @parent is ignored; though it is usual to pass a parent of @str for
163
 
 * style reasons.
164
 
 *
165
 
 * Returns: newly allocated string or NULL if allocation fails.
166
 
 **/
167
 
static char *
168
 
symbol_strcat_interface (char **     str,
169
 
                         const void *parent,
170
 
                         const char *format,
171
 
                         ...)
172
 
{
173
 
        nih_local char *name = NULL;
174
 
        va_list         args;
175
 
        size_t          len;
176
 
        char *          ret;
177
 
 
178
 
        nih_assert (str != NULL);
179
 
        nih_assert (*str != NULL);
180
 
        nih_assert (format != NULL);
181
 
 
182
 
        va_start (args, format);
183
 
        name = nih_vsprintf (NULL, format, args);
184
 
        if (! name)
185
 
                return NULL;
186
 
        va_end (args);
187
 
 
188
 
        len = strlen (*str);
189
 
 
190
 
        ret = nih_realloc (*str, parent, len + strlen (name) + 1);
191
 
        if (! ret)
192
 
                return NULL;
193
 
 
194
 
        *str = ret;
195
 
 
196
 
        /* Copy the characters across, replacing the periods between
197
 
         * interface components with underscores.
198
 
         */
199
 
        for (char *s = name; *s; s++)
200
 
                (*str)[len++] = (*s == '.' ? '_' : *s);
201
 
 
202
 
        (*str)[len] = '\0';
203
 
 
204
 
        return *str;
205
 
}
206
 
 
207
 
/**
208
 
 * symbol_strcat_title:
209
 
 * @str: pointer to string to modify,
210
 
 * @parent: parent object of new string,
211
 
 * @format: format to append to @str.
212
 
 *
213
 
 * Expands the @format string and modifies it so that each underscore-
214
 
 * separated word has the underscores removed and the initial character
215
 
 * uppercased.  The new string is allocated using nih_alloc() and @str
216
 
 * will be updated to point to the new string; use the return value
217
 
 * simply to check for success.
218
 
 *
219
 
 * If the string pointed to by @str is NULL, a new string will be
220
 
 * allocated and if @parent is not NULL, it should be a pointer to another
221
 
 * object which will be used as a parent for the returned string.  When all
222
 
 * parents of the returned string are freed, the returned string will also
223
 
 * be freed.
224
 
 *
225
 
 * When the string pointed to by @str is not NULL, @parent is ignored;
226
 
 * though it is usual to pass a parent of @str for style reasons.
227
 
 *
228
 
 * Returns: newly allocated string or NULL if allocation fails.
229
 
 **/
230
 
static char *
231
 
symbol_strcat_title (char **     str,
232
 
                     const void *parent,
233
 
                     const char *format,
234
 
                     ...)
235
 
{
236
 
        va_list         args;
237
 
        nih_local char *symbol = NULL;
238
 
        size_t          str_len;
239
 
        size_t          len;
240
 
        char *          ret;
241
 
        int             first = TRUE;
242
 
 
243
 
        nih_assert (str != NULL);
244
 
        nih_assert (format != NULL);
245
 
 
246
 
        va_start (args, format);
247
 
        symbol = nih_vsprintf (NULL, format, args);
248
 
        if (! symbol)
249
 
                return NULL;
250
 
        va_end (args);
251
 
 
252
 
        str_len = *str ? strlen (*str) : 0;
253
 
        len = str_len;
254
 
 
255
 
        /* Figure out how long the new string will be by counting how many
256
 
         * characters that aren't underscores we'll end up adding.
257
 
         */
258
 
        for (char *s = symbol; *s; s++)
259
 
                if (*s != '_')
260
 
                        len++;
261
 
 
262
 
        ret = nih_realloc (*str, parent, len + 1);
263
 
        if (! ret)
264
 
                return NULL;
265
 
 
266
 
        *str = ret;
267
 
 
268
 
        len = str_len;
269
 
 
270
 
        /* Copy the characters across, uppercasing the first character of
271
 
         * each word and stripping underscores.
272
 
         */
273
 
        for (char *s = symbol; *s; s++) {
274
 
                if (*s == '_') {
275
 
                        first = TRUE;
276
 
                } else {
277
 
                        (*str)[len++] = (first ? toupper (*s) : *s);
278
 
                        first = FALSE;
279
 
                }
280
 
        }
281
 
 
282
 
        (*str)[len] = '\0';
283
 
 
284
 
        return *str;
285
 
}
286
 
 
287
 
/**
288
 
 * symbol_impl:
289
 
 * @parent: parent object of new string,
290
 
 * @prefix: prefix for returned symbol,
291
 
 * @interface_name: name of D-Bus interface,
292
 
 * @name: name of interface member,
293
 
 * @postfix: postfix for returned symbol.
294
 
 *
295
 
 * Generates a C symbol for an implementation function, one that is hidden
296
 
 * from the API and thus uniqueness and verboseness is more desirable than
297
 
 * readability.  The @prefix is prepended to the @interface_name and
298
 
 * member @name, with the @postfix appended.
299
 
 *
300
 
 * If @parent is not NULL, it should be a pointer to another object which
301
 
 * will be used as a parent for the returned string.  When all parents
302
 
 * of the returned string are freed, the returned string will also be
303
 
 * freed.
304
 
 *
305
 
 * Returns: newly allocated string or NULL if allocation fails.
306
 
 **/
307
 
char *
308
 
symbol_impl (const void *parent,
309
 
             const char *prefix,
310
 
             const char *interface_name,
311
 
             const char *name,
312
 
             const char *postfix)
313
 
{
314
 
        char *str;
315
 
 
316
 
        nih_assert (prefix != NULL);
317
 
        nih_assert (interface_name != NULL);
318
 
        nih_assert ((postfix != NULL) || (name == NULL));
319
 
 
320
 
        str = nih_sprintf (parent, "%s_", prefix);
321
 
        if (! str)
322
 
                return NULL;
323
 
 
324
 
        if (! symbol_strcat_interface (&str, parent, (name ? "%s_" : "%s"),
325
 
                                       interface_name)) {
326
 
                nih_free (str);
327
 
                return NULL;
328
 
        }
329
 
 
330
 
        if (name) {
331
 
                if (! nih_strcat (&str, parent, name)) {
332
 
                        nih_free (str);
333
 
                        return NULL;
334
 
                }
335
 
        }
336
 
 
337
 
        if (postfix) {
338
 
                if (! nih_strcat_sprintf (&str, parent, "_%s", postfix)) {
339
 
                        nih_free (str);
340
 
                        return NULL;
341
 
                }
342
 
        }
343
 
 
344
 
        return str;
345
 
}
346
 
 
347
 
/**
348
 
 * symbol_extern:
349
 
 * @parent: parent object of new string,
350
 
 * @prefix: prefix for returned symbol,
351
 
 * @interface_symbol: symbol for D-Bus interface,
352
 
 * @midfix: midfix for returned symbol,
353
 
 * @symbol: symbol of interface member,
354
 
 * @postfix: postfix for returned symbol.
355
 
 *
356
 
 * Generates a C symbol for an external function, one that is either part of
357
 
 * the API or intended to be supplied externally, thus where readability
358
 
 * is more desirable than uniqueness or verboseness.  The @prefix is
359
 
 * prepended to the @interface_symbol (if supplied), @midfix (if supplied),
360
 
 * member @symbol, with the @postfix (if supplied) appended.
361
 
 *
362
 
 * If @parent is not NULL, it should be a pointer to another object which
363
 
 * will be used as a parent for the returned string.  When all parents
364
 
 * of the returned string are freed, the returned string will also be
365
 
 * freed.
366
 
 *
367
 
 * Returns: newly allocated string or NULL if allocation fails.
368
 
 **/
369
 
char *
370
 
symbol_extern (const void *parent,
371
 
               const char *prefix,
372
 
               const char *interface_symbol,
373
 
               const char *midfix,
374
 
               const char *symbol,
375
 
               const char *postfix)
376
 
{
377
 
        char *str;
378
 
 
379
 
        nih_assert (prefix != NULL);
380
 
        nih_assert (symbol != NULL);
381
 
 
382
 
        str = nih_sprintf (parent, "%s_", prefix);
383
 
        if (! str)
384
 
                return NULL;
385
 
 
386
 
        if (interface_symbol) {
387
 
                if (! nih_strcat_sprintf (&str, parent, "%s_",
388
 
                                          interface_symbol)) {
389
 
                        nih_free (str);
390
 
                        return NULL;
391
 
                }
392
 
        }
393
 
 
394
 
        if (midfix) {
395
 
                if (! nih_strcat_sprintf (&str, parent, "%s_", midfix)) {
396
 
                        nih_free (str);
397
 
                        return NULL;
398
 
                }
399
 
        }
400
 
 
401
 
        if (! nih_strcat (&str, parent, symbol)) {
402
 
                nih_free (str);
403
 
                return NULL;
404
 
        }
405
 
 
406
 
        if (postfix) {
407
 
                if (! nih_strcat_sprintf (&str, parent, "_%s", postfix)) {
408
 
                        nih_free (str);
409
 
                        return NULL;
410
 
                }
411
 
        }
412
 
 
413
 
        return str;
414
 
}
415
 
 
416
 
/**
417
 
 * symbol_typedef:
418
 
 * @parent: parent object of new string,
419
 
 * @prefix: prefix for returned symbol,
420
 
 * @interface_symbol: symbol for D-Bus interface,
421
 
 * @midfix: midfix for returned symbol,
422
 
 * @symbol: symbol of interface member,
423
 
 * @postfix: postfix for returned symbol.
424
 
 *
425
 
 * Generates a C typedef name for a function that is expected to be
426
 
 * supplied, this has the same basic form as an external symbol except that
427
 
 * underscores are removed and the first letter of each part is uppercased.
428
 
 * The @prefix is prepended to the @interface_symbol (if supplied), @midfix
429
 
 * (if supplied), member @symbol, with the @postfix appended.
430
 
 *
431
 
 * If @parent is not NULL, it should be a pointer to another object which
432
 
 * will be used as a parent for the returned string.  When all parents
433
 
 * of the returned string are freed, the returned string will also be
434
 
 * freed.
435
 
 *
436
 
 * Returns: newly allocated string or NULL if allocation fails.
437
 
 **/
438
 
char *
439
 
symbol_typedef (const void *parent,
440
 
                const char *prefix,
441
 
                const char *interface_symbol,
442
 
                const char *midfix,
443
 
                const char *symbol,
444
 
                const char *postfix)
445
 
{
446
 
        char *str;
447
 
 
448
 
        nih_assert (prefix != NULL);
449
 
        nih_assert (symbol != NULL);
450
 
 
451
 
        str = NULL;
452
 
        if (! symbol_strcat_title (&str, parent, "%s_", prefix))
453
 
                return NULL;
454
 
 
455
 
        if (interface_symbol) {
456
 
                if (! symbol_strcat_title (&str, parent, "%s_",
457
 
                                           interface_symbol)) {
458
 
                        nih_free (str);
459
 
                        return NULL;
460
 
                }
461
 
        }
462
 
 
463
 
        if (midfix) {
464
 
                if (! symbol_strcat_title (&str, parent, "%s_", midfix)) {
465
 
                        nih_free (str);
466
 
                        return NULL;
467
 
                }
468
 
        }
469
 
 
470
 
        if (! symbol_strcat_title (&str, parent, "%s", symbol)) {
471
 
                nih_free (str);
472
 
                return NULL;
473
 
        }
474
 
 
475
 
        if (postfix) {
476
 
                if (! symbol_strcat_title (&str, parent, "_%s", postfix)) {
477
 
                        nih_free (str);
478
 
                        return NULL;
479
 
                }
480
 
        }
481
 
 
482
 
        return str;
483
 
}