/** * @file websh.c * @author Tobias Eidelpes * @date 2018-04-16 * * @brief Formats program output for the web. */ #include #include #include #include #include #include #include static const char *pname; static int eFlag = 0; static int hFlag = 0; static int sFlag = 0; static const char *word; static const char *tag; volatile sig_atomic_t quit = 0; static void handle_signal(int signal) { quit = 1; } void usage(void) { fprintf(stderr, "Usage: websh [-e] [-h] [-s WORD:TAG]\n"); exit(EXIT_FAILURE); } void parsesArg(char *sArg) { char *token; int i = 0; while ((token = strsep(&sArg, ":"))) { if (strcmp(token, "") == 0) usage(); if (i == 2) usage(); if (i == 0) { word = token; } else { tag = token; } i++; } } void parse(int argc, char *argv[]) { int optInd = 0; char *sArg; while ((optInd = getopt(argc, argv, "ehs:")) != -1) { switch (optInd) { case 'e': if (eFlag != 0) { usage(); } else { eFlag = 1; } break; case 'h': if (hFlag != 0) { usage(); } else { hFlag = 1; } break; case 's': if (sFlag != 0) { usage(); } else { sFlag = 1; sArg = optarg; parsesArg(sArg); } break; case '?': usage(); default: assert(0); } } pname = argv[0]; } int main(int argc, char *argv[]) { parse(argc, argv); struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = handle_signal; sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); return 0; }