[Question]
  [
You must write a program or function that, when given a *nonempty* string **S** of **N** printable ASCII characters†, outputs a program that will exit with exit code **C**, where **C** is the ASCII codepoint at position 0 in **S**. This program you write will additionally output a program **P**, such that, when run, it exits with exit code **C′**, where **C′** is the ASCII codepoint at position 1 in **S**. Program **P** will output another program **P′**. This process repeats until there are no characters left in **S**. After this is done, you must output nothing, followed by an optional newline; and should exit with exit code 0.
†The characters between `0x20` and `0x7e` inclusive.
Some more rules:
* Self-modifying programs are not allowed: you must output the source to STDOUT (or, return value initially)
* You may not read your own source code.
The shortest such program in bytes will win.
For some rudimentary testing, [this ruby script](https://gist.github.com/ConorOBrien-Foxx/033e6d3e0c3755aea04fc02d18ecb15c) can be used. (The first argument is the way you invoke the script, the second is the program, and the third is the input string.)
## Hypothetical Example
Say the program is `FOO`. When given the string "ABC", it outputs `BARA`. This program exits with code `65` and outputs `BARB`. This in turn exits with code `66` and ouputs `BARC`. This program exits with code `67` and outputs `BAR!`. This outputs nothing, and exits with code `0`.
  
[Answer]
# Python 2, ~~126~~ ~~101~~ 94 bytes
In the process of making this, I found that Python code may not contain literal NUL bytes.
```
lambda i,s='''i=%r;s=%r
try:print s%%(i[1:],s,i[0])
except:0
exit(ord(%r))''':s%(i[1:],s,i[0])
```
[**Try it online**](https://tio.run/nexus/python2#S7ON@Z@TmJuUkqiQqVNsq66unmmrWmRdDCS4SooqrQqKMvNKFIpVVTUyow2tYnWKdTKjDWI1uVIrklMLSqwMgIzMEo38ohQN1SJNTaB2q2I0pf8hRqRpZOYVlJZoaGr@V/dIzcnJVwcA) *(shows the exit code in the Debug info)*
---
Note that each of the non-empty programs below has a trailing linefeed.
For input `Hello`, the above outputs:
```
i='ello';s='i=%r;s=%r\ntry:print s%%(i[1:],s,i[0])\nexcept:print s%%(0,s,"\\0")*(i>0)\nexit(ord(%r))'
try:print s%(i[1:],s,i[0])
except:0
exit(ord('H'))
```
which prints
...
which prints
```
i='o';s='i=%r;s=%r\ntry:print s%%(i[1:],s,i[0])\nexcept:print s%%(0,s,"\\0")*(i>0)\nexit(ord(%r))'
try:print s%(i[1:],s,i[0])
except:0
exit(ord('l'))
```
which prints
```
i='';s='i=%r;s=%r\ntry:print s%%(i[1:],s,i[0])\nexcept:print s%%(0,s,"\\0")*(i>0)\nexit(ord(%r))'
try:print s%(i[1:],s,i[0])
except:0
exit(ord('o'))
```
which prints nothing (the empty program)
which prints nothing and exits with code 0.
[Answer]
# Python 3, 77 bytes
```
p='exit(0)'
for c in input()[::-1]:p='print(%r);exit(ord(%r))'%(p,c)
print(p)
```
This code takes input from STDIN and outputs the first program to STDOUT.
If the input is `ABCDE`, the results are
```
 0 print('print(\'print(\\\'print("print(\\\\\\\'exit(0)\\\\\\\');exit(ord(\\\\\\\'E\\\\\\\'))");exit(ord(\\\\\\\'D\\\\\\\'))\\\');exit(ord(\\\'C\\\'))\');exit(ord(\'B\'))');exit(ord('A'))
65 print('print(\'print("print(\\\'exit(0)\\\');exit(ord(\\\'E\\\'))");exit(ord(\\\'D\\\'))\');exit(ord(\'C\'))');exit(ord('B'))
66 print('print("print(\'exit(0)\');exit(ord(\'E\'))");exit(ord(\'D\'))');exit(ord('C'))
67 print("print('exit(0)');exit(ord('E'))");exit(ord('D'))
68 print('exit(0)');exit(ord('E'))
69 exit(0)
 0 
```
where each line contains the exit code and the output of the previously executed program (the first line is the first program).
[Answer]
# [Python 3](https://docs.python.org/3/), ~~115~~ ~~108~~ 100 bytes
```
i=input()
r="%s"
k=""
for c in i:r%="print(%s\"%%s%s\");exit(%i)"%(k,k,ord(c));k+=k+"\\"
print(r%"")
```
[Try it online!](https://tio.run/nexus/python3#JctBCoQwDEbhfU9Rfgg06AmU7D1Ed45CiLSSKnj7OsOsHnzwuoqW874SBxdQQzABwl49rlFL1MlJcLqWK1HLIGq/8Lw9@hVlULLRxuqftDLPNogNyBnh/zgB3PuyHUd9AQ "Python 3 – TIO Nexus")
---
For the input `Hello`, the program prints:
```
print("print(\"print(\\\"print(\\\\\\\"print(\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\");exit(111)\\\\\\\");exit(108)\\\");exit(108)\");exit(101)");exit(72)
```
---
The program above prints:
```
print("print(\"print(\\\"print(\\\\\\\"\\\\\\\");exit(111)\\\");exit(108)\");exit(108)");exit(101)
```
and exits with code `72`.
[Try it online!](https://tio.run/nexus/python3#@19QlJlXoqEEoWJgdAwSKwaDBwNK6HxN69SKzBINQ0NDTXQhAwtNdC6CY6gJY5sbaf7/DwA)
---
The program above prints
```
print("print(\"print(\\\"\\\");exit(111)\");exit(108)");exit(108)
```
and exits with code `101`.
[Try it online!](https://tio.run/nexus/python3#@19QlJlXoqEEoWJgdAwSCwSUYLSmdWpFZomGoaGhJjLXwEIThYNgG2r@/w8A)
---
The program above prints:
```
print("print(\"\");exit(111)");exit(108)
```
and exits with code `108`.
[Try it online!](https://tio.run/nexus/python3#@19QlJlXoqEEoWJgdEyMEghrWqdWZJZoGBoaaiI4BhaayOz//wE)
---
The program above prints:
```
print("");exit(111)
```
and exits with code `108`.
[Try it online!](https://tio.run/nexus/python3#@19QlJlXoqEEoWKUYpQ0rVMrMks0DA0NNeFsAwvN//8B)
---
The program above prints nothing and exits with code `111`.
[Try it online!](https://tio.run/nexus/python3#@19QlJlXoqGkpGmdWpFZomFoaKj5/z8A)
---
The empty program prints nothing and exits with code `0`.
[Try it online!](https://tio.run/nexus/python3)
[Answer]
# C, 156 bytes
```
char*s="char*s=%c%s%c;n=%d;char*t=%c%s%c;main(){n<strlen(t)&&printf(s,34,s,34,n+1,34,t,34);return t[n];}";main(i,t)char**t;{printf(s,34,s,34,0,34,t[1],34);}
```
[Try it online!](https://tio.run/nexus/c-gcc#@5@ckVikVWyrBKVVk1WLVZOt82xVU6zBQiUwodzEzDwNzeo8m@KSopzUPI0STTW1gqLMvJI0jWIdYxMdMJGnbQiiSoCEpnVRaklpUZ5CSXRerHWtEsSATJ0STbC5WiXW1RjaDcCaow1jwfpr////7@jkDAA) (Open the debug-tab to see the exit code.)
Takes input as a command line argument.
For input "ABC", this outputs the program
```
char*s="char*s=%c%s%c;n=%d;char*t=%c%s%c;main(){n<strlen(t)&&printf(s,34,s,34,n+1,34,t,34);return t[n];}";n=0;char*t="ABC";main(){n<strlen(t)&&printf(s,34,s,34,n+1,34,t,34);return t[n];}
```
which returns 65 and outputs
```
char*s="char*s=%c%s%c;n=%d;char*t=%c%s%c;main(){n<strlen(t)&&printf(s,34,s,34,n+1,34,t,34);return t[n];}";n=1;char*t="ABC";main(){n<strlen(t)&&printf(s,34,s,34,n+1,34,t,34);return t[n];}
```
which returns 66 and outputs
```
char*s="char*s=%c%s%c;n=%d;char*t=%c%s%c;main(){n<strlen(t)&&printf(s,34,s,34,n+1,34,t,34);return t[n];}";n=2;char*t="ABC";main(){n<strlen(t)&&printf(s,34,s,34,n+1,34,t,34);return t[n];}
```
which returns 67 and outputs
```
char*s="char*s=%c%s%c;n=%d;char*t=%c%s%c;main(){n<strlen(t)&&printf(s,34,s,34,n+1,34,t,34);return t[n];}";n=3;char*t="ABC";main(){n<strlen(t)&&printf(s,34,s,34,n+1,34,t,34);return t[n];}
```
which outputs nothing and returns 0.
[Answer]
# Python 2, 67 bytes
Based on [this answer](https://codegolf.stackexchange.com/a/118302/34718), but modified to use Python 2, with a trivial program `0` to print nothing and exit.
```
p=0
for c in input()[::-1]:p='print %r;exit(ord(%r))'%(p,c)
print p
```
[**Try it online**](https://tio.run/nexus/python2#@19ga8CVll@kkKyQmQdEBaUlGprRVla6hrFWBbbqBUWZeSUKqkXWqRWZJRr5RSkaqkWamuqqGgU6yZpcENmC///VPVJzcvLVAQ)
[Answer]
# RPL, 73 bytes
With hp8 code page.
Turn on your HP48 or similar, or fire [droid48](https://play.google.com/store/apps/details?id=org.ab.x48). Do not forget to `-52 SF` for better visualization of the stack.
I assume you have already pushed the string, e.g. `"ABC"`, on the stack. Then key in the following function:
```
→ x«{LAST}x{DUP NUM 3ROLLD 2OVER SIZE DUP{SUB 2SWAP PUT}{4DROPN}IFTE}+ +»
```
(For convenience I suggest to push the α key twice before typing anything, hence locking alpha input mode. Latter just use the DEL key to cancel the automatically inserted closing delimiters. Simply use the ENTER key to validate. Do not forget the space after the stab operator.)
This function immediately pushes on the stack a self-modifying program, under the form of a list. (But the function above does not modify itself). As the L in RPL originally stands for LISP, pressing the EVAL key will indeed evaluate this program. It returns the exit code on stack level two, and leaves itself, modified (yes, *here* it is questionable), for a latter EVAL. So press EVAL repeatedly until the program finally stops to drop itself on stack level one. The final exit code 0 thus appears on level one, with past exit codes above. If you forgot to `-52 SF`, you can navigate in the stack after each EVAL by pressing the ▴ key (leave this navigation mode with the ON key). The above function does accept strings with 0x0 chars inside, to create such strings `0 CHR` and `+` are your friends. The self modification consists of removing the used char from the embedded string (the `SUB 2 SWAP PUT` branch). Hence the dropped program is shorter after each EVAL. The `4 DROPN` branch ensures the *output nothing* instruction from the OP is respected, dropping among others the program itself. Of course all this assumes your note a `-55 SF` adept. Users of `-55 SF` shall be banned. Forever.
I assume an RPL/2 solution exists, and could feature a real unix exit code, but afaik RPL/2 has limited introspection, and cannot evaluate lists.
[Answer]
# [sed](https://www.gnu.org/software/sed/), ~~467~~ 461 bytes
Character codes are hard:
```
s:^:Y:
:b
s:ZY[ (2<FPZdnx]:0_Y:
s:ZY[ )3=GQ[eoy]:1_Y:
s:ZY[ *4>HR\fpz]:2_Y:
s:ZY[]!+5?ISgq{]:3_Y:
s:ZY[",6@JT^hr|]:4_Y:
s:ZY[-#7AKU_is}]:5_Y:
s:ZY[$.8BLV`jt~]:6_Y:
s:ZY[%/9CMWaku]:7_Y:
s:ZY[&0:DNXblv]:8_Y:
s:ZY['1;EOYcmw]:9_Y:
s:Y[ -']:3Z&:
s:Y[(-1]:4Z&:
s:Y[2-9:;]:5Z&:
s:Y[<=>?@A-E]:6Z&:
s:Y[F-O]:7Z&:
s:Y[P-Y]:8Z&:
s:Y[]Z\-`abc]:9Z&:
s:Y[d-m]:10Z&:
s:Y[n-w]:11Z&:
s:Y[xyz{-~]:12Z&:
tb
s/([^_]+)_Y$/ q\1/
:
s/[/\]/\\&/g
s/([^_]+)_ (.*)/ s\/^\/\2\/;q\1/
/^\S/b
```
[Try it online!](https://tio.run/nexus/sed#TZDZVsIwFEXf@YqqWAoYQst8mVVwFhSntkkLBQQUKFBUBvXTrVFWqo9756x1Tq7rgAEq@MDyOaCpuiApuWpd64wXFKIme9jYYCx/dKV37SUF@c@G4oXja/I4WVFQPEu3woniSaM3XVOIeXZ7L1k6vTH6s3cKcc@inVT57NYcOB8UEp71R9L753fNp/knhaRnd3Hm4OK@9fxCIeVJMQqHlw/W8JVC2pMBOVupqe3RG4XMRrKtKMDmaOKGJCSzGZwUlIEsG8A5ly8US2VUYe1cVVGN1XKqI5X1caIaQc2W1WZtXHXQiB0qynGM2BRZ5rhYrtaI/U1WfsycXR5LumHScNBU/ViYEhn7WBLrmFBMiIh7/xKCFAkFseAQbBBMFIKzv3lGDWy5br87HNpf9mQ@sMeOi2bf "sed – TIO Nexus")
Otherwise, the logic is rather straightforward: (1) escape special characters (there're two), (2) wrap in an additional `s/^/…/;q\1` layer, (3) repeat.
Here's the output for `hello`:
```
 s/^/s\/^\/s\\\/^\\\/s\\\\\\\/^\\\\\\\/q111\\\\\\\/;q108\\\/;q108\/;q101/;q104
```
And a little script that I used:
```
#!/bin/bash
set -uo pipefail
IFS=$'\n'
P=$(echo $1 | sed -rf q.sed)
echo $P
echo $1 | od -An -tuC
for char in $(echo $1 | sed 's:.:&\n:g'); do
    P=$(echo | sed $P)
    printf ' %3d' $?
done
```
[Answer]
## PowerShell, 172 156 bytes.
```
param($i)
$s=@'
if($i='{0}'){{
$s=@'
{1}'@
$s-f($i-replace'^.'-replace"'","''"),"$s`n"
$host.setshouldexit($i[0])
}}
exit
'@
$s-f$i.replace("'","''"),"$s`n"
```
The `h3l}'{l0` input will result the next output
[Try it online!](https://tio.run/nexus/powershell#XYzBCsMgEETv@xmLsBESsfQcyH@UlkhrUbBNiBYKst9uDNhLb/NmmFdWs5lXJ7wEEceJwD8rjJQ1k8y5lfnENNU8HOOw2TWYu6Wbol9Gwh6JUPYo4vxGEG6JSUWbols@4WG/PtXrRV8lMMOB0IzCqybp/i2lFHTnwJSDxh0 "PowerShell – TIO Nexus")
```
if($i='h3l}''{l0'){
$s=@'
if($i='{0}'){{
$s=@'
{1}'@
$s-f($i-replace'^.'-replace"'","''"),"$s`n"
$host.setshouldexit($i[0])
}}
exit
'@
$s-f($i-replace'^.'-replace"'","''"),"$s`n"
$host.setshouldexit($i[0])
}
exit
```
Which in the own turn will output
[Try it online!](https://tio.run/nexus/powershell#rYxRCsIwEET/9xhLYAy0JeJ3ofcQRdFIAsGKG0EIe/aYir2Bf2/eMFPjbWPiiLBLCpTkYAsZGSfQrylOm1tl2Sqmxv1S9k//SOeLx3HAygzuGGDbsZHTncmEWfIgPkuYX@nq3zG36d4dLKnSEumPj9/DWj8 "PowerShell – TIO Nexus")
```
if($i='3l}''{l0'){
$s=@'
if($i='{0}'){{
$s=@'
{1}'@
$s-f($i-replace'^.'-replace"'","''"),"$s`n"
$host.setshouldexit($i[0])
}}
exit
'@
$s-f($i-replace'^.'-replace"'","''"),"$s`n"
$host.setshouldexit($i[0])
}
exit
```
The last run will output nothing and exit code will be 0.
[Try it online!](https://tio.run/nexus/powershell#@5@ZpqGSaauurlnNpVJs66DOBRWoNqgFisEEqw1r1R2AbF2QpG5RakFOYnKqepyeOoytpK6ko6SurqSpo6RSnJCnxKWSkV9colecWlKckV@ak5JakVkC1BptEKvJVVvLBeJyUdFEsIH//wMA "PowerShell – TIO Nexus")
```
if($i=''){
$s=@'
if($i='{0}'){{
$s=@'
{1}'@
$s-f($i-replace'^.'-replace"'","''"),"$s`n"
$host.setshouldexit($i[0])
}}
exit
'@
$s-f($i-replace'^.'-replace"'","''"),"$s`n"
$host.setshouldexit($i[0])
}
exit
```
]