File: token.c

package info (click to toggle)
k2pdfopt 2.53%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,668 kB
  • sloc: ansic: 81,507; cpp: 6,400; makefile: 5
file content (358 lines) | stat: -rw-r--r-- 9,764 bytes parent folder | download | duplicates (5)
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
/*
** token.c  Get next token from a file or buffer.
**
** Part of willus.com general purpose C code library.
**
** Copyright (C) 2012  http://willus.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
*/

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "willus.h"

static int allow_escapes = 1;
static int inited = 0;
static char allowed_white[64];

#define QUOTE(x)    ((x)=='\"' || (x)=='\'')
#define TRUE        1
#define FALSE       0

static int token_space(int ch);
static int token_nextline(FILE *f,int *linenum);
static void token_incline(int *linenum);
static void token_decline(int *linenum);


void cmdlineinput_init(CMDLINEINPUT *cl,int argc,char *argv[],char *opt_string)

    {
    cl->p = opt_string;
    cl->index = 0;
    cl->i=1;
    cl->argv=argv;
    cl->argc=argc;
    cl->cmdarg[0]='\0';
    }


char *cmdlineinput_next(CMDLINEINPUT *cl)

    {
    if (cl->p!=NULL)
        {
        if (token_next_from_string(cl->cmdarg,cl->p,&cl->index,1024))
            return(cl->cmdarg);
        else
            cl->p=NULL;
        }
    if (cl->i>=cl->argc)
        return(NULL);
    strncpy(cl->cmdarg,cl->argv[cl->i],1023);
    cl->cmdarg[1023]='\0';
    cl->i++;
    return(cl->cmdarg);
    }
        
    
/*
** Currently does not handle a double-quote within a double-quote.
** (e.g. a back-slashed double-quote)
*/
int token_next_from_string(char *dst,char *src,int *index,int max)

    {
    int     i,j;

    i=(*index);
    for (;src[i]==' ' || src[i]=='\t' || src[i]=='\n' || src[i]=='\r';i++);
    (*index)=i;
    if (src[i]=='\0')
        return(0);

    /* Is it quoted? */
    if (src[i]=='\"')
        {
        for (j=0,i++;src[i]!='\"' && src[i]!='\0';i++)
            {
            if (j<max-1)
                dst[j++]=src[i];
            }
        dst[j]='\0';
        if (src[i]=='\"')
            i++;
        }
    else
        {
        /* Not quoted */
        for (j=0;src[i]!=' ' && src[i]!='\t' && src[i]!='\n' 
                             && src[i]!='\r' && src[i]!='\0';i++)
            if (j<max-1)
                dst[j++]=src[i];
        dst[j]='\0';
        }
    for (;src[i]==' ' || src[i]=='\t' || src[i]=='\n' || src[i]=='\r';i++);
    (*index)=i;
    return(-1);
    }


void token_set_white(char *s)

    {
    strcpy(allowed_white,s);
    inited=1;
    }


void token_set_escapes(int status)

    {
    allow_escapes = status;
    }


/*
** nexttoken(FILE *stream, char *string, int max,int sameline)
**
** 1.  Skips past any white space (including ";" comments) to the
**     next character in the file.  NOTE that = and , are treated
**     as white space.
**
**     If the end of the file is encountered, a zero-length string
**     is returned, and the boolean value FALSE is returned.
**
**     If sameline==1 and a '\n' is encountered in the white
**     space, a zero-length string is returned, but TRUE is returned.
**     The file pointer is left pointing to the beginning of the
**     next line.
**
** 2.  Collects the next set of contiguous characters into the
**     string variable.  If the next contiguous string has more
**     characters than "max", then ALL of the characters are
**     read (i.e. the token is exhausted), but only the first
**     "max" characters are stored to the string.
**
**     Examples of a contiguous string:
**
**     a.  this_is_one_token
**     b.  "this is one token"
**     c.  'this is one token'
**     d.  'this " is " one " token'
**     e.  "this is one "\  "token"
**     f.  "this is one "\  any text without quotes can go here
**         and will be ignored.  "token"
**     g.   this_is_one_\   (a linefeed must immediately follow the \)
**          token
**     Note:  e., f., g. only if token_set_escapes(0) NOT called.
**
**     If a token splits across lines using the "\ method,
**     the value of sameline has no effect.
**
**     Tokens may not split across lines unless a \ is put at the
**     end of the line or after the last quote mark.
**
**     IF token_set_escapes(0) was NOT CALLED, these will also
**     be interpreted:
**     Special characters:  \n puts a linefeed in the string
**                          \f puts a formfeed in the string
**                          \" puts a double quote in the string
**                          \xHH puts binary byte HH (hex) into the string
**                             There MUST be two and only two digits
**                             specified for the hex value.
**                          \\ puts a backslash in the string
**
** 3.  The file pointer is left pointing to the first
**     white-space character which terminates the string.
**
**     If a token has quotes around it, the file pointer is left
**     pointing to the character just after the quote.
**
*/
int token_next(FILE *f,char *string,int max,int sameline,int *linenum,int *quoted)

    {
    int     c,j,d1,d2,qc;

    if (!inited)
        {
        strcpy(allowed_white," \n\f\t,=\x1a");
        inited=1;
        }
    string[0]='\0';
    if (quoted!=NULL)
        (*quoted)=0;
    for (c=fgetc(f);c!=EOF;c=fgetc(f))
        {
        if (c=='\n')
            token_incline(linenum);
        if (c=='\n' && sameline)
            return(TRUE);
        if (c==';')
            {
            if (!token_nextline(f,linenum))
                return(FALSE);
            if (sameline)
                return(TRUE);
            continue;
            }
        if (!token_space(c))
            break;
        }
    if (c==EOF)
        return(FALSE);
    j=0;
    if (!QUOTE(c))
        {
        string[j++]=c;
        for (c=fgetc(f);c!=EOF && c!=';' && !token_space(c);c=fgetc(f))
            if (j<max)
                string[j++]=c;
        if (c=='\n')
            token_incline(linenum);
        }
    else
        {
        if (quoted!=NULL)
            (*quoted)=1;
        qc=c;
        while (1)
            {
            for (c=fgetc(f);c!=EOF && c!=qc && c!='\n';c=fgetc(f))
                {
                if (allow_escapes && c=='\\')
                    {
                    c=fgetc(f);
                    if (c==EOF)
                        break;
                    if (c=='\n')
                        {
                        token_incline(linenum);
                        continue;
                        }
                    else if (c=='n')
                        {
                        if (j<max)
                            string[j++]='\n';
                        }
                    else if (c=='f')
                        {
                        if (j<max)
                            string[j++]='\f';
                        }
                    else if (c=='\"')
                        {
                        if (j<max)
                            string[j++]='\"';
                        }
                    else if (c=='x')
                        {
                        d1=tolower(fgetc(f));
                        d2=tolower(fgetc(f));
                        if (d1>'9')
                            d1=d1-'a'+10;
                        else
                            d1=d1-'0';
                        if (d2>'9')
                            d2=d2-'a'+10;
                        else
                            d2=d2-'0';
                        string[j++]=(char)((d1<<4)+d2);
                        }
                    else
                        {
                        if (j<max)
                            string[j++]=c;
                        }
                    continue;
                    }
                else
                    {
                    if (j<max)
                        string[j++]=c;
                    }
                }
            if (c=='\n')
                token_incline(linenum);
            if (c==qc)
                {
                c=fgetc(f);
                if (c=='\n')
                    token_incline(linenum);
                if (allow_escapes && c=='\\')
                    {
                    for (c=fgetc(f);1;c=fgetc(f))
                        if (c==EOF || c=='\"' || (c==';' && !token_nextline(f,linenum)))
                            break;
                    if (c==qc)
                        continue;
                    }
                }
            break;
            }
        }
    string[j]='\0';
    /* Note that fseek(f,-1L,1) on a non-binary opened file can be risky. */
    fseek(f,-1L,1);
    c=fgetc(f);
    fseek(f,-1L,1);
    if (c=='\n')
        token_decline(linenum);
    return(TRUE);
    }


static int token_space(int ch)

    {
    int i;

    for (i=0;i<64 && allowed_white[i]!='\0';i++)
        if (ch==allowed_white[i])
            return(-1);
    return(0);
    }


static int token_nextline(FILE *f,int *linenum)

    {
    int     c;

    while ((c=fgetc(f))!=EOF && c!='\n');
    if (c=='\n')
        token_incline(linenum);
    return(c!=EOF);
    }


static void token_incline(int *linenum)

    {
    if (linenum!=NULL)
        (*linenum) = (*linenum)+1;
    }


static void token_decline(int *linenum)

    {
    if (linenum!=NULL)
        (*linenum) = (*linenum)-1;
    }