]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/gettok.c
7d3e5b488cf041c31fbe3f27fe194c55fc5919bc
[netatalk.git] / etc / afpd / gettok.c
1 /*
2  * $Id: gettok.c,v 1.3 2001-06-20 18:33:04 rufustfirefly 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 <sys/param.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <pwd.h>
16
17 #include "globals.h"
18
19 static char     *l_curr;
20 static char     *l_end;
21
22 void initline( len, line )
23     int         len;
24     char        *line;
25 {
26     l_curr = line;
27     l_end = line + len;
28 }
29
30 #define ST_QUOTE        0
31 #define ST_WORD         1
32 #define ST_BEGIN        2
33
34     int
35 parseline( len, token )
36     int         len;
37     char        *token;
38 {
39     char        *p, *e;
40     int         state;
41
42     state = ST_BEGIN;
43     p = token;
44     e = token + len;
45
46     for (;;) {
47         if ( l_curr > l_end ) {                 /* end of line */
48             *token = '\0';
49             return( -1 );
50         }
51
52         switch ( *l_curr ) {
53         case '"' :
54             if ( state == ST_QUOTE ) {
55                 state = ST_WORD;
56             } else {
57                 state = ST_QUOTE;
58             }
59             break;
60
61         case '\0' :
62         case '\t' :
63         case '\n' :
64         case ' ' :
65             if ( state == ST_WORD ) {
66                 *p = '\0';
67                 return( p - token );
68             }
69             if ( state != ST_QUOTE ) {
70                 break;
71             }
72             /* FALL THROUGH */
73
74         default :
75             if ( state == ST_BEGIN ) {
76                 state = ST_WORD;
77             }
78             if ( p > e ) {                      /* end of token */
79                 *token = '\0';
80                 return( -1 );
81             }
82             *p++ = *l_curr;
83             break;
84         }
85
86         l_curr++;
87     }
88 }
89
90 #ifdef notdef
91 void parseline( token, user )
92     char        *token, *user;
93 {
94     char                *p = pos, *t = token, *u, *q, buf[ MAXPATHLEN ];
95     struct passwd       *pwent;
96     int                 quoted = 0;
97
98     while ( isspace( *p )) {
99         p++;
100     }
101
102     /*
103      * If we've reached the end of the line, or a comment,
104      * don't return any more tokens.
105      */
106     if ( *p == '\0' || *p == '#' ) {
107         *token = '\0';
108         return;
109     }
110
111     if ( *p == '"' ) {
112         p++;
113         quoted = 1;
114     }
115     while ( *p != '\0' && ( quoted || !isspace( *p ))) {
116         if ( *p == '"' ) {
117             if ( quoted ) {
118                 *t = '\0';
119                 break;
120             }
121             quoted = 1;
122             p++;
123         } else {
124             *t++ = *p++;
125         }
126     }
127     pos = p;
128     *t = '\0';
129
130     /*
131      * We got to the end of the line without closing an open quote
132      */
133     if ( *p == '\0' && quoted ) {
134         *token = '\0';
135         return;
136     }
137
138     t = token;
139     if ( *t == '~' ) {
140         t++;
141         if ( *t == '\0' || *t == '/' ) {
142             u = user;
143             if ( *t == '/' ) {
144                 t++;
145             }
146         } else {
147             u = t;
148             if (( q = strchr( t, '/' )) == NULL ) {
149                 t = "";
150             } else {
151                 *q = '\0';
152                 t = q + 1;
153             }
154         }
155         if ( u == NULL || ( pwent = getpwnam( u )) == NULL ) {
156             *token = '\0';
157             return;
158         }
159         strcpy( buf, pwent->pw_dir );
160         if ( *t != '\0' ) {
161             strcat( buf, "/" );
162             strcat( buf, t );
163         }
164         strcpy( token, buf );
165     }
166     return;
167 }
168 #endif /* notdef */