File: cpp.c

package info (click to toggle)
fped 0.1%2B201210-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 948 kB
  • ctags: 1,663
  • sloc: ansic: 12,650; yacc: 1,112; sh: 974; lex: 204; makefile: 140; awk: 75
file content (217 lines) | stat: -rw-r--r-- 3,879 bytes parent folder | download | duplicates (4)
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
/*
 * cpp.c - CPP subprocess
 *
 * Written 2002-2004, 2006, 2008 by Werner Almesberger
 * Copyright 2002, 2003 California Institute of Technology
 * Copyright 2004, 2006 Werner Almesberger
 * Copyright 2008 by OpenMoko, Inc.
 *
 * 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.
 */


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "cpp.h"


const char *cpp_command = CPP;

static pid_t cpp_pid;
static int cpp_argc = 0;
static const char **cpp_argv = NULL;
static int real_stdin = -1;


void add_cpp_arg(const char *arg)
{
    if (!cpp_argc)
	cpp_argc = 1;
    cpp_argv = realloc(cpp_argv,sizeof(const char *)*(cpp_argc+1));
    if (!cpp_argv) {
	perror("realloc");
	exit(1);
    }
    if (cpp_argc == 1)
	cpp_argv[0] = cpp_command;
    if (arg) {
	arg = strdup(arg);
	if (!arg) {
	    perror("strdup");
	    exit(1);
	}
    }
    cpp_argv[cpp_argc++] = arg;
}


void add_cpp_Wp(const char *arg)
{
    char *tmp = strdup(arg);
    char *curr,*end;

    if (!tmp) {
	perror("strdup");
	exit(1);
    }
    curr = tmp;
    do {
	end = strchr(curr,',');
	if (end)
	    *end++ = 0;
	add_cpp_arg(curr);
	curr = end;
    }
    while (end);
    free(tmp);
}


static void kill_cpp(void)
{
    if (cpp_pid)
	(void) kill(cpp_pid,SIGTERM);
}


static void run_cpp(const char *name,int fd,int close_fd)
{
    char **arg;
    int fds[2];

    if (pipe(fds) < 0) {
        perror("pipe");
        exit(1);
    }
    if (name)
	add_cpp_arg(name);
    add_cpp_arg(NULL);
    cpp_pid = fork();
    if (cpp_pid < 0) {
        perror("fork");
        exit(1);
    }
    if (!cpp_pid) {
	if (close(fds[0]) < 0) {
	    perror("close");
	    exit(1);
	}
	if (close_fd != -1 && close(close_fd) < 0) {
	    perror("close");
	    exit(1);
	}
	if (fd != -1 && dup2(fd,0) < 0) {
	    perror("dup2");
	    exit(1);
	}
	if (dup2(fds[1],1) < 0) {
	    perror("dup2");
	    exit(1);
	}
	if (execvp(cpp_command,(char **) cpp_argv) < 0) {
	  /* prototype is weird */
	    perror(cpp_command);
	    exit(1);
	}
	/* not reached */
    }
    if (close(fds[1]) < 0) {
	perror("close");
	exit(1);
    }
    real_stdin = dup(0);
    if (real_stdin < 0) {
	perror("dup");
	exit(1);
    }
    if (fd != -1 && close(fd) < 0) {
	perror("close");
	exit(1);
    }
    if (dup2(fds[0],0) < 0) {
	perror("dup2");
	exit(1);
    }
    for (arg = (char **) cpp_argv+1; *arg; arg++)
	free(*arg);
    free(cpp_argv);
    cpp_argv = NULL;
    cpp_argc = 0;
}


void run_cpp_on_file(const char *name)
{
    run_cpp(name,name ? -1 : 0,-1);
    atexit(kill_cpp);
}


void run_cpp_on_string(const char *str)
{
    int fds[2];
    pid_t pid;
    int left,wrote;

    if (pipe(fds) < 0) {
        perror("pipe");
        exit(1);
    }
    run_cpp(NULL,fds[0],fds[1]);
    pid = fork();
    if (pid < 0) {
	perror("fork");
	exit(1);
    }
    if (!pid) {
	for (left = strlen(str); left; left -= wrote) {
	    wrote = write(fds[1],str,left);
	    if (wrote < 0)
		break; /* die silently */
	    str += wrote;
	}
	exit(0);
    }
    if (close(fds[1]) < 0) {
	perror("close");
	exit(1);
    }
    atexit(kill_cpp);
}


void reap_cpp(void)
{
    int status;

    cpp_pid = 0;
    if (waitpid(cpp_pid,&status,0) < 0) {
	perror("waitpid");
	exit(1);
    }
    if (!status) {
	if (dup2(real_stdin,0) < 0) {
	    perror("dup2");
	    exit(1);
	}
	return;
    }
    if (WIFEXITED(status))
	exit(WEXITSTATUS(status));
    if (WIFSIGNALED(status))
	fprintf(stderr,"cpp terminated with signal %d\n",WTERMSIG(status));
    else
	fprintf(stderr,"cpp terminated with incomprehensible status %d\n",
	  status);
    exit(1);
}