summaryrefslogtreecommitdiff
path: root/src/jsmin.c
blob: f286e7b6d6feb3a51f9b23d0bbf8471f2d321f2f (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 2019 Andrey V.Kosteltsev

  Licensed under the Radix.pro License, Version 1.0 (the "License");
  you may not use this file  except  in compliance with the License.
  You may obtain a copy of the License at

     https://radix.pro/licenses/LICENSE-1.0-en_US.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  implied.

 **********************************************************************/

#include <config.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>   /* basename(3) */

#include <msglog.h>

#include <jsmin.h>

static FILE *ifile;
static FILE *ofile;

static const char *input_fname = NULL;

static void error( const char *fname, char *s )
{
  if( fname )
    ERROR( "JSMIN: %s: %s", basename( (char *)fname ), s );
  else
    ERROR( "JSMIN: %s", s );
}

static int is_alpha_or_num( int c )
{
  return( (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
          (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' || c > 126 );
}

static int  a;
static int  b;
static int  lookahead = EOF;
static int  x = EOF;
static int  y = EOF;

/*
  get - return the next character from stdin. Watch out for lookahead. If
        the character is a control character, translate it to a space or
        linefeed.
 */
static int get()
{
  int c = lookahead;
  lookahead = EOF;
  if( c == EOF )
  {
    c = getc( ifile );
  }
  if( c >= ' ' || c == '\n' || c == EOF )
  {
    return c;
  }
  if( c == '\r' )
  {
    return '\n';
  }
  return ' ';
}


/*
  peek - get the next character without getting it.
 */
static int peek()
{
  lookahead = get();
  return lookahead;
}


/*
  next - get the next character, excluding comments. peek() is used to see
         if a '/' is followed by a '/' or '*'.
 */
static int next()
{
  int c = get();
  if ( c == '/' )
  {
    switch( peek() )
    {
      case '/':
        for( ;; )
        {
          c = get();
          if( c <= '\n' )
          {
            break;
          }
        }
        break;
      case '*':
        get();
        while( c != ' ' )
        {
          switch( get() )
          {
            case '*':
              if( peek() == '/' )
              {
                get();
                c = ' ';
              }
              break;
            case EOF:
              error( input_fname, "Unterminated comment" );
              return EOF;
          }
        }
        break;
    }
  }
  y = x;
  x = c;
  return c;
}


/*
  action - do something! What you do is determined by the argument:
           1   Output A. Copy B to A. Get the next B.
           2   Copy B to A. Get the next B. (Delete A).
           3   Get the next B. (Delete B).
  action treats a string as a single character. Wow!
  action recognizes a regular expression if it is preceded by ( or , or =.
 */
static void action( int d )
{
  switch( d )
  {
    case 1:
      /* skip first carriage return */
      if( a != '\n' ) putc( a, ofile );
      if( (y == '\n' || y == ' ') &&
          (a == '+' || a == '-' || a == '*' || a == '/') &&
          (b == '+' || b == '-' || b == '*' || b == '/')    )
      {
        putc( y, ofile );
      }
    case 2:
      a = b;
      if( a == '\'' || a == '"' || a == '`' )
      {
        for( ;; )
        {
          putc( a, ofile );
          a = get();
          if( a == b )
          {
            break;
          }
          if( a == '\\' )
          {
            putc( a, ofile );
            a = get();
          }
          if( a == EOF )
          {
            error( input_fname, "Unterminated string literal" );
            return;
          }
        }
      }
    case 3:
      b = next();
      if( b == '/' &&
          ( a == '(' || a == ',' || a == '=' || a == ':' ||
            a == '[' || a == '!' || a == '&' || a == '|' ||
            a == '?' || a == '+' || a == '-' || a == '~' ||
            a == '*' || a == '/' || a == '{' || a == '\n' ) )
      {
        putc( a, ofile );
        if( a == '/' || a == '*' )
        {
          putc( ' ', ofile );
        }
        putc( b, ofile );
        for( ;; )
        {
          a = get();
          if( a == '[' )
          {
            for( ;; )
            {
              putc( a, ofile );
              a = get();
              if( a == ']' )
              {
                break;
              }
              if( a == '\\' )
              {
                putc( a, ofile );
                a = get();
              }
              if( a == EOF )
              {
                error( input_fname, "Unterminated set in Regular Expression literal" );
                return;
              }
            }
          }
          else if( a == '/' )
          {
            switch( peek() )
            {
              case '/':
              case '*':
                error( input_fname, "Unterminated set in Regular Expression literal" );
                return;
            }
            break;
          }
          else if( a =='\\' )
          {
            putc( a, ofile );
            a = get();
          }
          if( a == EOF )
          {
            error( input_fname, "Unterminated Regular Expression literal" );
            return;
          }
          putc( a, ofile );
        }
      b = next();
    }
  }
}


/*
  jsmin - Copy the input to the output, deleting the characters which are
          insignificant to JavaScript. Comments will be removed. Tabs will be
          replaced with spaces. Carriage returns will be replaced with linefeeds.
          Most spaces and linefeeds will be removed.
*/
static void jsmin()
{
  if( peek() == 0xEF ) { get(); get(); get(); }
  a = '\n';
  action( 3 );

  while( a != EOF )
  {
    switch( a )
    {
      case ' ':
        action(is_alpha_or_num(b) ? 1 : 2);
        break;
      case '\n':
        switch( b )
        {
          case '{': case '[': case '(':
          case '+': case '-': case '!':
          case '~':
            action( 1 );
            break;
          case ' ':
            action( 3 );
            break;
          default:
            action( is_alpha_or_num(b) ? 1 : 2 );
        }
        break;
      default:
        switch( b )
        {
          case ' ':
            action( is_alpha_or_num(a) ? 1 : 3 );
            break;
          case '\n':
            switch( a )
            {
              case '}':  case ']': case ')':
              case '+':  case '-': case '"':
              case '\'': case '`':
                action( 1 );
                break;
              default:
                action( is_alpha_or_num(a) ? 1 : 3 );
            }
            break;
          default:
            action( 1 );
            break;
        }
    }
  }
  /* lats carriage return */
  putc( '\n', ofile );
}


int minimize_json( const char *ifname, const char *ofname )
{
  int status, ret = -1;

  if( !ifname || !ofname ) return ret;

  status = exit_status; exit_status = 0;

  input_fname = ifname;

  ret = 0;

  ifile = fopen( ifname, "r" );
  if( ifile == NULL )
  {
    ERROR( "JSMIN: Can't open '%s' file", ifname );
    exit_status = status + exit_status;
    return ret;
  }

  ofile = fopen( ofname, "w+" );
  if( ofile == NULL )
  {
    ERROR( "JSMIN: Can't open '%s' file", ofname );
    exit_status = status + exit_status;
    return ret;
  }

  jsmin();

  fclose( ifile ); ifile = NULL;
  fflush( ofile ); fclose( ofile ); ofile = NULL;

  if( exit_status == 0 )
  {
    ret = 1;
    exit_status = status;
  }
  else
  {
    exit_status = status + exit_status;
  }

  return ret;
}