123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189(*********************************************************************************)(* Stog *)(* *)(* Copyright (C) 2012-2015 INRIA All rights reserved. *)(* Author: Maxence Guesdon, INRIA Saclay *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)moduleXR=Xtmpl.RewritemoduleXml=Xtmpl.Xmltyperesult=Okofstring|Errorofstringletexec_command?directorycommand=letcommand=Stog_base.Misc.strip_stringcommandinletin_dircom=matchdirectorywith|None->com|Somed->Printf.sprintf"cd %s && %s"(Filename.quoted)cominlettemp_file=Filename.temp_file"stogexec"".txt"inletcom=Printf.sprintf"(%s) > %s 2>&1"(in_dircommand)(Filename.quotetemp_file)inmatchSys.commandcomwith0->letoutput=Stog_base.Misc.string_of_filetemp_filein(trySys.removetemp_filewith_->());Okoutput|n->letoutput=Stog_base.Misc.string_of_filetemp_filein(trySys.removetemp_filewith_->());Erroroutputletconcat_code=letfb=functionXR.Dcode->Buffer.add_stringbcode.Xml.text|xml->failwith(Printf.sprintf"XML code in command: %s"(XR.to_string[xml]))infunxmls->letb=Buffer.create256inList.iter(fb)xmls;Buffer.contentsbletcommands_of_xmlxmls=letfxmlacc=matchxmlwithXR.D_|XR.C_|XR.PI_->acc|XR.E{XR.subs}->(concat_codesubs)::accinList.fold_rightfxmls[]letconcat_nl=Ocaml.concat_nlletlist_concat_nl=Ocaml.list_concat_nlletfun_execstogenv?locargscode=tryletdirectory=matchXR.get_att_cdataargs("","directory")withNone|Some""->None|x->xinletexc=XR.opt_att_cdataargs~def:"true"("","stop-on-error")="true"inletprompt=matchXR.get_att_cdataargs("","prompt")withNone|Some""->None|x->xinletshow_code=XR.opt_att_cdataargs~def:"true"("","show-code")<>"false"inletshow_stdout=XR.opt_att_cdataargs~def:(ifprompt<>Nonethen"true"else"false")("","show-stdout")<>"false"inletin_xml_block=XR.opt_att_cdataargs~def:"true"("","in-xml-block")<>"false"inletid_opt=XR.opt_att_cdataargs("","id")inletatts=XR.atts_of_list(matchid_optwith""->[]|id->[("","id"),[XR.cdataid]])inletlist=XR.opt_att_cdataargs~def:"false"("","list")="true"inletcommands=iflistthencommands_of_xmlcodeelse[concat_codecode]inletreciteracc=function[]->List.revacc|command::q->letlang_file=letd=stog.Types.stog_dirinFilename.concatd"sh.lang"inletopts=ifSys.file_existslang_filethenSome(Printf.sprintf"--config-file=%s"lang_file)elseNoneinletcode=ifshow_codethenHighlight.highlight~lang:"sh"?optscommandelse[XR.cdata""]in(*prerr_endline (Printf.sprintf "execute %S" command);*)let(output,error)=matchexec_command?directorycommandwithOkoutput->(output,false)|Erroroutput->(output,true)iniferror&&excthenbeginletmsg=Xtmpl.Xml.loc_sprintfloc"Exec error with command:\n%s\n%s"commandoutputinfailwithmsgend;letacc=letcode=ifin_xml_blockthenbeginletcode=matchpromptwithNone->code|Somestr->(Xtmpl.Xhtml.span~classes:["command-prompt"][XR.cdatastr])::codein[XR.node("","span")code]endelsecodeinmatchoutputwith""->list_concat_nlcodeacc|_->letclasses=Printf.sprintf"command-output%s"(iferrorthen" command-error"else"")inletxml=XR.node("","span")~atts:(XR.atts_one("","class")[XR.cdataclasses])[XR.cdataoutput]inlist_concat_nl(concat_nlxmlcode)acciniteraccqinletxml=iter[]commandsinifshow_code||show_stdoutthenletxml=ifin_xml_blockthen[XR.node("","pre")~atts:(XR.atts_of_list~atts[("","class"),[XR.cdata"command-exec"]])xml]elsexmlin(stog,xml)else(stog,[XR.cdata""])withe->raisee;;