#include #include #include inline int starts_with_seven_spaces(const char* line) { for (int i = 0;i<7;++i) { if (line[i] != ' ') return 0; } return 1; } inline int is_upper_case(char a, char b) { const auto a_ = 'A'; const auto b_ = 'Z'; return (a >= a_ && a_ <= b_ && b >= a_ && b <= b_); } void get(int indent_, char** argv) { const char* indent = "\t\t\t\t\t\t\t"; (void)indent_; (void)indent; (void)argv; char*buf = (char*)calloc(1024, 1); sprintf(buf, "man 2 %s 3 %s 2>/dev/null", argv[1], argv[1]); puts(buf); FILE* o = popen(buf, "r"); /*putchar(getc(o)); putchar(getc(o)); putchar(getc(o)); putchar(getc(o)); putchar(getc(o)); putchar(getc(o));*/ const size_t size_ = 4096; char* input=(char*)malloc(size_ + 1); if (!input) puts("Error"); size_t ret, size=size_; while ((size += ret = fread(input+size-size_,1, size_,o), ret ==size_)) { //printf("%d %d\n", size, ret); input = (char*)realloc(input,size + 1); if (!input) puts("ERR"); } input[size = size-size_] = '\0'; //} //printf("ret %d\n", ret); //printf("%d %d\n", size, ret); char* ptr1 = strstr(input, "ERRORS"); if (ptr1 == 0) ptr1 = input; ptr1 += 6; while (*ptr1 == '\n') ++ptr1; char* ptr2 = strstr(ptr1, "CONFORMING"); if (!ptr2) ptr2=input + strlen(input) - 1; //if (ptr2 == 0)ptr2 = --ptr2; char* ptr3 = ptr2; while (*ptr2 == ' ' || *ptr2 == '\n' || *ptr2 == '\t') --ptr2; // --ptr1; ++ptr2; *ptr2 = 0; ptr2 = ptr1; while ((ptr2=strstr(ptr2, " "))) { while (*ptr2 == ' ') ++ptr2; //*ptr *(int*)(&ptr2[-5]) = *(int*)("case"); while (*ptr2 != ' ' && *ptr2 != '\n') ptr2++; *ptr2 = ':'; //if (*ptr2 >= 'A' && /*--ptr2; --ptr2; --ptr2; --ptr2; --ptr2; *(int*)ptr2 = *(int*)"case"; */ //*ptr2 = '%'; //*ptr2 = '%'; //puts("Found"); //next:; } //puts ("switch (errno) {"); //puts(ptr1); //puts ("}"); FILE* a_ = popen("xclip -selection c", "w"); FILE* list[2] = {a_, stdout}; for (int i = 0;i<2; ++i) { //FILE* const file = list[i]; fputs ("switch (errno) {\n", list[i]); fwrite(ptr1, ptr3 - ptr1, 1, list[i]); fputs ("\n}\n",list[i]); //, a); } //fclose( fclose(a_); fclose(o); free(input); //free(buf); free(buf); //return ""; //exit(0); /* std::string txt; std::string cmd_plus_bracket = cmd + "("; for (const std::string& line : pystring::split("\n", input)) { if ( line.find(cmd_plus_bracket)!= std::string::npos) { auto cmd_ = pystring::split(line); txt += "do {"; // } txt += in_.c_str() + 2; //std::slice(in_, 2, in_.length() - 2, 1); //[2..]; txt += cmd_[0]; txt += ' '; txt += arg; txt += " = "; txt +=join(" ", cmd_.c_str()+1); // break break; } } auto pos_1_ = all_text.find("ERRORS"); auto pos_2_ = all_text.find("CONFORMING"); if (pos_1_ ==std::string::npos || !pos_2_ == std::string::npos) { return ""; } i = all_text.substr(pos_1_ + 6..pos_2_ - pos_1_ - 6 ); # i = i["ERRORS".size..-"CONFORMING".size - 1] while (i[0] == '\n' || i[0]==' ') # '\t' i = i.substr(1, i.end()); txt += "\n\n#{in_.substr(2, in_.end())}switch (errno)\n#{in_.substr(2,in_.end())}{\n\n/* from manpage" # item std::vector> items{}; for (const auto& line : i.split("\n")) { if (line.length() >= 9 && starts_with_seven_spaces(line) && is_upper_case(line[8], line[9])) { a = split(line.substr(line.begin() + 7, line.end() - 1)); items.emplace_back(std::vector{a[0]}); b = 1; while (a.length() > b && a[b] == "or") { items[-1].emplace_back(a[b + 1]); b += 2; } c = a.substr(b, a.length() - b); txt << in_ # if items.size < 1 # [-2].nil? # p = "" # else p = items[-2].join(" or ") rescue "" # end txt << "*" "/\n\t\twrite(2, \"#{p}\",#{p.size});\nbreak;\n\n\n#{items[-1].map do |i| "#{in_[2..]}case #{i}:" end.join("\n")} /*#{c.join(' ')} " next } txt += line } txt += in_ ; txt += "*" "/" "\n\n\t\t" "default:" "\n"; txt += in_; txt += "\n\n"; txt += in_.c_str() + 2; } } // while txt.includes? " " // txt = txt.gsub " ", " " //end return txt + "\n}while (0);\n\n"; */ } int main(int argc, char** argv) { (void)argc; //std::string //res ; (get(3, argv)); }