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