File: warn-raw.c

package info (click to toggle)
cxref 1.5c-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,504 kB
  • ctags: 1,359
  • sloc: ansic: 16,721; yacc: 1,930; sh: 722; lex: 416; makefile: 345; lisp: 256; python: 80; perl: 78
file content (724 lines) | stat: -rw-r--r-- 20,845 bytes parent folder | download
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/***************************************
  $Header: /home/amb/cxref/RCS/warn-raw.c 1.28 2001/01/06 13:05:13 amb Exp $

  C Cross Referencing & Documentation tool. Version 1.5c.

  Writes the raw information and / or warnings out.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1995,96,97,99,2001 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/

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

#include "datatype.h"
#include "cxref.h"
#include "memory.h"

static void WriteWarnRawFilePart(File file);
static void WriteWarnRawInclude(Include inc);
static void WriteWarnRawSubInclude(Include inc,int depth);
static void WriteWarnRawDefine(Define def);
static void WriteWarnRawTypedef(Typedef type);
static void WriteWarnRawStructUnion(StructUnion su, int depth,StructUnion base);
static void WriteWarnRawVariable(Variable var);
static void WriteWarnRawFunction(Function func);

/*+ Output option. +*/
extern int option_warn,option_raw,option_xref,option_index;

/*+ The name of the current file. +*/
static char* filename=NULL;

/*++++++++++++++++++++++++++++++++++++++
  Write the raw / warning output for a complete File structure and all components.

  File file The File structure to output.
  ++++++++++++++++++++++++++++++++++++++*/

void WriteWarnRawFile(File file)
{
 Include   inc =file->includes;
 Define    def =file->defines;
 Typedef   type=file->typedefs;
 Variable  var=file->variables;
 Function  func=file->functions;

 filename=file->name;

 /*+ The file structure is broken into its components and they are each written out. +*/

 if(option_raw)
    printf("----------------------------------------\n");

 WriteWarnRawFilePart(file);

 while(inc)
   {
    WriteWarnRawInclude(inc);
    inc=inc->next;
   }

 while(def)
   {
    WriteWarnRawDefine(def);
    def=def->next;
   }

 while(type)
   {
    WriteWarnRawTypedef(type);
    type=type->next;
   }

 while(var)
   {
    WriteWarnRawVariable(var);
    var=var->next;
   }

 while(func)
   {
    WriteWarnRawFunction(func);
    func=func->next;
   }

 if(option_raw)
    printf("----------------------------------------\n\n");
}


/*++++++++++++++++++++++++++++++++++++++
  Write a File structure out.

  File file The File structure to output.
  ++++++++++++++++++++++++++++++++++++++*/

static void WriteWarnRawFilePart(File file)
{
 int i;

 if(option_raw)
    printf("FILE : '%s'\n",file->name);

 if(file->comment && option_raw)
    printf("<<<\n%s\n>>>\n",file->comment);

 if(option_warn&WARN_COMMENT && !file->comment)
    printf("Warning %16s : File does not have a comment.\n",filename);

 if(option_xref&XREF_FILE)
   {
    if(option_raw)
       for(i=0;i<file->inc_in->n;i++)
          printf("Included in %s\n",file->inc_in->s[i]);

    if(option_warn&WARN_XREF)
      {
       int len=strlen(file->name)-2;
       if(!file->inc_in->n && !strcmp(&file->name[len],".h"))
          printf("Warning %16s : Header file %s is not included in any files.\n",filename,file->name);
       if( file->inc_in->n && !strcmp(&file->name[len],".c"))
          printf("Warning %16s : Source file %s is included in another file.\n",filename,file->name);
      }
   }

 if(option_xref&XREF_FUNC)
    for(i=0;i<file->f_refs->n;i++)
      {
       if(option_raw)
         {
          if(file->f_refs->s2[i])
             printf("References Function %s : %s\n",file->f_refs->s1[i],file->f_refs->s2[i]);
          else
             printf("References Function %s\n",file->f_refs->s1[i]);
         }
       if(option_warn&WARN_XREF && !file->f_refs->s2[i])
          printf("Warning %16s : File references function %s() whose definition is unknown.\n",filename,file->f_refs->s1[i]);
      }

 if(option_xref&XREF_VAR)
    for(i=0;i<file->v_refs->n;i++)
      {
       if(option_raw)
         {
          if(file->v_refs->s2[i])
             printf("References Variable %s : %s\n",file->v_refs->s1[i],file->v_refs->s2[i]);
          else
             printf("References Variable %s\n",file->v_refs->s1[i]);
         }
       if(option_warn&WARN_XREF && !file->v_refs->s2[i])
          printf("Warning %16s : File references variable %s whose definition is unknown.\n",filename,file->v_refs->s1[i]);
      }
}


/*++++++++++++++++++++++++++++++++++++++
  Write an Include structure out.

  Include inc The Include structure to output.
  ++++++++++++++++++++++++++++++++++++++*/

static void WriteWarnRawInclude(Include inc)
{
 if(option_raw)
    printf("\nINCLUDES : '%s' [%s file]\n",inc->name,(inc->scope==GLOBAL?"System":"Local"));

 if(inc->comment && option_raw)
    printf("<<<\n%s\n>>>\n",inc->comment);
 if(option_warn&WARN_COMMENT && !inc->comment)
    printf("Warning %16s : #Include %s does not have a comment.\n",filename,inc->name);

 if(option_raw && inc->includes)
    WriteWarnRawSubInclude(inc->includes,1);
}


/*++++++++++++++++++++++++++++++++++++++
  Write an Sub-Include structure out.

  Include inc The Include structure to output.

  int depth The depth of the include hierarchy.
  ++++++++++++++++++++++++++++++++++++++*/

static void WriteWarnRawSubInclude(Include inc,int depth)
{
 int i;

 while(inc)
   {
    for(i=0;i<depth;i++) printf("   ");
    printf("INCLUDES : '%s' [%s file]\n",inc->name,(inc->scope==GLOBAL?"System":"Local"));

    if(inc->includes)
       WriteWarnRawSubInclude(inc->includes,depth+1);

    inc=inc->next;
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Write a Define structure out.

  Define def The Define structure to output.
  ++++++++++++++++++++++++++++++++++++++*/

static void WriteWarnRawDefine(Define def)
{
 int i;

 if(option_raw)
   {
    printf("\nDEFINES : '%s' ",def->name);

    if(def->value)
       printf("= %s",def->value);

    if(def->args->n)
      {
       printf("(");
       for(i=0;i<def->args->n;i++)
          printf(i?",%s":"%s",def->args->s1[i]);
       printf(")");
      }

    printf("\n");
   }

 if(def->comment && option_raw)
    printf("<<<\n%s\n>>>\n",def->comment);
 if(option_warn&WARN_COMMENT && !def->comment)
    printf("Warning %16s : #Define %s does not have a comment.\n",filename,def->name);

 if(option_raw)
    printf("Defined: %s:%d\n",filename,def->lineno);

 for(i=0;i<def->args->n;i++)
   {
    if(option_raw)
      {
       if(def->args->s2[i])
          printf("Arguments: %s <<<%s>>>\n",def->args->s1[i],def->args->s2[i]);
       else
          printf("Arguments: %s\n",def->args->s1[i]);
      }
    if(option_warn&WARN_COMMENT && !def->args->s2[i])
       printf("Warning %16s : #Define %s has an argument %s with no comment.\n",filename,def->name,def->args->s1[i]);
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Write a Typedef structure out.

  Typedef type The Typedef structure to output.
  ++++++++++++++++++++++++++++++++++++++*/

static void WriteWarnRawTypedef(Typedef type)
{
 if(option_raw)
   {
    if(type->type)
       printf("\nTYPEDEF : '%s'\n",type->name);
    else
       printf("\nTYPE : '%s'\n",type->name);
   }

 if(type->comment && option_raw)
    printf("<<<\n%s\n>>>\n",type->comment);
 if(option_warn&WARN_COMMENT && !type->comment)
    printf("Warning %16s : Type %s does not have a comment.\n",filename,type->name);

 if(option_raw)
    printf("Defined: %s:%d\n",filename,type->lineno);

 if(option_raw)
    if(type->type)
       printf("Type: %s\n",type->type);

 if(option_raw)
    if(type->typexref)
       printf("See: %s %s\n",type->typexref->type?"Typedef":"Type",type->typexref->name);

 if(type->sutype)
    WriteWarnRawStructUnion(type->sutype,0,type->sutype);
}


/*++++++++++++++++++++++++++++++++++++++
  Write a structure / union / enum out.

  StructUnion su The structure / union / enum to write.

  int depth The depth within the structure.

  StructUnion base The base struct union that this one is part of.
  ++++++++++++++++++++++++++++++++++++++*/

static void WriteWarnRawStructUnion(StructUnion su, int depth,StructUnion base)
{
 int i;
 char* splitsu=NULL;

 if(option_warn&WARN_COMMENT && depth && !su->comment)
    printf("Struct/Union component %s in %s does not have a comment.\n",su->name,base->name);

 splitsu=strstr(su->name,"{...}");
 if(splitsu) splitsu[-1]=0;

 if(option_raw)
   {
    for(i=0;i<depth;i++) printf("   ");
    if(depth && su->comment && !su->comps)
       printf("%s; <<<%s>>>\n",su->name,su->comment);
    else if(!depth || su->comps)
       printf("%s\n",su->name);
    else
       printf("%s;\n",su->name);
   }

 if(!depth || su->comps)
   {
    if(option_raw)
      {
       for(i=0;i<depth;i++) printf("   ");
       printf("  {\n");
      }
    for(i=0;i<su->n_comp;i++)
       WriteWarnRawStructUnion(su->comps[i],depth+1,base);
    if(option_raw)
      {
       for(i=0;i<depth;i++) printf("   ");
       printf("  }\n");
       if(splitsu)
         {
          for(i=0;i<depth;i++) printf("   ");
          if(depth && su->comment)
             printf("%s; <<<%s>>>\n",splitsu[5]?&splitsu[6]:"",su->comment);
          else
             printf("%s;\n",splitsu[5]?&splitsu[6]:"");
         }
      }
   }

 if(splitsu) splitsu[-1]=' ';
}


/*++++++++++++++++++++++++++++++++++++++
  Write a Variable structure out.

  Variable var The Variable structure to output.
  ++++++++++++++++++++++++++++++++++++++*/

static void WriteWarnRawVariable(Variable var)
{
 int i;

 if(option_raw)
   {
    int done=0;

    printf("\nVARIABLE : %s [",var->name);
    if(var->scope&LOCAL)    done=printf("Local");
    if(var->scope&GLOBAL)   done=printf("Global");
    if(var->scope&EXTERNAL) done=printf("%sExternal",done?" and ":"");
    if(var->scope&EXTERN_H) done=printf("%sExternal from header file",done?" and ":"");
    if(var->scope&EXTERN_F)      printf("%sExternal within function",done?" and ":"");
    printf("]\n");

    if(var->comment)
       printf("<<<\n%s\n>>>\n",var->comment);
   }

 if(option_warn&WARN_COMMENT && !var->comment && (var->scope&(GLOBAL|LOCAL|EXTERNAL|EXTERN_F) || option_raw))
    printf("Warning %16s : Variable %s does not have a comment.\n",filename,var->name);

 if(option_raw)
    printf("Defined: %s:%d\n",var->incfrom?var->incfrom:filename,var->lineno);

 if(option_raw)
    printf("Type: %s\n",var->type);

 if(option_raw && var->incfrom)
    printf("Included from: %s\n",var->incfrom);

 if(option_xref&XREF_VAR)
   {
    if(option_raw)
      {
       if(var->scope&(EXTERNAL|EXTERN_F) && var->defined)
          printf("Declared global in '%s'\n",var->defined);

       if(var->scope&(GLOBAL|LOCAL))
         {
          for(i=0;i<var->visible->n;i++)
             if(var->visible->s1[i][0]=='$' && !var->visible->s1[i][1])
                printf("Visible in %s\n",var->visible->s2[i]);
             else
                printf("Visible in %s : %s\n",var->visible->s1[i],var->visible->s2[i]);

          for(i=0;i<var->used->n;i++)
             if(var->used->s1[i][0]=='$' && !var->used->s1[i][1])
                printf("Used in %s\n",var->used->s2[i]);
             else
                printf("Used in %s : %s\n",var->used->s1[i],var->used->s2[i]);
         }
      }

    if(option_warn&WARN_XREF)
      {
       if(var->scope&(EXTERNAL|EXTERN_F) && !var->defined)
          printf("Warning %16s : Variable %s has an unknown global definition.\n",filename,var->name);

       if(var->scope&(GLOBAL|LOCAL|EXTERNAL|EXTERN_F) && !var->used->n)
          printf("Warning %16s : Variable %s is not used anywhere.\n",filename,var->name);

       if(var->scope&(GLOBAL|EXTERNAL|EXTERN_F) && var->used->n)
         {
          int is_used_elsewhere=0,is_used_here=0;
          for(i=0;i<var->used->n;i++)
             if(!strcmp(filename,var->used->s2[i]))
                is_used_here=1;
             else
                is_used_elsewhere=1;
          if(!is_used_elsewhere)
             printf("Warning %16s : Variable %s is %s but only used in this file.\n",filename,var->name,var->scope&GLOBAL?"global":"extern");
          if(!is_used_here)
             printf("Warning %16s : Variable %s is %s but not used in this file.\n",filename,var->name,var->scope&GLOBAL?"global":"extern");
         }
      }
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Write a Function structure out.

  Function func The Function structure to output.
  ++++++++++++++++++++++++++++++++++++++*/

static void WriteWarnRawFunction(Function func)
{
 int i;

 if(option_raw)
   {
    int done=0;

    printf("\nFUNCTION : %s [",func->name);
    if(func->scope&LOCAL)    done=printf("Local");
    if(func->scope&GLOBAL)   done=printf("Global");
    if(func->scope&EXTERNAL) done=printf("External");
    if(func->scope&INLINED)       printf("%sInline",done?" and ":"");
    printf("]\n");

    if(func->comment)
       printf("<<<\n%s\n>>>\n",func->comment);
   }

 if(option_warn&WARN_COMMENT && !func->comment)
    printf("Warning %16s : Function %s() does not have a comment.\n",filename,func->name);

 if(option_raw)
    printf("Defined: %s:%d\n",filename,func->lineno);

 if(option_xref&XREF_FUNC)
   {
    if(func->protofile && option_raw)
       printf("Prototyped in %s\n",func->protofile);
    if(option_warn&WARN_XREF && !func->protofile)
       printf("Warning %16s : Function %s() is not prototyped.\n",filename,func->name);
   }

 if(option_raw)
   {
    if(func->cret)
       printf("Type: %s <<<%s>>>\n",func->type,func->cret);
    else
       printf("Type: %s\n",func->type);
   }
 if(option_warn&WARN_COMMENT && !func->cret && strncmp("void ",func->type,5))
    printf("Warning %16s : Function %s() has a return value with no comment.\n",filename,func->name);

 for(i=0;i<func->args->n;i++)
   {
    if(option_raw)
      {
       if(func->args->s2[i])
          printf("Arguments: %s <<<%s>>>\n",func->args->s1[i],func->args->s2[i]);
       else
          printf("Arguments: %s\n",func->args->s1[i]);
      }
    if(option_warn&WARN_COMMENT && !func->args->s2[i] && strcmp("void",func->args->s1[i]))
       printf("Warning %16s : Function %s() has an argument %s with no comment.\n",filename,func->name,func->args->s1[i]);
   }

 if(option_raw && func->incfrom)
    printf("Included from: %s\n",func->incfrom);

 if(option_xref&XREF_FUNC)
   {
    for(i=0;i<func->calls->n;i++)
      {
       if(option_raw)
         {
          if(func->calls->s2[i])
             printf("Calls %s : %s\n",func->calls->s1[i],func->calls->s2[i]);
          else
             printf("Calls %s\n",func->calls->s1[i]);
         }
#if 0 /* Too verbose */
       if(option_warn&WARN_XREF && !func->calls->s2[i])
          printf("Warning %16s : Function %s() calls function %s() whose definition is unknown.\n",filename,func->name,func->calls->s1[i]);
#endif
      }

    if(option_raw)
       for(i=0;i<func->called->n;i++)
          printf("Called from %s : %s\n",func->called->s1[i],func->called->s2[i]);

    if(option_raw)
       for(i=0;i<func->used->n;i++)
         {
          if(func->used->s1[i][0]=='$' && !func->used->s1[i][1])
             printf("Used in %s\n",func->used->s2[i]);
          else
             printf("Used in %s : %s\n",func->used->s1[i],func->used->s2[i]);
         }

    for(i=0;i<func->f_refs->n;i++)
      {
       if(option_raw)
         {
          if(func->f_refs->s2[i])
             printf("References Function %s : %s\n",func->f_refs->s1[i],func->f_refs->s2[i]);
          else
             printf("References Function %s\n",func->f_refs->s1[i]);
         }
       if(option_warn&WARN_XREF && !func->f_refs->s2[i])
          printf("Warning %16s : Function %s() references function %s() whose definition is unknown.\n",filename,func->name,func->f_refs->s1[i]);
      }
   }

 if(option_xref&XREF_VAR)
    for(i=0;i<func->v_refs->n;i++)
      {
       if(option_raw)
         {
          if(func->v_refs->s2[i])
             printf("References Variable %s : %s\n",func->v_refs->s1[i],func->v_refs->s2[i]);
          else
             printf("References Variable %s\n",func->v_refs->s1[i]);
         }
       if(option_warn&WARN_XREF && !func->v_refs->s2[i])
          printf("Warning %16s : Function %s() references variable %s whose definition is unknown.\n",filename,func->name,func->v_refs->s1[i]);
      }


 if(option_warn&WARN_XREF)
   {
    if(!func->used->n && !func->called->n)
       printf("Warning %16s : Function %s() is not used anywhere.\n",filename,func->name);

    if(func->scope&GLOBAL && (func->called->n || func->used->n))
      {
       int is_used_elsewhere=0;
       for(i=0;i<func->called->n;i++)
          if(strcmp(func->called->s2[i],filename))
            {is_used_elsewhere=1;break;}
       for(i=0;i<func->used->n;i++)
          if(strcmp(func->used->s2[i],filename))
            {is_used_elsewhere=1;break;}
       if(!is_used_elsewhere)
          printf("Warning %16s : Function %s() is global but is only used in this file.\n",filename,func->name);
      }
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Write out a raw version of the appendix.

  StringList files The list of files to write.

  StringList2 funcs The list of functions to write.

  StringList2 vars The list of variables to write.

  StringList2 types The list of types to write.
  ++++++++++++++++++++++++++++++++++++++*/

void WriteWarnRawAppendix(StringList files,StringList2 funcs,StringList2 vars,StringList2 types)
{
 int i;

 /* Write out the appendix of files. */

 if(option_index&INDEX_FILE)
   {
    if(files->n)
      {
       printf("\nAppendix - Files\n\n");
       for(i=0;i<files->n;i++)
          printf("%s\n",files->s[i]);
      }
    else
       if(option_warn&WARN_XREF)
          printf("Warning Index : No global files to index.\n");
   }

 /* Write out the appendix of functions. */

 if(option_index&INDEX_FUNC)
   {
    if(funcs->n)
      {
       printf("\nAppendix - Global Functions\n\n");
       for(i=0;i<funcs->n;i++)
          printf("%s : %s\n",funcs->s1[i],funcs->s2[i]);
      }
    else
       if(option_warn&WARN_XREF)
          printf("Warning Index : No global functions to index.\n");
   }

 /* Write out the appendix of variables. */

 if(option_index&INDEX_VAR)
   {
    if(vars->n)
      {
       printf("\nAppendix - Global Variables\n\n");
       for(i=0;i<vars->n;i++)
          printf("%s : %s\n",vars->s1[i],vars->s2[i]);
      }
    else
       if(option_warn&WARN_XREF)
          printf("Warning Index : No global variables to index.\n");
   }

 /* Write out the appendix of types. */

 if(option_index&INDEX_TYPE)
   {
    if(types->n)
      {
       printf("\nAppendix - Defined Types\n\n");
       for(i=0;i<types->n;i++)
          printf("%s : %s\n",types->s1[i],types->s2[i]);
      }
    else
       if(option_warn&WARN_XREF)
          printf("Warning Index : No types to index.\n");
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Decide if to copy or skip the next line.

  int CopyOrSkip Returns the number of characters to skip.

  char *string The string that starts the next line.

  char *type The type of file we are outputing.

  int *copy Returns true if we are to copy the line verbatim.

  int *skip Returns true if we are to skip the line.
  ++++++++++++++++++++++++++++++++++++++*/

int CopyOrSkip(char *string,char *type,int *copy,int *skip)
{
 char *p=string;
 int s=0;

 if(*p=='\n')
    p++;
 while(*p==' ' || *p=='\t')
    p++;

 *copy=*skip=0;

 switch(*type)
    {
    case 'h': /* html */
     if(!strncmp(p,"+html+",s=6) || !strncmp(p,"-latex-",s=7) || !strncmp(p,"-sgml-",s=6) || !strncmp(p,"-rtf-",s=5))
        *copy=1;
     if(!strncmp(p,"-html-",s=6) || !strncmp(p,"+latex+",s=7) || !strncmp(p,"+sgml+",s=6) || !strncmp(p,"+rtf+",s=5) || !strncmp(p,"+none+",s=6))
        *skip=1;
     break;
    case 'l': /* latex */
     if(!strncmp(p,"-html-",s=6) || !strncmp(p,"+latex+",s=7) || !strncmp(p,"-sgml-",s=6) || !strncmp(p,"-rtf-",s=5))
        *copy=1;
     if(!strncmp(p,"+html+",s=6) || !strncmp(p,"-latex-",s=7) || !strncmp(p,"+sgml+",s=6) || !strncmp(p,"+rtf+",s=5) || !strncmp(p,"+none+",s=6))
        *skip=1;
     break;
    case 's': /* sgml */
     if(!strncmp(p,"-html-",s=6) || !strncmp(p,"-latex-",s=7) || !strncmp(p,"+sgml+",s=6) || !strncmp(p,"-rtf-",s=5))
        *copy=1;
     if(!strncmp(p,"+html+",s=6) || !strncmp(p,"+latex+",s=7) || !strncmp(p,"-sgml-",s=6) || !strncmp(p,"+rtf+",s=5) || !strncmp(p,"+none+",s=6))
        *skip=1;
     break;
    case 'r': /* rtf */
     if(!strncmp(p,"-html-",s=6) || !strncmp(p,"-latex-",s=7) || !strncmp(p,"-sgml-",s=6) || !strncmp(p,"+rtf+",s=5))
        *copy=1;
     if(!strncmp(p,"+html+",s=6) || !strncmp(p,"+latex+",s=7) || !strncmp(p,"+sgml+",s=6) || !strncmp(p,"-rtf-",s=5) || !strncmp(p,"+none+",s=6))
        *skip=1;
     break;
    }

 if(*copy)
    return(p-string+s);
 else
    return(0);
}