Imported Upstream version 2.8.11.2
[platform/upstream/cmake.git] / Source / cmStringCommand.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmStringCommand.h"
13 #include "cmCryptoHash.h"
14
15 #include <cmsys/RegularExpression.hxx>
16 #include <cmsys/SystemTools.hxx>
17
18 #include <stdlib.h> // required for atoi
19 #include <ctype.h>
20 #include <time.h>
21
22 #include <cmTimestamp.h>
23
24 //----------------------------------------------------------------------------
25 bool cmStringCommand
26 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
27 {
28   if(args.size() < 1)
29     {
30     this->SetError("must be called with at least one argument.");
31     return false;
32     }
33
34   const std::string &subCommand = args[0];
35   if(subCommand == "REGEX")
36     {
37     return this->HandleRegexCommand(args);
38     }
39   else if(subCommand == "REPLACE")
40     {
41     return this->HandleReplaceCommand(args);
42     }
43   else if ( subCommand == "MD5" ||
44             subCommand == "SHA1" ||
45             subCommand == "SHA224" ||
46             subCommand == "SHA256" ||
47             subCommand == "SHA384" ||
48             subCommand == "SHA512" )
49     {
50     return this->HandleHashCommand(args);
51     }
52   else if(subCommand == "TOLOWER")
53     {
54     return this->HandleToUpperLowerCommand(args, false);
55     }
56   else if(subCommand == "TOUPPER")
57     {
58     return this->HandleToUpperLowerCommand(args, true);
59     }
60   else if(subCommand == "COMPARE")
61     {
62     return this->HandleCompareCommand(args);
63     }
64   else if(subCommand == "ASCII")
65     {
66     return this->HandleAsciiCommand(args);
67     }
68   else if(subCommand == "CONFIGURE")
69     {
70     return this->HandleConfigureCommand(args);
71     }
72   else if(subCommand == "LENGTH")
73     {
74     return this->HandleLengthCommand(args);
75     }
76   else if(subCommand == "SUBSTRING")
77     {
78     return this->HandleSubstringCommand(args);
79     }
80   else if(subCommand == "STRIP")
81     {
82     return this->HandleStripCommand(args);
83     }
84   else if(subCommand == "RANDOM")
85     {
86     return this->HandleRandomCommand(args);
87     }
88   else if(subCommand == "FIND")
89     {
90     return this->HandleFindCommand(args);
91     }
92   else if(subCommand == "TIMESTAMP")
93     {
94     return this->HandleTimestampCommand(args);
95     }
96
97   std::string e = "does not recognize sub-command "+subCommand;
98   this->SetError(e.c_str());
99   return false;
100 }
101
102 //----------------------------------------------------------------------------
103 bool cmStringCommand::HandleHashCommand(std::vector<std::string> const& args)
104 {
105 #if defined(CMAKE_BUILD_WITH_CMAKE)
106   if(args.size() != 3)
107     {
108     cmOStringStream e;
109     e << args[0] << " requires an output variable and an input string";
110     this->SetError(e.str().c_str());
111     return false;
112     }
113
114   cmsys::auto_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
115   if(hash.get())
116     {
117     std::string out = hash->HashString(args[2].c_str());
118     this->Makefile->AddDefinition(args[1].c_str(), out.c_str());
119     return true;
120     }
121   return false;
122 #else
123   cmOStringStream e;
124   e << args[0] << " not available during bootstrap";
125   this->SetError(e.str().c_str());
126   return false;
127 #endif
128 }
129
130 //----------------------------------------------------------------------------
131 bool cmStringCommand::HandleToUpperLowerCommand(
132   std::vector<std::string> const& args, bool toUpper)
133 {
134   if ( args.size() < 3 )
135     {
136     this->SetError("no output variable specified");
137     return false;
138     }
139
140   std::string outvar = args[2];
141   std::string output;
142
143   if (toUpper)
144     {
145     output = cmSystemTools::UpperCase(args[1]);
146     }
147   else
148     {
149     output = cmSystemTools::LowerCase(args[1]);
150     }
151
152   // Store the output in the provided variable.
153   this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
154   return true;
155 }
156
157 //----------------------------------------------------------------------------
158 bool cmStringCommand::HandleAsciiCommand(std::vector<std::string> const& args)
159 {
160   if ( args.size() < 3 )
161     {
162     this->SetError("No output variable specified");
163     return false;
164     }
165   std::string::size_type cc;
166   std::string outvar = args[args.size()-1];
167   std::string output = "";
168   for ( cc = 1; cc < args.size()-1; cc ++ )
169     {
170     int ch = atoi(args[cc].c_str());
171     if ( ch > 0 && ch < 256 )
172       {
173       output += static_cast<char>(ch);
174       }
175     else
176       {
177       std::string error = "Character with code ";
178       error += args[cc];
179       error += " does not exist.";
180       this->SetError(error.c_str());
181       return false;
182       }
183     }
184   // Store the output in the provided variable.
185   this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
186   return true;
187 }
188
189 //----------------------------------------------------------------------------
190 bool cmStringCommand::HandleConfigureCommand(
191   std::vector<std::string> const& args)
192 {
193   if ( args.size() < 2 )
194     {
195     this->SetError("No input string specified.");
196     return false;
197     }
198   else if ( args.size() < 3 )
199     {
200     this->SetError("No output variable specified.");
201     return false;
202     }
203
204   // Parse options.
205   bool escapeQuotes = false;
206   bool atOnly = false;
207   for(unsigned int i = 3; i < args.size(); ++i)
208     {
209     if(args[i] == "@ONLY")
210       {
211       atOnly = true;
212       }
213     else if(args[i] == "ESCAPE_QUOTES")
214       {
215       escapeQuotes = true;
216       }
217     else
218       {
219       cmOStringStream err;
220       err << "Unrecognized argument \"" << args[i] << "\"";
221       this->SetError(err.str().c_str());
222       return false;
223       }
224     }
225
226   // Configure the string.
227   std::string output;
228   this->Makefile->ConfigureString(args[1], output, atOnly, escapeQuotes);
229
230   // Store the output in the provided variable.
231   this->Makefile->AddDefinition(args[2].c_str(), output.c_str());
232
233   return true;
234 }
235
236 //----------------------------------------------------------------------------
237 bool cmStringCommand::HandleRegexCommand(std::vector<std::string> const& args)
238 {
239   if(args.size() < 2)
240     {
241     this->SetError("sub-command REGEX requires a mode to be specified.");
242     return false;
243     }
244   std::string mode = args[1];
245   if(mode == "MATCH")
246     {
247     if(args.size() < 5)
248       {
249       this->SetError("sub-command REGEX, mode MATCH needs "
250                      "at least 5 arguments total to command.");
251       return false;
252       }
253     return this->RegexMatch(args);
254     }
255   else if(mode == "MATCHALL")
256     {
257     if(args.size() < 5)
258       {
259       this->SetError("sub-command REGEX, mode MATCHALL needs "
260                      "at least 5 arguments total to command.");
261       return false;
262       }
263     return this->RegexMatchAll(args);
264     }
265   else if(mode == "REPLACE")
266     {
267     if(args.size() < 6)
268       {
269       this->SetError("sub-command REGEX, mode REPLACE needs "
270                      "at least 6 arguments total to command.");
271       return false;
272       }
273     return this->RegexReplace(args);
274     }
275
276   std::string e = "sub-command REGEX does not recognize mode "+mode;
277   this->SetError(e.c_str());
278   return false;
279 }
280
281 //----------------------------------------------------------------------------
282 bool cmStringCommand::RegexMatch(std::vector<std::string> const& args)
283 {
284   //"STRING(REGEX MATCH <regular_expression> <output variable>
285   // <input> [<input>...])\n";
286   std::string regex = args[2];
287   std::string outvar = args[3];
288
289   // Concatenate all the last arguments together.
290   std::string input = args[4];
291   for(unsigned int i=5; i < args.size(); ++i)
292     {
293     input += args[i];
294     }
295
296   this->ClearMatches(this->Makefile);
297   // Compile the regular expression.
298   cmsys::RegularExpression re;
299   if(!re.compile(regex.c_str()))
300     {
301     std::string e =
302       "sub-command REGEX, mode MATCH failed to compile regex \""+regex+"\".";
303     this->SetError(e.c_str());
304     return false;
305     }
306
307   // Scan through the input for all matches.
308   std::string output;
309   if(re.find(input.c_str()))
310     {
311     this->StoreMatches(this->Makefile, re);
312     std::string::size_type l = re.start();
313     std::string::size_type r = re.end();
314     if(r-l == 0)
315       {
316       std::string e =
317         "sub-command REGEX, mode MATCH regex \""+regex+
318         "\" matched an empty string.";
319       this->SetError(e.c_str());
320       return false;
321       }
322     output = input.substr(l, r-l);
323     }
324
325   // Store the output in the provided variable.
326   this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
327   return true;
328 }
329
330 //----------------------------------------------------------------------------
331 bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
332 {
333   //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input>
334   // [<input>...])\n";
335   std::string regex = args[2];
336   std::string outvar = args[3];
337
338   // Concatenate all the last arguments together.
339   std::string input = args[4];
340   for(unsigned int i=5; i < args.size(); ++i)
341     {
342     input += args[i];
343     }
344
345   this->ClearMatches(this->Makefile);
346   // Compile the regular expression.
347   cmsys::RegularExpression re;
348   if(!re.compile(regex.c_str()))
349     {
350     std::string e =
351       "sub-command REGEX, mode MATCHALL failed to compile regex \""+
352       regex+"\".";
353     this->SetError(e.c_str());
354     return false;
355     }
356
357   // Scan through the input for all matches.
358   std::string output;
359   const char* p = input.c_str();
360   while(re.find(p))
361     {
362     this->StoreMatches(this->Makefile, re);
363     std::string::size_type l = re.start();
364     std::string::size_type r = re.end();
365     if(r-l == 0)
366       {
367       std::string e = "sub-command REGEX, mode MATCHALL regex \""+
368         regex+"\" matched an empty string.";
369       this->SetError(e.c_str());
370       return false;
371       }
372     if(output.length() > 0)
373       {
374       output += ";";
375       }
376     output += std::string(p+l, r-l);
377     p += r;
378     }
379
380   // Store the output in the provided variable.
381   this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
382   return true;
383 }
384
385 //----------------------------------------------------------------------------
386 bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
387 {
388   //"STRING(REGEX REPLACE <regular_expression> <replace_expression>
389   // <output variable> <input> [<input>...])\n"
390   std::string regex = args[2];
391   std::string replace = args[3];
392   std::string outvar = args[4];
393
394   // Pull apart the replace expression to find the escaped [0-9] values.
395   std::vector<RegexReplacement> replacement;
396   std::string::size_type l = 0;
397   while(l < replace.length())
398     {
399     std::string::size_type r = replace.find("\\", l);
400     if(r == std::string::npos)
401       {
402       r = replace.length();
403       replacement.push_back(replace.substr(l, r-l));
404       }
405     else
406       {
407       if(r-l > 0)
408         {
409         replacement.push_back(replace.substr(l, r-l));
410         }
411       if(r == (replace.length()-1))
412         {
413         this->SetError("sub-command REGEX, mode REPLACE: "
414                        "replace-expression ends in a backslash.");
415         return false;
416         }
417       if((replace[r+1] >= '0') && (replace[r+1] <= '9'))
418         {
419         replacement.push_back(replace[r+1]-'0');
420         }
421       else if(replace[r+1] == 'n')
422         {
423         replacement.push_back("\n");
424         }
425       else if(replace[r+1] == '\\')
426         {
427         replacement.push_back("\\");
428         }
429       else
430         {
431         std::string e = "sub-command REGEX, mode REPLACE: Unknown escape \"";
432         e += replace.substr(r, 2);
433         e += "\" in replace-expression.";
434         this->SetError(e.c_str());
435         return false;
436         }
437       r += 2;
438       }
439     l = r;
440     }
441
442   // Concatenate all the last arguments together.
443   std::string input = args[5];
444   for(unsigned int i=6; i < args.size(); ++i)
445     {
446     input += args[i];
447     }
448
449   this->ClearMatches(this->Makefile);
450   // Compile the regular expression.
451   cmsys::RegularExpression re;
452   if(!re.compile(regex.c_str()))
453     {
454     std::string e =
455       "sub-command REGEX, mode REPLACE failed to compile regex \""+
456       regex+"\".";
457     this->SetError(e.c_str());
458     return false;
459     }
460
461   // Scan through the input for all matches.
462   std::string output;
463   std::string::size_type base = 0;
464   while(re.find(input.c_str()+base))
465     {
466     this->StoreMatches(this->Makefile, re);
467     std::string::size_type l2 = re.start();
468     std::string::size_type r = re.end();
469
470     // Concatenate the part of the input that was not matched.
471     output += input.substr(base, l2);
472
473     // Make sure the match had some text.
474     if(r-l2 == 0)
475       {
476       std::string e = "sub-command REGEX, mode REPLACE regex \""+
477         regex+"\" matched an empty string.";
478       this->SetError(e.c_str());
479       return false;
480       }
481
482     // Concatenate the replacement for the match.
483     for(unsigned int i=0; i < replacement.size(); ++i)
484       {
485       if(replacement[i].number < 0)
486         {
487         // This is just a plain-text part of the replacement.
488         output += replacement[i].value;
489         }
490       else
491         {
492         // Replace with part of the match.
493         int n = replacement[i].number;
494         std::string::size_type start = re.start(n);
495         std::string::size_type end = re.end(n);
496         std::string::size_type len = input.length()-base;
497         if((start != std::string::npos) && (end != std::string::npos) &&
498            (start <= len) && (end <= len))
499           {
500           output += input.substr(base+start, end-start);
501           }
502         else
503           {
504           std::string e =
505             "sub-command REGEX, mode REPLACE: replace expression \""+
506             replace+"\" contains an out-of-range escape for regex \""+
507             regex+"\".";
508           this->SetError(e.c_str());
509           return false;
510           }
511         }
512       }
513
514     // Move past the match.
515     base += r;
516     }
517
518   // Concatenate the text after the last match.
519   output += input.substr(base, input.length()-base);
520
521   // Store the output in the provided variable.
522   this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
523   return true;
524 }
525
526 //----------------------------------------------------------------------------
527 void cmStringCommand::ClearMatches(cmMakefile* mf)
528 {
529   for (unsigned int i=0; i<10; i++)
530     {
531     char name[128];
532     sprintf(name, "CMAKE_MATCH_%d", i);
533     mf->AddDefinition(name, "");
534     mf->MarkVariableAsUsed(name);
535     }
536 }
537
538 //----------------------------------------------------------------------------
539 void cmStringCommand::StoreMatches(cmMakefile* mf,cmsys::RegularExpression& re)
540 {
541   for (unsigned int i=0; i<10; i++)
542     {
543     char name[128];
544     sprintf(name, "CMAKE_MATCH_%d", i);
545     mf->AddDefinition(name, re.match(i).c_str());
546     mf->MarkVariableAsUsed(name);
547     }
548 }
549
550 //----------------------------------------------------------------------------
551 bool cmStringCommand::HandleFindCommand(std::vector<std::string> const&
552                                            args)
553 {
554   // check if all required parameters were passed
555   if(args.size() < 4 || args.size() > 5)
556     {
557     this->SetError("sub-command FIND requires 3 or 4 parameters.");
558     return false;
559     }
560
561   // check if the reverse flag was set or not
562   bool reverseMode = false;
563   if(args.size() == 5 && args[4] == "REVERSE")
564     {
565     reverseMode = true;
566     }
567
568   // if we have 5 arguments the last one must be REVERSE
569   if(args.size() == 5 && args[4] != "REVERSE")
570     {
571     this->SetError("sub-command FIND: unknown last parameter");
572     return false;
573     }
574
575   // local parameter names.
576   const std::string& sstring = args[1];
577   const std::string& schar = args[2];
578   const std::string& outvar = args[3];
579
580   // ensure that the user cannot accidentally specify REVERSE as a variable
581   if(outvar == "REVERSE")
582     {
583     this->SetError("sub-command FIND does not allow to select REVERSE as "
584                    "the output variable.  "
585                    "Maybe you missed the actual output variable?");
586     return false;
587     }
588
589   // try to find the character and return its position
590   size_t pos;
591   if(!reverseMode)
592     {
593     pos = sstring.find(schar);
594     }
595   else
596     {
597     pos = sstring.rfind(schar);
598     }
599   if(std::string::npos != pos)
600     {
601     cmOStringStream s;
602     s << pos;
603     this->Makefile->AddDefinition(outvar.c_str(), s.str().c_str());
604     return true;
605     }
606
607   // the character was not found, but this is not really an error
608   this->Makefile->AddDefinition(outvar.c_str(), "-1");
609   return true;
610 }
611
612 //----------------------------------------------------------------------------
613 bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const&
614                                            args)
615 {
616   if(args.size() < 2)
617     {
618     this->SetError("sub-command COMPARE requires a mode to be specified.");
619     return false;
620     }
621   std::string mode = args[1];
622   if((mode == "EQUAL") || (mode == "NOTEQUAL") ||
623      (mode == "LESS") || (mode == "GREATER"))
624     {
625     if(args.size() < 5)
626       {
627       std::string e = "sub-command COMPARE, mode ";
628       e += mode;
629       e += " needs at least 5 arguments total to command.";
630       this->SetError(e.c_str());
631       return false;
632       }
633
634     const std::string& left = args[2];
635     const std::string& right = args[3];
636     const std::string& outvar = args[4];
637     bool result;
638     if(mode == "LESS")
639       {
640       result = (left < right);
641       }
642     else if(mode == "GREATER")
643       {
644       result = (left > right);
645       }
646     else if(mode == "EQUAL")
647       {
648       result = (left == right);
649       }
650     else // if(mode == "NOTEQUAL")
651       {
652       result = !(left == right);
653       }
654     if(result)
655       {
656       this->Makefile->AddDefinition(outvar.c_str(), "1");
657       }
658     else
659       {
660       this->Makefile->AddDefinition(outvar.c_str(), "0");
661       }
662     return true;
663     }
664   std::string e = "sub-command COMPARE does not recognize mode "+mode;
665   this->SetError(e.c_str());
666   return false;
667 }
668
669 //----------------------------------------------------------------------------
670 bool cmStringCommand::HandleReplaceCommand(std::vector<std::string> const&
671                                            args)
672 {
673   if(args.size() < 5)
674     {
675     this->SetError("sub-command REPLACE requires at least four arguments.");
676     return false;
677     }
678
679   const std::string& matchExpression = args[1];
680   const std::string& replaceExpression = args[2];
681   const std::string& variableName = args[3];
682
683   std::string input = args[4];
684   for(unsigned int i=5; i < args.size(); ++i)
685     {
686     input += args[i];
687     }
688
689   cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(),
690                                     replaceExpression.c_str());
691
692   this->Makefile->AddDefinition(variableName.c_str(), input.c_str());
693   return true;
694 }
695
696 //----------------------------------------------------------------------------
697 bool cmStringCommand::HandleSubstringCommand(std::vector<std::string> const&
698                                              args)
699 {
700   if(args.size() != 5)
701     {
702     this->SetError("sub-command SUBSTRING requires four arguments.");
703     return false;
704     }
705
706   const std::string& stringValue = args[1];
707   int begin = atoi(args[2].c_str());
708   int end = atoi(args[3].c_str());
709   const std::string& variableName = args[4];
710
711   size_t stringLength = stringValue.size();
712   int intStringLength = static_cast<int>(stringLength);
713   if ( begin < 0 || begin > intStringLength )
714     {
715     cmOStringStream ostr;
716     ostr << "begin index: " << begin << " is out of range 0 - "
717          << stringLength;
718     this->SetError(ostr.str().c_str());
719     return false;
720     }
721   int leftOverLength = intStringLength - begin;
722   if ( end < -1 || end > leftOverLength )
723     {
724     cmOStringStream ostr;
725     ostr << "end index: " << end << " is out of range -1 - "
726          << leftOverLength;
727     this->SetError(ostr.str().c_str());
728     return false;
729     }
730
731   this->Makefile->AddDefinition(variableName.c_str(),
732                                 stringValue.substr(begin, end).c_str());
733   return true;
734 }
735
736 //----------------------------------------------------------------------------
737 bool cmStringCommand
738 ::HandleLengthCommand(std::vector<std::string> const& args)
739 {
740   if(args.size() != 3)
741     {
742     this->SetError("sub-command LENGTH requires two arguments.");
743     return false;
744     }
745
746   const std::string& stringValue = args[1];
747   const std::string& variableName = args[2];
748
749   size_t length = stringValue.size();
750   char buffer[1024];
751   sprintf(buffer, "%d", static_cast<int>(length));
752
753   this->Makefile->AddDefinition(variableName.c_str(), buffer);
754   return true;
755 }
756
757 //----------------------------------------------------------------------------
758 bool cmStringCommand::HandleStripCommand(
759   std::vector<std::string> const& args)
760 {
761  if(args.size() != 3)
762     {
763     this->SetError("sub-command STRIP requires two arguments.");
764     return false;
765     }
766
767   const std::string& stringValue = args[1];
768   const std::string& variableName = args[2];
769   size_t inStringLength = stringValue.size();
770   size_t startPos = inStringLength + 1;
771   size_t endPos = 0;
772   const char* ptr = stringValue.c_str();
773   size_t cc;
774   for ( cc = 0; cc < inStringLength; ++ cc )
775     {
776     if ( !isspace(*ptr) )
777       {
778       if ( startPos > inStringLength )
779         {
780         startPos = cc;
781         }
782       endPos = cc;
783       }
784     ++ ptr;
785     }
786
787   size_t outLength = 0;
788
789   // if the input string didn't contain any non-space characters, return
790   // an empty string
791   if (startPos > inStringLength)
792     {
793     outLength = 0;
794     startPos = 0;
795     }
796   else
797     {
798     outLength=endPos - startPos + 1;
799     }
800
801   this->Makefile->AddDefinition(variableName.c_str(),
802     stringValue.substr(startPos, outLength).c_str());
803   return true;
804 }
805
806 //----------------------------------------------------------------------------
807 bool cmStringCommand
808 ::HandleRandomCommand(std::vector<std::string> const& args)
809 {
810   if(args.size() < 2 || args.size() == 3 || args.size() == 5)
811     {
812     this->SetError("sub-command RANDOM requires at least one argument.");
813     return false;
814     }
815
816   static bool seeded = false;
817   bool force_seed = false;
818   unsigned int seed = 0;
819   int length = 5;
820   const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
821     "QWERTYUIOPASDFGHJKLZXCVBNM"
822     "0123456789";
823   std::string alphabet;
824
825   if ( args.size() > 3 )
826     {
827     size_t i = 1;
828     size_t stopAt = args.size() - 2;
829
830     for ( ; i < stopAt; ++i )
831       {
832       if ( args[i] == "LENGTH" )
833         {
834         ++i;
835         length = atoi(args[i].c_str());
836         }
837       else if ( args[i] == "ALPHABET" )
838         {
839         ++i;
840         alphabet = args[i];
841         }
842       else if ( args[i] == "RANDOM_SEED" )
843         {
844         ++i;
845         seed = static_cast<unsigned int>(atoi(args[i].c_str()));
846         force_seed = true;
847         }
848       }
849     }
850   if ( !alphabet.size() )
851     {
852     alphabet = cmStringCommandDefaultAlphabet;
853     }
854
855   double sizeofAlphabet = static_cast<double>(alphabet.size());
856   if ( sizeofAlphabet < 1 )
857     {
858     this->SetError("sub-command RANDOM invoked with bad alphabet.");
859     return false;
860     }
861   if ( length < 1 )
862     {
863     this->SetError("sub-command RANDOM invoked with bad length.");
864     return false;
865     }
866   const std::string& variableName = args[args.size()-1];
867
868   std::vector<char> result;
869
870   if (!seeded || force_seed)
871     {
872     seeded = true;
873     srand(force_seed? seed : cmSystemTools::RandomSeed());
874     }
875
876   const char* alphaPtr = alphabet.c_str();
877   int cc;
878   for ( cc = 0; cc < length; cc ++ )
879     {
880     int idx=(int) (sizeofAlphabet* rand()/(RAND_MAX+1.0));
881     result.push_back(*(alphaPtr + idx));
882     }
883   result.push_back(0);
884
885   this->Makefile->AddDefinition(variableName.c_str(), &*result.begin());
886   return true;
887 }
888
889 //----------------------------------------------------------------------------
890 bool cmStringCommand
891 ::HandleTimestampCommand(std::vector<std::string> const& args)
892 {
893   if(args.size() < 2)
894     {
895     this->SetError("sub-command TIMESTAMP requires at least one argument.");
896     return false;
897     }
898   else if(args.size() > 4)
899     {
900     this->SetError("sub-command TIMESTAMP takes at most three arguments.");
901     return false;
902     }
903
904   unsigned int argsIndex = 1;
905
906   const std::string &outputVariable = args[argsIndex++];
907
908   std::string formatString;
909   if(args.size() > argsIndex && args[argsIndex] != "UTC")
910     {
911     formatString = args[argsIndex++];
912     }
913
914   bool utcFlag = false;
915   if(args.size() > argsIndex)
916     {
917     if(args[argsIndex] == "UTC")
918       {
919       utcFlag = true;
920       }
921     else
922       {
923       std::string e = " TIMESTAMP sub-command does not recognize option " +
924           args[argsIndex] + ".";
925       this->SetError(e.c_str());
926       return false;
927       }
928     }
929
930   cmTimestamp timestamp;
931   std::string result = timestamp.CurrentTime(formatString, utcFlag);
932   this->Makefile->AddDefinition(outputVariable.c_str(), result.c_str());
933
934   return true;
935 }