Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file cinaps_runtime.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154openStdLabelsletin_place=reffalseletstyler=refNoneletdiff_command=refNoneletuse_color=reffalseletargs=letopenArgin["-i",Setin_place," Update the file in-place";"-diff-cmd",String(funs->diff_command:=Somes)," Diff command when using code expectations";"-no-color",Clearuse_color," Don't use colors when printing errors";"-styler",String(funs->styler:=Somes)," Code styler"]letinit()=letusage=Printf.sprintf"%s <options>"Sys.executable_nameinletanonfn=raise(Arg.Bad(Printf.sprintf"Don't know what to do with %S."fn))inArg.parse(Arg.alignargs)anonusagemodulePrint_diff=structletpatdiff_cmd()=letargs=List.concat[["-keep-whitespace"];["-location-style omake"];(if!use_colorthen["-unrefined"]else["-ascii"]);]inString.concat~sep:" "("patdiff"::args)letprint~file1~file2=letexeccmd=letcmd=Printf.sprintf"%s %s %s 1>&2"cmd(Filename.quotefile1)(Filename.quotefile2)inmatchSys.commandcmdwith|0->true|1->false|n->Printf.eprintf"%S exited with code %d\n"cmdn;exit2inmatch!diff_commandwith|Somes->ignore(execs:bool)|None->ifexec(patdiff_cmd())then(Printf.eprintf"File \"%s\", line 1, characters 0-0:\n%!"file1;ignore(exec"diff -u":bool);)endletfailure=reffalseletcurrent_file_contents=ref""letcopy_inputposlen=output_substringstdout!current_file_contentsposlenletinput_subposlen=String.sub!current_file_contents~pos~lenletprotect~finally~f=matchf()with|x->finally();x|exceptione->finally();raiseeletread_filefn=letic=open_in_binfninletlen=in_channel_lengthicinlets=really_input_stringicleninclose_inic;sletwrite_filefns=letoc=open_out_binfninoutput_stringocs;close_outocletprocess_file~file_name~file_contentsf=lettmp_fn,oc=Filename.open_temp_file"cinaps"(Filename.extensionfile_name)inletexpected=protect~finally:(fun()->Sys.removetmp_fn)~f:(fun()->letstdout_copy=Unix.dupUnix.stdoutinUnix.dup2(Unix.descr_of_out_channeloc)Unix.stdout;close_outoc;current_file_contents:=file_contents;f();flushstdout;Unix.closeUnix.stdout;Unix.dup2stdout_copyUnix.stdout;Unix.closestdout_copy;matchFilename.extensionfile_name,!stylerwith|(".ml"|".mli"),Somecmd->beginletcmd=String.concat~sep:""(matchString.split_on_charcmd~sep:'%'with|[]->assertfalse|x::l->x::List.mapl~f:(funs->letlen=String.lengthsiniflen>0&&s.[0]='i'then(Filename.quotefile_name)^String.subs~pos:1~len:(len-1)else"%"^s))inletcmd=Printf.sprintf"%s %s"cmd(Filename.quotetmp_fn)inletic=Unix.open_process_incmdinlets=letfile_len=String.lengthfile_contentsinletbuf=Buffer.createfile_lenintryBuffer.add_channelbuficfile_len;whiletruedoBuffer.add_channelbufic65536done;assertfalsewithEnd_of_file->Buffer.contentsbufinmatchUnix.close_process_inicwith|WEXITED0->s|WEXITEDn->Printf.eprintf"command exited with code %d: %s\n"ncmd;exit1|WSIGNALEDn->Printf.eprintf"command got signal %d: %s\n"ncmd;exit1|WSTOPPED_->assertfalseend|_->read_filetmp_fn)inletcorrected_fn=file_name^".cinaps-corrected"iniffile_contents=expectedthenbeginifSys.file_existscorrected_fnthenSys.removecorrected_fnendelseif!in_placethenwrite_filefile_nameexpectedelsebeginwrite_filecorrected_fnexpected;match!diff_commandwith|Some"-"->(* keep the corrected file but do not output the diff *)()|_->failure:=true;Print_diff.print~file1:file_name~file2:corrected_fnendletexit()=exit(if!failurethen1else0)