aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/config/pswrap/pswsemantics.c
blob: caaff800ec05dbffdc745c14e1271719a7aa2818 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
 *  pswsemantics.c
 *
 * (c) Copyright 1988-1994 Adobe Systems Incorporated.
 * All rights reserved.
 * 
 * Permission to use, copy, modify, distribute, and sublicense this software
 * and its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notices appear in all copies and that
 * both those copyright notices and this permission notice appear in
 * supporting documentation and that the name of Adobe Systems Incorporated
 * not be used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  No trademark license
 * to use the Adobe trademarks is hereby granted.  If the Adobe trademark
 * "Display PostScript"(tm) is used to describe this software, its
 * functionality or for any other purpose, such use shall be limited to a
 * statement that this software works in conjunction with the Display
 * PostScript system.  Proper trademark attribution to reflect Adobe's
 * ownership of the trademark shall be given whenever any such reference to
 * the Display PostScript system is made.
 * 
 * ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR
 * ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
 * ADOBE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON- INFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO EVENT SHALL ADOBE BE LIABLE
 * TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE, STRICT LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  ADOBE WILL NOT
 * PROVIDE ANY TRAINING OR OTHER SUPPORT FOR THE SOFTWARE.
 * 
 * Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
 * Incorporated which may be registered in certain jurisdictions
 * 
 * Author:  Adobe Systems Incorporated
 */

/***********/
/* Imports */
/***********/

#include <stdlib.h>
#include <stdio.h>

#ifdef XENVIRONMENT
#include <X11/Xos.h>
#else
#include <string.h>
#endif

#include "pswdict.h"
#include "pswpriv.h"
#include "pswsemantics.h"

/***********************/
/* Module-wide globals */
/***********************/

char *currentPSWName = NULL;
int reportedPSWName = 0;

static PSWDict currentDict = NULL;


/*************************************************/
/* Procedures called by the parser's annotations */
/*************************************************/

static boolean IsCharType(Type t)
{
  return (t == T_CHAR || t == T_UCHAR);
}

static boolean IsNumStrType(Type t)
{
	return (t == T_NUMSTR
			|| t == T_FLOATNUMSTR
			|| t == T_LONGNUMSTR
			|| t == T_SHORTNUMSTR);
}
  
void PSWName(char *s)
{
  currentPSWName = psw_malloc(strlen(s)+1);
  strcpy(currentPSWName, s);
  reportedPSWName = 0;
}
  
  /* Generate the code for this wrap now */
void FinalizePSWrapDef(Header hdr, Body body)
{
  if (header && ! hdr->isStatic) EmitPrototype(hdr); 

  printf("#line %d \"%s\"\n", ++outlineno, ofile);
  EmitBodyHeader(hdr);
  
  printf("{\n"); outlineno++;
  EmitBody(body, hdr);
  printf("}\n"); outlineno++;
  printf("#line %d \"%s\"\n", yylineno, ifile); outlineno++;

  /* release storage for this wrap */
  /* Omit if you have lots of memory and want pswrap lean and mean */
  if (bigFile) {
    register Arg arg, nextarg; register Item item, nextitem;
    for(arg = hdr->inArgs; arg; arg = nextarg) {
	nextarg = arg->next;
	for(item = arg->items; item; item = nextitem) {
	    nextitem = item->next;
	    if (item->subscripted) {
		  if (!item->subscript->constant) free(item->subscript->name);
		  free(item->subscript);
		  if(item->scaled) {
		  	if (!item->scale->constant) free(item->scale->name);
		  	free(item->scale);
		  }
	    }
	    free(item->name); free(item);
	}
	free(arg);
    }
    for(arg = hdr->outArgs; arg; arg = nextarg) {
	nextarg = arg->next;
	for(item = arg->items; item; item = nextitem) {
	    nextitem = item->next;
	    if (item->subscripted) {
		if (!item->subscript->constant) free(item->subscript->name);
		free(item->subscript);
	    }
	    free(item->name); free(item);
	}
	free(arg);
    }
    free(hdr->name); free(hdr);
    FreeBody(body);
  }

  DestroyPSWDict(currentDict);
  currentDict = NULL;
  currentPSWName = NULL;
  reportedPSWName = 0;
}

  /* Complete construction of the Header tree and make some semantic checks */
Header PSWHeader(boolean isStatic, Args inArgs, Args outArgs)
{
  char *name = currentPSWName;
  register Arg arg, prevArg;
  register Item item, prevItem;
  int nextTag = 0;
  
  Header hdr = (Header)psw_calloc(sizeof(HeaderRec), 1);
  hdr->isStatic = isStatic;
  hdr->name = name;

  currentDict = CreatePSWDict(511);

  prevArg = NULL;
  for (arg = inArgs; arg; arg = arg->next) { /* foreach input arg */
    prevItem = NULL;
    for (item = arg->items; item; item = item->next) {
      if (IsCharType(arg->type)
          && !(item->starred || item->subscripted)) {
          ErrIntro(item->sourceLine);
          fprintf(stderr,
                 "char input parameter %s must be starred or subscripted\n",
                 item->name);
          /* remove item from list */
          if (prevItem) {prevItem->next = item->next;}
          else if (item == arg->items) {arg->items = item->next;};
          /* free(item);  XXX? */
          continue;
      }
      if(item->scaled && !IsNumStrType(arg->type)) {
          ErrIntro(item->sourceLine);
          fprintf(stderr,"only numstring parameters may be scaled\n");
	  }
      if (IsNumStrType(arg->type)
	      && (item->starred || !item->subscripted)) {
          ErrIntro(item->sourceLine);
          fprintf(stderr,
                 "numstring parameter %s may only be subscripted\n",
                 item->name);
          /* remove item from list */
          if (prevItem) {prevItem->next = item->next;}
          else if (item == arg->items) {arg->items = item->next;};
          /* free(item);  XXX? */
          continue;
      }
      if (arg->type != T_CONTEXT) {
          if (PSWDictLookup(currentDict, item->name) != -1) {
             ErrIntro(item->sourceLine);
             fprintf(stderr,"parameter %s reused\n", item->name);
             if (prevItem) {prevItem->next = item->next;}
	         else if (item == arg->items) {arg->items = item->next;};
	         /* free this ? */
	         continue;
          }
	      PSWDictEnter(currentDict, item->name, (PSWDictValue) item);
          item->isoutput = false;
          item->type = arg->type;
          prevItem = item;
      }
    }
    if (arg->items == NULL) {
      	if (prevArg) { prevArg->next = arg->next;}
	    else if (arg == inArgs) {inArgs = arg->next;}
	    continue;
    }
      prevArg = arg;
  }

  prevArg = NULL;
  for (arg = outArgs; arg; arg = arg->next) { /* foreach output arg */
    prevItem = NULL;
    for (item = arg->items; item; item = item->next) {
      if (arg->type == T_USEROBJECT) {
 	     ErrIntro(item->sourceLine);
         fprintf(stderr,"output parameter %s can not be of type userobject\n",
               item->name);
 	     /* remove item from list */
 	     if (prevItem) {prevItem->next = item->next;}
 	     else if (item == arg->items) {arg->items = item->next;};
 	    /* free(item); XXX */
 	   continue;
      }
      if (arg->type == T_NUMSTR || arg->type == T_FLOATNUMSTR
      	  || arg->type == T_LONGNUMSTR || arg->type == T_SHORTNUMSTR) {
 	     ErrIntro(item->sourceLine);
         fprintf(stderr,"output parameter %s can not be of type numstring\n",
               item->name);
 	     /* remove item from list */
 	     if (prevItem) {prevItem->next = item->next;}
 	     else if (item == arg->items) {arg->items = item->next;};
 	    /* free(item); XXX */
 	   continue;
      }
      if (!(item->starred || item->subscripted)) {
	    ErrIntro(item->sourceLine);
        fprintf(stderr,"output parameter %s must be starred or subscripted\n",
              item->name);
	    /* remove item from list */
	    if (prevItem) {prevItem->next = item->next;}
	    else if (item == arg->items) {arg->items = item->next;};
	    /* free(item); XXX */
	    continue;
      }
      if (PSWDictLookup(currentDict, item->name) != -1) {
	    ErrIntro(item->sourceLine);
	    fprintf(stderr,"parameter %s reused\n", item->name);
	    /* remove item from list */
	    if (prevItem) {prevItem->next = item->next;}
	    else if (item == arg->items) {arg->items = item->next;};
	    /* free the storage? XXX */
        continue;
      }
      PSWDictEnter(currentDict, item->name, (PSWDictValue) item);
      item->isoutput = true;
      item->type = arg->type;
      item->tag = nextTag++;
      prevItem = item;
   } /* inside for loop */
   if (arg->items == NULL) {
    if (prevArg) { 
    	prevArg->next = arg->next;
    } else if (arg == outArgs) {
    	outArgs = arg->next;
    }
    continue;
   }
      prevArg = arg;
  } /* outside for loop */

  /* now go looking for subscripts that name an input arg */
  for (arg = inArgs; arg; arg = arg->next) { /* foreach input arg */
    for (item = arg->items; item; item = item->next) {
      if (item->subscripted && !item->subscript->constant) {
        PSWDictValue v = PSWDictLookup(currentDict, item->subscript->name);
        if (v != -1) {
          Item subItem = (Item)v;
          if (subItem->isoutput) {
	    ErrIntro(subItem->sourceLine);
            fprintf(stderr,"output parameter %s used as a subscript\n",
	    	subItem->name);
            continue;
            }
          if (subItem->type != T_INT) {
	    ErrIntro(subItem->sourceLine); 
            fprintf(stderr,
	        "input parameter %s used as a subscript is not an int\n",
                subItem->name);
            continue;
            }
          }
        }
      }
    }
  
  for (arg = outArgs; arg; arg = arg->next) { /* foreach output arg */
    for (item = arg->items; item; item = item->next) {
      if (item->subscripted && !item->subscript->constant) {
        PSWDictValue v = PSWDictLookup(currentDict, item->subscript->name);
        if (v != -1) {
          Item subItem = (Item)v;
          if (subItem->isoutput) {
	    ErrIntro(subItem->sourceLine);
            fprintf(stderr,"output parameter %s used as a subscript\n",
	    	subItem->name);
            continue;
            }
          if (subItem->type != T_INT) {
	    ErrIntro(subItem->sourceLine);
	    fprintf(stderr,
	       "input parameter %s used as a subscript is not an int\n",
               subItem->name);
            continue;
            }
          }
        }
      }
    }
  
  hdr->inArgs = inArgs;
  hdr->outArgs = outArgs;

  return hdr;
}

Token PSWToken(Type type, char *val)
{
  register Token token = (Token)psw_calloc(sizeof(TokenRec), 1);
 
  token->next = NULL;
  token->type = type;
  token->val = val;
  token->sourceLine = yylineno;

  switch (type) {
    case T_STRING:
    case T_NAME:
    case T_LITNAME: {
      Item dictVal = (Item) PSWDictLookup(currentDict, (char *)val);
      if ((PSWDictValue) dictVal != -1) {
	  if ((type != T_NAME) && (dictVal->isoutput)) {
	      ErrIntro(yylineno);
	      fprintf(stderr,"output parameter %s used as %s\n",
		dictVal->name,
		(type == T_STRING) ? "string": "literal name");
	  } else 
	      if ((type != T_NAME) && !IsCharType(dictVal->type)) {
	      	ErrIntro(yylineno);
	      	fprintf(stderr,"non-char input parameter %s used as %s\n",
			dictVal->name,
			(type == T_STRING) ? "string": "literal name");
	      } else 
	      	token->namedFormal = dictVal; /* ok, so assign a value */
      }
      break;
    }
    default:
      break;
    }

  return token;
}

Token PSWToken2(Type type, char *val, char *ind)
{
  register Token token = (Token)psw_calloc(sizeof(TokenRec), 1);
  Item dictVal = (Item) PSWDictLookup(currentDict, val);
  Item dvi;

  token->next = NULL;
  token->type = type;
  token->val = val;
  token->sourceLine = yylineno;

  /* Assert(type == T_SUBSCRIPTED); */
  if (((PSWDictValue) dictVal == -1) || (dictVal->isoutput)) {
    ErrIntro(yylineno);
    fprintf(stderr,"%s not an input parameter\n", val);
  }
  else if (!dictVal->subscripted) {
    ErrIntro(yylineno);
    fprintf(stderr,"%s not an array\n", val);
  }
  else if (dictVal->type >= T_NUMSTR) {
    ErrIntro(yylineno);
    fprintf(stderr,"cannot subscript numstring %s\n", val);
  }
  else if (IsCharType(dictVal->type)) {
    ErrIntro(yylineno);
    fprintf(stderr,"%s not a scalar type\n", val);
  }
  else {
    dvi = (Item) PSWDictLookup(currentDict, (char *)ind);
    if (((PSWDictValue) dvi != -1)
    && ((dvi->isoutput) || IsCharType(dvi->type))) {
      ErrIntro(yylineno);
      fprintf(stderr,"%s wrong type\n",(char *) ind);
    }
    else {
      token->body.var = (char *) ind;
      token->namedFormal = dictVal; /* ok, so assign a value */
      return token;
    }
  }

  /*  ERRORS fall through */
  free(token);
  return (PSWToken(T_NAME,val));
}

Arg PSWArg(Type type, Items items)
{
  register Arg arg = (Arg)psw_calloc(sizeof(ArgRec), 1);
  arg->next = NULL;
  arg->type = type;
  arg->items = items;
  return arg;
}

Item PSWItem(char *name)
{
  register Item item = (Item)psw_calloc(sizeof(ItemRec), 1);
  item->next = NULL;
  item->name = name;
  item->sourceLine = yylineno;
  return item;
}

Item PSWStarItem(char *name)
{
  register Item item = (Item)psw_calloc(sizeof(ItemRec), 1);
  item->next = NULL;
  item->name = name;
  item->starred = true;
  item->sourceLine = yylineno;
  return item;
}

Item PSWSubscriptItem(char *name, Subscript subscript)
{
  register Item item = (Item)psw_calloc(sizeof(ItemRec), 1);
  item->next = NULL;
  item->name = name;
  item->subscript = subscript;
  item->subscripted = true;
  item->sourceLine = yylineno;
  return item;
}

Item PSWScaleItem(char *name, Subscript subscript, char *nameval, int val)
{
  Item item;
  Scale scale = (Scale)psw_calloc(sizeof(ScaleRec), 1);
  item = PSWSubscriptItem(name, subscript);
  item->scaled = true;
  if(nameval)
  	scale->name = nameval;
  else {
	scale->constant = true;
  	scale->val = val;
  }
  item->scale = scale;
  return(item);
}
  
Subscript PSWNameSubscript(char *name)
{
  Subscript subscript = (Subscript)psw_calloc(sizeof(SubscriptRec), 1);
  subscript->name = name;
  return subscript;
}

Subscript PSWIntegerSubscript(int val)
{
  Subscript subscript = (Subscript)psw_calloc(sizeof(SubscriptRec), 1);
  subscript->constant = true;
  subscript->val = val;
  return subscript;
}

Args ConsPSWArgs(Arg arg, Args args)
{
  arg->next = args;
  return arg;
}

Tokens AppendPSWToken(Token token, Tokens tokens)
{
  register Token t;
  static Token firstToken, lastToken;	/* cache ptr to last */
  
  if ((token->type == T_NAME) && (token->namedFormal)) {
    if( token->namedFormal->isoutput) {
	Token oldtoken;
    	char *pos = "printobject";
    	char *ss = psw_malloc(strlen(pos) + 1);
   	strcpy(ss, pos);
    	free(token->val);
	oldtoken = token;
   	token = PSWToken(T_INT, (char *) token->namedFormal->tag);
    	free((char *)oldtoken);
    	token->next = PSWToken(T_NAME, ss);
     } else 
    	if (token->namedFormal->type == T_USEROBJECT) {
   		char *pos = "execuserobject";
    		char *ss = psw_malloc(strlen(pos) + 1);
    		strcpy(ss, pos);
   		token->next = PSWToken(T_NAME, ss);
   	}
   }
   	
  if (tokens == NULL) {
    firstToken = lastToken = token;
    return token;
  }
  
  if (tokens != firstToken)
    firstToken = lastToken = tokens;
  for (t = lastToken; t->next; t = t->next);
  lastToken = t->next = token;

  return tokens;
}

Args AppendPSWArgs(Arg arg, Args args)
{
  register Arg a;
  arg->next = NULL;
  if (args == NULL) return arg;
  
  for (a = args; a->next; a = a->next);

  a->next = arg; 
  return args;
}

Items AppendPSWItems(Item item, Items items)
{
  register Item t;
  item->next = NULL;
  if (items == NULL) return item;
  
  for (t = items; t->next; t = t->next);

  t->next = item; 
  return items;
}