import{createRequire} from"node:module";const require=createRequire(import.meta.url);
import{a as Z}from"./chunk-RBQG6ZUG.js";import{a as _,c as N}from"./chunk-27JIFWUR.js";import{k as A}from"./chunk-UO3VWIPS.js";import{a as z}from"./chunk-W5DWRFSU.js";import{a as F,b as W}from"./chunk-QAYAQNCG.js";import{sprintf as g}from"sprintf-js";function q(e){let s="",t=0;for(;t<e.length;){let r=e[t];if(r<128){s+=String.fromCharCode(r),t++;continue}if((r&224)===192){if(t+1<e.length&&(e[t+1]&192)===128&&r>=194){let i=(r&31)<<6|e[t+1]&63;s+=String.fromCharCode(i),t+=2;continue}s+=String.fromCharCode(r),t++;continue}if((r&240)===224){if(t+2<e.length&&(e[t+1]&192)===128&&(e[t+2]&192)===128){if(r===224&&e[t+1]<160){s+=String.fromCharCode(r),t++;continue}let i=(r&15)<<12|(e[t+1]&63)<<6|e[t+2]&63;if(i>=55296&&i<=57343){s+=String.fromCharCode(r),t++;continue}s+=String.fromCharCode(i),t+=3;continue}s+=String.fromCharCode(r),t++;continue}if((r&248)===240&&r<=244){if(t+3<e.length&&(e[t+1]&192)===128&&(e[t+2]&192)===128&&(e[t+3]&192)===128){if(r===240&&e[t+1]<144){s+=String.fromCharCode(r),t++;continue}let i=(r&7)<<18|(e[t+1]&63)<<12|(e[t+2]&63)<<6|e[t+3]&63;if(i>1114111){s+=String.fromCharCode(r),t++;continue}s+=String.fromCodePoint(i),t+=4;continue}s+=String.fromCharCode(r),t++;continue}s+=String.fromCharCode(r),t++}return s}var H={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)"]},re={name:"printf",async execute(e,s){if(W(e))return F(H);if(e.length===0)return{stdout:"",stderr:`printf: usage: printf format [arguments]
`,exitCode:2};let t=null,r=0;for(;r<e.length;){let l=e[r];if(l==="--"){r++;break}if(l==="-v"){if(r+1>=e.length)return{stdout:"",stderr:`printf: -v: option requires an argument
`,exitCode:1};if(t=e[r+1],!/^[a-zA-Z_][a-zA-Z0-9_]*(\[[a-zA-Z0-9_@*"'$]+\])?$/.test(t))return{stdout:"",stderr:`printf: \`${t}': not a valid identifier
`,exitCode:2};r+=2}else{if(l.startsWith("-")&&l!=="-")break;break}}if(r>=e.length)return{stdout:"",stderr:`printf: usage: printf format [arguments]
`,exitCode:1};let i=e[r],n=e.slice(r+1);try{let l=N(i),a="",f=0,o=!1,c="",E=s.env.get("TZ"),h=s.limits?.maxStringLength;do{let{result:m,argsConsumed:v,error:u,errMsg:C,stopped:S}=L(l,n,f,E);if(a+=m,h!==void 0&&h>0&&a.length>h)throw new A(`printf: output size limit exceeded (${h} bytes)`,"string_length");if(f+=v,u&&(o=!0,C&&(c=C)),S)break}while(f<n.length&&f>0);if(f===0&&n.length>0,t){let m=t.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(['"]?)(.+?)\2\]$/);if(m){let v=m[1],u=m[3];u=u.replace(/\$([a-zA-Z_][a-zA-Z0-9_]*)/g,(C,S)=>s.env.get(S)??""),s.env.set(`${v}_${u}`,a)}else s.env.set(t,a);return{stdout:"",stderr:c,exitCode:o?1:0}}return{stdout:a,stderr:c,exitCode:o?1:0}}catch(l){if(l instanceof A)throw l;return{stdout:"",stderr:`printf: ${z(l)}
`,exitCode:1}}}};function L(e,s,t,r){let i="",n=0,l=0,a=!1,f="";for(;n<e.length;)if(e[n]==="%"&&n+1<e.length){let o=n;if(n++,e[n]==="%"){i+="%",n++;continue}let c=e.slice(o).match(/^%(-?\d*)(?:\.(\d+))?\(([^)]*)\)T/);if(c){let x=c[1]?parseInt(c[1],10):0,b=c[2]?parseInt(c[2],10):-1,R=c[3],U=c[0],w=s[t+l]||"";l++;let $;w===""||w==="-1"||w==="-2"?$=Math.floor(Date.now()/1e3):$=parseInt(w,10)||0;let p=Z(R,$,r);if(b>=0&&p.length>b&&(p=p.slice(0,b)),x!==0){let M=Math.abs(x);p.length<M&&(x<0?p=p.padEnd(M," "):p=p.padStart(M," "))}i+=p,n=o+U.length;continue}for(;n<e.length&&"+-0 #'".includes(e[n]);)n++;let E=!1;if(e[n]==="*")E=!0,n++;else for(;n<e.length&&/\d/.test(e[n]);)n++;let h=!1;if(e[n]===".")if(n++,e[n]==="*")h=!0,n++;else for(;n<e.length&&/\d/.test(e[n]);)n++;n<e.length&&"hlL".includes(e[n])&&n++;let m=e[n]||"";n++;let u=e.slice(o,n);if(E){let x=parseInt(s[t+l]||"0",10);l++,u=u.replace("*",String(x))}if(h){let x=parseInt(s[t+l]||"0",10);l++,u=u.replace(".*",`.${x}`)}let C=s[t+l]||"";l++;let{value:S,parseError:P,parseErrMsg:k,stopped:j}=X(u,m,C);if(i+=S,P&&(a=!0,k&&(f=k)),j)return{result:i,argsConsumed:l,error:a,errMsg:f,stopped:!0}}else i+=e[n],n++;return{result:i,argsConsumed:l,error:a,errMsg:f,stopped:!1}}function X(e,s,t){let r=!1,i="";switch(s){case"d":case"i":{let n=I(t);return r=d,r&&(i=`printf: ${t}: invalid number
`),{value:T(e,n),parseError:r,parseErrMsg:i}}case"o":{let n=I(t);return r=d,r&&(i=`printf: ${t}: invalid number
`),{value:D(e,n),parseError:r,parseErrMsg:i}}case"u":{let n=I(t);r=d,r&&(i=`printf: ${t}: invalid number
`);let l=n<0?n>>>0:n;return{value:T(e.replace("u","d"),l),parseError:r,parseErrMsg:i}}case"x":case"X":{let n=I(t);return r=d,r&&(i=`printf: ${t}: invalid number
`),{value:G(e,n),parseError:r,parseErrMsg:i}}case"e":case"E":case"f":case"F":case"g":case"G":{let n=parseFloat(t)||0;return{value:y(e,s,n),parseError:!1,parseErrMsg:""}}case"c":{if(t==="")return{value:"",parseError:!1,parseErrMsg:""};let a=new TextEncoder().encode(t)[0];return{value:String.fromCharCode(a),parseError:!1,parseErrMsg:""}}case"s":return{value:Q(e,t),parseError:!1,parseErrMsg:""};case"q":return{value:V(e,t),parseError:!1,parseErrMsg:""};case"b":{let n=B(t);return{value:n.value,parseError:!1,parseErrMsg:"",stopped:n.stopped}}default:try{return{value:g(e,t),parseError:!1,parseErrMsg:""}}catch{return{value:"",parseError:!0,parseErrMsg:`printf: [sprintf] unexpected placeholder
`}}}}var d=!1;function I(e){d=!1;let s=e.trimStart(),t=s!==s.trimEnd();if(e=s.trimEnd(),e.startsWith("'")&&e.length>=2||e.startsWith('"')&&e.length>=2)return e.charCodeAt(1);if(e.startsWith("\\'")&&e.length>=3||e.startsWith('\\"')&&e.length>=3)return e.charCodeAt(2);if(e.startsWith("+")&&(e=e.slice(1)),e.startsWith("0x")||e.startsWith("0X")){let r=parseInt(e,16);return Number.isNaN(r)?(d=!0,0):(t&&(d=!0),r)}if(e.startsWith("0")&&e.length>1&&/^-?0[0-7]+$/.test(e))return t&&(d=!0),parseInt(e,8)||0;if(/^\d+#/.test(e)){d=!0;let r=e.match(/^(\d+)#/);return r?parseInt(r[1],10):0}if(e!==""&&!/^-?\d+$/.test(e)){d=!0;let r=parseInt(e,10);return Number.isNaN(r)?0:r}return t&&(d=!0),parseInt(e,10)||0}function T(e,s){let t=e.match(/^%([- +#0']*)(\d*)(\.(\d*))?[diu]$/);if(!t)return g(e.replace(/\.\d*/,""),s);let r=t[1]||"",i=t[2]?parseInt(t[2],10):0,n=t[3]!==void 0?t[4]?parseInt(t[4],10):0:-1,l=s<0,a=Math.abs(s),f=String(a);n>=0&&(f=f.padStart(n,"0"));let o="";l?o="-":r.includes("+")?o="+":r.includes(" ")&&(o=" ");let c=o+f;return i>c.length&&(r.includes("-")?c=c.padEnd(i," "):r.includes("0")&&n<0?c=o+f.padStart(i-o.length,"0"):c=c.padStart(i," ")),c}function D(e,s){let t=e.match(/^%([- +#0']*)(\d*)(\.(\d*))?o$/);if(!t)return g(e,s);let r=t[1]||"",i=t[2]?parseInt(t[2],10):0,n=t[3]!==void 0?t[4]?parseInt(t[4],10):0:-1,l=Math.abs(s).toString(8);n>=0&&(l=l.padStart(n,"0")),r.includes("#")&&!l.startsWith("0")&&(l=`0${l}`);let a=l;return i>a.length&&(r.includes("-")?a=a.padEnd(i," "):r.includes("0")&&n<0?a=a.padStart(i,"0"):a=a.padStart(i," ")),a}function G(e,s){let t=e.includes("X"),r=e.match(/^%([- +#0']*)(\d*)(\.(\d*))?[xX]$/);if(!r)return g(e,s);let i=r[1]||"",n=r[2]?parseInt(r[2],10):0,l=r[3]!==void 0?r[4]?parseInt(r[4],10):0:-1,a=Math.abs(s).toString(16);t&&(a=a.toUpperCase()),l>=0&&(a=a.padStart(l,"0"));let f="";i.includes("#")&&s!==0&&(f=t?"0X":"0x");let o=f+a;return n>o.length&&(i.includes("-")?o=o.padEnd(n," "):i.includes("0")&&l<0?o=f+a.padStart(n-f.length,"0"):o=o.padStart(n," ")),o}function O(e){if(e==="")return"''";if(/^[a-zA-Z0-9_./-]+$/.test(e))return e;if(/[\x00-\x1f\x7f-\xff]/.test(e)){let r="$'";for(let i of e){let n=i.charCodeAt(0);i==="'"?r+="\\'":i==="\\"?r+="\\\\":i===`
`?r+="\\n":i==="	"?r+="\\t":i==="\r"?r+="\\r":i==="\x07"?r+="\\a":i==="\b"?r+="\\b":i==="\f"?r+="\\f":i==="\v"?r+="\\v":i==="\x1B"?r+="\\E":n<32||n>=127&&n<=255?r+=`\\${n.toString(8).padStart(3,"0")}`:i==='"'?r+='\\"':r+=i}return r+="'",r}let t="";for(let r of e)" 	|&;<>()$`\\\"'*?[#~=%!{}".includes(r)?t+=`\\${r}`:t+=r;return t}function Q(e,s){let t=e.match(/^%(-?)(\d*)(\.(\d*))?s$/);if(!t)return g(e.replace(/0+(?=\d)/,""),s);let r=t[1]==="-",i=t[2]?parseInt(t[2],10):0,n=t[3]!==void 0?t[4]?parseInt(t[4],10):0:-1,l=r?-i:i;return _(s,l,n)}function V(e,s){let t=O(s),r=e.match(/^%(-?)(\d*)q$/);if(!r)return t;let i=r[1]==="-",n=r[2]?parseInt(r[2],10):0,l=t;return n>l.length&&(i?l=l.padEnd(n," "):l=l.padStart(n," ")),l}function y(e,s,t){let r=e.match(/^%([- +#0']*)(\d*)(\.(\d*))?[eEfFgG]$/);if(!r)return g(e,t);let i=r[1]||"",n=r[2]?parseInt(r[2],10):0,l=r[3]!==void 0?r[4]?parseInt(r[4],10):0:6,a,f=s.toLowerCase();if(f==="e"?(a=t.toExponential(l),a=a.replace(/e([+-])(\d)$/,"e$10$2"),s==="E"&&(a=a.toUpperCase())):f==="f"?(a=t.toFixed(l),i.includes("#")&&l===0&&!a.includes(".")&&(a+=".")):f==="g"?(a=t.toPrecision(l||1),i.includes("#")||(a=a.replace(/\.?0+$/,""),a=a.replace(/\.?0+e/,"e")),a=a.replace(/e([+-])(\d)$/,"e$10$2"),s==="G"&&(a=a.toUpperCase())):a=t.toString(),t>=0&&(i.includes("+")?a=`+${a}`:i.includes(" ")&&(a=` ${a}`)),n>a.length)if(i.includes("-"))a=a.padEnd(n," ");else if(i.includes("0")){let o=a.match(/^[+ -]/)?.[0]||"",c=o?a.slice(1):a;a=o+c.padStart(n-o.length,"0")}else a=a.padStart(n," ");return a}function B(e){let s="",t=0;for(;t<e.length;)if(e[t]==="\\"&&t+1<e.length)switch(e[t+1]){case"n":s+=`
`,t+=2;break;case"t":s+="	",t+=2;break;case"r":s+="\r",t+=2;break;case"\\":s+="\\",t+=2;break;case"a":s+="\x07",t+=2;break;case"b":s+="\b",t+=2;break;case"f":s+="\f",t+=2;break;case"v":s+="\v",t+=2;break;case"c":return{value:s,stopped:!0};case"x":{let i=[],n=t;for(;n+1<e.length&&e[n]==="\\"&&e[n+1]==="x";){let l="",a=n+2;for(;a<e.length&&a<n+4&&/[0-9a-fA-F]/.test(e[a]);)l+=e[a],a++;if(l)i.push(parseInt(l,16)),n=a;else break}i.length>0?(s+=q(i),t=n):(s+="\\x",t+=2);break}case"u":{let i="",n=t+2;for(;n<e.length&&n<t+6&&/[0-9a-fA-F]/.test(e[n]);)i+=e[n],n++;i?(s+=String.fromCodePoint(parseInt(i,16)),t=n):(s+="\\u",t+=2);break}case"0":{let i="",n=t+2;for(;n<e.length&&n<t+5&&/[0-7]/.test(e[n]);)i+=e[n],n++;i?s+=String.fromCharCode(parseInt(i,8)):s+="\0",t=n;break}case"1":case"2":case"3":case"4":case"5":case"6":case"7":{let i="",n=t+1;for(;n<e.length&&n<t+4&&/[0-7]/.test(e[n]);)i+=e[n],n++;s+=String.fromCharCode(parseInt(i,8)),t=n;break}default:s+=e[t],t++}else s+=e[t],t++;return{value:s,stopped:!1}}var ie={name:"printf",flags:[{flag:"-v",type:"value",valueHint:"string"}],stdinType:"none",needsArgs:!0};export{re as a,ie as b};
