c96ecec3a6fddd643ee9124b117834f5be98a135
[openafs.git] / src / uss / lex.l
1 %{
2 /*
3  * Copyright 2000, International Business Machines Corporation and others.
4  * All Rights Reserved.
5  * 
6  * This software has been released under the terms of the IBM Public
7  * License.  For details, see the LICENSE file in the top-level source
8  * directory or online at http://www.openafs.org/dl/license10.html
9  */
10
11 #include <afsconfig.h>
12 #include <afs/param.h>
13
14 RCSID("$Header$");
15
16
17 #include <string.h>
18
19 #include "y.tab.h"
20 #include "uss_common.h"
21 int line=1;
22 #ifdef DEBUG
23 #define dprint(x)       {fprintf(stderr, x); fflush(stderr);}
24 #else
25 #define dprint(x)
26 #endif
27 %}
28
29 /* definitions */
30 C       #[^\n]*
31 W       [ \t]+
32 L       [A-Za-z]
33 S       [\.A-Z0-9a-z/$][^ \t\n#=\^\!\|\(\)\{\};]*
34 Q       \"[^\"\n]*[\"\n]
35 INVAL   [^ADEFLSVGX# ]
36 EOL     [\n]
37
38 %%
39 {C}             {dprint(("got a comment\n"));}
40 ^{EOL}          {dprint(("got an empty line\n")); line++;}
41 ^{INVAL}        {uss_procs_PrintErr(line," Invalid command \n");}
42 ^[D]{W}         {dprint(("got a Dir\n"));return(DIR_TKN);}
43 ^[F]{W}         {dprint(("got a File\n"));return(FILE_TKN);}
44 ^[L]{W}         {dprint(("got a Link\n"));return(LINK_TKN);}
45 ^[S]{W}         {dprint(("got a Symlink\n"));return(SYMLINK_TKN);}
46 ^[E]{W}         {dprint(("got an Echo\n"));return(ECHO_TKN);}
47 ^[X]{W}         {dprint(("got an Exec\n"));return(EXEC_TKN);}
48 ^[V]{W}         {dprint(("got a Vol\n"));return(VOL_TKN);}
49 ^[G]{W}         {dprint(("got a Group Declaration\n"));return(GROUP_TKN);}
50 ^[A]{W}         {dprint(("got an Auth\n"));return(AUTH_TKN);}
51 ^[Y]{W}         {dprint(("got a Vol1\n"));return(VOL1_TKN);}
52 {S}             {dprint(("got a string(%s)\n", yytext));
53                  Replace(yytext, yylval.strval);
54                  return(STRING_TKN);
55                 }
56 {Q}             {dprint(("got a quote: '%s'\n", yytext));
57                  Replace(yytext, yylval.strval);
58                  return(STRING_TKN);
59                 }
60 {EOL}           {line++;
61                  return(EOL_TKN);};
62
63 %%
64
65 /*
66  * This routine copies the in buf to out and replaces every known
67  * variable, e.g. $user, $1, ... by its value.  This value either
68  * comes from main program, or the handling routine will figure it
69  * out.  If given a quoted string, it ignores the first double quote
70  * and replaces the second with a null.
71  */
72
73 Replace(in, out)
74     char *in, *out;
75
76 { /*Replace*/
77
78     char *in_text, *in_var, *out_cp, VarNo;
79     int n;
80     int isQuotedString;
81     char *nullP;
82     
83     if(in[0] == '"') {
84         /*
85          * Strip the opening quote, remember we're handling a
86          * quoted string
87          */
88         in_text = in+1;
89         isQuotedString = 1;
90     }
91     else {
92         in_text = in;
93         isQuotedString = 0;
94     }
95     out_cp = out;
96     
97     while ((in_var = strchr(in_text, '$')) != NULL) {
98         while(in_text < in_var)
99             *out_cp++ = *in_text++;
100         VarNo = *(in_var+1);
101         if(VarNo >= '0' && VarNo <= '9') {
102             /*In the 0-9 range*/
103             n = VarNo - '0';
104             if (n == 0) {
105                 fprintf(stderr,
106                         "$0 is the program name.  Please start from $1.\n");
107                 exit(-1);
108             }
109             if (n > uss_VarMax){
110                 fprintf(stderr,
111                         "Illegal variable number ($%d is the largest acceptable)\n",
112                         uss_VarMax);
113                 exit(-1);
114             }
115             
116             strcpy(out_cp, uss_Var[n]);
117             out_cp += strlen(uss_Var[n]);
118             in_text += 2;
119         }
120         
121         else if (strncmp(in_var, "$USER", 5) == 0) {
122             strcpy(out_cp, uss_User);
123             out_cp += strlen(uss_User);
124             in_text += 5;
125         }
126         
127         else if (strncmp(in_var, "$UID", 4) == 0) {
128             strcpy(out_cp, uss_Uid);
129             out_cp += strlen(uss_Uid);
130             in_text += 4;
131         }
132         
133         else if (strncmp(in_var, "$SERVER", 7) == 0) {
134             strcpy(out_cp, uss_Server);
135             out_cp += strlen(uss_Server);
136             in_text += 7;
137         }
138         
139         else if (strncmp(in_var, "$PART", 5) == 0) {
140             strcpy(out_cp, uss_Partition);
141             out_cp += strlen(uss_Partition);
142             in_text += 5;
143         }
144         
145         else if (strncmp(in_var, "$MTPT", 5) == 0) {
146             strcpy(out_cp, uss_MountPoint);
147             out_cp += strlen(uss_MountPoint);
148             in_text += 5;
149         }
150         
151         else if (strncmp(in_var, "$NAME", 5) == 0) {
152             strcpy(out_cp, uss_RealName);
153             out_cp += strlen(uss_RealName);
154             in_text += 5;
155         }
156         
157         else if (strncmp(in_var, "$AUTO", 5) == 0) {
158             /*Picks a dir with minimum entries*/
159             uss_procs_PickADir(out, out_cp /*, uss_Auto*/);
160             printf("debug: $AUTO = %s\n", uss_Auto);
161             strcpy(out_cp, uss_Auto);
162             out_cp += strlen(uss_Auto);
163             in_text += 5;
164         }
165         else if (strncmp(in_var, "$PWEXPIRES", 10) == 0) {
166             sprintf(out_cp, " %d ", uss_Expires);
167             out_cp += strlen(out_cp);
168             in_text += 10;
169         }
170         
171         else{
172             /*Unknown variable*/
173             fprintf(stderr,
174                     "Warning: unknown variable in config file: '%s'\n",
175                     in_var);
176             *out_cp++ = *in_text++;
177         }
178     }
179     
180     /*
181      * At this point, we've copied over the in buffer up to the point
182      * of the last variable instance, so copy over the rest. If this
183      * is a quoted string, we place the terminating null where the
184      * ending double quote is.
185      */
186     while(*in_text != '\0')
187         *out_cp++ = *in_text++;
188     
189     if (isQuotedString) {
190         nullP = strchr(out, '"');
191         if (nullP == NULL)
192             nullP = out_cp;
193     }
194     else
195         nullP = out_cp;
196     *nullP = '\0';
197
198 } /*Replace*/
199
200 yywrap()
201 {
202 return(1);
203 }