pull from samba
[tridge/junkcode.git] / mangle.c
1 #include <stdio.h>
2
3 typedef int BOOL;
4 typedef char pstring[1024];
5 #define True 1
6 #define False 0
7 #define DEBUG(l,x)
8
9 /****************************************************************************
10 line strncpy but always null terminates. Make sure there is room!
11 ****************************************************************************/
12 char *StrnCpy(char *dest,char *src,int n)
13 {
14   char *d = dest;
15   while (n-- && (*d++ = *src++)) ;
16   *d = 0;
17   return(dest);
18 }
19
20 /*******************************************************************
21   convert a string to upper case
22 ********************************************************************/
23 void strupper(char *s)
24 {
25   while (*s)
26     {
27 #ifdef KANJI
28         if (is_shift_jis (*s)) {
29             s += 2;
30         } else if (is_kana (*s)) {
31             s++;
32         } else {
33             if (islower(*s))
34                 *s = toupper(*s);
35             s++;
36         }
37 #else
38       if (islower(*s))
39         *s = toupper(*s);
40       s++;
41 #endif
42     }
43 }
44
45
46 static char *map_filename(char *s, /* This is null terminated */
47                           char *pattern, /* This isn't. */
48                           int len) /* This is the length of pattern. */
49 {
50   static pstring matching_bit;  /* The bit of the string which matches */
51                                 /* a * in pattern if indeed there is a * */
52   char *sp;                     /* Pointer into s. */
53   char *pp;                     /* Pointer into p. */
54   char *match_start;            /* Where the matching bit starts. */
55   pstring pat;
56
57   StrnCpy(pat, pattern, len);   /* Get pattern into a proper string! */
58   strcpy(matching_bit,"");      /* Match but no star gets this. */
59   pp = pat;                     /* Initialise the pointers. */
60   sp = s;
61   if ((len == 1) && (*pattern == '*')) {
62     return NULL;                /* Impossible, too ambiguous for */
63                                 /* words! */
64   }
65
66   while ((*sp)                  /* Not the end of the string. */
67          && (*pp)               /* Not the end of the pattern. */
68          && (*sp == *pp)        /* The two match. */
69          && (*pp != '*')) {     /* No wildcard. */
70     sp++;                       /* Keep looking. */
71     pp++;
72   }
73   if (!*sp && !*pp)             /* End of pattern. */
74     return matching_bit;        /* Simple match.  Return empty string. */
75   if (*pp == '*') {
76     pp++;                       /* Always interrested in the chacter */
77                                 /* after the '*' */
78     if (!*pp) {                 /* It is at the end of the pattern. */
79       StrnCpy(matching_bit, s, sp-s);
80       return matching_bit;
81     } else {
82       /* The next character in pattern must match a character further */
83       /* along s than sp so look for that character. */
84       match_start = sp;
85       while ((*sp)              /* Not the end of s. */
86              && (*sp != *pp))   /* Not the same  */
87         sp++;                   /* Keep looking. */
88       if (!*sp) {               /* Got to the end without a match. */
89         return NULL;
90       } else {                  /* Still hope for a match. */
91         /* Now sp should point to a matching character. */
92         StrnCpy(matching_bit, match_start, sp-match_start);
93         /* Back to needing a stright match again. */
94         while ((*sp)            /* Not the end of the string. */
95                && (*pp)         /* Not the end of the pattern. */
96                && (*sp == *pp)) { /* The two match. */
97           sp++;                 /* Keep looking. */
98           pp++;
99         }
100         if (!*sp && !*pp)       /* Both at end so it matched */
101           return matching_bit;
102         else
103           return NULL;
104       }
105     }
106   }
107   return NULL;                  /* No match. */
108 }
109
110
111 static void do_fwd_mangled_map(char *s, char *MangledMap)
112 {
113   /* MangledMap is a series of name pairs in () separated by spaces.
114    * If s matches the first of the pair then the name given is the
115    * second of the pair.  A * means any number of any character and if
116    * present in the second of the pair as well as the first the
117    * matching part of the first string takes the place of the * in the
118    * second.
119    *
120    * I wanted this so that we could have RCS files which can be used
121    * by UNIX and DOS programs.  My mapping string is (RCS rcs) which
122    * converts the UNIX RCS file subdirectory to lowercase thus
123    * preventing mangling.
124    */
125   char *start=MangledMap;       /* Use this to search for mappings. */
126   char *end;                    /* Used to find the end of strings. */
127   char *match_string;
128   pstring new_string;           /* Make up the result here. */
129   char *np;                     /* Points into new_string. */
130
131   DEBUG(5,("Mangled Mapping '%s' map '%s'\n", s, MangledMap));
132   while (*start) {
133     while ((*start) && (*start != '('))
134       start++;
135     start++;                    /* Skip the ( */
136     if (!*start)
137       continue;                 /* Always check for the end. */
138     end = start;                /* Search for the ' ' or a ')' */
139     DEBUG(5,("Start of first in pair '%s'\n", start));
140     while ((*end) && !((*end == ' ') || (*end == ')')))
141       end++;
142     if (!*end) {
143       start = end;
144       continue;                 /* Always check for the end. */
145     }
146     DEBUG(5,("End of first in pair '%s'\n", end));
147     if ((match_string = map_filename(s, start, end-start))) {
148       DEBUG(5,("Found a match\n"));
149       /* Found a match. */
150       start = end+1;            /* Point to start of what it is to become. */
151       DEBUG(5,("Start of second in pair '%s'\n", start));
152       end = start;
153       np = new_string;
154       while ((*end)             /* Not the end of string. */
155              && (*end != ')')   /* Not the end of the pattern. */
156              && (*end != '*'))  /* Not a wildcard. */
157         *np++ = *end++;
158       if (!*end) {
159         start = end;
160         continue;               /* Always check for the end. */
161       }
162       if (*end == '*') {
163         strcpy(np, match_string);
164         np += strlen(match_string);
165         end++;                  /* Skip the '*' */
166         while ((*end)             /* Not the end of string. */
167                && (*end != ')')   /* Not the end of the pattern. */
168                && (*end != '*'))  /* Not a wildcard. */
169           *np++ = *end++;
170       }
171       if (!*end) {
172         start = end;
173         continue;               /* Always check for the end. */
174       }
175       *np++ = '\0';             /* NULL terminate it. */
176       DEBUG(5,("End of second in pair '%s'\n", end));
177       strcpy(s, new_string);    /* Substitute with the new name. */
178       DEBUG(5,("s is now '%s'\n", s));
179     }
180     start = end;              /* Skip a bit which cannot be wanted */
181     /* anymore. */
182     start++;
183   }
184 }
185
186
187 main(int argc,char *argv[])
188 {
189   char name[100];
190
191   strcpy(name,argv[2]);
192   
193   do_fwd_mangled_map(name,argv[1]);
194   printf("name=%s\n",name);
195 }