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