msg_parser.awk 14.2 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
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
#! /usr/bin/env awk
#
# This script recreates C files containing header-specific boilerplate stuff
# using the given list of headers (usually obtained from the master structure).
#
# It can also create a parser table.
#
# --------------------------------------------------------------------
#
# This file is part of the Sofia-SIP package
#
# Copyright (C) 2005 Nokia Corporation.
#
# Contact: Pekka Pessi <pekka.pessi@nokia.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
#
# --------------------------------------------------------------------
#
# Contributor(s): Pekka.Pessi@nokia.com.
#
# Created: Fri Apr  6 12:59:59 2001 ppessi
#

BEGIN {
  "date '+%a %b %e %H:%M:%S %Y'" | getline date;

  ascii =			       \
     "                               " \
    " !\"#$%&'()*+,-./0123456789:;<=>?" \
    "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" \
    "`abcdefghijklmnopqrstuvwxyz{|}~";
  lower_case = "abcdefghijklmnopqrstuvwxyz";

  N=0;
  # Initialize these as arrays
  split("", symbols);
  split("", names);
  split("", comments); 
  split("", hashes);
  split("", NAMES);
  split("", Comments);
  split("", COMMENTS);
57
  split("", experimental);
58

59 60 61 62
  # indexed by the C name of the header
  split("", Since);		# Non-NUL if extra
  split("", Extra);		# Offset in extra headers

63
  without_experimental = 0;
64 65 66
  template="";
  template1="";
  template2="";
67 68 69
  template3="";
  prefix="";
  tprefix="";
70
  extra=0;
71 72
  failed=0;
  success=0;
73

74 75
  extra_struct = "msg_pub_extra";

76
  ERRNO="error";
Pekka Pessi's avatar
Pekka Pessi committed
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
}

function name_hash (name)
{
  hash = 0;

  len = split(name, chars, "");

  for (i = 1; i <= len; i++) {
    c = tolower(chars[i]);
    hash = (38501 * (hash + index(ascii, c))) % 65536;
  }

  if (0) {
    # Test that hash algorithm above agrees with the C version
    pipe = ("../msg/msg_name_hash " name);
    pipe | getline;
    close(pipe);
    if (hash != $0) {
      print name ": " hash " != " $0 > "/dev/stderr";
    }
  }

  return hash "";
}

#
# Replace magic patterns in template p with header-specific values
#
106
function protos (name, comment, hash, since)
Pekka Pessi's avatar
Pekka Pessi committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
{
  NAME=toupper(name);
  sub(/.*[\/][*][*][<][ 	]*/, "", comment); 
  sub(/[ 	]*[*][\/].*/, "", comment);
  sub(/[ 	]+/, " ", comment);

  short = match(comment, /[(][a-z][)]/);
  if (short) {
    short = substr(comment, short + 1, 1);
    sub(/ *[(][a-z][)]/, "", comment);
    shorts[index(lower_case, short)] = name;
  }

  do_hash = hash == 0;

  if (do_hash) {
    split(comment, parts, " ");
    name2 = tolower(parts[1]);
    gsub(/-/, "_", name2);
126
    if (name2 != name && name2 != tprefix "_" name) {
Pekka Pessi's avatar
Pekka Pessi committed
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
      print name " mismatch with " comment " (" real ")" > "/dev/stderr";
    }
    
    hash = name_hash(parts[1]);

    hashed[name] = hash;

    if (comment !~ /header/) { 
      comment = comment " header";
    }
  }

  Comment = comment;
  if (!do_hash) {
    comment = tolower(comment);
  }
  COMMENT = toupper(comment);

  # Store the various forms into an array for the footer processing
  N++; 
  hashes[N] = hash; 
  names[N] = name;
  NAMES[N] = NAME; 
  comments[N] = comment; 
  Comments[N] = comment; 
  COMMENTS[N] = COMMENT; 

  symbols[name] = comment;
155 156 157 158
  if (since) {
    Since[name] = since;
    Extra[name] = extra++;
  }
Pekka Pessi's avatar
Pekka Pessi committed
159

160
  expr = (without_experimental > 0 && do_hash);
161 162
  if (expr) {
    printf "%s is experimental\n", Comment;
163 164 165 166
  }    
  
  experimental[N] = expr;

Pekka Pessi's avatar
Pekka Pessi committed
167
  if (PR) {
168 169 170
    if (expr) {
      print "#if SU_HAVE_EXPERIMENTAL" > PR;
    }
171 172 173 174
    replace(template, hash, name, NAME, comment, Comment, COMMENT, since);
    replace(template1, hash, name, NAME, comment, Comment, COMMENT, since);
    replace(template2, hash, name, NAME, comment, Comment, COMMENT, since);
    replace(template3, hash, name, NAME, comment, Comment, COMMENT, since);
175 176 177
    if (expr) {
      print "#endif /* SU_HAVE_EXPERIMENTAL */" > PR;
    }
Pekka Pessi's avatar
Pekka Pessi committed
178 179 180
  }
}

181
function replace (p, hash, name, NAME, comment, Comment, COMMENT, since)
Pekka Pessi's avatar
Pekka Pessi committed
182
{
183 184 185
  #
  # Replace various forms of header name in template, print it out
  #
Pekka Pessi's avatar
Pekka Pessi committed
186 187 188 189 190 191 192
  if (p) {
    gsub(/#hash#/, hash, p);
    gsub(/#xxxxxx#/, name, p); 
    gsub(/#XXXXXX#/, NAME, p);
    gsub(/#xxxxxxx_xxxxxxx#/, comment, p);
    gsub(/#Xxxxxxx_Xxxxxxx#/, Comment, p);
    gsub(/#XXXXXXX_XXXXXXX#/, COMMENT, p);
193 194 195 196 197 198 199 200

    if (since) {
      gsub(/#version#/, since, p);
    }
    else {
      # Remove line with #version#
      gsub(/\n[^\n]*#version#[^\n]*\n/, "\n", p);
    }
Pekka Pessi's avatar
Pekka Pessi committed
201
    	    
202
    print p > PR;
Pekka Pessi's avatar
Pekka Pessi committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  }
}

# 
# Repeat each line in the footer containing the magic replacement
# pattern with a instance of all headers
#
function process_footer (text)
{ 
  if (!match(tolower(text), /#(xxxxxx(x_xxxxxxx)?|hash)#/)) {
    n = length(text);
    while (substr(text, n) == "\n") {
      n = n - 1;
      text = substr(text, 1, n);
    }
    if (n > 0)
219
      print text > PR;
Pekka Pessi's avatar
Pekka Pessi committed
220 221 222 223 224 225 226 227
    return;
  }
  
  n = split(text, lines, RS);

  for (i = 1; i <= n; i++) {
    l = lines[i];
    if (match(tolower(l), /#(xxxxxx(x_xxxxxxx)?|hash)#/)) {
228 229
      expr = 0;

Pekka Pessi's avatar
Pekka Pessi committed
230 231
      for (j = 1; j <= N; j++) {
	l = lines[i];
232 233 234 235 236 237
	if (expr != experimental[j]) {
	  expr = experimental[j];
	  if (expr) {
	    print "#if SU_HAVE_EXPERIMENTAL" > PR;
	  }
	  else {
238
	    print "#endif /* SU_HAVE_EXPERIMENTAL */" > PR;
239 240
	  }
	}
Pekka Pessi's avatar
Pekka Pessi committed
241 242 243 244 245 246
	gsub(/#hash#/, hashes[j], l);
	gsub(/#xxxxxxx_xxxxxxx#/, comments[j], l);
	gsub(/#Xxxxxxx_Xxxxxxx#/, Comments[j], l);
	gsub(/#XXXXXXX_XXXXXXX#/, COMMENTS[j], l);
	gsub(/#xxxxxx#/, names[j], l); 
	gsub(/#XXXXXX#/, NAMES[j], l);
247
	print l > PR;
Pekka Pessi's avatar
Pekka Pessi committed
248
      }
249 250

      if (expr) {
251
	print "#endif /* SU_HAVE_EXPERIMENTAL */" > PR;
252
      }
Pekka Pessi's avatar
Pekka Pessi committed
253
    } else {
254
      print l > PR;
Pekka Pessi's avatar
Pekka Pessi committed
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
    }
  }
}

#
# Read flags used with headers
#
function read_header_flags (flagfile,    line, tokens, name, value)
{
  while ((getline line < flagfile) > 0) {
    sub(/^[ \t]+/, "", line);
    sub(/[ \t]+$/, "", line);
    if (line ~ /^#/ || line ~ /^$/)
      continue;

    split(line, tokens,  /[ \t]*=[ \t]*/); 
    name = tolower(tokens[1]);
    gsub(/-/, "_", name);
    gsub(/,/, " ", name);
    # print "FLAG: " name " = " tokens[2]

    if (header_flags[name]) {
      print flagfile ": already defined " tokens[1];
    }
    else if (!symbols[name]) {
      print flagfile ": unknown header \"" tokens[1] "\"";
    }
    else {
      header_flags[name] = tokens[2];
    }
  }
  close(flagfile);
}

289 290 291 292 293
#
# Read in templates
#
function templates ()
{
Pekka Pessi's avatar
Pekka Pessi committed
294 295
  if (!auto) {
    auto = FILENAME; 
296 297 298 299

    if (!prefix) { prefix = module; }
    if (!tprefix) { tprefix = prefix; }

Pekka Pessi's avatar
Pekka Pessi committed
300 301 302 303 304
    sub(/.*\//, "", auto);
    auto = "This file is automatically generated from <" auto "> by msg_parser.awk.";

    if (PR) {
      if (TEMPLATE == "") { TEMPLATE = PR ".in"; }
305
      RS0=RS; RS="\f\n";
306 307 308 309 310
      if ((getline theader < TEMPLATE) < 0) {
	print ( TEMPLATE ": " ERRNO );
	failed=1;
        exit(1);
      }
Pekka Pessi's avatar
Pekka Pessi committed
311 312 313 314 315
      getline header < TEMPLATE;
      getline template < TEMPLATE;
      getline footer < TEMPLATE;

      if (TEMPLATE1) {
316 317 318 319 320
	if ((getline dummy < TEMPLATE1) < 0) {
	  print(TEMPLATE1 ": " ERRNO);
	  failed=1;
          exit(1);
        }
Pekka Pessi's avatar
Pekka Pessi committed
321 322 323 324 325 326
	getline dummy < TEMPLATE1;
	getline template1 < TEMPLATE1;
	getline dummy < TEMPLATE1;
      }

      if (TEMPLATE2) {
327 328 329 330 331
	if ((getline dummy < TEMPLATE2) < 0) {
	  print( TEMPLATE2 ": " ERRNO );
	  failed=1;
	  exit(1);
	}
Pekka Pessi's avatar
Pekka Pessi committed
332 333 334 335 336 337
	getline dummy < TEMPLATE2;
	getline template2 < TEMPLATE2;
	getline dummy < TEMPLATE2;
      }

      if (TEMPLATE3) {
338 339 340 341 342
	if ((getline dummy < TEMPLATE3) < 0) {
	  print( TEMPLATE3 ": " ERRNO );
	  failed=1;
	  exit(1);
	}
Pekka Pessi's avatar
Pekka Pessi committed
343 344 345 346 347 348 349 350
	getline dummy < TEMPLATE3;
	getline template3 < TEMPLATE3;
	getline dummy < TEMPLATE3;
      }

      sub(/.*[\/]/, "", TEMPLATE);
      gsub(/#AUTO#/, auto, header);
      gsub(/#DATE#/, "@date Generated: " date, header);
351 352
      if (PACKAGE_NAME) gsub(/#PACKAGE_NAME#/, PACKAGE_NAME, header);
      if (PACKAGE_VERSION) gsub(/#PACKAGE_VERSION#/, PACKAGE_VERSION, header);
353
      print header > PR;
Pekka Pessi's avatar
Pekka Pessi committed
354 355 356 357 358 359 360 361 362 363 364

      RS=RS0;
    }

    if (!NO_FIRST) {
      protos("request", "/**< Request line */", -1);
      protos("status", "/**< Status line */", -2);
    }
  }
}

365
/^#### EXTRA HEADER LIST STARTS HERE ####$/ { HLIST=1; templates(); }
366 367
HLIST && /^#### EXPERIMENTAL HEADER LIST STARTS HERE ####$/ { 
  without_experimental=total; }
368
HLIST && /^[a-z]/ { protos($1, $0, 0, $2); headers[total++] = $1; }
369 370 371 372
/^#### EXTRA HEADER LIST ENDS HERE ####$/ { HLIST=0;  }

/^ *\/\* === Headers start here \*\// { in_header_list=1;  templates(); }
/^ *\/\* === Headers end here \*\// { in_header_list=0; }
Pekka Pessi's avatar
Pekka Pessi committed
373

374
PT && /^ *\/\* === Hash headers end here \*\// { in_header_list=0;}
Pekka Pessi's avatar
Pekka Pessi committed
375 376 377 378 379 380 381 382 383 384 385 386

in_header_list && /^  (sip|rtsp|http|msg|mp)_[a-z_0-9]+_t/ { 
  n=$0
  sub(/;.*$/, "", n);   
  sub(/^ *(sip|rtsp|http|msg|mp)_[a-z0-9_]*_t[ 	]*/, "", n);
  sub(/^[*](sip|rtsp|http|msg|mp)_/, "", n);

  if ($0 !~ /[\/][*][*][<]/) {
    getline; 
  }
  if ($0 !~ /[\/][*][*][<]/) {
    printf "msg_protos.awk: header %s is malformed\n", n;
387
    failed=1;
Pekka Pessi's avatar
Pekka Pessi committed
388 389 390 391 392 393 394 395 396 397
    exit 1;
  }

  if (!NO_MIDDLE)
    protos(n, $0, 0);

  headers[total++] = n;
}

END {
398 399
  if (failed) { exit };

400 401 402
  if (without_experimental == 0)
    without_experimental = total;

Pekka Pessi's avatar
Pekka Pessi committed
403 404 405
  if (!NO_LAST) {
    protos("unknown", "/**< Unknown headers */", -3);
    protos("error", "/**< Erroneous headers */", -4);
406
    protos("separator", "/**< Separator line between headers and body */", -5);
Pekka Pessi's avatar
Pekka Pessi committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    protos("payload", "/**< Message payload */", -6);
    if (multipart)
      protos("multipart", "/**< Multipart payload */", -7);
  }
    
  if (PR) {
    process_footer(footer);
  }
  else if (PT) {
    if (FLAGFILE)
      read_header_flags(FLAGFILE);

    if (TEMPLATE == "") { TEMPLATE = PT ".in"; }
    RS0=RS; RS="\n";
    getline theader < TEMPLATE;
    getline header < TEMPLATE;
    getline template < TEMPLATE;
    getline footer < TEMPLATE;
    RS=RS0;

427 428
    module_struct = module "_t";

Pekka Pessi's avatar
Pekka Pessi committed
429 430 431
    sub(/.*[\/]/, "", TEMPLATE);
    gsub(/#AUTO#/, auto, header);
    gsub(/#DATE#/, "@date Generated: " date, header);
432
    print header > PT;
Pekka Pessi's avatar
Pekka Pessi committed
433

434 435 436 437
    print "" > PT;
    print "#define msg_offsetof(s, f) ((unsigned short)offsetof(s ,f))" > PT;
    print "" > PT;

Pekka Pessi's avatar
Pekka Pessi committed
438 439 440
    if (MC_SHORT_SIZE) {
      printf("static msg_href_t const " \
	     "%s_short_forms[MC_SHORT_SIZE] = \n{\n", 
441
	     module) > PT;      
Pekka Pessi's avatar
Pekka Pessi committed
442 443 444 445 446 447 448

      for (i = 1; i <= MC_SHORT_SIZE; i = i + 1) {
	c = (i == MC_SHORT_SIZE) ? "" : ",";
	if (i in shorts) {
	  n = shorts[i];
        flags = header_flags[n]; if (flags) flags = ",\n      " flags;
	  
449
	  printf("  { /* %s */ %s_%s_class, msg_offsetof(%s_t, %s_%s)%s }%s\n", 
Pekka Pessi's avatar
Pekka Pessi committed
450 451
		 substr(lower_case, i, 1), 
		 tprefix, n, module, prefix, n, flags, c)	\
452
	    > PT;
Pekka Pessi's avatar
Pekka Pessi committed
453 454 455
	}
	else {
	  printf("  { NULL }%s\n", c) \
456
	    > PT;
Pekka Pessi's avatar
Pekka Pessi committed
457 458
	}
      }
459
      printf("};\n\n") > PT;      
Pekka Pessi's avatar
Pekka Pessi committed
460 461
    }

462 463
    # printf("extern msg_hclass_t msg_multipart_class[];\n\n") > PT;

464 465
    if (extra > 0) {
      printf("struct %s {\n", extra_struct) > PT;
466
      printf("  %s base;\n", module_struct) > PT;
467 468 469 470 471 472
      if (total - without_experimental < extra) {
	printf("  msg_header_t *extra[%u];\n", 
	       extra - (total - without_experimental)) > PT;
      }
      if (total - without_experimental > 0) {
	print "#if SU_HAVE_EXPERIMENTAL" > PT;
473
	printf("  msg_header_t *experimental[%u];\n", 
474 475 476
	       total - without_experimental) > PT;
	print "#endif" > PT;
      }
477
      printf("};\n\n") > PT;
478
      module_struct = "struct " extra_struct;
479 480
    }

481 482 483 484 485 486 487 488 489 490 491 492 493
    printf("msg_mclass_t const %s_mclass[1] = \n{{\n", module) > PT;
    printf("# if defined (%s_HCLASS)\n", toupper(module)) > PT;
    printf("  %s_HCLASS,\n", toupper(module)) > PT;
    printf("#else\n") > PT;
    printf("  {{ 0 }},\n") > PT;
    printf("#endif\n") > PT;
    printf("  %s_VERSION_CURRENT,\n", toupper(module)) > PT;
    printf("  %s_PROTOCOL_TAG,\n", toupper(module)) > PT;
    printf("#if defined (%s_PARSER_FLAGS)\n", toupper(module)) > PT;
    printf("  %s_PARSER_FLAGS,\n", toupper(module)) > PT;
    printf("#else\n") > PT;
    printf("  0,\n") > PT;
    printf("#endif\n") > PT;
494
    printf("  sizeof (%s),\n", module_struct) > PT;
495
    printf("  %s_extract_body,\n", module) > PT;
Pekka Pessi's avatar
Pekka Pessi committed
496 497 498 499

    len = split("request status separator payload unknown error", unnamed, " ");

    for (i = 1; i <= len; i++) {
500
      printf("  {{ %s_%s_class, msg_offsetof(%s_t, %s_%s) }},\n", 
501
	     tprefix, unnamed[i], module, prefix, unnamed[i]) > PT;
Pekka Pessi's avatar
Pekka Pessi committed
502 503
    }
    if (multipart) {
504
      printf("  {{ %s_class, msg_offsetof(%s_t, %s_multipart) }},\n",
505
	     multipart, module, prefix) > PT;
Pekka Pessi's avatar
Pekka Pessi committed
506
    } else {
507
      printf("  {{ NULL, 0 }},\n") > PT;
Pekka Pessi's avatar
Pekka Pessi committed
508 509
    }
    if (MC_SHORT_SIZE) {
510
      printf("  %s_short_forms, \n", module) > PT;      
Pekka Pessi's avatar
Pekka Pessi committed
511 512
    }
    else {
513
      printf("  NULL, \n") > PT;
Pekka Pessi's avatar
Pekka Pessi committed
514
    }
515 516 517 518 519 520 521
    printf("  %d, \n", MC_HASH_SIZE) > PT;
    printf ("#if SU_HAVE_EXPERIMENTAL\n" \
	    "  %d,\n" \
	    "#else\n" \
	    "  %d,\n" \
	    "#endif\n", \
	    total, without_experimental) > PT;
522
    printf("  {\n") > PT;
Pekka Pessi's avatar
Pekka Pessi committed
523 524 525 526 527 528 529 530

    for (i = 0; i < total; i++) {
      n = headers[i];
      h = hashed[n];

      if (h < 0)
	continue;

531 532 533
      j = h % MC_HASH_SIZE; if (j == -0) j = 0;

      for (; j in header_hash;) {
Pekka Pessi's avatar
Pekka Pessi committed
534 535 536 537 538 539
	if (++j == MC_HASH_SIZE) {
	  j = 0;
	}
      }

      header_hash[j] = n;
540
      experimental2[j] = (i >= without_experimental);
Pekka Pessi's avatar
Pekka Pessi committed
541 542 543 544 545 546 547 548
    }

    for (i = 0; i < MC_HASH_SIZE; i++) {
      c = (i + 1 == MC_HASH_SIZE) ? "" : ",";
      if (i in header_hash) {
	n = header_hash[i];
        flags = header_flags[n]; if (flags) flags = ",\n      " flags;

549 550 551 552
	if (experimental2[i]) {
	  print "#if SU_HAVE_EXPERIMENTAL" > PT;
	}

553
	if (Since[n]) {
554
	  printf("    { %s_%s_class,\n" \
555
		 "      msg_offsetof(struct %s, extra[%u])%s }%s\n", 
556 557 558
		 tprefix, n, extra_struct, Extra[n], flags, c) > PT;
	}
	else {
559
	  printf("    { %s_%s_class, msg_offsetof(%s_t, %s_%s)%s }%s\n", 
560 561
		 tprefix, n, module, prefix, n, flags, c) > PT;
	}
562 563 564 565

	if (experimental2[i]) {
	  printf("#else\n    { NULL, 0 }%s\n#endif\n", c) > PT;
	}
Pekka Pessi's avatar
Pekka Pessi committed
566 567
      }
      else {
568
	printf("    { NULL, 0 }%s\n", c) > PT;
Pekka Pessi's avatar
Pekka Pessi committed
569 570
      }
    }
571
    printf("  }\n}};\n\n") > PT;
Pekka Pessi's avatar
Pekka Pessi committed
572
  }
573 574

  exit success;
Pekka Pessi's avatar
Pekka Pessi committed
575
}