import{a as D,c as F}from"./chunk-EEXR5ZDP.js";import{a as T}from"./chunk-44UOCSGV.js";import{a as I,b as E}from"./chunk-74CEPOFO.js";import"./chunk-DXB73IDG.js";import{sprintf as S}from"sprintf-js";function W(t,s,r){let e=new Date(s*1e3),a="",n=0;for(;n<t.length;)if(t[n]==="%"&&n+1<t.length){let o=t[n+1],i=_(e,o,r);i!==null?(a+=i,n+=2):(a+=t[n],n++)}else a+=t[n],n++;return a}function Y(t,s){let r={year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit",weekday:"short",hour12:!1,timeZone:s};try{let a=new Intl.DateTimeFormat("en-US",r).formatToParts(t),n=u=>a.find(c=>c.type===u)?.value??"",o=new Map([["Sun",0],["Mon",1],["Tue",2],["Wed",3],["Thu",4],["Fri",5],["Sat",6]]),i=n("weekday");return{year:Number.parseInt(n("year"),10)||t.getFullYear(),month:Number.parseInt(n("month"),10)||t.getMonth()+1,day:Number.parseInt(n("day"),10)||t.getDate(),hour:Number.parseInt(n("hour"),10)||t.getHours(),minute:Number.parseInt(n("minute"),10)||t.getMinutes(),second:Number.parseInt(n("second"),10)||t.getSeconds(),weekday:o.get(i)??t.getDay()}}catch{return{year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),second:t.getSeconds(),weekday:t.getDay()}}}function _(t,s,r){let e=Y(t,r),a=(u,c=2)=>String(u).padStart(c,"0"),n=P(e.year,e.month,e.day),o=A(e.year,e.month,e.day,e.weekday,0),i=A(e.year,e.month,e.day,e.weekday,1);switch(s){case"a":return["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][e.weekday];case"A":return["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][e.weekday];case"b":case"h":return["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][e.month-1];case"B":return["January","February","March","April","May","June","July","August","September","October","November","December"][e.month-1];case"c":return`${["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][e.weekday]} ${["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][e.month-1]} ${String(e.day).padStart(2," ")} ${a(e.hour)}:${a(e.minute)}:${a(e.second)} ${e.year}`;case"C":return a(Math.floor(e.year/100));case"d":return a(e.day);case"D":return`${a(e.month)}/${a(e.day)}/${a(e.year%100)}`;case"e":return String(e.day).padStart(2," ");case"F":return`${e.year}-${a(e.month)}-${a(e.day)}`;case"g":return a(N(e.year,e.month,e.day)%100);case"G":return String(N(e.year,e.month,e.day));case"H":return a(e.hour);case"I":return a(e.hour%12||12);case"j":return String(n).padStart(3,"0");case"k":return String(e.hour).padStart(2," ");case"l":return String(e.hour%12||12).padStart(2," ");case"m":return a(e.month);case"M":return a(e.minute);case"n":return`
`;case"N":return"000000000";case"p":return e.hour<12?"AM":"PM";case"P":return e.hour<12?"am":"pm";case"r":return`${a(e.hour%12||12)}:${a(e.minute)}:${a(e.second)} ${e.hour<12?"AM":"PM"}`;case"R":return`${a(e.hour)}:${a(e.minute)}`;case"s":return String(Math.floor(t.getTime()/1e3));case"S":return a(e.second);case"t":return"	";case"T":return`${a(e.hour)}:${a(e.minute)}:${a(e.second)}`;case"u":return String(e.weekday===0?7:e.weekday);case"U":return a(o);case"V":return a(R(e.year,e.month,e.day));case"w":return String(e.weekday);case"W":return a(i);case"x":return`${a(e.month)}/${a(e.day)}/${a(e.year%100)}`;case"X":return`${a(e.hour)}:${a(e.minute)}:${a(e.second)}`;case"y":return a(e.year%100);case"Y":return String(e.year);case"z":return G(t,r);case"Z":return H(t,r);case"%":return"%";default:return null}}function G(t,s){if(!s){let o=-t.getTimezoneOffset(),i=o>=0?"+":"-",u=Math.floor(Math.abs(o)/60),c=Math.abs(o)%60;return`${i}${String(u).padStart(2,"0")}${String(c).padStart(2,"0")}`}try{let u=new Intl.DateTimeFormat("en-US",{timeZone:s,timeZoneName:"longOffset"}).formatToParts(t).find(c=>c.type==="timeZoneName");if(u){let c=u.value.match(/GMT([+-])(\d{2}):(\d{2})/);if(c)return`${c[1]}${c[2]}${c[3]}`;if(u.value==="GMT"||u.value==="UTC")return"+0000"}}catch{}let r=-t.getTimezoneOffset(),e=r>=0?"+":"-",a=Math.floor(Math.abs(r)/60),n=Math.abs(r)%60;return`${e}${String(a).padStart(2,"0")}${String(n).padStart(2,"0")}`}function H(t,s){try{return new Intl.DateTimeFormat("en-US",{timeZone:s,timeZoneName:"short"}).formatToParts(t).find(n=>n.type==="timeZoneName")?.value??"UTC"}catch{return"UTC"}}function P(t,s,r){let e=[31,28,31,30,31,30,31,31,30,31,30,31];(t%4===0&&t%100!==0||t%400===0)&&(e[1]=29);let n=r;for(let o=0;o<s-1;o++)n+=e[o];return n}function A(t,s,r,e,a){let n=P(t,s,r),u=(new Date(t,0,1).getDay()-a+7)%7,c=(e-a+7)%7,l=n-1+u;return Math.floor((l-c+7)/7)}function R(t,s,r){let e=new Date(t,s-1,r,12,0,0);e.setDate(e.getDate()+3-(e.getDay()+6)%7);let a=new Date(e.getFullYear(),0,4);a.setDate(a.getDate()+3-(a.getDay()+6)%7);let n=e.getTime()-a.getTime();return 1+Math.round(n/(10080*60*1e3))}function N(t,s,r){let e=new Date(t,s-1,r,12,0,0);return e.setDate(e.getDate()+3-(e.getDay()+6)%7),e.getFullYear()}function V(t){let s="",r=0;for(;r<t.length;){let e=t[r];if(e<128){s+=String.fromCharCode(e),r++;continue}if((e&224)===192){if(r+1<t.length&&(t[r+1]&192)===128&&e>=194){let a=(e&31)<<6|t[r+1]&63;s+=String.fromCharCode(a),r+=2;continue}s+=String.fromCharCode(e),r++;continue}if((e&240)===224){if(r+2<t.length&&(t[r+1]&192)===128&&(t[r+2]&192)===128){if(e===224&&t[r+1]<160){s+=String.fromCharCode(e),r++;continue}let a=(e&15)<<12|(t[r+1]&63)<<6|t[r+2]&63;if(a>=55296&&a<=57343){s+=String.fromCharCode(e),r++;continue}s+=String.fromCharCode(a),r+=3;continue}s+=String.fromCharCode(e),r++;continue}if((e&248)===240&&e<=244){if(r+3<t.length&&(t[r+1]&192)===128&&(t[r+2]&192)===128&&(t[r+3]&192)===128){if(e===240&&t[r+1]<144){s+=String.fromCharCode(e),r++;continue}let a=(e&7)<<18|(t[r+1]&63)<<12|(t[r+2]&63)<<6|t[r+3]&63;if(a>1114111){s+=String.fromCharCode(e),r++;continue}s+=String.fromCodePoint(a),r+=4;continue}s+=String.fromCharCode(e),r++;continue}s+=String.fromCharCode(e),r++}return s}var X={name:"printf",summary:"format and print data",usage:"printf [-v var] FORMAT [ARGUMENT...]",options:["    -v var     assign the output to shell variable VAR rather than display it","    --help     display this help and exit"],notes:["FORMAT controls the output like in C printf.","Escape sequences: \\n (newline), \\t (tab), \\\\ (backslash)","Format specifiers: %s (string), %d (integer), %f (float), %x (hex), %o (octal), %% (literal %)","Width and precision: %10s (width 10), %.2f (2 decimal places), %010d (zero-padded)","Flags: %- (left-justify), %+ (show sign), %0 (zero-pad)"]},le={name:"printf",async execute(t,s){if(E(t))return I(X);if(t.length===0)return{stdout:"",stderr:`printf: usage: printf format [arguments]
`,exitCode:2};let r=null,e=0;for(;e<t.length;){let o=t[e];if(o==="--"){e++;break}if(o==="-v"){if(e+1>=t.length)return{stdout:"",stderr:`printf: -v: option requires an argument
`,exitCode:1};if(r=t[e+1],!/^[a-zA-Z_][a-zA-Z0-9_]*(\[[^\]]+\])?$/.test(r))return{stdout:"",stderr:`printf: \`${r}': not a valid identifier
`,exitCode:2};e+=2}else{if(o.startsWith("-")&&o!=="-")break;break}}if(e>=t.length)return{stdout:"",stderr:`printf: usage: printf format [arguments]
`,exitCode:1};let a=t[e],n=t.slice(e+1);try{let o=F(a),i="",u=0,c=!1,l="",y=s.env.get("TZ");do{let{result:p,argsConsumed:x,error:g,errMsg:d,stopped:$}=q(o,n,u,y);if(i+=p,u+=x,g&&(c=!0,d&&(l=d)),$)break}while(u<n.length&&u>0);if(u===0&&n.length>0,r){let p=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(['"]?)(.+?)\2\]$/);if(p){let x=p[1],g=p[3];g=g.replace(/\$([a-zA-Z_][a-zA-Z0-9_]*)/g,(d,$)=>s.env.get($)??""),s.env.set(`${x}_${g}`,i)}else s.env.set(r,i);return{stdout:"",stderr:l,exitCode:c?1:0}}return{stdout:i,stderr:l,exitCode:c?1:0}}catch(o){return{stdout:"",stderr:`printf: ${T(o)}
`,exitCode:1}}}};function q(t,s,r,e){let a="",n=0,o=0,i=!1,u="";for(;n<t.length;)if(t[n]==="%"&&n+1<t.length){let c=n;if(n++,t[n]==="%"){a+="%",n++;continue}let l=t.slice(c).match(/^%(-?\d*)(?:\.(\d+))?\(([^)]*)\)T/);if(l){let m=l[1]?parseInt(l[1],10):0,v=l[2]?parseInt(l[2],10):-1,U=l[3],J=l[0],w=s[r+o]||"";o++;let M;w===""||w==="-1"||w==="-2"?M=Math.floor(Date.now()/1e3):M=parseInt(w,10)||0;let h=W(U,M,e);if(v>=0&&h.length>v&&(h=h.slice(0,v)),m!==0){let b=Math.abs(m);h.length<b&&(m<0?h=h.padEnd(b," "):h=h.padStart(b," "))}a+=h,n=c+J.length;continue}for(;n<t.length&&"+-0 #'".includes(t[n]);)n++;let y=!1;if(t[n]==="*")y=!0,n++;else for(;n<t.length&&/\d/.test(t[n]);)n++;let p=!1;if(t[n]===".")if(n++,t[n]==="*")p=!0,n++;else for(;n<t.length&&/\d/.test(t[n]);)n++;n<t.length&&"hlL".includes(t[n])&&n++;let x=t[n]||"";n++;let d=t.slice(c,n);if(y){let m=parseInt(s[r+o]||"0",10);o++,d=d.replace("*",String(m))}if(p){let m=parseInt(s[r+o]||"0",10);o++,d=d.replace(".*",`.${m}`)}let $=s[r+o]||"";o++;let{value:z,parseError:Z,parseErrMsg:k,stopped:j}=L(d,x,$);if(a+=z,Z&&(i=!0,k&&(u=k)),j)return{result:a,argsConsumed:o,error:i,errMsg:u,stopped:!0}}else a+=t[n],n++;return{result:a,argsConsumed:o,error:i,errMsg:u,stopped:!1}}function L(t,s,r){let e=!1,a="";switch(s){case"d":case"i":{let n=C(r);return e=f,e&&(a=`printf: ${r}: invalid number
`),{value:O(t,n),parseError:e,parseErrMsg:a}}case"o":{let n=C(r);return e=f,e&&(a=`printf: ${r}: invalid number
`),{value:Q(t,n),parseError:e,parseErrMsg:a}}case"u":{let n=C(r);e=f,e&&(a=`printf: ${r}: invalid number
`);let o=n<0?n>>>0:n;return{value:O(t.replace("u","d"),o),parseError:e,parseErrMsg:a}}case"x":case"X":{let n=C(r);return e=f,e&&(a=`printf: ${r}: invalid number
`),{value:B(t,n),parseError:e,parseErrMsg:a}}case"e":case"E":case"f":case"F":case"g":case"G":{let n=parseFloat(r)||0;return{value:re(t,s,n),parseError:!1,parseErrMsg:""}}case"c":{if(r==="")return{value:"",parseError:!1,parseErrMsg:""};let i=new TextEncoder().encode(r)[0];return{value:String.fromCharCode(i),parseError:!1,parseErrMsg:""}}case"s":return{value:ee(t,r),parseError:!1,parseErrMsg:""};case"q":return{value:te(t,r),parseError:!1,parseErrMsg:""};case"b":{let n=ne(r);return{value:n.value,parseError:!1,parseErrMsg:"",stopped:n.stopped}}default:try{return{value:S(t,r),parseError:!1,parseErrMsg:""}}catch{return{value:"",parseError:!0,parseErrMsg:`printf: [sprintf] unexpected placeholder
`}}}}var f=!1;function C(t){f=!1;let s=t.trimStart(),r=s!==s.trimEnd();if(t=s.trimEnd(),t.startsWith("'")&&t.length>=2||t.startsWith('"')&&t.length>=2)return t.charCodeAt(1);if(t.startsWith("\\'")&&t.length>=3||t.startsWith('\\"')&&t.length>=3)return t.charCodeAt(2);if(t.startsWith("+")&&(t=t.slice(1)),t.startsWith("0x")||t.startsWith("0X")){let e=parseInt(t,16);return Number.isNaN(e)?(f=!0,0):(r&&(f=!0),e)}if(t.startsWith("0")&&t.length>1&&/^-?0[0-7]+$/.test(t))return r&&(f=!0),parseInt(t,8)||0;if(/^\d+#/.test(t)){f=!0;let e=t.match(/^(\d+)#/);return e?parseInt(e[1],10):0}if(t!==""&&!/^-?\d+$/.test(t)){f=!0;let e=parseInt(t,10);return Number.isNaN(e)?0:e}return r&&(f=!0),parseInt(t,10)||0}function O(t,s){let r=t.match(/^%([- +#0']*)(\d*)(\.(\d*))?[diu]$/);if(!r)return S(t.replace(/\.\d*/,""),s);let e=r[1]||"",a=r[2]?parseInt(r[2],10):0,n=r[3]!==void 0?r[4]?parseInt(r[4],10):0:-1,o=s<0,i=Math.abs(s),u=String(i);n>=0&&(u=u.padStart(n,"0"));let c="";o?c="-":e.includes("+")?c="+":e.includes(" ")&&(c=" ");let l=c+u;return a>l.length&&(e.includes("-")?l=l.padEnd(a," "):e.includes("0")&&n<0?l=c+u.padStart(a-c.length,"0"):l=l.padStart(a," ")),l}function Q(t,s){let r=t.match(/^%([- +#0']*)(\d*)(\.(\d*))?o$/);if(!r)return S(t,s);let e=r[1]||"",a=r[2]?parseInt(r[2],10):0,n=r[3]!==void 0?r[4]?parseInt(r[4],10):0:-1,o=Math.abs(s).toString(8);n>=0&&(o=o.padStart(n,"0")),e.includes("#")&&!o.startsWith("0")&&(o=`0${o}`);let i=o;return a>i.length&&(e.includes("-")?i=i.padEnd(a," "):e.includes("0")&&n<0?i=i.padStart(a,"0"):i=i.padStart(a," ")),i}function B(t,s){let r=t.includes("X"),e=t.match(/^%([- +#0']*)(\d*)(\.(\d*))?[xX]$/);if(!e)return S(t,s);let a=e[1]||"",n=e[2]?parseInt(e[2],10):0,o=e[3]!==void 0?e[4]?parseInt(e[4],10):0:-1,i=Math.abs(s).toString(16);r&&(i=i.toUpperCase()),o>=0&&(i=i.padStart(o,"0"));let u="";a.includes("#")&&s!==0&&(u=r?"0X":"0x");let c=u+i;return n>c.length&&(a.includes("-")?c=c.padEnd(n," "):a.includes("0")&&o<0?c=u+i.padStart(n-u.length,"0"):c=c.padStart(n," ")),c}function K(t){if(t==="")return"''";if(/^[a-zA-Z0-9_./-]+$/.test(t))return t;if(/[\x00-\x1f\x7f-\xff]/.test(t)){let e="$'";for(let a of t){let n=a.charCodeAt(0);a==="'"?e+="\\'":a==="\\"?e+="\\\\":a===`
`?e+="\\n":a==="	"?e+="\\t":a==="\r"?e+="\\r":a==="\x07"?e+="\\a":a==="\b"?e+="\\b":a==="\f"?e+="\\f":a==="\v"?e+="\\v":a==="\x1B"?e+="\\E":n<32||n>=127&&n<=255?e+=`\\${n.toString(8).padStart(3,"0")}`:a==='"'?e+='\\"':e+=a}return e+="'",e}let r="";for(let e of t)" 	|&;<>()$`\\\"'*?[#~=%!{}".includes(e)?r+=`\\${e}`:r+=e;return r}function ee(t,s){let r=t.match(/^%(-?)(\d*)(\.(\d*))?s$/);if(!r)return S(t.replace(/0+(?=\d)/,""),s);let e=r[1]==="-",a=r[2]?parseInt(r[2],10):0,n=r[3]!==void 0?r[4]?parseInt(r[4],10):0:-1,o=e?-a:a;return D(s,o,n)}function te(t,s){let r=K(s),e=t.match(/^%(-?)(\d*)q$/);if(!e)return r;let a=e[1]==="-",n=e[2]?parseInt(e[2],10):0,o=r;return n>o.length&&(a?o=o.padEnd(n," "):o=o.padStart(n," ")),o}function re(t,s,r){let e=t.match(/^%([- +#0']*)(\d*)(\.(\d*))?[eEfFgG]$/);if(!e)return S(t,r);let a=e[1]||"",n=e[2]?parseInt(e[2],10):0,o=e[3]!==void 0?e[4]?parseInt(e[4],10):0:6,i,u=s.toLowerCase();if(u==="e"?(i=r.toExponential(o),i=i.replace(/e([+-])(\d)$/,"e$10$2"),s==="E"&&(i=i.toUpperCase())):u==="f"?(i=r.toFixed(o),a.includes("#")&&o===0&&!i.includes(".")&&(i+=".")):u==="g"?(i=r.toPrecision(o||1),a.includes("#")||(i=i.replace(/\.?0+$/,""),i=i.replace(/\.?0+e/,"e")),i=i.replace(/e([+-])(\d)$/,"e$10$2"),s==="G"&&(i=i.toUpperCase())):i=r.toString(),r>=0&&(a.includes("+")?i=`+${i}`:a.includes(" ")&&(i=` ${i}`)),n>i.length)if(a.includes("-"))i=i.padEnd(n," ");else if(a.includes("0")){let c=i.match(/^[+ -]/)?.[0]||"",l=c?i.slice(1):i;i=c+l.padStart(n-c.length,"0")}else i=i.padStart(n," ");return i}function ne(t){let s="",r=0;for(;r<t.length;)if(t[r]==="\\"&&r+1<t.length)switch(t[r+1]){case"n":s+=`
`,r+=2;break;case"t":s+="	",r+=2;break;case"r":s+="\r",r+=2;break;case"\\":s+="\\",r+=2;break;case"a":s+="\x07",r+=2;break;case"b":s+="\b",r+=2;break;case"f":s+="\f",r+=2;break;case"v":s+="\v",r+=2;break;case"c":return{value:s,stopped:!0};case"x":{let a=[],n=r;for(;n+1<t.length&&t[n]==="\\"&&t[n+1]==="x";){let o="",i=n+2;for(;i<t.length&&i<n+4&&/[0-9a-fA-F]/.test(t[i]);)o+=t[i],i++;if(o)a.push(parseInt(o,16)),n=i;else break}a.length>0?(s+=V(a),r=n):(s+="\\x",r+=2);break}case"u":{let a="",n=r+2;for(;n<t.length&&n<r+6&&/[0-9a-fA-F]/.test(t[n]);)a+=t[n],n++;a?(s+=String.fromCodePoint(parseInt(a,16)),r=n):(s+="\\u",r+=2);break}case"0":{let a="",n=r+2;for(;n<t.length&&n<r+5&&/[0-7]/.test(t[n]);)a+=t[n],n++;a?s+=String.fromCharCode(parseInt(a,8)):s+="\0",r=n;break}case"1":case"2":case"3":case"4":case"5":case"6":case"7":{let a="",n=r+1;for(;n<t.length&&n<r+4&&/[0-7]/.test(t[n]);)a+=t[n],n++;s+=String.fromCharCode(parseInt(a,8)),r=n;break}default:s+=t[r],r++}else s+=t[r],r++;return{value:s,stopped:!1}}export{le as printfCommand};
