[PATCH] sed: @file reads pattern or replacement from a file. Useful for shader replacement.

José Fonseca jose.r.fonseca at gmail.com
Fri Jun 5 13:58:23 PDT 2015


I'm OK with the functionality, but I'd prefer `apitrace sed` to say similar
in look-n-feel to the true sed, so one can use it intuitvely.

One solution would be to read apitrace sed commands from a file (like

  $ cat my_shader_replacement.script
  s/"A
  multi-line
  shader"/"a different
  shader/

  $ apitrace sed -f my_shader_replacement.script foo.trace

The draw back would be that that would require escaping the shader text.

Another (maybe simpler solution) would be, instead of replace '/' with ',',
allow any character, as long it is consistent ("/foo/boo/", ";foo;boo;" ),
just like true sed allows.

Jose



On Fri, May 29, 2015 at 2:31 PM, Arthur Huillet <arthur.huillet at free.fr>
wrote:

> From: Arthur Huillet <ahuillet at nvidia.com>
>
> Replacing shaders isn't easily done on the commandline, so add "@file()" to
> tell apitrace sed to read pattern or replacement from a file.
> This replacement is implemented on String-type nodes, so apitrace sed now
> effectively works on string elements in addition to enum elements.
>
> Signed-off-by: Arthur Huillet <ahuillet at nvidia.com>
> ---
>  cli/cli_sed.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++-------
>  1 file changed, 44 insertions(+), 7 deletions(-)
>
> diff --git a/cli/cli_sed.cpp b/cli/cli_sed.cpp
> index ccb38b0..bb6ea8f 100644
> --- a/cli/cli_sed.cpp
> +++ b/cli/cli_sed.cpp
> @@ -46,8 +46,9 @@ usage(void)
>          << synopsis << "\n"
>          "\n"
>          "    -h, --help               Show detailed help for sed options
> and exit\n"
> -        "    -e s/SEARCH/REPLACE/     Search and replace a symbol.\n"
> -        "                             XXX: Only works for enums.\n"
> +        "    -e s,SEARCH,REPLACE,     Search and replace a symbol. Use
> @file(<path>)\n"
> +        "                             to read SEARCH or REPLACE from a
> file.\n"
> +        "                             XXX: Only works for enums and
> strings.\n"
>          "    -o, --output=TRACE_FILE  Output trace file\n"
>      ;
>  }
> @@ -104,6 +105,12 @@ public:
>      }
>
>      void visit(String *node) {
> +        if (!searchName.compare(node->value)) {
> +                delete [] node->value;
> +                char *str = new char [replaceName.length() + 1];
> +                strcpy(str, replaceName.c_str());
> +                node->value = str;
> +        }
>      }
>
>      void visit(WString *node) {
> @@ -210,7 +217,7 @@ sed_trace(Replacements &replacements, const char
> *inFileName, std::string &outFi
>
>
>  /**
> - * Parse a string in the format "s/SEARCH/REPLACE/".
> + * Parse a string in the format "s,SEARCH,REPLACE,".
>   */
>  static bool
>  parseSubstOpt(Replacements &replacements, const char *opt)
> @@ -219,13 +226,13 @@ parseSubstOpt(Replacements &replacements, const char
> *opt)
>          return false;
>      }
>
> -    if (*opt++ != '/') {
> +    if (*opt++ != ',') {
>          return false;
>      }
>
>      // Parse the search pattern
>      const char *search_begin = opt;
> -    while (*opt != '/') {
> +    while (*opt != ',') {
>          if (*opt == 0) {
>              return false;
>          }
> @@ -235,7 +242,7 @@ parseSubstOpt(Replacements &replacements, const char
> *opt)
>
>      // Parse the replace pattern
>      const char *replace_begin = opt;
> -    while (*opt != '/') {
> +    while (*opt != ',') {
>          if (*opt == 0) {
>              return false;
>          }
> @@ -250,6 +257,36 @@ parseSubstOpt(Replacements &replacements, const char
> *opt)
>      std::string search(search_begin, search_end);
>      std::string replace(replace_begin, replace_end);
>
> +    // If search or replace strings are taken from a file, read the file
> +    std::string file_subst = "@file(";
> +
> +    for (int i = 0; i < 2; i++) {
> +        std::string *str = i ? &search : &replace;
> +
> +        if (!str->compare(0, file_subst.length(), file_subst)) {
> +            if ((*str)[str->length()-1] != ')') {
> +                return false;
> +            }
> +
> +            std::string fname = str->substr(file_subst.length());
> +            fname[fname.length()-1] = 0;
> +            FILE *f = fopen(fname.c_str(), "r");
> +            if (!f) {
> +                std::cerr << "error: cannot open file " << fname << "\n";
> +                return false;
> +            }
> +            char buf[1024];
> +            (*str) = "";
> +            while (!feof(f)) {
> +                if (fgets(buf, 1024, f)) {
> +                    str->append(buf);
> +                }
> +            }
> +            fclose(f);
> +        }
> +    }
> +
> +
>      replacements.push_back(Replacer(search, replace));
>
>      return true;
> @@ -273,7 +310,7 @@ command(int argc, char *argv[])
>              break;
>          case 'e':
>              if (!parseSubstOpt(replacements, optarg)) {
> -                std::cerr << "error: invalid replacement patter `" <<
> optarg << "`\n";
> +                std::cerr << "error: invalid replacement pattern `" <<
> optarg << "`\n";
>              }
>              break;
>          default:
> --
> 2.3.5
> _______________________________________________
> apitrace mailing list
> apitrace at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/apitrace
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/apitrace/attachments/20150605/bfe6d8b3/attachment.html>


More information about the apitrace mailing list