~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to lefty/lefty.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This software may only be used by you under license from AT&T Corp.
 
3
    ("AT&T").  A copy of AT&T's Source Code Agreement is available at
 
4
    AT&T's Internet website having the URL:
 
5
    <http://www.research.att.com/sw/tools/graphviz/license/source.html>
 
6
    If you received this software without first entering into a license
 
7
    with AT&T, you have an infringing copy of this software and cannot use
 
8
    it without violating AT&T's intellectual property rights.
 
9
*/
 
10
 
 
11
#pragma prototyped
 
12
/* Lefteris Koutsofios - AT&T Bell Laboratories */
 
13
 
 
14
#define LEFTYVERSION "27 Nov 2001 (graphviz 1.7.16)"
 
15
 
 
16
#include "common.h"
 
17
#include "g.h"
 
18
#include "code.h"
 
19
#include "io.h"
 
20
#include "mem.h"
 
21
#include "tbl.h"
 
22
#include "parse.h"
 
23
#include "exec.h"
 
24
#include "str.h"
 
25
#include "display.h"
 
26
#include "internal.h"
 
27
#include "txtview.h"
 
28
#include "gfxview.h"
 
29
#ifdef FEATURE_GMAP
 
30
#include "gmap.h"
 
31
#include "gmap2l.h"
 
32
#endif
 
33
#ifndef FEATURE_MS
 
34
#include <sys/time.h>
 
35
#ifdef STATS
 
36
#include <sys/resource.h>
 
37
#endif
 
38
#endif
 
39
 
 
40
#ifdef FEATURE_CS
 
41
#define canread(fp) ((fp)->next < (fp)->endb)
 
42
#else
 
43
#ifdef FEATURE_GNU
 
44
#define canread(fp) ((fp)->_IO_read_end > (fp)->_IO_read_ptr)
 
45
#else
 
46
#ifdef NO_CNT_IN_FILESTRUCT
 
47
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__Mac_OSX__)
 
48
#define canread(fp) ((fp)->_r > 0)
 
49
#else
 
50
#define canread(fp) ((fp)->_IO_read_end > (fp)->_IO_read_ptr)
 
51
#endif
 
52
#else
 
53
#define canread(fp) ((fp)->_cnt > 0)
 
54
#endif
 
55
#endif
 
56
#endif
 
57
 
 
58
#ifdef FEATURE_GMAP
 
59
static int gmapon;
 
60
#endif
 
61
 
 
62
static Grect_t txtcoords = {
 
63
    { 1,  1 }, { 449, 599 },
 
64
};
 
65
 
 
66
#ifdef STATS
 
67
static time_t stime;
 
68
#endif
 
69
 
 
70
static int endflag = FALSE;
 
71
 
 
72
static char *exprstr;
 
73
static FILE *fp;
 
74
 
 
75
static int processinput (int);
 
76
static void processargs (int, char **);
 
77
static void processstr (char *);
 
78
static void printusage (void);
 
79
 
 
80
#if defined(FEATURE_X11) || defined(FEATURE_NONE)
 
81
 
 
82
int main (int argc, char **argv) {
 
83
    Tobj co;
 
84
    Psrc_t src;
 
85
 
 
86
#ifdef MTRACE
 
87
    extern int Mt_certify;
 
88
    Mt_certify = 1;
 
89
#endif
 
90
#ifdef STATS
 
91
    stime = time (NULL);
 
92
#endif
 
93
 
 
94
    idlerunmode = 0;
 
95
    exprstr = NULL;
 
96
    fp = NULL;
 
97
    init (argv[0]);
 
98
    Minit (GFXprune);
 
99
    Ginit ();
 
100
    FD_SET (Gxfd, &inputfds);
 
101
 
 
102
    Eerrlevel = 1;
 
103
    Estackdepth = 2;
 
104
    Eshowbody = 1;
 
105
    Eshowcalls = 1;
 
106
 
 
107
    processstr (leftyoptions);
 
108
    argv++, argc--;
 
109
    processargs (argc, argv);
 
110
 
 
111
    if (setjmp (exitljbuf))
 
112
        goto eop;
 
113
 
 
114
    Cinit ();
 
115
    IOinit ();
 
116
    Tinit ();
 
117
    Pinit ();
 
118
    Einit ();
 
119
    Sinit ();
 
120
    Dinit ();
 
121
    Iinit ();
 
122
    TXTinit (txtcoords);
 
123
    GFXinit ();
 
124
#ifdef FEATURE_GMAP
 
125
    gmapon = TRUE, G2Linit ();
 
126
#endif
 
127
 
 
128
    if (exprstr) {
 
129
        src.flag = CHARSRC, src.s = exprstr, src.fp = NULL;
 
130
        src.tok = -1, src.lnum = 1;
 
131
        while ((co = Punit (&src)))
 
132
            Eunit (co);
 
133
    }
 
134
    if (fp) {
 
135
        src.flag = FILESRC, src.s = NULL, src.fp = fp;
 
136
        src.tok = -1, src.lnum = 1;
 
137
        while ((co = Punit (&src)))
 
138
            Eunit (co);
 
139
    }
 
140
    if (endflag)
 
141
        goto eop;
 
142
 
 
143
    TXTupdate ();
 
144
 
 
145
    Gneedredraw = FALSE;
 
146
    for (;;) {
 
147
        if (Gneedredraw)
 
148
            GFXredraw (), Gneedredraw = FALSE;
 
149
        if (Gbuttonsdown > 0) {
 
150
            GFXmove (), Gprocessevents (FALSE, G_ONEEVENT);
 
151
            processinput (FALSE);
 
152
        } else {
 
153
            if (Mcouldgc) {
 
154
                if (!processinput (FALSE))
 
155
                    Mdogc (M_GCINCR);
 
156
            }
 
157
            if (idlerunmode) {
 
158
                if (!processinput (FALSE))
 
159
                    GFXidle ();
 
160
            } else
 
161
                processinput (TRUE);
 
162
        }
 
163
#ifdef FEATURE_GMAP
 
164
        if (gmapon)
 
165
            GMAPupdate ();
 
166
#endif
 
167
        if (Erun)
 
168
            TXTupdate (), Erun = FALSE;
 
169
    }
 
170
eop:
 
171
#ifdef PARANOID
 
172
#ifdef FEATURE_GMAP
 
173
    if (gmapon)
 
174
        G2Lterm ();
 
175
#endif
 
176
    GFXterm ();
 
177
    TXTterm ();
 
178
    Iterm ();
 
179
    Dterm ();
 
180
    Sterm ();
 
181
    Eterm ();
 
182
    Pterm ();
 
183
    Tterm ();
 
184
    IOterm ();
 
185
    Cterm ();
 
186
    Gterm ();
 
187
    Mterm ();
 
188
    FD_CLR (Gxfd, &inputfds);
 
189
    term ();
 
190
#endif
 
191
    printusage ();
 
192
    return 0;
 
193
}
 
194
 
 
195
#else
 
196
 
 
197
extern char ** __argv;
 
198
extern int __argc;
 
199
 
 
200
HANDLE hinstance, hprevinstance;
 
201
 
 
202
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
 
203
        LPSTR lpCmdLine, int nCmdShow) {
 
204
    Tobj co;
 
205
    Psrc_t src;
 
206
 
 
207
    hinstance = hInstance;
 
208
    hprevinstance = hPrevInstance;
 
209
    idlerunmode = 0;
 
210
    exprstr = NULL;
 
211
    fp = NULL;
 
212
    init (NULL);
 
213
    Ginit ();
 
214
#ifndef FEATURE_MS
 
215
    FD_SET (Gxfd, &inputfds);
 
216
#endif
 
217
 
 
218
    Eerrlevel = 1;
 
219
    Estackdepth = 2;
 
220
    Eshowbody = 1;
 
221
    Eshowcalls = 1;
 
222
 
 
223
    processstr (leftyoptions);
 
224
    __argv++, __argc--;
 
225
    processargs (__argc, __argv);
 
226
 
 
227
    if (setjmp (exitljbuf))
 
228
        goto eop;
 
229
 
 
230
    Cinit ();
 
231
    IOinit ();
 
232
    Minit (GFXprune);
 
233
    Tinit ();
 
234
    Pinit ();
 
235
    Einit ();
 
236
    Sinit ();
 
237
    Dinit ();
 
238
    Iinit ();
 
239
    TXTinit (txtcoords);
 
240
    GFXinit ();
 
241
 
 
242
    if (exprstr) {
 
243
        src.flag = CHARSRC, src.s = exprstr, src.fp = NULL;
 
244
        src.tok = -1, src.lnum = 1;
 
245
        while ((co = Punit (&src)))
 
246
            Eunit (co);
 
247
    }
 
248
    if (fp) {
 
249
        src.flag = FILESRC, src.s = NULL, src.fp = fp;
 
250
        src.tok = -1, src.lnum = 1;
 
251
        while ((co = Punit (&src)))
 
252
            Eunit (co);
 
253
    }
 
254
    if (endflag)
 
255
        goto eop;
 
256
 
 
257
    TXTupdate ();
 
258
 
 
259
    Gneedredraw = FALSE;
 
260
    for (;;) {
 
261
        if (Gneedredraw)
 
262
            GFXredraw (), Gneedredraw = FALSE;
 
263
        if (Gbuttonsdown > 0)
 
264
            GFXmove (), Gprocessevents (FALSE, G_ONEEVENT);
 
265
        else {
 
266
            if (Mcouldgc) {
 
267
                if (!processinput (FALSE))
 
268
                    Mdogc (M_GCINCR);
 
269
            } else if (idlerunmode) {
 
270
                if (!processinput (FALSE))
 
271
                    GFXidle ();
 
272
            } else
 
273
                processinput (TRUE);
 
274
        }
 
275
        if (Erun)
 
276
            TXTupdate (), Erun = FALSE;
 
277
    }
 
278
 
 
279
eop:
 
280
#ifdef PARANOID
 
281
    GFXterm ();
 
282
    TXTterm ();
 
283
    Iterm ();
 
284
    Dterm ();
 
285
    Sterm ();
 
286
    Eterm ();
 
287
    Pterm ();
 
288
    Tterm ();
 
289
    Mterm ();
 
290
    IOterm ();
 
291
    Cterm ();
 
292
    Gterm ();
 
293
    term ();
 
294
#endif
 
295
    printusage ();
 
296
    exit (0);
 
297
}
 
298
 
 
299
#endif
 
300
 
 
301
#ifndef FEATURE_MS
 
302
static struct timeval longwait = { 1000, 0 };
 
303
static struct timeval zerowait = { 0, 0 };
 
304
#endif
 
305
 
 
306
static int processinput (int waitflag) {
 
307
    fd_set fdset;
 
308
#ifndef FEATURE_MS
 
309
    struct timeval tz, *tzp;
 
310
#else
 
311
    HANDLE evs[MAXIMUM_WAIT_OBJECTS];
 
312
    int evn;
 
313
#endif
 
314
    int n, rtn;
 
315
    int ioi;
 
316
 
 
317
    rtn = 0;
 
318
    while (Gprocessevents (FALSE, G_MANYEVENTS))
 
319
        rtn = 1;
 
320
#ifndef FEATURE_MS
 
321
    for (ioi = 0, n = 0; ioi < ion; ioi++)
 
322
        if (iop[ioi].inuse && iop[ioi].ismonitored &&
 
323
                FD_ISSET (fileno (iop[ioi].ifp), &inputfds) &&
 
324
                canread (iop[ioi].ifp))
 
325
            GFXmonitorfile (ioi), n++;
 
326
    if (n || rtn)
 
327
        return 1;
 
328
    if (Gneedredraw)
 
329
        return 0;
 
330
    tz = (waitflag && !rtn) ? longwait : zerowait;
 
331
    tzp = &tz;
 
332
    fdset = inputfds;
 
333
    if ((n = select (FD_SETSIZE, &fdset, NULL, NULL, tzp)) <= 0)
 
334
        return rtn;
 
335
    rtn = 1;
 
336
    if (FD_ISSET (Gxfd, &fdset))
 
337
        Gprocessevents (TRUE, G_MANYEVENTS), n--;
 
338
    if (!n)
 
339
        return rtn;
 
340
    Gsync ();
 
341
    for (ioi = 0; n > 0 && ioi < ion; ioi++)
 
342
        if (iop[ioi].inuse && iop[ioi].ismonitored &&
 
343
                FD_ISSET (fileno (iop[ioi].ifp), &fdset))
 
344
            GFXmonitorfile (ioi), n--;
 
345
#else
 
346
    for (ioi = 0, n = 0, evn = 0; ioi < ion; ioi++) {
 
347
        if (!iop[ioi].inuse || !IOismonitored (ioi, inputfds))
 
348
            continue;
 
349
        if ((iop[ioi].type == IO_FILE && canread (iop[ioi].ifp)) ||
 
350
                (iop[ioi].type == IO_PIPE && iop[ioi].buf[0]))
 
351
            GFXmonitorfile (ioi), n++;
 
352
        if (iop[ioi].type != IO_PIPE)
 
353
            continue;
 
354
        evs[evn++] = iop[ioi].ifp;
 
355
    }
 
356
    if (n)
 
357
        return 1;
 
358
    if (Gneedredraw)
 
359
        return 0;
 
360
    n = MsgWaitForMultipleObjects (evn, evs, FALSE,
 
361
            (waitflag && !rtn) ? 1 : 0, QS_ALLINPUT);
 
362
    if (n == WAIT_TIMEOUT || n < WAIT_OBJECT_0 || n > WAIT_OBJECT_0 + evn)
 
363
        return rtn;
 
364
    if (n == WAIT_OBJECT_0 + evn)
 
365
        Gprocessevents (TRUE, G_MANYEVENTS);
 
366
    Gsync ();
 
367
    for (ioi = 0; ioi < ion; ioi++)
 
368
        if (iop[ioi].inuse && IOismonitored (ioi) &&
 
369
                (iop[ioi].type == IO_FILE || (iop[ioi].type == IO_PIPE &&
 
370
                evs[n - WAIT_OBJECT_0] == iop[ioi].ifp)))
 
371
            GFXmonitorfile (ioi);
 
372
#endif
 
373
    return rtn;
 
374
}
 
375
 
 
376
static void processstr (char *buf) {
 
377
    char *words[100];
 
378
    char *s, *s1;
 
379
    int i;
 
380
 
 
381
    if (!(s = buf) || *s == 0)
 
382
        return;
 
383
    s = strdup (s);
 
384
    for (i = 0, s1 = s; *s1; ) {
 
385
        for (; *s1 && *s1 == ' '; s1++)
 
386
            ;
 
387
        if (!*s1)
 
388
            break;
 
389
        words[i++] = s1;
 
390
        for (; *s1 && *s1 != ' '; s1++)
 
391
            ;
 
392
        if (*s1)
 
393
            *s1 = '\000', s1++;
 
394
    }
 
395
    words[i] = NULL;
 
396
    processargs (i, words);
 
397
    free (s);
 
398
}
 
399
 
 
400
static void processargs (int argc, char *argv[]) {
 
401
    while (argc) {
 
402
        if (Strcmp (argv[0], "-x") == 0)
 
403
            endflag = TRUE;
 
404
        else if (Strcmp (argv[0], "-e") == 0)
 
405
            exprstr = argv[1], argv++, argc--;
 
406
        else if (Strcmp (argv[0], "-el") == 0)
 
407
            Eerrlevel = atoi (argv[1]), argv++, argc--;
 
408
        else if (Strcmp (argv[0], "-sd") == 0)
 
409
            Estackdepth = atoi (argv[1]), argv++, argc--;
 
410
        else if (Strcmp (argv[0], "-sb") == 0)
 
411
            Eshowbody = atoi (argv[1]), argv++, argc--;
 
412
        else if (Strcmp (argv[0], "-sc") == 0)
 
413
            Eshowcalls = atoi (argv[1]), argv++, argc--;
 
414
        else if (Strcmp (argv[0], "-df") == 0)
 
415
            Gdefaultfont = argv[1], argv++, argc--;
 
416
        else if (Strcmp (argv[0], "-w") == 0)
 
417
            warnflag = TRUE;
 
418
        else if (Strcmp (argv[0], "-ps") == 0)
 
419
            Gpscanvasname = argv[1], argv++, argc--;
 
420
        else if (Strcmp (argv[0], "-V") == 0)
 
421
            fprintf (stderr, "lefty version %s\n", LEFTYVERSION);
 
422
        else if (Strcmp (argv[0], "-") == 0)
 
423
            fp = stdin;
 
424
        else {
 
425
            if ((fp = fopen (argv[0], "r")) == NULL)
 
426
                panic (POS, "main", "cannot open input file: %s", argv[0]);
 
427
        }
 
428
        argv++, argc--;
 
429
    }
 
430
    if (Eerrlevel > 1)
 
431
        Gerrflag = TRUE;
 
432
}
 
433
 
 
434
static void printusage (void) {
 
435
#ifdef STATS
 
436
    struct rusage ru;
 
437
 
 
438
    getrusage (RUSAGE_SELF, &ru);
 
439
    Mreport ();
 
440
#ifdef FEATURE_RUSAGE
 
441
    printf ("user   time %13.3lf\n",
 
442
            ru.ru_utime.tv_sec + ru.ru_utime.tv_nsec / 1000000000.0);
 
443
    printf ("system time %13.3lf\n",
 
444
            ru.ru_stime.tv_sec + ru.ru_stime.tv_nsec / 1000000000.0);
 
445
#else
 
446
    printf ("user   time %13.3lf\n",
 
447
            ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1000000.0);
 
448
    printf ("system time %13.3lf\n",
 
449
            ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1000000.0);
 
450
#endif
 
451
    printf ("resident size  %10d\n", ru.ru_maxrss * getpagesize ());
 
452
    printf ("input            %8d\n", ru.ru_inblock);
 
453
    printf ("output           %8d\n", ru.ru_oublock);
 
454
    printf ("socket msgs sent %8d\n", ru.ru_msgsnd);
 
455
    printf ("socket msgs rcvd %8d\n", ru.ru_msgrcv);
 
456
    printf ("real time        %8d\n", time (NULL) - stime);
 
457
    fflush (stdout);
 
458
#endif
 
459
}