summaryrefslogtreecommitdiffstats
path: root/funtools/funcalc.c
blob: 835b3fadf2c86c9e89927036b8bc7814a6f7cb4f (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
/*
 *	Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
 */

#if HAVE_CONFIG_H
#include <conf.h>
#endif
#include <funtools.h>
#include <file.h>
#include <find.h>
#include <word.h>
#ifdef USE_LAUNCH
#include <xlaunch.h>
#endif
#include <mkrtemp.h>
#include <calc.h>

extern char *optarg;
extern int optind;

#ifndef FUN_INCLUDE
#define FUN_INCLUDE "-I."
#endif

#ifndef FUN_LIB
#define FUN_LIB "-L. -lfuntools -lsocket -ldl -lm"
#endif

#ifdef ANSI_FUNC
static void
usage (char *fname)
#else
static void usage(fname)
     char *fname;
#endif
{
  fprintf(stderr,
  "usage: %s [-n] [-a 'args'] [-e expr] [-f file] [-l libs] [-p prog] [-u] iname [oname [columns]]\n",
  fname);
  fprintf(stderr, "optional switches:\n");
  fprintf(stderr, "  -a 'args'     # arguments to add to program command line\n");
  fprintf(stderr, "  -e 'expr'     # funcalc expression to execute\n");
  fprintf(stderr, "  -f [filename] # filename containing funcalc expression\n");
  fprintf(stderr, "  -l [libs]     # extra libraries to link\n");
  fprintf(stderr, "  -n            # write program to stdout instead of executing it \n");
  fprintf(stderr, "  -p [prog]     # create prog instead of executing it\n");
  fprintf(stderr, "  -u            # no auto-define: die if variable is undefined\n");
  fprintf(stderr, "\n(version: %s)\n", FUN_VERSION);
  exit(1);
}

#ifdef ANSI_FUNC
int 
main (int argc, char **argv)
#else
int
main(argc, argv)
     int argc;
     char **argv;
#endif
{
  int i, j=0;
  int dosave = 0;
  int c, args;
  int got, len, doexe;
  int exprlen, autolen;
  int tries;
  int error;
  int fd;
  char *s=NULL;
  char *cc, *cflags, *funpath;
  char *expr, *argstr, *libs, *autodefs, *codestr=NULL;
  char *tmpdir;
  char *iname=NULL, *oname=NULL;
  char prefix[SZ_LINE];
  char code[SZ_LINE], prog[SZ_LINE], log[SZ_LINE];
  char cmd[SZ_LINE], cmd2[SZ_LINE];
  char tbuf[SZ_LINE];
  char **nargv;
#ifdef USE_LAUNCH
  char *stdfiles[3];
#endif
  FILE *fp;

  /* exit on gio errors */
  if( !getenv("GERROR")  )
    setgerror(2);
  
  /* init */
  tries = 0;
  doexe = 1;
  expr = NULL;
  argstr = NULL;
  autodefs = xstrdup("");
  libs = xstrdup("");
  error = 0;
  *prog = '\0';

  /* we want the args in the same order in which they arrived, and
     gnu getopt sometimes changes things without this */
  putenv("POSIXLY_CORRECT=true");

  /* process switch arguments */
  while ((c = getopt(argc, argv, "a:1e:f:hl:np:u")) != -1){
    switch(c){
    case '1':
      tries = 1;
      break;
    case 'a':
      argstr = xstrdup(optarg);
      break;
    case 'e':
      expr = xstrdup(optarg);
      break;
    case 'f':
      expr = FileContents(optarg, 0, NULL);
      if( !expr || !*expr )
	gerror(stderr, "invalid funcalc expression file: %s\n", optarg);
      break;
    case 'h':
      usage(argv[0]);
      break;
    case 'l':
      libs = xstrdup(optarg);
      break;
    case 'n':
      doexe = 0;
      break;
    case 'p':
      strncpy(prog, optarg, SZ_LINE-1);
      doexe = 0;
      dosave = 1;
      break;
    case 'u':
      tries = 1;
      break;
    }
  }

  /* check for required arguments */
  args = argc - optind;
  if( args == 0 ) usage(argv[0]);
  iname = argv[optind+0];
  if( args >= 2 )
    oname = argv[optind+1];

  /* if an expression has not been supplied, read stdin */
  if( !expr || !*expr ){
    exprlen=0;
    while( fgets(tbuf, SZ_LINE-1, stdin) )
      _CalcCat(tbuf, &expr, &exprlen);
  }

  /* make sure we have something to work with */
  if( !expr || !*expr ) usage(argv[0]);

  /* generate code and exec */
  funpath = (char *)getenv("PATH");
  if( !(cc = xstrdup(getenv("FUNCALC_CC")))			&&
      !(cc = xstrdup(FUNCALC_CC))				&&
      !(cc = xstrdup(getenv("CC"))) 	        		&&
      !(cc = Find("gcc", "x", NULL, funpath))   		&&
      !(cc = Find("cc", "x", NULL, "."))  			&&
      !(cc = Find("cc", "x", NULL, FUNCALC_PATH)) 		){
    gerror(stderr, "no compiler for program generation\n");
  }
  
  /* get cflags */
  if( !(cflags = xstrdup(getenv("FUNCALC_CFLAGS")))	&&
      !(cflags = xstrdup(FUNCALC_CFLAGS))		)
    cflags = xstrdup(" ");

  /* get prefix for filter source and program */
  if( !(tmpdir = (char *)getenv("FUNCALC_TMPDIR")) &&
      !(tmpdir = (char *)getenv("TMPDIR"))         &&
      !(tmpdir = (char *)getenv("TMP"))             )
    tmpdir = DEFAULT_FUNCALC_TMPDIR;
  if( !*tmpdir )
    tmpdir = ".";
  snprintf(prefix, SZ_LINE-1, "%s/c", tmpdir);

again:
  /* make up name of C source file we will generate */
  if( (fd=mkrtemp(prefix, ".c", code, SZ_LINE, 1)) < 0 ){
    fprintf(stderr, "ERROR: could not generate C funcalc source name: %s\n",
	    prefix);
    error = 1;
    goto endgame;
  }

  /* make up the name of the program we will compile into.
     we make this different from the .c file name to make interception
     by an intruder harder */
  if( !*prog ){
    if( mkrtemp(prefix, NULL, prog, SZ_LINE, 0) < 0 ){
      fprintf(stderr, "ERROR: could not generate C funcalc program name: %s\n",
	      prefix);
      error = 1;
      goto endgame;
    }
  }

  /* make up the command strings */
  snprintf(cmd, SZ_LINE-1, "%s %s %s -o %s %s %s %s", 
	   cc, cflags, FUN_INCLUDE, prog, code, libs, FUN_LIB);
  snprintf(cmd2, SZ_LINE-1, "%s %s %s -o <prog> <this file>.c %s %s",
	   cc, cflags, FUN_INCLUDE, libs, FUN_LIB);

  /* parse expression */
  if(!(codestr = FunCalcParse(iname, oname, cmd2, expr, autodefs, args))){
    fprintf(stderr, "ERROR: no program generated!\n");
    error = 1;
    goto endgame;
  }

  /* get stdio handle and output the generated code */
  if( !(fp = fdopen(fd, "w+b")) ){
    fprintf(stderr, "ERROR: could not open funcalc source file: %s\n", code);
    error = 1;
    goto endgame;
  }
  fprintf(fp, "%s\n", codestr);
  fclose(fp);

  /* make log file name */
  snprintf(log, SZ_LINE-1, "%s.log", prog);

  /* issue the shell command to compile the program */
#ifdef USE_LAUNCH
  stdfiles[0] = NULL;
  stdfiles[1] = "/dev/null";
  stdfiles[2] = log;
  got = Launch(cmd, 1, stdfiles, NULL);
#else
  snprintf(tbuf, SZ_LINE-1, " 1>/dev/null 2>%s", log);
  strcat(cmd, tbuf);
  got = system(cmd);
#endif
  /* now we can see if we succeeded in issuing the command */
  if( got < 0 ){
    fprintf(stderr, "could not run funcalc compilation\n");
    error = 1;
    goto endgame;
  }
  
  /* if we have no executable program, we failed */
  if( !(s=Find(prog, "x", NULL, funpath)) ){
    if( autodefs ){
      xfree(autodefs);
      autodefs = NULL;
    }
    autolen = 0;
    if( s ){
      xfree(s);
      s = NULL;
    }
    /* first time through, see if we can auto-define undefined variables */
    if( !tries ){
      if( (s=Find(FUNCALC_SED, "r", NULL, funpath)) ){
	snprintf(tbuf, SZ_LINE-1,
		 "sed -n -f %s %s | awk '{print \"double\",$1\"=0.0;\"}'\n", 
		 s, log);
	if( (fp = popen(tbuf, "r")) ){
	  while( fgets(tbuf, SZ_LINE, fp) )
	    _CalcCat(tbuf, &autodefs, &autolen);
	  pclose(fp);
	  /* clean up from previous attempt */
	  if( s ){
	    xfree(s);
	    s = NULL;
	  }
	  if( codestr ){
	    xfree(codestr);
	    codestr = NULL;
	  }
	  unlink(code);
	  unlink(log);
	  tries++;
	  goto again;
	}
      }
    }
    fprintf(stderr, "ERROR: funcalc compilation failed\n");
    if( s ){
      xfree(s);
      s = NULL;
    }
    s = FileContents(log, 0, &len);
    if( s && *s && len )
      fprintf(stderr, "Compilation error message:\n%s\n", s);
    error = 1;
    goto endgame;
  }
  
endgame:
  /* done with the expression */
  if( autodefs ) xfree(autodefs);
  if( expr )     xfree(expr);
  if( s )        xfree(s);
  if( libs )     xfree(libs);
  if( cc )       xfree(cc);
  if( cflags )   xfree(cflags);
  if( argstr && !doexe )   xfree(argstr);

  /* delete the program body */
  if( !(s=getenv("FUNCALC_KEEP")) || !istrue(s) )
    unlink(code);

  /* Sun cc can leave an extraneous .o around, which we don't want */
  strcpy(tbuf, code);
  /* change .c to .o */
  tbuf[strlen(tbuf)-1] = 'o';
  unlink(tbuf);
  /* ... actually its usually left in the current directory */
  if( (s = strrchr(tbuf, '/')) )
    unlink(s+1);

  /* Sun cc can leave an extraneous .o around, which we don't want */
  snprintf(tbuf, SZ_LINE-1, "%s.o", prog);
  unlink(tbuf);
  /* ... actually its usually left in the current directory */
  if( (s = strrchr(tbuf, '/')) )
    unlink(s+1);
  
  /* delete log file */
  unlink(log);

  /* finally -- we have an executable and no errors, execute it */
  if( doexe ){
    if( codestr ) xfree(codestr);
    if( !error ){
      nargv = xcalloc(args+6, sizeof(char *));
      nargv[0] = prog;
      nargv[1] = "-d";
      if( argstr && *argstr ){
	nargv[2] = "-a";
	nargv[3] = argstr;
	j = 2;
      }
      for(i=0; i<args; i++){
	nargv[i+j+2] = argv[optind+i];
	nargv[i+j+3] = NULL;
      }
      execv(prog, (void *)nargv);
    }
  }
  else{
    /* output and delete program, if necesssary */
    if( !dosave ){
      /* output generated code */
      fprintf(stdout, "%s\n", codestr);
      /* delete executable */
      unlink(prog);
    }
    if( codestr ) xfree(codestr);
  }
  return(0);
}