home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / trace / tcpdump-2.2.1 / tcpgram.y < prev    next >
Encoding:
Lex Description  |  1992-03-17  |  6.0 KB  |  233 lines

  1. %{
  2. /*
  3.  * Copyright (c) 1988-1990 The Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that: (1) source code distributions
  8.  * retain the above copyright notice and this paragraph in its entirety, (2)
  9.  * distributions including binary code include the above copyright notice and
  10.  * this paragraph in its entirety in the documentation or other materials
  11.  * provided with the distribution, and (3) all advertising materials mentioning
  12.  * features or use of this software display the following acknowledgement:
  13.  * ``This product includes software developed by the University of California,
  14.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  15.  * the University nor the names of its contributors may be used to endorse
  16.  * or promote products derived from this software without specific prior
  17.  * written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  *
  22.  * Grammar for tcpdump.
  23.  */
  24. #ifndef lint
  25. static char rcsid[] =
  26.     "@(#) $Header: tcpgram.y,v 1.29 92/03/17 13:45:08 mccanne Exp $ (LBL)";
  27. #endif
  28.  
  29. #include <stdio.h>
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <net/if.h>
  33. #include <netinet/in.h>
  34. #include <netinet/if_ether.h>
  35.  
  36. #include "interface.h"
  37.  
  38. #include <sys/time.h>
  39. #include <net/bpf.h>
  40.  
  41. #include "gencode.h"
  42.  
  43. #define QSET(q, p, d, a) (q).proto = (p),\
  44.              (q).dir = (d),\
  45.              (q).addr = (a)
  46.  
  47. int n_errors = 0;
  48.  
  49. static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
  50.  
  51. static void
  52. yyerror()
  53. {
  54.     ++n_errors;
  55. }
  56.  
  57. %}
  58.  
  59. %union {
  60.     int i;
  61.     u_long h;
  62.     u_char *e;
  63.     char *s;
  64.     struct stmt *stmt;
  65.     struct arth *a;
  66.     struct {
  67.         struct qual q;
  68.         struct block *b;
  69.     } blk;
  70.     struct block *rblk;
  71. }
  72.  
  73. %type    <blk>    expr id nid pid term rterm qid
  74. %type    <blk>    head
  75. %type    <i>    pqual dqual aqual ndaqual
  76. %type    <a>    arth narth
  77. %type    <i>    byteop pname pnum relop irelop
  78. %type    <blk>    and or paren not null prog
  79. %type    <rblk>    other
  80.  
  81. %token  DST SRC HOST GATEWAY
  82. %token  NET PORT LESS GREATER PROTO BYTE
  83. %token  ARP RARP IP TCP UDP ICMP
  84. %token  TK_BROADCAST TK_MULTICAST
  85. %token  NUM
  86. %token  LINK
  87. %token    GEQ LEQ NEQ
  88. %token    ID EID HID
  89. %token    LSH RSH
  90. %token  LEN
  91.  
  92. %type    <s> ID
  93. %type    <e> EID
  94. %type    <h> HID
  95. %type    <i> NUM
  96.  
  97. %left OR AND
  98. %nonassoc  '!'
  99. %left '|'
  100. %left '&' 
  101. %left LSH RSH
  102. %left '+' '-'
  103. %left '*' '/'
  104. %nonassoc UMINUS
  105. %%
  106. prog:      null expr
  107.     finish_parse($2.b);
  108. }
  109.     | null
  110.     ;
  111. null:      /* null */        { $$.q = qerr; }
  112.     ;
  113. expr:      term
  114.     | expr and term        { gen_and($1.b, $3.b); $$ = $3; }
  115.     | expr and id        { gen_and($1.b, $3.b); $$ = $3; }
  116.     | expr or term        { gen_or($1.b, $3.b); $$ = $3; }
  117.     | expr or id        { gen_or($1.b, $3.b); $$ = $3; }
  118.     ;
  119. and:      AND            { $$ = $<blk>0; }
  120.     ;
  121. or:      OR            { $$ = $<blk>0; }
  122.     ;
  123. id:      nid
  124.     | pnum            { $$.b = gen_ncode((u_long)$1,
  125.                            $$.q = $<blk>0.q); }
  126.     | paren pid ')'        { $$ = $2; }
  127.     ;
  128. nid:      ID            { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
  129.     | HID            { $$.b = gen_ncode($1, $$.q = $<blk>0.q); }
  130.     | EID            { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
  131.     | not id        { gen_not($2.b); $$ = $2; }
  132.     ;
  133. not:      '!'            { $$ = $<blk>0; }
  134.     ;
  135. paren:      '('            { $$ = $<blk>0; }
  136.     ;
  137. pid:      nid
  138.     | qid and id        { gen_and($1.b, $3.b); $$ = $3; }
  139.     | qid or id        { gen_or($1.b, $3.b); $$ = $3; }
  140.     ;
  141. qid:      pnum            { $$.b = gen_ncode((u_long)$1, 
  142.                            $$.q = $<blk>0.q); }
  143.     | pid
  144.     ;
  145. term:      rterm
  146.     | not term        { gen_not($2.b); $$ = $2; }
  147.     ;
  148. head:      pqual dqual aqual    { QSET($$.q, $1, $2, $3); }
  149.     | pqual dqual        { QSET($$.q, $1, $2, Q_DEFAULT); }
  150.     | pqual aqual        { QSET($$.q, $1, Q_DEFAULT, $2); }
  151.     | pqual PROTO        { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
  152.     | pqual ndaqual        { QSET($$.q, $1, Q_DEFAULT, $2); }
  153.     ;
  154. rterm:      head id        { $$ = $2; }
  155.     | paren expr ')'    { $$.b = $2.b; $$.q = $1.q; }
  156.     | pname            { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
  157.     | arth relop arth    { $$.b = gen_relation($2, $1, $3, 0); 
  158.                   $$.q = qerr; }
  159.     | arth irelop arth    { $$.b = gen_relation($2, $1, $3, 1); 
  160.                   $$.q = qerr; }
  161.     | other            { $$.b = $1; $$.q = qerr; }
  162.     ;
  163. /* protocol level qualifiers */
  164. pqual:      pname
  165.     |            { $$ = Q_DEFAULT; }
  166.     ;
  167. /* 'direction' qualifiers */
  168. dqual:      SRC            { $$ = Q_SRC; }
  169.     | DST            { $$ = Q_DST; }
  170.     | SRC OR DST        { $$ = Q_OR; }
  171.     | DST OR SRC        { $$ = Q_OR; }
  172.     | SRC AND DST        { $$ = Q_AND; }
  173.     | DST AND SRC        { $$ = Q_AND; }
  174.     ;
  175. /* address type qualifiers */
  176. aqual:      HOST            { $$ = Q_HOST; }
  177.     | NET            { $$ = Q_NET; }
  178.     | PORT            { $$ = Q_PORT; }
  179.     ;
  180. /* non-directional address type qualifiers */
  181. ndaqual:  GATEWAY         { $$ = Q_GATEWAY; }
  182.     ;
  183. pname:      LINK            { $$ = Q_LINK; }
  184.     | IP            { $$ = Q_IP; }
  185.     | ARP            { $$ = Q_ARP; }
  186.     | RARP            { $$ = Q_RARP; }
  187.     | TCP            { $$ = Q_TCP; }
  188.     | UDP            { $$ = Q_UDP; }
  189.     | ICMP            { $$ = Q_ICMP; }
  190.     ;
  191. other:      pqual TK_BROADCAST    { $$ = gen_broadcast($1); }
  192.     | pqual TK_MULTICAST    { $$ = gen_multicast($1); }
  193.     | LESS NUM        { $$ = gen_less($2); }
  194.     | GREATER NUM        { $$ = gen_greater($2); }
  195.     | BYTE NUM byteop NUM    { $$ = gen_byteop($3, $2, $4); }
  196.     ;
  197. relop:      '>'            { $$ = BPF_JGT; }
  198.     | GEQ            { $$ = BPF_JGE; }
  199.     | '='            { $$ = BPF_JEQ; }
  200.     ;
  201. irelop:      LEQ            { $$ = BPF_JGT; }
  202.     | '<'            { $$ = BPF_JGE; }
  203.     | NEQ            { $$ = BPF_JEQ; }
  204.     ;
  205. arth:      pnum            { $$ = gen_loadi($1); }
  206.     | narth
  207.     ;
  208. narth:      pname '[' arth ']'        { $$ = gen_load($1, $3, 1); }
  209.     | pname '[' arth ':' NUM ']'    { $$ = gen_load($1, $3, $5); }
  210.     | arth '+' arth            { $$ = gen_arth(BPF_ADD, $1, $3); }
  211.     | arth '-' arth            { $$ = gen_arth(BPF_SUB, $1, $3); }
  212.     | arth '*' arth            { $$ = gen_arth(BPF_MUL, $1, $3); }
  213.     | arth '/' arth            { $$ = gen_arth(BPF_DIV, $1, $3); }
  214.     | arth '&' arth            { $$ = gen_arth(BPF_AND, $1, $3); }
  215.     | arth '|' arth            { $$ = gen_arth(BPF_OR, $1, $3); }
  216.     | arth LSH arth            { $$ = gen_arth(BPF_LSH, $1, $3); }
  217.     | arth RSH arth            { $$ = gen_arth(BPF_RSH, $1, $3); }
  218.     | '-' arth %prec UMINUS        { $$ = gen_neg($2); }
  219.     | paren narth ')'        { $$ = $2; }
  220.     | LEN                { $$ = gen_loadlen(); }
  221.     ;
  222. byteop:      '&'            { $$ = '&'; }
  223.     | '|'            { $$ = '|'; }
  224.     | '<'            { $$ = '<'; }
  225.     | '>'            { $$ = '>'; }
  226.     | '='            { $$ = '='; }
  227.     ;
  228. pnum:      NUM
  229.     | paren pnum ')'    { $$ = $2; }
  230.     ;
  231. %%
  232.