0

This is a practical question. Could you write a program in C (or C++) that would save its entire source file text to a string/char array, to be used as needed? Not writing the code specifically to be a quine, but just output what already exists. Obviously this means your code's verbosity would end up determining the size of your string, but it could be useful in niche cases.

For example, I have made a standalone ad-hoc piece of lab equipment using an Arduino. I have included the paper hardware schematics in the box, but I would like to include the code itself too, so people going forward could modify and debug as needed.

The obvious answer is to print it to paper, but this means if I make a small change to the code, it would not be reflected, and they whoever works on it down the line would have to manually type it in, leaving room for mistakes.

I searched for something like this already and have come up dry. If I missed it, I apologize.

EDIT: I realize some of the constraints I am dealing with are not mentioned here.

  1. Our organization does not allow flash drives or USB storage devices (blocked on computer level), so that is not an option unfortunately.

  2. GitHub is another good option for this; I could simply put a permalink to the page inside. However, this means if someone finds it after I have left the company, they will have to fork it and start over if they want to modify.

  3. A feature of this is that you would by definition always display the version of the code that the device is running, rather than having to guess.

Again, this is an application with an Arduino driving the functions; the only potential digital output from the hardware itself is plaintext through the serial. It even already has a USB port on it.

  • 4
    *The obvious answer is to print it to paper, but this means if I make a small change to the code, it would not be reflected, and they whoever works on it down the line would have to manually type it in, leaving room for mistakes.* - The obvious answer is to distribute the source code on CD/SD/GitHub properly noting the version information. You *really* don't want to clutter you presumably functional firmware in a resource-restricted environment with the full code information. – Eugene Sh. Jul 06 '22 at 18:51
  • 3
    The obvious answer is to include source code as compressed package (.zip or .tar.gz probably) with the binary distribution. What is the problem with this approach? – hyde Jul 06 '22 at 19:05
  • C and C++ programs are typically distributed as binary-only, so at runtime they don't typically have access to their source code. You could certainly write a separate preprocessor program that would run on your development machine, that would load all of the source code into memory, then generate a resource file (or .cpp file, or whatnot) containing that data as a string for your other program to link to... but I don't know what the advantage would be over @hyde's suggestion. – Jeremy Friesner Jul 06 '22 at 19:30
  • 1
    _"The obvious answer is to print it to paper"_ ??? Really? You think that is the most obvious? Why not simply distribute the source code electronically or on removable media? Since a "quine" only function is to output its own source, you would be distributing code that did _nothing useful_. There are no plausible use cases where this would be useful. – Clifford Jul 06 '22 at 19:34
  • Worst case scenario, you can embed a global hardcoded string variable into your binary eg `volatile char *lpszSrc = ":@SOURCECODE@:This is the source code of this file.";` which would be "easily" extractable with basic binary inspection of your output file by memory inspection tools or by looping through the binary file searching for `:@SOURCECODE@:` in a sequence. – Dmytro Jul 06 '22 at 20:48
  • @CliffordYou are correct in that I misused the word "quine"; as you said, a quine's only function is to produce its own source code, while I want something that does that as well as accomplishing other things, and outputs its source as well when asked. – mapplejacks Jul 06 '22 at 21:34
  • @JeremyFriesner the specific challenge here is that since the binary is not directly accessible from the Arduino hardware (at least not in any user-friendly way), I really only have program outputs and external sources. USB storage devices are not an option for security policy reasons, and nobody at this company has CD/DVD drives anymore, this essentially leaves a 3rd-party hosting site or my solution. I feel that for this particular case, this solution would be best for forward compatibility and guaranteeing what version of code is being run. – mapplejacks Jul 06 '22 at 21:44
  • 1
    @hyde the problem with this approach is that since the binary is not accessible from outside the microprocessor, the modes of output are limited essentially to serial plaintext output. – mapplejacks Jul 06 '22 at 21:46
  • 1
    @mapplejacks Right. So how would you initiate output of the source code from the device? – hyde Jul 07 '22 at 04:49
  • @hyde the simplest implementation I can think of is simply to dump it to terminal every time the device starts up. The terminal is not used for anything else, and the device is only ever hooked up to USB when its code is being modified. – mapplejacks Jul 07 '22 at 07:00
  • @mapplejacks Without knowing details, just dumping source code to console seems... agressive. – hyde Jul 07 '22 at 08:26
  • @hyde if you'd prefer, you could make a button just for that. However, given the fact that plugging the device into the computer and then opening a terminal to it is something you would not do by accident, it doesn't seem like an issue to me. Ultimately, is there a reason why this could not be done? All I've seen so far is reasons why people think it shouldn't. – mapplejacks Jul 08 '22 at 08:51
  • @mapplejacks I'm thinking it would make it very annoying in any debugging or troubleshooting scenario. Also having it explicitly triggered would allow use of for example XModem to transfer a .zip, and avoiding/detecting potential data corruption over serial (even if over USB that might not be a thing today). – hyde Jul 08 '22 at 10:26

2 Answers2

3

I would create a repository on github or gitlab or similar free repository hosting service with your source.

You can easily include the URL in the source and even include git describe as version string in the binary. You can create a QR code with the repository URL and print that on a piece of paper. If you have anything with a display you can print the QR code there too.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
  • Given my constraints, this is probably the most sensible answer. It will also force me to figure out how to use github, as well as anyone that has to modify this thing anyway. – mapplejacks Jul 06 '22 at 21:30
2

I have made a standalone ad-hoc piece of lab equipment using an Arduino. I have included the paper hardware schematics in the box, but I would like to include the code itself too, so people going forward could modify and debug as needed.

Concatenate (zip or tar and gzip) all your source files in a known format and then just embed the result in the executable. Implement a printing function that would print the data to the user in documented format. Work on a good build-system that will automate the process of packaging source files.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111