cmd.exe: Add support for env vars DATE, TIME, CD.
[wine] / programs / cmd / batch.c
1 /*
2  * CMD - Wine-compatible command line interface - batch interface.
3  *
4  * Copyright (C) 1999 D A Pickles
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "wcmd.h"
22
23 void WCMD_batch_command (char *line);
24 void WCMD_HandleTildaModifiers(char **start, char *forVariable);
25
26 extern int echo_mode;
27 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
28 extern BATCH_CONTEXT *context;
29 extern DWORD errorlevel;
30
31 /* msdn specified max for Win XP */
32 #define MAXSTRING 8192
33
34 /****************************************************************************
35  * WCMD_batch
36  *
37  * Open and execute a batch file.
38  * On entry *command includes the complete command line beginning with the name
39  * of the batch file (if a CALL command was entered the CALL has been removed).
40  * *file is the name of the file, which might not exist and may not have the
41  * .BAT suffix on. Called is 1 for a CALL, 0 otherwise.
42  *
43  * We need to handle recursion correctly, since one batch program might call another.
44  * So parameters for this batch file are held in a BATCH_CONTEXT structure.
45  */
46
47 void WCMD_batch (char *file, char *command, int called) {
48
49 #define WCMD_BATCH_EXT_SIZE 5
50
51 HANDLE h = INVALID_HANDLE_VALUE;
52 char string[MAXSTRING];
53 char extension_batch[][WCMD_BATCH_EXT_SIZE] = {".bat",".cmd"};
54 char extension_exe[WCMD_BATCH_EXT_SIZE] = ".exe";
55 unsigned int  i;
56 BATCH_CONTEXT *prev_context;
57
58   for(i=0; (i<(sizeof(extension_batch)/WCMD_BATCH_EXT_SIZE)) && 
59            (h == INVALID_HANDLE_VALUE); i++) {
60   strcpy (string, file);
61   CharLower (string);
62     if (strstr (string, extension_batch[i]) == NULL) strcat (string, extension_batch[i]);
63   h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ,
64                   NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
65   }
66   if (h == INVALID_HANDLE_VALUE) {
67     strcpy (string, file);
68     CharLower (string);
69     if (strstr (string, extension_exe) == NULL) strcat (string, extension_exe);
70     h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ,
71                     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
72     if (h != INVALID_HANDLE_VALUE) {
73       WCMD_run_program (command, 0);
74     } else {
75       SetLastError (ERROR_FILE_NOT_FOUND);
76       WCMD_print_error ();
77     }
78     return;
79   }
80
81 /*
82  *      Create a context structure for this batch file.
83  */
84
85   prev_context = context;
86   context = (BATCH_CONTEXT *)LocalAlloc (LMEM_FIXED, sizeof (BATCH_CONTEXT));
87   context -> h = h;
88   context -> command = command;
89   context -> shift_count = 0;
90   context -> prev_context = prev_context;
91   context -> skip_rest = FALSE;
92
93 /*
94  *      Work through the file line by line. Specific batch commands are processed here,
95  *      the rest are handled by the main command processor.
96  */
97
98   while (context -> skip_rest == FALSE && WCMD_fgets (string, sizeof(string), h)) {
99       if (strlen(string) == MAXSTRING -1) {
100           WCMD_output_asis( "Line in Batch processing possibly truncated. Using:\n");
101           WCMD_output_asis( string);
102           WCMD_output_asis( "\n");
103       }
104       if (string[0] != ':') {                      /* Skip over labels */
105           WCMD_batch_command (string);
106       }
107   }
108   CloseHandle (h);
109
110 /*
111  *      If invoked by a CALL, we return to the context of our caller. Otherwise return
112  *      to the caller's caller.
113  */
114
115   LocalFree ((HANDLE)context);
116   if ((prev_context != NULL) && (!called)) {
117     CloseHandle (prev_context -> h);
118     context = prev_context -> prev_context;
119     LocalFree ((HANDLE)prev_context);
120   }
121   else {
122     context = prev_context;
123   }
124 }
125
126 /****************************************************************************
127  * WCMD_batch_command
128  *
129  * Execute one line from a batch file, expanding parameters.
130  */
131
132 void WCMD_batch_command (char *line) {
133
134 DWORD status;
135 char cmd1[MAXSTRING],cmd2[MAXSTRING];
136 char *p, *s, *t;
137 int i;
138
139   /* Get working version of command line */
140   strcpy(cmd1, line);
141
142   /* Expand environment variables in a batch file %{0-9} first  */
143   /*   Then env vars, and if any left (ie use of undefined vars,*/
144   /*   replace with spaces                                      */
145   /* FIXME: Winnt would replace %1%fred%1 with first parm, then */
146   /*   contents of fred, then the digit 1. Would need to remove */
147   /*   ExpandEnvStrings to achieve this                         */
148
149   /* Replace use of %0...%9 and errorlevel*/
150   p = cmd1;
151   while ((p = strchr(p, '%'))) {
152     i = *(p+1) - '0';
153     if (*(p+1) == '~') {
154       WCMD_HandleTildaModifiers(&p, NULL);
155       p++;
156     } else if ((i >= 0) && (i <= 9)) {
157       s = strdup (p+2);
158       t = WCMD_parameter (context -> command, i + context -> shift_count, NULL);
159       strcpy (p, t);
160       strcat (p, s);
161       free (s);
162     } else if (*(p+1)=='*') {
163       char *startOfParms = NULL;
164       s = strdup (p+2);
165       t = WCMD_parameter (context -> command, 1, &startOfParms);
166       if (startOfParms != NULL) strcpy (p, startOfParms);
167       else *p = 0x00;
168       strcat (p, s);
169       free (s);
170
171     /* Handle DATE, TIME, ERRORLEVEL and CD replacements allowing */
172     /* override if existing env var called that name              */
173     } else if ((CompareString (LOCALE_USER_DEFAULT,
174                               NORM_IGNORECASE | SORT_STRINGSORT,
175                               (p+1), 11, "ERRORLEVEL%", -1) == 2) &&
176               (GetEnvironmentVariable("ERRORLEVEL", cmd2, 1) == 0) &&
177               (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
178       char output[10];
179       sprintf(output, "%d", errorlevel);
180       s = strdup (p+12);
181       strcpy (p, output);
182       strcat (p, s);
183
184     } else if ((CompareString (LOCALE_USER_DEFAULT,
185                               NORM_IGNORECASE | SORT_STRINGSORT,
186                               (p+1), 5, "DATE%", -1) == 2) &&
187               (GetEnvironmentVariable("DATE", cmd2, 1) == 0) &&
188               (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
189
190       GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL,
191                     NULL, cmd2, MAXSTRING);
192       s = strdup (p+6);
193       strcpy (p, cmd2);
194       strcat (p, s);
195
196     } else if ((CompareString (LOCALE_USER_DEFAULT,
197                               NORM_IGNORECASE | SORT_STRINGSORT,
198                               (p+1), 5, "TIME%", -1) == 2) &&
199               (GetEnvironmentVariable("TIME", cmd2, 1) == 0) &&
200               (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
201       GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, NULL,
202                         NULL, cmd2, MAXSTRING);
203       s = strdup (p+6);
204       strcpy (p, cmd2);
205       strcat (p, s);
206
207     } else if ((CompareString (LOCALE_USER_DEFAULT,
208                               NORM_IGNORECASE | SORT_STRINGSORT,
209                               (p+1), 3, "CD%", -1) == 2) &&
210               (GetEnvironmentVariable("CD", cmd2, 1) == 0) &&
211               (GetLastError() == ERROR_ENVVAR_NOT_FOUND)) {
212       GetCurrentDirectory (MAXSTRING, cmd2);
213       s = strdup (p+4);
214       strcpy (p, cmd2);
215       strcat (p, s);
216
217     } else {
218       p++;
219     }
220   }
221
222   /* Now replace environment variables */
223   status = ExpandEnvironmentStrings(cmd1, cmd2, sizeof(cmd2));
224   if (!status) {
225     WCMD_print_error ();
226     return;
227   }
228
229   /* In a batch program, unknown variables are replace by nothing */
230   /* so remove any remaining %var%                                */
231   p = cmd2;
232   while ((p = strchr(p, '%'))) {
233     s = strchr(p+1, '%');
234     if (!s) {
235       *p=0x00;
236     } else {
237       t = strdup(s+1);
238       strcpy(p, t);
239       free(t);
240     }
241   }
242
243   /* Show prompt before batch line IF echo is on */
244   if (echo_mode && (line[0] != '@')) {
245     WCMD_show_prompt();
246     WCMD_output_asis ( cmd2);
247     WCMD_output_asis ( "\n");
248   }
249
250   WCMD_process_command (cmd2);
251 }
252
253 /*******************************************************************
254  * WCMD_parameter - extract a parameter from a command line.
255  *
256  *      Returns the 'n'th space-delimited parameter on the command line (zero-based).
257  *      Parameter is in static storage overwritten on the next call.
258  *      Parameters in quotes (and brackets) are handled.
259  *      Also returns a pointer to the location of the parameter in the command line.
260  */
261
262 char *WCMD_parameter (char *s, int n, char **where) {
263
264 int i = 0;
265 static char param[MAX_PATH];
266 char *p;
267
268   p = param;
269   while (TRUE) {
270     switch (*s) {
271       case ' ':
272         s++;
273         break;
274       case '"':
275         if (where != NULL) *where = s;
276         s++;
277         while ((*s != '\0') && (*s != '"')) {
278           *p++ = *s++;
279         }
280         if (i == n) {
281           *p = '\0';
282           return param;
283         }
284         if (*s == '"') s++;
285           param[0] = '\0';
286           i++;
287         p = param;
288         break;
289       case '(':
290         if (where != NULL) *where = s;
291         s++;
292         while ((*s != '\0') && (*s != ')')) {
293           *p++ = *s++;
294         }
295         if (i == n) {
296           *p = '\0';
297           return param;
298         }
299         if (*s == ')') s++;
300           param[0] = '\0';
301           i++;
302         p = param;
303         break;
304       case '\0':
305         return param;
306       default:
307         /* Only return where if it is for the right parameter */
308         if (where != NULL && i==n) *where = s;
309         while ((*s != '\0') && (*s != ' ')) {
310           *p++ = *s++;
311         }
312         if (i == n) {
313           *p = '\0';
314           return param;
315         }
316           param[0] = '\0';
317           i++;
318         p = param;
319     }
320   }
321 }
322
323 /****************************************************************************
324  * WCMD_fgets
325  *
326  * Get one line from a batch file. We can't use the native f* functions because
327  * of the filename syntax differences between DOS and Unix. Also need to lose
328  * the LF (or CRLF) from the line.
329  */
330
331 char *WCMD_fgets (char *s, int n, HANDLE h) {
332
333 DWORD bytes;
334 BOOL status;
335 char *p;
336
337   p = s;
338   do {
339     status = ReadFile (h, s, 1, &bytes, NULL);
340     if ((status == 0) || ((bytes == 0) && (s == p))) return NULL;
341     if (*s == '\n') bytes = 0;
342     else if (*s != '\r') {
343       s++;
344       n--;
345     }
346     *s = '\0';
347   } while ((bytes == 1) && (n > 1));
348   return p;
349 }
350
351 /* _splitpath - copied from winefile as no obvious way to use it otherwise */
352 void _splitpath(const CHAR* path, CHAR* drv, CHAR* dir, CHAR* name, CHAR* ext)
353 {
354         const CHAR* end; /* end of processed string */
355         const CHAR* p;   /* search pointer */
356         const CHAR* s;   /* copy pointer */
357
358         /* extract drive name */
359         if (path[0] && path[1]==':') {
360                 if (drv) {
361                         *drv++ = *path++;
362                         *drv++ = *path++;
363                         *drv = '\0';
364                 }
365         } else if (drv)
366                 *drv = '\0';
367
368         /* search for end of string or stream separator */
369         for(end=path; *end && *end!=':'; )
370                 end++;
371
372         /* search for begin of file extension */
373         for(p=end; p>path && *--p!='\\' && *p!='/'; )
374                 if (*p == '.') {
375                         end = p;
376                         break;
377                 }
378
379         if (ext)
380                 for(s=end; (*ext=*s++); )
381                         ext++;
382
383         /* search for end of directory name */
384         for(p=end; p>path; )
385                 if (*--p=='\\' || *p=='/') {
386                         p++;
387                         break;
388                 }
389
390         if (name) {
391                 for(s=p; s<end; )
392                         *name++ = *s++;
393
394                 *name = '\0';
395         }
396
397         if (dir) {
398                 for(s=path; s<p; )
399                         *dir++ = *s++;
400
401                 *dir = '\0';
402         }
403 }
404
405 /****************************************************************************
406  * WCMD_HandleTildaModifiers
407  *
408  * Handle the ~ modifiers when expanding %0-9 or (%a-z in for command)
409  *    %~xxxxxV  (V=0-9 or A-Z)
410  * Where xxxx is any combination of:
411  *    ~ - Removes quotes
412  *    f - Fully qualified path (assumes current dir if not drive\dir)
413  *    d - drive letter
414  *    p - path
415  *    n - filename
416  *    x - file extension
417  *    s - path with shortnames
418  *    a - attributes
419  *    t - date/time
420  *    z - size
421  *    $ENVVAR: - Searches ENVVAR for (contents of V) and expands to fully
422  *                   qualified path
423  *
424  *  To work out the length of the modifier:
425  *
426  *  Note: In the case of %0-9 knowing the end of the modifier is easy,
427  *    but in a for loop, the for end character may also be a modifier
428  *    eg. for %a in (c:\a.a) do echo XXX
429  *             where XXX = %~a    (just ~)
430  *                         %~aa   (~ and attributes)
431  *                         %~aaxa (~, attributes and extension)
432  *                   BUT   %~aax  (~ and attributes followed by 'x')
433  *
434  *  Hence search forwards until find an invalid modifier, and then
435  *  backwards until find for variable or 0-9
436  */
437 void WCMD_HandleTildaModifiers(char **start, char *forVariable) {
438
439 #define NUMMODIFIERS 11
440   const char const validmodifiers[NUMMODIFIERS] = {
441         '~', 'f', 'd', 'p', 'n', 'x', 's', 'a', 't', 'z', '$'
442   };
443
444   WIN32_FILE_ATTRIBUTE_DATA fileInfo;
445   char  outputparam[MAX_PATH];
446   char  finaloutput[MAX_PATH];
447   char  fullfilename[MAX_PATH];
448   char  thisoutput[MAX_PATH];
449   char  *pos            = *start+1;
450   char  *firstModifier  = pos;
451   char  *lastModifier   = NULL;
452   int   modifierLen     = 0;
453   BOOL  finished        = FALSE;
454   int   i               = 0;
455   BOOL  exists          = TRUE;
456   BOOL  skipFileParsing = FALSE;
457   BOOL  doneModifier    = FALSE;
458
459   /* Search forwards until find invalid character modifier */
460   while (!finished) {
461
462     /* Work on the previous character */
463     if (lastModifier != NULL) {
464
465       for (i=0; i<NUMMODIFIERS; i++) {
466         if (validmodifiers[i] == *lastModifier) {
467
468           /* Special case '$' to skip until : found */
469           if (*lastModifier == '$') {
470             while (*pos != ':' && *pos) pos++;
471             if (*pos == 0x00) return; /* Invalid syntax */
472             pos++;                    /* Skip ':'       */
473           }
474           break;
475         }
476       }
477
478       if (i==NUMMODIFIERS) {
479         finished = TRUE;
480       }
481     }
482
483     /* Save this one away */
484     if (!finished) {
485       lastModifier = pos;
486       pos++;
487     }
488   }
489
490   /* Now make sure the position we stopped at is a valid parameter */
491   if (!(*lastModifier >= '0' || *lastModifier <= '9') &&
492       (forVariable != NULL) &&
493       (toupper(*lastModifier) != toupper(*forVariable)))  {
494
495     /* Its not... Step backwards until it matches or we get to the start */
496     while (toupper(*lastModifier) != toupper(*forVariable) &&
497           lastModifier > firstModifier) {
498       lastModifier--;
499     }
500     if (lastModifier == firstModifier) return; /* Invalid syntax */
501   }
502
503   /* Extract the parameter to play with */
504   if ((*lastModifier >= '0' && *lastModifier <= '9')) {
505     strcpy(outputparam, WCMD_parameter (context -> command,
506                  *lastModifier-'0' + context -> shift_count, NULL));
507   } else {
508     /* FIXME: Retrieve 'for' variable %c\n", *lastModifier); */
509     /* Need to get 'for' loop variable into outputparam      */
510     return;
511   }
512
513   /* So now, firstModifier points to beginning of modifiers, lastModifier
514      points to the variable just after the modifiers. Process modifiers
515      in a specific order, remembering there could be duplicates           */
516   modifierLen = lastModifier - firstModifier;
517   finaloutput[0] = 0x00;
518
519   /* Useful for debugging purposes: */
520   /*printf("Modifier string '%*.*s' and variable is %c\n Param starts as '%s'\n",
521              (modifierLen), (modifierLen), firstModifier, *lastModifier,
522              outputparam);*/
523
524   /* 1. Handle '~' : Strip surrounding quotes */
525   if (outputparam[0]=='"' &&
526       memchr(firstModifier, '~', modifierLen) != NULL) {
527     int len = strlen(outputparam);
528     if (outputparam[len-1] == '"') {
529         outputparam[len-1]=0x00;
530         len = len - 1;
531     }
532     memmove(outputparam, &outputparam[1], len-1);
533   }
534
535   /* 2. Handle the special case of a $ */
536   if (memchr(firstModifier, '$', modifierLen) != NULL) {
537     /* Special Case: Search envar specified in $[envvar] for outputparam
538        Note both $ and : are guaranteed otherwise check above would fail */
539     char *start = strchr(firstModifier, '$') + 1;
540     char *end   = strchr(firstModifier, ':');
541     char env[MAX_PATH];
542     char fullpath[MAX_PATH];
543
544     /* Extract the env var */
545     strncpy(env, start, (end-start));
546     env[(end-start)] = 0x00;
547
548     /* If env var not found, return emptry string */
549     if ((GetEnvironmentVariable(env, fullpath, MAX_PATH) == 0) ||
550         (SearchPath(fullpath, outputparam, NULL,
551                     MAX_PATH, outputparam, NULL) == 0)) {
552       finaloutput[0] = 0x00;
553       outputparam[0] = 0x00;
554       skipFileParsing = TRUE;
555     }
556   }
557
558   /* After this, we need full information on the file,
559     which is valid not to exist.  */
560   if (!skipFileParsing) {
561     if (GetFullPathName(outputparam, MAX_PATH, fullfilename, NULL) == 0)
562       return;
563
564     exists = GetFileAttributesExA(fullfilename, GetFileExInfoStandard,
565                                   &fileInfo);
566
567     /* 2. Handle 'a' : Output attributes */
568     if (exists &&
569         memchr(firstModifier, 'a', modifierLen) != NULL) {
570
571       doneModifier = TRUE;
572       strcpy(thisoutput, "---------");
573       if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
574         thisoutput[0]='d';
575       if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
576         thisoutput[1]='r';
577       if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
578         thisoutput[2]='a';
579       if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
580         thisoutput[3]='h';
581       if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
582         thisoutput[4]='s';
583       if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)
584         thisoutput[5]='c';
585       /* FIXME: What are 6 and 7? */
586       if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
587         thisoutput[8]='l';
588       strcat(finaloutput, thisoutput);
589     }
590
591     /* 3. Handle 't' : Date+time */
592     if (exists &&
593         memchr(firstModifier, 't', modifierLen) != NULL) {
594
595       SYSTEMTIME systime;
596       int datelen;
597
598       doneModifier = TRUE;
599       if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
600
601       /* Format the time */
602       FileTimeToSystemTime(&fileInfo.ftLastWriteTime, &systime);
603       GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systime,
604                         NULL, thisoutput, MAX_PATH);
605       strcat(thisoutput, " ");
606       datelen = strlen(thisoutput);
607       GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &systime,
608                         NULL, (thisoutput+datelen), MAX_PATH-datelen);
609       strcat(finaloutput, thisoutput);
610     }
611
612     /* 4. Handle 'z' : File length */
613     if (exists &&
614         memchr(firstModifier, 'z', modifierLen) != NULL) {
615       /* FIXME: Output full 64 bit size (sprintf not support I64 here) */
616       ULONG/*64*/ fullsize = /*(fileInfo.nFileSizeHigh << 32) +*/
617                                   fileInfo.nFileSizeLow;
618
619       doneModifier = TRUE;
620       if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
621       sprintf(thisoutput, "%u", fullsize);
622       strcat(finaloutput, thisoutput);
623     }
624
625     /* 4. Handle 's' : Use short paths (File doesnt have to exist) */
626     if (memchr(firstModifier, 's', modifierLen) != NULL) {
627       if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
628       /* Dont flag as doneModifier - %~s on its own is processed later */
629       GetShortPathName(outputparam, outputparam, sizeof(outputparam));
630     }
631
632     /* 5. Handle 'f' : Fully qualified path (File doesnt have to exist) */
633     /*      Note this overrides d,p,n,x                                 */
634     if (memchr(firstModifier, 'f', modifierLen) != NULL) {
635       doneModifier = TRUE;
636       if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
637       strcat(finaloutput, fullfilename);
638     } else {
639
640       char drive[10];
641       char dir[MAX_PATH];
642       char fname[MAX_PATH];
643       char ext[MAX_PATH];
644       BOOL doneFileModifier = FALSE;
645
646       if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
647
648       /* Split into components */
649       _splitpath(fullfilename, drive, dir, fname, ext);
650
651       /* 5. Handle 'd' : Drive Letter */
652       if (memchr(firstModifier, 'd', modifierLen) != NULL) {
653         strcat(finaloutput, drive);
654         doneModifier = TRUE;
655         doneFileModifier = TRUE;
656       }
657
658       /* 6. Handle 'p' : Path */
659       if (memchr(firstModifier, 'p', modifierLen) != NULL) {
660         strcat(finaloutput, dir);
661         doneModifier = TRUE;
662         doneFileModifier = TRUE;
663       }
664
665       /* 7. Handle 'n' : Name */
666       if (memchr(firstModifier, 'n', modifierLen) != NULL) {
667         strcat(finaloutput, fname);
668         doneModifier = TRUE;
669         doneFileModifier = TRUE;
670       }
671
672       /* 8. Handle 'x' : Ext */
673       if (memchr(firstModifier, 'x', modifierLen) != NULL) {
674         strcat(finaloutput, ext);
675         doneModifier = TRUE;
676         doneFileModifier = TRUE;
677       }
678
679       /* If 's' but no other parameter, dump the whole thing */
680       if (!doneFileModifier &&
681           memchr(firstModifier, 's', modifierLen) != NULL) {
682         doneModifier = TRUE;
683         if (finaloutput[0] != 0x00) strcat(finaloutput, " ");
684         strcat(finaloutput, outputparam);
685       }
686     }
687   }
688
689   /* If No other modifier processed,  just add in parameter */
690   if (!doneModifier) strcpy(finaloutput, outputparam);
691
692   /* Finish by inserting the replacement into the string */
693   pos = strdup (lastModifier+1);
694   strcpy(*start, finaloutput);
695   strcat(*start, pos);
696   free(pos);
697 }