make_fint.c 22.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif

#define _GNU_SOURCE // getline()

#include "config.h"

#include <ctype.h>
#include <regex.h>
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
11
12
13
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
14
#include <sys/types.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
15
#include <time.h>
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
16

17
18
19
20
21
22
23
24
#ifndef HAVE_GETLINE
#include "getline.c"
#endif

#ifndef VERSION
#define VERSION "1.0.0"
#endif

Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
25
26
27

typedef struct
{
28
  size_t naline;
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
29
30
31
32
33
34
35
  char *fname;
  char *aline[99];
  char *text;
}
Docu;

Docu cdoc[9999], fdoc[9999];
36
size_t ncdoc = 0, nfdoc = 0;
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

int doccmp(const void *s1, const void *s2)
{
  Docu *x = (Docu *) s1;
  Docu *y = (Docu *) s2;

  return (strcmp(x->fname, y->fname));
}

int strcompare(const char **s1, const char **s2)
{
  printf(">%s< >%s<\n", *s1, *s2);
  return (strcmp(*s1, *s2));
}

52
void doctotex(FILE *fp, Docu *doc, size_t ndoc)
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
53
{
54
  size_t i, k;
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
55
56
57
58
59
60
61
62
63
64
65
66

  for ( i = 0; i < ndoc; i++ )
    {
      fprintf(fp, "\\section*{\\tt \\htmlref{%s}{%s}}\n\n", doc[i].fname, doc[i].fname);
      fprintf(fp, "\\begin{verbatim}\n");
      for ( k = 0; k < doc[i].naline; k++ )
	fprintf(fp, "    %s\n", doc[i].aline[k]);
      fprintf(fp, "\\end{verbatim}\n");
      fprintf(fp, "\n%s.\n\n\n", doc[i].text);
    }
}

67
void doctotxt(FILE *fp, Docu *doc, size_t ndoc)
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
68
{
69
  size_t i, k;
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
70
71
72
73
74
75
76
77
78
79

  for ( i = 0; i < ndoc; i++ )
    {
      fprintf(fp, "%s\n\n", doc[i].fname);
      for ( k = 0; k < doc[i].naline; k++ )
	fprintf(fp, "    %s\n", doc[i].aline[k]);
      fprintf(fp, "\n  %s.\n\n", doc[i].text);
    }
}

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
enum {ISVOID, ISCONSTSTRING, ISINT, ISREAL, ISDOUBLE, ISINTP, ISINTV, ISINTVV, ISREALP, ISDOUBLEP, ISSTRING, ISSTRINGP, NUM_KNOWN_ARG_TYPES};

struct symbol {
  const char *f77name, *cfint, *cname, *parseRE;
  size_t nameMatch;
  regex_t preg;
};

/* C symbol names */
#define SYMRE "([A-Za-z_][A-Za-z_0-9]*)"
/* white-space */
#define WS "[[:blank:]\n]"
#define NWS "[^[:blank:]\n]"
static struct symbol funArgSym[] = {
  { "",                "",        "void",     "^"WS"*void"WS"*)", 0 },
  { "CHARACTER(80)",   "STRING",  "char *",   "^"WS"*const"WS"+char"WS"+\\*"SYMRE WS"*\\(", 1 },
  { "INTEGER",         "INT",     "int",      "^"WS"*(const"WS"+)?int("WS"+"SYMRE")?"WS"*[,\\)]", 3 },
  { "REAL",            "FLOAT",   "float",    "^"WS"*(const"WS"+)?float"WS"+"SYMRE"?"WS"*[,\\)]", 2 },
  { "DOUBLEPRECISION", "DOUBLE",  "double",   "^"WS"*(const"WS"+)?double"WS"+"SYMRE"?"WS"*[,\\)]", 2 },
  { "INTEGER",         "PINT",    "int *",    "^"WS"*(const"WS"+)?int"WS"+\\*"SYMRE"?"WS"*[,\\)]", 2 },
  { "INTEGER",         "INTV",    "int[]",    "^"WS"*(const"WS"+)?int("WS"+"SYMRE")?"WS"*\\[[^]]*\\]"WS"*[,\\)]", 3 },
  { "INTEGER",         "INTVV",    "int[][]", "^"WS"*(const"WS"+)?int("WS"+"SYMRE")?"WS"*\\[[^]]*\\]"WS"*\\[[^]]*\\]"WS"*[,\\)]", 3 },
  { "REAL",            "PFLOAT",  "float *",  "^"WS"*(const"WS"+)?float"WS"+\\*"SYMRE"?"WS"*[,\\)]", 2 },
  { "DOUBLEPRECISION", "PDOUBLE", "double *", "^"WS"*(const"WS"+)?double"WS"+\\*"SYMRE"?"WS"*[,\\)]", 2 },
  { "CHARACTER*(*)",   "STRING",  "char *",   "^"WS"*const"WS"+char"WS"+\\*"WS"*"SYMRE"?"WS"*[,\\)]", 1 },
  { "CHARACTER*(*)",   "PSTRING", "char *",   "^"WS"*char"WS"+\\*"SYMRE"?"WS"*[,\\)]", 1 },
};

static struct symbol funRet[] = {
  { "",                "",        "void",            "void"WS"+"SYMRE WS"*\\(", 1 },
  { "CHARACTER",       "STRING",  "(const) char *",  "(const"WS"+)?char"WS"+\\*"WS"*"SYMRE WS"*\\(", 2 },
  { "INTEGER",         "INT",     "int",             "(const"WS"+)?int"WS"+"SYMRE WS"*\\(", 2 },
  { "REAL",            "FLOAT",   "float",           "(const"WS"+)?float"WS"+"SYMRE WS"*\\(", 2 },
  { "DOUBLEPRECISION", "DOUBLE",  "double",          "(const"WS"+)?double"WS"+"SYMRE WS"*\\(", 2 },
};

enum { NUM_RET_TYPES = sizeof (funRet) / sizeof (funRet[0]) };
enum decl { UNKNOWN_DECL, FUNC_DECL, PARAM_DECL };

enum {
  MAX_FUNC_ARGS = 200,
};

Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
123
124
125
126
void fortran_interface(char *fname, char *fnameinc, char *fnameint)
{
  FILE *fpin, *fpinc, *fpint;
  FILE *fp;
127
128
129
  char *line = NULL, *pline;
  size_t lineBufSize = 0;
  char sname[128], *parname;
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
130
  char xname[128], xdes[128];
131
  xname[0] = 0;
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
132
133
134
  int parvalue;
  int functype;
  int lineno = 0;
135
136
  size_t len;

Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
137
  char funcname[128];
138
139
140
141
142
  regmatch_t funcargfull[MAX_FUNC_ARGS];
  regmatch_t funcargname[MAX_FUNC_ARGS];
  int  funcargtype[MAX_FUNC_ARGS];
  ssize_t funcargc;
  /* char *strsort[99999]; */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144
145
  char timestr[30];
  time_t date_and_time_in_sec;
  struct tm *date_and_time;
146
147
  regmatch_t *reMatch = NULL;
  size_t maxMatch = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149
150
151
152
153
154
155
156

  date_and_time_in_sec = time(NULL);
  timestr[0] = 0;

  if ( date_and_time_in_sec != -1 )
    {
      date_and_time = localtime(&date_and_time_in_sec);
      (void) strftime(timestr, sizeof(timestr), "%B %Y", date_and_time);
    }
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
157
158

  /*
159
  for ( i = 0; i < 8; i++ ) strsort[i] = funArgSym[i].cname;
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  for ( i = 0; i < 8; i++ ) printf("%d %d >%s<\n", i, &strsort[i], strsort[i]);
  qsort(strsort, 8, sizeof(char *), strcompare);
  for ( i = 0; i < 8; i++ ) printf("%d %d >%s<\n", i, &strsort[i], strsort[i]);

  return;
  */

  fpin = fopen(fname, "r");
  if ( fpin == NULL ) { perror(fname); return; }

  fpinc = fopen(fnameinc, "w");
  if ( fpinc == NULL ) { perror(fnameinc); return; }

  fpint = fopen(fnameint, "w");
  if ( fpint == NULL ) { perror(fnameint); return; }

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
  /* complete symbol table data */
  for (size_t argt = 0; argt < NUM_KNOWN_ARG_TYPES; ++argt)
    {
      int errcode;
      if ((errcode = regcomp(&funArgSym[argt].preg, funArgSym[argt].parseRE,
                             REG_EXTENDED)))
        {
          line = realloc(line, lineBufSize = 1024);
          if (line)
            {
              regerror(errcode, &funArgSym[argt].preg, line, lineBufSize);
              fprintf(stderr, "Error compiling regular expression: %s: %s\n",
                      funArgSym[argt].parseRE, line);
            }
          exit(EXIT_FAILURE);
        }
      if (funArgSym[argt].nameMatch > maxMatch)
        maxMatch = funArgSym[argt].nameMatch;
    }
  for (size_t retType = 0; retType < NUM_RET_TYPES; ++retType)
    {
      int errcode;
      if ((errcode = regcomp(&funRet[retType].preg, funRet[retType].parseRE,
                             REG_EXTENDED)))
        {
          line = realloc(line, lineBufSize = 1024);
          if (line)
            {
              regerror(errcode, &funRet[retType].preg, line, lineBufSize);
              fprintf(stderr, "Error compiling regular expression: %s: %s\n",
                      funRet[retType].parseRE, line);
            }
          exit(EXIT_FAILURE);
        }
      if (funArgSym[retType].nameMatch > maxMatch)
        maxMatch = funArgSym[retType].nameMatch;
    }
  ++maxMatch;
  reMatch = malloc((size_t)maxMatch * sizeof (reMatch[0]));
  /* compile comment regular expression */
  regex_t commentRE;
  {
    int errcode;
    char commentREString[] = "^"WS"*/\\*"WS"*(.*"NWS")"WS"*\\*/";
    if ((errcode = regcomp(&commentRE, commentREString, REG_EXTENDED)))
      {
        line = realloc(line, lineBufSize = 1024);
        if (line)
          {
            regerror(errcode, &commentRE, line, lineBufSize);
            fprintf(stderr, "Error compiling regular expression: %s: %s\n",
                    commentREString, line);
            exit(EXIT_FAILURE);
          }
      }
  }
  /* compile documentation comment regular expression */
  regex_t docCommentRE;
  {
    int errcode;
    char docCommentREString[] = "^"WS"*/\\*"WS"*"SYMRE":"
      WS"*("NWS".*"NWS")"WS"*\\*/";
    if ((errcode = regcomp(&docCommentRE, docCommentREString, REG_EXTENDED)))
      {
        line = realloc(line, lineBufSize = 1024);
        if (line)
          {
            regerror(errcode, &commentRE, line, lineBufSize);
            fprintf(stderr, "Error compiling regular expression: %s: %s\n",
                    docCommentREString, line);
            exit(EXIT_FAILURE);
          }
      }
  }
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
250
251
  /* fortran include */

Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
  fprintf(fpinc, "! This file was automatically generated, don't edit!\n");
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
253
254
  fprintf(fpinc, "!\n");
  fprintf(fpinc, "! Fortran interface for CDI library version %s\n", VERSION);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
256
257
258
  fprintf(fpinc, "!\n");
  fprintf(fpinc, "! Author:\n");
  fprintf(fpinc, "! -------\n");
  fprintf(fpinc, "! Uwe Schulzweida, MPI-MET, Hamburg,   %s\n", timestr);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
259
260
261
262
  fprintf(fpinc, "!\n\n");

  /* fortran interface */

Uwe Schulzweida's avatar
Uwe Schulzweida committed
263
  fprintf(fpint, "/* Automatically generated by make_fint.c, don't edit! */\n");
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
264
265
266
267
268
  fprintf(fpint, "\n");
  fprintf(fpint, "#if defined (HAVE_CONFIG_H)\n");
  fprintf(fpint, "#  include \"config.h\"\n");
  fprintf(fpint, "#endif\n");
  fprintf(fpint, "\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
270
271
272
  fprintf(fpint, "#if ! defined (_CDI_H)\n");
  fprintf(fpint, "#  include \"cdi.h\"\n");
  fprintf(fpint, "#endif\n");
  fprintf(fpint, "\n");
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
273
274
275
276
277
278
279
  fprintf(fpint, "#if defined (HAVE_CF_INTERFACE)\n");
  fprintf(fpint, "\n");
  fprintf(fpint, "#if ! defined (__CFORTRAN_LOADED)\n");
  fprintf(fpint, "#  include \"cfortran.h\"\n");
  fprintf(fpint, "#endif\n");
  fprintf(fpint, "\n");

280
281
  ssize_t lineLen;
  while ((lineLen = getline(&line, &lineBufSize, fpin)) >= 0)
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
282
283
284
285
    {
      lineno++;
      functype = ISVOID;
      funcargc = 0;
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
      pline = line;
      enum decl declType = UNKNOWN_DECL;
      do {
        for (int retType = 0; retType < NUM_RET_TYPES; ++retType)
          if (!regexec(&funRet[retType].preg, pline, maxMatch, reMatch, 0))
            {
              functype = retType;
              declType = FUNC_DECL;
              break;
            }
        if (declType == UNKNOWN_DECL)
          break;
        regmatch_t *nameMatch = reMatch + funRet[functype].nameMatch;
        printf("Found: %.*s\n",
               nameMatch->rm_eo - nameMatch->rm_so,
               pline + nameMatch->rm_so);
        ssize_t funNameLast = reMatch[0].rm_eo - 1;
        ssize_t nameLen = nameMatch->rm_eo - nameMatch->rm_so;
        if ( pline[funNameLast] != '(' )
          {
            printf("%s\n>(< not found!", line);
            return;
          }
        memcpy(funcname, pline + nameMatch->rm_so, (size_t)nameLen);
        funcname[nameLen] = 0;
        pline += reMatch[0].rm_eo;
      } while (0);
      if (declType == FUNC_DECL)
        {
	  /*
           * len = 0;
	   * while ( isalnum((int) pline[len]) ) len++;
           * memcpy(funcname, pline, len);
	   * funcname[len] = 0;
           */
	  /* printf("%s\n", funcname); */
	  /* pline += len; */
	  /*
           * if ( *pline != '(' )
	   *   {
	   *     printf("%s\n>(< not found!", line);
	   *     return;
	   *   }
	   * pline++;
           */

	  funcargname[funcargc].rm_so = (regoff_t)(pline - line);
          ssize_t i = 0;
          do {
            ssize_t restLen = lineLen - (ssize_t)(pline - line);
            for (; i < restLen; i++ )
              {
                if ( pline[i] == ',' )
                  {
                    funcargc++;
                    funcargname[funcargc].rm_so
                      = (regoff_t)(pline - line + i + 1);
                  }
                if ( pline[i] == ')' )
                  {
                    funcargc++;
                    funcargname[funcargc].rm_so
                      = (regoff_t)(pline - line + i + 1);
                    break;
                  }
              }
            if (i < restLen)
              break;
            char *lineExtension = NULL;
            size_t extSize = 0, plineOff = (size_t)(pline - line);
            ssize_t extLen;
            if ((extLen = getline(&lineExtension, &extSize, fpin)) <= 0)
              break;
            if ((size_t)(lineLen + extLen) >= lineBufSize)
              if (!(line = realloc(line, (size_t)(lineLen + extLen + 1))))
                exit(EXIT_FAILURE);
            memcpy(line + lineLen, lineExtension, (size_t)extLen + 1);
            lineLen += extLen;
            pline = line + plineOff;
          } while (1);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
366
367
368
369
370

	  /*  printf("funcargc = %d\n", funcargc);*/

	  for ( i = 0; i < funcargc; i++ )
	    {
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
	      pline = line + funcargname[i].rm_so;
              int argtype;
              regoff_t argStart = (regoff_t)(pline - line);
              for (argtype = ISVOID;
                   argtype < NUM_KNOWN_ARG_TYPES;
                   ++argtype)
                if (!regexec(&funArgSym[argtype].preg, pline, maxMatch,
                             reMatch, 0))
                  {
                    funcargtype[i] = argtype;
                    funcargfull[i].rm_so = reMatch[0].rm_so + argStart;
                    funcargfull[i].rm_eo = reMatch[0].rm_eo + argStart;
                    regmatch_t *nameMatch = reMatch + funArgSym[argtype].nameMatch;
                    funcargname[i].rm_so = nameMatch->rm_so + argStart;
                    funcargname[i].rm_eo = nameMatch->rm_eo + argStart;
                    break;
                  }
              if (argtype == NUM_KNOWN_ARG_TYPES)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389
		{
390
		  printf("%s not implemented\n", line + funcargname[i].rm_so);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
391
392
393
		  break;
		}
	    }
394
395
396
397
398
	  if ( i != funcargc )
            {
              printf("problem parsing line: %s\n", line);
              continue;
            }
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

	  strcpy(sname, funcname);
	  len = strlen(sname);
	  /*
	  ii = 0;
	  for ( i = 0; i < len; i++ )
	    {
	      if ( isupper((int) sname[i]) ) break;
	      if ( islower((int) sname[i]) ) sname[i] = toupper((int) sname[i]);
	    }
	  sname[len+1] = 0;
	  for ( ii = len; ii > i; ii-- )
	    {
	      sname[ii] = sname[ii-1];
	    }
	  sname[i] = '_';
	  len++;
	  */
417

Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
418
419
420
421
422
	  /* fortran include */

	  if ( functype == ISVOID )
	    fprintf(fpinc, "!     %-16s", "");
	  else
423
	    fprintf(fpinc, "      %-16s", funArgSym[functype].f77name);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
424
425
426
427
428
429
430
431
432
433

          fprintf(fpinc, "%s", sname);
	  fprintf(fpinc, "\n");
	  if ( (funcargc == 1 && funcargtype[0] == ISVOID) ) funcargc = 0;
	  for ( i = 0; i < funcargc; i++ )
	    {
	      if ( i == 0 )
		fprintf(fpinc, "!%36s(", "");
	      else
		fprintf(fpinc, ",\n!%36s ", "");
434
435
436
	      fprintf(fpinc, "%-16s%.*s", funArgSym[funcargtype[i]].f77name,
                      (int)(funcargname[i].rm_eo - funcargname[i].rm_so),
                      line + funcargname[i].rm_so);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
437
438
439
440
441
442
443
444
445
446
447
	    }
	  if ( funcargc )
	    fprintf(fpinc, ")\n");
	  fprintf(fpinc, "      %-16s%s\n\n", "EXTERNAL", sname);

	  /* fortran interface */

	  if ( functype == ISVOID )
	    fprintf(fpint, "FCALLSCSUB");
	  else
	    fprintf(fpint, "FCALLSCFUN");
448
	  fprintf(fpint, "%zd ", funcargc);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
449
450
	  fprintf(fpint, "(");
	  if ( functype != ISVOID )
451
	    fprintf(fpint, "%s, ", funArgSym[functype].cfint);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
452
	  fprintf(fpint, "%s, ", funcname);
453
	  for (size_t i = 0; i < len; i++ ) sname[i] = (char)toupper((int) sname[i]);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
454
	  fprintf(fpint, "%s, ", sname);
455
	  for (size_t i = 0; i < len; i++ ) sname[i] = (char)tolower((int) sname[i]);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
456
	  fprintf(fpint, "%s", sname);
457
	  for (ssize_t i = 0; i < funcargc; i++ )
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
458
	    {
459
460
	      fprintf(fpint, ", %s", funArgSym[funcargtype[i]].cfint);
	    }
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
461
462
463
	  fprintf(fpint, ")\n");


464
	  if ( strcmp(funcname, xname) == 0 )
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
465
466
	    {
	      char xline[128];
467
468
	      int nch;

Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
469
470
471
472
473
474
	      /* C Quick Guide */

	      cdoc[ncdoc].naline = 0;
	      cdoc[ncdoc].text   = NULL;
	      cdoc[ncdoc].fname  = strdup(funcname);

475
	      nch = sprintf(xline, "%s %s (", funArgSym[functype].cname, xname);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
476
477
478
479
480
481
482

	      if ( (funcargc == 1 && funcargtype[0] == ISVOID) ) funcargc = 0;

	      for ( i = 0; i < funcargc; i++ )
		{
		  if ( i ) strcat(xline, ", ");

483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
                  /* extract full argument text from match */
                  char farg[128];
                  /* - 1 to omit closing paren ) or comma , */
                  int nchn = snprintf(farg, sizeof (farg), "%.*s",
                                      (int)(funcargfull[i].rm_eo
                                            - funcargfull[i].rm_so - 1),
                                      line + funcargfull[i].rm_so);
                  if (nchn < 0)
                    abort();
                  /* compress white-space */
                  {
                    size_t wpos = 0;
                    size_t i = 0;
                    /* skip leading white-space */
                    while (i < (size_t)nchn
                           && (isblank(farg[i]) || farg[i] == '\n'))
                      ++i;
                    /* after the leading white-space the following is
                     * an alternation of white- and non-white-space
                     * characters, where sequences of the latter will
                     * be compressed to a single space */
                    while (i < (size_t)nchn)
                      {
                        /* handle white-space */
                        while (i < (size_t)nchn
                               && !(isblank(farg[i]) || farg[i] == '\n'))
                          farg[wpos++] = farg[i++];
                        /* skip non-white-space */
                        size_t wscount = 0;
                        while (i < (size_t)nchn
                               && (isblank(farg[i]) || farg[i] == '\n'))
                          ++i, ++wscount;
                        if (wscount)
                          farg[wpos++] = ' ';
                      }
                    farg[wpos] = '\0';
                    nchn = (int)wpos;
                  }
		  if ( (strlen(xline)+(size_t)nchn) > (size_t)80 )
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
522
		    {
523
                      if (i) xline[strlen(xline) - 1] = 0;
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
		      cdoc[ncdoc].aline[cdoc[ncdoc].naline++] = strdup(xline);
		      sprintf(xline, "%*s", nch, "");
		    }
		  strcat(xline, farg);
		}
	      if ( funcargc ) strcat(xline, ");");
	      cdoc[ncdoc].aline[cdoc[ncdoc].naline++] = strdup(xline);
	      cdoc[ncdoc].text  = strdup(xdes);

	      ncdoc++;

	      /* Fortran Quick Guide */

	      fdoc[nfdoc].naline = 0;
	      fdoc[nfdoc].text   = NULL;
	      fdoc[nfdoc].fname  = strdup(funcname);

	      if ( functype == ISVOID )
		nch = sprintf(xline, "SUBROUTINE %s (", xname);
	      else
544
		nch = sprintf(xline, "%s FUNCTION %s (", funArgSym[functype].f77name, xname);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
545
546
547
548
549
550
551

	      if ( (funcargc == 1 && funcargtype[0] == ISVOID) ) funcargc = 0;

	      for ( i = 0; i < funcargc; i++ )
		{
		  if ( i ) strcat(xline, ", ");

552
553
554
555
556
557
558
559
560
561
562
                  char farg[128];
                  /* FIXME: optional empty argument name unhandled */
		  int nchn
                    = snprintf(farg, sizeof (farg), "%s %.*s",
                               funArgSym[funcargtype[i]].f77name,
                               (int)(funcargname[i].rm_eo
                                     - funcargname[i].rm_so),
                               line + funcargname[i].rm_so);
                  if (nchn < 0)
                    abort();
		  if ( (strlen(xline)+(size_t)nchn) > 80 )
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
563
564
565
566
567
568
569
570
571
572
573
574
575
		    {
		      fdoc[nfdoc].aline[fdoc[nfdoc].naline++] = strdup(xline);
		      sprintf(xline, "%*s", nch, "");
		    }
		  strcat(xline, farg);
		}
	      if ( funcargc ) strcat(xline, ")");
	      fdoc[nfdoc].aline[fdoc[nfdoc].naline++] = strdup(xline);
	      fdoc[nfdoc].text  = strdup(xdes);

	      nfdoc++;
	    }
	}
576
      else if ( memcmp(line, "#define", 7) == 0 )
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
577
578
579
580
581
582
	{
	  pline = line;
	  pline += 7;
	  while ( isspace((int) *pline) ) pline++;
	  parname = pline;
	  len = strlen(pline);
583
584
          size_t i = 0;
	  for (; i < len; i++ )
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
585
586
587
588
589
590
591
592
593
594
595
	    {
	      if ( isspace((int) pline[i]) ) break;
	    }
	  if ( i == len ) continue;
	  parname[i] = 0;
	  pline += i+1;
	  while ( isspace((int) *pline) ) pline++;
	  if ( ! (isdigit((int) *pline) || *pline == '-') ) continue;
	  parvalue = atoi(pline);

	  /* fortran include */
596
597
598
	  fprintf(fpinc, "      INTEGER    %-22s\n"
                  "      PARAMETER (%-22s = %2d)\n", parname, parname,
                  parvalue);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
599
	}
600
601
602
603
604
605
606
607
608
609
610
611
612
      else if (!regexec(&docCommentRE, line, maxMatch, reMatch, 0))
        {
          /* found documentation comment */
          size_t nameMatchLen = (size_t)(reMatch[1].rm_eo - reMatch[1].rm_so),
            docMatchLen = (size_t)(reMatch[2].rm_eo - reMatch[2].rm_so);
          memcpy(xname, line + reMatch[1].rm_so, nameMatchLen);
          xname[nameMatchLen] = 0;
          memcpy(xdes, line + reMatch[2].rm_so, docMatchLen);
          xdes[docMatchLen] = 0;
          printf("Found documentation for \"%s\": \"%s\"\n", xname,
                 xdes);
        }
      else if (!regexec(&commentRE, line, maxMatch, reMatch, 0))
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
613
	{
614
615
616
617
          size_t commentLen = (size_t)(reMatch[1].rm_eo - reMatch[1].rm_so);
          const char *comment = line + reMatch[1].rm_so;
          /* fortran include */
          fprintf(fpinc, "!\n!  %.*s\n!\n", (int)commentLen, comment);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
618

619
620
          /* fortran interface */
          fprintf(fpint, "\n/*  %.*s  */\n\n", (int)commentLen, comment);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
621
622
623
	}
      else
	{
624
625
626
	  if ( lineLen > 1 )
	    printf("skip: line %3d  size %3zd  %s%s", lineno, lineLen, line,
                   line[lineLen-1]=='\n'?"":"missing new-line\n");
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
	}
    }

  fprintf(fpint, "\n");
  fprintf(fpint, "#endif\n");

  fclose(fpin);
  fclose(fpinc);
  fclose(fpint);

  qsort(cdoc, ncdoc, sizeof(Docu), doccmp);
  qsort(fdoc, nfdoc, sizeof(Docu), doccmp);

  fp = fopen("../doc/c_quick_ref.txt", "w");
  if ( fp )
    {
      fprintf(fp, "C Quick Reference\n");
      fprintf(fp, "-----------------\n\n");

      doctotxt(fp, cdoc, ncdoc);
      fclose(fp);
    }

  fp = fopen("../doc/f_quick_ref.txt", "w");
  if ( fp )
    {
      fprintf(fp, "Fortran Quick Reference\n");
      fprintf(fp, "-----------------------\n\n");

      doctotxt(fp, fdoc, nfdoc);
      fclose(fp);
    }

  fp = fopen("../doc/tex/c_quick_ref.tex", "w");
  if ( fp )
    {
      fprintf(fp, "\\chapter*{Quick Reference}\n");
      fprintf(fp, "\\addcontentsline{toc}{chapter}{Quick Reference}\n");
      fprintf(fp, "\n");
      fprintf(fp, "This appendix provide a brief listing of the C language bindings of the\n");
      fprintf(fp, "CDI library routines:\n");
      fprintf(fp, "\n");

      doctotex(fp, cdoc, ncdoc);
      fclose(fp);
    }

  fp = fopen("../doc/tex/f_quick_ref.tex", "w");
  if ( fp )
    {
      fprintf(fp, "\\chapter*{Quick Reference}\n");
      fprintf(fp, "\\addcontentsline{toc}{chapter}{Quick Reference}\n");
      fprintf(fp, "\n");
      fprintf(fp, "This appendix provide a brief listing of the Fortran language bindings of the\n");
      fprintf(fp, "CDI library routines:\n");
      fprintf(fp, "\n");

      doctotex(fp, fdoc, nfdoc);
      fclose(fp);
    }
}

int main(int argc, char *argv[])
{
  char *fname;
  char *cp;
  char fnameinc[128], fnameint[128];
  size_t len;

  if ( argc != 2 )
    {
      printf("Usage:  %s  includefile\n", argv[0]);
      return (1);
    }

  fname = argv[1];

  cp = strrchr(fname, '.');
  if ( cp == NULL ) len = strlen(fname);
706
707
  else              len = (size_t)(cp - fname);

708
709
  memcpy(fnameinc, fname, len);
  memcpy(fnameint, fname, len);
Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
710
711
712

  strcpy(fnameinc+len, ".inc");
  strcpy(fnameint+len, "Fortran.c");
713

Uwe Schulzweida's avatar
Cleanup  
Uwe Schulzweida committed
714
715
716
717
  fortran_interface(fname, fnameinc, fnameint);

  return (0);
}
718
719
720
721
722
723
724
725
726
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */
727