1/*
2 * $Id: comment.c,v 1.10 2009-10-13 22:55:37 didg Exp $
3 *
4 * Copyright (c) 1990,1994 Regents of The University of Michigan.
5 * All Rights Reserved.  See COPYRIGHT.
6 */
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif /* HAVE_CONFIG_H */
11
12#include <atalk/logger.h>
13#include <sys/param.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <errno.h>
18
19#include "comment.h"
20
21struct comstate	*comstate;
22
23char	*comcont = "%%+";
24
25void compop( void )
26{
27    struct comstate	*cs;
28
29    cs = comstate;
30    comstate = cs->cs_prev;
31    free( cs );
32}
33
34void compush(struct papd_comment *comment)
35{
36    struct comstate	*cs;
37
38    if (( cs = (struct comstate *)malloc( sizeof( struct comstate ))) ==
39	    NULL ) {
40	LOG(log_error, logtype_papd, "malloc: %s", strerror(errno) );
41	exit( 1 );
42    }
43
44    cs->cs_comment = comment;
45    cs->cs_prev = comstate;
46    cs->cs_flags = 0;
47    comstate = cs;
48}
49
50int comswitch(struct papd_comment *comments, int (*handler)())
51{
52    struct papd_comment	*c, *comment = NULL;
53
54    for ( c = comments; c->c_begin; c++ ) {
55	if ( c->c_handler == handler ) {
56	    comment = c;
57	}
58    }
59    if ( comment == NULL || comment->c_handler != handler ) {
60	LOG(log_error, logtype_papd, "comswitch: can't find handler!" );
61	return( -1 );
62    }
63    compop();
64    compush( comment );
65    return( 0 );
66}
67
68int comcmp( char *start, char *stop, char *str,int how)
69{
70    int		cc, len;
71
72    len = stop - start;
73    cc = strlen( str );
74    if ( how & C_FULL ) {
75	if ( (cc == len) && (strncmp( str, start, cc ) == 0) ) {
76	    return( 0 );
77	}
78    } else {
79	if ( (cc <= len) && (strncmp( str, start, cc ) == 0) ) {
80	    return( 0 );
81	}
82    }
83
84    return( 1 );
85}
86
87struct papd_comment *commatch( char *start, char *stop, struct papd_comment comments[])
88{
89    struct papd_comment	*comment;
90
91    for ( comment = comments; comment->c_begin; comment++ ) {
92	if ( comcmp( start, stop, comment->c_begin, comment->c_flags ) == 0 ) {
93	    break;
94	}
95    }
96    if ( comment->c_begin ) {
97	return( comment );
98    } else {
99	return( NULL );
100    }
101}
102
103char *comtoken( char *start, char *stop, char *pos, char *delim)
104{
105    if ( pos < start || pos > stop ) {
106	abort();
107    }
108
109    for ( ; pos < stop; pos++ ) {
110	if ( index( delim, *pos )) {
111	    break;
112	}
113    }
114    if ( ++pos < stop ) {
115	return( pos );
116    } else {
117	return( NULL );
118    }
119}
120