aboutsummaryrefslogtreecommitdiff
path: root/freetype/builds/atari/deflinejoiner.awk
blob: 16d9e6dd9333e05e73a5b2b699c72ab7966d11a9 (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
#!/usr/bin/env awk


function shift( array, \
                junk, elm0, l )
{
  elm0 = array[0]
  for ( l = 0; l < asorti( array, junk ) - 1; l++ )
    array[l] = array[l+1];
  delete array[l]
  return elm0
}


function init_cpp_src_line()
{
  logical_line = ""
  delete break_pos
}


function shift_valid_bp( array, \
                         junk, elm )
{
  elm = -1

  if ( 0 < asorti( array, junk ) )
    do {
      elm = shift( array )
    } while ( 0 > elm );

  return elm
}


function check_cpp_src_line_break_pos( \
                                       i, junk )
{
  printf( "break_pos:" )
  for ( i = 0; i < asorti( break_pos, junk ); i++ )
    printf( " %d", break_pos[i] );
  printf( "\n" )
}


function check_cpp_src_line()
{
  printf( "logical_line[%s]\n", logical_line )
  check_cpp_src_line_break_pos()
}


function append_line( phys_line, \
                      filt_line, bp_len )
{
  filt_line = phys_line
  sub( /\\$/, " ", filt_line )
  logical_line    = logical_line filt_line
  bp_len = asorti( break_pos, junk )
  break_pos[bp_len] = length( logical_line ) - 1
}


function print_line( \
                     c0, c1, i, junk, part_str )
{
  c0 = 0

  while( asorti( break_pos, junk ) > 1 )
  {
    if ( ( c1 = shift_valid_bp( break_pos ) ) < 1 )
    {
      part_str = substr( logical_line, c0 + 1 )
      printf( "%s\n", part_str )
      return
    }

    part_str = substr( logical_line, c0 + 1, c1 - c0 + 1 )
    gsub( / $/, "\\", part_str )
    printf( "%s\n", part_str )
    c0 = c1 + 1
  }

  part_str = substr( logical_line, c0 + 1 )
  printf( "%s\n", part_str )
}


function shrink_spaces( pos, \
                        tail, removed_length, k )
{
  tail = substr( logical_line, pos )
  sub( /^[ \t]+/, " ", tail )
  removed_length = length( logical_line ) - pos - length( tail ) + 1
  logical_line = substr( logical_line, 0, pos - 1 ) tail


  for ( k = 0; k < asorti( break_pos, junk ); k++ )
    if ( ( pos + removed_length ) <= break_pos[k] )
      break_pos[k] = break_pos[k] - removed_length;
    else if ( pos <= break_pos[k] )
      break_pos[k] = -1;

  return removed_length
}


function shrink_spaces_to_linebreak( pos, \
                                     junk, part_str, removed_length, i )
{
  for ( i = 0; i < asorti( break_pos, junk ) && break_pos[i] < pos ; i++ )
    ;

  if ( break_pos[i] < 1 )
    return;

  part_str = substr( logical_line, pos, break_pos[i] - pos + 1 )
  sub( /^[ \t]+/, " ", part_str )
  removed_length = ( break_pos[i] - pos + 1 ) - length( part_str )

  tail = substr( logical_line, pos + removed_length )
  logical_line = substr( logical_line, 0, pos - 1 ) tail

  for ( ; i < asorti( break_pos, junk ); i++ )
    break_pos[i] -= removed_length;

  return removed_length
}


function delete_linebreaks_in_2nd_token( \
                                           tail, paren_depth, junk, i, j, k, l )
{
  if ( logical_line ~ /^[ \t]*#[ \t]*define[ \t]+[0-9A-Za-z_]+\(/ )
  {
    tail = logical_line
    sub( /^[ \t]*#[ \t]*define[ \t]+[0-9A-Za-z_]+/, "", tail )

    paren_depth = 0
    l = 0
    i = length( logical_line ) - length( tail ) + 1 # seek to the 1st op paren
    j = i
    do {
      if ( substr( logical_line, j, 2 ) ~ /[ \t][ \t]/ )
        l = shrink_spaces( j );
      else if ( substr( logical_line, j, 1 ) == "(" )
        paren_depth += 1;
      else if ( substr( logical_line, j, 1 ) == ")" )
        paren_depth -= 1;
      j += 1
    } while ( j < length( logical_line ) && paren_depth != 0 )

    for ( k = 0; k < asorti( break_pos, junk ); k++ )
      if ( i <= break_pos[k] && break_pos[k] < j )
        break_pos[k] = -1;

    if ( l > 0 )
      shrink_spaces_to_linebreak( j );
  }
}


BEGIN{
  init_cpp_src_line()
}
{
  append_line( $0 )
  if ( $0 !~ /\\$/ )
  {
    delete_linebreaks_in_2nd_token()
    print_line()
    init_cpp_src_line()
  }
}
END{
  if ( 0 < length( logical_line ) )
  {
    delete_linebreaks_in_2nd_token()
    print_line()
  }
}