Home /Modules/operating-system/SLOSLoaderEmitter.sleela

SLOSLoaderEmitter.sleela

lib/operating-system/os-creator/SLOSLoaderEmitter.sleela · 172 line(s) · sleela

<< lib/operating-system/os-creator/SLOSLoaderEmitter.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines SLOSLoaderEmitter — the OS Creator(TM) emitter for the
     OS-loader piece: PID 1 and the service manager. Given an SLOSModel it
     produces the C and C++ source for the first user-space process the kernel
     starts, which brings up the model's service manager (systemd / Service
     Control Manager / launchd) and the services the SLServiceSet enables, then
     (on a desktop edition) starts the display stack. It emits a services.conf
     listing each unit and its mode (boot|ondemand).
   
   This is piece #6 of the chain (userspace services). The emitted C starts the
   named manager and iterates the model's boot-time units.
   
   Family: operating-system. OS Creator(TM) piece emitter; consumes SLOSModel, fills SLOSPiece. >>
#sleela 2.5
material;
class SLOSLoaderEmitter {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10929;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-10";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  SLOSPiece emit(SLOSModel m) {
    SLOSPiece p = new SLOSPiece();
    p.configure("OS Loader", "userspace/os-loader", "os_loader");

    String mgr = m.serviceSet().serviceManager();
    String units = m.serviceSet().unitManifest();
    int hasDesktop = 0;
    if (m.desktopSpec().hasDesktop()) { hasDesktop = 1; }

    String c = "/* os_loader.c - PID 1 / service manager, OS Creator(TM) for " + m.name() + " */\n";
    c = c + "#include <stdint.h>\n\n";
    c = c + "extern int start_manager(const char *manager);\n";
    c = c + "extern int start_service(const char *unit);\n";
    c = c + "extern int start_session(void);\n\n";
    c = c + "/* The kernel execs this as PID 1. Bring up the service manager. */\n";
    c = c + "int os_loader_main(void) {\n";
    c = c + "    if (start_manager(\"" + mgr + "\") != 0) return 1;\n";
    c = c + emitServiceStarts(units);
    if (hasDesktop == 1) {
      c = c + "    start_session();  /* hand off to the desktop GUI loader */\n";
    }
    c = c + "    for (;;) { /* reap children; PID 1 never exits */ }\n";
    c = c + "    return 0;\n";
    c = c + "}\n";
    p.setC(c);

    String cpp = "// os_loader.cpp - PID 1 / service manager (C++ surface) for " + m.name() + "\n";
    cpp = cpp + "#include <string>\n#include <vector>\nnamespace oscreator {\n";
    cpp = cpp + "class OsLoader {\npublic:\n";
    cpp = cpp + "  std::string manager = \"" + mgr + "\";\n";
    cpp = cpp + "  std::vector<std::string> bootUnits = {" + bootUnitList(units) + "};\n";
    cpp = cpp + "  bool desktop = " + boolLit(hasDesktop == 1) + ";\n";
    cpp = cpp + "  int run() {\n";
    cpp = cpp + "    if (!startManager(manager)) return 1;\n";
    cpp = cpp + "    for (auto &u : bootUnits) startService(u);\n";
    if (hasDesktop == 1) { cpp = cpp + "    startSession();\n"; }
    cpp = cpp + "    supervise();\n    return 0;\n  }\n";
    cpp = cpp + "private:\n  bool startManager(const std::string&);\n  void startService(const std::string&);\n";
    cpp = cpp + "  void startSession();\n  void supervise();\n";
    cpp = cpp + "};\n} // namespace oscreator\n";
    p.setCpp(cpp);

    String conf = "# services.conf - generated by OS Creator(TM)\n";
    conf = conf + "manager = " + mgr + "\n";
    conf = conf + "# format: <unit> <mode>\n";
    conf = conf + units + "\n";
    p.setConfig("services.conf", conf);

    p.setReadme("# OS Loader piece\n\nPID 1 for `" + mgr + "` (`os_loader.c`/`os_loader.cpp`): "
      + "start the service manager, bring up boot-time units from `services.conf`, then hand off "
      + "to the desktop GUI loader (`../../desktop/gui-loader`) on a desktop edition.\n");
    return p;
  }

  < One start_service() call per boot-mode unit in the manifest. >
  String emitServiceStarts(String units) {
    String out = "";
    String line = "";
    int i = 0;
    int n = strLen(units);
    while (i <= n) {
      String ch = charAt(units, i);
      if (ch == "\n" || i == n) {
        if (modeOf(line) == "boot") {
          out = out + "    start_service(\"" + nameOf(line) + "\");\n";
        }
        line = "";
      } else {
        line = line + ch;
      }
      i = i + 1;
    }
    return out;
  }

  String bootUnitList(String units) {
    String out = "";
    String line = "";
    int i = 0;
    int n = strLen(units);
    int first = 1;
    while (i <= n) {
      String ch = charAt(units, i);
      if (ch == "\n" || i == n) {
        if (modeOf(line) == "boot") {
          if (first == 1) { out = out + "\"" + nameOf(line) + "\""; first = 0; }
          else { out = out + ", \"" + nameOf(line) + "\""; }
        }
        line = "";
      } else {
        line = line + ch;
      }
      i = i + 1;
    }
    return out;
  }

  String nameOf(String line) {
    int i = 0; int n = strLen(line);
    while (i < n) { if (charAt(line, i) == " ") { return substr(line, 0, i); } i = i + 1; }
    return line;
  }
  String modeOf(String line) {
    int i = 0; int n = strLen(line);
    while (i < n) { if (charAt(line, i) == " ") { return substr(line, i + 1, n); } i = i + 1; }
    return "";
  }

  int strLen(String s) { return native sleela_str_length(s); }
  String substr(String s, int a, int b) { return native sleela_str_substring(s, a, b); }
  String charAt(String s, int i) { return substr(s, i, i + 1); }
  String boolLit(boolean b) { if (b) { return "true"; } return "false"; }
}