source: mainline/uspace/lib/pcut/src/preproc.c@ 1433ecda

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1433ecda was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2014 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <ctype.h>
32#include <string.h>
33
34#define MAX_IDENTIFIER_LENGTH 256
35
36static int counter = 0;
37
38static void print_numbered_identifier(int value, FILE *output)
39{
40 fprintf(output, "pcut_item_%d", value);
41}
42
43static void print_numbered_identifier2(int value, FILE *output)
44{
45 fprintf(output, "pcut_item2_%d", value);
46}
47
48static void print_numbered_identifier3(int value, FILE *output)
49{
50 fprintf(output, "pcut_item3_%d", value);
51}
52
53typedef struct {
54 char name[MAX_IDENTIFIER_LENGTH];
55 size_t length;
56} identifier_t;
57
58static void identifier_init(identifier_t *identifier)
59{
60 identifier->name[0] = 0;
61 identifier->length = 0;
62}
63
64static void identifier_add_char(identifier_t *identifier, char c)
65{
66 if (identifier->length + 1 >= MAX_IDENTIFIER_LENGTH) {
67 fprintf(stderr, "Identifier %s is too long, aborting!\n", identifier->name);
68 exit(1);
69 }
70
71 identifier->name[identifier->length] = c;
72 identifier->length++;
73 identifier->name[identifier->length] = 0;
74}
75
76static void identifier_print_or_expand(identifier_t *identifier, FILE *output)
77{
78 const char *name = identifier->name;
79 if (strcmp(name, "PCUT_ITEM_NAME") == 0) {
80 print_numbered_identifier(counter, output);
81 } else if (strcmp(name, "PCUT_ITEM_NAME_PREV") == 0) {
82 print_numbered_identifier(counter - 1, output);
83 } else if (strcmp(name, "PCUT_ITEM_COUNTER_INCREMENT") == 0) {
84 counter++;
85 } else if (strcmp(name, "PCUT_ITEM2_NAME") == 0) {
86 print_numbered_identifier2(counter, output);
87 } else if (strcmp(name, "PCUT_ITEM3_NAME") == 0) {
88 print_numbered_identifier3(counter, output);
89 } else {
90 fprintf(output, "%s", name);
91 }
92}
93
94static int is_identifier_char(int c, int inside_identifier)
95{
96 return isalpha(c) || (c == '_') || (inside_identifier && isdigit(c));
97}
98
99int main(int argc, char *argv[])
100{
101 FILE *input = stdin;
102 FILE *output = stdout;
103
104 int last_char_was_identifier = 0;
105 identifier_t last_identifier;
106
107 /* Unused parameters. */
108 (void) argc;
109 (void) argv;
110
111 while (1) {
112 int current_char_denotes_identifier;
113
114 int current_char = fgetc(input);
115 if (current_char == EOF) {
116 break;
117 }
118
119 current_char_denotes_identifier = is_identifier_char(current_char, last_char_was_identifier);
120 if (current_char_denotes_identifier) {
121 if (!last_char_was_identifier) {
122 identifier_init(&last_identifier);
123 }
124 identifier_add_char(&last_identifier, current_char);
125 last_char_was_identifier = 1;
126 } else {
127 if (last_char_was_identifier) {
128 identifier_print_or_expand(&last_identifier, output);
129 }
130 fprintf(stdout, "%c", current_char);
131 last_char_was_identifier = 0;
132 }
133 }
134
135 if (last_char_was_identifier) {
136 identifier_print_or_expand(&last_identifier, output);
137 }
138 fprintf(output, "\n");
139
140 return 0;
141}
Note: See TracBrowser for help on using the repository browser.