{"id":15497247,"url":"https://github.com/jwerle/repl.h","last_synced_at":"2025-07-23T23:04:22.246Z","repository":{"id":11157394,"uuid":"13528427","full_name":"jwerle/repl.h","owner":"jwerle","description":"Create a repl with eval/print/error hooks with given stdin, stdout, and stderr streams","archived":false,"fork":false,"pushed_at":"2014-01-06T01:11:38.000Z","size":227,"stargazers_count":9,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-22T21:51:31.214Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwerle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-10-12T20:21:24.000Z","updated_at":"2025-03-06T03:38:03.000Z","dependencies_parsed_at":"2022-08-28T09:13:00.440Z","dependency_job_id":null,"html_url":"https://github.com/jwerle/repl.h","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jwerle/repl.h","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Frepl.h","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Frepl.h/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Frepl.h/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Frepl.h/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwerle","download_url":"https://codeload.github.com/jwerle/repl.h/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Frepl.h/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266764585,"owners_count":23980587,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T08:32:16.830Z","updated_at":"2025-07-23T23:04:22.229Z","avatar_url":"https://github.com/jwerle.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"repl.h\n======\n\nCreate a repl with eval/print/error hooks with given stdin, stdout, and stderr streams\n\n## install\n\n```sh\n$ clib install jwerle/repl.h\n```\n\n## usage\n\nThe `repl` interface is more or less just an api for defining functions\nthat answer the \"eval\", and \"print\" parts of a REPL routine. The \"read\"\npart is just a read from a defined \"stdin\" stream that passes a buffer\nto the defined \"eval\" function which returns a `char *` that is passed\nto the defined \"print\" function. The print function if not defined will\nprint to the defined \"stdout\" stream.\n\n\nA simple math repl can be constructed like this:\n\n```c\n\n#include \u003crepl.h\u003e\n\nrepl_session_opts opts;\n\nstatic char *\neval (repl_session_t *sess, char *buf);\n\nstatic void\nprint (repl_session_t *sess, char *buf);\n\nstatic void\nerror (repl_session_t *sess, char *err);\n\nint\nmain (void) {\n  int rc;\n  // opts\n  opts.prompt = \"math\u003e\";\n  opts.eval_cb = eval;\n  opts.print_cb = print;\n  opts.error_cb = error;\n\n  // initiate\n  repl_session_t *sess = repl_session_new(\u0026opts);\n\n  // start\n  rc = repl_session_start(sess);\n\n  // destroy\n  repl_session_destroy(sess);\n  printf(\"\\n\");\n  return (-1 == rc || 0 == rc)? 0 : rc;\n}\n\n\nstatic char *\neval (repl_session_t *sess, char *buf) {\n  // got nothing\n  if (feof(sess-\u003estdin)) {\n    sess-\u003erc = 0;\n    return NULL;\n  }\n\n  char op[1];\n  char *result = NULL;\n  char tmp[4096];\n  int lval = 0;\n  int rval = 0;\n\n  sscanf(buf, \"%d %s %d\", \u0026lval, op, \u0026rval);\n\n  if (NULL == \u0026lval) {\n    sess-\u003erc = 1;\n    return repl_session_set_error(\"Missing left operand\\n\");\n  } else if (NULL == \u0026rval) {\n    sess-\u003erc = 1;\n    return repl_session_set_error(\"Missing right operand\\n\");\n  } if (NULL == op) {\n    sess-\u003erc = 1;\n    return repl_session_set_error(\"Missing operator\\n\");\n  }\n\n  switch (op[0]) {\n    case '+':\n      sprintf(tmp, \"%d\", (lval + rval));\n      break;\n\n    case '-':\n      sprintf(tmp, \"%d\", (lval - rval));\n      break;\n    \n    case '*':\n      sprintf(tmp, \"%d\", (lval * rval));\n      break;\n    \n    case '/':\n      sprintf(tmp, \"%d\", (lval / rval));\n      break;\n    \n    case '%':\n      sprintf(tmp, \"%d\", (lval % rval));\n      break;\n\n    default:\n      sess-\u003erc = 1;\n      return repl_session_set_error(\"Invalid operator\\n\");\n  }\n\n  if (NULL == tmp) return NULL;\n\n  result = tmp;\n  result[strlen(tmp)] = '\\0';\n\n  return result;\n}\n\nstatic void\nprint (repl_session_t *sess, char *buf) {\n  fprintf(sess-\u003estdout, \"%s\\n\", buf);\n  repl_loop(sess);\n}\n\nstatic void\nerror (repl_session_t *sess, char *err) {\n  fprintf(sess-\u003estderr, \"error: '%s'\\n\", err);\n  repl_loop(sess);\n}\n\n```\n\n## license\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwerle%2Frepl.h","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwerle%2Frepl.h","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwerle%2Frepl.h/lists"}